2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <tzplatform_config.h>
21 #include <gio/gunixfdlist.h>
23 #include <json-glib/json-glib.h>
24 #include <package_manager.h>
26 #include <sys/types.h>
31 #include "stickerd_dbus.h"
32 #include "stickerd_data_manager.h"
33 #include "stickerd_db_manager.h"
34 #include "sticker_defs.h"
35 #include "stickerd_error.h"
40 #define LOG_TAG "STICKERD_DATA_MANAGER"
42 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
43 #define MAX_ERROR_BUFFER 256
45 static GHashTable *_monitoring_hash = NULL;
46 static char error_buffer[MAX_ERROR_BUFFER];
47 static GList *consumer_list = NULL;
48 extern GMainLoop *main_loop;
50 static void _check_watcher_exist()
52 if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
53 LOGD("Terminate sticker daemon");
54 g_hash_table_destroy(_monitoring_hash);
55 _monitoring_hash = NULL;
56 g_list_free_full(consumer_list, free);
58 g_main_loop_quit(main_loop);
62 static void _on_name_appeared(GDBusConnection *connection,
64 const gchar *name_owner,
67 LOGD("name: %s", name);
70 static void _on_name_vanished(GDBusConnection *connection,
74 monitoring_info_s *info = (monitoring_info_s *)user_data;
77 if (_monitoring_hash != NULL && g_hash_table_lookup(_monitoring_hash, GUINT_TO_POINTER(info->watcher_id)) != NULL) {
78 LOGD("name: %s", name);
79 g_bus_unwatch_name(info->watcher_id);
80 delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
83 if (g_list_find(consumer_list, info->bus_name))
84 consumer_list = g_list_remove(consumer_list, info->bus_name);
92 _check_watcher_exist();
95 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
96 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
99 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
101 if (_monitoring_hash == NULL)
102 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
104 if (consumer_list == NULL)
105 consumer_list = g_list_alloc();
107 GVariant *reply_body = NULL;
108 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
110 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
111 ret = stickerd_server_register(parameters, &reply_body, sender,
112 _on_name_appeared, _on_name_vanished, &_monitoring_hash, &consumer_list);
113 } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
114 ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash, &consumer_list);
115 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
116 ret = stickerd_insert_sticker_info(parameters, &reply_body);
117 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
118 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
119 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
120 ret = stickerd_del_sticker_info(parameters, &reply_body);
121 } else if (g_strcmp0(method_name, "delete_sticker_info_by_uri") == 0) {
122 ret = stickerd_del_sticker_info_by_uri(parameters, &reply_body);
123 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
124 ret = stickerd_update_sticker_type(parameters, &reply_body);
125 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
126 ret = stickerd_update_sticker_uri(parameters, &reply_body);
127 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
128 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
129 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
130 ret = stickerd_update_sticker_description(parameters, &reply_body);
131 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
132 ret = stickerd_update_sticker_group(parameters, &reply_body);
133 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
134 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
135 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
136 ret = stickerd_get_sticker_info(parameters, &reply_body);
137 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
138 ret = stickerd_get_group_list(parameters, &reply_body);
139 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
140 ret = stickerd_get_keyword_list(parameters, &reply_body);
141 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
142 ret = stickerd_get_sticker_count(parameters, &reply_body);
143 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
144 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
145 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
146 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
147 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
148 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
149 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
150 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
151 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
152 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
153 } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
154 ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
155 } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
156 ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
157 } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
158 ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
159 } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
160 ret = stickerd_check_file_exists(parameters, &reply_body);
161 } else if (g_strcmp0(method_name, "insert_recent_sticker_info") == 0) {
162 ret = stickerd_insert_recent_sticker_info(parameters, &reply_body);
163 } else if (g_strcmp0(method_name, "get_recent_sticker_info") == 0) {
164 ret = stickerd_get_recent_sticker_info(parameters, &reply_body);
165 } else if (g_strcmp0(method_name, "send_update_event") == 0) {
166 ret = stickerd_send_update_event(parameters, &reply_body);
169 if (ret == STICKERD_SERVER_ERROR_NONE) {
170 LOGD("method_call successful, method_name : %s", method_name);
171 g_dbus_method_invocation_return_value(invocation, reply_body);
173 LOGE("method_call failed, method_name : %s", method_name);
174 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
177 _check_watcher_exist();
180 static const GDBusInterfaceVTable _sticker_interface_vtable = {
181 _stickerd_client_dbus_method_call_handler,
186 int stickerd_register_dbus_interface(void)
188 static gchar introspection_xml[] =
190 " <interface name='org.tizen.sticker_service'>"
191 " <method name='sticker_service_register'>"
192 " <arg type='i' name='lib_type' direction='in'/>"
193 " <arg type='i' name='watcher_id' direction='out'/>"
196 " <method name='sticker_service_unregister'>"
197 " <arg type='i' name='lib_type' direction='in'/>"
198 " <arg type='i' name='watcher_id' direction='in'/>"
201 " <method name='insert_sticker_info'>"
202 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
203 " <arg type='a(s)' name='keyword_list' direction='in'/>"
204 " <arg type='i' name='record_id' direction='out'/>"
207 " <method name='update_sticker_info_by_json'>"
208 " <arg type='s' name='app_id' direction='in'/>"
209 " <arg type='s' name='json_path' direction='in'/>"
212 " <method name='delete_sticker_info'>"
213 " <arg type='i' name='record_id' direction='in'/>"
216 " <method name='delete_sticker_info_by_uri'>"
217 " <arg type='s' name='uri' direction='in'/>"
220 " <method name='update_sticker_type'>"
221 " <arg type='i' name='record_id' direction='in'/>"
222 " <arg type='i' name='type' direction='in'/>"
225 " <method name='update_sticker_uri'>"
226 " <arg type='i' name='record_id' direction='in'/>"
227 " <arg type='s' name='app_id' direction='in'/>"
228 " <arg type='i' name='type' direction='in'/>"
229 " <arg type='s' name='uri' direction='in'/>"
232 " <method name='update_sticker_thumbnail'>"
233 " <arg type='i' name='record_id' direction='in'/>"
234 " <arg type='s' name='app_id' direction='in'/>"
235 " <arg type='s' name='thumbnail' direction='in'/>"
238 " <method name='update_sticker_description'>"
239 " <arg type='i' name='record_id' direction='in'/>"
240 " <arg type='s' name='description' direction='in'/>"
243 " <method name='update_sticker_group'>"
244 " <arg type='i' name='record_id' direction='in'/>"
245 " <arg type='s' name='group' direction='in'/>"
248 " <method name='update_sticker_keyword'>"
249 " <arg type='i' name='record_id' direction='in'/>"
250 " <arg type='a(s)' name='keyword' direction='in'/>"
253 " <method name='get_sticker_info'>"
254 " <arg type='i' name='record_id' direction='in'/>"
255 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
256 " <arg type='a(s)' name='keyword_list' direction='out'/>"
259 " <method name='get_group_list'>"
260 " <arg type='s' name='app_id' direction='in'/>"
261 " <arg type='a(s)' name='group_list' direction='out'/>"
264 " <method name='get_keyword_list'>"
265 " <arg type='s' name='app_id' direction='in'/>"
266 " <arg type='a(s)' name='keyword_list' direction='out'/>"
269 " <method name='get_sticker_count'>"
270 " <arg type='s' name='app_id' direction='in'/>"
271 " <arg type='i' name='count' direction='out'/>"
274 " <method name='get_all_sticker_info'>"
275 " <arg type='s' name='app_id' direction='in'/>"
276 " <arg type='i' name='offset' direction='in'/>"
277 " <arg type='i' name='count' direction='in'/>"
278 " <arg type='a(i)' name='id_list' direction='out'/>"
281 " <method name='get_sticker_info_by_appid'>"
282 " <arg type='s' name='app_id' direction='in'/>"
283 " <arg type='i' name='offset' direction='in'/>"
284 " <arg type='i' name='count' direction='in'/>"
285 " <arg type='a(i)' name='id_list' direction='out'/>"
288 " <method name='get_sticker_info_by_type'>"
289 " <arg type='s' name='app_id' direction='in'/>"
290 " <arg type='i' name='type' direction='in'/>"
291 " <arg type='i' name='offset' direction='in'/>"
292 " <arg type='i' name='count' direction='in'/>"
293 " <arg type='a(i)' name='id_list' direction='out'/>"
296 " <method name='get_sticker_info_by_group'>"
297 " <arg type='s' name='app_id' direction='in'/>"
298 " <arg type='s' name='group' direction='in'/>"
299 " <arg type='i' name='offset' direction='in'/>"
300 " <arg type='i' name='count' direction='in'/>"
301 " <arg type='a(i)' name='id_list' direction='out'/>"
304 " <method name='get_sticker_info_by_keyword'>"
305 " <arg type='s' name='app_id' direction='in'/>"
306 " <arg type='s' name='keyword' direction='in'/>"
307 " <arg type='i' name='offset' direction='in'/>"
308 " <arg type='i' name='count' direction='in'/>"
309 " <arg type='a(i)' name='id_list' direction='out'/>"
312 " <method name='get_sticker_info_by_disp_type'>"
313 " <arg type='s' name='app_id' direction='in'/>"
314 " <arg type='i' name='type' direction='in'/>"
315 " <arg type='i' name='offset' direction='in'/>"
316 " <arg type='i' name='count' direction='in'/>"
317 " <arg type='a(i)' name='id_list' direction='out'/>"
320 " <method name='get_group_list_by_disp_type'>"
321 " <arg type='s' name='app_id' direction='in'/>"
322 " <arg type='i' name='disp_type' direction='in'/>"
323 " <arg type='a(s)' name='group_list' direction='out'/>"
326 " <method name='update_sticker_disp_type'>"
327 " <arg type='i' name='record_id' direction='in'/>"
328 " <arg type='i' name='disp_type' direction='in'/>"
331 " <method name='check_file_exists'>"
332 " <arg type='s' name='uri' direction='in'/>"
333 " <arg type='i' name='result' direction='out'/>"
336 " <method name='insert_recent_sticker_info'>"
337 " <arg type='i' name='record_id' direction='in'/>"
340 " <method name='get_recent_sticker_info'>"
341 " <arg type='i' name='count' direction='in'/>"
342 " <arg type='a(i)' name='id_list' direction='out'/>"
345 " <method name='send_update_event'>"
346 " <arg type='i' name='record_id' direction='in'/>"
351 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
354 int stickerd_dbus_init(void)
358 ret = stickerd_register_dbus_interface();
359 if (ret != STICKERD_SERVER_ERROR_NONE) {
360 LOGE("Failed to register dbus interface : %d", ret);
361 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
364 return STICKERD_SERVER_ERROR_NONE;
367 static int _check_file_exist(const char *app_id, const char *path)
370 package_info_h package_info = NULL;
371 char *app_path = NULL;
372 char *file_path = NULL;
374 if (access(path, F_OK) == 0) {
379 ret = package_info_create(app_id, &package_info);
380 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
381 LOGE("failed to create package_info. ret: %d", ret);
386 ret = package_info_get_root_path(package_info, &app_path);
387 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
388 LOGE("failed to create package_info. ret: %d", ret);
393 int path_len = strlen(app_path) + strlen(path) + 2;
394 file_path = (char *)calloc(path_len, sizeof(char));
396 LOGE("Failed to alloc memory");
402 snprintf(file_path, path_len, "%s%s",app_path, path);
404 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
406 if (access(file_path, F_OK) != 0) {
407 LOGE("%s does not exist", file_path);
414 package_info_destroy(package_info);
429 static int _mkdirs(const char *path, mode_t mode)
432 char prev_path[2048];
433 const char *tmp = path;
435 if (!path || strlen(path) > 2048)
438 memset(prev_path, '\0', 2048);
439 while ((tmp = strchr(tmp, '/')) != NULL) {
446 strncpy(prev_path, path, len);
447 prev_path[len] = 0x00;
449 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
450 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
451 LOGE("directory create error : %s", error_buffer);
456 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
457 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
458 LOGE("directory create error : %s", error_buffer);
465 static int _file_copy(const char *src, const char *dest)
468 int fd = -1, n_fd = -1;
473 memset(buf, '\0', 4096);
474 fd = open(src, O_RDONLY);
475 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
477 if (fd == -1 || n_fd == -1) {
483 while((size = read(fd, buf, 4096))) {
493 while(write(n_fd, buf, size) == -1) {
514 static char* _convert_sticker_uri(const char *uri, const char *appid)
517 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
518 char * new_path = (char *)calloc(len, sizeof(char));
519 if (new_path == NULL) {
520 LOGE("Failed to alloc memory");
525 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
527 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
529 if (access(new_path, F_OK) == 0) {
530 LOGE("sticker file already exists : %s", new_path);
535 ret = _mkdirs(new_path, 0755);
537 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
538 LOGE("directory create error : %s", error_buffer);
542 ret = _file_copy(uri, new_path);
544 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
545 LOGE("failed to copy sticker file : %s", error_buffer);
558 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
561 LOGE("keyword doesn't exist");
565 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
568 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
570 GVariantBuilder *info_builder;
571 GVariantBuilder *keyword_builder;
573 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
574 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
575 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
576 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
577 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
578 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
579 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
580 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
581 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
583 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
584 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
586 GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
587 g_variant_builder_unref(info_builder);
588 g_variant_builder_unref(keyword_builder);
596 void _get_consumer_busname(gpointer data, gpointer user_data)
602 char *cmd = "send_sticker_changed_event";
603 char *sender = (char *)data;
604 GVariant *body = (GVariant *)user_data;
606 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
607 if (ret != STICKERD_SERVER_ERROR_NONE)
608 LOGE("Failed to send sticker changed event");
611 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
613 GVariant *body = _get_sticker_g_variant(type, sticker_info);
616 g_list_foreach(consumer_list, _get_consumer_busname, body);
619 g_variant_unref(body);
622 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
626 STICKER_DAT_TYPE key;
627 sticker_info_db *sticker_info = NULL;
628 GVariant *value = NULL;
629 GVariantIter *info_iter = NULL;
630 GVariantIter *keyword_iter = NULL;
633 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
634 if (!info_iter || !keyword_iter) {
635 LOGD("failed to get iter");
636 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
640 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
643 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
647 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
649 case STICKER_DATA_TYPE_APP_ID:
650 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
652 case STICKER_DATA_TYPE_URI_TYPE:
653 sticker_info->type = g_variant_get_int32(value);
655 case STICKER_DATA_TYPE_URI:
656 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
658 case STICKER_DATA_TYPE_THUMBNAIL:
659 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
661 case STICKER_DATA_TYPE_DESCRIPTION:
662 sticker_info->description = (char *) g_variant_get_string(value, NULL);
664 case STICKER_DATA_TYPE_GROUP:
665 sticker_info->group = (char *) g_variant_get_string(value, NULL);
667 case STICKER_DATA_TYPE_DISP_TYPE:
668 sticker_info->display_type = g_variant_get_int32(value);
674 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
675 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
678 if (sticker_info->type == 1) {
679 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
680 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
681 if (!sticker_info->uri) {
682 LOGE("failed to copy sticker file");
683 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
687 LOGE("sticker file does not exist");
688 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
693 if (sticker_info->thumbnail) {
694 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
695 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
696 if (!sticker_info->thumbnail) {
697 LOGE("failed to copy sticker thumbnail");
698 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
702 LOGE("sticker thumbnail does not exist");
703 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
708 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
709 if (ret != STICKERD_SERVER_ERROR_NONE) {
710 LOGE("Failed to insert sticker info");
711 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
715 *reply_body = g_variant_new("(i)", record_id);
716 if (*reply_body == NULL) {
717 LOGE("Failed to create reply_body");
718 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
720 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
724 g_variant_unref(value);
727 g_variant_iter_free(info_iter);
730 g_variant_iter_free(keyword_iter);
740 static char* _get_string_from_object(JsonObject *object, const char *key)
742 if (json_object_has_member(object, key) == false)
745 const char *str = json_object_get_string_member(object, key);
752 static int _get_int_from_object(JsonObject *object, const char *key)
754 if (json_object_has_member(object, key) == false)
757 int type = json_object_get_int_member(object, key);
762 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
764 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
766 sticker_info_db *sticker_info = NULL;
768 char *json_path = NULL;
769 JsonParser* parser = NULL;
770 GError* err_msg = NULL;
771 GVariant *body = NULL;
772 char *cmd = "send_insert_result";
774 *reply_body = g_variant_new("()");
775 if (*reply_body == NULL) {
776 LOGE("Failed to create reply_body");
777 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
780 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
782 if (!app_id || !json_path) {
783 LOGE("failed to get parameter");
784 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
787 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
789 parser = json_parser_new();
790 json_parser_load_from_file(parser, json_path, &err_msg);
792 LOGE("failed to load json file. error message: %s", err_msg->message);
793 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
797 JsonNode *root = json_parser_get_root(parser);
799 LOGE("failed to get root");
800 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
804 JsonObject *root_obj = json_node_get_object(root);
805 if (root_obj == NULL) {
806 LOGE("failed to get object");
807 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
811 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
812 if (sticker_arr == NULL) {
813 LOGE("failed to get array member");
814 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
818 int arr_len = json_array_get_length(sticker_arr);
819 for (int i = 0; i < arr_len; i++) {
820 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
821 if (info_object != NULL) {
822 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
824 LOGE("Failed to alloc memory");
828 sticker_info->app_id = app_id;
829 sticker_info->type = _get_int_from_object(info_object, "type");
830 if (sticker_info->type < 1)
833 sticker_info->uri = _get_string_from_object(info_object, "uri");
834 if (!sticker_info->uri)
837 if (sticker_info->type == 1) {
838 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
839 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
840 if (!sticker_info->uri)
847 sticker_info->group = _get_string_from_object(info_object, "group");
848 if (!sticker_info->group)
851 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
852 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
853 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
854 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
855 if (!sticker_info->thumbnail)
862 sticker_info->description = _get_string_from_object(info_object, "description");
864 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
866 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
867 int keyword_arr_len = json_array_get_length(keyword_arr);
868 if (keyword_arr_len < 1)
871 for (int j = 0; j < keyword_arr_len; j++) {
872 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
875 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
876 if (ret != STICKERD_SERVER_ERROR_NONE) {
877 LOGE("Failed to insert sticker info");
878 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
880 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
890 g_error_free(err_msg);
892 g_object_unref(parser);
894 body = g_variant_new("(i)", ret);
896 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
897 if (ret != STICKERD_SERVER_ERROR_NONE)
898 LOGE("Failed to send insert result to client");
901 g_variant_unref(body);
906 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
911 *reply_body = g_variant_new("()");
912 if (*reply_body == NULL) {
913 LOGE("Failed to create reply_body");
914 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
917 g_variant_get(parameters, "(i)", &record_id);
919 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
921 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
923 ret = stickerd_db_delete_sticker_info(record_id);
924 if (ret != STICKERD_SERVER_ERROR_NONE) {
925 LOGE("Failed to delete sticker info");
926 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
928 if (sticker_info && sticker_info->uri) {
929 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
938 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
943 *reply_body = g_variant_new("()");
944 if (*reply_body == NULL) {
945 LOGE("Failed to create reply_body");
946 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
949 g_variant_get(parameters, "(&s)", &uri);
951 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
953 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
955 ret = stickerd_db_delete_sticker_info_by_uri(uri);
956 if (ret != STICKERD_SERVER_ERROR_NONE) {
957 LOGE("Failed to delete sticker info");
958 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
960 if (sticker_info && sticker_info->uri) {
961 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
970 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
976 *reply_body = g_variant_new("()");
977 if (*reply_body == NULL) {
978 LOGE("Failed to create reply_body");
979 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
982 g_variant_get(parameters, "(ii)", &record_id, &type);
984 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
985 if (ret != STICKERD_SERVER_ERROR_NONE) {
986 LOGE("Failed to update sticker type");
987 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
993 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1001 *reply_body = g_variant_new("()");
1002 if (*reply_body == NULL) {
1003 LOGE("Failed to create reply_body");
1004 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1007 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1010 if (_check_file_exist(app_id, uri) == 0) {
1011 uri = _convert_sticker_uri(uri, app_id);
1013 LOGE("failed to copy sticker file");
1014 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1017 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1021 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1022 if (ret != STICKERD_SERVER_ERROR_NONE) {
1023 LOGE("Failed to update sticker uri");
1024 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1030 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1037 *reply_body = g_variant_new("()");
1038 if (*reply_body == NULL) {
1039 LOGE("Failed to create reply_body");
1040 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1043 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1045 if (_check_file_exist(app_id, thumbnail) == 0) {
1046 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1048 LOGE("failed to copy sticker thumbnail");
1049 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1052 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1055 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1056 if (ret != STICKERD_SERVER_ERROR_NONE) {
1057 LOGE("Failed to update sticker thumbnail");
1058 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1064 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1070 *reply_body = g_variant_new("()");
1071 if (*reply_body == NULL) {
1072 LOGE("Failed to create reply_body");
1073 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1076 g_variant_get(parameters, "(i&s)", &record_id, &description);
1078 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1079 if (ret != STICKERD_SERVER_ERROR_NONE) {
1080 LOGE("Failed to update sticker description");
1081 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1087 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1093 *reply_body = g_variant_new("()");
1094 if (*reply_body == NULL) {
1095 LOGE("Failed to create reply_body");
1096 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1099 g_variant_get(parameters, "(i&s)", &record_id, &group);
1101 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1102 if (ret != STICKERD_SERVER_ERROR_NONE) {
1103 LOGE("Failed to update sticker group");
1104 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1110 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1114 GVariantIter *keyword_iter = NULL;
1115 char *keyword = NULL;
1116 GList *keyword_list = NULL;
1118 *reply_body = g_variant_new("()");
1119 if (*reply_body == NULL) {
1120 LOGE("Failed to create reply_body");
1121 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1124 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1126 if (!keyword_iter) {
1127 LOGD("failed to get iter");
1128 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1131 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1132 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1135 g_variant_iter_free(keyword_iter);
1137 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1138 if (ret != STICKERD_SERVER_ERROR_NONE) {
1139 LOGE("Failed to update sticker keyword");
1140 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1146 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1150 GVariantBuilder *info_builder;
1151 GVariantBuilder *keyword_builder;
1153 g_variant_get(parameters, "(i)", &record_id);
1154 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1157 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1159 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1160 if (ret != STICKERD_SERVER_ERROR_NONE) {
1161 LOGE("Failed to get sticker info");
1163 if (sticker_info->app_id)
1164 free(sticker_info->app_id);
1166 if (sticker_info->uri)
1167 free(sticker_info->uri);
1169 if (sticker_info->thumbnail)
1170 free(sticker_info->thumbnail);
1172 if (sticker_info->keyword)
1173 free(sticker_info->keyword);
1175 if (sticker_info->group)
1176 free(sticker_info->group);
1178 if (sticker_info->description)
1179 free(sticker_info->description);
1181 if (sticker_info->date)
1182 free(sticker_info->date);
1185 sticker_info = NULL;
1186 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1189 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1190 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1191 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1192 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1193 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1194 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1195 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1196 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1197 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1199 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1200 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1202 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1203 g_variant_builder_unref(info_builder);
1204 g_variant_builder_unref(keyword_builder);
1206 if (*reply_body == NULL) {
1207 LOGE("Failed to create reply_body");
1209 sticker_info = NULL;
1210 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1214 sticker_info = NULL;
1218 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1221 GVariantBuilder *builder = NULL;
1222 char *app_id = NULL;
1224 g_variant_get(parameters, "(&s)", &app_id);
1226 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1227 ret = stickerd_db_get_group_list(builder, app_id);
1228 if (ret != STICKERD_SERVER_ERROR_NONE) {
1229 LOGE("Failed to get sticker group list");
1230 g_variant_builder_unref(builder);
1231 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1234 *reply_body = g_variant_new("(a(s))", builder);
1235 g_variant_builder_unref(builder);
1237 if (*reply_body == NULL) {
1238 LOGE("Failed to create reply_body");
1239 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1245 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1248 GVariantBuilder *builder = NULL;
1249 char *app_id = NULL;
1251 g_variant_get(parameters, "(&s)", &app_id);
1253 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1254 ret = stickerd_db_get_keyword_list(builder, app_id);
1255 if (ret != STICKERD_SERVER_ERROR_NONE) {
1256 LOGE("Failed to get sticker keyword list");
1257 g_variant_builder_unref(builder);
1258 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1261 *reply_body = g_variant_new("(a(s))", builder);
1262 g_variant_builder_unref(builder);
1264 if (*reply_body == NULL) {
1265 LOGE("Failed to create reply_body");
1266 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1272 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1276 char *app_id = NULL;
1278 g_variant_get(parameters, "(&s)", &app_id);
1280 ret = stickerd_db_get_sticker_count(&count, app_id);
1281 if (ret != STICKERD_SERVER_ERROR_NONE) {
1282 LOGE("Failed to get sticker count");
1283 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1286 *reply_body = g_variant_new("(i)", count);
1287 if (*reply_body == NULL) {
1288 LOGE("Failed to create reply_body");
1289 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1296 // Send the sticker information by asynchronous communication.
1297 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1302 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1305 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1307 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1308 if (ret != STICKERD_SERVER_ERROR_NONE) {
1309 LOGE("Failed to get sticker info");
1311 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1315 case STICKER_DB_STICKER_ALL:
1316 cmd = "send_all_sticker_info";
1318 case STICKER_DB_STICKER_APPID:
1319 cmd = "send_sticker_info_by_appid";
1321 case STICKER_DB_STICKER_TYPE:
1322 cmd = "send_sticker_info_by_type";
1324 case STICKER_DB_STICKER_GROUP:
1325 cmd = "send_sticker_info_by_group";
1327 case STICKER_DB_STICKER_KEYWORD:
1328 cmd = "send_sticker_info_by_keyword";
1335 GVariantBuilder *info_builder;
1336 GVariantBuilder *keyword_builder;
1338 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1339 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1340 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1341 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1342 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1343 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1344 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1345 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1346 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1348 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1349 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1351 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1352 g_variant_builder_unref(info_builder);
1353 g_variant_builder_unref(keyword_builder);
1355 ret = stickerd_send_dbus_message(body, sender, cmd);
1356 if (ret != STICKERD_SERVER_ERROR_NONE) {
1357 LOGE("Failed to send sticker info to client");
1359 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1367 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1370 LOGE("id doesn't exist");
1374 g_variant_builder_add(id_builder, "(i)", atoi(id));
1377 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1381 char *app_id = NULL;
1382 GList *id_list = NULL;
1383 GVariantBuilder *id_builder = NULL;
1385 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1387 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1388 if (ret != STICKERD_SERVER_ERROR_NONE) {
1389 LOGE("Failed to get all sticker id");
1391 g_list_free_full(id_list, free);
1392 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1395 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1396 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1398 *reply_body = g_variant_new("(a(i))", id_builder);
1399 if (*reply_body == NULL) {
1400 LOGE("Failed to create reply_body");
1401 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1405 g_list_free_full(id_list, free);
1408 g_variant_builder_unref(id_builder);
1413 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1416 GList *id_list = NULL;
1417 char *app_id = 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_APPID, &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_type(GVariant *parameters, GVariant **reply_body)
1452 GList *id_list = NULL;
1453 char *app_id = NULL;
1454 int type, offset, count;
1455 GVariantBuilder *id_builder = NULL;
1457 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1459 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, 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_group(GVariant *parameters, GVariant **reply_body)
1488 GList *id_list = NULL;
1489 char *app_id = NULL;
1492 GVariantBuilder *id_builder = NULL;
1494 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1496 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1497 if (ret != STICKERD_SERVER_ERROR_NONE) {
1498 LOGE("Failed to get all sticker id");
1500 g_list_free_full(id_list, free);
1501 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1504 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1505 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1507 *reply_body = g_variant_new("(a(i))", id_builder);
1508 if (*reply_body == NULL) {
1509 LOGE("Failed to create reply_body");
1510 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1514 g_list_free_full(id_list, free);
1517 g_variant_builder_unref(id_builder);
1522 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1525 GList *id_list = NULL;
1526 char *app_id = NULL;
1527 char *keyword = NULL;
1529 GVariantBuilder *id_builder = NULL;
1531 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1533 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1534 if (ret != STICKERD_SERVER_ERROR_NONE) {
1535 LOGE("Failed to get all sticker id");
1537 g_list_free_full(id_list, free);
1538 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1541 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1542 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1544 *reply_body = g_variant_new("(a(i))", id_builder);
1545 if (*reply_body == NULL) {
1546 LOGE("Failed to create reply_body");
1547 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1551 g_list_free_full(id_list, free);
1554 g_variant_builder_unref(id_builder);
1559 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1562 GList *id_list = NULL;
1563 char *app_id = NULL;
1564 int type, offset, count;
1565 GVariantBuilder *id_builder = NULL;
1567 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1569 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, 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_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1598 GVariantBuilder *builder = NULL;
1599 char *app_id = NULL;
1602 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1604 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1605 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1606 if (ret != STICKERD_SERVER_ERROR_NONE) {
1607 LOGE("Failed to get sticker group list");
1608 g_variant_builder_unref(builder);
1609 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1612 *reply_body = g_variant_new("(a(s))", builder);
1613 g_variant_builder_unref(builder);
1615 if (*reply_body == NULL) {
1616 LOGE("Failed to create reply_body");
1617 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1623 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1629 *reply_body = g_variant_new("()");
1630 if (*reply_body == NULL) {
1631 LOGE("Failed to create reply_body");
1632 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1635 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1637 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1638 if (ret != STICKERD_SERVER_ERROR_NONE) {
1639 LOGE("Failed to update sticker disp_type");
1640 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1646 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1652 g_variant_get(parameters, "(&s)", &uri);
1654 ret = stickerd_db_check_file_exists(&result, uri);
1655 if (ret != STICKERD_SERVER_ERROR_NONE) {
1656 LOGE("Failed to get sticker count");
1657 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1660 *reply_body = g_variant_new("(i)", result);
1661 if (*reply_body == NULL) {
1662 LOGE("Failed to create reply_body");
1663 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1669 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1674 *reply_body = g_variant_new("()");
1675 if (*reply_body == NULL) {
1676 LOGE("Failed to create reply_body");
1677 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1680 g_variant_get(parameters, "(i)", &record_id);
1682 ret = stickerd_db_insert_recent_sticker_info(record_id);
1683 if (ret != STICKERD_SERVER_ERROR_NONE) {
1684 LOGE("Failed to insert recent sticker info");
1685 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1691 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1695 GList *id_list = NULL;
1696 GVariantBuilder *id_builder = NULL;
1698 g_variant_get(parameters, "(i)", &count);
1700 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1701 if (ret != STICKERD_SERVER_ERROR_NONE) {
1702 LOGE("Failed to get recent sticker id");
1704 g_list_free_full(id_list, free);
1705 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1708 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1709 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1711 *reply_body = g_variant_new("(a(i))", id_builder);
1712 if (*reply_body == NULL) {
1713 LOGE("Failed to create reply_body");
1714 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1718 g_list_free_full(id_list, free);
1721 g_variant_builder_unref(id_builder);
1726 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1728 int ret = STICKERD_SERVER_ERROR_NONE;
1731 *reply_body = g_variant_new("()");
1732 if (*reply_body == NULL) {
1733 LOGE("Failed to create reply_body");
1734 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1737 g_variant_get(parameters, "(i)", &record_id);
1739 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1741 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1742 if (ret == STICKERD_SERVER_ERROR_NONE) {
1743 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1745 sticker_info = NULL;