merge vui
[apps/agl-service-homescreen.git] / src / hs-appinfo.cpp
index e1c1990..6b22265 100644 (file)
@@ -15,8 +15,9 @@
  */
 
 #include <unistd.h>
+#include <cstring>
 #include "hs-appinfo.h"
-#include "hmi-debug.h"
+#include "hs-helper.h"
 #include "hs-clientmanager.h"
 
 #define RETRY_CNT 10
@@ -31,6 +32,28 @@ const char _keyStart[] = "start";
 const char _keyApplistChanged[] = "application-list-changed";
 
 HS_AppInfo* HS_AppInfo::me = nullptr;
+const std::unordered_map<std::string, HS_AppInfo::func_handler> HS_AppInfo::concerned_event_list {
+    {"afm-main/application-list-changed",    &HS_AppInfo::updateAppDetailList}
+};
+
+
+/**
+ * event hook function
+ *
+ * #### Parameters
+ *  - api : the api serving the request
+ *  - event  : event name
+ *  - object : event json object
+ *
+ * #### Return
+ * 0 : continue transfer
+ * 1 : blocked
+ *
+ */
+static int eventHandler(afb_api_t api, const char *event, struct json_object *object)
+{
+    return HS_AppInfo::instance()->onEvent(api, event, object);
+}
 
 /**
  * get application property function
@@ -47,7 +70,7 @@ std::string AppDetail::getProperty(std::string key) const
     struct json_object *j_obj;
     struct json_object *j_detail = json_tokener_parse(this->detail.c_str());
     if(json_object_object_get_ex(j_detail, key.c_str(), &j_obj) == 0) {
-        HMI_ERROR("homescreen-service","can't find key=%s.", key.c_str());
+        AFB_WARNING("can't find key=%s.", key.c_str());
         return std::string();
     }
     return std::string(json_object_get_string(j_obj));
@@ -102,7 +125,7 @@ int HS_AppInfo::init(afb_api_t api)
 {
     afmmain = new HS_AfmMainProxy();
     if(afmmain == nullptr) {
-        HMI_ERROR("homescreen-service","Fatal Error:new HS_AfmMainProxy failed");
+        AFB_ERROR("new HS_AfmMainProxy failed");
         return -1;
     }
 
@@ -117,14 +140,18 @@ int HS_AppInfo::init(afb_api_t api)
 
         ++retry;
         if(retry == RETRY_CNT) {
-            HMI_ERROR("homescreen-service","get runnables list failed");
+            AFB_ERROR("get runnables list failed");
             json_object_put(j_runnable);
             return -1;
         }
-        HMI_NOTICE("homescreen-service","retry to get runnables list %d", retry);
+        AFB_DEBUG("retry to get runnables list %d", retry);
         usleep(100000); // 100ms
     } while(1);
 
+    for(auto &ref : concerned_event_list) {
+        setEventHook(ref.first.c_str(), eventHandler);
+    }
+
     return 0;
 }
 
@@ -137,16 +164,17 @@ int HS_AppInfo::init(afb_api_t api)
  *  - object : event json object
  *
  * #### Return
- * None
- *
+ * 0 : continue transfer
+ * 1 : blocked
  */
-void HS_AppInfo::onEvent(afb_api_t api, const char *event, struct json_object *object)
+int HS_AppInfo::onEvent(afb_api_t api, const char *event, struct json_object *object)
 {
+    int ret = 0;
     auto ip = concerned_event_list.find(std::string(event));
     if(ip != concerned_event_list.end()) {
-        HMI_NOTICE("homescreen-service","[%s] event received.", event);
-        (this->*(ip->second))(api, object);
+        ret = (this->*(ip->second))(api, object);
     }
+    return ret;
 }
 
 /**
@@ -161,7 +189,7 @@ void HS_AppInfo::onEvent(afb_api_t api, const char *event, struct json_object *o
  */
 void HS_AppInfo::createAppDetailList(struct json_object *object)
 {
-    HMI_NOTICE("homescreen-service","applist:%s", json_object_to_json_string(object));
+    AFB_DEBUG("applist:%s", json_object_to_json_string(object));
 
     if(json_object_get_type(object) ==  json_type_array) {
         int array_len = json_object_array_length(object);
@@ -171,7 +199,7 @@ void HS_AppInfo::createAppDetailList(struct json_object *object)
         }
     }
     else {
-        HMI_ERROR("homescreen-service","Apps information input error.");
+        AFB_ERROR("Apps information input error.");
     }
 }
 
@@ -182,29 +210,30 @@ void HS_AppInfo::createAppDetailList(struct json_object *object)
  *  - object : the detail of all applications
  *
  * #### Return
- * None
+ * 0 : continue transfer
+ * 1 : blocked
  *
  */
-void HS_AppInfo::updateAppDetailList(afb_api_t api, struct json_object *object)
+int HS_AppInfo::updateAppDetailList(afb_api_t api, struct json_object *object)
 {
-    HMI_NOTICE("homescreen-service","update:%s", json_object_to_json_string(object));
+    AFB_DEBUG("update:%s", json_object_to_json_string(object));
     if(json_object_get_type(object) != json_type_object) {
-        HMI_ERROR("homescreen-service","input detail object error.");
-        return;
+        AFB_ERROR("input detail object error.");
+        return 1;
     }
 
     struct json_object *obj_oper, *obj_data;
     if(json_object_object_get_ex(object, _keyOperation, &obj_oper) == 0
     ||  json_object_object_get_ex(object, _keyData, &obj_data) == 0) {
-        HMI_ERROR("homescreen-service","can't find key=%s, %s.", _keyOperation, _keyData);
-        return;
+        AFB_ERROR("can't find key=%s, %s.", _keyOperation, _keyData);
+        return 1;
     }
 
     std::string id = json_object_get_string(obj_data);
     std::string appid = id2appid(id);
     if(isPeripheryApp(appid.c_str())) {
-        HMI_NOTICE("homescreen-service", "install/uninstall application is periphery.");
-        return;
+        AFB_INFO( "install/uninstall application is periphery.");
+        return 1;
     }
 
     std::string oper = json_object_get_string(obj_oper);
@@ -214,30 +243,31 @@ void HS_AppInfo::updateAppDetailList(afb_api_t api, struct json_object *object)
         if(!ret) {
             struct json_object *j_found = retrieveRunnables(j_runnable, id);
             if(j_found == nullptr) {
-                HMI_NOTICE("homescreen-service", "installed application isn't runnables.");
+                AFB_INFO( "installed application isn't runnables.");
                 json_object_put(j_runnable);
-                return;
+                return 1;
             }
             addAppDetail(j_found);
             pushAppListChangedEvent(_keyInstall, j_found);
         }
         else {
-            HMI_ERROR("homescreen-service","get runnalbes failed.");
+            AFB_ERROR("get runnalbes failed.");
         }
         json_object_put(j_runnable);
     }
     else if(oper == _keyUninstall) {
         std::string appid_checked = checkAppId(appid);
         if(appid_checked.empty()) {
-            HMI_NOTICE("homescreen-service","uninstalled application isn't in runnables list, appid=%s.", appid.c_str());
-            return;
+            AFB_INFO("uninstalled application isn't in runnables list, appid=%s.", appid.c_str());
+            return 1;
         }
         pushAppListChangedEvent(_keyUninstall, obj_data);
         removeAppDetail(appid);
     }
     else {
-        HMI_ERROR("homescreen-service","operation error.");
+        AFB_ERROR("operation error.");
     }
+    return 1;
 }
 
 /**
@@ -256,7 +286,7 @@ std::string HS_AppInfo::parseAppDetail(struct json_object *object, AppDetail &in
     struct json_object *name, *id;
     if(json_object_object_get_ex(object, _keyName, &name) == 0
     || json_object_object_get_ex(object, _keyId, &id) == 0) {
-        HMI_ERROR("homescreen-service","can't find key=%s, %s.", _keyName, _keyId);
+        AFB_ERROR("can't find key=%s, %s.", _keyName, _keyId);
         return std::string();
     }
     std::string appid = id2appid(json_object_get_string(id));
@@ -285,7 +315,7 @@ void HS_AppInfo::addAppDetail(struct json_object *object)
     AppDetail info;
     std::string appid = parseAppDetail(object, info);
     if(appid.empty()) {
-        HMI_ERROR("homescreen-service","application id error");
+        AFB_ERROR("application id error");
         return;
     }
 
@@ -315,7 +345,7 @@ void HS_AppInfo::removeAppDetail(std::string appid)
         app_detail_list.erase(it);
     }
     else {
-        HMI_WARNING("homescreen-service","erase application(%s) wasn't in applist.", appid.c_str());
+        AFB_WARNING("erase application(%s) wasn't in applist.", appid.c_str());
     }
 }
 
@@ -332,7 +362,6 @@ void HS_AppInfo::removeAppDetail(std::string appid)
  */
 void HS_AppInfo::pushAppListChangedEvent(const char *oper, struct json_object *object)
 {
-    HMI_NOTICE("homescreen-service","called.");
     struct json_object *push_obj = json_object_new_object();
     json_object_object_add(push_obj, _keyOperation, json_object_new_string(oper));
     json_object_object_add(push_obj, _keyData, object);
@@ -360,7 +389,7 @@ struct json_object* HS_AppInfo::retrieveRunnables(struct json_object *obj_runnab
             struct json_object *obj = json_object_array_get_idx(obj_runnables, i);
             struct json_object *j_id;
             if(json_object_object_get_ex(obj, _keyId, &j_id) == 0) {
-                HMI_WARNING("homescreen-service","can't find id.");
+                AFB_WARNING("can't find id.");
                 continue;
             }
             if(id == json_object_get_string(j_id)) {
@@ -370,7 +399,7 @@ struct json_object* HS_AppInfo::retrieveRunnables(struct json_object *obj_runnab
         }
     }
     else {
-        HMI_ERROR("homescreen-service","Apps information input error.");
+        AFB_ERROR("Apps information input error.");
     }
     return j_found;
 }
@@ -393,7 +422,7 @@ std::string HS_AppInfo::id2appid(const std::string &id) const
         appid = id.substr(0,pos);
     }
     else {
-        HMI_ERROR("homescreen-service","input id error.");
+        AFB_WARNING("input id error.");
     }
     return appid;
 }
@@ -411,7 +440,7 @@ std::string HS_AppInfo::id2appid(const std::string &id) const
 void HS_AppInfo::getRunnables(struct json_object **object)
 {
     if(json_object_get_type(*object) !=  json_type_array) {
-        HMI_ERROR("homescreen-service","json type error.");
+        AFB_ERROR("json type error.");
         return;
     }