Add progress item implementation
[platform/core/api/notification.git] / notification-ex / progress_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
19 #include <memory>
20
21 #include "notification-ex/exception.h"
22 #include "notification-ex/progress_item.h"
23 #include "notification-ex/progress_item_implementation.h"
24 #include "notification-ex/item_factory.h"
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "NOTIFICATION_EX"
31 #define PROGRESS_MIN_KEY "__PROGRESS_MIN_KEY__"
32 #define PROGRESS_CURRENT_KEY "__PROGRESS_CURRENT_KEY__"
33 #define PROGRESS_MAX_KEY "__PROGRESS_MAX_KEY__"
34
35 using namespace std;
36 namespace notification {
37 namespace item {
38
39 ProgressItem::ProgressItem(float min_val, float current, float max_val,
40     std::shared_ptr<AbstractAction> action)
41   : AbstractItem(AbstractItem::Progress, action),
42     impl_(new Impl(min_val, current, max_val, this)) {
43 }
44
45 ProgressItem::ProgressItem(string id, float min_val, float current, float max_val,
46     std::shared_ptr<AbstractAction> action)
47   : AbstractItem(id, AbstractItem::Progress, action),
48   impl_(new Impl(min_val, current, max_val, this)) {
49 }
50
51 ProgressItem::~ProgressItem() = default;
52 ProgressItem::Impl::~Impl() = default;
53
54 ProgressItem::Impl::Impl(float min_val, float current, float max_val, ProgressItem* parent)
55   : min_(min_val), current_(current), max_(max_val), parent_(parent) {
56   if (min_val > current || max_val < current)
57     THROW(NOTIFICATION_ERROR_INVALID_PARAMETER);
58   LOGI("ProgressItem impl created");
59 }
60
61 Bundle ProgressItem::Serialize() {
62   Bundle b;
63   b = AbstractItem::Serialize();
64   b.Add(PROGRESS_MIN_KEY, to_string(impl_->min_));
65   b.Add(PROGRESS_CURRENT_KEY, to_string(impl_->current_));
66   b.Add(PROGRESS_MAX_KEY, to_string(impl_->max_));
67   return b;
68 }
69
70 void ProgressItem::Deserialize(Bundle b) {
71   AbstractItem::Deserialize(b);
72   impl_->min_ = strtof(b.GetString(PROGRESS_MIN_KEY).c_str(), 0);
73   impl_->current_ = strtof(b.GetString(PROGRESS_CURRENT_KEY).c_str(), 0);
74   impl_->max_ = strtof(b.GetString(PROGRESS_MAX_KEY).c_str(), 0);
75 }
76
77 AbstractItem& ProgressItem::FindByID(std::string id) {
78   if (GetId() == id)
79     return *this;
80   return ItemFactory::GetNullItem();
81 }
82
83 float ProgressItem::GetCurrent() const {
84   return impl_->current_;
85 }
86
87 void ProgressItem::SetCurrent(float current) {
88   impl_->current_ = current;
89 }
90
91 float ProgressItem::GetMin() const {
92   return impl_->min_;
93 }
94
95 float ProgressItem::GetMax() const {
96   return impl_->max_;
97 }
98
99 }  // namespace item
100 }  // namespace notification