Support display_type
[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("failed 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("failed 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     CHECK_STICKER_FEATURE();
99
100     if (!data_handle)
101         return STICKER_ERROR_INVALID_PARAMETER;
102
103     sticker_data_h data_struct = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
104
105     if (!data_struct)
106         return STICKER_ERROR_OUT_OF_MEMORY;
107
108     char *app_id = NULL;
109     int ret = app_get_id(&app_id);
110     if (ret != APP_ERROR_NONE) {
111         LOGE("Failed to get app_id : %d", ret);
112         free(data_struct);
113         ret = STICKER_ERROR_OPERATION_FAILED;
114         goto cleanup;
115     }
116
117     data_struct->app_id = strdup(app_id);
118     *data_handle = data_struct;
119
120 cleanup:
121     free(app_id);
122
123     return ret;
124 }
125
126 EXPORT_API int sticker_data_destroy(sticker_data_h data_handle)
127 {
128     CHECK_STICKER_FEATURE();
129
130     if (!data_handle)
131         return STICKER_ERROR_INVALID_PARAMETER;
132
133     if (data_handle->app_id) {
134         free(data_handle->app_id);
135         data_handle->app_id = NULL;
136     }
137
138     if (data_handle->uri) {
139         free(data_handle->uri);
140         data_handle->uri = NULL;
141     }
142
143     if (data_handle->thumbnail) {
144         free(data_handle->thumbnail);
145         data_handle->thumbnail = NULL;
146     }
147
148     if (data_handle->keyword) {
149         g_list_free_full(data_handle->keyword, free);
150         data_handle->keyword = NULL;
151     }
152
153     if (data_handle->group) {
154         free(data_handle->group);
155         data_handle->group = NULL;
156     }
157
158     if (data_handle->description) {
159         free(data_handle->description);
160         data_handle->description = NULL;
161     }
162
163     if (data_handle->date) {
164         free(data_handle->date);
165         data_handle->date = NULL;
166     }
167
168     free(data_handle);
169     data_handle = NULL;
170
171     return STICKER_ERROR_NONE;
172 }
173
174 EXPORT_API int sticker_data_clone(sticker_data_h origin_handle, sticker_data_h *target_handle)
175 {
176     CHECK_STICKER_FEATURE();
177
178     sticker_data_h handle;
179     if (!origin_handle || !target_handle)
180         return STICKER_ERROR_INVALID_PARAMETER;
181
182     sticker_data_create(&handle);
183     if (!handle)
184         return STICKER_ERROR_OUT_OF_MEMORY;
185
186     handle->sticker_info_id = origin_handle->sticker_info_id;
187
188     if (origin_handle->app_id)
189         handle->app_id = strdup(origin_handle->app_id);
190
191     handle->type = origin_handle->type;
192
193     if (origin_handle->uri)
194         handle->uri = strdup(origin_handle->uri);
195
196     if (origin_handle->thumbnail)
197         handle->thumbnail = strdup(origin_handle->thumbnail);
198
199     if (origin_handle->keyword)
200         handle->keyword = g_list_copy_deep(origin_handle->keyword, (GCopyFunc) g_strdup, NULL);
201
202     if (origin_handle->group)
203         handle->group = strdup(origin_handle->group);
204
205     if (origin_handle->description)
206         handle->description = strdup(origin_handle->description);
207
208     if (origin_handle->date)
209         handle->date = strdup(origin_handle->date);
210
211     handle->disp_type = origin_handle->disp_type;
212
213     *target_handle = handle;
214
215     return STICKER_ERROR_NONE;
216 }
217
218 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
219 {
220     CHECK_STICKER_FEATURE();
221
222     if (!data_handle || !app_id)
223         return STICKER_ERROR_INVALID_PARAMETER;
224
225     if (!data_handle->app_id)
226         return STICKER_ERROR_OPERATION_FAILED;
227
228     *app_id = strdup(data_handle->app_id);
229
230     return STICKER_ERROR_NONE;
231 }
232
233 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
234 {
235     CHECK_STICKER_FEATURE();
236
237     char *file_path = NULL;
238     if (!data_handle || !type || !uri)
239         return STICKER_ERROR_INVALID_PARAMETER;
240
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;
246             }
247         } else
248             file_path = strdup(uri);
249     }
250
251     if (data_handle->uri)
252         free(data_handle->uri);
253
254     data_handle->type = type;
255     if (type == STICKER_DATA_URI_LOCAL_PATH)
256         data_handle->uri = file_path;
257     else
258         data_handle->uri = strdup(uri);
259
260     return STICKER_ERROR_NONE;
261 }
262
263 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
264 {
265     CHECK_STICKER_FEATURE();
266
267     if (!data_handle || !type || !uri)
268         return STICKER_ERROR_INVALID_PARAMETER;
269
270     if (!data_handle->type || !data_handle->uri)
271         return STICKER_ERROR_OPERATION_FAILED;
272
273     *type = data_handle->type;
274     *uri = strdup(data_handle->uri);
275
276     return STICKER_ERROR_NONE;
277 }
278
279 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
280 {
281     CHECK_STICKER_FEATURE();
282
283     if (!data_handle || !callback)
284         return STICKER_ERROR_INVALID_PARAMETER;
285
286     if (!data_handle->keyword)
287         return STICKER_ERROR_OPERATION_FAILED;
288
289     GList *list = NULL;
290     for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
291         callback(list->data, user_data);
292     }
293
294     return STICKER_ERROR_NONE;
295 }
296
297 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
298 {
299     CHECK_STICKER_FEATURE();
300
301     GList *node;
302     if (!data_handle || !keyword)
303         return STICKER_ERROR_INVALID_PARAMETER;
304
305     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
306
307     if (node) {
308         LOGE("keyword already exists");
309         return STICKER_ERROR_INVALID_PARAMETER;
310     } else {
311         data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
312     }
313
314     return STICKER_ERROR_NONE;
315 }
316
317 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
318 {
319     CHECK_STICKER_FEATURE();
320
321     GList *node;
322     if (!data_handle || !keyword)
323         return STICKER_ERROR_INVALID_PARAMETER;
324
325     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
326
327     if (node) {
328         data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
329     } else {
330         LOGE("keyword does not exist");
331         return STICKER_ERROR_INVALID_PARAMETER;
332     }
333
334     return STICKER_ERROR_NONE;
335 }
336
337 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
338 {
339     CHECK_STICKER_FEATURE();
340
341     if (!data_handle || !group)
342         return STICKER_ERROR_INVALID_PARAMETER;
343
344     if (data_handle->group)
345         free(data_handle->group);
346
347     data_handle->group = strdup(group);
348
349     return STICKER_ERROR_NONE;
350 }
351
352 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
353 {
354     CHECK_STICKER_FEATURE();
355
356     if (!data_handle || !group)
357         return STICKER_ERROR_INVALID_PARAMETER;
358
359     if (!data_handle->group)
360         return STICKER_ERROR_OPERATION_FAILED;
361
362     *group = strdup(data_handle->group);
363
364     return STICKER_ERROR_NONE;
365 }
366
367 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
368 {
369     CHECK_STICKER_FEATURE();
370
371     char *file_path = NULL;
372     if (!data_handle || !thumbnail)
373         return STICKER_ERROR_INVALID_PARAMETER;
374
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;
379     } else
380         file_path = strdup(thumbnail);
381
382     if (data_handle->thumbnail)
383         free(data_handle->thumbnail);
384
385     data_handle->thumbnail = file_path;
386
387     return STICKER_ERROR_NONE;
388 }
389
390 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
391 {
392     CHECK_STICKER_FEATURE();
393
394     if (!data_handle || !thumbnail)
395         return STICKER_ERROR_INVALID_PARAMETER;
396
397     if (!data_handle->thumbnail)
398         *thumbnail = strdup("");
399     else
400         *thumbnail = strdup(data_handle->thumbnail);
401
402     return STICKER_ERROR_NONE;
403 }
404
405 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
406 {
407     CHECK_STICKER_FEATURE();
408
409     if (!data_handle || !description)
410         return STICKER_ERROR_INVALID_PARAMETER;
411
412     if (data_handle->description)
413         free(data_handle->description);
414
415     data_handle->description = strdup(description);
416
417     return STICKER_ERROR_NONE;
418 }
419
420 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
421 {
422     CHECK_STICKER_FEATURE();
423
424     if (!data_handle || !description)
425         return STICKER_ERROR_INVALID_PARAMETER;
426
427     if (!data_handle->description)
428         *description = strdup("");
429     else
430         *description = strdup(data_handle->description);
431
432     return STICKER_ERROR_NONE;
433 }
434
435 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
436 {
437     CHECK_STICKER_FEATURE();
438
439     if (!data_handle || !date)
440         return STICKER_ERROR_INVALID_PARAMETER;
441
442     if (!data_handle->date)
443         return STICKER_ERROR_OPERATION_FAILED;
444
445     *date = strdup(data_handle->date);
446
447     return STICKER_ERROR_NONE;
448 }
449
450 EXPORT_API int sticker_data_set_display_type(sticker_data_h data_handle, sticker_data_display_type_e type)
451 {
452     CHECK_STICKER_FEATURE();
453
454     if (!data_handle || !type || type < STICKER_DATA_DISP_EMOJI || type > STICKER_DATA_DISP_WALLPAPER)
455         return STICKER_ERROR_INVALID_PARAMETER;
456
457     data_handle->disp_type = type;
458
459     return STICKER_ERROR_NONE;
460 }
461
462 EXPORT_API int sticker_data_get_display_type(sticker_data_h data_handle, sticker_data_display_type_e *type)
463 {
464     CHECK_STICKER_FEATURE();
465
466     if (!data_handle || !type)
467         return STICKER_ERROR_INVALID_PARAMETER;
468
469     *type = data_handle->disp_type;
470
471     return STICKER_ERROR_NONE;
472 }