Fix build error in sticker-receiver
[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); //LCOV_EXCL_LINE
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); //LCOV_EXCL_LINE
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); //LCOV_EXCL_LINE
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"); //LCOV_EXCL_LINE
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); //LCOV_EXCL_LINE
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 static void _free_sticker_data(sticker_data_h sticker_data)
97 {
98     if (!sticker_data)
99         return;
100
101     if (sticker_data->app_id) {
102         free(sticker_data->app_id);
103         sticker_data->app_id = NULL;
104     }
105
106     if (sticker_data->uri) {
107         free(sticker_data->uri);
108         sticker_data->uri = NULL;
109     }
110
111     if (sticker_data->thumbnail) {
112         free(sticker_data->thumbnail);
113         sticker_data->thumbnail = NULL;
114     }
115
116     if (sticker_data->keyword) {
117         g_list_free_full(sticker_data->keyword, free);
118         sticker_data->keyword = NULL;
119     }
120
121     if (sticker_data->group) {
122         free(sticker_data->group);
123         sticker_data->group = NULL;
124     }
125
126     if (sticker_data->description) {
127         free(sticker_data->description);
128         sticker_data->description = NULL;
129     }
130
131     if (sticker_data->date) {
132         free(sticker_data->date);
133         sticker_data->date = NULL;
134     }
135
136     free(sticker_data);
137     sticker_data = NULL;
138 }
139
140 EXPORT_API int sticker_data_create(sticker_data_h *data_handle)
141 {
142     CHECK_STICKER_FEATURE();
143
144     if (!data_handle)
145         return STICKER_ERROR_INVALID_PARAMETER;
146
147     sticker_data_h data_struct = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
148
149     if (!data_struct)
150         return STICKER_ERROR_OUT_OF_MEMORY;
151
152     char *app_id = NULL;
153     int ret = app_get_id(&app_id);
154     if (ret != APP_ERROR_NONE) {
155         //LCOV_EXCL_START
156         LOGE("Failed to get app_id : %d", ret);
157         free(data_struct);
158         ret = STICKER_ERROR_OPERATION_FAILED;
159         goto cleanup;
160         //LCOV_EXCL_STOP
161     }
162
163     data_struct->app_id = strdup(app_id);
164     *data_handle = data_struct;
165
166 cleanup:
167     free(app_id);
168
169     return ret;
170 }
171
172 EXPORT_API int sticker_data_destroy(sticker_data_h data_handle)
173 {
174     CHECK_STICKER_FEATURE();
175
176     if (!data_handle)
177         return STICKER_ERROR_INVALID_PARAMETER;
178
179     if (data_handle->app_id) {
180         free(data_handle->app_id);
181         data_handle->app_id = NULL;
182     }
183
184     if (data_handle->uri) {
185         free(data_handle->uri);
186         data_handle->uri = NULL;
187     }
188
189     if (data_handle->thumbnail) {
190         free(data_handle->thumbnail);
191         data_handle->thumbnail = NULL;
192     }
193
194     if (data_handle->keyword) {
195         g_list_free_full(data_handle->keyword, free);
196         data_handle->keyword = NULL;
197     }
198
199     if (data_handle->group) {
200         free(data_handle->group);
201         data_handle->group = NULL;
202     }
203
204     if (data_handle->description) {
205         free(data_handle->description);
206         data_handle->description = NULL;
207     }
208
209     if (data_handle->date) {
210         free(data_handle->date);
211         data_handle->date = NULL;
212     }
213
214     free(data_handle);
215     data_handle = NULL;
216
217     return STICKER_ERROR_NONE;
218 }
219
220 EXPORT_API int sticker_data_clone(sticker_data_h origin_handle, sticker_data_h *target_handle)
221 {
222     CHECK_STICKER_FEATURE();
223
224     sticker_data_h handle;
225     if (!origin_handle || !target_handle)
226         return STICKER_ERROR_INVALID_PARAMETER;
227
228     sticker_data_create(&handle);
229     if (!handle)
230         return STICKER_ERROR_OUT_OF_MEMORY;
231
232     handle->sticker_info_id = origin_handle->sticker_info_id;
233
234     if (origin_handle->app_id)
235         handle->app_id = strdup(origin_handle->app_id);
236
237     handle->type = origin_handle->type;
238
239     if (origin_handle->uri)
240         handle->uri = strdup(origin_handle->uri);
241
242     if (origin_handle->thumbnail)
243         handle->thumbnail = strdup(origin_handle->thumbnail);
244
245     if (origin_handle->keyword)
246         handle->keyword = g_list_copy_deep(origin_handle->keyword, (GCopyFunc) g_strdup, NULL);
247
248     if (origin_handle->group)
249         handle->group = strdup(origin_handle->group);
250
251     if (origin_handle->description)
252         handle->description = strdup(origin_handle->description);
253
254     if (origin_handle->date)
255         handle->date = strdup(origin_handle->date);
256
257     handle->disp_type = origin_handle->disp_type;
258
259     *target_handle = handle;
260
261     return STICKER_ERROR_NONE;
262 }
263
264 EXPORT_API int sticker_data_get_handle(const char* uri, sticker_data_h *data_handle)
265 {
266     CHECK_STICKER_FEATURE();
267
268     int ret;
269     GDBusConnection *gdbus_connection = NULL;
270     int server_watcher_id = 0;
271     int monitor_id = 0;
272     int server_monitor_id = 0;
273     int is_exist = 0;
274
275     if (!uri || uri[0] == '\0' || !data_handle)
276         return STICKER_ERROR_INVALID_PARAMETER;
277
278     struct sticker_data_s *handle = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
279     if (!handle)
280         return STICKER_ERROR_OUT_OF_MEMORY;
281
282     ret = sticker_dbus_init(&gdbus_connection, &server_watcher_id, &monitor_id, &server_monitor_id, STICKER_CLIENT_LIB_PROVIDER, NULL);
283     if (ret != STICKER_ERROR_NONE) {
284         LOGE("Failed to initialize dbus : %d", ret);
285         ret = STICKER_ERROR_OPERATION_FAILED;
286         goto cleanup;
287     }
288
289     ret = sticker_dbus_check_file_exists(gdbus_connection, uri, &is_exist);
290     if (ret != STICKER_ERROR_NONE) {
291         LOGE("Failed to check file exists : %d", ret);
292         ret = STICKER_ERROR_OPERATION_FAILED;
293         goto cleanup;
294     }
295
296     if (!is_exist) {
297         LOGE("Sticker does not exist. URI : %s", uri);
298         ret = STICKER_ERROR_NO_SUCH_FILE;
299         goto cleanup;
300     }
301
302     ret = sticker_dbus_get_sticker_info_by_uri(gdbus_connection, handle, uri);
303     if (ret != STICKER_ERROR_NONE) {
304         LOGE("Failed to get sticker information : %d", ret);
305         goto cleanup;
306     }
307
308     *data_handle = handle;
309
310     ret = sticker_dbus_shutdown(gdbus_connection, &server_watcher_id, &server_monitor_id, &monitor_id, STICKER_CLIENT_LIB_PROVIDER);
311     if (ret != STICKER_ERROR_NONE)
312         LOGE("Failed to finalize dbus : %d", ret);
313
314     g_object_unref(gdbus_connection);
315
316     return STICKER_ERROR_NONE;
317
318 cleanup:
319     if (handle)
320         _free_sticker_data(handle);
321
322     if (gdbus_connection) {
323         int ret_err = sticker_dbus_shutdown(gdbus_connection, &server_watcher_id, &server_monitor_id, &monitor_id, STICKER_CLIENT_LIB_PROVIDER);
324         if (ret_err != STICKER_ERROR_NONE)
325             LOGE("Failed to finalize dbus : %d", ret_err);
326
327         g_object_unref(gdbus_connection);
328     }
329
330     return ret;
331 }
332
333 EXPORT_API int sticker_data_get_app_id(sticker_data_h data_handle, char **app_id)
334 {
335     CHECK_STICKER_FEATURE();
336
337     if (!data_handle || !app_id)
338         return STICKER_ERROR_INVALID_PARAMETER;
339
340     if (!data_handle->app_id)
341         return STICKER_ERROR_NO_DATA;
342
343     *app_id = strdup(data_handle->app_id);
344
345     return STICKER_ERROR_NONE;
346 }
347
348 EXPORT_API int sticker_data_set_uri(sticker_data_h data_handle, sticker_data_uri_type_e type, const char *uri)
349 {
350     CHECK_STICKER_FEATURE();
351
352     char *file_path = NULL;
353     if (!data_handle || !type || !uri || uri[0] == '\0')
354         return STICKER_ERROR_INVALID_PARAMETER;
355
356     if (type == STICKER_DATA_URI_LOCAL_PATH) {
357         if (access(uri, F_OK) != 0) {
358             file_path = _make_absolute_path(uri);
359             if (file_path == NULL) {
360                 return STICKER_ERROR_INVALID_PARAMETER;
361             }
362         } else
363             file_path = strdup(uri);
364     }
365
366     if (data_handle->uri)
367         free(data_handle->uri);
368
369     data_handle->type = type;
370     if (type == STICKER_DATA_URI_LOCAL_PATH)
371         data_handle->uri = file_path;
372     else
373         data_handle->uri = strdup(uri);
374
375     return STICKER_ERROR_NONE;
376 }
377
378 EXPORT_API int sticker_data_get_uri(sticker_data_h data_handle, sticker_data_uri_type_e *type, char **uri)
379 {
380     CHECK_STICKER_FEATURE();
381
382     if (!data_handle || !type || !uri)
383         return STICKER_ERROR_INVALID_PARAMETER;
384
385     if (!data_handle->type || !data_handle->uri)
386         return STICKER_ERROR_NO_DATA;
387
388     *type = data_handle->type;
389     *uri = strdup(data_handle->uri);
390
391     return STICKER_ERROR_NONE;
392 }
393
394 EXPORT_API int sticker_data_foreach_keyword(sticker_data_h data_handle, sticker_data_keyword_foreach_cb callback, void *user_data)
395 {
396     CHECK_STICKER_FEATURE();
397
398     if (!data_handle || !callback)
399         return STICKER_ERROR_INVALID_PARAMETER;
400
401     if (!data_handle->keyword)
402         return STICKER_ERROR_NO_DATA;
403
404     GList *list = NULL;
405     for(list = g_list_first(data_handle->keyword); list != NULL; list=list->next) {
406         callback(list->data, user_data);
407     }
408
409     return STICKER_ERROR_NONE;
410 }
411
412 EXPORT_API int sticker_data_add_keyword(sticker_data_h data_handle, const char *keyword)
413 {
414     CHECK_STICKER_FEATURE();
415
416     GList *node;
417     if (!data_handle || !keyword)
418         return STICKER_ERROR_INVALID_PARAMETER;
419
420     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
421
422     if (node) {
423         LOGE("keyword already exists");
424         return STICKER_ERROR_INVALID_PARAMETER;
425     } else {
426         data_handle->keyword = g_list_append(data_handle->keyword, strdup(keyword));
427     }
428
429     return STICKER_ERROR_NONE;
430 }
431
432 EXPORT_API int sticker_data_remove_keyword(sticker_data_h data_handle, const char *keyword)
433 {
434     CHECK_STICKER_FEATURE();
435
436     GList *node;
437     if (!data_handle || !keyword)
438         return STICKER_ERROR_INVALID_PARAMETER;
439
440     node = g_list_find_custom(data_handle->keyword, keyword, (GCompareFunc) strcmp);
441
442     if (node) {
443         data_handle->keyword = g_list_delete_link(data_handle->keyword, node);
444     } else {
445         LOGE("keyword does not exist");
446         return STICKER_ERROR_INVALID_PARAMETER;
447     }
448
449     return STICKER_ERROR_NONE;
450 }
451
452 EXPORT_API int sticker_data_set_group_name(sticker_data_h data_handle, const char *group)
453 {
454     CHECK_STICKER_FEATURE();
455
456     if (!data_handle || !group)
457         return STICKER_ERROR_INVALID_PARAMETER;
458
459     if (data_handle->group)
460         free(data_handle->group);
461
462     data_handle->group = strdup(group);
463
464     return STICKER_ERROR_NONE;
465 }
466
467 EXPORT_API int sticker_data_get_group_name(sticker_data_h data_handle, char **group)
468 {
469     CHECK_STICKER_FEATURE();
470
471     if (!data_handle || !group)
472         return STICKER_ERROR_INVALID_PARAMETER;
473
474     if (!data_handle->group)
475         return STICKER_ERROR_NO_DATA;
476
477     *group = strdup(data_handle->group);
478
479     return STICKER_ERROR_NONE;
480 }
481
482 EXPORT_API int sticker_data_set_thumbnail(sticker_data_h data_handle, const char *thumbnail)
483 {
484     CHECK_STICKER_FEATURE();
485
486     char *file_path = NULL;
487     if (!data_handle || !thumbnail || thumbnail[0] == '\0')
488         return STICKER_ERROR_INVALID_PARAMETER;
489
490     if (access(thumbnail, F_OK) != 0) {
491         file_path = _make_absolute_path(thumbnail);
492         if (file_path == NULL)
493             return STICKER_ERROR_INVALID_PARAMETER;
494     } else
495         file_path = strdup(thumbnail);
496
497     if (data_handle->thumbnail)
498         free(data_handle->thumbnail);
499
500     data_handle->thumbnail = file_path;
501
502     return STICKER_ERROR_NONE;
503 }
504
505 EXPORT_API int sticker_data_get_thumbnail(sticker_data_h data_handle, char **thumbnail)
506 {
507     CHECK_STICKER_FEATURE();
508
509     if (!data_handle || !thumbnail)
510         return STICKER_ERROR_INVALID_PARAMETER;
511
512     if (!data_handle->thumbnail)
513         *thumbnail = strdup("");
514     else
515         *thumbnail = strdup(data_handle->thumbnail);
516
517     return STICKER_ERROR_NONE;
518 }
519
520 EXPORT_API int sticker_data_set_description(sticker_data_h data_handle, const char *description)
521 {
522     CHECK_STICKER_FEATURE();
523
524     if (!data_handle || !description)
525         return STICKER_ERROR_INVALID_PARAMETER;
526
527     if (data_handle->description)
528         free(data_handle->description);
529
530     data_handle->description = strdup(description);
531
532     return STICKER_ERROR_NONE;
533 }
534
535 EXPORT_API int sticker_data_get_description(sticker_data_h data_handle, char **description)
536 {
537     CHECK_STICKER_FEATURE();
538
539     if (!data_handle || !description)
540         return STICKER_ERROR_INVALID_PARAMETER;
541
542     if (!data_handle->description)
543         *description = strdup("");
544     else
545         *description = strdup(data_handle->description);
546
547     return STICKER_ERROR_NONE;
548 }
549
550 EXPORT_API int sticker_data_get_date(sticker_data_h data_handle, char **date)
551 {
552     CHECK_STICKER_FEATURE();
553
554     if (!data_handle || !date)
555         return STICKER_ERROR_INVALID_PARAMETER;
556
557     if (!data_handle->date)
558         return STICKER_ERROR_NO_DATA;
559
560     *date = strdup(data_handle->date);
561
562     return STICKER_ERROR_NONE;
563 }
564
565 EXPORT_API int sticker_data_set_display_type(sticker_data_h data_handle, sticker_data_display_type_e type)
566 {
567     CHECK_STICKER_FEATURE();
568
569     if (!data_handle || !type || type < STICKER_DATA_DISP_EMOJI || type > STICKER_DATA_DISP_WALLPAPER)
570         return STICKER_ERROR_INVALID_PARAMETER;
571
572     data_handle->disp_type = type;
573
574     return STICKER_ERROR_NONE;
575 }
576
577 EXPORT_API int sticker_data_get_display_type(sticker_data_h data_handle, sticker_data_display_type_e *type)
578 {
579     CHECK_STICKER_FEATURE();
580
581     if (!data_handle || !type)
582         return STICKER_ERROR_INVALID_PARAMETER;
583
584     *type = data_handle->disp_type;
585
586     return STICKER_ERROR_NONE;
587 }