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