Support the event callback to send sticker DB changes
[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 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
623 {
624     int ret;
625     int record_id = 0;
626     STICKER_DAT_TYPE key;
627     sticker_info_db *sticker_info = NULL;
628     GVariant *value = NULL;
629     GVariantIter *info_iter = NULL;
630     GVariantIter *keyword_iter = NULL;
631     char *keyword;
632
633     g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
634     if (!info_iter || !keyword_iter) {
635         LOGD("failed to get iter");
636         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
637         goto cleanup;
638     }
639
640     sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
641
642     if (!sticker_info) {
643         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
644         goto cleanup;
645     }
646
647     while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
648         switch(key) {
649             case STICKER_DATA_TYPE_APP_ID:
650             sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
651             break;
652             case STICKER_DATA_TYPE_URI_TYPE:
653             sticker_info->type = g_variant_get_int32(value);
654             break;
655             case STICKER_DATA_TYPE_URI:
656             sticker_info->uri = (char *) g_variant_get_string(value, NULL);
657             break;
658             case STICKER_DATA_TYPE_THUMBNAIL:
659             sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
660             break;
661             case STICKER_DATA_TYPE_DESCRIPTION:
662             sticker_info->description = (char *) g_variant_get_string(value, NULL);
663             break;
664             case STICKER_DATA_TYPE_GROUP:
665             sticker_info->group = (char *) g_variant_get_string(value, NULL);
666             break;
667             case STICKER_DATA_TYPE_DISP_TYPE:
668             sticker_info->display_type = g_variant_get_int32(value);
669             default:
670             break;
671         }
672     }
673
674     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
675         sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
676     }
677
678     if (sticker_info->type == 1) {
679         if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
680             sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
681             if (!sticker_info->uri) {
682                 LOGE("failed to copy sticker file");
683                 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
684                 goto cleanup;
685             }
686         } else {
687             LOGE("sticker file does not exist");
688             ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
689             goto cleanup;
690         }
691     }
692
693     if (sticker_info->thumbnail) {
694         if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
695             sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
696             if (!sticker_info->thumbnail) {
697                 LOGE("failed to copy sticker thumbnail");
698                 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
699                 goto cleanup;
700             }
701         } else {
702             LOGE("sticker thumbnail does not exist");
703             ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
704             goto cleanup;
705         }
706     }
707
708     ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
709     if (ret != STICKERD_SERVER_ERROR_NONE) {
710         LOGE("Failed to insert sticker info");
711         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
712         goto cleanup;
713     }
714
715     *reply_body = g_variant_new("(i)", record_id);
716     if (*reply_body == NULL) {
717         LOGE("Failed to create reply_body");
718         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
719     } else
720         _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
721
722 cleanup:
723     if (value)
724         g_variant_unref(value);
725
726     if (info_iter)
727         g_variant_iter_free(info_iter);
728
729     if (keyword_iter)
730         g_variant_iter_free(keyword_iter);
731
732     if (sticker_info) {
733         free(sticker_info);
734         sticker_info = NULL;
735     }
736
737     return ret;
738 }
739
740 static char* _get_string_from_object(JsonObject *object, const char *key)
741 {
742     if (json_object_has_member(object, key) == false)
743         return NULL;
744
745     const char *str = json_object_get_string_member(object, key);
746     if (str != NULL)
747         return strdup(str);
748     else
749         return NULL;
750 }
751
752 static int _get_int_from_object(JsonObject *object, const char *key)
753 {
754     if (json_object_has_member(object, key) == false)
755         return -1;
756
757     int type = json_object_get_int_member(object, key);
758
759     return type;
760 }
761
762 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
763 {
764     int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
765     int record_id;
766     sticker_info_db *sticker_info = NULL;
767     char *app_id = NULL;
768     char *json_path = NULL;
769     JsonParser* parser = NULL;
770     GError* err_msg = NULL;
771     GVariant *body = NULL;
772     char *cmd = "send_insert_result";
773
774     *reply_body = g_variant_new("()");
775         if (*reply_body == NULL) {
776         LOGE("Failed to create reply_body");
777         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
778     }
779
780     g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
781
782     if (!app_id || !json_path) {
783         LOGE("failed to get parameter");
784         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
785     }
786
787     SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
788
789     parser = json_parser_new();
790     json_parser_load_from_file(parser, json_path, &err_msg);
791     if (err_msg) {
792         LOGE("failed to load json file. error message: %s", err_msg->message);
793         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
794         goto cleanup;
795     }
796
797     JsonNode *root = json_parser_get_root(parser);
798     if (root == NULL) {
799         LOGE("failed to get root");
800         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
801         goto cleanup;
802     }
803
804     JsonObject *root_obj = json_node_get_object(root);
805     if (root_obj == NULL) {
806         LOGE("failed to get object");
807         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
808         goto cleanup;
809     }
810
811     JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
812     if (sticker_arr == NULL) {
813         LOGE("failed to get array member");
814         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
815         goto cleanup;
816     }
817
818     int arr_len = json_array_get_length(sticker_arr);
819     for (int i = 0; i < arr_len; i++) {
820         JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
821         if (info_object != NULL) {
822             sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
823             if (!sticker_info) {
824                 LOGE("Failed to alloc memory");
825                 continue;
826             }
827
828             sticker_info->app_id = app_id;
829             sticker_info->type = _get_int_from_object(info_object, "type");
830             if (sticker_info->type < 1)
831                 goto free_memory;
832
833             sticker_info->uri = _get_string_from_object(info_object, "uri");
834             if (!sticker_info->uri)
835                 goto free_memory;
836
837             if (sticker_info->type == 1) {
838                 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
839                     sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
840                     if (!sticker_info->uri)
841                         goto free_memory;
842                 } else {
843                     goto free_memory;
844                 }
845             }
846
847             sticker_info->group = _get_string_from_object(info_object, "group");
848             if (!sticker_info->group)
849                 goto free_memory;
850
851             sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
852             if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
853                 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
854                     sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
855                     if (!sticker_info->thumbnail)
856                         goto free_memory;
857                 } else {
858                     goto free_memory;
859                 }
860             }
861
862             sticker_info->description = _get_string_from_object(info_object, "description");
863
864             sticker_info->display_type = _get_int_from_object(info_object, "display_type");
865
866             JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
867             int keyword_arr_len = json_array_get_length(keyword_arr);
868             if (keyword_arr_len < 1)
869                 goto free_memory;
870
871             for (int j = 0; j < keyword_arr_len; j++) {
872                 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
873             }
874
875             ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
876             if (ret != STICKERD_SERVER_ERROR_NONE) {
877                 LOGE("Failed to insert sticker info");
878                 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
879             } else
880                 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
881
882 free_memory:
883             free(sticker_info);
884             sticker_info = NULL;
885         }
886     }
887
888 cleanup:
889     if (err_msg)
890         g_error_free(err_msg);
891     if (parser)
892         g_object_unref(parser);
893
894     body = g_variant_new("(i)", ret);
895
896     ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
897     if (ret != STICKERD_SERVER_ERROR_NONE)
898         LOGE("Failed to send insert result to client");
899
900     if(body)
901         g_variant_unref(body);
902
903     return ret;
904 }
905
906 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
907 {
908     int ret;
909     int record_id;
910
911     *reply_body = g_variant_new("()");
912     if (*reply_body == NULL) {
913         LOGE("Failed to create reply_body");
914         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
915     }
916
917     g_variant_get(parameters, "(i)", &record_id);
918
919     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
920     if (sticker_info)
921         stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
922
923     ret = stickerd_db_delete_sticker_info(record_id);
924     if (ret != STICKERD_SERVER_ERROR_NONE) {
925         LOGE("Failed to delete sticker info");
926         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
927     } else {
928         if (sticker_info && sticker_info->uri) {
929             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
930             free(sticker_info);
931             sticker_info = NULL;
932         }
933     }
934
935     return ret;
936 }
937
938 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
939 {
940     int ret;
941     char *uri = NULL;
942
943     *reply_body = g_variant_new("()");
944     if (*reply_body == NULL) {
945         LOGE("Failed to create reply_body");
946         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
947     }
948
949     g_variant_get(parameters, "(&s)", &uri);
950
951     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
952     if (sticker_info)
953         stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
954
955     ret = stickerd_db_delete_sticker_info_by_uri(uri);
956     if (ret != STICKERD_SERVER_ERROR_NONE) {
957         LOGE("Failed to delete sticker info");
958         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
959     } else {
960         if (sticker_info && sticker_info->uri) {
961             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
962             free(sticker_info);
963             sticker_info = NULL;
964         }
965     }
966
967     return ret;
968 }
969
970 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
971 {
972     int ret;
973     int record_id;
974     int type;
975
976     *reply_body = g_variant_new("()");
977     if (*reply_body == NULL) {
978         LOGE("Failed to create reply_body");
979         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
980     }
981
982     g_variant_get(parameters, "(ii)", &record_id, &type);
983
984     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
985     if (ret != STICKERD_SERVER_ERROR_NONE) {
986         LOGE("Failed to update sticker type");
987         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
988     }
989
990     return ret;
991 }
992
993 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
994 {
995     int ret;
996     int record_id;
997     int type;
998     char *app_id;
999     char *uri;
1000
1001     *reply_body = g_variant_new("()");
1002     if (*reply_body == NULL) {
1003         LOGE("Failed to create reply_body");
1004         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1005     }
1006
1007     g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1008
1009     if (type == 1) {
1010         if (_check_file_exist(app_id, uri) == 0) {
1011             uri = _convert_sticker_uri(uri, app_id);
1012             if (!uri) {
1013                 LOGE("failed to copy sticker file");
1014                 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1015             }
1016         } else {
1017             return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1018         }
1019     }
1020
1021     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1022     if (ret != STICKERD_SERVER_ERROR_NONE) {
1023         LOGE("Failed to update sticker uri");
1024         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1025     }
1026
1027     return ret;
1028 }
1029
1030 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1031 {
1032     int ret;
1033     int record_id;
1034     char *app_id;
1035     char *thumbnail;
1036
1037     *reply_body = g_variant_new("()");
1038     if (*reply_body == NULL) {
1039         LOGE("Failed to create reply_body");
1040         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1041     }
1042
1043     g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1044
1045     if (_check_file_exist(app_id, thumbnail) == 0) {
1046         thumbnail = _convert_sticker_uri(thumbnail, app_id);
1047         if (!thumbnail) {
1048             LOGE("failed to copy sticker thumbnail");
1049             return STICKERD_SERVER_ERROR_FILE_EXISTS;
1050         }
1051     } else {
1052         return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1053     }
1054
1055     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1056     if (ret != STICKERD_SERVER_ERROR_NONE) {
1057         LOGE("Failed to update sticker thumbnail");
1058         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1059     }
1060
1061     return ret;
1062 }
1063
1064 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1065 {
1066     int ret;
1067     int record_id;
1068     char *description;
1069
1070     *reply_body = g_variant_new("()");
1071     if (*reply_body == NULL) {
1072         LOGE("Failed to create reply_body");
1073         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1074     }
1075
1076     g_variant_get(parameters, "(i&s)", &record_id, &description);
1077
1078     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1079     if (ret != STICKERD_SERVER_ERROR_NONE) {
1080         LOGE("Failed to update sticker description");
1081         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1082     }
1083
1084     return ret;
1085 }
1086
1087 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1088 {
1089     int ret;
1090     int record_id;
1091     char *group;
1092
1093     *reply_body = g_variant_new("()");
1094     if (*reply_body == NULL) {
1095         LOGE("Failed to create reply_body");
1096         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1097     }
1098
1099     g_variant_get(parameters, "(i&s)", &record_id, &group);
1100
1101     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1102     if (ret != STICKERD_SERVER_ERROR_NONE) {
1103         LOGE("Failed to update sticker group");
1104         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1105     }
1106
1107     return ret;
1108 }
1109
1110 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1111 {
1112     int ret;
1113     int record_id;
1114     GVariantIter *keyword_iter = NULL;
1115     char *keyword = NULL;
1116     GList *keyword_list = NULL;
1117
1118     *reply_body = g_variant_new("()");
1119     if (*reply_body == NULL) {
1120         LOGE("Failed to create reply_body");
1121         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1122     }
1123
1124     g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1125
1126     if (!keyword_iter) {
1127         LOGD("failed to get iter");
1128         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1129     }
1130
1131     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1132         keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1133     }
1134
1135     g_variant_iter_free(keyword_iter);
1136
1137     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1138     if (ret != STICKERD_SERVER_ERROR_NONE) {
1139         LOGE("Failed to update sticker keyword");
1140         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1141     }
1142
1143     return ret;
1144 }
1145
1146 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1147 {
1148     int ret;
1149     int record_id;
1150     GVariantBuilder *info_builder;
1151     GVariantBuilder *keyword_builder;
1152
1153     g_variant_get(parameters, "(i)", &record_id);
1154     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1155
1156     if (!sticker_info)
1157         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1158
1159     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1160     if (ret != STICKERD_SERVER_ERROR_NONE) {
1161         LOGE("Failed to get sticker info");
1162
1163         if (sticker_info->app_id)
1164             free(sticker_info->app_id);
1165
1166         if (sticker_info->uri)
1167             free(sticker_info->uri);
1168
1169         if (sticker_info->thumbnail)
1170             free(sticker_info->thumbnail);
1171
1172         if (sticker_info->keyword)
1173             free(sticker_info->keyword);
1174
1175         if (sticker_info->group)
1176             free(sticker_info->group);
1177
1178         if (sticker_info->description)
1179             free(sticker_info->description);
1180
1181         if (sticker_info->date)
1182             free(sticker_info->date);
1183
1184         free(sticker_info);
1185         sticker_info = NULL;
1186         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1187     }
1188
1189     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1190     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1191     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1192     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1193     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1194     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1195     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1196     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1197     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1198
1199     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1200     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1201
1202     *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1203     g_variant_builder_unref(info_builder);
1204     g_variant_builder_unref(keyword_builder);
1205
1206     if (*reply_body == NULL) {
1207         LOGE("Failed to create reply_body");
1208         free(sticker_info);
1209         sticker_info = NULL;
1210         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1211     }
1212
1213     free(sticker_info);
1214     sticker_info = NULL;
1215     return ret;
1216 }
1217
1218 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1219 {
1220     int ret;
1221     GVariantBuilder *builder = NULL;
1222     char *app_id = NULL;
1223
1224     g_variant_get(parameters, "(&s)", &app_id);
1225
1226     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1227     ret = stickerd_db_get_group_list(builder, app_id);
1228     if (ret != STICKERD_SERVER_ERROR_NONE) {
1229         LOGE("Failed to get sticker group list");
1230         g_variant_builder_unref(builder);
1231         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1232     }
1233
1234     *reply_body = g_variant_new("(a(s))", builder);
1235     g_variant_builder_unref(builder);
1236
1237     if (*reply_body == NULL) {
1238         LOGE("Failed to create reply_body");
1239         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1240     }
1241
1242     return ret;
1243 }
1244
1245 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1246 {
1247     int ret;
1248     GVariantBuilder *builder = NULL;
1249     char *app_id = NULL;
1250
1251     g_variant_get(parameters, "(&s)", &app_id);
1252
1253     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1254     ret = stickerd_db_get_keyword_list(builder, app_id);
1255     if (ret != STICKERD_SERVER_ERROR_NONE) {
1256         LOGE("Failed to get sticker keyword list");
1257         g_variant_builder_unref(builder);
1258         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1259     }
1260
1261     *reply_body = g_variant_new("(a(s))", builder);
1262     g_variant_builder_unref(builder);
1263
1264     if (*reply_body == NULL) {
1265         LOGE("Failed to create reply_body");
1266         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1267     }
1268
1269     return ret;
1270 }
1271
1272 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1273 {
1274     int ret;
1275     int count;
1276     char *app_id = NULL;
1277
1278     g_variant_get(parameters, "(&s)", &app_id);
1279
1280     ret = stickerd_db_get_sticker_count(&count, app_id);
1281     if (ret != STICKERD_SERVER_ERROR_NONE) {
1282         LOGE("Failed to get sticker count");
1283         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1284     }
1285
1286     *reply_body = g_variant_new("(i)", count);
1287     if (*reply_body == NULL) {
1288         LOGE("Failed to create reply_body");
1289         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1290     }
1291
1292     return ret;
1293 }
1294
1295 #if 0
1296 // Send the sticker information by asynchronous communication.
1297 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1298 {
1299     int ret;
1300     char *cmd = NULL;
1301
1302     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1303
1304     if (!sticker_info)
1305         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1306
1307     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1308     if (ret != STICKERD_SERVER_ERROR_NONE) {
1309         LOGE("Failed to get sticker info");
1310         free(sticker_info);
1311         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1312     }
1313
1314     switch (type) {
1315         case STICKER_DB_STICKER_ALL:
1316         cmd = "send_all_sticker_info";
1317         break;
1318         case STICKER_DB_STICKER_APPID:
1319         cmd = "send_sticker_info_by_appid";
1320         break;
1321         case STICKER_DB_STICKER_TYPE:
1322         cmd = "send_sticker_info_by_type";
1323         break;
1324         case STICKER_DB_STICKER_GROUP:
1325         cmd = "send_sticker_info_by_group";
1326         break;
1327         case STICKER_DB_STICKER_KEYWORD:
1328         cmd = "send_sticker_info_by_keyword";
1329         break;
1330         default:
1331         cmd = "";
1332         break;
1333     }
1334
1335     GVariantBuilder *info_builder;
1336     GVariantBuilder *keyword_builder;
1337
1338     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1339     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1340     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1341     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1342     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1343     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1344     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1345     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1346     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1347
1348     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1349     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1350
1351     GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1352     g_variant_builder_unref(info_builder);
1353     g_variant_builder_unref(keyword_builder);
1354
1355     ret = stickerd_send_dbus_message(body, sender, cmd);
1356     if (ret != STICKERD_SERVER_ERROR_NONE) {
1357         LOGE("Failed to send sticker info to client");
1358         free(sticker_info);
1359         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1360     }
1361
1362     free(sticker_info);
1363     return ret;
1364 }
1365 #endif
1366
1367 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1368 {
1369     if (!id) {
1370         LOGE("id doesn't exist");
1371         return;
1372     }
1373
1374     g_variant_builder_add(id_builder, "(i)", atoi(id));
1375 }
1376
1377 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1378 {
1379     int ret;
1380     int offset, count;
1381     char *app_id = NULL;
1382     GList *id_list = NULL;
1383     GVariantBuilder *id_builder = NULL;
1384
1385     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1386
1387     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1388     if (ret != STICKERD_SERVER_ERROR_NONE) {
1389         LOGE("Failed to get all sticker id");
1390         if(id_list)
1391             g_list_free_full(id_list, free);
1392         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1393     }
1394
1395     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1396     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1397
1398     *reply_body = g_variant_new("(a(i))", id_builder);
1399     if (*reply_body == NULL) {
1400         LOGE("Failed to create reply_body");
1401         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1402     }
1403
1404     if (id_list)
1405         g_list_free_full(id_list, free);
1406
1407     if (id_builder)
1408         g_variant_builder_unref(id_builder);
1409
1410     return ret;
1411 }
1412
1413 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1414 {
1415     int ret;
1416     GList *id_list = NULL;
1417     char *app_id = NULL;
1418     int offset, count;
1419     GVariantBuilder *id_builder = NULL;
1420
1421     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1422
1423     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1424     if (ret != STICKERD_SERVER_ERROR_NONE) {
1425         LOGE("Failed to get all sticker id");
1426         if(id_list)
1427             g_list_free_full(id_list, free);
1428         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1429     }
1430
1431     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1432     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1433
1434     *reply_body = g_variant_new("(a(i))", id_builder);
1435     if (*reply_body == NULL) {
1436         LOGE("Failed to create reply_body");
1437         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1438     }
1439
1440     if (id_list)
1441         g_list_free_full(id_list, free);
1442
1443     if (id_builder)
1444         g_variant_builder_unref(id_builder);
1445
1446     return ret;
1447 }
1448
1449 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1450 {
1451     int ret;
1452     GList *id_list = NULL;
1453     char *app_id = NULL;
1454     int type, offset, count;
1455     GVariantBuilder *id_builder = NULL;
1456
1457     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1458
1459     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1460     if (ret != STICKERD_SERVER_ERROR_NONE) {
1461         LOGE("Failed to get all sticker id");
1462         if(id_list)
1463             g_list_free_full(id_list, free);
1464         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1465     }
1466
1467     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1468     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1469
1470     *reply_body = g_variant_new("(a(i))", id_builder);
1471     if (*reply_body == NULL) {
1472         LOGE("Failed to create reply_body");
1473         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1474     }
1475
1476     if (id_list)
1477         g_list_free_full(id_list, free);
1478
1479     if (id_builder)
1480         g_variant_builder_unref(id_builder);
1481
1482     return ret;
1483 }
1484
1485 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1486 {
1487     int ret;
1488     GList *id_list = NULL;
1489     char *app_id = NULL;
1490     char *group = NULL;
1491     int offset, count;
1492     GVariantBuilder *id_builder = NULL;
1493
1494     g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1495
1496     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1497     if (ret != STICKERD_SERVER_ERROR_NONE) {
1498         LOGE("Failed to get all sticker id");
1499         if(id_list)
1500             g_list_free_full(id_list, free);
1501         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1502     }
1503
1504     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1505     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1506
1507     *reply_body = g_variant_new("(a(i))", id_builder);
1508     if (*reply_body == NULL) {
1509         LOGE("Failed to create reply_body");
1510         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1511     }
1512
1513     if (id_list)
1514         g_list_free_full(id_list, free);
1515
1516     if (id_builder)
1517         g_variant_builder_unref(id_builder);
1518
1519     return ret;
1520 }
1521
1522 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1523 {
1524     int ret;
1525     GList *id_list = NULL;
1526     char *app_id = NULL;
1527     char *keyword = NULL;
1528     int offset, count;
1529     GVariantBuilder *id_builder = NULL;
1530
1531     g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1532
1533     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1534     if (ret != STICKERD_SERVER_ERROR_NONE) {
1535         LOGE("Failed to get all sticker id");
1536         if(id_list)
1537             g_list_free_full(id_list, free);
1538         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1539     }
1540
1541     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1542     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1543
1544     *reply_body = g_variant_new("(a(i))", id_builder);
1545     if (*reply_body == NULL) {
1546         LOGE("Failed to create reply_body");
1547         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1548     }
1549
1550     if (id_list)
1551         g_list_free_full(id_list, free);
1552
1553     if (id_builder)
1554         g_variant_builder_unref(id_builder);
1555
1556     return ret;
1557 }
1558
1559 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1560 {
1561     int ret;
1562     GList *id_list = NULL;
1563     char *app_id = NULL;
1564     int type, offset, count;
1565     GVariantBuilder *id_builder = NULL;
1566
1567     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1568
1569     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1570     if (ret != STICKERD_SERVER_ERROR_NONE) {
1571         LOGE("Failed to get all sticker id");
1572         if(id_list)
1573             g_list_free_full(id_list, free);
1574         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1575     }
1576
1577     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1578     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1579
1580     *reply_body = g_variant_new("(a(i))", id_builder);
1581     if (*reply_body == NULL) {
1582         LOGE("Failed to create reply_body");
1583         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1584     }
1585
1586     if (id_list)
1587         g_list_free_full(id_list, free);
1588
1589     if (id_builder)
1590         g_variant_builder_unref(id_builder);
1591
1592     return ret;
1593 }
1594
1595 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1596 {
1597     int ret;
1598     GVariantBuilder *builder = NULL;
1599     char *app_id = NULL;
1600     int disp_type;
1601
1602     g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1603
1604     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1605     ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1606     if (ret != STICKERD_SERVER_ERROR_NONE) {
1607         LOGE("Failed to get sticker group list");
1608         g_variant_builder_unref(builder);
1609         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1610     }
1611
1612     *reply_body = g_variant_new("(a(s))", builder);
1613     g_variant_builder_unref(builder);
1614
1615     if (*reply_body == NULL) {
1616         LOGE("Failed to create reply_body");
1617         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1618     }
1619
1620     return ret;
1621 }
1622
1623 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1624 {
1625     int ret;
1626     int record_id;
1627     int disp_type;
1628
1629     *reply_body = g_variant_new("()");
1630     if (*reply_body == NULL) {
1631         LOGE("Failed to create reply_body");
1632         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1633     }
1634
1635     g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1636
1637     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1638     if (ret != STICKERD_SERVER_ERROR_NONE) {
1639         LOGE("Failed to update sticker disp_type");
1640         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1641     }
1642
1643     return ret;
1644 }
1645
1646 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1647 {
1648     int ret;
1649     int result;
1650     char *uri = NULL;
1651
1652     g_variant_get(parameters, "(&s)", &uri);
1653
1654     ret = stickerd_db_check_file_exists(&result, uri);
1655     if (ret != STICKERD_SERVER_ERROR_NONE) {
1656         LOGE("Failed to get sticker count");
1657         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1658     }
1659
1660     *reply_body = g_variant_new("(i)", result);
1661     if (*reply_body == NULL) {
1662         LOGE("Failed to create reply_body");
1663         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1664     }
1665
1666     return ret;
1667 }
1668
1669 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1670 {
1671     int ret;
1672     int record_id;
1673
1674     *reply_body = g_variant_new("()");
1675     if (*reply_body == NULL) {
1676         LOGE("Failed to create reply_body");
1677         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1678     }
1679
1680     g_variant_get(parameters, "(i)", &record_id);
1681
1682     ret = stickerd_db_insert_recent_sticker_info(record_id);
1683     if (ret != STICKERD_SERVER_ERROR_NONE) {
1684         LOGE("Failed to insert recent sticker info");
1685         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1686     }
1687
1688     return ret;
1689 }
1690
1691 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1692 {
1693     int ret;
1694     int count;
1695     GList *id_list = NULL;
1696     GVariantBuilder *id_builder = NULL;
1697
1698     g_variant_get(parameters, "(i)", &count);
1699
1700     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1701     if (ret != STICKERD_SERVER_ERROR_NONE) {
1702         LOGE("Failed to get recent sticker id");
1703         if(id_list)
1704             g_list_free_full(id_list, free);
1705         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1706     }
1707
1708     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1709     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1710
1711     *reply_body = g_variant_new("(a(i))", id_builder);
1712     if (*reply_body == NULL) {
1713         LOGE("Failed to create reply_body");
1714         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1715     }
1716
1717     if (id_list)
1718         g_list_free_full(id_list, free);
1719
1720     if (id_builder)
1721         g_variant_builder_unref(id_builder);
1722
1723     return ret;
1724 }
1725
1726 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1727 {
1728     int ret = STICKERD_SERVER_ERROR_NONE;
1729     int record_id;
1730
1731     *reply_body = g_variant_new("()");
1732     if (*reply_body == NULL) {
1733         LOGE("Failed to create reply_body");
1734         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1735     }
1736
1737     g_variant_get(parameters, "(i)", &record_id);
1738
1739     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1740     if (sticker_info) {
1741         ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1742         if (ret == STICKERD_SERVER_ERROR_NONE) {
1743             _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1744             free(sticker_info);
1745             sticker_info = NULL;
1746         }
1747     }
1748
1749     return ret;
1750 }