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 func;
50 DBusConnection *connection;
55 static struct name_data *name_data_find(DBusConnection *connection,
60 for (current = name_listeners;
61 current != NULL; current = current->next) {
62 struct name_data *data = current->data;
64 if (name == NULL && data->name == NULL) {
65 if (connection == data->connection)
68 if (strcmp(name, data->name) == 0)
76 static struct name_callback *name_callback_find(GSList *callbacks,
77 GDBusWatchFunction func, void *user_data)
81 for (current = callbacks; current != NULL; current = current->next) {
82 struct name_callback *cb = current->data;
83 if (cb->func == func && cb->user_data == user_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->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 func, void *user_data, guint id)
122 struct name_data *data = NULL;
123 struct name_callback *cb = NULL;
125 cb = g_new(struct name_callback, 1);
128 cb->user_data = user_data;
131 data = name_data_find(connection, name);
137 data = g_new0(struct name_data, 1);
139 data->connection = connection;
140 data->name = g_strdup(name);
142 name_listeners = g_slist_append(name_listeners, data);
145 data->callbacks = g_slist_append(data->callbacks, cb);
149 static void name_data_remove(DBusConnection *connection,
150 const char *name, GDBusWatchFunction func, void *user_data)
152 struct name_data *data;
153 struct name_callback *cb = NULL;
155 data = name_data_find(connection, name);
159 cb = name_callback_find(data->callbacks, func, user_data);
161 data->callbacks = g_slist_remove(data->callbacks, cb);
165 if (!data->callbacks) {
166 name_listeners = g_slist_remove(name_listeners, data);
167 name_data_free(data);
171 static gboolean add_match(DBusConnection *connection, const char *name)
174 char match_string[128];
176 snprintf(match_string, sizeof(match_string),
177 "interface=%s,member=NameOwnerChanged,arg0=%s",
178 DBUS_INTERFACE_DBUS, name);
180 dbus_error_init(&err);
182 dbus_bus_add_match(connection, match_string, &err);
184 if (dbus_error_is_set(&err)) {
185 error("Adding match rule \"%s\" failed: %s", match_string,
187 dbus_error_free(&err);
194 static gboolean remove_match(DBusConnection *connection, const char *name)
197 char match_string[128];
199 snprintf(match_string, sizeof(match_string),
200 "interface=%s,member=NameOwnerChanged,arg0=%s",
201 DBUS_INTERFACE_DBUS, name);
203 dbus_error_init(&err);
205 dbus_bus_remove_match(connection, match_string, &err);
207 if (dbus_error_is_set(&err)) {
208 error("Removing owner match rule for %s failed: %s",
210 dbus_error_free(&err);
217 static DBusHandlerResult name_exit_filter(DBusConnection *connection,
218 DBusMessage *message, void *user_data)
221 struct name_data *data;
222 char *name, *old, *new;
224 if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
226 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
228 if (!dbus_message_get_args(message, NULL,
229 DBUS_TYPE_STRING, &name,
230 DBUS_TYPE_STRING, &old,
231 DBUS_TYPE_STRING, &new,
232 DBUS_TYPE_INVALID)) {
233 error("Invalid arguments for NameOwnerChanged signal");
234 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
237 /* We are not interested of service creations */
239 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;
249 cb->func(connection, cb->user_data);
252 name_listeners = g_slist_remove(name_listeners, data);
253 name_data_free(data);
255 remove_match(connection, name);
257 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
260 guint g_dbus_add_disconnect_watch(DBusConnection *connection,
262 GDBusWatchFunction func,
263 void *user_data, GDBusDestroyFunction destroy)
268 if (!dbus_connection_add_filter(connection,
269 name_exit_filter, NULL, NULL)) {
270 error("dbus_connection_add_filter() failed");
276 first = name_data_add(connection, name, func, user_data, listener_id);
277 /* The filter is already added if this is not the first callback
278 * registration for the name */
283 debug("name_listener_add(%s)", name);
285 if (!add_match(connection, name)) {
286 name_data_remove(connection, name, func, user_data);
294 guint g_dbus_add_signal_watch(DBusConnection *connection,
295 const char *rule, GDBusSignalFunction function,
296 void *user_data, GDBusDestroyFunction destroy)
301 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
303 struct name_data *data;
304 struct name_callback *cb;
310 for (ldata = name_listeners; ldata; ldata = ldata->next) {
312 for (lcb = data->callbacks; lcb; lcb = lcb->next) {
322 data->callbacks = g_slist_remove(data->callbacks, cb);
325 /* Don't remove the filter if other callbacks exist */
330 if (!remove_match(data->connection, data->name))
334 name_listeners = g_slist_remove(name_listeners, data);
335 name_data_free(data);
340 void g_dbus_remove_all_watches(DBusConnection *connection)
342 struct name_data *data;
344 data = name_data_find(connection, NULL);
346 error("name_listener_indicate_disconnect: no listener found");
350 debug("name_listener_indicate_disconnect");
352 name_data_call_and_free(data);