Prevent memory leak
[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             sticker_info->type = _get_int_from_object(info_object, "type");
873             if (sticker_info->type < 1)
874                 goto free_memory;
875
876             sticker_info->uri = _get_string_from_object(info_object, "uri");
877             if (!sticker_info->uri)
878                 goto free_memory;
879
880             if (sticker_info->type == 1) {
881                 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
882                     sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
883                     if (!sticker_info->uri)
884                         goto free_memory;
885                 } else {
886                     goto free_memory;
887                 }
888             }
889
890             sticker_info->group = _get_string_from_object(info_object, "group");
891             if (!sticker_info->group)
892                 goto free_memory;
893
894             sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
895             if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
896                 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
897                     sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
898                     if (!sticker_info->thumbnail)
899                         goto free_memory;
900                 } else {
901                     goto free_memory;
902                 }
903             }
904
905             sticker_info->description = _get_string_from_object(info_object, "description");
906
907             sticker_info->display_type = _get_int_from_object(info_object, "display_type");
908
909             JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
910             int keyword_arr_len = json_array_get_length(keyword_arr);
911             if (keyword_arr_len < 1)
912                 goto free_memory;
913
914             for (int j = 0; j < keyword_arr_len; j++) {
915                 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
916             }
917
918             ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
919             if (ret != STICKERD_SERVER_ERROR_NONE) {
920                 LOGE("Failed to insert sticker info");
921                 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
922             } else
923                 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
924
925 free_memory:
926             _free_sticker_data(sticker_info);
927             sticker_info = NULL;
928         }
929     }
930
931 cleanup:
932     if (err_msg)
933         g_error_free(err_msg);
934     if (parser)
935         g_object_unref(parser);
936
937     body = g_variant_new("(i)", ret);
938
939     ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
940     if (ret != STICKERD_SERVER_ERROR_NONE)
941         LOGE("Failed to send insert result to client");
942
943     if(body)
944         g_variant_unref(body);
945
946     return ret;
947 }
948
949 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
950 {
951     int ret;
952     int record_id;
953
954     *reply_body = g_variant_new("()");
955     if (*reply_body == NULL) {
956         LOGE("Failed to create reply_body");
957         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
958     }
959
960     g_variant_get(parameters, "(i)", &record_id);
961
962     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
963     if (sticker_info)
964         stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
965
966     ret = stickerd_db_delete_sticker_info(record_id);
967     if (ret != STICKERD_SERVER_ERROR_NONE) {
968         LOGE("Failed to delete sticker info");
969         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
970     } else {
971         if (sticker_info && sticker_info->uri)
972             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
973     }
974
975     if (sticker_info) {
976         _free_sticker_data(sticker_info);
977         sticker_info = NULL;
978     }
979
980     return ret;
981 }
982
983 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
984 {
985     int ret;
986     char *uri = NULL;
987
988     *reply_body = g_variant_new("()");
989     if (*reply_body == NULL) {
990         LOGE("Failed to create reply_body");
991         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
992     }
993
994     g_variant_get(parameters, "(&s)", &uri);
995
996     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
997     if (sticker_info)
998         stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
999
1000     ret = stickerd_db_delete_sticker_info_by_uri(uri);
1001     if (ret != STICKERD_SERVER_ERROR_NONE) {
1002         LOGE("Failed to delete sticker info");
1003         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1004     } else {
1005         if (sticker_info && sticker_info->uri)
1006             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1007     }
1008
1009     if (sticker_info) {
1010         _free_sticker_data(sticker_info);
1011         sticker_info = NULL;
1012     }
1013
1014     return ret;
1015 }
1016
1017 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1018 {
1019     int ret;
1020     int record_id;
1021     int type;
1022
1023     *reply_body = g_variant_new("()");
1024     if (*reply_body == NULL) {
1025         LOGE("Failed to create reply_body");
1026         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1027     }
1028
1029     g_variant_get(parameters, "(ii)", &record_id, &type);
1030
1031     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1032     if (ret != STICKERD_SERVER_ERROR_NONE) {
1033         LOGE("Failed to update sticker type");
1034         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1035     }
1036
1037     return ret;
1038 }
1039
1040 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1041 {
1042     int ret;
1043     int record_id;
1044     int type;
1045     char *app_id;
1046     char *uri;
1047
1048     *reply_body = g_variant_new("()");
1049     if (*reply_body == NULL) {
1050         LOGE("Failed to create reply_body");
1051         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1052     }
1053
1054     g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1055
1056     if (type == 1) {
1057         if (_check_file_exist(app_id, uri) == 0) {
1058             uri = _convert_sticker_uri(uri, app_id);
1059             if (!uri) {
1060                 LOGE("failed to copy sticker file");
1061                 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1062             }
1063         } else {
1064             return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1065         }
1066     }
1067
1068     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1069     if (ret != STICKERD_SERVER_ERROR_NONE) {
1070         LOGE("Failed to update sticker uri");
1071         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1072     }
1073
1074     return ret;
1075 }
1076
1077 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1078 {
1079     int ret;
1080     int record_id;
1081     char *app_id;
1082     char *thumbnail;
1083
1084     *reply_body = g_variant_new("()");
1085     if (*reply_body == NULL) {
1086         LOGE("Failed to create reply_body");
1087         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1088     }
1089
1090     g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1091
1092     if (_check_file_exist(app_id, thumbnail) == 0) {
1093         thumbnail = _convert_sticker_uri(thumbnail, app_id);
1094         if (!thumbnail) {
1095             LOGE("failed to copy sticker thumbnail");
1096             return STICKERD_SERVER_ERROR_FILE_EXISTS;
1097         }
1098     } else {
1099         return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1100     }
1101
1102     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1103     if (ret != STICKERD_SERVER_ERROR_NONE) {
1104         LOGE("Failed to update sticker thumbnail");
1105         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1106     }
1107
1108     return ret;
1109 }
1110
1111 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1112 {
1113     int ret;
1114     int record_id;
1115     char *description;
1116
1117     *reply_body = g_variant_new("()");
1118     if (*reply_body == NULL) {
1119         LOGE("Failed to create reply_body");
1120         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1121     }
1122
1123     g_variant_get(parameters, "(i&s)", &record_id, &description);
1124
1125     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1126     if (ret != STICKERD_SERVER_ERROR_NONE) {
1127         LOGE("Failed to update sticker description");
1128         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1129     }
1130
1131     return ret;
1132 }
1133
1134 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1135 {
1136     int ret;
1137     int record_id;
1138     char *group;
1139
1140     *reply_body = g_variant_new("()");
1141     if (*reply_body == NULL) {
1142         LOGE("Failed to create reply_body");
1143         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1144     }
1145
1146     g_variant_get(parameters, "(i&s)", &record_id, &group);
1147
1148     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1149     if (ret != STICKERD_SERVER_ERROR_NONE) {
1150         LOGE("Failed to update sticker group");
1151         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1152     }
1153
1154     return ret;
1155 }
1156
1157 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1158 {
1159     int ret;
1160     int record_id;
1161     GVariantIter *keyword_iter = NULL;
1162     char *keyword = NULL;
1163     GList *keyword_list = NULL;
1164
1165     *reply_body = g_variant_new("()");
1166     if (*reply_body == NULL) {
1167         LOGE("Failed to create reply_body");
1168         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1169     }
1170
1171     g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1172
1173     if (!keyword_iter) {
1174         LOGD("failed to get iter");
1175         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1176     }
1177
1178     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1179         keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1180     }
1181
1182     g_variant_iter_free(keyword_iter);
1183
1184     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1185     if (ret != STICKERD_SERVER_ERROR_NONE) {
1186         LOGE("Failed to update sticker keyword");
1187         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1188     }
1189
1190     return ret;
1191 }
1192
1193 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1194 {
1195     int ret;
1196     int record_id;
1197     GVariantBuilder *info_builder;
1198     GVariantBuilder *keyword_builder;
1199
1200     g_variant_get(parameters, "(i)", &record_id);
1201     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1202
1203     if (!sticker_info)
1204         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1205
1206     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1207     if (ret != STICKERD_SERVER_ERROR_NONE) {
1208         LOGE("Failed to get sticker info");
1209         _free_sticker_data(sticker_info);
1210         sticker_info = NULL;
1211         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1212     }
1213
1214     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1215     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1216     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1217     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1218     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1219     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1220     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1221     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1222     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1223
1224     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1225     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1226
1227     *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1228     g_variant_builder_unref(info_builder);
1229     g_variant_builder_unref(keyword_builder);
1230
1231     if (*reply_body == NULL) {
1232         LOGE("Failed to create reply_body");
1233         _free_sticker_data(sticker_info);
1234         sticker_info = NULL;
1235         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1236     }
1237
1238     _free_sticker_data(sticker_info);
1239     sticker_info = NULL;
1240
1241     return ret;
1242 }
1243
1244 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1245 {
1246     int ret;
1247     GVariantBuilder *builder = NULL;
1248     char *app_id = NULL;
1249
1250     g_variant_get(parameters, "(&s)", &app_id);
1251
1252     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1253     ret = stickerd_db_get_group_list(builder, app_id);
1254     if (ret != STICKERD_SERVER_ERROR_NONE) {
1255         LOGE("Failed to get sticker group list");
1256         g_variant_builder_unref(builder);
1257         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1258     }
1259
1260     *reply_body = g_variant_new("(a(s))", builder);
1261     g_variant_builder_unref(builder);
1262
1263     if (*reply_body == NULL) {
1264         LOGE("Failed to create reply_body");
1265         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1266     }
1267
1268     return ret;
1269 }
1270
1271 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1272 {
1273     int ret;
1274     GVariantBuilder *builder = NULL;
1275     char *app_id = NULL;
1276
1277     g_variant_get(parameters, "(&s)", &app_id);
1278
1279     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1280     ret = stickerd_db_get_keyword_list(builder, app_id);
1281     if (ret != STICKERD_SERVER_ERROR_NONE) {
1282         LOGE("Failed to get sticker keyword list");
1283         g_variant_builder_unref(builder);
1284         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1285     }
1286
1287     *reply_body = g_variant_new("(a(s))", builder);
1288     g_variant_builder_unref(builder);
1289
1290     if (*reply_body == NULL) {
1291         LOGE("Failed to create reply_body");
1292         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1293     }
1294
1295     return ret;
1296 }
1297
1298 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1299 {
1300     int ret;
1301     int count;
1302     char *app_id = NULL;
1303
1304     g_variant_get(parameters, "(&s)", &app_id);
1305
1306     ret = stickerd_db_get_sticker_count(&count, app_id);
1307     if (ret != STICKERD_SERVER_ERROR_NONE) {
1308         LOGE("Failed to get sticker count");
1309         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1310     }
1311
1312     *reply_body = g_variant_new("(i)", count);
1313     if (*reply_body == NULL) {
1314         LOGE("Failed to create reply_body");
1315         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1316     }
1317
1318     return ret;
1319 }
1320
1321 #if 0
1322 // Send the sticker information by asynchronous communication.
1323 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1324 {
1325     int ret;
1326     char *cmd = NULL;
1327
1328     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1329
1330     if (!sticker_info)
1331         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1332
1333     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1334     if (ret != STICKERD_SERVER_ERROR_NONE) {
1335         LOGE("Failed to get sticker info");
1336         free(sticker_info);
1337         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1338     }
1339
1340     switch (type) {
1341         case STICKER_DB_STICKER_ALL:
1342         cmd = "send_all_sticker_info";
1343         break;
1344         case STICKER_DB_STICKER_APPID:
1345         cmd = "send_sticker_info_by_appid";
1346         break;
1347         case STICKER_DB_STICKER_TYPE:
1348         cmd = "send_sticker_info_by_type";
1349         break;
1350         case STICKER_DB_STICKER_GROUP:
1351         cmd = "send_sticker_info_by_group";
1352         break;
1353         case STICKER_DB_STICKER_KEYWORD:
1354         cmd = "send_sticker_info_by_keyword";
1355         break;
1356         default:
1357         cmd = "";
1358         break;
1359     }
1360
1361     GVariantBuilder *info_builder;
1362     GVariantBuilder *keyword_builder;
1363
1364     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1365     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1366     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1367     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1368     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1369     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1370     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1371     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1372     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1373
1374     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1375     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1376
1377     GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1378     g_variant_builder_unref(info_builder);
1379     g_variant_builder_unref(keyword_builder);
1380
1381     ret = stickerd_send_dbus_message(body, sender, cmd);
1382     if (ret != STICKERD_SERVER_ERROR_NONE) {
1383         LOGE("Failed to send sticker info to client");
1384         free(sticker_info);
1385         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1386     }
1387
1388     free(sticker_info);
1389     return ret;
1390 }
1391 #endif
1392
1393 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1394 {
1395     if (!id) {
1396         LOGE("id doesn't exist");
1397         return;
1398     }
1399
1400     g_variant_builder_add(id_builder, "(i)", atoi(id));
1401 }
1402
1403 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1404 {
1405     int ret;
1406     int offset, count;
1407     char *app_id = NULL;
1408     GList *id_list = NULL;
1409     GVariantBuilder *id_builder = NULL;
1410
1411     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1412
1413     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1414     if (ret != STICKERD_SERVER_ERROR_NONE) {
1415         LOGE("Failed to get all sticker id");
1416         if(id_list)
1417             g_list_free_full(id_list, free);
1418         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1419     }
1420
1421     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1422     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1423
1424     *reply_body = g_variant_new("(a(i))", id_builder);
1425     if (*reply_body == NULL) {
1426         LOGE("Failed to create reply_body");
1427         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1428     }
1429
1430     if (id_list)
1431         g_list_free_full(id_list, free);
1432
1433     if (id_builder)
1434         g_variant_builder_unref(id_builder);
1435
1436     return ret;
1437 }
1438
1439 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1440 {
1441     int ret;
1442     GList *id_list = NULL;
1443     char *app_id = NULL;
1444     int offset, count;
1445     GVariantBuilder *id_builder = NULL;
1446
1447     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1448
1449     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1450     if (ret != STICKERD_SERVER_ERROR_NONE) {
1451         LOGE("Failed to get all sticker id");
1452         if(id_list)
1453             g_list_free_full(id_list, free);
1454         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1455     }
1456
1457     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1458     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1459
1460     *reply_body = g_variant_new("(a(i))", id_builder);
1461     if (*reply_body == NULL) {
1462         LOGE("Failed to create reply_body");
1463         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1464     }
1465
1466     if (id_list)
1467         g_list_free_full(id_list, free);
1468
1469     if (id_builder)
1470         g_variant_builder_unref(id_builder);
1471
1472     return ret;
1473 }
1474
1475 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1476 {
1477     int ret;
1478     GList *id_list = NULL;
1479     char *app_id = NULL;
1480     int type, offset, count;
1481     GVariantBuilder *id_builder = NULL;
1482
1483     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1484
1485     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1486     if (ret != STICKERD_SERVER_ERROR_NONE) {
1487         LOGE("Failed to get all sticker id");
1488         if(id_list)
1489             g_list_free_full(id_list, free);
1490         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1491     }
1492
1493     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1494     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1495
1496     *reply_body = g_variant_new("(a(i))", id_builder);
1497     if (*reply_body == NULL) {
1498         LOGE("Failed to create reply_body");
1499         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1500     }
1501
1502     if (id_list)
1503         g_list_free_full(id_list, free);
1504
1505     if (id_builder)
1506         g_variant_builder_unref(id_builder);
1507
1508     return ret;
1509 }
1510
1511 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1512 {
1513     int ret;
1514     GList *id_list = NULL;
1515     char *app_id = NULL;
1516     char *group = NULL;
1517     int offset, count;
1518     GVariantBuilder *id_builder = NULL;
1519
1520     g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1521
1522     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1523     if (ret != STICKERD_SERVER_ERROR_NONE) {
1524         LOGE("Failed to get all sticker id");
1525         if(id_list)
1526             g_list_free_full(id_list, free);
1527         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1528     }
1529
1530     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1531     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1532
1533     *reply_body = g_variant_new("(a(i))", id_builder);
1534     if (*reply_body == NULL) {
1535         LOGE("Failed to create reply_body");
1536         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1537     }
1538
1539     if (id_list)
1540         g_list_free_full(id_list, free);
1541
1542     if (id_builder)
1543         g_variant_builder_unref(id_builder);
1544
1545     return ret;
1546 }
1547
1548 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1549 {
1550     int ret;
1551     GList *id_list = NULL;
1552     char *app_id = NULL;
1553     char *keyword = NULL;
1554     int offset, count;
1555     GVariantBuilder *id_builder = NULL;
1556
1557     g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1558
1559     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1560     if (ret != STICKERD_SERVER_ERROR_NONE) {
1561         LOGE("Failed to get all sticker id");
1562         if(id_list)
1563             g_list_free_full(id_list, free);
1564         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1565     }
1566
1567     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1568     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1569
1570     *reply_body = g_variant_new("(a(i))", id_builder);
1571     if (*reply_body == NULL) {
1572         LOGE("Failed to create reply_body");
1573         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1574     }
1575
1576     if (id_list)
1577         g_list_free_full(id_list, free);
1578
1579     if (id_builder)
1580         g_variant_builder_unref(id_builder);
1581
1582     return ret;
1583 }
1584
1585 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1586 {
1587     int ret;
1588     GList *id_list = NULL;
1589     char *app_id = NULL;
1590     int type, offset, count;
1591     GVariantBuilder *id_builder = NULL;
1592
1593     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1594
1595     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1596     if (ret != STICKERD_SERVER_ERROR_NONE) {
1597         LOGE("Failed to get all sticker id");
1598         if(id_list)
1599             g_list_free_full(id_list, free);
1600         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1601     }
1602
1603     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1604     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1605
1606     *reply_body = g_variant_new("(a(i))", id_builder);
1607     if (*reply_body == NULL) {
1608         LOGE("Failed to create reply_body");
1609         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1610     }
1611
1612     if (id_list)
1613         g_list_free_full(id_list, free);
1614
1615     if (id_builder)
1616         g_variant_builder_unref(id_builder);
1617
1618     return ret;
1619 }
1620
1621 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1622 {
1623     int ret;
1624     GVariantBuilder *builder = NULL;
1625     char *app_id = NULL;
1626     int disp_type;
1627
1628     g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1629
1630     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1631     ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1632     if (ret != STICKERD_SERVER_ERROR_NONE) {
1633         LOGE("Failed to get sticker group list");
1634         g_variant_builder_unref(builder);
1635         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1636     }
1637
1638     *reply_body = g_variant_new("(a(s))", builder);
1639     g_variant_builder_unref(builder);
1640
1641     if (*reply_body == NULL) {
1642         LOGE("Failed to create reply_body");
1643         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1644     }
1645
1646     return ret;
1647 }
1648
1649 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1650 {
1651     int ret;
1652     int record_id;
1653     int disp_type;
1654
1655     *reply_body = g_variant_new("()");
1656     if (*reply_body == NULL) {
1657         LOGE("Failed to create reply_body");
1658         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1659     }
1660
1661     g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1662
1663     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1664     if (ret != STICKERD_SERVER_ERROR_NONE) {
1665         LOGE("Failed to update sticker disp_type");
1666         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1667     }
1668
1669     return ret;
1670 }
1671
1672 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1673 {
1674     int ret;
1675     int result;
1676     char *uri = NULL;
1677
1678     g_variant_get(parameters, "(&s)", &uri);
1679
1680     ret = stickerd_db_check_file_exists(&result, uri);
1681     if (ret != STICKERD_SERVER_ERROR_NONE) {
1682         LOGE("Failed to get sticker count");
1683         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1684     }
1685
1686     *reply_body = g_variant_new("(i)", result);
1687     if (*reply_body == NULL) {
1688         LOGE("Failed to create reply_body");
1689         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1690     }
1691
1692     return ret;
1693 }
1694
1695 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1696 {
1697     int ret;
1698     int record_id;
1699
1700     *reply_body = g_variant_new("()");
1701     if (*reply_body == NULL) {
1702         LOGE("Failed to create reply_body");
1703         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1704     }
1705
1706     g_variant_get(parameters, "(i)", &record_id);
1707
1708     ret = stickerd_db_insert_recent_sticker_info(record_id);
1709     if (ret != STICKERD_SERVER_ERROR_NONE) {
1710         LOGE("Failed to insert recent sticker info");
1711         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1712     }
1713
1714     return ret;
1715 }
1716
1717 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1718 {
1719     int ret;
1720     int count;
1721     GList *id_list = NULL;
1722     GVariantBuilder *id_builder = NULL;
1723
1724     g_variant_get(parameters, "(i)", &count);
1725
1726     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1727     if (ret != STICKERD_SERVER_ERROR_NONE) {
1728         LOGE("Failed to get recent sticker id");
1729         if(id_list)
1730             g_list_free_full(id_list, free);
1731         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1732     }
1733
1734     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1735     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1736
1737     *reply_body = g_variant_new("(a(i))", id_builder);
1738     if (*reply_body == NULL) {
1739         LOGE("Failed to create reply_body");
1740         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1741     }
1742
1743     if (id_list)
1744         g_list_free_full(id_list, free);
1745
1746     if (id_builder)
1747         g_variant_builder_unref(id_builder);
1748
1749     return ret;
1750 }
1751
1752 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1753 {
1754     int ret = STICKERD_SERVER_ERROR_NONE;
1755     int record_id;
1756
1757     *reply_body = g_variant_new("()");
1758     if (*reply_body == NULL) {
1759         LOGE("Failed to create reply_body");
1760         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1761     }
1762
1763     g_variant_get(parameters, "(i)", &record_id);
1764
1765     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1766     if (sticker_info) {
1767         ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1768         if (ret == STICKERD_SERVER_ERROR_NONE)
1769             _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1770
1771         _free_sticker_data(sticker_info);
1772         sticker_info = NULL;
1773     }
1774
1775     return ret;
1776 }