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