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