5 * Copyright (C) 2004-2009 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 DBusHandlerResult name_exit_filter(DBusConnection *connection,
41 DBusMessage *message, void *user_data);
43 static guint listener_id = 0;
44 static GSList *name_listeners = NULL;
46 struct name_callback {
47 GDBusWatchFunction conn_func;
48 GDBusWatchFunction disc_func;
54 DBusConnection *connection;
59 static struct name_data *name_data_find(DBusConnection *connection,
64 for (current = name_listeners;
65 current != NULL; current = current->next) {
66 struct name_data *data = current->data;
68 if (connection != data->connection)
71 if (name == NULL || g_str_equal(name, data->name))
78 static struct name_callback *name_callback_find(GSList *callbacks, guint id)
82 for (current = callbacks; current != NULL; current = current->next) {
83 struct name_callback *cb = current->data;
91 static void name_data_call_and_free(struct name_data *data)
95 for (l = data->callbacks; l != NULL; l = l->next) {
96 struct name_callback *cb = l->data;
98 cb->disc_func(data->connection, cb->user_data);
102 g_slist_free(data->callbacks);
107 static void name_data_free(struct name_data *data)
111 for (l = data->callbacks; l != NULL; l = l->next)
114 g_slist_free(data->callbacks);
119 static int name_data_add(DBusConnection *connection, const char *name,
120 GDBusWatchFunction connect,
121 GDBusWatchFunction disconnect,
122 void *user_data, guint id)
125 struct name_data *data = NULL;
126 struct name_callback *cb = NULL;
128 cb = g_new(struct name_callback, 1);
130 cb->conn_func = connect;
131 cb->disc_func = disconnect;
132 cb->user_data = user_data;
135 data = name_data_find(connection, name);
141 data = g_new0(struct name_data, 1);
143 data->connection = connection;
144 data->name = g_strdup(name);
146 name_listeners = g_slist_append(name_listeners, data);
149 data->callbacks = g_slist_append(data->callbacks, cb);
153 static void name_data_remove(DBusConnection *connection,
154 const char *name, guint id)
156 struct name_data *data;
157 struct name_callback *cb = NULL;
159 data = name_data_find(connection, name);
163 cb = name_callback_find(data->callbacks, id);
165 data->callbacks = g_slist_remove(data->callbacks, cb);
172 name_listeners = g_slist_remove(name_listeners, data);
173 name_data_free(data);
175 /* Remove filter if there are no listeners left for the connection */
176 data = name_data_find(connection, NULL);
178 dbus_connection_remove_filter(connection,
183 static gboolean add_match(DBusConnection *connection, const char *name)
186 char match_string[128];
188 snprintf(match_string, sizeof(match_string),
189 "interface=%s,member=NameOwnerChanged,arg0=%s",
190 DBUS_INTERFACE_DBUS, name);
192 dbus_error_init(&err);
194 dbus_bus_add_match(connection, match_string, &err);
196 if (dbus_error_is_set(&err)) {
197 error("Adding match rule \"%s\" failed: %s", match_string,
199 dbus_error_free(&err);
206 static gboolean remove_match(DBusConnection *connection, const char *name)
209 char match_string[128];
211 snprintf(match_string, sizeof(match_string),
212 "interface=%s,member=NameOwnerChanged,arg0=%s",
213 DBUS_INTERFACE_DBUS, name);
215 dbus_error_init(&err);
217 dbus_bus_remove_match(connection, match_string, &err);
219 if (dbus_error_is_set(&err)) {
220 error("Removing owner match rule for %s failed: %s",
222 dbus_error_free(&err);
229 static DBusHandlerResult name_exit_filter(DBusConnection *connection,
230 DBusMessage *message, void *user_data)
233 struct name_data *data;
234 char *name, *old, *new;
237 if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
239 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
241 if (!dbus_message_get_args(message, NULL,
242 DBUS_TYPE_STRING, &name,
243 DBUS_TYPE_STRING, &old,
244 DBUS_TYPE_STRING, &new,
245 DBUS_TYPE_INVALID)) {
246 error("Invalid arguments for NameOwnerChanged signal");
247 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
250 data = name_data_find(connection, name);
252 error("Got NameOwnerChanged signal for %s which has no listeners", name);
253 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
256 for (l = data->callbacks; l != NULL; l = l->next) {
257 struct name_callback *cb = l->data;
260 cb->disc_func(connection, cb->user_data);
263 cb->conn_func(connection, cb->user_data);
265 if (cb->conn_func && cb->disc_func)
270 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
272 name_listeners = g_slist_remove(name_listeners, data);
273 name_data_free(data);
275 /* Remove filter if there no listener left for the connection */
276 data = name_data_find(connection, NULL);
278 dbus_connection_remove_filter(connection, name_exit_filter,
281 remove_match(connection, name);
283 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
286 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
287 GDBusWatchFunction connect,
288 GDBusWatchFunction disconnect,
289 void *user_data, GDBusDestroyFunction destroy)
293 if (!name_data_find(connection, NULL)) {
294 if (!dbus_connection_add_filter(connection,
295 name_exit_filter, NULL, NULL)) {
296 error("dbus_connection_add_filter() failed");
302 first = name_data_add(connection, name, connect, disconnect,
303 user_data, listener_id);
304 /* The filter is already added if this is not the first callback
305 * registration for the name */
310 debug("name_listener_add(%s)", name);
312 if (!add_match(connection, name)) {
313 name_data_remove(connection, name, listener_id);
321 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
322 GDBusWatchFunction func,
323 void *user_data, GDBusDestroyFunction destroy)
325 return g_dbus_add_service_watch(connection, name, NULL, func,
329 guint g_dbus_add_signal_watch(DBusConnection *connection,
330 const char *rule, GDBusSignalFunction function,
331 void *user_data, GDBusDestroyFunction destroy)
336 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
338 struct name_data *data;
339 struct name_callback *cb;
345 for (ldata = name_listeners; ldata; ldata = ldata->next) {
347 for (lcb = data->callbacks; lcb; lcb = lcb->next) {
357 data->callbacks = g_slist_remove(data->callbacks, cb);
360 /* Don't remove the filter if other callbacks exist */
365 if (!remove_match(data->connection, data->name))
369 name_listeners = g_slist_remove(name_listeners, data);
370 name_data_free(data);
372 /* Remove filter if there are no listeners left for the connection */
373 data = name_data_find(connection, NULL);
375 dbus_connection_remove_filter(connection, name_exit_filter,
381 void g_dbus_remove_all_watches(DBusConnection *connection)
383 struct name_data *data;
385 while ((data = name_data_find(connection, NULL))) {
386 name_listeners = g_slist_remove(name_listeners, data);
387 name_data_call_and_free(data);
390 dbus_connection_remove_filter(connection, name_exit_filter, NULL);