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