Add interface to check file exists
[platform/core/uifw/capi-ui-sticker.git] / server / stickerd_data_manager.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 <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <tzplatform_config.h>
21 #include <gio/gunixfdlist.h>
22 #include <dlog.h>
23 #include <json-glib/json-glib.h>
24 #include <package_manager.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30
31 #include "stickerd_dbus.h"
32 #include "stickerd_data_manager.h"
33 #include "stickerd_db_manager.h"
34 #include "sticker_defs.h"
35 #include "stickerd_error.h"
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40 #define LOG_TAG "STICKERD_DATA_MANAGER"
41
42 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
43 #define MAX_ERROR_BUFFER  256
44
45 static GHashTable *_monitoring_hash = NULL;
46 static char error_buffer[MAX_ERROR_BUFFER];
47 extern GMainLoop *main_loop;
48
49 static void _check_watcher_exist()
50 {
51     if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
52         LOGD("Terminate sticker daemon");
53         g_hash_table_destroy(_monitoring_hash);
54         _monitoring_hash = NULL;
55         g_main_loop_quit(main_loop);
56     }
57 }
58
59 static void _on_name_appeared(GDBusConnection *connection,
60         const gchar     *name,
61         const gchar     *name_owner,
62         gpointer         user_data)
63 {
64     LOGD("name: %s", name);
65 }
66
67 static void _on_name_vanished(GDBusConnection *connection,
68         const gchar     *name,
69         gpointer         user_data)
70 {
71     monitoring_info_s *info = (monitoring_info_s *)user_data;
72
73     if (info) {
74         if (_monitoring_hash != NULL && g_hash_table_lookup(_monitoring_hash, GUINT_TO_POINTER(info->watcher_id)) != NULL) {
75             LOGD("name: %s", name);
76             g_bus_unwatch_name(info->watcher_id);
77             delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
78         }
79
80         if (info->bus_name)
81             free(info->bus_name);
82         free(info);
83         info = NULL;
84     }
85
86     _check_watcher_exist();
87 }
88
89 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
90         const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
91         gpointer user_data)
92 {
93     LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
94
95     if (_monitoring_hash == NULL)
96         _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
97
98     GVariant *reply_body = NULL;
99     int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
100
101     if (g_strcmp0(method_name, "sticker_service_register") == 0) {
102         ret = stickerd_server_register(parameters, &reply_body, sender,
103             _on_name_appeared, _on_name_vanished, &_monitoring_hash);
104     } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
105         ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash);
106     } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
107         ret = stickerd_insert_sticker_info(parameters, &reply_body);
108     } else if  (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
109         ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
110     } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
111         ret = stickerd_del_sticker_info(parameters, &reply_body);
112     } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
113         ret = stickerd_update_sticker_type(parameters, &reply_body);
114     } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
115         ret = stickerd_update_sticker_uri(parameters, &reply_body);
116     } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
117         ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
118     } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
119         ret = stickerd_update_sticker_description(parameters, &reply_body);
120     } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
121         ret = stickerd_update_sticker_group(parameters, &reply_body);
122     } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
123         ret = stickerd_update_sticker_keyword(parameters, &reply_body);
124     } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
125         ret = stickerd_get_sticker_info(parameters, &reply_body);
126     } else if (g_strcmp0(method_name, "get_group_list") == 0) {
127         ret = stickerd_get_group_list(parameters, &reply_body);
128     } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
129         ret = stickerd_get_keyword_list(parameters, &reply_body);
130     } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
131         ret = stickerd_get_sticker_count(parameters, &reply_body);
132     } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
133         ret = stickerd_get_all_sticker_info(parameters, &reply_body);
134     } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
135         ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
136     } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
137         ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
138     } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
139         ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
140     } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
141         ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
142     } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
143         ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
144     } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
145         ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
146     } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
147         ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
148     } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
149         ret = stickerd_check_file_exists(parameters, &reply_body);
150     }
151
152     if (ret == STICKERD_SERVER_ERROR_NONE) {
153         LOGD("method_call successful, method_name : %s", method_name);
154         g_dbus_method_invocation_return_value(invocation, reply_body);
155     } else {
156         LOGE("method_call failed, method_name : %s", method_name);
157         g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
158     }
159
160     _check_watcher_exist();
161 }
162
163 static const GDBusInterfaceVTable _sticker_interface_vtable = {
164         _stickerd_client_dbus_method_call_handler,
165         NULL,
166         NULL
167 };
168
169 int stickerd_register_dbus_interface(void)
170 {
171     static gchar introspection_xml[] =
172             "  <node>"
173             "  <interface name='org.tizen.sticker_service'>"
174             "        <method name='sticker_service_register'>"
175             "          <arg type='i' name='watcher_id' direction='out'/>"
176             "        </method>"
177
178             "        <method name='sticker_service_unregister'>"
179             "          <arg type='i' name='watcher_id' direction='in'/>"
180             "        </method>"
181
182             "        <method name='insert_sticker_info'>"
183             "          <arg type='a{iv}' name='sticker_info' direction='in'/>"
184             "          <arg type='a(s)' name='keyword_list' direction='in'/>"
185             "          <arg type='i' name='record_id' direction='out'/>"
186             "        </method>"
187
188             "        <method name='update_sticker_info_by_json'>"
189             "          <arg type='s' name='app_id' direction='in'/>"
190             "          <arg type='s' name='json_path' direction='in'/>"
191             "        </method>"
192
193             "        <method name='delete_sticker_info'>"
194             "          <arg type='i' name='record_id' direction='in'/>"
195             "        </method>"
196
197             "        <method name='update_sticker_type'>"
198             "          <arg type='i' name='record_id' direction='in'/>"
199             "          <arg type='i' name='type' direction='in'/>"
200             "        </method>"
201
202             "        <method name='update_sticker_uri'>"
203             "          <arg type='i' name='record_id' direction='in'/>"
204             "          <arg type='s' name='app_id' direction='in'/>"
205             "          <arg type='i' name='type' direction='in'/>"
206             "          <arg type='s' name='uri' direction='in'/>"
207             "        </method>"
208
209             "        <method name='update_sticker_thumbnail'>"
210             "          <arg type='i' name='record_id' direction='in'/>"
211             "          <arg type='s' name='thumbnail' direction='in'/>"
212             "        </method>"
213
214             "        <method name='update_sticker_description'>"
215             "          <arg type='i' name='record_id' direction='in'/>"
216             "          <arg type='s' name='description' direction='in'/>"
217             "        </method>"
218
219             "        <method name='update_sticker_group'>"
220             "          <arg type='i' name='record_id' direction='in'/>"
221             "          <arg type='s' name='group' direction='in'/>"
222             "        </method>"
223
224             "        <method name='update_sticker_keyword'>"
225             "          <arg type='i' name='record_id' direction='in'/>"
226             "          <arg type='a(s)' name='keyword' direction='in'/>"
227             "        </method>"
228
229             "        <method name='get_sticker_info'>"
230             "          <arg type='i' name='record_id' direction='in'/>"
231             "          <arg type='a{iv}' name='sticker_info' direction='out'/>"
232             "          <arg type='a(s)' name='keyword_list' direction='out'/>"
233             "        </method>"
234
235             "        <method name='get_group_list'>"
236             "          <arg type='s' name='app_id' direction='in'/>"
237             "          <arg type='a(s)' name='group_list' direction='out'/>"
238             "        </method>"
239
240             "        <method name='get_keyword_list'>"
241             "          <arg type='s' name='app_id' direction='in'/>"
242             "          <arg type='a(s)' name='keyword_list' direction='out'/>"
243             "        </method>"
244
245             "        <method name='get_sticker_count'>"
246             "          <arg type='s' name='app_id' direction='in'/>"
247             "          <arg type='i' name='count' direction='out'/>"
248             "        </method>"
249
250             "        <method name='get_all_sticker_info'>"
251             "          <arg type='s' name='app_id' direction='in'/>"
252             "          <arg type='i' name='offset' direction='in'/>"
253             "          <arg type='i' name='count' direction='in'/>"
254             "          <arg type='a(i)' name='id_list' direction='out'/>"
255             "        </method>"
256
257             "        <method name='get_sticker_info_by_appid'>"
258             "          <arg type='s' name='app_id' direction='in'/>"
259             "          <arg type='i' name='offset' direction='in'/>"
260             "          <arg type='i' name='count' direction='in'/>"
261             "          <arg type='a(i)' name='id_list' direction='out'/>"
262             "        </method>"
263
264             "        <method name='get_sticker_info_by_type'>"
265             "          <arg type='s' name='app_id' direction='in'/>"
266             "          <arg type='i' name='type' direction='in'/>"
267             "          <arg type='i' name='offset' direction='in'/>"
268             "          <arg type='i' name='count' direction='in'/>"
269             "          <arg type='a(i)' name='id_list' direction='out'/>"
270             "        </method>"
271
272             "        <method name='get_sticker_info_by_group'>"
273             "          <arg type='s' name='app_id' direction='in'/>"
274             "          <arg type='s' name='group' direction='in'/>"
275             "          <arg type='i' name='offset' direction='in'/>"
276             "          <arg type='i' name='count' direction='in'/>"
277             "          <arg type='a(i)' name='id_list' direction='out'/>"
278             "        </method>"
279
280             "        <method name='get_sticker_info_by_keyword'>"
281             "          <arg type='s' name='app_id' direction='in'/>"
282             "          <arg type='s' name='keyword' direction='in'/>"
283             "          <arg type='i' name='offset' direction='in'/>"
284             "          <arg type='i' name='count' direction='in'/>"
285             "          <arg type='a(i)' name='id_list' direction='out'/>"
286             "        </method>"
287
288             "        <method name='get_sticker_info_by_disp_type'>"
289             "          <arg type='s' name='app_id' direction='in'/>"
290             "          <arg type='i' name='type' direction='in'/>"
291             "          <arg type='i' name='offset' direction='in'/>"
292             "          <arg type='i' name='count' direction='in'/>"
293             "          <arg type='a(i)' name='id_list' direction='out'/>"
294             "        </method>"
295
296             "        <method name='get_group_list_by_disp_type'>"
297             "          <arg type='s' name='app_id' direction='in'/>"
298             "          <arg type='i' name='disp_type' direction='in'/>"
299             "          <arg type='a(s)' name='group_list' direction='out'/>"
300             "        </method>"
301
302             "        <method name='update_sticker_disp_type'>"
303             "          <arg type='i' name='record_id' direction='in'/>"
304             "          <arg type='i' name='disp_type' direction='in'/>"
305             "        </method>"
306
307             "        <method name='check_file_exists'>"
308             "          <arg type='s' name='uri' direction='in'/>"
309             "          <arg type='i' name='result' direction='out'/>"
310             "        </method>"
311             "  </interface>"
312             "  </node>";
313
314     return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
315 }
316
317 int stickerd_dbus_init(void)
318 {
319     int ret;
320
321     ret = stickerd_register_dbus_interface();
322     if (ret != STICKERD_SERVER_ERROR_NONE) {
323         LOGE("Failed to register dbus interface : %d", ret);
324         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
325     }
326
327     return STICKERD_SERVER_ERROR_NONE;
328 }
329
330 static int _check_file_exist(const char *app_id, const char *path)
331 {
332     int ret = 0;
333     package_info_h package_info = NULL;
334     char *app_path = NULL;
335     char *file_path = NULL;
336
337     if (access(path, F_OK) == 0) {
338         ret = 0;
339         goto cleanup;
340     }
341
342     ret = package_info_create(app_id, &package_info);
343     if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
344         LOGE("failed to create package_info. ret: %d", ret);
345         ret = -1;
346         goto cleanup;
347     }
348
349     ret = package_info_get_root_path(package_info, &app_path);
350     if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
351         LOGE("failed to create package_info. ret: %d", ret);
352         ret = -1;
353         goto cleanup;
354     }
355
356     int path_len = strlen(app_path) + strlen(path) + 2;
357     file_path = (char *)calloc(path_len, sizeof(char));
358     if (!file_path) {
359         LOGE("Failed to alloc memory");
360         ret = -1;
361         goto cleanup;
362     }
363
364     if(path[0] == '/')
365         snprintf(file_path, path_len, "%s%s",app_path, path);
366     else
367         snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
368
369     if (access(file_path, F_OK) != 0) {
370         LOGE("%s does not exist", file_path);
371         ret = -1;
372     } else
373         ret = 0;
374
375 cleanup:
376     if (package_info)
377         package_info_destroy(package_info);
378
379     if (app_path) {
380         free(app_path);
381         app_path = NULL;
382     }
383
384     if (file_path) {
385         free(file_path);
386         file_path = NULL;
387     }
388
389     return ret;
390 }
391
392 static int _mkdirs(const char *path, mode_t mode)
393 {
394     int len = 0;
395     char prev_path[2048];
396     const char *tmp = path;
397
398     if (!path || strlen(path) > 2048)
399         return -1;
400
401     memset(prev_path, '\0', 2048);
402     while ((tmp = strchr(tmp, '/')) != NULL) {
403         len = tmp - path;
404         tmp++;
405
406         if (len == 0)
407             continue;
408
409         strncpy(prev_path, path, len);
410         prev_path[len] = 0x00;
411
412         if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
413             strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
414             LOGE("directory create error : %s", error_buffer);
415             return -1;
416         }
417     }
418
419     if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
420         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
421         LOGE("directory create error : %s", error_buffer);
422         return -1;
423     }
424
425     return 0;
426 }
427
428 static int _file_copy(const char *src, const char *dest)
429 {
430     int ret = 0;
431     int fd = -1, n_fd = -1;
432     char buf[4096];
433     int tmp_err = 0;
434     int size;
435
436     memset(buf, '\0', 4096);
437     fd = open(src, O_RDONLY);
438     n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
439
440     if (fd == -1 || n_fd == -1) {
441         tmp_err = errno;
442         ret = -1;
443         goto cleanup;
444     }
445
446     while((size = read(fd, buf, 4096))) {
447         if (size == -1) {
448             if(errno == EINTR)
449                 continue;
450
451             tmp_err = errno;
452             ret = -1;
453             goto cleanup;
454         }
455
456         while(write(n_fd, buf, size) == -1) {
457             if(errno == EINTR) {
458                 continue;
459             } else {
460                 tmp_err = errno;
461                 goto cleanup;
462             }
463         }
464     }
465
466 cleanup:
467     if (fd != -1)
468         close(fd);
469
470     if (n_fd != -1)
471         close(n_fd);
472
473     errno = tmp_err;
474     return ret;
475 }
476
477 static char* _convert_sticker_uri(const char *uri, const char *appid)
478 {
479     int ret;
480     int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
481     char * new_path = (char *)calloc(len, sizeof(char));
482     if (new_path == NULL) {
483         LOGE("Failed to alloc memory");
484         return NULL;
485     }
486
487     if (uri[0] == '/')
488         snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
489     else
490         snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
491
492     if (access(new_path, F_OK) == 0) {
493         LOGE("sticker file already exists");
494         ret = -1;
495         goto cleanup;
496     }
497
498     ret = _mkdirs(new_path, 0755);
499     if (ret != 0) {
500         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
501         LOGE("directory create error : %s", error_buffer);
502         goto cleanup;
503     }
504
505     ret = _file_copy(uri, new_path);
506     if (ret != 0) {
507         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
508         LOGE("failed to copy sticker file : %s", error_buffer);
509     }
510
511 cleanup:
512     if (ret == 0) {
513         return new_path;
514     } else {
515         free(new_path);
516         new_path = NULL;
517         return NULL;
518     }
519 }
520
521 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
522 {
523     int ret;
524     int record_id = 0;
525     STICKER_DAT_TYPE key;
526     sticker_info_db *sticker_info = NULL;
527     GVariant *value = NULL;
528     GVariantIter *info_iter = NULL;
529     GVariantIter *keyword_iter = NULL;
530     char *keyword;
531
532     g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
533     if (!info_iter || !keyword_iter) {
534         LOGD("failed to get iter");
535         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
536         goto cleanup;
537     }
538
539     sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
540
541     if (!sticker_info) {
542         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
543         goto cleanup;
544     }
545
546     while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
547         switch(key) {
548             case STICKER_DATA_TYPE_APP_ID:
549             sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
550             break;
551             case STICKER_DATA_TYPE_URI_TYPE:
552             sticker_info->type = g_variant_get_int32(value);
553             break;
554             case STICKER_DATA_TYPE_URI:
555             sticker_info->uri = (char *) g_variant_get_string(value, NULL);
556             break;
557             case STICKER_DATA_TYPE_THUMBNAIL:
558             sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
559             break;
560             case STICKER_DATA_TYPE_DESCRIPTION:
561             sticker_info->description = (char *) g_variant_get_string(value, NULL);
562             break;
563             case STICKER_DATA_TYPE_GROUP:
564             sticker_info->group = (char *) g_variant_get_string(value, NULL);
565             break;
566             case STICKER_DATA_TYPE_DISP_TYPE:
567             sticker_info->display_type = g_variant_get_int32(value);
568             default:
569             break;
570         }
571     }
572
573     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
574         sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
575     }
576
577     if (sticker_info->type == 1) {
578         if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
579             sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
580             if (!sticker_info->uri) {
581                 LOGE("failed to copy sticker file");
582                 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
583                 goto cleanup;
584             }
585         } else {
586             LOGE("sticker file does not exist");
587             ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
588             goto cleanup;
589         }
590     }
591
592     ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
593     if (ret != STICKERD_SERVER_ERROR_NONE) {
594         LOGE("Failed to insert sticker info");
595         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
596         goto cleanup;
597     }
598
599     *reply_body = g_variant_new("(i)", record_id);
600     if (*reply_body == NULL) {
601         LOGE("Failed to create reply_body");
602         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
603     }
604
605 cleanup:
606     if (value)
607         g_variant_unref(value);
608
609     if (info_iter)
610         g_variant_iter_free(info_iter);
611
612     if (keyword_iter)
613         g_variant_iter_free(keyword_iter);
614
615     if (sticker_info) {
616         free(sticker_info);
617         sticker_info = NULL;
618     }
619
620     return ret;
621 }
622
623 static char* _get_string_from_object(JsonObject *object, const char *key)
624 {
625     if (json_object_has_member(object, key) == false)
626         return NULL;
627
628     const char *str = json_object_get_string_member(object, key);
629     if (str != NULL)
630         return strdup(str);
631     else
632         return NULL;
633 }
634
635 static int _get_int_from_object(JsonObject *object, const char *key)
636 {
637     if (json_object_has_member(object, key) == false)
638         return -1;
639
640     int type = json_object_get_int_member(object, key);
641
642     return type;
643 }
644
645 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
646 {
647     int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
648     int record_id;
649     sticker_info_db *sticker_info = NULL;
650     char *app_id = NULL;
651     char *json_path = NULL;
652     JsonParser* parser = NULL;
653     GError* err_msg = NULL;
654     GVariant *body = NULL;
655     char *cmd = "send_insert_result";
656
657     *reply_body = g_variant_new("()");
658         if (*reply_body == NULL) {
659         LOGE("Failed to create reply_body");
660         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
661     }
662
663     g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
664
665     if (!app_id || !json_path) {
666         LOGE("failed to get parameter");
667         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
668     }
669
670     SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
671
672     parser = json_parser_new();
673     json_parser_load_from_file(parser, json_path, &err_msg);
674     if (err_msg) {
675         LOGE("failed to load json file. error message: %s", err_msg->message);
676         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
677         goto cleanup;
678     }
679
680     JsonNode *root = json_parser_get_root(parser);
681     if (root == NULL) {
682         LOGE("failed to get root");
683         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
684         goto cleanup;
685     }
686
687     JsonObject *root_obj = json_node_get_object(root);
688     if (root_obj == NULL) {
689         LOGE("failed to get object");
690         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
691         goto cleanup;
692     }
693
694     JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
695     if (sticker_arr == NULL) {
696         LOGE("failed to get array member");
697         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
698         goto cleanup;
699     }
700
701     int arr_len = json_array_get_length(sticker_arr);
702     for (int i = 0; i < arr_len; i++) {
703         JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
704         if (info_object != NULL) {
705             sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
706             if (!sticker_info) {
707                 LOGE("Failed to alloc memory");
708                 continue;
709             }
710
711             sticker_info->app_id = app_id;
712             sticker_info->type = _get_int_from_object(info_object, "type");
713             if (sticker_info->type < 1)
714                 goto free_memory;
715
716             sticker_info->uri = _get_string_from_object(info_object, "uri");
717             if (!sticker_info->uri)
718                 goto free_memory;
719
720             if (sticker_info->type == 1) {
721                 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
722                     sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
723                     if (!sticker_info->uri)
724                         goto free_memory;
725                 } else {
726                     goto free_memory;
727                 }
728             }
729
730             sticker_info->group = _get_string_from_object(info_object, "group");
731             if (!sticker_info->group)
732                 goto free_memory;
733
734             sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
735             if (!sticker_info->thumbnail)
736                 goto free_memory;
737
738             sticker_info->description = _get_string_from_object(info_object, "description");
739
740             sticker_info->display_type = _get_int_from_object(info_object, "display_type");
741
742             JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
743             int keyword_arr_len = json_array_get_length(keyword_arr);
744             if (keyword_arr_len < 1)
745                 goto free_memory;
746
747             for (int j = 0; j < keyword_arr_len; j++) {
748                 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
749             }
750
751             ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
752             if (ret != STICKERD_SERVER_ERROR_NONE) {
753                 LOGE("Failed to insert sticker info");
754                 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
755             }
756
757 free_memory:
758             free(sticker_info);
759             sticker_info = NULL;
760         }
761     }
762
763 cleanup:
764     if (err_msg)
765         g_error_free(err_msg);
766     if (parser)
767         g_object_unref(parser);
768
769     body = g_variant_new("(i)", ret);
770
771     ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
772     if (ret != STICKERD_SERVER_ERROR_NONE)
773         LOGE("Failed to send insert result to client");
774
775     if(body)
776         g_variant_unref(body);
777
778     return ret;
779 }
780
781 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
782 {
783     int ret;
784     int record_id;
785
786     *reply_body = g_variant_new("()");
787     if (*reply_body == NULL) {
788         LOGE("Failed to create reply_body");
789         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
790     }
791
792     g_variant_get(parameters, "(i)", &record_id);
793
794     ret = stickerd_db_delete_sticker_info(record_id);
795     if (ret != STICKERD_SERVER_ERROR_NONE) {
796         LOGE("Failed to delete sticker info");
797         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
798     }
799
800     return ret;
801 }
802
803 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
804 {
805     int ret;
806     int record_id;
807     int type;
808
809     *reply_body = g_variant_new("()");
810     if (*reply_body == NULL) {
811         LOGE("Failed to create reply_body");
812         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
813     }
814
815     g_variant_get(parameters, "(ii)", &record_id, &type);
816
817     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
818     if (ret != STICKERD_SERVER_ERROR_NONE) {
819         LOGE("Failed to update sticker type");
820         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
821     }
822
823     return ret;
824 }
825
826 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
827 {
828     int ret;
829     int record_id;
830     int type;
831     char *app_id;
832     char *uri;
833
834     *reply_body = g_variant_new("()");
835     if (*reply_body == NULL) {
836         LOGE("Failed to create reply_body");
837         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
838     }
839
840     g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
841
842     if (type == 1) {
843         if (_check_file_exist(app_id, uri) == 0) {
844             uri = _convert_sticker_uri(uri, app_id);
845             if (!uri) {
846                 LOGE("failed to copy sticker file");
847                 return STICKERD_SERVER_ERROR_FILE_EXISTS;
848             }
849         } else {
850             return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
851         }
852     }
853
854     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
855     if (ret != STICKERD_SERVER_ERROR_NONE) {
856         LOGE("Failed to update sticker uri");
857         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
858     }
859
860     return ret;
861 }
862
863 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
864 {
865     int ret;
866     int record_id;
867     char *thumbnail;
868
869     *reply_body = g_variant_new("()");
870     if (*reply_body == NULL) {
871         LOGE("Failed to create reply_body");
872         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
873     }
874
875     g_variant_get(parameters, "(i&s)", &record_id, &thumbnail);
876
877     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
878     if (ret != STICKERD_SERVER_ERROR_NONE) {
879         LOGE("Failed to update sticker thumbnail");
880         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
881     }
882
883     return ret;
884 }
885
886 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
887 {
888     int ret;
889     int record_id;
890     char *description;
891
892     *reply_body = g_variant_new("()");
893     if (*reply_body == NULL) {
894         LOGE("Failed to create reply_body");
895         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
896     }
897
898     g_variant_get(parameters, "(i&s)", &record_id, &description);
899
900     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
901     if (ret != STICKERD_SERVER_ERROR_NONE) {
902         LOGE("Failed to update sticker description");
903         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
904     }
905
906     return ret;
907 }
908
909 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
910 {
911     int ret;
912     int record_id;
913     char *group;
914
915     *reply_body = g_variant_new("()");
916     if (*reply_body == NULL) {
917         LOGE("Failed to create reply_body");
918         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
919     }
920
921     g_variant_get(parameters, "(i&s)", &record_id, &group);
922
923     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
924     if (ret != STICKERD_SERVER_ERROR_NONE) {
925         LOGE("Failed to update sticker group");
926         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
927     }
928
929     return ret;
930 }
931
932 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
933 {
934     int ret;
935     int record_id;
936     GVariantIter *keyword_iter = NULL;
937     char *keyword = NULL;
938     GList *keyword_list = NULL;
939
940     *reply_body = g_variant_new("()");
941     if (*reply_body == NULL) {
942         LOGE("Failed to create reply_body");
943         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
944     }
945
946     g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
947
948     if (!keyword_iter) {
949         LOGD("failed to get iter");
950         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
951     }
952
953     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
954         keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
955     }
956
957     g_variant_iter_free(keyword_iter);
958
959     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
960     if (ret != STICKERD_SERVER_ERROR_NONE) {
961         LOGE("Failed to update sticker keyword");
962         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
963     }
964
965     return ret;
966 }
967
968 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
969 {
970     if (!keyword) {
971         LOGE("keyword doesn't exist");
972         return;
973     }
974
975     g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
976 }
977
978 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
979 {
980     int ret;
981     int record_id;
982     GVariantBuilder *info_builder;
983     GVariantBuilder *keyword_builder;
984
985     g_variant_get(parameters, "(i)", &record_id);
986     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
987
988     if (!sticker_info)
989         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
990
991     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
992     if (ret != STICKERD_SERVER_ERROR_NONE) {
993         LOGE("Failed to get sticker info");
994
995         if (sticker_info->app_id)
996             free(sticker_info->app_id);
997
998         if (sticker_info->uri)
999             free(sticker_info->uri);
1000
1001         if (sticker_info->thumbnail)
1002             free(sticker_info->thumbnail);
1003
1004         if (sticker_info->keyword)
1005             free(sticker_info->keyword);
1006
1007         if (sticker_info->group)
1008             free(sticker_info->group);
1009
1010         if (sticker_info->description)
1011             free(sticker_info->description);
1012
1013         if (sticker_info->date)
1014             free(sticker_info->date);
1015
1016         free(sticker_info);
1017         sticker_info = NULL;
1018         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1019     }
1020
1021     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1022     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1023     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1024     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1025     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1026     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1027     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1028     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1029     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1030
1031     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1032     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1033
1034     *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1035     g_variant_builder_unref(info_builder);
1036     g_variant_builder_unref(keyword_builder);
1037
1038     if (*reply_body == NULL) {
1039         LOGE("Failed to create reply_body");
1040         free(sticker_info);
1041         sticker_info = NULL;
1042         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1043     }
1044
1045     free(sticker_info);
1046     sticker_info = NULL;
1047     return ret;
1048 }
1049
1050 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1051 {
1052     int ret;
1053     GVariantBuilder *builder = NULL;
1054     char *app_id = NULL;
1055
1056     g_variant_get(parameters, "(&s)", &app_id);
1057
1058     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1059     ret = stickerd_db_get_group_list(builder, app_id);
1060     if (ret != STICKERD_SERVER_ERROR_NONE) {
1061         LOGE("Failed to get sticker group list");
1062         g_variant_builder_unref(builder);
1063         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1064     }
1065
1066     *reply_body = g_variant_new("(a(s))", builder);
1067     g_variant_builder_unref(builder);
1068
1069     if (*reply_body == NULL) {
1070         LOGE("Failed to create reply_body");
1071         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1072     }
1073
1074     return ret;
1075 }
1076
1077 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1078 {
1079     int ret;
1080     GVariantBuilder *builder = NULL;
1081     char *app_id = NULL;
1082
1083     g_variant_get(parameters, "(&s)", &app_id);
1084
1085     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1086     ret = stickerd_db_get_keyword_list(builder, app_id);
1087     if (ret != STICKERD_SERVER_ERROR_NONE) {
1088         LOGE("Failed to get sticker keyword list");
1089         g_variant_builder_unref(builder);
1090         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1091     }
1092
1093     *reply_body = g_variant_new("(a(s))", builder);
1094     g_variant_builder_unref(builder);
1095
1096     if (*reply_body == NULL) {
1097         LOGE("Failed to create reply_body");
1098         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1099     }
1100
1101     return ret;
1102 }
1103
1104 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1105 {
1106     int ret;
1107     int count;
1108     char *app_id = NULL;
1109
1110     g_variant_get(parameters, "(&s)", &app_id);
1111
1112     ret = stickerd_db_get_sticker_count(&count, app_id);
1113     if (ret != STICKERD_SERVER_ERROR_NONE) {
1114         LOGE("Failed to get sticker count");
1115         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1116     }
1117
1118     *reply_body = g_variant_new("(i)", count);
1119     if (*reply_body == NULL) {
1120         LOGE("Failed to create reply_body");
1121         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1122     }
1123
1124     return ret;
1125 }
1126
1127 #if 0
1128 // Send the sticker information by asynchronous communication.
1129 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1130 {
1131     int ret;
1132     char *cmd = NULL;
1133
1134     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1135
1136     if (!sticker_info)
1137         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1138
1139     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1140     if (ret != STICKERD_SERVER_ERROR_NONE) {
1141         LOGE("Failed to get sticker info");
1142         free(sticker_info);
1143         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1144     }
1145
1146     switch (type) {
1147         case STICKER_DB_STICKER_ALL:
1148         cmd = "send_all_sticker_info";
1149         break;
1150         case STICKER_DB_STICKER_APPID:
1151         cmd = "send_sticker_info_by_appid";
1152         break;
1153         case STICKER_DB_STICKER_TYPE:
1154         cmd = "send_sticker_info_by_type";
1155         break;
1156         case STICKER_DB_STICKER_GROUP:
1157         cmd = "send_sticker_info_by_group";
1158         break;
1159         case STICKER_DB_STICKER_KEYWORD:
1160         cmd = "send_sticker_info_by_keyword";
1161         break;
1162         default:
1163         cmd = "";
1164         break;
1165     }
1166
1167     GVariantBuilder *info_builder;
1168     GVariantBuilder *keyword_builder;
1169
1170     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1171     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1172     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1173     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1174     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1175     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1176     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1177     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1178     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1179
1180     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1181     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1182
1183     GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1184     g_variant_builder_unref(info_builder);
1185     g_variant_builder_unref(keyword_builder);
1186
1187     ret = stickerd_send_dbus_message(body, sender, cmd);
1188     if (ret != STICKERD_SERVER_ERROR_NONE) {
1189         LOGE("Failed to send sticker info to client");
1190         free(sticker_info);
1191         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1192     }
1193
1194     free(sticker_info);
1195     return ret;
1196 }
1197 #endif
1198
1199 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1200 {
1201     if (!id) {
1202         LOGE("id doesn't exist");
1203         return;
1204     }
1205
1206     g_variant_builder_add(id_builder, "(i)", atoi(id));
1207 }
1208
1209 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1210 {
1211     int ret;
1212     int offset, count;
1213     char *app_id = NULL;
1214     GList *id_list = NULL;
1215     GVariantBuilder *id_builder = NULL;
1216
1217     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1218
1219     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1220     if (ret != STICKERD_SERVER_ERROR_NONE) {
1221         LOGE("Failed to get all sticker id");
1222         if(id_list)
1223             g_list_free_full(id_list, free);
1224         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1225     }
1226
1227     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1228     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1229
1230     *reply_body = g_variant_new("(a(i))", id_builder);
1231     if (*reply_body == NULL) {
1232         LOGE("Failed to create reply_body");
1233         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1234     }
1235
1236     if (id_list)
1237         g_list_free_full(id_list, free);
1238
1239     if (id_builder)
1240         g_variant_builder_unref(id_builder);
1241
1242     return ret;
1243 }
1244
1245 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1246 {
1247     int ret;
1248     GList *id_list = NULL;
1249     char *app_id = NULL;
1250     int offset, count;
1251     GVariantBuilder *id_builder = NULL;
1252
1253     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1254
1255     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1256     if (ret != STICKERD_SERVER_ERROR_NONE) {
1257         LOGE("Failed to get all sticker id");
1258         if(id_list)
1259             g_list_free_full(id_list, free);
1260         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1261     }
1262
1263     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1264     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1265
1266     *reply_body = g_variant_new("(a(i))", id_builder);
1267     if (*reply_body == NULL) {
1268         LOGE("Failed to create reply_body");
1269         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1270     }
1271
1272     if (id_list)
1273         g_list_free_full(id_list, free);
1274
1275     if (id_builder)
1276         g_variant_builder_unref(id_builder);
1277
1278     return ret;
1279 }
1280
1281 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1282 {
1283     int ret;
1284     GList *id_list = NULL;
1285     char *app_id = NULL;
1286     int type, offset, count;
1287     GVariantBuilder *id_builder = NULL;
1288
1289     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1290
1291     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1292     if (ret != STICKERD_SERVER_ERROR_NONE) {
1293         LOGE("Failed to get all sticker id");
1294         if(id_list)
1295             g_list_free_full(id_list, free);
1296         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1297     }
1298
1299     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1300     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1301
1302     *reply_body = g_variant_new("(a(i))", id_builder);
1303     if (*reply_body == NULL) {
1304         LOGE("Failed to create reply_body");
1305         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1306     }
1307
1308     if (id_list)
1309         g_list_free_full(id_list, free);
1310
1311     if (id_builder)
1312         g_variant_builder_unref(id_builder);
1313
1314     return ret;
1315 }
1316
1317 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1318 {
1319     int ret;
1320     GList *id_list = NULL;
1321     char *app_id = NULL;
1322     char *group = NULL;
1323     int offset, count;
1324     GVariantBuilder *id_builder = NULL;
1325
1326     g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1327
1328     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1329     if (ret != STICKERD_SERVER_ERROR_NONE) {
1330         LOGE("Failed to get all sticker id");
1331         if(id_list)
1332             g_list_free_full(id_list, free);
1333         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1334     }
1335
1336     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1337     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1338
1339     *reply_body = g_variant_new("(a(i))", id_builder);
1340     if (*reply_body == NULL) {
1341         LOGE("Failed to create reply_body");
1342         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1343     }
1344
1345     if (id_list)
1346         g_list_free_full(id_list, free);
1347
1348     if (id_builder)
1349         g_variant_builder_unref(id_builder);
1350
1351     return ret;
1352 }
1353
1354 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1355 {
1356     int ret;
1357     GList *id_list = NULL;
1358     char *app_id = NULL;
1359     char *keyword = NULL;
1360     int offset, count;
1361     GVariantBuilder *id_builder = NULL;
1362
1363     g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1364
1365     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1366     if (ret != STICKERD_SERVER_ERROR_NONE) {
1367         LOGE("Failed to get all sticker id");
1368         if(id_list)
1369             g_list_free_full(id_list, free);
1370         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1371     }
1372
1373     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1374     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1375
1376     *reply_body = g_variant_new("(a(i))", id_builder);
1377     if (*reply_body == NULL) {
1378         LOGE("Failed to create reply_body");
1379         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1380     }
1381
1382     if (id_list)
1383         g_list_free_full(id_list, free);
1384
1385     if (id_builder)
1386         g_variant_builder_unref(id_builder);
1387
1388     return ret;
1389 }
1390
1391 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1392 {
1393     int ret;
1394     GList *id_list = NULL;
1395     char *app_id = NULL;
1396     int type, offset, count;
1397     GVariantBuilder *id_builder = NULL;
1398
1399     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1400
1401     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1402     if (ret != STICKERD_SERVER_ERROR_NONE) {
1403         LOGE("Failed to get all sticker id");
1404         if(id_list)
1405             g_list_free_full(id_list, free);
1406         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1407     }
1408
1409     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1410     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1411
1412     *reply_body = g_variant_new("(a(i))", id_builder);
1413     if (*reply_body == NULL) {
1414         LOGE("Failed to create reply_body");
1415         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1416     }
1417
1418     if (id_list)
1419         g_list_free_full(id_list, free);
1420
1421     if (id_builder)
1422         g_variant_builder_unref(id_builder);
1423
1424     return ret;
1425 }
1426
1427 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1428 {
1429     int ret;
1430     GVariantBuilder *builder = NULL;
1431     char *app_id = NULL;
1432     int disp_type;
1433
1434     g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1435
1436     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1437     ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1438     if (ret != STICKERD_SERVER_ERROR_NONE) {
1439         LOGE("Failed to get sticker group list");
1440         g_variant_builder_unref(builder);
1441         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1442     }
1443
1444     *reply_body = g_variant_new("(a(s))", builder);
1445     g_variant_builder_unref(builder);
1446
1447     if (*reply_body == NULL) {
1448         LOGE("Failed to create reply_body");
1449         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1450     }
1451
1452     return ret;
1453 }
1454
1455 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1456 {
1457     int ret;
1458     int record_id;
1459     int disp_type;
1460
1461     *reply_body = g_variant_new("()");
1462     if (*reply_body == NULL) {
1463         LOGE("Failed to create reply_body");
1464         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1465     }
1466
1467     g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1468
1469     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1470     if (ret != STICKERD_SERVER_ERROR_NONE) {
1471         LOGE("Failed to update sticker disp_type");
1472         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1473     }
1474
1475     return ret;
1476 }
1477
1478 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1479 {
1480     int ret;
1481     int result;
1482     char *uri = NULL;
1483
1484     g_variant_get(parameters, "(&s)", &uri);
1485
1486     ret = stickerd_db_check_file_exists(&result, uri);
1487     if (ret != STICKERD_SERVER_ERROR_NONE) {
1488         LOGE("Failed to get sticker count");
1489         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1490     }
1491
1492     *reply_body = g_variant_new("(i)", result);
1493     if (*reply_body == NULL) {
1494         LOGE("Failed to create reply_body");
1495         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1496     }
1497
1498     return ret;
1499 }