summaryrefslogtreecommitdiff
path: root/notification-ex/chat_message_item.cc
blob: ec592498c1e8a34e9a0b31dae2b0c9410d32d314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <dlog.h>

#include "notification-ex/chat_message_item.h"
#include "notification-ex/chat_message_item_implementation.h"
#include "notification-ex/factory_manager.h"
#include "notification-ex/exception.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "NOTIFICATION_EX"
#define CHATMESSAGE_NAME_KEY "__CHATMESSAGE_NAME_KEY__"
#define CHATMESSAGE_TEXT_KEY "__CHATMESSAGE_TEXT_KEY__"
#define CHATMESSAGE_DATA_KEY "__CHATMESSAGE_DATA_KEY__"
#define CHATMESSAGE_TIME_KEY "__CHATMESSAGE_TIME_KEY__"
#define CHATMESSAGE_TYPE_KEY "__CHATMESSAGE_TYPE_KEY__"

namespace notification {
namespace item {

ChatMessageItem::ChatMessageItem(std::string id, std::shared_ptr<TextItem> name,
  std::shared_ptr<TextItem> text, std::shared_ptr<TextItem> data,
  std::shared_ptr<TimeItem> time, Type type, std::shared_ptr<AbstractAction> action)
  : AbstractItem(id, action),
  impl_(new Impl(this, name, text, data, time, type)) {
}

ChatMessageItem::Impl::Impl(ChatMessageItem* parent,
  std::shared_ptr<TextItem> name, std::shared_ptr<TextItem> text,
  std::shared_ptr<TextItem> data, std::shared_ptr<TimeItem> time, Type type)
  : parent_(parent), name_(name), text_(text), data_(data), time_(time), type_(type) {
  LOGI("ChatMessageItem impl created");
}

int ChatMessageItem::GetType() const {
  return AbstractItem::ChatMessage;
}

Bundle ChatMessageItem::Serialize() const {
  Bundle b;
  b = AbstractItem::Serialize();

  b.Add(CHATMESSAGE_NAME_KEY,
    reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
  b.Add(CHATMESSAGE_TEXT_KEY,
    reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
  b.Add(CHATMESSAGE_DATA_KEY,
    reinterpret_cast<char*>(impl_->data_->Serialize().ToRaw().first.get()));
  b.Add(CHATMESSAGE_TIME_KEY,
    reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
  b.Add(CHATMESSAGE_TYPE_KEY, std::to_string((int)impl_->type_));
  return b;
}

void ChatMessageItem::Deserialize(Bundle b) {
  AbstractItem::Deserialize(b);

  std::shared_ptr<AbstractItem> name =
      FactoryManager::GetInst().CreateItem(AbstractItem::Text);
  name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
  impl_->name_ = std::static_pointer_cast<TextItem>(name);

  std::shared_ptr<AbstractItem> text =
      FactoryManager::GetInst().CreateItem(AbstractItem::Text);
  text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
  impl_->text_ = std::static_pointer_cast<TextItem>(text);

  std::shared_ptr<AbstractItem> data =
      FactoryManager::GetInst().CreateItem(AbstractItem::Text);
  data.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_DATA_KEY)));
  impl_->data_ = std::static_pointer_cast<TextItem>(data);

  std::shared_ptr<AbstractItem> time =
      FactoryManager::GetInst().CreateItem(AbstractItem::Time);
  time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
  impl_->time_ = std::static_pointer_cast<TimeItem>(time);

  impl_->type_ = static_cast<Type>(std::stoi(b.GetString(CHATMESSAGE_TYPE_KEY)));
}

AbstractItem&  ChatMessageItem::FindByID(std::string id) {
  if (GetId() == id)
    return *this;

  return FactoryManager::GetInst().GetNullItem();
}

TextItem& ChatMessageItem::GetNameItem() const {
  return *(impl_->name_);
}

TextItem& ChatMessageItem::GetTextItem() const {
  return *(impl_->text_);
}

TextItem& ChatMessageItem::GetDataItem() const {
  return *(impl_->data_);
}

TimeItem& ChatMessageItem::GetTimeItem() const {
  return *(impl_->time_);
}

ChatMessageItem::Type ChatMessageItem::GetMessageType() const {
  return impl_->type_;
}

ChatMessageItem::~ChatMessageItem() = default;
ChatMessageItem::Impl::~Impl() = default;

}  // namespace item
}  // namespace notification_ex