Release version 0.7.18
[platform/core/api/notification.git] / notification-ex / time_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 "notification-ex/time_item.h"
20 #include "notification-ex/time_item_implementation.h"
21 #include "notification-ex/factory_manager.h"
22 #include "notification-ex/exception.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
28 #define LOG_TAG "NOTIFICATION_EX"
29 #define TIME_KEY "__TIME_KEY__"
30
31 using namespace tizen_base;
32
33 namespace notification {
34 namespace item {
35
36 TimeItem::TimeItem(std::shared_ptr<AbstractAction> action)
37     : AbstractItem(action), impl_(new Impl(this, 0)) {
38   time_t current_time;
39   time(&current_time);
40
41   impl_->time_ = current_time;
42 }
43
44 TimeItem::TimeItem(time_t time, std::shared_ptr<AbstractAction> action)
45     : AbstractItem(action), impl_(new Impl(this, time)) {
46 }
47
48 TimeItem::TimeItem(std::string id, time_t time,
49     std::shared_ptr<AbstractAction> action)
50     : AbstractItem(id, action), impl_(new Impl(this, time)) {
51 }
52
53 TimeItem::Impl::Impl(TimeItem* parent, time_t time)
54     : parent_(parent), time_(time) {
55   LOGI("TimeItem impl created");
56 }
57
58 int TimeItem::GetType() const {
59   return AbstractItem::Time;
60 }
61
62 Bundle TimeItem::Serialize() const {
63   Bundle b;
64   struct tm timeinfo;
65   char buf[80] = {0, };
66
67   b = AbstractItem::Serialize();
68
69   // timt_t to tm
70   localtime_r(&impl_->time_, &timeinfo);
71   // tm to str
72   strftime (buf, sizeof(buf), "%s", &timeinfo);
73   b.Add(TIME_KEY, std::string(buf));
74
75   return b;
76 }
77
78 void TimeItem::Deserialize(Bundle b) {
79   std::string time_s;
80   struct tm timeinfo;
81
82   AbstractItem::Deserialize(b);
83   time_s = b.GetString(TIME_KEY);
84
85   // str to tm
86   strptime(time_s.c_str(), "%s", &timeinfo);
87   // tm to time_t
88   impl_->time_ = mktime(&timeinfo);
89 }
90
91 bool TimeItem::IsItemTypeExist(int type) {
92   if (GetType() == type)
93     return true;
94   return false;
95 }
96
97 time_t TimeItem::GetTime() const {
98   return impl_->time_;
99 }
100
101 void TimeItem::SetTime(time_t time) {
102   impl_->time_ = time;
103 }
104
105 TimeItem::~TimeItem() = default;
106 TimeItem::Impl::~Impl() = default;
107
108 }  // namespace item
109 }  // namespace notification