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.
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"
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__"
30 namespace notification {
33 VisibilityAction::VisibilityAction()
34 : AbstractAction(true), impl_(new Impl(this)) {
37 VisibilityAction::VisibilityAction(std::string extra)
38 : AbstractAction(true, extra), impl_(new Impl(this)) {
41 VisibilityAction::Impl::Impl(VisibilityAction* parent) : parent_(parent) {
42 LOGI("VisibilityAction created");
45 VisibilityAction::~VisibilityAction() = default;
46 VisibilityAction::Impl::~Impl() = default;
48 int VisibilityAction::GetType() const {
49 return AbstractAction::Visibility;
52 Bundle VisibilityAction::Serialize() const {
58 b = AbstractAction::Serialize();
60 std::list<std::unique_ptr<Impl::VisibilityEntity>>::iterator it;
62 for (it = impl_->entities_.begin(); it != impl_->entities_.end(); it++) {
66 snprintf(buf_key, sizeof(buf_key), "%s:%d",
67 VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
69 snprintf(buf_data, sizeof(buf_data), "%s:%d",
70 (*it)->id_.c_str(), (*it)->visibility_);
72 b.Add(std::string(buf_key), std::string(buf_data));
76 b.Add(VISIBILITY_ACTION_ENTITY_SIZE_KEY, std::to_string(i));
80 void VisibilityAction::Deserialize(Bundle b) {
85 AbstractAction::Deserialize(b);
87 size = std::stoi(b.GetString(VISIBILITY_ACTION_ENTITY_SIZE_KEY));
89 for (int i = 0; i < size; i++) {
92 snprintf(buf_key, sizeof(buf_key), "%s:%d",
93 VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
95 std::string str = b.GetString(std::string(buf_key));
98 std::string id = str.substr(0, pos);
99 bool visible = std::stoi(str.substr(pos+1));
101 impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
106 bool VisibilityAction::IsLocal() const {
107 return AbstractAction::IsLocal();
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_);
118 std::string VisibilityAction::GetExtra() const {
119 return AbstractAction::GetExtra();
122 void VisibilityAction::SetVisibility(std::string id, bool visible) {
123 impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
128 } //namespace notification