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