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 handle->disp_type = origin_handle->disp_type;
213 *target_handle = handle;
215 return STICKER_ERROR_NONE;
218 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
220 CHECK_STICKER_FEATURE();
222 if (!data_handle || !app_id)
223 return STICKER_ERROR_INVALID_PARAMETER;
225 if (!data_handle->app_id)
226 return STICKER_ERROR_OPERATION_FAILED;
228 *app_id = strdup(data_handle->app_id);
230 return STICKER_ERROR_NONE;
233 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
235 CHECK_STICKER_FEATURE();
237 char *file_path = NULL;
238 if (!data_handle || !type || !uri)
239 return STICKER_ERROR_INVALID_PARAMETER;
241 if (type == STICKER_DATA_URI_LOCAL_PATH) {
242 if (access(uri, F_OK) != 0) {
243 file_path = _make_absolute_path(uri);
244 if (file_path == NULL) {
245 return STICKER_ERROR_INVALID_PARAMETER;
248 file_path = strdup(uri);
251 if (data_handle->uri)
252 free(data_handle->uri);
254 data_handle->type = type;
255 if (type == STICKER_DATA_URI_LOCAL_PATH)
256 data_handle->uri = file_path;
258 data_handle->uri = strdup(uri);
260 return STICKER_ERROR_NONE;
263 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
265 CHECK_STICKER_FEATURE();
267 if (!data_handle || !type || !uri)
268 return STICKER_ERROR_INVALID_PARAMETER;
270 if (!data_handle->type || !data_handle->uri)
271 return STICKER_ERROR_OPERATION_FAILED;
273 *type = data_handle->type;
274 *uri = strdup(data_handle->uri);
276 return STICKER_ERROR_NONE;
279 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
281 CHECK_STICKER_FEATURE();
283 if (!data_handle || !callback)
284 return STICKER_ERROR_INVALID_PARAMETER;
286 if (!data_handle->keyword)
287 return STICKER_ERROR_OPERATION_FAILED;
290 for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
291 callback(list->data, user_data);
294 return STICKER_ERROR_NONE;
297 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
299 CHECK_STICKER_FEATURE();
302 if (!data_handle || !keyword)
303 return STICKER_ERROR_INVALID_PARAMETER;
305 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
308 LOGE("keyword already exists");
309 return STICKER_ERROR_INVALID_PARAMETER;
311 data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
314 return STICKER_ERROR_NONE;
317 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
319 CHECK_STICKER_FEATURE();
322 if (!data_handle || !keyword)
323 return STICKER_ERROR_INVALID_PARAMETER;
325 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
328 data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
330 LOGE("keyword does not exist");
331 return STICKER_ERROR_INVALID_PARAMETER;
334 return STICKER_ERROR_NONE;
337 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
339 CHECK_STICKER_FEATURE();
341 if (!data_handle || !group)
342 return STICKER_ERROR_INVALID_PARAMETER;
344 if (data_handle->group)
345 free(data_handle->group);
347 data_handle->group = strdup(group);
349 return STICKER_ERROR_NONE;
352 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
354 CHECK_STICKER_FEATURE();
356 if (!data_handle || !group)
357 return STICKER_ERROR_INVALID_PARAMETER;
359 if (!data_handle->group)
360 return STICKER_ERROR_OPERATION_FAILED;
362 *group = strdup(data_handle->group);
364 return STICKER_ERROR_NONE;
367 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
369 CHECK_STICKER_FEATURE();
371 char *file_path = NULL;
372 if (!data_handle || !thumbnail)
373 return STICKER_ERROR_INVALID_PARAMETER;
375 if (access(thumbnail, F_OK) != 0) {
376 file_path = _make_absolute_path(thumbnail);
377 if (file_path == NULL)
378 return STICKER_ERROR_INVALID_PARAMETER;
380 file_path = strdup(thumbnail);
382 if (data_handle->thumbnail)
383 free(data_handle->thumbnail);
385 data_handle->thumbnail = file_path;
387 return STICKER_ERROR_NONE;
390 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
392 CHECK_STICKER_FEATURE();
394 if (!data_handle || !thumbnail)
395 return STICKER_ERROR_INVALID_PARAMETER;
397 if (!data_handle->thumbnail)
398 *thumbnail = strdup("");
400 *thumbnail = strdup(data_handle->thumbnail);
402 return STICKER_ERROR_NONE;
405 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
407 CHECK_STICKER_FEATURE();
409 if (!data_handle || !description)
410 return STICKER_ERROR_INVALID_PARAMETER;
412 if (data_handle->description)
413 free(data_handle->description);
415 data_handle->description = strdup(description);
417 return STICKER_ERROR_NONE;
420 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
422 CHECK_STICKER_FEATURE();
424 if (!data_handle || !description)
425 return STICKER_ERROR_INVALID_PARAMETER;
427 if (!data_handle->description)
428 *description = strdup("");
430 *description = strdup(data_handle->description);
432 return STICKER_ERROR_NONE;
435 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
437 CHECK_STICKER_FEATURE();
439 if (!data_handle || !date)
440 return STICKER_ERROR_INVALID_PARAMETER;
442 if (!data_handle->date)
443 return STICKER_ERROR_OPERATION_FAILED;
445 *date = strdup(data_handle->date);
447 return STICKER_ERROR_NONE;
450 EXPORT_API int sticker_data_set_display_type(sticker_data_h data_handle, sticker_data_display_type_e type)
452 CHECK_STICKER_FEATURE();
454 if (!data_handle || !type || type < STICKER_DATA_DISP_EMOJI || type > STICKER_DATA_DISP_WALLPAPER)
455 return STICKER_ERROR_INVALID_PARAMETER;
457 data_handle->disp_type = type;
459 return STICKER_ERROR_NONE;
462 EXPORT_API int sticker_data_get_display_type(sticker_data_h data_handle, sticker_data_display_type_e *type)
464 CHECK_STICKER_FEATURE();
466 if (!data_handle || !type)
467 return STICKER_ERROR_INVALID_PARAMETER;
469 *type = data_handle->disp_type;
471 return STICKER_ERROR_NONE;