Fix group name for noti_ex
[platform/core/api/notification.git] / notification-ex / abstract_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 <uuid/uuid.h>
19 #include <unistd.h>
20
21 #include <memory>
22 #include <algorithm>
23 #include <vector>
24
25 #include "notification-ex/exception.h"
26 #include "notification-ex/abstract_item.h"
27 #include "notification-ex/abstract_item_implementation.h"
28 #include "notification-ex/item_info_internal.h"
29 #include "notification-ex/multi_language.h"
30 #include "notification-ex/ex_util.h"
31 #include "notification-ex/action_inflator.h"
32 #include "notification-ex/factory_manager.h"
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37
38 #define LOG_TAG "NOTIFICATION_EX"
39 #define ABSTRACT_ITEM_TYPE_KEY "__ABSTRACT_ITEM_TYPE_KEY__"
40 #define ABSTRACT_ITEM_ID_KEY "__ABSTRACT_ITEM_ID_KEY__"
41 #define ABSTRACT_ITEM_UID_KEY "__ABSTRACT_ITEM_UID_KEY__"
42 #define ABSTRACT_ITEM_TIME_KEY "__ABSTRACT_ITEM_TIME_KEY__"
43 #define ABSTRACT_ITEM_SENDER_APPID_KEY "__ABSTRACT_ITEM_SENDER_APPID_KEY__"
44 #define ABSTRACT_ITEM_TAG_KEY "__ABSTRACT_ITEM_TAG_KEY__"
45 #define ABSTRACT_ITEM_CHANNEL_KEY "__ABSTRACT_ITEM_CHANNEL_KEY__"
46 #define ABSTRACT_ITEM_VERSION_KEY "__ABSTRACT_ITEM_VERSION_KEY__"
47 #define ABSTRACT_ITEM_HIDE_TIME_KEY "__ABSTRACT_ITEM_HIDE_TIME_KEY__"
48 #define ABSTRACT_ITEM_DELETE_TIME_KEY "__ABSTRACT_ITEM_DELETE_TIME_KEY__"
49 #define ABSTRACT_ITEM_CAN_RECEIVE_KEY "__ABSTRACT_ITEM_CAN_RECEIVE_KEY__"
50 #define ABSTRACT_ITEM_SOUND_PATH_KEY "__ABSTRACT_ITEM_SOUND_PATH_KEY__"
51 #define ABSTRACT_ITEM_VIBRATION_PATH_KEY "__ABSTRACT_ITEM_VIBRATION_PATH_KEY__"
52 #define ABSTRACT_ITEM_POLICY_KEY "__ABSTRACT_ITEM_POLICY_KEY__"
53 #define ABSTRACT_ITEM_RECEIVER_GROUP_KEY "__ABSTRACT_ITEM_RECEIVER_GROUP_KEY__"
54 #define ABSTRACT_ITEM_VISIBLE_KEY "__ABSTRACT_ITEM_VISIBLE_KEY__"
55 #define ABSTRACT_ITEM_ENABLE_KEY "__ABSTRACT_ITEM_ENABLE_KEY__"
56 #define ABSTRACT_ITEM_STYLE_IS_EXIST_KEY "__ABSTRACT_ITEM_STYLE_IS_EXIST_KEY__"
57 #define ABSTRACT_ITEM_STYLE_PADDING_TOP_KEY "__ABSTRACT_ITEM_STYLE_PADDING_TOP_KEY__"
58 #define ABSTRACT_ITEM_STYLE_PADDING_LEFT_KEY "__ABSTRACT_ITEM_STYLE_PADDING_LEFT_KEY__"
59 #define ABSTRACT_ITEM_STYLE_PADDING_RIGHT_KEY "__ABSTRACT_ITEM_STYLE_PADDING_RIGHT_KEY__"
60 #define ABSTRACT_ITEM_STYLE_PADDING_BOTTOM_KEY "__ABSTRACT_ITEM_STYLE_PADDING_BOTTOM_KEY__"
61 #define ABSTRACT_ITEM_STYLE_COLOR_A_KEY "__ABSTRACT_ITEM_STYLE_COLOR_A_KEY__"
62 #define ABSTRACT_ITEM_STYLE_COLOR_R_KEY "__ABSTRACT_ITEM_STYLE_COLOR_R_KEY__"
63 #define ABSTRACT_ITEM_STYLE_COLOR_G_KEY "__ABSTRACT_ITEM_STYLE_COLOR_G_KEY__"
64 #define ABSTRACT_ITEM_STYLE_COLOR_B_KEY "__ABSTRACT_ITEM_STYLE_COLOR_B_KEY__"
65 #define ABSTRACT_ITEM_STYLE_BG_COLOR_A_KEY "__ABSTRACT_ITEM_STYLE_BG_COLOR_A_KEY__"
66 #define ABSTRACT_ITEM_STYLE_BG_COLOR_R_KEY "__ABSTRACT_ITEM_STYLE_BG_COLOR_R_KEY__"
67 #define ABSTRACT_ITEM_STYLE_BG_COLOR_G_KEY "__ABSTRACT_ITEM_STYLE_BG_COLOR_G_KEY__"
68 #define ABSTRACT_ITEM_STYLE_BG_COLOR_B_KEY "__ABSTRACT_ITEM_STYLE_BG_COLOR_B_KEY__"
69 #define ABSTRACT_ITEM_STYLE_BG_IMAGE_PATH_KEY "__ABSTRACT_ITEM_STYLE_BG_IMAGE_PATH_KEY__"
70 #define ABSTRACT_ITEM_STYLE_GEOMETRY_X_KEY "__ABSTRACT_ITEM_STYLE_GEOMETRY_X_KEY__"
71 #define ABSTRACT_ITEM_STYLE_GEOMETRY_Y_KEY "__ABSTRACT_ITEM_STYLE_GEOMETRY_Y_KEY__"
72 #define ABSTRACT_ITEM_STYLE_GEOMETRY_WIDTH_KEY "__ABSTRACT_ITEM_STYLE_GEOMETRY_WIDTH_KEY__"
73 #define ABSTRACT_ITEM_STYLE_GEOMETRY_HEIGHT_KEY "__ABSTRACT_ITEM_STYLE_GEOMETRY_HEIGHT_KEY__"
74 #define ABSTRACT_ITEM_LED_ON_PERIOD_KEY "__ABSTRACT_ITEM_LED_ON_PERIOD_KEY__"
75 #define ABSTRACT_ITEM_LED_OFF_PERIOD_KEY "__ABSTRACT_ITEM_LED_OFF_PERIOD_KEY__"
76 #define ABSTRACT_ITEM_LED_COLOR_A_KEY "__ABSTRACT_ITEM_LED_COLOR_A_KEY__"
77 #define ABSTRACT_ITEM_LED_COLOR_R_KEY "__ABSTRACT_ITEM_LED_COLOR_R_KEY__"
78 #define ABSTRACT_ITEM_LED_COLOR_G_KEY "__ABSTRACT_ITEM_LED_COLOR_G_KEY__"
79 #define ABSTRACT_ITEM_LED_COLOR_B_KEY "__ABSTRACT_ITEM_LED_COLOR_B_KEY__"
80 #define ABSTRACT_ITEM_LED_IS_EXIST_KEY "__ABSTRACT_ITEM_LED_IS_EXIST_KEY__"
81 #define ABSTRACT_ITEM_ACTION_KEY "__ABSTRACT_ITEM_ACTION_KEY__"
82 #define ABSTRACT_ITEM_HIDE_VIEWER_KEY "__ABSTRACT_ITEM_HIDE_VIEWER_KEY__"
83 #define ABSTRACT_ITEM_TRUE "TRUE"
84 #define ABSTRACT_ITEM_ONGOING_KEY "__ABSTRACT_ITEM_ONGOING_KEY__"
85 #define ABSTRACT_ITEM_MAIN_TYPE_KEY "__ABSTRACT_ITEM_MAIN_TYPE_KEY__"
86 #define ABSTRACT_ITEM_MULTI_LANG_ARR_KEY "__ABSTRACT_ITEM_MULTI_LANG_ARR_KEY__"
87 #define ABSTRACT_ITEM_MULTI_LANG_KEY "__ABSTRACT_ITEM_MULTI_LANG_KEY__"
88
89 using namespace std;
90 using namespace tizen_base;
91 namespace notification {
92 namespace item {
93
94 const string ReceiverGroup::Panel = "tizen.org/receiver/panel";
95 const string ReceiverGroup::Ticker = "tizen.org/receiver/ticker";
96 const string ReceiverGroup::LockScreen = "tizen.org/receiver/lockscreen";
97 const string ReceiverGroup::Indicator = "tizen.org/receiver/indicator";
98 const string ReceiverGroup::Popup = "tizen.org/receiver/popup";
99
100 string AbstractItem::Impl::GenerateItemId() {
101   char uuid[37];
102   uuid_t u;
103   uuid_generate(u);
104   uuid_unparse(u, uuid);
105   string id = util::GetAppId();
106   return string(uuid) + ":" + id;
107 }
108
109 AbstractItem::AbstractItem(std::shared_ptr<AbstractAction> action)
110     : impl_(new Impl(this, action)) {
111 }
112
113 AbstractItem::AbstractItem(std::string id,
114     std::shared_ptr<AbstractAction> action)
115     : impl_(new Impl(this, id, action)) {
116 }
117
118 AbstractItem::Impl::Impl(AbstractItem* parent, string id,
119     std::shared_ptr<AbstractAction> action)
120     : id_(id), action_(action), parent_(parent),
121     info_(std::make_shared<ItemInfo>(this)) {
122   if (id_.empty())
123     id_ = GenerateItemId();
124   sender_appid_ = util::GetAppId();
125   LOGI("AbstractItem created (%s)", id_.c_str());
126 }
127
128 AbstractItem::Impl::Impl(AbstractItem* parent,
129     std::shared_ptr<AbstractAction> action)
130     : action_(action), parent_(parent),
131     info_(std::make_shared<ItemInfo>(this)) {
132   LOGI("AbstractItem created");
133
134   id_ = GenerateItemId();
135   sender_appid_ = util::GetAppId();
136   LOGI("AbstractItem created (%s)", id_.c_str());
137 }
138
139 AbstractItem::~AbstractItem() = default;
140 AbstractItem::Impl::~Impl() = default;
141
142 Bundle AbstractItem::Serialize() const {
143   Bundle b;
144   struct tm timeinfo;
145   char buf[80] = {0, };
146
147   if (impl_->uid_ == 0)
148     impl_->uid_ = getuid();
149
150   if (!impl_->channel_.empty())
151     b.Add(ABSTRACT_ITEM_CHANNEL_KEY, impl_->channel_);
152
153   b.Add(ABSTRACT_ITEM_ID_KEY, impl_->id_);
154   b.Add(ABSTRACT_ITEM_SENDER_APPID_KEY, GetSenderAppId().c_str());
155   b.Add(ABSTRACT_ITEM_TYPE_KEY, to_string(GetType()));
156   b.Add(ABSTRACT_ITEM_VERSION_KEY, to_string(impl_->version_));
157   b.Add(ABSTRACT_ITEM_HIDE_TIME_KEY, to_string(impl_->hide_time_));
158   b.Add(ABSTRACT_ITEM_DELETE_TIME_KEY, to_string(impl_->delete_time_));
159   b.Add(ABSTRACT_ITEM_UID_KEY, to_string(impl_->uid_));
160   b.Add(ABSTRACT_ITEM_TAG_KEY, impl_->tag_);
161
162   localtime_r(&impl_->time_, &timeinfo);
163   strftime (buf, sizeof(buf), "%s", &timeinfo);
164   b.Add(ABSTRACT_ITEM_TIME_KEY, string(buf));
165
166   if (!impl_->can_receive_.empty())
167     b.Add(ABSTRACT_ITEM_CAN_RECEIVE_KEY, impl_->can_receive_);
168
169   if (!impl_->sound_path_.empty())
170     b.Add(ABSTRACT_ITEM_SOUND_PATH_KEY, impl_->sound_path_);
171
172   if (!impl_->vibration_path_.empty())
173     b.Add(ABSTRACT_ITEM_VIBRATION_PATH_KEY, impl_->vibration_path_);
174
175   b.Add(ABSTRACT_ITEM_VISIBLE_KEY, to_string((int)impl_->visible_));
176   b.Add(ABSTRACT_ITEM_ENABLE_KEY, to_string((int)impl_->enable_));
177   b.Add(ABSTRACT_ITEM_POLICY_KEY, to_string((int)impl_->policy_));
178   b.Add(ABSTRACT_ITEM_ONGOING_KEY, to_string((int)impl_->ongoing_));
179   b.Add(ABSTRACT_ITEM_MAIN_TYPE_KEY, to_string((int)impl_->main_type_));
180
181   if (impl_->receiver_group_list_.size() != 0) {
182     vector<string> arr;
183
184     for (auto& i : impl_->receiver_group_list_) {
185       string receiver = i;
186
187       arr.push_back(receiver);
188     }
189     b.Add(ABSTRACT_ITEM_RECEIVER_GROUP_KEY, arr);
190   }
191
192   if (impl_->style_ != nullptr) {
193     shared_ptr<Padding> padding = impl_->style_->GetPadding();
194     if (padding.get() != nullptr) {
195       b.Add(ABSTRACT_ITEM_STYLE_PADDING_LEFT_KEY, to_string(padding->GetLeft()));
196       b.Add(ABSTRACT_ITEM_STYLE_PADDING_TOP_KEY, to_string(padding->GetTop()));
197       b.Add(ABSTRACT_ITEM_STYLE_PADDING_RIGHT_KEY,
198           to_string(padding->GetRight()));
199       b.Add(ABSTRACT_ITEM_STYLE_PADDING_BOTTOM_KEY,
200           to_string(padding->GetBottom()));
201     }
202
203     shared_ptr<Color> color = impl_->style_->GetColor();
204     if (color.get() != nullptr) {
205       b.Add(ABSTRACT_ITEM_STYLE_COLOR_A_KEY,
206           to_string(static_cast<int>(color->GetAVal())));
207       b.Add(ABSTRACT_ITEM_STYLE_COLOR_R_KEY,
208           to_string(static_cast<int>(color->GetRVal())));
209       b.Add(ABSTRACT_ITEM_STYLE_COLOR_G_KEY,
210           to_string(static_cast<int>(color->GetGVal())));
211       b.Add(ABSTRACT_ITEM_STYLE_COLOR_B_KEY,
212           to_string(static_cast<int>(color->GetBVal())));
213     }
214
215     shared_ptr<Geometry> geometry = impl_->style_->GetGeometry();
216     if (geometry.get() != nullptr) {
217       b.Add(ABSTRACT_ITEM_STYLE_GEOMETRY_X_KEY, to_string(geometry->GetX()));
218       b.Add(ABSTRACT_ITEM_STYLE_GEOMETRY_Y_KEY, to_string(geometry->GetY()));
219       b.Add(ABSTRACT_ITEM_STYLE_GEOMETRY_WIDTH_KEY,
220           to_string(geometry->GetWidth()));
221       b.Add(ABSTRACT_ITEM_STYLE_GEOMETRY_HEIGHT_KEY,
222           to_string(geometry->GetHeight()));
223     }
224
225     shared_ptr<Color> bg_color = impl_->style_->GetBackgroundColor();
226     if (bg_color.get() != nullptr) {
227       b.Add(ABSTRACT_ITEM_STYLE_BG_COLOR_A_KEY,
228           to_string(static_cast<int>(bg_color->GetAVal())));
229       b.Add(ABSTRACT_ITEM_STYLE_BG_COLOR_R_KEY,
230           to_string(static_cast<int>(bg_color->GetRVal())));
231       b.Add(ABSTRACT_ITEM_STYLE_BG_COLOR_G_KEY,
232           to_string(static_cast<int>(bg_color->GetGVal())));
233       b.Add(ABSTRACT_ITEM_STYLE_BG_COLOR_B_KEY,
234           to_string(static_cast<int>(bg_color->GetBVal())));
235     }
236     b.Add(ABSTRACT_ITEM_STYLE_BG_IMAGE_PATH_KEY,
237         impl_->style_->GetBackgroundImage());
238     b.Add(ABSTRACT_ITEM_STYLE_IS_EXIST_KEY, ABSTRACT_ITEM_TRUE);
239   }
240
241   if (impl_->led_ != nullptr) {
242     b.Add(ABSTRACT_ITEM_LED_ON_PERIOD_KEY, to_string(impl_->led_->GetOnPeriod()));
243     b.Add(ABSTRACT_ITEM_LED_OFF_PERIOD_KEY, to_string(impl_->led_->GetOffPeriod()));
244     b.Add(ABSTRACT_ITEM_LED_COLOR_A_KEY,
245         to_string(static_cast<int>(impl_->led_->GetColor()->GetAVal())));
246     b.Add(ABSTRACT_ITEM_LED_COLOR_R_KEY,
247         to_string(static_cast<int>(impl_->led_->GetColor()->GetRVal())));
248     b.Add(ABSTRACT_ITEM_LED_COLOR_G_KEY,
249         to_string(static_cast<int>(impl_->led_->GetColor()->GetGVal())));
250     b.Add(ABSTRACT_ITEM_LED_COLOR_B_KEY,
251         to_string(static_cast<int>(impl_->led_->GetColor()->GetBVal())));
252     b.Add(ABSTRACT_ITEM_LED_IS_EXIST_KEY, ABSTRACT_ITEM_TRUE);
253   }
254
255   if (impl_->action_ != nullptr) {
256     b.Add(ABSTRACT_ITEM_ACTION_KEY,
257       reinterpret_cast<char*>(impl_->action_->Serialize().ToRaw().first.get()));
258   }
259
260   if (impl_->multi_lang_ != nullptr) {
261     b.Add(ABSTRACT_ITEM_MULTI_LANG_KEY,
262       reinterpret_cast<char*>(impl_->multi_lang_->Serialize().ToRaw().first.get()));
263   }
264
265   if (impl_->multi_lang_arr_.size() > 0) {
266     vector<string> arr;
267     for (auto& i : impl_->multi_lang_arr_) {
268       arr.push_back(reinterpret_cast<char*>(i->Serialize().ToRaw().first.get()));
269     }
270     b.Add(ABSTRACT_ITEM_MULTI_LANG_ARR_KEY, arr);
271   }
272
273   if (impl_->hide_viewer_list_.size() != 0) {
274     vector<string> arr;
275
276     for (auto& i : impl_->hide_viewer_list_) {
277       string viewer = i;
278
279       arr.push_back(viewer);
280     }
281     b.Add(ABSTRACT_ITEM_HIDE_VIEWER_KEY, arr);
282   }
283
284   return b;
285 }
286
287 void AbstractItem::Deserialize(Bundle b) {
288   string time_s;
289   struct tm timeinfo;
290
291   string type_str = b.GetString(ABSTRACT_ITEM_TYPE_KEY);
292   if (type_str.empty())
293     THROW(ERROR_IO_ERROR);
294
295   impl_->id_ = b.GetString(ABSTRACT_ITEM_ID_KEY);
296   impl_->sender_appid_ = b.GetString(ABSTRACT_ITEM_SENDER_APPID_KEY);
297   impl_->channel_ = b.GetString(ABSTRACT_ITEM_CHANNEL_KEY);
298   impl_->version_ = stoi(b.GetString(ABSTRACT_ITEM_VERSION_KEY));
299   impl_->hide_time_ = stoi(b.GetString(ABSTRACT_ITEM_HIDE_TIME_KEY));
300   impl_->delete_time_ = stoi(b.GetString(ABSTRACT_ITEM_DELETE_TIME_KEY));
301   impl_->can_receive_ = b.GetString(ABSTRACT_ITEM_CAN_RECEIVE_KEY);
302   impl_->sound_path_ = b.GetString(ABSTRACT_ITEM_SOUND_PATH_KEY);
303   impl_->vibration_path_ = b.GetString(ABSTRACT_ITEM_VIBRATION_PATH_KEY);
304   impl_->uid_ = stoi(b.GetString(ABSTRACT_ITEM_UID_KEY));
305   impl_->tag_ = b.GetString(ABSTRACT_ITEM_TAG_KEY);
306
307   time_s = b.GetString(ABSTRACT_ITEM_TIME_KEY);
308   strptime(time_s.c_str(), "%s", &timeinfo);
309   impl_->time_ = mktime(&timeinfo);
310
311   string policy_str = b.GetString(ABSTRACT_ITEM_POLICY_KEY);
312
313   impl_->policy_ = static_cast<Policy>(stoi(policy_str));
314   impl_->visible_ = static_cast<bool>(stoi(b.GetString(ABSTRACT_ITEM_VISIBLE_KEY)));
315   impl_->enable_ = static_cast<bool>(stoi(b.GetString(ABSTRACT_ITEM_ENABLE_KEY)));
316   impl_->ongoing_ = static_cast<bool>(stoi(b.GetString(ABSTRACT_ITEM_ONGOING_KEY)));
317   impl_->main_type_ = static_cast<MainType>(stoi(b.GetString(ABSTRACT_ITEM_MAIN_TYPE_KEY)));
318
319   vector<string> receiver_group = b.GetStringArray(ABSTRACT_ITEM_RECEIVER_GROUP_KEY);
320   if (receiver_group.size() != 0) {
321     for (string str : receiver_group) {
322       impl_->receiver_group_list_.push_back(str);
323     }
324   }
325
326   if (!b.GetString(ABSTRACT_ITEM_STYLE_IS_EXIST_KEY).compare(ABSTRACT_ITEM_TRUE)) {
327     shared_ptr<Padding> padding;
328     if (!b.GetString(ABSTRACT_ITEM_STYLE_PADDING_LEFT_KEY).empty()) {
329       padding = make_shared<Padding>(
330         stoi(b.GetString(ABSTRACT_ITEM_STYLE_PADDING_LEFT_KEY)),
331         stoi(b.GetString(ABSTRACT_ITEM_STYLE_PADDING_TOP_KEY)),
332         stoi(b.GetString(ABSTRACT_ITEM_STYLE_PADDING_RIGHT_KEY)),
333         stoi(b.GetString(ABSTRACT_ITEM_STYLE_PADDING_BOTTOM_KEY)));
334     }
335
336     shared_ptr<Color> color;
337     if (!b.GetString(ABSTRACT_ITEM_STYLE_COLOR_A_KEY).empty()) {
338       color = make_shared<Color>(
339         static_cast<unsigned char>(
340             stoi(b.GetString(ABSTRACT_ITEM_STYLE_COLOR_A_KEY))),
341         static_cast<unsigned char>(
342             stoi(b.GetString(ABSTRACT_ITEM_STYLE_COLOR_R_KEY))),
343         static_cast<unsigned char>(
344             stoi(b.GetString(ABSTRACT_ITEM_STYLE_COLOR_G_KEY))),
345         static_cast<unsigned char>(
346             stoi(b.GetString(ABSTRACT_ITEM_STYLE_COLOR_B_KEY))));
347     }
348
349     shared_ptr<Geometry> geometry;
350     if (!b.GetString(ABSTRACT_ITEM_STYLE_GEOMETRY_X_KEY).empty()) {
351       geometry = make_shared<Geometry>(
352         stoi(b.GetString(ABSTRACT_ITEM_STYLE_GEOMETRY_X_KEY)),
353         stoi(b.GetString(ABSTRACT_ITEM_STYLE_GEOMETRY_Y_KEY)),
354         stoi(b.GetString(ABSTRACT_ITEM_STYLE_GEOMETRY_WIDTH_KEY)),
355         stoi(b.GetString(ABSTRACT_ITEM_STYLE_GEOMETRY_HEIGHT_KEY)));
356     }
357
358     shared_ptr<Color> bg_color;
359     if (!b.GetString(ABSTRACT_ITEM_STYLE_BG_COLOR_A_KEY).empty()) {
360       bg_color = make_shared<Color>(
361         static_cast<unsigned char>(
362             stoi(b.GetString(ABSTRACT_ITEM_STYLE_BG_COLOR_A_KEY))),
363         static_cast<unsigned char>(
364             stoi(b.GetString(ABSTRACT_ITEM_STYLE_BG_COLOR_R_KEY))),
365         static_cast<unsigned char>(
366             stoi(b.GetString(ABSTRACT_ITEM_STYLE_BG_COLOR_G_KEY))),
367         static_cast<unsigned char>(
368             stoi(b.GetString(ABSTRACT_ITEM_STYLE_BG_COLOR_B_KEY))));
369     }
370
371     impl_->style_ = make_shared<Style>(color, padding, geometry, bg_color,
372         b.GetString(ABSTRACT_ITEM_STYLE_BG_IMAGE_PATH_KEY));
373   }
374
375   if (!b.GetString(ABSTRACT_ITEM_LED_IS_EXIST_KEY).compare(ABSTRACT_ITEM_TRUE)) {
376     shared_ptr<Color> led_color = make_shared<Color>(
377       static_cast<unsigned char>(
378         stoi(b.GetString(ABSTRACT_ITEM_LED_COLOR_A_KEY))),
379       static_cast<unsigned char>(
380         stoi(b.GetString(ABSTRACT_ITEM_LED_COLOR_R_KEY))),
381       static_cast<unsigned char>(
382         stoi(b.GetString(ABSTRACT_ITEM_LED_COLOR_G_KEY))),
383       static_cast<unsigned char>(
384         stoi(b.GetString(ABSTRACT_ITEM_LED_COLOR_B_KEY))));
385
386     impl_->led_ = make_shared<LEDInfo>(led_color);
387     impl_->led_->SetOnPeriod(stoi(b.GetString(ABSTRACT_ITEM_LED_ON_PERIOD_KEY)));
388     impl_->led_->SetOffPeriod(stoi(b.GetString(ABSTRACT_ITEM_LED_OFF_PERIOD_KEY)));
389   }
390
391   string action_str = b.GetString(ABSTRACT_ITEM_ACTION_KEY);
392   if (!action_str.empty())
393     impl_->action_ = ActionInflator::Create(Bundle(action_str));
394
395   string multi_str = b.GetString(ABSTRACT_ITEM_MULTI_LANG_KEY);
396   if (!multi_str.empty())
397     impl_->multi_lang_ = shared_ptr<MultiLanguage>(new MultiLanguage(multi_str));
398
399   vector<string> multi_arr_str = b.GetStringArray(ABSTRACT_ITEM_MULTI_LANG_ARR_KEY);
400   if (!multi_arr_str.empty()) {
401     for (string str : multi_arr_str) {
402       impl_->multi_lang_arr_.push_back(
403           shared_ptr<MultiLanguage>(new MultiLanguage(str)));
404     }
405   }
406
407   vector<string> hide_viewer = b.GetStringArray(ABSTRACT_ITEM_HIDE_VIEWER_KEY);
408   if (hide_viewer.size() != 0) {
409     for (string str : hide_viewer) {
410       impl_->hide_viewer_list_.push_back(str);
411     }
412   }
413 }
414
415 void AbstractItem::SetId(std::string id) {
416   impl_->id_ = id;
417 }
418
419 string AbstractItem::GetId() const {
420   return impl_->id_;
421 }
422
423 std::list<std::string> AbstractItem::GetSharedPath() const {
424   return {};
425 }
426
427 shared_ptr<AbstractAction> AbstractItem::GetAction() const {
428   return impl_->action_;
429 }
430
431 void AbstractItem::SetAction(std::shared_ptr<AbstractAction> action) {
432   impl_->action_ = action;
433 }
434
435 shared_ptr<Style> AbstractItem::GetStyle() const {
436   return impl_->style_;
437 }
438
439 void AbstractItem::SetStyle(shared_ptr<Style> style) {
440   impl_->style_ = style;
441 }
442
443 void AbstractItem::SetLEDInfo(std::shared_ptr<LEDInfo> led) {
444   impl_->led_ = led;
445 }
446
447 std::shared_ptr<LEDInfo> AbstractItem::GetLEDInfo() const {
448   return impl_->led_;
449 }
450
451 int AbstractItem::GetType(Bundle b) {
452   string type_str = b.GetString(ABSTRACT_ITEM_TYPE_KEY);
453   if (type_str.empty())
454     THROW(ERROR_IO_ERROR);
455   return strtol(type_str.c_str(), NULL, 10);
456 }
457
458 void AbstractItem::SetVisible(bool visible) {
459   impl_->visible_ = visible;
460 }
461
462 bool AbstractItem::GetVisible() const {
463   return impl_->visible_;
464 }
465
466 void AbstractItem::SetEnable(bool enable) {
467   impl_->enable_ = enable;
468 }
469
470 bool AbstractItem::GetEnable() const {
471   return impl_->enable_;
472 }
473
474 void AbstractItem::AddReceiver(std::string receiver_group) {
475   impl_->receiver_group_list_.push_back(receiver_group);
476 }
477
478 void AbstractItem::RemoveReceiver(std::string receiver_group) {
479   impl_->receiver_group_list_.remove(receiver_group);
480 }
481
482 list<string> AbstractItem::GetReceiverList() {
483   return impl_->receiver_group_list_;
484 }
485
486 void AbstractItem::SetPolicy(int policy) {
487   impl_->policy_ = policy;
488 }
489
490 int AbstractItem::GetPolicy() const {
491   return impl_->policy_;
492 }
493
494 std::shared_ptr<IItemInfo> AbstractItem::GetInfo() const {
495   return impl_->info_;
496 }
497
498 string AbstractItem::GetChannel() const {
499   return impl_->channel_;
500 }
501
502 void AbstractItem::SetChannel(string channel) {
503   impl_->channel_ = channel;
504 }
505
506 void AbstractItem::SetSoundPath(std::string path) {
507   impl_->sound_path_ = path;
508 }
509
510 void AbstractItem::SetVibrationPath(std::string path) {
511   impl_->vibration_path_ = path;
512 }
513
514 std::string AbstractItem::GetSoundPath() const {
515   return impl_->sound_path_;
516 }
517
518 std::string AbstractItem::GetVibrationPath() const {
519   return impl_->vibration_path_;
520 }
521
522 std::string AbstractItem::GetSenderAppId() const {
523   return impl_->sender_appid_;
524 }
525
526 void AbstractItem::SetSenderAppId(std::string sender_appid) {
527   impl_->sender_appid_ = sender_appid;
528 }
529
530 std::string AbstractItem::GetTag() const {
531   return impl_->tag_;
532 }
533
534 void AbstractItem::SetTag(std::string tag) {
535   impl_->tag_ = tag;
536 }
537
538 void AbstractItem::SetOnGoingState(bool ongoing) {
539   impl_->ongoing_ = ongoing;
540 }
541
542 bool AbstractItem::GetOnGoingState() const {
543   return impl_->ongoing_;
544 }
545
546 bool AbstractItem::Impl::IsValidMainType(AbstractItem& target, MainType type) {
547   int item_type = target.GetType();
548
549   switch (type) {
550     case MainTitle:
551     case MainContents:
552       if (item_type != Text)
553         return false;
554       break;
555     case MainIcon:
556       if (item_type != Image)
557         return false;
558       break;
559     case MainButton:
560       if (item_type != Button)
561         return false;
562       break;
563     default:
564       return false;
565   }
566
567   return true;
568 }
569
570 bool AbstractItem::SetMainType(std::string target_id, MainType type) {
571   AbstractItem& target = FindByID(target_id);
572   if (target.GetType() == NullObject)
573     return false;
574   if (!impl_->IsValidMainType(target, type)) {
575     LOGE("Main type and item type are not matched (%d, %d)",
576         type, target.GetType());
577     return false;
578   }
579
580   AbstractItem& old = FindByMainType(type);
581   if (target.GetType() != NullObject)
582     old.impl_->main_type_ = MainNone;
583   target.impl_->main_type_ = type;
584
585   return true;
586 }
587
588 AbstractItem::MainType AbstractItem::GetMainType() const {
589   return impl_->main_type_;
590 }
591
592 AbstractItem& AbstractItem::FindByID(std::string id) {
593   if (GetId() == id)
594     return *this;
595
596   return FactoryManager::GetInst().GetNullItem();
597 }
598
599 AbstractItem& AbstractItem::FindByMainType(AbstractItem::MainType type) {
600   if (impl_->main_type_ == type)
601     return *this;
602
603   return FactoryManager::GetInst().GetNullItem();
604 }
605
606 std::shared_ptr<MultiLanguage> AbstractItem::GetMultiLanguage() const {
607   return impl_->multi_lang_;
608 }
609
610 vector<shared_ptr<MultiLanguage>> AbstractItem::GetMultiLanguageArr() const {
611   return impl_->multi_lang_arr_;
612 }
613
614 void AbstractItem::SetMultiLanguage(std::shared_ptr<MultiLanguage> multi) {
615   impl_->multi_lang_ = multi;
616 }
617
618 void AbstractItem::SetMultiLanguage(
619     vector<shared_ptr<MultiLanguage>> multi_arr) {
620   impl_->multi_lang_arr_ = multi_arr;
621 }
622
623 }  // namespace item
624 }  // namespace notification