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);
483 LOGE("Failed to open file (%s) for reading", src);
486 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
488 LOGE("Failed to open file (%s) for writing", dest);
491 if (fd == -1 || n_fd == -1) {
497 while((size = read(fd, buf, 4096))) {
507 while(write(n_fd, buf, size) == -1) {
528 static char* _convert_sticker_uri(const char *uri, const char *appid)
531 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
532 char * new_path = (char *)calloc(len, sizeof(char));
533 if (new_path == NULL) {
534 LOGE("Failed to alloc memory");
539 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
541 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
543 if (access(new_path, F_OK) == 0) {
544 LOGE("sticker file already exists : %s", new_path);
549 ret = _mkdirs(new_path, 0755);
551 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
552 LOGE("directory create error : %s", error_buffer);
556 ret = _file_copy(uri, new_path);
558 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
559 LOGE("failed to copy sticker file : %s", error_buffer);
572 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
575 LOGE("keyword doesn't exist");
579 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
582 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
584 GVariantBuilder *info_builder;
585 GVariantBuilder *keyword_builder;
587 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
588 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
589 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
590 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
591 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
592 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
593 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
594 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
595 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
597 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
598 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
600 GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
601 g_variant_builder_unref(info_builder);
602 g_variant_builder_unref(keyword_builder);
610 void _get_consumer_busname(gpointer data, gpointer user_data)
616 char *cmd = "send_sticker_changed_event";
617 char *sender = (char *)data;
618 GVariant *body = (GVariant *)user_data;
620 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
621 if (ret != STICKERD_SERVER_ERROR_NONE)
622 LOGE("Failed to send sticker changed event");
625 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
627 GVariant *body = _get_sticker_g_variant(type, sticker_info);
630 g_list_foreach(consumer_list, _get_consumer_busname, body);
633 g_variant_unref(body);
636 static void _free_sticker_data(sticker_info_db *sticker_data)
641 if (sticker_data->app_id) {
642 free(sticker_data->app_id);
643 sticker_data->app_id = NULL;
646 if (sticker_data->uri) {
647 free(sticker_data->uri);
648 sticker_data->uri = NULL;
651 if (sticker_data->thumbnail) {
652 free(sticker_data->thumbnail);
653 sticker_data->thumbnail = NULL;
656 if (sticker_data->keyword) {
657 g_list_free_full(sticker_data->keyword, free);
658 sticker_data->keyword = NULL;
661 if (sticker_data->group) {
662 free(sticker_data->group);
663 sticker_data->group = NULL;
666 if (sticker_data->description) {
667 free(sticker_data->description);
668 sticker_data->description = NULL;
671 if (sticker_data->date) {
672 free(sticker_data->date);
673 sticker_data->date = NULL;
679 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
683 STICKER_DAT_TYPE key;
684 sticker_info_db *sticker_info = NULL;
685 GVariant *value = NULL;
686 GVariantIter *info_iter = NULL;
687 GVariantIter *keyword_iter = NULL;
690 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
691 if (!info_iter || !keyword_iter) {
692 LOGD("failed to get iter");
693 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
697 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
700 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
704 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
706 case STICKER_DATA_TYPE_APP_ID:
707 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
709 case STICKER_DATA_TYPE_URI_TYPE:
710 sticker_info->type = g_variant_get_int32(value);
712 case STICKER_DATA_TYPE_URI:
713 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
715 case STICKER_DATA_TYPE_THUMBNAIL:
716 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
718 case STICKER_DATA_TYPE_DESCRIPTION:
719 sticker_info->description = (char *) g_variant_get_string(value, NULL);
721 case STICKER_DATA_TYPE_GROUP:
722 sticker_info->group = (char *) g_variant_get_string(value, NULL);
724 case STICKER_DATA_TYPE_DISP_TYPE:
725 sticker_info->display_type = g_variant_get_int32(value);
731 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
732 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
735 if (sticker_info->type == 1) {
736 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
737 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
738 if (!sticker_info->uri) {
739 LOGE("failed to copy sticker file");
740 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
744 LOGE("sticker file does not exist");
745 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
750 if (sticker_info->thumbnail) {
751 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
752 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
753 if (!sticker_info->thumbnail) {
754 LOGE("failed to copy sticker thumbnail");
755 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
759 LOGE("sticker thumbnail does not exist");
760 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
765 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
766 if (ret != STICKERD_SERVER_ERROR_NONE) {
767 LOGE("Failed to insert sticker info");
768 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
772 *reply_body = g_variant_new("(i)", record_id);
773 if (*reply_body == NULL) {
774 LOGE("Failed to create reply_body");
775 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
777 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
781 g_variant_unref(value);
784 g_variant_iter_free(info_iter);
787 g_variant_iter_free(keyword_iter);
797 static char* _get_string_from_object(JsonObject *object, const char *key)
799 if (json_object_has_member(object, key) == false)
802 const char *str = json_object_get_string_member(object, key);
809 static int _get_int_from_object(JsonObject *object, const char *key)
811 if (json_object_has_member(object, key) == false)
814 int type = json_object_get_int_member(object, key);
819 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
821 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
823 sticker_info_db *sticker_info = NULL;
825 char *json_path = NULL;
826 JsonParser* parser = NULL;
827 GError* err_msg = NULL;
828 GVariant *body = NULL;
829 char *cmd = "send_insert_result";
831 *reply_body = g_variant_new("()");
832 if (*reply_body == NULL) {
833 LOGE("Failed to create reply_body");
834 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
837 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
839 if (!app_id || !json_path) {
840 LOGE("failed to get parameter");
841 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
844 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
846 parser = json_parser_new();
847 json_parser_load_from_file(parser, json_path, &err_msg);
849 LOGE("failed to load json file. error message: %s", err_msg->message);
850 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
854 JsonNode *root = json_parser_get_root(parser);
856 LOGE("failed to get root");
857 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
861 JsonObject *root_obj = json_node_get_object(root);
862 if (root_obj == NULL) {
863 LOGE("failed to get object");
864 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
868 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
869 if (sticker_arr == NULL) {
870 LOGE("failed to get array member");
871 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
875 int arr_len = json_array_get_length(sticker_arr);
876 for (int i = 0; i < arr_len; i++) {
877 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
878 if (info_object != NULL) {
879 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
881 LOGE("Failed to alloc memory");
885 sticker_info->app_id = strdup(app_id);
886 if (!sticker_info->app_id)
889 sticker_info->type = _get_int_from_object(info_object, "type");
890 if (sticker_info->type < 1)
893 sticker_info->uri = _get_string_from_object(info_object, "uri");
894 if (!sticker_info->uri || sticker_info->uri[0] == '\0')
897 if (sticker_info->type == 1) {
898 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
899 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
900 if (!sticker_info->uri)
907 sticker_info->group = _get_string_from_object(info_object, "group");
908 if (!sticker_info->group)
911 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
912 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
913 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
914 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
915 if (!sticker_info->thumbnail)
922 sticker_info->description = _get_string_from_object(info_object, "description");
924 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
926 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
927 int keyword_arr_len = json_array_get_length(keyword_arr);
928 if (keyword_arr_len < 1)
931 for (int j = 0; j < keyword_arr_len; j++) {
932 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
935 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
936 if (ret != STICKERD_SERVER_ERROR_NONE) {
937 LOGE("Failed to insert sticker info");
938 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
940 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
943 _free_sticker_data(sticker_info);
950 g_error_free(err_msg);
952 g_object_unref(parser);
954 body = g_variant_new("(i)", ret);
956 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
957 if (ret != STICKERD_SERVER_ERROR_NONE)
958 LOGE("Failed to send insert result to client");
961 g_variant_unref(body);
966 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
971 *reply_body = g_variant_new("()");
972 if (*reply_body == NULL) {
973 LOGE("Failed to create reply_body");
974 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
977 g_variant_get(parameters, "(i)", &record_id);
979 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
981 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
983 ret = stickerd_db_delete_sticker_info(record_id);
984 if (ret != STICKERD_SERVER_ERROR_NONE) {
985 LOGE("Failed to delete sticker info");
986 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
988 if (sticker_info && sticker_info->uri)
989 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
993 _free_sticker_data(sticker_info);
1000 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1005 *reply_body = g_variant_new("()");
1006 if (*reply_body == NULL) {
1007 LOGE("Failed to create reply_body");
1008 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1011 g_variant_get(parameters, "(&s)", &uri);
1013 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1015 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1017 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1018 if (ret != STICKERD_SERVER_ERROR_NONE) {
1019 LOGE("Failed to delete sticker info");
1020 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1022 if (sticker_info && sticker_info->uri)
1023 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1027 _free_sticker_data(sticker_info);
1028 sticker_info = NULL;
1034 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1040 *reply_body = g_variant_new("()");
1041 if (*reply_body == NULL) {
1042 LOGE("Failed to create reply_body");
1043 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1046 g_variant_get(parameters, "(ii)", &record_id, &type);
1048 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1049 if (ret != STICKERD_SERVER_ERROR_NONE) {
1050 LOGE("Failed to update sticker type");
1051 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1057 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1065 *reply_body = g_variant_new("()");
1066 if (*reply_body == NULL) {
1067 LOGE("Failed to create reply_body");
1068 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1071 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1074 if (_check_file_exist(app_id, uri) == 0) {
1075 uri = _convert_sticker_uri(uri, app_id);
1077 LOGE("failed to copy sticker file");
1078 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1081 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1085 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1086 if (ret != STICKERD_SERVER_ERROR_NONE) {
1087 LOGE("Failed to update sticker uri");
1088 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1094 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1101 *reply_body = g_variant_new("()");
1102 if (*reply_body == NULL) {
1103 LOGE("Failed to create reply_body");
1104 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1107 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1109 if (_check_file_exist(app_id, thumbnail) == 0) {
1110 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1112 LOGE("failed to copy sticker thumbnail");
1113 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1116 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1119 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1120 if (ret != STICKERD_SERVER_ERROR_NONE) {
1121 LOGE("Failed to update sticker thumbnail");
1122 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1128 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1134 *reply_body = g_variant_new("()");
1135 if (*reply_body == NULL) {
1136 LOGE("Failed to create reply_body");
1137 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1140 g_variant_get(parameters, "(i&s)", &record_id, &description);
1142 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1143 if (ret != STICKERD_SERVER_ERROR_NONE) {
1144 LOGE("Failed to update sticker description");
1145 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1151 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1157 *reply_body = g_variant_new("()");
1158 if (*reply_body == NULL) {
1159 LOGE("Failed to create reply_body");
1160 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1163 g_variant_get(parameters, "(i&s)", &record_id, &group);
1165 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1166 if (ret != STICKERD_SERVER_ERROR_NONE) {
1167 LOGE("Failed to update sticker group");
1168 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1174 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1178 GVariantIter *keyword_iter = NULL;
1179 char *keyword = NULL;
1180 GList *keyword_list = NULL;
1182 *reply_body = g_variant_new("()");
1183 if (*reply_body == NULL) {
1184 LOGE("Failed to create reply_body");
1185 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1188 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1190 if (!keyword_iter) {
1191 LOGD("failed to get iter");
1192 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1195 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1196 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1199 g_variant_iter_free(keyword_iter);
1201 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1202 if (ret != STICKERD_SERVER_ERROR_NONE) {
1203 LOGE("Failed to update sticker keyword");
1204 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1210 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1214 GVariantBuilder *info_builder;
1215 GVariantBuilder *keyword_builder;
1217 g_variant_get(parameters, "(i)", &record_id);
1218 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1221 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1223 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1224 if (ret != STICKERD_SERVER_ERROR_NONE) {
1225 LOGE("Failed to get sticker info");
1226 _free_sticker_data(sticker_info);
1227 sticker_info = NULL;
1228 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1231 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1232 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1233 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1234 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1235 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1236 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1237 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1238 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1239 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1241 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1242 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1244 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1245 g_variant_builder_unref(info_builder);
1246 g_variant_builder_unref(keyword_builder);
1248 if (*reply_body == NULL) {
1249 LOGE("Failed to create reply_body");
1250 _free_sticker_data(sticker_info);
1251 sticker_info = NULL;
1252 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1255 _free_sticker_data(sticker_info);
1256 sticker_info = NULL;
1261 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1264 GVariantBuilder *builder = NULL;
1265 char *app_id = NULL;
1267 g_variant_get(parameters, "(&s)", &app_id);
1269 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1270 ret = stickerd_db_get_group_list(builder, app_id);
1271 if (ret != STICKERD_SERVER_ERROR_NONE) {
1272 LOGE("Failed to get sticker group list");
1273 g_variant_builder_unref(builder);
1274 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1277 *reply_body = g_variant_new("(a(s))", builder);
1278 g_variant_builder_unref(builder);
1280 if (*reply_body == NULL) {
1281 LOGE("Failed to create reply_body");
1282 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1288 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1291 GVariantBuilder *builder = NULL;
1292 char *app_id = NULL;
1294 g_variant_get(parameters, "(&s)", &app_id);
1296 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1297 ret = stickerd_db_get_keyword_list(builder, app_id);
1298 if (ret != STICKERD_SERVER_ERROR_NONE) {
1299 LOGE("Failed to get sticker keyword list");
1300 g_variant_builder_unref(builder);
1301 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1304 *reply_body = g_variant_new("(a(s))", builder);
1305 g_variant_builder_unref(builder);
1307 if (*reply_body == NULL) {
1308 LOGE("Failed to create reply_body");
1309 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1315 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1319 char *app_id = NULL;
1321 g_variant_get(parameters, "(&s)", &app_id);
1323 ret = stickerd_db_get_sticker_count(&count, app_id);
1324 if (ret != STICKERD_SERVER_ERROR_NONE) {
1325 LOGE("Failed to get sticker count");
1326 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1329 *reply_body = g_variant_new("(i)", count);
1330 if (*reply_body == NULL) {
1331 LOGE("Failed to create reply_body");
1332 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1339 // Send the sticker information by asynchronous communication.
1340 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1345 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1348 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1350 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1351 if (ret != STICKERD_SERVER_ERROR_NONE) {
1352 LOGE("Failed to get sticker info");
1354 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1358 case STICKER_DB_STICKER_ALL:
1359 cmd = "send_all_sticker_info";
1361 case STICKER_DB_STICKER_APPID:
1362 cmd = "send_sticker_info_by_appid";
1364 case STICKER_DB_STICKER_TYPE:
1365 cmd = "send_sticker_info_by_type";
1367 case STICKER_DB_STICKER_GROUP:
1368 cmd = "send_sticker_info_by_group";
1370 case STICKER_DB_STICKER_KEYWORD:
1371 cmd = "send_sticker_info_by_keyword";
1378 GVariantBuilder *info_builder;
1379 GVariantBuilder *keyword_builder;
1381 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1382 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1383 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1384 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1385 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1386 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1387 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1388 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1389 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1391 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1392 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1394 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1395 g_variant_builder_unref(info_builder);
1396 g_variant_builder_unref(keyword_builder);
1398 ret = stickerd_send_dbus_message(body, sender, cmd);
1399 if (ret != STICKERD_SERVER_ERROR_NONE) {
1400 LOGE("Failed to send sticker info to client");
1402 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1410 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1413 LOGE("id doesn't exist");
1417 g_variant_builder_add(id_builder, "(i)", atoi(id));
1420 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1424 char *app_id = NULL;
1425 GList *id_list = NULL;
1426 GVariantBuilder *id_builder = NULL;
1428 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1430 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1431 if (ret != STICKERD_SERVER_ERROR_NONE) {
1432 LOGE("Failed to get all sticker id");
1434 g_list_free_full(id_list, free);
1435 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1438 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1439 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1441 *reply_body = g_variant_new("(a(i))", id_builder);
1442 if (*reply_body == NULL) {
1443 LOGE("Failed to create reply_body");
1444 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1448 g_list_free_full(id_list, free);
1451 g_variant_builder_unref(id_builder);
1456 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1459 GList *id_list = NULL;
1460 char *app_id = NULL;
1462 GVariantBuilder *id_builder = NULL;
1464 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1466 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1467 if (ret != STICKERD_SERVER_ERROR_NONE) {
1468 LOGE("Failed to get all sticker id");
1470 g_list_free_full(id_list, free);
1471 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1474 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1475 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1477 *reply_body = g_variant_new("(a(i))", id_builder);
1478 if (*reply_body == NULL) {
1479 LOGE("Failed to create reply_body");
1480 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1484 g_list_free_full(id_list, free);
1487 g_variant_builder_unref(id_builder);
1492 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1495 GList *id_list = NULL;
1496 char *app_id = NULL;
1497 int type, offset, count;
1498 GVariantBuilder *id_builder = NULL;
1500 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1502 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1503 if (ret != STICKERD_SERVER_ERROR_NONE) {
1504 LOGE("Failed to get all sticker id");
1506 g_list_free_full(id_list, free);
1507 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1510 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1511 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1513 *reply_body = g_variant_new("(a(i))", id_builder);
1514 if (*reply_body == NULL) {
1515 LOGE("Failed to create reply_body");
1516 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1520 g_list_free_full(id_list, free);
1523 g_variant_builder_unref(id_builder);
1528 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1531 GList *id_list = NULL;
1532 char *app_id = NULL;
1535 GVariantBuilder *id_builder = NULL;
1537 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1539 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1540 if (ret != STICKERD_SERVER_ERROR_NONE) {
1541 LOGE("Failed to get all sticker id");
1543 g_list_free_full(id_list, free);
1544 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1547 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1548 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1550 *reply_body = g_variant_new("(a(i))", id_builder);
1551 if (*reply_body == NULL) {
1552 LOGE("Failed to create reply_body");
1553 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1557 g_list_free_full(id_list, free);
1560 g_variant_builder_unref(id_builder);
1565 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1568 GList *id_list = NULL;
1569 char *app_id = NULL;
1570 char *keyword = NULL;
1572 GVariantBuilder *id_builder = NULL;
1574 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1576 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1577 if (ret != STICKERD_SERVER_ERROR_NONE) {
1578 LOGE("Failed to get all sticker id");
1580 g_list_free_full(id_list, free);
1581 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1584 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1585 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1587 *reply_body = g_variant_new("(a(i))", id_builder);
1588 if (*reply_body == NULL) {
1589 LOGE("Failed to create reply_body");
1590 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1594 g_list_free_full(id_list, free);
1597 g_variant_builder_unref(id_builder);
1602 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1605 GList *id_list = NULL;
1606 char *app_id = NULL;
1607 int type, offset, count;
1608 GVariantBuilder *id_builder = NULL;
1610 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1612 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1613 if (ret != STICKERD_SERVER_ERROR_NONE) {
1614 LOGE("Failed to get all sticker id");
1616 g_list_free_full(id_list, free);
1617 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1620 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1621 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1623 *reply_body = g_variant_new("(a(i))", id_builder);
1624 if (*reply_body == NULL) {
1625 LOGE("Failed to create reply_body");
1626 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1630 g_list_free_full(id_list, free);
1633 g_variant_builder_unref(id_builder);
1638 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1641 GVariantBuilder *builder = NULL;
1642 char *app_id = NULL;
1645 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1647 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1648 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1649 if (ret != STICKERD_SERVER_ERROR_NONE) {
1650 LOGE("Failed to get sticker group list");
1651 g_variant_builder_unref(builder);
1652 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1655 *reply_body = g_variant_new("(a(s))", builder);
1656 g_variant_builder_unref(builder);
1658 if (*reply_body == NULL) {
1659 LOGE("Failed to create reply_body");
1660 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1666 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1672 *reply_body = g_variant_new("()");
1673 if (*reply_body == NULL) {
1674 LOGE("Failed to create reply_body");
1675 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1678 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1680 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1681 if (ret != STICKERD_SERVER_ERROR_NONE) {
1682 LOGE("Failed to update sticker disp_type");
1683 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1689 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1695 g_variant_get(parameters, "(&s)", &uri);
1697 ret = stickerd_db_check_file_exists(&result, uri);
1698 if (ret != STICKERD_SERVER_ERROR_NONE) {
1699 LOGE("Failed to get sticker count");
1700 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1703 *reply_body = g_variant_new("(i)", result);
1704 if (*reply_body == NULL) {
1705 LOGE("Failed to create reply_body");
1706 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1712 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1717 *reply_body = g_variant_new("()");
1718 if (*reply_body == NULL) {
1719 LOGE("Failed to create reply_body");
1720 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1723 g_variant_get(parameters, "(i)", &record_id);
1725 ret = stickerd_db_insert_recent_sticker_info(record_id);
1726 if (ret != STICKERD_SERVER_ERROR_NONE) {
1727 LOGE("Failed to insert recent sticker info");
1728 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1734 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1738 GList *id_list = NULL;
1739 GVariantBuilder *id_builder = NULL;
1741 g_variant_get(parameters, "(i)", &count);
1743 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1744 if (ret != STICKERD_SERVER_ERROR_NONE) {
1745 LOGE("Failed to get recent sticker id");
1747 g_list_free_full(id_list, free);
1748 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1751 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1752 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1754 *reply_body = g_variant_new("(a(i))", id_builder);
1755 if (*reply_body == NULL) {
1756 LOGE("Failed to create reply_body");
1757 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1761 g_list_free_full(id_list, free);
1764 g_variant_builder_unref(id_builder);
1769 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1771 int ret = STICKERD_SERVER_ERROR_NONE;
1774 *reply_body = g_variant_new("()");
1775 if (*reply_body == NULL) {
1776 LOGE("Failed to create reply_body");
1777 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1780 g_variant_get(parameters, "(i)", &record_id);
1782 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1784 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1785 if (ret == STICKERD_SERVER_ERROR_NONE)
1786 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1788 _free_sticker_data(sticker_info);
1789 sticker_info = NULL;
1795 int stickerd_get_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1799 GVariantBuilder *info_builder;
1800 GVariantBuilder *keyword_builder;
1802 g_variant_get(parameters, "(&s)", &uri);
1804 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1807 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1809 ret = stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1810 if (ret != STICKERD_SERVER_ERROR_NONE) {
1811 LOGE("Failed to get sticker info");
1812 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1816 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1817 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(sticker_info->record_id));
1818 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1819 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1820 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1821 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1822 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1823 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1824 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1825 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1827 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1828 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1830 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1831 g_variant_builder_unref(info_builder);
1832 g_variant_builder_unref(keyword_builder);
1834 if (*reply_body == NULL) {
1835 LOGE("Failed to create reply_body");
1836 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1841 _free_sticker_data(sticker_info);
1842 sticker_info = NULL;