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 MAX_ERROR_BUFFER 256
44 static GHashTable *_monitoring_hash = NULL;
45 static char error_buffer[MAX_ERROR_BUFFER];
46 static GList *consumer_list = NULL;
47 extern GMainLoop *main_loop;
49 static void _check_watcher_exist()
51 if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
52 LOGD("Terminate sticker daemon");
53 g_hash_table_destroy(_monitoring_hash);
54 _monitoring_hash = NULL;
55 g_list_free_full(consumer_list, free);
57 g_main_loop_quit(main_loop);
61 static void _on_name_appeared(GDBusConnection *connection,
63 const gchar *name_owner,
66 LOGD("name: %s", name);
69 static void _on_name_vanished(GDBusConnection *connection,
73 monitoring_info_s *info = (monitoring_info_s *)user_data;
76 if (_monitoring_hash != NULL && g_hash_table_lookup(_monitoring_hash, GUINT_TO_POINTER(info->watcher_id)) != NULL) {
77 LOGD("name: %s", name);
78 g_bus_unwatch_name(info->watcher_id);
79 delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
82 if (g_list_find(consumer_list, info->bus_name))
83 consumer_list = g_list_remove(consumer_list, info->bus_name);
91 _check_watcher_exist();
94 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
95 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
98 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
100 if (_monitoring_hash == NULL)
101 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
103 if (consumer_list == NULL)
104 consumer_list = g_list_alloc();
106 GVariant *reply_body = NULL;
107 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
109 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
110 ret = stickerd_server_register(parameters, &reply_body, sender,
111 _on_name_appeared, _on_name_vanished, &_monitoring_hash, &consumer_list);
112 } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
113 ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash, &consumer_list);
114 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
115 ret = stickerd_insert_sticker_info(parameters, &reply_body);
116 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
117 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
118 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
119 ret = stickerd_del_sticker_info(parameters, &reply_body);
120 } else if (g_strcmp0(method_name, "delete_sticker_info_by_uri") == 0) {
121 ret = stickerd_del_sticker_info_by_uri(parameters, &reply_body);
122 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
123 ret = stickerd_update_sticker_type(parameters, &reply_body);
124 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
125 ret = stickerd_update_sticker_uri(parameters, &reply_body);
126 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
127 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
128 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
129 ret = stickerd_update_sticker_description(parameters, &reply_body);
130 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
131 ret = stickerd_update_sticker_group(parameters, &reply_body);
132 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
133 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
134 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
135 ret = stickerd_get_sticker_info(parameters, &reply_body);
136 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
137 ret = stickerd_get_group_list(parameters, &reply_body);
138 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
139 ret = stickerd_get_keyword_list(parameters, &reply_body);
140 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
141 ret = stickerd_get_sticker_count(parameters, &reply_body);
142 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
143 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
144 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
145 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
146 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
147 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
148 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
149 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
150 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
151 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
152 } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
153 ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
154 } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
155 ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
156 } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
157 ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
158 } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
159 ret = stickerd_check_file_exists(parameters, &reply_body);
160 } else if (g_strcmp0(method_name, "insert_recent_sticker_info") == 0) {
161 ret = stickerd_insert_recent_sticker_info(parameters, &reply_body);
162 } else if (g_strcmp0(method_name, "get_recent_sticker_info") == 0) {
163 ret = stickerd_get_recent_sticker_info(parameters, &reply_body);
164 } else if (g_strcmp0(method_name, "send_update_event") == 0) {
165 ret = stickerd_send_update_event(parameters, &reply_body);
166 } else if (g_strcmp0(method_name, "get_sticker_info_by_uri") == 0) {
167 ret = stickerd_get_sticker_info_by_uri(parameters, &reply_body);
170 if (ret == STICKERD_SERVER_ERROR_NONE) {
171 LOGD("method_call successful, method_name : %s", method_name);
172 g_dbus_method_invocation_return_value(invocation, reply_body);
174 LOGE("method_call failed, method_name : %s", method_name);
175 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
178 _check_watcher_exist();
181 static const GDBusInterfaceVTable _sticker_interface_vtable = {
182 _stickerd_client_dbus_method_call_handler,
187 int stickerd_register_dbus_interface(void)
189 static gchar introspection_xml[] =
191 " <interface name='org.tizen.sticker_service'>"
192 " <method name='sticker_service_register'>"
193 " <arg type='i' name='lib_type' direction='in'/>"
194 " <arg type='i' name='watcher_id' direction='out'/>"
197 " <method name='sticker_service_unregister'>"
198 " <arg type='i' name='lib_type' direction='in'/>"
199 " <arg type='i' name='watcher_id' direction='in'/>"
202 " <method name='insert_sticker_info'>"
203 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
204 " <arg type='a(s)' name='keyword_list' direction='in'/>"
205 " <arg type='i' name='record_id' direction='out'/>"
208 " <method name='update_sticker_info_by_json'>"
209 " <arg type='s' name='app_id' direction='in'/>"
210 " <arg type='s' name='json_path' direction='in'/>"
213 " <method name='delete_sticker_info'>"
214 " <arg type='i' name='record_id' direction='in'/>"
217 " <method name='delete_sticker_info_by_uri'>"
218 " <arg type='s' name='uri' direction='in'/>"
221 " <method name='update_sticker_type'>"
222 " <arg type='i' name='record_id' direction='in'/>"
223 " <arg type='i' name='type' direction='in'/>"
226 " <method name='update_sticker_uri'>"
227 " <arg type='i' name='record_id' direction='in'/>"
228 " <arg type='s' name='app_id' direction='in'/>"
229 " <arg type='i' name='type' direction='in'/>"
230 " <arg type='s' name='uri' direction='in'/>"
233 " <method name='update_sticker_thumbnail'>"
234 " <arg type='i' name='record_id' direction='in'/>"
235 " <arg type='s' name='app_id' direction='in'/>"
236 " <arg type='s' name='thumbnail' direction='in'/>"
239 " <method name='update_sticker_description'>"
240 " <arg type='i' name='record_id' direction='in'/>"
241 " <arg type='s' name='description' direction='in'/>"
244 " <method name='update_sticker_group'>"
245 " <arg type='i' name='record_id' direction='in'/>"
246 " <arg type='s' name='group' direction='in'/>"
249 " <method name='update_sticker_keyword'>"
250 " <arg type='i' name='record_id' direction='in'/>"
251 " <arg type='a(s)' name='keyword' direction='in'/>"
254 " <method name='get_sticker_info'>"
255 " <arg type='i' name='record_id' direction='in'/>"
256 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
257 " <arg type='a(s)' name='keyword_list' direction='out'/>"
260 " <method name='get_group_list'>"
261 " <arg type='s' name='app_id' direction='in'/>"
262 " <arg type='a(s)' name='group_list' direction='out'/>"
265 " <method name='get_keyword_list'>"
266 " <arg type='s' name='app_id' direction='in'/>"
267 " <arg type='a(s)' name='keyword_list' direction='out'/>"
270 " <method name='get_sticker_count'>"
271 " <arg type='s' name='app_id' direction='in'/>"
272 " <arg type='i' name='count' direction='out'/>"
275 " <method name='get_all_sticker_info'>"
276 " <arg type='s' name='app_id' direction='in'/>"
277 " <arg type='i' name='offset' direction='in'/>"
278 " <arg type='i' name='count' direction='in'/>"
279 " <arg type='a(i)' name='id_list' direction='out'/>"
282 " <method name='get_sticker_info_by_appid'>"
283 " <arg type='s' name='app_id' direction='in'/>"
284 " <arg type='i' name='offset' direction='in'/>"
285 " <arg type='i' name='count' direction='in'/>"
286 " <arg type='a(i)' name='id_list' direction='out'/>"
289 " <method name='get_sticker_info_by_type'>"
290 " <arg type='s' name='app_id' direction='in'/>"
291 " <arg type='i' name='type' direction='in'/>"
292 " <arg type='i' name='offset' direction='in'/>"
293 " <arg type='i' name='count' direction='in'/>"
294 " <arg type='a(i)' name='id_list' direction='out'/>"
297 " <method name='get_sticker_info_by_group'>"
298 " <arg type='s' name='app_id' direction='in'/>"
299 " <arg type='s' name='group' direction='in'/>"
300 " <arg type='i' name='offset' direction='in'/>"
301 " <arg type='i' name='count' direction='in'/>"
302 " <arg type='a(i)' name='id_list' direction='out'/>"
305 " <method name='get_sticker_info_by_keyword'>"
306 " <arg type='s' name='app_id' direction='in'/>"
307 " <arg type='s' name='keyword' direction='in'/>"
308 " <arg type='i' name='offset' direction='in'/>"
309 " <arg type='i' name='count' direction='in'/>"
310 " <arg type='a(i)' name='id_list' direction='out'/>"
313 " <method name='get_sticker_info_by_disp_type'>"
314 " <arg type='s' name='app_id' direction='in'/>"
315 " <arg type='i' name='type' direction='in'/>"
316 " <arg type='i' name='offset' direction='in'/>"
317 " <arg type='i' name='count' direction='in'/>"
318 " <arg type='a(i)' name='id_list' direction='out'/>"
321 " <method name='get_group_list_by_disp_type'>"
322 " <arg type='s' name='app_id' direction='in'/>"
323 " <arg type='i' name='disp_type' direction='in'/>"
324 " <arg type='a(s)' name='group_list' direction='out'/>"
327 " <method name='update_sticker_disp_type'>"
328 " <arg type='i' name='record_id' direction='in'/>"
329 " <arg type='i' name='disp_type' direction='in'/>"
332 " <method name='check_file_exists'>"
333 " <arg type='s' name='uri' direction='in'/>"
334 " <arg type='i' name='result' direction='out'/>"
337 " <method name='insert_recent_sticker_info'>"
338 " <arg type='i' name='record_id' direction='in'/>"
341 " <method name='get_recent_sticker_info'>"
342 " <arg type='i' name='count' direction='in'/>"
343 " <arg type='a(i)' name='id_list' direction='out'/>"
346 " <method name='send_update_event'>"
347 " <arg type='i' name='record_id' direction='in'/>"
350 " <method name='get_sticker_info_by_uri'>"
351 " <arg type='s' name='uri' direction='in'/>"
352 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
353 " <arg type='a(s)' name='keyword_list' direction='out'/>"
358 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
361 int stickerd_dbus_init(void)
365 ret = stickerd_register_dbus_interface();
366 if (ret != STICKERD_SERVER_ERROR_NONE) {
367 LOGE("Failed to register dbus interface : %d", ret);
368 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
371 return STICKERD_SERVER_ERROR_NONE;
374 static int _check_file_exist(const char *app_id, const char *path)
377 package_info_h package_info = NULL;
378 char *app_path = NULL;
379 char *file_path = NULL;
381 if (access(path, F_OK) == 0) {
386 ret = package_info_create(app_id, &package_info);
387 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
388 LOGE("failed to create package_info. ret: %d", ret);
393 ret = package_info_get_root_path(package_info, &app_path);
394 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
395 LOGE("failed to create package_info. ret: %d", ret);
400 int path_len = strlen(app_path) + strlen(path) + 2;
401 file_path = (char *)calloc(path_len, sizeof(char));
403 LOGE("Failed to alloc memory");
409 snprintf(file_path, path_len, "%s%s",app_path, path);
411 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
413 if (access(file_path, F_OK) != 0) {
414 LOGE("%s does not exist", file_path);
421 package_info_destroy(package_info);
436 static int _mkdirs(const char *path, mode_t mode)
439 char prev_path[2048];
440 const char *tmp = path;
442 if (!path || strlen(path) > 2048)
445 memset(prev_path, '\0', 2048);
446 while ((tmp = strchr(tmp, '/')) != NULL) {
453 strncpy(prev_path, path, len);
454 prev_path[len] = 0x00;
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);
463 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
464 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
465 LOGE("directory create error : %s", error_buffer);
472 static int _file_copy(const char *src, const char *dest)
475 int fd = -1, n_fd = -1;
480 memset(buf, '\0', 4096);
481 fd = open(src, O_RDONLY);
482 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
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 if (!sticker_info->app_id)
882 sticker_info->type = _get_int_from_object(info_object, "type");
883 if (sticker_info->type < 1)
886 sticker_info->uri = _get_string_from_object(info_object, "uri");
887 if (!sticker_info->uri || sticker_info->uri[0] == '\0')
890 if (sticker_info->type == 1) {
891 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
892 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
893 if (!sticker_info->uri)
900 sticker_info->group = _get_string_from_object(info_object, "group");
901 if (!sticker_info->group)
904 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
905 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
906 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
907 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
908 if (!sticker_info->thumbnail)
915 sticker_info->description = _get_string_from_object(info_object, "description");
917 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
919 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
920 int keyword_arr_len = json_array_get_length(keyword_arr);
921 if (keyword_arr_len < 1)
924 for (int j = 0; j < keyword_arr_len; j++) {
925 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
928 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
929 if (ret != STICKERD_SERVER_ERROR_NONE) {
930 LOGE("Failed to insert sticker info");
931 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
933 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
936 _free_sticker_data(sticker_info);
943 g_error_free(err_msg);
945 g_object_unref(parser);
947 body = g_variant_new("(i)", ret);
949 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
950 if (ret != STICKERD_SERVER_ERROR_NONE)
951 LOGE("Failed to send insert result to client");
954 g_variant_unref(body);
959 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
964 *reply_body = g_variant_new("()");
965 if (*reply_body == NULL) {
966 LOGE("Failed to create reply_body");
967 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
970 g_variant_get(parameters, "(i)", &record_id);
972 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
974 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
976 ret = stickerd_db_delete_sticker_info(record_id);
977 if (ret != STICKERD_SERVER_ERROR_NONE) {
978 LOGE("Failed to delete sticker info");
979 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
981 if (sticker_info && sticker_info->uri)
982 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
986 _free_sticker_data(sticker_info);
993 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
998 *reply_body = g_variant_new("()");
999 if (*reply_body == NULL) {
1000 LOGE("Failed to create reply_body");
1001 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1004 g_variant_get(parameters, "(&s)", &uri);
1006 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1008 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1010 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1011 if (ret != STICKERD_SERVER_ERROR_NONE) {
1012 LOGE("Failed to delete sticker info");
1013 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1015 if (sticker_info && sticker_info->uri)
1016 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1020 _free_sticker_data(sticker_info);
1021 sticker_info = NULL;
1027 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1033 *reply_body = g_variant_new("()");
1034 if (*reply_body == NULL) {
1035 LOGE("Failed to create reply_body");
1036 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1039 g_variant_get(parameters, "(ii)", &record_id, &type);
1041 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1042 if (ret != STICKERD_SERVER_ERROR_NONE) {
1043 LOGE("Failed to update sticker type");
1044 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1050 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1058 *reply_body = g_variant_new("()");
1059 if (*reply_body == NULL) {
1060 LOGE("Failed to create reply_body");
1061 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1064 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1067 if (_check_file_exist(app_id, uri) == 0) {
1068 uri = _convert_sticker_uri(uri, app_id);
1070 LOGE("failed to copy sticker file");
1071 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1074 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1078 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1079 if (ret != STICKERD_SERVER_ERROR_NONE) {
1080 LOGE("Failed to update sticker uri");
1081 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1087 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1094 *reply_body = g_variant_new("()");
1095 if (*reply_body == NULL) {
1096 LOGE("Failed to create reply_body");
1097 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1100 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1102 if (_check_file_exist(app_id, thumbnail) == 0) {
1103 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1105 LOGE("failed to copy sticker thumbnail");
1106 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1109 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1112 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1113 if (ret != STICKERD_SERVER_ERROR_NONE) {
1114 LOGE("Failed to update sticker thumbnail");
1115 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1121 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1127 *reply_body = g_variant_new("()");
1128 if (*reply_body == NULL) {
1129 LOGE("Failed to create reply_body");
1130 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1133 g_variant_get(parameters, "(i&s)", &record_id, &description);
1135 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1136 if (ret != STICKERD_SERVER_ERROR_NONE) {
1137 LOGE("Failed to update sticker description");
1138 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1144 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1150 *reply_body = g_variant_new("()");
1151 if (*reply_body == NULL) {
1152 LOGE("Failed to create reply_body");
1153 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1156 g_variant_get(parameters, "(i&s)", &record_id, &group);
1158 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1159 if (ret != STICKERD_SERVER_ERROR_NONE) {
1160 LOGE("Failed to update sticker group");
1161 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1167 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1171 GVariantIter *keyword_iter = NULL;
1172 char *keyword = NULL;
1173 GList *keyword_list = NULL;
1175 *reply_body = g_variant_new("()");
1176 if (*reply_body == NULL) {
1177 LOGE("Failed to create reply_body");
1178 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1181 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1183 if (!keyword_iter) {
1184 LOGD("failed to get iter");
1185 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1188 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1189 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1192 g_variant_iter_free(keyword_iter);
1194 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1195 if (ret != STICKERD_SERVER_ERROR_NONE) {
1196 LOGE("Failed to update sticker keyword");
1197 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1203 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1207 GVariantBuilder *info_builder;
1208 GVariantBuilder *keyword_builder;
1210 g_variant_get(parameters, "(i)", &record_id);
1211 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1214 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1216 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1217 if (ret != STICKERD_SERVER_ERROR_NONE) {
1218 LOGE("Failed to get sticker info");
1219 _free_sticker_data(sticker_info);
1220 sticker_info = NULL;
1221 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1224 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1225 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1226 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1227 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1228 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1229 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1230 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1231 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1232 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1234 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1235 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1237 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1238 g_variant_builder_unref(info_builder);
1239 g_variant_builder_unref(keyword_builder);
1241 if (*reply_body == NULL) {
1242 LOGE("Failed to create reply_body");
1243 _free_sticker_data(sticker_info);
1244 sticker_info = NULL;
1245 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1248 _free_sticker_data(sticker_info);
1249 sticker_info = NULL;
1254 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1257 GVariantBuilder *builder = NULL;
1258 char *app_id = NULL;
1260 g_variant_get(parameters, "(&s)", &app_id);
1262 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1263 ret = stickerd_db_get_group_list(builder, app_id);
1264 if (ret != STICKERD_SERVER_ERROR_NONE) {
1265 LOGE("Failed to get sticker group list");
1266 g_variant_builder_unref(builder);
1267 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1270 *reply_body = g_variant_new("(a(s))", builder);
1271 g_variant_builder_unref(builder);
1273 if (*reply_body == NULL) {
1274 LOGE("Failed to create reply_body");
1275 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1281 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1284 GVariantBuilder *builder = NULL;
1285 char *app_id = NULL;
1287 g_variant_get(parameters, "(&s)", &app_id);
1289 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1290 ret = stickerd_db_get_keyword_list(builder, app_id);
1291 if (ret != STICKERD_SERVER_ERROR_NONE) {
1292 LOGE("Failed to get sticker keyword list");
1293 g_variant_builder_unref(builder);
1294 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1297 *reply_body = g_variant_new("(a(s))", builder);
1298 g_variant_builder_unref(builder);
1300 if (*reply_body == NULL) {
1301 LOGE("Failed to create reply_body");
1302 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1308 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1312 char *app_id = NULL;
1314 g_variant_get(parameters, "(&s)", &app_id);
1316 ret = stickerd_db_get_sticker_count(&count, app_id);
1317 if (ret != STICKERD_SERVER_ERROR_NONE) {
1318 LOGE("Failed to get sticker count");
1319 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1322 *reply_body = g_variant_new("(i)", count);
1323 if (*reply_body == NULL) {
1324 LOGE("Failed to create reply_body");
1325 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1332 // Send the sticker information by asynchronous communication.
1333 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1338 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1341 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1343 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1344 if (ret != STICKERD_SERVER_ERROR_NONE) {
1345 LOGE("Failed to get sticker info");
1347 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1351 case STICKER_DB_STICKER_ALL:
1352 cmd = "send_all_sticker_info";
1354 case STICKER_DB_STICKER_APPID:
1355 cmd = "send_sticker_info_by_appid";
1357 case STICKER_DB_STICKER_TYPE:
1358 cmd = "send_sticker_info_by_type";
1360 case STICKER_DB_STICKER_GROUP:
1361 cmd = "send_sticker_info_by_group";
1363 case STICKER_DB_STICKER_KEYWORD:
1364 cmd = "send_sticker_info_by_keyword";
1371 GVariantBuilder *info_builder;
1372 GVariantBuilder *keyword_builder;
1374 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1375 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1376 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1377 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1378 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1379 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1380 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1381 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1382 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1384 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1385 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1387 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1388 g_variant_builder_unref(info_builder);
1389 g_variant_builder_unref(keyword_builder);
1391 ret = stickerd_send_dbus_message(body, sender, cmd);
1392 if (ret != STICKERD_SERVER_ERROR_NONE) {
1393 LOGE("Failed to send sticker info to client");
1395 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1403 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1406 LOGE("id doesn't exist");
1410 g_variant_builder_add(id_builder, "(i)", atoi(id));
1413 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1417 char *app_id = NULL;
1418 GList *id_list = NULL;
1419 GVariantBuilder *id_builder = NULL;
1421 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1423 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1424 if (ret != STICKERD_SERVER_ERROR_NONE) {
1425 LOGE("Failed to get all sticker id");
1427 g_list_free_full(id_list, free);
1428 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1431 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1432 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1434 *reply_body = g_variant_new("(a(i))", id_builder);
1435 if (*reply_body == NULL) {
1436 LOGE("Failed to create reply_body");
1437 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1441 g_list_free_full(id_list, free);
1444 g_variant_builder_unref(id_builder);
1449 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1452 GList *id_list = NULL;
1453 char *app_id = NULL;
1455 GVariantBuilder *id_builder = NULL;
1457 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1459 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1460 if (ret != STICKERD_SERVER_ERROR_NONE) {
1461 LOGE("Failed to get all sticker id");
1463 g_list_free_full(id_list, free);
1464 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1467 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1468 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1470 *reply_body = g_variant_new("(a(i))", id_builder);
1471 if (*reply_body == NULL) {
1472 LOGE("Failed to create reply_body");
1473 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1477 g_list_free_full(id_list, free);
1480 g_variant_builder_unref(id_builder);
1485 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1488 GList *id_list = NULL;
1489 char *app_id = NULL;
1490 int type, offset, count;
1491 GVariantBuilder *id_builder = NULL;
1493 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1495 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1496 if (ret != STICKERD_SERVER_ERROR_NONE) {
1497 LOGE("Failed to get all sticker id");
1499 g_list_free_full(id_list, free);
1500 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1503 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1504 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1506 *reply_body = g_variant_new("(a(i))", id_builder);
1507 if (*reply_body == NULL) {
1508 LOGE("Failed to create reply_body");
1509 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1513 g_list_free_full(id_list, free);
1516 g_variant_builder_unref(id_builder);
1521 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1524 GList *id_list = NULL;
1525 char *app_id = NULL;
1528 GVariantBuilder *id_builder = NULL;
1530 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1532 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1533 if (ret != STICKERD_SERVER_ERROR_NONE) {
1534 LOGE("Failed to get all sticker id");
1536 g_list_free_full(id_list, free);
1537 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1540 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1541 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1543 *reply_body = g_variant_new("(a(i))", id_builder);
1544 if (*reply_body == NULL) {
1545 LOGE("Failed to create reply_body");
1546 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1550 g_list_free_full(id_list, free);
1553 g_variant_builder_unref(id_builder);
1558 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1561 GList *id_list = NULL;
1562 char *app_id = NULL;
1563 char *keyword = NULL;
1565 GVariantBuilder *id_builder = NULL;
1567 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1569 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1570 if (ret != STICKERD_SERVER_ERROR_NONE) {
1571 LOGE("Failed to get all sticker id");
1573 g_list_free_full(id_list, free);
1574 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1577 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1578 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1580 *reply_body = g_variant_new("(a(i))", id_builder);
1581 if (*reply_body == NULL) {
1582 LOGE("Failed to create reply_body");
1583 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1587 g_list_free_full(id_list, free);
1590 g_variant_builder_unref(id_builder);
1595 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1598 GList *id_list = NULL;
1599 char *app_id = NULL;
1600 int type, offset, count;
1601 GVariantBuilder *id_builder = NULL;
1603 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1605 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1606 if (ret != STICKERD_SERVER_ERROR_NONE) {
1607 LOGE("Failed to get all sticker id");
1609 g_list_free_full(id_list, free);
1610 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1613 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1614 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1616 *reply_body = g_variant_new("(a(i))", id_builder);
1617 if (*reply_body == NULL) {
1618 LOGE("Failed to create reply_body");
1619 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1623 g_list_free_full(id_list, free);
1626 g_variant_builder_unref(id_builder);
1631 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1634 GVariantBuilder *builder = NULL;
1635 char *app_id = NULL;
1638 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1640 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1641 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1642 if (ret != STICKERD_SERVER_ERROR_NONE) {
1643 LOGE("Failed to get sticker group list");
1644 g_variant_builder_unref(builder);
1645 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1648 *reply_body = g_variant_new("(a(s))", builder);
1649 g_variant_builder_unref(builder);
1651 if (*reply_body == NULL) {
1652 LOGE("Failed to create reply_body");
1653 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1659 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1665 *reply_body = g_variant_new("()");
1666 if (*reply_body == NULL) {
1667 LOGE("Failed to create reply_body");
1668 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1671 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1673 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1674 if (ret != STICKERD_SERVER_ERROR_NONE) {
1675 LOGE("Failed to update sticker disp_type");
1676 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1682 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1688 g_variant_get(parameters, "(&s)", &uri);
1690 ret = stickerd_db_check_file_exists(&result, uri);
1691 if (ret != STICKERD_SERVER_ERROR_NONE) {
1692 LOGE("Failed to get sticker count");
1693 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1696 *reply_body = g_variant_new("(i)", result);
1697 if (*reply_body == NULL) {
1698 LOGE("Failed to create reply_body");
1699 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1705 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1710 *reply_body = g_variant_new("()");
1711 if (*reply_body == NULL) {
1712 LOGE("Failed to create reply_body");
1713 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1716 g_variant_get(parameters, "(i)", &record_id);
1718 ret = stickerd_db_insert_recent_sticker_info(record_id);
1719 if (ret != STICKERD_SERVER_ERROR_NONE) {
1720 LOGE("Failed to insert recent sticker info");
1721 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1727 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1731 GList *id_list = NULL;
1732 GVariantBuilder *id_builder = NULL;
1734 g_variant_get(parameters, "(i)", &count);
1736 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1737 if (ret != STICKERD_SERVER_ERROR_NONE) {
1738 LOGE("Failed to get recent sticker id");
1740 g_list_free_full(id_list, free);
1741 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1744 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1745 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1747 *reply_body = g_variant_new("(a(i))", id_builder);
1748 if (*reply_body == NULL) {
1749 LOGE("Failed to create reply_body");
1750 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1754 g_list_free_full(id_list, free);
1757 g_variant_builder_unref(id_builder);
1762 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1764 int ret = STICKERD_SERVER_ERROR_NONE;
1767 *reply_body = g_variant_new("()");
1768 if (*reply_body == NULL) {
1769 LOGE("Failed to create reply_body");
1770 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1773 g_variant_get(parameters, "(i)", &record_id);
1775 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1777 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1778 if (ret == STICKERD_SERVER_ERROR_NONE)
1779 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1781 _free_sticker_data(sticker_info);
1782 sticker_info = NULL;
1788 int stickerd_get_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1792 GVariantBuilder *info_builder;
1793 GVariantBuilder *keyword_builder;
1795 g_variant_get(parameters, "(&s)", &uri);
1797 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1800 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1802 ret = stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1803 if (ret != STICKERD_SERVER_ERROR_NONE) {
1804 LOGE("Failed to get sticker info");
1805 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1809 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1810 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(sticker_info->record_id));
1811 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1812 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1813 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1814 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1815 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1816 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1817 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1818 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1820 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1821 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1823 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1824 g_variant_builder_unref(info_builder);
1825 g_variant_builder_unref(keyword_builder);
1827 if (*reply_body == NULL) {
1828 LOGE("Failed to create reply_body");
1829 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1834 _free_sticker_data(sticker_info);
1835 sticker_info = NULL;