Update package version to 0.1.23
[platform/core/uifw/capi-ui-sticker.git] / provider / sticker_provider.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_provider.h"
25 #include "sticker_provider_main.h"
26 #include "sticker_dbus.h"
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31 #define LOG_TAG "STICKER_PROVIDER"
32
33 static void _free_sticker_data(sticker_data_h sticker_data)
34 {
35     if (sticker_data->app_id) {
36         free(sticker_data->app_id);
37         sticker_data->app_id = NULL;
38     }
39
40     if (sticker_data->uri) {
41         free(sticker_data->uri);
42         sticker_data->uri = NULL;
43     }
44
45     if (sticker_data->thumbnail) {
46         free(sticker_data->thumbnail);
47         sticker_data->thumbnail = NULL;
48     }
49
50     if (sticker_data->keyword) {
51         g_list_free_full(sticker_data->keyword, free);
52         sticker_data->keyword = NULL;
53     }
54
55     if (sticker_data->group) {
56         free(sticker_data->group);
57         sticker_data->group = NULL;
58     }
59
60     if (sticker_data->description) {
61         free(sticker_data->description);
62         sticker_data->description = NULL;
63     }
64
65     if (sticker_data->date) {
66         free(sticker_data->date);
67         sticker_data->date = NULL;
68     }
69
70     free(sticker_data);
71     sticker_data = NULL;
72 }
73
74 EXPORT_API int sticker_provider_create(sticker_provider_h *provider_handle)
75 {
76     CHECK_STICKER_FEATURE();
77
78     int ret;
79     if (!provider_handle)
80         return STICKER_ERROR_INVALID_PARAMETER;
81
82     struct sticker_provider_s *provider_struct = (sticker_provider_h)calloc(1, sizeof(struct sticker_provider_s));
83
84     if (!provider_struct)
85         return STICKER_ERROR_OUT_OF_MEMORY;
86
87     ret = sticker_dbus_init(&provider_struct->gdbus_connection, &provider_struct->server_watcher_id,
88         &provider_struct->monitor_id, &provider_struct->server_monitor_id, STICKER_CLIENT_LIB_PROVIDER, (void *)provider_struct);
89     if (ret != STICKER_ERROR_NONE) {
90         LOGE("Failed to initialize dbus : %d", ret);
91         free(provider_struct);
92         return STICKER_ERROR_OPERATION_FAILED;
93     }
94
95     *provider_handle = provider_struct;
96
97     return STICKER_ERROR_NONE;
98 }
99
100 EXPORT_API int sticker_provider_destroy(sticker_provider_h provider_handle)
101 {
102     CHECK_STICKER_FEATURE();
103
104     int ret;
105     if (!provider_handle)
106         return STICKER_ERROR_INVALID_PARAMETER;
107
108     LOGD("provider_handle : %p", provider_handle);
109     ret = sticker_dbus_shutdown(provider_handle->gdbus_connection, &provider_handle->server_watcher_id,
110                                 &provider_handle->server_monitor_id, &provider_handle->monitor_id);
111     if (ret != STICKER_ERROR_NONE) {
112         LOGE("Failed to finalize dbus : %d", ret);
113         free(provider_handle);
114         return STICKER_ERROR_OPERATION_FAILED;
115     }
116
117     if (provider_handle->gdbus_connection)
118         g_object_unref(provider_handle->gdbus_connection);
119
120     free(provider_handle);
121
122     return STICKER_ERROR_NONE;
123 }
124
125 EXPORT_API int sticker_provider_insert_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
126 {
127     CHECK_STICKER_FEATURE();
128
129     int ret;
130     int is_exist = 0;
131
132     if (!provider_handle || !data_handle || (data_handle->sticker_info_id > 0) || !data_handle->uri)
133         return STICKER_ERROR_INVALID_PARAMETER;
134
135     ret = sticker_dbus_check_file_exists(provider_handle->gdbus_connection, data_handle->uri, &is_exist);
136     if (ret != STICKER_ERROR_NONE) {
137         LOGE("Failed to check file exists : %d", ret);
138         return STICKER_ERROR_OPERATION_FAILED;
139     }
140
141     if (is_exist) {
142         LOGE("Sticker already exists");
143         return STICKER_ERROR_FILE_EXISTS;
144     }
145
146     ret = sticker_dbus_insert_sticker_info(provider_handle->gdbus_connection, data_handle);
147     if (ret != STICKER_ERROR_NONE) {
148         LOGE("Failed to insert sticker information : %d", ret);
149         if (ret == STICKER_CLIENT_ERROR_NO_SUCH_FILE)
150             return STICKER_ERROR_NO_SUCH_FILE;
151         else
152             return STICKER_ERROR_OPERATION_FAILED;
153     }
154
155     return STICKER_ERROR_NONE;
156 }
157
158 EXPORT_API int sticker_provider_insert_data_by_json_file(sticker_provider_h provider_handle, const char *json_path, sticker_provider_insert_finished_cb callback, void *user_data)
159 {
160     CHECK_STICKER_FEATURE();
161
162     int ret;
163     char *app_id = NULL;
164     package_info_h package_info = NULL;
165     char *app_path = NULL;
166     char *file_path = NULL;
167
168     if (!provider_handle || !json_path || !callback)
169         return STICKER_ERROR_INVALID_PARAMETER;
170
171     ret = app_get_id(&app_id);
172     if (ret != APP_ERROR_NONE) {
173         LOGE("Failed to get app_id : %d", ret);
174         ret = STICKER_ERROR_OPERATION_FAILED;
175         goto cleanup;
176     }
177
178     if (access(json_path, F_OK) != 0) {
179         ret = package_info_create(app_id, &package_info);
180         if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
181             LOGE("failed to create package_info. ret: %d", ret);
182             ret = STICKER_ERROR_OPERATION_FAILED;
183             goto cleanup;
184         }
185
186         ret = package_info_get_root_path(package_info, &app_path);
187         if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
188             LOGE("failed to create package_info. ret: %d", ret);
189             ret = STICKER_ERROR_OPERATION_FAILED;
190             goto cleanup;
191         }
192
193         int path_len = strlen(app_path) + strlen(json_path) + 2;
194         file_path = (char *)calloc(path_len, sizeof(char));
195         if (!file_path) {
196             LOGE("failed to alloc memory");
197             ret = STICKER_ERROR_OPERATION_FAILED;
198             goto cleanup;
199         }
200
201         if(json_path[0] == '/')
202             snprintf(file_path, path_len, "%s%s",app_path, json_path);
203         else
204             snprintf(file_path, path_len, "%s%s%s",app_path, "/", json_path);
205
206         if (access(file_path, F_OK) != 0) {
207             LOGE("%s does not exist", file_path);
208             ret = STICKER_ERROR_INVALID_PARAMETER;
209             goto cleanup;
210         }
211     } else
212         file_path = strdup(json_path);
213
214     SECURE_LOGD("json path : %s", file_path);
215     ret = sticker_dbus_insert_sticker_info_by_json(provider_handle->gdbus_connection, app_id, file_path);
216     if (ret != STICKER_ERROR_NONE) {
217         LOGE("Failed to load json file : %d", ret);
218         ret = STICKER_ERROR_OPERATION_FAILED;
219         goto cleanup;
220     }
221
222     provider_handle->insert_finished_cb = callback;
223     provider_handle->insert_finished_cb_user_data = user_data;
224
225 cleanup:
226     if (app_id)
227         free(app_id);
228
229     if (package_info)
230         package_info_destroy(package_info);
231
232     if (app_path)
233         free(app_path);
234
235     if (file_path)
236         free(file_path);
237
238     return ret;
239 }
240
241 EXPORT_API int sticker_provider_update_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
242 {
243     CHECK_STICKER_FEATURE();
244
245     int ret;
246     if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
247         return STICKER_ERROR_INVALID_PARAMETER;
248
249     ret = sticker_dbus_update_sticker_info(provider_handle->gdbus_connection, data_handle);
250     if (ret != STICKER_ERROR_NONE) {
251         LOGE("Failed to update sticker information : %d", ret);
252         if (ret == STICKER_CLIENT_ERROR_FILE_EXISTS)
253             return STICKER_ERROR_FILE_EXISTS;
254         else if (ret == STICKER_CLIENT_ERROR_NO_SUCH_FILE)
255             return STICKER_ERROR_NO_SUCH_FILE;
256         else
257             return STICKER_ERROR_OPERATION_FAILED;
258     }
259
260     return STICKER_ERROR_NONE;
261 }
262
263 EXPORT_API int sticker_provider_delete_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
264 {
265     CHECK_STICKER_FEATURE();
266
267     int ret;
268     if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
269         return STICKER_ERROR_INVALID_PARAMETER;
270
271     ret = sticker_dbus_delete_sticker_info(provider_handle->gdbus_connection, data_handle->sticker_info_id);
272     if (ret != STICKER_ERROR_NONE) {
273         LOGE("Failed to delete sticker information : %d", ret);
274         return STICKER_ERROR_OPERATION_FAILED;
275     }
276
277     return STICKER_ERROR_NONE;
278 }
279
280 EXPORT_API int sticker_provider_get_sticker_count(sticker_provider_h provider_handle, int *count)
281 {
282     CHECK_STICKER_FEATURE();
283
284     int ret;
285     char *app_id = NULL;
286
287     if (!provider_handle || !count)
288         return STICKER_ERROR_INVALID_PARAMETER;
289
290     ret = app_get_id(&app_id);
291     if (ret != APP_ERROR_NONE) {
292         LOGE("Failed to get app_id : %d", ret);
293         ret = STICKER_ERROR_OPERATION_FAILED;
294         goto cleanup;
295     }
296
297     ret = sticker_dbus_get_sticker_count(provider_handle->gdbus_connection, app_id, count);
298     if (ret != STICKER_ERROR_NONE) {
299         LOGE("Failed to get sticker count : %d", ret);
300         ret = STICKER_ERROR_OPERATION_FAILED;
301     }
302
303 cleanup:
304     if (app_id)
305         free(app_id);
306
307     return ret;
308 }
309
310 EXPORT_API int sticker_provider_data_foreach_all(sticker_provider_h provider_handle, int offset, int count, int *result, sticker_provider_data_foreach_cb callback, void *user_data)
311 {
312     CHECK_STICKER_FEATURE();
313
314     int ret;
315     int info_id;
316     int sticker_count = 0;
317     char *app_id = NULL;
318     GVariantIter *id_iter = NULL;
319
320     if (!provider_handle || (offset < 0) || (count <= 0) || !result || !callback)
321         return STICKER_ERROR_INVALID_PARAMETER;
322
323     ret = app_get_id(&app_id);
324     if (ret != APP_ERROR_NONE) {
325         LOGE("Failed to get app_id : %d", ret);
326         ret = STICKER_ERROR_OPERATION_FAILED;
327         goto cleanup;
328     }
329
330     ret = sticker_dbus_get_sticker_info_by_appid(provider_handle->gdbus_connection, app_id, offset, count, &id_iter);
331     if (ret != STICKER_ERROR_NONE) {
332         LOGE("Failed to get sticker information : %d", ret);
333         ret = STICKER_ERROR_OPERATION_FAILED;
334         goto cleanup;
335     }
336
337     if (id_iter) {
338         while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
339             sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
340             if (!sticker_data) {
341                 ret = STICKER_ERROR_OUT_OF_MEMORY;
342                 goto cleanup;
343             }
344
345             ret = sticker_dbus_get_sticker_info_by_record_id(provider_handle->gdbus_connection, sticker_data, info_id);
346             if (ret == STICKER_ERROR_NONE) {
347                 sticker_count++;
348                 callback(sticker_data, user_data);
349                 _free_sticker_data(sticker_data);
350             } else {
351                 _free_sticker_data(sticker_data);
352                 goto cleanup;
353             }
354         }
355     }
356
357     *result = sticker_count;
358
359 cleanup:
360     if (app_id)
361         free(app_id);
362
363     if (id_iter)
364         g_variant_iter_free(id_iter);
365
366     return ret;
367 }
368
369 EXPORT_API int sticker_provider_delete_data_by_uri(sticker_provider_h provider_handle, const char *uri)
370 {
371     CHECK_STICKER_FEATURE();
372
373     int ret;
374     int is_exist = 0;
375
376     if (!provider_handle || !uri)
377         return STICKER_ERROR_INVALID_PARAMETER;
378
379     ret = sticker_dbus_check_file_exists(provider_handle->gdbus_connection, uri, &is_exist);
380     if (ret != STICKER_ERROR_NONE) {
381         LOGE("Failed to check file exists : %d", ret);
382         return STICKER_ERROR_OPERATION_FAILED;
383     }
384
385     if (!is_exist) {
386         LOGE("Sticker does not exist");
387         return STICKER_ERROR_NO_SUCH_FILE;
388     }
389
390     ret = sticker_dbus_delete_sticker_info_by_uri(provider_handle->gdbus_connection, uri);
391     if (ret != STICKER_ERROR_NONE) {
392         LOGE("Failed to delete sticker information : %d", ret);
393         return STICKER_ERROR_OPERATION_FAILED;
394     }
395
396     return STICKER_ERROR_NONE;
397 }