2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "notification-ex/chat_message_item.h"
20 #include "notification-ex/chat_message_item_implementation.h"
21 #include "notification-ex/factory_manager.h"
22 #include "notification-ex/exception.h"
28 #define LOG_TAG "NOTIFICATION_EX"
29 #define CHATMESSAGE_NAME_KEY "__CHATMESSAGE_NAME_KEY__"
30 #define CHATMESSAGE_TEXT_KEY "__CHATMESSAGE_TEXT_KEY__"
31 #define CHATMESSAGE_IMAGE_KEY "__CHATMESSAGE_IMAGE_KEY__"
32 #define CHATMESSAGE_TIME_KEY "__CHATMESSAGE_TIME_KEY__"
33 #define CHATMESSAGE_TYPE_KEY "__CHATMESSAGE_TYPE_KEY__"
35 using namespace tizen_base;
37 namespace notification {
40 ChatMessageItem::ChatMessageItem(std::string id,
41 std::shared_ptr<TextItem> name, std::shared_ptr<TextItem> text,
42 std::shared_ptr<ImageItem> image, std::shared_ptr<TimeItem> time,
43 Type type, std::shared_ptr<AbstractAction> action)
44 : AbstractItem(id, action),
45 impl_(new Impl(this, name, text, image, time, type)) {
48 ChatMessageItem::Impl::Impl(ChatMessageItem* parent,
49 std::shared_ptr<TextItem> name, std::shared_ptr<TextItem> text,
50 std::shared_ptr<ImageItem> image, std::shared_ptr<TimeItem> time, Type type)
51 : parent_(parent), name_(name), text_(text), image_(image), time_(time),
53 LOGI("ChatMessageItem impl created");
56 int ChatMessageItem::GetType() const {
57 return AbstractItem::ChatMessage;
60 Bundle ChatMessageItem::Serialize() const {
62 b = AbstractItem::Serialize();
64 if (impl_->name_ != nullptr)
65 b.Add(CHATMESSAGE_NAME_KEY,
66 reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
68 if (impl_->text_ != nullptr)
69 b.Add(CHATMESSAGE_TEXT_KEY,
70 reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
72 if (impl_->image_ != nullptr)
73 b.Add(CHATMESSAGE_IMAGE_KEY,
74 reinterpret_cast<char*>(impl_->image_->Serialize().ToRaw().first.get()));
76 if (impl_->time_ != nullptr)
77 b.Add(CHATMESSAGE_TIME_KEY,
78 reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
80 b.Add(CHATMESSAGE_TYPE_KEY, std::to_string(static_cast<int>(impl_->type_)));
84 void ChatMessageItem::Deserialize(Bundle b) {
85 AbstractItem::Deserialize(b);
87 if (!b.GetString(CHATMESSAGE_NAME_KEY).empty()) {
88 std::shared_ptr<AbstractItem> name =
89 FactoryManager::GetInst().CreateItem(AbstractItem::Text);
90 name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
91 impl_->name_ = std::static_pointer_cast<TextItem>(name);
94 if (!b.GetString(CHATMESSAGE_TEXT_KEY).empty()) {
95 std::shared_ptr<AbstractItem> text =
96 FactoryManager::GetInst().CreateItem(AbstractItem::Text);
97 text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
98 impl_->text_ = std::static_pointer_cast<TextItem>(text);
101 if (!b.GetString(CHATMESSAGE_IMAGE_KEY).empty()) {
102 std::shared_ptr<AbstractItem> image =
103 FactoryManager::GetInst().CreateItem(AbstractItem::Image);
104 image.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_IMAGE_KEY)));
105 impl_->image_ = std::static_pointer_cast<ImageItem>(image);
108 if (!b.GetString(CHATMESSAGE_TIME_KEY).empty()) {
109 std::shared_ptr<AbstractItem> time =
110 FactoryManager::GetInst().CreateItem(AbstractItem::Time);
111 time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
112 impl_->time_ = std::static_pointer_cast<TimeItem>(time);
115 impl_->type_ = static_cast<Type>(std::stoi(b.GetString(CHATMESSAGE_TYPE_KEY)));
118 AbstractItem& ChatMessageItem::FindByID(std::string id) {
122 if (impl_->name_->GetId() == id)
123 return *(impl_->name_);
125 if (impl_->text_->GetId() == id)
126 return *(impl_->text_);
128 if (impl_->image_->GetId() == id)
129 return *(impl_->image_);
131 if (impl_->time_->GetId() == id)
132 return *(impl_->time_);
134 return FactoryManager::GetInst().GetNullItem();
137 AbstractItem& ChatMessageItem::FindByMainType(MainType type) {
138 if (GetMainType() == type)
141 if (impl_->name_->GetMainType() == type)
142 return *(impl_->name_);
144 if (impl_->text_->GetMainType() == type)
145 return *(impl_->text_);
147 if (impl_->image_->GetMainType() == type)
148 return *(impl_->image_);
150 if (impl_->time_->GetMainType() == type)
151 return *(impl_->time_);
153 return FactoryManager::GetInst().GetNullItem();
156 bool ChatMessageItem::IsItemTypeExist(int type) {
157 if (GetType() == type)
162 std::list<std::string> ChatMessageItem::GetSharedPath() const {
163 std::list<std::string> ret;
165 auto image = impl_->image_->GetSharedPath();
166 for (auto& i : image) {
167 ret.push_back(std::move(i));
173 TextItem& ChatMessageItem::GetNameItem() const {
174 if (impl_->name_ == nullptr)
175 return static_cast<TextItem&>(FactoryManager::GetInst().GetNullItem());
176 return *(impl_->name_);
179 TextItem& ChatMessageItem::GetTextItem() const {
180 if (impl_->text_ == nullptr)
181 return static_cast<TextItem&>(FactoryManager::GetInst().GetNullItem());
182 return *(impl_->text_);
185 ImageItem& ChatMessageItem::GetImageItem() const {
186 if (impl_->image_ == nullptr)
187 return static_cast<ImageItem&>(FactoryManager::GetInst().GetNullItem());
188 return *(impl_->image_);
191 TimeItem& ChatMessageItem::GetTimeItem() const {
192 if (impl_->time_ == nullptr)
193 return static_cast<TimeItem&>(FactoryManager::GetInst().GetNullItem());
194 return *(impl_->time_);
197 ChatMessageItem::Type ChatMessageItem::GetMessageType() const {
201 ChatMessageItem::~ChatMessageItem() = default;
202 ChatMessageItem::Impl::~Impl() = default;
205 } // namespace notification