Initial commit
[platform/core/uifw/capi-ui-sticker.git] / client / sticker_dbus.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dlog.h>
18
19 #include "sticker_dbus.h"
20
21 #ifdef LOG_TAG
22 #undef LOG_TAG
23 #endif
24 #define LOG_TAG "STICKER_DBUS"
25
26 static int is_server_started = 0;
27
28 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
29 {
30     LOGD("name : %s, name_owner : %s", name, name_owner);
31 }
32
33 static void _server_vanished_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
34 {
35     LOGD("name : %s", name);
36 }
37
38 static int _dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id)
39 {
40     GError *error = NULL;
41
42     if (*gdbus_connection == NULL) {
43         GDBusConnection *conn = NULL;
44         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
45         if (conn == NULL) {
46             if (error != NULL) {
47                 LOGE("g_bus_get_sync error message = %s", error->message);
48                 g_error_free(error);
49             }
50             return STICKER_CLIENT_ERROR_IO_ERROR;
51         }
52         *gdbus_connection = conn;
53     }
54
55     LOGD("Connected bus name : %s", g_dbus_connection_get_unique_name(*gdbus_connection));
56     if (*server_watcher_id == 0) {
57         *server_watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
58                             STICKER_DBUS_NAME,
59                             G_BUS_NAME_WATCHER_FLAGS_NONE,
60                             _server_appeared_cb,
61                             _server_vanished_cb,
62                             NULL, NULL);
63     }
64
65     LOGD("server_watcher_id : %d", *server_watcher_id);
66     if (*server_watcher_id == 0) {
67         LOGE("Failed to get identifier");
68         return STICKER_CLIENT_ERROR_IO_ERROR;
69     }
70
71     return STICKER_CLIENT_ERROR_NONE;
72 }
73
74 static void _get_sticker_info_from_gvariant(GVariant *body, sticker_data_h sticker_data)
75 {
76     STICKER_DAT_TYPE key;
77     GVariant *value = NULL;
78     GVariantIter *info_iter = NULL;
79     GVariantIter *keyword_iter = NULL;
80     char *keyword = NULL;
81
82     g_variant_get(body, "(a{iv}a(s))", &info_iter, &keyword_iter);
83
84     if (!info_iter || !keyword_iter) {
85         LOGD("failed to get iter");
86         return;
87     }
88
89     while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
90         switch(key) {
91             case STICKER_DATA_TYPE_APP_ID:
92             sticker_data->app_id = g_variant_dup_string(value, NULL);
93             break;
94             case STICKER_DATA_TYPE_URI_TYPE:
95             sticker_data->type = g_variant_get_int32(value);
96             break;
97             case STICKER_DATA_TYPE_URI:
98             sticker_data->uri = g_variant_dup_string(value, NULL);
99             break;
100             case STICKER_DATA_TYPE_THUMBNAIL:
101             sticker_data->thumbnail = g_variant_dup_string(value, NULL);
102             break;
103             case STICKER_DATA_TYPE_DESCRIPTION:
104             sticker_data->description = g_variant_dup_string(value, NULL);
105             break;
106             case STICKER_DATA_TYPE_GROUP:
107             sticker_data->group = g_variant_dup_string(value, NULL);
108             break;
109             case STICKER_DATA_TYPE_DATE:
110             sticker_data->date = g_variant_dup_string(value, NULL);
111             break;
112             default:
113             break;
114         }
115     }
116
117     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
118         sticker_data->keyword = g_list_append(sticker_data->keyword, strdup((const char *)keyword));
119     }
120
121     if (value)
122         g_variant_unref(value);
123
124     g_variant_iter_free(info_iter);
125     g_variant_iter_free(keyword_iter);
126 }
127
128 static void _free_sticker_data(sticker_data_h sticker_data)
129 {
130     if (sticker_data->app_id) {
131         free(sticker_data->app_id);
132         sticker_data->app_id = NULL;
133     }
134
135     if (sticker_data->uri) {
136         free(sticker_data->uri);
137         sticker_data->uri = NULL;
138     }
139
140     if (sticker_data->thumbnail) {
141         free(sticker_data->thumbnail);
142         sticker_data->thumbnail = NULL;
143     }
144
145     if (sticker_data->keyword) {
146         g_list_free_full(sticker_data->keyword, free);
147         sticker_data->keyword = NULL;
148     }
149
150     if (sticker_data->group) {
151         free(sticker_data->group);
152         sticker_data->group = NULL;
153     }
154
155     if (sticker_data->description) {
156         free(sticker_data->description);
157         sticker_data->description = NULL;
158     }
159
160     if (sticker_data->date) {
161         free(sticker_data->date);
162         sticker_data->date = NULL;
163     }
164
165     free(sticker_data);
166     sticker_data = NULL;
167 }
168
169 static void _call_insert_finished_cb(sticker_provider_h provider_handle, GVariant *body)
170 {
171     int ret;
172     g_variant_get(body, "(i)", &ret);
173
174     if (ret == 0) {
175         provider_handle->insert_finished_cb(STICKER_ERROR_NONE, provider_handle->insert_finished_cb_user_data);
176     } else {
177         provider_handle->insert_finished_cb(STICKER_ERROR_OPERATION_FAILED, provider_handle->insert_finished_cb_user_data);
178     }
179 }
180
181 static void _handle_sticker_consumer_cb(GDBusConnection *connection,
182                                const gchar *sender_name,
183                                const gchar *object_path,
184                                const gchar *interface_name,
185                                const gchar *signal_name,
186                                GVariant *parameters,
187                                gpointer user_data)
188 {
189     LOGD("own_name : %s, signal_name : %s", g_dbus_connection_get_unique_name(connection), signal_name);
190     sticker_consumer_h consumer_handle = (sticker_consumer_h)user_data;
191
192     if (consumer_handle == NULL) {
193         LOGE("consumer handle is not available");
194         return;
195     }
196
197     if (parameters == NULL) {
198         LOGE("failed to get sticker info");
199         return;
200     }
201
202     #if 0 // Receive the sticker information by asynchronous communication.
203     if (g_strcmp0(signal_name, "send_group_list") == 0) {
204         if (consumer_handle->group_foreach_cb != NULL)
205             _call_sticker_list_cb(consumer_handle, parameters, signal_name);
206         return;
207     } else if (g_strcmp0(signal_name, "send_keyword_list") == 0) {
208         if (consumer_handle->keyword_foreach_cb != NULL)
209             _call_sticker_list_cb(consumer_handle, parameters, signal_name);
210         return;
211     }
212
213     sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
214
215     if (!sticker_data) {
216         LOGE("failed to allocate memory");
217         return;
218     }
219
220     _get_sticker_info_from_gvariant(parameters, sticker_data);
221
222     if (g_strcmp0(signal_name, "send_all_sticker_info") == 0) {
223         if (consumer_handle->data_foreach_cb != NULL)
224             consumer_handle->data_foreach_cb(sticker_data, consumer_handle->data_foreach_cb_user_data);
225         else
226             LOGW("No registered callback function");
227     } else if (g_strcmp0(signal_name, "send_sticker_info_by_keyword") == 0) {
228         if (consumer_handle->data_foreach_by_keyword_cb != NULL)
229             consumer_handle->data_foreach_by_keyword_cb(sticker_data, consumer_handle->data_foreach_by_keyword_cb_user_data);
230         else
231             LOGW("No registered callback function");
232     } else if (g_strcmp0(signal_name, "send_sticker_info_by_goup") == 0) {
233         if (consumer_handle->data_foreach_by_group_cb != NULL)
234             consumer_handle->data_foreach_by_group_cb(sticker_data, consumer_handle->data_foreach_by_group_cb_user_data);
235         else
236             LOGW("No registered callback function");
237     } else if (g_strcmp0(signal_name, "send_sticker_info_by_type") == 0) {
238         if (consumer_handle->data_foreach_by_type_cb != NULL)
239             consumer_handle->data_foreach_by_type_cb(sticker_data, consumer_handle->data_foreach_by_type_cb_user_data);
240         else
241             LOGW("No registered callback function");
242     }
243
244     _free_sticker_data(sticker_data);
245     #endif
246 }
247
248 static void _handle_sticker_provider_cb(GDBusConnection *connection,
249                                const gchar *sender_name,
250                                const gchar *object_path,
251                                const gchar *interface_name,
252                                const gchar *signal_name,
253                                GVariant *parameters,
254                                gpointer user_data)
255 {
256     LOGD("own_name : %s, signal_name : %s", g_dbus_connection_get_unique_name(connection), signal_name);
257     sticker_provider_h provider_handle = (sticker_provider_h)user_data;
258
259     if (provider_handle == NULL) {
260         LOGE("provider handle is not available");
261         return;
262     }
263
264     if (parameters == NULL) {
265         LOGE("failed to get sticker info");
266         return;
267     }
268
269     if (g_strcmp0(signal_name, "send_insert_result") == 0) {
270         if (provider_handle->insert_finished_cb != NULL)
271             _call_insert_finished_cb(provider_handle, parameters);
272     }
273
274     #if 0 // Receive the sticker information by asynchronous communication.
275     sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
276
277     if (!sticker_data) {
278         LOGE("failed to allocate memory");
279         return;
280     }
281
282     _get_sticker_info_from_gvariant(parameters, sticker_data);
283
284     if (g_strcmp0(signal_name, "send_sticker_info_by_appid") == 0) {
285         if (provider_handle->data_foreach_cb != NULL)
286             provider_handle->data_foreach_cb(sticker_data, provider_handle->data_foreach_cb_user_data);
287         else
288             LOGW("No registered callback function");
289     }
290
291     _free_sticker_data(sticker_data);
292     #endif
293 }
294
295 static int _dbus_signal_init(GDBusConnection *gdbus_connection, int *monitor_id, CLIENT_LIB lib, void *data)
296 {
297     int ret = STICKER_CLIENT_ERROR_NONE;
298     if (*monitor_id == 0) {
299         int id = 0;
300         if (lib == STICKER_CLIENT_LIB_CONSUMER)
301             id = g_dbus_connection_signal_subscribe(gdbus_connection,
302                                                     STICKER_DBUS_NAME,
303                                                     STICKER_CONSUMER_INTERFACE_NAME,
304                                                     NULL,
305                                                     STICKER_OBJECT_PATH,
306                                                     NULL,
307                                                     G_DBUS_SIGNAL_FLAGS_NONE,
308                                                     _handle_sticker_consumer_cb,
309                                                     data,
310                                                     NULL);
311         else if (lib == STICKER_CLIENT_LIB_PROVIDER)
312             id = g_dbus_connection_signal_subscribe(gdbus_connection,
313                                                     STICKER_DBUS_NAME,
314                                                     STICKER_PROVIDER_INTERFACE_NAME,
315                                                     NULL,
316                                                     STICKER_OBJECT_PATH,
317                                                     NULL,
318                                                     G_DBUS_SIGNAL_FLAGS_NONE,
319                                                     _handle_sticker_provider_cb,
320                                                     data,
321                                                     NULL);
322         LOGD("id : %d", id);
323         if (id == 0) {
324             ret = STICKER_CLIENT_ERROR_IO_ERROR;
325             LOGE("g_dbus_connection_signal_subscribe() failed");
326         } else {
327             *monitor_id = id;
328         }
329     }
330
331     return ret;
332 }
333
334 static GDBusMessage *_get_gbus_message(GVariant *body, const char *cmd)
335 {
336     GDBusMessage *message = NULL;
337     message = g_dbus_message_new_method_call(
338         STICKER_DBUS_NAME,
339         STICKER_OBJECT_PATH,
340         STICKER_INTERFACE_NAME,
341         cmd);
342
343     if (!message) {
344         LOGE("Failed to create a new gdbus message");
345         if (body)
346             g_variant_unref(body);
347         return NULL;
348     }
349
350     if (body != NULL)
351         g_dbus_message_set_body(message, body);
352
353     return message;
354 }
355
356 static int _send_gdbus_sync_message(GDBusConnection *gdbus_connection, GDBusMessage *msg, GDBusMessage **reply, const char *cmd)
357 {
358     int ret = STICKER_CLIENT_ERROR_NONE;
359     GError *err = NULL;
360
361     *reply = g_dbus_connection_send_message_with_reply_sync(
362             gdbus_connection,
363             msg,
364             G_DBUS_SEND_MESSAGE_FLAGS_NONE,
365             -1,
366             NULL,
367             NULL,
368             &err);
369
370     if (!*reply) {
371         ret = STICKER_CLIENT_ERROR_SERVICE_NOT_READY;
372         if (err != NULL) {
373             LOGE("Error occurred when sending message(%s) : %s", cmd, err->message);
374             if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
375                 ret = STICKER_CLIENT_ERROR_PERMISSION_DENIED;
376             g_error_free(err);
377         }
378         return ret;
379     }
380
381     if (g_dbus_message_to_gerror(*reply, &err)) {
382         LOGE("error message = %s, code = %d", err->message, err->code);
383         if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
384             ret = STICKER_CLIENT_ERROR_PERMISSION_DENIED;
385         else
386             ret = err->code;
387         g_error_free(err);
388         return ret;
389     }
390
391     LOGD("Send a message to server(cmd : %s)", cmd);
392     return STICKER_CLIENT_ERROR_NONE;
393 }
394
395 static int _send_sync_message(GDBusConnection *gdbus_connection, GVariant *body, GDBusMessage **reply, char *cmd)
396 {
397     int ret = STICKER_CLIENT_ERROR_NONE;
398     GDBusMessage *msg = NULL;
399
400     msg = _get_gbus_message(body, cmd);
401     if (msg == NULL)
402         return STICKER_CLIENT_ERROR_IO_ERROR;
403
404     ret = _send_gdbus_sync_message(gdbus_connection, msg, reply, cmd);
405
406     if (msg)
407         g_object_unref(msg);
408
409     return ret;
410 }
411
412 static void _gdbus_reply_message_async_cb(GDBusConnection *connection, GAsyncResult *res, gpointer user_data)
413 {
414     GDBusMessage *reply = NULL;
415     GError *err = NULL;
416
417     reply = g_dbus_connection_send_message_with_reply_finish(connection, res, &err);
418
419     if (reply) {
420         if (g_dbus_message_to_gerror(reply, &err)) {
421             LOGE("error message = %s, code = %d", err->message, err->code);
422             g_error_free(err);
423             return;
424         }
425     } else {
426         LOGE("There is no reply");
427         return;
428     }
429
430     if (reply)
431         g_object_unref(reply);
432
433     LOGD("Reply message was received");
434     return;
435 }
436
437 static int _send_async_message(GDBusConnection *gdbus_connection, GVariant *body, char *cmd)
438 {
439     int ret = STICKER_CLIENT_ERROR_NONE;
440     GDBusMessage *msg = NULL;
441
442     msg = _get_gbus_message(body, cmd);
443     if (msg == NULL)
444         return STICKER_CLIENT_ERROR_IO_ERROR;
445
446     g_dbus_connection_send_message_with_reply(
447         gdbus_connection,
448         msg,
449         G_DBUS_SEND_MESSAGE_FLAGS_NONE,
450         -1,
451         NULL,
452         NULL,
453         (GAsyncReadyCallback)_gdbus_reply_message_async_cb,
454         NULL);
455
456     if (msg)
457         g_object_unref(msg);
458
459     return ret;
460 }
461
462 static int _monitor_register(GDBusConnection *gdbus_connection)
463 {
464     int ret;
465     GDBusMessage *reply = NULL;
466
467     ret = _send_sync_message(gdbus_connection, g_variant_new("()"), &reply, "sticker_service_register");
468     if (reply)
469         g_object_unref(reply);
470
471     if (ret != STICKER_CLIENT_ERROR_NONE) {
472         LOGE("_send_sync_message() failed : %d", ret);
473         return ret;
474     }
475
476     is_server_started = 1;
477     return ret;
478 }
479
480 static void _on_name_appeared(GDBusConnection *connection,
481         const gchar     *name,
482         const gchar     *name_owner,
483         gpointer         user_data)
484 {
485     if (is_server_started == 0)
486         _monitor_register(connection);
487 }
488
489 static void _on_name_vanished(GDBusConnection *connection,
490         const gchar     *name,
491         gpointer         user_data)
492 {
493     is_server_started = 0;
494 }
495
496 int sticker_dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id,
497                       int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, void *data)
498 {
499     int ret;
500
501     ret = _dbus_init(gdbus_connection, server_watcher_id);
502     if (ret != STICKER_CLIENT_ERROR_NONE) {
503         LOGE("_dbus_init() failed : %d", ret);
504         return ret;
505     }
506
507     ret = _dbus_signal_init(*gdbus_connection, monitor_id, lib, data);
508     if (ret != STICKER_CLIENT_ERROR_NONE) {
509         LOGE("_dbus_signal_init() failed : %d", ret);
510         return ret;
511     }
512
513     ret = _monitor_register(*gdbus_connection);
514     if (ret != STICKER_CLIENT_ERROR_NONE) {
515         LOGE("_monitor_register() failed : %d", ret);
516         return ret;
517     }
518
519     if (*server_monitor_id == 0) {
520         *server_monitor_id = g_bus_watch_name_on_connection(
521                 *gdbus_connection,
522                 STICKER_DBUS_NAME,
523                 G_BUS_NAME_WATCHER_FLAGS_NONE,
524                 _on_name_appeared,
525                 _on_name_vanished,
526                 NULL,
527                 NULL);
528         if (*server_monitor_id == 0) {
529             g_dbus_connection_signal_unsubscribe(*gdbus_connection, *monitor_id);
530             *monitor_id = 0;
531             LOGE("Failed to get identifier");
532             return STICKER_CLIENT_ERROR_IO_ERROR;
533         }
534     }
535
536     return STICKER_CLIENT_ERROR_NONE;
537 }
538
539 int sticker_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_monitor_id, int *monitor_id)
540 {
541     if (*server_monitor_id) {
542         g_bus_unwatch_name(*server_monitor_id);
543         *server_monitor_id = 0;
544     }
545
546     if (*monitor_id) {
547         g_dbus_connection_signal_unsubscribe(gdbus_connection, *monitor_id);
548         *monitor_id = 0;
549     }
550
551     return STICKER_CLIENT_ERROR_NONE;
552 }
553
554 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
555 {
556     if (!keyword) {
557         LOGE("keyword doesn't exist");
558         return;
559     }
560
561     g_variant_builder_add(keyword_builder, "(s)", strdup((const char *)keyword));
562 }
563
564 int sticker_dbus_insert_sticker_info(GDBusConnection *gdbus_connection, sticker_data_h sticker_data)
565 {
566     int ret;
567     int ret_id = -1;
568     GDBusMessage *reply = NULL;
569     GVariant *body = NULL;
570     GVariant *reply_body = NULL;
571     GVariantBuilder *info_builder;
572     GVariantBuilder *keyword_builder;
573
574     if (!sticker_data->app_id || (sticker_data->type < 1) || !sticker_data->uri || !sticker_data->group || !sticker_data->keyword)
575         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
576
577     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
578     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_data->app_id));
579     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_data->type));
580     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_data->uri));
581     if (sticker_data->thumbnail)
582         g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_data->thumbnail));
583     if (sticker_data->description)
584         g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_data->description));
585     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_data->group));
586
587     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
588     g_list_foreach(sticker_data->keyword, (GFunc) _set_keyword_builder, keyword_builder);
589
590     body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
591
592     g_variant_builder_unref(info_builder);
593     g_variant_builder_unref(keyword_builder);
594
595     ret = _send_sync_message(gdbus_connection, body, &reply, "insert_sticker_info");
596     if (ret != STICKER_CLIENT_ERROR_NONE) {
597         LOGW("Failed to save sticker info");
598         return ret;
599     }
600
601     reply_body = g_dbus_message_get_body(reply);
602     g_variant_get(reply_body, "(i)", &ret_id);
603     sticker_data->sticker_info_id = ret_id;
604
605     LOGD("ret_id : %d", ret_id);
606     if (body)
607         g_variant_unref(body);
608
609     if (reply_body)
610         g_variant_unref(reply_body);
611
612     if (reply)
613         g_object_unref(reply);
614
615     return ret;
616 }
617
618 int sticker_dbus_insert_sticker_info_by_json(GDBusConnection *gdbus_connection, const char *app_id, const char *json_path)
619 {
620     int ret;
621     GVariant *body = NULL;
622
623     body = g_variant_new("(ss)", app_id, json_path);
624     ret = _send_async_message(gdbus_connection, body, "update_sticker_info_by_json");
625     if (ret != STICKER_CLIENT_ERROR_NONE)
626         LOGE("failed to send json path");
627
628     if (body)
629         g_variant_unref(body);
630
631     return ret;
632 }
633
634 int sticker_dbus_delete_sticker_info(GDBusConnection *gdbus_connection, int record_id)
635 {
636     int ret;
637     GDBusMessage *reply = NULL;
638     GVariant *body = NULL;
639
640     body = g_variant_new("(i)", record_id);
641     ret = _send_sync_message(gdbus_connection, body, &reply, "delete_sticker_info");
642     if (ret != STICKER_CLIENT_ERROR_NONE)
643         LOGE("failed to delete sticker info");
644
645     if (body)
646         g_variant_unref(body);
647
648     if (reply)
649         g_object_unref(reply);
650
651     return ret;
652 }
653
654 int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_data_h sticker_data)
655 {
656     int ret;
657     GDBusMessage *reply = NULL;
658     GVariant *body = NULL;
659     GVariant *reply_body = NULL;
660     sticker_data_h origin_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
661
662     if (!origin_data) {
663         LOGE("failed to allocate memory");
664         return STICKER_CLIENT_ERROR_OUT_OF_MEMORY;
665     }
666
667     body = g_variant_new("(i)", sticker_data->sticker_info_id);
668     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info");
669
670     if (ret == STICKER_CLIENT_ERROR_NONE) {
671         reply_body = g_dbus_message_get_body(reply);
672         _get_sticker_info_from_gvariant(reply_body, origin_data);
673     } else {
674         LOGW("failed to get sticker info");
675         free(origin_data);
676         if (reply)
677             g_object_unref(reply);
678         if (body)
679             g_variant_unref(body);
680         return ret;
681     }
682
683     if (sticker_data->type != 0 && sticker_data->type != origin_data->type) {
684         LOGD("origin_type : %d, new_type : %d", origin_data->type, sticker_data->type);
685         g_variant_unref(body);
686         body = g_variant_new("(ii)", sticker_data->sticker_info_id, sticker_data->type);
687         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_type");
688         if (ret != STICKER_CLIENT_ERROR_NONE)
689             LOGE("falied to update sticker type");
690     }
691
692     if (sticker_data->uri && strcmp(sticker_data->uri, origin_data->uri) != 0) {
693         LOGD("origin_uri : %s, new_uri : %s", origin_data->uri, sticker_data->uri);
694         g_variant_unref(body);
695         body = g_variant_new("(isis)", sticker_data->sticker_info_id, sticker_data->app_id, sticker_data->type, sticker_data->uri);
696         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_uri");
697         if (ret != STICKER_CLIENT_ERROR_NONE)
698             LOGE("falied to update sticker uri");
699     }
700
701     if (sticker_data->thumbnail && strcmp(sticker_data->thumbnail, origin_data->thumbnail) != 0) {
702         LOGD("origin_thumbnail : %s, new_thumbnail : %s", origin_data->thumbnail, sticker_data->thumbnail);
703         g_variant_unref(body);
704         body = g_variant_new("(is)", sticker_data->sticker_info_id, sticker_data->thumbnail);
705         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_thumbnail");
706         if (ret != STICKER_CLIENT_ERROR_NONE)
707             LOGE("falied to update sticker thumbnail");
708     }
709
710     if (sticker_data->description && strcmp(sticker_data->description, origin_data->description) != 0) {
711         LOGD("origin_description : %s, new_description : %s", origin_data->description, sticker_data->description);
712         g_variant_unref(body);
713         body = g_variant_new("(is)", sticker_data->sticker_info_id, sticker_data->description);
714         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_description");
715         if (ret != STICKER_CLIENT_ERROR_NONE)
716             LOGE("falied to update sticker description");
717     }
718
719     if (sticker_data->group && strcmp(sticker_data->group, origin_data->group) != 0) {
720         LOGD("origin_group : %s, new_group : %s", origin_data->group, sticker_data->group);
721         g_variant_unref(body);
722         body = g_variant_new("(is)", sticker_data->sticker_info_id, sticker_data->group);
723         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_group");
724         if (ret != STICKER_CLIENT_ERROR_NONE)
725             LOGE("falied to update sticker group");
726     }
727
728     if (sticker_data->keyword) {
729         GVariantBuilder *keyword_builder;
730         g_variant_unref(body);
731         keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
732         g_list_foreach(sticker_data->keyword, (GFunc) _set_keyword_builder, keyword_builder);
733         body = g_variant_new("(ia(s))", sticker_data->sticker_info_id, keyword_builder);
734         ret = _send_sync_message(gdbus_connection, body, &reply, "update_sticker_keyword");
735         if (ret != STICKER_CLIENT_ERROR_NONE)
736             LOGE("falied to update sticker keyword");
737     }
738
739     _free_sticker_data(origin_data);
740
741     if (body)
742         g_variant_unref(body);
743
744     if (reply_body)
745         g_variant_unref(reply_body);
746
747     if (reply)
748         g_object_unref(reply);
749
750     return ret;
751 }
752
753 int sticker_dbus_get_sticker_info_by_record_id(GDBusConnection *gdbus_connection, sticker_data_h sticker_data, int record_id)
754 {
755     int ret;
756     GDBusMessage *reply = NULL;
757     GVariant *body = NULL;
758     GVariant *reply_body = NULL;
759
760     body = g_variant_new("(i)", record_id);
761     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info");
762
763     if (ret == STICKER_CLIENT_ERROR_NONE) {
764         reply_body = g_dbus_message_get_body(reply);
765         sticker_data->sticker_info_id = record_id;
766         _get_sticker_info_from_gvariant(reply_body, sticker_data);
767
768         if (reply_body)
769             g_variant_unref(reply_body);
770     }
771
772     if (body)
773         g_variant_unref(body);
774
775     if (reply)
776         g_object_unref(reply);
777
778     return ret;
779 }
780
781 int sticker_dbus_get_group_list(GDBusConnection *gdbus_connection, GList **group_list)
782 {
783     int ret;
784     GDBusMessage *reply = NULL;
785     GVariantIter *iter = NULL;
786     GVariant *body = NULL;
787     GVariant *reply_body = NULL;
788     char *group = NULL;
789
790     if (group_list == NULL) {
791         LOGE("group_list is invalid");
792         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
793     }
794
795     body = g_variant_new("()");
796     ret = _send_sync_message(gdbus_connection, body, &reply, "get_group_list");
797
798     if (ret == STICKER_CLIENT_ERROR_NONE) {
799         reply_body = g_dbus_message_get_body(reply);
800         g_variant_get(reply_body, "(a(s))", &iter);
801
802         if (!iter) {
803             LOGD("falied to get iter");
804             return STICKER_CLIENT_ERROR_OPERATION_FAILED;
805         }
806
807         while (g_variant_iter_loop (iter, "(s)", &group)) {
808             *group_list = g_list_append(*group_list, strdup((const char *)group));
809         }
810
811         g_variant_iter_free(iter);
812     }
813
814     if (body)
815         g_variant_unref(body);
816
817     if (reply_body)
818         g_variant_unref(reply_body);
819
820     if (reply)
821         g_object_unref(reply);
822
823     return ret;
824 }
825
826 int sticker_dbus_get_keyword_list(GDBusConnection *gdbus_connection, GList **keyword_list)
827 {
828     int ret;
829     GDBusMessage *reply = NULL;
830     GVariantIter *iter = NULL;
831     GVariant *body = NULL;
832     GVariant *reply_body = NULL;
833     char *keyword = NULL;
834
835     if (keyword_list == NULL) {
836         LOGE("keyword_list is invalid");
837         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
838     }
839
840     body = g_variant_new("()");
841     ret = _send_sync_message(gdbus_connection, body, &reply, "get_keyword_list");
842
843     if (ret == STICKER_CLIENT_ERROR_NONE) {
844         reply_body = g_dbus_message_get_body(reply);
845         g_variant_get(reply_body, "(a(s))", &iter);
846
847         if (!iter) {
848             LOGD("falied to get iter");
849             return STICKER_CLIENT_ERROR_OPERATION_FAILED;
850         }
851
852         while (g_variant_iter_loop (iter, "(s)", &keyword)) {
853             *keyword_list = g_list_append(*keyword_list, strdup((const char *)keyword));
854         }
855
856         g_variant_iter_free(iter);
857     }
858
859     if (body)
860         g_variant_unref(body);
861
862     if (reply_body)
863         g_variant_unref(reply_body);
864
865     if (reply)
866         g_object_unref(reply);
867
868     return ret;
869 }
870
871 int sticker_dbus_get_sticker_count(GDBusConnection *gdbus_connection, const char *app_id, int *count)
872 {
873     int ret;
874     GDBusMessage *reply = NULL;
875     GVariant *body = NULL;
876     GVariant *reply_body = NULL;
877
878     body = g_variant_new("(s)", app_id);
879     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_count");
880
881     if (ret == STICKER_CLIENT_ERROR_NONE) {
882         reply_body = g_dbus_message_get_body(reply);
883         g_variant_get(reply_body, "(i)", count);
884     }
885
886     if (body)
887         g_variant_unref(body);
888
889     if (reply_body)
890         g_variant_unref(reply_body);
891
892     if (reply)
893         g_object_unref(reply);
894
895     return ret;
896 }
897
898 int sticker_dbus_get_all_sticker_info(GDBusConnection *gdbus_connection, int offset, int count, GVariantIter **id_iter)
899 {
900     int ret;
901     GDBusMessage *reply = NULL;
902     GVariant *body = NULL;
903     GVariant *reply_body = NULL;
904
905     body = g_variant_new("(ii)", offset, count);
906     ret = _send_sync_message(gdbus_connection, body, &reply, "get_all_sticker_info");
907     if (ret == STICKER_CLIENT_ERROR_NONE) {
908         reply_body = g_dbus_message_get_body(reply);
909         g_variant_get(reply_body, "(a(i))", &(*id_iter));
910     }
911
912     if (body)
913         g_variant_unref(body);
914
915     if (reply_body)
916         g_variant_unref(reply_body);
917
918     if (reply)
919         g_object_unref(reply);
920
921     return ret;
922 }
923
924 int sticker_dbus_get_sticker_info_by_appid(GDBusConnection *gdbus_connection, const char *app_id, int offset, int count, GVariantIter **id_iter)
925 {
926     int ret;
927     GDBusMessage *reply = NULL;
928     GVariant *body = NULL;
929     GVariant *reply_body = NULL;
930
931     body = g_variant_new("(sii)", app_id, offset, count);
932     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info_by_appid");
933     if (ret == STICKER_CLIENT_ERROR_NONE) {
934         reply_body = g_dbus_message_get_body(reply);
935         g_variant_get(reply_body, "(a(i))", &(*id_iter));
936     }
937
938     if (body)
939         g_variant_unref(body);
940
941     if (reply_body)
942         g_variant_unref(reply_body);
943
944     if (reply)
945         g_object_unref(reply);
946
947     return ret;
948 }
949
950 int sticker_dbus_get_sticker_info_by_type(GDBusConnection *gdbus_connection, sticker_data_uri_type_e type, int offset, int count, GVariantIter **id_iter)
951 {
952     int ret;
953     GDBusMessage *reply = NULL;
954     GVariant *body = NULL;
955     GVariant *reply_body = NULL;
956
957     body = g_variant_new("(iii)", (int)type, offset, count);
958     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info_by_type");
959     if (ret == STICKER_CLIENT_ERROR_NONE) {
960         reply_body = g_dbus_message_get_body(reply);
961         g_variant_get(reply_body, "(a(i))", &(*id_iter));
962     }
963
964     if (body)
965         g_variant_unref(body);
966
967     if (reply_body)
968         g_variant_unref(reply_body);
969
970     if (reply)
971         g_object_unref(reply);
972
973     return ret;
974 }
975
976 int sticker_dbus_get_sticker_info_by_group(GDBusConnection *gdbus_connection, const char *group, int offset, int count, GVariantIter **id_iter)
977 {
978     int ret;
979     GDBusMessage *reply = NULL;
980     GVariant *body = NULL;
981     GVariant *reply_body = NULL;
982
983     body = g_variant_new("(sii)", group, offset, count);
984     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info_by_group");
985     if (ret == STICKER_CLIENT_ERROR_NONE) {
986         reply_body = g_dbus_message_get_body(reply);
987         g_variant_get(reply_body, "(a(i))", &(*id_iter));
988     }
989
990     if (body)
991         g_variant_unref(body);
992
993     if (reply_body)
994         g_variant_unref(reply_body);
995
996     if (reply)
997         g_object_unref(reply);
998
999     return ret;
1000 }
1001
1002 int sticker_dbus_get_sticker_info_by_keyword(GDBusConnection *gdbus_connection, const char *keyword, int offset, int count, GVariantIter **id_iter)
1003 {
1004     int ret;
1005     GDBusMessage *reply = NULL;
1006     GVariant *body = NULL;
1007     GVariant *reply_body = NULL;
1008
1009     body = g_variant_new("(sii)", keyword, offset, count);
1010     ret = _send_sync_message(gdbus_connection, body, &reply, "get_sticker_info_by_keyword");
1011     if (ret == STICKER_CLIENT_ERROR_NONE) {
1012         reply_body = g_dbus_message_get_body(reply);
1013         g_variant_get(reply_body, "(a(i))", &(*id_iter));
1014     }
1015
1016     if (body)
1017         g_variant_unref(body);
1018
1019     if (reply_body)
1020         g_variant_unref(reply_body);
1021
1022     if (reply)
1023         g_object_unref(reply);
1024
1025     return ret;
1026 }