5 * Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
40 static guint listener_id = 0;
41 static GSList *name_listeners = NULL;
43 struct name_callback {
44 GDBusWatchFunction conn_func;
45 GDBusWatchFunction disc_func;
51 DBusConnection *connection;
56 static struct name_data *name_data_find(DBusConnection *connection,
61 for (current = name_listeners;
62 current != NULL; current = current->next) {
63 struct name_data *data = current->data;
65 if (name == NULL && data->name == NULL) {
66 if (connection == data->connection)
69 if (strcmp(name, data->name) == 0)
77 static struct name_callback *name_callback_find(GSList *callbacks, guint id)
81 for (current = callbacks; current != NULL; current = current->next) {
82 struct name_callback *cb = current->data;
90 static void name_data_call_and_free(struct name_data *data)
94 for (l = data->callbacks; l != NULL; l = l->next) {
95 struct name_callback *cb = l->data;
97 cb->disc_func(data->connection, cb->user_data);
101 g_slist_free(data->callbacks);
106 static void name_data_free(struct name_data *data)
110 for (l = data->callbacks; l != NULL; l = l->next)
113 g_slist_free(data->callbacks);
118 static int name_data_add(DBusConnection *connection, const char *name,
119 GDBusWatchFunction connect,
120 GDBusWatchFunction disconnect,
121 void *user_data, guint id)
124 struct name_data *data = NULL;
125 struct name_callback *cb = NULL;
127 cb = g_new(struct name_callback, 1);
129 cb->conn_func = connect;
130 cb->disc_func = disconnect;
131 cb->user_data = user_data;
134 data = name_data_find(connection, name);
140 data = g_new0(struct name_data, 1);
142 data->connection = connection;
143 data->name = g_strdup(name);
145 name_listeners = g_slist_append(name_listeners, data);
148 data->callbacks = g_slist_append(data->callbacks, cb);
152 static void name_data_remove(DBusConnection *connection,
153 const char *name, guint id)
155 struct name_data *data;
156 struct name_callback *cb = NULL;
158 data = name_data_find(connection, name);
162 cb = name_callback_find(data->callbacks, id);
164 data->callbacks = g_slist_remove(data->callbacks, cb);
168 if (!data->callbacks) {
169 name_listeners = g_slist_remove(name_listeners, data);
170 name_data_free(data);
174 static gboolean add_match(DBusConnection *connection, const char *name)
177 char match_string[128];
179 snprintf(match_string, sizeof(match_string),
180 "interface=%s,member=NameOwnerChanged,arg0=%s",
181 DBUS_INTERFACE_DBUS, name);
183 dbus_error_init(&err);
185 dbus_bus_add_match(connection, match_string, &err);
187 if (dbus_error_is_set(&err)) {
188 error("Adding match rule \"%s\" failed: %s", match_string,
190 dbus_error_free(&err);
197 static gboolean remove_match(DBusConnection *connection, const char *name)
200 char match_string[128];
202 snprintf(match_string, sizeof(match_string),
203 "interface=%s,member=NameOwnerChanged,arg0=%s",
204 DBUS_INTERFACE_DBUS, name);
206 dbus_error_init(&err);
208 dbus_bus_remove_match(connection, match_string, &err);
210 if (dbus_error_is_set(&err)) {
211 error("Removing owner match rule for %s failed: %s",
213 dbus_error_free(&err);
220 static DBusHandlerResult name_exit_filter(DBusConnection *connection,
221 DBusMessage *message, void *user_data)
224 struct name_data *data;
225 char *name, *old, *new;
228 if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
230 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
232 if (!dbus_message_get_args(message, NULL,
233 DBUS_TYPE_STRING, &name,
234 DBUS_TYPE_STRING, &old,
235 DBUS_TYPE_STRING, &new,
236 DBUS_TYPE_INVALID)) {
237 error("Invalid arguments for NameOwnerChanged signal");
238 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
241 data = name_data_find(connection, name);
243 error("Got NameOwnerChanged signal for %s which has no listeners", name);
244 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
247 for (l = data->callbacks; l != NULL; l = l->next) {
248 struct name_callback *cb = l->data;
251 cb->disc_func(connection, cb->user_data);
254 cb->conn_func(connection, cb->user_data);
256 if (cb->conn_func && cb->disc_func)
261 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263 name_listeners = g_slist_remove(name_listeners, data);
264 name_data_free(data);
266 remove_match(connection, name);
268 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
271 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
272 GDBusWatchFunction connect,
273 GDBusWatchFunction disconnect,
274 void *user_data, GDBusDestroyFunction destroy)
279 if (!dbus_connection_add_filter(connection,
280 name_exit_filter, NULL, NULL)) {
281 error("dbus_connection_add_filter() failed");
287 first = name_data_add(connection, name, connect, disconnect,
288 user_data, listener_id);
289 /* The filter is already added if this is not the first callback
290 * registration for the name */
295 debug("name_listener_add(%s)", name);
297 if (!add_match(connection, name)) {
298 name_data_remove(connection, name, listener_id);
306 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
307 GDBusWatchFunction func,
308 void *user_data, GDBusDestroyFunction destroy)
310 return g_dbus_add_service_watch(connection, name, NULL, func,
314 guint g_dbus_add_signal_watch(DBusConnection *connection,
315 const char *rule, GDBusSignalFunction function,
316 void *user_data, GDBusDestroyFunction destroy)
321 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
323 struct name_data *data;
324 struct name_callback *cb;
330 for (ldata = name_listeners; ldata; ldata = ldata->next) {
332 for (lcb = data->callbacks; lcb; lcb = lcb->next) {
342 data->callbacks = g_slist_remove(data->callbacks, cb);
345 /* Don't remove the filter if other callbacks exist */
350 if (!remove_match(data->connection, data->name))
354 name_listeners = g_slist_remove(name_listeners, data);
355 name_data_free(data);
360 void g_dbus_remove_all_watches(DBusConnection *connection)
362 struct name_data *data;
364 data = name_data_find(connection, NULL);
366 error("name_listener_indicate_disconnect: no listener found");
370 debug("name_listener_indicate_disconnect");
372 name_data_call_and_free(data);