static GSList *name_listeners = NULL;
struct name_callback {
- GDBusWatchFunction func;
+ GDBusWatchFunction conn_func;
+ GDBusWatchFunction disc_func;
void *user_data;
guint id;
};
return NULL;
}
-static struct name_callback *name_callback_find(GSList *callbacks,
- GDBusWatchFunction func, void *user_data)
+static struct name_callback *name_callback_find(GSList *callbacks, guint id)
{
GSList *current;
for (current = callbacks; current != NULL; current = current->next) {
struct name_callback *cb = current->data;
- if (cb->func == func && cb->user_data == user_data)
+ if (cb->id == id)
return cb;
}
for (l = data->callbacks; l != NULL; l = l->next) {
struct name_callback *cb = l->data;
- if (cb->func)
- cb->func(data->connection, cb->user_data);
+ if (cb->disc_func)
+ cb->disc_func(data->connection, cb->user_data);
g_free(cb);
}
}
static int name_data_add(DBusConnection *connection, const char *name,
- GDBusWatchFunction func, void *user_data, guint id)
+ GDBusWatchFunction connect,
+ GDBusWatchFunction disconnect,
+ void *user_data, guint id)
{
int first = 1;
struct name_data *data = NULL;
cb = g_new(struct name_callback, 1);
- cb->func = func;
+ cb->conn_func = connect;
+ cb->disc_func = disconnect;
cb->user_data = user_data;
cb->id = id;
}
static void name_data_remove(DBusConnection *connection,
- const char *name, GDBusWatchFunction func, void *user_data)
+ const char *name, guint id)
{
struct name_data *data;
struct name_callback *cb = NULL;
if (!data)
return;
- cb = name_callback_find(data->callbacks, func, user_data);
+ cb = name_callback_find(data->callbacks, id);
if (cb) {
data->callbacks = g_slist_remove(data->callbacks, cb);
g_free(cb);
GSList *l;
struct name_data *data;
char *name, *old, *new;
+ int keep = 0;
if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
"NameOwnerChanged"))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
- /* We are not interested of service creations */
- if (*new != '\0')
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
data = name_data_find(connection, name);
if (!data) {
error("Got NameOwnerChanged signal for %s which has no listeners", name);
for (l = data->callbacks; l != NULL; l = l->next) {
struct name_callback *cb = l->data;
- cb->func(connection, cb->user_data);
+ if (*new == '\0') {
+ if (cb->disc_func)
+ cb->disc_func(connection, cb->user_data);
+ } else {
+ if (cb->conn_func)
+ cb->conn_func(connection, cb->user_data);
+ }
+ if (cb->conn_func && cb->disc_func)
+ keep = 1;
}
+ if (keep)
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
name_listeners = g_slist_remove(name_listeners, data);
name_data_free(data);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-guint g_dbus_add_disconnect_watch(DBusConnection *connection,
- const char *name,
- GDBusWatchFunction func,
+guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
+ GDBusWatchFunction connect,
+ GDBusWatchFunction disconnect,
void *user_data, GDBusDestroyFunction destroy)
{
int first;
}
listener_id++;
- first = name_data_add(connection, name, func, user_data, listener_id);
+ first = name_data_add(connection, name, connect, disconnect,
+ user_data, listener_id);
/* The filter is already added if this is not the first callback
* registration for the name */
if (!first)
debug("name_listener_add(%s)", name);
if (!add_match(connection, name)) {
- name_data_remove(connection, name, func, user_data);
+ name_data_remove(connection, name, listener_id);
return 0;
}
}
return listener_id;
}
+guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
+ GDBusWatchFunction func,
+ void *user_data, GDBusDestroyFunction destroy)
+{
+ return g_dbus_add_service_watch(connection, name, NULL, func,
+ user_data, destroy);
+}
+
guint g_dbus_add_signal_watch(DBusConnection *connection,
const char *rule, GDBusSignalFunction function,
void *user_data, GDBusDestroyFunction destroy)