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)
76 CHECK_STICKER_FEATURE();
80 return STICKER_ERROR_INVALID_PARAMETER;
82 struct sticker_provider_s *provider_struct = (sticker_provider_h)calloc(1, sizeof(struct sticker_provider_s));
85 return STICKER_ERROR_OUT_OF_MEMORY;
87 ret = sticker_dbus_init(&provider_struct->gdbus_connection, &provider_struct->server_watcher_id,
88 &provider_struct->monitor_id, &provider_struct->server_monitor_id, STICKER_CLIENT_LIB_PROVIDER, (void *)provider_struct);
89 if (ret != STICKER_ERROR_NONE) {
90 LOGE("Failed to initialize dbus : %d", ret);
91 free(provider_struct);
92 return STICKER_ERROR_OPERATION_FAILED;
95 *provider_handle = provider_struct;
97 return STICKER_ERROR_NONE;
100 EXPORT_API int sticker_provider_destroy(sticker_provider_h provider_handle)
102 CHECK_STICKER_FEATURE();
105 if (!provider_handle)
106 return STICKER_ERROR_INVALID_PARAMETER;
108 LOGD("provider_handle : %p", provider_handle);
109 ret = sticker_dbus_shutdown(provider_handle->gdbus_connection, &provider_handle->server_watcher_id,
110 &provider_handle->server_monitor_id, &provider_handle->monitor_id);
111 if (ret != STICKER_ERROR_NONE) {
112 LOGE("Failed to finalize dbus : %d", ret);
113 free(provider_handle);
114 return STICKER_ERROR_OPERATION_FAILED;
117 if (provider_handle->gdbus_connection)
118 g_object_unref(provider_handle->gdbus_connection);
120 free(provider_handle);
122 return STICKER_ERROR_NONE;
125 EXPORT_API int sticker_provider_insert_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
127 CHECK_STICKER_FEATURE();
132 if (!provider_handle || !data_handle || (data_handle->sticker_info_id > 0) || !data_handle->uri)
133 return STICKER_ERROR_INVALID_PARAMETER;
135 ret = sticker_dbus_check_file_exists(provider_handle->gdbus_connection, data_handle->uri, &is_exist);
136 if (ret != STICKER_ERROR_NONE) {
137 LOGE("Failed to check file exists : %d", ret);
138 return STICKER_ERROR_OPERATION_FAILED;
142 LOGE("Sticker already exists");
143 return STICKER_ERROR_FILE_EXISTS;
146 ret = sticker_dbus_insert_sticker_info(provider_handle->gdbus_connection, data_handle);
147 if (ret != STICKER_ERROR_NONE) {
148 LOGE("Failed to insert sticker information : %d", ret);
149 if (ret == STICKER_CLIENT_ERROR_NO_SUCH_FILE)
150 return STICKER_ERROR_NO_SUCH_FILE;
152 return STICKER_ERROR_OPERATION_FAILED;
155 return STICKER_ERROR_NONE;
158 EXPORT_API int sticker_provider_insert_data_by_json_file(sticker_provider_h provider_handle, const char *json_path, sticker_provider_insert_finished_cb callback, void *user_data)
160 CHECK_STICKER_FEATURE();
164 package_info_h package_info = NULL;
165 char *app_path = NULL;
166 char *file_path = NULL;
168 if (!provider_handle || !json_path || !callback)
169 return STICKER_ERROR_INVALID_PARAMETER;
171 ret = app_get_id(&app_id);
172 if (ret != APP_ERROR_NONE) {
173 LOGE("Failed to get app_id : %d", ret);
174 ret = STICKER_ERROR_OPERATION_FAILED;
178 if (access(json_path, F_OK) != 0) {
179 ret = package_info_create(app_id, &package_info);
180 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
181 LOGE("failed to create package_info. ret: %d", ret);
182 ret = STICKER_ERROR_OPERATION_FAILED;
186 ret = package_info_get_root_path(package_info, &app_path);
187 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
188 LOGE("failed to create package_info. ret: %d", ret);
189 ret = STICKER_ERROR_OPERATION_FAILED;
193 int path_len = strlen(app_path) + strlen(json_path) + 2;
194 file_path = (char *)calloc(path_len, sizeof(char));
196 LOGE("failed to alloc memory");
197 ret = STICKER_ERROR_OPERATION_FAILED;
201 if(json_path[0] == '/')
202 snprintf(file_path, path_len, "%s%s",app_path, json_path);
204 snprintf(file_path, path_len, "%s%s%s",app_path, "/", json_path);
206 if (access(file_path, F_OK) != 0) {
207 LOGE("%s does not exist", file_path);
208 ret = STICKER_ERROR_INVALID_PARAMETER;
212 file_path = strdup(json_path);
214 SECURE_LOGD("json path : %s", file_path);
215 ret = sticker_dbus_insert_sticker_info_by_json(provider_handle->gdbus_connection, app_id, file_path);
216 if (ret != STICKER_ERROR_NONE) {
217 LOGE("Failed to load json file : %d", ret);
218 ret = STICKER_ERROR_OPERATION_FAILED;
222 provider_handle->insert_finished_cb = callback;
223 provider_handle->insert_finished_cb_user_data = user_data;
230 package_info_destroy(package_info);
241 EXPORT_API int sticker_provider_update_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
243 CHECK_STICKER_FEATURE();
246 if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
247 return STICKER_ERROR_INVALID_PARAMETER;
249 ret = sticker_dbus_update_sticker_info(provider_handle->gdbus_connection, data_handle);
250 if (ret != STICKER_ERROR_NONE) {
251 LOGE("Failed to update sticker information : %d", ret);
252 if (ret == STICKER_CLIENT_ERROR_FILE_EXISTS)
253 return STICKER_ERROR_FILE_EXISTS;
254 else if (ret == STICKER_CLIENT_ERROR_NO_SUCH_FILE)
255 return STICKER_ERROR_NO_SUCH_FILE;
257 return STICKER_ERROR_OPERATION_FAILED;
260 return STICKER_ERROR_NONE;
263 EXPORT_API int sticker_provider_delete_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
265 CHECK_STICKER_FEATURE();
268 if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
269 return STICKER_ERROR_INVALID_PARAMETER;
271 ret = sticker_dbus_delete_sticker_info(provider_handle->gdbus_connection, data_handle->sticker_info_id);
272 if (ret != STICKER_ERROR_NONE) {
273 LOGE("Failed to delete sticker information : %d", ret);
274 return STICKER_ERROR_OPERATION_FAILED;
277 return STICKER_ERROR_NONE;
280 EXPORT_API int sticker_provider_get_sticker_count(sticker_provider_h provider_handle, int *count)
282 CHECK_STICKER_FEATURE();
287 if (!provider_handle || !count)
288 return STICKER_ERROR_INVALID_PARAMETER;
290 ret = app_get_id(&app_id);
291 if (ret != APP_ERROR_NONE) {
292 LOGE("Failed to get app_id : %d", ret);
293 ret = STICKER_ERROR_OPERATION_FAILED;
297 ret = sticker_dbus_get_sticker_count(provider_handle->gdbus_connection, app_id, count);
298 if (ret != STICKER_ERROR_NONE) {
299 LOGE("Failed to get sticker count : %d", ret);
300 ret = STICKER_ERROR_OPERATION_FAILED;
310 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)
312 CHECK_STICKER_FEATURE();
316 int sticker_count = 0;
318 GVariantIter *id_iter = NULL;
320 if (!provider_handle || (offset < 0) || (count <= 0) || !result || !callback)
321 return STICKER_ERROR_INVALID_PARAMETER;
323 ret = app_get_id(&app_id);
324 if (ret != APP_ERROR_NONE) {
325 LOGE("Failed to get app_id : %d", ret);
326 ret = STICKER_ERROR_OPERATION_FAILED;
330 ret = sticker_dbus_get_sticker_info_by_appid(provider_handle->gdbus_connection, app_id, offset, count, &id_iter);
331 if (ret != STICKER_ERROR_NONE) {
332 LOGE("Failed to get sticker information : %d", ret);
333 ret = STICKER_ERROR_OPERATION_FAILED;
338 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
339 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
341 ret = STICKER_ERROR_OUT_OF_MEMORY;
345 ret = sticker_dbus_get_sticker_info_by_record_id(provider_handle->gdbus_connection, sticker_data, info_id);
346 if (ret == STICKER_ERROR_NONE) {
348 callback(sticker_data, user_data);
349 _free_sticker_data(sticker_data);
351 _free_sticker_data(sticker_data);
357 *result = sticker_count;
364 g_variant_iter_free(id_iter);