aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2012-05-27 09:22:28 -0500
committerCharles Kerr <charles.kerr@canonical.com>2012-05-27 09:22:28 -0500
commit23a2672a1a763cdef67ccc5cb9f8fec7daf021be (patch)
tree12e65c5de81ee72c02d58a3183035eb007bd7db5
parent675cc7955e0e8834a3ea4bb46fb347bb744f24a7 (diff)
downloadayatana-indicator-power-23a2672a1a763cdef67ccc5cb9f8fec7daf021be.tar.gz
ayatana-indicator-power-23a2672a1a763cdef67ccc5cb9f8fec7daf021be.tar.bz2
ayatana-indicator-power-23a2672a1a763cdef67ccc5cb9f8fec7daf021be.zip
use signals to decouple i-power and dbus-listener
-rw-r--r--src/dbus-listener.c100
-rw-r--r--src/dbus-listener.h5
-rw-r--r--src/indicator-power.c32
-rw-r--r--src/indicator-power.h3
-rw-r--r--tests/test-indicator.cc60
5 files changed, 66 insertions, 134 deletions
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,
@@ -105,20 +100,6 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self)
}
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)
{
IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(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);
}
@@ -143,47 +122,14 @@ 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; i<device_count; i++)
{
GVariant * v = g_variant_get_child_value (devices_variant, i);
- devices[i] = indicator_power_device_new_from_variant (v);
+ devices = g_slist_prepend (devices, indicator_power_device_new_from_variant (v));
g_variant_unref (v);
}
+ devices = g_slist_reverse (devices);
g_variant_unref (devices_variant);
g_variant_unref (devices_container);
}
- if (priv->ipower != 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; i<device_count; ++i)
- new_devices = g_slist_prepend (new_devices, g_object_ref(devices[i]));
- new_devices = g_slist_reverse (new_devices);
-
- /* clear out the old devices */
+ /* update our devices & primary device */
+ g_slist_foreach (devices, (GFunc)g_object_ref, NULL);
dispose_devices (self);
-
- /* add the new ones */
- priv->devices = 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
diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc
index 5875df6..50ae2c1 100644
--- a/tests/test-indicator.cc
+++ b/tests/test-indicator.cc
@@ -103,17 +103,22 @@ TEST_F(IndicatorTest, GObjectNew)
TEST_F(IndicatorTest, SetDevices)
{
+ GSList * devices;
IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
- IndicatorPowerDevice * devices[] = { ac_device, battery_device };
- indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices));
-
+ devices = NULL;
+ devices = g_slist_append (devices, ac_device);
+ devices = g_slist_append (devices, battery_device);
+ indicator_power_set_devices (power, devices);
+ g_slist_free (devices);
+
g_object_unref (power);
}
TEST_F(IndicatorTest, DischargingStrings)
{
IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
+ GSList * devices = g_slist_append (NULL, battery_device);
// give the indicator a discharging battery with 30 minutes of life left
g_object_set (battery_device,
@@ -121,7 +126,7 @@ TEST_F(IndicatorTest, DischargingStrings)
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*30),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))");
// give the indicator a discharging battery with 1 hour of life left
@@ -130,7 +135,7 @@ TEST_F(IndicatorTest, DischargingStrings)
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))");
// give the indicator a discharging battery with 2 hours of life left
@@ -138,21 +143,21 @@ TEST_F(IndicatorTest, DischargingStrings)
INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))");
// give the indicator a discharging battery with over 12 hours of life left
g_object_set (battery_device,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery");
// give the indicator a discharging battery with 29 seconds left
g_object_set (battery_device,
INDICATOR_POWER_DEVICE_TIME, guint64(29),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))");
// what happens if the time estimate isn't available
@@ -160,7 +165,7 @@ TEST_F(IndicatorTest, DischargingStrings)
INDICATOR_POWER_DEVICE_TIME, guint64(0),
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)");
// what happens if the time estimate AND percentage isn't available
@@ -168,16 +173,18 @@ TEST_F(IndicatorTest, DischargingStrings)
INDICATOR_POWER_DEVICE_TIME, guint64(0),
INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)");
// cleanup
+ g_slist_free (devices);
g_object_unref (power);
}
TEST_F(IndicatorTest, ChargingStrings)
{
IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
+ GSList * devices = g_slist_prepend (NULL, battery_device);
// give the indicator a discharging battery with 1 hour of life left
g_object_set (battery_device,
@@ -185,23 +192,25 @@ TEST_F(IndicatorTest, ChargingStrings)
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))");
// give the indicator a discharging battery with 2 hours of life left
g_object_set (battery_device,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))");
// cleanup
+ g_slist_free (devices);
g_object_unref (power);
}
TEST_F(IndicatorTest, ChargedStrings)
{
IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
+ GSList * devices = g_slist_append (NULL, battery_device);
// give the indicator a discharging battery with 1 hour of life left
g_object_set (battery_device,
@@ -209,10 +218,11 @@ TEST_F(IndicatorTest, ChargedStrings)
INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
- indicator_power_set_devices (power, &battery_device, 1);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)");
// cleanup
+ g_slist_free (devices);
g_object_unref (power);
}
@@ -231,28 +241,14 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft)
". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ",
53, UP_DEVICE_STATE_CHARGING, 0);
- IndicatorPowerDevice * devices[] = { battery_device, bad_battery_device };
- indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices));
+ GSList * devices = NULL;
+ devices = g_slist_append (devices, battery_device);
+ devices = g_slist_append (devices, bad_battery_device);
+ indicator_power_set_devices (power, devices);
ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)");
// cleanup
+ g_slist_free (devices);
g_object_unref (power);
g_object_unref (bad_battery_device);
}
-
-TEST_F(IndicatorTest, DbusListenerGetProperty)
-{
- IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
- GObject * dbus_listener = G_OBJECT(g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE,
- INDICATOR_POWER_DBUS_LISTENER_INDICATOR, power,
- NULL));
- GObject * indicator = NULL;
- g_object_get (dbus_listener,
- INDICATOR_POWER_DBUS_LISTENER_INDICATOR, &indicator,
- NULL);
- ASSERT_EQ(INDICATOR_POWER(indicator), power);
-
- // cleanup
- g_object_unref (dbus_listener);
- g_object_unref (power);
-}