Initial commit
[platform/core/uifw/capi-ui-sticker.git] / client / sticker_data.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <dlog.h>
21 #include <app_common.h>
22 #include <package_manager.h>
23
24 #include "sticker_data.h"
25 #include "sticker_dbus.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30 #define LOG_TAG "STICKER_DATA"
31
32 static char* _make_absolute_path(const char *relative_path)
33 {
34     int ret = STICKER_ERROR_NONE;
35     char *app_id = NULL;
36     package_info_h package_info = NULL;
37     char *app_path = NULL;
38     char *file_path = NULL;
39
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;
44         goto cleanup;
45     }
46
47     ret = package_info_create(app_id, &package_info);
48     if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
49         LOGE("faild to create package_info. ret: %d", ret);
50         ret = STICKER_ERROR_OPERATION_FAILED;
51         goto cleanup;
52     }
53
54     ret = package_info_get_root_path(package_info, &app_path);
55     if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
56         LOGE("faild to create package_info. ret: %d", ret);
57         ret = STICKER_ERROR_OPERATION_FAILED;
58         goto cleanup;
59     }
60
61     int path_len = strlen(app_path) + strlen(relative_path) + 2;
62     file_path = (char *)calloc(path_len, sizeof(char));
63     if (!file_path) {
64         LOGE("failed to alloc memory");
65         ret = STICKER_ERROR_OPERATION_FAILED;
66         goto cleanup;
67     }
68
69     if(relative_path[0] == '/')
70         snprintf(file_path, path_len, "%s%s",app_path, relative_path);
71     else
72         snprintf(file_path, path_len, "%s%s%s",app_path, "/", relative_path);
73
74     if (access(file_path, F_OK) != 0) {
75         LOGE("%s does not exist", file_path);
76         ret = STICKER_ERROR_OPERATION_FAILED;
77         free(file_path);
78     }
79
80 cleanup:
81     if (app_id)
82         free(app_id);
83
84     if (package_info)
85         package_info_destroy(package_info);
86
87     if (app_path)
88         free(app_path);
89
90     if (ret == STICKER_ERROR_NONE)
91         return file_path;
92     else
93         return NULL;
94 }
95
96 EXPORT_API int sticker_data_create(sticker_data_h *data_handle)
97 {
98     if (!data_handle)
99         return STICKER_ERROR_INVALID_PARAMETER;
100
101     sticker_data_h data_struct = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
102
103     if (!data_struct)
104         return STICKER_ERROR_OUT_OF_MEMORY;
105
106     char *app_id = NULL;
107     int ret = app_get_id(&app_id);
108     if (ret != APP_ERROR_NONE) {
109         LOGE("Failed to get app_id : %d", ret);
110         free(data_struct);
111         ret = STICKER_ERROR_OPERATION_FAILED;
112         goto cleanup;
113     }
114
115     data_struct->app_id = strdup(app_id);
116     *data_handle = data_struct;
117
118 cleanup:
119     if (app_id)
120         free(app_id);
121
122     return ret;
123 }
124
125 EXPORT_API int sticker_data_destroy(sticker_data_h data_handle)
126 {
127     if (!data_handle)
128         return STICKER_ERROR_INVALID_PARAMETER;
129
130     if (data_handle->app_id) {
131         free(data_handle->app_id);
132         data_handle->app_id = NULL;
133     }
134
135     if (data_handle->uri) {
136         free(data_handle->uri);
137         data_handle->uri = NULL;
138     }
139
140     if (data_handle->thumbnail) {
141         free(data_handle->thumbnail);
142         data_handle->thumbnail = NULL;
143     }
144
145     if (data_handle->keyword) {
146         g_list_free_full(data_handle->keyword, free);
147         data_handle->keyword = NULL;
148     }
149
150     if (data_handle->group) {
151         free(data_handle->group);
152         data_handle->group = NULL;
153     }
154
155     if (data_handle->description) {
156         free(data_handle->description);
157         data_handle->description = NULL;
158     }
159
160     if (data_handle->date) {
161         free(data_handle->date);
162         data_handle->date = NULL;
163     }
164
165     free(data_handle);
166     data_handle = NULL;
167
168     return STICKER_ERROR_NONE;
169 }
170
171 EXPORT_API int sticker_data_clone(sticker_data_h origin_handle, sticker_data_h *target_handle)
172 {
173     sticker_data_h handle;
174
175     if (!origin_handle || !target_handle)
176         return STICKER_ERROR_INVALID_PARAMETER;
177
178     sticker_data_create(&handle);
179     if (!handle)
180         return STICKER_ERROR_OUT_OF_MEMORY;
181
182     handle->sticker_info_id = origin_handle->sticker_info_id;
183
184     if (origin_handle->app_id)
185         handle->app_id = strdup(origin_handle->app_id);
186
187     handle->type = origin_handle->type;
188
189     if (origin_handle->uri)
190         handle->uri = strdup(origin_handle->uri);
191
192     if (origin_handle->thumbnail)
193         handle->thumbnail = strdup(origin_handle->thumbnail);
194
195     if (origin_handle->keyword)
196         handle->keyword = g_list_copy_deep(origin_handle->keyword, (GCopyFunc) g_strdup, NULL);
197
198     if (origin_handle->group)
199         handle->group = strdup(origin_handle->group);
200
201     if (origin_handle->description)
202         handle->description = strdup(origin_handle->description);
203
204     if (origin_handle->date)
205         handle->date = strdup(origin_handle->date);
206
207     *target_handle = handle;
208
209     return STICKER_ERROR_NONE;
210 }
211
212 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
213 {
214     if (!data_handle || !app_id)
215         return STICKER_ERROR_INVALID_PARAMETER;
216
217     if (!data_handle->app_id)
218         return STICKER_ERROR_OPERATION_FAILED;
219
220     *app_id = strdup(data_handle->app_id);
221
222     return STICKER_ERROR_NONE;
223 }
224
225 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
226 {
227     char *file_path = NULL;
228
229     if (!data_handle || !type || !uri)
230         return STICKER_ERROR_INVALID_PARAMETER;
231
232     if (type == STICKER_DATA_URI_LOCAL_PATH) {
233         if (access(uri, F_OK) != 0) {
234             file_path = _make_absolute_path(uri);
235             if (file_path == NULL) {
236                 return STICKER_ERROR_INVALID_PARAMETER;
237             }
238         } else
239             file_path = strdup(uri);
240     }
241
242     if (data_handle->uri)
243         free(data_handle->uri);
244
245     data_handle->type = type;
246     if (type == STICKER_DATA_URI_LOCAL_PATH)
247         data_handle->uri = file_path;
248     else
249         data_handle->uri = strdup(uri);
250
251     return STICKER_ERROR_NONE;
252 }
253
254 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
255 {
256     if (!data_handle || !type || !uri)
257         return STICKER_ERROR_INVALID_PARAMETER;
258
259     if (!data_handle->type || !data_handle->uri)
260         return STICKER_ERROR_OPERATION_FAILED;
261
262     *type = data_handle->type;
263     *uri = strdup(data_handle->uri);
264
265     return STICKER_ERROR_NONE;
266 }
267
268 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
269 {
270     if (!data_handle || !callback)
271         return STICKER_ERROR_INVALID_PARAMETER;
272
273     if (!data_handle->keyword)
274         return STICKER_ERROR_OPERATION_FAILED;
275
276     GList *list = NULL;
277     for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
278         callback(list->data, user_data);
279     }
280
281     return STICKER_ERROR_NONE;
282 }
283
284 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
285 {
286     GList *node;
287
288     if (!data_handle || !keyword)
289         return STICKER_ERROR_INVALID_PARAMETER;
290
291     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
292
293     if (node) {
294         LOGE("keyword already exists");
295         return STICKER_ERROR_INVALID_PARAMETER;
296     } else {
297         data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
298     }
299
300     return STICKER_ERROR_NONE;
301 }
302
303 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
304 {
305     GList *node;
306
307     if (!data_handle || !keyword)
308         return STICKER_ERROR_INVALID_PARAMETER;
309
310     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
311
312     if (node) {
313         data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
314     } else {
315         LOGE("keyword does not exist");
316         return STICKER_ERROR_INVALID_PARAMETER;
317     }
318
319     return STICKER_ERROR_NONE;
320 }
321
322 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
323 {
324     if (!data_handle || !group)
325         return STICKER_ERROR_INVALID_PARAMETER;
326
327     if (data_handle->group)
328         free(data_handle->group);
329
330     data_handle->group = strdup(group);
331
332     return STICKER_ERROR_NONE;
333 }
334
335 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
336 {
337     if (!data_handle || !group)
338         return STICKER_ERROR_INVALID_PARAMETER;
339
340     if (!data_handle->group)
341         return STICKER_ERROR_OPERATION_FAILED;
342
343     *group = strdup(data_handle->group);
344
345     return STICKER_ERROR_NONE;
346 }
347
348 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
349 {
350     char *file_path = NULL;
351
352     if (!data_handle || !thumbnail)
353         return STICKER_ERROR_INVALID_PARAMETER;
354
355     if (access(thumbnail, F_OK) != 0) {
356         file_path = _make_absolute_path(thumbnail);
357         if (file_path == NULL)
358             return STICKER_ERROR_INVALID_PARAMETER;
359     } else
360         file_path = strdup(thumbnail);
361
362     if (data_handle->thumbnail)
363         free(data_handle->thumbnail);
364
365     data_handle->thumbnail = file_path;
366
367     return STICKER_ERROR_NONE;
368 }
369
370 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
371 {
372     if (!data_handle || !thumbnail)
373         return STICKER_ERROR_INVALID_PARAMETER;
374
375     if (!data_handle->thumbnail)
376         *thumbnail = strdup("");
377     else
378         *thumbnail = strdup(data_handle->thumbnail);
379
380     return STICKER_ERROR_NONE;
381 }
382
383 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
384 {
385     if (!data_handle || !description)
386         return STICKER_ERROR_INVALID_PARAMETER;
387
388     if (data_handle->description)
389         free(data_handle->description);
390
391     data_handle->description = strdup(description);
392
393     return STICKER_ERROR_NONE;
394 }
395
396 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
397 {
398     if (!data_handle || !description)
399         return STICKER_ERROR_INVALID_PARAMETER;
400
401     if (!data_handle->description)
402         *description = strdup("");
403     else
404         *description = strdup(data_handle->description);
405
406     return STICKER_ERROR_NONE;
407 }
408
409 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
410 {
411     if (!data_handle || !date)
412         return STICKER_ERROR_INVALID_PARAMETER;
413
414     if (!data_handle->date)
415         return STICKER_ERROR_OPERATION_FAILED;
416
417     *date = strdup(data_handle->date);
418
419     return STICKER_ERROR_NONE;
420 }