Merge branch 'tizen' into tizen_5.5
[platform/core/api/notification.git] / notification-ex / group_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 #include <app_common.h>
19
20 #include <memory>
21 #include <vector>
22
23 #include "notification-ex/group_item.h"
24 #include "notification-ex/group_item_implementation.h"
25 #include "notification-ex/factory_manager.h"
26 #include "notification-ex/exception.h"
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31
32 #define LOG_TAG "NOTIFICATION_EX"
33 #define GROUP_CHILDREN_KEY "__GROUP_CHILDREN_KEY__"
34 #define GROUP_CHILDREN_TYPE_KEY "__GROUP_CHILDREN_TYPE_KEY__"
35 #define GROUP_DIRECTION_KEY "__GROUP_DIRECTION_KEY__"
36 #define GROUP_DIRECTION_VERTICAL "VERTICAL"
37 #define GROUP_DIRECTION_HORIZONTAL "HORIZONTAL"
38 #define GROUP_APP_LABEL_KEY "__GROUP_APP_LABEL_KEY__"
39
40 using namespace std;
41 using namespace tizen_base;
42 namespace notification {
43 namespace item {
44
45 GroupItem::GroupItem(string id, shared_ptr<AbstractAction> action)
46     : AbstractItem(id), impl_(new Impl(this)) {
47 }
48
49 GroupItem::GroupItem(shared_ptr<AbstractAction> action)
50     : AbstractItem(), impl_(new Impl(this)) {
51 }
52
53 GroupItem::Impl::Impl(GroupItem* parent)
54     : parent_(parent) {
55   LOGI("GroupItem created");
56 }
57
58 GroupItem::~GroupItem() {
59 }
60
61 int GroupItem::GetType() const {
62   return AbstractItem::Group;
63 }
64
65 Bundle GroupItem::Serialize() const {
66   Bundle b;
67   b = AbstractItem::Serialize();
68   b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ?
69       GROUP_DIRECTION_VERTICAL : GROUP_DIRECTION_HORIZONTAL);
70   if (!impl_->app_label_.empty())
71     b.Add(GROUP_APP_LABEL_KEY, impl_->app_label_);
72
73   if (impl_->children_list_.size() == 0)
74     return b;
75
76   vector<std::string> arr;
77   for (auto& i : impl_->children_list_) {
78     Bundle serialized = i.get()->Serialize();
79     serialized.Add(
80         GROUP_CHILDREN_TYPE_KEY, to_string(static_cast<int>(i.get()->GetType())));
81     arr.push_back(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
82   }
83   b.Add(GROUP_CHILDREN_KEY, arr);
84
85   return b;
86 }
87
88 void GroupItem::Deserialize(Bundle b) {
89   AbstractItem::Deserialize(b);
90   impl_->is_vertical_ =
91       (b.GetString(GROUP_DIRECTION_KEY) == GROUP_DIRECTION_VERTICAL);
92   impl_->app_label_ = b.GetString(GROUP_APP_LABEL_KEY);
93
94   vector<string> str_arr = b.GetStringArray(GROUP_CHILDREN_KEY);
95   if (str_arr.size() == 0)
96     return;
97
98   for (string str : str_arr) {
99     Bundle serialized(str);
100     string type_str = serialized.GetString(GROUP_CHILDREN_TYPE_KEY);
101     AbstractItem::Type child_type =
102         static_cast<AbstractItem::Type>(strtol(type_str.c_str(), NULL, 10));
103     shared_ptr<AbstractItem> child =
104         FactoryManager::GetInst().CreateItem(child_type);
105     child.get()->Deserialize(serialized);
106     AddChild(child);
107   }
108 }
109
110 AbstractItem& GroupItem::FindByID(std::string id) {
111   if (GetId() == id)
112     return *this;
113
114   for (auto& i : impl_->children_list_) {
115     auto& re = i.get()->FindByID(id);
116     if (re.GetId() == id)
117       return re;
118   }
119   return FactoryManager::GetInst().GetNullItem();
120 }
121
122 AbstractItem& GroupItem::FindByMainType(MainType type) {
123   if (GetMainType() == type)
124     return *this;
125
126   for (auto& i : impl_->children_list_) {
127     auto& re = i.get()->FindByMainType(type);
128     if (re.GetType() != NullObject)
129       return re;
130   }
131   return FactoryManager::GetInst().GetNullItem();
132 }
133
134 bool GroupItem::IsItemTypeExist(int type) {
135   if (GetType() == type)
136     return true;
137
138   for (auto& i : impl_->children_list_) {
139     if (i.get()->IsItemTypeExist(type))
140       return true;
141   }
142   return false;
143 }
144
145 list<string> GroupItem::GetSharedPath() const {
146   list<string> ret;
147
148   auto r = AbstractItem::GetSharedPath();
149   for (auto& shared_path_r : r)
150     ret.push_back(move(shared_path_r));
151
152   for (auto& i : impl_->children_list_) {
153     auto c = i->GetSharedPath();
154     for (auto& shared_path_c : c)
155       ret.push_back(move(shared_path_c));
156   }
157
158   return ret;
159 }
160
161 void GroupItem::SetSharedPath() {
162   for (auto& i : impl_->children_list_)
163     i->SetSharedPath();
164 }
165
166 list<map<string, string>> GroupItem::GetPathMapList() const {
167   list<map<string, string>> path_map_list;
168
169   auto r = AbstractItem::GetPathMapList();
170   for (auto& path_map_r : r)
171     path_map_list.push_back(move(path_map_r));
172
173   for (auto& i : impl_->children_list_) {
174     auto c = i->GetPathMapList();
175     for (auto& path_map_c : c)
176       path_map_list.push_back(move(path_map_c));
177   }
178
179   return path_map_list;
180 }
181
182 void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
183   impl_->children_list_.emplace_back(child);
184 }
185
186 void GroupItem::RemoveChild(string itemId) {
187   for (auto& i : impl_->children_list_) {
188     if (i.get()->GetId() == itemId) {
189       impl_->children_list_.remove(i);
190       break;
191     }
192   }
193 }
194
195 list<shared_ptr<AbstractItem>> GroupItem::GetChildren() {
196   return impl_->children_list_;
197 }
198
199 void GroupItem::SetDirection(bool vertical) {
200   impl_->is_vertical_ = vertical;
201 }
202
203 bool GroupItem::IsVertical() {
204   return impl_->is_vertical_;
205 }
206
207 string GroupItem::GetAppLabel() {
208   if (impl_->app_label_.empty()) {
209     char* name;
210     int ret = app_get_name(&name);
211     if (ret != APP_ERROR_NONE)
212       THROW(ERROR_IO_ERROR);
213     impl_->app_label_ = string(name);
214     free(name);
215   }
216   return impl_->app_label_;
217 }
218
219 void GroupItem::SetAppLabel(string label) {
220   impl_->app_label_ = label;
221 }
222
223 }  // namespace item
224 }  // namespace notification