Fix group name for noti_ex
[platform/core/api/notification.git] / notification-ex / chat_message_item.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dlog.h>
18
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"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
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__"
34
35 using namespace tizen_base;
36
37 namespace notification {
38 namespace item {
39
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)) {
46 }
47
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),
52     type_(type) {
53   LOGI("ChatMessageItem impl created");
54 }
55
56 int ChatMessageItem::GetType() const {
57   return AbstractItem::ChatMessage;
58 }
59
60 std::list<std::string> ChatMessageItem::GetSharedPath() const {
61   std::list<std::string> ret;
62
63   auto name = impl_->name_->GetSharedPath();
64   auto text = impl_->text_->GetSharedPath();
65   auto image = impl_->image_->GetSharedPath();
66   auto time = impl_->time_->GetSharedPath();
67
68   for (auto& i : name) {
69     ret.push_back(std::move(i));
70   }
71
72   for (auto& i : text) {
73     ret.push_back(std::move(i));
74   }
75
76   for (auto& i : image) {
77     ret.push_back(std::move(i));
78   }
79
80   for (auto& i : time) {
81     ret.push_back(std::move(i));
82   }
83
84   return ret;
85 }
86
87 Bundle ChatMessageItem::Serialize() const {
88   Bundle b;
89   b = AbstractItem::Serialize();
90
91   b.Add(CHATMESSAGE_NAME_KEY,
92     reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
93   b.Add(CHATMESSAGE_TEXT_KEY,
94     reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
95   b.Add(CHATMESSAGE_IMAGE_KEY,
96     reinterpret_cast<char*>(impl_->image_->Serialize().ToRaw().first.get()));
97   b.Add(CHATMESSAGE_TIME_KEY,
98     reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
99   b.Add(CHATMESSAGE_TYPE_KEY, std::to_string(static_cast<int>(impl_->type_)));
100   return b;
101 }
102
103 void ChatMessageItem::Deserialize(Bundle b) {
104   AbstractItem::Deserialize(b);
105
106   std::shared_ptr<AbstractItem> name =
107       FactoryManager::GetInst().CreateItem(AbstractItem::Text);
108   name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
109   impl_->name_ = std::static_pointer_cast<TextItem>(name);
110
111   std::shared_ptr<AbstractItem> text =
112       FactoryManager::GetInst().CreateItem(AbstractItem::Text);
113   text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
114   impl_->text_ = std::static_pointer_cast<TextItem>(text);
115
116   std::shared_ptr<AbstractItem> image =
117       FactoryManager::GetInst().CreateItem(AbstractItem::Image);
118   image.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_IMAGE_KEY)));
119   impl_->image_ = std::static_pointer_cast<ImageItem>(image);
120
121   std::shared_ptr<AbstractItem> time =
122       FactoryManager::GetInst().CreateItem(AbstractItem::Time);
123   time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
124   impl_->time_ = std::static_pointer_cast<TimeItem>(time);
125
126   impl_->type_ = static_cast<Type>(std::stoi(b.GetString(CHATMESSAGE_TYPE_KEY)));
127 }
128
129 AbstractItem& ChatMessageItem::FindByID(std::string id) {
130   if (GetId() == id)
131     return *this;
132
133   if (impl_->name_->GetId() == id)
134     return *(impl_->name_);
135
136   if (impl_->text_->GetId() == id)
137     return *(impl_->text_);
138
139   if (impl_->image_->GetId() == id)
140     return *(impl_->image_);
141
142   if (impl_->time_->GetId() == id)
143     return *(impl_->time_);
144
145   return FactoryManager::GetInst().GetNullItem();
146 }
147
148 AbstractItem& ChatMessageItem::FindByMainType(MainType type) {
149   if (GetMainType() == type)
150     return *this;
151
152   if (impl_->name_->GetMainType() == type)
153     return *(impl_->name_);
154
155   if (impl_->text_->GetMainType() == type)
156     return *(impl_->text_);
157
158   if (impl_->image_->GetMainType() == type)
159     return *(impl_->image_);
160
161   if (impl_->time_->GetMainType() == type)
162     return *(impl_->time_);
163
164   return FactoryManager::GetInst().GetNullItem();
165 }
166
167 bool ChatMessageItem::IsItemTypeExist(int type) {
168   if (GetType() == type)
169     return true;
170   return false;
171 }
172
173 TextItem& ChatMessageItem::GetNameItem() const {
174   return *(impl_->name_);
175 }
176
177 TextItem& ChatMessageItem::GetTextItem() const {
178   return *(impl_->text_);
179 }
180
181 ImageItem& ChatMessageItem::GetImageItem() const {
182   return *(impl_->image_);
183 }
184
185 TimeItem& ChatMessageItem::GetTimeItem() const {
186   return *(impl_->time_);
187 }
188
189 ChatMessageItem::Type ChatMessageItem::GetMessageType() const {
190   return impl_->type_;
191 }
192
193 ChatMessageItem::~ChatMessageItem() = default;
194 ChatMessageItem::Impl::~Impl() = default;
195
196 }  // namespace item
197 }  // namespace notification