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);
476 LOGE("Failed to open file (%s) for reading", src);
479 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
481 LOGE("Failed to open file (%s) for writing", dest);
484 if (fd == -1 || n_fd == -1) {
490 while((size = read(fd, buf, 4096))) {
500 while(write(n_fd, buf, size) == -1) {
521 static char* _convert_sticker_uri(const char *uri, const char *appid)
524 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
525 char * new_path = (char *)calloc(len, sizeof(char));
526 if (new_path == NULL) {
527 LOGE("Failed to alloc memory");
532 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
534 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
536 if (access(new_path, F_OK) == 0) {
537 LOGE("sticker file already exists : %s", new_path);
542 ret = _mkdirs(new_path, 0755);
544 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
545 LOGE("directory create error : %s", error_buffer);
549 ret = _file_copy(uri, new_path);
551 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
552 LOGE("failed to copy sticker file : %s", error_buffer);
565 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
568 LOGE("keyword doesn't exist");
572 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
575 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
577 GVariantBuilder *info_builder;
578 GVariantBuilder *keyword_builder;
580 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
581 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
582 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
583 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
584 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
585 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
586 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
587 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
588 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
590 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
591 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
593 GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
594 g_variant_builder_unref(info_builder);
595 g_variant_builder_unref(keyword_builder);
603 void _get_consumer_busname(gpointer data, gpointer user_data)
609 char *cmd = "send_sticker_changed_event";
610 char *sender = (char *)data;
611 GVariant *body = (GVariant *)user_data;
613 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
614 if (ret != STICKERD_SERVER_ERROR_NONE)
615 LOGE("Failed to send sticker changed event");
618 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
620 GVariant *body = _get_sticker_g_variant(type, sticker_info);
623 g_list_foreach(consumer_list, _get_consumer_busname, body);
626 g_variant_unref(body);
629 static void _free_sticker_data(sticker_info_db *sticker_data)
634 if (sticker_data->app_id) {
635 free(sticker_data->app_id);
636 sticker_data->app_id = NULL;
639 if (sticker_data->uri) {
640 free(sticker_data->uri);
641 sticker_data->uri = NULL;
644 if (sticker_data->thumbnail) {
645 free(sticker_data->thumbnail);
646 sticker_data->thumbnail = NULL;
649 if (sticker_data->keyword) {
650 g_list_free_full(sticker_data->keyword, free);
651 sticker_data->keyword = NULL;
654 if (sticker_data->group) {
655 free(sticker_data->group);
656 sticker_data->group = NULL;
659 if (sticker_data->description) {
660 free(sticker_data->description);
661 sticker_data->description = NULL;
664 if (sticker_data->date) {
665 free(sticker_data->date);
666 sticker_data->date = NULL;
672 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
676 STICKER_DAT_TYPE key;
677 sticker_info_db *sticker_info = NULL;
678 GVariant *value = NULL;
679 GVariantIter *info_iter = NULL;
680 GVariantIter *keyword_iter = NULL;
683 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
684 if (!info_iter || !keyword_iter) {
685 LOGD("failed to get iter");
686 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
690 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
693 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
697 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
699 case STICKER_DATA_TYPE_APP_ID:
700 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
702 case STICKER_DATA_TYPE_URI_TYPE:
703 sticker_info->type = g_variant_get_int32(value);
705 case STICKER_DATA_TYPE_URI:
706 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
708 case STICKER_DATA_TYPE_THUMBNAIL:
709 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
711 case STICKER_DATA_TYPE_DESCRIPTION:
712 sticker_info->description = (char *) g_variant_get_string(value, NULL);
714 case STICKER_DATA_TYPE_GROUP:
715 sticker_info->group = (char *) g_variant_get_string(value, NULL);
717 case STICKER_DATA_TYPE_DISP_TYPE:
718 sticker_info->display_type = g_variant_get_int32(value);
724 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
725 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
728 if (sticker_info->type == 1) {
729 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
730 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
731 if (!sticker_info->uri) {
732 LOGE("failed to copy sticker file");
733 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
737 LOGE("sticker file does not exist");
738 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
743 if (sticker_info->thumbnail) {
744 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
745 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
746 if (!sticker_info->thumbnail) {
747 LOGE("failed to copy sticker thumbnail");
748 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
752 LOGE("sticker thumbnail does not exist");
753 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
758 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
759 if (ret != STICKERD_SERVER_ERROR_NONE) {
760 LOGE("Failed to insert sticker info");
761 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
765 *reply_body = g_variant_new("(i)", record_id);
766 if (*reply_body == NULL) {
767 LOGE("Failed to create reply_body");
768 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
770 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
774 g_variant_unref(value);
777 g_variant_iter_free(info_iter);
780 g_variant_iter_free(keyword_iter);
790 static char* _get_string_from_object(JsonObject *object, const char *key)
792 if (json_object_has_member(object, key) == false)
795 const char *str = json_object_get_string_member(object, key);
802 static int _get_int_from_object(JsonObject *object, const char *key)
804 if (json_object_has_member(object, key) == false)
807 int type = json_object_get_int_member(object, key);
812 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
814 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
816 sticker_info_db *sticker_info = NULL;
818 char *json_path = NULL;
819 JsonParser* parser = NULL;
820 GError* err_msg = NULL;
821 GVariant *body = NULL;
822 char *cmd = "send_insert_result";
824 *reply_body = g_variant_new("()");
825 if (*reply_body == NULL) {
826 LOGE("Failed to create reply_body");
827 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
830 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
832 if (!app_id || !json_path) {
833 LOGE("failed to get parameter");
834 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
837 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
839 parser = json_parser_new();
840 json_parser_load_from_file(parser, json_path, &err_msg);
842 LOGE("failed to load json file. error message: %s", err_msg->message);
843 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
847 JsonNode *root = json_parser_get_root(parser);
849 LOGE("failed to get root");
850 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
854 JsonObject *root_obj = json_node_get_object(root);
855 if (root_obj == NULL) {
856 LOGE("failed to get object");
857 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
861 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
862 if (sticker_arr == NULL) {
863 LOGE("failed to get array member");
864 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
868 int arr_len = json_array_get_length(sticker_arr);
869 for (int i = 0; i < arr_len; i++) {
870 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
871 if (info_object != NULL) {
872 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
874 LOGE("Failed to alloc memory");
878 sticker_info->app_id = strdup(app_id);
879 sticker_info->type = _get_int_from_object(info_object, "type");
880 if (sticker_info->type < 1)
883 sticker_info->uri = _get_string_from_object(info_object, "uri");
884 if (!sticker_info->uri || sticker_info->uri[0] != '\0')
887 if (sticker_info->type == 1) {
888 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
889 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
890 if (!sticker_info->uri)
897 sticker_info->group = _get_string_from_object(info_object, "group");
898 if (!sticker_info->group)
901 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
902 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
903 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
904 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
905 if (!sticker_info->thumbnail)
912 sticker_info->description = _get_string_from_object(info_object, "description");
914 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
916 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
917 int keyword_arr_len = json_array_get_length(keyword_arr);
918 if (keyword_arr_len < 1)
921 for (int j = 0; j < keyword_arr_len; j++) {
922 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
925 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
926 if (ret != STICKERD_SERVER_ERROR_NONE) {
927 LOGE("Failed to insert sticker info");
928 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
930 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
933 _free_sticker_data(sticker_info);
940 g_error_free(err_msg);
942 g_object_unref(parser);
944 body = g_variant_new("(i)", ret);
946 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
947 if (ret != STICKERD_SERVER_ERROR_NONE)
948 LOGE("Failed to send insert result to client");
951 g_variant_unref(body);
956 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
961 *reply_body = g_variant_new("()");
962 if (*reply_body == NULL) {
963 LOGE("Failed to create reply_body");
964 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
967 g_variant_get(parameters, "(i)", &record_id);
969 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
971 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
973 ret = stickerd_db_delete_sticker_info(record_id);
974 if (ret != STICKERD_SERVER_ERROR_NONE) {
975 LOGE("Failed to delete sticker info");
976 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
978 if (sticker_info && sticker_info->uri)
979 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
983 _free_sticker_data(sticker_info);
990 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
995 *reply_body = g_variant_new("()");
996 if (*reply_body == NULL) {
997 LOGE("Failed to create reply_body");
998 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1001 g_variant_get(parameters, "(&s)", &uri);
1003 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1005 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1007 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1008 if (ret != STICKERD_SERVER_ERROR_NONE) {
1009 LOGE("Failed to delete sticker info");
1010 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1012 if (sticker_info && sticker_info->uri)
1013 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1017 _free_sticker_data(sticker_info);
1018 sticker_info = NULL;
1024 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1030 *reply_body = g_variant_new("()");
1031 if (*reply_body == NULL) {
1032 LOGE("Failed to create reply_body");
1033 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1036 g_variant_get(parameters, "(ii)", &record_id, &type);
1038 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1039 if (ret != STICKERD_SERVER_ERROR_NONE) {
1040 LOGE("Failed to update sticker type");
1041 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1047 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1055 *reply_body = g_variant_new("()");
1056 if (*reply_body == NULL) {
1057 LOGE("Failed to create reply_body");
1058 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1061 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1064 if (_check_file_exist(app_id, uri) == 0) {
1065 uri = _convert_sticker_uri(uri, app_id);
1067 LOGE("failed to copy sticker file");
1068 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1071 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1075 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1076 if (ret != STICKERD_SERVER_ERROR_NONE) {
1077 LOGE("Failed to update sticker uri");
1078 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1084 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1091 *reply_body = g_variant_new("()");
1092 if (*reply_body == NULL) {
1093 LOGE("Failed to create reply_body");
1094 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1097 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1099 if (_check_file_exist(app_id, thumbnail) == 0) {
1100 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1102 LOGE("failed to copy sticker thumbnail");
1103 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1106 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1109 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1110 if (ret != STICKERD_SERVER_ERROR_NONE) {
1111 LOGE("Failed to update sticker thumbnail");
1112 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1118 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1124 *reply_body = g_variant_new("()");
1125 if (*reply_body == NULL) {
1126 LOGE("Failed to create reply_body");
1127 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1130 g_variant_get(parameters, "(i&s)", &record_id, &description);
1132 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1133 if (ret != STICKERD_SERVER_ERROR_NONE) {
1134 LOGE("Failed to update sticker description");
1135 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1141 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1147 *reply_body = g_variant_new("()");
1148 if (*reply_body == NULL) {
1149 LOGE("Failed to create reply_body");
1150 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1153 g_variant_get(parameters, "(i&s)", &record_id, &group);
1155 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1156 if (ret != STICKERD_SERVER_ERROR_NONE) {
1157 LOGE("Failed to update sticker group");
1158 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1164 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1168 GVariantIter *keyword_iter = NULL;
1169 char *keyword = NULL;
1170 GList *keyword_list = NULL;
1172 *reply_body = g_variant_new("()");
1173 if (*reply_body == NULL) {
1174 LOGE("Failed to create reply_body");
1175 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1178 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1180 if (!keyword_iter) {
1181 LOGD("failed to get iter");
1182 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1185 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1186 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1189 g_variant_iter_free(keyword_iter);
1191 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1192 if (ret != STICKERD_SERVER_ERROR_NONE) {
1193 LOGE("Failed to update sticker keyword");
1194 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1200 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1204 GVariantBuilder *info_builder;
1205 GVariantBuilder *keyword_builder;
1207 g_variant_get(parameters, "(i)", &record_id);
1208 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1211 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1213 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1214 if (ret != STICKERD_SERVER_ERROR_NONE) {
1215 LOGE("Failed to get sticker info");
1216 _free_sticker_data(sticker_info);
1217 sticker_info = NULL;
1218 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1221 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1222 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1223 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1224 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1225 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1226 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1227 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1228 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1229 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1231 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1232 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1234 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1235 g_variant_builder_unref(info_builder);
1236 g_variant_builder_unref(keyword_builder);
1238 if (*reply_body == NULL) {
1239 LOGE("Failed to create reply_body");
1240 _free_sticker_data(sticker_info);
1241 sticker_info = NULL;
1242 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1245 _free_sticker_data(sticker_info);
1246 sticker_info = NULL;
1251 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1254 GVariantBuilder *builder = NULL;
1255 char *app_id = NULL;
1257 g_variant_get(parameters, "(&s)", &app_id);
1259 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1260 ret = stickerd_db_get_group_list(builder, app_id);
1261 if (ret != STICKERD_SERVER_ERROR_NONE) {
1262 LOGE("Failed to get sticker group list");
1263 g_variant_builder_unref(builder);
1264 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1267 *reply_body = g_variant_new("(a(s))", builder);
1268 g_variant_builder_unref(builder);
1270 if (*reply_body == NULL) {
1271 LOGE("Failed to create reply_body");
1272 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1278 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1281 GVariantBuilder *builder = NULL;
1282 char *app_id = NULL;
1284 g_variant_get(parameters, "(&s)", &app_id);
1286 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1287 ret = stickerd_db_get_keyword_list(builder, app_id);
1288 if (ret != STICKERD_SERVER_ERROR_NONE) {
1289 LOGE("Failed to get sticker keyword list");
1290 g_variant_builder_unref(builder);
1291 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1294 *reply_body = g_variant_new("(a(s))", builder);
1295 g_variant_builder_unref(builder);
1297 if (*reply_body == NULL) {
1298 LOGE("Failed to create reply_body");
1299 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1305 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1309 char *app_id = NULL;
1311 g_variant_get(parameters, "(&s)", &app_id);
1313 ret = stickerd_db_get_sticker_count(&count, app_id);
1314 if (ret != STICKERD_SERVER_ERROR_NONE) {
1315 LOGE("Failed to get sticker count");
1316 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1319 *reply_body = g_variant_new("(i)", count);
1320 if (*reply_body == NULL) {
1321 LOGE("Failed to create reply_body");
1322 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1329 // Send the sticker information by asynchronous communication.
1330 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1335 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1338 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1340 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1341 if (ret != STICKERD_SERVER_ERROR_NONE) {
1342 LOGE("Failed to get sticker info");
1344 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1348 case STICKER_DB_STICKER_ALL:
1349 cmd = "send_all_sticker_info";
1351 case STICKER_DB_STICKER_APPID:
1352 cmd = "send_sticker_info_by_appid";
1354 case STICKER_DB_STICKER_TYPE:
1355 cmd = "send_sticker_info_by_type";
1357 case STICKER_DB_STICKER_GROUP:
1358 cmd = "send_sticker_info_by_group";
1360 case STICKER_DB_STICKER_KEYWORD:
1361 cmd = "send_sticker_info_by_keyword";
1368 GVariantBuilder *info_builder;
1369 GVariantBuilder *keyword_builder;
1371 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1372 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1373 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1374 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1375 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1376 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1377 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1378 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1379 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1381 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1382 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1384 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1385 g_variant_builder_unref(info_builder);
1386 g_variant_builder_unref(keyword_builder);
1388 ret = stickerd_send_dbus_message(body, sender, cmd);
1389 if (ret != STICKERD_SERVER_ERROR_NONE) {
1390 LOGE("Failed to send sticker info to client");
1392 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1400 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1403 LOGE("id doesn't exist");
1407 g_variant_builder_add(id_builder, "(i)", atoi(id));
1410 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1414 char *app_id = NULL;
1415 GList *id_list = NULL;
1416 GVariantBuilder *id_builder = NULL;
1418 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1420 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1421 if (ret != STICKERD_SERVER_ERROR_NONE) {
1422 LOGE("Failed to get all sticker id");
1424 g_list_free_full(id_list, free);
1425 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1428 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1429 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1431 *reply_body = g_variant_new("(a(i))", id_builder);
1432 if (*reply_body == NULL) {
1433 LOGE("Failed to create reply_body");
1434 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1438 g_list_free_full(id_list, free);
1441 g_variant_builder_unref(id_builder);
1446 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1449 GList *id_list = NULL;
1450 char *app_id = NULL;
1452 GVariantBuilder *id_builder = NULL;
1454 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1456 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1457 if (ret != STICKERD_SERVER_ERROR_NONE) {
1458 LOGE("Failed to get all sticker id");
1460 g_list_free_full(id_list, free);
1461 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1464 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1465 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1467 *reply_body = g_variant_new("(a(i))", id_builder);
1468 if (*reply_body == NULL) {
1469 LOGE("Failed to create reply_body");
1470 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1474 g_list_free_full(id_list, free);
1477 g_variant_builder_unref(id_builder);
1482 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1485 GList *id_list = NULL;
1486 char *app_id = NULL;
1487 int type, offset, count;
1488 GVariantBuilder *id_builder = NULL;
1490 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1492 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1493 if (ret != STICKERD_SERVER_ERROR_NONE) {
1494 LOGE("Failed to get all sticker id");
1496 g_list_free_full(id_list, free);
1497 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1500 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1501 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1503 *reply_body = g_variant_new("(a(i))", id_builder);
1504 if (*reply_body == NULL) {
1505 LOGE("Failed to create reply_body");
1506 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1510 g_list_free_full(id_list, free);
1513 g_variant_builder_unref(id_builder);
1518 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1521 GList *id_list = NULL;
1522 char *app_id = NULL;
1525 GVariantBuilder *id_builder = NULL;
1527 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1529 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1530 if (ret != STICKERD_SERVER_ERROR_NONE) {
1531 LOGE("Failed to get all sticker id");
1533 g_list_free_full(id_list, free);
1534 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1537 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1538 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1540 *reply_body = g_variant_new("(a(i))", id_builder);
1541 if (*reply_body == NULL) {
1542 LOGE("Failed to create reply_body");
1543 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1547 g_list_free_full(id_list, free);
1550 g_variant_builder_unref(id_builder);
1555 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1558 GList *id_list = NULL;
1559 char *app_id = NULL;
1560 char *keyword = NULL;
1562 GVariantBuilder *id_builder = NULL;
1564 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1566 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1567 if (ret != STICKERD_SERVER_ERROR_NONE) {
1568 LOGE("Failed to get all sticker id");
1570 g_list_free_full(id_list, free);
1571 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1574 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1575 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1577 *reply_body = g_variant_new("(a(i))", id_builder);
1578 if (*reply_body == NULL) {
1579 LOGE("Failed to create reply_body");
1580 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1584 g_list_free_full(id_list, free);
1587 g_variant_builder_unref(id_builder);
1592 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1595 GList *id_list = NULL;
1596 char *app_id = NULL;
1597 int type, offset, count;
1598 GVariantBuilder *id_builder = NULL;
1600 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1602 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1603 if (ret != STICKERD_SERVER_ERROR_NONE) {
1604 LOGE("Failed to get all sticker id");
1606 g_list_free_full(id_list, free);
1607 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1610 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1611 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1613 *reply_body = g_variant_new("(a(i))", id_builder);
1614 if (*reply_body == NULL) {
1615 LOGE("Failed to create reply_body");
1616 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1620 g_list_free_full(id_list, free);
1623 g_variant_builder_unref(id_builder);
1628 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1631 GVariantBuilder *builder = NULL;
1632 char *app_id = NULL;
1635 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1637 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1638 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1639 if (ret != STICKERD_SERVER_ERROR_NONE) {
1640 LOGE("Failed to get sticker group list");
1641 g_variant_builder_unref(builder);
1642 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1645 *reply_body = g_variant_new("(a(s))", builder);
1646 g_variant_builder_unref(builder);
1648 if (*reply_body == NULL) {
1649 LOGE("Failed to create reply_body");
1650 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1656 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1662 *reply_body = g_variant_new("()");
1663 if (*reply_body == NULL) {
1664 LOGE("Failed to create reply_body");
1665 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1668 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1670 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1671 if (ret != STICKERD_SERVER_ERROR_NONE) {
1672 LOGE("Failed to update sticker disp_type");
1673 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1679 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1685 g_variant_get(parameters, "(&s)", &uri);
1687 ret = stickerd_db_check_file_exists(&result, uri);
1688 if (ret != STICKERD_SERVER_ERROR_NONE) {
1689 LOGE("Failed to get sticker count");
1690 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1693 *reply_body = g_variant_new("(i)", result);
1694 if (*reply_body == NULL) {
1695 LOGE("Failed to create reply_body");
1696 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1702 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1707 *reply_body = g_variant_new("()");
1708 if (*reply_body == NULL) {
1709 LOGE("Failed to create reply_body");
1710 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1713 g_variant_get(parameters, "(i)", &record_id);
1715 ret = stickerd_db_insert_recent_sticker_info(record_id);
1716 if (ret != STICKERD_SERVER_ERROR_NONE) {
1717 LOGE("Failed to insert recent sticker info");
1718 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1724 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1728 GList *id_list = NULL;
1729 GVariantBuilder *id_builder = NULL;
1731 g_variant_get(parameters, "(i)", &count);
1733 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1734 if (ret != STICKERD_SERVER_ERROR_NONE) {
1735 LOGE("Failed to get recent sticker id");
1737 g_list_free_full(id_list, free);
1738 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1741 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1742 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1744 *reply_body = g_variant_new("(a(i))", id_builder);
1745 if (*reply_body == NULL) {
1746 LOGE("Failed to create reply_body");
1747 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1751 g_list_free_full(id_list, free);
1754 g_variant_builder_unref(id_builder);
1759 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1761 int ret = STICKERD_SERVER_ERROR_NONE;
1764 *reply_body = g_variant_new("()");
1765 if (*reply_body == NULL) {
1766 LOGE("Failed to create reply_body");
1767 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1770 g_variant_get(parameters, "(i)", &record_id);
1772 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1774 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1775 if (ret == STICKERD_SERVER_ERROR_NONE)
1776 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1778 _free_sticker_data(sticker_info);
1779 sticker_info = NULL;