Support the event callback to send sticker DB changes
[platform/core/uifw/capi-ui-sticker.git] / client / sticker_dbus.c
index f8a4b72..412a143 100644 (file)
@@ -26,6 +26,7 @@
 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
 
 static int is_server_started = 0;
+static CLIENT_LIB last_req_lib = STICKER_CLIENT_LIB_NONE;
 
 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
 {
@@ -74,18 +75,14 @@ static int _dbus_init(GDBusConnection **gdbus_connection)
     return STICKER_CLIENT_ERROR_NONE;
 }
 
-static void _get_sticker_info_from_gvariant(GVariant *body, sticker_data_h sticker_data)
+static void _get_sticker_info_from_gvariant(GVariantIter *info_iter, GVariantIter *keyword_iter, sticker_data_h sticker_data)
 {
     STICKER_DAT_TYPE key;
     GVariant *value = NULL;
-    GVariantIter *info_iter = NULL;
-    GVariantIter *keyword_iter = NULL;
     char *keyword = NULL;
 
-    g_variant_get(body, "(a{iv}a(s))", &info_iter, &keyword_iter);
-
     if (!info_iter || !keyword_iter) {
-        LOGD("failed to get iter");
+        LOGW("failed to get iter");
         return;
     }
 
@@ -206,6 +203,29 @@ static void _handle_sticker_consumer_cb(GDBusConnection *connection,
         return;
     }
 
+    if (g_strcmp0(signal_name, "send_sticker_changed_event") == 0) {
+        if (consumer_handle->event_cb != NULL) {
+            sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
+            if (sticker_data) {
+                int event_type;
+                GVariantIter *info_iter = NULL;
+                GVariantIter *keyword_iter = NULL;
+
+                g_variant_get(parameters, "(ia{iv}a(s))", &event_type, &info_iter, &keyword_iter);
+                _get_sticker_info_from_gvariant(info_iter, keyword_iter, sticker_data);
+                consumer_handle->event_cb((sticker_consumer_event_type_e)event_type, sticker_data, consumer_handle->event_cb_user_data);
+
+                if (info_iter)
+                    g_variant_iter_free(info_iter);
+
+                if (keyword_iter)
+                    g_variant_iter_free(keyword_iter);
+
+                _free_sticker_data(sticker_data);
+            }
+        }
+    }
+
     #if 0 // Receive the sticker information by asynchronous communication.
     if (g_strcmp0(signal_name, "send_group_list") == 0) {
         if (consumer_handle->group_foreach_cb != NULL)
@@ -446,14 +466,13 @@ static int _send_async_message(GDBusConnection *gdbus_connection, GVariant *body
     return ret;
 }
 
-static int _monitor_register(GDBusConnection *gdbus_connection, int *server_watcher_id)
+static int _monitor_register(GDBusConnection *gdbus_connection, int *server_watcher_id, CLIENT_LIB lib)
 {
     int ret;
     GDBusMessage *reply = NULL;
     GVariant *reply_body = NULL;
 
-    ret = _send_sync_message(gdbus_connection, g_variant_new("()"), &reply, "sticker_service_register");
-
+    ret = _send_sync_message(gdbus_connection, g_variant_new("(i)", (int)lib), &reply, "sticker_service_register");
     if (ret != STICKER_CLIENT_ERROR_NONE) {
         LOGE("_send_sync_message() failed : %d", ret);
         return ret;
@@ -476,7 +495,7 @@ static void _on_name_appeared(GDBusConnection *connection,
 {
     if (is_server_started == 0) {
         int *watcher_id = (int *)user_data;
-        _monitor_register(connection, watcher_id);
+        _monitor_register(connection, watcher_id, last_req_lib);
     }
 }
 
@@ -493,6 +512,7 @@ int sticker_dbus_init(GDBusConnection **gdbus_connection, int *server_watcher_id
                       int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, void *data)
 {
     int ret;
+    last_req_lib = lib;
 
     ret = _dbus_init(gdbus_connection);
     if (ret != STICKER_CLIENT_ERROR_NONE) {
@@ -506,7 +526,7 @@ int sticker_dbus_init(GDBusConnection **gdbus_connection, int *server_watcher_id
         return ret;
     }
 
-    ret = _monitor_register(*gdbus_connection, server_watcher_id);
+    ret = _monitor_register(*gdbus_connection, server_watcher_id, lib);
     if (ret != STICKER_CLIENT_ERROR_NONE) {
         LOGE("_monitor_register() failed : %d", ret);
         return ret;
@@ -532,12 +552,12 @@ int sticker_dbus_init(GDBusConnection **gdbus_connection, int *server_watcher_id
     return STICKER_CLIENT_ERROR_NONE;
 }
 
-int sticker_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_watcher_id, int *server_monitor_id, int *monitor_id)
+int sticker_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_watcher_id, int *server_monitor_id, int *monitor_id, CLIENT_LIB lib)
 {
     int ret;
 
     if (server_watcher_id) {
-        ret = _send_async_message(gdbus_connection, g_variant_new("(i)", *server_watcher_id), "sticker_service_unregister");
+        ret = _send_async_message(gdbus_connection, g_variant_new("(ii)", (int)lib, *server_watcher_id), "sticker_service_unregister");
         if (ret != STICKER_CLIENT_ERROR_NONE) {
             LOGE("Failed to unregister sticker service");
             return ret;
@@ -663,6 +683,7 @@ int sticker_dbus_delete_sticker_info_by_uri(GDBusConnection *gdbus_connection, c
 int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_data_h sticker_data)
 {
     int ret;
+    bool is_updated = false;
     GDBusMessage *reply = NULL;
     GVariant *reply_body = NULL;
     sticker_data_h origin_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
@@ -675,7 +696,17 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
     ret = _send_sync_message(gdbus_connection, g_variant_new("(i)", sticker_data->sticker_info_id), &reply, "get_sticker_info");
     if (ret == STICKER_CLIENT_ERROR_NONE) {
         reply_body = g_dbus_message_get_body(reply);
-        _get_sticker_info_from_gvariant(reply_body, origin_data);
+        GVariantIter *info_iter = NULL;
+        GVariantIter *keyword_iter = NULL;
+
+        g_variant_get(reply_body, "(a{iv}a(s))", &info_iter, &keyword_iter);
+        _get_sticker_info_from_gvariant(info_iter, keyword_iter, origin_data);
+
+        if (info_iter)
+            g_variant_iter_free(info_iter);
+
+        if (keyword_iter)
+            g_variant_iter_free(keyword_iter);
     } else {
         LOGW("failed to get sticker info");
         free(origin_data);
@@ -721,6 +752,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
                 free(conv_path);
                 goto cleanup;
             }
+            is_updated = true;
         }
         free(conv_path);
     }
@@ -732,6 +764,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
             LOGE("failed to update sticker type");
             goto cleanup;
         }
+        is_updated = true;
     }
 
     if (sticker_data->thumbnail) {
@@ -749,6 +782,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
                     free(conv_path);
                     goto cleanup;
                 }
+                is_updated = true;
             }
             free(conv_path);
         }
@@ -761,6 +795,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
             LOGE("failed to update sticker description");
             goto cleanup;
         }
+        is_updated = true;
     }
 
     if (sticker_data->group && strcmp(sticker_data->group, origin_data->group) != 0) {
@@ -770,6 +805,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
             LOGE("failed to update sticker group");
             goto cleanup;
         }
+        is_updated = true;
     }
 
     if (sticker_data->disp_type != 0 && sticker_data->disp_type != origin_data->disp_type) {
@@ -779,6 +815,7 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
             LOGE("failed to update sticker display type");
             goto cleanup;
         }
+        is_updated = true;
     }
 
     if (sticker_data->keyword) {
@@ -788,9 +825,14 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
         ret = _send_sync_message(gdbus_connection, g_variant_new("(ia(s))", sticker_data->sticker_info_id, keyword_builder), &reply, "update_sticker_keyword");
         if (ret != STICKER_CLIENT_ERROR_NONE)
             LOGE("failed to update sticker keyword");
+        else
+            is_updated = true;
         g_variant_builder_unref(keyword_builder);
     }
 
+    if (is_updated)
+        ret = _send_sync_message(gdbus_connection, g_variant_new("(i)", sticker_data->sticker_info_id), &reply, "send_update_event");
+
 cleanup:
     _free_sticker_data(origin_data);
 
@@ -808,15 +850,25 @@ int sticker_dbus_get_sticker_info_by_record_id(GDBusConnection *gdbus_connection
     int ret;
     GDBusMessage *reply = NULL;
     GVariant *reply_body = NULL;
+    GVariantIter *info_iter = NULL;
+    GVariantIter *keyword_iter = NULL;
 
     ret = _send_sync_message(gdbus_connection, g_variant_new("(i)", record_id), &reply, "get_sticker_info");
     if (ret == STICKER_CLIENT_ERROR_NONE) {
         reply_body = g_dbus_message_get_body(reply);
         sticker_data->sticker_info_id = record_id;
-        _get_sticker_info_from_gvariant(reply_body, sticker_data);
+
+        g_variant_get(reply_body, "(a{iv}a(s))", &info_iter, &keyword_iter);
+        _get_sticker_info_from_gvariant(info_iter, keyword_iter, sticker_data);
 
         if (reply_body)
             g_variant_unref(reply_body);
+
+        if (info_iter)
+            g_variant_iter_free(info_iter);
+
+        if (keyword_iter)
+            g_variant_iter_free(keyword_iter);
     }
 
     if (reply)