Check coding style
[platform/core/api/notification.git] / notification-ex / visibility_action.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 "notification-ex/abstract_item.h"
18 #include "notification-ex/visibility_action.h"
19 #include "notification-ex/visibility_action_implementation.h"
20 #include "notification-ex/exception.h"
21
22 #ifdef LOG_TAG
23 #undef LOG_TAG
24 #endif
25
26 #define LOG_TAG "NOTIFICATION_EX"
27 #define VISIBILITY_ACTION_ENTITY_KEY "__VISIBILITY_ACTION_VISIBLE_KEY__"
28 #define VISIBILITY_ACTION_ENTITY_SIZE_KEY "__VISIBILITY_ACTION_ENTITY_SIZE_KEY__"
29
30 namespace notification {
31 namespace item {
32
33 VisibilityAction::VisibilityAction()
34     : AbstractAction(true), impl_(new Impl(this)) {
35 }
36
37 VisibilityAction::VisibilityAction(std::string extra)
38     : AbstractAction(true, extra), impl_(new Impl(this)) {
39 }
40
41 VisibilityAction::Impl::Impl(VisibilityAction* parent) : parent_(parent) {
42   LOGI("VisibilityAction created");
43 }
44
45 VisibilityAction::~VisibilityAction() = default;
46 VisibilityAction::Impl::~Impl() = default;
47
48 int VisibilityAction::GetType() const {
49   return AbstractAction::Visibility;
50 }
51
52 Bundle VisibilityAction::Serialize() const {
53   char buf_key[128];
54   char buf_data[128];
55   Bundle b;
56   int i = 0;
57
58   b = AbstractAction::Serialize();
59
60   std::list<std::unique_ptr<Impl::VisibilityEntity>>::iterator it;
61
62   for (it = impl_->entities_.begin(); it != impl_->entities_.end(); it++) {
63     buf_key[0] = '\0';
64     buf_data[0] = '\0';
65
66     snprintf(buf_key, sizeof(buf_key), "%s:%d",
67       VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
68
69     snprintf(buf_data, sizeof(buf_data), "%s:%d",
70       (*it)->id_.c_str(), (*it)->visibility_);
71
72     b.Add(std::string(buf_key), std::string(buf_data));
73     i++;
74   }
75
76   b.Add(VISIBILITY_ACTION_ENTITY_SIZE_KEY, std::to_string(i));
77   return b;
78 }
79
80 void VisibilityAction::Deserialize(Bundle b) {
81   char buf_key[128];
82   int size;
83   std::size_t pos;
84
85   AbstractAction::Deserialize(b);
86
87   size = std::stoi(b.GetString(VISIBILITY_ACTION_ENTITY_SIZE_KEY));
88
89   for (int i = 0; i < size; i++) {
90     buf_key[0] = '\0';
91
92     snprintf(buf_key, sizeof(buf_key), "%s:%d",
93       VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
94
95     std::string str = b.GetString(std::string(buf_key));
96
97     pos = str.find(":");
98     std::string id = str.substr(0, pos);
99     bool visible = std::stoi(str.substr(pos+1));
100
101     impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
102       id, visible));
103   }
104 }
105
106 bool VisibilityAction::IsLocal() const {
107   return AbstractAction::IsLocal();
108 }
109
110 void VisibilityAction::Execute(std::shared_ptr<AbstractItem> item) {
111   for (const auto& entity : impl_->entities_) {
112     AbstractItem& item_ = item->FindByID(entity->id_);
113     if (item_.GetType() != AbstractItem::NullObject)
114       item_.SetVisible(entity->visibility_);
115   }
116 }
117
118 std::string VisibilityAction::GetExtra() const {
119   return AbstractAction::GetExtra();
120 }
121
122 void VisibilityAction::SetVisibility(std::string id, bool visible) {
123   impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
124       id, visible));
125 }
126
127 }  //namespace item
128 }  //namespace notification