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);
148 } else if (g_strcmp0(method_name, "check_file_exists") == 0) {
149 ret = stickerd_check_file_exists(parameters, &reply_body);
152 if (ret == STICKERD_SERVER_ERROR_NONE) {
153 LOGD("method_call successful, method_name : %s", method_name);
154 g_dbus_method_invocation_return_value(invocation, reply_body);
156 LOGE("method_call failed, method_name : %s", method_name);
157 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
160 _check_watcher_exist();
163 static const GDBusInterfaceVTable _sticker_interface_vtable = {
164 _stickerd_client_dbus_method_call_handler,
169 int stickerd_register_dbus_interface(void)
171 static gchar introspection_xml[] =
173 " <interface name='org.tizen.sticker_service'>"
174 " <method name='sticker_service_register'>"
175 " <arg type='i' name='watcher_id' direction='out'/>"
178 " <method name='sticker_service_unregister'>"
179 " <arg type='i' name='watcher_id' direction='in'/>"
182 " <method name='insert_sticker_info'>"
183 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
184 " <arg type='a(s)' name='keyword_list' direction='in'/>"
185 " <arg type='i' name='record_id' direction='out'/>"
188 " <method name='update_sticker_info_by_json'>"
189 " <arg type='s' name='app_id' direction='in'/>"
190 " <arg type='s' name='json_path' direction='in'/>"
193 " <method name='delete_sticker_info'>"
194 " <arg type='i' name='record_id' direction='in'/>"
197 " <method name='update_sticker_type'>"
198 " <arg type='i' name='record_id' direction='in'/>"
199 " <arg type='i' name='type' direction='in'/>"
202 " <method name='update_sticker_uri'>"
203 " <arg type='i' name='record_id' direction='in'/>"
204 " <arg type='s' name='app_id' direction='in'/>"
205 " <arg type='i' name='type' direction='in'/>"
206 " <arg type='s' name='uri' direction='in'/>"
209 " <method name='update_sticker_thumbnail'>"
210 " <arg type='i' name='record_id' direction='in'/>"
211 " <arg type='s' name='thumbnail' direction='in'/>"
214 " <method name='update_sticker_description'>"
215 " <arg type='i' name='record_id' direction='in'/>"
216 " <arg type='s' name='description' direction='in'/>"
219 " <method name='update_sticker_group'>"
220 " <arg type='i' name='record_id' direction='in'/>"
221 " <arg type='s' name='group' direction='in'/>"
224 " <method name='update_sticker_keyword'>"
225 " <arg type='i' name='record_id' direction='in'/>"
226 " <arg type='a(s)' name='keyword' direction='in'/>"
229 " <method name='get_sticker_info'>"
230 " <arg type='i' name='record_id' direction='in'/>"
231 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
232 " <arg type='a(s)' name='keyword_list' direction='out'/>"
235 " <method name='get_group_list'>"
236 " <arg type='s' name='app_id' direction='in'/>"
237 " <arg type='a(s)' name='group_list' direction='out'/>"
240 " <method name='get_keyword_list'>"
241 " <arg type='s' name='app_id' direction='in'/>"
242 " <arg type='a(s)' name='keyword_list' direction='out'/>"
245 " <method name='get_sticker_count'>"
246 " <arg type='s' name='app_id' direction='in'/>"
247 " <arg type='i' name='count' direction='out'/>"
250 " <method name='get_all_sticker_info'>"
251 " <arg type='s' name='app_id' direction='in'/>"
252 " <arg type='i' name='offset' direction='in'/>"
253 " <arg type='i' name='count' direction='in'/>"
254 " <arg type='a(i)' name='id_list' direction='out'/>"
257 " <method name='get_sticker_info_by_appid'>"
258 " <arg type='s' name='app_id' direction='in'/>"
259 " <arg type='i' name='offset' direction='in'/>"
260 " <arg type='i' name='count' direction='in'/>"
261 " <arg type='a(i)' name='id_list' direction='out'/>"
264 " <method name='get_sticker_info_by_type'>"
265 " <arg type='s' name='app_id' direction='in'/>"
266 " <arg type='i' name='type' direction='in'/>"
267 " <arg type='i' name='offset' direction='in'/>"
268 " <arg type='i' name='count' direction='in'/>"
269 " <arg type='a(i)' name='id_list' direction='out'/>"
272 " <method name='get_sticker_info_by_group'>"
273 " <arg type='s' name='app_id' direction='in'/>"
274 " <arg type='s' name='group' direction='in'/>"
275 " <arg type='i' name='offset' direction='in'/>"
276 " <arg type='i' name='count' direction='in'/>"
277 " <arg type='a(i)' name='id_list' direction='out'/>"
280 " <method name='get_sticker_info_by_keyword'>"
281 " <arg type='s' name='app_id' direction='in'/>"
282 " <arg type='s' name='keyword' direction='in'/>"
283 " <arg type='i' name='offset' direction='in'/>"
284 " <arg type='i' name='count' direction='in'/>"
285 " <arg type='a(i)' name='id_list' direction='out'/>"
288 " <method name='get_sticker_info_by_disp_type'>"
289 " <arg type='s' name='app_id' direction='in'/>"
290 " <arg type='i' name='type' direction='in'/>"
291 " <arg type='i' name='offset' direction='in'/>"
292 " <arg type='i' name='count' direction='in'/>"
293 " <arg type='a(i)' name='id_list' direction='out'/>"
296 " <method name='get_group_list_by_disp_type'>"
297 " <arg type='s' name='app_id' direction='in'/>"
298 " <arg type='i' name='disp_type' direction='in'/>"
299 " <arg type='a(s)' name='group_list' direction='out'/>"
302 " <method name='update_sticker_disp_type'>"
303 " <arg type='i' name='record_id' direction='in'/>"
304 " <arg type='i' name='disp_type' direction='in'/>"
307 " <method name='check_file_exists'>"
308 " <arg type='s' name='uri' direction='in'/>"
309 " <arg type='i' name='result' direction='out'/>"
314 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
317 int stickerd_dbus_init(void)
321 ret = stickerd_register_dbus_interface();
322 if (ret != STICKERD_SERVER_ERROR_NONE) {
323 LOGE("Failed to register dbus interface : %d", ret);
324 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
327 return STICKERD_SERVER_ERROR_NONE;
330 static int _check_file_exist(const char *app_id, const char *path)
333 package_info_h package_info = NULL;
334 char *app_path = NULL;
335 char *file_path = NULL;
337 if (access(path, F_OK) == 0) {
342 ret = package_info_create(app_id, &package_info);
343 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
344 LOGE("failed to create package_info. ret: %d", ret);
349 ret = package_info_get_root_path(package_info, &app_path);
350 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
351 LOGE("failed to create package_info. ret: %d", ret);
356 int path_len = strlen(app_path) + strlen(path) + 2;
357 file_path = (char *)calloc(path_len, sizeof(char));
359 LOGE("Failed to alloc memory");
365 snprintf(file_path, path_len, "%s%s",app_path, path);
367 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
369 if (access(file_path, F_OK) != 0) {
370 LOGE("%s does not exist", file_path);
377 package_info_destroy(package_info);
392 static int _mkdirs(const char *path, mode_t mode)
395 char prev_path[2048];
396 const char *tmp = path;
398 if (!path || strlen(path) > 2048)
401 memset(prev_path, '\0', 2048);
402 while ((tmp = strchr(tmp, '/')) != NULL) {
409 strncpy(prev_path, path, len);
410 prev_path[len] = 0x00;
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);
419 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
420 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
421 LOGE("directory create error : %s", error_buffer);
428 static int _file_copy(const char *src, const char *dest)
431 int fd = -1, n_fd = -1;
436 memset(buf, '\0', 4096);
437 fd = open(src, O_RDONLY);
438 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
440 if (fd == -1 || n_fd == -1) {
446 while((size = read(fd, buf, 4096))) {
456 while(write(n_fd, buf, size) == -1) {
477 static char* _convert_sticker_uri(const char *uri, const char *appid)
480 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
481 char * new_path = (char *)calloc(len, sizeof(char));
482 if (new_path == NULL) {
483 LOGE("Failed to alloc memory");
488 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
490 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
492 if (access(new_path, F_OK) == 0) {
493 LOGE("sticker file already exists");
498 ret = _mkdirs(new_path, 0755);
500 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
501 LOGE("directory create error : %s", error_buffer);
505 ret = _file_copy(uri, new_path);
507 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
508 LOGE("failed to copy sticker file : %s", error_buffer);
521 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
525 STICKER_DAT_TYPE key;
526 sticker_info_db *sticker_info = NULL;
527 GVariant *value = NULL;
528 GVariantIter *info_iter = NULL;
529 GVariantIter *keyword_iter = NULL;
532 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
533 if (!info_iter || !keyword_iter) {
534 LOGD("failed to get iter");
535 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
539 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
542 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
546 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
548 case STICKER_DATA_TYPE_APP_ID:
549 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
551 case STICKER_DATA_TYPE_URI_TYPE:
552 sticker_info->type = g_variant_get_int32(value);
554 case STICKER_DATA_TYPE_URI:
555 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
557 case STICKER_DATA_TYPE_THUMBNAIL:
558 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
560 case STICKER_DATA_TYPE_DESCRIPTION:
561 sticker_info->description = (char *) g_variant_get_string(value, NULL);
563 case STICKER_DATA_TYPE_GROUP:
564 sticker_info->group = (char *) g_variant_get_string(value, NULL);
566 case STICKER_DATA_TYPE_DISP_TYPE:
567 sticker_info->display_type = g_variant_get_int32(value);
573 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
574 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
577 if (sticker_info->type == 1) {
578 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
579 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
580 if (!sticker_info->uri) {
581 LOGE("failed to copy sticker file");
582 ret = STICKERD_SERVER_ERROR_FILE_EXISTS;
586 LOGE("sticker file does not exist");
587 ret = STICKERD_SERVER_ERROR_NO_SUCH_FILE;
592 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
593 if (ret != STICKERD_SERVER_ERROR_NONE) {
594 LOGE("Failed to insert sticker info");
595 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
599 *reply_body = g_variant_new("(i)", record_id);
600 if (*reply_body == NULL) {
601 LOGE("Failed to create reply_body");
602 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
607 g_variant_unref(value);
610 g_variant_iter_free(info_iter);
613 g_variant_iter_free(keyword_iter);
623 static char* _get_string_from_object(JsonObject *object, const char *key)
625 if (json_object_has_member(object, key) == false)
628 const char *str = json_object_get_string_member(object, key);
635 static int _get_int_from_object(JsonObject *object, const char *key)
637 if (json_object_has_member(object, key) == false)
640 int type = json_object_get_int_member(object, key);
645 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
647 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
649 sticker_info_db *sticker_info = NULL;
651 char *json_path = NULL;
652 JsonParser* parser = NULL;
653 GError* err_msg = NULL;
654 GVariant *body = NULL;
655 char *cmd = "send_insert_result";
657 *reply_body = g_variant_new("()");
658 if (*reply_body == NULL) {
659 LOGE("Failed to create reply_body");
660 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
663 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
665 if (!app_id || !json_path) {
666 LOGE("failed to get parameter");
667 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
670 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
672 parser = json_parser_new();
673 json_parser_load_from_file(parser, json_path, &err_msg);
675 LOGE("failed to load json file. error message: %s", err_msg->message);
676 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
680 JsonNode *root = json_parser_get_root(parser);
682 LOGE("failed to get root");
683 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
687 JsonObject *root_obj = json_node_get_object(root);
688 if (root_obj == NULL) {
689 LOGE("failed to get object");
690 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
694 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
695 if (sticker_arr == NULL) {
696 LOGE("failed to get array member");
697 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
701 int arr_len = json_array_get_length(sticker_arr);
702 for (int i = 0; i < arr_len; i++) {
703 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
704 if (info_object != NULL) {
705 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
707 LOGE("Failed to alloc memory");
711 sticker_info->app_id = app_id;
712 sticker_info->type = _get_int_from_object(info_object, "type");
713 if (sticker_info->type < 1)
716 sticker_info->uri = _get_string_from_object(info_object, "uri");
717 if (!sticker_info->uri)
720 if (sticker_info->type == 1) {
721 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
722 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
723 if (!sticker_info->uri)
730 sticker_info->group = _get_string_from_object(info_object, "group");
731 if (!sticker_info->group)
734 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
735 if (!sticker_info->thumbnail)
738 sticker_info->description = _get_string_from_object(info_object, "description");
740 sticker_info->display_type = _get_int_from_object(info_object, "display_type");
742 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
743 int keyword_arr_len = json_array_get_length(keyword_arr);
744 if (keyword_arr_len < 1)
747 for (int j = 0; j < keyword_arr_len; j++) {
748 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
751 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
752 if (ret != STICKERD_SERVER_ERROR_NONE) {
753 LOGE("Failed to insert sticker info");
754 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
765 g_error_free(err_msg);
767 g_object_unref(parser);
769 body = g_variant_new("(i)", ret);
771 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
772 if (ret != STICKERD_SERVER_ERROR_NONE)
773 LOGE("Failed to send insert result to client");
776 g_variant_unref(body);
781 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
786 *reply_body = g_variant_new("()");
787 if (*reply_body == NULL) {
788 LOGE("Failed to create reply_body");
789 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
792 g_variant_get(parameters, "(i)", &record_id);
794 ret = stickerd_db_delete_sticker_info(record_id);
795 if (ret != STICKERD_SERVER_ERROR_NONE) {
796 LOGE("Failed to delete sticker info");
797 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
803 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
809 *reply_body = g_variant_new("()");
810 if (*reply_body == NULL) {
811 LOGE("Failed to create reply_body");
812 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
815 g_variant_get(parameters, "(ii)", &record_id, &type);
817 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
818 if (ret != STICKERD_SERVER_ERROR_NONE) {
819 LOGE("Failed to update sticker type");
820 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
826 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
834 *reply_body = g_variant_new("()");
835 if (*reply_body == NULL) {
836 LOGE("Failed to create reply_body");
837 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
840 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
843 if (_check_file_exist(app_id, uri) == 0) {
844 uri = _convert_sticker_uri(uri, app_id);
846 LOGE("failed to copy sticker file");
847 return STICKERD_SERVER_ERROR_FILE_EXISTS;
850 return STICKERD_SERVER_ERROR_NO_SUCH_FILE;
854 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
855 if (ret != STICKERD_SERVER_ERROR_NONE) {
856 LOGE("Failed to update sticker uri");
857 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
863 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
869 *reply_body = g_variant_new("()");
870 if (*reply_body == NULL) {
871 LOGE("Failed to create reply_body");
872 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
875 g_variant_get(parameters, "(i&s)", &record_id, &thumbnail);
877 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
878 if (ret != STICKERD_SERVER_ERROR_NONE) {
879 LOGE("Failed to update sticker thumbnail");
880 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
886 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
892 *reply_body = g_variant_new("()");
893 if (*reply_body == NULL) {
894 LOGE("Failed to create reply_body");
895 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
898 g_variant_get(parameters, "(i&s)", &record_id, &description);
900 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
901 if (ret != STICKERD_SERVER_ERROR_NONE) {
902 LOGE("Failed to update sticker description");
903 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
909 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
915 *reply_body = g_variant_new("()");
916 if (*reply_body == NULL) {
917 LOGE("Failed to create reply_body");
918 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
921 g_variant_get(parameters, "(i&s)", &record_id, &group);
923 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
924 if (ret != STICKERD_SERVER_ERROR_NONE) {
925 LOGE("Failed to update sticker group");
926 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
932 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
936 GVariantIter *keyword_iter = NULL;
937 char *keyword = NULL;
938 GList *keyword_list = NULL;
940 *reply_body = g_variant_new("()");
941 if (*reply_body == NULL) {
942 LOGE("Failed to create reply_body");
943 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
946 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
949 LOGD("failed to get iter");
950 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
953 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
954 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
957 g_variant_iter_free(keyword_iter);
959 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
960 if (ret != STICKERD_SERVER_ERROR_NONE) {
961 LOGE("Failed to update sticker keyword");
962 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
968 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
971 LOGE("keyword doesn't exist");
975 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
978 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
982 GVariantBuilder *info_builder;
983 GVariantBuilder *keyword_builder;
985 g_variant_get(parameters, "(i)", &record_id);
986 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
989 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
991 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
992 if (ret != STICKERD_SERVER_ERROR_NONE) {
993 LOGE("Failed to get sticker info");
995 if (sticker_info->app_id)
996 free(sticker_info->app_id);
998 if (sticker_info->uri)
999 free(sticker_info->uri);
1001 if (sticker_info->thumbnail)
1002 free(sticker_info->thumbnail);
1004 if (sticker_info->keyword)
1005 free(sticker_info->keyword);
1007 if (sticker_info->group)
1008 free(sticker_info->group);
1010 if (sticker_info->description)
1011 free(sticker_info->description);
1013 if (sticker_info->date)
1014 free(sticker_info->date);
1017 sticker_info = NULL;
1018 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1021 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1022 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1023 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1024 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1025 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1026 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1027 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1028 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1029 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DISP_TYPE, g_variant_new_int32(sticker_info->display_type));
1031 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1032 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1034 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1035 g_variant_builder_unref(info_builder);
1036 g_variant_builder_unref(keyword_builder);
1038 if (*reply_body == NULL) {
1039 LOGE("Failed to create reply_body");
1041 sticker_info = NULL;
1042 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1046 sticker_info = NULL;
1050 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
1053 GVariantBuilder *builder = NULL;
1054 char *app_id = NULL;
1056 g_variant_get(parameters, "(&s)", &app_id);
1058 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1059 ret = stickerd_db_get_group_list(builder, app_id);
1060 if (ret != STICKERD_SERVER_ERROR_NONE) {
1061 LOGE("Failed to get sticker group list");
1062 g_variant_builder_unref(builder);
1063 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1066 *reply_body = g_variant_new("(a(s))", builder);
1067 g_variant_builder_unref(builder);
1069 if (*reply_body == NULL) {
1070 LOGE("Failed to create reply_body");
1071 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1077 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1080 GVariantBuilder *builder = NULL;
1081 char *app_id = NULL;
1083 g_variant_get(parameters, "(&s)", &app_id);
1085 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1086 ret = stickerd_db_get_keyword_list(builder, app_id);
1087 if (ret != STICKERD_SERVER_ERROR_NONE) {
1088 LOGE("Failed to get sticker keyword list");
1089 g_variant_builder_unref(builder);
1090 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1093 *reply_body = g_variant_new("(a(s))", builder);
1094 g_variant_builder_unref(builder);
1096 if (*reply_body == NULL) {
1097 LOGE("Failed to create reply_body");
1098 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1104 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1108 char *app_id = NULL;
1110 g_variant_get(parameters, "(&s)", &app_id);
1112 ret = stickerd_db_get_sticker_count(&count, app_id);
1113 if (ret != STICKERD_SERVER_ERROR_NONE) {
1114 LOGE("Failed to get sticker count");
1115 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1118 *reply_body = g_variant_new("(i)", count);
1119 if (*reply_body == NULL) {
1120 LOGE("Failed to create reply_body");
1121 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1128 // Send the sticker information by asynchronous communication.
1129 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1134 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1137 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1139 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1140 if (ret != STICKERD_SERVER_ERROR_NONE) {
1141 LOGE("Failed to get sticker info");
1143 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1147 case STICKER_DB_STICKER_ALL:
1148 cmd = "send_all_sticker_info";
1150 case STICKER_DB_STICKER_APPID:
1151 cmd = "send_sticker_info_by_appid";
1153 case STICKER_DB_STICKER_TYPE:
1154 cmd = "send_sticker_info_by_type";
1156 case STICKER_DB_STICKER_GROUP:
1157 cmd = "send_sticker_info_by_group";
1159 case STICKER_DB_STICKER_KEYWORD:
1160 cmd = "send_sticker_info_by_keyword";
1167 GVariantBuilder *info_builder;
1168 GVariantBuilder *keyword_builder;
1170 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1171 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1172 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1173 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1174 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1175 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1176 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1177 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1178 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1180 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1181 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1183 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1184 g_variant_builder_unref(info_builder);
1185 g_variant_builder_unref(keyword_builder);
1187 ret = stickerd_send_dbus_message(body, sender, cmd);
1188 if (ret != STICKERD_SERVER_ERROR_NONE) {
1189 LOGE("Failed to send sticker info to client");
1191 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1199 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1202 LOGE("id doesn't exist");
1206 g_variant_builder_add(id_builder, "(i)", atoi(id));
1209 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1213 char *app_id = NULL;
1214 GList *id_list = NULL;
1215 GVariantBuilder *id_builder = NULL;
1217 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1219 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, app_id, offset, count);
1220 if (ret != STICKERD_SERVER_ERROR_NONE) {
1221 LOGE("Failed to get all sticker id");
1223 g_list_free_full(id_list, free);
1224 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1227 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1228 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1230 *reply_body = g_variant_new("(a(i))", id_builder);
1231 if (*reply_body == NULL) {
1232 LOGE("Failed to create reply_body");
1233 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1237 g_list_free_full(id_list, free);
1240 g_variant_builder_unref(id_builder);
1245 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1248 GList *id_list = NULL;
1249 char *app_id = NULL;
1251 GVariantBuilder *id_builder = NULL;
1253 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1255 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, NULL, app_id, offset, count);
1256 if (ret != STICKERD_SERVER_ERROR_NONE) {
1257 LOGE("Failed to get all sticker id");
1259 g_list_free_full(id_list, free);
1260 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1263 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1264 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1266 *reply_body = g_variant_new("(a(i))", id_builder);
1267 if (*reply_body == NULL) {
1268 LOGE("Failed to create reply_body");
1269 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1273 g_list_free_full(id_list, free);
1276 g_variant_builder_unref(id_builder);
1281 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1284 GList *id_list = NULL;
1285 char *app_id = NULL;
1286 int type, offset, count;
1287 GVariantBuilder *id_builder = NULL;
1289 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1291 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, app_id, offset, count);
1292 if (ret != STICKERD_SERVER_ERROR_NONE) {
1293 LOGE("Failed to get all sticker id");
1295 g_list_free_full(id_list, free);
1296 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1299 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1300 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1302 *reply_body = g_variant_new("(a(i))", id_builder);
1303 if (*reply_body == NULL) {
1304 LOGE("Failed to create reply_body");
1305 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1309 g_list_free_full(id_list, free);
1312 g_variant_builder_unref(id_builder);
1317 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1320 GList *id_list = NULL;
1321 char *app_id = NULL;
1324 GVariantBuilder *id_builder = NULL;
1326 g_variant_get(parameters, "(&s&sii)", &app_id, &group, &offset, &count);
1328 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, app_id, offset, count);
1329 if (ret != STICKERD_SERVER_ERROR_NONE) {
1330 LOGE("Failed to get all sticker id");
1332 g_list_free_full(id_list, free);
1333 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1336 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1337 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1339 *reply_body = g_variant_new("(a(i))", id_builder);
1340 if (*reply_body == NULL) {
1341 LOGE("Failed to create reply_body");
1342 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1346 g_list_free_full(id_list, free);
1349 g_variant_builder_unref(id_builder);
1354 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1357 GList *id_list = NULL;
1358 char *app_id = NULL;
1359 char *keyword = NULL;
1361 GVariantBuilder *id_builder = NULL;
1363 g_variant_get(parameters, "(&s&sii)", &app_id, &keyword, &offset, &count);
1365 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, app_id, offset, count);
1366 if (ret != STICKERD_SERVER_ERROR_NONE) {
1367 LOGE("Failed to get all sticker id");
1369 g_list_free_full(id_list, free);
1370 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1373 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1374 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1376 *reply_body = g_variant_new("(a(i))", id_builder);
1377 if (*reply_body == NULL) {
1378 LOGE("Failed to create reply_body");
1379 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1383 g_list_free_full(id_list, free);
1386 g_variant_builder_unref(id_builder);
1391 int stickerd_get_sticker_info_by_display_type(GVariant *parameters, GVariant **reply_body)
1394 GList *id_list = NULL;
1395 char *app_id = NULL;
1396 int type, offset, count;
1397 GVariantBuilder *id_builder = NULL;
1399 g_variant_get(parameters, "(&siii)", &app_id, &type, &offset, &count);
1401 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_DISP_TYPE, &id_list, &type, app_id, offset, count);
1402 if (ret != STICKERD_SERVER_ERROR_NONE) {
1403 LOGE("Failed to get all sticker id");
1405 g_list_free_full(id_list, free);
1406 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1409 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1410 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1412 *reply_body = g_variant_new("(a(i))", id_builder);
1413 if (*reply_body == NULL) {
1414 LOGE("Failed to create reply_body");
1415 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1419 g_list_free_full(id_list, free);
1422 g_variant_builder_unref(id_builder);
1427 int stickerd_get_group_list_by_disp_type(GVariant *parameters, GVariant **reply_body)
1430 GVariantBuilder *builder = NULL;
1431 char *app_id = NULL;
1434 g_variant_get(parameters, "(&si)", &app_id, &disp_type);
1436 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1437 ret = stickerd_db_get_group_list_by_display_type(builder, app_id, disp_type);
1438 if (ret != STICKERD_SERVER_ERROR_NONE) {
1439 LOGE("Failed to get sticker group list");
1440 g_variant_builder_unref(builder);
1441 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1444 *reply_body = g_variant_new("(a(s))", builder);
1445 g_variant_builder_unref(builder);
1447 if (*reply_body == NULL) {
1448 LOGE("Failed to create reply_body");
1449 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1455 int stickerd_update_sticker_disp_type(GVariant *parameters, GVariant **reply_body)
1461 *reply_body = g_variant_new("()");
1462 if (*reply_body == NULL) {
1463 LOGE("Failed to create reply_body");
1464 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1467 g_variant_get(parameters, "(ii)", &record_id, &disp_type);
1469 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DISP_TYPE, &disp_type);
1470 if (ret != STICKERD_SERVER_ERROR_NONE) {
1471 LOGE("Failed to update sticker disp_type");
1472 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1478 int stickerd_check_file_exists(GVariant *parameters, GVariant **reply_body)
1484 g_variant_get(parameters, "(&s)", &uri);
1486 ret = stickerd_db_check_file_exists(&result, uri);
1487 if (ret != STICKERD_SERVER_ERROR_NONE) {
1488 LOGE("Failed to get sticker count");
1489 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1492 *reply_body = g_variant_new("(i)", result);
1493 if (*reply_body == NULL) {
1494 LOGE("Failed to create reply_body");
1495 return STICKERD_SERVER_ERROR_OPERATION_FAILED;