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