summaryrefslogtreecommitdiff
path: root/src/notification_status.c
blob: 5071851cfe04cc481333abddb2b21aea132d588c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  libnotification
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <vconf.h>
#include <E_DBus.h>

#include <notification.h>
#include <notification_db.h>
#include <notification_noti.h>
#include <notification_debug.h>
#include <notification_private.h>
#include <notification_status.h>

#define PATH_NAME    "/Org/Tizen/System/Notification/Status_message"
#define INTERFACE_NAME "org.tizen.system.notification.status_message"
#define MEMBER_NAME	"status_message"

struct _message_cb_data {
	notification_status_message_cb callback;
	void *data;
	E_DBus_Connection *dbus_connection;
	E_DBus_Signal_Handler *dbus_hdlr;
};

static struct _message_cb_data md;

static void __notification_status_message_dbus_callback(void *data, DBusMessage *msg)
{
	int ret = 0;
	DBusError err;
	char *message = NULL;

	if(data==NULL||msg==NULL)
	{
		NOTIFICATION_ERR("message is NULL");
		return;
	}

	dbus_error_init(&err);
	ret = dbus_message_get_args(msg, &err,
			DBUS_TYPE_STRING, &message,
			DBUS_TYPE_INVALID);
	if(ret == 0)
	{
		NOTIFICATION_ERR("dbus_message_get_args error");
		return;
	}

	if (dbus_error_is_set(&err)) {
		NOTIFICATION_ERR("Dbus err: %s", err.message);
		dbus_error_free(&err);
		return;
	}
	/*if (!md.callback)
		return;

	if (strlen(message) <= 0){
		NOTIFICATION_ERR("message has only NULL");
		return;
	}

	md.callback(message, md.data);*/

	notification_noti_post_toast_message(message);
}

EXPORT_API
int notification_status_message_post(const char *message)
{
	DBusConnection *connection = NULL;
	DBusMessage *signal = NULL;
	DBusError err;
	dbus_bool_t ret;

	if (!message) {
		NOTIFICATION_ERR("message is NULL");
		return NOTIFICATION_ERROR_INVALID_PARAMETER;
	}

	if (strlen(message) <= 0) {
		NOTIFICATION_ERR("message has only NULL");
		return NOTIFICATION_ERROR_INVALID_PARAMETER;
	}

	dbus_error_init(&err);
	connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
	if (!connection) {
		NOTIFICATION_ERR("Fail to dbus_bus_get");
		return NOTIFICATION_ERROR_FROM_DBUS;
	}

	signal =
	    dbus_message_new_signal(PATH_NAME, INTERFACE_NAME,
				    MEMBER_NAME);
	if (!signal) {
		NOTIFICATION_ERR("Fail to dbus_message_new_signal");
		return NOTIFICATION_ERROR_FROM_DBUS;
	}

	ret = dbus_message_append_args(signal,
					   DBUS_TYPE_STRING, &message,
					   DBUS_TYPE_INVALID);
	if (ret) {
		ret = dbus_connection_send(connection, signal, NULL);

		if (ret) {
			dbus_connection_flush(connection);
		}
	}

	dbus_message_unref(signal);
	dbus_connection_unref(connection);

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API
int notification_status_monitor_message_cb_set(notification_status_message_cb callback, void *user_data)
{
	if (!callback)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;
	E_DBus_Connection *dbus_connection;
	E_DBus_Signal_Handler *dbus_handler_size = NULL;

	e_dbus_init();
	dbus_connection = e_dbus_bus_get(DBUS_BUS_SYSTEM);
	if (dbus_connection == NULL) {
		NOTIFICATION_ERR("noti register : failed to get dbus bus");
		return NOTIFICATION_ERROR_FROM_DBUS;
	}
	dbus_handler_size =
		e_dbus_signal_handler_add(dbus_connection, NULL,
			PATH_NAME,
			INTERFACE_NAME, MEMBER_NAME,
			__notification_status_message_dbus_callback,
			user_data);
	if (dbus_handler_size == NULL)
	{
		NOTIFICATION_ERR("fail to add size signal");
		return NOTIFICATION_ERROR_FROM_DBUS;
	}


	md.callback = callback;
	md.data = user_data;
	md.dbus_connection = dbus_connection;
	md.dbus_hdlr = dbus_handler_size;

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API
int notification_status_monitor_message_cb_unset(void)
{
	if (md.dbus_hdlr != NULL) {
			e_dbus_signal_handler_del(md.dbus_connection,
					md.dbus_hdlr);
			md.dbus_hdlr = NULL;
	}
	if (md.dbus_connection != NULL) {
		e_dbus_connection_close(md.dbus_connection);
		md.dbus_connection = NULL;
		e_dbus_shutdown();
	}

	md.callback = NULL;
	md.data = NULL;

	return NOTIFICATION_ERROR_NONE;
}