2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dbus/dbus.h>
20 #include <cynara-client.h>
21 #include <cynara-error.h>
22 #include <cynara-session.h>
24 #include "sticker_consumer.h"
25 #include "sticker_consumer_main.h"
26 #include "sticker_dbus.h"
31 #define LOG_TAG "STICKER_CONSUMER"
33 static cynara *p_cynara = NULL;
35 static void _free_sticker_data(sticker_data_h sticker_data)
37 if (sticker_data->app_id) {
38 free(sticker_data->app_id);
39 sticker_data->app_id = NULL;
42 if (sticker_data->uri) {
43 free(sticker_data->uri);
44 sticker_data->uri = NULL;
47 if (sticker_data->thumbnail) {
48 free(sticker_data->thumbnail);
49 sticker_data->thumbnail = NULL;
52 if (sticker_data->keyword) {
53 g_list_free_full(sticker_data->keyword, free);
54 sticker_data->keyword = NULL;
57 if (sticker_data->group) {
58 free(sticker_data->group);
59 sticker_data->group = NULL;
62 if (sticker_data->description) {
63 free(sticker_data->description);
64 sticker_data->description = NULL;
67 if (sticker_data->date) {
68 free(sticker_data->date);
69 sticker_data->date = NULL;
76 static int _cynara_initialize()
78 int ret = cynara_initialize(&p_cynara, NULL);
79 if (ret != CYNARA_API_SUCCESS)
80 LOGE("Failed to cynara initialize");
85 static int _check_privilege(const char *uid, const char *privilege)
89 char label_path[1024] = "/proc/self/attr/current";
90 char smack_label[1024] = {'\0',};
96 fp = fopen(label_path, "r");
98 ret = fread(smack_label, 1, sizeof(smack_label), fp);
100 LOGE("Failed to fread");
105 pid_t pid = getpid();
106 char *session = cynara_session_from_pid(pid);
107 ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
111 if (ret != CYNARA_API_ACCESS_ALLOWED) {
112 LOGE("Access denied. The result of cynara_check() : %d.", ret);
119 static void _cynara_deinitialize()
122 cynara_finish(p_cynara);
127 static int _sticker_check_privilege() {
129 int ret = STICKER_ERROR_NONE;
131 if (_cynara_initialize() != CYNARA_API_SUCCESS)
132 return STICKER_ERROR_PERMISSION_DENIED;
134 snprintf(uid, 16, "%d", getuid());
135 if (_check_privilege(uid, STICKER_PRIVILEGE_MEDIASTORAGE) < 0) {
136 LOGE("Permission is denied");
137 ret = STICKER_ERROR_PERMISSION_DENIED;
140 _cynara_deinitialize();
145 EXPORT_API int sticker_consumer_create(sticker_consumer_h *consumer_handle)
149 if (!consumer_handle)
150 return STICKER_ERROR_INVALID_PARAMETER;
152 if (_sticker_check_privilege() != STICKER_ERROR_NONE)
153 return STICKER_ERROR_PERMISSION_DENIED;
155 struct sticker_consumer_s *consumer_struct = (sticker_consumer_h)calloc(1, sizeof(struct sticker_consumer_s));
157 if (!consumer_struct)
158 return STICKER_ERROR_OUT_OF_MEMORY;
160 ret = sticker_dbus_init(&consumer_struct->gdbus_connection, &consumer_struct->server_watcher_id,
161 &consumer_struct->monitor_id, &consumer_struct->server_monitor_id, STICKER_CLIENT_LIB_CONSUMER, (void *)consumer_struct);
162 if (ret != STICKER_ERROR_NONE) {
163 LOGE("Failed to initialize dbus : %d", ret);
164 free(consumer_struct);
165 return STICKER_ERROR_OPERATION_FAILED;
168 *consumer_handle = consumer_struct;
170 return STICKER_ERROR_NONE;
173 EXPORT_API int sticker_consumer_destroy(sticker_consumer_h consumer_handle)
175 LOGD("consumer_handle : %p", consumer_handle);
178 if (!consumer_handle)
179 return STICKER_ERROR_INVALID_PARAMETER;
181 ret = sticker_dbus_shutdown(consumer_handle->gdbus_connection, &consumer_handle->server_monitor_id, &consumer_handle->monitor_id);
182 if (ret != STICKER_ERROR_NONE) {
183 LOGE("Failed to finalize dbus : %d", ret);
184 free(consumer_handle);
185 return STICKER_ERROR_OPERATION_FAILED;
188 if (consumer_handle->gdbus_connection)
189 g_object_unref(consumer_handle->gdbus_connection);
191 free(consumer_handle);
193 return STICKER_ERROR_NONE;
196 EXPORT_API int sticker_consumer_data_foreach_all(sticker_consumer_h consumer_handle, int offset, int count, int *result, sticker_consumer_data_foreach_cb callback, void *user_data)
200 int sticker_count = 0;
201 GVariantIter *id_iter = NULL;
203 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !callback)
204 return STICKER_ERROR_INVALID_PARAMETER;
206 ret = sticker_dbus_get_all_sticker_info(consumer_handle->gdbus_connection, offset, count, &id_iter);
207 if (ret != STICKER_ERROR_NONE) {
208 LOGE("Failed to get all sticker information : %d", ret);
209 ret = STICKER_ERROR_OPERATION_FAILED;
214 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
215 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
217 ret = STICKER_ERROR_OUT_OF_MEMORY;
221 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
222 if (ret == STICKER_ERROR_NONE) {
224 callback(sticker_data, user_data);
225 _free_sticker_data(sticker_data);
227 _free_sticker_data(sticker_data);
233 *result = sticker_count;
237 g_variant_iter_free(id_iter);
242 EXPORT_API int sticker_consumer_data_foreach_by_keyword(sticker_consumer_h consumer_handle, int offset, int count, int *result, const char *keyword, sticker_consumer_data_foreach_cb callback, void *user_data)
246 int sticker_count = 0;
247 GVariantIter *id_iter = NULL;
249 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !keyword || !callback)
250 return STICKER_ERROR_INVALID_PARAMETER;
252 ret = sticker_dbus_get_sticker_info_by_keyword(consumer_handle->gdbus_connection, keyword, offset, count, &id_iter);
253 if (ret != STICKER_ERROR_NONE) {
254 LOGE("Failed to get sticker information by keyword : %d", ret);
255 ret = STICKER_ERROR_OPERATION_FAILED;
260 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
261 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
263 ret = STICKER_ERROR_OUT_OF_MEMORY;
267 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
268 if (ret == STICKER_ERROR_NONE) {
270 callback(sticker_data, user_data);
271 _free_sticker_data(sticker_data);
273 _free_sticker_data(sticker_data);
279 *result = sticker_count;
283 g_variant_iter_free(id_iter);
288 EXPORT_API int sticker_consumer_data_foreach_by_group(sticker_consumer_h consumer_handle, int offset, int count, int *result, const char *group, sticker_consumer_data_foreach_cb callback, void *user_data)
292 int sticker_count = 0;
293 GVariantIter *id_iter = NULL;
295 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !group || !callback)
296 return STICKER_ERROR_INVALID_PARAMETER;
298 ret = sticker_dbus_get_sticker_info_by_group(consumer_handle->gdbus_connection, group, offset, count, &id_iter);
299 if (ret != STICKER_ERROR_NONE) {
300 LOGE("Failed to get sticker information by group : %d", ret);
301 ret = STICKER_ERROR_OPERATION_FAILED;
306 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
307 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
309 ret = STICKER_ERROR_OUT_OF_MEMORY;
313 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
314 if (ret == STICKER_ERROR_NONE) {
316 callback(sticker_data, user_data);
317 _free_sticker_data(sticker_data);
319 _free_sticker_data(sticker_data);
325 *result = sticker_count;
329 g_variant_iter_free(id_iter);
334 EXPORT_API int sticker_consumer_data_foreach_by_type(sticker_consumer_h consumer_handle, int offset, int count, int *result, sticker_data_uri_type_e type, sticker_consumer_data_foreach_cb callback, void *user_data)
338 int sticker_count = 0;
339 GVariantIter *id_iter = NULL;
341 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
342 return STICKER_ERROR_INVALID_PARAMETER;
344 ret = sticker_dbus_get_sticker_info_by_type(consumer_handle->gdbus_connection, type, offset, count, &id_iter);
345 if (ret != STICKER_ERROR_NONE) {
346 LOGE("Failed to get sticker information by group : %d", ret);
347 ret = STICKER_ERROR_OPERATION_FAILED;
352 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
353 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
355 ret = STICKER_ERROR_OUT_OF_MEMORY;
359 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
360 if (ret == STICKER_ERROR_NONE) {
362 callback(sticker_data, user_data);
363 _free_sticker_data(sticker_data);
365 _free_sticker_data(sticker_data);
371 *result = sticker_count;
375 g_variant_iter_free(id_iter);
380 EXPORT_API int sticker_consumer_group_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_group_list_foreach_cb callback, void *user_data)
385 if (!consumer_handle || !callback)
386 return STICKER_ERROR_INVALID_PARAMETER;
388 ret = sticker_dbus_get_group_list(consumer_handle->gdbus_connection, &list);
389 if (ret != STICKER_ERROR_NONE) {
390 LOGE("Failed to get group list : %d", ret);
391 ret = STICKER_ERROR_OPERATION_FAILED;
395 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
396 callback((const char *)tmp->data, user_data);
401 g_list_free_full(list, free);
406 EXPORT_API int sticker_consumer_keyword_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_keyword_list_foreach_cb callback, void *user_data)
411 if (!consumer_handle || !callback)
412 return STICKER_ERROR_INVALID_PARAMETER;
414 ret = sticker_dbus_get_keyword_list(consumer_handle->gdbus_connection, &list);
415 if (ret != STICKER_ERROR_NONE) {
416 LOGE("Failed to get keyword list : %d", ret);
417 ret = STICKER_ERROR_OPERATION_FAILED;
421 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
422 callback((const char *)tmp->data, user_data);
427 g_list_free_full(list, free);