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.
18 #include <app_common.h>
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"
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__"
42 using namespace tizen_base;
43 namespace notification {
46 GroupItem::GroupItem(string id, shared_ptr<AbstractAction> action)
47 : AbstractItem(id), impl_(new Impl(this)) {
50 GroupItem::GroupItem(shared_ptr<AbstractAction> action)
51 : AbstractItem(), impl_(new Impl(this)) {
54 GroupItem::Impl::Impl(GroupItem* parent)
56 LOGI("GroupItem created");
58 int ret = app_get_name(&name);
59 if (ret != APP_ERROR_NONE) {
60 app_label_ = util::GetAppId();
62 app_label_ = string(name);
67 GroupItem::~GroupItem() {
70 int GroupItem::GetType() const {
71 return AbstractItem::Group;
74 Bundle GroupItem::Serialize() const {
76 b = AbstractItem::Serialize();
77 b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ?
78 GROUP_DIRECTION_VERTICAL : GROUP_DIRECTION_HORIZONTAL);
80 b.Add(GROUP_APP_LABEL_KEY, impl_->app_label_);
81 if (impl_->children_list_.size() == 0)
84 vector<std::string> arr;
85 for (auto& i : impl_->children_list_) {
86 Bundle serialized = i.get()->Serialize();
88 GROUP_CHILDREN_TYPE_KEY, to_string(static_cast<int>(i.get()->GetType())));
89 arr.push_back(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
91 b.Add(GROUP_CHILDREN_KEY, arr);
96 void GroupItem::Deserialize(Bundle b) {
97 AbstractItem::Deserialize(b);
99 (b.GetString(GROUP_DIRECTION_KEY) == GROUP_DIRECTION_VERTICAL);
100 impl_->app_label_ = b.GetString(GROUP_APP_LABEL_KEY);
102 vector<string> str_arr = b.GetStringArray(GROUP_CHILDREN_KEY);
103 if (str_arr.size() == 0)
106 for (string str : str_arr) {
107 Bundle serialized(str);
108 string type_str = serialized.GetString(GROUP_CHILDREN_TYPE_KEY);
109 AbstractItem::Type child_type =
110 static_cast<AbstractItem::Type>(strtol(type_str.c_str(), NULL, 10));
111 shared_ptr<AbstractItem> child =
112 FactoryManager::GetInst().CreateItem(child_type);
113 child.get()->Deserialize(serialized);
118 AbstractItem& GroupItem::FindByID(std::string id) {
122 for (auto& i : impl_->children_list_) {
123 auto& re = i.get()->FindByID(id);
124 if (re.GetId() == id)
127 return FactoryManager::GetInst().GetNullItem();
130 AbstractItem& GroupItem::FindByMainType(MainType type) {
131 if (GetMainType() == type)
134 for (auto& i : impl_->children_list_) {
135 auto& re = i.get()->FindByMainType(type);
136 if (re.GetType() != NullObject)
139 return FactoryManager::GetInst().GetNullItem();
142 bool GroupItem::IsItemTypeExist(int type) {
143 if (GetType() == type)
146 for (auto& i : impl_->children_list_) {
147 if (i.get()->IsItemTypeExist(type))
153 list<string> GroupItem::GetSharedPath() const {
156 auto r = AbstractItem::GetSharedPath();
157 for (auto& shared_path_r : r)
158 ret.push_back(move(shared_path_r));
160 for (auto& i : impl_->children_list_) {
161 auto c = i->GetSharedPath();
162 for (auto& shared_path_c : c)
163 ret.push_back(move(shared_path_c));
169 void GroupItem::SetSharedPath() {
170 for (auto& i : impl_->children_list_)
174 list<map<string, string>> GroupItem::GetPathMapList() const {
175 list<map<string, string>> path_map_list;
177 auto r = AbstractItem::GetPathMapList();
178 for (auto& path_map_r : r)
179 path_map_list.push_back(move(path_map_r));
181 for (auto& i : impl_->children_list_) {
182 auto c = i->GetPathMapList();
183 for (auto& path_map_c : c)
184 path_map_list.push_back(move(path_map_c));
187 return path_map_list;
190 void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
191 impl_->children_list_.emplace_back(child);
194 void GroupItem::RemoveChild(string itemId) {
195 for (auto& i : impl_->children_list_) {
196 if (i.get()->GetId() == itemId) {
197 impl_->children_list_.remove(i);
203 void GroupItem::RemoveChildren() {
204 for (auto& i : impl_->children_list_) {
205 impl_->children_list_.remove(i);
209 list<shared_ptr<AbstractItem>> GroupItem::GetChildren() {
210 return impl_->children_list_;
213 void GroupItem::SetDirection(bool vertical) {
214 impl_->is_vertical_ = vertical;
217 bool GroupItem::IsVertical() {
218 return impl_->is_vertical_;
221 string GroupItem::GetAppLabel() {
222 return impl_->app_label_;
225 void GroupItem::SetAppLabel(string label) {
226 impl_->app_label_ = label;
230 } // namespace notification