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 if (!sticker_info->app_id)
875 sticker_info->type = _get_int_from_object(info_object, "type");
876 if (sticker_info->type < 1)
879 sticker_info->uri = _get_string_from_object(info_object, "uri");
880 if (!sticker_info->uri || sticker_info->uri[0] == '\0')
883 if (sticker_info->type == 1) {
884 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
885 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
886 if (!sticker_info->uri)
893 sticker_info->group = _get_string_from_object(info_object, "group");
894 if (!sticker_info->group)
897 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
898 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
899 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
900 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
901 if (!sticker_info->thumbnail)
908 sticker_info->description = _get_string_from_object(info_object, "description");
910 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
912 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
913 int keyword_arr_len = json_array_get_length(keyword_arr);
914 if (keyword_arr_len < 1)
917 for (int j = 0; j < keyword_arr_len; j++) {
918 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
921 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
922 if (ret != STICKERD_SERVER_ERROR_NONE) {
923 LOGE("Failed to insert sticker info");
924 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
926 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
929 _free_sticker_data(sticker_info);
936 g_error_free(err_msg);
938 g_object_unref(parser);
940 body = g_variant_new("(i)", ret);
942 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
943 if (ret != STICKERD_SERVER_ERROR_NONE)
944 LOGE("Failed to send insert result to client");
947 g_variant_unref(body);
952 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
957 *reply_body = g_variant_new("()");
958 if (*reply_body == NULL) {
959 LOGE("Failed to create reply_body");
960 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
963 g_variant_get(parameters, "(i)", &record_id);
965 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
967 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
969 ret = stickerd_db_delete_sticker_info(record_id);
970 if (ret != STICKERD_SERVER_ERROR_NONE) {
971 LOGE("Failed to delete sticker info");
972 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
974 if (sticker_info && sticker_info->uri)
975 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
979 _free_sticker_data(sticker_info);
986 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
991 *reply_body = g_variant_new("()");
992 if (*reply_body == NULL) {
993 LOGE("Failed to create reply_body");
994 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
997 g_variant_get(parameters, "(&s)", &uri);
999 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1001 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1003 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1004 if (ret != STICKERD_SERVER_ERROR_NONE) {
1005 LOGE("Failed to delete sticker info");
1006 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1008 if (sticker_info && sticker_info->uri)
1009 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1013 _free_sticker_data(sticker_info);
1014 sticker_info = NULL;
1020 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1026 *reply_body = g_variant_new("()");
1027 if (*reply_body == NULL) {
1028 LOGE("Failed to create reply_body");
1029 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1032 g_variant_get(parameters, "(ii)", &record_id, &type);
1034 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1035 if (ret != STICKERD_SERVER_ERROR_NONE) {
1036 LOGE("Failed to update sticker type");
1037 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1043 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1051 *reply_body = g_variant_new("()");
1052 if (*reply_body == NULL) {
1053 LOGE("Failed to create reply_body");
1054 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1057 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1060 if (_check_file_exist(app_id, uri) == 0) {
1061 uri = _convert_sticker_uri(uri, app_id);
1063 LOGE("failed to copy sticker file");
1064 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1067 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1071 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1072 if (ret != STICKERD_SERVER_ERROR_NONE) {
1073 LOGE("Failed to update sticker uri");
1074 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1080 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1087 *reply_body = g_variant_new("()");
1088 if (*reply_body == NULL) {
1089 LOGE("Failed to create reply_body");
1090 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1093 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1095 if (_check_file_exist(app_id, thumbnail) == 0) {
1096 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1098 LOGE("failed to copy sticker thumbnail");
1099 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1102 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1105 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1106 if (ret != STICKERD_SERVER_ERROR_NONE) {
1107 LOGE("Failed to update sticker thumbnail");
1108 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1114 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1120 *reply_body = g_variant_new("()");
1121 if (*reply_body == NULL) {
1122 LOGE("Failed to create reply_body");
1123 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1126 g_variant_get(parameters, "(i&s)", &record_id, &description);
1128 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1129 if (ret != STICKERD_SERVER_ERROR_NONE) {
1130 LOGE("Failed to update sticker description");
1131 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1137 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1143 *reply_body = g_variant_new("()");
1144 if (*reply_body == NULL) {
1145 LOGE("Failed to create reply_body");
1146 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1149 g_variant_get(parameters, "(i&s)", &record_id, &group);
1151 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1152 if (ret != STICKERD_SERVER_ERROR_NONE) {
1153 LOGE("Failed to update sticker group");
1154 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1160 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1164 GVariantIter *keyword_iter = NULL;
1165 char *keyword = NULL;
1166 GList *keyword_list = NULL;
1168 *reply_body = g_variant_new("()");
1169 if (*reply_body == NULL) {
1170 LOGE("Failed to create reply_body");
1171 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1174 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1176 if (!keyword_iter) {
1177 LOGD("failed to get iter");
1178 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1181 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1182 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1185 g_variant_iter_free(keyword_iter);
1187 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1188 if (ret != STICKERD_SERVER_ERROR_NONE) {
1189 LOGE("Failed to update sticker keyword");
1190 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1196 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1200 GVariantBuilder *info_builder;
1201 GVariantBuilder *keyword_builder;
1203 g_variant_get(parameters, "(i)", &record_id);
1204 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1207 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1209 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1210 if (ret != STICKERD_SERVER_ERROR_NONE) {
1211 LOGE("Failed to get sticker info");
1212 _free_sticker_data(sticker_info);
1213 sticker_info = NULL;
1214 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1217 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1218 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1219 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1220 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1221 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1222 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1223 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1224 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1225 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1227 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1228 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1230 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1231 g_variant_builder_unref(info_builder);
1232 g_variant_builder_unref(keyword_builder);
1234 if (*reply_body == NULL) {
1235 LOGE("Failed to create reply_body");
1236 _free_sticker_data(sticker_info);
1237 sticker_info = NULL;
1238 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1241 _free_sticker_data(sticker_info);
1242 sticker_info = NULL;
1247 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1250 GVariantBuilder *builder = NULL;
1251 char *app_id = NULL;
1253 g_variant_get(parameters, "(&s)", &app_id);
1255 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1256 ret = stickerd_db_get_group_list(builder, app_id);
1257 if (ret != STICKERD_SERVER_ERROR_NONE) {
1258 LOGE("Failed to get sticker group list");
1259 g_variant_builder_unref(builder);
1260 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1263 *reply_body = g_variant_new("(a(s))", builder);
1264 g_variant_builder_unref(builder);
1266 if (*reply_body == NULL) {
1267 LOGE("Failed to create reply_body");
1268 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1274 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1277 GVariantBuilder *builder = NULL;
1278 char *app_id = NULL;
1280 g_variant_get(parameters, "(&s)", &app_id);
1282 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1283 ret = stickerd_db_get_keyword_list(builder, app_id);
1284 if (ret != STICKERD_SERVER_ERROR_NONE) {
1285 LOGE("Failed to get sticker keyword list");
1286 g_variant_builder_unref(builder);
1287 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1290 *reply_body = g_variant_new("(a(s))", builder);
1291 g_variant_builder_unref(builder);
1293 if (*reply_body == NULL) {
1294 LOGE("Failed to create reply_body");
1295 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1301 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1305 char *app_id = NULL;
1307 g_variant_get(parameters, "(&s)", &app_id);
1309 ret = stickerd_db_get_sticker_count(&count, app_id);
1310 if (ret != STICKERD_SERVER_ERROR_NONE) {
1311 LOGE("Failed to get sticker count");
1312 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1315 *reply_body = g_variant_new("(i)", count);
1316 if (*reply_body == NULL) {
1317 LOGE("Failed to create reply_body");
1318 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1325 // Send the sticker information by asynchronous communication.
1326 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1331 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1334 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1336 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1337 if (ret != STICKERD_SERVER_ERROR_NONE) {
1338 LOGE("Failed to get sticker info");
1340 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1344 case STICKER_DB_STICKER_ALL:
1345 cmd = "send_all_sticker_info";
1347 case STICKER_DB_STICKER_APPID:
1348 cmd = "send_sticker_info_by_appid";
1350 case STICKER_DB_STICKER_TYPE:
1351 cmd = "send_sticker_info_by_type";
1353 case STICKER_DB_STICKER_GROUP:
1354 cmd = "send_sticker_info_by_group";
1356 case STICKER_DB_STICKER_KEYWORD:
1357 cmd = "send_sticker_info_by_keyword";
1364 GVariantBuilder *info_builder;
1365 GVariantBuilder *keyword_builder;
1367 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1368 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1369 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1370 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1371 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1372 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1373 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1374 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1375 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1377 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1378 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1380 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1381 g_variant_builder_unref(info_builder);
1382 g_variant_builder_unref(keyword_builder);
1384 ret = stickerd_send_dbus_message(body, sender, cmd);
1385 if (ret != STICKERD_SERVER_ERROR_NONE) {
1386 LOGE("Failed to send sticker info to client");
1388 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1396 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1399 LOGE("id doesn't exist");
1403 g_variant_builder_add(id_builder, "(i)", atoi(id));
1406 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1410 char *app_id = NULL;
1411 GList *id_list = NULL;
1412 GVariantBuilder *id_builder = NULL;
1414 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1416 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1417 if (ret != STICKERD_SERVER_ERROR_NONE) {
1418 LOGE("Failed to get all sticker id");
1420 g_list_free_full(id_list, free);
1421 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1424 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1425 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1427 *reply_body = g_variant_new("(a(i))", id_builder);
1428 if (*reply_body == NULL) {
1429 LOGE("Failed to create reply_body");
1430 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1434 g_list_free_full(id_list, free);
1437 g_variant_builder_unref(id_builder);
1442 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1445 GList *id_list = NULL;
1446 char *app_id = NULL;
1448 GVariantBuilder *id_builder = NULL;
1450 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1452 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1453 if (ret != STICKERD_SERVER_ERROR_NONE) {
1454 LOGE("Failed to get all sticker id");
1456 g_list_free_full(id_list, free);
1457 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1460 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1461 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1463 *reply_body = g_variant_new("(a(i))", id_builder);
1464 if (*reply_body == NULL) {
1465 LOGE("Failed to create reply_body");
1466 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1470 g_list_free_full(id_list, free);
1473 g_variant_builder_unref(id_builder);
1478 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1481 GList *id_list = NULL;
1482 char *app_id = NULL;
1483 int type, offset, count;
1484 GVariantBuilder *id_builder = NULL;
1486 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1488 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1489 if (ret != STICKERD_SERVER_ERROR_NONE) {
1490 LOGE("Failed to get all sticker id");
1492 g_list_free_full(id_list, free);
1493 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1496 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1497 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1499 *reply_body = g_variant_new("(a(i))", id_builder);
1500 if (*reply_body == NULL) {
1501 LOGE("Failed to create reply_body");
1502 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1506 g_list_free_full(id_list, free);
1509 g_variant_builder_unref(id_builder);
1514 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1517 GList *id_list = NULL;
1518 char *app_id = NULL;
1521 GVariantBuilder *id_builder = NULL;
1523 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1525 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1526 if (ret != STICKERD_SERVER_ERROR_NONE) {
1527 LOGE("Failed to get all sticker id");
1529 g_list_free_full(id_list, free);
1530 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1533 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1534 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1536 *reply_body = g_variant_new("(a(i))", id_builder);
1537 if (*reply_body == NULL) {
1538 LOGE("Failed to create reply_body");
1539 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1543 g_list_free_full(id_list, free);
1546 g_variant_builder_unref(id_builder);
1551 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1554 GList *id_list = NULL;
1555 char *app_id = NULL;
1556 char *keyword = NULL;
1558 GVariantBuilder *id_builder = NULL;
1560 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1562 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1563 if (ret != STICKERD_SERVER_ERROR_NONE) {
1564 LOGE("Failed to get all sticker id");
1566 g_list_free_full(id_list, free);
1567 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1570 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1571 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1573 *reply_body = g_variant_new("(a(i))", id_builder);
1574 if (*reply_body == NULL) {
1575 LOGE("Failed to create reply_body");
1576 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1580 g_list_free_full(id_list, free);
1583 g_variant_builder_unref(id_builder);
1588 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1591 GList *id_list = NULL;
1592 char *app_id = NULL;
1593 int type, offset, count;
1594 GVariantBuilder *id_builder = NULL;
1596 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1598 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1599 if (ret != STICKERD_SERVER_ERROR_NONE) {
1600 LOGE("Failed to get all sticker id");
1602 g_list_free_full(id_list, free);
1603 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1606 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1607 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1609 *reply_body = g_variant_new("(a(i))", id_builder);
1610 if (*reply_body == NULL) {
1611 LOGE("Failed to create reply_body");
1612 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1616 g_list_free_full(id_list, free);
1619 g_variant_builder_unref(id_builder);
1624 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1627 GVariantBuilder *builder = NULL;
1628 char *app_id = NULL;
1631 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1633 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1634 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1635 if (ret != STICKERD_SERVER_ERROR_NONE) {
1636 LOGE("Failed to get sticker group list");
1637 g_variant_builder_unref(builder);
1638 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1641 *reply_body = g_variant_new("(a(s))", builder);
1642 g_variant_builder_unref(builder);
1644 if (*reply_body == NULL) {
1645 LOGE("Failed to create reply_body");
1646 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1652 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1658 *reply_body = g_variant_new("()");
1659 if (*reply_body == NULL) {
1660 LOGE("Failed to create reply_body");
1661 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1664 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1666 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1667 if (ret != STICKERD_SERVER_ERROR_NONE) {
1668 LOGE("Failed to update sticker disp_type");
1669 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1675 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1681 g_variant_get(parameters, "(&s)", &uri);
1683 ret = stickerd_db_check_file_exists(&result, uri);
1684 if (ret != STICKERD_SERVER_ERROR_NONE) {
1685 LOGE("Failed to get sticker count");
1686 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1689 *reply_body = g_variant_new("(i)", result);
1690 if (*reply_body == NULL) {
1691 LOGE("Failed to create reply_body");
1692 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1698 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1703 *reply_body = g_variant_new("()");
1704 if (*reply_body == NULL) {
1705 LOGE("Failed to create reply_body");
1706 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1709 g_variant_get(parameters, "(i)", &record_id);
1711 ret = stickerd_db_insert_recent_sticker_info(record_id);
1712 if (ret != STICKERD_SERVER_ERROR_NONE) {
1713 LOGE("Failed to insert recent sticker info");
1714 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1720 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1724 GList *id_list = NULL;
1725 GVariantBuilder *id_builder = NULL;
1727 g_variant_get(parameters, "(i)", &count);
1729 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1730 if (ret != STICKERD_SERVER_ERROR_NONE) {
1731 LOGE("Failed to get recent sticker id");
1733 g_list_free_full(id_list, free);
1734 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1737 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1738 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1740 *reply_body = g_variant_new("(a(i))", id_builder);
1741 if (*reply_body == NULL) {
1742 LOGE("Failed to create reply_body");
1743 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1747 g_list_free_full(id_list, free);
1750 g_variant_builder_unref(id_builder);
1755 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1757 int ret = STICKERD_SERVER_ERROR_NONE;
1760 *reply_body = g_variant_new("()");
1761 if (*reply_body == NULL) {
1762 LOGE("Failed to create reply_body");
1763 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1766 g_variant_get(parameters, "(i)", &record_id);
1768 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1770 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1771 if (ret == STICKERD_SERVER_ERROR_NONE)
1772 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1774 _free_sticker_data(sticker_info);
1775 sticker_info = NULL;