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 <app_common.h>
21 #include <cynara-client.h>
22 #include <cynara-error.h>
23 #include <cynara-session.h>
25 #include "sticker_consumer.h"
26 #include "sticker_consumer_main.h"
27 #include "sticker_dbus.h"
32 #define LOG_TAG "STICKER_CONSUMER"
34 static cynara *p_cynara = NULL;
36 static void _free_sticker_data(sticker_data_h sticker_data)
38 if (sticker_data->app_id) {
39 free(sticker_data->app_id);
40 sticker_data->app_id = NULL;
43 if (sticker_data->uri) {
44 free(sticker_data->uri);
45 sticker_data->uri = NULL;
48 if (sticker_data->thumbnail) {
49 free(sticker_data->thumbnail);
50 sticker_data->thumbnail = NULL;
53 if (sticker_data->keyword) {
54 g_list_free_full(sticker_data->keyword, free);
55 sticker_data->keyword = NULL;
58 if (sticker_data->group) {
59 free(sticker_data->group);
60 sticker_data->group = NULL;
63 if (sticker_data->description) {
64 free(sticker_data->description);
65 sticker_data->description = NULL;
68 if (sticker_data->date) {
69 free(sticker_data->date);
70 sticker_data->date = NULL;
77 static int _cynara_initialize()
79 int ret = cynara_initialize(&p_cynara, NULL);
80 if (ret != CYNARA_API_SUCCESS)
81 LOGE("Failed to cynara initialize"); //LCOV_EXCL_LINE
86 static int _check_privilege(const char *uid, const char *privilege)
90 char label_path[1024] = "/proc/self/attr/current";
91 char smack_label[1024] = {'\0',};
97 fp = fopen(label_path, "r");
99 ret = fread(smack_label, 1, sizeof(smack_label), fp);
101 LOGE("Failed to fread");
106 pid_t pid = getpid();
107 char *session = cynara_session_from_pid(pid);
108 ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
112 if (ret != CYNARA_API_ACCESS_ALLOWED) {
113 LOGE("Access denied. The result of cynara_check() : %d.", ret); //LCOV_EXCL_LINE
120 static void _cynara_deinitialize()
123 cynara_finish(p_cynara);
128 static int _sticker_check_privilege() {
130 int ret = STICKER_ERROR_NONE;
132 if (_cynara_initialize() != CYNARA_API_SUCCESS)
133 return STICKER_ERROR_PERMISSION_DENIED;
135 snprintf(uid, 16, "%d", getuid());
136 if (_check_privilege(uid, STICKER_PRIVILEGE_MEDIASTORAGE) < 0) {
137 LOGE("Permission is denied"); //LCOV_EXCL_LINE
138 ret = STICKER_ERROR_PERMISSION_DENIED;
141 _cynara_deinitialize();
146 EXPORT_API int sticker_consumer_create(sticker_consumer_h *consumer_handle)
148 CHECK_STICKER_FEATURE();
151 if (!consumer_handle)
152 return STICKER_ERROR_INVALID_PARAMETER;
154 if (_sticker_check_privilege() != STICKER_ERROR_NONE)
155 return STICKER_ERROR_PERMISSION_DENIED;
157 struct sticker_consumer_s *consumer_struct = (sticker_consumer_h)calloc(1, sizeof(struct sticker_consumer_s));
159 if (!consumer_struct)
160 return STICKER_ERROR_OUT_OF_MEMORY;
162 ret = app_get_id(&consumer_struct->app_id);
163 if (ret != APP_ERROR_NONE) {
165 LOGE("Failed to get app_id : %d", ret);
166 free(consumer_struct);
167 return STICKER_ERROR_OPERATION_FAILED;
171 ret = sticker_dbus_init(&consumer_struct->gdbus_connection, &consumer_struct->server_watcher_id,
172 &consumer_struct->monitor_id, &consumer_struct->server_monitor_id, STICKER_CLIENT_LIB_CONSUMER, (void *)consumer_struct);
173 if (ret != STICKER_ERROR_NONE) {
175 LOGE("Failed to initialize dbus : %d", ret);
176 free(consumer_struct);
177 return STICKER_ERROR_OPERATION_FAILED;
181 *consumer_handle = consumer_struct;
183 return STICKER_ERROR_NONE;
186 EXPORT_API int sticker_consumer_destroy(sticker_consumer_h consumer_handle)
188 CHECK_STICKER_FEATURE();
191 if (!consumer_handle)
192 return STICKER_ERROR_INVALID_PARAMETER;
194 LOGD("consumer_handle : %p", consumer_handle);
195 ret = sticker_dbus_shutdown(consumer_handle->gdbus_connection, &consumer_handle->server_watcher_id,
196 &consumer_handle->server_monitor_id, &consumer_handle->monitor_id, STICKER_CLIENT_LIB_CONSUMER);
197 if (ret != STICKER_ERROR_NONE) {
199 LOGE("Failed to finalize dbus : %d", ret);
200 free(consumer_handle);
201 return STICKER_ERROR_OPERATION_FAILED;
205 if (consumer_handle->gdbus_connection)
206 g_object_unref(consumer_handle->gdbus_connection);
208 if (consumer_handle->app_id) {
209 free(consumer_handle->app_id);
210 consumer_handle->app_id = NULL;
213 free(consumer_handle);
215 return STICKER_ERROR_NONE;
218 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)
220 CHECK_STICKER_FEATURE();
224 int sticker_count = 0;
225 GVariantIter *id_iter = NULL;
227 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !callback)
228 return STICKER_ERROR_INVALID_PARAMETER;
230 ret = sticker_dbus_get_all_sticker_info(consumer_handle->gdbus_connection, consumer_handle->app_id, offset, count, &id_iter);
231 if (ret != STICKER_ERROR_NONE) {
232 LOGE("Failed to get all sticker information : %d", ret);
233 ret = STICKER_ERROR_OPERATION_FAILED;
238 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
239 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
241 ret = STICKER_ERROR_OUT_OF_MEMORY;
245 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
246 if (ret == STICKER_ERROR_NONE) {
248 callback(sticker_data, user_data);
249 _free_sticker_data(sticker_data);
251 _free_sticker_data(sticker_data);
257 *result = sticker_count;
261 g_variant_iter_free(id_iter);
266 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)
268 CHECK_STICKER_FEATURE();
272 int sticker_count = 0;
273 GVariantIter *id_iter = NULL;
275 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !keyword || !callback)
276 return STICKER_ERROR_INVALID_PARAMETER;
278 ret = sticker_dbus_get_sticker_info_by_keyword(consumer_handle->gdbus_connection, consumer_handle->app_id, keyword, offset, count, &id_iter);
279 if (ret != STICKER_ERROR_NONE) {
280 LOGE("Failed to get sticker information by keyword : %d", ret);
281 ret = STICKER_ERROR_OPERATION_FAILED;
286 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
287 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
289 ret = STICKER_ERROR_OUT_OF_MEMORY;
293 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
294 if (ret == STICKER_ERROR_NONE) {
296 callback(sticker_data, user_data);
297 _free_sticker_data(sticker_data);
299 _free_sticker_data(sticker_data);
305 *result = sticker_count;
309 g_variant_iter_free(id_iter);
314 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)
316 CHECK_STICKER_FEATURE();
320 int sticker_count = 0;
321 GVariantIter *id_iter = NULL;
323 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !group || !callback)
324 return STICKER_ERROR_INVALID_PARAMETER;
326 ret = sticker_dbus_get_sticker_info_by_group(consumer_handle->gdbus_connection, consumer_handle->app_id, group, offset, count, &id_iter);
327 if (ret != STICKER_ERROR_NONE) {
328 LOGE("Failed to get sticker information by group : %d", ret);
329 ret = STICKER_ERROR_OPERATION_FAILED;
334 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
335 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
337 ret = STICKER_ERROR_OUT_OF_MEMORY;
341 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
342 if (ret == STICKER_ERROR_NONE) {
344 callback(sticker_data, user_data);
345 _free_sticker_data(sticker_data);
347 _free_sticker_data(sticker_data);
353 *result = sticker_count;
357 g_variant_iter_free(id_iter);
362 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)
364 CHECK_STICKER_FEATURE();
368 int sticker_count = 0;
369 GVariantIter *id_iter = NULL;
371 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
372 return STICKER_ERROR_INVALID_PARAMETER;
374 ret = sticker_dbus_get_sticker_info_by_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, offset, count, &id_iter);
375 if (ret != STICKER_ERROR_NONE) {
376 LOGE("Failed to get sticker information by uri type : %d", ret);
377 ret = STICKER_ERROR_OPERATION_FAILED;
382 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
383 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
385 ret = STICKER_ERROR_OUT_OF_MEMORY;
389 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
390 if (ret == STICKER_ERROR_NONE) {
392 callback(sticker_data, user_data);
393 _free_sticker_data(sticker_data);
395 _free_sticker_data(sticker_data);
401 *result = sticker_count;
405 g_variant_iter_free(id_iter);
410 EXPORT_API int sticker_consumer_group_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_group_list_foreach_cb callback, void *user_data)
412 CHECK_STICKER_FEATURE();
417 if (!consumer_handle || !callback)
418 return STICKER_ERROR_INVALID_PARAMETER;
420 ret = sticker_dbus_get_group_list(consumer_handle->gdbus_connection, consumer_handle->app_id, &list);
421 if (ret != STICKER_ERROR_NONE) {
422 LOGE("Failed to get group list : %d", ret);
423 ret = STICKER_ERROR_OPERATION_FAILED;
427 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
428 callback((const char *)tmp->data, user_data);
433 g_list_free_full(list, free);
438 EXPORT_API int sticker_consumer_keyword_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_keyword_list_foreach_cb callback, void *user_data)
440 CHECK_STICKER_FEATURE();
445 if (!consumer_handle || !callback)
446 return STICKER_ERROR_INVALID_PARAMETER;
448 ret = sticker_dbus_get_keyword_list(consumer_handle->gdbus_connection, consumer_handle->app_id, &list);
449 if (ret != STICKER_ERROR_NONE) {
450 LOGE("Failed to get keyword list : %d", ret);
451 ret = STICKER_ERROR_OPERATION_FAILED;
455 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
456 callback((const char *)tmp->data, user_data);
461 g_list_free_full(list, free);
466 EXPORT_API int sticker_consumer_data_foreach_by_display_type(sticker_consumer_h consumer_handle, int offset, int count, int *result, sticker_data_display_type_e type, sticker_consumer_data_foreach_cb callback, void *user_data)
468 CHECK_STICKER_FEATURE();
472 int sticker_count = 0;
473 GVariantIter *id_iter = NULL;
475 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
476 return STICKER_ERROR_INVALID_PARAMETER;
478 ret = sticker_dbus_get_sticker_info_by_display_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, offset, count, &id_iter);
479 if (ret != STICKER_ERROR_NONE) {
480 LOGE("Failed to get sticker information by display type : %d", ret);
481 ret = STICKER_ERROR_OPERATION_FAILED;
486 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
487 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
489 ret = STICKER_ERROR_OUT_OF_MEMORY;
493 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
494 if (ret == STICKER_ERROR_NONE) {
496 callback(sticker_data, user_data);
497 _free_sticker_data(sticker_data);
499 _free_sticker_data(sticker_data);
505 *result = sticker_count;
509 g_variant_iter_free(id_iter);
514 EXPORT_API int sticker_consumer_group_list_foreach_by_display_type(sticker_consumer_h consumer_handle, sticker_data_display_type_e type, sticker_consumer_group_list_foreach_cb callback, void *user_data)
516 CHECK_STICKER_FEATURE();
521 if (!consumer_handle || (type < 1) || !callback)
522 return STICKER_ERROR_INVALID_PARAMETER;
524 ret = sticker_dbus_get_group_list_by_display_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, &list);
525 if (ret != STICKER_ERROR_NONE) {
526 LOGE("Failed to get group list : %d", ret);
527 ret = STICKER_ERROR_OPERATION_FAILED;
531 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
532 callback((const char *)tmp->data, user_data);
537 g_list_free_full(list, free);
542 EXPORT_API int sticker_consumer_add_recent_data(sticker_consumer_h consumer_handle, sticker_data_h data_handle)
544 CHECK_STICKER_FEATURE();
548 if (!consumer_handle || !data_handle || (data_handle->sticker_info_id <= 0) || !data_handle->uri)
549 return STICKER_ERROR_INVALID_PARAMETER;
551 ret = sticker_dbus_insert_recent_sticker_info(consumer_handle->gdbus_connection, data_handle->sticker_info_id);
552 if (ret != STICKER_ERROR_NONE) {
553 LOGE("Failed to add recent sticker information : %d", ret);
554 return STICKER_ERROR_OPERATION_FAILED;
557 return STICKER_ERROR_NONE;
560 EXPORT_API int sticker_consumer_get_recent_data_list(sticker_consumer_h consumer_handle, int count, int *result, sticker_consumer_data_foreach_cb callback, void *user_data)
562 CHECK_STICKER_FEATURE();
566 int sticker_count = 0;
567 GVariantIter *id_iter = NULL;
569 if (!consumer_handle || (count <= 0) || !result || !callback)
570 return STICKER_ERROR_INVALID_PARAMETER;
572 ret = sticker_dbus_get_recent_sticker_list(consumer_handle->gdbus_connection, count, &id_iter);
573 if (ret != STICKER_ERROR_NONE) {
574 LOGE("Failed to get recent sticker information : %d", ret);
575 ret = STICKER_ERROR_OPERATION_FAILED;
580 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
581 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
583 ret = STICKER_ERROR_OUT_OF_MEMORY;
587 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
588 if (ret == STICKER_ERROR_NONE) {
590 callback(sticker_data, user_data);
591 _free_sticker_data(sticker_data);
593 _free_sticker_data(sticker_data);
599 *result = sticker_count;
603 g_variant_iter_free(id_iter);
608 EXPORT_API int sticker_consumer_set_event_cb(sticker_consumer_h consumer_handle, sticker_consumer_event_cb callback, void *user_data)
610 CHECK_STICKER_FEATURE();
612 if (!consumer_handle || !callback)
613 return STICKER_ERROR_INVALID_PARAMETER;
615 consumer_handle->event_cb = callback;
616 consumer_handle->event_cb_user_data = user_data;
618 return STICKER_ERROR_NONE;
621 EXPORT_API int sticker_consumer_unset_event_cb(sticker_consumer_h consumer_handle)
623 CHECK_STICKER_FEATURE();
625 if (!consumer_handle)
626 return STICKER_ERROR_INVALID_PARAMETER;
628 consumer_handle->event_cb = NULL;
629 consumer_handle->event_cb_user_data = NULL;
631 return STICKER_ERROR_NONE;