2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <tzplatform_config.h>
21 #include <gio/gunixfdlist.h>
23 #include <json-glib/json-glib.h>
24 #include <package_manager.h>
26 #include <sys/types.h>
31 #include "stickerd_dbus.h"
32 #include "stickerd_data_manager.h"
33 #include "stickerd_db_manager.h"
34 #include "sticker_defs.h"
35 #include "stickerd_error.h"
40 #define LOG_TAG "STICKERD_DATA_MANAGER"
42 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
43 #define MAX_ERROR_BUFFER 256
45 static GHashTable *_monitoring_hash = NULL;
46 static char error_buffer[MAX_ERROR_BUFFER];
47 extern GMainLoop *main_loop;
49 static void _check_watcher_exist()
51 if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
52 LOGD("Terminate sticker daemon");
53 g_hash_table_destroy(_monitoring_hash);
54 _monitoring_hash = NULL;
55 g_main_loop_quit(main_loop);
59 static void _on_name_appeared(GDBusConnection *connection,
61 const gchar *name_owner,
64 LOGD("name: %s", name);
67 static void _on_name_vanished(GDBusConnection *connection,
71 monitoring_info_s *info = (monitoring_info_s *)user_data;
74 if (_monitoring_hash != NULL && g_hash_table_lookup(_monitoring_hash, GUINT_TO_POINTER(info->watcher_id)) != NULL) {
75 LOGD("name: %s", name);
76 g_bus_unwatch_name(info->watcher_id);
77 delete_monitoring_list(&_monitoring_hash, info->bus_name, info->watcher_id);
86 _check_watcher_exist();
89 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
90 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
93 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
95 if (_monitoring_hash == NULL)
96 _monitoring_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
98 GVariant *reply_body = NULL;
99 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
101 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
102 ret = stickerd_server_register(parameters, &reply_body, sender,
103 _on_name_appeared, _on_name_vanished, &_monitoring_hash);
104 } else if (g_strcmp0(method_name, "sticker_service_unregister") == 0) {
105 ret = stickerd_server_unregister(parameters, &reply_body, sender, &_monitoring_hash);
106 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
107 ret = stickerd_insert_sticker_info(parameters, &reply_body);
108 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
109 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
110 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
111 ret = stickerd_del_sticker_info(parameters, &reply_body);
112 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
113 ret = stickerd_update_sticker_type(parameters, &reply_body);
114 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
115 ret = stickerd_update_sticker_uri(parameters, &reply_body);
116 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
117 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
118 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
119 ret = stickerd_update_sticker_description(parameters, &reply_body);
120 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
121 ret = stickerd_update_sticker_group(parameters, &reply_body);
122 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
123 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
124 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
125 ret = stickerd_get_sticker_info(parameters, &reply_body);
126 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
127 ret = stickerd_get_group_list(parameters, &reply_body);
128 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
129 ret = stickerd_get_keyword_list(parameters, &reply_body);
130 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
131 ret = stickerd_get_sticker_count(parameters, &reply_body);
132 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
133 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
134 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
135 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
136 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
137 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
138 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
139 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
140 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
141 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
144 if (ret == STICKERD_SERVER_ERROR_NONE) {
145 LOGD("method_call successful, method_name : %s", method_name);
146 g_dbus_method_invocation_return_value(invocation, reply_body);
148 LOGE("method_call failed, method_name : %s", method_name);
149 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
152 _check_watcher_exist();
155 static const GDBusInterfaceVTable _sticker_interface_vtable = {
156 _stickerd_client_dbus_method_call_handler,
161 int stickerd_register_dbus_interface(void)
163 static gchar introspection_xml[] =
165 " <interface name='org.tizen.sticker_service'>"
166 " <method name='sticker_service_register'>"
167 " <arg type='i' name='watcher_id' direction='out'/>"
170 " <method name='sticker_service_unregister'>"
171 " <arg type='i' name='watcher_id' direction='in'/>"
174 " <method name='insert_sticker_info'>"
175 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
176 " <arg type='a(s)' name='keyword_list' direction='in'/>"
177 " <arg type='i' name='record_id' direction='out'/>"
180 " <method name='update_sticker_info_by_json'>"
181 " <arg type='s' name='app_id' direction='in'/>"
182 " <arg type='s' name='json_path' direction='in'/>"
185 " <method name='delete_sticker_info'>"
186 " <arg type='i' name='record_id' direction='in'/>"
189 " <method name='update_sticker_type'>"
190 " <arg type='i' name='record_id' direction='in'/>"
191 " <arg type='i' name='type' direction='in'/>"
194 " <method name='update_sticker_uri'>"
195 " <arg type='i' name='record_id' direction='in'/>"
196 " <arg type='s' name='app_id' direction='in'/>"
197 " <arg type='i' name='type' direction='in'/>"
198 " <arg type='s' name='uri' direction='in'/>"
201 " <method name='update_sticker_thumbnail'>"
202 " <arg type='i' name='record_id' direction='in'/>"
203 " <arg type='s' name='thumbnail' direction='in'/>"
206 " <method name='update_sticker_description'>"
207 " <arg type='i' name='record_id' direction='in'/>"
208 " <arg type='s' name='description' direction='in'/>"
211 " <method name='update_sticker_group'>"
212 " <arg type='i' name='record_id' direction='in'/>"
213 " <arg type='s' name='group' direction='in'/>"
216 " <method name='update_sticker_keyword'>"
217 " <arg type='i' name='record_id' direction='in'/>"
218 " <arg type='a(s)' name='keyword' direction='in'/>"
221 " <method name='get_sticker_info'>"
222 " <arg type='i' name='record_id' direction='in'/>"
223 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
224 " <arg type='a(s)' name='keyword_list' direction='out'/>"
227 " <method name='get_group_list'>"
228 " <arg type='a(s)' name='group_list' direction='out'/>"
231 " <method name='get_keyword_list'>"
232 " <arg type='a(s)' name='keyword_list' direction='out'/>"
235 " <method name='get_sticker_count'>"
236 " <arg type='s' name='app_id' direction='in'/>"
237 " <arg type='i' name='count' direction='out'/>"
240 " <method name='get_all_sticker_info'>"
241 " <arg type='i' name='offset' direction='in'/>"
242 " <arg type='i' name='count' direction='in'/>"
243 " <arg type='a(i)' name='id_list' direction='out'/>"
246 " <method name='get_sticker_info_by_appid'>"
247 " <arg type='s' name='app_id' direction='in'/>"
248 " <arg type='i' name='offset' direction='in'/>"
249 " <arg type='i' name='count' direction='in'/>"
250 " <arg type='a(i)' name='id_list' direction='out'/>"
253 " <method name='get_sticker_info_by_type'>"
254 " <arg type='i' name='type' direction='in'/>"
255 " <arg type='i' name='offset' direction='in'/>"
256 " <arg type='i' name='count' direction='in'/>"
257 " <arg type='a(i)' name='id_list' direction='out'/>"
260 " <method name='get_sticker_info_by_group'>"
261 " <arg type='s' name='group' direction='in'/>"
262 " <arg type='i' name='offset' direction='in'/>"
263 " <arg type='i' name='count' direction='in'/>"
264 " <arg type='a(i)' name='id_list' direction='out'/>"
267 " <method name='get_sticker_info_by_keyword'>"
268 " <arg type='s' name='keyword' direction='in'/>"
269 " <arg type='i' name='offset' direction='in'/>"
270 " <arg type='i' name='count' direction='in'/>"
271 " <arg type='a(i)' name='id_list' direction='out'/>"
276 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
279 int stickerd_dbus_init(void)
283 ret = stickerd_register_dbus_interface();
284 if (ret != STICKERD_SERVER_ERROR_NONE) {
285 LOGE("Failed to register dbus interface : %d", ret);
286 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
289 return STICKERD_SERVER_ERROR_NONE;
292 static int _check_file_exist(const char *app_id, const char *path)
295 package_info_h package_info = NULL;
296 char *app_path = NULL;
297 char *file_path = NULL;
299 if (access(path, F_OK) == 0) {
304 ret = package_info_create(app_id, &package_info);
305 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
306 LOGE("failed to create package_info. ret: %d", ret);
311 ret = package_info_get_root_path(package_info, &app_path);
312 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
313 LOGE("failed to create package_info. ret: %d", ret);
318 int path_len = strlen(app_path) + strlen(path) + 2;
319 file_path = (char *)calloc(path_len, sizeof(char));
321 LOGE("Failed to alloc memory");
327 snprintf(file_path, path_len, "%s%s",app_path, path);
329 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
331 if (access(file_path, F_OK) != 0) {
332 LOGE("%s does not exist", file_path);
339 package_info_destroy(package_info);
354 static int _mkdirs(const char *path, mode_t mode)
357 char prev_path[2048];
358 const char *tmp = path;
360 if (!path || strlen(path) > 2048)
363 memset(prev_path, '\0', 2048);
364 while ((tmp = strchr(tmp, '/')) != NULL) {
371 strncpy(prev_path, path, len);
372 prev_path[len] = 0x00;
374 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
375 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
376 LOGE("directory create error : %s", error_buffer);
381 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
382 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
383 LOGE("directory create error : %s", error_buffer);
390 static int _file_copy(const char *src, const char *dest)
393 int fd = -1, n_fd = -1;
398 memset(buf, '\0', 4096);
399 fd = open(src, O_RDONLY);
401 LOGE("Failed to open file (%s) for reading", src);
404 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
406 LOGE("Failed to open file (%s) for writing", dest);
409 if (fd == -1 || n_fd == -1) {
415 while((size = read(fd, buf, 4096))) {
425 while(write(n_fd, buf, size) == -1) {
446 static char* _convert_sticker_uri(const char *uri, const char *appid)
449 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
450 char * new_path = (char *)calloc(len, sizeof(char));
451 if (new_path == NULL) {
452 LOGE("Failed to alloc memory");
457 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
459 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
461 if (access(new_path, F_OK) == 0) {
462 LOGE("sticker file already exists");
467 ret = _mkdirs(new_path, 0755);
469 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
470 LOGE("directory create error : %s", error_buffer);
474 ret = _file_copy(uri, new_path);
476 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
477 LOGE("failed to copy sticker file : %s", error_buffer);
490 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
494 STICKER_DAT_TYPE key;
495 sticker_info_db *sticker_info = NULL;
496 GVariant *value = NULL;
497 GVariantIter *info_iter = NULL;
498 GVariantIter *keyword_iter = NULL;
501 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
502 if (!info_iter || !keyword_iter) {
503 LOGD("failed to get iter");
504 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
508 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
511 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
515 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
517 case STICKER_DATA_TYPE_APP_ID:
518 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
520 case STICKER_DATA_TYPE_URI_TYPE:
521 sticker_info->type = g_variant_get_int32(value);
523 case STICKER_DATA_TYPE_URI:
524 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
526 case STICKER_DATA_TYPE_THUMBNAIL:
527 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
529 case STICKER_DATA_TYPE_DESCRIPTION:
530 sticker_info->description = (char *) g_variant_get_string(value, NULL);
532 case STICKER_DATA_TYPE_GROUP:
533 sticker_info->group = (char *) g_variant_get_string(value, NULL);
540 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
541 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
544 if (sticker_info->type == 1) {
545 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
546 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
547 if (!sticker_info->uri) {
548 LOGE("failed to copy sticker file");
549 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
553 LOGE("sticker file does not exist");
554 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
559 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
560 if (ret != STICKERD_SERVER_ERROR_NONE) {
561 LOGE("Failed to insert sticker info");
562 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
566 *reply_body = g_variant_new("(i)", record_id);
567 if (*reply_body == NULL) {
568 LOGE("Failed to create reply_body");
569 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
574 g_variant_unref(value);
577 g_variant_iter_free(info_iter);
580 g_variant_iter_free(keyword_iter);
590 static char* _get_string_from_object(JsonObject *object, const char *key)
592 if (json_object_has_member(object, key) == false)
595 const char *str = json_object_get_string_member(object, key);
602 static int _get_int_from_object(JsonObject *object, const char *key)
604 if (json_object_has_member(object, key) == false)
607 int type = json_object_get_int_member(object, key);
612 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
614 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
616 sticker_info_db *sticker_info = NULL;
618 char *json_path = NULL;
619 JsonParser* parser = NULL;
620 GError* err_msg = NULL;
621 GVariant *body = NULL;
622 char *cmd = "send_insert_result";
624 *reply_body = g_variant_new("()");
625 if (*reply_body == NULL) {
626 LOGE("Failed to create reply_body");
627 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
630 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
632 if (!app_id || !json_path) {
633 LOGE("failed to get parameter");
634 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
637 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
639 parser = json_parser_new();
640 json_parser_load_from_file(parser, json_path, &err_msg);
642 LOGE("failed to load json file. error message: %s", err_msg->message);
643 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
647 JsonNode *root = json_parser_get_root(parser);
649 LOGE("failed to get root");
650 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
654 JsonObject *root_obj = json_node_get_object(root);
655 if (root_obj == NULL) {
656 LOGE("failed to get object");
657 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
661 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
662 if (sticker_arr == NULL) {
663 LOGE("failed to get array member");
664 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
668 int arr_len = json_array_get_length(sticker_arr);
669 for (int i = 0; i < arr_len; i++) {
670 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
671 if (info_object != NULL) {
672 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
674 LOGE("Failed to alloc memory");
678 sticker_info->app_id = app_id;
679 sticker_info->type = _get_int_from_object(info_object, "type");
680 if (sticker_info->type < 1)
683 sticker_info->uri = _get_string_from_object(info_object, "uri");
684 if (!sticker_info->uri)
687 if (sticker_info->type == 1) {
688 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
689 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
690 if (!sticker_info->uri)
695 sticker_info->group = _get_string_from_object(info_object, "group");
696 if (!sticker_info->group)
699 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
700 if (!sticker_info->thumbnail)
703 sticker_info->description = _get_string_from_object(info_object, "description");
705 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
706 int keyword_arr_len = json_array_get_length(keyword_arr);
707 if (keyword_arr_len < 1)
710 for (int j = 0; j < keyword_arr_len; j++) {
711 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
714 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
715 if (ret != STICKERD_SERVER_ERROR_NONE) {
716 LOGE("Failed to insert sticker info");
717 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
728 g_error_free(err_msg);
730 g_object_unref(parser);
732 body = g_variant_new("(i)", ret);
734 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
735 if (ret != STICKERD_SERVER_ERROR_NONE)
736 LOGE("Failed to send insert result to client");
739 g_variant_unref(body);
744 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
749 *reply_body = g_variant_new("()");
750 if (*reply_body == NULL) {
751 LOGE("Failed to create reply_body");
752 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
755 g_variant_get(parameters, "(i)", &record_id);
757 ret = stickerd_db_delete_sticker_info(record_id);
758 if (ret != STICKERD_SERVER_ERROR_NONE) {
759 LOGE("Failed to delete sticker info");
760 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
766 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
772 *reply_body = g_variant_new("()");
773 if (*reply_body == NULL) {
774 LOGE("Failed to create reply_body");
775 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
778 g_variant_get(parameters, "(ii)", &record_id, &type);
780 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
781 if (ret != STICKERD_SERVER_ERROR_NONE) {
782 LOGE("Failed to update sticker type");
783 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
789 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
797 *reply_body = g_variant_new("()");
798 if (*reply_body == NULL) {
799 LOGE("Failed to create reply_body");
800 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
803 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
806 if (_check_file_exist(app_id, uri) == 0) {
807 uri = _convert_sticker_uri(uri, app_id);
809 LOGE("failed to copy sticker file");
810 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
815 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
816 if (ret != STICKERD_SERVER_ERROR_NONE) {
817 LOGE("Failed to update sticker uri");
818 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
824 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
830 *reply_body = g_variant_new("()");
831 if (*reply_body == NULL) {
832 LOGE("Failed to create reply_body");
833 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
836 g_variant_get(parameters, "(i&s)", &record_id, &thumbnail);
838 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
839 if (ret != STICKERD_SERVER_ERROR_NONE) {
840 LOGE("Failed to update sticker thumbnail");
841 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
847 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
853 *reply_body = g_variant_new("()");
854 if (*reply_body == NULL) {
855 LOGE("Failed to create reply_body");
856 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
859 g_variant_get(parameters, "(i&s)", &record_id, &description);
861 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
862 if (ret != STICKERD_SERVER_ERROR_NONE) {
863 LOGE("Failed to update sticker description");
864 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
870 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
876 *reply_body = g_variant_new("()");
877 if (*reply_body == NULL) {
878 LOGE("Failed to create reply_body");
879 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
882 g_variant_get(parameters, "(i&s)", &record_id, &group);
884 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
885 if (ret != STICKERD_SERVER_ERROR_NONE) {
886 LOGE("Failed to update sticker group");
887 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
893 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
897 GVariantIter *keyword_iter = NULL;
898 char *keyword = NULL;
899 GList *keyword_list = NULL;
901 *reply_body = g_variant_new("()");
902 if (*reply_body == NULL) {
903 LOGE("Failed to create reply_body");
904 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
907 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
910 LOGD("failed to get iter");
911 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
914 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
915 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
918 g_variant_iter_free(keyword_iter);
920 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
921 if (ret != STICKERD_SERVER_ERROR_NONE) {
922 LOGE("Failed to update sticker keyword");
923 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
929 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
932 LOGE("keyword doesn't exist");
936 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
939 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
943 GVariantBuilder *info_builder;
944 GVariantBuilder *keyword_builder;
946 g_variant_get(parameters, "(i)", &record_id);
947 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
950 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
952 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
953 if (ret != STICKERD_SERVER_ERROR_NONE) {
954 LOGE("Failed to get sticker info");
956 if (sticker_info->app_id)
957 free(sticker_info->app_id);
959 if (sticker_info->uri)
960 free(sticker_info->uri);
962 if (sticker_info->thumbnail)
963 free(sticker_info->thumbnail);
965 if (sticker_info->keyword)
966 free(sticker_info->keyword);
968 if (sticker_info->group)
969 free(sticker_info->group);
971 if (sticker_info->description)
972 free(sticker_info->description);
974 if (sticker_info->date)
975 free(sticker_info->date);
979 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
982 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
983 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
984 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
985 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
986 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
987 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
988 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
989 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
991 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
992 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
994 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
995 g_variant_builder_unref(info_builder);
996 g_variant_builder_unref(keyword_builder);
998 if (*reply_body == NULL) {
999 LOGE("Failed to create reply_body");
1001 sticker_info = NULL;
1002 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1006 sticker_info = NULL;
1010 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1013 GVariantBuilder *builder = NULL;
1015 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1016 ret = stickerd_db_get_group_list(builder);
1018 if (ret != STICKERD_SERVER_ERROR_NONE) {
1019 LOGE("Failed to get sticker group list");
1020 g_variant_builder_unref(builder);
1021 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1024 *reply_body = g_variant_new("(a(s))", builder);
1025 g_variant_builder_unref(builder);
1027 if (*reply_body == NULL) {
1028 LOGE("Failed to create reply_body");
1029 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1035 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1038 GVariantBuilder *builder = NULL;
1040 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1041 ret = stickerd_db_get_keyword_list(builder);
1042 if (ret != STICKERD_SERVER_ERROR_NONE) {
1043 LOGE("Failed to get sticker keyword list");
1044 g_variant_builder_unref(builder);
1045 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1048 *reply_body = g_variant_new("(a(s))", builder);
1049 g_variant_builder_unref(builder);
1051 if (*reply_body == NULL) {
1052 LOGE("Failed to create reply_body");
1053 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1059 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1063 char *app_id = NULL;
1065 g_variant_get(parameters, "(&s)", &app_id);
1067 ret = stickerd_db_get_sticker_count(&count, app_id);
1068 if (ret != STICKERD_SERVER_ERROR_NONE) {
1069 LOGE("Failed to get sticker count");
1070 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1073 *reply_body = g_variant_new("(i)", count);
1074 if (*reply_body == NULL) {
1075 LOGE("Failed to create reply_body");
1076 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1083 // Send the sticker information by asynchronous communication.
1084 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1089 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1092 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1094 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1095 if (ret != STICKERD_SERVER_ERROR_NONE) {
1096 LOGE("Failed to get sticker info");
1098 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1102 case STICKER_DB_STICKER_ALL:
1103 cmd = "send_all_sticker_info";
1105 case STICKER_DB_STICKER_APPID:
1106 cmd = "send_sticker_info_by_appid";
1108 case STICKER_DB_STICKER_TYPE:
1109 cmd = "send_sticker_info_by_type";
1111 case STICKER_DB_STICKER_GROUP:
1112 cmd = "send_sticker_info_by_group";
1114 case STICKER_DB_STICKER_KEYWORD:
1115 cmd = "send_sticker_info_by_keyword";
1122 GVariantBuilder *info_builder;
1123 GVariantBuilder *keyword_builder;
1125 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1126 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1127 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1128 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1129 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1130 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1131 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1132 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1133 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1135 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1136 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1138 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1139 g_variant_builder_unref(info_builder);
1140 g_variant_builder_unref(keyword_builder);
1142 ret = stickerd_send_dbus_message(body, sender, cmd);
1143 if (ret != STICKERD_SERVER_ERROR_NONE) {
1144 LOGE("Failed to send sticker info to client");
1146 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1154 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1157 LOGE("id doesn't exist");
1161 g_variant_builder_add(id_builder, "(i)", atoi(id));
1164 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1168 GList *id_list = NULL;
1169 GVariantBuilder *id_builder = NULL;
1171 g_variant_get(parameters, "(ii)", &offset, &count);
1173 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, offset, count);
1174 if (ret != STICKERD_SERVER_ERROR_NONE) {
1175 LOGE("Failed to get all sticker id");
1177 g_list_free_full(id_list, free);
1178 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1181 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1182 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1184 *reply_body = g_variant_new("(a(i))", id_builder);
1185 if (*reply_body == NULL) {
1186 LOGE("Failed to create reply_body");
1187 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1191 g_list_free_full(id_list, free);
1194 g_variant_builder_unref(id_builder);
1199 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1202 GList *id_list = NULL;
1203 char *app_id = NULL;
1205 GVariantBuilder *id_builder = NULL;
1207 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1209 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, (void *)app_id, offset, count);
1210 if (ret != STICKERD_SERVER_ERROR_NONE) {
1211 LOGE("Failed to get all sticker id");
1213 g_list_free_full(id_list, free);
1214 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1217 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1218 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1220 *reply_body = g_variant_new("(a(i))", id_builder);
1221 if (*reply_body == NULL) {
1222 LOGE("Failed to create reply_body");
1223 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1227 g_list_free_full(id_list, free);
1230 g_variant_builder_unref(id_builder);
1235 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1238 GList *id_list = NULL;
1239 int type, offset, count;
1240 GVariantBuilder *id_builder = NULL;
1242 g_variant_get(parameters, "(iii)", &type, &offset, &count);
1244 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, offset, count);
1245 if (ret != STICKERD_SERVER_ERROR_NONE) {
1246 LOGE("Failed to get all sticker id");
1248 g_list_free_full(id_list, free);
1249 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1252 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1253 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1255 *reply_body = g_variant_new("(a(i))", id_builder);
1256 if (*reply_body == NULL) {
1257 LOGE("Failed to create reply_body");
1258 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1262 g_list_free_full(id_list, free);
1265 g_variant_builder_unref(id_builder);
1270 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1273 GList *id_list = NULL;
1276 GVariantBuilder *id_builder = NULL;
1278 g_variant_get(parameters, "(&sii)", &group, &offset, &count);
1280 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, offset, count);
1281 if (ret != STICKERD_SERVER_ERROR_NONE) {
1282 LOGE("Failed to get all sticker id");
1284 g_list_free_full(id_list, free);
1285 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1288 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1289 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1291 *reply_body = g_variant_new("(a(i))", id_builder);
1292 if (*reply_body == NULL) {
1293 LOGE("Failed to create reply_body");
1294 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1298 g_list_free_full(id_list, free);
1301 g_variant_builder_unref(id_builder);
1306 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1309 GList *id_list = NULL;
1310 char *keyword = NULL;
1312 GVariantBuilder *id_builder = NULL;
1314 g_variant_get(parameters, "(&sii)", &keyword, &offset, &count);
1316 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, offset, count);
1317 if (ret != STICKERD_SERVER_ERROR_NONE) {
1318 LOGE("Failed to get all sticker id");
1320 g_list_free_full(id_list, free);
1321 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1324 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1325 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1327 *reply_body = g_variant_new("(a(i))", id_builder);
1328 if (*reply_body == NULL) {
1329 LOGE("Failed to create reply_body");
1330 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1334 g_list_free_full(id_list, free);
1337 g_variant_builder_unref(id_builder);