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