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_data.h"
25 #include "sticker_dbus.h"
30 #define LOG_TAG "STICKER_DATA"
32 static char* _make_absolute_path(const char *relative_path)
34 int ret = STICKER_ERROR_NONE;
36 package_info_h package_info = NULL;
37 char *app_path = NULL;
38 char *file_path = NULL;
40 ret = app_get_id(&app_id);
41 if (ret != APP_ERROR_NONE) {
42 LOGE("Failed to get app_id : %d", ret);
43 ret = STICKER_ERROR_OPERATION_FAILED;
47 ret = package_info_create(app_id, &package_info);
48 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
49 LOGE("failed to create package_info. ret: %d", ret);
50 ret = STICKER_ERROR_OPERATION_FAILED;
54 ret = package_info_get_root_path(package_info, &app_path);
55 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
56 LOGE("failed to create package_info. ret: %d", ret);
57 ret = STICKER_ERROR_OPERATION_FAILED;
61 int path_len = strlen(app_path) + strlen(relative_path) + 2;
62 file_path = (char *)calloc(path_len, sizeof(char));
64 LOGE("failed to alloc memory");
65 ret = STICKER_ERROR_OPERATION_FAILED;
69 if(relative_path[0] == '/')
70 snprintf(file_path, path_len, "%s%s",app_path, relative_path);
72 snprintf(file_path, path_len, "%s%s%s",app_path, "/", relative_path);
74 if (access(file_path, F_OK) != 0) {
75 LOGE("%s does not exist", file_path);
76 ret = STICKER_ERROR_OPERATION_FAILED;
85 package_info_destroy(package_info);
90 if (ret == STICKER_ERROR_NONE)
96 EXPORT_API int sticker_data_create(sticker_data_h *data_handle)
98 CHECK_STICKER_FEATURE();
101 return STICKER_ERROR_INVALID_PARAMETER;
103 sticker_data_h data_struct = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
106 return STICKER_ERROR_OUT_OF_MEMORY;
109 int ret = app_get_id(&app_id);
110 if (ret != APP_ERROR_NONE) {
111 LOGE("Failed to get app_id : %d", ret);
113 ret = STICKER_ERROR_OPERATION_FAILED;
117 data_struct->app_id = strdup(app_id);
118 *data_handle = data_struct;
126 EXPORT_API int sticker_data_destroy(sticker_data_h data_handle)
128 CHECK_STICKER_FEATURE();
131 return STICKER_ERROR_INVALID_PARAMETER;
133 if (data_handle->app_id) {
134 free(data_handle->app_id);
135 data_handle->app_id = NULL;
138 if (data_handle->uri) {
139 free(data_handle->uri);
140 data_handle->uri = NULL;
143 if (data_handle->thumbnail) {
144 free(data_handle->thumbnail);
145 data_handle->thumbnail = NULL;
148 if (data_handle->keyword) {
149 g_list_free_full(data_handle->keyword, free);
150 data_handle->keyword = NULL;
153 if (data_handle->group) {
154 free(data_handle->group);
155 data_handle->group = NULL;
158 if (data_handle->description) {
159 free(data_handle->description);
160 data_handle->description = NULL;
163 if (data_handle->date) {
164 free(data_handle->date);
165 data_handle->date = NULL;
171 return STICKER_ERROR_NONE;
174 EXPORT_API int sticker_data_clone(sticker_data_h origin_handle, sticker_data_h *target_handle)
176 CHECK_STICKER_FEATURE();
178 sticker_data_h handle;
179 if (!origin_handle || !target_handle)
180 return STICKER_ERROR_INVALID_PARAMETER;
182 sticker_data_create(&handle);
184 return STICKER_ERROR_OUT_OF_MEMORY;
186 handle->sticker_info_id = origin_handle->sticker_info_id;
188 if (origin_handle->app_id)
189 handle->app_id = strdup(origin_handle->app_id);
191 handle->type = origin_handle->type;
193 if (origin_handle->uri)
194 handle->uri = strdup(origin_handle->uri);
196 if (origin_handle->thumbnail)
197 handle->thumbnail = strdup(origin_handle->thumbnail);
199 if (origin_handle->keyword)
200 handle->keyword = g_list_copy_deep(origin_handle->keyword, (GCopyFunc) g_strdup, NULL);
202 if (origin_handle->group)
203 handle->group = strdup(origin_handle->group);
205 if (origin_handle->description)
206 handle->description = strdup(origin_handle->description);
208 if (origin_handle->date)
209 handle->date = strdup(origin_handle->date);
211 *target_handle = handle;
213 return STICKER_ERROR_NONE;
216 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
218 CHECK_STICKER_FEATURE();
220 if (!data_handle || !app_id)
221 return STICKER_ERROR_INVALID_PARAMETER;
223 if (!data_handle->app_id)
224 return STICKER_ERROR_OPERATION_FAILED;
226 *app_id = strdup(data_handle->app_id);
228 return STICKER_ERROR_NONE;
231 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
233 CHECK_STICKER_FEATURE();
235 char *file_path = NULL;
236 if (!data_handle || !type || !uri)
237 return STICKER_ERROR_INVALID_PARAMETER;
239 if (type == STICKER_DATA_URI_LOCAL_PATH) {
240 if (access(uri, F_OK) != 0) {
241 file_path = _make_absolute_path(uri);
242 if (file_path == NULL) {
243 return STICKER_ERROR_INVALID_PARAMETER;
246 file_path = strdup(uri);
249 if (data_handle->uri)
250 free(data_handle->uri);
252 data_handle->type = type;
253 if (type == STICKER_DATA_URI_LOCAL_PATH)
254 data_handle->uri = file_path;
256 data_handle->uri = strdup(uri);
258 return STICKER_ERROR_NONE;
261 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
263 CHECK_STICKER_FEATURE();
265 if (!data_handle || !type || !uri)
266 return STICKER_ERROR_INVALID_PARAMETER;
268 if (!data_handle->type || !data_handle->uri)
269 return STICKER_ERROR_OPERATION_FAILED;
271 *type = data_handle->type;
272 *uri = strdup(data_handle->uri);
274 return STICKER_ERROR_NONE;
277 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
279 CHECK_STICKER_FEATURE();
281 if (!data_handle || !callback)
282 return STICKER_ERROR_INVALID_PARAMETER;
284 if (!data_handle->keyword)
285 return STICKER_ERROR_OPERATION_FAILED;
288 for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
289 callback(list->data, user_data);
292 return STICKER_ERROR_NONE;
295 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
297 CHECK_STICKER_FEATURE();
300 if (!data_handle || !keyword)
301 return STICKER_ERROR_INVALID_PARAMETER;
303 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
306 LOGE("keyword already exists");
307 return STICKER_ERROR_INVALID_PARAMETER;
309 data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
312 return STICKER_ERROR_NONE;
315 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
317 CHECK_STICKER_FEATURE();
320 if (!data_handle || !keyword)
321 return STICKER_ERROR_INVALID_PARAMETER;
323 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
326 data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
328 LOGE("keyword does not exist");
329 return STICKER_ERROR_INVALID_PARAMETER;
332 return STICKER_ERROR_NONE;
335 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
337 CHECK_STICKER_FEATURE();
339 if (!data_handle || !group)
340 return STICKER_ERROR_INVALID_PARAMETER;
342 if (data_handle->group)
343 free(data_handle->group);
345 data_handle->group = strdup(group);
347 return STICKER_ERROR_NONE;
350 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
352 CHECK_STICKER_FEATURE();
354 if (!data_handle || !group)
355 return STICKER_ERROR_INVALID_PARAMETER;
357 if (!data_handle->group)
358 return STICKER_ERROR_OPERATION_FAILED;
360 *group = strdup(data_handle->group);
362 return STICKER_ERROR_NONE;
365 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
367 CHECK_STICKER_FEATURE();
369 char *file_path = NULL;
370 if (!data_handle || !thumbnail)
371 return STICKER_ERROR_INVALID_PARAMETER;
373 if (access(thumbnail, F_OK) != 0) {
374 file_path = _make_absolute_path(thumbnail);
375 if (file_path == NULL)
376 return STICKER_ERROR_INVALID_PARAMETER;
378 file_path = strdup(thumbnail);
380 if (data_handle->thumbnail)
381 free(data_handle->thumbnail);
383 data_handle->thumbnail = file_path;
385 return STICKER_ERROR_NONE;
388 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
390 CHECK_STICKER_FEATURE();
392 if (!data_handle || !thumbnail)
393 return STICKER_ERROR_INVALID_PARAMETER;
395 if (!data_handle->thumbnail)
396 *thumbnail = strdup("");
398 *thumbnail = strdup(data_handle->thumbnail);
400 return STICKER_ERROR_NONE;
403 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
405 CHECK_STICKER_FEATURE();
407 if (!data_handle || !description)
408 return STICKER_ERROR_INVALID_PARAMETER;
410 if (data_handle->description)
411 free(data_handle->description);
413 data_handle->description = strdup(description);
415 return STICKER_ERROR_NONE;
418 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
420 CHECK_STICKER_FEATURE();
422 if (!data_handle || !description)
423 return STICKER_ERROR_INVALID_PARAMETER;
425 if (!data_handle->description)
426 *description = strdup("");
428 *description = strdup(data_handle->description);
430 return STICKER_ERROR_NONE;
433 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
435 CHECK_STICKER_FEATURE();
437 if (!data_handle || !date)
438 return STICKER_ERROR_INVALID_PARAMETER;
440 if (!data_handle->date)
441 return STICKER_ERROR_OPERATION_FAILED;
443 *date = strdup(data_handle->date);
445 return STICKER_ERROR_NONE;