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