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