From 622c18549745d3824713fd9d7a4a0d89b31257a5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 09:49:30 -0500 Subject: add private container struct IndicatorPowerDevice --- src/device.c | 330 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 src/device.c (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c new file mode 100644 index 0000000..d52ce8a --- /dev/null +++ b/src/device.c @@ -0,0 +1,330 @@ +/* + +A simple Device structure used internally by indicator-power + +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 "device.h" + +struct _IndicatorPowerDevicePrivate +{ + UpDeviceKind kind; + UpDeviceState state; + gchar * object_path; + gchar * icon; + gdouble percentage; + time_t time; +}; + +#define INDICATOR_POWER_DEVICE_GET_PRIVATE(o) (INDICATOR_POWER_DEVICE(o)->priv) + +/* Properties */ +/* Enum for the properties so that they can be quickly found and looked up. */ +enum { + PROP_0, + PROP_KIND, + PROP_STATE, + PROP_OBJECT_PATH, + PROP_ICON, + PROP_PERCENTAGE, + PROP_TIME +}; + +/* GObject stuff */ +static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass); +static void indicator_power_device_init (IndicatorPowerDevice *self); +static void indicator_power_device_dispose (GObject *object); +static void indicator_power_device_finalize (GObject *object); +static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); +static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); + +G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); + +static void +indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) +{ + GParamSpec * pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorPowerDevicePrivate)); + + object_class->dispose = indicator_power_device_dispose; + object_class->finalize = indicator_power_device_finalize; + object_class->set_property = set_property; + object_class->get_property = get_property; + + pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, "kind", "The device's UpDeviceKind", + UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, UP_DEVICE_KIND_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_KIND, pspec); + + pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState", + UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, UP_DEVICE_STATE_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_STATE, pspec); + + pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, "object path", "The device's DBus object path", NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec); + + pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_ICON, "icon", "The device's icon", NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_ICON, pspec); + + pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged", + 0.0, 100.0, 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec); + + pspec = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, "time", "time left", + 0, G_MAXUINT64, 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_TIME, pspec); +} + +/* Initialize an instance */ +static void +indicator_power_device_init (IndicatorPowerDevice *self) +{ + IndicatorPowerDevicePrivate * priv; + + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DEVICE_TYPE, + IndicatorPowerDevicePrivate); + priv->kind = UP_DEVICE_KIND_UNKNOWN; + priv->state = UP_DEVICE_STATE_UNKNOWN; + priv->object_path = NULL; + priv->icon = NULL; + priv->percentage = 0.0; + priv->time = 0; + + self->priv = priv; +} + +static void +indicator_power_device_dispose (GObject *object) +{ + G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object); +} + +static void +indicator_power_device_finalize (GObject *object) +{ + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object); + IndicatorPowerDevicePrivate * priv = self->priv; + + g_clear_pointer (&priv->object_path, g_free); + g_clear_pointer (&priv->icon, g_free); +} + +/*** +**** +***/ + +static void +get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); + IndicatorPowerDevicePrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_KIND: + g_return_if_fail (G_VALUE_HOLDS_INT(value)); + g_value_set_int (value, priv->kind); + break; + + case PROP_STATE: + g_return_if_fail (G_VALUE_HOLDS_INT(value)); + g_value_set_int (value, priv->state); + break; + + case PROP_OBJECT_PATH: + g_return_if_fail (G_VALUE_HOLDS_STRING(value)); + g_value_set_string (value, priv->object_path); + break; + + case PROP_ICON: + g_return_if_fail (G_VALUE_HOLDS_STRING(value)); + g_value_set_string (value, priv->icon); + break; + + case PROP_PERCENTAGE: + g_return_if_fail (G_VALUE_HOLDS_DOUBLE(value)); + g_value_set_double (value, priv->percentage); + break; + + case PROP_TIME: + g_return_if_fail (G_VALUE_HOLDS_UINT64(value)); + g_value_set_uint64 (value, priv->time); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (o, prop_id, pspec); + break; + } +} + +static void +set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); + IndicatorPowerDevicePrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_KIND: + g_return_if_fail (G_VALUE_HOLDS_INT(value)); + priv->kind = g_value_get_int (value); + break; + + case PROP_STATE: + g_return_if_fail (G_VALUE_HOLDS_INT(value)); + priv->state = g_value_get_int (value); + break; + + case PROP_OBJECT_PATH: + g_return_if_fail (G_VALUE_HOLDS_STRING(value)); + g_free (priv->object_path); + priv->object_path = g_value_dup_string (value); + break; + + case PROP_ICON: + g_return_if_fail (G_VALUE_HOLDS_STRING(value)); + g_free (priv->icon); + priv->icon = g_value_dup_string (value); + break; + + case PROP_PERCENTAGE: + g_return_if_fail (G_VALUE_HOLDS_DOUBLE(value)); + priv->percentage = g_value_get_double (value); + break; + + case PROP_TIME: + g_return_if_fail (G_VALUE_HOLDS_UINT64(value)); + priv->time = g_value_get_uint64(value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (o, prop_id, pspec); + break; + } +} + +/*** +**** +***/ + +UpDeviceKind +indicator_power_device_get_kind (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + + return device->priv->kind; +} + +UpDeviceState +indicator_power_device_get_state (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); + + return device->priv->state; +} + +const gchar * +indicator_power_device_get_object_path (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + + return device->priv->object_path; +} + +const gchar * +indicator_power_device_get_icon (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + + return device->priv->icon; +} + +gdouble +indicator_power_device_get_percentage (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + + return device->priv->percentage; +} + +time_t +indicator_power_device_get_time (const IndicatorPowerDevice * device) +{ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + + return device->priv->time; +} + +/*** +**** +***/ + +IndicatorPowerDevice * +indicator_power_device_new (const gchar * object_path, + UpDeviceKind kind, + const gchar * icon_path, + gdouble percentage, + UpDeviceState state, + time_t timestamp) +{ + GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE, + INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, state, + INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, + INDICATOR_POWER_DEVICE_ICON, icon_path, + INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, + INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp, + NULL); + return INDICATOR_POWER_DEVICE(o); +} + +IndicatorPowerDevice * +indicator_power_device_new_from_variant (GVariant * v) +{ + UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; + UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; + const gchar * icon = NULL; + const gchar * object_path = NULL; + gdouble percentage = 0; + guint64 time = 0; + + g_variant_get (v, "(&su&sdut)", + &object_path, + &kind, + &icon, + &percentage, + &state, + &time); + + return indicator_power_device_new (object_path, + kind, + icon, + percentage, + state, + time); +} -- cgit v1.2.3 From caae4241b85412d6914d6fd1e0a9bac9da35c6f3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 11:50:38 -0500 Subject: remove the g_clear_pointer() calls s.t. things will build and run on alesage's Jenkins setup running Precise --- src/device.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index d52ce8a..219e738 100644 --- a/src/device.c +++ b/src/device.c @@ -130,8 +130,12 @@ indicator_power_device_finalize (GObject *object) IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object); IndicatorPowerDevicePrivate * priv = self->priv; - g_clear_pointer (&priv->object_path, g_free); - g_clear_pointer (&priv->icon, g_free); + //g_clear_pointer (&priv->object_path, g_free); + //g_clear_pointer (&priv->icon, g_free); + g_free (priv->object_path); + priv->object_path = NULL; + g_free (priv->icon); + priv->icon = NULL; } /*** -- cgit v1.2.3 From f023be5dbef15c87fbb10a0b891ee19c73595791 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 18:47:38 -0500 Subject: remove some unnecessary type compatibilty tests... g_object_set_property() does these tests for us --- src/device.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 219e738..d1b1a84 100644 --- a/src/device.c +++ b/src/device.c @@ -195,34 +195,28 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp switch (prop_id) { case PROP_KIND: - g_return_if_fail (G_VALUE_HOLDS_INT(value)); priv->kind = g_value_get_int (value); break; case PROP_STATE: - g_return_if_fail (G_VALUE_HOLDS_INT(value)); priv->state = g_value_get_int (value); break; case PROP_OBJECT_PATH: - g_return_if_fail (G_VALUE_HOLDS_STRING(value)); g_free (priv->object_path); priv->object_path = g_value_dup_string (value); break; case PROP_ICON: - g_return_if_fail (G_VALUE_HOLDS_STRING(value)); g_free (priv->icon); priv->icon = g_value_dup_string (value); break; case PROP_PERCENTAGE: - g_return_if_fail (G_VALUE_HOLDS_DOUBLE(value)); priv->percentage = g_value_get_double (value); break; case PROP_TIME: - g_return_if_fail (G_VALUE_HOLDS_UINT64(value)); priv->time = g_value_get_uint64(value); break; -- cgit v1.2.3 From 2643aa2da220f14f28744a0f2403a8e68364239a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 18:56:15 -0500 Subject: remove some unnecessary type compatibilty tests... g_object_get_property() does these tests for us --- src/device.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index d1b1a84..80b3208 100644 --- a/src/device.c +++ b/src/device.c @@ -151,32 +151,26 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) switch (prop_id) { case PROP_KIND: - g_return_if_fail (G_VALUE_HOLDS_INT(value)); g_value_set_int (value, priv->kind); break; case PROP_STATE: - g_return_if_fail (G_VALUE_HOLDS_INT(value)); g_value_set_int (value, priv->state); break; case PROP_OBJECT_PATH: - g_return_if_fail (G_VALUE_HOLDS_STRING(value)); g_value_set_string (value, priv->object_path); break; case PROP_ICON: - g_return_if_fail (G_VALUE_HOLDS_STRING(value)); g_value_set_string (value, priv->icon); break; case PROP_PERCENTAGE: - g_return_if_fail (G_VALUE_HOLDS_DOUBLE(value)); g_value_set_double (value, priv->percentage); break; case PROP_TIME: - g_return_if_fail (G_VALUE_HOLDS_UINT64(value)); g_value_set_uint64 (value, priv->time); break; -- cgit v1.2.3 From 9e188d4b15e27ec26dd139fee61a4af3c522a5a8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 19:00:51 -0500 Subject: remove the 'default' clause from get_property() and set_property(). Coverage testing isn't reaching them... glib is weeding out these invalid property keys before the device.c functions are ever reached. Nevertheless, leaving out a 'default' clause in a switch statement feels very unnatural to me. *twitch* *twitch* --- src/device.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 80b3208..4b983e9 100644 --- a/src/device.c +++ b/src/device.c @@ -173,10 +173,6 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) case PROP_TIME: g_value_set_uint64 (value, priv->time); break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (o, prop_id, pspec); - break; } } @@ -213,10 +209,6 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp case PROP_TIME: priv->time = g_value_get_uint64(value); break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (o, prop_id, pspec); - break; } } -- cgit v1.2.3 From 83aee49a3f8390f986bcaf8f255535f1c9072d91 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:19:05 -0500 Subject: try adding LCOV_EXCL_LINE for unreachable conditions (glib looking for subclasses of IndicatorPowerDevice; unreachables in G_DEFINE_TYPE) --- src/device.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 4b983e9..a5aa388 100644 --- a/src/device.c +++ b/src/device.c @@ -55,7 +55,7 @@ static void indicator_power_device_finalize (GObject *object); static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); -G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); +G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); /* LCOV_EXCL_LINE */ static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) @@ -219,7 +219,7 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->kind; } @@ -227,7 +227,7 @@ indicator_power_device_get_kind (const IndicatorPowerDevice * device) UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->state; } @@ -235,7 +235,7 @@ indicator_power_device_get_state (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->object_path; } @@ -243,7 +243,7 @@ indicator_power_device_get_object_path (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_icon (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->icon; } @@ -251,7 +251,7 @@ indicator_power_device_get_icon (const IndicatorPowerDevice * device) gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->percentage; } @@ -259,7 +259,7 @@ indicator_power_device_get_percentage (const IndicatorPowerDevice * device) time_t indicator_power_device_get_time (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ return device->priv->time; } -- cgit v1.2.3 From 21656d99ee4991f25b22963b7c3d961174e4b9f8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:22:51 -0500 Subject: nope, LCOV_EXCL_LINE doesn't work on macros --- src/device.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index a5aa388..4b983e9 100644 --- a/src/device.c +++ b/src/device.c @@ -55,7 +55,7 @@ static void indicator_power_device_finalize (GObject *object); static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); -G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); /* LCOV_EXCL_LINE */ +G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) @@ -219,7 +219,7 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); return device->priv->kind; } @@ -227,7 +227,7 @@ indicator_power_device_get_kind (const IndicatorPowerDevice * device) UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); return device->priv->state; } @@ -235,7 +235,7 @@ indicator_power_device_get_state (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); return device->priv->object_path; } @@ -243,7 +243,7 @@ indicator_power_device_get_object_path (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_icon (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); return device->priv->icon; } @@ -251,7 +251,7 @@ indicator_power_device_get_icon (const IndicatorPowerDevice * device) gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); return device->priv->percentage; } @@ -259,7 +259,7 @@ indicator_power_device_get_percentage (const IndicatorPowerDevice * device) time_t indicator_power_device_get_time (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); /* LCOV_EXCL_LINE */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); return device->priv->time; } -- cgit v1.2.3 From 194cfbc5c7be5b7b298a6e7079452af2f8331142 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:57:13 -0500 Subject: try adding LCOV_EXCL_{START,STOP} for unreachable conditions (glib looking for subclasses of IndicatorPowerDevice; unreachables in G_DEFINE_TYPE) --- src/device.c | 20 +++++++++++++++++--- src/indicator-power.c | 6 ++++++ 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 4b983e9..5cd39dd 100644 --- a/src/device.c +++ b/src/device.c @@ -55,7 +55,9 @@ static void indicator_power_device_finalize (GObject *object); static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); +/* LCOV_EXCL_START */ G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); +/* LCOV_EXCL_STOP */ static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) @@ -219,7 +221,9 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device) { + /* LCOV_EXCL_START */ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_STOP */ return device->priv->kind; } @@ -227,7 +231,9 @@ indicator_power_device_get_kind (const IndicatorPowerDevice * device) UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device) { + /* LCOV_EXCL_START */ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); + /* LCOV_EXCL_STOP */ return device->priv->state; } @@ -235,7 +241,9 @@ indicator_power_device_get_state (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); + /* LCOV_EXCL_STOP */ return device->priv->object_path; } @@ -243,7 +251,9 @@ indicator_power_device_get_object_path (const IndicatorPowerDevice * device) const gchar * indicator_power_device_get_icon (const IndicatorPowerDevice * device) { + /* LCOV_EXCL_START */ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_STOP */ return device->priv->icon; } @@ -251,7 +261,9 @@ indicator_power_device_get_icon (const IndicatorPowerDevice * device) gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), 0.0); + /* LCOV_EXCL_STOP */ return device->priv->percentage; } @@ -259,7 +271,9 @@ indicator_power_device_get_percentage (const IndicatorPowerDevice * device) time_t indicator_power_device_get_time (const IndicatorPowerDevice * device) { - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), (time_t)0); + /* LCOV_EXCL_STOP */ return device->priv->time; } diff --git a/src/indicator-power.c b/src/indicator-power.c index dc95aae..cf96619 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -59,8 +59,10 @@ struct _IndicatorPowerPrivate }; +/* LCOV_EXCL_START */ INDICATOR_SET_VERSION INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) +/* LCOV_EXCL_STOP */ /* Prototypes */ static void indicator_power_dispose (GObject *object); @@ -81,7 +83,9 @@ static void on_entry_added (IndicatorObject * io, I static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); */ +/* LCOV_EXCL_START */ G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); +/* LCOV_EXCL_STOP */ static void indicator_power_class_init (IndicatorPowerClass *klass) @@ -767,7 +771,9 @@ indicator_power_set_devices (IndicatorPower * self, GSList * new_devices; IndicatorPowerPrivate * priv; +/* LCOV_EXCL_START */ g_return_if_fail (IS_INDICATOR_POWER(self)); +/* LCOV_EXCL_STOP */ priv = self->priv; /* make a reff'ed list of the new devices */ -- cgit v1.2.3 From 1c35339e5c32df4908659e53be54d146893b6fe3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 13:35:52 -0500 Subject: use g_clear_pointer() where useful --- src/device.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 5cd39dd..bd182bf 100644 --- a/src/device.c +++ b/src/device.c @@ -132,12 +132,8 @@ indicator_power_device_finalize (GObject *object) IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object); IndicatorPowerDevicePrivate * priv = self->priv; - //g_clear_pointer (&priv->object_path, g_free); - //g_clear_pointer (&priv->icon, g_free); - g_free (priv->object_path); - priv->object_path = NULL; - g_free (priv->icon); - priv->icon = NULL; + g_clear_pointer (&priv->object_path, g_free); + g_clear_pointer (&priv->icon, g_free); } /*** -- cgit v1.2.3 From b18b5862fb4e69773bf89328f4c65fdd344d8f1a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 13:00:31 -0500 Subject: Add indicator_power_device_get_icon_names(). The main goal of this change is to make it possible to test the device's icon. A secondary goal is to clarify in the code how indicator-power's icons differ from the ones recommended by GSD. --- src/device.c | 243 +++++++++++++++++++++++++++++++++++++++++++++++++- src/device.h | 5 ++ src/indicator-power.c | 116 ++---------------------- tests/test-device.cc | 201 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 451 insertions(+), 114 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index bd182bf..3dbfd4e 100644 --- a/src/device.c +++ b/src/device.c @@ -211,7 +211,7 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp } /*** -**** +**** Accessors ***/ UpDeviceKind @@ -276,6 +276,247 @@ indicator_power_device_get_time (const IndicatorPowerDevice * device) /*** **** +**** +***/ + +/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ +static const gchar * +gpm_upower_get_device_icon_suffix (gdouble percentage) +{ + if (percentage < 10) return "caution"; + if (percentage < 30) return "low"; + if (percentage < 60) return "good"; + return "full"; +} + +/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ +static const gchar * +gpm_upower_get_device_icon_index (gdouble percentage) +{ + if (percentage < 10) return "000"; + if (percentage < 30) return "020"; + if (percentage < 50) return "040"; + if (percentage < 70) return "060"; + if (percentage < 90) return "080"; + return "100"; +} + +/** + indicator_power_device_get_icon_names: + @device: #IndicatorPowerDevice to generate the icon names from + + Based on GSD's power plugin, (c) Richard Hughes and licensed GPL >= 2. + It differs in these ways: + + (1) all charging batteries use the same icon regardless of progress: + + + (2) discharging batteries are keyed off of time left, rather than + percentage left, s.t. <= 30 minutes remaining gives the 'caution' icon. + + + Return value: (array zero-terminated=1) (transfer full): + a GStrv of icon names suitable for passing to g_themed_icon_new_from_names(). + Free with g_strfreev() when done. +*/ +GStrv +indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) +{ + char ** ret = NULL; + const gchar *suffix_str; + const gchar *index_str; + + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_STOP */ + + gdouble percentage = indicator_power_device_get_percentage (device); + const UpDeviceKind kind = indicator_power_device_get_kind (device); + const UpDeviceState state = indicator_power_device_get_state (device); + const char * kind_str = kind_str = up_device_kind_to_string (kind); + + /* get correct icon prefix */ + GString * filename = g_string_new (NULL); + + /* get the icon from some simple rules */ + if (kind == UP_DEVICE_KIND_LINE_POWER) { + g_string_append (filename, "ac-adapter-symbolic;"); + g_string_append (filename, "ac-adapter;"); + } else if (kind == UP_DEVICE_KIND_MONITOR) { + g_string_append (filename, "gpm-monitor-symbolic;"); + g_string_append (filename, "gpm-monitor;"); + } else switch (state) { + case UP_DEVICE_STATE_EMPTY: + g_string_append (filename, "battery-empty-symbolic;"); + g_string_append_printf (filename, "gpm-%s-empty;", kind_str); + g_string_append_printf (filename, "gpm-%s-000;", kind_str); + g_string_append (filename, "battery-empty;"); + break; + case UP_DEVICE_STATE_FULLY_CHARGED: + g_string_append (filename, "battery-full-charged-symbolic;"); + g_string_append (filename, "battery-full-charging-symbolic;"); + g_string_append_printf (filename, "gpm-%s-full;", kind_str); + g_string_append_printf (filename, "gpm-%s-100;", kind_str); + g_string_append (filename, "battery-full-charged;"); + g_string_append (filename, "battery-full-charging;"); + break; + case UP_DEVICE_STATE_CHARGING: + case UP_DEVICE_STATE_PENDING_CHARGE: + /* When charging, always use the same icon regardless of percentage. + */ + percentage = 0; + + suffix_str = gpm_upower_get_device_icon_suffix (percentage); + index_str = gpm_upower_get_device_icon_index (percentage); + g_string_append_printf (filename, "battery-%s-charging-symbolic;", suffix_str); + g_string_append_printf (filename, "gpm-%s-%s-charging;", kind_str, index_str); + g_string_append_printf (filename, "battery-%s-charging;", suffix_str); + break; + case UP_DEVICE_STATE_DISCHARGING: + case UP_DEVICE_STATE_PENDING_DISCHARGE: { + /* Don't show the caution/red icons unless we have <=30 min left. + + Themes use the caution color when the percentage is 0% or 20%, + so if we have >30 min left, use 30% as the icon's percentage floor */ + if (indicator_power_device_get_time (device) > (30*60)) + percentage = MAX(percentage, 30); + + suffix_str = gpm_upower_get_device_icon_suffix (percentage); + index_str = gpm_upower_get_device_icon_index (percentage); + g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); + g_string_append_printf (filename, "gpm-%s-%s;", kind_str, index_str); + g_string_append_printf (filename, "battery-%s;", suffix_str); + break; + } + default: + g_string_append (filename, "battery-missing-symbolic;"); + g_string_append (filename, "gpm-battery-missing;"); + g_string_append (filename, "battery-missing;"); + } + + ret = g_strsplit (filename->str, ";", -1); + g_string_free (filename, TRUE); + return ret; +} + +/** + indicator_power_device_get_gicon: + @device: #IndicatorPowerDevice to generate the icon names from + + A convenience function to call g_themed_icon_new_from_names() + with the names returned by indicator_power_device_get_icon_names() + + Return value: (transfer full): A themed GIcon +*/ +GIcon * +indicator_power_device_get_gicon (const IndicatorPowerDevice * device) +{ + GStrv names = indicator_power_device_get_icon_names (device); + GIcon * icon = g_themed_icon_new_from_names (names, -1); + g_strfreev (names); + return icon; +} + + + +#if 0 +static const gchar * +get_icon_percentage_for_status (const gchar *status) +{ + + if (g_strcmp0 (status, "caution") == 0) + return "000"; + else if (g_strcmp0 (status, "low") == 0) + return "040"; + else if (g_strcmp0 (status, "good") == 0) + return "080"; + else + return "100"; +} + +static GIcon* +build_battery_icon (UpDeviceState state, + gchar *suffix_str) +{ + GIcon *gicon; + + GString *filename; + gchar **iconnames; + + filename = g_string_new (NULL); + + if (state == UP_DEVICE_STATE_FULLY_CHARGED) + { + g_string_append (filename, "battery-charged;"); + g_string_append (filename, "battery-full-charged-symbolic;"); + g_string_append (filename, "battery-full-charged;"); + g_string_append (filename, "gpm-battery-charged;"); + g_string_append (filename, "gpm-battery-100-charging;"); + } + else if (state == UP_DEVICE_STATE_CHARGING) + { + g_string_append (filename, "battery-000-charging;"); + g_string_append (filename, "battery-caution-charging-symbolic;"); + g_string_append (filename, "battery-caution-charging;"); + g_string_append (filename, "gpm-battery-000-charging;"); + } + else if (state == UP_DEVICE_STATE_DISCHARGING) + { + const gchar *percentage = get_icon_percentage_for_status (suffix_str); + g_string_append_printf (filename, "battery-%s;", suffix_str); + g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); + g_string_append_printf (filename, "battery-%s;", percentage); + g_string_append_printf (filename, "gpm-battery-%s;", percentage); + } + + iconnames = g_strsplit (filename->str, ";", -1); + gicon = g_themed_icon_new_from_names (iconnames, -1); + + g_strfreev (iconnames); + g_string_free (filename, TRUE); + + return gicon; +} + +static GIcon* +get_device_icon (UpDeviceKind kind, + UpDeviceState state, + guint64 time_sec, + const gchar *device_icon) +{ + GIcon *gicon = NULL; + + if (kind == UP_DEVICE_KIND_BATTERY && + (state == UP_DEVICE_STATE_FULLY_CHARGED || + state == UP_DEVICE_STATE_CHARGING || + state == UP_DEVICE_STATE_DISCHARGING)) + { + if (state == UP_DEVICE_STATE_FULLY_CHARGED || + state == UP_DEVICE_STATE_CHARGING) + { + gicon = build_battery_icon (state, NULL); + } + else if (state == UP_DEVICE_STATE_DISCHARGING) + { + if ((time_sec > 60 * 30) && /* more than 30 minutes left */ + (g_strrstr (device_icon, "000") || + g_strrstr (device_icon, "020") || + g_strrstr (device_icon, "caution"))) /* the icon is red */ + { + gicon = build_battery_icon (state, "low"); + } + } + } + + if (gicon == NULL) + gicon = g_icon_new_for_string (device_icon, NULL); + + return gicon; +} +#endif + +/*** +**** Instantiation ***/ IndicatorPowerDevice * diff --git a/src/device.h b/src/device.h index e9de044..9b82830 100644 --- a/src/device.h +++ b/src/device.h @@ -94,6 +94,11 @@ const gchar * indicator_power_device_get_icon (const IndicatorPowerDevice gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device); time_t indicator_power_device_get_time (const IndicatorPowerDevice * device); +GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device); +GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device); + + + G_END_DECLS diff --git a/src/indicator-power.c b/src/indicator-power.c index 7029739..96c0f1d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -431,101 +431,6 @@ set_accessible_desc (IndicatorPower *self, const gchar *desc) } } -static const gchar * -get_icon_percentage_for_status (const gchar *status) -{ - - if (g_strcmp0 (status, "caution") == 0) - return "000"; - else if (g_strcmp0 (status, "low") == 0) - return "040"; - else if (g_strcmp0 (status, "good") == 0) - return "080"; - else - return "100"; -} - -static GIcon* -build_battery_icon (UpDeviceState state, - gchar *suffix_str) -{ - GIcon *gicon; - - GString *filename; - gchar **iconnames; - - filename = g_string_new (NULL); - - if (state == UP_DEVICE_STATE_FULLY_CHARGED) - { - g_string_append (filename, "battery-charged;"); - g_string_append (filename, "battery-full-charged-symbolic;"); - g_string_append (filename, "battery-full-charged;"); - g_string_append (filename, "gpm-battery-charged;"); - g_string_append (filename, "gpm-battery-100-charging;"); - } - else if (state == UP_DEVICE_STATE_CHARGING) - { - g_string_append (filename, "battery-000-charging;"); - g_string_append (filename, "battery-caution-charging-symbolic;"); - g_string_append (filename, "battery-caution-charging;"); - g_string_append (filename, "gpm-battery-000-charging;"); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - const gchar *percentage = get_icon_percentage_for_status (suffix_str); - g_string_append_printf (filename, "battery-%s;", suffix_str); - g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); - g_string_append_printf (filename, "battery-%s;", percentage); - g_string_append_printf (filename, "gpm-battery-%s;", percentage); - } - - iconnames = g_strsplit (filename->str, ";", -1); - gicon = g_themed_icon_new_from_names (iconnames, -1); - - g_strfreev (iconnames); - g_string_free (filename, TRUE); - - return gicon; -} - -static GIcon* -get_device_icon (UpDeviceKind kind, - UpDeviceState state, - guint64 time_sec, - const gchar *device_icon) -{ - GIcon *gicon = NULL; - - if (kind == UP_DEVICE_KIND_BATTERY && - (state == UP_DEVICE_STATE_FULLY_CHARGED || - state == UP_DEVICE_STATE_CHARGING || - state == UP_DEVICE_STATE_DISCHARGING)) - { - if (state == UP_DEVICE_STATE_FULLY_CHARGED || - state == UP_DEVICE_STATE_CHARGING) - { - gicon = build_battery_icon (state, NULL); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - if ((time_sec > 60 * 30) && /* more than 30 minutes left */ - (g_strrstr (device_icon, "000") || - g_strrstr (device_icon, "020") || - g_strrstr (device_icon, "caution"))) /* the icon is red */ - { - gicon = build_battery_icon (state, "low"); - } - } - } - - if (gicon == NULL) - gicon = g_icon_new_for_string (device_icon, NULL); - - return gicon; -} - - static gboolean menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) { @@ -538,7 +443,7 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) GtkWidget *item; GtkWidget *details_label; GtkWidget *grid; - GIcon *device_gicons; + GIcon *device_gicon; const gchar *device_name; gchar *short_details = NULL; gchar *details = NULL; @@ -546,14 +451,12 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) AtkObject *atk_object; const time_t time = indicator_power_device_get_time (device); const UpDeviceState state = indicator_power_device_get_state (device); - const gchar * device_icon = indicator_power_device_get_icon (device); const gdouble percentage = indicator_power_device_get_percentage (device); /* Process the data */ - device_gicons = get_device_icon (kind, state, time, device_icon); - icon = gtk_image_new_from_gicon (device_gicons, - GTK_ICON_SIZE_SMALL_TOOLBAR); - g_clear_object (&device_gicons); + device_gicon = indicator_power_device_get_gicon (device); + icon = gtk_image_new_from_gicon (device_gicon, GTK_ICON_SIZE_SMALL_TOOLBAR); + g_clear_object (&device_gicon); device_name = device_kind_to_localised_string (kind); @@ -727,7 +630,7 @@ get_primary_device (GSList * devices) static void put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) { - GIcon *device_gicons; + GIcon *device_gicon; gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; @@ -736,15 +639,12 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) const time_t time = indicator_power_device_get_time (device); const UpDeviceKind kind = indicator_power_device_get_kind (device); const UpDeviceState state = indicator_power_device_get_state (device); - const gchar * device_icon = indicator_power_device_get_icon (device); const gdouble percentage = indicator_power_device_get_percentage (device); /* set icon */ - device_gicons = get_device_icon (kind, state, time, device_icon); - gtk_image_set_from_gicon (priv->status_image, - device_gicons, - GTK_ICON_SIZE_LARGE_TOOLBAR); - g_clear_object (&device_gicons); + device_gicon = indicator_power_device_get_gicon (device); + gtk_image_set_from_gicon (priv->status_image, device_gicon, GTK_ICON_SIZE_LARGE_TOOLBAR); + g_clear_object (&device_gicon); gtk_widget_show (GTK_WIDGET (priv->status_image)); diff --git a/tests/test-device.cc b/tests/test-device.cc index 132d762..b11188c 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -34,11 +34,37 @@ namespace } } +class DeviceTest : public ::testing::Test +{ + protected: + + virtual void SetUp() + { + ensure_glib_initialized (); + } + + virtual void TearDown() + { + } + + protected: + + void check_icon_names (const IndicatorPowerDevice * device, const char * expected) + { + char ** names = indicator_power_device_get_icon_names (device); + char * str = g_strjoinv (";", names); + ASSERT_STREQ (expected, str); + g_free (str); + g_strfreev (names); + } +}; + + /*** **** ***/ -TEST(DeviceTest, GObjectNew) +TEST_F(DeviceTest, GObjectNew) { ensure_glib_initialized (); @@ -49,7 +75,7 @@ TEST(DeviceTest, GObjectNew) g_object_unref (o); } -TEST(DeviceTest, Properties) +TEST_F(DeviceTest, Properties) { int i; gdouble d; @@ -108,7 +134,7 @@ TEST(DeviceTest, Properties) g_object_unref (o); } -TEST(DeviceTest, New) +TEST_F(DeviceTest, New) { ensure_glib_initialized (); @@ -131,7 +157,7 @@ TEST(DeviceTest, New) g_object_unref (device); } -TEST(DeviceTest, NewFromVariant) +TEST_F(DeviceTest, NewFromVariant) { ensure_glib_initialized (); @@ -158,7 +184,7 @@ TEST(DeviceTest, NewFromVariant) g_variant_unref (variant); } -TEST(DeviceTest, BadAccessors) +TEST_F(DeviceTest, BadAccessors) { ensure_glib_initialized (); @@ -181,3 +207,168 @@ TEST(DeviceTest, BadAccessors) indicator_power_device_get_object_path (device); g_object_unref (device); } + +/*** +**** +***/ + +TEST_F(DeviceTest, IconNames) +{ + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); + GObject * o = G_OBJECT(device); + + /* power */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, + NULL); + check_icon_names (device, "ac-adapter-symbolic;" + "ac-adapter;"); + + /* monitor */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_MONITOR, + NULL); + check_icon_names (device, "gpm-monitor-symbolic;" + "gpm-monitor;"); + + /* empty battery */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY, + NULL); + check_icon_names (device, "battery-empty-symbolic;" + "gpm-battery-empty;" + "gpm-battery-000;" + "battery-empty;"); + + /* charged battery */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + NULL); + check_icon_names (device, "battery-full-charged-symbolic;battery-full-charging-symbolic;" + "gpm-battery-full;gpm-battery-100;" + "battery-full-charged;battery-full-charging;"); + + /* charging battery, 95% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, + NULL); + check_icon_names (device, "battery-caution-charging-symbolic;" + "gpm-battery-000-charging;" + "battery-caution-charging;"); + + /* charging battery, 85% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, + NULL); + check_icon_names (device, "battery-caution-charging-symbolic;" + "gpm-battery-000-charging;" + "battery-caution-charging;"); + + /* charging battery, 50% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + NULL); + check_icon_names (device, "battery-caution-charging-symbolic;" + "gpm-battery-000-charging;" + "battery-caution-charging;"); + + /* charging battery, 25% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + NULL); + check_icon_names (device, "battery-caution-charging-symbolic;" + "gpm-battery-000-charging;" + "battery-caution-charging;"); + + /* charging battery, 5% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + NULL); + check_icon_names (device, "battery-caution-charging-symbolic;" + "gpm-battery-000-charging;" + "battery-caution-charging;"); + + + /* discharging battery, 95% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, + NULL); + check_icon_names (device, "battery-full-symbolic;" + "gpm-battery-100;" + "battery-full;"); + + /* discharging battery, 85% */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, + NULL); + check_icon_names (device, "battery-full-symbolic;" + "gpm-battery-080;" + "battery-full;"); + + /* discharging battery, 50% -- 1 hour left */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + NULL); + check_icon_names (device, "battery-good-symbolic;" + "gpm-battery-060;" + "battery-good;"); + + /* discharging battery, 25% -- 1 hour left */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + NULL); + check_icon_names (device, "battery-good-symbolic;" + "gpm-battery-040;" + "battery-good;"); + + /* discharging battery, 25% -- 15 minutes left */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + NULL); + check_icon_names (device, "battery-low-symbolic;" + "gpm-battery-020;" + "battery-low;"); + + /* discharging battery, 5% -- 1 hour left */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + NULL); + check_icon_names (device, "battery-good-symbolic;" + "gpm-battery-040;" + "battery-good;"); + + /* discharging battery, 5% -- 15 minutes left */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + NULL); + check_icon_names (device, "battery-caution-symbolic;" + "gpm-battery-000;" + "battery-caution;"); + + /* state unknown */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, + NULL); + check_icon_names (device, "battery-missing-symbolic;" + "gpm-battery-missing;" + "battery-missing;"); + + /* cleanup */ + g_object_unref(o); +} + -- cgit v1.2.3 From 97867c31fd3f4767fe891de31786b7b32d6dd35a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 13:08:27 -0500 Subject: remove IndicatorPowerDevice's now-unused device icon string. --- src/device.c | 126 ------------------------------------------------ src/device.h | 3 -- tests/test-device.cc | 12 ----- tests/test-indicator.cc | 23 ++++----- 4 files changed, 10 insertions(+), 154 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 3dbfd4e..cd35c9b 100644 --- a/src/device.c +++ b/src/device.c @@ -28,7 +28,6 @@ struct _IndicatorPowerDevicePrivate UpDeviceKind kind; UpDeviceState state; gchar * object_path; - gchar * icon; gdouble percentage; time_t time; }; @@ -86,10 +85,6 @@ indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec); - pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_ICON, "icon", "The device's icon", NULL, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_ICON, pspec); - pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged", 0.0, 100.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); @@ -113,7 +108,6 @@ indicator_power_device_init (IndicatorPowerDevice *self) priv->kind = UP_DEVICE_KIND_UNKNOWN; priv->state = UP_DEVICE_STATE_UNKNOWN; priv->object_path = NULL; - priv->icon = NULL; priv->percentage = 0.0; priv->time = 0; @@ -133,7 +127,6 @@ indicator_power_device_finalize (GObject *object) IndicatorPowerDevicePrivate * priv = self->priv; g_clear_pointer (&priv->object_path, g_free); - g_clear_pointer (&priv->icon, g_free); } /*** @@ -160,10 +153,6 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) g_value_set_string (value, priv->object_path); break; - case PROP_ICON: - g_value_set_string (value, priv->icon); - break; - case PROP_PERCENTAGE: g_value_set_double (value, priv->percentage); break; @@ -195,11 +184,6 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp priv->object_path = g_value_dup_string (value); break; - case PROP_ICON: - g_free (priv->icon); - priv->icon = g_value_dup_string (value); - break; - case PROP_PERCENTAGE: priv->percentage = g_value_get_double (value); break; @@ -244,16 +228,6 @@ indicator_power_device_get_object_path (const IndicatorPowerDevice * device) return device->priv->object_path; } -const gchar * -indicator_power_device_get_icon (const IndicatorPowerDevice * device) -{ - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); - /* LCOV_EXCL_STOP */ - - return device->priv->icon; -} - gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device) { @@ -418,103 +392,6 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device) } - -#if 0 -static const gchar * -get_icon_percentage_for_status (const gchar *status) -{ - - if (g_strcmp0 (status, "caution") == 0) - return "000"; - else if (g_strcmp0 (status, "low") == 0) - return "040"; - else if (g_strcmp0 (status, "good") == 0) - return "080"; - else - return "100"; -} - -static GIcon* -build_battery_icon (UpDeviceState state, - gchar *suffix_str) -{ - GIcon *gicon; - - GString *filename; - gchar **iconnames; - - filename = g_string_new (NULL); - - if (state == UP_DEVICE_STATE_FULLY_CHARGED) - { - g_string_append (filename, "battery-charged;"); - g_string_append (filename, "battery-full-charged-symbolic;"); - g_string_append (filename, "battery-full-charged;"); - g_string_append (filename, "gpm-battery-charged;"); - g_string_append (filename, "gpm-battery-100-charging;"); - } - else if (state == UP_DEVICE_STATE_CHARGING) - { - g_string_append (filename, "battery-000-charging;"); - g_string_append (filename, "battery-caution-charging-symbolic;"); - g_string_append (filename, "battery-caution-charging;"); - g_string_append (filename, "gpm-battery-000-charging;"); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - const gchar *percentage = get_icon_percentage_for_status (suffix_str); - g_string_append_printf (filename, "battery-%s;", suffix_str); - g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); - g_string_append_printf (filename, "battery-%s;", percentage); - g_string_append_printf (filename, "gpm-battery-%s;", percentage); - } - - iconnames = g_strsplit (filename->str, ";", -1); - gicon = g_themed_icon_new_from_names (iconnames, -1); - - g_strfreev (iconnames); - g_string_free (filename, TRUE); - - return gicon; -} - -static GIcon* -get_device_icon (UpDeviceKind kind, - UpDeviceState state, - guint64 time_sec, - const gchar *device_icon) -{ - GIcon *gicon = NULL; - - if (kind == UP_DEVICE_KIND_BATTERY && - (state == UP_DEVICE_STATE_FULLY_CHARGED || - state == UP_DEVICE_STATE_CHARGING || - state == UP_DEVICE_STATE_DISCHARGING)) - { - if (state == UP_DEVICE_STATE_FULLY_CHARGED || - state == UP_DEVICE_STATE_CHARGING) - { - gicon = build_battery_icon (state, NULL); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - if ((time_sec > 60 * 30) && /* more than 30 minutes left */ - (g_strrstr (device_icon, "000") || - g_strrstr (device_icon, "020") || - g_strrstr (device_icon, "caution"))) /* the icon is red */ - { - gicon = build_battery_icon (state, "low"); - } - } - } - - if (gicon == NULL) - gicon = g_icon_new_for_string (device_icon, NULL); - - return gicon; -} -#endif - /*** **** Instantiation ***/ @@ -522,7 +399,6 @@ get_device_icon (UpDeviceKind kind, IndicatorPowerDevice * indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, - const gchar * icon_path, gdouble percentage, UpDeviceState state, time_t timestamp) @@ -531,7 +407,6 @@ indicator_power_device_new (const gchar * object_path, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, state, INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, - INDICATOR_POWER_DEVICE_ICON, icon_path, INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp, NULL); @@ -558,7 +433,6 @@ indicator_power_device_new_from_variant (GVariant * v) return indicator_power_device_new (object_path, kind, - icon, percentage, state, time); diff --git a/src/device.h b/src/device.h index 9b82830..11cd83b 100644 --- a/src/device.h +++ b/src/device.h @@ -43,7 +43,6 @@ typedef struct _IndicatorPowerDevicePrivate IndicatorPowerDevicePrivate; #define INDICATOR_POWER_DEVICE_KIND "indicator-power-device-kind" #define INDICATOR_POWER_DEVICE_STATE "indicator-power-device-state" #define INDICATOR_POWER_DEVICE_OBJECT_PATH "indicator-power-device-object-path" -#define INDICATOR_POWER_DEVICE_ICON "indicator-power-device-icon" #define INDICATOR_POWER_DEVICE_PERCENTAGE "indicator-power-device-percentage" #define INDICATOR_POWER_DEVICE_TIME "indicator-power-device-time" @@ -75,7 +74,6 @@ GType indicator_power_device_get_type (void); IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, - const gchar * icon, gdouble percentage, UpDeviceState state, time_t time); @@ -90,7 +88,6 @@ IndicatorPowerDevice* indicator_power_device_new_from_variant (GVariant * varian UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device); UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device); const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device); -const gchar * indicator_power_device_get_icon (const IndicatorPowerDevice * device); gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device); time_t indicator_power_device_get_time (const IndicatorPowerDevice * device); diff --git a/tests/test-device.cc b/tests/test-device.cc index b11188c..0ea7177 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -111,13 +111,6 @@ TEST_F(DeviceTest, Properties) ASSERT_STREQ (str, "/object/path"); g_free (str); - // ICON - key = INDICATOR_POWER_DEVICE_ICON; - g_object_set (o, key, "something", NULL); - g_object_get (o, key, &str, NULL); - ASSERT_STREQ (str, "something"); - g_free (str); - // PERCENTAGE key = INDICATOR_POWER_DEVICE_PERCENTAGE; g_object_set (o, key, 50.0, NULL); @@ -140,7 +133,6 @@ TEST_F(DeviceTest, New) IndicatorPowerDevice * device = indicator_power_device_new ("/object/path", UP_DEVICE_KIND_BATTERY, - "icon", 50.0, UP_DEVICE_STATE_CHARGING, 30); @@ -149,7 +141,6 @@ TEST_F(DeviceTest, New) ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY); ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING); ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path"); - ASSERT_STREQ (indicator_power_device_get_icon(device), "icon"); ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); ASSERT_EQ (indicator_power_device_get_time(device), 30); @@ -175,7 +166,6 @@ TEST_F(DeviceTest, NewFromVariant) ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY); ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING); ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path"); - ASSERT_STREQ (indicator_power_device_get_icon(device), "icon"); ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); ASSERT_EQ (indicator_power_device_get_time(device), 30); @@ -192,7 +182,6 @@ TEST_F(DeviceTest, BadAccessors) IndicatorPowerDevice * device = NULL; indicator_power_device_get_kind (device); indicator_power_device_get_time (device); - indicator_power_device_get_icon (device); indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); @@ -201,7 +190,6 @@ TEST_F(DeviceTest, BadAccessors) device = reinterpret_cast(g_cancellable_new ()); indicator_power_device_get_kind (device); indicator_power_device_get_time (device); - indicator_power_device_get_icon (device); indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 32d6f4f..b9f7321 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -61,13 +61,11 @@ class IndicatorTest : public ::testing::Test ac_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/line_power_AC", UP_DEVICE_KIND_LINE_POWER, - ". GThemedIcon ac-adapter-symbolic ac-adapter ", 0.0, UP_DEVICE_STATE_UNKNOWN, 0); battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", UP_DEVICE_KIND_BATTERY, - ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 52.871712, UP_DEVICE_STATE_DISCHARGING, 8834); } @@ -238,7 +236,6 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) IndicatorPowerDevice * bad_battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", UP_DEVICE_KIND_BATTERY, - ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 53, UP_DEVICE_STATE_CHARGING, 0); GSList * devices = NULL; @@ -262,34 +259,34 @@ TEST_F(IndicatorTest, OtherDevices) devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/mouse", UP_DEVICE_KIND_MOUSE, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/media_player", UP_DEVICE_KIND_MEDIA_PLAYER, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); indicator_power_set_devices (power, devices); -- cgit v1.2.3 From 8476e802d84ee91e50dd6556d8293ece77033201 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 14:00:12 -0500 Subject: Move private indicator-power function build_device_time_details() to device.c to public function indicator_power_device_get_time_details() so that we can unit test the user-visible strings. --- po/POTFILES.in | 2 + src/device.c | 195 ++++++++++++++++++++++++++++++++++++++++++++++++ src/device.h | 5 ++ src/indicator-power.c | 201 +------------------------------------------------- 4 files changed, 205 insertions(+), 198 deletions(-) (limited to 'src/device.c') diff --git a/po/POTFILES.in b/po/POTFILES.in index 7c8c8a1..3061414 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,2 +1,4 @@ data/com.canonical.indicator.power.gschema.xml.in +src/device.c +src/dbus-listener.c src/indicator-power.c diff --git a/src/device.c b/src/device.c index cd35c9b..dc8db3a 100644 --- a/src/device.c +++ b/src/device.c @@ -21,6 +21,12 @@ License along with this library. If not, see . */ +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include + #include "device.h" struct _IndicatorPowerDevicePrivate @@ -391,6 +397,195 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device) return icon; } +/*** +**** +***/ + +static void +get_timestring (guint64 time_secs, + gchar **short_timestring, + gchar **detailed_timestring) +{ + gint hours; + gint minutes; + + /* Add 0.5 to do rounding */ + minutes = (int) ( ( time_secs / 60.0 ) + 0.5 ); + + if (minutes == 0) + { + *short_timestring = g_strdup (_("Unknown time")); + *detailed_timestring = g_strdup (_("Unknown time")); + + return; + } + + if (minutes < 60) + { + *short_timestring = g_strdup_printf ("0:%.2i", minutes); + *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute", + "%i minutes", + minutes), minutes); + return; + } + + hours = minutes / 60; + minutes = minutes % 60; + + *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); + + if (minutes == 0) + { + *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, + "%i hour", + "%i hours", + hours), hours); + } + else + { + /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes" + * Swap order with "%2$s %2$i %1$s %1$i if needed */ + *detailed_timestring = g_strdup_printf (_("%i %s %i %s"), + hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours), + minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes)); + } +} + +static const gchar * +device_kind_to_localised_string (UpDeviceKind kind) +{ + const gchar *text = NULL; + + switch (kind) { + case UP_DEVICE_KIND_LINE_POWER: + /* TRANSLATORS: system power cord */ + text = _("AC adapter"); + break; + case UP_DEVICE_KIND_BATTERY: + /* TRANSLATORS: laptop primary battery */ + text = _("Battery"); + break; + case UP_DEVICE_KIND_UPS: + /* TRANSLATORS: battery-backed AC power source */ + text = _("UPS"); + break; + case UP_DEVICE_KIND_MONITOR: + /* TRANSLATORS: a monitor is a device to measure voltage and current */ + text = _("Monitor"); + break; + case UP_DEVICE_KIND_MOUSE: + /* TRANSLATORS: wireless mice with internal batteries */ + text = _("Mouse"); + break; + case UP_DEVICE_KIND_KEYBOARD: + /* TRANSLATORS: wireless keyboard with internal battery */ + text = _("Keyboard"); + break; + case UP_DEVICE_KIND_PDA: + /* TRANSLATORS: portable device */ + text = _("PDA"); + break; + case UP_DEVICE_KIND_PHONE: + /* TRANSLATORS: cell phone (mobile...) */ + text = _("Cell phone"); + break; + case UP_DEVICE_KIND_MEDIA_PLAYER: + /* TRANSLATORS: media player, mp3 etc */ + text = _("Media player"); + break; + case UP_DEVICE_KIND_TABLET: + /* TRANSLATORS: tablet device */ + text = _("Tablet"); + break; + case UP_DEVICE_KIND_COMPUTER: + /* TRANSLATORS: tablet device */ + text = _("Computer"); + break; + default: + g_warning ("enum unrecognised: %i", kind); + text = up_device_kind_to_string (kind); + } + + return text; +} + +void +indicator_power_device_get_time_details (const IndicatorPowerDevice * device, + gchar ** short_details, + gchar ** details, + gchar ** accessible_name) +{ + const time_t time = indicator_power_device_get_time (device); + const UpDeviceState state = indicator_power_device_get_state (device); + const gdouble percentage = indicator_power_device_get_percentage (device); + const gchar * device_name = device_kind_to_localised_string (indicator_power_device_get_kind(device)); + + gchar *short_timestring = NULL; + gchar *detailed_timestring = NULL; + + if (time > 0) + { + get_timestring (time, + &short_timestring, + &detailed_timestring); + + if (state == UP_DEVICE_STATE_CHARGING) + { + /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ + *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), + device_name, detailed_timestring, percentage); + *details = g_strdup_printf (_("%s (%s to charge)"), + device_name, short_timestring); + *short_details = g_strdup_printf ("(%s)", short_timestring); + } + else if (state == UP_DEVICE_STATE_DISCHARGING) + { + *short_details = g_strdup_printf ("%s", short_timestring); + + if (time > 43200) /* 12 hours */ + { + *accessible_name = g_strdup_printf (_("%s"), device_name); + *details = g_strdup_printf (_("%s"), device_name); + } + else + { + /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ + *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), + device_name, detailed_timestring, percentage); + *details = g_strdup_printf (_("%s (%s left)"), + device_name, short_timestring); + } + } + + g_free (short_timestring); + g_free (detailed_timestring); + } + else + { + if (state == UP_DEVICE_STATE_FULLY_CHARGED) + { + *details = g_strdup_printf (_("%s (charged)"), device_name); + *accessible_name = g_strdup (*details); + *short_details = g_strdup (""); + } + else if (percentage > 0) + { + /* TRANSLATORS: %2 is a percentage value. Note: this string is only + * used when we don't have a time value */ + *details = g_strdup_printf (_("%s (%.0lf%%)"), + device_name, percentage); + *accessible_name = g_strdup (*details); + *short_details = g_strdup_printf (_("(%.0lf%%)"), + percentage); + } + else + { + *details = g_strdup_printf (_("%s (not present)"), device_name); + *accessible_name = g_strdup (*details); + *short_details = g_strdup (_("(not present)")); + } + } +} /*** **** Instantiation diff --git a/src/device.h b/src/device.h index 11cd83b..566b196 100644 --- a/src/device.h +++ b/src/device.h @@ -94,6 +94,11 @@ time_t indicator_power_device_get_time (const IndicatorPowerDevice GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device); GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device); +void indicator_power_device_get_time_details (const IndicatorPowerDevice * device, + gchar ** short_details, + gchar ** details, + gchar ** accessible_name); + diff --git a/src/indicator-power.c b/src/indicator-power.c index 96c0f1d..d5735ee 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -202,190 +202,6 @@ show_preferences_cb (GtkMenuItem *item, spawn_command_line_async ("gnome-control-center power"); } -static void -get_timestring (guint64 time_secs, - gchar **short_timestring, - gchar **detailed_timestring) -{ - gint hours; - gint minutes; - - /* Add 0.5 to do rounding */ - minutes = (int) ( ( time_secs / 60.0 ) + 0.5 ); - - if (minutes == 0) - { - *short_timestring = g_strdup (_("Unknown time")); - *detailed_timestring = g_strdup (_("Unknown time")); - - return; - } - - if (minutes < 60) - { - *short_timestring = g_strdup_printf ("0:%.2i", minutes); - *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute", - "%i minutes", - minutes), minutes); - return; - } - - hours = minutes / 60; - minutes = minutes % 60; - - *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes); - - if (minutes == 0) - { - *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, - "%i hour", - "%i hours", - hours), hours); - } - else - { - /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes" - * Swap order with "%2$s %2$i %1$s %1$i if needed */ - *detailed_timestring = g_strdup_printf (_("%i %s %i %s"), - hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours), - minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes)); - } -} - -static const gchar * -device_kind_to_localised_string (UpDeviceKind kind) -{ - const gchar *text = NULL; - - switch (kind) { - case UP_DEVICE_KIND_LINE_POWER: - /* TRANSLATORS: system power cord */ - text = _("AC adapter"); - break; - case UP_DEVICE_KIND_BATTERY: - /* TRANSLATORS: laptop primary battery */ - text = _("Battery"); - break; - case UP_DEVICE_KIND_UPS: - /* TRANSLATORS: battery-backed AC power source */ - text = _("UPS"); - break; - case UP_DEVICE_KIND_MONITOR: - /* TRANSLATORS: a monitor is a device to measure voltage and current */ - text = _("Monitor"); - break; - case UP_DEVICE_KIND_MOUSE: - /* TRANSLATORS: wireless mice with internal batteries */ - text = _("Mouse"); - break; - case UP_DEVICE_KIND_KEYBOARD: - /* TRANSLATORS: wireless keyboard with internal battery */ - text = _("Keyboard"); - break; - case UP_DEVICE_KIND_PDA: - /* TRANSLATORS: portable device */ - text = _("PDA"); - break; - case UP_DEVICE_KIND_PHONE: - /* TRANSLATORS: cell phone (mobile...) */ - text = _("Cell phone"); - break; - case UP_DEVICE_KIND_MEDIA_PLAYER: - /* TRANSLATORS: media player, mp3 etc */ - text = _("Media player"); - break; - case UP_DEVICE_KIND_TABLET: - /* TRANSLATORS: tablet device */ - text = _("Tablet"); - break; - case UP_DEVICE_KIND_COMPUTER: - /* TRANSLATORS: tablet device */ - text = _("Computer"); - break; - default: - g_warning ("enum unrecognised: %i", kind); - text = up_device_kind_to_string (kind); - } - - return text; -} - -static void -build_device_time_details (const gchar *device_name, - guint64 time, - UpDeviceState state, - gdouble percentage, - gchar **short_details, - gchar **details, - gchar **accessible_name) -{ - gchar *short_timestring = NULL; - gchar *detailed_timestring = NULL; - - if (time > 0) - { - get_timestring (time, - &short_timestring, - &detailed_timestring); - - if (state == UP_DEVICE_STATE_CHARGING) - { - /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), - device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s to charge)"), - device_name, short_timestring); - *short_details = g_strdup_printf ("(%s)", short_timestring); - } - else if (state == UP_DEVICE_STATE_DISCHARGING) - { - *short_details = g_strdup_printf ("%s", short_timestring); - - if (time > 43200) /* 12 hours */ - { - *accessible_name = g_strdup_printf (_("%s"), device_name); - *details = g_strdup_printf (_("%s"), device_name); - } - else - { - /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), - device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s left)"), - device_name, short_timestring); - } - } - - g_free (short_timestring); - g_free (detailed_timestring); - } - else - { - if (state == UP_DEVICE_STATE_FULLY_CHARGED) - { - *details = g_strdup_printf (_("%s (charged)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (""); - } - else if (percentage > 0) - { - /* TRANSLATORS: %2 is a percentage value. Note: this string is only - * used when we don't have a time value */ - *details = g_strdup_printf (_("%s (%.0lf%%)"), - device_name, percentage); - *accessible_name = g_strdup (*details); - *short_details = g_strdup_printf (_("(%.0lf%%)"), - percentage); - } - else - { - *details = g_strdup_printf (_("%s (not present)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (_("(not present)")); - } - } -} - /* ensure that the entry is using self's accessible description */ static void refresh_entry_accessible_desc (IndicatorPower * self, IndicatorObjectEntry * entry) @@ -444,23 +260,17 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) GtkWidget *details_label; GtkWidget *grid; GIcon *device_gicon; - const gchar *device_name; gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; AtkObject *atk_object; - const time_t time = indicator_power_device_get_time (device); - const UpDeviceState state = indicator_power_device_get_state (device); - const gdouble percentage = indicator_power_device_get_percentage (device); /* Process the data */ device_gicon = indicator_power_device_get_gicon (device); icon = gtk_image_new_from_gicon (device_gicon, GTK_ICON_SIZE_SMALL_TOOLBAR); g_clear_object (&device_gicon); - device_name = device_kind_to_localised_string (kind); - - build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); + indicator_power_device_get_time_details (device, &short_details, &details, &accessible_name); /* Create menu item */ item = gtk_image_menu_item_new (); @@ -634,12 +444,7 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; - const gchar *device_name; IndicatorPowerPrivate * priv = self->priv; - const time_t time = indicator_power_device_get_time (device); - const UpDeviceKind kind = indicator_power_device_get_kind (device); - const UpDeviceState state = indicator_power_device_get_state (device); - const gdouble percentage = indicator_power_device_get_percentage (device); /* set icon */ device_gicon = indicator_power_device_get_gicon (device); @@ -649,10 +454,10 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) /* get the device name */ - device_name = device_kind_to_localised_string (kind); + //device_name = device_kind_to_localised_string (kind); /* get the description */ - build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); + indicator_power_device_get_time_details (device, &short_details, &details, &accessible_name); gtk_label_set_label (GTK_LABEL (priv->label), short_details); -- cgit v1.2.3 From 9a753ec3cfbf64ec11ea2bccbaa5c511177a69b3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 14:07:14 -0500 Subject: in indicator_power_device_get_time_details(), limit the scope of variables short_timestring and detailed_timestring --- src/device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index dc8db3a..8f040b2 100644 --- a/src/device.c +++ b/src/device.c @@ -520,11 +520,11 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, const gdouble percentage = indicator_power_device_get_percentage (device); const gchar * device_name = device_kind_to_localised_string (indicator_power_device_get_kind(device)); - gchar *short_timestring = NULL; - gchar *detailed_timestring = NULL; - if (time > 0) { + gchar *short_timestring = NULL; + gchar *detailed_timestring = NULL; + get_timestring (time, &short_timestring, &detailed_timestring); -- cgit v1.2.3 From 53d9c7e23ebe4a031af88a71a694643b02598b6e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:20:02 -0500 Subject: In indicator_power_device_get_time_details(), remove an unlikely branch that could result in time/detail strings not being set. --- src/device.c | 72 +++++++++++++++++++++++++----------------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 8f040b2..75a4139 100644 --- a/src/device.c +++ b/src/device.c @@ -516,7 +516,7 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, gchar ** accessible_name) { const time_t time = indicator_power_device_get_time (device); - const UpDeviceState state = indicator_power_device_get_state (device); + const UpDeviceState state = indicator_power_device_get_state (device); const gdouble percentage = indicator_power_device_get_percentage (device); const gchar * device_name = device_kind_to_localised_string (indicator_power_device_get_kind(device)); @@ -532,58 +532,46 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, if (state == UP_DEVICE_STATE_CHARGING) { /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), - device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s to charge)"), - device_name, short_timestring); + *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, detailed_timestring, percentage); + *details = g_strdup_printf (_("%s (%s to charge)"), device_name, short_timestring); *short_details = g_strdup_printf ("(%s)", short_timestring); } - else if (state == UP_DEVICE_STATE_DISCHARGING) + else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time > (60*60*12))) { + *accessible_name = g_strdup_printf (_("%s"), device_name); + *details = g_strdup_printf (_("%s"), device_name); + *short_details = g_strdup_printf ("%s", short_timestring); + } + else + { + /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ + *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage); + *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring); *short_details = g_strdup_printf ("%s", short_timestring); - - if (time > 43200) /* 12 hours */ - { - *accessible_name = g_strdup_printf (_("%s"), device_name); - *details = g_strdup_printf (_("%s"), device_name); - } - else - { - /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ - *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), - device_name, detailed_timestring, percentage); - *details = g_strdup_printf (_("%s (%s left)"), - device_name, short_timestring); - } } g_free (short_timestring); g_free (detailed_timestring); } + else if (state == UP_DEVICE_STATE_FULLY_CHARGED) + { + *details = g_strdup_printf (_("%s (charged)"), device_name); + *accessible_name = g_strdup (*details); + *short_details = g_strdup (""); + } + else if (percentage > 0) + { + /* TRANSLATORS: %2 is a percentage value. Note: this string is only + * used when we don't have a time value */ + *details = g_strdup_printf (_("%s (%.0lf%%)"), device_name, percentage); + *accessible_name = g_strdup (*details); + *short_details = g_strdup_printf (_("(%.0lf%%)"), percentage); + } else { - if (state == UP_DEVICE_STATE_FULLY_CHARGED) - { - *details = g_strdup_printf (_("%s (charged)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (""); - } - else if (percentage > 0) - { - /* TRANSLATORS: %2 is a percentage value. Note: this string is only - * used when we don't have a time value */ - *details = g_strdup_printf (_("%s (%.0lf%%)"), - device_name, percentage); - *accessible_name = g_strdup (*details); - *short_details = g_strdup_printf (_("(%.0lf%%)"), - percentage); - } - else - { - *details = g_strdup_printf (_("%s (not present)"), device_name); - *accessible_name = g_strdup (*details); - *short_details = g_strdup (_("(not present)")); - } + *details = g_strdup_printf (_("%s (not present)"), device_name); + *accessible_name = g_strdup (*details); + *short_details = g_strdup (_("(not present)")); } } -- cgit v1.2.3 From 9937a075bd77b36f1e8cb7cc9182139b95d66048 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:37:05 -0500 Subject: in indicator_power_device_get_time_details(), don't list 'not present' for AC Adapters that have no % and no time estimate --- src/device.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 75a4139..ae886cc 100644 --- a/src/device.c +++ b/src/device.c @@ -518,7 +518,8 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, const time_t time = indicator_power_device_get_time (device); const UpDeviceState state = indicator_power_device_get_state (device); const gdouble percentage = indicator_power_device_get_percentage (device); - const gchar * device_name = device_kind_to_localised_string (indicator_power_device_get_kind(device)); + const UpDeviceKind kind = indicator_power_device_get_kind (device); + const gchar * device_name = device_kind_to_localised_string (kind); if (time > 0) { @@ -567,6 +568,12 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, *accessible_name = g_strdup (*details); *short_details = g_strdup_printf (_("(%.0lf%%)"), percentage); } + else if (kind == UP_DEVICE_KIND_LINE_POWER) + { + *details = g_strdup (device_name); + *accessible_name = g_strdup (device_name); + *short_details = g_strdup (device_name); + } else { *details = g_strdup_printf (_("%s (not present)"), device_name); -- cgit v1.2.3 From 7c3ac190e4542ddafc475375855b63142bef8e5c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:37:24 -0500 Subject: capitalize the second word in 'AC Adapter' --- src/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index ae886cc..8752cb1 100644 --- a/src/device.c +++ b/src/device.c @@ -459,7 +459,7 @@ device_kind_to_localised_string (UpDeviceKind kind) switch (kind) { case UP_DEVICE_KIND_LINE_POWER: /* TRANSLATORS: system power cord */ - text = _("AC adapter"); + text = _("AC Adapter"); break; case UP_DEVICE_KIND_BATTERY: /* TRANSLATORS: laptop primary battery */ -- cgit v1.2.3 From 4c433bf6980f43c5fca5d7c86b167f50cc0295fd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:39:10 -0500 Subject: In indicator_power_device_get_time_details(), use g_strdup(foo) instead of g_strdup_printf("%s",foo) --- src/device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 8752cb1..ac03009 100644 --- a/src/device.c +++ b/src/device.c @@ -541,14 +541,14 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, { *accessible_name = g_strdup_printf (_("%s"), device_name); *details = g_strdup_printf (_("%s"), device_name); - *short_details = g_strdup_printf ("%s", short_timestring); + *short_details = g_strdup (short_timestring); } else { /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */ *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage); *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring); - *short_details = g_strdup_printf ("%s", short_timestring); + *short_details = g_strdup (short_timestring); } g_free (short_timestring); -- cgit v1.2.3 From 5b17593fb388ab9febc2e4bc26d8054a34dd58c0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:43:40 -0500 Subject: Fix edge case in indicator_power_device_get_icon_names() that returned a bad value if the caller passed in a NULL pointer as a device. Added regression test. --- src/device.c | 2 +- tests/test-device.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index ac03009..b163682 100644 --- a/src/device.c +++ b/src/device.c @@ -307,7 +307,7 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) const gchar *index_str; /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); /* LCOV_EXCL_STOP */ gdouble percentage = indicator_power_device_get_percentage (device); diff --git a/tests/test-device.cc b/tests/test-device.cc index 1233133..525eee0 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -224,6 +224,9 @@ TEST_F(DeviceTest, IconNames) IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); GObject * o = G_OBJECT(device); + /* bad arguments */ + ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL); + /* power */ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, NULL); -- cgit v1.2.3 From 0b46d0852fa764f5a9cc5d7a71c279ed56c1401d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:57:03 -0500 Subject: In indicator_power_device_get_time_details(), add a test for non-devices being passed in. --- src/device.c | 9 +++++++++ tests/test-device.cc | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index b163682..84af69c 100644 --- a/src/device.c +++ b/src/device.c @@ -515,6 +515,15 @@ indicator_power_device_get_time_details (const IndicatorPowerDevice * device, gchar ** details, gchar ** accessible_name) { + if (!INDICATOR_IS_POWER_DEVICE(device)) + { + *short_details = NULL; + *details = NULL; + *accessible_name = NULL; + g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device); + return; + } + const time_t time = indicator_power_device_get_time (device); const UpDeviceState state = indicator_power_device_get_state (device); const gdouble percentage = indicator_power_device_get_percentage (device); diff --git a/tests/test-device.cc b/tests/test-device.cc index 525eee0..5d68880 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -388,8 +388,20 @@ TEST_F(DeviceTest, Labels) char * real_lang = g_strdup(g_getenv ("LANG")); g_setenv ("LANG", "en_US.UTF-8", TRUE); + /* bad args: NULL device */ + check_strings (NULL, NULL, NULL, NULL); + + /* bad args: a GObject that isn't a device */ + GObject * o = G_OBJECT(g_cancellable_new()); + check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL); + g_object_unref (o); + + /** + *** + **/ + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); - GObject * o = G_OBJECT(device); + o = G_OBJECT(device); /* charging */ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, -- cgit v1.2.3 From 52d1979d7332427054251501b438fdf61e3a8dd8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 14:19:27 -0500 Subject: copyediting: fix tab damage in device.[ch] --- src/device.c | 350 ++++++++++++++++++++++++++++++----------------------------- src/device.h | 6 +- 2 files changed, 179 insertions(+), 177 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 84af69c..9e01b3d 100644 --- a/src/device.c +++ b/src/device.c @@ -31,11 +31,11 @@ License along with this library. If not, see struct _IndicatorPowerDevicePrivate { - UpDeviceKind kind; - UpDeviceState state; - gchar * object_path; - gdouble percentage; - time_t time; + UpDeviceKind kind; + UpDeviceState state; + gchar * object_path; + gdouble percentage; + time_t time; }; #define INDICATOR_POWER_DEVICE_GET_PRIVATE(o) (INDICATOR_POWER_DEVICE(o)->priv) @@ -43,13 +43,13 @@ struct _IndicatorPowerDevicePrivate /* Properties */ /* Enum for the properties so that they can be quickly found and looked up. */ enum { - PROP_0, - PROP_KIND, - PROP_STATE, - PROP_OBJECT_PATH, - PROP_ICON, - PROP_PERCENTAGE, - PROP_TIME + PROP_0, + PROP_KIND, + PROP_STATE, + PROP_OBJECT_PATH, + PROP_ICON, + PROP_PERCENTAGE, + PROP_TIME }; /* GObject stuff */ @@ -67,72 +67,71 @@ G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) { - GParamSpec * pspec; - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - g_type_class_add_private (klass, sizeof (IndicatorPowerDevicePrivate)); - - object_class->dispose = indicator_power_device_dispose; - object_class->finalize = indicator_power_device_finalize; - object_class->set_property = set_property; - object_class->get_property = get_property; - - pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, "kind", "The device's UpDeviceKind", - UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, UP_DEVICE_KIND_UNKNOWN, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_KIND, pspec); - - pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState", - UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, UP_DEVICE_STATE_UNKNOWN, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_STATE, pspec); - - pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, "object path", "The device's DBus object path", NULL, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec); - - pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged", - 0.0, 100.0, 0.0, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec); - - pspec = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, "time", "time left", - 0, G_MAXUINT64, 0, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_TIME, pspec); + GParamSpec * pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorPowerDevicePrivate)); + + object_class->dispose = indicator_power_device_dispose; + object_class->finalize = indicator_power_device_finalize; + object_class->set_property = set_property; + object_class->get_property = get_property; + + pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, "kind", "The device's UpDeviceKind", + UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, UP_DEVICE_KIND_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_KIND, pspec); + + pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState", + UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, UP_DEVICE_STATE_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_STATE, pspec); + + pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, "object path", "The device's DBus object path", NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec); + + pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged", + 0.0, 100.0, 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec); + + pspec = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, "time", "time left", + 0, G_MAXUINT64, 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_TIME, pspec); } /* Initialize an instance */ static void indicator_power_device_init (IndicatorPowerDevice *self) { - IndicatorPowerDevicePrivate * priv; + IndicatorPowerDevicePrivate * priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_DEVICE_TYPE, + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDevicePrivate); - priv->kind = UP_DEVICE_KIND_UNKNOWN; - priv->state = UP_DEVICE_STATE_UNKNOWN; - priv->object_path = NULL; - priv->percentage = 0.0; - priv->time = 0; + priv->kind = UP_DEVICE_KIND_UNKNOWN; + priv->state = UP_DEVICE_STATE_UNKNOWN; + priv->object_path = NULL; + priv->percentage = 0.0; + priv->time = 0; - self->priv = priv; + self->priv = priv; } static void indicator_power_device_dispose (GObject *object) { - G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object); + G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object); } static void indicator_power_device_finalize (GObject *object) { - IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object); - IndicatorPowerDevicePrivate * priv = self->priv; + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object); + IndicatorPowerDevicePrivate * priv = self->priv; - g_clear_pointer (&priv->object_path, g_free); + g_clear_pointer (&priv->object_path, g_free); } /*** @@ -142,62 +141,62 @@ indicator_power_device_finalize (GObject *object) static void get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) { - IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); - IndicatorPowerDevicePrivate * priv = self->priv; - - switch (prop_id) - { - case PROP_KIND: - g_value_set_int (value, priv->kind); - break; - - case PROP_STATE: - g_value_set_int (value, priv->state); - break; - - case PROP_OBJECT_PATH: - g_value_set_string (value, priv->object_path); - break; - - case PROP_PERCENTAGE: - g_value_set_double (value, priv->percentage); - break; - - case PROP_TIME: - g_value_set_uint64 (value, priv->time); - break; - } + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); + IndicatorPowerDevicePrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_KIND: + g_value_set_int (value, priv->kind); + break; + + case PROP_STATE: + g_value_set_int (value, priv->state); + break; + + case PROP_OBJECT_PATH: + g_value_set_string (value, priv->object_path); + break; + + case PROP_PERCENTAGE: + g_value_set_double (value, priv->percentage); + break; + + case PROP_TIME: + g_value_set_uint64 (value, priv->time); + break; + } } static void set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) { - IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); - IndicatorPowerDevicePrivate * priv = self->priv; - - switch (prop_id) - { - case PROP_KIND: - priv->kind = g_value_get_int (value); - break; - - case PROP_STATE: - priv->state = g_value_get_int (value); - break; - - case PROP_OBJECT_PATH: - g_free (priv->object_path); - priv->object_path = g_value_dup_string (value); - break; - - case PROP_PERCENTAGE: - priv->percentage = g_value_get_double (value); - break; - - case PROP_TIME: - priv->time = g_value_get_uint64(value); - break; - } + IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o); + IndicatorPowerDevicePrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_KIND: + priv->kind = g_value_get_int (value); + break; + + case PROP_STATE: + priv->state = g_value_get_int (value); + break; + + case PROP_OBJECT_PATH: + g_free (priv->object_path); + priv->object_path = g_value_dup_string (value); + break; + + case PROP_PERCENTAGE: + priv->percentage = g_value_get_double (value); + break; + + case PROP_TIME: + priv->time = g_value_get_uint64(value); + break; + } } /*** @@ -207,51 +206,51 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device) { - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); - /* LCOV_EXCL_STOP */ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN); + /* LCOV_EXCL_STOP */ - return device->priv->kind; + return device->priv->kind; } UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device) { - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); - /* LCOV_EXCL_STOP */ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN); + /* LCOV_EXCL_STOP */ - return device->priv->state; + return device->priv->state; } const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device) { - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); - /* LCOV_EXCL_STOP */ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); + /* LCOV_EXCL_STOP */ - return device->priv->object_path; + return device->priv->object_path; } gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device) { - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), 0.0); - /* LCOV_EXCL_STOP */ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), 0.0); + /* LCOV_EXCL_STOP */ - return device->priv->percentage; + return device->priv->percentage; } time_t indicator_power_device_get_time (const IndicatorPowerDevice * device) { - /* LCOV_EXCL_START */ - g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), (time_t)0); - /* LCOV_EXCL_STOP */ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), (time_t)0); + /* LCOV_EXCL_STOP */ - return device->priv->time; + return device->priv->time; } /*** @@ -263,46 +262,49 @@ indicator_power_device_get_time (const IndicatorPowerDevice * device) static const gchar * gpm_upower_get_device_icon_suffix (gdouble percentage) { - if (percentage < 10) return "caution"; - if (percentage < 30) return "low"; - if (percentage < 60) return "good"; - return "full"; + if (percentage < 10) return "caution"; + if (percentage < 30) return "low"; + if (percentage < 60) return "good"; + return "full"; } /* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ static const gchar * gpm_upower_get_device_icon_index (gdouble percentage) { - if (percentage < 10) return "000"; - if (percentage < 30) return "020"; - if (percentage < 50) return "040"; - if (percentage < 70) return "060"; - if (percentage < 90) return "080"; - return "100"; + if (percentage < 10) return "000"; + if (percentage < 30) return "020"; + if (percentage < 50) return "040"; + if (percentage < 70) return "060"; + if (percentage < 90) return "080"; + return "100"; } /** indicator_power_device_get_icon_names: - @device: #IndicatorPowerDevice to generate the icon names from + @device: #IndicatorPowerDevice from which to generate the icon names Based on GSD's power plugin, (c) Richard Hughes and licensed GPL >= 2. It differs in these ways: - (1) all charging batteries use the same icon regardless of progress: + 1. All charging batteries use the same icon regardless of progress. - (2) discharging batteries are keyed off of time left, rather than - percentage left, s.t. <= 30 minutes remaining gives the 'caution' icon. + 2. For discharging batteries, we decide whether or not to use the 'caution' + icon based on whether or not we have <= 30 minutes remaining, rather than + looking at the battery's percentage left. + See also #indicator_power_device_get_gicon. + Return value: (array zero-terminated=1) (transfer full): - a GStrv of icon names suitable for passing to g_themed_icon_new_from_names(). + A GStrv of icon names suitable for passing to g_themed_icon_new_from_names(). Free with g_strfreev() when done. */ GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) { - char ** ret = NULL; + gchar ** ret = NULL; const gchar *suffix_str; const gchar *index_str; @@ -313,7 +315,7 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) gdouble percentage = indicator_power_device_get_percentage (device); const UpDeviceKind kind = indicator_power_device_get_kind (device); const UpDeviceState state = indicator_power_device_get_state (device); - const char * kind_str = kind_str = up_device_kind_to_string (kind); + const gchar * kind_str = kind_str = up_device_kind_to_string (kind); /* get correct icon prefix */ GString * filename = g_string_new (NULL); @@ -602,37 +604,37 @@ indicator_power_device_new (const gchar * object_path, UpDeviceState state, time_t timestamp) { - GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE, - INDICATOR_POWER_DEVICE_KIND, kind, - INDICATOR_POWER_DEVICE_STATE, state, - INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, - INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, - INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp, - NULL); - return INDICATOR_POWER_DEVICE(o); + GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE, + INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, state, + INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, + INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, + INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp, + NULL); + return INDICATOR_POWER_DEVICE(o); } IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant * v) { - UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; - UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; - const gchar * icon = NULL; - const gchar * object_path = NULL; - gdouble percentage = 0; - guint64 time = 0; - - g_variant_get (v, "(&su&sdut)", - &object_path, - &kind, - &icon, - &percentage, - &state, - &time); - - return indicator_power_device_new (object_path, - kind, - percentage, - state, - time); + UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; + UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; + const gchar * icon = NULL; + const gchar * object_path = NULL; + gdouble percentage = 0; + guint64 time = 0; + + g_variant_get (v, "(&su&sdut)", + &object_path, + &kind, + &icon, + &percentage, + &state, + &time); + + return indicator_power_device_new (object_path, + kind, + percentage, + state, + time); } diff --git a/src/device.h b/src/device.h index 566b196..c7ba638 100644 --- a/src/device.h +++ b/src/device.h @@ -52,7 +52,7 @@ typedef struct _IndicatorPowerDevicePrivate IndicatorPowerDevicePrivate; */ struct _IndicatorPowerDeviceClass { - GObjectClass parent_class; + GObjectClass parent_class; }; /** @@ -62,8 +62,8 @@ struct _IndicatorPowerDeviceClass */ struct _IndicatorPowerDevice { - GObject parent; - IndicatorPowerDevicePrivate * priv; + GObject parent; + IndicatorPowerDevicePrivate * priv; }; /*** -- cgit v1.2.3 From da3c570125d63198a2fa7c60998db162fd2e0fcc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:02:55 -0500 Subject: in IndicatorPowerDevice's class init function, use g_object_class_install_properties() instead of installing each property separately. --- src/device.c | 65 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 9e01b3d..8ea440c 100644 --- a/src/device.c +++ b/src/device.c @@ -47,11 +47,13 @@ enum { PROP_KIND, PROP_STATE, PROP_OBJECT_PATH, - PROP_ICON, PROP_PERCENTAGE, - PROP_TIME + PROP_TIME, + N_PROPERTIES }; +static GParamSpec * properties[N_PROPERTIES]; + /* GObject stuff */ static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass); static void indicator_power_device_init (IndicatorPowerDevice *self); @@ -67,7 +69,6 @@ G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT); static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) { - GParamSpec * pspec; GObjectClass *object_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (IndicatorPowerDevicePrivate)); @@ -77,29 +78,41 @@ indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) object_class->set_property = set_property; object_class->get_property = get_property; - pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, "kind", "The device's UpDeviceKind", - UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, UP_DEVICE_KIND_UNKNOWN, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_KIND, pspec); - - pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState", - UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, UP_DEVICE_STATE_UNKNOWN, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_STATE, pspec); - - pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, "object path", "The device's DBus object path", NULL, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec); - - pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged", - 0.0, 100.0, 0.0, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec); - - pspec = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, "time", "time left", - 0, G_MAXUINT64, 0, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_TIME, pspec); + properties[PROP_KIND] = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, + "kind", + "The device's UpDeviceKind", + UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, + UP_DEVICE_KIND_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + properties[PROP_STATE] = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, + "state", + "The device's UpDeviceState", + UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, + UP_DEVICE_STATE_UNKNOWN, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + properties[PROP_OBJECT_PATH] = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, + "object path", + "The device's DBus object path", + NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + properties[PROP_PERCENTAGE] = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, + "percentage", + "percent charged", + 0.0, 100.0, + 0.0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + properties[PROP_TIME] = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, + "time", + "time left", + 0, G_MAXUINT64, + 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, N_PROPERTIES, properties); } /* Initialize an instance */ -- cgit v1.2.3 From de56d59d57a585d80618873531f05f13ea08c8a2 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:04:05 -0500 Subject: IndicatorObjectDevice's finalize() function needs to chain up to the parent class. --- src/device.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 8ea440c..5919aa8 100644 --- a/src/device.c +++ b/src/device.c @@ -145,6 +145,8 @@ indicator_power_device_finalize (GObject *object) IndicatorPowerDevicePrivate * priv = self->priv; g_clear_pointer (&priv->object_path, g_free); + + G_OBJECT_CLASS (indicator_power_device_parent_class)->finalize (object); } /*** -- cgit v1.2.3 From 50fb08d451719f049daa53d4a9649223a980e87b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:06:59 -0500 Subject: add G_OBJECT_WARN_INVALID_PROPERTY_ID for the 'default' switch case in Device's get/set property methods --- src/device.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 5919aa8..1c818cc 100644 --- a/src/device.c +++ b/src/device.c @@ -180,6 +180,10 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) case PROP_TIME: g_value_set_uint64 (value, priv->time); break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec); + break; } } @@ -211,6 +215,10 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp case PROP_TIME: priv->time = g_value_get_uint64(value); break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec); + break; } } -- cgit v1.2.3 From 73bef489d16b6be22435247e23bf1ed80f4e99f1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:19:26 -0500 Subject: In indicator_power_device_new_from_variant(), check whether the input variant has the correct type before using it. --- src/device.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 1c818cc..ce24520 100644 --- a/src/device.c +++ b/src/device.c @@ -640,6 +640,10 @@ indicator_power_device_new (const gchar * object_path, IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant * v) { + g_return_val_if_fail (v != NULL, NULL); + g_return_val_if_fail (g_variant_type_is_tuple(g_variant_get_type(v)), NULL); + g_return_val_if_fail (g_variant_n_children(v) == 6, NULL); + UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; const gchar * icon = NULL; -- cgit v1.2.3 From 0acce2391386f3a4cbd2c548a67db099a1d2df5c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:21:35 -0500 Subject: For GTK-Doc, reference functions with function_name() instead of #function_name --- src/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index ce24520..e9e228a 100644 --- a/src/device.c +++ b/src/device.c @@ -318,7 +318,7 @@ gpm_upower_get_device_icon_index (gdouble percentage) looking at the battery's percentage left. - See also #indicator_power_device_get_gicon. + See also indicator_power_device_get_gicon(). Return value: (array zero-terminated=1) (transfer full): A GStrv of icon names suitable for passing to g_themed_icon_new_from_names(). -- cgit v1.2.3 From 9091bba8ac0ac70107bfe27276b1d6e9e3471fc8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 16:00:06 -0500 Subject: reimplement indicator_power_device_get_icon_names() since our CA is incompatible with reusing code from GSD. --- src/device.c | 153 ++++++++++++++++++++++++++------------------------- tests/test-device.cc | 34 ++++++------ 2 files changed, 94 insertions(+), 93 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index e9e228a..c79e145 100644 --- a/src/device.c +++ b/src/device.c @@ -281,34 +281,31 @@ indicator_power_device_get_time (const IndicatorPowerDevice * device) **** ***/ -/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ static const gchar * -gpm_upower_get_device_icon_suffix (gdouble percentage) +get_device_icon_suffix (gdouble percentage) { - if (percentage < 10) return "caution"; - if (percentage < 30) return "low"; - if (percentage < 60) return "good"; - return "full"; + if (percentage >= 60) return "full"; + if (percentage >= 30) return "good"; + if (percentage >= 10) return "low"; + return "caution"; } -/* taken from GSD's power plugin, (c) Richard Hughes and licensed GPL >=2 */ static const gchar * -gpm_upower_get_device_icon_index (gdouble percentage) +get_device_icon_index (gdouble percentage) { - if (percentage < 10) return "000"; - if (percentage < 30) return "020"; - if (percentage < 50) return "040"; - if (percentage < 70) return "060"; - if (percentage < 90) return "080"; - return "100"; + if (percentage >= 90) return "100"; + if (percentage >= 70) return "080"; + if (percentage >= 50) return "060"; + if (percentage >= 30) return "040"; + if (percentage >= 10) return "020"; + return "000"; } /** indicator_power_device_get_icon_names: @device: #IndicatorPowerDevice from which to generate the icon names - Based on GSD's power plugin, (c) Richard Hughes and licensed GPL >= 2. - It differs in these ways: + This function's logic differs from GSD's power plugin in some ways: 1. All charging batteries use the same icon regardless of progress. @@ -327,7 +324,6 @@ gpm_upower_get_device_icon_index (gdouble percentage) GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) { - gchar ** ret = NULL; const gchar *suffix_str; const gchar *index_str; @@ -340,68 +336,73 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device) const UpDeviceState state = indicator_power_device_get_state (device); const gchar * kind_str = kind_str = up_device_kind_to_string (kind); - /* get correct icon prefix */ - GString * filename = g_string_new (NULL); - - /* get the icon from some simple rules */ - if (kind == UP_DEVICE_KIND_LINE_POWER) { - g_string_append (filename, "ac-adapter-symbolic;"); - g_string_append (filename, "ac-adapter;"); - } else if (kind == UP_DEVICE_KIND_MONITOR) { - g_string_append (filename, "gpm-monitor-symbolic;"); - g_string_append (filename, "gpm-monitor;"); - } else switch (state) { - case UP_DEVICE_STATE_EMPTY: - g_string_append (filename, "battery-empty-symbolic;"); - g_string_append_printf (filename, "gpm-%s-empty;", kind_str); - g_string_append_printf (filename, "gpm-%s-000;", kind_str); - g_string_append (filename, "battery-empty;"); - break; - case UP_DEVICE_STATE_FULLY_CHARGED: - g_string_append (filename, "battery-full-charged-symbolic;"); - g_string_append (filename, "battery-full-charging-symbolic;"); - g_string_append_printf (filename, "gpm-%s-full;", kind_str); - g_string_append_printf (filename, "gpm-%s-100;", kind_str); - g_string_append (filename, "battery-full-charged;"); - g_string_append (filename, "battery-full-charging;"); - break; - case UP_DEVICE_STATE_CHARGING: - case UP_DEVICE_STATE_PENDING_CHARGE: - /* When charging, always use the same icon regardless of percentage. - */ - percentage = 0; - - suffix_str = gpm_upower_get_device_icon_suffix (percentage); - index_str = gpm_upower_get_device_icon_index (percentage); - g_string_append_printf (filename, "battery-%s-charging-symbolic;", suffix_str); - g_string_append_printf (filename, "gpm-%s-%s-charging;", kind_str, index_str); - g_string_append_printf (filename, "battery-%s-charging;", suffix_str); - break; - case UP_DEVICE_STATE_DISCHARGING: - case UP_DEVICE_STATE_PENDING_DISCHARGE: { - /* Don't show the caution/red icons unless we have <=30 min left. - - Themes use the caution color when the percentage is 0% or 20%, - so if we have >30 min left, use 30% as the icon's percentage floor */ - if (indicator_power_device_get_time (device) > (30*60)) - percentage = MAX(percentage, 30); - - suffix_str = gpm_upower_get_device_icon_suffix (percentage); - index_str = gpm_upower_get_device_icon_index (percentage); - g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str); - g_string_append_printf (filename, "gpm-%s-%s;", kind_str, index_str); - g_string_append_printf (filename, "battery-%s;", suffix_str); - break; + GPtrArray * names = g_ptr_array_new (); + + if (kind == UP_DEVICE_KIND_LINE_POWER) + { + g_ptr_array_add (names, g_strdup("ac-adapter-symbolic")); + g_ptr_array_add (names, g_strdup("ac-adapter")); } - default: - g_string_append (filename, "battery-missing-symbolic;"); - g_string_append (filename, "gpm-battery-missing;"); - g_string_append (filename, "battery-missing;"); + else if (kind == UP_DEVICE_KIND_MONITOR) + { + g_ptr_array_add (names, g_strdup("gpm-monitor-symbolic")); + g_ptr_array_add (names, g_strdup("gpm-monitor")); + } + else switch (state) + { + case UP_DEVICE_STATE_EMPTY: + g_ptr_array_add (names, g_strdup("battery-empty-symbolic")); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-empty", kind_str)); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-000", kind_str)); + g_ptr_array_add (names, g_strdup("battery-empty")); + break; + + case UP_DEVICE_STATE_FULLY_CHARGED: + g_ptr_array_add (names, g_strdup("battery-full-charged-symbolic")); + g_ptr_array_add (names, g_strdup("battery-full-charging-symbolic")); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-full", kind_str)); + g_ptr_array_add (names, g_strdup_printf("gpm-%s-100", kind_str)); + g_ptr_array_add (names, g_strdup("battery-full-charged")); + g_ptr_array_add (names, g_strdup("battery-full-charging")); + break; + + case UP_DEVICE_STATE_CHARGING: + case UP_DEVICE_STATE_PENDING_CHARGE: + /* When charging, always use the same icon regardless of percentage. + */ + percentage = 0; + + suffix_str = get_device_icon_suffix (percentage); + index_str = get_device_icon_index (percentage); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging-symbolic", suffix_str)); + g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s-charging", kind_str, index_str)); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging", suffix_str)); + break; + + case UP_DEVICE_STATE_DISCHARGING: + case UP_DEVICE_STATE_PENDING_DISCHARGE: + /* Don't show the caution/red icons unless we have <=30 min left. + + Themes use the caution color when the percentage is 0% or 20%, + so if we have >30 min left, use 30% as the icon's percentage floor */ + if (indicator_power_device_get_time (device) > (30*60)) + percentage = MAX(percentage, 30); + + suffix_str = get_device_icon_suffix (percentage); + index_str = get_device_icon_index (percentage); + g_ptr_array_add (names, g_strdup_printf ("battery-%s-symbolic", suffix_str)); + g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s", kind_str, index_str)); + g_ptr_array_add (names, g_strdup_printf ("battery-%s", suffix_str)); + break; + + default: + g_ptr_array_add (names, g_strdup("battery-missing-symbolic")); + g_ptr_array_add (names, g_strdup("gpm-battery-missing")); + g_ptr_array_add (names, g_strdup("battery-missing")); } - ret = g_strsplit (filename->str, ";", -1); - g_string_free (filename, TRUE); - return ret; + g_ptr_array_add (names, NULL); /* terminates the strv */ + return (GStrv) g_ptr_array_free (names, FALSE); } /** diff --git a/tests/test-device.cc b/tests/test-device.cc index cc0d9c5..2c88ea1 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -231,13 +231,13 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, NULL); check_icon_names (device, "ac-adapter-symbolic;" - "ac-adapter;"); + "ac-adapter"); // monitor g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_MONITOR, NULL); check_icon_names (device, "gpm-monitor-symbolic;" - "gpm-monitor;"); + "gpm-monitor"); // empty battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -246,7 +246,7 @@ TEST_F(DeviceTest, IconNames) check_icon_names (device, "battery-empty-symbolic;" "gpm-battery-empty;" "gpm-battery-000;" - "battery-empty;"); + "battery-empty"); // charged battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -254,7 +254,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-charged-symbolic;battery-full-charging-symbolic;" "gpm-battery-full;gpm-battery-100;" - "battery-full-charged;battery-full-charging;"); + "battery-full-charged;battery-full-charging"); // charging battery, 95% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -263,7 +263,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 85% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -272,7 +272,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 50% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -281,7 +281,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 25% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -290,7 +290,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // charging battery, 5% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -299,7 +299,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-charging-symbolic;" "gpm-battery-000-charging;" - "battery-caution-charging;"); + "battery-caution-charging"); // discharging battery, 95% @@ -309,7 +309,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-symbolic;" "gpm-battery-100;" - "battery-full;"); + "battery-full"); // discharging battery, 85% g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -318,7 +318,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-full-symbolic;" "gpm-battery-080;" - "battery-full;"); + "battery-full"); // discharging battery, 50% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -328,7 +328,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-060;" - "battery-good;"); + "battery-good"); // discharging battery, 25% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -338,7 +338,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-040;" - "battery-good;"); + "battery-good"); // discharging battery, 25% -- 15 minutes left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -348,7 +348,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-low-symbolic;" "gpm-battery-020;" - "battery-low;"); + "battery-low"); // discharging battery, 5% -- 1 hour left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -358,7 +358,7 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-good-symbolic;" "gpm-battery-040;" - "battery-good;"); + "battery-good"); // discharging battery, 5% -- 15 minutes left g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, @@ -368,14 +368,14 @@ TEST_F(DeviceTest, IconNames) NULL); check_icon_names (device, "battery-caution-symbolic;" "gpm-battery-000;" - "battery-caution;"); + "battery-caution"); // state unknown g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, NULL); check_icon_names (device, "battery-missing-symbolic;" "gpm-battery-missing;" - "battery-missing;"); + "battery-missing"); // cleanup g_object_unref(o); -- cgit v1.2.3 From 8b31b764a1f13c4f1651790157712794c11ea8d4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 7 Jun 2012 08:57:15 -0500 Subject: improve the variant sanity tests in indicator_power_device_new_from_variant() --- src/device.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index c79e145..c22aae8 100644 --- a/src/device.c +++ b/src/device.c @@ -641,9 +641,7 @@ indicator_power_device_new (const gchar * object_path, IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant * v) { - g_return_val_if_fail (v != NULL, NULL); - g_return_val_if_fail (g_variant_type_is_tuple(g_variant_get_type(v)), NULL); - g_return_val_if_fail (g_variant_n_children(v) == 6, NULL); + g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdut)")), NULL); UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; -- cgit v1.2.3