From 8a1255676ed6c7091e9c1c2dfda183dacf00ab31 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 6 Jul 2010 10:36:27 -0500 Subject: Switching the watchers data structure over to a hashtable. --- libindicator/indicator-service.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index fc3c7de..74b5a2e 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -51,7 +51,7 @@ struct _IndicatorServicePrivate { gchar * name; DBusGProxy * dbus_proxy; guint timeout; - GList * watchers; + GHashTable * watchers; guint this_service_version; }; @@ -160,6 +160,8 @@ indicator_service_init (IndicatorService *self) priv->watchers = NULL; priv->this_service_version = 0; + priv->watchers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref); + /* Start talkin' dbus */ GError * error = NULL; DBusGConnection * bus = dbus_g_bus_get(DBUS_BUS_STARTER, &error); @@ -230,8 +232,7 @@ indicator_service_finalize (GObject *object) } if (priv->watchers != NULL) { - g_list_foreach(priv->watchers, (GFunc)g_free, NULL); - g_list_free(priv->watchers); + g_hash_table_destroy(priv->watchers); priv->watchers = NULL; } @@ -382,8 +383,12 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati g_return_val_if_fail(INDICATOR_IS_SERVICE(service), FALSE); IndicatorServicePrivate * priv = INDICATOR_SERVICE_GET_PRIVATE(service); - priv->watchers = g_list_append(priv->watchers, - g_strdup(dbus_g_method_get_sender(method))); + const gchar * sender = dbus_g_method_get_sender(method); + if (g_hash_table_lookup(priv->watchers, sender) == NULL) { + DBusGProxy * senderproxy = (gpointer)1; + + g_hash_table_insert(priv->watchers, g_strdup(sender), senderproxy); + } if (priv->timeout != 0) { g_source_remove(priv->timeout); @@ -394,13 +399,6 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati return TRUE; } -/* Mung g_strcmp0 into GCompareFunc */ -static gint -find_watcher (gconstpointer a, gconstpointer b) -{ - return g_strcmp0((const gchar *)a, (const gchar *)b); -} - /* A function connecting into the dbus interface for the "UnWatch" function. It is also an async function to get the sender. It then looks the sender up and removes them @@ -413,19 +411,17 @@ _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvoc IndicatorServicePrivate * priv = INDICATOR_SERVICE_GET_PRIVATE(service); /* Remove us from the watcher list here */ - GList * watcher_item = g_list_find_custom(priv->watchers, dbus_g_method_get_sender(method), find_watcher); + gpointer watcher_item = g_hash_table_lookup(priv->watchers, dbus_g_method_get_sender(method)); if (watcher_item != NULL) { /* Free the watcher */ - gchar * name = watcher_item->data; - priv->watchers = g_list_remove(priv->watchers, name); - g_free(name); + g_hash_table_remove(priv->watchers, dbus_g_method_get_sender(method)); } else { /* Odd that we couldn't find the person, but, eh */ g_warning("Unable to find watcher who is unwatching: %s", dbus_g_method_get_sender(method)); } /* If we're out of watchers set the timeout for shutdown */ - if (priv->watchers == NULL) { + if (g_hash_table_size(priv->watchers) == 0) { if (priv->timeout != 0) { /* This should never really happen, but let's ensure that bad things don't happen if it does. */ -- cgit v1.2.3 From 632c3f997f091be8daeedcd0eaf7f1043dca5530 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 6 Jul 2010 10:39:12 -0500 Subject: Making ths bus tracked in the private structure --- libindicator/indicator-service.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index 74b5a2e..d3e873b 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -50,6 +50,7 @@ typedef struct _IndicatorServicePrivate IndicatorServicePrivate; struct _IndicatorServicePrivate { gchar * name; DBusGProxy * dbus_proxy; + DBusGConnection * bus; guint timeout; GHashTable * watchers; guint this_service_version; @@ -158,13 +159,14 @@ indicator_service_init (IndicatorService *self) priv->dbus_proxy = NULL; priv->timeout = 0; priv->watchers = NULL; + priv->bus = NULL; priv->this_service_version = 0; priv->watchers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref); /* Start talkin' dbus */ GError * error = NULL; - DBusGConnection * bus = dbus_g_bus_get(DBUS_BUS_STARTER, &error); + priv->bus = dbus_g_bus_get(DBUS_BUS_STARTER, &error); if (error != NULL) { g_error("Unable to get starter bus: %s", error->message); g_error_free(error); @@ -173,7 +175,7 @@ indicator_service_init (IndicatorService *self) /* I think this should automatically, but I can't find confirmation of that, so we're putting the extra little code in here. */ error = NULL; - bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); + priv->bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if (error != NULL) { g_error("Unable to get session bus: %s", error->message); g_error_free(error); @@ -181,7 +183,7 @@ indicator_service_init (IndicatorService *self) } } - priv->dbus_proxy = dbus_g_proxy_new_for_name_owner(bus, + priv->dbus_proxy = dbus_g_proxy_new_for_name_owner(priv->bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, @@ -192,7 +194,7 @@ indicator_service_init (IndicatorService *self) return; } - dbus_g_connection_register_g_object(bus, + dbus_g_connection_register_g_object(priv->bus, INDICATOR_SERVICE_OBJECT, G_OBJECT(self)); -- cgit v1.2.3 From d23ca44c07ddf691ae41b4a6c5c10b794f1de765 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 6 Jul 2010 10:47:58 -0500 Subject: Actually creating a proxy to see if we can detect the watcher going away. --- libindicator/indicator-service.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index d3e873b..01fabce 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -387,9 +387,19 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati const gchar * sender = dbus_g_method_get_sender(method); if (g_hash_table_lookup(priv->watchers, sender) == NULL) { - DBusGProxy * senderproxy = (gpointer)1; - - g_hash_table_insert(priv->watchers, g_strdup(sender), senderproxy); + GError * error = NULL; + DBusGProxy * senderproxy = dbus_g_proxy_new_for_name_owner(priv->bus, + sender, + "/", + DBUS_INTERFACE_INTROSPECTABLE, + &error); + + if (error == NULL) { + g_hash_table_insert(priv->watchers, g_strdup(sender), senderproxy); + } else { + g_warning("Unable to create proxy for watcher '%s': %s", sender, error->message); + g_error_free(error); + } } if (priv->timeout != 0) { -- cgit v1.2.3 From dd7106ee06b70f95879875acccab132a3ef2a8cb Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 6 Jul 2010 11:04:41 -0500 Subject: Creating a callback for the destory signal, but then moving the unwatch into a core function so that we can call it. --- libindicator/indicator-service.c | 42 ++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index 01fabce..15335f3 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -29,6 +29,7 @@ License along with this library. If not, see #include "indicator-service.h" +static void unwatch_core (IndicatorService * service, const gchar * name); /* DBus Prototypes */ static gboolean _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocation * method); static gboolean _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvocation * method); @@ -375,6 +376,19 @@ try_and_get_name (IndicatorService * service) return; } +/* If the proxy gets destroyed that's the same as getting an + unwatch signal. Make it so. */ +static void +proxy_destroyed (GObject * proxy, gpointer user_data) +{ + g_return_if_fail(INDICATOR_IS_SERVICE(user_data)); + + const gchar * name = dbus_g_proxy_get_bus_name(DBUS_G_PROXY(proxy)); + unwatch_core(INDICATOR_SERVICE(user_data), name); + + return; +} + /* Here is the function that gets called by the dbus interface "Watch" function. It is an async function so that we can get the sender and store that information. We @@ -394,6 +408,8 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati DBUS_INTERFACE_INTROSPECTABLE, &error); + g_signal_connect(G_OBJECT(senderproxy), "destroy", G_CALLBACK(proxy_destroyed), service); + if (error == NULL) { g_hash_table_insert(priv->watchers, g_strdup(sender), senderproxy); } else { @@ -413,23 +429,34 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati /* A function connecting into the dbus interface for the "UnWatch" function. It is also an async function to get - the sender. It then looks the sender up and removes them - from the list of watchers. If there are none left, it then - starts the timer for the shutdown signal. */ + the sender and passes everything to unwatch_core to remove it. */ static gboolean _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvocation * method) { g_return_val_if_fail(INDICATOR_IS_SERVICE(service), FALSE); + + unwatch_core(service, dbus_g_method_get_sender(method)); + + dbus_g_method_return(method); + return TRUE; +} + +/* Performs the core of loosing a watcher; it removes them + from the list of watchers. If there are none left, it then + starts the timer for the shutdown signal. */ +static void +unwatch_core (IndicatorService * service, const gchar * name) +{ IndicatorServicePrivate * priv = INDICATOR_SERVICE_GET_PRIVATE(service); /* Remove us from the watcher list here */ - gpointer watcher_item = g_hash_table_lookup(priv->watchers, dbus_g_method_get_sender(method)); + gpointer watcher_item = g_hash_table_lookup(priv->watchers, name); if (watcher_item != NULL) { /* Free the watcher */ - g_hash_table_remove(priv->watchers, dbus_g_method_get_sender(method)); + g_hash_table_remove(priv->watchers, name); } else { /* Odd that we couldn't find the person, but, eh */ - g_warning("Unable to find watcher who is unwatching: %s", dbus_g_method_get_sender(method)); + g_warning("Unable to find watcher who is unwatching: %s", name); } /* If we're out of watchers set the timeout for shutdown */ @@ -445,8 +472,7 @@ _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvoc priv->timeout = g_timeout_add(500, timeout_no_watchers, service); } - dbus_g_method_return(method); - return TRUE; + return; } /* API */ -- cgit v1.2.3