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;
127 EXPORT_API int sticker_data_destroy(sticker_data_h data_handle)
129 CHECK_STICKER_FEATURE();
132 return STICKER_ERROR_INVALID_PARAMETER;
134 if (data_handle->app_id) {
135 free(data_handle->app_id);
136 data_handle->app_id = NULL;
139 if (data_handle->uri) {
140 free(data_handle->uri);
141 data_handle->uri = NULL;
144 if (data_handle->thumbnail) {
145 free(data_handle->thumbnail);
146 data_handle->thumbnail = NULL;
149 if (data_handle->keyword) {
150 g_list_free_full(data_handle->keyword, free);
151 data_handle->keyword = NULL;
154 if (data_handle->group) {
155 free(data_handle->group);
156 data_handle->group = NULL;
159 if (data_handle->description) {
160 free(data_handle->description);
161 data_handle->description = NULL;
164 if (data_handle->date) {
165 free(data_handle->date);
166 data_handle->date = NULL;
172 return STICKER_ERROR_NONE;
175 EXPORT_API int sticker_data_clone(sticker_data_h origin_handle, sticker_data_h *target_handle)
177 CHECK_STICKER_FEATURE();
179 sticker_data_h handle;
180 if (!origin_handle || !target_handle)
181 return STICKER_ERROR_INVALID_PARAMETER;
183 sticker_data_create(&handle);
185 return STICKER_ERROR_OUT_OF_MEMORY;
187 handle->sticker_info_id = origin_handle->sticker_info_id;
189 if (origin_handle->app_id)
190 handle->app_id = strdup(origin_handle->app_id);
192 handle->type = origin_handle->type;
194 if (origin_handle->uri)
195 handle->uri = strdup(origin_handle->uri);
197 if (origin_handle->thumbnail)
198 handle->thumbnail = strdup(origin_handle->thumbnail);
200 if (origin_handle->keyword)
201 handle->keyword = g_list_copy_deep(origin_handle->keyword, (GCopyFunc) g_strdup, NULL);
203 if (origin_handle->group)
204 handle->group = strdup(origin_handle->group);
206 if (origin_handle->description)
207 handle->description = strdup(origin_handle->description);
209 if (origin_handle->date)
210 handle->date = strdup(origin_handle->date);
212 *target_handle = handle;
214 return STICKER_ERROR_NONE;
217 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
219 CHECK_STICKER_FEATURE();
221 if (!data_handle || !app_id)
222 return STICKER_ERROR_INVALID_PARAMETER;
224 if (!data_handle->app_id)
225 return STICKER_ERROR_OPERATION_FAILED;
227 *app_id = strdup(data_handle->app_id);
229 return STICKER_ERROR_NONE;
232 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
234 CHECK_STICKER_FEATURE();
236 char *file_path = NULL;
237 if (!data_handle || !type || !uri)
238 return STICKER_ERROR_INVALID_PARAMETER;
240 if (type == STICKER_DATA_URI_LOCAL_PATH) {
241 if (access(uri, F_OK) != 0) {
242 file_path = _make_absolute_path(uri);
243 if (file_path == NULL) {
244 return STICKER_ERROR_INVALID_PARAMETER;
247 file_path = strdup(uri);
250 if (data_handle->uri)
251 free(data_handle->uri);
253 data_handle->type = type;
254 if (type == STICKER_DATA_URI_LOCAL_PATH)
255 data_handle->uri = file_path;
257 data_handle->uri = strdup(uri);
259 return STICKER_ERROR_NONE;
262 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
264 CHECK_STICKER_FEATURE();
266 if (!data_handle || !type || !uri)
267 return STICKER_ERROR_INVALID_PARAMETER;
269 if (!data_handle->type || !data_handle->uri)
270 return STICKER_ERROR_OPERATION_FAILED;
272 *type = data_handle->type;
273 *uri = strdup(data_handle->uri);
275 return STICKER_ERROR_NONE;
278 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
280 CHECK_STICKER_FEATURE();
282 if (!data_handle || !callback)
283 return STICKER_ERROR_INVALID_PARAMETER;
285 if (!data_handle->keyword)
286 return STICKER_ERROR_OPERATION_FAILED;
289 for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
290 callback(list->data, user_data);
293 return STICKER_ERROR_NONE;
296 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
298 CHECK_STICKER_FEATURE();
301 if (!data_handle || !keyword)
302 return STICKER_ERROR_INVALID_PARAMETER;
304 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
307 LOGE("keyword already exists");
308 return STICKER_ERROR_INVALID_PARAMETER;
310 data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
313 return STICKER_ERROR_NONE;
316 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
318 CHECK_STICKER_FEATURE();
321 if (!data_handle || !keyword)
322 return STICKER_ERROR_INVALID_PARAMETER;
324 node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
327 data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
329 LOGE("keyword does not exist");
330 return STICKER_ERROR_INVALID_PARAMETER;
333 return STICKER_ERROR_NONE;
336 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
338 CHECK_STICKER_FEATURE();
340 if (!data_handle || !group)
341 return STICKER_ERROR_INVALID_PARAMETER;
343 if (data_handle->group)
344 free(data_handle->group);
346 data_handle->group = strdup(group);
348 return STICKER_ERROR_NONE;
351 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
353 CHECK_STICKER_FEATURE();
355 if (!data_handle || !group)
356 return STICKER_ERROR_INVALID_PARAMETER;
358 if (!data_handle->group)
359 return STICKER_ERROR_OPERATION_FAILED;
361 *group = strdup(data_handle->group);
363 return STICKER_ERROR_NONE;
366 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
368 CHECK_STICKER_FEATURE();
370 char *file_path = NULL;
371 if (!data_handle || !thumbnail)
372 return STICKER_ERROR_INVALID_PARAMETER;
374 if (access(thumbnail, F_OK) != 0) {
375 file_path = _make_absolute_path(thumbnail);
376 if (file_path == NULL)
377 return STICKER_ERROR_INVALID_PARAMETER;
379 file_path = strdup(thumbnail);
381 if (data_handle->thumbnail)
382 free(data_handle->thumbnail);
384 data_handle->thumbnail = file_path;
386 return STICKER_ERROR_NONE;
389 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
391 CHECK_STICKER_FEATURE();
393 if (!data_handle || !thumbnail)
394 return STICKER_ERROR_INVALID_PARAMETER;
396 if (!data_handle->thumbnail)
397 *thumbnail = strdup("");
399 *thumbnail = strdup(data_handle->thumbnail);
401 return STICKER_ERROR_NONE;
404 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
406 CHECK_STICKER_FEATURE();
408 if (!data_handle || !description)
409 return STICKER_ERROR_INVALID_PARAMETER;
411 if (data_handle->description)
412 free(data_handle->description);
414 data_handle->description = strdup(description);
416 return STICKER_ERROR_NONE;
419 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
421 CHECK_STICKER_FEATURE();
423 if (!data_handle || !description)
424 return STICKER_ERROR_INVALID_PARAMETER;
426 if (!data_handle->description)
427 *description = strdup("");
429 *description = strdup(data_handle->description);
431 return STICKER_ERROR_NONE;
434 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
436 CHECK_STICKER_FEATURE();
438 if (!data_handle || !date)
439 return STICKER_ERROR_INVALID_PARAMETER;
441 if (!data_handle->date)
442 return STICKER_ERROR_OPERATION_FAILED;
444 *date = strdup(data_handle->date);
446 return STICKER_ERROR_NONE;