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