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;
61 static struct name_data *name_data_find(DBusConnection *connection,
66 for (current = name_listeners;
67 current != NULL; current = current->next) {
68 struct name_data *data = current->data;
70 if (connection != data->connection)
73 if (name == NULL || g_str_equal(name, data->name))
80 static struct name_callback *name_callback_find(GSList *callbacks, guint id)
84 for (current = callbacks; current != NULL; current = current->next) {
85 struct name_callback *cb = current->data;
93 static void name_data_call_and_free(struct name_data *data)
97 for (l = data->callbacks; l != NULL; l = l->next) {
98 struct name_callback *cb = l->data;
100 cb->disc_func(data->connection, cb->user_data);
104 g_slist_free(data->callbacks);
109 static void name_data_free(struct name_data *data)
113 for (l = data->callbacks; l != NULL; l = l->next)
116 g_slist_free(data->callbacks);
121 static int name_data_add(DBusConnection *connection, const char *name,
122 GDBusWatchFunction connect,
123 GDBusWatchFunction disconnect,
124 void *user_data, guint id)
127 struct name_data *data = NULL;
128 struct name_callback *cb = NULL;
130 cb = g_new(struct name_callback, 1);
132 cb->conn_func = connect;
133 cb->disc_func = disconnect;
134 cb->user_data = user_data;
137 data = name_data_find(connection, name);
143 data = g_new0(struct name_data, 1);
145 data->connection = connection;
146 data->name = g_strdup(name);
148 name_listeners = g_slist_append(name_listeners, data);
152 data->processed = g_slist_append(data->processed, cb);
154 data->callbacks = g_slist_append(data->callbacks, cb);
159 static void name_data_remove(DBusConnection *connection,
160 const char *name, guint id)
162 struct name_data *data;
163 struct name_callback *cb = NULL;
165 data = name_data_find(connection, name);
169 cb = name_callback_find(data->callbacks, id);
171 data->callbacks = g_slist_remove(data->callbacks, cb);
178 name_listeners = g_slist_remove(name_listeners, data);
179 name_data_free(data);
181 /* Remove filter if there are no listeners left for the connection */
182 data = name_data_find(connection, NULL);
184 dbus_connection_remove_filter(connection,
189 static gboolean add_match(DBusConnection *connection, const char *name)
192 char match_string[128];
194 snprintf(match_string, sizeof(match_string),
195 "interface=%s,member=NameOwnerChanged,arg0=%s",
196 DBUS_INTERFACE_DBUS, name);
198 dbus_error_init(&err);
200 dbus_bus_add_match(connection, match_string, &err);
202 if (dbus_error_is_set(&err)) {
203 error("Adding match rule \"%s\" failed: %s", match_string,
205 dbus_error_free(&err);
212 static gboolean remove_match(DBusConnection *connection, const char *name)
215 char match_string[128];
217 snprintf(match_string, sizeof(match_string),
218 "interface=%s,member=NameOwnerChanged,arg0=%s",
219 DBUS_INTERFACE_DBUS, name);
221 dbus_error_init(&err);
223 dbus_bus_remove_match(connection, match_string, &err);
225 if (dbus_error_is_set(&err)) {
226 error("Removing owner match rule for %s failed: %s",
228 dbus_error_free(&err);
235 static DBusHandlerResult name_exit_filter(DBusConnection *connection,
236 DBusMessage *message, void *user_data)
238 struct name_data *data;
239 struct name_callback *cb;
240 char *name, *old, *new;
242 if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
244 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
246 if (!dbus_message_get_args(message, NULL,
247 DBUS_TYPE_STRING, &name,
248 DBUS_TYPE_STRING, &old,
249 DBUS_TYPE_STRING, &new,
250 DBUS_TYPE_INVALID)) {
251 error("Invalid arguments for NameOwnerChanged signal");
252 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
255 data = name_data_find(connection, name);
257 error("Got NameOwnerChanged signal for %s which has no listeners", name);
258 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263 while (data->callbacks) {
264 cb = data->callbacks->data;
268 cb->disc_func(connection, cb->user_data);
271 cb->conn_func(connection, cb->user_data);
274 /* Check if the watch was removed/freed by the callback
276 if (!g_slist_find(data->callbacks, cb))
279 data->callbacks = g_slist_remove(data->callbacks, cb);
281 if (!cb->conn_func || !cb->disc_func) {
286 data->processed = g_slist_append(data->processed, cb);
289 data->callbacks = data->processed;
290 data->processed = NULL;
294 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
296 name_listeners = g_slist_remove(name_listeners, data);
297 name_data_free(data);
299 /* Remove filter if there no listener left for the connection */
300 data = name_data_find(connection, NULL);
302 dbus_connection_remove_filter(connection, name_exit_filter,
305 remove_match(connection, name);
307 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
310 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
311 GDBusWatchFunction connect,
312 GDBusWatchFunction disconnect,
313 void *user_data, GDBusDestroyFunction destroy)
317 if (!name_data_find(connection, NULL)) {
318 if (!dbus_connection_add_filter(connection,
319 name_exit_filter, NULL, NULL)) {
320 error("dbus_connection_add_filter() failed");
326 first = name_data_add(connection, name, connect, disconnect,
327 user_data, listener_id);
328 /* The filter is already added if this is not the first callback
329 * registration for the name */
334 debug("name_listener_add(%s)", name);
336 if (!add_match(connection, name)) {
337 name_data_remove(connection, name, listener_id);
345 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
346 GDBusWatchFunction func,
347 void *user_data, GDBusDestroyFunction destroy)
349 return g_dbus_add_service_watch(connection, name, NULL, func,
353 guint g_dbus_add_signal_watch(DBusConnection *connection,
354 const char *rule, GDBusSignalFunction function,
355 void *user_data, GDBusDestroyFunction destroy)
360 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
362 struct name_data *data;
363 struct name_callback *cb;
369 for (ldata = name_listeners; ldata; ldata = ldata->next) {
371 for (lcb = data->callbacks; lcb; lcb = lcb->next) {
376 for (lcb = data->processed; lcb; lcb = lcb->next) {
386 data->callbacks = g_slist_remove(data->callbacks, cb);
387 data->processed = g_slist_remove(data->processed, cb);
390 /* Don't remove the filter if other callbacks exist or data is lock
391 * processing callbacks */
392 if (data->callbacks || data->lock)
396 if (!remove_match(data->connection, data->name))
400 name_listeners = g_slist_remove(name_listeners, data);
401 name_data_free(data);
403 /* Remove filter if there are no listeners left for the connection */
404 data = name_data_find(connection, NULL);
406 dbus_connection_remove_filter(connection, name_exit_filter,
412 void g_dbus_remove_all_watches(DBusConnection *connection)
414 struct name_data *data;
416 while ((data = name_data_find(connection, NULL))) {
417 name_listeners = g_slist_remove(name_listeners, data);
418 name_data_call_and_free(data);
421 dbus_connection_remove_filter(connection, name_exit_filter, NULL);