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