Update api about private_id
[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/factory_manager.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 #define PROGRESS_TYPE_KEY "__PROGRESS_TYPE_KEY__"
35 #define PROGRESS_UNIT_KEY "__PROGRESS_UNIT_KEY__"
36
37 using namespace std;
38 using namespace tizen_base;
39
40 namespace notification {
41 namespace item {
42
43 ProgressItem::ProgressItem(float min_val, float current, float max_val,
44     std::shared_ptr<AbstractAction> action)
45     : AbstractItem(action),
46     impl_(new Impl(min_val, current, max_val, this)) {
47 }
48
49 ProgressItem::ProgressItem(string id, float min_val, float current,
50     float max_val, std::shared_ptr<AbstractAction> action)
51     : AbstractItem(id, action),
52     impl_(new Impl(min_val, current, max_val, this)) {
53 }
54
55 ProgressItem::~ProgressItem() = default;
56 ProgressItem::Impl::~Impl() = default;
57
58 ProgressItem::Impl::Impl(float min_val, float current, float max_val,
59     ProgressItem* parent)
60     : min_(min_val), current_(current), max_(max_val), parent_(parent) {
61   if (min_val > current || max_val < current)
62     THROW(ERROR_INVALID_PARAMETER);
63
64   type_ = ProgressItem::Type::Default;
65   LOGI("ProgressItem impl created");
66 }
67
68 int ProgressItem::GetType() const {
69   return AbstractItem::Progress;
70 }
71
72 Bundle ProgressItem::Serialize() const {
73   Bundle b;
74   b = AbstractItem::Serialize();
75   b.Add(PROGRESS_MIN_KEY, to_string(impl_->min_));
76   b.Add(PROGRESS_CURRENT_KEY, to_string(impl_->current_));
77   b.Add(PROGRESS_MAX_KEY, to_string(impl_->max_));
78   b.Add(PROGRESS_TYPE_KEY, to_string(static_cast<int>(impl_->type_)));
79   b.Add(PROGRESS_UNIT_KEY, impl_->unit_);
80   return b;
81 }
82
83 void ProgressItem::Deserialize(Bundle b) {
84   AbstractItem::Deserialize(b);
85   impl_->min_ = strtof(b.GetString(PROGRESS_MIN_KEY).c_str(), 0);
86   impl_->current_ = strtof(b.GetString(PROGRESS_CURRENT_KEY).c_str(), 0);
87   impl_->max_ = strtof(b.GetString(PROGRESS_MAX_KEY).c_str(), 0);
88   impl_->type_ = static_cast<Type>(std::stoi(b.GetString(PROGRESS_TYPE_KEY)));
89   impl_->unit_ = b.GetString(PROGRESS_UNIT_KEY);
90 }
91
92 bool ProgressItem::IsItemTypeExist(int type) {
93   if (GetType() == type)
94     return true;
95   return false;
96 }
97
98 float ProgressItem::GetCurrent() const {
99   return impl_->current_;
100 }
101
102 void ProgressItem::SetCurrent(float current) {
103   impl_->current_ = current;
104 }
105
106 float ProgressItem::GetMin() const {
107   return impl_->min_;
108 }
109
110 float ProgressItem::GetMax() const {
111   return impl_->max_;
112 }
113
114 void ProgressItem::SetProgressType(ProgressItem::Type type) {
115   impl_->type_ = type;
116 }
117
118 ProgressItem::Type ProgressItem::GetProgressType() const {
119   return impl_->type_;
120 }
121
122 void ProgressItem::SetDefaultUnit(std::string unit)
123 {
124   impl_->unit_ = unit;
125 }
126
127 std::string ProgressItem::GetDefaultUnit() const {
128   return impl_->unit_;
129 }
130
131 }  // namespace item
132 }  // namespace notification