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)
147 CHECK_STICKER_FEATURE();
150 if (!consumer_handle)
151 return STICKER_ERROR_INVALID_PARAMETER;
153 if (_sticker_check_privilege() != STICKER_ERROR_NONE)
154 return STICKER_ERROR_PERMISSION_DENIED;
156 struct sticker_consumer_s *consumer_struct = (sticker_consumer_h)calloc(1, sizeof(struct sticker_consumer_s));
158 if (!consumer_struct)
159 return STICKER_ERROR_OUT_OF_MEMORY;
161 ret = sticker_dbus_init(&consumer_struct->gdbus_connection, &consumer_struct->server_watcher_id,
162 &consumer_struct->monitor_id, &consumer_struct->server_monitor_id, STICKER_CLIENT_LIB_CONSUMER, (void *)consumer_struct);
163 if (ret != STICKER_ERROR_NONE) {
164 LOGE("Failed to initialize dbus : %d", ret);
165 free(consumer_struct);
166 return STICKER_ERROR_OPERATION_FAILED;
169 *consumer_handle = consumer_struct;
171 return STICKER_ERROR_NONE;
174 EXPORT_API int sticker_consumer_destroy(sticker_consumer_h consumer_handle)
176 CHECK_STICKER_FEATURE();
179 if (!consumer_handle)
180 return STICKER_ERROR_INVALID_PARAMETER;
182 LOGD("consumer_handle : %p", consumer_handle);
183 ret = sticker_dbus_shutdown(consumer_handle->gdbus_connection, &consumer_handle->server_watcher_id,
184 &consumer_handle->server_monitor_id, &consumer_handle->monitor_id);
185 if (ret != STICKER_ERROR_NONE) {
186 LOGE("Failed to finalize dbus : %d", ret);
187 free(consumer_handle);
188 return STICKER_ERROR_OPERATION_FAILED;
191 if (consumer_handle->gdbus_connection)
192 g_object_unref(consumer_handle->gdbus_connection);
194 free(consumer_handle);
196 return STICKER_ERROR_NONE;
199 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)
201 CHECK_STICKER_FEATURE();
205 int sticker_count = 0;
206 GVariantIter *id_iter = NULL;
208 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !callback)
209 return STICKER_ERROR_INVALID_PARAMETER;
211 ret = sticker_dbus_get_all_sticker_info(consumer_handle->gdbus_connection, offset, count, &id_iter);
212 if (ret != STICKER_ERROR_NONE) {
213 LOGE("Failed to get all sticker information : %d", ret);
214 ret = STICKER_ERROR_OPERATION_FAILED;
219 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
220 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
222 ret = STICKER_ERROR_OUT_OF_MEMORY;
226 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
227 if (ret == STICKER_ERROR_NONE) {
229 callback(sticker_data, user_data);
230 _free_sticker_data(sticker_data);
232 _free_sticker_data(sticker_data);
238 *result = sticker_count;
242 g_variant_iter_free(id_iter);
247 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)
249 CHECK_STICKER_FEATURE();
253 int sticker_count = 0;
254 GVariantIter *id_iter = NULL;
256 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !keyword || !callback)
257 return STICKER_ERROR_INVALID_PARAMETER;
259 ret = sticker_dbus_get_sticker_info_by_keyword(consumer_handle->gdbus_connection, keyword, offset, count, &id_iter);
260 if (ret != STICKER_ERROR_NONE) {
261 LOGE("Failed to get sticker information by keyword : %d", ret);
262 ret = STICKER_ERROR_OPERATION_FAILED;
267 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
268 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
270 ret = STICKER_ERROR_OUT_OF_MEMORY;
274 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
275 if (ret == STICKER_ERROR_NONE) {
277 callback(sticker_data, user_data);
278 _free_sticker_data(sticker_data);
280 _free_sticker_data(sticker_data);
286 *result = sticker_count;
290 g_variant_iter_free(id_iter);
295 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)
297 CHECK_STICKER_FEATURE();
301 int sticker_count = 0;
302 GVariantIter *id_iter = NULL;
304 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !group || !callback)
305 return STICKER_ERROR_INVALID_PARAMETER;
307 ret = sticker_dbus_get_sticker_info_by_group(consumer_handle->gdbus_connection, group, offset, count, &id_iter);
308 if (ret != STICKER_ERROR_NONE) {
309 LOGE("Failed to get sticker information by group : %d", ret);
310 ret = STICKER_ERROR_OPERATION_FAILED;
315 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
316 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
318 ret = STICKER_ERROR_OUT_OF_MEMORY;
322 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
323 if (ret == STICKER_ERROR_NONE) {
325 callback(sticker_data, user_data);
326 _free_sticker_data(sticker_data);
328 _free_sticker_data(sticker_data);
334 *result = sticker_count;
338 g_variant_iter_free(id_iter);
343 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)
345 CHECK_STICKER_FEATURE();
349 int sticker_count = 0;
350 GVariantIter *id_iter = NULL;
352 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
353 return STICKER_ERROR_INVALID_PARAMETER;
355 ret = sticker_dbus_get_sticker_info_by_type(consumer_handle->gdbus_connection, type, offset, count, &id_iter);
356 if (ret != STICKER_ERROR_NONE) {
357 LOGE("Failed to get sticker information by group : %d", ret);
358 ret = STICKER_ERROR_OPERATION_FAILED;
363 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
364 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
366 ret = STICKER_ERROR_OUT_OF_MEMORY;
370 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
371 if (ret == STICKER_ERROR_NONE) {
373 callback(sticker_data, user_data);
374 _free_sticker_data(sticker_data);
376 _free_sticker_data(sticker_data);
382 *result = sticker_count;
386 g_variant_iter_free(id_iter);
391 EXPORT_API int sticker_consumer_group_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_group_list_foreach_cb callback, void *user_data)
393 CHECK_STICKER_FEATURE();
398 if (!consumer_handle || !callback)
399 return STICKER_ERROR_INVALID_PARAMETER;
401 ret = sticker_dbus_get_group_list(consumer_handle->gdbus_connection, &list);
402 if (ret != STICKER_ERROR_NONE) {
403 LOGE("Failed to get group list : %d", ret);
404 ret = STICKER_ERROR_OPERATION_FAILED;
408 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
409 callback((const char *)tmp->data, user_data);
414 g_list_free_full(list, free);
419 EXPORT_API int sticker_consumer_keyword_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_keyword_list_foreach_cb callback, void *user_data)
421 CHECK_STICKER_FEATURE();
426 if (!consumer_handle || !callback)
427 return STICKER_ERROR_INVALID_PARAMETER;
429 ret = sticker_dbus_get_keyword_list(consumer_handle->gdbus_connection, &list);
430 if (ret != STICKER_ERROR_NONE) {
431 LOGE("Failed to get keyword list : %d", ret);
432 ret = STICKER_ERROR_OPERATION_FAILED;
436 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
437 callback((const char *)tmp->data, user_data);
442 g_list_free_full(list, free);