AppControl : Add Reply method
authorSeungkeun Lee <sngn.lee@samsung.com>
Mon, 20 Apr 2015 02:53:00 +0000 (11:53 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Tue, 21 Apr 2015 03:26:10 +0000 (12:26 +0900)
Change-Id: Iee009597210d9b08dd5cee57ab9b2b1259e3ac94

src/runtime/app_control.cc [changed mode: 0644->0755]
src/runtime/app_control.h

old mode 100644 (file)
new mode 100755 (executable)
index ef022cd..ed10b4e
 namespace wrt {
 
 namespace {
+static bool BundleAddData(bundle* target, const std::string& key,
+                          const std::string& value) {
+  int result = appsvc_add_data(target, key.c_str(), value.c_str());
+  if (result < 0) {
+    LoggerE("BundleAddData : appsvc_add_data fail");
+    return false;
+  } else {
+    return true;
+  }
+}
+
+static bool BundleAddDataArray(bundle* target, const std::string& key,
+                               const std::vector<std::string>& value_array) {
+  int n = value_array.size();
+  std::vector<const char*> v;
+  std::for_each(value_array.begin(), value_array.end(),
+                [&v] (std::string str) {
+                  v.push_back(static_cast<const char*>(str.c_str()));
+                });
+
+  int result = appsvc_add_data_array(target, key.c_str(),
+                                     v.data(), n);
+  if (result < 0) {
+    LoggerE("BundleAddDataArray appsvc_add_data_array fail");
+    return false;
+  } else {
+    return true;
+  }
+}
+
+
 }  // namespace
 
 AppControl::AppControl(app_control_h app_control) {
@@ -105,32 +136,39 @@ bool AppControl::IsDataArray(const std::string& key) {
 }
 
 bool AppControl::AddData(const std::string& key, const std::string& value) {
-  int result = appsvc_add_data(app_control_bundle_, key.c_str(), value.c_str());
-  if (result < 0) {
-    LoggerE("AppControl::AddData Fail");
-    return false;
-  } else {
-    return true;
-  }
+  return BundleAddData(app_control_bundle_, key, value);
 }
 
+
+
 bool AppControl::AddDataArray(const std::string& key,
                               const std::vector<std::string>& value_array) {
-  int n = value_array.size();
-  std::vector<const char*> v;
-  std::for_each(value_array.begin(), value_array.end(),
-                [&v] (std::string str) {
-                  v.push_back(static_cast<const char*>(str.c_str()));
-                });
+  return BundleAddDataArray(app_control_bundle_, key, value_array);
+}
 
-  int result = appsvc_add_data_array(app_control_bundle_, key.c_str(),
-                                     v.data(), n);
-  if (result < 0) {
-    LoggerE("AppControl::AddDataArray Fail");
+
+bool AppControl::Reply(const std::map<std::string,
+                                      std::vector<std::string>>& data) {
+  bundle* result;
+  if (appsvc_create_result_bundle(app_control_bundle_,
+                                  &result) != APPSVC_RET_OK) {
+    LoggerE("AppControl::Reply Fail : fail to create result bundle");
     return false;
-  } else {
-    return true;
   }
+  auto it = data.begin();
+  for ( ; it != data.end(); ++it) {
+    const std::string& key = it->first;
+    if (it->second.size() == 1) {
+      BundleAddData(result, key, it->second[0]);
+    } else {
+      BundleAddDataArray(result, key, it->second);
+    }
+  }
+
+  int ret = appsvc_send_result(result, APPSVC_RES_OK);
+  bundle_free(result);
+
+  return ret == APPSVC_RET_OK ? true : false;
 }
 
 }  // namespace wrt
index 2ac6f84..94c80df 100755 (executable)
@@ -9,6 +9,7 @@
 #include <bundle.h>
 #include <string>
 #include <vector>
+#include <map>
 
 namespace wrt {
 
@@ -32,6 +33,7 @@ class AppControl {
   bool AddData(const std::string& key, const std::string& value);
   bool AddDataArray(const std::string& key,
                     const std::vector<std::string>& value_array);
+  bool Reply(const std::map<std::string, std::vector<std::string>>& data);
 
  private:
   app_control_h app_control_;