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 +++++++++++--------------------------------------- src/dbus-listener.h | 5 ++- src/indicator-power.c | 32 ++++++---------- src/indicator-power.h | 3 +- 4 files changed, 38 insertions(+), 102 deletions(-) (limited to 'src') 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 diff --git a/src/dbus-listener.h b/src/dbus-listener.h index 816ecc5..2dc0865 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -41,7 +41,8 @@ typedef struct _IndicatorPowerDbusListener IndicatorPowerDbusListener; typedef struct _IndicatorPowerDbusListenerClass IndicatorPowerDbusListenerClass; typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPrivate; -#define INDICATOR_POWER_DBUS_LISTENER_INDICATOR "indicator-power-dbus-listener-indicator" +/* signals */ +#define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "indicator-power-dbus-listener-devices-enumerated" /** * IndicatorPowerDbusListenerClass: @@ -50,6 +51,8 @@ typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPriv struct _IndicatorPowerDbusListenerClass { GObjectClass parent_class; + + void (* devices_enumerated) (IndicatorPowerDbusListener*, GSList * devices); }; /** diff --git a/src/indicator-power.c b/src/indicator-power.c index cf96619..6690f9b 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -116,9 +116,10 @@ indicator_power_init (IndicatorPower *self) priv->accessible_desc = NULL; - priv->dbus_listener = g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, self, - NULL); + priv->dbus_listener = g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL); + g_signal_connect_swapped (priv->dbus_listener, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(indicator_power_set_devices), self); + priv->settings = g_settings_new ("com.canonical.indicator.power"); g_signal_connect_swapped (priv->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); @@ -763,33 +764,22 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) } void -indicator_power_set_devices (IndicatorPower * self, - IndicatorPowerDevice ** devices, - gsize device_count) +indicator_power_set_devices (IndicatorPower * self, GSList * devices) { - gsize i; - GSList * new_devices; IndicatorPowerPrivate * priv; -/* LCOV_EXCL_START */ + /* LCOV_EXCL_START */ g_return_if_fail (IS_INDICATOR_POWER(self)); -/* LCOV_EXCL_STOP */ + /* LCOV_EXCL_STOP */ priv = self->priv; - /* make a reff'ed list of the new devices */ - new_devices = NULL; - for (i=0; idevices = new_devices; + priv->devices = g_slist_copy (devices); priv->device = get_primary_device (priv->devices); - /* and update ourselves based on this new data */ + /* and our menus/visibility from the new device list */ if (priv->device != NULL) put_primary_device (self, priv->device); else diff --git a/src/indicator-power.h b/src/indicator-power.h index bd47535..69ed888 100644 --- a/src/indicator-power.h +++ b/src/indicator-power.h @@ -55,7 +55,6 @@ struct _IndicatorPower GType indicator_power_get_type (void) G_GNUC_CONST; void indicator_power_set_devices (IndicatorPower * power, - IndicatorPowerDevice ** devices, - gsize deviceCount); + GSList * devices); G_END_DECLS -- cgit v1.2.3