Add internal api to support C#
[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 Bundle ChatMessageItem::Serialize() const {
61   Bundle b;
62   b = AbstractItem::Serialize();
63
64   b.Add(CHATMESSAGE_NAME_KEY,
65     reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
66   b.Add(CHATMESSAGE_TEXT_KEY,
67     reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
68   b.Add(CHATMESSAGE_IMAGE_KEY,
69     reinterpret_cast<char*>(impl_->image_->Serialize().ToRaw().first.get()));
70   b.Add(CHATMESSAGE_TIME_KEY,
71     reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
72   b.Add(CHATMESSAGE_TYPE_KEY, std::to_string(static_cast<int>(impl_->type_)));
73   return b;
74 }
75
76 void ChatMessageItem::Deserialize(Bundle b) {
77   AbstractItem::Deserialize(b);
78
79   std::shared_ptr<AbstractItem> name =
80       FactoryManager::GetInst().CreateItem(AbstractItem::Text);
81   name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
82   impl_->name_ = std::static_pointer_cast<TextItem>(name);
83
84   std::shared_ptr<AbstractItem> text =
85       FactoryManager::GetInst().CreateItem(AbstractItem::Text);
86   text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
87   impl_->text_ = std::static_pointer_cast<TextItem>(text);
88
89   std::shared_ptr<AbstractItem> image =
90       FactoryManager::GetInst().CreateItem(AbstractItem::Image);
91   image.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_IMAGE_KEY)));
92   impl_->image_ = std::static_pointer_cast<ImageItem>(image);
93
94   std::shared_ptr<AbstractItem> time =
95       FactoryManager::GetInst().CreateItem(AbstractItem::Time);
96   time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
97   impl_->time_ = std::static_pointer_cast<TimeItem>(time);
98
99   impl_->type_ = static_cast<Type>(std::stoi(b.GetString(CHATMESSAGE_TYPE_KEY)));
100 }
101
102 AbstractItem& ChatMessageItem::FindByID(std::string id) {
103   if (GetId() == id)
104     return *this;
105
106   if (impl_->name_->GetId() == id)
107     return *(impl_->name_);
108
109   if (impl_->text_->GetId() == id)
110     return *(impl_->text_);
111
112   if (impl_->image_->GetId() == id)
113     return *(impl_->image_);
114
115   if (impl_->time_->GetId() == id)
116     return *(impl_->time_);
117
118   return FactoryManager::GetInst().GetNullItem();
119 }
120
121 AbstractItem& ChatMessageItem::FindByMainType(MainType type) {
122   if (GetMainType() == type)
123     return *this;
124
125   if (impl_->name_->GetMainType() == type)
126     return *(impl_->name_);
127
128   if (impl_->text_->GetMainType() == type)
129     return *(impl_->text_);
130
131   if (impl_->image_->GetMainType() == type)
132     return *(impl_->image_);
133
134   if (impl_->time_->GetMainType() == type)
135     return *(impl_->time_);
136
137   return FactoryManager::GetInst().GetNullItem();
138 }
139
140 bool ChatMessageItem::IsItemTypeExist(int type) {
141   if (GetType() == type)
142     return true;
143   return false;
144 }
145
146 std::list<std::string> ChatMessageItem::GetSharedPath() const {
147   std::list<std::string> ret;
148
149   auto image = impl_->image_->GetSharedPath();
150   for (auto& i : image) {
151     ret.push_back(std::move(i));
152   }
153
154   return ret;
155 }
156
157 TextItem& ChatMessageItem::GetNameItem() const {
158   return *(impl_->name_);
159 }
160
161 TextItem& ChatMessageItem::GetTextItem() const {
162   return *(impl_->text_);
163 }
164
165 ImageItem& ChatMessageItem::GetImageItem() const {
166   return *(impl_->image_);
167 }
168
169 TimeItem& ChatMessageItem::GetTimeItem() const {
170   return *(impl_->time_);
171 }
172
173 ChatMessageItem::Type ChatMessageItem::GetMessageType() const {
174   return impl_->type_;
175 }
176
177 ChatMessageItem::~ChatMessageItem() = default;
178 ChatMessageItem::Impl::~Impl() = default;
179
180 }  // namespace item
181 }  // namespace notification