2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <tzplatform_config.h>
21 #include <gio/gunixfdlist.h>
23 #include <json-glib/json-glib.h>
24 #include <package_manager.h>
26 #include <sys/types.h>
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"
40 #define LOG_TAG "STICKERD_DATA_MANAGER"
42 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
43 #define MAX_ERROR_BUFFER 256
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;
50 static void _check_watcher_exist()
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);
58 g_main_loop_quit(main_loop);
62 static void _on_name_appeared(GDBusConnection *connection,
64 const gchar *name_owner,
67 LOGD("name: %s", name);
70 static void _on_name_vanished(GDBusConnection *connection,
74 monitoring_info_s *info = (monitoring_info_s *)user_data;
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);
83 if (g_list_find(consumer_list, info->bus_name))
84 consumer_list = g_list_remove(consumer_list, info->bus_name);
92 _check_watcher_exist();
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,
99 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
101 if (_monitoring_hash == NULL)
102 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
104 if (consumer_list == NULL)
105 consumer_list = g_list_alloc();
107 GVariant *reply_body = NULL;
108 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
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);
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);
173 LOGE("method_call failed, method_name : %s", method_name);
174 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
177 _check_watcher_exist();
180 static const GDBusInterfaceVTable _sticker_interface_vtable = {
181 _stickerd_client_dbus_method_call_handler,
186 int stickerd_register_dbus_interface(void)
188 static gchar introspection_xml[] =
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'/>"
196 " <method name='sticker_service_unregister'>"
197 " <arg type='i' name='lib_type' direction='in'/>"
198 " <arg type='i' name='watcher_id' direction='in'/>"
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'/>"
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'/>"
212 " <method name='delete_sticker_info'>"
213 " <arg type='i' name='record_id' direction='in'/>"
216 " <method name='delete_sticker_info_by_uri'>"
217 " <arg type='s' name='uri' direction='in'/>"
220 " <method name='update_sticker_type'>"
221 " <arg type='i' name='record_id' direction='in'/>"
222 " <arg type='i' name='type' direction='in'/>"
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'/>"
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'/>"
238 " <method name='update_sticker_description'>"
239 " <arg type='i' name='record_id' direction='in'/>"
240 " <arg type='s' name='description' direction='in'/>"
243 " <method name='update_sticker_group'>"
244 " <arg type='i' name='record_id' direction='in'/>"
245 " <arg type='s' name='group' direction='in'/>"
248 " <method name='update_sticker_keyword'>"
249 " <arg type='i' name='record_id' direction='in'/>"
250 " <arg type='a(s)' name='keyword' direction='in'/>"
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'/>"
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'/>"
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'/>"
269 " <method name='get_sticker_count'>"
270 " <arg type='s' name='app_id' direction='in'/>"
271 " <arg type='i' name='count' direction='out'/>"
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'/>"
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'/>"
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'/>"
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'/>"
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'/>"
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'/>"
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'/>"
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'/>"
331 " <method name='check_file_exists'>"
332 " <arg type='s' name='uri' direction='in'/>"
333 " <arg type='i' name='result' direction='out'/>"
336 " <method name='insert_recent_sticker_info'>"
337 " <arg type='i' name='record_id' direction='in'/>"
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'/>"
345 " <method name='send_update_event'>"
346 " <arg type='i' name='record_id' direction='in'/>"
351 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
354 int stickerd_dbus_init(void)
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;
364 return STICKERD_SERVER_ERROR_NONE;
367 static int _check_file_exist(const char *app_id, const char *path)
370 package_info_h package_info = NULL;
371 char *app_path = NULL;
372 char *file_path = NULL;
374 if (access(path, F_OK) == 0) {
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);
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);
393 int path_len = strlen(app_path) + strlen(path) + 2;
394 file_path = (char *)calloc(path_len, sizeof(char));
396 LOGE("Failed to alloc memory");
402 snprintf(file_path, path_len, "%s%s",app_path, path);
404 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
406 if (access(file_path, F_OK) != 0) {
407 LOGE("%s does not exist", file_path);
414 package_info_destroy(package_info);
429 static int _mkdirs(const char *path, mode_t mode)
432 char prev_path[2048];
433 const char *tmp = path;
435 if (!path || strlen(path) > 2048)
438 memset(prev_path, '\0', 2048);
439 while ((tmp = strchr(tmp, '/')) != NULL) {
446 strncpy(prev_path, path, len);
447 prev_path[len] = 0x00;
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);
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);
465 static int _file_copy(const char *src, const char *dest)
468 int fd = -1, n_fd = -1;
473 memset(buf, '\0', 4096);
474 fd = open(src, O_RDONLY);
475 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
477 if (fd == -1 || n_fd == -1) {
483 while((size = read(fd, buf, 4096))) {
493 while(write(n_fd, buf, size) == -1) {
514 static char* _convert_sticker_uri(const char *uri, const char *appid)
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");
525 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
527 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
529 if (access(new_path, F_OK) == 0) {
530 LOGE("sticker file already exists : %s", new_path);
535 ret = _mkdirs(new_path, 0755);
537 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
538 LOGE("directory create error : %s", error_buffer);
542 ret = _file_copy(uri, new_path);
544 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
545 LOGE("failed to copy sticker file : %s", error_buffer);
558 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
561 LOGE("keyword doesn't exist");
565 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
568 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
570 GVariantBuilder *info_builder;
571 GVariantBuilder *keyword_builder;
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));
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);
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);
596 void _get_consumer_busname(gpointer data, gpointer user_data)
602 char *cmd = "send_sticker_changed_event";
603 char *sender = (char *)data;
604 GVariant *body = (GVariant *)user_data;
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");
611 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
613 GVariant *body = _get_sticker_g_variant(type, sticker_info);
616 g_list_foreach(consumer_list, _get_consumer_busname, body);
619 g_variant_unref(body);
622 static void _free_sticker_data(sticker_info_db *sticker_data)
627 if (sticker_data->app_id) {
628 free(sticker_data->app_id);
629 sticker_data->app_id = NULL;
632 if (sticker_data->uri) {
633 free(sticker_data->uri);
634 sticker_data->uri = NULL;
637 if (sticker_data->thumbnail) {
638 free(sticker_data->thumbnail);
639 sticker_data->thumbnail = NULL;
642 if (sticker_data->keyword) {
643 g_list_free_full(sticker_data->keyword, free);
644 sticker_data->keyword = NULL;
647 if (sticker_data->group) {
648 free(sticker_data->group);
649 sticker_data->group = NULL;
652 if (sticker_data->description) {
653 free(sticker_data->description);
654 sticker_data->description = NULL;
657 if (sticker_data->date) {
658 free(sticker_data->date);
659 sticker_data->date = NULL;
665 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
669 STICKER_DAT_TYPE key;
670 sticker_info_db *sticker_info = NULL;
671 GVariant *value = NULL;
672 GVariantIter *info_iter = NULL;
673 GVariantIter *keyword_iter = NULL;
676 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
677 if (!info_iter || !keyword_iter) {
678 LOGD("failed to get iter");
679 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
683 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
686 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
690 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
692 case STICKER_DATA_TYPE_APP_ID:
693 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
695 case STICKER_DATA_TYPE_URI_TYPE:
696 sticker_info->type = g_variant_get_int32(value);
698 case STICKER_DATA_TYPE_URI:
699 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
701 case STICKER_DATA_TYPE_THUMBNAIL:
702 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
704 case STICKER_DATA_TYPE_DESCRIPTION:
705 sticker_info->description = (char *) g_variant_get_string(value, NULL);
707 case STICKER_DATA_TYPE_GROUP:
708 sticker_info->group = (char *) g_variant_get_string(value, NULL);
710 case STICKER_DATA_TYPE_DISP_TYPE:
711 sticker_info->display_type = g_variant_get_int32(value);
717 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
718 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
721 if (sticker_info->type == 1) {
722 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
723 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
724 if (!sticker_info->uri) {
725 LOGE("failed to copy sticker file");
726 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
730 LOGE("sticker file does not exist");
731 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
736 if (sticker_info->thumbnail) {
737 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
738 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
739 if (!sticker_info->thumbnail) {
740 LOGE("failed to copy sticker thumbnail");
741 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
745 LOGE("sticker thumbnail does not exist");
746 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
751 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
752 if (ret != STICKERD_SERVER_ERROR_NONE) {
753 LOGE("Failed to insert sticker info");
754 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
758 *reply_body = g_variant_new("(i)", record_id);
759 if (*reply_body == NULL) {
760 LOGE("Failed to create reply_body");
761 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
763 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
767 g_variant_unref(value);
770 g_variant_iter_free(info_iter);
773 g_variant_iter_free(keyword_iter);
783 static char* _get_string_from_object(JsonObject *object, const char *key)
785 if (json_object_has_member(object, key) == false)
788 const char *str = json_object_get_string_member(object, key);
795 static int _get_int_from_object(JsonObject *object, const char *key)
797 if (json_object_has_member(object, key) == false)
800 int type = json_object_get_int_member(object, key);
805 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
807 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
809 sticker_info_db *sticker_info = NULL;
811 char *json_path = NULL;
812 JsonParser* parser = NULL;
813 GError* err_msg = NULL;
814 GVariant *body = NULL;
815 char *cmd = "send_insert_result";
817 *reply_body = g_variant_new("()");
818 if (*reply_body == NULL) {
819 LOGE("Failed to create reply_body");
820 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
823 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
825 if (!app_id || !json_path) {
826 LOGE("failed to get parameter");
827 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
830 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
832 parser = json_parser_new();
833 json_parser_load_from_file(parser, json_path, &err_msg);
835 LOGE("failed to load json file. error message: %s", err_msg->message);
836 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
840 JsonNode *root = json_parser_get_root(parser);
842 LOGE("failed to get root");
843 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
847 JsonObject *root_obj = json_node_get_object(root);
848 if (root_obj == NULL) {
849 LOGE("failed to get object");
850 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
854 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
855 if (sticker_arr == NULL) {
856 LOGE("failed to get array member");
857 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
861 int arr_len = json_array_get_length(sticker_arr);
862 for (int i = 0; i < arr_len; i++) {
863 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
864 if (info_object != NULL) {
865 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
867 LOGE("Failed to alloc memory");
871 sticker_info->app_id = strdup(app_id);
872 sticker_info->type = _get_int_from_object(info_object, "type");
873 if (sticker_info->type < 1)
876 sticker_info->uri = _get_string_from_object(info_object, "uri");
877 if (!sticker_info->uri)
880 if (sticker_info->type == 1) {
881 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
882 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
883 if (!sticker_info->uri)
890 sticker_info->group = _get_string_from_object(info_object, "group");
891 if (!sticker_info->group)
894 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
895 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
896 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
897 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
898 if (!sticker_info->thumbnail)
905 sticker_info->description = _get_string_from_object(info_object, "description");
907 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
909 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
910 int keyword_arr_len = json_array_get_length(keyword_arr);
911 if (keyword_arr_len < 1)
914 for (int j = 0; j < keyword_arr_len; j++) {
915 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
918 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
919 if (ret != STICKERD_SERVER_ERROR_NONE) {
920 LOGE("Failed to insert sticker info");
921 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
923 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
926 _free_sticker_data(sticker_info);
933 g_error_free(err_msg);
935 g_object_unref(parser);
937 body = g_variant_new("(i)", ret);
939 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
940 if (ret != STICKERD_SERVER_ERROR_NONE)
941 LOGE("Failed to send insert result to client");
944 g_variant_unref(body);
949 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
954 *reply_body = g_variant_new("()");
955 if (*reply_body == NULL) {
956 LOGE("Failed to create reply_body");
957 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
960 g_variant_get(parameters, "(i)", &record_id);
962 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
964 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
966 ret = stickerd_db_delete_sticker_info(record_id);
967 if (ret != STICKERD_SERVER_ERROR_NONE) {
968 LOGE("Failed to delete sticker info");
969 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
971 if (sticker_info && sticker_info->uri)
972 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
976 _free_sticker_data(sticker_info);
983 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
988 *reply_body = g_variant_new("()");
989 if (*reply_body == NULL) {
990 LOGE("Failed to create reply_body");
991 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
994 g_variant_get(parameters, "(&s)", &uri);
996 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
998 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1000 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1001 if (ret != STICKERD_SERVER_ERROR_NONE) {
1002 LOGE("Failed to delete sticker info");
1003 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1005 if (sticker_info && sticker_info->uri)
1006 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1010 _free_sticker_data(sticker_info);
1011 sticker_info = NULL;
1017 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1023 *reply_body = g_variant_new("()");
1024 if (*reply_body == NULL) {
1025 LOGE("Failed to create reply_body");
1026 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1029 g_variant_get(parameters, "(ii)", &record_id, &type);
1031 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1032 if (ret != STICKERD_SERVER_ERROR_NONE) {
1033 LOGE("Failed to update sticker type");
1034 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1040 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1048 *reply_body = g_variant_new("()");
1049 if (*reply_body == NULL) {
1050 LOGE("Failed to create reply_body");
1051 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1054 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1057 if (_check_file_exist(app_id, uri) == 0) {
1058 uri = _convert_sticker_uri(uri, app_id);
1060 LOGE("failed to copy sticker file");
1061 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1064 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1068 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1069 if (ret != STICKERD_SERVER_ERROR_NONE) {
1070 LOGE("Failed to update sticker uri");
1071 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1077 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1084 *reply_body = g_variant_new("()");
1085 if (*reply_body == NULL) {
1086 LOGE("Failed to create reply_body");
1087 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1090 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1092 if (_check_file_exist(app_id, thumbnail) == 0) {
1093 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1095 LOGE("failed to copy sticker thumbnail");
1096 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1099 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1102 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1103 if (ret != STICKERD_SERVER_ERROR_NONE) {
1104 LOGE("Failed to update sticker thumbnail");
1105 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1111 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1117 *reply_body = g_variant_new("()");
1118 if (*reply_body == NULL) {
1119 LOGE("Failed to create reply_body");
1120 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1123 g_variant_get(parameters, "(i&s)", &record_id, &description);
1125 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1126 if (ret != STICKERD_SERVER_ERROR_NONE) {
1127 LOGE("Failed to update sticker description");
1128 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1134 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1140 *reply_body = g_variant_new("()");
1141 if (*reply_body == NULL) {
1142 LOGE("Failed to create reply_body");
1143 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1146 g_variant_get(parameters, "(i&s)", &record_id, &group);
1148 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1149 if (ret != STICKERD_SERVER_ERROR_NONE) {
1150 LOGE("Failed to update sticker group");
1151 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1157 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1161 GVariantIter *keyword_iter = NULL;
1162 char *keyword = NULL;
1163 GList *keyword_list = NULL;
1165 *reply_body = g_variant_new("()");
1166 if (*reply_body == NULL) {
1167 LOGE("Failed to create reply_body");
1168 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1171 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1173 if (!keyword_iter) {
1174 LOGD("failed to get iter");
1175 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1178 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1179 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1182 g_variant_iter_free(keyword_iter);
1184 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1185 if (ret != STICKERD_SERVER_ERROR_NONE) {
1186 LOGE("Failed to update sticker keyword");
1187 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1193 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1197 GVariantBuilder *info_builder;
1198 GVariantBuilder *keyword_builder;
1200 g_variant_get(parameters, "(i)", &record_id);
1201 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1204 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1206 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1207 if (ret != STICKERD_SERVER_ERROR_NONE) {
1208 LOGE("Failed to get sticker info");
1209 _free_sticker_data(sticker_info);
1210 sticker_info = NULL;
1211 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1214 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1215 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1216 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1217 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1218 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1219 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1220 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1221 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1222 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1224 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1225 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1227 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1228 g_variant_builder_unref(info_builder);
1229 g_variant_builder_unref(keyword_builder);
1231 if (*reply_body == NULL) {
1232 LOGE("Failed to create reply_body");
1233 _free_sticker_data(sticker_info);
1234 sticker_info = NULL;
1235 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1238 _free_sticker_data(sticker_info);
1239 sticker_info = NULL;
1244 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1247 GVariantBuilder *builder = NULL;
1248 char *app_id = NULL;
1250 g_variant_get(parameters, "(&s)", &app_id);
1252 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1253 ret = stickerd_db_get_group_list(builder, app_id);
1254 if (ret != STICKERD_SERVER_ERROR_NONE) {
1255 LOGE("Failed to get sticker group list");
1256 g_variant_builder_unref(builder);
1257 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1260 *reply_body = g_variant_new("(a(s))", builder);
1261 g_variant_builder_unref(builder);
1263 if (*reply_body == NULL) {
1264 LOGE("Failed to create reply_body");
1265 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1271 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1274 GVariantBuilder *builder = NULL;
1275 char *app_id = NULL;
1277 g_variant_get(parameters, "(&s)", &app_id);
1279 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1280 ret = stickerd_db_get_keyword_list(builder, app_id);
1281 if (ret != STICKERD_SERVER_ERROR_NONE) {
1282 LOGE("Failed to get sticker keyword list");
1283 g_variant_builder_unref(builder);
1284 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1287 *reply_body = g_variant_new("(a(s))", builder);
1288 g_variant_builder_unref(builder);
1290 if (*reply_body == NULL) {
1291 LOGE("Failed to create reply_body");
1292 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1298 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1302 char *app_id = NULL;
1304 g_variant_get(parameters, "(&s)", &app_id);
1306 ret = stickerd_db_get_sticker_count(&count, app_id);
1307 if (ret != STICKERD_SERVER_ERROR_NONE) {
1308 LOGE("Failed to get sticker count");
1309 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1312 *reply_body = g_variant_new("(i)", count);
1313 if (*reply_body == NULL) {
1314 LOGE("Failed to create reply_body");
1315 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1322 // Send the sticker information by asynchronous communication.
1323 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1328 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1331 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1333 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1334 if (ret != STICKERD_SERVER_ERROR_NONE) {
1335 LOGE("Failed to get sticker info");
1337 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1341 case STICKER_DB_STICKER_ALL:
1342 cmd = "send_all_sticker_info";
1344 case STICKER_DB_STICKER_APPID:
1345 cmd = "send_sticker_info_by_appid";
1347 case STICKER_DB_STICKER_TYPE:
1348 cmd = "send_sticker_info_by_type";
1350 case STICKER_DB_STICKER_GROUP:
1351 cmd = "send_sticker_info_by_group";
1353 case STICKER_DB_STICKER_KEYWORD:
1354 cmd = "send_sticker_info_by_keyword";
1361 GVariantBuilder *info_builder;
1362 GVariantBuilder *keyword_builder;
1364 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1365 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1366 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1367 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1368 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1369 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1370 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1371 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1372 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1374 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1375 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1377 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1378 g_variant_builder_unref(info_builder);
1379 g_variant_builder_unref(keyword_builder);
1381 ret = stickerd_send_dbus_message(body, sender, cmd);
1382 if (ret != STICKERD_SERVER_ERROR_NONE) {
1383 LOGE("Failed to send sticker info to client");
1385 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1393 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1396 LOGE("id doesn't exist");
1400 g_variant_builder_add(id_builder, "(i)", atoi(id));
1403 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1407 char *app_id = NULL;
1408 GList *id_list = NULL;
1409 GVariantBuilder *id_builder = NULL;
1411 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1413 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1414 if (ret != STICKERD_SERVER_ERROR_NONE) {
1415 LOGE("Failed to get all sticker id");
1417 g_list_free_full(id_list, free);
1418 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1421 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1422 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1424 *reply_body = g_variant_new("(a(i))", id_builder);
1425 if (*reply_body == NULL) {
1426 LOGE("Failed to create reply_body");
1427 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1431 g_list_free_full(id_list, free);
1434 g_variant_builder_unref(id_builder);
1439 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1442 GList *id_list = NULL;
1443 char *app_id = NULL;
1445 GVariantBuilder *id_builder = NULL;
1447 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1449 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1450 if (ret != STICKERD_SERVER_ERROR_NONE) {
1451 LOGE("Failed to get all sticker id");
1453 g_list_free_full(id_list, free);
1454 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1457 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1458 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1460 *reply_body = g_variant_new("(a(i))", id_builder);
1461 if (*reply_body == NULL) {
1462 LOGE("Failed to create reply_body");
1463 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1467 g_list_free_full(id_list, free);
1470 g_variant_builder_unref(id_builder);
1475 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1478 GList *id_list = NULL;
1479 char *app_id = NULL;
1480 int type, offset, count;
1481 GVariantBuilder *id_builder = NULL;
1483 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1485 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1486 if (ret != STICKERD_SERVER_ERROR_NONE) {
1487 LOGE("Failed to get all sticker id");
1489 g_list_free_full(id_list, free);
1490 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1493 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1494 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1496 *reply_body = g_variant_new("(a(i))", id_builder);
1497 if (*reply_body == NULL) {
1498 LOGE("Failed to create reply_body");
1499 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1503 g_list_free_full(id_list, free);
1506 g_variant_builder_unref(id_builder);
1511 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1514 GList *id_list = NULL;
1515 char *app_id = NULL;
1518 GVariantBuilder *id_builder = NULL;
1520 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1522 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1523 if (ret != STICKERD_SERVER_ERROR_NONE) {
1524 LOGE("Failed to get all sticker id");
1526 g_list_free_full(id_list, free);
1527 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1530 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1531 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1533 *reply_body = g_variant_new("(a(i))", id_builder);
1534 if (*reply_body == NULL) {
1535 LOGE("Failed to create reply_body");
1536 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1540 g_list_free_full(id_list, free);
1543 g_variant_builder_unref(id_builder);
1548 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1551 GList *id_list = NULL;
1552 char *app_id = NULL;
1553 char *keyword = NULL;
1555 GVariantBuilder *id_builder = NULL;
1557 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1559 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1560 if (ret != STICKERD_SERVER_ERROR_NONE) {
1561 LOGE("Failed to get all sticker id");
1563 g_list_free_full(id_list, free);
1564 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1567 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1568 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1570 *reply_body = g_variant_new("(a(i))", id_builder);
1571 if (*reply_body == NULL) {
1572 LOGE("Failed to create reply_body");
1573 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1577 g_list_free_full(id_list, free);
1580 g_variant_builder_unref(id_builder);
1585 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1588 GList *id_list = NULL;
1589 char *app_id = NULL;
1590 int type, offset, count;
1591 GVariantBuilder *id_builder = NULL;
1593 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1595 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1596 if (ret != STICKERD_SERVER_ERROR_NONE) {
1597 LOGE("Failed to get all sticker id");
1599 g_list_free_full(id_list, free);
1600 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1603 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1604 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1606 *reply_body = g_variant_new("(a(i))", id_builder);
1607 if (*reply_body == NULL) {
1608 LOGE("Failed to create reply_body");
1609 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1613 g_list_free_full(id_list, free);
1616 g_variant_builder_unref(id_builder);
1621 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1624 GVariantBuilder *builder = NULL;
1625 char *app_id = NULL;
1628 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1630 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1631 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1632 if (ret != STICKERD_SERVER_ERROR_NONE) {
1633 LOGE("Failed to get sticker group list");
1634 g_variant_builder_unref(builder);
1635 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1638 *reply_body = g_variant_new("(a(s))", builder);
1639 g_variant_builder_unref(builder);
1641 if (*reply_body == NULL) {
1642 LOGE("Failed to create reply_body");
1643 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1649 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1655 *reply_body = g_variant_new("()");
1656 if (*reply_body == NULL) {
1657 LOGE("Failed to create reply_body");
1658 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1661 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1663 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1664 if (ret != STICKERD_SERVER_ERROR_NONE) {
1665 LOGE("Failed to update sticker disp_type");
1666 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1672 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1678 g_variant_get(parameters, "(&s)", &uri);
1680 ret = stickerd_db_check_file_exists(&result, uri);
1681 if (ret != STICKERD_SERVER_ERROR_NONE) {
1682 LOGE("Failed to get sticker count");
1683 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1686 *reply_body = g_variant_new("(i)", result);
1687 if (*reply_body == NULL) {
1688 LOGE("Failed to create reply_body");
1689 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1695 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1700 *reply_body = g_variant_new("()");
1701 if (*reply_body == NULL) {
1702 LOGE("Failed to create reply_body");
1703 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1706 g_variant_get(parameters, "(i)", &record_id);
1708 ret = stickerd_db_insert_recent_sticker_info(record_id);
1709 if (ret != STICKERD_SERVER_ERROR_NONE) {
1710 LOGE("Failed to insert recent sticker info");
1711 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1717 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1721 GList *id_list = NULL;
1722 GVariantBuilder *id_builder = NULL;
1724 g_variant_get(parameters, "(i)", &count);
1726 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1727 if (ret != STICKERD_SERVER_ERROR_NONE) {
1728 LOGE("Failed to get recent sticker id");
1730 g_list_free_full(id_list, free);
1731 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1734 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1735 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1737 *reply_body = g_variant_new("(a(i))", id_builder);
1738 if (*reply_body == NULL) {
1739 LOGE("Failed to create reply_body");
1740 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1744 g_list_free_full(id_list, free);
1747 g_variant_builder_unref(id_builder);
1752 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1754 int ret = STICKERD_SERVER_ERROR_NONE;
1757 *reply_body = g_variant_new("()");
1758 if (*reply_body == NULL) {
1759 LOGE("Failed to create reply_body");
1760 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1763 g_variant_get(parameters, "(i)", &record_id);
1765 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1767 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1768 if (ret == STICKERD_SERVER_ERROR_NONE)
1769 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1771 _free_sticker_data(sticker_info);
1772 sticker_info = NULL;