2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "common/app_control.h"
23 #include "common/logger.h"
28 static bool BundleAddData(bundle* target, const std::string& key,
29 const std::string& value) {
30 int result = appsvc_add_data(target, key.c_str(), value.c_str());
32 LOGGER(ERROR) << "Failed to add data to appsvc.";
39 static bool BundleAddDataArray(bundle* target, const std::string& key,
40 const std::vector<std::string>& value_array) {
41 int n = value_array.size();
42 std::vector<const char*> v;
43 std::for_each(value_array.begin(), value_array.end(),
44 [&v] (std::string str) {
45 v.push_back(static_cast<const char*>(str.c_str()));
48 int result = appsvc_add_data_array(target, key.c_str(),
51 LOGGER(ERROR) << "Failed to add an array of data to appsvc.";
61 AppControl::AppControl(app_control_h app_control) {
62 app_control_clone(&app_control_, app_control);
63 app_control_to_bundle(app_control_, &app_control_bundle_);
66 AppControl:: AppControl() {
67 app_control_create(&app_control_);
68 app_control_to_bundle(app_control_, &app_control_bundle_);
71 AppControl::~AppControl() {
72 if (app_control_ != NULL) {
73 app_control_destroy(app_control_);
77 std::string AppControl::operation() const {
78 const char* operation = appsvc_get_operation(app_control_bundle_);
80 if (operation != NULL) {
81 return std::string(operation);
87 void AppControl::set_operation(const std::string& operation) {
88 appsvc_set_operation(app_control_bundle_, operation.c_str());
91 std::string AppControl::mime() const {
92 const char* mime = appsvc_get_mime(app_control_bundle_);
95 return std::string(mime);
101 void AppControl::set_mime(const std::string& mime) {
102 appsvc_set_mime(app_control_bundle_, mime.c_str());
105 std::string AppControl::uri() const {
106 const char* uri = appsvc_get_uri(app_control_bundle_);
109 return std::string(uri);
111 return std::string();
115 void AppControl::set_uri(const std::string& uri) {
116 appsvc_set_uri(app_control_bundle_, uri.c_str());
119 std::string AppControl::category() const {
120 const char* category = appsvc_get_category(app_control_bundle_);
122 if (category != NULL) {
123 return std::string(category);
125 return std::string();
129 void AppControl::set_category(const std::string& category) {
130 appsvc_set_category(app_control_bundle_, category.c_str());
133 std::string AppControl::data(const std::string& key) const {
134 const char* data = appsvc_get_data(app_control_bundle_, key.c_str());
137 return std::string(data);
139 return std::string();
143 std::vector<std::string> AppControl::data_array(const std::string& key) const {
144 int data_array_len = 0;
145 const char** data_array = appsvc_get_data_array(app_control_bundle_,
146 key.c_str(), &data_array_len);
147 std::vector<std::string> data_vector;
149 if (data_array_len > 0) {
150 for (int i = 0; i < data_array_len; i++) {
151 data_vector.push_back(data_array[i]);
157 std::string AppControl::encoded_bundle() {
158 bundle_raw* encoded_data;
160 bundle_encode(app_control_bundle_, &encoded_data, &len);
161 std::unique_ptr<bundle_raw*, decltype(bundle_free_encoded_rawdata)*>
162 ptr { &encoded_data, bundle_free_encoded_rawdata};
163 return std::string(reinterpret_cast<char*>(encoded_data), len);
166 bool AppControl::IsDataArray(const std::string& key) {
167 return appsvc_data_is_array(app_control_bundle_, key.c_str());
170 bool AppControl::AddData(const std::string& key, const std::string& value) {
171 return BundleAddData(app_control_bundle_, key, value);
176 bool AppControl::AddDataArray(const std::string& key,
177 const std::vector<std::string>& value_array) {
178 return BundleAddDataArray(app_control_bundle_, key, value_array);
182 bool AppControl::Reply(const std::map<std::string,
183 std::vector<std::string>>& data) {
185 if (appsvc_create_result_bundle(app_control_bundle_,
186 &result) != APPSVC_RET_OK) {
187 LOGGER(ERROR) << "Failed to craete result bundle.";
190 auto it = data.begin();
191 for ( ; it != data.end(); ++it) {
192 const std::string& key = it->first;
193 if (it->second.size() == 1) {
194 BundleAddData(result, key, it->second[0]);
196 BundleAddDataArray(result, key, it->second);
200 int ret = appsvc_send_result(result, APPSVC_RES_OK);
203 return ret == APPSVC_RET_OK ? true : false;
206 bool AppControl::LaunchRequest() {
207 return (app_control_send_launch_request(app_control_, NULL, NULL) ==
208 APP_CONTROL_ERROR_NONE);