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);
142 } else if (g_strcmp0(method_name, "get_sticker_info_by_disp_type") == 0) {
143 ret = stickerd_get_sticker_info_by_display_type(parameters, &reply_body);
144 } else if (g_strcmp0(method_name, "get_group_list_by_disp_type") == 0) {
145 ret = stickerd_get_group_list_by_disp_type(parameters, &reply_body);
146 } else if (g_strcmp0(method_name, "update_sticker_disp_type") == 0) {
147 ret = stickerd_update_sticker_disp_type(parameters, &reply_body);
150 if (ret == STICKERD_SERVER_ERROR_NONE) {
151 LOGD("method_call successful, method_name : %s", method_name);
152 g_dbus_method_invocation_return_value(invocation, reply_body);
154 LOGE("method_call failed, method_name : %s", method_name);
155 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
158 _check_watcher_exist();
161 static const GDBusInterfaceVTable _sticker_interface_vtable = {
162 _stickerd_client_dbus_method_call_handler,
167 int stickerd_register_dbus_interface(void)
169 static gchar introspection_xml[] =
171 " <interface name='org.tizen.sticker_service'>"
172 " <method name='sticker_service_register'>"
173 " <arg type='i' name='watcher_id' direction='out'/>"
176 " <method name='sticker_service_unregister'>"
177 " <arg type='i' name='watcher_id' direction='in'/>"
180 " <method name='insert_sticker_info'>"
181 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
182 " <arg type='a(s)' name='keyword_list' direction='in'/>"
183 " <arg type='i' name='record_id' direction='out'/>"
186 " <method name='update_sticker_info_by_json'>"
187 " <arg type='s' name='app_id' direction='in'/>"
188 " <arg type='s' name='json_path' direction='in'/>"
191 " <method name='delete_sticker_info'>"
192 " <arg type='i' name='record_id' direction='in'/>"
195 " <method name='update_sticker_type'>"
196 " <arg type='i' name='record_id' direction='in'/>"
197 " <arg type='i' name='type' direction='in'/>"
200 " <method name='update_sticker_uri'>"
201 " <arg type='i' name='record_id' direction='in'/>"
202 " <arg type='s' name='app_id' direction='in'/>"
203 " <arg type='i' name='type' direction='in'/>"
204 " <arg type='s' name='uri' direction='in'/>"
207 " <method name='update_sticker_thumbnail'>"
208 " <arg type='i' name='record_id' direction='in'/>"
209 " <arg type='s' name='thumbnail' direction='in'/>"
212 " <method name='update_sticker_description'>"
213 " <arg type='i' name='record_id' direction='in'/>"
214 " <arg type='s' name='description' direction='in'/>"
217 " <method name='update_sticker_group'>"
218 " <arg type='i' name='record_id' direction='in'/>"
219 " <arg type='s' name='group' direction='in'/>"
222 " <method name='update_sticker_keyword'>"
223 " <arg type='i' name='record_id' direction='in'/>"
224 " <arg type='a(s)' name='keyword' direction='in'/>"
227 " <method name='get_sticker_info'>"
228 " <arg type='i' name='record_id' direction='in'/>"
229 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
230 " <arg type='a(s)' name='keyword_list' direction='out'/>"
233 " <method name='get_group_list'>"
234 " <arg type='s' name='app_id' direction='in'/>"
235 " <arg type='a(s)' name='group_list' direction='out'/>"
238 " <method name='get_keyword_list'>"
239 " <arg type='s' name='app_id' direction='in'/>"
240 " <arg type='a(s)' name='keyword_list' direction='out'/>"
243 " <method name='get_sticker_count'>"
244 " <arg type='s' name='app_id' direction='in'/>"
245 " <arg type='i' name='count' direction='out'/>"
248 " <method name='get_all_sticker_info'>"
249 " <arg type='s' name='app_id' direction='in'/>"
250 " <arg type='i' name='offset' direction='in'/>"
251 " <arg type='i' name='count' direction='in'/>"
252 " <arg type='a(i)' name='id_list' direction='out'/>"
255 " <method name='get_sticker_info_by_appid'>"
256 " <arg type='s' name='app_id' direction='in'/>"
257 " <arg type='i' name='offset' direction='in'/>"
258 " <arg type='i' name='count' direction='in'/>"
259 " <arg type='a(i)' name='id_list' direction='out'/>"
262 " <method name='get_sticker_info_by_type'>"
263 " <arg type='s' name='app_id' direction='in'/>"
264 " <arg type='i' name='type' direction='in'/>"
265 " <arg type='i' name='offset' direction='in'/>"
266 " <arg type='i' name='count' direction='in'/>"
267 " <arg type='a(i)' name='id_list' direction='out'/>"
270 " <method name='get_sticker_info_by_group'>"
271 " <arg type='s' name='app_id' direction='in'/>"
272 " <arg type='s' name='group' direction='in'/>"
273 " <arg type='i' name='offset' direction='in'/>"
274 " <arg type='i' name='count' direction='in'/>"
275 " <arg type='a(i)' name='id_list' direction='out'/>"
278 " <method name='get_sticker_info_by_keyword'>"
279 " <arg type='s' name='app_id' direction='in'/>"
280 " <arg type='s' name='keyword' direction='in'/>"
281 " <arg type='i' name='offset' direction='in'/>"
282 " <arg type='i' name='count' direction='in'/>"
283 " <arg type='a(i)' name='id_list' direction='out'/>"
286 " <method name='get_sticker_info_by_disp_type'>"
287 " <arg type='s' name='app_id' direction='in'/>"
288 " <arg type='i' name='type' direction='in'/>"
289 " <arg type='i' name='offset' direction='in'/>"
290 " <arg type='i' name='count' direction='in'/>"
291 " <arg type='a(i)' name='id_list' direction='out'/>"
294 " <method name='get_group_list_by_disp_type'>"
295 " <arg type='s' name='app_id' direction='in'/>"
296 " <arg type='i' name='disp_type' direction='in'/>"
297 " <arg type='a(s)' name='group_list' direction='out'/>"
300 " <method name='update_sticker_disp_type'>"
301 " <arg type='i' name='record_id' direction='in'/>"
302 " <arg type='i' name='disp_type' direction='in'/>"
307 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
310 int stickerd_dbus_init(void)
314 ret = stickerd_register_dbus_interface();
315 if (ret != STICKERD_SERVER_ERROR_NONE) {
316 LOGE("Failed to register dbus interface : %d", ret);
317 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
320 return STICKERD_SERVER_ERROR_NONE;
323 static int _check_file_exist(const char *app_id, const char *path)
326 package_info_h package_info = NULL;
327 char *app_path = NULL;
328 char *file_path = NULL;
330 if (access(path, F_OK) == 0) {
335 ret = package_info_create(app_id, &package_info);
336 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
337 LOGE("failed to create package_info. ret: %d", ret);
342 ret = package_info_get_root_path(package_info, &app_path);
343 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
344 LOGE("failed to create package_info. ret: %d", ret);
349 int path_len = strlen(app_path) + strlen(path) + 2;
350 file_path = (char *)calloc(path_len, sizeof(char));
352 LOGE("Failed to alloc memory");
358 snprintf(file_path, path_len, "%s%s",app_path, path);
360 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
362 if (access(file_path, F_OK) != 0) {
363 LOGE("%s does not exist", file_path);
370 package_info_destroy(package_info);
385 static int _mkdirs(const char *path, mode_t mode)
388 char prev_path[2048];
389 const char *tmp = path;
391 if (!path || strlen(path) > 2048)
394 memset(prev_path, '\0', 2048);
395 while ((tmp = strchr(tmp, '/')) != NULL) {
402 strncpy(prev_path, path, len);
403 prev_path[len] = 0x00;
405 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
406 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
407 LOGE("directory create error : %s", error_buffer);
412 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
413 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
414 LOGE("directory create error : %s", error_buffer);
421 static int _file_copy(const char *src, const char *dest)
424 int fd = -1, n_fd = -1;
429 memset(buf, '\0', 4096);
430 fd = open(src, O_RDONLY);
431 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
433 if (fd == -1 || n_fd == -1) {
439 while((size = read(fd, buf, 4096))) {
449 while(write(n_fd, buf, size) == -1) {
470 static char* _convert_sticker_uri(const char *uri, const char *appid)
473 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
474 char * new_path = (char *)calloc(len, sizeof(char));
475 if (new_path == NULL) {
476 LOGE("Failed to alloc memory");
481 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
483 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
485 if (access(new_path, F_OK) == 0) {
486 LOGE("sticker file already exists");
491 ret = _mkdirs(new_path, 0755);
493 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
494 LOGE("directory create error : %s", error_buffer);
498 ret = _file_copy(uri, new_path);
500 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
501 LOGE("failed to copy sticker file : %s", error_buffer);
514 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
518 STICKER_DAT_TYPE key;
519 sticker_info_db *sticker_info = NULL;
520 GVariant *value = NULL;
521 GVariantIter *info_iter = NULL;
522 GVariantIter *keyword_iter = NULL;
525 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
526 if (!info_iter || !keyword_iter) {
527 LOGD("failed to get iter");
528 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
532 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
535 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
539 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
541 case STICKER_DATA_TYPE_APP_ID:
542 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
544 case STICKER_DATA_TYPE_URI_TYPE:
545 sticker_info->type = g_variant_get_int32(value);
547 case STICKER_DATA_TYPE_URI:
548 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
550 case STICKER_DATA_TYPE_THUMBNAIL:
551 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
553 case STICKER_DATA_TYPE_DESCRIPTION:
554 sticker_info->description = (char *) g_variant_get_string(value, NULL);
556 case STICKER_DATA_TYPE_GROUP:
557 sticker_info->group = (char *) g_variant_get_string(value, NULL);
559 case STICKER_DATA_TYPE_DISP_TYPE:
560 sticker_info->display_type = g_variant_get_int32(value);
566 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
567 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
570 if (sticker_info->type == 1) {
571 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
572 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
573 if (!sticker_info->uri) {
574 LOGE("failed to copy sticker file");
575 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
579 LOGE("sticker file does not exist");
580 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
585 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
586 if (ret != STICKERD_SERVER_ERROR_NONE) {
587 LOGE("Failed to insert sticker info");
588 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
592 *reply_body = g_variant_new("(i)", record_id);
593 if (*reply_body == NULL) {
594 LOGE("Failed to create reply_body");
595 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
600 g_variant_unref(value);
603 g_variant_iter_free(info_iter);
606 g_variant_iter_free(keyword_iter);
616 static char* _get_string_from_object(JsonObject *object, const char *key)
618 if (json_object_has_member(object, key) == false)
621 const char *str = json_object_get_string_member(object, key);
628 static int _get_int_from_object(JsonObject *object, const char *key)
630 if (json_object_has_member(object, key) == false)
633 int type = json_object_get_int_member(object, key);
638 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
640 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
642 sticker_info_db *sticker_info = NULL;
644 char *json_path = NULL;
645 JsonParser* parser = NULL;
646 GError* err_msg = NULL;
647 GVariant *body = NULL;
648 char *cmd = "send_insert_result";
650 *reply_body = g_variant_new("()");
651 if (*reply_body == NULL) {
652 LOGE("Failed to create reply_body");
653 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
656 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
658 if (!app_id || !json_path) {
659 LOGE("failed to get parameter");
660 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
663 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
665 parser = json_parser_new();
666 json_parser_load_from_file(parser, json_path, &err_msg);
668 LOGE("failed to load json file. error message: %s", err_msg->message);
669 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
673 JsonNode *root = json_parser_get_root(parser);
675 LOGE("failed to get root");
676 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
680 JsonObject *root_obj = json_node_get_object(root);
681 if (root_obj == NULL) {
682 LOGE("failed to get object");
683 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
687 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
688 if (sticker_arr == NULL) {
689 LOGE("failed to get array member");
690 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
694 int arr_len = json_array_get_length(sticker_arr);
695 for (int i = 0; i < arr_len; i++) {
696 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
697 if (info_object != NULL) {
698 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
700 LOGE("Failed to alloc memory");
704 sticker_info->app_id = app_id;
705 sticker_info->type = _get_int_from_object(info_object, "type");
706 if (sticker_info->type < 1)
709 sticker_info->uri = _get_string_from_object(info_object, "uri");
710 if (!sticker_info->uri)
713 if (sticker_info->type == 1) {
714 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
715 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
716 if (!sticker_info->uri)
723 sticker_info->group = _get_string_from_object(info_object, "group");
724 if (!sticker_info->group)
727 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
728 if (!sticker_info->thumbnail)
731 sticker_info->description = _get_string_from_object(info_object, "description");
733 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
735 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
736 int keyword_arr_len = json_array_get_length(keyword_arr);
737 if (keyword_arr_len < 1)
740 for (int j = 0; j < keyword_arr_len; j++) {
741 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
744 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
745 if (ret != STICKERD_SERVER_ERROR_NONE) {
746 LOGE("Failed to insert sticker info");
747 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
758 g_error_free(err_msg);
760 g_object_unref(parser);
762 body = g_variant_new("(i)", ret);
764 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
765 if (ret != STICKERD_SERVER_ERROR_NONE)
766 LOGE("Failed to send insert result to client");
769 g_variant_unref(body);
774 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
779 *reply_body = g_variant_new("()");
780 if (*reply_body == NULL) {
781 LOGE("Failed to create reply_body");
782 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
785 g_variant_get(parameters, "(i)", &record_id);
787 ret = stickerd_db_delete_sticker_info(record_id);
788 if (ret != STICKERD_SERVER_ERROR_NONE) {
789 LOGE("Failed to delete sticker info");
790 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
796 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
802 *reply_body = g_variant_new("()");
803 if (*reply_body == NULL) {
804 LOGE("Failed to create reply_body");
805 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
808 g_variant_get(parameters, "(ii)", &record_id, &type);
810 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
811 if (ret != STICKERD_SERVER_ERROR_NONE) {
812 LOGE("Failed to update sticker type");
813 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
819 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
827 *reply_body = g_variant_new("()");
828 if (*reply_body == NULL) {
829 LOGE("Failed to create reply_body");
830 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
833 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
836 if (_check_file_exist(app_id, uri) == 0) {
837 uri = _convert_sticker_uri(uri, app_id);
839 LOGE("failed to copy sticker file");
840 return STICKERD_SERVER_ERROR_FILE_EXISTS;
843 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
847 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
848 if (ret != STICKERD_SERVER_ERROR_NONE) {
849 LOGE("Failed to update sticker uri");
850 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
856 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
862 *reply_body = g_variant_new("()");
863 if (*reply_body == NULL) {
864 LOGE("Failed to create reply_body");
865 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
868 g_variant_get(parameters, "(i&s)", &record_id, &thumbnail);
870 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
871 if (ret != STICKERD_SERVER_ERROR_NONE) {
872 LOGE("Failed to update sticker thumbnail");
873 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
879 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
885 *reply_body = g_variant_new("()");
886 if (*reply_body == NULL) {
887 LOGE("Failed to create reply_body");
888 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
891 g_variant_get(parameters, "(i&s)", &record_id, &description);
893 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
894 if (ret != STICKERD_SERVER_ERROR_NONE) {
895 LOGE("Failed to update sticker description");
896 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
902 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
908 *reply_body = g_variant_new("()");
909 if (*reply_body == NULL) {
910 LOGE("Failed to create reply_body");
911 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
914 g_variant_get(parameters, "(i&s)", &record_id, &group);
916 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
917 if (ret != STICKERD_SERVER_ERROR_NONE) {
918 LOGE("Failed to update sticker group");
919 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
925 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
929 GVariantIter *keyword_iter = NULL;
930 char *keyword = NULL;
931 GList *keyword_list = NULL;
933 *reply_body = g_variant_new("()");
934 if (*reply_body == NULL) {
935 LOGE("Failed to create reply_body");
936 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
939 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
942 LOGD("failed to get iter");
943 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
946 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
947 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
950 g_variant_iter_free(keyword_iter);
952 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
953 if (ret != STICKERD_SERVER_ERROR_NONE) {
954 LOGE("Failed to update sticker keyword");
955 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
961 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
964 LOGE("keyword doesn't exist");
968 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
971 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
975 GVariantBuilder *info_builder;
976 GVariantBuilder *keyword_builder;
978 g_variant_get(parameters, "(i)", &record_id);
979 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
982 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
984 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
985 if (ret != STICKERD_SERVER_ERROR_NONE) {
986 LOGE("Failed to get sticker info");
988 if (sticker_info->app_id)
989 free(sticker_info->app_id);
991 if (sticker_info->uri)
992 free(sticker_info->uri);
994 if (sticker_info->thumbnail)
995 free(sticker_info->thumbnail);
997 if (sticker_info->keyword)
998 free(sticker_info->keyword);
1000 if (sticker_info->group)
1001 free(sticker_info->group);
1003 if (sticker_info->description)
1004 free(sticker_info->description);
1006 if (sticker_info->date)
1007 free(sticker_info->date);
1010 sticker_info = NULL;
1011 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1014 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1015 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1016 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1017 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1018 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1019 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1020 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1021 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1022 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1024 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1025 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1027 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1028 g_variant_builder_unref(info_builder);
1029 g_variant_builder_unref(keyword_builder);
1031 if (*reply_body == NULL) {
1032 LOGE("Failed to create reply_body");
1034 sticker_info = NULL;
1035 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1039 sticker_info = NULL;
1043 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1046 GVariantBuilder *builder = NULL;
1047 char *app_id = NULL;
1049 g_variant_get(parameters, "(&s)", &app_id);
1051 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1052 ret = stickerd_db_get_group_list(builder, app_id);
1053 if (ret != STICKERD_SERVER_ERROR_NONE) {
1054 LOGE("Failed to get sticker group list");
1055 g_variant_builder_unref(builder);
1056 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1059 *reply_body = g_variant_new("(a(s))", builder);
1060 g_variant_builder_unref(builder);
1062 if (*reply_body == NULL) {
1063 LOGE("Failed to create reply_body");
1064 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1070 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1073 GVariantBuilder *builder = NULL;
1074 char *app_id = NULL;
1076 g_variant_get(parameters, "(&s)", &app_id);
1078 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1079 ret = stickerd_db_get_keyword_list(builder, app_id);
1080 if (ret != STICKERD_SERVER_ERROR_NONE) {
1081 LOGE("Failed to get sticker keyword list");
1082 g_variant_builder_unref(builder);
1083 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1086 *reply_body = g_variant_new("(a(s))", builder);
1087 g_variant_builder_unref(builder);
1089 if (*reply_body == NULL) {
1090 LOGE("Failed to create reply_body");
1091 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1097 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1101 char *app_id = NULL;
1103 g_variant_get(parameters, "(&s)", &app_id);
1105 ret = stickerd_db_get_sticker_count(&count, app_id);
1106 if (ret != STICKERD_SERVER_ERROR_NONE) {
1107 LOGE("Failed to get sticker count");
1108 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1111 *reply_body = g_variant_new("(i)", count);
1112 if (*reply_body == NULL) {
1113 LOGE("Failed to create reply_body");
1114 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1121 // Send the sticker information by asynchronous communication.
1122 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1127 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1130 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1132 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1133 if (ret != STICKERD_SERVER_ERROR_NONE) {
1134 LOGE("Failed to get sticker info");
1136 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1140 case STICKER_DB_STICKER_ALL:
1141 cmd = "send_all_sticker_info";
1143 case STICKER_DB_STICKER_APPID:
1144 cmd = "send_sticker_info_by_appid";
1146 case STICKER_DB_STICKER_TYPE:
1147 cmd = "send_sticker_info_by_type";
1149 case STICKER_DB_STICKER_GROUP:
1150 cmd = "send_sticker_info_by_group";
1152 case STICKER_DB_STICKER_KEYWORD:
1153 cmd = "send_sticker_info_by_keyword";
1160 GVariantBuilder *info_builder;
1161 GVariantBuilder *keyword_builder;
1163 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1164 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1165 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1166 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1167 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1168 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1169 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1170 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1171 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1173 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1174 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1176 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1177 g_variant_builder_unref(info_builder);
1178 g_variant_builder_unref(keyword_builder);
1180 ret = stickerd_send_dbus_message(body, sender, cmd);
1181 if (ret != STICKERD_SERVER_ERROR_NONE) {
1182 LOGE("Failed to send sticker info to client");
1184 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1192 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1195 LOGE("id doesn't exist");
1199 g_variant_builder_add(id_builder, "(i)", atoi(id));
1202 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1206 char *app_id = NULL;
1207 GList *id_list = NULL;
1208 GVariantBuilder *id_builder = NULL;
1210 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1212 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1213 if (ret != STICKERD_SERVER_ERROR_NONE) {
1214 LOGE("Failed to get all sticker id");
1216 g_list_free_full(id_list, free);
1217 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1220 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1221 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1223 *reply_body = g_variant_new("(a(i))", id_builder);
1224 if (*reply_body == NULL) {
1225 LOGE("Failed to create reply_body");
1226 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1230 g_list_free_full(id_list, free);
1233 g_variant_builder_unref(id_builder);
1238 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1241 GList *id_list = NULL;
1242 char *app_id = NULL;
1244 GVariantBuilder *id_builder = NULL;
1246 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1248 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1249 if (ret != STICKERD_SERVER_ERROR_NONE) {
1250 LOGE("Failed to get all sticker id");
1252 g_list_free_full(id_list, free);
1253 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1256 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1257 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1259 *reply_body = g_variant_new("(a(i))", id_builder);
1260 if (*reply_body == NULL) {
1261 LOGE("Failed to create reply_body");
1262 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1266 g_list_free_full(id_list, free);
1269 g_variant_builder_unref(id_builder);
1274 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1277 GList *id_list = NULL;
1278 char *app_id = NULL;
1279 int type, offset, count;
1280 GVariantBuilder *id_builder = NULL;
1282 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1284 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1285 if (ret != STICKERD_SERVER_ERROR_NONE) {
1286 LOGE("Failed to get all sticker id");
1288 g_list_free_full(id_list, free);
1289 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1292 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1293 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1295 *reply_body = g_variant_new("(a(i))", id_builder);
1296 if (*reply_body == NULL) {
1297 LOGE("Failed to create reply_body");
1298 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1302 g_list_free_full(id_list, free);
1305 g_variant_builder_unref(id_builder);
1310 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1313 GList *id_list = NULL;
1314 char *app_id = NULL;
1317 GVariantBuilder *id_builder = NULL;
1319 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1321 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1322 if (ret != STICKERD_SERVER_ERROR_NONE) {
1323 LOGE("Failed to get all sticker id");
1325 g_list_free_full(id_list, free);
1326 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1329 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1330 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1332 *reply_body = g_variant_new("(a(i))", id_builder);
1333 if (*reply_body == NULL) {
1334 LOGE("Failed to create reply_body");
1335 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1339 g_list_free_full(id_list, free);
1342 g_variant_builder_unref(id_builder);
1347 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1350 GList *id_list = NULL;
1351 char *app_id = NULL;
1352 char *keyword = NULL;
1354 GVariantBuilder *id_builder = NULL;
1356 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1358 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1359 if (ret != STICKERD_SERVER_ERROR_NONE) {
1360 LOGE("Failed to get all sticker id");
1362 g_list_free_full(id_list, free);
1363 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1366 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1367 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1369 *reply_body = g_variant_new("(a(i))", id_builder);
1370 if (*reply_body == NULL) {
1371 LOGE("Failed to create reply_body");
1372 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1376 g_list_free_full(id_list, free);
1379 g_variant_builder_unref(id_builder);
1384 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1387 GList *id_list = NULL;
1388 char *app_id = NULL;
1389 int type, offset, count;
1390 GVariantBuilder *id_builder = NULL;
1392 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1394 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1395 if (ret != STICKERD_SERVER_ERROR_NONE) {
1396 LOGE("Failed to get all sticker id");
1398 g_list_free_full(id_list, free);
1399 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1402 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1403 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1405 *reply_body = g_variant_new("(a(i))", id_builder);
1406 if (*reply_body == NULL) {
1407 LOGE("Failed to create reply_body");
1408 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1412 g_list_free_full(id_list, free);
1415 g_variant_builder_unref(id_builder);
1420 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1423 GVariantBuilder *builder = NULL;
1424 char *app_id = NULL;
1427 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1429 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1430 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1431 if (ret != STICKERD_SERVER_ERROR_NONE) {
1432 LOGE("Failed to get sticker group list");
1433 g_variant_builder_unref(builder);
1434 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1437 *reply_body = g_variant_new("(a(s))", builder);
1438 g_variant_builder_unref(builder);
1440 if (*reply_body == NULL) {
1441 LOGE("Failed to create reply_body");
1442 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1448 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1454 *reply_body = g_variant_new("()");
1455 if (*reply_body == NULL) {
1456 LOGE("Failed to create reply_body");
1457 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1460 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1462 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1463 if (ret != STICKERD_SERVER_ERROR_NONE) {
1464 LOGE("Failed to update sticker disp_type");
1465 return STICKERD_SERVER_ERROR_OPERATION_FAILED;