Prevent duplicated function call
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
index 9b214bc..b45ddf9 100644 (file)
@@ -42,6 +42,7 @@
 #include "appcore_base.h"
 #include "appcore_base_private.h"
 #include "appcore_watchdog.h"
+#include "appcore_base_control.h"
 
 #define PATH_LOCALE "locale"
 #define RESOURCED_FREEZER_PATH "/Org/Tizen/ResourceD/Freezer"
@@ -66,6 +67,7 @@ typedef struct _appcore_base_event_node {
        int type;
        appcore_base_event_cb cb;
        void *data;
+       void *prev_event;
 } appcore_base_event_node;
 
 typedef struct _appcore_base_rotation {
@@ -128,6 +130,70 @@ appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
        return profile;
 }
 
+static int __compare_event(void *prev_event, void *curr_event, int type)
+{
+       char *curr_strval;
+       char *prev_strval;
+       int curr_intval;
+       int prev_intval;
+       int ret = -1;
+
+       switch (type) {
+       case APPCORE_BASE_EVENT_LOW_MEMORY:
+       case APPCORE_BASE_EVENT_LOW_BATTERY:
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+       case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
+               prev_intval = GPOINTER_TO_INT(prev_event);
+               curr_intval = *(int *)curr_event;
+               if (prev_intval == curr_intval)
+                       ret = 0;
+               break;
+       case APPCORE_BASE_EVENT_LANG_CHANGE:
+       case APPCORE_BASE_EVENT_REGION_CHANGE:
+               prev_strval = prev_event;
+               curr_strval = curr_event;
+               if (prev_strval && curr_strval &&
+                               !strcmp(prev_strval, curr_strval))
+                       ret = 0;
+               break;
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+static void __unset_prev_event(void **prev_event, int type)
+{
+       if (type == APPCORE_BASE_EVENT_LANG_CHANGE ||
+                       type == APPCORE_BASE_EVENT_REGION_CHANGE)
+               free(*prev_event);
+       *prev_event = NULL;
+}
+
+static void __set_prev_event(void **prev_event, void *curr_event, int type)
+{
+       int curr_intval;
+       char *curr_strval;
+
+       switch (type) {
+       case APPCORE_BASE_EVENT_LOW_MEMORY:
+       case APPCORE_BASE_EVENT_LOW_BATTERY:
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+       case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
+               curr_intval = *(int *)curr_event;
+               *prev_event = GINT_TO_POINTER(curr_intval);
+               break;
+       case APPCORE_BASE_EVENT_LANG_CHANGE:
+       case APPCORE_BASE_EVENT_REGION_CHANGE:
+               curr_strval = curr_event;
+               if (curr_strval)
+                       *prev_event = strdup(curr_strval);
+               break;
+       default:
+               break;
+       }
+}
 
 static void __invoke_callback(void *event, int type)
 {
@@ -135,10 +201,16 @@ static void __invoke_callback(void *event, int type)
 
        while (iter) {
                appcore_base_event_node *node = iter->data;
+               iter = g_list_next(iter);
 
-               if (node->type == type)
+               if (node->type != type)
+                       continue;
+
+               if (__compare_event(node->prev_event, event, type)) {
                        node->cb(event, node->data);
-               iter = g_list_next(iter);
+                       __unset_prev_event(&node->prev_event, type);
+                       __set_prev_event(&node->prev_event, event, type);
+               }
        }
 }
 
@@ -669,6 +741,7 @@ static void __update_lang(void)
                }
                setenv("LANG", lang, 1);
                setenv("LC_MESSAGES", lang, 1);
+               setenv("LC_ALL", lang, 1);
                r = setlocale(LC_ALL, "");
                if (r == NULL) {
                        r = setlocale(LC_ALL, "en_US.UTF-8");
@@ -1119,6 +1192,7 @@ EXPORT_API void appcore_base_fini(void)
                }
        }
 
+       appcore_base_control_fini();
        __unset_default_events();
 
        if (__context.sid) {
@@ -1194,8 +1268,10 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
                if (__context.ops.exit)
                        __context.ops.exit(__context.data);
                break;
+       case AUL_TERMINATE_INST:
+       case AUL_TERMINATE_BG_INST:
        case AUL_TERMINATE_BGAPP:
-               _DBG("[APP %d]     AUL event: AUL_TERMINATE_BGAPP", getpid());
+               _DBG("[APP %d]     AUL event: %d", getpid(), type);
                if (!__context.allowed_bg)
                        __remove_suspend_timer();
                break;
@@ -1238,7 +1314,7 @@ EXPORT_API int appcore_base_on_create(void)
 {
        int r;
 
-       r = aul_launch_init(__context.ops.receive, NULL);
+       r = aul_launch_init(__context.ops.receive, __context.data);
        if (r < 0) {
                _ERR("Aul init failed: %d", r);
                return -1;
@@ -1255,6 +1331,8 @@ EXPORT_API int appcore_base_on_create(void)
 
 EXPORT_API int appcore_base_on_control(bundle *b)
 {
+       appcore_base_control_invoke(b);
+
        return 0;
 }
 
@@ -1331,6 +1409,7 @@ EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event e
        node->cb = cb;
        node->type = event;
        node->data = data;
+       node->prev_event = NULL;
        __events = g_list_append(__events, node);
 
        return node;
@@ -1346,6 +1425,7 @@ EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
 
        event = node->type;
        __events = g_list_remove(__events, node);
+       __unset_prev_event(&node->prev_event, event);
        free(node);
        if (__context.dirty && !__exist_callback(event)) {
                if (__context.ops.unset_event)