summaryrefslogtreecommitdiff
path: root/src/notifications/noti_event_manager.c
blob: 609d7b667d0eb838f3735df5d12331e046191b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2013 - 2016 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common/log.h"
#include "common/callback_handler.h"
#include "notifications/noti_event_manager.h"

#include <stdlib.h>

static struct __noti_event_s {
	callback_h *changed_cb_handler;
} s_info = {
	.changed_cb_handler = NULL,
};

static void __notification_changed_cb(void *data, notification_type_e type, notification_op *operation_list, int operation_count);

bool noti_event_manager_init(void)
{
	_D("%s", __func__);

	int ret = NOTIFICATION_ERROR_NONE;

	ret = notification_register_detailed_changed_cb(__notification_changed_cb, NULL);

	if (ret != NOTIFICATION_ERROR_NONE) {
		_E("notification_register_detailed_changed_cb failed [%d]", ret);
		return false;
	}

	return true;
}

void noti_event_manager_fini(void)
{
	callback_handler_free(s_info.changed_cb_handler);
	s_info.changed_cb_handler = NULL;

	int ret = NOTIFICATION_ERROR_NONE;
	ret = notification_unregister_detailed_changed_cb(__notification_changed_cb, NULL);

	if (ret != NOTIFICATION_ERROR_NONE) {
		_E("notification_unregister_detailed_changed_cb failed [%d]", ret);
	}
}

void noti_event_manager_add_changed_cb(void(*cb)(void *noti_info))
{
	if (s_info.changed_cb_handler == NULL) {
		s_info.changed_cb_handler = callback_handler_new("noti_manager");
	}

	callback_handler_add_cb(s_info.changed_cb_handler, cb);
}

void noti_event_manager_del_changed_cb(void(*cb)(void *noti_info))
{
	callback_handler_del_cb(s_info.changed_cb_handler, cb);
}

static void __notification_changed_cb(void *data, notification_type_e type, notification_op *operation_list, int operation_count)
{
	if (operation_list == NULL || operation_count < 1) {
		_E("Invalid parameter");
		return;
	}

	int i = 0;
	noti_message_t *info = (noti_message_t *)malloc(sizeof(noti_message_t));
	if (info == NULL) {
		_E("failed to malloc");
		return ;
	}

	info->id = 0;
	info->noti_handler = NULL;
	info->operation_type = NOTIFICATION_OP_NONE;
	for (i = 0; i < operation_count; i++) {
		notification_op_get_data(operation_list + i, NOTIFICATION_OP_DATA_NOTI, &info->noti_handler);
		notification_op_get_data(operation_list + i, NOTIFICATION_OP_DATA_PRIV_ID, &info->id);
		notification_op_get_data(operation_list + i, NOTIFICATION_OP_DATA_TYPE, &info->operation_type);

		_D("notification id [%d], operation type [%d]", info->id, info->operation_type);
		callback_handler_execute_cb(s_info.changed_cb_handler, info);
	}
}