Initial commit
[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     int ret;
77
78     if (!provider_handle)
79         return STICKER_ERROR_INVALID_PARAMETER;
80
81     struct sticker_provider_s *provider_struct = (sticker_provider_h)calloc(1, sizeof(struct sticker_provider_s));
82
83     if (!provider_struct)
84         return STICKER_ERROR_OUT_OF_MEMORY;
85
86     ret = sticker_dbus_init(&provider_struct->gdbus_connection, &provider_struct->server_watcher_id,
87         &provider_struct->monitor_id, &provider_struct->server_monitor_id, STICKER_CLIENT_LIB_PROVIDER, (void *)provider_struct);
88     if (ret != STICKER_ERROR_NONE) {
89         LOGE("Failed to initialize dbus : %d", ret);
90         free(provider_struct);
91         return STICKER_ERROR_OPERATION_FAILED;
92     }
93
94     *provider_handle = provider_struct;
95
96     return STICKER_ERROR_NONE;
97 }
98
99 EXPORT_API int sticker_provider_destroy(sticker_provider_h provider_handle)
100 {
101     LOGD("provider_handle : %p", provider_handle);
102     int ret;
103
104     if (!provider_handle)
105         return STICKER_ERROR_INVALID_PARAMETER;
106
107     ret = sticker_dbus_shutdown(provider_handle->gdbus_connection, &provider_handle->server_monitor_id, &provider_handle->monitor_id);
108     if (ret != STICKER_ERROR_NONE) {
109         LOGE("Failed to finalize dbus : %d", ret);
110         free(provider_handle);
111         return STICKER_ERROR_OPERATION_FAILED;
112     }
113
114     if (provider_handle->gdbus_connection)
115         g_object_unref(provider_handle->gdbus_connection);
116
117     free(provider_handle);
118
119     return STICKER_ERROR_NONE;
120 }
121
122 EXPORT_API int sticker_provider_insert_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
123 {
124     int ret;
125
126     if (!provider_handle || !data_handle || (data_handle->sticker_info_id > 0))
127         return STICKER_ERROR_INVALID_PARAMETER;
128
129     ret = sticker_dbus_insert_sticker_info(provider_handle->gdbus_connection, data_handle);
130     if (ret != STICKER_ERROR_NONE) {
131         LOGE("Failed to insert sticker information : %d", ret);
132         return STICKER_ERROR_OPERATION_FAILED;
133     }
134
135     return STICKER_ERROR_NONE;
136 }
137
138 EXPORT_API int sticker_privider_insert_data_by_json_file(sticker_provider_h provider_handle, const char *json_path, sticker_provider_insert_finished_cb callback, void *user_data)
139 {
140     int ret;
141     char *app_id = NULL;
142     package_info_h package_info = NULL;
143     char *app_path = NULL;
144     char *file_path = NULL;
145
146     if (!provider_handle || !json_path || !callback)
147         return STICKER_ERROR_INVALID_PARAMETER;
148
149     ret = app_get_id(&app_id);
150     if (ret != APP_ERROR_NONE) {
151         LOGE("Failed to get app_id : %d", ret);
152         ret = STICKER_ERROR_OPERATION_FAILED;
153         goto cleanup;
154     }
155
156     if (access(json_path, F_OK) != 0) {
157         ret = package_info_create(app_id, &package_info);
158         if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
159             LOGE("faild to create package_info. ret: %d", ret);
160             ret = STICKER_ERROR_OPERATION_FAILED;
161             goto cleanup;
162         }
163
164         ret = package_info_get_root_path(package_info, &app_path);
165         if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
166             LOGE("faild to create package_info. ret: %d", ret);
167             ret = STICKER_ERROR_OPERATION_FAILED;
168             goto cleanup;
169         }
170
171         int path_len = strlen(app_path) + strlen(json_path) + 2;
172         file_path = (char *)calloc(path_len, sizeof(char));
173         if (!file_path) {
174             LOGE("failed to alloc memory");
175             ret = STICKER_ERROR_OPERATION_FAILED;
176             goto cleanup;
177         }
178
179         if(json_path[0] == '/')
180             snprintf(file_path, path_len, "%s%s",app_path, json_path);
181         else
182             snprintf(file_path, path_len, "%s%s%s",app_path, "/", json_path);
183
184         if (access(file_path, F_OK) != 0) {
185             LOGE("%s does not exist", file_path);
186             ret = STICKER_ERROR_INVALID_PARAMETER;
187             goto cleanup;
188         }
189     } else
190         file_path = strdup(json_path);
191
192     SECURE_LOGD("json path : %s", file_path);
193     ret = sticker_dbus_insert_sticker_info_by_json(provider_handle->gdbus_connection, app_id, file_path);
194     if (ret != STICKER_ERROR_NONE) {
195         LOGE("Failed to load json file : %d", ret);
196         return STICKER_ERROR_OPERATION_FAILED;
197     }
198
199     provider_handle->insert_finished_cb = callback;
200     provider_handle->insert_finished_cb_user_data = user_data;
201
202 cleanup:
203     if (app_id)
204         free(app_id);
205
206     if (package_info)
207         package_info_destroy(package_info);
208
209     if (app_path)
210         free(app_path);
211
212     if (file_path)
213         free(file_path);
214
215     return ret;
216 }
217
218 EXPORT_API int sticker_provider_update_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
219 {
220     int ret;
221
222     if (!provider_handle || !data_handle || (data_handle->sticker_info_id <= 0))
223         return STICKER_ERROR_INVALID_PARAMETER;
224
225     ret = sticker_dbus_update_sticker_info(provider_handle->gdbus_connection, data_handle);
226     if (ret != STICKER_ERROR_NONE) {
227         LOGE("Failed to update sticker information : %d", ret);
228         return STICKER_ERROR_OPERATION_FAILED;
229     }
230
231     return STICKER_ERROR_NONE;
232 }
233
234 EXPORT_API int sticker_provider_delete_data(sticker_provider_h provider_handle, sticker_data_h data_handle)
235 {
236     int ret;
237
238     if (!provider_handle || (data_handle->sticker_info_id <= 0))
239         return STICKER_ERROR_INVALID_PARAMETER;
240
241     ret = sticker_dbus_delete_sticker_info(provider_handle->gdbus_connection, data_handle->sticker_info_id);
242     if (ret != STICKER_ERROR_NONE) {
243         LOGE("Failed to delete sticker information : %d", ret);
244         return STICKER_ERROR_OPERATION_FAILED;
245     }
246
247     return STICKER_ERROR_NONE;
248 }
249
250 EXPORT_API int sticker_provider_get_sticker_count(sticker_provider_h provider_handle, int *count)
251 {
252     int ret;
253     char *app_id = NULL;
254
255     if (!provider_handle)
256         return STICKER_ERROR_INVALID_PARAMETER;
257
258     ret = app_get_id(&app_id);
259     if (ret != APP_ERROR_NONE) {
260         LOGE("Failed to get app_id : %d", ret);
261         ret = STICKER_ERROR_OPERATION_FAILED;
262         goto cleanup;
263     }
264
265     ret = sticker_dbus_get_sticker_count(provider_handle->gdbus_connection, app_id, count);
266     if (ret != STICKER_ERROR_NONE) {
267         LOGE("Failed to get sticker count : %d", ret);
268         ret = STICKER_ERROR_OPERATION_FAILED;
269     }
270
271 cleanup:
272     if (app_id)
273         free(app_id);
274
275     return ret;
276 }
277
278 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)
279 {
280     int ret;
281     int info_id;
282     int sticker_count = 0;
283     char *app_id = NULL;
284     GVariantIter *id_iter = NULL;
285
286     if (!provider_handle || (offset < 0) || (count <= 0) || !result || !callback)
287         return STICKER_ERROR_INVALID_PARAMETER;
288
289     ret = app_get_id(&app_id);
290     if (ret != APP_ERROR_NONE) {
291         LOGE("Failed to get app_id : %d", ret);
292         ret = STICKER_ERROR_OPERATION_FAILED;
293         goto cleanup;
294     }
295
296     ret = sticker_dbus_get_sticker_info_by_appid(provider_handle->gdbus_connection, app_id, offset, count, &id_iter);
297     if (ret != STICKER_ERROR_NONE) {
298         LOGE("Failed to get sticker information : %d", ret);
299         ret = STICKER_ERROR_OPERATION_FAILED;
300         goto cleanup;
301     }
302
303     if (id_iter) {
304         while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
305             sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
306             if (!sticker_data) {
307                 ret = STICKER_ERROR_OUT_OF_MEMORY;
308                 goto cleanup;
309             }
310
311             ret = sticker_dbus_get_sticker_info_by_record_id(provider_handle->gdbus_connection, sticker_data, info_id);
312             if (ret == STICKER_ERROR_NONE) {
313                 sticker_count++;
314                 callback(sticker_data, user_data);
315                 _free_sticker_data(sticker_data);
316             } else {
317                 _free_sticker_data(sticker_data);
318                 goto cleanup;
319             }
320         }
321     }
322
323     *result = sticker_count;
324
325 cleanup:
326     if (app_id)
327         free(app_id);
328
329     if (id_iter)
330         g_variant_iter_free(id_iter);
331
332     return ret;
333 }