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