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;
46 static char error_buffer[MAX_ERROR_BUFFER];
48 static void _on_name_appeared(GDBusConnection *connection,
50 const gchar *name_owner,
53 LOGD("name: %s", name);
56 static void _on_name_vanished(GDBusConnection *connection,
60 monitoring_info_s *info = (monitoring_info_s *)user_data;
63 LOGD("name: %s", name);
64 g_bus_unwatch_name(info->watcher_id);
65 delete_monitoring_list(&_monitoring_hash, info->bus_name, info->uid);
74 static void _stickerd_client_dbus_method_call_handler(GDBusConnection *conn, const gchar *sender, const gchar *object_path,
75 const gchar *iface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation,
78 LOGD("stickerd method_name: %s, sender: %s", method_name, sender);
80 GVariant *reply_body = NULL;
81 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
83 if (g_strcmp0(method_name, "sticker_service_register") == 0) {
84 ret = stickerd_server_register(parameters, &reply_body, sender,
85 _on_name_appeared, _on_name_vanished, &_monitoring_hash);
86 } else if (g_strcmp0(method_name, "insert_sticker_info") == 0) {
87 ret = stickerd_insert_sticker_info(parameters, &reply_body);
88 } else if (g_strcmp0(method_name, "update_sticker_info_by_json") == 0) {
89 ret = stickerd_insert_sticker_info_by_json(parameters, &reply_body, sender);
90 } else if (g_strcmp0(method_name, "delete_sticker_info") == 0) {
91 ret = stickerd_del_sticker_info(parameters, &reply_body);
92 } else if (g_strcmp0(method_name, "update_sticker_type") == 0) {
93 ret = stickerd_update_sticker_type(parameters, &reply_body);
94 } else if (g_strcmp0(method_name, "update_sticker_uri") == 0) {
95 ret = stickerd_update_sticker_uri(parameters, &reply_body);
96 } else if (g_strcmp0(method_name, "update_sticker_thumbnail") == 0) {
97 ret = stickerd_update_sticker_thumbnail(parameters, &reply_body);
98 } else if (g_strcmp0(method_name, "update_sticker_description") == 0) {
99 ret = stickerd_update_sticker_description(parameters, &reply_body);
100 } else if (g_strcmp0(method_name, "update_sticker_group") == 0) {
101 ret = stickerd_update_sticker_group(parameters, &reply_body);
102 } else if (g_strcmp0(method_name, "update_sticker_keyword") == 0) {
103 ret = stickerd_update_sticker_keyword(parameters, &reply_body);
104 } else if (g_strcmp0(method_name, "get_sticker_info") == 0) {
105 ret = stickerd_get_sticker_info(parameters, &reply_body);
106 } else if (g_strcmp0(method_name, "get_group_list") == 0) {
107 ret = stickerd_get_group_list(parameters, &reply_body);
108 } else if (g_strcmp0(method_name, "get_keyword_list") == 0) {
109 ret = stickerd_get_keyword_list(parameters, &reply_body);
110 } else if (g_strcmp0(method_name, "get_sticker_count") == 0) {
111 ret = stickerd_get_sticker_count(parameters, &reply_body);
112 } else if (g_strcmp0(method_name, "get_all_sticker_info") == 0) {
113 ret = stickerd_get_all_sticker_info(parameters, &reply_body);
114 } else if (g_strcmp0(method_name, "get_sticker_info_by_appid") == 0) {
115 ret = stickerd_get_sticker_info_by_app_id(parameters, &reply_body);
116 } else if (g_strcmp0(method_name, "get_sticker_info_by_type") == 0) {
117 ret = stickerd_get_sticker_info_by_type(parameters, &reply_body);
118 } else if (g_strcmp0(method_name, "get_sticker_info_by_group") == 0) {
119 ret = stickerd_get_sticker_info_by_group(parameters, &reply_body);
120 } else if (g_strcmp0(method_name, "get_sticker_info_by_keyword") == 0) {
121 ret = stickerd_get_sticker_info_by_keyword(parameters, &reply_body);
124 if (ret == STICKERD_SERVER_ERROR_NONE) {
125 LOGD("method_call successful, method_name : %s", method_name);
126 g_dbus_method_invocation_return_value(invocation, reply_body);
128 LOGE("method_call failed, method_name : %s", method_name);
129 g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, ret, "sticker error");
133 g_variant_unref(reply_body);
136 static const GDBusInterfaceVTable _sticker_interface_vtable = {
137 _stickerd_client_dbus_method_call_handler,
142 int stickerd_register_dbus_interface(void)
144 static gchar introspection_xml[] =
146 " <interface name='org.tizen.sticker_service'>"
147 " <method name='sticker_service_register'>"
150 " <method name='insert_sticker_info'>"
151 " <arg type='a{iv}' name='sticker_info' direction='in'/>"
152 " <arg type='a(s)' name='keyword_list' direction='in'/>"
153 " <arg type='i' name='record_id' direction='out'/>"
156 " <method name='update_sticker_info_by_json'>"
157 " <arg type='s' name='app_id' direction='in'/>"
158 " <arg type='s' name='json_path' direction='in'/>"
161 " <method name='delete_sticker_info'>"
162 " <arg type='i' name='record_id' direction='in'/>"
165 " <method name='update_sticker_type'>"
166 " <arg type='i' name='record_id' direction='in'/>"
167 " <arg type='i' name='type' direction='in'/>"
170 " <method name='update_sticker_uri'>"
171 " <arg type='i' name='record_id' direction='in'/>"
172 " <arg type='s' name='app_id' direction='in'/>"
173 " <arg type='i' name='type' direction='in'/>"
174 " <arg type='s' name='uri' direction='in'/>"
177 " <method name='update_sticker_thumbnail'>"
178 " <arg type='i' name='record_id' direction='in'/>"
179 " <arg type='s' name='thumbnail' direction='in'/>"
182 " <method name='update_sticker_description'>"
183 " <arg type='i' name='record_id' direction='in'/>"
184 " <arg type='s' name='description' direction='in'/>"
187 " <method name='update_sticker_group'>"
188 " <arg type='i' name='record_id' direction='in'/>"
189 " <arg type='s' name='group' direction='in'/>"
192 " <method name='update_sticker_keyword'>"
193 " <arg type='i' name='record_id' direction='in'/>"
194 " <arg type='a(s)' name='keyword' direction='in'/>"
197 " <method name='get_sticker_info'>"
198 " <arg type='i' name='record_id' direction='in'/>"
199 " <arg type='a{iv}' name='sticker_info' direction='out'/>"
200 " <arg type='a(s)' name='keyword_list' direction='out'/>"
203 " <method name='get_group_list'>"
204 " <arg type='a(s)' name='group_list' direction='out'/>"
207 " <method name='get_keyword_list'>"
208 " <arg type='a(s)' name='keyword_list' direction='out'/>"
211 " <method name='get_sticker_count'>"
212 " <arg type='s' name='app_id' direction='in'/>"
213 " <arg type='i' name='count' direction='out'/>"
216 " <method name='get_all_sticker_info'>"
217 " <arg type='i' name='offset' direction='in'/>"
218 " <arg type='i' name='count' direction='in'/>"
219 " <arg type='a(i)' name='id_list' direction='out'/>"
222 " <method name='get_sticker_info_by_appid'>"
223 " <arg type='s' name='app_id' direction='in'/>"
224 " <arg type='i' name='offset' direction='in'/>"
225 " <arg type='i' name='count' direction='in'/>"
226 " <arg type='a(i)' name='id_list' direction='out'/>"
229 " <method name='get_sticker_info_by_type'>"
230 " <arg type='i' name='type' direction='in'/>"
231 " <arg type='i' name='offset' direction='in'/>"
232 " <arg type='i' name='count' direction='in'/>"
233 " <arg type='a(i)' name='id_list' direction='out'/>"
236 " <method name='get_sticker_info_by_group'>"
237 " <arg type='s' name='group' direction='in'/>"
238 " <arg type='i' name='offset' direction='in'/>"
239 " <arg type='i' name='count' direction='in'/>"
240 " <arg type='a(i)' name='id_list' direction='out'/>"
243 " <method name='get_sticker_info_by_keyword'>"
244 " <arg type='s' name='keyword' direction='in'/>"
245 " <arg type='i' name='offset' direction='in'/>"
246 " <arg type='i' name='count' direction='in'/>"
247 " <arg type='a(i)' name='id_list' direction='out'/>"
252 return stickerd_server_register_dbus_interface(introspection_xml, _sticker_interface_vtable);
255 int stickerd_dbus_init(void)
259 ret = stickerd_register_dbus_interface();
260 if (ret != STICKERD_SERVER_ERROR_NONE) {
261 LOGE("Failed to register dbus interface : %d", ret);
262 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
265 return STICKERD_SERVER_ERROR_NONE;
268 static int _check_file_exist(const char *app_id, const char *path)
271 package_info_h package_info = NULL;
272 char *app_path = NULL;
273 char *file_path = NULL;
275 if (access(path, F_OK) == 0) {
280 ret = package_info_create(app_id, &package_info);
281 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
282 LOGE("failed to create package_info. ret: %d", ret);
287 ret = package_info_get_root_path(package_info, &app_path);
288 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
289 LOGE("failed to create package_info. ret: %d", ret);
294 int path_len = strlen(app_path) + strlen(path) + 2;
295 file_path = (char *)calloc(path_len, sizeof(char));
297 LOGE("Failed to alloc memory");
303 snprintf(file_path, path_len, "%s%s",app_path, path);
305 snprintf(file_path, path_len, "%s%s%s",app_path, "/", path);
307 if (access(file_path, F_OK) != 0) {
308 LOGE("%s does not exist", file_path);
315 package_info_destroy(package_info);
330 static int _mkdirs(const char *path, mode_t mode)
333 char prev_path[2048];
334 const char *tmp = path;
336 if (!path || strlen(path) > 2048)
339 memset(prev_path, '\0', 2048);
340 while ((tmp = strchr(tmp, '/')) != NULL) {
347 strncpy(prev_path, path, len);
348 prev_path[len] = 0x00;
350 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
351 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
352 LOGE("directory create error : %s", error_buffer);
357 if (mkdir(prev_path, mode) == -1 && errno != EEXIST) {
358 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
359 LOGE("directory create error : %s", error_buffer);
366 static int _file_copy(const char *src, const char *dest)
369 int fd = -1, n_fd = -1;
374 memset(buf, '\0', 4096);
375 fd = open(src, O_RDONLY);
376 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
378 if (fd == -1 || n_fd == -1) {
384 while((size = read(fd, buf, 4096))) {
394 while(write(n_fd, buf, size) == -1) {
415 static char* _convert_sticker_uri(const char *uri, const char *appid)
418 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 3;
419 char * new_path = (char *)calloc(len, sizeof(char));
420 if (new_path == NULL) {
421 LOGE("Failed to alloc memory");
426 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
428 snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
430 if (access(new_path, F_OK) == 0) {
431 LOGE("sticker file already exists");
436 ret = _mkdirs(new_path, 0755);
438 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
439 LOGE("directory create error : %s", error_buffer);
443 ret = _file_copy(uri, new_path);
445 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
446 LOGE("failed to copy sticker file : %s", error_buffer);
459 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
463 STICKER_DAT_TYPE key;
464 sticker_info_db *sticker_info = NULL;
465 GVariant *value = NULL;
466 GVariantIter *info_iter = NULL;
467 GVariantIter *keyword_iter = NULL;
470 g_variant_get(parameters, "(a{iv}a(s))", &info_iter, &keyword_iter);
471 if (!info_iter || !keyword_iter) {
472 LOGD("failed to get iter");
473 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
477 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
480 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
484 while (g_variant_iter_loop (info_iter, "{iv}", &key, &value)) {
486 case STICKER_DATA_TYPE_APP_ID:
487 sticker_info->app_id = (char *) g_variant_get_string(value, NULL);
489 case STICKER_DATA_TYPE_URI_TYPE:
490 sticker_info->type = g_variant_get_int32(value);
492 case STICKER_DATA_TYPE_URI:
493 sticker_info->uri = (char *) g_variant_get_string(value, NULL);
495 case STICKER_DATA_TYPE_THUMBNAIL:
496 sticker_info->thumbnail = (char *) g_variant_get_string(value, NULL);
498 case STICKER_DATA_TYPE_DESCRIPTION:
499 sticker_info->description = (char *) g_variant_get_string(value, NULL);
501 case STICKER_DATA_TYPE_GROUP:
502 sticker_info->group = (char *) g_variant_get_string(value, NULL);
509 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
510 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
513 if (sticker_info->type == 1) {
514 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
515 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
516 if (!sticker_info->uri) {
517 LOGE("failed to copy sticker file");
518 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
522 LOGE("sticker file does not exist");
523 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
528 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
529 if (ret != STICKERD_SERVER_ERROR_NONE) {
530 LOGE("Failed to insert sticker info");
531 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
535 *reply_body = g_variant_new("(i)", record_id);
536 if (*reply_body == NULL) {
537 LOGE("Failed to create reply_body");
538 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
543 g_variant_unref(value);
546 g_variant_iter_free(info_iter);
549 g_variant_iter_free(keyword_iter);
559 static char* _get_string_from_object(JsonObject *object, const char *key)
561 if (json_object_has_member(object, key) == false)
564 const char *str = json_object_get_string_member(object, key);
571 static int _get_int_from_object(JsonObject *object, const char *key)
573 if (json_object_has_member(object, key) == false)
576 int type = json_object_get_int_member(object, key);
581 int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_body, const char *sender)
583 int ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
585 sticker_info_db *sticker_info = NULL;
587 char *json_path = NULL;
588 JsonParser* parser = NULL;
589 GError* err_msg = NULL;
590 GVariant *body = NULL;
591 char *cmd = "send_insert_result";
593 *reply_body = g_variant_new("()");
594 if (*reply_body == NULL) {
595 LOGE("Failed to create reply_body");
596 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
599 g_variant_get(parameters, "(&s&s)", &app_id, &json_path);
601 if (!app_id || !json_path) {
602 LOGE("failed to get parameter");
603 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
606 SECURE_LOGD("app_id: %s, json path: %s", app_id, json_path);
608 parser = json_parser_new();
609 json_parser_load_from_file(parser, json_path, &err_msg);
611 LOGE("failed to load json file. error message: %s", err_msg->message);
612 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
616 JsonNode *root = json_parser_get_root(parser);
618 LOGE("failed to get root");
619 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
623 JsonObject *root_obj = json_node_get_object(root);
624 if (root_obj == NULL) {
625 LOGE("failed to get object");
626 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
630 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
631 if (sticker_arr == NULL) {
632 LOGE("failed to get array member");
633 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
637 int arr_len = json_array_get_length(sticker_arr);
638 for (int i = 0; i < arr_len; i++) {
639 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
640 if (info_object != NULL) {
641 sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
643 LOGE("Failed to alloc memory");
647 sticker_info->app_id = app_id;
648 sticker_info->type = _get_int_from_object(info_object, "type");
649 if (sticker_info->type < 1)
652 sticker_info->uri = _get_string_from_object(info_object, "uri");
653 if (!sticker_info->uri)
656 if (sticker_info->type == 1) {
657 if (_check_file_exist(sticker_info->app_id, sticker_info->uri) == 0) {
658 sticker_info->uri = _convert_sticker_uri(sticker_info->uri, sticker_info->app_id);
659 if (!sticker_info->uri)
664 sticker_info->group = _get_string_from_object(info_object, "group");
665 if (!sticker_info->group)
668 sticker_info->thumbnail = _get_string_from_object(info_object, "thumbnail");
669 if (!sticker_info->thumbnail)
672 sticker_info->description = _get_string_from_object(info_object, "description");
674 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
675 int keyword_arr_len = json_array_get_length(keyword_arr);
676 if (keyword_arr_len < 1)
679 for (int j = 0; j < keyword_arr_len; j++) {
680 sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)json_array_get_string_element(keyword_arr, j)));
683 ret = stickerd_db_insert_sticker_info(&record_id, sticker_info);
684 if (ret != STICKERD_SERVER_ERROR_NONE) {
685 LOGE("Failed to insert sticker info");
686 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
697 g_error_free(err_msg);
699 g_object_unref(parser);
701 body = g_variant_new("(i)", ret);
703 ret = stickerd_send_dbus_message(body, sender, cmd, STICKER_CLIENT_LIB_PROVIDER);
704 if (ret != STICKERD_SERVER_ERROR_NONE)
705 LOGE("Failed to send insert result to client");
708 g_variant_unref(body);
713 int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
718 *reply_body = g_variant_new("()");
719 if (*reply_body == NULL) {
720 LOGE("Failed to create reply_body");
721 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
724 g_variant_get(parameters, "(i)", &record_id);
726 ret = stickerd_db_delete_sticker_info(record_id);
727 if (ret != STICKERD_SERVER_ERROR_NONE) {
728 LOGE("Failed to delete sticker info");
729 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
735 int stickerd_update_sticker_type(GVariant *parameters, GVariant **reply_body)
741 *reply_body = g_variant_new("()");
742 if (*reply_body == NULL) {
743 LOGE("Failed to create reply_body");
744 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
747 g_variant_get(parameters, "(ii)", &record_id, &type);
749 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_TYPE, &type);
750 if (ret != STICKERD_SERVER_ERROR_NONE) {
751 LOGE("Failed to update sticker type");
752 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
758 int stickerd_update_sticker_uri(GVariant *parameters, GVariant **reply_body)
766 *reply_body = g_variant_new("()");
767 if (*reply_body == NULL) {
768 LOGE("Failed to create reply_body");
769 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
772 g_variant_get(parameters, "(i&si&s)", &record_id, &app_id, &type, &uri);
775 if (_check_file_exist(app_id, uri) == 0) {
776 uri = _convert_sticker_uri(uri, app_id);
778 LOGE("failed to copy sticker file");
779 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
784 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_URI, (void *)uri);
785 if (ret != STICKERD_SERVER_ERROR_NONE) {
786 LOGE("Failed to update sticker uri");
787 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
793 int stickerd_update_sticker_thumbnail(GVariant *parameters, GVariant **reply_body)
799 *reply_body = g_variant_new("()");
800 if (*reply_body == NULL) {
801 LOGE("Failed to create reply_body");
802 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
805 g_variant_get(parameters, "(i&s)", &record_id, &thumbnail);
807 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_THUMBNAIL, (void *)thumbnail);
808 if (ret != STICKERD_SERVER_ERROR_NONE) {
809 LOGE("Failed to update sticker thumbnail");
810 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
816 int stickerd_update_sticker_description(GVariant *parameters, GVariant **reply_body)
822 *reply_body = g_variant_new("()");
823 if (*reply_body == NULL) {
824 LOGE("Failed to create reply_body");
825 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
828 g_variant_get(parameters, "(i&s)", &record_id, &description);
830 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_DESCRIPTION, (void *)description);
831 if (ret != STICKERD_SERVER_ERROR_NONE) {
832 LOGE("Failed to update sticker description");
833 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
839 int stickerd_update_sticker_group(GVariant *parameters, GVariant **reply_body)
845 *reply_body = g_variant_new("()");
846 if (*reply_body == NULL) {
847 LOGE("Failed to create reply_body");
848 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
851 g_variant_get(parameters, "(i&s)", &record_id, &group);
853 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_GROUP, (void *)group);
854 if (ret != STICKERD_SERVER_ERROR_NONE) {
855 LOGE("Failed to update sticker group");
856 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
862 int stickerd_update_sticker_keyword(GVariant *parameters, GVariant **reply_body)
866 GVariantIter *keyword_iter = NULL;
867 char *keyword = NULL;
868 GList *keyword_list = NULL;
870 *reply_body = g_variant_new("()");
871 if (*reply_body == NULL) {
872 LOGE("Failed to create reply_body");
873 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
876 g_variant_get(parameters, "(ia(s))", &record_id, &keyword_iter);
879 LOGD("failed to get iter");
880 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
883 while (g_variant_iter_loop (keyword_iter, "(s)", &keyword)) {
884 keyword_list = g_list_append(keyword_list, strdup((const char *)keyword));
887 g_variant_iter_free(keyword_iter);
889 ret = stickerd_db_update_sticker_info(record_id, STICKER_DB_STICKER_KEYWORD, (void *)keyword_list);
890 if (ret != STICKERD_SERVER_ERROR_NONE) {
891 LOGE("Failed to update sticker keyword");
892 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
898 static void _set_keyword_builder(char *keyword, GVariantBuilder *keyword_builder)
901 LOGE("keyword doesn't exist");
905 g_variant_builder_add(keyword_builder, "(s)", (const char *)keyword);
908 int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
912 GVariantBuilder *info_builder;
913 GVariantBuilder *keyword_builder;
915 g_variant_get(parameters, "(i)", &record_id);
916 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
919 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
921 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
922 if (ret != STICKERD_SERVER_ERROR_NONE) {
923 LOGE("Failed to get sticker info");
925 if (sticker_info->app_id)
926 free(sticker_info->app_id);
928 if (sticker_info->uri)
929 free(sticker_info->uri);
931 if (sticker_info->thumbnail)
932 free(sticker_info->thumbnail);
934 if (sticker_info->keyword)
935 free(sticker_info->keyword);
937 if (sticker_info->group)
938 free(sticker_info->group);
940 if (sticker_info->description)
941 free(sticker_info->description);
943 if (sticker_info->date)
944 free(sticker_info->date);
948 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
951 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
952 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
953 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
954 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
955 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
956 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
957 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
958 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
960 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
961 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
963 *reply_body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
964 g_variant_builder_unref(info_builder);
965 g_variant_builder_unref(keyword_builder);
967 if (*reply_body == NULL) {
968 LOGE("Failed to create reply_body");
971 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
979 int stickerd_get_group_list(GVariant *parameters, GVariant **reply_body)
982 GVariantBuilder *builder = NULL;
984 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
985 ret = stickerd_db_get_group_list(builder);
987 if (ret != STICKERD_SERVER_ERROR_NONE) {
988 LOGE("Failed to get sticker group list");
989 g_variant_builder_unref(builder);
990 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
993 *reply_body = g_variant_new("(a(s))", builder);
994 g_variant_builder_unref(builder);
996 if (*reply_body == NULL) {
997 LOGE("Failed to create reply_body");
998 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1004 int stickerd_get_keyword_list(GVariant *parameters, GVariant **reply_body)
1007 GVariantBuilder *builder = NULL;
1009 builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1010 ret = stickerd_db_get_keyword_list(builder);
1011 if (ret != STICKERD_SERVER_ERROR_NONE) {
1012 LOGE("Failed to get sticker keyword list");
1013 g_variant_builder_unref(builder);
1014 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1017 *reply_body = g_variant_new("(a(s))", builder);
1018 g_variant_builder_unref(builder);
1020 if (*reply_body == NULL) {
1021 LOGE("Failed to create reply_body");
1022 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1028 int stickerd_get_sticker_count(GVariant *parameters, GVariant **reply_body)
1032 char *app_id = NULL;
1034 g_variant_get(parameters, "(&s)", &app_id);
1036 ret = stickerd_db_get_sticker_count(&count, app_id);
1037 if (ret != STICKERD_SERVER_ERROR_NONE) {
1038 LOGE("Failed to get sticker count");
1039 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1042 *reply_body = g_variant_new("(i)", count);
1043 if (*reply_body == NULL) {
1044 LOGE("Failed to create reply_body");
1045 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1052 // Send the sticker information by asynchronous communication.
1053 static int send_sticker_info_async(int record_id, sticker_info_db_type type, const char *sender)
1058 sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
1061 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
1063 ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
1064 if (ret != STICKERD_SERVER_ERROR_NONE) {
1065 LOGE("Failed to get sticker info");
1067 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1071 case STICKER_DB_STICKER_ALL:
1072 cmd = "send_all_sticker_info";
1074 case STICKER_DB_STICKER_APPID:
1075 cmd = "send_sticker_info_by_appid";
1077 case STICKER_DB_STICKER_TYPE:
1078 cmd = "send_sticker_info_by_type";
1080 case STICKER_DB_STICKER_GROUP:
1081 cmd = "send_sticker_info_by_group";
1083 case STICKER_DB_STICKER_KEYWORD:
1084 cmd = "send_sticker_info_by_keyword";
1091 GVariantBuilder *info_builder;
1092 GVariantBuilder *keyword_builder;
1094 info_builder = g_variant_builder_new(G_VARIANT_TYPE("a{iv}"));
1095 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_INFO_ID, g_variant_new_int32(record_id));
1096 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_APP_ID, g_variant_new_string((const gchar *)sticker_info->app_id));
1097 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI_TYPE, g_variant_new_int32(sticker_info->type));
1098 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_URI, g_variant_new_string((const gchar *)sticker_info->uri));
1099 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_THUMBNAIL, g_variant_new_string((const gchar *)sticker_info->thumbnail));
1100 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DESCRIPTION, g_variant_new_string((const gchar *)sticker_info->description));
1101 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_GROUP, g_variant_new_string((const gchar *)sticker_info->group));
1102 g_variant_builder_add(info_builder, "{iv}", STICKER_DATA_TYPE_DATE, g_variant_new_string((const gchar *)sticker_info->date));
1104 keyword_builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
1105 g_list_foreach(sticker_info->keyword, (GFunc) _set_keyword_builder, keyword_builder);
1107 GVariant *body = g_variant_new("(a{iv}a(s))", info_builder, keyword_builder);
1108 g_variant_builder_unref(info_builder);
1109 g_variant_builder_unref(keyword_builder);
1111 ret = stickerd_send_dbus_message(body, sender, cmd);
1112 if (ret != STICKERD_SERVER_ERROR_NONE) {
1113 LOGE("Failed to send sticker info to client");
1115 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1123 static void _set_id_builder(char *id, GVariantBuilder *id_builder)
1126 LOGE("id doesn't exist");
1130 g_variant_builder_add(id_builder, "(i)", atoi(id));
1133 int stickerd_get_all_sticker_info(GVariant *parameters, GVariant **reply_body)
1137 GList *id_list = NULL;
1138 GVariantBuilder *id_builder = NULL;
1140 g_variant_get(parameters, "(ii)", &offset, &count);
1142 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_ALL, &id_list, NULL, offset, count);
1143 if (ret != STICKERD_SERVER_ERROR_NONE) {
1144 LOGE("Failed to get all sticker id");
1146 g_list_free_full(id_list, free);
1147 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1150 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1151 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1153 *reply_body = g_variant_new("(a(i))", id_builder);
1154 if (*reply_body == NULL) {
1155 LOGE("Failed to create reply_body");
1156 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1160 g_list_free_full(id_list, free);
1163 g_variant_builder_unref(id_builder);
1168 int stickerd_get_sticker_info_by_app_id(GVariant *parameters, GVariant **reply_body)
1171 GList *id_list = NULL;
1172 char *app_id = NULL;
1174 GVariantBuilder *id_builder = NULL;
1176 g_variant_get(parameters, "(&sii)", &app_id, &offset, &count);
1178 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_APPID, &id_list, (void *)app_id, offset, count);
1179 if (ret != STICKERD_SERVER_ERROR_NONE) {
1180 LOGE("Failed to get all sticker id");
1182 g_list_free_full(id_list, free);
1183 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1186 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1187 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1189 *reply_body = g_variant_new("(a(i))", id_builder);
1190 if (*reply_body == NULL) {
1191 LOGE("Failed to create reply_body");
1192 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1196 g_list_free_full(id_list, free);
1199 g_variant_builder_unref(id_builder);
1204 int stickerd_get_sticker_info_by_type(GVariant *parameters, GVariant **reply_body)
1207 GList *id_list = NULL;
1208 int type, offset, count;
1209 GVariantBuilder *id_builder = NULL;
1211 g_variant_get(parameters, "(iii)", &type, &offset, &count);
1213 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_TYPE, &id_list, &type, offset, count);
1214 if (ret != STICKERD_SERVER_ERROR_NONE) {
1215 LOGE("Failed to get all sticker id");
1217 g_list_free_full(id_list, free);
1218 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1221 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1222 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1224 *reply_body = g_variant_new("(a(i))", id_builder);
1225 if (*reply_body == NULL) {
1226 LOGE("Failed to create reply_body");
1227 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1231 g_list_free_full(id_list, free);
1234 g_variant_builder_unref(id_builder);
1239 int stickerd_get_sticker_info_by_group(GVariant *parameters, GVariant **reply_body)
1242 GList *id_list = NULL;
1245 GVariantBuilder *id_builder = NULL;
1247 g_variant_get(parameters, "(&sii)", &group, &offset, &count);
1249 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_GROUP, &id_list, (void *)group, offset, count);
1250 if (ret != STICKERD_SERVER_ERROR_NONE) {
1251 LOGE("Failed to get all sticker id");
1253 g_list_free_full(id_list, free);
1254 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1257 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1258 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1260 *reply_body = g_variant_new("(a(i))", id_builder);
1261 if (*reply_body == NULL) {
1262 LOGE("Failed to create reply_body");
1263 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1267 g_list_free_full(id_list, free);
1270 g_variant_builder_unref(id_builder);
1275 int stickerd_get_sticker_info_by_keyword(GVariant *parameters, GVariant **reply_body)
1278 GList *id_list = NULL;
1279 char *keyword = NULL;
1281 GVariantBuilder *id_builder = NULL;
1283 g_variant_get(parameters, "(&sii)", &keyword, &offset, &count);
1285 ret = stickerd_db_get_record_id(STICKER_DB_STICKER_KEYWORD, &id_list, (void *)keyword, offset, count);
1286 if (ret != STICKERD_SERVER_ERROR_NONE) {
1287 LOGE("Failed to get all sticker id");
1289 g_list_free_full(id_list, free);
1290 return STICKERD_SERVER_ERROR_OPERATION_FAILED;
1293 id_builder = g_variant_builder_new(G_VARIANT_TYPE("a(i)"));
1294 g_list_foreach(id_list, (GFunc) _set_id_builder, id_builder);
1296 *reply_body = g_variant_new("(a(i))", id_builder);
1297 if (*reply_body == NULL) {
1298 LOGE("Failed to create reply_body");
1299 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
1303 g_list_free_full(id_list, free);
1306 g_variant_builder_unref(id_builder);