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