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>
32 #include "stickerd_dbus.h"
33 #include "stickerd_data_manager.h"
34 #include "stickerd_db_manager.h"
35 #include "sticker_defs.h"
36 #include "stickerd_error.h"
41 #define LOG_TAG "STICKERD_DATA_MANAGER"
43 #define MAX_ERROR_BUFFER 256
44 #define TERMINATE_TIMER_INTERVAL 3000
47 STICKER_URI_TYPE_NONE,
48 STICKER_URI_TYPE_LOCAL_PATH,
49 STICKER_URI_TYPE_WEB_RESOURCE,
52 static GHashTable *_monitoring_hash = NULL;
53 static char error_buffer[MAX_ERROR_BUFFER];
54 static GList *consumer_list = NULL;
55 static guint _terminate_timer_id = 0;
56 extern GMainLoop *main_loop;
58 static void _terminate_daemon()
60 LOGD("Terminate sticker daemon");
61 g_hash_table_destroy(_monitoring_hash);
62 _monitoring_hash = NULL;
63 g_list_free_full(consumer_list, free);
65 g_main_loop_quit(main_loop);
68 static gboolean _terminate_timer_handler(gpointer user_data)
74 static void _check_watcher_exist()
76 if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
77 if (_terminate_timer_id != 0) {
78 g_source_remove(_terminate_timer_id);
79 _terminate_timer_id = 0;
82 _terminate_timer_id = g_timeout_add(TERMINATE_TIMER_INTERVAL, _terminate_timer_handler, NULL);
83 if (_terminate_timer_id <= 0)
88 static void _on_name_appeared(GDBusConnection *connection,
90 const gchar *name_owner,
93 LOGD("name: %s", name);
94 if (_terminate_timer_id != 0) {
95 g_source_remove(_terminate_timer_id);
96 _terminate_timer_id = 0;
100 static void _on_name_vanished(GDBusConnection *connection,
104 monitoring_info_s *info = (monitoring_info_s *)user_data;
107 if (_monitoring_hash != NULL && g_hash_table_lookup(_monitoring_hash, GUINT_TO_POINTER(info->watcher_id)) != NULL) {
108 LOGD("name: %s", name);
109 g_bus_unwatch_name(info->watcher_id);
110 delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
113 if (g_list_find(consumer_list, info->bus_name))
114 consumer_list = g_list_remove(consumer_list, info->bus_name);
117 free(info->bus_name);
122 _check_watcher_exist();
125 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
126 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
129 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
131 if (_monitoring_hash == NULL)
132 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
134 if (consumer_list == NULL)
135 consumer_list = g_list_alloc();
137 GVariant *reply_body = NULL;
138 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
140 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
141 ret = stickerd_server_register(parameters, &reply_body, sender,
142 _on_name_appeared, _on_name_vanished, &_monitoring_hash, &consumer_list);
143 } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
144 ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash, &consumer_list);
145 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
146 ret = stickerd_insert_sticker_info(parameters, &reply_body);
147 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
148 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
149 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
150 ret = stickerd_del_sticker_info(parameters, &reply_body);
151 } else if (g_strcmp0(method_name, "delete_sticker_info_by_uri") == 0) {
152 ret = stickerd_del_sticker_info_by_uri(parameters, &reply_body);
153 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
154 ret = stickerd_update_sticker_type(parameters, &reply_body);
155 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
156 ret = stickerd_update_sticker_uri(parameters, &reply_body);
157 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
158 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
159 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
160 ret = stickerd_update_sticker_description(parameters, &reply_body);
161 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
162 ret = stickerd_update_sticker_group(parameters, &reply_body);
163 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
164 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
165 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
166 ret = stickerd_get_sticker_info(parameters, &reply_body);
167 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
168 ret = stickerd_get_group_list(parameters, &reply_body);
169 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
170 ret = stickerd_get_keyword_list(parameters, &reply_body);
171 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
172 ret = stickerd_get_sticker_count(parameters, &reply_body);
173 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
174 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
175 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
176 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
177 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
178 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
179 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
180 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
181 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
182 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
183 } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
184 ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
185 } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
186 ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
187 } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
188 ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
189 } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
190 ret = stickerd_check_file_exists(parameters, &reply_body);
191 } else if (g_strcmp0(method_name, "insert_recent_sticker_info") == 0) {
192 ret = stickerd_insert_recent_sticker_info(parameters, &reply_body);
193 } else if (g_strcmp0(method_name, "get_recent_sticker_info") == 0) {
194 ret = stickerd_get_recent_sticker_info(parameters, &reply_body);
195 } else if (g_strcmp0(method_name, "send_update_event") == 0) {
196 ret = stickerd_send_update_event(parameters, &reply_body);
197 } else if (g_strcmp0(method_name, "get_sticker_info_by_uri") == 0) {
198 ret = stickerd_get_sticker_info_by_uri(parameters, &reply_body);
199 } else if (g_strcmp0(method_name, "check_group_exists") == 0) {
200 ret = stickerd_check_group_exists(parameters, &reply_body);
201 } else if (g_strcmp0(method_name, "set_group_image") == 0) {
202 ret = stickerd_set_group_image(parameters, &reply_body);
203 } else if (g_strcmp0(method_name, "get_group_image_list") == 0) {
204 ret = stickerd_get_group_image_list(parameters, &reply_body);
207 if (ret == STICKERD_SERVER_ERROR_NONE) {
208 LOGD("method_call successful, method_name : %s", method_name);
209 g_dbus_method_invocation_return_value(invocation, reply_body);
211 LOGE("method_call failed, method_name : %s", method_name);
212 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
215 _check_watcher_exist();
218 static const GDBusInterfaceVTable _sticker_interface_vtable = {
219 _stickerd_client_dbus_method_call_handler,
224 int stickerd_register_dbus_interface(void)
226 static gchar introspection_xml[] =
228 " <interface name='org.tizen.sticker_service'>"
229 " <method name='sticker_service_register'>"
230 " <arg type='i' name='lib_type' direction='in'/>"
231 " <arg type='i' name='watcher_id' direction='out'/>"
234 " <method name='sticker_service_unregister'>"
235 " <arg type='i' name='lib_type' direction='in'/>"
236 " <arg type='i' name='watcher_id' direction='in'/>"
239 " <method name='insert_sticker_info'>"
240 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
241 " <arg type='a(s)' name='keyword_list' direction='in'/>"
242 " <arg type='i' name='record_id' direction='out'/>"
245 " <method name='update_sticker_info_by_json'>"
246 " <arg type='s' name='app_id' direction='in'/>"
247 " <arg type='s' name='json_path' direction='in'/>"
250 " <method name='delete_sticker_info'>"
251 " <arg type='i' name='record_id' direction='in'/>"
254 " <method name='delete_sticker_info_by_uri'>"
255 " <arg type='s' name='uri' direction='in'/>"
258 " <method name='update_sticker_type'>"
259 " <arg type='i' name='record_id' direction='in'/>"
260 " <arg type='i' name='type' direction='in'/>"
263 " <method name='update_sticker_uri'>"
264 " <arg type='i' name='record_id' direction='in'/>"
265 " <arg type='s' name='app_id' direction='in'/>"
266 " <arg type='i' name='type' direction='in'/>"
267 " <arg type='s' name='uri' direction='in'/>"
270 " <method name='update_sticker_thumbnail'>"
271 " <arg type='i' name='record_id' direction='in'/>"
272 " <arg type='s' name='app_id' direction='in'/>"
273 " <arg type='s' name='thumbnail' direction='in'/>"
276 " <method name='update_sticker_description'>"
277 " <arg type='i' name='record_id' direction='in'/>"
278 " <arg type='s' name='description' direction='in'/>"
281 " <method name='update_sticker_group'>"
282 " <arg type='i' name='record_id' direction='in'/>"
283 " <arg type='s' name='group' direction='in'/>"
286 " <method name='update_sticker_keyword'>"
287 " <arg type='i' name='record_id' direction='in'/>"
288 " <arg type='a(s)' name='keyword' direction='in'/>"
291 " <method name='get_sticker_info'>"
292 " <arg type='i' name='record_id' direction='in'/>"
293 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
294 " <arg type='a(s)' name='keyword_list' direction='out'/>"
297 " <method name='get_group_list'>"
298 " <arg type='s' name='app_id' direction='in'/>"
299 " <arg type='a(s)' name='group_list' direction='out'/>"
302 " <method name='get_keyword_list'>"
303 " <arg type='s' name='app_id' direction='in'/>"
304 " <arg type='a(s)' name='keyword_list' direction='out'/>"
307 " <method name='get_sticker_count'>"
308 " <arg type='s' name='app_id' direction='in'/>"
309 " <arg type='i' name='count' direction='out'/>"
312 " <method name='get_all_sticker_info'>"
313 " <arg type='s' name='app_id' direction='in'/>"
314 " <arg type='i' name='offset' direction='in'/>"
315 " <arg type='i' name='count' direction='in'/>"
316 " <arg type='a(i)' name='id_list' direction='out'/>"
319 " <method name='get_sticker_info_by_appid'>"
320 " <arg type='s' name='app_id' direction='in'/>"
321 " <arg type='i' name='offset' direction='in'/>"
322 " <arg type='i' name='count' direction='in'/>"
323 " <arg type='a(i)' name='id_list' direction='out'/>"
326 " <method name='get_sticker_info_by_type'>"
327 " <arg type='s' name='app_id' direction='in'/>"
328 " <arg type='i' name='type' direction='in'/>"
329 " <arg type='i' name='offset' direction='in'/>"
330 " <arg type='i' name='count' direction='in'/>"
331 " <arg type='a(i)' name='id_list' direction='out'/>"
334 " <method name='get_sticker_info_by_group'>"
335 " <arg type='s' name='app_id' direction='in'/>"
336 " <arg type='s' name='group' direction='in'/>"
337 " <arg type='i' name='offset' direction='in'/>"
338 " <arg type='i' name='count' direction='in'/>"
339 " <arg type='a(i)' name='id_list' direction='out'/>"
342 " <method name='get_sticker_info_by_keyword'>"
343 " <arg type='s' name='app_id' direction='in'/>"
344 " <arg type='s' name='keyword' direction='in'/>"
345 " <arg type='i' name='offset' direction='in'/>"
346 " <arg type='i' name='count' direction='in'/>"
347 " <arg type='a(i)' name='id_list' direction='out'/>"
350 " <method name='get_sticker_info_by_disp_type'>"
351 " <arg type='s' name='app_id' direction='in'/>"
352 " <arg type='i' name='type' direction='in'/>"
353 " <arg type='i' name='offset' direction='in'/>"
354 " <arg type='i' name='count' direction='in'/>"
355 " <arg type='a(i)' name='id_list' direction='out'/>"
358 " <method name='get_group_list_by_disp_type'>"
359 " <arg type='s' name='app_id' direction='in'/>"
360 " <arg type='i' name='disp_type' direction='in'/>"
361 " <arg type='a(s)' name='group_list' direction='out'/>"
364 " <method name='update_sticker_disp_type'>"
365 " <arg type='i' name='record_id' direction='in'/>"
366 " <arg type='i' name='disp_type' direction='in'/>"
369 " <method name='check_file_exists'>"
370 " <arg type='s' name='uri' direction='in'/>"
371 " <arg type='i' name='result' direction='out'/>"
374 " <method name='insert_recent_sticker_info'>"
375 " <arg type='i' name='record_id' direction='in'/>"
378 " <method name='get_recent_sticker_info'>"
379 " <arg type='i' name='count' direction='in'/>"
380 " <arg type='a(i)' name='id_list' direction='out'/>"
383 " <method name='send_update_event'>"
384 " <arg type='i' name='record_id' direction='in'/>"
387 " <method name='get_sticker_info_by_uri'>"
388 " <arg type='s' name='uri' direction='in'/>"
389 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
390 " <arg type='a(s)' name='keyword_list' direction='out'/>"
393 " <method name='check_group_exists'>"
394 " <arg type='s' name='app_id' direction='in'/>"
395 " <arg type='s' name='group' direction='in'/>"
396 " <arg type='i' name='result' direction='out'/>"
399 " <method name='set_group_image'>"
400 " <arg type='s' name='app_id' direction='in'/>"
401 " <arg type='s' name='group' direction='in'/>"
402 " <arg type='i' name='type' direction='in'/>"
403 " <arg type='s' name='uri' direction='in'/>"
406 " <method name='get_group_image_list'>"
407 " <arg type='s' name='app_id' direction='in'/>"
408 " <arg type='a(sis)' name='group_image_list' direction='out'/>"
413 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
416 int stickerd_dbus_init(void)
420 ret = stickerd_register_dbus_interface();
421 if (ret != STICKERD_SERVER_ERROR_NONE) {
422 LOGE("Failed to register dbus interface : %d", ret);
423 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
426 return STICKERD_SERVER_ERROR_NONE;
429 static int _check_file_exist(const char *app_id, const char *path)
433 package_info_h package_info = NULL;
434 char *app_path = NULL;
435 char *file_path = NULL;
437 if (access(path, F_OK) == 0) {
442 ret = package_info_create(app_id, &package_info);
443 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
444 LOGE("failed to create package_info. ret: %d", ret);
449 ret = package_info_get_root_path(package_info, &app_path);
450 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
451 LOGE("failed to create package_info. ret: %d", ret);
456 path_len = strlen(app_path) + strlen(path) + 2;
457 file_path = (char *)calloc(path_len, sizeof(char));
459 LOGE("Failed to alloc memory");
465 snprintf(file_path, path_len, "%s%s",app_path, path);
467 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
469 if (access(file_path, F_OK) != 0) {
470 LOGE("%s does not exist", file_path);
477 package_info_destroy(package_info);
492 static int _mkdirs(const char *path, mode_t mode)
495 char prev_path[2048];
496 const char *tmp = path;
498 if (!path || strlen(path) > 2048)
501 memset(prev_path, '\0', 2048);
502 while ((tmp = strchr(tmp, '/')) != NULL) {
509 strncpy(prev_path, path, len);
510 prev_path[len] = 0x00;
512 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
513 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
514 LOGE("directory create error : %s", error_buffer);
519 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
520 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
521 LOGE("directory create error : %s", error_buffer);
528 static int _file_copy(const char *src, const char *dest)
531 int fd = -1, n_fd = -1;
536 memset(buf, '\0', 4096);
537 fd = open(src, O_RDONLY);
539 LOGE("Failed to open file (%s) for reading", src);
542 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
544 LOGE("Failed to open file (%s) for writing", dest);
547 if (fd == -1 || n_fd == -1) {
553 while ((size = read(fd, buf, 4096))) {
563 while (write(n_fd, buf, size) == -1) {
564 if (errno == EINTR) {
584 static char* _convert_sticker_uri(const char *uri, const char *appid)
587 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
588 char * new_path = (char *)calloc(len, sizeof(char));
589 if (new_path == NULL) {
590 LOGE("Failed to alloc memory");
595 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
597 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
599 if (access(new_path, F_OK) == 0) {
600 LOGE("sticker file already exists : %s", new_path);
605 ret = _mkdirs(new_path, 0755);
607 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
608 LOGE("directory create error : %s", error_buffer);
612 ret = _file_copy(uri, new_path);
614 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
615 LOGE("failed to copy sticker file : %s", error_buffer);
628 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
631 LOGE("keyword doesn't exist");
635 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
638 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
640 GVariantBuilder *info_builder;
641 GVariantBuilder *keyword_builder;
643 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
644 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
645 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
646 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
647 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
648 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
649 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
650 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
651 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
653 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
654 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
656 GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
657 g_variant_builder_unref(info_builder);
658 g_variant_builder_unref(keyword_builder);
666 void _get_consumer_busname(gpointer data, gpointer user_data)
672 char *cmd = "send_sticker_changed_event";
673 char *sender = (char *)data;
674 GVariant *body = (GVariant *)user_data;
676 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
677 if (ret != STICKERD_SERVER_ERROR_NONE)
678 LOGE("Failed to send sticker changed event");
681 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
683 GVariant *body = _get_sticker_g_variant(type, sticker_info);
686 g_list_foreach(consumer_list, _get_consumer_busname, body);
689 g_variant_unref(body);
692 static void _free_sticker_data(sticker_info_db *sticker_data)
697 if (sticker_data->app_id) {
698 free(sticker_data->app_id);
699 sticker_data->app_id = NULL;
702 if (sticker_data->uri) {
703 free(sticker_data->uri);
704 sticker_data->uri = NULL;
707 if (sticker_data->thumbnail) {
708 free(sticker_data->thumbnail);
709 sticker_data->thumbnail = NULL;
712 if (sticker_data->keyword) {
713 g_list_free_full(sticker_data->keyword, free);
714 sticker_data->keyword = NULL;
717 if (sticker_data->group) {
718 free(sticker_data->group);
719 sticker_data->group = NULL;
722 if (sticker_data->description) {
723 free(sticker_data->description);
724 sticker_data->description = NULL;
727 if (sticker_data->date) {
728 free(sticker_data->date);
729 sticker_data->date = NULL;
735 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
739 STICKER_DAT_TYPE key;
740 sticker_info_db *sticker_info = NULL;
741 GVariant *value = NULL;
742 GVariantIter *info_iter = NULL;
743 GVariantIter *keyword_iter = NULL;
746 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
747 if (!info_iter || !keyword_iter) {
748 LOGD("failed to get iter");
749 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
753 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
756 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
760 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
762 case STICKER_DATA_TYPE_APP_ID:
763 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
765 case STICKER_DATA_TYPE_URI_TYPE:
766 sticker_info->type = g_variant_get_int32(value);
768 case STICKER_DATA_TYPE_URI:
769 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
771 case STICKER_DATA_TYPE_THUMBNAIL:
772 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
774 case STICKER_DATA_TYPE_DESCRIPTION:
775 sticker_info->description = (char *) g_variant_get_string(value, NULL);
777 case STICKER_DATA_TYPE_GROUP:
778 sticker_info->group = (char *) g_variant_get_string(value, NULL);
780 case STICKER_DATA_TYPE_DISP_TYPE:
781 sticker_info->display_type = g_variant_get_int32(value);
787 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
788 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
791 if (sticker_info->type == STICKER_URI_TYPE_LOCAL_PATH) {
792 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
793 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
794 if (!sticker_info->uri) {
795 LOGE("failed to copy sticker file");
796 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
800 LOGE("sticker file does not exist");
801 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
806 if (sticker_info->thumbnail) {
807 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
808 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
809 if (!sticker_info->thumbnail) {
810 LOGE("failed to copy sticker thumbnail");
811 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
815 LOGE("sticker thumbnail does not exist");
816 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
821 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
822 if (ret != STICKERD_SERVER_ERROR_NONE) {
823 LOGE("Failed to insert sticker info");
824 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
828 *reply_body = g_variant_new("(i)", record_id);
829 if (*reply_body == NULL) {
830 LOGE("Failed to create reply_body");
831 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
833 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
837 g_variant_unref(value);
840 g_variant_iter_free(info_iter);
843 g_variant_iter_free(keyword_iter);
853 static char* _get_string_from_object(JsonObject *object, const char *key)
855 if (json_object_has_member(object, key) == false)
858 const char *str = json_object_get_string_member(object, key);
865 static int _get_int_from_object(JsonObject *object, const char *key)
867 if (json_object_has_member(object, key) == false)
870 int type = json_object_get_int_member(object, key);
875 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
877 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
879 sticker_info_db *sticker_info = NULL;
881 char *json_path = NULL;
882 JsonParser* parser = NULL;
883 GError* err_msg = NULL;
884 GVariant *body = NULL;
885 char *cmd = "send_insert_result";
886 JsonArray *sticker_arr = NULL;
887 JsonArray *keyword_arr = NULL;
889 int keyword_arr_len = 0;
890 const char *keyword = NULL;
892 JsonNode *root = NULL;
893 JsonObject *root_obj = NULL;
895 *reply_body = g_variant_new("()");
896 if (*reply_body == NULL) {
897 LOGE("Failed to create reply_body");
898 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
901 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
903 if (!app_id || !json_path) {
904 LOGE("failed to get parameter");
905 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
908 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
910 parser = json_parser_new();
911 json_parser_load_from_file(parser, json_path, &err_msg);
913 LOGE("failed to load json file. error message: %s", err_msg->message);
914 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
918 root = json_parser_get_root(parser);
920 LOGE("failed to get root");
921 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
925 root_obj = json_node_get_object(root);
926 if (root_obj == NULL) {
927 LOGE("failed to get object");
928 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
932 sticker_arr = json_object_get_array_member(root_obj, "sticker");
933 if (sticker_arr == NULL) {
934 LOGE("failed to get array member");
935 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
939 arr_len = json_array_get_length(sticker_arr);
940 for (int i = 0; i < arr_len; i++) {
941 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
942 if (info_object != NULL) {
943 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
945 LOGE("Failed to alloc memory");
949 sticker_info->app_id = strdup(app_id);
950 if (!sticker_info->app_id)
953 sticker_info->type = _get_int_from_object(info_object, "type");
954 if (sticker_info->type < 1)
957 sticker_info->uri = _get_string_from_object(info_object, "uri");
958 if (!sticker_info->uri || sticker_info->uri[0] == '\0')
961 if (sticker_info->type == STICKER_URI_TYPE_LOCAL_PATH) {
962 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
963 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
964 if (!sticker_info->uri)
971 sticker_info->group = _get_string_from_object(info_object, "group");
972 if (!sticker_info->group)
975 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
976 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
977 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
978 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
979 if (!sticker_info->thumbnail)
986 sticker_info->description = _get_string_from_object(info_object, "description");
988 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
990 keyword_arr = json_object_get_array_member(info_object, "keyword");
991 keyword_arr_len = json_array_get_length(keyword_arr);
992 if (keyword_arr_len < 1)
995 for (int j = 0; j < keyword_arr_len; j++) {
996 keyword = json_array_get_string_element(keyword_arr, j);
998 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup(keyword));
1001 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
1002 if (ret != STICKERD_SERVER_ERROR_NONE) {
1003 LOGE("Failed to insert sticker info");
1004 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1006 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
1009 _free_sticker_data(sticker_info);
1010 sticker_info = NULL;
1016 g_error_free(err_msg);
1018 g_object_unref(parser);
1020 body = g_variant_new("(i)", ret);
1022 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
1023 if (ret != STICKERD_SERVER_ERROR_NONE)
1024 LOGE("Failed to send insert result to client");
1027 g_variant_unref(body);
1032 int stickerd_del_sticker_info(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)", &record_id);
1045 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1047 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1049 ret = stickerd_db_delete_sticker_info(record_id);
1050 if (ret != STICKERD_SERVER_ERROR_NONE) {
1051 LOGE("Failed to delete sticker info");
1052 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1054 if (sticker_info && sticker_info->uri)
1055 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1059 _free_sticker_data(sticker_info);
1060 sticker_info = NULL;
1066 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1071 *reply_body = g_variant_new("()");
1072 if (*reply_body == NULL) {
1073 LOGE("Failed to create reply_body");
1074 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1077 g_variant_get(parameters, "(&s)", &uri);
1079 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1081 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1083 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1084 if (ret != STICKERD_SERVER_ERROR_NONE) {
1085 LOGE("Failed to delete sticker info");
1086 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1088 if (sticker_info && sticker_info->uri)
1089 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1093 _free_sticker_data(sticker_info);
1094 sticker_info = NULL;
1100 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1106 *reply_body = g_variant_new("()");
1107 if (*reply_body == NULL) {
1108 LOGE("Failed to create reply_body");
1109 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1112 g_variant_get(parameters, "(ii)", &record_id, &type);
1114 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1115 if (ret != STICKERD_SERVER_ERROR_NONE) {
1116 LOGE("Failed to update sticker type");
1117 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1123 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1131 *reply_body = g_variant_new("()");
1132 if (*reply_body == NULL) {
1133 LOGE("Failed to create reply_body");
1134 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1137 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1139 if (type == STICKER_URI_TYPE_LOCAL_PATH) {
1140 if (_check_file_exist(app_id, uri) == 0) {
1141 uri = _convert_sticker_uri(uri, app_id);
1143 LOGE("failed to copy sticker file");
1144 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1147 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1151 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1152 if (ret != STICKERD_SERVER_ERROR_NONE) {
1153 LOGE("Failed to update sticker uri");
1154 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1160 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1167 *reply_body = g_variant_new("()");
1168 if (*reply_body == NULL) {
1169 LOGE("Failed to create reply_body");
1170 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1173 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1175 if (_check_file_exist(app_id, thumbnail) == 0) {
1176 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1178 LOGE("failed to copy sticker thumbnail");
1179 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1182 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1185 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1186 if (ret != STICKERD_SERVER_ERROR_NONE) {
1187 LOGE("Failed to update sticker thumbnail");
1188 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1194 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1200 *reply_body = g_variant_new("()");
1201 if (*reply_body == NULL) {
1202 LOGE("Failed to create reply_body");
1203 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1206 g_variant_get(parameters, "(i&s)", &record_id, &description);
1208 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1209 if (ret != STICKERD_SERVER_ERROR_NONE) {
1210 LOGE("Failed to update sticker description");
1211 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1217 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1223 *reply_body = g_variant_new("()");
1224 if (*reply_body == NULL) {
1225 LOGE("Failed to create reply_body");
1226 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1229 g_variant_get(parameters, "(i&s)", &record_id, &group);
1231 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1232 if (ret != STICKERD_SERVER_ERROR_NONE) {
1233 LOGE("Failed to update sticker group");
1234 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1240 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1244 GVariantIter *keyword_iter = NULL;
1245 char *keyword = NULL;
1246 GList *keyword_list = NULL;
1248 *reply_body = g_variant_new("()");
1249 if (*reply_body == NULL) {
1250 LOGE("Failed to create reply_body");
1251 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1254 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1256 if (!keyword_iter) {
1257 LOGD("failed to get iter");
1258 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1261 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1262 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1265 g_variant_iter_free(keyword_iter);
1267 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1268 if (ret != STICKERD_SERVER_ERROR_NONE) {
1269 LOGE("Failed to update sticker keyword");
1270 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1276 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1280 GVariantBuilder *info_builder;
1281 GVariantBuilder *keyword_builder;
1283 g_variant_get(parameters, "(i)", &record_id);
1284 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1287 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1289 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1290 if (ret != STICKERD_SERVER_ERROR_NONE) {
1291 LOGE("Failed to get sticker info");
1292 _free_sticker_data(sticker_info);
1293 sticker_info = NULL;
1294 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1297 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1298 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1299 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1300 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1301 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1302 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1303 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1304 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1305 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1307 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1308 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1310 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1311 g_variant_builder_unref(info_builder);
1312 g_variant_builder_unref(keyword_builder);
1314 if (*reply_body == NULL) {
1315 LOGE("Failed to create reply_body");
1316 _free_sticker_data(sticker_info);
1317 sticker_info = NULL;
1318 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1321 _free_sticker_data(sticker_info);
1322 sticker_info = NULL;
1327 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1330 GVariantBuilder *builder = NULL;
1331 char *app_id = NULL;
1333 g_variant_get(parameters, "(&s)", &app_id);
1335 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1336 ret = stickerd_db_get_group_list(builder, app_id);
1337 if (ret != STICKERD_SERVER_ERROR_NONE) {
1338 LOGE("Failed to get sticker group list");
1339 g_variant_builder_unref(builder);
1340 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1343 *reply_body = g_variant_new("(a(s))", builder);
1344 g_variant_builder_unref(builder);
1346 if (*reply_body == NULL) {
1347 LOGE("Failed to create reply_body");
1348 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1354 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1357 GVariantBuilder *builder = NULL;
1358 char *app_id = NULL;
1360 g_variant_get(parameters, "(&s)", &app_id);
1362 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1363 ret = stickerd_db_get_keyword_list(builder, app_id);
1364 if (ret != STICKERD_SERVER_ERROR_NONE) {
1365 LOGE("Failed to get sticker keyword list");
1366 g_variant_builder_unref(builder);
1367 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1370 *reply_body = g_variant_new("(a(s))", builder);
1371 g_variant_builder_unref(builder);
1373 if (*reply_body == NULL) {
1374 LOGE("Failed to create reply_body");
1375 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1381 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1385 char *app_id = NULL;
1387 g_variant_get(parameters, "(&s)", &app_id);
1389 ret = stickerd_db_get_sticker_count(&count, app_id);
1390 if (ret != STICKERD_SERVER_ERROR_NONE) {
1391 LOGE("Failed to get sticker count");
1392 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1395 *reply_body = g_variant_new("(i)", count);
1396 if (*reply_body == NULL) {
1397 LOGE("Failed to create reply_body");
1398 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1404 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1407 LOGE("id doesn't exist");
1411 g_variant_builder_add(id_builder, "(i)", atoi(id));
1414 static int _set_reply_body(GList *list, GVariant **reply_body)
1416 int ret = STICKERD_SERVER_ERROR_NONE;
1417 GVariantBuilder *builder = NULL;
1419 builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1420 g_list_foreach(list, (GFunc) _set_id_builder, builder);
1422 *reply_body = g_variant_new("(a(i))", builder);
1423 if (*reply_body == NULL) {
1424 LOGE("Failed to create reply_body");
1425 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1429 g_variant_builder_unref(builder);
1434 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1438 char *app_id = NULL;
1439 GList *id_list = NULL;
1441 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1443 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1444 if (ret != STICKERD_SERVER_ERROR_NONE) {
1445 LOGE("Failed to get all sticker id");
1447 g_list_free_full(id_list, free);
1448 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1451 ret = _set_reply_body(id_list, reply_body);
1453 g_list_free_full(id_list, free);
1458 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1461 GList *id_list = NULL;
1462 char *app_id = NULL;
1465 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1467 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1468 if (ret != STICKERD_SERVER_ERROR_NONE) {
1469 LOGE("Failed to get all sticker id");
1471 g_list_free_full(id_list, free);
1472 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1475 ret = _set_reply_body(id_list, reply_body);
1477 g_list_free_full(id_list, free);
1482 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1485 GList *id_list = NULL;
1486 char *app_id = NULL;
1487 int type, offset, count;
1489 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1491 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1492 if (ret != STICKERD_SERVER_ERROR_NONE) {
1493 LOGE("Failed to get all sticker id");
1495 g_list_free_full(id_list, free);
1496 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1499 ret = _set_reply_body(id_list, reply_body);
1501 g_list_free_full(id_list, free);
1506 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1509 GList *id_list = NULL;
1510 char *app_id = NULL;
1514 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1516 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1517 if (ret != STICKERD_SERVER_ERROR_NONE) {
1518 LOGE("Failed to get all sticker id");
1520 g_list_free_full(id_list, free);
1521 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1524 ret = _set_reply_body(id_list, reply_body);
1526 g_list_free_full(id_list, free);
1531 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1534 GList *id_list = NULL;
1535 char *app_id = NULL;
1536 char *keyword = NULL;
1539 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1541 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1542 if (ret != STICKERD_SERVER_ERROR_NONE) {
1543 LOGE("Failed to get all sticker id");
1545 g_list_free_full(id_list, free);
1546 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1549 ret = _set_reply_body(id_list, reply_body);
1551 g_list_free_full(id_list, free);
1556 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1559 GList *id_list = NULL;
1560 char *app_id = NULL;
1561 int type, offset, count;
1563 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1565 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1566 if (ret != STICKERD_SERVER_ERROR_NONE) {
1567 LOGE("Failed to get all sticker id");
1569 g_list_free_full(id_list, free);
1570 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1573 ret = _set_reply_body(id_list, reply_body);
1575 g_list_free_full(id_list, free);
1580 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1583 GVariantBuilder *builder = NULL;
1584 char *app_id = NULL;
1587 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1589 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1590 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1591 if (ret != STICKERD_SERVER_ERROR_NONE) {
1592 LOGE("Failed to get sticker group list");
1593 g_variant_builder_unref(builder);
1594 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1597 *reply_body = g_variant_new("(a(s))", builder);
1598 g_variant_builder_unref(builder);
1600 if (*reply_body == NULL) {
1601 LOGE("Failed to create reply_body");
1602 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1608 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1614 *reply_body = g_variant_new("()");
1615 if (*reply_body == NULL) {
1616 LOGE("Failed to create reply_body");
1617 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1620 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1622 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1623 if (ret != STICKERD_SERVER_ERROR_NONE) {
1624 LOGE("Failed to update sticker disp_type");
1625 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1631 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1637 g_variant_get(parameters, "(&s)", &uri);
1639 ret = stickerd_db_check_file_exists(&result, uri);
1640 if (ret != STICKERD_SERVER_ERROR_NONE) {
1641 LOGE("Failed to get sticker count");
1642 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1645 *reply_body = g_variant_new("(i)", result);
1646 if (*reply_body == NULL) {
1647 LOGE("Failed to create reply_body");
1648 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1654 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1659 *reply_body = g_variant_new("()");
1660 if (*reply_body == NULL) {
1661 LOGE("Failed to create reply_body");
1662 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1665 g_variant_get(parameters, "(i)", &record_id);
1667 ret = stickerd_db_insert_recent_sticker_info(record_id);
1668 if (ret != STICKERD_SERVER_ERROR_NONE) {
1669 LOGE("Failed to insert recent sticker info");
1670 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1676 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1680 GList *id_list = NULL;
1682 g_variant_get(parameters, "(i)", &count);
1684 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1685 if (ret != STICKERD_SERVER_ERROR_NONE) {
1686 LOGE("Failed to get recent sticker id");
1688 g_list_free_full(id_list, free);
1689 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1692 ret = _set_reply_body(id_list, reply_body);
1694 g_list_free_full(id_list, free);
1699 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1701 int ret = STICKERD_SERVER_ERROR_NONE;
1704 *reply_body = g_variant_new("()");
1705 if (*reply_body == NULL) {
1706 LOGE("Failed to create reply_body");
1707 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1710 g_variant_get(parameters, "(i)", &record_id);
1712 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1714 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1715 if (ret == STICKERD_SERVER_ERROR_NONE)
1716 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1718 _free_sticker_data(sticker_info);
1719 sticker_info = NULL;
1725 int stickerd_get_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1729 GVariantBuilder *info_builder;
1730 GVariantBuilder *keyword_builder;
1732 g_variant_get(parameters, "(&s)", &uri);
1734 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1737 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1739 ret = stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1740 if (ret != STICKERD_SERVER_ERROR_NONE) {
1741 LOGE("Failed to get sticker info");
1742 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1746 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1747 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(sticker_info->record_id));
1748 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1749 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1750 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1751 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1752 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1753 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1754 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1755 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1757 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1758 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1760 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1761 g_variant_builder_unref(info_builder);
1762 g_variant_builder_unref(keyword_builder);
1764 if (*reply_body == NULL) {
1765 LOGE("Failed to create reply_body");
1766 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1771 _free_sticker_data(sticker_info);
1772 sticker_info = NULL;
1778 int stickerd_check_group_exists(GVariant *parameters, GVariant **reply_body)
1782 char *app_id = NULL;
1785 g_variant_get(parameters, "(&s&s)", &app_id, &group);
1787 ret = stickerd_db_check_group_exists(&result, app_id, group);
1788 if (ret != STICKERD_SERVER_ERROR_NONE) {
1789 LOGE("Failed to check group exists");
1790 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1793 *reply_body = g_variant_new("(i)", result);
1794 if (*reply_body == NULL) {
1795 LOGE("Failed to create reply_body");
1796 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1802 int stickerd_set_group_image(GVariant *parameters, GVariant **reply_body)
1806 char *app_id = NULL;
1810 *reply_body = g_variant_new("()");
1811 if (*reply_body == NULL) {
1812 LOGE("Failed to create reply_body");
1813 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1816 g_variant_get(parameters, "(&s&si&s)", &app_id, &group, &type, &uri);
1818 if (type == STICKER_URI_TYPE_LOCAL_PATH) {
1819 if (_check_file_exist(app_id, uri) == 0) {
1820 uri = _convert_sticker_uri(uri, app_id);
1822 LOGE("failed to copy sticker file");
1823 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1826 LOGE("sticker file does not exist");
1827 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1831 ret = stickerd_db_set_group_image(app_id, group, type, uri);
1832 if (ret != STICKERD_SERVER_ERROR_NONE) {
1833 LOGE("Failed to set group image");
1834 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1840 int stickerd_get_group_image_list(GVariant *parameters, GVariant **reply_body)
1843 GVariantBuilder *builder = NULL;
1844 char *app_id = NULL;
1846 g_variant_get(parameters, "(&s)", &app_id);
1848 builder = g_variant_builder_new(G_VARIANT_TYPE("a(sis)"));
1849 ret = stickerd_db_get_group_image_list(builder, app_id);
1850 if (ret != STICKERD_SERVER_ERROR_NONE) {
1851 LOGE("Failed to get sticker group image list");
1852 g_variant_builder_unref(builder);
1853 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1856 *reply_body = g_variant_new("(a(sis))", builder);
1857 g_variant_builder_unref(builder);
1859 if (*reply_body == NULL) {
1860 LOGE("Failed to create reply_body");
1861 return STICKERD_SERVER_ERROR_OPERATION_FAILED;