From 9ee74a367606a4dd6779b0448bef237dd9e2797e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:19:38 -0500 Subject: separate the dbus org.gnome.SettingsDaemon.Power logic into a separate class --- src/dbus-listener.c | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 src/dbus-listener.c (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c new file mode 100644 index 0000000..fe8af09 --- /dev/null +++ b/src/dbus-listener.c @@ -0,0 +1,276 @@ +/* + +Listens on DBus for Power changes and passes them to an IndicatorPower + +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +version 3.0 as published by the Free Software Foundation. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License version 3.0 for more details. + +You should have received a copy of the GNU General Public +License along with this library. If not, see +. +*/ + +#include "dbus-listener.h" +#include "indicator-power.h" + +#define DBUS_SERVICE "org.gnome.SettingsDaemon" +#define DBUS_PATH "/org/gnome/SettingsDaemon" +#define POWER_DBUS_PATH DBUS_PATH "/Power" +#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power" + +struct _IndicatorPowerDbusListenerPrivate +{ + IndicatorPower * ipower; + + GCancellable * proxy_cancel; + GDBusProxy * proxy; + guint watcher_id; +}; + +#define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) + +/* Properties */ +/* Enum for the properties so that they can be quickly found and looked up. */ +enum { + PROP_0, + PROP_INDICATOR +}; + +/* GObject stuff */ +static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass); +static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self); +static void indicator_power_dbus_listener_dispose (GObject *object); +static void indicator_power_dbus_listener_finalize (GObject *object); +static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); +static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); + +static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); + +G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE_OBJECT); + +static void +indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) +{ + GParamSpec * pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + + object_class->dispose = indicator_power_dbus_listener_dispose; + object_class->finalize = indicator_power_dbus_listener_finalize; + object_class->set_property = set_property; + object_class->get_property = get_property; + + pspec = g_param_spec_object (INDICATOR_POWER_DBUS_LISTENER_INDICATOR, + "indicator", + "The IndicatorPower to notify when power changes are received", + INDICATOR_POWER_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_INDICATOR, pspec); +} + +/* Initialize an instance */ +static void +indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) +{ + IndicatorPowerDbusListenerPrivate * priv; + + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); + priv->ipower = NULL; + + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + DBUS_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); + + self->priv = priv; +} + +static void +indicator_power_dbus_listener_dispose (GObject *object) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + g_clear_object (&priv->proxy); + g_clear_object (&priv->proxy_cancel); + g_clear_object (&priv->ipower); + + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); +} + +static void +indicator_power_dbus_listener_finalize (GObject *object) +{ + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); +} + +/*** +**** +***/ + +static void +get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_INDICATOR: + g_value_set_object (value, priv->ipower); + break; + } +} + +static void +set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_INDICATOR: + priv->ipower = g_value_dup_object (value); + break; + } +} + +/*** +**** +***/ + +static void +get_devices_cb (GObject * source_object, + GAsyncResult * res, + gpointer user_data) +{ + GError *error; + int device_count = 0; + GVariant * devices_container; + IndicatorPowerDevice ** devices = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + /* build an array of IndicatorPowerDevices from the DBus response */ + error = NULL; + devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); + if (devices_container == NULL) + { + g_message ("Couldn't get devices: %s\n", error->message); + g_error_free (error); + } + else + { + gsize i; + GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); + device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; + devices = g_new0 (IndicatorPowerDevice*, device_count); + + for (i=0; iipower != NULL) + { + indicator_power_set_devices (priv->ipower, devices, device_count); + } + + g_free (devices); +} + +static void +request_device_list (IndicatorPowerDbusListener * self) +{ + g_dbus_proxy_call (self->priv->proxy, + "GetDevices", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + self->priv->proxy_cancel, + get_devices_cb, + self); +} + +static void +receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, + GVariant *changed_properties G_GNUC_UNUSED, + GStrv invalidated_properties G_GNUC_UNUSED, + gpointer user_data) +{ + request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); +} + + +static void +service_proxy_cb (GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + GError * error = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + g_clear_object (&priv->proxy_cancel); + + if (error != NULL) { + g_error ("Error creating proxy: %s", error->message); + g_error_free (error); + return; + } + + /* we want to change the primary device changes */ + g_signal_connect (priv->proxy, + "g-properties-changed", + G_CALLBACK (receive_properties_changed), + user_data); + + /* get the initial state */ + request_device_list (self); +} + +static void +gsd_appeared_callback (GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy_cancel = g_cancellable_new (); + + g_dbus_proxy_new (connection, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + NULL, + name, + POWER_DBUS_PATH, + POWER_DBUS_INTERFACE, + priv->proxy_cancel, + service_proxy_cb, + self); +} -- cgit v1.2.3 From b3a1b201431b649b5fb37d1c10a2609e92d06239 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:56:10 -0500 Subject: avoid a cyclical refcount dependency between IndicatorPower and its DBusListener --- src/dbus-listener.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index fe8af09..9f62f25 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -101,6 +101,20 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) self->priv = priv; } +static void +set_indicator (IndicatorPowerDbusListener * self, GObject * ipower) +{ + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + if (priv->ipower != NULL) + g_object_remove_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); + + priv->ipower = INDICATOR_POWER(ipower); + + if (priv->ipower != NULL) + g_object_add_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); +}; + static void indicator_power_dbus_listener_dispose (GObject *object) { @@ -109,7 +123,8 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->proxy_cancel); - g_clear_object (&priv->ipower); + + set_indicator (self, NULL); G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } @@ -142,12 +157,11 @@ static void set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) { IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - IndicatorPowerDbusListenerPrivate * priv = self->priv; switch (prop_id) { case PROP_INDICATOR: - priv->ipower = g_value_dup_object (value); + set_indicator (self, g_value_get_object(value)); break; } } -- cgit v1.2.3 From 5937352499a753898e36b53512f53ac1781034bd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:11:03 -0500 Subject: reuse the same cancellable across multiple non-concurrent dbus calls --- src/dbus-listener.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 9f62f25..5539098 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -33,7 +33,7 @@ struct _IndicatorPowerDbusListenerPrivate { IndicatorPower * ipower; - GCancellable * proxy_cancel; + GCancellable * cancellable; GDBusProxy * proxy; guint watcher_id; }; @@ -85,18 +85,21 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) { IndicatorPowerDbusListenerPrivate * priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_DBUS_LISTENER_TYPE, - IndicatorPowerDbusListenerPrivate); - priv->ipower = NULL; + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); + priv->ipower = NULL; + + priv->cancellable = g_cancellable_new (); + + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + DBUS_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); self->priv = priv; } @@ -122,7 +125,7 @@ indicator_power_dbus_listener_dispose (GObject *object) IndicatorPowerDbusListenerPrivate * priv = self->priv; g_clear_object (&priv->proxy); - g_clear_object (&priv->proxy_cancel); + g_clear_object (&priv->cancellable); set_indicator (self, NULL); @@ -224,7 +227,7 @@ request_device_list (IndicatorPowerDbusListener * self) NULL, G_DBUS_CALL_FLAGS_NONE, -1, - self->priv->proxy_cancel, + self->priv->cancellable, get_devices_cb, self); } @@ -249,7 +252,6 @@ service_proxy_cb (GObject *object, IndicatorPowerDbusListenerPrivate * priv = self->priv; priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - g_clear_object (&priv->proxy_cancel); if (error != NULL) { g_error ("Error creating proxy: %s", error->message); @@ -276,15 +278,13 @@ gsd_appeared_callback (GDBusConnection *connection, IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); IndicatorPowerDbusListenerPrivate * priv = self->priv; - priv->proxy_cancel = g_cancellable_new (); - g_dbus_proxy_new (connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, name, POWER_DBUS_PATH, POWER_DBUS_INTERFACE, - priv->proxy_cancel, + priv->cancellable, service_proxy_cb, self); } -- cgit v1.2.3 From 23a2672a1a763cdef67ccc5cb9f8fec7daf021be Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 27 May 2012 09:22:28 -0500 Subject: use signals to decouple i-power and dbus-listener --- src/dbus-listener.c | 100 ++++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 78 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 5539098..6a85fd4 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -22,7 +22,7 @@ License along with this library. If not, see */ #include "dbus-listener.h" -#include "indicator-power.h" +#include "device.h" #define DBUS_SERVICE "org.gnome.SettingsDaemon" #define DBUS_PATH "/org/gnome/SettingsDaemon" @@ -31,8 +31,6 @@ License along with this library. If not, see struct _IndicatorPowerDbusListenerPrivate { - IndicatorPower * ipower; - GCancellable * cancellable; GDBusProxy * proxy; guint watcher_id; @@ -40,20 +38,19 @@ struct _IndicatorPowerDbusListenerPrivate #define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) -/* Properties */ -/* Enum for the properties so that they can be quickly found and looked up. */ +/* Signals */ enum { - PROP_0, - PROP_INDICATOR + SIGNAL_DEVICES, + SIGNAL_LAST }; +static guint signals[SIGNAL_LAST] = { 0 }; + /* GObject stuff */ static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass); static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self); static void indicator_power_dbus_listener_dispose (GObject *object); static void indicator_power_dbus_listener_finalize (GObject *object); -static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); -static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); @@ -62,21 +59,21 @@ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) { - GParamSpec * pspec; GObjectClass *object_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + /* methods */ object_class->dispose = indicator_power_dbus_listener_dispose; object_class->finalize = indicator_power_dbus_listener_finalize; - object_class->set_property = set_property; - object_class->get_property = get_property; - - pspec = g_param_spec_object (INDICATOR_POWER_DBUS_LISTENER_INDICATOR, - "indicator", - "The IndicatorPower to notify when power changes are received", - INDICATOR_POWER_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_INDICATOR, pspec); + + /* signals */ + signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_TYPE_FROM_CLASS(klass), 0, + G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); } /* Initialize an instance */ @@ -89,8 +86,6 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) INDICATOR_POWER_DBUS_LISTENER_TYPE, IndicatorPowerDbusListenerPrivate); - priv->ipower = NULL; - priv->cancellable = g_cancellable_new (); priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, @@ -104,20 +99,6 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) self->priv = priv; } -static void -set_indicator (IndicatorPowerDbusListener * self, GObject * ipower) -{ - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - if (priv->ipower != NULL) - g_object_remove_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); - - priv->ipower = INDICATOR_POWER(ipower); - - if (priv->ipower != NULL) - g_object_add_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); -}; - static void indicator_power_dbus_listener_dispose (GObject *object) { @@ -127,8 +108,6 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->cancellable); - set_indicator (self, NULL); - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } @@ -142,48 +121,15 @@ indicator_power_dbus_listener_finalize (GObject *object) **** ***/ -static void -get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) -{ - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - switch (prop_id) - { - case PROP_INDICATOR: - g_value_set_object (value, priv->ipower); - break; - } -} - -static void -set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) -{ - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - - switch (prop_id) - { - case PROP_INDICATOR: - set_indicator (self, g_value_get_object(value)); - break; - } -} - -/*** -**** -***/ - static void get_devices_cb (GObject * source_object, GAsyncResult * res, gpointer user_data) { GError *error; - int device_count = 0; + GSList * devices = NULL; GVariant * devices_container; - IndicatorPowerDevice ** devices = NULL; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -197,26 +143,24 @@ get_devices_cb (GObject * source_object, { gsize i; GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); - device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - devices = g_new0 (IndicatorPowerDevice*, device_count); + const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; for (i=0; iipower != NULL) - { - indicator_power_set_devices (priv->ipower, devices, device_count); - } + g_signal_emit (self, signals[SIGNAL_DEVICES], (GQuark)0, devices); - g_free (devices); + g_slist_free_full (devices, g_object_unref); + } static void -- cgit v1.2.3 From b9a6194c12bf39fb6317edc1126ea32e890da583 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 May 2012 11:33:07 -0500 Subject: add a mock GSD.Power to handle the GetPower requests --- src/dbus-listener.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 6a85fd4..a7a29ee 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -24,11 +24,6 @@ License along with this library. If not, see #include "dbus-listener.h" #include "device.h" -#define DBUS_SERVICE "org.gnome.SettingsDaemon" -#define DBUS_PATH "/org/gnome/SettingsDaemon" -#define POWER_DBUS_PATH DBUS_PATH "/Power" -#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power" - struct _IndicatorPowerDbusListenerPrivate { GCancellable * cancellable; @@ -89,7 +84,7 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) priv->cancellable = g_cancellable_new (); priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, + GSD_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, gsd_appeared_callback, NULL, @@ -226,8 +221,8 @@ gsd_appeared_callback (GDBusConnection *connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, name, - POWER_DBUS_PATH, - POWER_DBUS_INTERFACE, + GSD_POWER_DBUS_PATH, + GSD_POWER_DBUS_INTERFACE, priv->cancellable, service_proxy_cb, self); -- cgit v1.2.3 From 7f08c9a317a964aaf859df9489a7361fb60ecd42 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 13:28:47 -0500 Subject: exclude G_DEFINE_TYPE from coverage testing in dbus-listener.c --- src/dbus-listener.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index a7a29ee..feaf78e 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -49,7 +49,9 @@ static void indicator_power_dbus_listener_finalize (GObject *object); static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); +/* LCOV_EXCL_START */ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE_OBJECT); +/* LCOV_EXCL_STOP */ static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) -- cgit v1.2.3 From ab87d8f8eaf75dda49dbd6e9ef56bf94dcf98e28 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 17:28:15 -0500 Subject: add a test to make sure that the listener responds to a PropertiesChanged signal --- src/dbus-listener.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index feaf78e..bc31ee3 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -127,6 +127,7 @@ get_devices_cb (GObject * source_object, GSList * devices = NULL; GVariant * devices_container; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); +g_message ("%s", G_STRLOC); /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -163,6 +164,7 @@ get_devices_cb (GObject * source_object, static void request_device_list (IndicatorPowerDbusListener * self) { +g_message ("calling GetDevices... self %p priv %p proxy %p", self, self->priv, self->priv->proxy); g_dbus_proxy_call (self->priv->proxy, "GetDevices", NULL, @@ -179,6 +181,8 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { +g_message ("properties changed: %p %p %p %p", proxy, changed_properties, invalidated_properties, user_data); +g_message ("hey someone told me the properties changed"); request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -200,6 +204,7 @@ service_proxy_cb (GObject *object, return; } +g_message ("registering for g-properties-changed callback"); /* we want to change the primary device changes */ g_signal_connect (priv->proxy, "g-properties-changed", -- cgit v1.2.3 From 89c515defeb4403ec9452221f130be1007e0c7df Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 12:59:24 -0500 Subject: remove some g_message() calls that shouldn't've been committed. --- src/dbus-listener.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index bc31ee3..feaf78e 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -127,7 +127,6 @@ get_devices_cb (GObject * source_object, GSList * devices = NULL; GVariant * devices_container; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); -g_message ("%s", G_STRLOC); /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -164,7 +163,6 @@ g_message ("%s", G_STRLOC); static void request_device_list (IndicatorPowerDbusListener * self) { -g_message ("calling GetDevices... self %p priv %p proxy %p", self, self->priv, self->priv->proxy); g_dbus_proxy_call (self->priv->proxy, "GetDevices", NULL, @@ -181,8 +179,6 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { -g_message ("properties changed: %p %p %p %p", proxy, changed_properties, invalidated_properties, user_data); -g_message ("hey someone told me the properties changed"); request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -204,7 +200,6 @@ service_proxy_cb (GObject *object, return; } -g_message ("registering for g-properties-changed callback"); /* we want to change the primary device changes */ g_signal_connect (priv->proxy, "g-properties-changed", -- cgit v1.2.3 From 99a719aac32c4347c9b250372d887ca94aa56330 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 14:24:17 -0500 Subject: copyediting: fix tab damage in dbus-listener.[ch] --- src/dbus-listener.c | 222 ++++++++++++++++++++++++++-------------------------- 1 file changed, 110 insertions(+), 112 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index feaf78e..7f399f4 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -26,17 +26,17 @@ License along with this library. If not, see struct _IndicatorPowerDbusListenerPrivate { - GCancellable * cancellable; - GDBusProxy * proxy; - guint watcher_id; + GCancellable * cancellable; + GDBusProxy * proxy; + guint watcher_id; }; #define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) /* Signals */ enum { - SIGNAL_DEVICES, - SIGNAL_LAST + SIGNAL_DEVICES, + SIGNAL_LAST }; static guint signals[SIGNAL_LAST] = { 0 }; @@ -56,62 +56,62 @@ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); - g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); - /* methods */ - object_class->dispose = indicator_power_dbus_listener_dispose; - object_class->finalize = indicator_power_dbus_listener_finalize; + /* methods */ + object_class->dispose = indicator_power_dbus_listener_dispose; + object_class->finalize = indicator_power_dbus_listener_finalize; - /* signals */ - signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, - G_TYPE_FROM_CLASS(klass), 0, - G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), - NULL, NULL, - g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); + /* signals */ + signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_TYPE_FROM_CLASS(klass), 0, + G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); } /* Initialize an instance */ static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) { - IndicatorPowerDbusListenerPrivate * priv; + IndicatorPowerDbusListenerPrivate * priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_DBUS_LISTENER_TYPE, - IndicatorPowerDbusListenerPrivate); + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); - priv->cancellable = g_cancellable_new (); + priv->cancellable = g_cancellable_new (); - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - GSD_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + GSD_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); - self->priv = priv; + self->priv = priv; } static void indicator_power_dbus_listener_dispose (GObject *object) { - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); - IndicatorPowerDbusListenerPrivate * priv = self->priv; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); + IndicatorPowerDbusListenerPrivate * priv = self->priv; - g_clear_object (&priv->proxy); - g_clear_object (&priv->cancellable); + g_clear_object (&priv->proxy); + g_clear_object (&priv->cancellable); - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } static void indicator_power_dbus_listener_finalize (GObject *object) { - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); } /*** @@ -123,54 +123,53 @@ get_devices_cb (GObject * source_object, GAsyncResult * res, gpointer user_data) { - GError *error; - GSList * devices = NULL; - GVariant * devices_container; - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); - - /* build an array of IndicatorPowerDevices from the DBus response */ - error = NULL; - devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); - if (devices_container == NULL) - { - g_message ("Couldn't get devices: %s\n", error->message); - g_error_free (error); - } - else - { - gsize i; - GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); - const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - - for (i=0; imessage); + g_error_free (error); + } + else + { + gsize i; + GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); + const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; + + for (i=0; ipriv->proxy, - "GetDevices", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - self->priv->cancellable, - get_devices_cb, - self); + g_dbus_proxy_call (self->priv->proxy, + "GetDevices", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + self->priv->cancellable, + get_devices_cb, + self); } static void @@ -179,7 +178,7 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { - request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); + request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -188,26 +187,25 @@ service_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data) { - GError * error = NULL; - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - - if (error != NULL) { - g_error ("Error creating proxy: %s", error->message); - g_error_free (error); - return; - } - - /* we want to change the primary device changes */ - g_signal_connect (priv->proxy, - "g-properties-changed", - G_CALLBACK (receive_properties_changed), - user_data); - - /* get the initial state */ - request_device_list (self); + GError * error = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + + if (error != NULL) + { + g_error ("Error creating proxy: %s", error->message); + g_error_free (error); + return; + } + + /* we want to change the primary device changes */ + g_signal_connect (priv->proxy, "g-properties-changed", + G_CALLBACK (receive_properties_changed), user_data); + + /* get the initial state */ + request_device_list (self); } static void @@ -216,16 +214,16 @@ gsd_appeared_callback (GDBusConnection *connection, const gchar *name_owner, gpointer user_data) { - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - g_dbus_proxy_new (connection, - G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, - NULL, - name, - GSD_POWER_DBUS_PATH, - GSD_POWER_DBUS_INTERFACE, - priv->cancellable, - service_proxy_cb, - self); + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + g_dbus_proxy_new (connection, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + NULL, + name, + GSD_POWER_DBUS_PATH, + GSD_POWER_DBUS_INTERFACE, + priv->cancellable, + service_proxy_cb, + self); } -- cgit v1.2.3 From f2310de23a3012fd579a659a99b09b030e9df108 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:39:51 -0500 Subject: add a GTK-Doc signal comment block for indictor-power-dbus-listener's enumerated signal --- src/dbus-listener.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 7f399f4..9143a25 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -64,13 +64,20 @@ indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass object_class->dispose = indicator_power_dbus_listener_dispose; object_class->finalize = indicator_power_dbus_listener_finalize; - /* signals */ + /** + * IndicatorPowerDbusListener::indicator-power-dbus-listener-devices-enumerated: + * + * @listener: the IndicatorPowerDbusListener + * @devices: a GSList of #IndicatorPowerDevice objects. (transfer none) + * + * This is emitted each time a new set of devices is enumerated over the bus. + */ signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, G_TYPE_FROM_CLASS(klass), 0, G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), NULL, NULL, g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); + G_TYPE_NONE, 1, G_TYPE_POINTER); } /* Initialize an instance */ -- cgit v1.2.3 From 79d37d6827bdfd9b1a94ce6918157ce475bc012a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:42:23 -0500 Subject: add g_bus_unwatch_name() to watcher's dispose() method --- src/dbus-listener.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 9143a25..7e3d7b5 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -112,6 +112,12 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->cancellable); + if (priv->watcher_id) + { + g_bus_unwatch_name (priv->watcher_id); + priv->watcher_id = 0; + } + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } -- cgit v1.2.3 From 5cac4ebb939cde67c78cf8aa59d27beafd187ac8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:43:11 -0500 Subject: remove trailing whitespace --- src/dbus-listener.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 7e3d7b5..4923fe1 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -154,7 +154,7 @@ get_devices_cb (GObject * source_object, gsize i; GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - + for (i=0; i Date: Wed, 6 Jun 2012 14:47:08 -0500 Subject: if self->cancellable is non-NULL in dispose(), pass it to g_cancellable_cancel() before clearing the listener's reference --- src/dbus-listener.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 4923fe1..34df925 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -110,7 +110,12 @@ indicator_power_dbus_listener_dispose (GObject *object) IndicatorPowerDbusListenerPrivate * priv = self->priv; g_clear_object (&priv->proxy); - g_clear_object (&priv->cancellable); + + if (priv->cancellable != NULL) + { + g_cancellable_cancel (priv->cancellable); + g_clear_object (&priv->cancellable); + } if (priv->watcher_id) { -- cgit v1.2.3 From 65f33de3763fc9540f994abf29ca05f7714f81ee Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:50:39 -0500 Subject: simplify the devices-enumerated signal's name --- src/dbus-listener.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/dbus-listener.c') diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 34df925..b1f64a7 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -65,7 +65,7 @@ indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass object_class->finalize = indicator_power_dbus_listener_finalize; /** - * IndicatorPowerDbusListener::indicator-power-dbus-listener-devices-enumerated: + * IndicatorPowerDbusListener::devices-enumerated: * * @listener: the IndicatorPowerDbusListener * @devices: a GSList of #IndicatorPowerDevice objects. (transfer none) -- cgit v1.2.3