Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / aul_svc.cc
index 7e2956c..389dc50 100644 (file)
@@ -28,7 +28,9 @@
 
 #include <atomic>
 #include <memory>
+#include <mutex>
 #include <string>
+#include <unordered_map>
 #include <vector>
 
 #include "aul/app_control/resolve_info.hh"
@@ -92,6 +94,54 @@ class AliasInfo {
   std::string appid_;
 };
 
+class AppSvcAlias {
+ public:
+  AppSvcAlias() = default;
+
+  void Load() {
+    std::lock_guard<std::mutex> lock(mutex_);
+    if (loaded_)
+      return;
+
+    dictionary* dict = iniparser_load("/usr/share/appsvc/alias.ini");
+    if (dict == nullptr) {
+      loaded_ = true;
+      return;
+    }
+
+    std::string delimiter = "Alias:";
+    for (int i = 0; i < dict->n; ++i) {
+      if (dict->key[i] == nullptr || dict->val[i] == nullptr)
+        continue;
+
+      std::string key = dict->key[i];
+      key.erase(0, key.find(delimiter) + delimiter.length() + 1);
+      std::string value = dict->val[i];
+      map_[key] = value;
+    }
+
+    iniparser_freedict(dict);
+    loaded_ = true;
+  }
+
+  std::string GetAliasAppId(const std::string& alias_appid) {
+    auto found = map_.find(alias_appid);
+    if (found == map_.end())
+      return {};
+
+    SECURE_LOGD("alias_appid: %s, appid: %s",
+        alias_appid.c_str(), found->second.c_str());
+    return found->second;
+  }
+
+ private:
+  bool loaded_ = false;
+  std::unordered_map<std::string, std::string> map_;
+  std::mutex mutex_;
+};
+
+AppSvcAlias appsvc_alias;
+
 int SetBundle(bundle* b, const char* key, const char* value) {
   if (bundle_get_type(b, key) != BUNDLE_TYPE_NONE) {
     if (bundle_del(b, key) != BUNDLE_ERROR_NONE)
@@ -127,20 +177,8 @@ int SetBundleArray(bundle* b, const char* key, const char** value,
 }
 
 std::string GetAliasAppId(const char* appid) {
-  dictionary* dic = iniparser_load("/usr/share/appsvc/alias.ini");
-  if (dic == nullptr)
-    return {};
-
-  auto dic_ptr = std::unique_ptr<dictionary, decltype(iniparser_freedict)*>(
-      dic, iniparser_freedict);
-
-  std::string key = std::string("Alias:") + appid;
-  const char* value = iniparser_getstring(dic, key.c_str(), nullptr);
-  SECURE_LOGD("appid(%s), alias_id(%s)", appid, value);
-  if (value == nullptr)
-    return {};
-
-  return std::string(value);
+  appsvc_alias.Load();
+  return appsvc_alias.GetAliasAppId(appid);
 }
 
 bool IsSpecialApp(const char* appid) {
@@ -219,6 +257,8 @@ int AulErrorConvert(int res) {
     return AUL_SVC_RET_ENOMATCH;
   case AUL_R_ECANCELED:
     return AUL_SVC_RET_ECANCELED;
+  case AUL_R_ETIMEOUT:
+    return AUL_SVC_RET_ETIMEOUT;
   default:
     return AUL_SVC_RET_ELAUNCH;
   }
@@ -458,9 +498,11 @@ extern "C" API int aul_svc_set_appid(bundle* b, const char* appid) {
     return AUL_SVC_RET_EINVAL;
   }
 
-  std::string alias_id = ::GetAliasAppId(appid);
-  if (!alias_id.empty())
-    appid = alias_id.c_str();
+  if (TIZEN_FEATURE_APPSVC_ALIAS) {
+    std::string alias_id = ::GetAliasAppId(appid);
+    if (!alias_id.empty())
+      return ::SetBundle(b, AUL_SVC_K_PKG_NAME, alias_id.c_str());
+  }
 
   return ::SetBundle(b, AUL_SVC_K_PKG_NAME, appid);
 }
@@ -1191,6 +1233,58 @@ extern "C" API void aul_svc_free_appid_array(char** appid_array,
   free(appid_array);
 }
 
+extern "C" API int aul_svc_set_window_position(bundle* b,
+    int x, int y, int w, int h) {
+  if (b == nullptr) {
+    _E("Invalid parameter");
+    return AUL_SVC_RET_EINVAL;
+  }
+
+  ::SetBundle(b, AUL_K_HINT_SCREEN_POS_X, std::to_string(x).c_str());
+  ::SetBundle(b, AUL_K_HINT_SCREEN_POS_Y, std::to_string(y).c_str());
+  ::SetBundle(b, AUL_K_HINT_SCREEN_WIDTH, std::to_string(w).c_str());
+  ::SetBundle(b, AUL_K_HINT_SCREEN_HEIGHT, std::to_string(h).c_str());
+
+  return AUL_SVC_RET_OK;
+}
+
+extern "C" API int aul_svc_get_window_position(bundle* b,
+    int* x, int* y, int* w, int* h) {
+  if (b == nullptr ||
+      x == nullptr ||
+      y == nullptr ||
+      w == nullptr ||
+      h == nullptr) {
+    _E("Invalid parameter");
+    return AUL_SVC_RET_EINVAL;
+  }
+
+  char* x_str = nullptr;
+  char* y_str = nullptr;
+  char* w_str = nullptr;
+  char* h_str = nullptr;
+
+  bundle_get_str(b , AUL_K_HINT_SCREEN_POS_X, &x_str);
+  bundle_get_str(b , AUL_K_HINT_SCREEN_POS_Y, &y_str);
+  bundle_get_str(b , AUL_K_HINT_SCREEN_WIDTH, &w_str);
+  bundle_get_str(b , AUL_K_HINT_SCREEN_HEIGHT, &h_str);
+
+  if (x_str == nullptr ||
+      y_str == nullptr ||
+      w_str == nullptr ||
+      h_str == nullptr) {
+    _E("failed to get position");
+    return AUL_SVC_RET_ERROR;
+  }
+
+  *x = atoi(x_str);
+  *y = atoi(y_str);
+  *w = atoi(w_str);
+  *h = atoi(h_str);
+
+  return AUL_SVC_RET_OK;
+}
+
 extern "C" API int aul_svc_set_defapp(const char* op, const char* mime_type,
     const char* uri, const char* defapp) {
   DEPRECATION_WARNING();