Add internal api to support C#
[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   LOGI("EventInfo impl created");
58 }
59
60 EventInfo::EventInfo(Bundle serialized)
61   : impl_(new Impl(this, EventInfo::Post, "", "", "")) {
62   string event_str = serialized.GetString(NOTIFICATION_EX_EVENT_TYPE_KEY);
63   impl_->type_ = std::stoi(event_str, nullptr, 10);
64   impl_->owner_ = serialized.GetString(NOTIFICATION_EX_EVENT_OWNER_KEY);
65   impl_->channel_ = serialized.GetString(NOTIFICATION_EX_EVENT_CHANNEL_KEY);
66   impl_->item_id_ = serialized.GetString(NOTIFICATION_EX_EVENT_ITEM_ID_KEY);
67   string uid_str = serialized.GetString(NOTIFICATION_EX_EVENT_UID_KEY);
68   impl_->uid_ = (uid_t)std::stoi(uid_str, nullptr, 10);
69   string request_id_str =
70       serialized.GetString(NOTIFICATION_EX_EVENT_REQUEST_ID_KEY);
71   impl_->request_id_ = std::stoi(request_id_str, nullptr, 10);
72   string error_str =
73       serialized.GetString(NOTIFICATION_EX_EVENT_ERROR_KEY);
74   impl_->error_ = (NotificationError)strtol(error_str.c_str(), NULL, 10);
75 }
76
77 string EventInfo::GetString(int type) {
78   switch (type) {
79     case Post:
80       return "Post";
81     case Update:
82       return "Update";
83     case Delete:
84       return "Delete";
85     case Get:
86       return "Get";
87     case Error:
88       return "Error";
89     case Count:
90       return "Count";
91     case DeleteAll:
92       return "DeleteAll";
93     case Register:
94       return "Register";
95     case Unregister:
96       return "Unregister";
97     default:
98       return "Custom" + std::to_string(Custom);
99   }
100 }
101
102 Bundle EventInfo::Serialize() const {
103   Bundle serialized;
104   serialized.Add(NOTIFICATION_EX_EVENT_TYPE_KEY, to_string(static_cast<int>(impl_->type_)));
105   serialized.Add(NOTIFICATION_EX_EVENT_OWNER_KEY, impl_->owner_);
106   serialized.Add(NOTIFICATION_EX_EVENT_CHANNEL_KEY, impl_->channel_);
107   serialized.Add(NOTIFICATION_EX_EVENT_ITEM_ID_KEY, impl_->item_id_);
108   serialized.Add(NOTIFICATION_EX_EVENT_UID_KEY, to_string(static_cast<int>(impl_->uid_)));
109   serialized.Add(
110       NOTIFICATION_EX_EVENT_REQUEST_ID_KEY, to_string(impl_->request_id_));
111   serialized.Add(
112       NOTIFICATION_EX_EVENT_ERROR_KEY, to_string(impl_->error_));
113
114   return serialized;
115 }
116
117 int EventInfo::GetEventType() const {
118   return impl_->type_;
119 }
120
121 void EventInfo::SetEventType(int type) {
122   impl_->type_ = type;
123 }
124
125 string EventInfo::GetOwner() const {
126   return impl_->owner_;
127 }
128
129 string EventInfo::GetChannel() const {
130   return impl_->channel_;
131 }
132
133 string EventInfo::GetItemId() const {
134   return impl_->item_id_;
135 }
136
137 uid_t EventInfo::GetUid() const {
138   return impl_->uid_;
139 }
140
141 void EventInfo::SetUid(uid_t uid) {
142   impl_->uid_ = uid;
143 }
144
145 int EventInfo::GetRequestId() const {
146   return impl_->request_id_;
147 }
148
149 NotificationError EventInfo::GetError() const {
150   return impl_->error_;
151 }
152
153 void EventInfo::SetError(NotificationError error) {
154   impl_->error_ = error;
155 }
156
157 }  // namespace notification