Merge branch 'tizen_5.5' into tizen
[platform/core/uifw/capi-ui-sticker.git] / server / stickerd_data_manager.c
index 7b67475..25863a1 100644 (file)
@@ -44,6 +44,7 @@
 
 static GHashTable *_monitoring_hash = NULL;
 static char error_buffer[MAX_ERROR_BUFFER];
+static GList *consumer_list = NULL;
 extern GMainLoop *main_loop;
 
 static void _check_watcher_exist()
@@ -52,6 +53,8 @@ static void _check_watcher_exist()
         LOGD("Terminate sticker daemon");
         g_hash_table_destroy(_monitoring_hash);
         _monitoring_hash = NULL;
+        g_list_free_full(consumer_list, free);
+        consumer_list = NULL;
         g_main_loop_quit(main_loop);
     }
 }
@@ -77,6 +80,9 @@ static void _on_name_vanished(GDBusConnection *connection,
             delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
         }
 
+        if (g_list_find(consumer_list, info->bus_name))
+            consumer_list = g_list_remove(consumer_list, info->bus_name);
+
         if (info->bus_name)
             free(info->bus_name);
         free(info);
@@ -95,14 +101,17 @@ static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, con
     if (_monitoring_hash == NULL)
         _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
 
+    if (consumer_list == NULL)
+        consumer_list = g_list_alloc();
+
     GVariant *reply_body = NULL;
     int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
 
     if (g_strcmp0(method_name, "sticker_service_register") == 0) {
         ret = stickerd_server_register(parameters, &reply_body, sender,
-            _on_name_appeared, _on_name_vanished, &_monitoring_hash);
+            _on_name_appeared, _on_name_vanished, &_monitoring_hash, &consumer_list);
     } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
-        ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash);
+        ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash, &consumer_list);
     } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
         ret = stickerd_insert_sticker_info(parameters, &reply_body);
     } else if  (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
@@ -153,6 +162,8 @@ static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, con
         ret = stickerd_insert_recent_sticker_info(parameters, &reply_body);
     } else if (g_strcmp0(method_name, "get_recent_sticker_info") == 0) {
         ret = stickerd_get_recent_sticker_info(parameters, &reply_body);
+    } else if (g_strcmp0(method_name, "send_update_event") == 0) {
+        ret = stickerd_send_update_event(parameters, &reply_body);
     }
 
     if (ret == STICKERD_SERVER_ERROR_NONE) {
@@ -178,10 +189,12 @@ int stickerd_register_dbus_interface(void)
             "  <node>"
             "  <interface name='org.tizen.sticker_service'>"
             "        <method name='sticker_service_register'>"
+            "          <arg type='i' name='lib_type' direction='in'/>"
             "          <arg type='i' name='watcher_id' direction='out'/>"
             "        </method>"
 
             "        <method name='sticker_service_unregister'>"
+            "          <arg type='i' name='lib_type' direction='in'/>"
             "          <arg type='i' name='watcher_id' direction='in'/>"
             "        </method>"
 
@@ -328,6 +341,10 @@ int stickerd_register_dbus_interface(void)
             "          <arg type='i' name='count' direction='in'/>"
             "          <arg type='a(i)' name='id_list' direction='out'/>"
             "        </method>"
+
+            "        <method name='send_update_event'>"
+            "          <arg type='i' name='record_id' direction='in'/>"
+            "        </method>"
             "  </interface>"
             "  </node>";
 
@@ -545,6 +562,70 @@ cleanup:
     }
 }
 
+static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
+{
+    if (!keyword) {
+        LOGE("keyword doesn't exist");
+        return;
+    }
+
+    g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
+}
+
+static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
+{
+    GVariantBuilder *info_builder;
+    GVariantBuilder *keyword_builder;
+
+    info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
+    g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
+
+    keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
+    g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
+
+    GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
+    g_variant_builder_unref(info_builder);
+    g_variant_builder_unref(keyword_builder);
+
+    if (body)
+        return body;
+    else
+        return NULL;
+}
+
+void _get_consumer_busname(gpointer data, gpointer user_data)
+{
+    if (data == NULL)
+        return;
+
+    int ret;
+    char *cmd = "send_sticker_changed_event";
+    char *sender = (char *)data;
+    GVariant *body = (GVariant *)user_data;
+
+    ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
+    if (ret != STICKERD_SERVER_ERROR_NONE)
+        LOGE("Failed to send sticker changed event");
+}
+
+static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
+{
+    GVariant *body = _get_sticker_g_variant(type, sticker_info);
+
+    if (body)
+        g_list_foreach(consumer_list, _get_consumer_busname, body);
+
+    if (body)
+        g_variant_unref(body);
+}
+
 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
 {
     int ret;
@@ -642,7 +723,8 @@ int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
     if (*reply_body == NULL) {
         LOGE("Failed to create reply_body");
         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
-    }
+    } else
+        _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
 
 cleanup:
     if (value)
@@ -774,7 +856,7 @@ int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_
                 goto free_memory;
 
             sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
-            if (sticker_info->thumbnail) {
+            if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
                 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
                     sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
                     if (!sticker_info->thumbnail)
@@ -801,7 +883,8 @@ int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_
             if (ret != STICKERD_SERVER_ERROR_NONE) {
                 LOGE("Failed to insert sticker info");
                 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
-            }
+            } else
+                _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
 
 free_memory:
             free(sticker_info);
@@ -840,10 +923,20 @@ int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
 
     g_variant_get(parameters, "(i)", &record_id);
 
+    sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
+    if (sticker_info)
+        stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
+
     ret = stickerd_db_delete_sticker_info(record_id);
     if (ret != STICKERD_SERVER_ERROR_NONE) {
         LOGE("Failed to delete sticker info");
         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
+    } else {
+        if (sticker_info && sticker_info->uri) {
+            _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
+            free(sticker_info);
+            sticker_info = NULL;
+        }
     }
 
     return ret;
@@ -862,10 +955,20 @@ int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body
 
     g_variant_get(parameters, "(&s)", &uri);
 
+    sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
+    if (sticker_info)
+        stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
+
     ret = stickerd_db_delete_sticker_info_by_uri(uri);
     if (ret != STICKERD_SERVER_ERROR_NONE) {
         LOGE("Failed to delete sticker info");
         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
+    } else {
+        if (sticker_info && sticker_info->uri) {
+            _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
+            free(sticker_info);
+            sticker_info = NULL;
+        }
     }
 
     return ret;
@@ -1047,16 +1150,6 @@ int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
     return ret;
 }
 
-static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
-{
-    if (!keyword) {
-        LOGE("keyword doesn't exist");
-        return;
-    }
-
-    g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
-}
-
 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
 {
     int ret;
@@ -1635,4 +1728,30 @@ int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body
         g_variant_builder_unref(id_builder);
 
     return ret;
+}
+
+int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
+{
+    int ret = STICKERD_SERVER_ERROR_NONE;
+    int record_id;
+
+    *reply_body = g_variant_new("()");
+    if (*reply_body == NULL) {
+        LOGE("Failed to create reply_body");
+        return STICKERD_SERVER_ERROR_OPERATION_FAILED;
+    }
+
+    g_variant_get(parameters, "(i)", &record_id);
+
+    sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
+    if (sticker_info) {
+        ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
+        if (ret == STICKERD_SERVER_ERROR_NONE) {
+            _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
+            free(sticker_info);
+            sticker_info = NULL;
+        }
+    }
+
+    return ret;
 }
\ No newline at end of file