1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "common/app_control.h"
11 #include "common/logger.h"
16 static bool BundleAddData(bundle* target, const std::string& key,
17 const std::string& value) {
18 int result = appsvc_add_data(target, key.c_str(), value.c_str());
20 LOGGER(ERROR) << "Failed to add data to appsvc.";
27 static bool BundleAddDataArray(bundle* target, const std::string& key,
28 const std::vector<std::string>& value_array) {
29 int n = value_array.size();
30 std::vector<const char*> v;
31 std::for_each(value_array.begin(), value_array.end(),
32 [&v] (std::string str) {
33 v.push_back(static_cast<const char*>(str.c_str()));
36 int result = appsvc_add_data_array(target, key.c_str(),
39 LOGGER(ERROR) << "Failed to add an array of data to appsvc.";
49 AppControl::AppControl(app_control_h app_control) {
50 app_control_clone(&app_control_, app_control);
51 app_control_to_bundle(app_control_, &app_control_bundle_);
54 AppControl:: AppControl() {
55 app_control_create(&app_control_);
56 app_control_to_bundle(app_control_, &app_control_bundle_);
59 AppControl::~AppControl() {
60 if (app_control_ != NULL) {
61 app_control_destroy(app_control_);
65 std::string AppControl::operation() const {
66 const char* operation = appsvc_get_operation(app_control_bundle_);
68 if (operation != NULL) {
69 return std::string(operation);
75 void AppControl::set_operation(const std::string& operation) {
76 appsvc_set_operation(app_control_bundle_, operation.c_str());
79 std::string AppControl::mime() const {
80 const char* mime = appsvc_get_mime(app_control_bundle_);
83 return std::string(mime);
89 void AppControl::set_mime(const std::string& mime) {
90 appsvc_set_mime(app_control_bundle_, mime.c_str());
93 std::string AppControl::uri() const {
94 const char* uri = appsvc_get_uri(app_control_bundle_);
97 return std::string(uri);
103 void AppControl::set_uri(const std::string& uri) {
104 appsvc_set_uri(app_control_bundle_, uri.c_str());
107 std::string AppControl::category() const {
108 const char* category = appsvc_get_category(app_control_bundle_);
110 if (category != NULL) {
111 return std::string(category);
113 return std::string();
117 void AppControl::set_category(const std::string& category) {
118 appsvc_set_category(app_control_bundle_, category.c_str());
121 std::string AppControl::data(const std::string& key) const {
122 const char* data = appsvc_get_data(app_control_bundle_, key.c_str());
125 return std::string(data);
127 return std::string();
131 std::vector<std::string> AppControl::data_array(const std::string& key) const {
132 int data_array_len = 0;
133 const char** data_array = appsvc_get_data_array(app_control_bundle_,
134 key.c_str(), &data_array_len);
135 std::vector<std::string> data_vector;
137 if (data_array_len > 0) {
138 for (int i = 0; i < data_array_len; i++) {
139 data_vector.push_back(data_array[i]);
145 std::string AppControl::encoded_bundle() {
146 bundle_raw* encoded_data;
148 bundle_encode(app_control_bundle_, &encoded_data, &len);
149 std::unique_ptr<bundle_raw*, decltype(bundle_free_encoded_rawdata)*>
150 ptr { &encoded_data, bundle_free_encoded_rawdata};
151 return std::string(reinterpret_cast<char*>(encoded_data), len);
154 bool AppControl::IsDataArray(const std::string& key) {
155 return appsvc_data_is_array(app_control_bundle_, key.c_str());
158 bool AppControl::AddData(const std::string& key, const std::string& value) {
159 return BundleAddData(app_control_bundle_, key, value);
164 bool AppControl::AddDataArray(const std::string& key,
165 const std::vector<std::string>& value_array) {
166 return BundleAddDataArray(app_control_bundle_, key, value_array);
170 bool AppControl::Reply(const std::map<std::string,
171 std::vector<std::string>>& data) {
173 if (appsvc_create_result_bundle(app_control_bundle_,
174 &result) != APPSVC_RET_OK) {
175 LOGGER(ERROR) << "Failed to craete result bundle.";
178 auto it = data.begin();
179 for ( ; it != data.end(); ++it) {
180 const std::string& key = it->first;
181 if (it->second.size() == 1) {
182 BundleAddData(result, key, it->second[0]);
184 BundleAddDataArray(result, key, it->second);
188 int ret = appsvc_send_result(result, APPSVC_RES_OK);
191 return ret == APPSVC_RET_OK ? true : false;
194 bool AppControl::LaunchRequest() {
195 return (app_control_send_launch_request(app_control_, NULL, NULL) ==
196 APP_CONTROL_ERROR_NONE);