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(consumer_list);
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 GList *node = g_list_find_custom(consumer_list, info->bus_name, (GCompareFunc) strcmp);
115 consumer_list = g_list_delete_link(consumer_list, node);
119 free(info->bus_name);
124 _check_watcher_exist();
127 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
128 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
131 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
133 if (_monitoring_hash == NULL)
134 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
136 GVariant *reply_body = NULL;
137 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
139 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
140 ret = stickerd_server_register(parameters, &reply_body, sender,
141 _on_name_appeared, _on_name_vanished, &_monitoring_hash, &consumer_list);
142 } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
143 ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash, &consumer_list);
144 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
145 ret = stickerd_insert_sticker_info(parameters, &reply_body);
146 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
147 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
148 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
149 ret = stickerd_del_sticker_info(parameters, &reply_body);
150 } else if (g_strcmp0(method_name, "delete_sticker_info_by_uri") == 0) {
151 ret = stickerd_del_sticker_info_by_uri(parameters, &reply_body);
152 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
153 ret = stickerd_update_sticker_type(parameters, &reply_body);
154 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
155 ret = stickerd_update_sticker_uri(parameters, &reply_body);
156 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
157 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
158 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
159 ret = stickerd_update_sticker_description(parameters, &reply_body);
160 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
161 ret = stickerd_update_sticker_group(parameters, &reply_body);
162 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
163 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
164 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
165 ret = stickerd_get_sticker_info(parameters, &reply_body);
166 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
167 ret = stickerd_get_group_list(parameters, &reply_body);
168 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
169 ret = stickerd_get_keyword_list(parameters, &reply_body);
170 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
171 ret = stickerd_get_sticker_count(parameters, &reply_body);
172 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
173 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
174 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
175 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
176 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
177 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
178 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
179 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
180 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
181 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
182 } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
183 ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
184 } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
185 ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
186 } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
187 ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
188 } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
189 ret = stickerd_check_file_exists(parameters, &reply_body);
190 } else if (g_strcmp0(method_name, "insert_recent_sticker_info") == 0) {
191 ret = stickerd_insert_recent_sticker_info(parameters, &reply_body);
192 } else if (g_strcmp0(method_name, "get_recent_sticker_info") == 0) {
193 ret = stickerd_get_recent_sticker_info(parameters, &reply_body);
194 } else if (g_strcmp0(method_name, "send_update_event") == 0) {
195 ret = stickerd_send_update_event(parameters, &reply_body);
196 } else if (g_strcmp0(method_name, "get_sticker_info_by_uri") == 0) {
197 ret = stickerd_get_sticker_info_by_uri(parameters, &reply_body);
198 } else if (g_strcmp0(method_name, "check_group_exists") == 0) {
199 ret = stickerd_check_group_exists(parameters, &reply_body);
200 } else if (g_strcmp0(method_name, "set_group_image") == 0) {
201 ret = stickerd_set_group_image(parameters, &reply_body);
202 } else if (g_strcmp0(method_name, "get_group_image_list") == 0) {
203 ret = stickerd_get_group_image_list(parameters, &reply_body);
206 if (ret == STICKERD_SERVER_ERROR_NONE) {
207 LOGD("method_call successful, method_name : %s", method_name);
208 g_dbus_method_invocation_return_value(invocation, reply_body);
210 LOGE("method_call failed, method_name : %s", method_name);
211 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
214 _check_watcher_exist();
217 static const GDBusInterfaceVTable _sticker_interface_vtable = {
218 _stickerd_client_dbus_method_call_handler,
223 int stickerd_register_dbus_interface(void)
225 static gchar introspection_xml[] =
227 " <interface name='org.tizen.sticker_service'>"
228 " <method name='sticker_service_register'>"
229 " <arg type='i' name='lib_type' direction='in'/>"
230 " <arg type='i' name='watcher_id' direction='out'/>"
233 " <method name='sticker_service_unregister'>"
234 " <arg type='i' name='lib_type' direction='in'/>"
235 " <arg type='i' name='watcher_id' direction='in'/>"
238 " <method name='insert_sticker_info'>"
239 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
240 " <arg type='a(s)' name='keyword_list' direction='in'/>"
241 " <arg type='i' name='record_id' direction='out'/>"
244 " <method name='update_sticker_info_by_json'>"
245 " <arg type='s' name='app_id' direction='in'/>"
246 " <arg type='s' name='json_path' direction='in'/>"
249 " <method name='delete_sticker_info'>"
250 " <arg type='i' name='record_id' direction='in'/>"
253 " <method name='delete_sticker_info_by_uri'>"
254 " <arg type='s' name='uri' direction='in'/>"
257 " <method name='update_sticker_type'>"
258 " <arg type='i' name='record_id' direction='in'/>"
259 " <arg type='i' name='type' direction='in'/>"
262 " <method name='update_sticker_uri'>"
263 " <arg type='i' name='record_id' direction='in'/>"
264 " <arg type='s' name='app_id' direction='in'/>"
265 " <arg type='i' name='type' direction='in'/>"
266 " <arg type='s' name='uri' direction='in'/>"
269 " <method name='update_sticker_thumbnail'>"
270 " <arg type='i' name='record_id' direction='in'/>"
271 " <arg type='s' name='app_id' direction='in'/>"
272 " <arg type='s' name='thumbnail' direction='in'/>"
275 " <method name='update_sticker_description'>"
276 " <arg type='i' name='record_id' direction='in'/>"
277 " <arg type='s' name='description' direction='in'/>"
280 " <method name='update_sticker_group'>"
281 " <arg type='i' name='record_id' direction='in'/>"
282 " <arg type='s' name='group' direction='in'/>"
285 " <method name='update_sticker_keyword'>"
286 " <arg type='i' name='record_id' direction='in'/>"
287 " <arg type='a(s)' name='keyword' direction='in'/>"
290 " <method name='get_sticker_info'>"
291 " <arg type='i' name='record_id' direction='in'/>"
292 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
293 " <arg type='a(s)' name='keyword_list' direction='out'/>"
296 " <method name='get_group_list'>"
297 " <arg type='s' name='app_id' direction='in'/>"
298 " <arg type='a(s)' name='group_list' direction='out'/>"
301 " <method name='get_keyword_list'>"
302 " <arg type='s' name='app_id' direction='in'/>"
303 " <arg type='a(s)' name='keyword_list' direction='out'/>"
306 " <method name='get_sticker_count'>"
307 " <arg type='s' name='app_id' direction='in'/>"
308 " <arg type='i' name='count' direction='out'/>"
311 " <method name='get_all_sticker_info'>"
312 " <arg type='s' name='app_id' direction='in'/>"
313 " <arg type='i' name='offset' direction='in'/>"
314 " <arg type='i' name='count' direction='in'/>"
315 " <arg type='a(i)' name='id_list' direction='out'/>"
318 " <method name='get_sticker_info_by_appid'>"
319 " <arg type='s' name='app_id' direction='in'/>"
320 " <arg type='i' name='offset' direction='in'/>"
321 " <arg type='i' name='count' direction='in'/>"
322 " <arg type='a(i)' name='id_list' direction='out'/>"
325 " <method name='get_sticker_info_by_type'>"
326 " <arg type='s' name='app_id' direction='in'/>"
327 " <arg type='i' name='type' direction='in'/>"
328 " <arg type='i' name='offset' direction='in'/>"
329 " <arg type='i' name='count' direction='in'/>"
330 " <arg type='a(i)' name='id_list' direction='out'/>"
333 " <method name='get_sticker_info_by_group'>"
334 " <arg type='s' name='app_id' direction='in'/>"
335 " <arg type='s' name='group' direction='in'/>"
336 " <arg type='i' name='offset' direction='in'/>"
337 " <arg type='i' name='count' direction='in'/>"
338 " <arg type='a(i)' name='id_list' direction='out'/>"
341 " <method name='get_sticker_info_by_keyword'>"
342 " <arg type='s' name='app_id' direction='in'/>"
343 " <arg type='s' name='keyword' direction='in'/>"
344 " <arg type='i' name='offset' direction='in'/>"
345 " <arg type='i' name='count' direction='in'/>"
346 " <arg type='a(i)' name='id_list' direction='out'/>"
349 " <method name='get_sticker_info_by_disp_type'>"
350 " <arg type='s' name='app_id' direction='in'/>"
351 " <arg type='i' name='type' direction='in'/>"
352 " <arg type='i' name='offset' direction='in'/>"
353 " <arg type='i' name='count' direction='in'/>"
354 " <arg type='a(i)' name='id_list' direction='out'/>"
357 " <method name='get_group_list_by_disp_type'>"
358 " <arg type='s' name='app_id' direction='in'/>"
359 " <arg type='i' name='disp_type' direction='in'/>"
360 " <arg type='a(s)' name='group_list' direction='out'/>"
363 " <method name='update_sticker_disp_type'>"
364 " <arg type='i' name='record_id' direction='in'/>"
365 " <arg type='i' name='disp_type' direction='in'/>"
368 " <method name='check_file_exists'>"
369 " <arg type='s' name='uri' direction='in'/>"
370 " <arg type='i' name='result' direction='out'/>"
373 " <method name='insert_recent_sticker_info'>"
374 " <arg type='i' name='record_id' direction='in'/>"
377 " <method name='get_recent_sticker_info'>"
378 " <arg type='i' name='count' direction='in'/>"
379 " <arg type='a(i)' name='id_list' direction='out'/>"
382 " <method name='send_update_event'>"
383 " <arg type='i' name='record_id' direction='in'/>"
386 " <method name='get_sticker_info_by_uri'>"
387 " <arg type='s' name='uri' direction='in'/>"
388 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
389 " <arg type='a(s)' name='keyword_list' direction='out'/>"
392 " <method name='check_group_exists'>"
393 " <arg type='s' name='app_id' direction='in'/>"
394 " <arg type='s' name='group' direction='in'/>"
395 " <arg type='i' name='result' direction='out'/>"
398 " <method name='set_group_image'>"
399 " <arg type='s' name='app_id' direction='in'/>"
400 " <arg type='s' name='group' direction='in'/>"
401 " <arg type='i' name='type' direction='in'/>"
402 " <arg type='s' name='uri' direction='in'/>"
405 " <method name='get_group_image_list'>"
406 " <arg type='s' name='app_id' direction='in'/>"
407 " <arg type='a(sis)' name='group_image_list' direction='out'/>"
412 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
415 int stickerd_dbus_init(void)
419 ret = stickerd_register_dbus_interface();
420 if (ret != STICKERD_SERVER_ERROR_NONE) {
421 LOGE("Failed to register dbus interface : %d", ret);
422 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
425 return STICKERD_SERVER_ERROR_NONE;
428 static int _check_file_exist(const char *app_id, const char *path)
432 package_info_h package_info = NULL;
433 char *app_path = NULL;
434 char *file_path = NULL;
436 if (access(path, F_OK) == 0) {
441 ret = package_info_create(app_id, &package_info);
442 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
443 LOGE("failed to create package_info. ret: %d", ret);
448 ret = package_info_get_root_path(package_info, &app_path);
449 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
450 LOGE("failed to create package_info. ret: %d", ret);
455 path_len = strlen(app_path) + strlen(path) + 2;
456 file_path = (char *)calloc(path_len, sizeof(char));
458 LOGE("Failed to alloc memory");
464 snprintf(file_path, path_len, "%s%s",app_path, path);
466 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
468 if (access(file_path, F_OK) != 0) {
469 LOGE("%s does not exist", file_path);
476 package_info_destroy(package_info);
491 static int _mkdirs(const char *path, mode_t mode)
494 char prev_path[2048];
495 const char *tmp = path;
497 if (!path || strlen(path) > 2048)
500 memset(prev_path, '\0', 2048);
501 while ((tmp = strchr(tmp, '/')) != NULL) {
508 strncpy(prev_path, path, len);
509 prev_path[len] = 0x00;
511 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
512 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
513 LOGE("directory create error : %s", error_buffer);
518 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
519 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
520 LOGE("directory create error : %s", error_buffer);
527 static int _file_copy(const char *src, const char *dest)
530 int fd = -1, n_fd = -1;
535 memset(buf, '\0', 4096);
536 fd = open(src, O_RDONLY);
538 LOGE("Failed to open file (%s) for reading", src);
541 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
543 LOGE("Failed to open file (%s) for writing", dest);
546 if (fd == -1 || n_fd == -1) {
552 while ((size = read(fd, buf, 4096))) {
562 while (write(n_fd, buf, size) == -1) {
563 if (errno == EINTR) {
583 static char* _convert_sticker_uri(const char *uri, const char *appid)
586 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
587 char * new_path = (char *)calloc(len, sizeof(char));
588 if (new_path == NULL) {
589 LOGE("Failed to alloc memory");
594 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
596 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
598 if (access(new_path, F_OK) == 0) {
599 LOGE("sticker file already exists : %s", new_path);
604 ret = _mkdirs(new_path, 0755);
606 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
607 LOGE("directory create error : %s", error_buffer);
611 ret = _file_copy(uri, new_path);
613 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
614 LOGE("failed to copy sticker file : %s", error_buffer);
627 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
630 LOGE("keyword doesn't exist");
634 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
637 static GVariant* _get_sticker_g_variant(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
639 GVariantBuilder *info_builder;
640 GVariantBuilder *keyword_builder;
642 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
643 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
644 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
645 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
646 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
647 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
648 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
649 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
650 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
652 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
653 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
655 GVariant *body = g_variant_new("(ia{iv}a(s))", (int)type, info_builder, keyword_builder);
656 g_variant_builder_unref(info_builder);
657 g_variant_builder_unref(keyword_builder);
665 void _get_consumer_busname(gpointer data, gpointer user_data)
671 char *cmd = "send_sticker_changed_event";
672 char *sender = (char *)data;
673 GVariant *body = (GVariant *)user_data;
675 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_CONSUMER);
676 if (ret != STICKERD_SERVER_ERROR_NONE)
677 LOGE("Failed to send sticker changed event");
680 static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db *sticker_info)
682 GVariant *body = _get_sticker_g_variant(type, sticker_info);
685 g_list_foreach(consumer_list, _get_consumer_busname, body);
688 g_variant_unref(body);
691 static void _free_sticker_data(sticker_info_db *sticker_data)
696 if (sticker_data->app_id) {
697 free(sticker_data->app_id);
698 sticker_data->app_id = NULL;
701 if (sticker_data->uri) {
702 free(sticker_data->uri);
703 sticker_data->uri = NULL;
706 if (sticker_data->thumbnail) {
707 free(sticker_data->thumbnail);
708 sticker_data->thumbnail = NULL;
711 if (sticker_data->keyword) {
712 g_list_free_full(sticker_data->keyword, free);
713 sticker_data->keyword = NULL;
716 if (sticker_data->group) {
717 free(sticker_data->group);
718 sticker_data->group = NULL;
721 if (sticker_data->description) {
722 free(sticker_data->description);
723 sticker_data->description = NULL;
726 if (sticker_data->date) {
727 free(sticker_data->date);
728 sticker_data->date = NULL;
734 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
738 STICKER_DAT_TYPE key;
739 sticker_info_db *sticker_info = NULL;
740 GVariant *value = NULL;
741 GVariantIter *info_iter = NULL;
742 GVariantIter *keyword_iter = NULL;
745 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
746 if (!info_iter || !keyword_iter) {
747 LOGD("failed to get iter");
748 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
752 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
755 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
759 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
761 case STICKER_DATA_TYPE_APP_ID:
762 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
764 case STICKER_DATA_TYPE_URI_TYPE:
765 sticker_info->type = g_variant_get_int32(value);
767 case STICKER_DATA_TYPE_URI:
768 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
770 case STICKER_DATA_TYPE_THUMBNAIL:
771 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
773 case STICKER_DATA_TYPE_DESCRIPTION:
774 sticker_info->description = (char *) g_variant_get_string(value, NULL);
776 case STICKER_DATA_TYPE_GROUP:
777 sticker_info->group = (char *) g_variant_get_string(value, NULL);
779 case STICKER_DATA_TYPE_DISP_TYPE:
780 sticker_info->display_type = g_variant_get_int32(value);
786 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
787 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
790 if (sticker_info->type == STICKER_URI_TYPE_LOCAL_PATH) {
791 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
792 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
793 if (!sticker_info->uri) {
794 LOGE("failed to copy sticker file");
795 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
799 LOGE("sticker file does not exist");
800 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
805 if (sticker_info->thumbnail) {
806 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
807 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
808 if (!sticker_info->thumbnail) {
809 LOGE("failed to copy sticker thumbnail");
810 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
814 LOGE("sticker thumbnail does not exist");
815 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
820 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
821 if (ret != STICKERD_SERVER_ERROR_NONE) {
822 LOGE("Failed to insert sticker info");
823 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
827 *reply_body = g_variant_new("(i)", record_id);
828 if (*reply_body == NULL) {
829 LOGE("Failed to create reply_body");
830 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
832 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
836 g_variant_unref(value);
839 g_variant_iter_free(info_iter);
842 g_variant_iter_free(keyword_iter);
852 static char* _get_string_from_object(JsonObject *object, const char *key)
854 if (json_object_has_member(object, key) == false)
857 const char *str = json_object_get_string_member(object, key);
864 static int _get_int_from_object(JsonObject *object, const char *key)
866 if (json_object_has_member(object, key) == false)
869 int type = json_object_get_int_member(object, key);
874 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
876 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
878 sticker_info_db *sticker_info = NULL;
880 char *json_path = NULL;
881 JsonParser* parser = NULL;
882 GError* err_msg = NULL;
883 GVariant *body = NULL;
884 char *cmd = "send_insert_result";
885 JsonArray *sticker_arr = NULL;
886 JsonArray *keyword_arr = NULL;
888 int keyword_arr_len = 0;
889 const char *keyword = NULL;
891 JsonNode *root = NULL;
892 JsonObject *root_obj = NULL;
894 *reply_body = g_variant_new("()");
895 if (*reply_body == NULL) {
896 LOGE("Failed to create reply_body");
897 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
900 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
902 if (!app_id || !json_path) {
903 LOGE("failed to get parameter");
904 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
907 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
909 parser = json_parser_new();
910 json_parser_load_from_file(parser, json_path, &err_msg);
912 LOGE("failed to load json file. error message: %s", err_msg->message);
913 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
917 root = json_parser_get_root(parser);
919 LOGE("failed to get root");
920 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
924 root_obj = json_node_get_object(root);
925 if (root_obj == NULL) {
926 LOGE("failed to get object");
927 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
931 sticker_arr = json_object_get_array_member(root_obj, "sticker");
932 if (sticker_arr == NULL) {
933 LOGE("failed to get array member");
934 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
938 arr_len = json_array_get_length(sticker_arr);
939 for (int i = 0; i < arr_len; i++) {
940 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
941 if (info_object != NULL) {
942 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
944 LOGE("Failed to alloc memory");
948 sticker_info->app_id = strdup(app_id);
949 if (!sticker_info->app_id)
952 sticker_info->type = _get_int_from_object(info_object, "type");
953 if (sticker_info->type < 1)
956 sticker_info->uri = _get_string_from_object(info_object, "uri");
957 if (!sticker_info->uri || sticker_info->uri[0] == '\0')
960 if (sticker_info->type == STICKER_URI_TYPE_LOCAL_PATH) {
961 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
962 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
963 if (!sticker_info->uri)
970 sticker_info->group = _get_string_from_object(info_object, "group");
971 if (!sticker_info->group)
974 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
975 if (sticker_info->thumbnail && sticker_info->thumbnail[0] != '\0') {
976 if (_check_file_exist(sticker_info->app_id, sticker_info->thumbnail) == 0) {
977 sticker_info->thumbnail = _convert_sticker_uri(sticker_info->thumbnail, sticker_info->app_id);
978 if (!sticker_info->thumbnail)
985 sticker_info->description = _get_string_from_object(info_object, "description");
987 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
989 keyword_arr = json_object_get_array_member(info_object, "keyword");
990 keyword_arr_len = json_array_get_length(keyword_arr);
991 if (keyword_arr_len < 1)
994 for (int j = 0; j < keyword_arr_len; j++) {
995 keyword = json_array_get_string_element(keyword_arr, j);
997 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup(keyword));
1000 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
1001 if (ret != STICKERD_SERVER_ERROR_NONE) {
1002 LOGE("Failed to insert sticker info");
1003 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1005 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
1008 _free_sticker_data(sticker_info);
1009 sticker_info = NULL;
1015 g_error_free(err_msg);
1017 g_object_unref(parser);
1019 body = g_variant_new("(i)", ret);
1021 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
1022 if (ret != STICKERD_SERVER_ERROR_NONE)
1023 LOGE("Failed to send insert result to client");
1026 g_variant_unref(body);
1031 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
1036 *reply_body = g_variant_new("()");
1037 if (*reply_body == NULL) {
1038 LOGE("Failed to create reply_body");
1039 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1042 g_variant_get(parameters, "(i)", &record_id);
1044 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1046 stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1048 ret = stickerd_db_delete_sticker_info(record_id);
1049 if (ret != STICKERD_SERVER_ERROR_NONE) {
1050 LOGE("Failed to delete sticker info");
1051 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1053 if (sticker_info && sticker_info->uri)
1054 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1058 _free_sticker_data(sticker_info);
1059 sticker_info = NULL;
1065 int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1070 *reply_body = g_variant_new("()");
1071 if (*reply_body == NULL) {
1072 LOGE("Failed to create reply_body");
1073 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1076 g_variant_get(parameters, "(&s)", &uri);
1078 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1080 stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1082 ret = stickerd_db_delete_sticker_info_by_uri(uri);
1083 if (ret != STICKERD_SERVER_ERROR_NONE) {
1084 LOGE("Failed to delete sticker info");
1085 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1087 if (sticker_info && sticker_info->uri)
1088 _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
1092 _free_sticker_data(sticker_info);
1093 sticker_info = NULL;
1099 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
1105 *reply_body = g_variant_new("()");
1106 if (*reply_body == NULL) {
1107 LOGE("Failed to create reply_body");
1108 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1111 g_variant_get(parameters, "(ii)", &record_id, &type);
1113 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
1114 if (ret != STICKERD_SERVER_ERROR_NONE) {
1115 LOGE("Failed to update sticker type");
1116 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1122 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
1130 *reply_body = g_variant_new("()");
1131 if (*reply_body == NULL) {
1132 LOGE("Failed to create reply_body");
1133 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1136 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
1138 if (type == STICKER_URI_TYPE_LOCAL_PATH) {
1139 if (_check_file_exist(app_id, uri) == 0) {
1140 uri = _convert_sticker_uri(uri, app_id);
1142 LOGE("failed to copy sticker file");
1143 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1146 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1150 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
1151 if (ret != STICKERD_SERVER_ERROR_NONE) {
1152 LOGE("Failed to update sticker uri");
1153 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1159 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
1166 *reply_body = g_variant_new("()");
1167 if (*reply_body == NULL) {
1168 LOGE("Failed to create reply_body");
1169 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1172 g_variant_get(parameters, "(i&s&s)", &record_id, &app_id, &thumbnail);
1174 if (_check_file_exist(app_id, thumbnail) == 0) {
1175 thumbnail = _convert_sticker_uri(thumbnail, app_id);
1177 LOGE("failed to copy sticker thumbnail");
1178 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1181 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1184 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
1185 if (ret != STICKERD_SERVER_ERROR_NONE) {
1186 LOGE("Failed to update sticker thumbnail");
1187 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1193 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
1199 *reply_body = g_variant_new("()");
1200 if (*reply_body == NULL) {
1201 LOGE("Failed to create reply_body");
1202 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1205 g_variant_get(parameters, "(i&s)", &record_id, &description);
1207 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
1208 if (ret != STICKERD_SERVER_ERROR_NONE) {
1209 LOGE("Failed to update sticker description");
1210 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1216 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
1222 *reply_body = g_variant_new("()");
1223 if (*reply_body == NULL) {
1224 LOGE("Failed to create reply_body");
1225 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1228 g_variant_get(parameters, "(i&s)", &record_id, &group);
1230 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
1231 if (ret != STICKERD_SERVER_ERROR_NONE) {
1232 LOGE("Failed to update sticker group");
1233 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1239 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
1243 GVariantIter *keyword_iter = NULL;
1244 char *keyword = NULL;
1245 GList *keyword_list = NULL;
1247 *reply_body = g_variant_new("()");
1248 if (*reply_body == NULL) {
1249 LOGE("Failed to create reply_body");
1250 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1253 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
1255 if (!keyword_iter) {
1256 LOGD("failed to get iter");
1257 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1260 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
1261 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
1264 g_variant_iter_free(keyword_iter);
1266 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
1267 if (ret != STICKERD_SERVER_ERROR_NONE) {
1268 LOGE("Failed to update sticker keyword");
1269 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1275 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
1279 GVariantBuilder *info_builder;
1280 GVariantBuilder *keyword_builder;
1282 g_variant_get(parameters, "(i)", &record_id);
1283 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1286 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1288 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1289 if (ret != STICKERD_SERVER_ERROR_NONE) {
1290 LOGE("Failed to get sticker info");
1291 _free_sticker_data(sticker_info);
1292 sticker_info = NULL;
1293 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1296 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1297 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1298 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1299 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1300 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1301 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1302 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1303 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1304 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1306 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1307 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1309 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1310 g_variant_builder_unref(info_builder);
1311 g_variant_builder_unref(keyword_builder);
1313 if (*reply_body == NULL) {
1314 LOGE("Failed to create reply_body");
1315 _free_sticker_data(sticker_info);
1316 sticker_info = NULL;
1317 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1320 _free_sticker_data(sticker_info);
1321 sticker_info = NULL;
1326 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1329 GVariantBuilder *builder = NULL;
1330 char *app_id = NULL;
1332 g_variant_get(parameters, "(&s)", &app_id);
1334 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1335 ret = stickerd_db_get_group_list(builder, app_id);
1336 if (ret != STICKERD_SERVER_ERROR_NONE) {
1337 LOGE("Failed to get sticker group list");
1338 g_variant_builder_unref(builder);
1339 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1342 *reply_body = g_variant_new("(a(s))", builder);
1343 g_variant_builder_unref(builder);
1345 if (*reply_body == NULL) {
1346 LOGE("Failed to create reply_body");
1347 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1353 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1356 GVariantBuilder *builder = NULL;
1357 char *app_id = NULL;
1359 g_variant_get(parameters, "(&s)", &app_id);
1361 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1362 ret = stickerd_db_get_keyword_list(builder, app_id);
1363 if (ret != STICKERD_SERVER_ERROR_NONE) {
1364 LOGE("Failed to get sticker keyword list");
1365 g_variant_builder_unref(builder);
1366 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1369 *reply_body = g_variant_new("(a(s))", builder);
1370 g_variant_builder_unref(builder);
1372 if (*reply_body == NULL) {
1373 LOGE("Failed to create reply_body");
1374 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1380 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1384 char *app_id = NULL;
1386 g_variant_get(parameters, "(&s)", &app_id);
1388 ret = stickerd_db_get_sticker_count(&count, app_id);
1389 if (ret != STICKERD_SERVER_ERROR_NONE) {
1390 LOGE("Failed to get sticker count");
1391 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1394 *reply_body = g_variant_new("(i)", count);
1395 if (*reply_body == NULL) {
1396 LOGE("Failed to create reply_body");
1397 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1403 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1406 LOGE("id doesn't exist");
1410 g_variant_builder_add(id_builder, "(i)", atoi(id));
1413 static int _set_reply_body(GList *list, GVariant **reply_body)
1415 int ret = STICKERD_SERVER_ERROR_NONE;
1416 GVariantBuilder *builder = NULL;
1418 builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1419 g_list_foreach(list, (GFunc) _set_id_builder, builder);
1421 *reply_body = g_variant_new("(a(i))", builder);
1422 if (*reply_body == NULL) {
1423 LOGE("Failed to create reply_body");
1424 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1428 g_variant_builder_unref(builder);
1433 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1437 char *app_id = NULL;
1438 GList *id_list = NULL;
1440 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1442 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1443 if (ret != STICKERD_SERVER_ERROR_NONE) {
1444 LOGE("Failed to get all sticker id");
1446 g_list_free_full(id_list, free);
1447 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1450 ret = _set_reply_body(id_list, reply_body);
1452 g_list_free_full(id_list, free);
1457 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1460 GList *id_list = NULL;
1461 char *app_id = 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 ret = _set_reply_body(id_list, reply_body);
1476 g_list_free_full(id_list, free);
1481 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1484 GList *id_list = NULL;
1485 char *app_id = NULL;
1486 int type, offset, count;
1488 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1490 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1491 if (ret != STICKERD_SERVER_ERROR_NONE) {
1492 LOGE("Failed to get all sticker id");
1494 g_list_free_full(id_list, free);
1495 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1498 ret = _set_reply_body(id_list, reply_body);
1500 g_list_free_full(id_list, free);
1505 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1508 GList *id_list = NULL;
1509 char *app_id = NULL;
1513 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1515 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1516 if (ret != STICKERD_SERVER_ERROR_NONE) {
1517 LOGE("Failed to get all sticker id");
1519 g_list_free_full(id_list, free);
1520 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1523 ret = _set_reply_body(id_list, reply_body);
1525 g_list_free_full(id_list, free);
1530 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1533 GList *id_list = NULL;
1534 char *app_id = NULL;
1535 char *keyword = NULL;
1538 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1540 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1541 if (ret != STICKERD_SERVER_ERROR_NONE) {
1542 LOGE("Failed to get all sticker id");
1544 g_list_free_full(id_list, free);
1545 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1548 ret = _set_reply_body(id_list, reply_body);
1550 g_list_free_full(id_list, free);
1555 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1558 GList *id_list = NULL;
1559 char *app_id = NULL;
1560 int type, offset, count;
1562 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1564 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1565 if (ret != STICKERD_SERVER_ERROR_NONE) {
1566 LOGE("Failed to get all sticker id");
1568 g_list_free_full(id_list, free);
1569 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1572 ret = _set_reply_body(id_list, reply_body);
1574 g_list_free_full(id_list, free);
1579 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1582 GVariantBuilder *builder = NULL;
1583 char *app_id = NULL;
1586 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1588 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1589 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1590 if (ret != STICKERD_SERVER_ERROR_NONE) {
1591 LOGE("Failed to get sticker group list");
1592 g_variant_builder_unref(builder);
1593 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1596 *reply_body = g_variant_new("(a(s))", builder);
1597 g_variant_builder_unref(builder);
1599 if (*reply_body == NULL) {
1600 LOGE("Failed to create reply_body");
1601 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1607 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1613 *reply_body = g_variant_new("()");
1614 if (*reply_body == NULL) {
1615 LOGE("Failed to create reply_body");
1616 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1619 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1621 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1622 if (ret != STICKERD_SERVER_ERROR_NONE) {
1623 LOGE("Failed to update sticker disp_type");
1624 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1630 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1636 g_variant_get(parameters, "(&s)", &uri);
1638 ret = stickerd_db_check_file_exists(&result, uri);
1639 if (ret != STICKERD_SERVER_ERROR_NONE) {
1640 LOGE("Failed to get sticker count");
1641 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1644 *reply_body = g_variant_new("(i)", result);
1645 if (*reply_body == NULL) {
1646 LOGE("Failed to create reply_body");
1647 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1653 int stickerd_insert_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1658 *reply_body = g_variant_new("()");
1659 if (*reply_body == NULL) {
1660 LOGE("Failed to create reply_body");
1661 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1664 g_variant_get(parameters, "(i)", &record_id);
1666 ret = stickerd_db_insert_recent_sticker_info(record_id);
1667 if (ret != STICKERD_SERVER_ERROR_NONE) {
1668 LOGE("Failed to insert recent sticker info");
1669 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1675 int stickerd_get_recent_sticker_info(GVariant *parameters, GVariant **reply_body)
1679 GList *id_list = NULL;
1681 g_variant_get(parameters, "(i)", &count);
1683 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_RECENT_HISTORY, &id_list, NULL, NULL, 0, count);
1684 if (ret != STICKERD_SERVER_ERROR_NONE) {
1685 LOGE("Failed to get recent sticker id");
1687 g_list_free_full(id_list, free);
1688 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1691 ret = _set_reply_body(id_list, reply_body);
1693 g_list_free_full(id_list, free);
1698 int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
1700 int ret = STICKERD_SERVER_ERROR_NONE;
1703 *reply_body = g_variant_new("()");
1704 if (*reply_body == NULL) {
1705 LOGE("Failed to create reply_body");
1706 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1709 g_variant_get(parameters, "(i)", &record_id);
1711 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1713 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1714 if (ret == STICKERD_SERVER_ERROR_NONE)
1715 _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
1717 _free_sticker_data(sticker_info);
1718 sticker_info = NULL;
1724 int stickerd_get_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body)
1728 GVariantBuilder *info_builder;
1729 GVariantBuilder *keyword_builder;
1731 g_variant_get(parameters, "(&s)", &uri);
1733 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1736 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1738 ret = stickerd_db_get_sticker_info_by_uri(uri, sticker_info);
1739 if (ret != STICKERD_SERVER_ERROR_NONE) {
1740 LOGE("Failed to get sticker info");
1741 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1745 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1746 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(sticker_info->record_id));
1747 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1748 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1749 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1750 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1751 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1752 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1753 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1754 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1756 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1757 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1759 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1760 g_variant_builder_unref(info_builder);
1761 g_variant_builder_unref(keyword_builder);
1763 if (*reply_body == NULL) {
1764 LOGE("Failed to create reply_body");
1765 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1770 _free_sticker_data(sticker_info);
1771 sticker_info = NULL;
1777 int stickerd_check_group_exists(GVariant *parameters, GVariant **reply_body)
1781 char *app_id = NULL;
1784 g_variant_get(parameters, "(&s&s)", &app_id, &group);
1786 ret = stickerd_db_check_group_exists(&result, app_id, group);
1787 if (ret != STICKERD_SERVER_ERROR_NONE) {
1788 LOGE("Failed to check group exists");
1789 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1792 *reply_body = g_variant_new("(i)", result);
1793 if (*reply_body == NULL) {
1794 LOGE("Failed to create reply_body");
1795 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1801 int stickerd_set_group_image(GVariant *parameters, GVariant **reply_body)
1805 char *app_id = NULL;
1809 *reply_body = g_variant_new("()");
1810 if (*reply_body == NULL) {
1811 LOGE("Failed to create reply_body");
1812 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1815 g_variant_get(parameters, "(&s&si&s)", &app_id, &group, &type, &uri);
1817 if (type == STICKER_URI_TYPE_LOCAL_PATH) {
1818 if (_check_file_exist(app_id, uri) == 0) {
1819 uri = _convert_sticker_uri(uri, app_id);
1821 LOGE("failed to copy sticker file");
1822 return STICKERD_SERVER_ERROR_FILE_EXISTS;
1825 LOGE("sticker file does not exist");
1826 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
1830 ret = stickerd_db_set_group_image(app_id, group, type, uri);
1831 if (ret != STICKERD_SERVER_ERROR_NONE) {
1832 LOGE("Failed to set group image");
1833 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1839 int stickerd_get_group_image_list(GVariant *parameters, GVariant **reply_body)
1842 GVariantBuilder *builder = NULL;
1843 char *app_id = NULL;
1845 g_variant_get(parameters, "(&s)", &app_id);
1847 builder = g_variant_builder_new(G_VARIANT_TYPE("a(sis)"));
1848 ret = stickerd_db_get_group_image_list(builder, app_id);
1849 if (ret != STICKERD_SERVER_ERROR_NONE) {
1850 LOGE("Failed to get sticker group image list");
1851 g_variant_builder_unref(builder);
1852 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1855 *reply_body = g_variant_new("(a(sis))", builder);
1856 g_variant_builder_unref(builder);
1858 if (*reply_body == NULL) {
1859 LOGE("Failed to create reply_body");
1860 return STICKERD_SERVER_ERROR_OPERATION_FAILED;