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