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");
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);
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");
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) {
164 LOGE("Failed to get app_id : %d", ret);
165 free(consumer_struct);
166 return STICKER_ERROR_OPERATION_FAILED;
169 ret = sticker_dbus_init(&consumer_struct->gdbus_connection, &consumer_struct->server_watcher_id,
170 &consumer_struct->monitor_id, &consumer_struct->server_monitor_id, STICKER_CLIENT_LIB_CONSUMER, (void *)consumer_struct);
171 if (ret != STICKER_ERROR_NONE) {
172 LOGE("Failed to initialize dbus : %d", ret);
173 free(consumer_struct);
174 return STICKER_ERROR_OPERATION_FAILED;
177 *consumer_handle = consumer_struct;
179 return STICKER_ERROR_NONE;
182 EXPORT_API int sticker_consumer_destroy(sticker_consumer_h consumer_handle)
184 CHECK_STICKER_FEATURE();
187 if (!consumer_handle)
188 return STICKER_ERROR_INVALID_PARAMETER;
190 LOGD("consumer_handle : %p", consumer_handle);
191 ret = sticker_dbus_shutdown(consumer_handle->gdbus_connection, &consumer_handle->server_watcher_id,
192 &consumer_handle->server_monitor_id, &consumer_handle->monitor_id, STICKER_CLIENT_LIB_CONSUMER);
193 if (ret != STICKER_ERROR_NONE) {
194 LOGE("Failed to finalize dbus : %d", ret);
195 free(consumer_handle);
196 return STICKER_ERROR_OPERATION_FAILED;
199 if (consumer_handle->gdbus_connection)
200 g_object_unref(consumer_handle->gdbus_connection);
202 if (consumer_handle->app_id) {
203 free(consumer_handle->app_id);
204 consumer_handle->app_id = NULL;
207 free(consumer_handle);
209 return STICKER_ERROR_NONE;
212 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)
214 CHECK_STICKER_FEATURE();
218 int sticker_count = 0;
219 GVariantIter *id_iter = NULL;
221 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !callback)
222 return STICKER_ERROR_INVALID_PARAMETER;
224 ret = sticker_dbus_get_all_sticker_info(consumer_handle->gdbus_connection, consumer_handle->app_id, offset, count, &id_iter);
225 if (ret != STICKER_ERROR_NONE) {
226 LOGE("Failed to get all sticker information : %d", ret);
227 ret = STICKER_ERROR_OPERATION_FAILED;
232 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
233 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
235 ret = STICKER_ERROR_OUT_OF_MEMORY;
239 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
240 if (ret == STICKER_ERROR_NONE) {
242 callback(sticker_data, user_data);
243 _free_sticker_data(sticker_data);
245 _free_sticker_data(sticker_data);
251 *result = sticker_count;
255 g_variant_iter_free(id_iter);
260 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)
262 CHECK_STICKER_FEATURE();
266 int sticker_count = 0;
267 GVariantIter *id_iter = NULL;
269 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !keyword || !callback)
270 return STICKER_ERROR_INVALID_PARAMETER;
272 ret = sticker_dbus_get_sticker_info_by_keyword(consumer_handle->gdbus_connection, consumer_handle->app_id, keyword, offset, count, &id_iter);
273 if (ret != STICKER_ERROR_NONE) {
274 LOGE("Failed to get sticker information by keyword : %d", ret);
275 ret = STICKER_ERROR_OPERATION_FAILED;
280 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
281 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
283 ret = STICKER_ERROR_OUT_OF_MEMORY;
287 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
288 if (ret == STICKER_ERROR_NONE) {
290 callback(sticker_data, user_data);
291 _free_sticker_data(sticker_data);
293 _free_sticker_data(sticker_data);
299 *result = sticker_count;
303 g_variant_iter_free(id_iter);
308 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)
310 CHECK_STICKER_FEATURE();
314 int sticker_count = 0;
315 GVariantIter *id_iter = NULL;
317 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || !group || !callback)
318 return STICKER_ERROR_INVALID_PARAMETER;
320 ret = sticker_dbus_get_sticker_info_by_group(consumer_handle->gdbus_connection, consumer_handle->app_id, group, offset, count, &id_iter);
321 if (ret != STICKER_ERROR_NONE) {
322 LOGE("Failed to get sticker information by group : %d", ret);
323 ret = STICKER_ERROR_OPERATION_FAILED;
328 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
329 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
331 ret = STICKER_ERROR_OUT_OF_MEMORY;
335 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
336 if (ret == STICKER_ERROR_NONE) {
338 callback(sticker_data, user_data);
339 _free_sticker_data(sticker_data);
341 _free_sticker_data(sticker_data);
347 *result = sticker_count;
351 g_variant_iter_free(id_iter);
356 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)
358 CHECK_STICKER_FEATURE();
362 int sticker_count = 0;
363 GVariantIter *id_iter = NULL;
365 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
366 return STICKER_ERROR_INVALID_PARAMETER;
368 ret = sticker_dbus_get_sticker_info_by_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, offset, count, &id_iter);
369 if (ret != STICKER_ERROR_NONE) {
370 LOGE("Failed to get sticker information by uri type : %d", ret);
371 ret = STICKER_ERROR_OPERATION_FAILED;
376 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
377 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
379 ret = STICKER_ERROR_OUT_OF_MEMORY;
383 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
384 if (ret == STICKER_ERROR_NONE) {
386 callback(sticker_data, user_data);
387 _free_sticker_data(sticker_data);
389 _free_sticker_data(sticker_data);
395 *result = sticker_count;
399 g_variant_iter_free(id_iter);
404 EXPORT_API int sticker_consumer_group_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_group_list_foreach_cb callback, void *user_data)
406 CHECK_STICKER_FEATURE();
411 if (!consumer_handle || !callback)
412 return STICKER_ERROR_INVALID_PARAMETER;
414 ret = sticker_dbus_get_group_list(consumer_handle->gdbus_connection, consumer_handle->app_id, &list);
415 if (ret != STICKER_ERROR_NONE) {
416 LOGE("Failed to get group 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);
432 EXPORT_API int sticker_consumer_keyword_list_foreach_all(sticker_consumer_h consumer_handle, sticker_consumer_keyword_list_foreach_cb callback, void *user_data)
434 CHECK_STICKER_FEATURE();
439 if (!consumer_handle || !callback)
440 return STICKER_ERROR_INVALID_PARAMETER;
442 ret = sticker_dbus_get_keyword_list(consumer_handle->gdbus_connection, consumer_handle->app_id, &list);
443 if (ret != STICKER_ERROR_NONE) {
444 LOGE("Failed to get keyword list : %d", ret);
445 ret = STICKER_ERROR_OPERATION_FAILED;
449 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
450 callback((const char *)tmp->data, user_data);
455 g_list_free_full(list, free);
460 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)
462 CHECK_STICKER_FEATURE();
466 int sticker_count = 0;
467 GVariantIter *id_iter = NULL;
469 if (!consumer_handle || (offset < 0) || (count <= 0) || !result || (type < 1) || !callback)
470 return STICKER_ERROR_INVALID_PARAMETER;
472 ret = sticker_dbus_get_sticker_info_by_display_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, offset, count, &id_iter);
473 if (ret != STICKER_ERROR_NONE) {
474 LOGE("Failed to get sticker information by display type : %d", ret);
475 ret = STICKER_ERROR_OPERATION_FAILED;
480 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
481 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
483 ret = STICKER_ERROR_OUT_OF_MEMORY;
487 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
488 if (ret == STICKER_ERROR_NONE) {
490 callback(sticker_data, user_data);
491 _free_sticker_data(sticker_data);
493 _free_sticker_data(sticker_data);
499 *result = sticker_count;
503 g_variant_iter_free(id_iter);
508 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)
510 CHECK_STICKER_FEATURE();
515 if (!consumer_handle || (type < 1) || !callback)
516 return STICKER_ERROR_INVALID_PARAMETER;
518 ret = sticker_dbus_get_group_list_by_display_type(consumer_handle->gdbus_connection, consumer_handle->app_id, type, &list);
519 if (ret != STICKER_ERROR_NONE) {
520 LOGE("Failed to get group list : %d", ret);
521 ret = STICKER_ERROR_OPERATION_FAILED;
525 for(GList *tmp = g_list_first(list); tmp != NULL; tmp=tmp->next) {
526 callback((const char *)tmp->data, user_data);
531 g_list_free_full(list, free);
536 EXPORT_API int sticker_consumer_add_recent_data(sticker_consumer_h consumer_handle, sticker_data_h data_handle)
538 CHECK_STICKER_FEATURE();
542 if (!consumer_handle || !data_handle || (data_handle->sticker_info_id > 0) || !data_handle->uri)
543 return STICKER_ERROR_INVALID_PARAMETER;
545 ret = sticker_dbus_insert_recent_sticker_info(consumer_handle->gdbus_connection, data_handle->sticker_info_id);
546 if (ret != STICKER_ERROR_NONE) {
547 LOGE("Failed to add recent sticker information : %d", ret);
548 return STICKER_ERROR_OPERATION_FAILED;
551 return STICKER_ERROR_NONE;
554 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)
556 CHECK_STICKER_FEATURE();
560 int sticker_count = 0;
561 GVariantIter *id_iter = NULL;
563 if (!consumer_handle || (count <= 0) || !result || !callback)
564 return STICKER_ERROR_INVALID_PARAMETER;
566 ret = sticker_dbus_get_recent_sticker_list(consumer_handle->gdbus_connection, count, &id_iter);
567 if (ret != STICKER_ERROR_NONE) {
568 LOGE("Failed to get recent sticker information : %d", ret);
569 ret = STICKER_ERROR_OPERATION_FAILED;
574 while (g_variant_iter_loop (id_iter, "(i)", &info_id)) {
575 sticker_data_h sticker_data = (sticker_data_h)calloc(1, sizeof(struct sticker_data_s));
577 ret = STICKER_ERROR_OUT_OF_MEMORY;
581 ret = sticker_dbus_get_sticker_info_by_record_id(consumer_handle->gdbus_connection, sticker_data, info_id);
582 if (ret == STICKER_ERROR_NONE) {
584 callback(sticker_data, user_data);
585 _free_sticker_data(sticker_data);
587 _free_sticker_data(sticker_data);
593 *result = sticker_count;
597 g_variant_iter_free(id_iter);
602 EXPORT_API int sticker_consumer_set_event_cb(sticker_consumer_h consumer_handle, sticker_consumer_event_cb callback, void *user_data)
604 CHECK_STICKER_FEATURE();
606 if (!consumer_handle || !callback)
607 return STICKER_ERROR_INVALID_PARAMETER;
609 consumer_handle->event_cb = callback;
610 consumer_handle->event_cb_user_data = user_data;
612 return STICKER_ERROR_NONE;
615 EXPORT_API int sticker_consumer_unset_event_cb(sticker_consumer_h consumer_handle)
617 CHECK_STICKER_FEATURE();
619 if (!consumer_handle)
620 return STICKER_ERROR_INVALID_PARAMETER;
622 consumer_handle->event_cb = NULL;
623 consumer_handle->event_cb_user_data = NULL;
625 return STICKER_ERROR_NONE;