Release version 0.5.85
[platform/core/api/notification.git] / notification-ex / event_info.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 #include <unistd.h>
19
20 #include <memory>
21
22 #include "notification-ex/event_info_internal.h"
23 #include "notification-ex/event_info_implementation.h"
24 #include "notification-ex/ex_util.h"
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "NOTIFICATION_EX"
31 #define NOTIFICATION_EX_EVENT_TYPE_KEY "__NOTIFICATION_EX_EVENT_TYPE_KEY__"
32 #define NOTIFICATION_EX_EVENT_OWNER_KEY "__NOTIFICATION_EX_EVENT_OWNER_KEY__"
33 #define NOTIFICATION_EX_EVENT_CHANNEL_KEY "__NOTIFICATION_EX_EVENT_CHANNEL_KEY__"
34 #define NOTIFICATION_EX_EVENT_ITEM_ID_KEY "__NOTIFICATION_EX_EVENT_ITEM_ID_KEY__"
35 #define NOTIFICATION_EX_EVENT_UID_KEY "__NOTIFICATION_EX_EVENT_UID_KEY__"
36 #define NOTIFICATION_EX_EVENT_REQUEST_ID_KEY "__NOTIFICATION_EX_EVENT_REQUEST_ID_KEY__"
37 #define NOTIFICATION_EX_EVENT_ERROR_KEY "__NOTIFICATION_EX_EVENT_ERROR_KEY__"
38
39 using namespace std;
40 using namespace tizen_base;
41 namespace notification {
42
43 EventInfo::EventInfo(int type, std::string owner, std::string channel,
44     std::string item_id)
45     : impl_(new Impl(this, type, owner, channel, item_id)) {
46 }
47 EventInfo::~EventInfo() = default;
48 EventInfo::Impl::~Impl() = default;
49
50 EventInfo::Impl::Impl(EventInfo* parent, int type, std::string owner,
51     std::string channel, std::string item_id)
52     : type_(type), owner_(owner), channel_(channel), item_id_(item_id),
53     parent_(parent) {
54   uid_ = getuid();
55   request_id_ = util::GetRequestId();
56   error_ = ERROR_NONE;
57   validated_uid_ = 0;
58   LOGI("EventInfo impl created");
59 }
60
61 EventInfo::EventInfo(Bundle serialized)
62   : impl_(new Impl(this, EventInfo::Post, "", "", "")) {
63   string event_str = serialized.GetString(NOTIFICATION_EX_EVENT_TYPE_KEY);
64   impl_->type_ = std::stoi(event_str, nullptr, 10);
65   impl_->owner_ = serialized.GetString(NOTIFICATION_EX_EVENT_OWNER_KEY);
66   impl_->channel_ = serialized.GetString(NOTIFICATION_EX_EVENT_CHANNEL_KEY);
67   impl_->item_id_ = serialized.GetString(NOTIFICATION_EX_EVENT_ITEM_ID_KEY);
68   string uid_str = serialized.GetString(NOTIFICATION_EX_EVENT_UID_KEY);
69   impl_->uid_ = (uid_t)std::stoi(uid_str, nullptr, 10);
70   string request_id_str =
71       serialized.GetString(NOTIFICATION_EX_EVENT_REQUEST_ID_KEY);
72   impl_->request_id_ = std::stoi(request_id_str, nullptr, 10);
73   string error_str =
74       serialized.GetString(NOTIFICATION_EX_EVENT_ERROR_KEY);
75   impl_->error_ = (NotificationError)strtol(error_str.c_str(), NULL, 10);
76 }
77
78 string EventInfo::GetString(int type) {
79   switch (type) {
80     case Post:
81       return "Post";
82     case Update:
83       return "Update";
84     case Delete:
85       return "Delete";
86     case Get:
87       return "Get";
88     case Error:
89       return "Error";
90     case Count:
91       return "Count";
92     case DeleteAll:
93       return "DeleteAll";
94     case Register:
95       return "Register";
96     case Unregister:
97       return "Unregister";
98     default:
99       return "Custom" + std::to_string(Custom);
100   }
101 }
102
103 Bundle EventInfo::Serialize() const {
104   Bundle serialized;
105   serialized.Add(NOTIFICATION_EX_EVENT_TYPE_KEY, to_string(static_cast<int>(impl_->type_)));
106   serialized.Add(NOTIFICATION_EX_EVENT_OWNER_KEY, impl_->owner_);
107   serialized.Add(NOTIFICATION_EX_EVENT_CHANNEL_KEY, impl_->channel_);
108   serialized.Add(NOTIFICATION_EX_EVENT_ITEM_ID_KEY, impl_->item_id_);
109   serialized.Add(NOTIFICATION_EX_EVENT_UID_KEY, to_string(static_cast<int>(impl_->uid_)));
110   serialized.Add(
111       NOTIFICATION_EX_EVENT_REQUEST_ID_KEY, to_string(impl_->request_id_));
112   serialized.Add(
113       NOTIFICATION_EX_EVENT_ERROR_KEY, to_string(impl_->error_));
114
115   return serialized;
116 }
117
118 int EventInfo::GetEventType() const {
119   return impl_->type_;
120 }
121
122 void EventInfo::SetEventType(int type) {
123   impl_->type_ = type;
124 }
125
126 string EventInfo::GetOwner() const {
127   return impl_->owner_;
128 }
129
130 void EventInfo::SetValidatedOwner(std::string owner) {
131   impl_->validated_owner_ = owner;
132 }
133
134 string EventInfo::GetValidatedOwner() const {
135   return impl_->validated_owner_;
136 }
137
138 string EventInfo::GetChannel() const {
139   return impl_->channel_;
140 }
141
142 string EventInfo::GetItemId() const {
143   return impl_->item_id_;
144 }
145
146 uid_t EventInfo::GetUid() const {
147   return impl_->uid_;
148 }
149
150 void EventInfo::SetUid(uid_t uid) {
151   impl_->uid_ = uid;
152 }
153
154 uid_t EventInfo::GetValidatedUid() const {
155   return impl_->validated_uid_;
156 }
157
158 void EventInfo::SetValidatedUid(uid_t uid) {
159   impl_->validated_uid_ = uid;
160 }
161
162 int EventInfo::GetRequestId() const {
163   return impl_->request_id_;
164 }
165
166 NotificationError EventInfo::GetError() const {
167   return impl_->error_;
168 }
169
170 void EventInfo::SetError(NotificationError error) {
171   impl_->error_ = error;
172 }
173
174 }  // namespace notification