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