Modified to delete image files when sticker data is deleted from DB
[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             snprintf(conv_path, len, "%s/%s%s", STICKER_DIRECTORY, sticker_data->app_id, sticker_data->uri);
694         } else
695             conv_path = strdup(sticker_data->uri);
696
697         if (strcmp(conv_path, origin_data->uri) != 0) {
698             LOGD("origin_uri : %s, new_uri : %s", origin_data->uri, conv_path);
699             int is_exist = 0;
700             ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", sticker_data->uri), &reply, "check_file_exists");
701             if (ret == STICKER_CLIENT_ERROR_NONE) {
702                 reply_body = g_dbus_message_get_body(reply);
703                 g_variant_get(reply_body, "(i)", &is_exist);
704
705                 if (is_exist) {
706                     LOGE("file already exists");
707                     ret = STICKER_CLIENT_ERROR_FILE_EXISTS;
708                     free(conv_path);
709                     goto cleanup;
710                 }
711             } else {
712                 LOGE("failed to check file exists");
713                 free(conv_path);
714                 goto cleanup;
715             }
716
717             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");
718             if (ret != STICKER_CLIENT_ERROR_NONE) {
719                 LOGE("failed to update sticker uri");
720                 free(conv_path);
721                 goto cleanup;
722             }
723         }
724         free(conv_path);
725     }
726
727     if (sticker_data->type != 0 && sticker_data->type != origin_data->type) {
728         LOGD("origin_type : %d, new_type : %d", origin_data->type, sticker_data->type);
729         ret = _send_sync_message(gdbus_connection, g_variant_new("(ii)", sticker_data->sticker_info_id, sticker_data->type), &reply, "update_sticker_type");
730         if (ret != STICKER_CLIENT_ERROR_NONE) {
731             LOGE("failed to update sticker type");
732             goto cleanup;
733         }
734     }
735
736     if (sticker_data->thumbnail) {
737         int len = strlen(STICKER_DIRECTORY) + strlen(sticker_data->app_id) + strlen(sticker_data->thumbnail) + 2;
738         char *conv_path = (char *)calloc(len, sizeof(char));
739         snprintf(conv_path, len, "%s/%s%s", STICKER_DIRECTORY, sticker_data->app_id, sticker_data->thumbnail);
740         if (strcmp(conv_path, origin_data->thumbnail) != 0) {
741             LOGD("origin_thumbnail : %s, new_thumbnail : %s", origin_data->thumbnail, conv_path);
742             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");
743             if (ret != STICKER_CLIENT_ERROR_NONE) {
744                 LOGE("failed to update sticker thumbnail");
745                 free(conv_path);
746                 goto cleanup;
747             }
748         }
749         free(conv_path);
750     }
751
752     if (sticker_data->description && strcmp(sticker_data->description, origin_data->description) != 0) {
753         LOGD("origin_description : %s, new_description : %s", origin_data->description, sticker_data->description);
754         ret = _send_sync_message(gdbus_connection, g_variant_new("(is)", sticker_data->sticker_info_id, sticker_data->description), &reply, "update_sticker_description");
755         if (ret != STICKER_CLIENT_ERROR_NONE) {
756             LOGE("failed to update sticker description");
757             goto cleanup;
758         }
759     }
760
761     if (sticker_data->group && strcmp(sticker_data->group, origin_data->group) != 0) {
762         LOGD("origin_group : %s, new_group : %s", origin_data->group, sticker_data->group);
763         ret = _send_sync_message(gdbus_connection, g_variant_new("(is)", sticker_data->sticker_info_id, sticker_data->group), &reply, "update_sticker_group");
764         if (ret != STICKER_CLIENT_ERROR_NONE) {
765             LOGE("failed to update sticker group");
766             goto cleanup;
767         }
768     }
769
770     if (sticker_data->disp_type != 0 && sticker_data->disp_type != origin_data->disp_type) {
771         LOGD("origin_disp_type : %d, new_disp_type : %d", origin_data->disp_type, sticker_data->disp_type);
772         ret = _send_sync_message(gdbus_connection, g_variant_new("(ii)", sticker_data->sticker_info_id, sticker_data->disp_type), &reply, "update_sticker_disp_type");
773         if (ret != STICKER_CLIENT_ERROR_NONE) {
774             LOGE("failed to update sticker display type");
775             goto cleanup;
776         }
777     }
778
779     if (sticker_data->keyword) {
780         GVariantBuilder *keyword_builder;
781         keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
782         g_list_foreach(sticker_data->keyword, (GFunc) _set_keyword_builder, keyword_builder);
783         ret = _send_sync_message(gdbus_connection, g_variant_new("(ia(s))", sticker_data->sticker_info_id, keyword_builder), &reply, "update_sticker_keyword");
784         if (ret != STICKER_CLIENT_ERROR_NONE)
785             LOGE("failed to update sticker keyword");
786         g_variant_builder_unref(keyword_builder);
787     }
788
789 cleanup:
790     _free_sticker_data(origin_data);
791
792     if (reply_body)
793         g_variant_unref(reply_body);
794
795     if (reply)
796         g_object_unref(reply);
797
798     return ret;
799 }
800
801 int sticker_dbus_get_sticker_info_by_record_id(GDBusConnection *gdbus_connection, sticker_data_h sticker_data, int record_id)
802 {
803     int ret;
804     GDBusMessage *reply = NULL;
805     GVariant *reply_body = NULL;
806
807     ret = _send_sync_message(gdbus_connection, g_variant_new("(i)", record_id), &reply, "get_sticker_info");
808     if (ret == STICKER_CLIENT_ERROR_NONE) {
809         reply_body = g_dbus_message_get_body(reply);
810         sticker_data->sticker_info_id = record_id;
811         _get_sticker_info_from_gvariant(reply_body, sticker_data);
812
813         if (reply_body)
814             g_variant_unref(reply_body);
815     }
816
817     if (reply)
818         g_object_unref(reply);
819
820     return ret;
821 }
822
823 int sticker_dbus_get_group_list(GDBusConnection *gdbus_connection, const char *app_id, GList **group_list)
824 {
825     int ret;
826     GDBusMessage *reply = NULL;
827     GVariantIter *iter = NULL;
828     GVariant *reply_body = NULL;
829     char *group = NULL;
830
831     if (group_list == NULL) {
832         LOGE("group_list is invalid");
833         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
834     }
835
836     ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", app_id), &reply, "get_group_list");
837     if (ret == STICKER_CLIENT_ERROR_NONE) {
838         reply_body = g_dbus_message_get_body(reply);
839         g_variant_get(reply_body, "(a(s))", &iter);
840
841         if (!iter) {
842             LOGD("failed to get iter");
843             return STICKER_CLIENT_ERROR_OPERATION_FAILED;
844         }
845
846         while (g_variant_iter_loop (iter, "(s)", &group)) {
847             *group_list = g_list_append(*group_list, strdup((const char *)group));
848         }
849
850         g_variant_iter_free(iter);
851     }
852
853     if (reply_body)
854         g_variant_unref(reply_body);
855
856     if (reply)
857         g_object_unref(reply);
858
859     return ret;
860 }
861
862 int sticker_dbus_get_keyword_list(GDBusConnection *gdbus_connection, const char *app_id, GList **keyword_list)
863 {
864     int ret;
865     GDBusMessage *reply = NULL;
866     GVariantIter *iter = NULL;
867     GVariant *reply_body = NULL;
868     char *keyword = NULL;
869
870     if (keyword_list == NULL) {
871         LOGE("keyword_list is invalid");
872         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
873     }
874
875     ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", app_id), &reply, "get_keyword_list");
876     if (ret == STICKER_CLIENT_ERROR_NONE) {
877         reply_body = g_dbus_message_get_body(reply);
878         g_variant_get(reply_body, "(a(s))", &iter);
879
880         if (!iter) {
881             LOGD("failed to get iter");
882             return STICKER_CLIENT_ERROR_OPERATION_FAILED;
883         }
884
885         while (g_variant_iter_loop (iter, "(s)", &keyword)) {
886             *keyword_list = g_list_append(*keyword_list, strdup((const char *)keyword));
887         }
888
889         g_variant_iter_free(iter);
890     }
891
892     if (reply_body)
893         g_variant_unref(reply_body);
894
895     if (reply)
896         g_object_unref(reply);
897
898     return ret;
899 }
900
901 int sticker_dbus_get_sticker_count(GDBusConnection *gdbus_connection, const char *app_id, int *count)
902 {
903     int ret;
904     GDBusMessage *reply = NULL;
905     GVariant *reply_body = NULL;
906
907     ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", app_id), &reply, "get_sticker_count");
908     if (ret == STICKER_CLIENT_ERROR_NONE) {
909         reply_body = g_dbus_message_get_body(reply);
910         g_variant_get(reply_body, "(i)", count);
911     }
912
913     if (reply_body)
914         g_variant_unref(reply_body);
915
916     if (reply)
917         g_object_unref(reply);
918
919     return ret;
920 }
921
922 int sticker_dbus_get_all_sticker_info(GDBusConnection *gdbus_connection, const char *app_id, int offset, int count, GVariantIter **id_iter)
923 {
924     int ret;
925     GDBusMessage *reply = NULL;
926     GVariant *reply_body = NULL;
927
928     ret = _send_sync_message(gdbus_connection, g_variant_new("(sii)", app_id, offset, count), &reply, "get_all_sticker_info");
929     if (ret == STICKER_CLIENT_ERROR_NONE) {
930         reply_body = g_dbus_message_get_body(reply);
931         g_variant_get(reply_body, "(a(i))", &(*id_iter));
932     }
933
934     if (reply_body)
935         g_variant_unref(reply_body);
936
937     if (reply)
938         g_object_unref(reply);
939
940     return ret;
941 }
942
943 int sticker_dbus_get_sticker_info_by_appid(GDBusConnection *gdbus_connection, const char *app_id, int offset, int count, GVariantIter **id_iter)
944 {
945     int ret;
946     GDBusMessage *reply = NULL;
947     GVariant *reply_body = NULL;
948
949     ret = _send_sync_message(gdbus_connection, g_variant_new("(sii)", app_id, offset, count), &reply, "get_sticker_info_by_appid");
950     if (ret == STICKER_CLIENT_ERROR_NONE) {
951         reply_body = g_dbus_message_get_body(reply);
952         g_variant_get(reply_body, "(a(i))", &(*id_iter));
953     }
954
955     if (reply_body)
956         g_variant_unref(reply_body);
957
958     if (reply)
959         g_object_unref(reply);
960
961     return ret;
962 }
963
964 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)
965 {
966     int ret;
967     GDBusMessage *reply = NULL;
968     GVariant *reply_body = NULL;
969
970     ret = _send_sync_message(gdbus_connection, g_variant_new("(siii)", app_id, (int)type, offset, count), &reply, "get_sticker_info_by_type");
971     if (ret == STICKER_CLIENT_ERROR_NONE) {
972         reply_body = g_dbus_message_get_body(reply);
973         g_variant_get(reply_body, "(a(i))", &(*id_iter));
974     }
975
976     if (reply_body)
977         g_variant_unref(reply_body);
978
979     if (reply)
980         g_object_unref(reply);
981
982     return ret;
983 }
984
985 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)
986 {
987     int ret;
988     GDBusMessage *reply = NULL;
989     GVariant *reply_body = NULL;
990
991     ret = _send_sync_message(gdbus_connection, g_variant_new("(ssii)", app_id, group, offset, count), &reply, "get_sticker_info_by_group");
992     if (ret == STICKER_CLIENT_ERROR_NONE) {
993         reply_body = g_dbus_message_get_body(reply);
994         g_variant_get(reply_body, "(a(i))", &(*id_iter));
995     }
996
997     if (reply_body)
998         g_variant_unref(reply_body);
999
1000     if (reply)
1001         g_object_unref(reply);
1002
1003     return ret;
1004 }
1005
1006 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)
1007 {
1008     int ret;
1009     GDBusMessage *reply = NULL;
1010     GVariant *reply_body = NULL;
1011
1012     ret = _send_sync_message(gdbus_connection, g_variant_new("(ssii)", app_id, keyword, offset, count), &reply, "get_sticker_info_by_keyword");
1013     if (ret == STICKER_CLIENT_ERROR_NONE) {
1014         reply_body = g_dbus_message_get_body(reply);
1015         g_variant_get(reply_body, "(a(i))", &(*id_iter));
1016     }
1017
1018     if (reply_body)
1019         g_variant_unref(reply_body);
1020
1021     if (reply)
1022         g_object_unref(reply);
1023
1024     return ret;
1025 }
1026
1027 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)
1028 {
1029     int ret;
1030     GDBusMessage *reply = NULL;
1031     GVariant *reply_body = NULL;
1032
1033     ret = _send_sync_message(gdbus_connection, g_variant_new("(siii)", app_id, (int)type, offset, count), &reply, "get_sticker_info_by_disp_type");
1034     if (ret == STICKER_CLIENT_ERROR_NONE) {
1035         reply_body = g_dbus_message_get_body(reply);
1036         g_variant_get(reply_body, "(a(i))", &(*id_iter));
1037     }
1038
1039     if (reply_body)
1040         g_variant_unref(reply_body);
1041
1042     if (reply)
1043         g_object_unref(reply);
1044
1045     return ret;
1046 }
1047
1048 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)
1049 {
1050     int ret;
1051     GDBusMessage *reply = NULL;
1052     GVariantIter *iter = NULL;
1053     GVariant *reply_body = NULL;
1054     char *group = NULL;
1055
1056     if (group_list == NULL) {
1057         LOGE("group_list is invalid");
1058         return STICKER_CLIENT_ERROR_INVALID_PARAMETER;
1059     }
1060
1061     ret = _send_sync_message(gdbus_connection, g_variant_new("(si)", app_id, (int)type), &reply, "get_group_list_by_disp_type");
1062     if (ret == STICKER_CLIENT_ERROR_NONE) {
1063         reply_body = g_dbus_message_get_body(reply);
1064         g_variant_get(reply_body, "(a(s))", &iter);
1065
1066         if (!iter) {
1067             LOGD("failed to get iter");
1068             return STICKER_CLIENT_ERROR_OPERATION_FAILED;
1069         }
1070
1071         while (g_variant_iter_loop (iter, "(s)", &group)) {
1072             *group_list = g_list_append(*group_list, strdup((const char *)group));
1073         }
1074
1075         g_variant_iter_free(iter);
1076     }
1077
1078     if (reply_body)
1079         g_variant_unref(reply_body);
1080
1081     if (reply)
1082         g_object_unref(reply);
1083
1084     return ret;
1085 }
1086
1087 int sticker_dbus_check_file_exists(GDBusConnection *gdbus_connection, const char *uri, int *result)
1088 {
1089     int ret;
1090     GDBusMessage *reply = NULL;
1091     GVariant *reply_body = NULL;
1092
1093     ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", uri), &reply, "check_file_exists");
1094     if (ret == STICKER_CLIENT_ERROR_NONE) {
1095         reply_body = g_dbus_message_get_body(reply);
1096         g_variant_get(reply_body, "(i)", result);
1097     }
1098
1099     if (reply_body)
1100         g_variant_unref(reply_body);
1101
1102     if (reply)
1103         g_object_unref(reply);
1104
1105     return ret;
1106 }