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.
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"
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__"
33 using namespace tizen_base;
35 namespace notification {
38 VisibilityAction::VisibilityAction()
39 : AbstractAction(true), impl_(new Impl(this)) {
42 VisibilityAction::VisibilityAction(std::string extra)
43 : AbstractAction(true, extra), impl_(new Impl(this)) {
46 VisibilityAction::Impl::Impl(VisibilityAction* parent) : parent_(parent) {
47 LOGI("VisibilityAction created");
50 VisibilityAction::~VisibilityAction() = default;
51 VisibilityAction::Impl::~Impl() = default;
53 int VisibilityAction::GetType() const {
54 return AbstractAction::Visibility;
57 Bundle VisibilityAction::Serialize() const {
63 b = AbstractAction::Serialize();
65 std::list<std::unique_ptr<Impl::VisibilityEntity>>::iterator it;
67 for (it = impl_->entities_.begin(); it != impl_->entities_.end(); it++) {
71 snprintf(buf_key, sizeof(buf_key), "%s:%d",
72 VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
74 snprintf(buf_data, sizeof(buf_data), "%s:%d",
75 (*it)->id_.c_str(), (*it)->visibility_);
77 b.Add(std::string(buf_key), std::string(buf_data));
81 b.Add(VISIBILITY_ACTION_ENTITY_SIZE_KEY, std::to_string(i));
85 void VisibilityAction::Deserialize(Bundle b) {
90 AbstractAction::Deserialize(b);
92 size = std::stoi(b.GetString(VISIBILITY_ACTION_ENTITY_SIZE_KEY));
94 for (int i = 0; i < size; i++) {
97 snprintf(buf_key, sizeof(buf_key), "%s:%d",
98 VISIBILITY_ACTION_ENTITY_SIZE_KEY, i);
100 std::string str = b.GetString(std::string(buf_key));
103 std::string id = str.substr(0, pos);
104 bool visible = std::stoi(str.substr(pos+1));
106 impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
111 bool VisibilityAction::IsLocal() const {
112 return AbstractAction::IsLocal();
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_);
123 std::string VisibilityAction::GetExtra() const {
124 return AbstractAction::GetExtra();
127 void VisibilityAction::SetVisibility(std::string id, bool visible) {
128 impl_->entities_.emplace_back(new VisibilityAction::Impl::VisibilityEntity(
133 } // namespace notification