Update api about private_id
[platform/core/api/notification.git] / notification-ex / app_control_action.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 <app_control_internal.h>
19
20 #include <string>
21
22 #include "notification-ex/app_control_action.h"
23 #include "notification-ex/app_control_action_implementation.h"
24 #include "notification-ex/exception.h"
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "NOTIFICATION_EX"
31 #define APPCONTROL_ACTION_KEY "__APPCONTROL_ACTION_KEY__"
32
33 using namespace tizen_base;
34
35 namespace notification {
36 namespace item {
37
38 AppControlAction::AppControlAction(app_control_h app_control)
39     : AbstractAction(true), impl_(new Impl(this)) {
40   app_control_h control_;
41   app_control_clone(&control_, app_control);
42   impl_->control_ = control_;
43 }
44
45 AppControlAction::AppControlAction(app_control_h app_control, std::string extra)
46     : AbstractAction(true, extra), impl_(new Impl(this)) {
47   app_control_h control_;
48   app_control_clone(&control_, app_control);
49   impl_->control_ = control_;
50 }
51
52 AppControlAction::Impl::Impl(AppControlAction* parent)
53     : parent_(parent) {
54   LOGI("AppControlAction created");
55 }
56
57 AppControlAction::~AppControlAction() = default;
58 AppControlAction::Impl::~Impl() {
59   if (control_)
60     app_control_destroy(control_);
61 }
62
63 int AppControlAction::GetType() const {
64   return AbstractAction::AppControl;
65 }
66
67 Bundle AppControlAction::Serialize() const {
68   Bundle b;
69   bundle* control_b = NULL;
70   bundle_raw* control_raw;
71   int len = 0;
72
73   app_control_export_as_bundle(impl_->control_, &control_b);
74   if (control_b == NULL) {
75     LOGE("failed to get bundle from app_control");
76     return {};
77   }
78
79   bundle_encode(control_b, &control_raw, &len);
80   if (len <= 0) {
81     LOGE("bundle encode failed");
82     bundle_free(control_b);
83     return {};
84   }
85
86   b = AbstractAction::Serialize();
87   b.Add(APPCONTROL_ACTION_KEY,
88     std::string(reinterpret_cast<const char*>(control_raw)));
89
90   bundle_free(control_b);
91   free(control_raw);
92   return b;
93 }
94
95 void AppControlAction::Deserialize(Bundle b) {
96   app_control_h app_control = nullptr;
97   bundle* b_ = nullptr;
98   std::string control_str;
99   int ret;
100
101   AbstractAction::Deserialize(b);
102
103   control_str = b.GetString(APPCONTROL_ACTION_KEY);
104
105   b_ = bundle_decode((bundle_raw*)control_str.c_str(), control_str.length());
106   if (b_ == nullptr) {
107     LOGE("bundle_decode failed");
108     return;
109   }
110
111   ret = app_control_create(&app_control);
112   if (ret != APP_CONTROL_ERROR_NONE) {
113     LOGE("failed to create app_control");
114     bundle_free(b_);
115     return;
116   }
117
118   ret = app_control_import_from_bundle(app_control, b_);
119   bundle_free(b_);
120   if (ret != APP_CONTROL_ERROR_NONE) {
121     LOGE("failed to make app_control from bundle");
122     app_control_destroy(app_control);
123     return;
124   }
125
126   impl_->control_ = app_control;
127 }
128
129 bool AppControlAction::IsLocal() const {
130   return AbstractAction::IsLocal();
131 }
132
133 void AppControlAction::Execute(std::shared_ptr<AbstractItem> item) {
134   app_control_send_launch_request(impl_->control_, NULL, NULL);
135 }
136
137 std::string AppControlAction::GetExtra() const {
138   return AbstractAction::GetExtra();
139 }
140
141 void AppControlAction::SetAppControl(app_control_h app_control) {
142   if (impl_->control_ != nullptr)
143     app_control_destroy(impl_->control_);
144
145   app_control_h control_;
146   app_control_clone(&control_, app_control);
147
148   impl_->control_ = control_;
149 }
150
151 app_control_h AppControlAction::GetAppControl() const {
152   return impl_->control_;
153 }
154
155 }  // namespace item
156 }  // namespace notification