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