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