Merge branch 'tizen_5.5' into tizen
[platform/core/uifw/capi-ui-sticker.git] / server / stickerd_data_manager.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <tzplatform_config.h>
21 #include <gio/gunixfdlist.h>
22 #include <dlog.h>
23 #include <json-glib/json-glib.h>
24 #include <package_manager.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30
31 #include "stickerd_dbus.h"
32 #include "stickerd_data_manager.h"
33 #include "stickerd_db_manager.h"
34 #include "sticker_defs.h"
35 #include "stickerd_error.h"
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40 #define LOG_TAG "STICKERD_DATA_MANAGER"
41
42 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
43 #define MAX_ERROR_BUFFER  256
44
45 static GHashTable *_monitoring_hash = NULL;
46 static char error_buffer[MAX_ERROR_BUFFER];
47 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     if (fd == -1) {
476         LOGE("Failed to open file (%s) for reading", src);
477     }
478
479     n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
480     if (n_fd == -1) {
481         LOGE("Failed to open file (%s) for writing", dest);
482     }
483
484     if (fd == -1 || n_fd == -1) {
485         tmp_err = errno;
486         ret = -1;
487         goto cleanup;
488     }
489
490     while((size = read(fd, buf, 4096))) {
491         if (size == -1) {
492             if(errno == EINTR)
493                 continue;
494
495             tmp_err = errno;
496             ret = -1;
497             goto cleanup;
498         }
499
500         while(write(n_fd, buf, size) == -1) {
501             if(errno == EINTR) {
502                 continue;
503             } else {
504                 tmp_err = errno;
505                 goto cleanup;
506             }
507         }
508     }
509
510 cleanup:
511     if (fd != -1)
512         close(fd);
513
514     if (n_fd != -1)
515         close(n_fd);
516
517     errno = tmp_err;
518     return ret;
519 }
520
521 static char* _convert_sticker_uri(const char *uri, const char *appid)
522 {
523     int ret;
524     int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
525     char * new_path = (char *)calloc(len, sizeof(char));
526     if (new_path == NULL) {
527         LOGE("Failed to alloc memory");
528         return NULL;
529     }
530
531     if (uri[0] == '/')
532         snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
533     else
534         snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
535
536     if (access(new_path, F_OK) == 0) {
537         LOGE("sticker file already exists : %s", new_path);
538         ret = -1;
539         goto cleanup;
540     }
541
542     ret = _mkdirs(new_path, 0755);
543     if (ret != 0) {
544         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
545         LOGE("directory create error : %s", error_buffer);
546         goto cleanup;
547     }
548
549     ret = _file_copy(uri, new_path);
550     if (ret != 0) {
551         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
552         LOGE("failed to copy sticker file : %s", error_buffer);
553     }
554
555 cleanup:
556     if (ret == 0) {
557         return new_path;
558     } else {
559         free(new_path);
560         new_path = NULL;
561         return NULL;
562     }
563 }
564
565 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
566 {
567     if (!keyword) {
568         LOGE("keyword doesn't exist");
569         return;
570     }
571
572     g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
573 }
574
575 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
576 {
577     GVariantBuilder *info_builder;
578     GVariantBuilder *keyword_builder;
579
580     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
581     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
582     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
583     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
584     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
585     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
586     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
587     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
588     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
589
590     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
591     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
592
593     GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
594     g_variant_builder_unref(info_builder);
595     g_variant_builder_unref(keyword_builder);
596
597     if (body)
598         return body;
599     else
600         return NULL;
601 }
602
603 void _get_consumer_busname(gpointer data, gpointer user_data)
604 {
605     if (data == NULL)
606         return;
607
608     int ret;
609     char *cmd = "send_sticker_changed_event";
610     char *sender = (char *)data;
611     GVariant *body = (GVariant *)user_data;
612
613     ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
614     if (ret != STICKERD_SERVER_ERROR_NONE)
615         LOGE("Failed to send sticker changed event");
616 }
617
618 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
619 {
620     GVariant *body = _get_sticker_g_variant(type, sticker_info);
621
622     if (body)
623         g_list_foreach(consumer_list, _get_consumer_busname, body);
624
625     if (body)
626         g_variant_unref(body);
627 }
628
629 static void _free_sticker_data(sticker_info_db *sticker_data)
630 {
631     if (!sticker_data)
632         return;
633
634     if (sticker_data->app_id) {
635         free(sticker_data->app_id);
636         sticker_data->app_id = NULL;
637     }
638
639     if (sticker_data->uri) {
640         free(sticker_data->uri);
641         sticker_data->uri = NULL;
642     }
643
644     if (sticker_data->thumbnail) {
645         free(sticker_data->thumbnail);
646         sticker_data->thumbnail = NULL;
647     }
648
649     if (sticker_data->keyword) {
650         g_list_free_full(sticker_data->keyword, free);
651         sticker_data->keyword = NULL;
652     }
653
654     if (sticker_data->group) {
655         free(sticker_data->group);
656         sticker_data->group = NULL;
657     }
658
659     if (sticker_data->description) {
660         free(sticker_data->description);
661         sticker_data->description = NULL;
662     }
663
664     if (sticker_data->date) {
665         free(sticker_data->date);
666         sticker_data->date = NULL;
667     }
668
669     free(sticker_data);
670 }
671
672 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
673 {
674     int ret;
675     int record_id = 0;
676     STICKER_DAT_TYPE key;
677     sticker_info_db *sticker_info = NULL;
678     GVariant *value = NULL;
679     GVariantIter *info_iter = NULL;
680     GVariantIter *keyword_iter = NULL;
681     char *keyword;
682
683     g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
684     if (!info_iter || !keyword_iter) {
685         LOGD("failed to get iter");
686         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
687         goto cleanup;
688     }
689
690     sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
691
692     if (!sticker_info) {
693         ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
694         goto cleanup;
695     }
696
697     while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
698         switch(key) {
699             case STICKER_DATA_TYPE_APP_ID:
700             sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
701             break;
702             case STICKER_DATA_TYPE_URI_TYPE:
703             sticker_info->type = g_variant_get_int32(value);
704             break;
705             case STICKER_DATA_TYPE_URI:
706             sticker_info->uri = (char *) g_variant_get_string(value, NULL);
707             break;
708             case STICKER_DATA_TYPE_THUMBNAIL:
709             sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
710             break;
711             case STICKER_DATA_TYPE_DESCRIPTION:
712             sticker_info->description = (char *) g_variant_get_string(value, NULL);
713             break;
714             case STICKER_DATA_TYPE_GROUP:
715             sticker_info->group = (char *) g_variant_get_string(value, NULL);
716             break;
717             case STICKER_DATA_TYPE_DISP_TYPE:
718             sticker_info->display_type = g_variant_get_int32(value);
719             default:
720             break;
721         }
722     }
723
724     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
725         sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
726     }
727
728     if (sticker_info->type == 1) {
729         if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
730             sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
731             if (!sticker_info->uri) {
732                 LOGE("failed to copy sticker file");
733                 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
734                 goto cleanup;
735             }
736         } else {
737             LOGE("sticker file does not exist");
738             ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
739             goto cleanup;
740         }
741     }
742
743     if (sticker_info->thumbnail) {
744         if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
745             sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
746             if (!sticker_info->thumbnail) {
747                 LOGE("failed to copy sticker thumbnail");
748                 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
749                 goto cleanup;
750             }
751         } else {
752             LOGE("sticker thumbnail does not exist");
753             ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
754             goto cleanup;
755         }
756     }
757
758     ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
759     if (ret != STICKERD_SERVER_ERROR_NONE) {
760         LOGE("Failed to insert sticker info");
761         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
762         goto cleanup;
763     }
764
765     *reply_body = g_variant_new("(i)", record_id);
766     if (*reply_body == NULL) {
767         LOGE("Failed to create reply_body");
768         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
769     } else
770         _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
771
772 cleanup:
773     if (value)
774         g_variant_unref(value);
775
776     if (info_iter)
777         g_variant_iter_free(info_iter);
778
779     if (keyword_iter)
780         g_variant_iter_free(keyword_iter);
781
782     if (sticker_info) {
783         free(sticker_info);
784         sticker_info = NULL;
785     }
786
787     return ret;
788 }
789
790 static char* _get_string_from_object(JsonObject *object, const char *key)
791 {
792     if (json_object_has_member(object, key) == false)
793         return NULL;
794
795     const char *str = json_object_get_string_member(object, key);
796     if (str != NULL)
797         return strdup(str);
798     else
799         return NULL;
800 }
801
802 static int _get_int_from_object(JsonObject *object, const char *key)
803 {
804     if (json_object_has_member(object, key) == false)
805         return -1;
806
807     int type = json_object_get_int_member(object, key);
808
809     return type;
810 }
811
812 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
813 {
814     int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
815     int record_id;
816     sticker_info_db *sticker_info = NULL;
817     char *app_id = NULL;
818     char *json_path = NULL;
819     JsonParser* parser = NULL;
820     GError* err_msg = NULL;
821     GVariant *body = NULL;
822     char *cmd = "send_insert_result";
823
824     *reply_body = g_variant_new("()");
825         if (*reply_body == NULL) {
826         LOGE("Failed to create reply_body");
827         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
828     }
829
830     g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
831
832     if (!app_id || !json_path) {
833         LOGE("failed to get parameter");
834         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
835     }
836
837     SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
838
839     parser = json_parser_new();
840     json_parser_load_from_file(parser, json_path, &err_msg);
841     if (err_msg) {
842         LOGE("failed to load json file. error message: %s", err_msg->message);
843         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
844         goto cleanup;
845     }
846
847     JsonNode *root = json_parser_get_root(parser);
848     if (root == NULL) {
849         LOGE("failed to get root");
850         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
851         goto cleanup;
852     }
853
854     JsonObject *root_obj = json_node_get_object(root);
855     if (root_obj == NULL) {
856         LOGE("failed to get object");
857         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
858         goto cleanup;
859     }
860
861     JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
862     if (sticker_arr == NULL) {
863         LOGE("failed to get array member");
864         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
865         goto cleanup;
866     }
867
868     int arr_len = json_array_get_length(sticker_arr);
869     for (int i = 0; i < arr_len; i++) {
870         JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
871         if (info_object != NULL) {
872             sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
873             if (!sticker_info) {
874                 LOGE("Failed to alloc memory");
875                 continue;
876             }
877
878             sticker_info->app_id = strdup(app_id);
879             sticker_info->type = _get_int_from_object(info_object, "type");
880             if (sticker_info->type < 1)
881                 goto free_memory;
882
883             sticker_info->uri = _get_string_from_object(info_object, "uri");
884             if (!sticker_info->uri || sticker_info->uri[0] != '\0')
885                 goto free_memory;
886
887             if (sticker_info->type == 1) {
888                 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
889                     sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
890                     if (!sticker_info->uri)
891                         goto free_memory;
892                 } else {
893                     goto free_memory;
894                 }
895             }
896
897             sticker_info->group = _get_string_from_object(info_object, "group");
898             if (!sticker_info->group)
899                 goto free_memory;
900
901             sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
902             if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
903                 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
904                     sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
905                     if (!sticker_info->thumbnail)
906                         goto free_memory;
907                 } else {
908                     goto free_memory;
909                 }
910             }
911
912             sticker_info->description = _get_string_from_object(info_object, "description");
913
914             sticker_info->display_type = _get_int_from_object(info_object, "display_type");
915
916             JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
917             int keyword_arr_len = json_array_get_length(keyword_arr);
918             if (keyword_arr_len < 1)
919                 goto free_memory;
920
921             for (int j = 0; j < keyword_arr_len; j++) {
922                 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
923             }
924
925             ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
926             if (ret != STICKERD_SERVER_ERROR_NONE) {
927                 LOGE("Failed to insert sticker info");
928                 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
929             } else
930                 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
931
932 free_memory:
933             _free_sticker_data(sticker_info);
934             sticker_info = NULL;
935         }
936     }
937
938 cleanup:
939     if (err_msg)
940         g_error_free(err_msg);
941     if (parser)
942         g_object_unref(parser);
943
944     body = g_variant_new("(i)", ret);
945
946     ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
947     if (ret != STICKERD_SERVER_ERROR_NONE)
948         LOGE("Failed to send insert result to client");
949
950     if(body)
951         g_variant_unref(body);
952
953     return ret;
954 }
955
956 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
957 {
958     int ret;
959     int record_id;
960
961     *reply_body = g_variant_new("()");
962     if (*reply_body == NULL) {
963         LOGE("Failed to create reply_body");
964         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
965     }
966
967     g_variant_get(parameters, "(i)", &record_id);
968
969     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
970     if (sticker_info)
971         stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
972
973     ret = stickerd_db_delete_sticker_info(record_id);
974     if (ret != STICKERD_SERVER_ERROR_NONE) {
975         LOGE("Failed to delete sticker info");
976         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
977     } else {
978         if (sticker_info && sticker_info->uri)
979             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
980     }
981
982     if (sticker_info) {
983         _free_sticker_data(sticker_info);
984         sticker_info = NULL;
985     }
986
987     return ret;
988 }
989
990 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
991 {
992     int ret;
993     char *uri = NULL;
994
995     *reply_body = g_variant_new("()");
996     if (*reply_body == NULL) {
997         LOGE("Failed to create reply_body");
998         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
999     }
1000
1001     g_variant_get(parameters, "(&s)", &uri);
1002
1003     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1004     if (sticker_info)
1005         stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1006
1007     ret = stickerd_db_delete_sticker_info_by_uri(uri);
1008     if (ret != STICKERD_SERVER_ERROR_NONE) {
1009         LOGE("Failed to delete sticker info");
1010         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1011     } else {
1012         if (sticker_info && sticker_info->uri)
1013             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1014     }
1015
1016     if (sticker_info) {
1017         _free_sticker_data(sticker_info);
1018         sticker_info = NULL;
1019     }
1020
1021     return ret;
1022 }
1023
1024 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1025 {
1026     int ret;
1027     int record_id;
1028     int type;
1029
1030     *reply_body = g_variant_new("()");
1031     if (*reply_body == NULL) {
1032         LOGE("Failed to create reply_body");
1033         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1034     }
1035
1036     g_variant_get(parameters, "(ii)", &record_id, &type);
1037
1038     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1039     if (ret != STICKERD_SERVER_ERROR_NONE) {
1040         LOGE("Failed to update sticker type");
1041         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1042     }
1043
1044     return ret;
1045 }
1046
1047 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1048 {
1049     int ret;
1050     int record_id;
1051     int type;
1052     char *app_id;
1053     char *uri;
1054
1055     *reply_body = g_variant_new("()");
1056     if (*reply_body == NULL) {
1057         LOGE("Failed to create reply_body");
1058         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1059     }
1060
1061     g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1062
1063     if (type == 1) {
1064         if (_check_file_exist(app_id, uri) == 0) {
1065             uri = _convert_sticker_uri(uri, app_id);
1066             if (!uri) {
1067                 LOGE("failed to copy sticker file");
1068                 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1069             }
1070         } else {
1071             return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1072         }
1073     }
1074
1075     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1076     if (ret != STICKERD_SERVER_ERROR_NONE) {
1077         LOGE("Failed to update sticker uri");
1078         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1079     }
1080
1081     return ret;
1082 }
1083
1084 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1085 {
1086     int ret;
1087     int record_id;
1088     char *app_id;
1089     char *thumbnail;
1090
1091     *reply_body = g_variant_new("()");
1092     if (*reply_body == NULL) {
1093         LOGE("Failed to create reply_body");
1094         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1095     }
1096
1097     g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1098
1099     if (_check_file_exist(app_id, thumbnail) == 0) {
1100         thumbnail = _convert_sticker_uri(thumbnail, app_id);
1101         if (!thumbnail) {
1102             LOGE("failed to copy sticker thumbnail");
1103             return STICKERD_SERVER_ERROR_FILE_EXISTS;
1104         }
1105     } else {
1106         return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1107     }
1108
1109     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1110     if (ret != STICKERD_SERVER_ERROR_NONE) {
1111         LOGE("Failed to update sticker thumbnail");
1112         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1113     }
1114
1115     return ret;
1116 }
1117
1118 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1119 {
1120     int ret;
1121     int record_id;
1122     char *description;
1123
1124     *reply_body = g_variant_new("()");
1125     if (*reply_body == NULL) {
1126         LOGE("Failed to create reply_body");
1127         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1128     }
1129
1130     g_variant_get(parameters, "(i&s)", &record_id, &description);
1131
1132     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1133     if (ret != STICKERD_SERVER_ERROR_NONE) {
1134         LOGE("Failed to update sticker description");
1135         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1136     }
1137
1138     return ret;
1139 }
1140
1141 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1142 {
1143     int ret;
1144     int record_id;
1145     char *group;
1146
1147     *reply_body = g_variant_new("()");
1148     if (*reply_body == NULL) {
1149         LOGE("Failed to create reply_body");
1150         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1151     }
1152
1153     g_variant_get(parameters, "(i&s)", &record_id, &group);
1154
1155     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1156     if (ret != STICKERD_SERVER_ERROR_NONE) {
1157         LOGE("Failed to update sticker group");
1158         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1159     }
1160
1161     return ret;
1162 }
1163
1164 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1165 {
1166     int ret;
1167     int record_id;
1168     GVariantIter *keyword_iter = NULL;
1169     char *keyword = NULL;
1170     GList *keyword_list = NULL;
1171
1172     *reply_body = g_variant_new("()");
1173     if (*reply_body == NULL) {
1174         LOGE("Failed to create reply_body");
1175         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1176     }
1177
1178     g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1179
1180     if (!keyword_iter) {
1181         LOGD("failed to get iter");
1182         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1183     }
1184
1185     while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1186         keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1187     }
1188
1189     g_variant_iter_free(keyword_iter);
1190
1191     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1192     if (ret != STICKERD_SERVER_ERROR_NONE) {
1193         LOGE("Failed to update sticker keyword");
1194         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1195     }
1196
1197     return ret;
1198 }
1199
1200 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1201 {
1202     int ret;
1203     int record_id;
1204     GVariantBuilder *info_builder;
1205     GVariantBuilder *keyword_builder;
1206
1207     g_variant_get(parameters, "(i)", &record_id);
1208     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1209
1210     if (!sticker_info)
1211         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1212
1213     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1214     if (ret != STICKERD_SERVER_ERROR_NONE) {
1215         LOGE("Failed to get sticker info");
1216         _free_sticker_data(sticker_info);
1217         sticker_info = NULL;
1218         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1219     }
1220
1221     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1222     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1223     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1224     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1225     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1226     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1227     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1228     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1229     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1230
1231     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1232     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1233
1234     *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1235     g_variant_builder_unref(info_builder);
1236     g_variant_builder_unref(keyword_builder);
1237
1238     if (*reply_body == NULL) {
1239         LOGE("Failed to create reply_body");
1240         _free_sticker_data(sticker_info);
1241         sticker_info = NULL;
1242         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1243     }
1244
1245     _free_sticker_data(sticker_info);
1246     sticker_info = NULL;
1247
1248     return ret;
1249 }
1250
1251 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1252 {
1253     int ret;
1254     GVariantBuilder *builder = NULL;
1255     char *app_id = NULL;
1256
1257     g_variant_get(parameters, "(&s)", &app_id);
1258
1259     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1260     ret = stickerd_db_get_group_list(builder, app_id);
1261     if (ret != STICKERD_SERVER_ERROR_NONE) {
1262         LOGE("Failed to get sticker group list");
1263         g_variant_builder_unref(builder);
1264         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1265     }
1266
1267     *reply_body = g_variant_new("(a(s))", builder);
1268     g_variant_builder_unref(builder);
1269
1270     if (*reply_body == NULL) {
1271         LOGE("Failed to create reply_body");
1272         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1273     }
1274
1275     return ret;
1276 }
1277
1278 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1279 {
1280     int ret;
1281     GVariantBuilder *builder = NULL;
1282     char *app_id = NULL;
1283
1284     g_variant_get(parameters, "(&s)", &app_id);
1285
1286     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1287     ret = stickerd_db_get_keyword_list(builder, app_id);
1288     if (ret != STICKERD_SERVER_ERROR_NONE) {
1289         LOGE("Failed to get sticker keyword list");
1290         g_variant_builder_unref(builder);
1291         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1292     }
1293
1294     *reply_body = g_variant_new("(a(s))", builder);
1295     g_variant_builder_unref(builder);
1296
1297     if (*reply_body == NULL) {
1298         LOGE("Failed to create reply_body");
1299         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1300     }
1301
1302     return ret;
1303 }
1304
1305 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1306 {
1307     int ret;
1308     int count;
1309     char *app_id = NULL;
1310
1311     g_variant_get(parameters, "(&s)", &app_id);
1312
1313     ret = stickerd_db_get_sticker_count(&count, app_id);
1314     if (ret != STICKERD_SERVER_ERROR_NONE) {
1315         LOGE("Failed to get sticker count");
1316         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1317     }
1318
1319     *reply_body = g_variant_new("(i)", count);
1320     if (*reply_body == NULL) {
1321         LOGE("Failed to create reply_body");
1322         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1323     }
1324
1325     return ret;
1326 }
1327
1328 #if 0
1329 // Send the sticker information by asynchronous communication.
1330 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1331 {
1332     int ret;
1333     char *cmd = NULL;
1334
1335     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1336
1337     if (!sticker_info)
1338         return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1339
1340     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1341     if (ret != STICKERD_SERVER_ERROR_NONE) {
1342         LOGE("Failed to get sticker info");
1343         free(sticker_info);
1344         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1345     }
1346
1347     switch (type) {
1348         case STICKER_DB_STICKER_ALL:
1349         cmd = "send_all_sticker_info";
1350         break;
1351         case STICKER_DB_STICKER_APPID:
1352         cmd = "send_sticker_info_by_appid";
1353         break;
1354         case STICKER_DB_STICKER_TYPE:
1355         cmd = "send_sticker_info_by_type";
1356         break;
1357         case STICKER_DB_STICKER_GROUP:
1358         cmd = "send_sticker_info_by_group";
1359         break;
1360         case STICKER_DB_STICKER_KEYWORD:
1361         cmd = "send_sticker_info_by_keyword";
1362         break;
1363         default:
1364         cmd = "";
1365         break;
1366     }
1367
1368     GVariantBuilder *info_builder;
1369     GVariantBuilder *keyword_builder;
1370
1371     info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1372     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1373     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1374     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1375     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1376     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1377     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1378     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1379     g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1380
1381     keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1382     g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1383
1384     GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1385     g_variant_builder_unref(info_builder);
1386     g_variant_builder_unref(keyword_builder);
1387
1388     ret = stickerd_send_dbus_message(body, sender, cmd);
1389     if (ret != STICKERD_SERVER_ERROR_NONE) {
1390         LOGE("Failed to send sticker info to client");
1391         free(sticker_info);
1392         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1393     }
1394
1395     free(sticker_info);
1396     return ret;
1397 }
1398 #endif
1399
1400 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1401 {
1402     if (!id) {
1403         LOGE("id doesn't exist");
1404         return;
1405     }
1406
1407     g_variant_builder_add(id_builder, "(i)", atoi(id));
1408 }
1409
1410 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1411 {
1412     int ret;
1413     int offset, count;
1414     char *app_id = NULL;
1415     GList *id_list = NULL;
1416     GVariantBuilder *id_builder = NULL;
1417
1418     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1419
1420     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1421     if (ret != STICKERD_SERVER_ERROR_NONE) {
1422         LOGE("Failed to get all sticker id");
1423         if(id_list)
1424             g_list_free_full(id_list, free);
1425         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1426     }
1427
1428     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1429     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1430
1431     *reply_body = g_variant_new("(a(i))", id_builder);
1432     if (*reply_body == NULL) {
1433         LOGE("Failed to create reply_body");
1434         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1435     }
1436
1437     if (id_list)
1438         g_list_free_full(id_list, free);
1439
1440     if (id_builder)
1441         g_variant_builder_unref(id_builder);
1442
1443     return ret;
1444 }
1445
1446 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1447 {
1448     int ret;
1449     GList *id_list = NULL;
1450     char *app_id = NULL;
1451     int offset, count;
1452     GVariantBuilder *id_builder = NULL;
1453
1454     g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1455
1456     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1457     if (ret != STICKERD_SERVER_ERROR_NONE) {
1458         LOGE("Failed to get all sticker id");
1459         if(id_list)
1460             g_list_free_full(id_list, free);
1461         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1462     }
1463
1464     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1465     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1466
1467     *reply_body = g_variant_new("(a(i))", id_builder);
1468     if (*reply_body == NULL) {
1469         LOGE("Failed to create reply_body");
1470         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1471     }
1472
1473     if (id_list)
1474         g_list_free_full(id_list, free);
1475
1476     if (id_builder)
1477         g_variant_builder_unref(id_builder);
1478
1479     return ret;
1480 }
1481
1482 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1483 {
1484     int ret;
1485     GList *id_list = NULL;
1486     char *app_id = NULL;
1487     int type, offset, count;
1488     GVariantBuilder *id_builder = NULL;
1489
1490     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1491
1492     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1493     if (ret != STICKERD_SERVER_ERROR_NONE) {
1494         LOGE("Failed to get all sticker id");
1495         if(id_list)
1496             g_list_free_full(id_list, free);
1497         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1498     }
1499
1500     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1501     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1502
1503     *reply_body = g_variant_new("(a(i))", id_builder);
1504     if (*reply_body == NULL) {
1505         LOGE("Failed to create reply_body");
1506         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1507     }
1508
1509     if (id_list)
1510         g_list_free_full(id_list, free);
1511
1512     if (id_builder)
1513         g_variant_builder_unref(id_builder);
1514
1515     return ret;
1516 }
1517
1518 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1519 {
1520     int ret;
1521     GList *id_list = NULL;
1522     char *app_id = NULL;
1523     char *group = NULL;
1524     int offset, count;
1525     GVariantBuilder *id_builder = NULL;
1526
1527     g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1528
1529     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1530     if (ret != STICKERD_SERVER_ERROR_NONE) {
1531         LOGE("Failed to get all sticker id");
1532         if(id_list)
1533             g_list_free_full(id_list, free);
1534         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1535     }
1536
1537     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1538     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1539
1540     *reply_body = g_variant_new("(a(i))", id_builder);
1541     if (*reply_body == NULL) {
1542         LOGE("Failed to create reply_body");
1543         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1544     }
1545
1546     if (id_list)
1547         g_list_free_full(id_list, free);
1548
1549     if (id_builder)
1550         g_variant_builder_unref(id_builder);
1551
1552     return ret;
1553 }
1554
1555 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1556 {
1557     int ret;
1558     GList *id_list = NULL;
1559     char *app_id = NULL;
1560     char *keyword = NULL;
1561     int offset, count;
1562     GVariantBuilder *id_builder = NULL;
1563
1564     g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1565
1566     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1567     if (ret != STICKERD_SERVER_ERROR_NONE) {
1568         LOGE("Failed to get all sticker id");
1569         if(id_list)
1570             g_list_free_full(id_list, free);
1571         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1572     }
1573
1574     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1575     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1576
1577     *reply_body = g_variant_new("(a(i))", id_builder);
1578     if (*reply_body == NULL) {
1579         LOGE("Failed to create reply_body");
1580         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1581     }
1582
1583     if (id_list)
1584         g_list_free_full(id_list, free);
1585
1586     if (id_builder)
1587         g_variant_builder_unref(id_builder);
1588
1589     return ret;
1590 }
1591
1592 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1593 {
1594     int ret;
1595     GList *id_list = NULL;
1596     char *app_id = NULL;
1597     int type, offset, count;
1598     GVariantBuilder *id_builder = NULL;
1599
1600     g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1601
1602     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1603     if (ret != STICKERD_SERVER_ERROR_NONE) {
1604         LOGE("Failed to get all sticker id");
1605         if(id_list)
1606             g_list_free_full(id_list, free);
1607         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1608     }
1609
1610     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1611     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1612
1613     *reply_body = g_variant_new("(a(i))", id_builder);
1614     if (*reply_body == NULL) {
1615         LOGE("Failed to create reply_body");
1616         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1617     }
1618
1619     if (id_list)
1620         g_list_free_full(id_list, free);
1621
1622     if (id_builder)
1623         g_variant_builder_unref(id_builder);
1624
1625     return ret;
1626 }
1627
1628 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1629 {
1630     int ret;
1631     GVariantBuilder *builder = NULL;
1632     char *app_id = NULL;
1633     int disp_type;
1634
1635     g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1636
1637     builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1638     ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1639     if (ret != STICKERD_SERVER_ERROR_NONE) {
1640         LOGE("Failed to get sticker group list");
1641         g_variant_builder_unref(builder);
1642         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1643     }
1644
1645     *reply_body = g_variant_new("(a(s))", builder);
1646     g_variant_builder_unref(builder);
1647
1648     if (*reply_body == NULL) {
1649         LOGE("Failed to create reply_body");
1650         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1651     }
1652
1653     return ret;
1654 }
1655
1656 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1657 {
1658     int ret;
1659     int record_id;
1660     int disp_type;
1661
1662     *reply_body = g_variant_new("()");
1663     if (*reply_body == NULL) {
1664         LOGE("Failed to create reply_body");
1665         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1666     }
1667
1668     g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1669
1670     ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1671     if (ret != STICKERD_SERVER_ERROR_NONE) {
1672         LOGE("Failed to update sticker disp_type");
1673         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1674     }
1675
1676     return ret;
1677 }
1678
1679 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1680 {
1681     int ret;
1682     int result;
1683     char *uri = NULL;
1684
1685     g_variant_get(parameters, "(&s)", &uri);
1686
1687     ret = stickerd_db_check_file_exists(&result, uri);
1688     if (ret != STICKERD_SERVER_ERROR_NONE) {
1689         LOGE("Failed to get sticker count");
1690         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1691     }
1692
1693     *reply_body = g_variant_new("(i)", result);
1694     if (*reply_body == NULL) {
1695         LOGE("Failed to create reply_body");
1696         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1697     }
1698
1699     return ret;
1700 }
1701
1702 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1703 {
1704     int ret;
1705     int record_id;
1706
1707     *reply_body = g_variant_new("()");
1708     if (*reply_body == NULL) {
1709         LOGE("Failed to create reply_body");
1710         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1711     }
1712
1713     g_variant_get(parameters, "(i)", &record_id);
1714
1715     ret = stickerd_db_insert_recent_sticker_info(record_id);
1716     if (ret != STICKERD_SERVER_ERROR_NONE) {
1717         LOGE("Failed to insert recent sticker info");
1718         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1719     }
1720
1721     return ret;
1722 }
1723
1724 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1725 {
1726     int ret;
1727     int count;
1728     GList *id_list = NULL;
1729     GVariantBuilder *id_builder = NULL;
1730
1731     g_variant_get(parameters, "(i)", &count);
1732
1733     ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1734     if (ret != STICKERD_SERVER_ERROR_NONE) {
1735         LOGE("Failed to get recent sticker id");
1736         if(id_list)
1737             g_list_free_full(id_list, free);
1738         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1739     }
1740
1741     id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1742     g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1743
1744     *reply_body = g_variant_new("(a(i))", id_builder);
1745     if (*reply_body == NULL) {
1746         LOGE("Failed to create reply_body");
1747         ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1748     }
1749
1750     if (id_list)
1751         g_list_free_full(id_list, free);
1752
1753     if (id_builder)
1754         g_variant_builder_unref(id_builder);
1755
1756     return ret;
1757 }
1758
1759 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1760 {
1761     int ret = STICKERD_SERVER_ERROR_NONE;
1762     int record_id;
1763
1764     *reply_body = g_variant_new("()");
1765     if (*reply_body == NULL) {
1766         LOGE("Failed to create reply_body");
1767         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1768     }
1769
1770     g_variant_get(parameters, "(i)", &record_id);
1771
1772     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1773     if (sticker_info) {
1774         ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1775         if (ret == STICKERD_SERVER_ERROR_NONE)
1776             _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1777
1778         _free_sticker_data(sticker_info);
1779         sticker_info = NULL;
1780     }
1781
1782     return ret;
1783 }