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.
21 #include <app_common.h>
22 #include <package_manager.h>
24 #include "sticker_provider.h"
25 #include "sticker_provider_main.h"
26 #include "sticker_dbus.h"
31 #define LOG_TAG "STICKER_PROVIDER"
33 static void _free_sticker_data(sticker_data_h sticker_data)
35 if (sticker_data->app_id) {
36 free(sticker_data->app_id);
37 sticker_data->app_id = NULL;
40 if (sticker_data->uri) {
41 free(sticker_data->uri);
42 sticker_data->uri = NULL;
45 if (sticker_data->thumbnail) {
46 free(sticker_data->thumbnail);
47 sticker_data->thumbnail = NULL;
50 if (sticker_data->keyword) {
51 g_list_free_full(sticker_data->keyword, free);
52 sticker_data->keyword = NULL;
55 if (sticker_data->group) {
56 free(sticker_data->group);
57 sticker_data->group = NULL;
60 if (sticker_data->description) {
61 free(sticker_data->description);
62 sticker_data->description = NULL;
65 if (sticker_data->date) {
66 free(sticker_data->date);
67 sticker_data->date = NULL;
74 EXPORT_API int sticker_provider_create(sticker_provider_h *provider_handle)
79 return STICKER_ERROR_INVALID_PARAMETER;
81 struct sticker_provider_s *provider_struct = (sticker_provider_h)calloc(1, sizeof(struct sticker_provider_s));
84 return STICKER_ERROR_OUT_OF_MEMORY;
86 ret = sticker_dbus_init(&provider_struct->gdbus_connection, &provider_struct->server_watcher_id,
87 &provider_struct->monitor_id, &provider_struct->server_monitor_id, STICKER_CLIENT_LIB_PROVIDER, (void *)provider_struct);
88 if (ret != STICKER_ERROR_NONE) {
89 LOGE("Failed to initialize dbus : %d", ret);
90 free(provider_struct);
91 return STICKER_ERROR_OPERATION_FAILED;
94 *provider_handle = provider_struct;
96 return STICKER_ERROR_NONE;
99 EXPORT_API int sticker_provider_destroy(sticker_provider_h provider_handle)
101 LOGD("provider_handle : %p", provider_handle);
104 if (!provider_handle)
105 return STICKER_ERROR_INVALID_PARAMETER;
107 ret = sticker_dbus_shutdown(provider_handle->gdbus_connection, &provider_handle->server_monitor_id, &provider_handle->monitor_id);
108 if (ret != STICKER_ERROR_NONE) {
109 LOGE("Failed to finalize dbus : %d", ret);
110 free(provider_handle);
111 return STICKER_ERROR_OPERATION_FAILED;
114 if (provider_handle->gdbus_connection)
115 g_object_unref(provider_handle->gdbus_connection);
117 free(provider_handle);
119 return STICKER_ERROR_NONE;
122 EXPORT_API int sticker_provider_insert_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
126 if (!provider_handle || !data_handle || (data_handle->sticker_info_id > 0))
127 return STICKER_ERROR_INVALID_PARAMETER;
129 ret = sticker_dbus_insert_sticker_info(provider_handle->gdbus_connection, data_handle);
130 if (ret != STICKER_ERROR_NONE) {
131 LOGE("Failed to insert sticker information : %d", ret);
132 return STICKER_ERROR_OPERATION_FAILED;
135 return STICKER_ERROR_NONE;
138 EXPORT_API int sticker_privider_insert_data_by_json_file(sticker_provider_h provider_handle, const char *json_path, sticker_provider_insert_finished_cb callback, void *user_data)
142 package_info_h package_info = NULL;
143 char *app_path = NULL;
144 char *file_path = NULL;
146 if (!provider_handle || !json_path || !callback)
147 return STICKER_ERROR_INVALID_PARAMETER;
149 ret = app_get_id(&app_id);
150 if (ret != APP_ERROR_NONE) {
151 LOGE("Failed to get app_id : %d", ret);
152 ret = STICKER_ERROR_OPERATION_FAILED;
156 if (access(json_path, F_OK) != 0) {
157 ret = package_info_create(app_id, &package_info);
158 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
159 LOGE("faild to create package_info. ret: %d", ret);
160 ret = STICKER_ERROR_OPERATION_FAILED;
164 ret = package_info_get_root_path(package_info, &app_path);
165 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
166 LOGE("faild to create package_info. ret: %d", ret);
167 ret = STICKER_ERROR_OPERATION_FAILED;
171 int path_len = strlen(app_path) + strlen(json_path) + 2;
172 file_path = (char *)calloc(path_len, sizeof(char));
174 LOGE("failed to alloc memory");
175 ret = STICKER_ERROR_OPERATION_FAILED;
179 if(json_path[0] == '/')
180 snprintf(file_path, path_len, "%s%s",app_path, json_path);
182 snprintf(file_path, path_len, "%s%s%s",app_path, "/", json_path);
184 if (access(file_path, F_OK) != 0) {
185 LOGE("%s does not exist", file_path);
186 ret = STICKER_ERROR_INVALID_PARAMETER;
190 file_path = strdup(json_path);
192 SECURE_LOGD("json path : %s", file_path);
193 ret = sticker_dbus_insert_sticker_info_by_json(provider_handle->gdbus_connection, app_id, file_path);
194 if (ret != STICKER_ERROR_NONE) {
195 LOGE("Failed to load json file : %d", ret);
196 return STICKER_ERROR_OPERATION_FAILED;
199 provider_handle->insert_finished_cb = callback;
200 provider_handle->insert_finished_cb_user_data = user_data;
207 package_info_destroy(package_info);
218 EXPORT_API int sticker_provider_update_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
222 if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
223 return STICKER_ERROR_INVALID_PARAMETER;
225 ret = sticker_dbus_update_sticker_info(provider_handle->gdbus_connection, data_handle);
226 if (ret != STICKER_ERROR_NONE) {
227 LOGE("Failed to update sticker information : %d", ret);
228 return STICKER_ERROR_OPERATION_FAILED;
231 return STICKER_ERROR_NONE;
234 EXPORT_API int sticker_provider_delete_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
238 if (!provider_handle || (data_handle->sticker_info_id <= 0))
239 return STICKER_ERROR_INVALID_PARAMETER;
241 ret = sticker_dbus_delete_sticker_info(provider_handle->gdbus_connection, data_handle->sticker_info_id);
242 if (ret != STICKER_ERROR_NONE) {
243 LOGE("Failed to delete sticker information : %d", ret);
244 return STICKER_ERROR_OPERATION_FAILED;
247 return STICKER_ERROR_NONE;
250 EXPORT_API int sticker_provider_get_sticker_count(sticker_provider_h provider_handle, int *count)
255 if (!provider_handle)
256 return STICKER_ERROR_INVALID_PARAMETER;
258 ret = app_get_id(&app_id);
259 if (ret != APP_ERROR_NONE) {
260 LOGE("Failed to get app_id : %d", ret);
261 ret = STICKER_ERROR_OPERATION_FAILED;
265 ret = sticker_dbus_get_sticker_count(provider_handle->gdbus_connection, app_id, count);
266 if (ret != STICKER_ERROR_NONE) {
267 LOGE("Failed to get sticker count : %d", ret);
268 ret = STICKER_ERROR_OPERATION_FAILED;
278 EXPORT_API int sticker_provider_data_foreach_all(sticker_provider_h provider_handle, int offset, int count, int *result, sticker_provider_data_foreach_cb callback, void *user_data)
282 int sticker_count = 0;
284 GVariantIter *id_iter = NULL;
286 if (!provider_handle || (offset < 0) || (count <= 0) || !result || !callback)
287 return STICKER_ERROR_INVALID_PARAMETER;
289 ret = app_get_id(&app_id);
290 if (ret != APP_ERROR_NONE) {
291 LOGE("Failed to get app_id : %d", ret);
292 ret = STICKER_ERROR_OPERATION_FAILED;
296 ret = sticker_dbus_get_sticker_info_by_appid(provider_handle->gdbus_connection, app_id, offset, count, &id_iter);
297 if (ret != STICKER_ERROR_NONE) {
298 LOGE("Failed to get sticker information : %d", ret);
299 ret = STICKER_ERROR_OPERATION_FAILED;
304 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
305 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
307 ret = STICKER_ERROR_OUT_OF_MEMORY;
311 ret = sticker_dbus_get_sticker_info_by_record_id(provider_handle->gdbus_connection, sticker_data, info_id);
312 if (ret == STICKER_ERROR_NONE) {
314 callback(sticker_data, user_data);
315 _free_sticker_data(sticker_data);
317 _free_sticker_data(sticker_data);
323 *result = sticker_count;
330 g_variant_iter_free(id_iter);