Add SetAppLabel function for group item
[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 namespace notification {
42 namespace item {
43
44 GroupItem::GroupItem(string id, shared_ptr<AbstractAction> action)
45     : AbstractItem(id), impl_(new Impl(this)) {
46 }
47
48 GroupItem::GroupItem(shared_ptr<AbstractAction> action)
49     : AbstractItem(), impl_(new Impl(this)) {
50 }
51
52 GroupItem::Impl::Impl(GroupItem* parent)
53     : parent_(parent) {
54   LOGI("GroupItem created");
55 }
56
57 GroupItem::~GroupItem() {
58 }
59
60 int GroupItem::GetType() const {
61   return AbstractItem::Group;
62 }
63
64 Bundle GroupItem::Serialize() const {
65   Bundle b;
66   b = AbstractItem::Serialize();
67   b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ?
68       GROUP_DIRECTION_VERTICAL : GROUP_DIRECTION_HORIZONTAL);
69   if (!impl_->app_label_.empty())
70     b.Add(GROUP_APP_LABEL_KEY, impl_->app_label_);
71
72   if (impl_->children_list_.size() == 0)
73     return b;
74
75   vector<std::string> arr;
76   for (auto& i : impl_->children_list_) {
77     Bundle serialized = i.get()->Serialize();
78     serialized.Add(
79         GROUP_CHILDREN_TYPE_KEY, to_string(static_cast<int>(i.get()->GetType())));
80     arr.push_back(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
81   }
82   b.Add(GROUP_CHILDREN_KEY, arr);
83
84   return b;
85 }
86
87 void GroupItem::Deserialize(Bundle b) {
88   AbstractItem::Deserialize(b);
89   impl_->is_vertical_ =
90       (b.GetString(GROUP_DIRECTION_KEY) == GROUP_DIRECTION_VERTICAL);
91   impl_->app_label_ = b.GetString(GROUP_APP_LABEL_KEY);
92
93   vector<string> str_arr = b.GetStringArray(GROUP_CHILDREN_KEY);
94   if (str_arr.size() == 0)
95     return;
96
97   for (string str : str_arr) {
98     Bundle serialized(str);
99     string type_str = serialized.GetString(GROUP_CHILDREN_TYPE_KEY);
100     AbstractItem::Type child_type =
101         static_cast<AbstractItem::Type>(strtol(type_str.c_str(), NULL, 10));
102     shared_ptr<AbstractItem> child =
103         FactoryManager::GetInst().CreateItem(child_type);
104     child.get()->Deserialize(serialized);
105     AddChild(child);
106   }
107 }
108
109 AbstractItem& GroupItem::FindByID(std::string id) {
110   if (GetId() == id)
111     return *this;
112
113   for (auto& i : impl_->children_list_) {
114     auto& re = i.get()->FindByID(id);
115     if (re.GetId() == id)
116       return re;
117   }
118   return FactoryManager::GetInst().GetNullItem();
119 }
120
121 list<string> GroupItem::GetSharedPath() const {
122   list<string> ret;
123
124   for (auto& i : impl_->children_list_) {
125     auto s = i->GetSharedPath();
126     for (auto& j : s) {
127       ret.push_back(move(j));
128     }
129   }
130
131   return ret;
132 }
133
134 void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
135   impl_->children_list_.emplace_back(child);
136 }
137
138 void GroupItem::RemoveChild(string itemId) {
139   for (auto& i : impl_->children_list_) {
140     if (i.get()->GetId() == itemId) {
141       impl_->children_list_.remove(i);
142       break;
143     }
144   }
145 }
146
147 list<shared_ptr<AbstractItem>> GroupItem::GetChildren() {
148   return impl_->children_list_;
149 }
150
151 void GroupItem::SetDirection(bool vertical) {
152   impl_->is_vertical_ = vertical;
153 }
154
155 bool GroupItem::IsVertical() {
156   return impl_->is_vertical_;
157 }
158
159 string GroupItem::GetAppLabel() {
160   if (impl_->app_label_.empty()) {
161     char* name;
162     int ret = app_get_name(&name);
163     if (ret != APP_ERROR_NONE)
164       THROW(ERROR_IO_ERROR);
165     impl_->app_label_ = string(name);
166   }
167   return impl_->app_label_;
168 }
169
170 void GroupItem::SetAppLabel(string label) {
171   impl_->app_label_ = label;
172 }
173
174 }  // namespace item
175 }  // namespace notification