From 122e56d9e1b4b8d2f2b7bf3dd9daf5577d6b2699 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 May 2012 10:42:13 -0500 Subject: move libpower's automake rules into src/ --- Makefile.am | 28 +--------------------------- configure.ac | 1 + src/Makefile.am | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 src/Makefile.am diff --git a/Makefile.am b/Makefile.am index 25cbee5..54ec6a2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,30 +1,4 @@ -ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} - -SUBDIRS = \ - po \ - data - -################### -# Indicator Stuff -################### - -powerlibdir = $(INDICATORDIR) -powerlib_LTLIBRARIES = libpower.la -libpower_la_SOURCES = \ - src/indicator-power.c -libpower_la_CFLAGS = \ - $(UPOWER_CFLAGS) \ - $(INDICATOR_CFLAGS) \ - $(COVERAGE_CFLAGS) \ - -Wall -Werror \ - -DG_LOG_DOMAIN=\"Indicator-Power\" -libpower_la_LIBADD = \ - $(UPOWER_LIBS) \ - $(INDICATOR_LIBS) -libpower_la_LDFLAGS = \ - $(COVERAGE_LDFLAGS) \ - -module \ - -avoid-version +SUBDIRS = po data src ############################################################ diff --git a/configure.ac b/configure.ac index fc287a2..433c7be 100644 --- a/configure.ac +++ b/configure.ac @@ -119,6 +119,7 @@ AC_CONFIG_FILES([ Makefile po/Makefile.in data/Makefile +src/Makefile ]) AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..e0211a0 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,28 @@ +ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} + +################### +# Indicator Stuff +################### + +powerlibdir = $(INDICATORDIR) +powerlib_LTLIBRARIES = libpower.la + +libpower_la_SOURCES = \ + indicator-power.c + +libpower_la_CFLAGS = \ + $(UPOWER_CFLAGS) \ + $(INDICATOR_CFLAGS) \ + $(COVERAGE_CFLAGS) \ + -Wall -Werror \ + -DG_LOG_DOMAIN=\"Indicator-Power\" + +libpower_la_LIBADD = \ + $(UPOWER_LIBS) \ + $(INDICATOR_LIBS) + +libpower_la_LDFLAGS = \ + $(COVERAGE_LDFLAGS) \ + -module \ + -avoid-version + -- cgit v1.2.3 From 71a3e5394346f529ae74e7174dd4156c0229d190 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 May 2012 22:11:51 -0500 Subject: =?UTF-8?q?replace=20'Power=20Settings...'=20with=20'Power=20Setti?= =?UTF-8?q?ngs=E2=80=A6'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/indicator-power.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 1b3794e..7cdaeb2 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -677,7 +677,7 @@ build_menu (IndicatorPower *self) gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); /* preferences */ - item = gtk_image_menu_item_new_with_label (_("Power Settings...")); + item = gtk_image_menu_item_new_with_label (_("Power Settings…")); image = gtk_image_new_from_icon_name (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); g_signal_connect (G_OBJECT (item), "activate", -- cgit v1.2.3 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 --- Makefile.am | 7 + configure.ac | 26 +++- m4/gtest.m4 | 63 +++++++++ src/Makefile.am | 8 +- src/device.c | 330 ++++++++++++++++++++++++++++++++++++++++++++++ src/device.h | 100 ++++++++++++++ tests/Makefile.am | 38 ++++++ tests/Makefile.am.strings | 38 ++++++ tests/test-device.cc | 99 ++++++++++++++ 9 files changed, 706 insertions(+), 3 deletions(-) create mode 100644 m4/gtest.m4 create mode 100644 src/device.c create mode 100644 src/device.h create mode 100644 tests/Makefile.am create mode 100644 tests/Makefile.am.strings create mode 100644 tests/test-device.cc diff --git a/Makefile.am b/Makefile.am index 54ec6a2..00e108d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,12 @@ SUBDIRS = po data src +if BUILD_TESTS +SUBDIRS += tests +# build src first +tests: src +endif + + ############################################################ dist_noinst_SCRIPTS = \ diff --git a/configure.ac b/configure.ac index 433c7be..876edf7 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,7 @@ AC_CONFIG_SRCDIR([configure.ac]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_AUX_DIR([build-aux]) -AM_INIT_AUTOMAKE([1.11 -Wall foreign subdir-objects dist-xz]) +AM_INIT_AUTOMAKE([1.11 -Wall foreign dist-xz]) AM_MAINTAINER_MODE([enable]) AM_SILENT_RULES([yes]) @@ -19,6 +19,7 @@ AM_SILENT_RULES([yes]) # Check for programs AC_PROG_CC AM_PROG_CC_C_O +AC_PROG_CXX # Initialize libtool LT_PREREQ([2.2.6]) @@ -47,6 +48,26 @@ PKG_CHECK_MODULES([GSD],[gnome-settings-daemon >= $GSD_REQUIRED_VERSION]) ########################### GLIB_GSETTINGS +########################### +# Google Test framework +########################### + +AC_ARG_ENABLE([tests], + [AS_HELP_STRING([--disable-tests], [Disable test scripts and tools (default=auto)])], + [enable_tests=${enableval}], + [enable_tests=auto]) +if test "x$enable_tests" != "xno"; then + m4_include([m4/gtest.m4]) + CHECK_GTEST + CHECK_XORG_GTEST + if test "x$enable_tests" = "xauto"; then + enable_tests=${have_gtest} + elif test "x$enable_tests" = "xyes" && test "x$have_gtest" != "xyes"; then + AC_MSG_ERROR([tests were requested but gtest is not installed.]) + fi +fi +AM_CONDITIONAL([BUILD_TESTS],[test "x$enable_tests" = "xyes"]) + ########################### # gcov coverage reporting ########################### @@ -120,6 +141,7 @@ Makefile po/Makefile.in data/Makefile src/Makefile +tests/Makefile ]) AC_OUTPUT @@ -133,5 +155,7 @@ Power Indicator Configuration: Prefix: $prefix Local Install: $with_localinstall + Unit Tests: $enable_tests gcov: $use_gcov + ]) diff --git a/m4/gtest.m4 b/m4/gtest.m4 new file mode 100644 index 0000000..2de334c --- /dev/null +++ b/m4/gtest.m4 @@ -0,0 +1,63 @@ +# Copyright (C) 2012 Canonical, Ltd. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice (including the next +# paragraph) shall be included in all copies or substantial portions of the +# Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# Checks whether the gtest source is available on the system. Allows for +# adjusting the include and source path. Sets have_gtest=yes if the source is +# present. Sets GTEST_CPPFLAGS and GTEST_SOURCE to the preprocessor flags and +# source location respectively. +AC_DEFUN([CHECK_GTEST], +[ + AC_ARG_WITH([gtest-include-path], + [AS_HELP_STRING([--with-gtest-include-path], + [location of the Google test headers])], + [GTEST_CPPFLAGS="-I$withval"]) + + AC_ARG_WITH([gtest-source-path], + [AS_HELP_STRING([--with-gtest-source-path], + [location of the Google test sources, defaults to /usr/src/gtest])], + [GTEST_SOURCE="$withval"], + [GTEST_SOURCE="/usr/src/gtest"]) + + GTEST_CPPFLAGS="$GTEST_CPPFLAGS -I$GTEST_SOURCE" + + AC_LANG_PUSH([C++]) + + tmp_CPPFLAGS="$CPPFLAGS" + CPPFLAGS="$CPPFLAGS $GTEST_CPPFLAGS" + + AC_CHECK_HEADER([gtest/gtest.h]) + + CPPFLAGS="$tmp_CPPFLAGS" + + AC_LANG_POP + + AC_CHECK_FILES([$GTEST_SOURCE/src/gtest-all.cc] + [$GTEST_SOURCE/src/gtest_main.cc], + [have_gtest_source=yes], + [have_gtest_source=no]) + + AS_IF([test "x$ac_cv_header_gtest_gtest_h" = xyes -a \ + "x$have_gtest_source" = xyes], + [have_gtest=yes] + [AC_SUBST(GTEST_CPPFLAGS)] + [AC_SUBST(GTEST_SOURCE)], + [have_gtest=no]) +]) # CHECK_GTEST diff --git a/src/Makefile.am b/src/Makefile.am index e0211a0..d14c270 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,12 +8,16 @@ powerlibdir = $(INDICATORDIR) powerlib_LTLIBRARIES = libpower.la libpower_la_SOURCES = \ + device.c \ indicator-power.c +noinst_HEADERS = \ + device.h + libpower_la_CFLAGS = \ $(UPOWER_CFLAGS) \ $(INDICATOR_CFLAGS) \ - $(COVERAGE_CFLAGS) \ + $(COVERAGE_CFLAGS) \ -Wall -Werror \ -DG_LOG_DOMAIN=\"Indicator-Power\" @@ -22,7 +26,7 @@ libpower_la_LIBADD = \ $(INDICATOR_LIBS) libpower_la_LDFLAGS = \ - $(COVERAGE_LDFLAGS) \ + $(COVERAGE_LDFLAGS) \ -module \ -avoid-version 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); +} diff --git a/src/device.h b/src/device.h new file mode 100644 index 0000000..e9de044 --- /dev/null +++ b/src/device.h @@ -0,0 +1,100 @@ +/* + +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 +. +*/ + +#ifndef __INDICATOR_POWER_DEVICE_H__ +#define __INDICATOR_POWER_DEVICE_H__ + +#include +#include + +G_BEGIN_DECLS + +#define INDICATOR_POWER_DEVICE_TYPE (indicator_power_device_get_type ()) +#define INDICATOR_POWER_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDevice)) +#define INDICATOR_POWER_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass)) +#define INDICATOR_IS_POWER_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_DEVICE_TYPE)) +#define INDICATOR_IS_POWER_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_DEVICE_TYPE)) +#define INDICATOR_POWER_DEVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass)) + +typedef struct _IndicatorPowerDevice IndicatorPowerDevice; +typedef struct _IndicatorPowerDeviceClass IndicatorPowerDeviceClass; +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" + +/** + * IndicatorPowerDeviceClass: + * @parent_class: #GObjectClass + */ +struct _IndicatorPowerDeviceClass +{ + GObjectClass parent_class; +}; + +/** + * IndicatorPowerDevice: + * @parent: #GObject + * @priv: A cached reference to the private data for the instance. +*/ +struct _IndicatorPowerDevice +{ + GObject parent; + IndicatorPowerDevicePrivate * priv; +}; + +/*** +**** +***/ + +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); + +/** + * Convenience wrapper around indicator_power_device_new() + * @variant holds the same args as indicator_power_device_new() in "(susdut)" + */ +IndicatorPowerDevice* indicator_power_device_new_from_variant (GVariant * variant); + + +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); + + +G_END_DECLS + +#endif diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..87dd606 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,38 @@ +TESTS = +CLEANFILES = +check_PROGRAMS = + +AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror +AM_CXXFLAGS = $(GTEST_CXXFLAGS) + +### +### +### + +# stock UMB tests on user-visible strings +include $(srcdir)/Makefile.am.strings + +### +### +### + +TESTS += test-device +check_PROGRAMS += test-device +test_device_SOURCES = test-device.cc +test_device_LDADD = \ + $(top_builddir)/src/libpower.la \ + $(INDICATOR_LIBS) \ + $(UPOWER_LIBS) \ + $(COVERAGE_LDFLAGS) \ + libgtest.a + +test_device_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + $(UPOWER_CFLAGS) \ + $(INDICATOR_CFLAGS) \ + $(COVERAGE_CFLAGS) + +check_LIBRARIES = libgtest.a +nodist_libgtest_a_SOURCES = \ + $(GTEST_SOURCE)/src/gtest-all.cc \ + $(GTEST_SOURCE)/src/gtest_main.cc diff --git a/tests/Makefile.am.strings b/tests/Makefile.am.strings new file mode 100644 index 0000000..26a23a8 --- /dev/null +++ b/tests/Makefile.am.strings @@ -0,0 +1,38 @@ +TESTS += \ + test-ellipsis \ + test-space-ellipsis \ + test-ascii-quotes + +##### +# Tests for there being proper ellipsis instead of three periods in a row +##### +test-ellipsis: $(top_srcdir)/po + @echo "#!/bin/bash" > $@ + @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ + @echo "grep -c -e \"^msgid.*\.\.\.\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"Ellipsis found in user visible strings\" >&2 && exit 1" >> $@ + @echo "exit 0" >> $@ + @chmod +x $@ + +##### +# Tests for there being a space before an ellipsis +##### +test-space-ellipsis: $(top_srcdir)/po + @echo "#!/bin/bash" > $@ + @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ + @echo "grep -c -e \"^msgid.* …\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"Space before ellipsis found in user visible strings\" >&2 && exit 1" >> $@ + @echo "exit 0" >> $@ + @chmod +x $@ + +##### +# Tests for ASCII quote types +##### +test-ascii-quotes: $(top_srcdir)/po + @echo "#!/bin/bash" > $@ + @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ + @echo "grep -c -e \"^msgid \\\".*'.*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII apostrophy found in user visible strings\" >&2 && exit 1" >> $@ + @echo "grep -c -e \"^msgid \\\".*\\\".*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII quote found in user visible strings\" >&2 && exit 1" >> $@ + @echo "grep -c -e \"^msgid \\\".*\\\`.*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII backtick found in user visible strings\" >&2 && exit 1" >> $@ + @echo "exit 0" >> $@ + @chmod +x $@ + +CLEANFILES += $(TESTS) diff --git a/tests/test-device.cc b/tests/test-device.cc new file mode 100644 index 0000000..02516f4 --- /dev/null +++ b/tests/test-device.cc @@ -0,0 +1,99 @@ +/* +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#include +#include "device.h" + +namespace +{ + void ensure_glib_initialized () + { + static bool initialized = false; + + if (G_UNLIKELY(!initialized)) + { + initialized = true; + g_type_init(); + } + } +} + +/*** +**** +***/ + +TEST(DeviceTest, GObjectNew) +{ + ensure_glib_initialized (); + + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); + g_clear_pointer (&o, g_object_unref); +} + +TEST(DeviceTest, New) +{ + ensure_glib_initialized (); + + IndicatorPowerDevice * device = indicator_power_device_new ("/object/path", + UP_DEVICE_KIND_BATTERY, + "icon", + 50.0, + UP_DEVICE_STATE_CHARGING, + 30); + ASSERT_TRUE (device != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); + 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); + + /* cleanup */ + g_clear_pointer (&device, g_object_unref); +} + +TEST(DeviceTest, NewFromVariant) +{ + ensure_glib_initialized (); + + GVariant * variant = g_variant_new ("(susdut)", + "/object/path", + UP_DEVICE_KIND_BATTERY, + "icon", + 50.0, + UP_DEVICE_STATE_CHARGING, + 30); + IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant); + ASSERT_TRUE (variant != NULL); + ASSERT_TRUE (device != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); + 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); + + /* cleanup */ + g_clear_pointer (&device, g_object_unref); + g_clear_pointer (&variant, g_variant_unref); +} -- cgit v1.2.3 From f434dc2f9579aeea0c7afe6fa8b78d56c560707f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 10:00:15 -0500 Subject: add a 'hello world' test for instantiating IndicatorPower --- src/indicator-power.c | 41 +------------------------------ src/indicator-power.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 37 +++++++++++++++++++--------- 3 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 src/indicator-power.h diff --git a/src/indicator-power.c b/src/indicator-power.c index 7cdaeb2..ea1caa5 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -31,9 +31,7 @@ with this program. If not, see . /* upower */ #include -/* Indicator Stuff */ -#include -#include +#include "indicator-power.h" #define ICON_POLICY_KEY "icon-policy" @@ -50,43 +48,6 @@ enum { POWER_INDICATOR_ICON_POLICY_NEVER }; -#define INDICATOR_POWER_TYPE (indicator_power_get_type ()) -#define INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_TYPE, IndicatorPower)) -#define INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_TYPE, IndicatorPowerClass)) -#define IS_INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_TYPE)) -#define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) -#define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) - -typedef struct _IndicatorPowerClass IndicatorPowerClass; -typedef struct _IndicatorPower IndicatorPower; - -struct _IndicatorPowerClass -{ - IndicatorObjectClass parent_class; -}; - -struct _IndicatorPower -{ - IndicatorObject parent_instance; - - GtkMenu *menu; - - GtkLabel *label; - GtkImage *status_image; - gchar *accessible_desc; - - GCancellable *proxy_cancel; - GDBusProxy *proxy; - guint watcher_id; - - GVariant *devices; - GVariant *device; - - GSettings *settings; -}; - -GType indicator_power_get_type (void) G_GNUC_CONST; - INDICATOR_SET_VERSION INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) diff --git a/src/indicator-power.h b/src/indicator-power.h new file mode 100644 index 0000000..6fadae3 --- /dev/null +++ b/src/indicator-power.h @@ -0,0 +1,68 @@ +/* +An indicator to power related information in the menubar. + +Copyright 2011 Canonical Ltd. + +Authors: + Javier Jardon + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +/* Gtk required */ +#include + +/* parent class */ +#include +#include + +G_BEGIN_DECLS + +#define INDICATOR_POWER_TYPE (indicator_power_get_type ()) +#define INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_TYPE, IndicatorPower)) +#define INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_TYPE, IndicatorPowerClass)) +#define IS_INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_TYPE)) +#define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) +#define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) + +typedef struct _IndicatorPowerClass IndicatorPowerClass; +typedef struct _IndicatorPower IndicatorPower; + +struct _IndicatorPowerClass +{ + IndicatorObjectClass parent_class; +}; + +struct _IndicatorPower +{ + IndicatorObject parent_instance; + + GtkMenu *menu; + + GtkLabel *label; + GtkImage *status_image; + gchar *accessible_desc; + + GCancellable *proxy_cancel; + GDBusProxy *proxy; + guint watcher_id; + + GVariant *devices; + GVariant *device; + + GSettings *settings; +}; + +GType indicator_power_get_type (void) G_GNUC_CONST; + +G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index 87dd606..341abdd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,9 +2,6 @@ TESTS = CLEANFILES = check_PROGRAMS = -AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror -AM_CXXFLAGS = $(GTEST_CXXFLAGS) - ### ### ### @@ -12,27 +9,43 @@ AM_CXXFLAGS = $(GTEST_CXXFLAGS) # stock UMB tests on user-visible strings include $(srcdir)/Makefile.am.strings +check_LIBRARIES = libgtest.a +nodist_libgtest_a_SOURCES = \ + $(GTEST_SOURCE)/src/gtest-all.cc \ + $(GTEST_SOURCE)/src/gtest_main.cc + +AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror +AM_CXXFLAGS = $(GTEST_CXXFLAGS) + ### ### ### -TESTS += test-device -check_PROGRAMS += test-device -test_device_SOURCES = test-device.cc -test_device_LDADD = \ +TEST_LIBS = \ $(top_builddir)/src/libpower.la \ $(INDICATOR_LIBS) \ $(UPOWER_LIBS) \ $(COVERAGE_LDFLAGS) \ libgtest.a -test_device_CPPFLAGS = \ +TEST_CPPFLAGS = \ $(AM_CPPFLAGS) \ $(UPOWER_CFLAGS) \ $(INDICATOR_CFLAGS) \ $(COVERAGE_CFLAGS) -check_LIBRARIES = libgtest.a -nodist_libgtest_a_SOURCES = \ - $(GTEST_SOURCE)/src/gtest-all.cc \ - $(GTEST_SOURCE)/src/gtest_main.cc +TESTS += test-device +check_PROGRAMS += test-device +test_device_SOURCES = test-device.cc +test_device_LDADD = $(TEST_LIBS) +test_device_CPPFLAGS = $(TEST_CPPFLAGS) + +TESTS += test-indicator +check_PROGRAMS += test-indicator +test_indicator_SOURCES = test-indicator.cc +test_indicator_LDADD = $(TEST_LIBS) +test_indicator_CPPFLAGS = $(TEST_CPPFLAGS) + +### +### +### -- cgit v1.2.3 From 4f74252ce9a8ae3e651cc9e61dc280da82f12d9b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 10:15:28 -0500 Subject: add test-indicator.cc --- tests/test-indicator.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/test-indicator.cc diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc new file mode 100644 index 0000000..2f16176 --- /dev/null +++ b/tests/test-indicator.cc @@ -0,0 +1,52 @@ +/* +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#include + +#include "device.h" +#include "indicator-power.h" + +namespace +{ + void ensure_glib_initialized () + { + static bool initialized = false; + + if (G_UNLIKELY(!initialized)) + { + initialized = true; + g_type_init(); + } + } +} + +/*** +**** +***/ + +TEST(IndicatorTest, GObjectNew) +{ + ensure_glib_initialized (); + + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (IS_INDICATOR_POWER(o)); + g_clear_pointer (&o, g_object_unref); +} + -- cgit v1.2.3 From f18b60b0ce389bee7fc4784b6714f28567397b42 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 11:32:16 -0500 Subject: remove the g_clear_pointer() calls s.t. things will build and run on alesage's Jenkins setup running Precise --- tests/test-device.cc | 8 ++++---- tests/test-indicator.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 02516f4..4dde4af 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -45,7 +45,7 @@ TEST(DeviceTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); - g_clear_pointer (&o, g_object_unref); + g_object_unref (o); } TEST(DeviceTest, New) @@ -68,7 +68,7 @@ TEST(DeviceTest, New) ASSERT_EQ (indicator_power_device_get_time(device), 30); /* cleanup */ - g_clear_pointer (&device, g_object_unref); + g_object_unref (device); } TEST(DeviceTest, NewFromVariant) @@ -94,6 +94,6 @@ TEST(DeviceTest, NewFromVariant) ASSERT_EQ (indicator_power_device_get_time(device), 30); /* cleanup */ - g_clear_pointer (&device, g_object_unref); - g_clear_pointer (&variant, g_variant_unref); + g_object_unref (device); + g_variant_unref (variant); } diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 2f16176..e3533e9 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -47,6 +47,6 @@ TEST(IndicatorTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); - g_clear_pointer (&o, g_object_unref); + g_object_unref (o); } -- 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(-) 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 d8484ce5caf9fd5f4efb30368ccc9b70b1f38710 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 16:50:58 -0500 Subject: first draft of getting GSettings working in the unit tests before the schema is installed. --- tests/Makefile.am | 10 ++++++++-- tests/test-indicator.cc | 27 +++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index 341abdd..8169e3a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,6 @@ TESTS = -CLEANFILES = +CLEANFILES = +BUILT_SOURCES = check_PROGRAMS = ### @@ -34,6 +35,11 @@ TEST_CPPFLAGS = \ $(INDICATOR_CFLAGS) \ $(COVERAGE_CFLAGS) +BUILT_SOURCES += gschemas.compiled +CLEANFILES += gschemas.compiled +gschemas.compiled: Makefile + @glib-compile-schemas --targetdir=$(abs_builddir) $(top_builddir)/data + TESTS += test-device check_PROGRAMS += test-device test_device_SOURCES = test-device.cc @@ -44,7 +50,7 @@ TESTS += test-indicator check_PROGRAMS += test-indicator test_indicator_SOURCES = test-indicator.cc test_indicator_LDADD = $(TEST_LIBS) -test_indicator_CPPFLAGS = $(TEST_CPPFLAGS) +test_indicator_CPPFLAGS = $(TEST_CPPFLAGS) -DSCHEMA_DIR="\"$(top_builddir)/tests/\"" ### ### diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index e3533e9..af494ee 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -22,6 +22,10 @@ with this program. If not, see . #include "device.h" #include "indicator-power.h" +/*** +**** +***/ + namespace { void ensure_glib_initialized () @@ -40,7 +44,27 @@ namespace **** ***/ -TEST(IndicatorTest, GObjectNew) +class IndicatorTest : public ::testing::Test +{ + protected: + + virtual void SetUp() + { + ensure_glib_initialized (); + g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); + g_message ("GSETTINGS_SCHEMA_DIR is %s", g_getenv("GSETTINGS_SCHEMA_DIR")); + } + + virtual void TearDown() + { + } +}; + +/*** +**** +***/ + +TEST_F(IndicatorTest, GObjectNew) { ensure_glib_initialized (); @@ -49,4 +73,3 @@ TEST(IndicatorTest, GObjectNew) ASSERT_TRUE (IS_INDICATOR_POWER(o)); g_object_unref (o); } - -- cgit v1.2.3 From 4c95f0b14ad44f39b8e8e0b01a959e2a82e297a9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 18:21:34 -0500 Subject: add unit tests for getting/setting device properties --- tests/test-device.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/test-device.cc b/tests/test-device.cc index 4dde4af..0a86478 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -48,6 +48,96 @@ TEST(DeviceTest, GObjectNew) g_object_unref (o); } +TEST(DeviceTest, Properties) +{ + int i; + gdouble d; + GObject * o; + gchar * str; + guint64 u64; + const gchar * key; + GValue string_value = G_VALUE_INIT; + GValue object_value = G_VALUE_INIT; + + ensure_glib_initialized (); + + g_value_init (&string_value, G_TYPE_STRING); + g_value_init (&object_value, G_TYPE_OBJECT); + g_value_set_string (&string_value, "i am a string"); + + o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); + + /* Test getting & setting a Device's properties. + * Also test that setting a property from an unrelated type has no effect. */ + + // KIND + key = INDICATOR_POWER_DEVICE_KIND; + g_object_set (o, key, UP_DEVICE_KIND_BATTERY, NULL); + g_object_get (o, key, &i, NULL); + ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY); + g_object_set_property (o, key, &string_value); + g_object_get (o, key, &i, NULL); + ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY); + + // STATE + key = INDICATOR_POWER_DEVICE_STATE; + g_object_set (o, key, UP_DEVICE_STATE_CHARGING, NULL); + g_object_get (o, key, &i, NULL); + ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING); + g_object_set_property (o, key, &string_value); + g_object_get (o, key, &i, NULL); + ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING); + + // OBJECT_PATH + key = INDICATOR_POWER_DEVICE_OBJECT_PATH; + g_object_set (o, key, "/object/path", NULL); + g_object_get (o, key, &str, NULL); + ASSERT_STREQ (str, "/object/path"); + g_free (str); + g_object_set_property (o, key, &object_value); + g_object_get (o, key, &str, NULL); + 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); + str = NULL; + g_object_set_property (o, key, &object_value); + g_object_get (o, key, &str, NULL); + ASSERT_STREQ (str, "something"); + g_free (str); + str = NULL; + + // PERCENTAGE + key = INDICATOR_POWER_DEVICE_PERCENTAGE; + g_object_set (o, key, 50.0, NULL); + g_object_get (o, key, &d, NULL); + ASSERT_EQ((int)d, 50); + g_object_set_property (o, key, &object_value); + g_object_get (o, key, &d, NULL); + ASSERT_EQ((int)d, 50); + + // TIME + key = INDICATOR_POWER_DEVICE_TIME; + g_object_set (o, key, 30, NULL); + g_object_get (o, key, &u64, NULL); + ASSERT_EQ(u64, 30); + g_object_set_property (o, key, &object_value); + g_object_get (o, key, &u64, NULL); + ASSERT_EQ(u64, 30); + + // cleanup + g_object_unref (o); + g_value_unset (&object_value); + g_value_unset (&string_value); +} + TEST(DeviceTest, New) { ensure_glib_initialized (); -- 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(-) 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 fd7ad85760663cf176e126d7e350b9f010630b9b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 18:48:04 -0500 Subject: don't include the unit tests' code in coverage metrics --- tests/Makefile.am | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index 8169e3a..b61bbe0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,7 +23,7 @@ AM_CXXFLAGS = $(GTEST_CXXFLAGS) ### TEST_LIBS = \ - $(top_builddir)/src/libpower.la \ + $(top_builddir)/src/.libs/libpower.a \ $(INDICATOR_LIBS) \ $(UPOWER_LIBS) \ $(COVERAGE_LDFLAGS) \ @@ -32,8 +32,7 @@ TEST_LIBS = \ TEST_CPPFLAGS = \ $(AM_CPPFLAGS) \ $(UPOWER_CFLAGS) \ - $(INDICATOR_CFLAGS) \ - $(COVERAGE_CFLAGS) + $(INDICATOR_CFLAGS) BUILT_SOURCES += gschemas.compiled CLEANFILES += gschemas.compiled -- cgit v1.2.3 From d2ba05cfd549a09ff8651c1700b8bf0a8e7d0451 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 18:51:51 -0500 Subject: remove the tests that attempted to exercise the code removed in r151 --- tests/test-device.cc | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 0a86478..70beeb7 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -56,39 +56,26 @@ TEST(DeviceTest, Properties) gchar * str; guint64 u64; const gchar * key; - GValue string_value = G_VALUE_INIT; - GValue object_value = G_VALUE_INIT; ensure_glib_initialized (); - g_value_init (&string_value, G_TYPE_STRING); - g_value_init (&object_value, G_TYPE_OBJECT); - g_value_set_string (&string_value, "i am a string"); - o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); - /* Test getting & setting a Device's properties. - * Also test that setting a property from an unrelated type has no effect. */ + /* Test getting & setting a Device's properties. */ // KIND key = INDICATOR_POWER_DEVICE_KIND; g_object_set (o, key, UP_DEVICE_KIND_BATTERY, NULL); g_object_get (o, key, &i, NULL); ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY); - g_object_set_property (o, key, &string_value); - g_object_get (o, key, &i, NULL); - ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY); // STATE key = INDICATOR_POWER_DEVICE_STATE; g_object_set (o, key, UP_DEVICE_STATE_CHARGING, NULL); g_object_get (o, key, &i, NULL); ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING); - g_object_set_property (o, key, &string_value); - g_object_get (o, key, &i, NULL); - ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING); // OBJECT_PATH key = INDICATOR_POWER_DEVICE_OBJECT_PATH; @@ -96,10 +83,6 @@ TEST(DeviceTest, Properties) g_object_get (o, key, &str, NULL); ASSERT_STREQ (str, "/object/path"); g_free (str); - g_object_set_property (o, key, &object_value); - g_object_get (o, key, &str, NULL); - ASSERT_STREQ (str, "/object/path"); - g_free (str); // ICON key = INDICATOR_POWER_DEVICE_ICON; @@ -107,35 +90,21 @@ TEST(DeviceTest, Properties) g_object_get (o, key, &str, NULL); ASSERT_STREQ (str, "something"); g_free (str); - str = NULL; - g_object_set_property (o, key, &object_value); - g_object_get (o, key, &str, NULL); - ASSERT_STREQ (str, "something"); - g_free (str); - str = NULL; // PERCENTAGE key = INDICATOR_POWER_DEVICE_PERCENTAGE; g_object_set (o, key, 50.0, NULL); g_object_get (o, key, &d, NULL); ASSERT_EQ((int)d, 50); - g_object_set_property (o, key, &object_value); - g_object_get (o, key, &d, NULL); - ASSERT_EQ((int)d, 50); // TIME key = INDICATOR_POWER_DEVICE_TIME; - g_object_set (o, key, 30, NULL); - g_object_get (o, key, &u64, NULL); - ASSERT_EQ(u64, 30); - g_object_set_property (o, key, &object_value); + g_object_set (o, key, (guint64)30, NULL); g_object_get (o, key, &u64, NULL); ASSERT_EQ(u64, 30); // cleanup g_object_unref (o); - g_value_unset (&object_value); - g_value_unset (&string_value); } TEST(DeviceTest, New) -- 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(-) 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(-) 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 88d486192c176f938b09ee86c5cd6f95dfd02aba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 19:06:58 -0500 Subject: add unit tests confirming that the device accessor functions won't crash when you pass NULL for the device --- tests/test-device.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test-device.cc b/tests/test-device.cc index 70beeb7..0102185 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -156,3 +156,17 @@ TEST(DeviceTest, NewFromVariant) g_object_unref (device); g_variant_unref (variant); } + +TEST(DeviceTest, BadAccessors) +{ + ensure_glib_initialized (); + + // test that these functions can handle being passed NULL pointers + IndicatorPowerDevice * device = NULL; + indicator_power_device_get_kind(device); + indicator_power_device_get_state(device); + indicator_power_device_get_object_path(device); + indicator_power_device_get_icon(device); + indicator_power_device_get_percentage(device); + indicator_power_device_get_time(device); +} -- cgit v1.2.3 From ebf2edadf40205a11998eb37c59cab1f2116d7f5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 19:24:11 -0500 Subject: test passing bad non-NULL pointers to Device's accessors --- tests/test-device.cc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 0102185..3a7e8d0 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -163,10 +163,20 @@ TEST(DeviceTest, BadAccessors) // test that these functions can handle being passed NULL pointers IndicatorPowerDevice * device = NULL; - indicator_power_device_get_kind(device); - indicator_power_device_get_state(device); - indicator_power_device_get_object_path(device); - indicator_power_device_get_icon(device); - indicator_power_device_get_percentage(device); - indicator_power_device_get_time(device); + 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); + + // test that these functions can handle being passed non-device GObjects + device = 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); + g_object_unref (device); } -- cgit v1.2.3 From 4d1bb110b97fc1e9835d295cfcd30c7125a2380d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 19:27:59 -0500 Subject: ah, c++ is too smart for the last commit's implicit conversion. --- tests/test-device.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 3a7e8d0..388460e 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -171,7 +171,7 @@ TEST(DeviceTest, BadAccessors) indicator_power_device_get_object_path (device); // test that these functions can handle being passed non-device GObjects - device = g_cancellable_new (); + device = reinterpret_cast(g_cancellable_new ()); indicator_power_device_get_kind (device); indicator_power_device_get_time (device); indicator_power_device_get_icon (device); -- cgit v1.2.3 From 33f4e3d7024984b8b4f3b4fe0b1dfa4ec8ab5700 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 09:26:06 -0500 Subject: put IndicatorPower's fields back inside a priv struct --- src/indicator-power.c | 154 +++++++++++++++++++++++++++++++------------------- src/indicator-power.h | 19 +------ 2 files changed, 99 insertions(+), 74 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index ea1caa5..dc1c512 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -48,6 +48,25 @@ enum { POWER_INDICATOR_ICON_POLICY_NEVER }; +struct _IndicatorPowerPrivate +{ + GtkMenu *menu; + + GtkLabel *label; + GtkImage *status_image; + gchar *accessible_desc; + + GCancellable *proxy_cancel; + GDBusProxy *proxy; + guint watcher_id; + + GVariant *devices; + GVariant *device; + + GSettings *settings; +}; + + INDICATOR_SET_VERSION INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) @@ -76,6 +95,8 @@ indicator_power_class_init (IndicatorPowerClass *klass) GObjectClass *object_class = G_OBJECT_CLASS (klass); IndicatorObjectClass *io_class = INDICATOR_OBJECT_CLASS (klass); + g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate)); + object_class->dispose = indicator_power_dispose; object_class->finalize = indicator_power_finalize; @@ -89,11 +110,15 @@ indicator_power_class_init (IndicatorPowerClass *klass) static void indicator_power_init (IndicatorPower *self) { - self->menu = GTK_MENU(gtk_menu_new()); + IndicatorPowerPrivate * priv; + + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, INDICATOR_POWER_TYPE, IndicatorPowerPrivate); + + priv->menu = GTK_MENU(gtk_menu_new()); - self->accessible_desc = NULL; + priv->accessible_desc = NULL; - self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, DBUS_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, gsd_appeared_callback, @@ -101,8 +126,8 @@ indicator_power_init (IndicatorPower *self) self, NULL); - self->settings = g_settings_new ("com.canonical.indicator.power"); - g_signal_connect_swapped (self->settings, "changed::" ICON_POLICY_KEY, + priv->settings = g_settings_new ("com.canonical.indicator.power"); + g_signal_connect_swapped (priv->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); g_object_set (G_OBJECT(self), INDICATOR_OBJECT_DEFAULT_VISIBILITY, FALSE, @@ -110,27 +135,30 @@ indicator_power_init (IndicatorPower *self) g_signal_connect (INDICATOR_OBJECT(self), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED, G_CALLBACK(on_entry_added), NULL); + + self->priv = priv; } static void indicator_power_dispose (GObject *object) { IndicatorPower *self = INDICATOR_POWER(object); + IndicatorPowerPrivate * priv = self->priv; - if (self->devices != NULL) { - g_variant_unref (self->devices); - self->devices = NULL; + if (priv->devices != NULL) { + g_variant_unref (priv->devices); + priv->devices = NULL; } - if (self->device != NULL) { - g_variant_unref (self->device); - self->device = NULL; + if (priv->device != NULL) { + g_variant_unref (priv->device); + priv->device = NULL; } - g_clear_object (&self->proxy); - g_clear_object (&self->proxy_cancel); + g_clear_object (&priv->proxy); + g_clear_object (&priv->proxy_cancel); - g_clear_object (&self->settings); + g_clear_object (&priv->settings); G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); } @@ -139,8 +167,9 @@ static void indicator_power_finalize (GObject *object) { IndicatorPower *self = INDICATOR_POWER(object); + IndicatorPowerPrivate * priv = self->priv; - g_free (self->accessible_desc); + g_free (priv->accessible_desc); G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); } @@ -169,7 +198,7 @@ show_info_cb (GtkMenuItem *item, static void option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self) { - gtk_widget_set_visible (GTK_WIDGET (self->label), + gtk_widget_set_visible (GTK_WIDGET (self->priv->label), gtk_check_menu_item_get_active(item)); } @@ -368,7 +397,7 @@ build_device_time_details (const gchar *device_name, static void refresh_entry_accessible_desc (IndicatorPower * self, IndicatorObjectEntry * entry) { - const char * newval = self->accessible_desc; + const char * newval = self->priv->accessible_desc; if (entry->accessible_desc != newval) { @@ -394,8 +423,8 @@ set_accessible_desc (IndicatorPower *self, const gchar *desc) if (desc && *desc) { /* update our copy of the string */ - char * oldval = self->accessible_desc; - self->accessible_desc = g_strdup (desc); + char * oldval = self->priv->accessible_desc; + self->priv->accessible_desc = g_strdup (desc); /* ensure that the entries are using self's accessible description */ GList * l; @@ -614,28 +643,29 @@ build_menu (IndicatorPower *self) GtkWidget *image; GList *children; gsize n_devices = 0; + IndicatorPowerPrivate * priv = self->priv; /* remove the existing menuitems */ - children = gtk_container_get_children (GTK_CONTAINER (self->menu)); + children = gtk_container_get_children (GTK_CONTAINER (priv->menu)); g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); g_list_free (children); /* devices */ - n_devices = menu_add_devices (self->menu, self->devices); + n_devices = menu_add_devices (priv->menu, priv->devices); if (!get_greeter_mode ()) { /* only do the separator if we have at least one device */ if (n_devices != 0) { item = gtk_separator_menu_item_new (); - gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); + gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); } /* options */ item = gtk_check_menu_item_new_with_label (_("Show Time in Menu Bar")); g_signal_connect (item, "toggled", G_CALLBACK(option_toggled_cb), self); - g_settings_bind (self->settings, "show-time", item, "active", G_SETTINGS_BIND_DEFAULT); - gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); + g_settings_bind (priv->settings, "show-time", item, "active", G_SETTINGS_BIND_DEFAULT); + gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); /* preferences */ item = gtk_image_menu_item_new_with_label (_("Power Settings…")); @@ -643,11 +673,11 @@ build_menu (IndicatorPower *self) gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (show_preferences_cb), NULL); - gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); + gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); } /* show the menu */ - gtk_widget_show_all (GTK_WIDGET (self->menu)); + gtk_widget_show_all (GTK_WIDGET (priv->menu)); } static GVariant * @@ -751,6 +781,7 @@ put_primary_device (IndicatorPower *self, gdouble percentage; guint64 time; const gchar *device_name; + IndicatorPowerPrivate * priv = self->priv; /* set the icon and text */ g_variant_get (device, @@ -766,11 +797,11 @@ put_primary_device (IndicatorPower *self, /* set icon */ device_gicons = get_device_icon (kind, state, time, device_icon); - gtk_image_set_from_gicon (self->status_image, + gtk_image_set_from_gicon (priv->status_image, device_gicons, GTK_ICON_SIZE_LARGE_TOOLBAR); g_clear_object (&device_gicons); - gtk_widget_show (GTK_WIDGET (self->status_image)); + gtk_widget_show (GTK_WIDGET (priv->status_image)); /* get the device name */ @@ -779,7 +810,7 @@ put_primary_device (IndicatorPower *self, /* get the description */ build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); - gtk_label_set_label (GTK_LABEL (self->label), + gtk_label_set_label (GTK_LABEL (priv->label), short_details); set_accessible_desc (self, accessible_name); @@ -794,6 +825,7 @@ get_devices_cb (GObject *source_object, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); + IndicatorPowerPrivate * priv = self->priv; GVariant *devices_container; GError *error = NULL; @@ -805,23 +837,23 @@ get_devices_cb (GObject *source_object, } else /* update 'devices' */ { - if (self->devices != NULL) - g_variant_unref (self->devices); - self->devices = g_variant_get_child_value (devices_container, 0); + if (priv->devices != NULL) + g_variant_unref (priv->devices); + priv->devices = g_variant_get_child_value (devices_container, 0); g_variant_unref (devices_container); - if (self->device != NULL) - g_variant_unref (self->device); - self->device = get_primary_device (self->devices); + if (priv->device != NULL) + g_variant_unref (priv->device); + priv->device = get_primary_device (priv->devices); - if (self->device == NULL) + if (priv->device == NULL) { g_message ("Couldn't find primary device"); } else { - put_primary_device (self, self->device); + put_primary_device (self, priv->device); } } @@ -844,14 +876,15 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); + IndicatorPowerPrivate * priv = self->priv; /* it's time to refresh our device list */ - g_dbus_proxy_call (self->proxy, + g_dbus_proxy_call (priv->proxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, - self->proxy_cancel, + priv->proxy_cancel, get_devices_cb, user_data); } @@ -862,11 +895,12 @@ service_proxy_cb (GObject *object, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); + IndicatorPowerPrivate * priv = self->priv; GError *error = NULL; - self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - g_clear_object (&self->proxy_cancel); + g_clear_object (&priv->proxy_cancel); if (error != NULL) { @@ -877,18 +911,18 @@ service_proxy_cb (GObject *object, } /* we want to change the primary device changes */ - g_signal_connect (self->proxy, + g_signal_connect (priv->proxy, "g-properties-changed", G_CALLBACK (receive_properties_changed), user_data); /* get the initial state */ - g_dbus_proxy_call (self->proxy, + g_dbus_proxy_call (priv->proxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, - self->proxy_cancel, + priv->proxy_cancel, get_devices_cb, user_data); } @@ -900,8 +934,9 @@ gsd_appeared_callback (GDBusConnection *connection, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); + IndicatorPowerPrivate * priv = self->priv; - self->proxy_cancel = g_cancellable_new (); + priv->proxy_cancel = g_cancellable_new (); g_dbus_proxy_new (connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, @@ -909,7 +944,7 @@ gsd_appeared_callback (GDBusConnection *connection, name, POWER_DBUS_PATH, POWER_DBUS_INTERFACE, - self->proxy_cancel, + priv->proxy_cancel, service_proxy_cb, self); } @@ -923,32 +958,34 @@ static GtkLabel * get_label (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); + IndicatorPowerPrivate * priv = self->priv; - if (self->label == NULL) + if (priv->label == NULL) { /* Create the label if it doesn't exist already */ - self->label = GTK_LABEL (gtk_label_new ("")); - gtk_widget_set_visible (GTK_WIDGET (self->label), FALSE); + priv->label = GTK_LABEL (gtk_label_new ("")); + gtk_widget_set_visible (GTK_WIDGET (priv->label), FALSE); } - return self->label; + return priv->label; } static GtkImage * get_image (IndicatorObject *io) { - IndicatorPower *self = INDICATOR_POWER (io); GIcon *gicon; + IndicatorPower *self = INDICATOR_POWER (io); + IndicatorPowerPrivate * priv = self->priv; - if (self->status_image == NULL) + if (priv->status_image == NULL) { /* Will create the status icon if it doesn't exist already */ gicon = g_themed_icon_new (DEFAULT_ICON); - self->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, + priv->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_LARGE_TOOLBAR)); } - return self->status_image; + return priv->status_image; } static GtkMenu * @@ -958,7 +995,7 @@ get_menu (IndicatorObject *io) build_menu (self); - return GTK_MENU (self->menu); + return GTK_MENU (self->priv->menu); } static const gchar * @@ -966,7 +1003,7 @@ get_accessible_desc (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); - return self->accessible_desc; + return self->priv->accessible_desc; } static const gchar * @@ -1011,8 +1048,9 @@ static gboolean should_be_visible (IndicatorPower * self) { gboolean visible = TRUE; + IndicatorPowerPrivate * priv = self->priv; - const int icon_policy = g_settings_get_enum (self->settings, ICON_POLICY_KEY); + const int icon_policy = g_settings_get_enum (priv->settings, ICON_POLICY_KEY); g_debug ("icon_policy is: %d (present==0, charge==1, never==2)", icon_policy); @@ -1023,7 +1061,7 @@ should_be_visible (IndicatorPower * self) else { int batteries=0, inuse=0; - count_batteries (self->devices, &batteries, &inuse); + count_batteries (priv->devices, &batteries, &inuse); if (icon_policy == POWER_INDICATOR_ICON_POLICY_PRESENT) { diff --git a/src/indicator-power.h b/src/indicator-power.h index 6fadae3..39ac0ca 100644 --- a/src/indicator-power.h +++ b/src/indicator-power.h @@ -35,8 +35,9 @@ G_BEGIN_DECLS #define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) #define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) -typedef struct _IndicatorPowerClass IndicatorPowerClass; typedef struct _IndicatorPower IndicatorPower; +typedef struct _IndicatorPowerClass IndicatorPowerClass; +typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate; struct _IndicatorPowerClass { @@ -46,21 +47,7 @@ struct _IndicatorPowerClass struct _IndicatorPower { IndicatorObject parent_instance; - - GtkMenu *menu; - - GtkLabel *label; - GtkImage *status_image; - gchar *accessible_desc; - - GCancellable *proxy_cancel; - GDBusProxy *proxy; - guint watcher_id; - - GVariant *devices; - GVariant *device; - - GSettings *settings; + IndicatorPowerPrivate * priv; }; GType indicator_power_get_type (void) G_GNUC_CONST; -- cgit v1.2.3 From 34c756aa7a894ee9c12f3dce0b4ae6e4ac6a30a8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 14:58:56 -0500 Subject: modify IndicatorPower to use IndicatorPowerDevices internally --- src/indicator-power.c | 344 ++++++++++++++++++++++-------------------------- src/indicator-power.h | 6 + tests/test-indicator.cc | 31 ++++- 3 files changed, 193 insertions(+), 188 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index dc1c512..c2ace80 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -31,6 +31,7 @@ with this program. If not, see . /* upower */ #include +#include "device.h" #include "indicator-power.h" #define ICON_POLICY_KEY "icon-policy" @@ -60,8 +61,8 @@ struct _IndicatorPowerPrivate GDBusProxy *proxy; guint watcher_id; - GVariant *devices; - GVariant *device; + GSList * devices; + IndicatorPowerDevice * device; GSettings *settings; }; @@ -139,21 +140,22 @@ indicator_power_init (IndicatorPower *self) self->priv = priv; } +static void +dispose_devices (IndicatorPower * self) +{ + IndicatorPowerPrivate * priv = self->priv; + + g_slist_free_full (priv->devices, g_object_unref); + priv->devices = NULL; + g_clear_object (&priv->device); +} static void indicator_power_dispose (GObject *object) { IndicatorPower *self = INDICATOR_POWER(object); IndicatorPowerPrivate * priv = self->priv; - if (priv->devices != NULL) { - g_variant_unref (priv->devices); - priv->devices = NULL; - } - - if (priv->device != NULL) { - g_variant_unref (priv->device); - priv->device = NULL; - } + dispose_devices (self); g_clear_object (&priv->proxy); g_clear_object (&priv->proxy_cancel); @@ -533,99 +535,78 @@ get_device_icon (UpDeviceKind kind, } -static void -menu_add_device (GtkMenu *menu, - GVariant *device) +static gboolean +menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) { - UpDeviceKind kind; - UpDeviceState state; - GtkWidget *icon; - GtkWidget *item; - GtkWidget *details_label; - GtkWidget *grid; - GIcon *device_gicons; - const gchar *device_icon = NULL; - const gchar *object_path = NULL; - gdouble percentage; - guint64 time; - const gchar *device_name; - gchar *short_details = NULL; - gchar *details = NULL; - gchar *accessible_name = NULL; - AtkObject *atk_object; - - if (device == NULL) - return; - - g_variant_get (device, - "(&su&sdut)", - &object_path, - &kind, - &device_icon, - &percentage, - &state, - &time); - - g_debug ("%s: got data from object %s", G_STRFUNC, object_path); + gboolean added = FALSE; + const UpDeviceKind kind = indicator_power_device_get_kind (device); if (kind == UP_DEVICE_KIND_LINE_POWER) - return; - - /* 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_name = device_kind_to_localised_string (kind); - - build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); - - /* Create menu item */ - item = gtk_image_menu_item_new (); - atk_object = gtk_widget_get_accessible(item); - if (atk_object != NULL) - atk_object_set_name (atk_object, accessible_name); - - grid = gtk_grid_new (); - gtk_grid_set_column_spacing (GTK_GRID (grid), 6); - gtk_grid_attach (GTK_GRID (grid), icon, 0, 0, 1, 1); - details_label = gtk_label_new (details); - gtk_grid_attach_next_to (GTK_GRID (grid), details_label, icon, GTK_POS_RIGHT, 1, 1); - gtk_container_add (GTK_CONTAINER (item), grid); - gtk_widget_show (grid); + { + GtkWidget *icon; + GtkWidget *item; + GtkWidget *details_label; + GtkWidget *grid; + GIcon *device_gicons; + 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 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_name = device_kind_to_localised_string (kind); + + build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accessible_name); + + /* Create menu item */ + item = gtk_image_menu_item_new (); + atk_object = gtk_widget_get_accessible(item); + if (atk_object != NULL) + atk_object_set_name (atk_object, accessible_name); + + grid = gtk_grid_new (); + gtk_grid_set_column_spacing (GTK_GRID (grid), 6); + gtk_grid_attach (GTK_GRID (grid), icon, 0, 0, 1, 1); + details_label = gtk_label_new (details); + gtk_grid_attach_next_to (GTK_GRID (grid), details_label, icon, GTK_POS_RIGHT, 1, 1); + gtk_container_add (GTK_CONTAINER (item), grid); + gtk_widget_show (grid); + + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + added = TRUE; - gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + g_signal_connect (G_OBJECT (item), "activate", + G_CALLBACK (show_info_cb), NULL); - g_signal_connect (G_OBJECT (item), "activate", - G_CALLBACK (show_info_cb), NULL); + g_free (short_details); + g_free (details); + g_free (accessible_name); + } - g_free (short_details); - g_free (details); - g_free (accessible_name); + return added; } static gsize -menu_add_devices (GtkMenu *menu, - GVariant *devices) +menu_add_devices (GtkMenu * menu, GSList * devices) { - gsize n_devices; - guint i; - - if (devices == NULL) - return 0; + GSList * l; + gsize n_added = 0; - n_devices = g_variant_n_children (devices); - g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); + for (l=devices; l!=NULL; l=l->next) + if (menu_add_device (menu, l->data)) + ++n_added; - for (i = 0; i < n_devices; i++) - { - GVariant * device = g_variant_get_child_value (devices, i); - menu_add_device (menu, device); - g_variant_unref (device); - } - - return n_devices; + return n_added; } static gboolean @@ -680,39 +661,25 @@ build_menu (IndicatorPower *self) gtk_widget_show_all (GTK_WIDGET (priv->menu)); } -static GVariant * -get_primary_device (GVariant *devices) +static IndicatorPowerDevice* +get_primary_device (GSList * devices) { - gint primary_device_charging_index = -1; - gint primary_device_discharging_index = -1; - gint primary_device_index = -1; + IndicatorPowerDevice * primary_device = NULL; + IndicatorPowerDevice * primary_device_charging = NULL; + IndicatorPowerDevice * primary_device_discharging = NULL; gboolean charging = FALSE; gboolean discharging = FALSE; guint64 min_discharging_time = G_MAXUINT64; guint64 max_charging_time = 0; - guint i; + GSList * l; - const gsize n_devices = devices ? g_variant_n_children (devices) : 0; - g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); - - for (i = 0; i < n_devices; i++) + for (l=devices; l!=NULL; l=l->next) { - const gchar *object_path; - UpDeviceKind kind; - const gchar *device_icon; - gdouble percentage; - UpDeviceState state; - guint64 time = 0; - - g_variant_get_child (devices, i, "(&su&sdut)", - &object_path, - &kind, - &device_icon, - &percentage, - &state, - &time); - - g_debug ("%s: got data from object %s", G_STRFUNC, object_path); + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE(l->data); + 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); + const time_t time = indicator_power_device_get_time (device); /* Try to fix the case when we get a empty battery bay as a real battery */ if (state == UP_DEVICE_STATE_UNKNOWN && @@ -729,7 +696,7 @@ get_primary_device (GVariant *devices) if (time < min_discharging_time) { min_discharging_time = time; - primary_device_discharging_index = i; + primary_device_discharging = device; } } else if (state == UP_DEVICE_STATE_CHARGING) @@ -737,64 +704,49 @@ get_primary_device (GVariant *devices) charging = TRUE; if (time == 0) /* Battery broken */ { - primary_device_charging_index = i; + primary_device_charging = device; } if (time > max_charging_time) { max_charging_time = time; - primary_device_charging_index = i; + primary_device_charging = device; } } else { - primary_device_index = i; + primary_device = device; } } if (discharging) { - primary_device_index = primary_device_discharging_index; + primary_device = primary_device_discharging; } else if (charging) { - primary_device_index = primary_device_charging_index; + primary_device = primary_device_charging; } - if (primary_device_index >= 0) - return g_variant_get_child_value (devices, primary_device_index); + if (primary_device != NULL) + g_object_ref (primary_device); - return NULL; + return primary_device; } static void -put_primary_device (IndicatorPower *self, - GVariant *device) +put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) { - UpDeviceKind kind; - UpDeviceState state; GIcon *device_gicons; gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; - const gchar *device_icon = NULL; - const gchar *object_path = NULL; - gdouble percentage; - guint64 time; const gchar *device_name; IndicatorPowerPrivate * priv = self->priv; - - /* set the icon and text */ - g_variant_get (device, - "(&su&sdut)", - &object_path, - &kind, - &device_icon, - &percentage, - &state, - &time); - - g_debug ("%s: got data from object %s", G_STRFUNC, object_path); - + 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, @@ -819,47 +771,75 @@ put_primary_device (IndicatorPower *self, g_free (accessible_name); } +void +indicator_power_set_devices (IndicatorPower * self, + IndicatorPowerDevice ** devices, + gsize device_count) +{ + gsize i; + IndicatorPowerPrivate * priv; + + g_return_if_fail (IS_INDICATOR_POWER(self)); + priv = self->priv; + + /* clear out the previous values */ + dispose_devices (self); + + /* get the new devices */ + for (i=0; idevices = g_slist_prepend (priv->devices, g_object_ref(devices[i])); + priv->devices = g_slist_reverse (priv->devices); + + /* get the new primary device */ + priv->device = get_primary_device (priv->devices); + if (priv->device) + put_primary_device (self, priv->device); + else + g_message ("Couldn't find primary device"); + + build_menu (self); + update_visibility (self); +} + static void -get_devices_cb (GObject *source_object, - GAsyncResult *res, - gpointer user_data) +get_devices_cb (GObject * source_object, + GAsyncResult * res, + gpointer user_data) { + GError *error; + int device_count; + IndicatorPowerDevice ** devices; + GVariant * devices_container; IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate * priv = self->priv; - GVariant *devices_container; - GError *error = NULL; + /* build an array of IndicatorPowerDevices from the DBus response */ + error = NULL; devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); if (devices_container == NULL) { g_message ("Couldn't get devices: %s\n", error->message); g_error_free (error); } - else /* update 'devices' */ + else { - if (priv->devices != NULL) - g_variant_unref (priv->devices); - priv->devices = g_variant_get_child_value (devices_container, 0); + gsize i; + GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); + device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; + devices = g_new0 (IndicatorPowerDevice*, device_count); - g_variant_unref (devices_container); - - if (priv->device != NULL) - g_variant_unref (priv->device); - priv->device = get_primary_device (priv->devices); - - if (priv->device == NULL) + for (i=0; idevice); - } - } - build_menu (self); + g_variant_unref (devices_variant); + g_variant_unref (devices_container); + } - update_visibility (self); + indicator_power_set_devices (self, devices, device_count); + g_free (devices); } static void @@ -1017,28 +997,22 @@ get_name_hint (IndicatorObject *io) ***/ static void -count_batteries(GVariant *devices, int *total, int *inuse) +count_batteries (GSList * devices, int *total, int *inuse) { - const int n_devices = devices ? g_variant_n_children (devices) : 0; + GSList * l; - int i; - for (i=0; inext) { - GVariant * device = g_variant_get_child_value (devices, i); + const IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE(l->data); - UpDeviceKind kind; - g_variant_get_child (device, 1, "u", &kind); - if (kind == UP_DEVICE_KIND_BATTERY) + if (indicator_power_device_get_kind(device) == UP_DEVICE_KIND_BATTERY) { ++*total; - UpDeviceState state; - g_variant_get_child (device, 4, "u", &state); + const UpDeviceState state = indicator_power_device_get_state (device); if ((state == UP_DEVICE_STATE_CHARGING) || (state == UP_DEVICE_STATE_DISCHARGING)) ++*inuse; } - - g_variant_unref (device); } g_debug("count_batteries found %d batteries (%d are charging/discharging)", *total, *inuse); diff --git a/src/indicator-power.h b/src/indicator-power.h index 39ac0ca..bd47535 100644 --- a/src/indicator-power.h +++ b/src/indicator-power.h @@ -26,6 +26,8 @@ with this program. If not, see . #include #include +#include "device.h" + G_BEGIN_DECLS #define INDICATOR_POWER_TYPE (indicator_power_get_type ()) @@ -52,4 +54,8 @@ struct _IndicatorPower GType indicator_power_get_type (void) G_GNUC_CONST; +void indicator_power_set_devices (IndicatorPower * power, + IndicatorPowerDevice ** devices, + gsize deviceCount); + G_END_DECLS diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index af494ee..3e9d97b 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -48,15 +48,32 @@ class IndicatorTest : public ::testing::Test { protected: + IndicatorPowerDevice * ac_device; + IndicatorPowerDevice * battery_device; + virtual void SetUp() { ensure_glib_initialized (); + g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); - g_message ("GSETTINGS_SCHEMA_DIR is %s", g_getenv("GSETTINGS_SCHEMA_DIR")); + + 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); } virtual void TearDown() { + g_clear_object (&battery_device); + g_clear_object (&ac_device); } }; @@ -66,10 +83,18 @@ class IndicatorTest : public ::testing::Test TEST_F(IndicatorTest, GObjectNew) { - ensure_glib_initialized (); - GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); g_object_unref (o); } + +TEST_F(IndicatorTest, SetDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + IndicatorPowerDevice * devices[] = { ac_device, battery_device }; + + indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + + g_object_unref (power); +} -- cgit v1.2.3 From 6bbfe3a79da52a5d03224bdb66faf0d41385158d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 15:02:03 -0500 Subject: make Jenkins happy --- tests/test-indicator.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 3e9d97b..c3f502a 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -72,8 +72,8 @@ class IndicatorTest : public ::testing::Test virtual void TearDown() { - g_clear_object (&battery_device); - g_clear_object (&ac_device); + g_object_unref (battery_device); + g_object_unref (ac_device); } }; -- cgit v1.2.3 From ce8268447f48ba8d8bff99d8ac402be7bb9c01d5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 15:31:16 -0500 Subject: indicator_power_set_devices() should fail gracefully when no devices are available --- src/indicator-power.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index c2ace80..59f884a 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -747,6 +747,9 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *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); + +g_message ("new primary device: %s", indicator_power_device_get_object_path(device)); + /* set icon */ device_gicons = get_device_icon (kind, state, time, device_icon); gtk_image_set_from_gicon (priv->status_image, @@ -807,9 +810,9 @@ get_devices_cb (GObject * source_object, gpointer user_data) { GError *error; - int device_count; - IndicatorPowerDevice ** devices; + int device_count = 0; GVariant * devices_container; + IndicatorPowerDevice ** devices = NULL; IndicatorPower *self = INDICATOR_POWER (user_data); /* build an array of IndicatorPowerDevices from the DBus response */ -- cgit v1.2.3 From 73d31da1baed181dee9cead8a710444f4a4363a6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 16:00:56 -0500 Subject: fix negated logic in menu_add_device() introduced in r160 --- src/indicator-power.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 59f884a..768504d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -541,7 +541,7 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) gboolean added = FALSE; const UpDeviceKind kind = indicator_power_device_get_kind (device); - if (kind == UP_DEVICE_KIND_LINE_POWER) + if (kind != UP_DEVICE_KIND_LINE_POWER) { GtkWidget *icon; GtkWidget *item; -- cgit v1.2.3 From 7ac2b78246e26f354453935301ed322eb6870c3a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 17:10:52 -0500 Subject: make indicator_power_set_devices() safe for passing in the same devices more than once --- src/indicator-power.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 768504d..6eb6904 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -145,9 +145,9 @@ dispose_devices (IndicatorPower * self) { IndicatorPowerPrivate * priv = self->priv; + g_clear_object (&priv->device); g_slist_free_full (priv->devices, g_object_unref); priv->devices = NULL; - g_clear_object (&priv->device); } static void indicator_power_dispose (GObject *object) @@ -748,8 +748,6 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) const gchar * device_icon = indicator_power_device_get_icon (device); const gdouble percentage = indicator_power_device_get_percentage (device); -g_message ("new primary device: %s", indicator_power_device_get_object_path(device)); - /* set icon */ device_gicons = get_device_icon (kind, state, time, device_icon); gtk_image_set_from_gicon (priv->status_image, @@ -780,26 +778,30 @@ indicator_power_set_devices (IndicatorPower * self, gsize device_count) { gsize i; + GSList * new_devices; IndicatorPowerPrivate * priv; g_return_if_fail (IS_INDICATOR_POWER(self)); priv = self->priv; - /* clear out the previous values */ - dispose_devices (self); - - /* get the new devices */ + /* make a reff'ed list of the new devices */ + new_devices = NULL; for (i=0; idevices = g_slist_prepend (priv->devices, g_object_ref(devices[i])); - priv->devices = g_slist_reverse (priv->devices); + new_devices = g_slist_prepend (new_devices, g_object_ref(devices[i])); + new_devices = g_slist_reverse (new_devices); - /* get the new primary device */ + /* clear out the old devices */ + dispose_devices (self); + + /* add the new ones */ + priv->devices = new_devices; priv->device = get_primary_device (priv->devices); - if (priv->device) + + /* and update ourselves based on this new data */ + if (priv->device != NULL) put_primary_device (self, priv->device); else g_message ("Couldn't find primary device"); - build_menu (self); update_visibility (self); } -- cgit v1.2.3 From 676aef0608d08a7d2086742e0dbd6b05e2d548bd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 14:16:26 -0500 Subject: first draft of adding tests for a discharging battery --- tests/test-indicator.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index c3f502a..4b39579 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -98,3 +98,38 @@ TEST_F(IndicatorTest, SetDevices) g_object_unref (power); } + +TEST_F(IndicatorTest, DischargingStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour left (50%))"); + ASSERT_STREQ(entry->name_hint, "indicator-power"); + g_list_free (entries); + + // give the indicator a discharging battery with over 12 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} -- cgit v1.2.3 From e0d1b3299edde62797fe6fa08be3740f24c7d38e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 14:35:22 -0500 Subject: add coverage for various charging/discharging/charged states --- tests/test-indicator.cc | 104 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 4b39579..ad619e2 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -110,7 +110,6 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); ASSERT_EQ (g_list_length(entries), 1); IndicatorObjectEntry * entry = static_cast(entries->data); @@ -118,16 +117,115 @@ TEST_F(IndicatorTest, DischargingStrings) ASSERT_STREQ(entry->name_hint, "indicator-power"); g_list_free (entries); + // give the indicator a discharging battery with 2 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours left (100%))"); + g_list_free (entries); + // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); indicator_power_set_devices (power, &battery_device, 1); entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); ASSERT_EQ (g_list_length(entries), 1); entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery"); + g_list_free (entries); + + // give the indicator a discharging battery with 29 seconds left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(29), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (Unknown time left (100%))"); + g_list_free (entries); + + // what happens if the time estimate isn't available + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (50%)"); + g_list_free (entries); + + // what happens if the time estimate AND percentage isn't available + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (not present)"); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, ChargingStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour to charge (50%))"); + g_list_free (entries); + + // give the indicator a discharging battery with 2 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours to charge (50%))"); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, ChargedStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (charged)"); g_list_free (entries); // cleanup -- cgit v1.2.3 From 9ee74a367606a4dd6779b0448bef237dd9e2797e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:19:38 -0500 Subject: separate the dbus org.gnome.SettingsDaemon.Power logic into a separate class --- src/Makefile.am | 5 +- src/dbus-listener.c | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/dbus-listener.h | 77 ++++++++++++++ src/indicator-power.c | 155 ++-------------------------- 4 files changed, 366 insertions(+), 147 deletions(-) create mode 100644 src/dbus-listener.c create mode 100644 src/dbus-listener.h diff --git a/src/Makefile.am b/src/Makefile.am index d14c270..c3200a7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,11 +8,14 @@ powerlibdir = $(INDICATORDIR) powerlib_LTLIBRARIES = libpower.la libpower_la_SOURCES = \ + dbus-listener.c \ device.c \ indicator-power.c noinst_HEADERS = \ - device.h + dbus-listener.h \ + device.h \ + indicator-power.h libpower_la_CFLAGS = \ $(UPOWER_CFLAGS) \ diff --git a/src/dbus-listener.c b/src/dbus-listener.c new file mode 100644 index 0000000..fe8af09 --- /dev/null +++ b/src/dbus-listener.c @@ -0,0 +1,276 @@ +/* + +Listens on DBus for Power changes and passes them to an IndicatorPower + +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +version 3.0 as published by the Free Software Foundation. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License version 3.0 for more details. + +You should have received a copy of the GNU General Public +License along with this library. If not, see +. +*/ + +#include "dbus-listener.h" +#include "indicator-power.h" + +#define DBUS_SERVICE "org.gnome.SettingsDaemon" +#define DBUS_PATH "/org/gnome/SettingsDaemon" +#define POWER_DBUS_PATH DBUS_PATH "/Power" +#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power" + +struct _IndicatorPowerDbusListenerPrivate +{ + IndicatorPower * ipower; + + GCancellable * proxy_cancel; + GDBusProxy * proxy; + guint watcher_id; +}; + +#define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) + +/* Properties */ +/* Enum for the properties so that they can be quickly found and looked up. */ +enum { + PROP_0, + PROP_INDICATOR +}; + +/* GObject stuff */ +static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass); +static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self); +static void indicator_power_dbus_listener_dispose (GObject *object); +static void indicator_power_dbus_listener_finalize (GObject *object); +static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); +static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); + +static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); + +G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE_OBJECT); + +static void +indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) +{ + GParamSpec * pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + + object_class->dispose = indicator_power_dbus_listener_dispose; + object_class->finalize = indicator_power_dbus_listener_finalize; + object_class->set_property = set_property; + object_class->get_property = get_property; + + pspec = g_param_spec_object (INDICATOR_POWER_DBUS_LISTENER_INDICATOR, + "indicator", + "The IndicatorPower to notify when power changes are received", + INDICATOR_POWER_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_INDICATOR, pspec); +} + +/* Initialize an instance */ +static void +indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) +{ + IndicatorPowerDbusListenerPrivate * priv; + + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); + priv->ipower = NULL; + + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + DBUS_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); + + self->priv = priv; +} + +static void +indicator_power_dbus_listener_dispose (GObject *object) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + g_clear_object (&priv->proxy); + g_clear_object (&priv->proxy_cancel); + g_clear_object (&priv->ipower); + + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); +} + +static void +indicator_power_dbus_listener_finalize (GObject *object) +{ + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); +} + +/*** +**** +***/ + +static void +get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_INDICATOR: + g_value_set_object (value, priv->ipower); + break; + } +} + +static void +set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + switch (prop_id) + { + case PROP_INDICATOR: + priv->ipower = g_value_dup_object (value); + break; + } +} + +/*** +**** +***/ + +static void +get_devices_cb (GObject * source_object, + GAsyncResult * res, + gpointer user_data) +{ + GError *error; + int device_count = 0; + GVariant * devices_container; + IndicatorPowerDevice ** devices = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + /* build an array of IndicatorPowerDevices from the DBus response */ + error = NULL; + devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); + if (devices_container == NULL) + { + g_message ("Couldn't get devices: %s\n", error->message); + g_error_free (error); + } + else + { + gsize i; + GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); + device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; + devices = g_new0 (IndicatorPowerDevice*, device_count); + + for (i=0; iipower != NULL) + { + indicator_power_set_devices (priv->ipower, devices, device_count); + } + + g_free (devices); +} + +static void +request_device_list (IndicatorPowerDbusListener * self) +{ + g_dbus_proxy_call (self->priv->proxy, + "GetDevices", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + self->priv->proxy_cancel, + get_devices_cb, + self); +} + +static void +receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, + GVariant *changed_properties G_GNUC_UNUSED, + GStrv invalidated_properties G_GNUC_UNUSED, + gpointer user_data) +{ + request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); +} + + +static void +service_proxy_cb (GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + GError * error = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + g_clear_object (&priv->proxy_cancel); + + if (error != NULL) { + g_error ("Error creating proxy: %s", error->message); + g_error_free (error); + return; + } + + /* we want to change the primary device changes */ + g_signal_connect (priv->proxy, + "g-properties-changed", + G_CALLBACK (receive_properties_changed), + user_data); + + /* get the initial state */ + request_device_list (self); +} + +static void +gsd_appeared_callback (GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) +{ + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy_cancel = g_cancellable_new (); + + g_dbus_proxy_new (connection, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + NULL, + name, + POWER_DBUS_PATH, + POWER_DBUS_INTERFACE, + priv->proxy_cancel, + service_proxy_cb, + self); +} diff --git a/src/dbus-listener.h b/src/dbus-listener.h new file mode 100644 index 0000000..816ecc5 --- /dev/null +++ b/src/dbus-listener.h @@ -0,0 +1,77 @@ +/* + +Listens for Power changes from org.gnome.SettingsDaemon.Power on Dbus + +Copyright 2012 Canonical Ltd. + +Authors: + Javier Jardon + 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 +. +*/ + +#ifndef __INDICATOR_POWER_DBUS_LISTENER_H__ +#define __INDICATOR_POWER_DBUS_LISTENER_H__ + +#include +#include + +G_BEGIN_DECLS + +#define INDICATOR_POWER_DBUS_LISTENER_TYPE (indicator_power_dbus_listener_get_type ()) +#define INDICATOR_POWER_DBUS_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_DBUS_LISTENER_TYPE, IndicatorPowerDbusListener)) +#define INDICATOR_POWER_DBUS_LISTENER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_DBUS_LISTENER_TYPE, IndicatorPowerDbusListenerClass)) +#define INDICATOR_IS_POWER_DBUS_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_DBUS_LISTENER_TYPE)) +#define INDICATOR_IS_POWER_DBUS_LISTENER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_DBUS_LISTENER_TYPE)) +#define INDICATOR_POWER_DBUS_LISTENER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_DBUS_LISTENER_TYPE, IndicatorPowerDbusListenerClass)) + +typedef struct _IndicatorPowerDbusListener IndicatorPowerDbusListener; +typedef struct _IndicatorPowerDbusListenerClass IndicatorPowerDbusListenerClass; +typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPrivate; + +#define INDICATOR_POWER_DBUS_LISTENER_INDICATOR "indicator-power-dbus-listener-indicator" + +/** + * IndicatorPowerDbusListenerClass: + * @parent_class: #GObjectClass + */ +struct _IndicatorPowerDbusListenerClass +{ + GObjectClass parent_class; +}; + +/** + * IndicatorPowerDbusListener: + * @parent: #GObject + * @priv: A cached reference to the private data for the instance. +*/ +struct _IndicatorPowerDbusListener +{ + GObject parent; + IndicatorPowerDbusListenerPrivate * priv; +}; + +/*** +**** +***/ + +GType indicator_power_dbus_listener_get_type (void); + +IndicatorPowerDbusListener* indicator_power_dbus_listener_new (void); + + +G_END_DECLS + +#endif diff --git a/src/indicator-power.c b/src/indicator-power.c index 6eb6904..dc95aae 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -20,7 +20,7 @@ with this program. If not, see . */ #ifdef HAVE_CONFIG_H -#include "config.h" + #include "config.h" #endif /* GStuff */ @@ -28,9 +28,7 @@ with this program. If not, see . #include #include -/* upower */ -#include - +#include "dbus-listener.h" #include "device.h" #include "indicator-power.h" @@ -38,11 +36,6 @@ with this program. If not, see . #define DEFAULT_ICON "gpm-battery-missing" -#define DBUS_SERVICE "org.gnome.SettingsDaemon" -#define DBUS_PATH "/org/gnome/SettingsDaemon" -#define POWER_DBUS_PATH DBUS_PATH "/Power" -#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power" - enum { POWER_INDICATOR_ICON_POLICY_PRESENT, POWER_INDICATOR_ICON_POLICY_CHARGE, @@ -57,9 +50,7 @@ struct _IndicatorPowerPrivate GtkImage *status_image; gchar *accessible_desc; - GCancellable *proxy_cancel; - GDBusProxy *proxy; - guint watcher_id; + IndicatorPowerDbusListener * dbus_listener; GSList * devices; IndicatorPowerDevice * device; @@ -86,7 +77,9 @@ static gboolean should_be_visible (IndicatorPower * self); static void on_entry_added (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer user_data); +/* static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); +*/ G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); @@ -119,14 +112,9 @@ indicator_power_init (IndicatorPower *self) priv->accessible_desc = NULL; - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); - + priv->dbus_listener = g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, + INDICATOR_POWER_DBUS_LISTENER_INDICATOR, self, + NULL); priv->settings = g_settings_new ("com.canonical.indicator.power"); g_signal_connect_swapped (priv->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); @@ -157,9 +145,7 @@ indicator_power_dispose (GObject *object) dispose_devices (self); - g_clear_object (&priv->proxy); - g_clear_object (&priv->proxy_cancel); - + g_clear_object (&priv->dbus_listener); g_clear_object (&priv->settings); G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); @@ -806,47 +792,6 @@ indicator_power_set_devices (IndicatorPower * self, update_visibility (self); } -static void -get_devices_cb (GObject * source_object, - GAsyncResult * res, - gpointer user_data) -{ - GError *error; - int device_count = 0; - GVariant * devices_container; - IndicatorPowerDevice ** devices = NULL; - IndicatorPower *self = INDICATOR_POWER (user_data); - - /* build an array of IndicatorPowerDevices from the DBus response */ - error = NULL; - devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); - if (devices_container == NULL) - { - g_message ("Couldn't get devices: %s\n", error->message); - g_error_free (error); - } - else - { - gsize i; - GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); - device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - devices = g_new0 (IndicatorPowerDevice*, device_count); - - for (i=0; ipriv; - - /* it's time to refresh our device list */ - g_dbus_proxy_call (priv->proxy, - "GetDevices", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - priv->proxy_cancel, - get_devices_cb, - user_data); -} - -static void -service_proxy_cb (GObject *object, - GAsyncResult *res, - gpointer user_data) -{ - IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate * priv = self->priv; - GError *error = NULL; - - priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - - g_clear_object (&priv->proxy_cancel); - - if (error != NULL) - { - g_error ("Error creating proxy: %s", error->message); - g_error_free (error); - - return; - } - - /* we want to change the primary device changes */ - g_signal_connect (priv->proxy, - "g-properties-changed", - G_CALLBACK (receive_properties_changed), - user_data); - - /* get the initial state */ - g_dbus_proxy_call (priv->proxy, - "GetDevices", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - priv->proxy_cancel, - get_devices_cb, - user_data); -} - -static void -gsd_appeared_callback (GDBusConnection *connection, - const gchar *name, - const gchar *name_owner, - gpointer user_data) -{ - IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate * priv = self->priv; - - priv->proxy_cancel = g_cancellable_new (); - - g_dbus_proxy_new (connection, - G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, - NULL, - name, - POWER_DBUS_PATH, - POWER_DBUS_INTERFACE, - priv->proxy_cancel, - service_proxy_cb, - self); -} - - - /* Grabs the label. Creates it if it doesn't exist already */ -- cgit v1.2.3 From 29ec6d23c13cbe7487232b79700c5d5406c26014 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:42:58 -0500 Subject: extract-method to simplify testing the accessible text --- tests/test-indicator.cc | 92 ++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index ad619e2..52e319f 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -75,6 +75,16 @@ class IndicatorTest : public ::testing::Test g_object_unref (battery_device); g_object_unref (ac_device); } + + const char* GetAccessibleDesc (IndicatorPower * power) const + { + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + g_assert (g_list_length(entries) == 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + const char * ret = entry->accessible_desc; + g_list_free (entries); + return ret; + } }; /*** @@ -109,13 +119,8 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour left (50%))"); - ASSERT_STREQ(entry->name_hint, "indicator-power"); - g_list_free (entries); + indicator_power_set_devices (power, &battery_device, 1); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, @@ -123,33 +128,21 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours left (100%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))"); // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery"); // give the indicator a discharging battery with 29 seconds left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(29), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (Unknown time left (100%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))"); // what happens if the time estimate isn't available g_object_set (battery_device, @@ -157,11 +150,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (50%)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)"); // what happens if the time estimate AND percentage isn't available g_object_set (battery_device, @@ -169,11 +158,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (not present)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)"); // cleanup g_object_unref (power); @@ -190,22 +175,14 @@ TEST_F(IndicatorTest, ChargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour to charge (50%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours to charge (50%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))"); // cleanup g_object_unref (power); @@ -222,12 +199,33 @@ TEST_F(IndicatorTest, ChargedStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (charged)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)"); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + 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); + + IndicatorPowerDevice * devices[] = { battery_device, bad_battery_device }; + indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)"); // cleanup g_object_unref (power); + g_object_unref (bad_battery_device); } + -- cgit v1.2.3 From b3a1b201431b649b5fb37d1c10a2609e92d06239 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:56:10 -0500 Subject: avoid a cyclical refcount dependency between IndicatorPower and its DBusListener --- src/dbus-listener.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index fe8af09..9f62f25 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -101,6 +101,20 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) self->priv = priv; } +static void +set_indicator (IndicatorPowerDbusListener * self, GObject * ipower) +{ + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + if (priv->ipower != NULL) + g_object_remove_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); + + priv->ipower = INDICATOR_POWER(ipower); + + if (priv->ipower != NULL) + g_object_add_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); +}; + static void indicator_power_dbus_listener_dispose (GObject *object) { @@ -109,7 +123,8 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->proxy_cancel); - g_clear_object (&priv->ipower); + + set_indicator (self, NULL); G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } @@ -142,12 +157,11 @@ static void set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) { IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - IndicatorPowerDbusListenerPrivate * priv = self->priv; switch (prop_id) { case PROP_INDICATOR: - priv->ipower = g_value_dup_object (value); + set_indicator (self, g_value_get_object(value)); break; } } -- cgit v1.2.3 From 5937352499a753898e36b53512f53ac1781034bd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:11:03 -0500 Subject: reuse the same cancellable across multiple non-concurrent dbus calls --- src/dbus-listener.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 9f62f25..5539098 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -33,7 +33,7 @@ struct _IndicatorPowerDbusListenerPrivate { IndicatorPower * ipower; - GCancellable * proxy_cancel; + GCancellable * cancellable; GDBusProxy * proxy; guint watcher_id; }; @@ -85,18 +85,21 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) { IndicatorPowerDbusListenerPrivate * priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_DBUS_LISTENER_TYPE, - IndicatorPowerDbusListenerPrivate); - priv->ipower = NULL; + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); + priv->ipower = NULL; + + priv->cancellable = g_cancellable_new (); + + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + DBUS_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); self->priv = priv; } @@ -122,7 +125,7 @@ indicator_power_dbus_listener_dispose (GObject *object) IndicatorPowerDbusListenerPrivate * priv = self->priv; g_clear_object (&priv->proxy); - g_clear_object (&priv->proxy_cancel); + g_clear_object (&priv->cancellable); set_indicator (self, NULL); @@ -224,7 +227,7 @@ request_device_list (IndicatorPowerDbusListener * self) NULL, G_DBUS_CALL_FLAGS_NONE, -1, - self->priv->proxy_cancel, + self->priv->cancellable, get_devices_cb, self); } @@ -249,7 +252,6 @@ service_proxy_cb (GObject *object, IndicatorPowerDbusListenerPrivate * priv = self->priv; priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - g_clear_object (&priv->proxy_cancel); if (error != NULL) { g_error ("Error creating proxy: %s", error->message); @@ -276,15 +278,13 @@ gsd_appeared_callback (GDBusConnection *connection, IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); IndicatorPowerDbusListenerPrivate * priv = self->priv; - priv->proxy_cancel = g_cancellable_new (); - g_dbus_proxy_new (connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, name, POWER_DBUS_PATH, POWER_DBUS_INTERFACE, - priv->proxy_cancel, + priv->cancellable, service_proxy_cb, self); } -- cgit v1.2.3 From 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(-) 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(-) 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 aeb6ab59af1f325261db754aade493a949d5a1e3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:26:02 -0500 Subject: call g_object_run_dispose() in a standalone test to get coverage on the NULL / non-NULL branches of dispose()'s g_clear_pointer() calls --- tests/test-device.cc | 1 + tests/test-indicator.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/test-device.cc b/tests/test-device.cc index 388460e..132d762 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -45,6 +45,7 @@ TEST(DeviceTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); } diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 52e319f..dedf54c 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -96,6 +96,7 @@ TEST_F(IndicatorTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); } -- 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(-) 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 eda15ff15c3603deb748daaa59c9b28a576a9c4d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 17:10:48 -0500 Subject: add coverage for DBusListener get_property() --- tests/test-indicator.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index dedf54c..2229d77 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -19,6 +19,7 @@ with this program. If not, see . #include +#include "dbus-listener.h" #include "device.h" #include "indicator-power.h" @@ -230,3 +231,19 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) g_object_unref (bad_battery_device); } +TEST_F(IndicatorTest, DbusListenerGetProperty) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GObject * dbus_listener = G_OBJECT(g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, + INDICATOR_POWER_DBUS_LISTENER_INDICATOR, power, + NULL)); + GObject * indicator = NULL; + g_object_get (dbus_listener, + INDICATOR_POWER_DBUS_LISTENER_INDICATOR, &indicator, + NULL); + ASSERT_EQ(INDICATOR_POWER(indicator), power); + + // cleanup + g_object_unref (dbus_listener); + g_object_unref (power); +} -- cgit v1.2.3 From 2f1281d4faa6bed2a0be484bd722504df290880c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 17:49:13 -0500 Subject: add coverage for charging with >1 minute but <1 hour left --- tests/test-indicator.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 2229d77..5875df6 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -115,6 +115,15 @@ TEST_F(IndicatorTest, DischargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + // give the indicator a discharging battery with 30 minutes of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*30), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))"); + // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, -- cgit v1.2.3 From 675cc7955e0e8834a3ea4bb46fb347bb744f24a7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 27 May 2012 08:35:52 -0500 Subject: Add skeleton test for IndicatorPowerDbusListener --- tests/Makefile.am | 6 ++++ tests/test-dbus-listener.cc | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/test-dbus-listener.cc diff --git a/tests/Makefile.am b/tests/Makefile.am index b61bbe0..b6b543b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -45,6 +45,12 @@ test_device_SOURCES = test-device.cc test_device_LDADD = $(TEST_LIBS) test_device_CPPFLAGS = $(TEST_CPPFLAGS) +TESTS += test-dbus-listener +check_PROGRAMS += test-dbus-listener +test_dbus_listener_SOURCES = test-dbus-listener.cc +test_dbus_listener_LDADD = $(TEST_LIBS) +test_dbus_listener_CPPFLAGS = $(TEST_CPPFLAGS) + TESTS += test-indicator check_PROGRAMS += test-indicator test_indicator_SOURCES = test-indicator.cc diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc new file mode 100644 index 0000000..869d08a --- /dev/null +++ b/tests/test-dbus-listener.cc @@ -0,0 +1,73 @@ +/* +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#include + +#include "dbus-listener.h" +#include "device.h" + +/*** +**** +***/ + +namespace +{ + void ensure_glib_initialized () + { + static bool initialized = false; + + if (G_UNLIKELY(!initialized)) + { + initialized = true; + g_type_init(); + } + } +} + +/*** +**** +***/ + +class DbusListenerTest : public ::testing::Test +{ + protected: + + virtual void SetUp() + { + ensure_glib_initialized (); + } + + virtual void TearDown() + { + } +}; + +/*** +**** +***/ + +TEST_F(DbusListenerTest, GObjectNew) +{ + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o)); + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls + g_object_unref (o); +} + -- cgit v1.2.3 From 23a2672a1a763cdef67ccc5cb9f8fec7daf021be Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 27 May 2012 09:22:28 -0500 Subject: use signals to decouple i-power and dbus-listener --- src/dbus-listener.c | 100 +++++++++++------------------------------------- src/dbus-listener.h | 5 ++- src/indicator-power.c | 32 ++++++---------- src/indicator-power.h | 3 +- tests/test-indicator.cc | 60 ++++++++++++++--------------- 5 files changed, 66 insertions(+), 134 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 5539098..6a85fd4 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -22,7 +22,7 @@ License along with this library. If not, see */ #include "dbus-listener.h" -#include "indicator-power.h" +#include "device.h" #define DBUS_SERVICE "org.gnome.SettingsDaemon" #define DBUS_PATH "/org/gnome/SettingsDaemon" @@ -31,8 +31,6 @@ License along with this library. If not, see struct _IndicatorPowerDbusListenerPrivate { - IndicatorPower * ipower; - GCancellable * cancellable; GDBusProxy * proxy; guint watcher_id; @@ -40,20 +38,19 @@ struct _IndicatorPowerDbusListenerPrivate #define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) -/* Properties */ -/* Enum for the properties so that they can be quickly found and looked up. */ +/* Signals */ enum { - PROP_0, - PROP_INDICATOR + SIGNAL_DEVICES, + SIGNAL_LAST }; +static guint signals[SIGNAL_LAST] = { 0 }; + /* GObject stuff */ static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass); static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self); static void indicator_power_dbus_listener_dispose (GObject *object); static void indicator_power_dbus_listener_finalize (GObject *object); -static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* ); -static void get_property (GObject*, guint prop_id, GValue*, GParamSpec* ); static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); @@ -62,21 +59,21 @@ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) { - GParamSpec * pspec; GObjectClass *object_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + /* methods */ object_class->dispose = indicator_power_dbus_listener_dispose; object_class->finalize = indicator_power_dbus_listener_finalize; - object_class->set_property = set_property; - object_class->get_property = get_property; - - pspec = g_param_spec_object (INDICATOR_POWER_DBUS_LISTENER_INDICATOR, - "indicator", - "The IndicatorPower to notify when power changes are received", - INDICATOR_POWER_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, PROP_INDICATOR, pspec); + + /* signals */ + signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_TYPE_FROM_CLASS(klass), 0, + G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); } /* Initialize an instance */ @@ -89,8 +86,6 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) INDICATOR_POWER_DBUS_LISTENER_TYPE, IndicatorPowerDbusListenerPrivate); - priv->ipower = NULL; - priv->cancellable = g_cancellable_new (); priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, @@ -104,20 +99,6 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) self->priv = priv; } -static void -set_indicator (IndicatorPowerDbusListener * self, GObject * ipower) -{ - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - if (priv->ipower != NULL) - g_object_remove_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); - - priv->ipower = INDICATOR_POWER(ipower); - - if (priv->ipower != NULL) - g_object_add_weak_pointer (G_OBJECT(priv->ipower), (gpointer*)&priv->ipower); -}; - static void indicator_power_dbus_listener_dispose (GObject *object) { @@ -127,8 +108,6 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->cancellable); - set_indicator (self, NULL); - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } @@ -142,48 +121,15 @@ indicator_power_dbus_listener_finalize (GObject *object) **** ***/ -static void -get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) -{ - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - switch (prop_id) - { - case PROP_INDICATOR: - g_value_set_object (value, priv->ipower); - break; - } -} - -static void -set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec) -{ - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(o); - - switch (prop_id) - { - case PROP_INDICATOR: - set_indicator (self, g_value_get_object(value)); - break; - } -} - -/*** -**** -***/ - static void get_devices_cb (GObject * source_object, GAsyncResult * res, gpointer user_data) { GError *error; - int device_count = 0; + GSList * devices = NULL; GVariant * devices_container; - IndicatorPowerDevice ** devices = NULL; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -197,26 +143,24 @@ get_devices_cb (GObject * source_object, { gsize i; GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); - device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - devices = g_new0 (IndicatorPowerDevice*, device_count); + const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; for (i=0; iipower != NULL) - { - indicator_power_set_devices (priv->ipower, devices, device_count); - } + g_signal_emit (self, signals[SIGNAL_DEVICES], (GQuark)0, devices); - g_free (devices); + g_slist_free_full (devices, g_object_unref); + } static void diff --git a/src/dbus-listener.h b/src/dbus-listener.h index 816ecc5..2dc0865 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -41,7 +41,8 @@ typedef struct _IndicatorPowerDbusListener IndicatorPowerDbusListener; typedef struct _IndicatorPowerDbusListenerClass IndicatorPowerDbusListenerClass; typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPrivate; -#define INDICATOR_POWER_DBUS_LISTENER_INDICATOR "indicator-power-dbus-listener-indicator" +/* signals */ +#define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "indicator-power-dbus-listener-devices-enumerated" /** * IndicatorPowerDbusListenerClass: @@ -50,6 +51,8 @@ typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPriv struct _IndicatorPowerDbusListenerClass { GObjectClass parent_class; + + void (* devices_enumerated) (IndicatorPowerDbusListener*, GSList * devices); }; /** diff --git a/src/indicator-power.c b/src/indicator-power.c index cf96619..6690f9b 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -116,9 +116,10 @@ indicator_power_init (IndicatorPower *self) priv->accessible_desc = NULL; - priv->dbus_listener = g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, self, - NULL); + priv->dbus_listener = g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL); + g_signal_connect_swapped (priv->dbus_listener, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(indicator_power_set_devices), self); + priv->settings = g_settings_new ("com.canonical.indicator.power"); g_signal_connect_swapped (priv->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); @@ -763,33 +764,22 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) } void -indicator_power_set_devices (IndicatorPower * self, - IndicatorPowerDevice ** devices, - gsize device_count) +indicator_power_set_devices (IndicatorPower * self, GSList * devices) { - gsize i; - GSList * new_devices; IndicatorPowerPrivate * priv; -/* LCOV_EXCL_START */ + /* LCOV_EXCL_START */ g_return_if_fail (IS_INDICATOR_POWER(self)); -/* LCOV_EXCL_STOP */ + /* LCOV_EXCL_STOP */ priv = self->priv; - /* make a reff'ed list of the new devices */ - new_devices = NULL; - for (i=0; idevices = new_devices; + priv->devices = g_slist_copy (devices); priv->device = get_primary_device (priv->devices); - /* and update ourselves based on this new data */ + /* and our menus/visibility from the new device list */ if (priv->device != NULL) put_primary_device (self, priv->device); else diff --git a/src/indicator-power.h b/src/indicator-power.h index bd47535..69ed888 100644 --- a/src/indicator-power.h +++ b/src/indicator-power.h @@ -55,7 +55,6 @@ struct _IndicatorPower GType indicator_power_get_type (void) G_GNUC_CONST; void indicator_power_set_devices (IndicatorPower * power, - IndicatorPowerDevice ** devices, - gsize deviceCount); + GSList * devices); G_END_DECLS diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 5875df6..50ae2c1 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -103,17 +103,22 @@ TEST_F(IndicatorTest, GObjectNew) TEST_F(IndicatorTest, SetDevices) { + GSList * devices; IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); - IndicatorPowerDevice * devices[] = { ac_device, battery_device }; - indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); - + devices = NULL; + devices = g_slist_append (devices, ac_device); + devices = g_slist_append (devices, battery_device); + indicator_power_set_devices (power, devices); + g_slist_free (devices); + g_object_unref (power); } TEST_F(IndicatorTest, DischargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_append (NULL, battery_device); // give the indicator a discharging battery with 30 minutes of life left g_object_set (battery_device, @@ -121,7 +126,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*30), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))"); // give the indicator a discharging battery with 1 hour of life left @@ -130,7 +135,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))"); // give the indicator a discharging battery with 2 hours of life left @@ -138,21 +143,21 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))"); // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery"); // give the indicator a discharging battery with 29 seconds left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(29), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))"); // what happens if the time estimate isn't available @@ -160,7 +165,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)"); // what happens if the time estimate AND percentage isn't available @@ -168,16 +173,18 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)"); // cleanup + g_slist_free (devices); g_object_unref (power); } TEST_F(IndicatorTest, ChargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_prepend (NULL, battery_device); // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, @@ -185,23 +192,25 @@ TEST_F(IndicatorTest, ChargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))"); // cleanup + g_slist_free (devices); g_object_unref (power); } TEST_F(IndicatorTest, ChargedStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_append (NULL, battery_device); // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, @@ -209,10 +218,11 @@ TEST_F(IndicatorTest, ChargedStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, INDICATOR_POWER_DEVICE_TIME, guint64(0), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)"); // cleanup + g_slist_free (devices); g_object_unref (power); } @@ -231,28 +241,14 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 53, UP_DEVICE_STATE_CHARGING, 0); - IndicatorPowerDevice * devices[] = { battery_device, bad_battery_device }; - indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + GSList * devices = NULL; + devices = g_slist_append (devices, battery_device); + devices = g_slist_append (devices, bad_battery_device); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)"); // cleanup + g_slist_free (devices); g_object_unref (power); g_object_unref (bad_battery_device); } - -TEST_F(IndicatorTest, DbusListenerGetProperty) -{ - IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); - GObject * dbus_listener = G_OBJECT(g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, power, - NULL)); - GObject * indicator = NULL; - g_object_get (dbus_listener, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, &indicator, - NULL); - ASSERT_EQ(INDICATOR_POWER(indicator), power); - - // cleanup - g_object_unref (dbus_listener); - g_object_unref (power); -} -- cgit v1.2.3 From b9a6194c12bf39fb6317edc1126ea32e890da583 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 May 2012 11:33:07 -0500 Subject: add a mock GSD.Power to handle the GetPower requests --- src/dbus-listener.c | 11 +-- src/dbus-listener.h | 5 ++ tests/test-dbus-listener.cc | 170 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 177 insertions(+), 9 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 6a85fd4..a7a29ee 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -24,11 +24,6 @@ License along with this library. If not, see #include "dbus-listener.h" #include "device.h" -#define DBUS_SERVICE "org.gnome.SettingsDaemon" -#define DBUS_PATH "/org/gnome/SettingsDaemon" -#define POWER_DBUS_PATH DBUS_PATH "/Power" -#define POWER_DBUS_INTERFACE "org.gnome.SettingsDaemon.Power" - struct _IndicatorPowerDbusListenerPrivate { GCancellable * cancellable; @@ -89,7 +84,7 @@ indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) priv->cancellable = g_cancellable_new (); priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, + GSD_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, gsd_appeared_callback, NULL, @@ -226,8 +221,8 @@ gsd_appeared_callback (GDBusConnection *connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, name, - POWER_DBUS_PATH, - POWER_DBUS_INTERFACE, + GSD_POWER_DBUS_PATH, + GSD_POWER_DBUS_INTERFACE, priv->cancellable, service_proxy_cb, self); diff --git a/src/dbus-listener.h b/src/dbus-listener.h index 2dc0865..d1ab993 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -41,6 +41,11 @@ typedef struct _IndicatorPowerDbusListener IndicatorPowerDbusListener; typedef struct _IndicatorPowerDbusListenerClass IndicatorPowerDbusListenerClass; typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPrivate; +#define GSD_SERVICE "org.gnome.SettingsDaemon" +#define GSD_PATH "/org/gnome/SettingsDaemon" +#define GSD_POWER_DBUS_INTERFACE GSD_SERVICE ".Power" +#define GSD_POWER_DBUS_PATH GSD_PATH "/Power" + /* signals */ #define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "indicator-power-dbus-listener-devices-enumerated" diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc index 869d08a..2cfe4a7 100644 --- a/tests/test-dbus-listener.cc +++ b/tests/test-dbus-listener.cc @@ -46,15 +46,33 @@ namespace class DbusListenerTest : public ::testing::Test { - protected: + public: + + GSList * devices; + GDBusConnection * connection; + GMainLoop * mainloop; + GTestDBus * bus; + bool bus_acquired; + bool name_acquired; virtual void SetUp() { + bus_acquired = false; + name_acquired = false; + connection = NULL; + devices = NULL; + ensure_glib_initialized (); + mainloop = g_main_loop_new (NULL, FALSE); + bus = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_up (bus); } virtual void TearDown() { + g_slist_free_full (devices, g_object_unref); + g_test_dbus_down (bus); + g_main_loop_unref (mainloop); } }; @@ -62,6 +80,7 @@ class DbusListenerTest : public ::testing::Test **** ***/ +/* TEST_F(DbusListenerTest, GObjectNew) { GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); @@ -70,4 +89,153 @@ TEST_F(DbusListenerTest, GObjectNew) g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); } +*/ +static void +on_bus_acquired (GDBusConnection *conn, const gchar * name, gpointer gself) +{ + g_debug ("bus acquired: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->connection = conn; + test->bus_acquired = true; +} + +static void +on_name_acquired (GDBusConnection * conn, const gchar * name, gpointer gself) +{ + g_debug ("name acquired: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = true; + g_main_loop_quit (test->mainloop); +} + +static void +on_name_lost (GDBusConnection * conn, const gchar * name, gpointer gself) +{ + g_debug ("name lost: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = false; + g_main_loop_quit (test->mainloop); +} + +static void +on_devices_enumerated (IndicatorPowerDbusListener * l, GSList * devices, gpointer gself) +{ + g_debug ("on_devices_enumerated"); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = false; + g_slist_foreach (devices, (GFunc)g_object_ref, NULL); + test->devices = g_slist_copy (devices); + g_main_loop_quit (test->mainloop); +} + +static void +gsd_power_handle_method_call (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + if (!g_strcmp0 (method_name, "GetDevices")) + { + /* create builder */ + GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)")); + + g_variant_builder_add (builder, "(susdut)", + "/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); + g_variant_builder_add (builder, "(susdut)", + "/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); + + GVariant * value = g_variant_builder_end (builder); + GVariant * tuple = g_variant_new_tuple (&value, 1); + g_dbus_method_invocation_return_value (invocation, tuple); + g_variant_builder_unref (builder); + } +} + +static const GDBusInterfaceVTable gsd_power_interface_vtable = +{ + gsd_power_handle_method_call, + NULL, /* GetProperty */ + NULL, /* SetProperty */ +}; + +TEST_F(DbusListenerTest, GnomeSettingsDaemon) +{ + // own org.gnome.SettingsDameon on the bus + const int name_ownership_id = g_bus_own_name ( + G_BUS_TYPE_SESSION, + GSD_SERVICE, + G_BUS_NAME_OWNER_FLAGS_NONE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + this, NULL); + ASSERT_FALSE (bus_acquired); + ASSERT_FALSE (name_acquired); + ASSERT_TRUE (connection == NULL); + g_main_loop_run (mainloop); + ASSERT_TRUE (bus_acquired); + ASSERT_TRUE (name_acquired); + ASSERT_TRUE (G_IS_DBUS_CONNECTION(connection)); + + // register a mock org.gnome.SettingsDaemon.Power interface + const gchar introspection_xml[] = "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + GDBusNodeInfo * introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL); + ASSERT_TRUE (introspection_data != NULL); + const int registration_id = g_dbus_connection_register_object (connection, + GSD_POWER_DBUS_PATH, + introspection_data->interfaces[0], + &gsd_power_interface_vtable, + NULL, NULL, NULL); + + // create an i-power dbus listener to watch for GSD + ASSERT_EQ (g_slist_length(devices), 0); + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o)); + g_signal_connect(o, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(on_devices_enumerated), this); + g_main_loop_run (mainloop); + + // test the devices should have gotten + ASSERT_EQ (g_slist_length(devices), 2); + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 0)); + ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_LINE_POWER); + ASSERT_STREQ ("/org/freedesktop/UPower/devices/line_power_AC", indicator_power_device_get_object_path(device)); + device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 1)); + ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_STREQ ("/org/freedesktop/UPower/devices/battery_BAT0", indicator_power_device_get_object_path(device)); + + // cleanup + g_object_unref (o); + g_dbus_connection_unregister_object (connection, registration_id); + g_bus_unown_name (name_ownership_id); +} -- cgit v1.2.3 From 7b8cd0982a266d589a7dcc0a76b04776d72e1bf4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 May 2012 19:37:43 -0500 Subject: add more tests to improve coverage: different device types, no primary device --- tests/test-dbus-listener.cc | 1 + tests/test-indicator.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc index 2cfe4a7..62caeb0 100644 --- a/tests/test-dbus-listener.cc +++ b/tests/test-dbus-listener.cc @@ -235,6 +235,7 @@ TEST_F(DbusListenerTest, GnomeSettingsDaemon) ASSERT_STREQ ("/org/freedesktop/UPower/devices/battery_BAT0", indicator_power_device_get_object_path(device)); // cleanup + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); g_dbus_connection_unregister_object (connection, registration_id); g_bus_unown_name (name_ownership_id); diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 50ae2c1..32d6f4f 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -252,3 +252,64 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) g_object_unref (power); g_object_unref (bad_battery_device); } + +TEST_F(IndicatorTest, OtherDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + g_object_ref (battery_device); + GSList * devices = g_slist_append (NULL, battery_device); + + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + + indicator_power_set_devices (power, devices); + + // FIXME: this tests to confirm the code doesn't crash, + // but further tests would be helpful + + // cleanup + g_slist_free_full (devices, g_object_unref); + g_object_unref (power); +} + +TEST_F(IndicatorTest, NoDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + indicator_power_set_devices (power, NULL); + + // FIXME: this tests to confirm the code doesn't crash, + // but further tests would be helpful + + // cleanup + g_object_unref (power); +} -- cgit v1.2.3 From 7f08c9a317a964aaf859df9489a7361fb60ecd42 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 13:28:47 -0500 Subject: exclude G_DEFINE_TYPE from coverage testing in dbus-listener.c --- src/dbus-listener.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index a7a29ee..feaf78e 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -49,7 +49,9 @@ static void indicator_power_dbus_listener_finalize (GObject *object); static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); +/* LCOV_EXCL_START */ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE_OBJECT); +/* LCOV_EXCL_STOP */ static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) -- cgit v1.2.3 From 8a37ae27c57386bb321f6d97cfc9883d03b9027a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 13:29:32 -0500 Subject: add tests for gsd GetDevices returning (a) no devices and (b) an error --- tests/test-dbus-listener.cc | 326 ++++++++++++++++++++++++++------------------ 1 file changed, 196 insertions(+), 130 deletions(-) diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc index 62caeb0..4c6c607 100644 --- a/tests/test-dbus-listener.cc +++ b/tests/test-dbus-listener.cc @@ -46,14 +46,87 @@ namespace class DbusListenerTest : public ::testing::Test { - public: - + protected: GSList * devices; GDBusConnection * connection; GMainLoop * mainloop; GTestDBus * bus; + GDBusNodeInfo * gsd_power_introspection_data; + GVariant * get_devices_retval; bool bus_acquired; bool name_acquired; + int gsd_name_ownership_id; + int gsd_power_registration_id; + char * gsd_power_error_string; + + protected: + + static void + on_bus_acquired (GDBusConnection *conn, const gchar * name, gpointer gself) + { + g_debug ("bus acquired: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->connection = conn; + test->bus_acquired = true; + } + + static void + on_name_acquired (GDBusConnection * conn, const gchar * name, gpointer gself) + { + g_debug ("name acquired: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = true; + g_main_loop_quit (test->mainloop); + } + + static void + on_name_lost (GDBusConnection * conn, const gchar * name, gpointer gself) + { + g_debug ("name lost: %s, connection is %p", name, conn); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = false; + g_main_loop_quit (test->mainloop); + } + + protected: + + static void + on_devices_enumerated (IndicatorPowerDbusListener * l, GSList * devices, gpointer gself) + { + g_debug ("on_devices_enumerated"); + + DbusListenerTest * test = static_cast (gself); + test->name_acquired = false; + g_slist_foreach (devices, (GFunc)g_object_ref, NULL); + test->devices = g_slist_copy (devices); + g_main_loop_quit (test->mainloop); + } + + static void + gsd_power_handle_method_call (GDBusConnection * connection, + const gchar * sender, + const gchar * object_path, + const gchar * interface_name, + const gchar * method_name, + GVariant * parameters, + GDBusMethodInvocation * invocation, + gpointer gself) + { + DbusListenerTest * test = static_cast (gself); + g_assert (!g_strcmp0 (method_name, "GetDevices")); + if (test->gsd_power_error_string != NULL) { + const GQuark domain = g_quark_from_static_string ("mock-gsd-power"); + g_message ("returning an error"); + g_dbus_method_invocation_return_error_literal (invocation, domain, 0, test->gsd_power_error_string); + } else { + g_dbus_method_invocation_return_value (invocation, test->get_devices_retval); + } + } + + protected: virtual void SetUp() { @@ -61,15 +134,72 @@ class DbusListenerTest : public ::testing::Test name_acquired = false; connection = NULL; devices = NULL; + get_devices_retval = NULL; + gsd_power_error_string = NULL; + // bring up the test bus ensure_glib_initialized (); mainloop = g_main_loop_new (NULL, FALSE); bus = g_test_dbus_new (G_TEST_DBUS_NONE); g_test_dbus_up (bus); + + // own org.gnome.SettingsDameon on this test bus + gsd_name_ownership_id = g_bus_own_name ( + G_BUS_TYPE_SESSION, + GSD_SERVICE, + G_BUS_NAME_OWNER_FLAGS_NONE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + this, NULL); + ASSERT_FALSE (bus_acquired); + ASSERT_FALSE (name_acquired); + ASSERT_TRUE (connection == NULL); + g_main_loop_run (mainloop); + ASSERT_TRUE (bus_acquired); + ASSERT_TRUE (name_acquired); + ASSERT_TRUE (G_IS_DBUS_CONNECTION(connection)); + + // parse the org.gnome.SettingsDaemon.Power interface + const gchar introspection_xml[] = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + gsd_power_introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL); + ASSERT_TRUE (gsd_power_introspection_data != NULL); + + // Set up a mock GSD. + // All it really does is wait for calls to GetDevice and + // returns the get_devices_retval variant + const GDBusInterfaceVTable gsd_power_interface_vtable = { + gsd_power_handle_method_call, + NULL, /* GetProperty */ + NULL, /* SetProperty */ + }; + gsd_power_registration_id = g_dbus_connection_register_object (connection, + GSD_POWER_DBUS_PATH, + gsd_power_introspection_data->interfaces[0], + &gsd_power_interface_vtable, + this, NULL, NULL); } virtual void TearDown() { + g_free (gsd_power_error_string); + + g_dbus_connection_unregister_object (connection, gsd_power_registration_id); + + g_dbus_node_info_unref (gsd_power_introspection_data); + g_bus_unown_name (gsd_name_ownership_id); + g_slist_free_full (devices, g_object_unref); g_test_dbus_down (bus); g_main_loop_unref (mainloop); @@ -80,141 +210,82 @@ class DbusListenerTest : public ::testing::Test **** ***/ -/* -TEST_F(DbusListenerTest, GObjectNew) + +TEST_F(DbusListenerTest, GSDHasPowerAndBattery) { + // build a GetDevices retval that shows an AC line and a discharging battery + GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)")); + g_variant_builder_add (builder, "(susdut)", + "/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); + g_variant_builder_add (builder, "(susdut)", + "/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); + GVariant * value = g_variant_builder_end (builder); + get_devices_retval = g_variant_new_tuple (&value, 1); + g_variant_builder_unref (builder); + + // create an i-power dbus listener to watch for GSD + ASSERT_EQ (g_slist_length(devices), 0); GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o)); - g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls - g_object_unref (o); -} -*/ - -static void -on_bus_acquired (GDBusConnection *conn, const gchar * name, gpointer gself) -{ - g_debug ("bus acquired: %s, connection is %p", name, conn); - - DbusListenerTest * test = static_cast (gself); - test->connection = conn; - test->bus_acquired = true; -} + g_signal_connect(o, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(on_devices_enumerated), this); + g_main_loop_run (mainloop); -static void -on_name_acquired (GDBusConnection * conn, const gchar * name, gpointer gself) -{ - g_debug ("name acquired: %s, connection is %p", name, conn); + // test the devices should have gotten + ASSERT_EQ (g_slist_length(devices), 2); + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 0)); + ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_LINE_POWER); + ASSERT_STREQ ("/org/freedesktop/UPower/devices/line_power_AC", indicator_power_device_get_object_path(device)); + device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 1)); + ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_STREQ ("/org/freedesktop/UPower/devices/battery_BAT0", indicator_power_device_get_object_path(device)); - DbusListenerTest * test = static_cast (gself); - test->name_acquired = true; - g_main_loop_quit (test->mainloop); + // cleanup + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls + g_object_unref (o); } -static void -on_name_lost (GDBusConnection * conn, const gchar * name, gpointer gself) +TEST_F(DbusListenerTest, GSDHasNoDevices) { - g_debug ("name lost: %s, connection is %p", name, conn); + GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)")); + GVariant * value = g_variant_builder_end (builder); + get_devices_retval = g_variant_new_tuple (&value, 1); + g_variant_builder_unref (builder); - DbusListenerTest * test = static_cast (gself); - test->name_acquired = false; - g_main_loop_quit (test->mainloop); -} + // create an i-power dbus listener to watch for GSD + ASSERT_EQ (g_slist_length(devices), 0); + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o)); + g_signal_connect(o, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(on_devices_enumerated), this); + g_main_loop_run (mainloop); -static void -on_devices_enumerated (IndicatorPowerDbusListener * l, GSList * devices, gpointer gself) -{ - g_debug ("on_devices_enumerated"); + // test the devices should have gotten + ASSERT_EQ (g_slist_length(devices), 0); - DbusListenerTest * test = static_cast (gself); - test->name_acquired = false; - g_slist_foreach (devices, (GFunc)g_object_ref, NULL); - test->devices = g_slist_copy (devices); - g_main_loop_quit (test->mainloop); -} + // FIXME: this test improves coverage and confirms that the code + // doesn't crash, but more meaningful tests would be better too. -static void -gsd_power_handle_method_call (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - if (!g_strcmp0 (method_name, "GetDevices")) - { - /* create builder */ - GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)")); - - g_variant_builder_add (builder, "(susdut)", - "/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); - g_variant_builder_add (builder, "(susdut)", - "/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); - - GVariant * value = g_variant_builder_end (builder); - GVariant * tuple = g_variant_new_tuple (&value, 1); - g_dbus_method_invocation_return_value (invocation, tuple); - g_variant_builder_unref (builder); - } + // cleanup + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls + g_object_unref (o); } -static const GDBusInterfaceVTable gsd_power_interface_vtable = +TEST_F(DbusListenerTest, GSDReturnsError) { - gsd_power_handle_method_call, - NULL, /* GetProperty */ - NULL, /* SetProperty */ -}; - -TEST_F(DbusListenerTest, GnomeSettingsDaemon) -{ - // own org.gnome.SettingsDameon on the bus - const int name_ownership_id = g_bus_own_name ( - G_BUS_TYPE_SESSION, - GSD_SERVICE, - G_BUS_NAME_OWNER_FLAGS_NONE, - on_bus_acquired, - on_name_acquired, - on_name_lost, - this, NULL); - ASSERT_FALSE (bus_acquired); - ASSERT_FALSE (name_acquired); - ASSERT_TRUE (connection == NULL); - g_main_loop_run (mainloop); - ASSERT_TRUE (bus_acquired); - ASSERT_TRUE (name_acquired); - ASSERT_TRUE (G_IS_DBUS_CONNECTION(connection)); - - // register a mock org.gnome.SettingsDaemon.Power interface - const gchar introspection_xml[] = "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - GDBusNodeInfo * introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL); - ASSERT_TRUE (introspection_data != NULL); - const int registration_id = g_dbus_connection_register_object (connection, - GSD_POWER_DBUS_PATH, - introspection_data->interfaces[0], - &gsd_power_interface_vtable, - NULL, NULL, NULL); + gsd_power_error_string = g_strdup ("no devices for you lol"); // create an i-power dbus listener to watch for GSD ASSERT_EQ (g_slist_length(devices), 0); @@ -226,17 +297,12 @@ TEST_F(DbusListenerTest, GnomeSettingsDaemon) g_main_loop_run (mainloop); // test the devices should have gotten - ASSERT_EQ (g_slist_length(devices), 2); - IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 0)); - ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_LINE_POWER); - ASSERT_STREQ ("/org/freedesktop/UPower/devices/line_power_AC", indicator_power_device_get_object_path(device)); - device = INDICATOR_POWER_DEVICE (g_slist_nth_data (devices, 1)); - ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); - ASSERT_STREQ ("/org/freedesktop/UPower/devices/battery_BAT0", indicator_power_device_get_object_path(device)); + ASSERT_EQ (g_slist_length(devices), 0); + + // FIXME: this test improves coverage and confirms that the code + // doesn't crash, but more meaningful tests would be better too. // cleanup g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); - g_dbus_connection_unregister_object (connection, registration_id); - g_bus_unown_name (name_ownership_id); } -- 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(-) 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 5786ecae0d9ecae7aaf2b19094474db54e53a691 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 14:41:27 -0500 Subject: t^Cak to indicator_set_power_differences() --- src/indicator-power.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 6690f9b..7029739 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -766,12 +766,11 @@ put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) void indicator_power_set_devices (IndicatorPower * self, GSList * devices) { - IndicatorPowerPrivate * priv; - /* LCOV_EXCL_START */ g_return_if_fail (IS_INDICATOR_POWER(self)); /* LCOV_EXCL_STOP */ - priv = self->priv; + + IndicatorPowerPrivate * priv = self->priv; /* update our devices & primary device */ g_slist_foreach (devices, (GFunc)g_object_ref, NULL); -- cgit v1.2.3 From ab87d8f8eaf75dda49dbd6e9ef56bf94dcf98e28 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 May 2012 17:28:15 -0500 Subject: add a test to make sure that the listener responds to a PropertiesChanged signal --- src/dbus-listener.c | 5 ++++ tests/test-dbus-listener.cc | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index feaf78e..bc31ee3 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -127,6 +127,7 @@ get_devices_cb (GObject * source_object, GSList * devices = NULL; GVariant * devices_container; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); +g_message ("%s", G_STRLOC); /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -163,6 +164,7 @@ get_devices_cb (GObject * source_object, static void request_device_list (IndicatorPowerDbusListener * self) { +g_message ("calling GetDevices... self %p priv %p proxy %p", self, self->priv, self->priv->proxy); g_dbus_proxy_call (self->priv->proxy, "GetDevices", NULL, @@ -179,6 +181,8 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { +g_message ("properties changed: %p %p %p %p", proxy, changed_properties, invalidated_properties, user_data); +g_message ("hey someone told me the properties changed"); request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -200,6 +204,7 @@ service_proxy_cb (GObject *object, return; } +g_message ("registering for g-properties-changed callback"); /* we want to change the primary device changes */ g_signal_connect (priv->proxy, "g-properties-changed", diff --git a/tests/test-dbus-listener.cc b/tests/test-dbus-listener.cc index 4c6c607..8bf74bf 100644 --- a/tests/test-dbus-listener.cc +++ b/tests/test-dbus-listener.cc @@ -306,3 +306,65 @@ TEST_F(DbusListenerTest, GSDReturnsError) g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); } + +/* This test emits a PropertiesChanged signal and confirms that + the dbus listener asks for a fresh set of devices as a result. */ +TEST_F(DbusListenerTest, GSDPropChanged) +{ + gsd_power_error_string = g_strdup ("no devices for you lol"); + + // create an i-power dbus listener to watch for GSD + ASSERT_EQ (g_slist_length(devices), 0); + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, NULL)); + ASSERT_TRUE (INDICATOR_IS_POWER_DBUS_LISTENER(o)); + g_signal_connect(o, INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_CALLBACK(on_devices_enumerated), this); + g_main_loop_run (mainloop); + // test the devices should have gotten + ASSERT_EQ (g_slist_length(devices), 0); + + // build a GetDevices retval that shows an AC line and a discharging battery + g_clear_pointer (&gsd_power_error_string, g_free); + GVariantBuilder * builder = g_variant_builder_new (G_VARIANT_TYPE("a(susdut)")); + g_variant_builder_add (builder, "(susdut)", + "/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); + g_variant_builder_add (builder, "(susdut)", + "/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); + GVariant * value = g_variant_builder_end (builder); + get_devices_retval = g_variant_new_tuple (&value, 1); + g_variant_builder_unref (builder); + + // tell the listener that some property changed + GVariantBuilder props_builder; + g_variant_builder_init (&props_builder, G_VARIANT_TYPE ("a{sv}")); + GVariant * props_changed = g_variant_new ("(s@a{sv}@as)", GSD_POWER_DBUS_INTERFACE, + g_variant_builder_end (&props_builder), + g_variant_new_strv (NULL, 0)); + g_variant_ref_sink (props_changed); + GError * error = NULL; + g_dbus_connection_emit_signal (connection, + NULL, + GSD_POWER_DBUS_PATH, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + props_changed, + &error); + ASSERT_TRUE(error == NULL); + + // give i-power's dbus listener a chance to ask for a fresh device list + g_main_loop_run (mainloop); + ASSERT_EQ (g_slist_length(devices), 2); + + // cleanup + g_object_unref (o); +} -- cgit v1.2.3 From 89c515defeb4403ec9452221f130be1007e0c7df Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 12:59:24 -0500 Subject: remove some g_message() calls that shouldn't've been committed. --- src/dbus-listener.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index bc31ee3..feaf78e 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -127,7 +127,6 @@ get_devices_cb (GObject * source_object, GSList * devices = NULL; GVariant * devices_container; IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); -g_message ("%s", G_STRLOC); /* build an array of IndicatorPowerDevices from the DBus response */ error = NULL; @@ -164,7 +163,6 @@ g_message ("%s", G_STRLOC); static void request_device_list (IndicatorPowerDbusListener * self) { -g_message ("calling GetDevices... self %p priv %p proxy %p", self, self->priv, self->priv->proxy); g_dbus_proxy_call (self->priv->proxy, "GetDevices", NULL, @@ -181,8 +179,6 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { -g_message ("properties changed: %p %p %p %p", proxy, changed_properties, invalidated_properties, user_data); -g_message ("hey someone told me the properties changed"); request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -204,7 +200,6 @@ service_proxy_cb (GObject *object, return; } -g_message ("registering for g-properties-changed callback"); /* we want to change the primary device changes */ g_signal_connect (priv->proxy, "g-properties-changed", -- cgit v1.2.3 From 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(-) 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(-) 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(-) 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(-) 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 624a1f305ba4b9914ac489e86a649d117cc33995 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 12:39:15 -0500 Subject: Add gcda/gcno to CLEANFILES s.t. 'make clean' will cover them --- src/Makefile.am | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Makefile.am b/src/Makefile.am index c3200a7..73c30a0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,4 @@ +CLEANFILES = ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} ################### @@ -12,6 +13,8 @@ libpower_la_SOURCES = \ device.c \ indicator-power.c +CLEANFILES += .libs/*.gcda .libs/*.gcno *.gcda *.gcno + noinst_HEADERS = \ dbus-listener.h \ device.h \ -- 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(-) 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 ea0293e260480be4ccb796a51c614966a253fdbe Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:21:08 -0500 Subject: Add unit tests for indicator_power_device_get_time_details() --- tests/test-device.cc | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 0ea7177..3b3ff73 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -57,6 +57,25 @@ class DeviceTest : public ::testing::Test g_free (str); g_strfreev (names); } + + void check_strings (const IndicatorPowerDevice * device, + const char * expected_timestring, + const char * expected_details, + const char * expected_accessible) + { + char * timestring = NULL; + char * details = NULL; + char * accessible = NULL; + + indicator_power_device_get_time_details (device, ×tring, &details, &accessible); + EXPECT_STREQ (expected_timestring, timestring); + EXPECT_STREQ (expected_details, details); + EXPECT_STREQ (expected_accessible, accessible); + + g_free (accessible); + g_free (details); + g_free (timestring); + } }; @@ -346,8 +365,7 @@ 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, @@ -360,3 +378,70 @@ TEST_F(DeviceTest, IconNames) g_object_unref(o); } + +TEST_F(DeviceTest, Labels) +{ + // set our language so that i18n won't break these tests + char * real_lang = g_strdup(g_getenv ("LANG")); + g_setenv ("LANG", "en_US.UTF-8", TRUE); + + IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); + GObject * o = G_OBJECT(device); + + /* charging */ + 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, + INDICATOR_POWER_DEVICE_TIME, guint64(60*61), + NULL); + check_strings (device, "(1:01)", + "Battery (1:01 to charge)", + "Battery (1 hour 1 minute to charge (50%))"); + + /* discharging, < 12 hours 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*61), + NULL); + check_strings (device, "1:01", + "Battery (1:01 left)", + "Battery (1 hour 1 minute left (50%))"); + + /* discharging, > 12 hours 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*13), + NULL); + check_strings (device, "13:00", "Battery", "Battery"); + + /* fully charged */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + check_strings (device, "", "Battery (charged)", "Battery (charged)"); + + /* percentage but no time estimate */ + 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(0), + NULL); + check_strings (device, "(50%)", "Battery (50%)", "Battery (50%)"); + + /* no percentage, no time estimate */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + check_strings (device, "(not present)", "Battery (not present)", "Battery (not present)"); + + /* cleanup */ + g_object_unref(o); + g_setenv ("LANG", real_lang, TRUE); + g_free (real_lang); +} -- 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(-) 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(-) 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 560aa0ec6f3b4f845e6cc0e6b3c25197d1905794 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:38:16 -0500 Subject: Add coverage test for AC Adapters in indicator_power_device_get_time_details() --- tests/test-device.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test-device.cc b/tests/test-device.cc index 3b3ff73..1233133 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -440,6 +440,14 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "(not present)", "Battery (not present)", "Battery (not present)"); + /* power line */ + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, + INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + check_strings (device, "AC Adapter", "AC Adapter", "AC Adapter"); + /* cleanup */ g_object_unref(o); g_setenv ("LANG", real_lang, TRUE); -- 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(-) 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(-) 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(-) 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 b1080101b59542f27922d7b184b7b21e27919a85 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 13:58:43 -0500 Subject: use C++-style comments in test-device.cc --- tests/test-device.cc | 64 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/test-device.cc b/tests/test-device.cc index 5d68880..cc0d9c5 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -109,7 +109,7 @@ TEST_F(DeviceTest, Properties) ASSERT_TRUE (o != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o)); - /* Test getting & setting a Device's properties. */ + // Test getting & setting a Device's properties. // KIND key = INDICATOR_POWER_DEVICE_KIND; @@ -163,7 +163,7 @@ TEST_F(DeviceTest, New) ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); ASSERT_EQ (indicator_power_device_get_time(device), 30); - /* cleanup */ + // cleanup g_object_unref (device); } @@ -188,7 +188,7 @@ TEST_F(DeviceTest, NewFromVariant) ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); ASSERT_EQ (indicator_power_device_get_time(device), 30); - /* cleanup */ + // cleanup g_object_unref (device); g_variant_unref (variant); } @@ -224,22 +224,22 @@ TEST_F(DeviceTest, IconNames) IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); GObject * o = G_OBJECT(device); - /* bad arguments */ + // bad arguments ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL); - /* power */ + // 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 */ + // 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 */ + // empty battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY, NULL); @@ -248,7 +248,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-000;" "battery-empty;"); - /* charged battery */ + // charged battery g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, NULL); @@ -256,7 +256,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-full;gpm-battery-100;" "battery-full-charged;battery-full-charging;"); - /* charging battery, 95% */ + // 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, @@ -265,7 +265,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-000-charging;" "battery-caution-charging;"); - /* charging battery, 85% */ + // 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, @@ -274,7 +274,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-000-charging;" "battery-caution-charging;"); - /* charging battery, 50% */ + // 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, @@ -283,7 +283,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-000-charging;" "battery-caution-charging;"); - /* charging battery, 25% */ + // 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, @@ -292,7 +292,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-000-charging;" "battery-caution-charging;"); - /* charging battery, 5% */ + // 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, @@ -302,7 +302,7 @@ TEST_F(DeviceTest, IconNames) "battery-caution-charging;"); - /* discharging battery, 95% */ + // 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, @@ -311,7 +311,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-100;" "battery-full;"); - /* discharging battery, 85% */ + // 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, @@ -320,7 +320,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-080;" "battery-full;"); - /* discharging battery, 50% -- 1 hour left */ + // 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, @@ -330,7 +330,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-060;" "battery-good;"); - /* discharging battery, 25% -- 1 hour left */ + // 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, @@ -340,7 +340,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-040;" "battery-good;"); - /* discharging battery, 25% -- 15 minutes left */ + // 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, @@ -350,7 +350,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-020;" "battery-low;"); - /* discharging battery, 5% -- 1 hour left */ + // 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, @@ -360,7 +360,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-040;" "battery-good;"); - /* discharging battery, 5% -- 15 minutes left */ + // 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, @@ -369,7 +369,7 @@ TEST_F(DeviceTest, IconNames) check_icon_names (device, "battery-caution-symbolic;" "gpm-battery-000;" "battery-caution;"); - /* state unknown */ + // state unknown g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, NULL); @@ -377,7 +377,7 @@ TEST_F(DeviceTest, IconNames) "gpm-battery-missing;" "battery-missing;"); - /* cleanup */ + // cleanup g_object_unref(o); } @@ -388,10 +388,10 @@ TEST_F(DeviceTest, Labels) char * real_lang = g_strdup(g_getenv ("LANG")); g_setenv ("LANG", "en_US.UTF-8", TRUE); - /* bad args: NULL device */ + // bad args: NULL device check_strings (NULL, NULL, NULL, NULL); - /* bad args: a GObject that isn't a device */ + // 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); @@ -403,7 +403,7 @@ TEST_F(DeviceTest, Labels) IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL)); o = G_OBJECT(device); - /* charging */ + // charging 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, @@ -413,7 +413,7 @@ TEST_F(DeviceTest, Labels) "Battery (1:01 to charge)", "Battery (1 hour 1 minute to charge (50%))"); - /* discharging, < 12 hours left */ + // discharging, < 12 hours 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, @@ -423,7 +423,7 @@ TEST_F(DeviceTest, Labels) "Battery (1:01 left)", "Battery (1 hour 1 minute left (50%))"); - /* discharging, > 12 hours left */ + // discharging, > 12 hours 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, @@ -431,7 +431,7 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "13:00", "Battery", "Battery"); - /* fully charged */ + // fully charged g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, @@ -439,7 +439,7 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "", "Battery (charged)", "Battery (charged)"); - /* percentage but no time estimate */ + // percentage but no time estimate 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, @@ -447,7 +447,7 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "(50%)", "Battery (50%)", "Battery (50%)"); - /* no percentage, no time estimate */ + // no percentage, no time estimate g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, @@ -455,7 +455,7 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "(not present)", "Battery (not present)", "Battery (not present)"); - /* power line */ + // power line g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, @@ -463,7 +463,7 @@ TEST_F(DeviceTest, Labels) NULL); check_strings (device, "AC Adapter", "AC Adapter", "AC Adapter"); - /* cleanup */ + // cleanup g_object_unref(o); g_setenv ("LANG", real_lang, TRUE); g_free (real_lang); -- 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(-) 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 99a719aac32c4347c9b250372d887ca94aa56330 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 14:24:17 -0500 Subject: copyediting: fix tab damage in dbus-listener.[ch] --- src/dbus-listener.c | 222 ++++++++++++++++++++++++++-------------------------- src/dbus-listener.h | 8 +- 2 files changed, 114 insertions(+), 116 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index feaf78e..7f399f4 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -26,17 +26,17 @@ License along with this library. If not, see struct _IndicatorPowerDbusListenerPrivate { - GCancellable * cancellable; - GDBusProxy * proxy; - guint watcher_id; + GCancellable * cancellable; + GDBusProxy * proxy; + guint watcher_id; }; #define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv) /* Signals */ enum { - SIGNAL_DEVICES, - SIGNAL_LAST + SIGNAL_DEVICES, + SIGNAL_LAST }; static guint signals[SIGNAL_LAST] = { 0 }; @@ -56,62 +56,62 @@ G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); - g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); + g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate)); - /* methods */ - object_class->dispose = indicator_power_dbus_listener_dispose; - object_class->finalize = indicator_power_dbus_listener_finalize; + /* methods */ + object_class->dispose = indicator_power_dbus_listener_dispose; + object_class->finalize = indicator_power_dbus_listener_finalize; - /* signals */ - signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, - G_TYPE_FROM_CLASS(klass), 0, - G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), - NULL, NULL, - g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); + /* signals */ + signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, + G_TYPE_FROM_CLASS(klass), 0, + G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); } /* Initialize an instance */ static void indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self) { - IndicatorPowerDbusListenerPrivate * priv; + IndicatorPowerDbusListenerPrivate * priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_DBUS_LISTENER_TYPE, - IndicatorPowerDbusListenerPrivate); + priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_DBUS_LISTENER_TYPE, + IndicatorPowerDbusListenerPrivate); - priv->cancellable = g_cancellable_new (); + priv->cancellable = g_cancellable_new (); - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - GSD_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + GSD_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); - self->priv = priv; + self->priv = priv; } static void indicator_power_dbus_listener_dispose (GObject *object) { - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); - IndicatorPowerDbusListenerPrivate * priv = self->priv; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object); + IndicatorPowerDbusListenerPrivate * priv = self->priv; - g_clear_object (&priv->proxy); - g_clear_object (&priv->cancellable); + g_clear_object (&priv->proxy); + g_clear_object (&priv->cancellable); - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } static void indicator_power_dbus_listener_finalize (GObject *object) { - G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object); } /*** @@ -123,54 +123,53 @@ get_devices_cb (GObject * source_object, GAsyncResult * res, gpointer user_data) { - GError *error; - GSList * devices = NULL; - GVariant * devices_container; - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data); - - /* build an array of IndicatorPowerDevices from the DBus response */ - error = NULL; - devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); - if (devices_container == NULL) - { - g_message ("Couldn't get devices: %s\n", error->message); - g_error_free (error); - } - else - { - gsize i; - GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); - const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - - for (i=0; imessage); + g_error_free (error); + } + else + { + gsize i; + GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); + const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; + + for (i=0; ipriv->proxy, - "GetDevices", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - self->priv->cancellable, - get_devices_cb, - self); + g_dbus_proxy_call (self->priv->proxy, + "GetDevices", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + self->priv->cancellable, + get_devices_cb, + self); } static void @@ -179,7 +178,7 @@ receive_properties_changed (GDBusProxy *proxy G_GNUC_UNUSED, GStrv invalidated_properties G_GNUC_UNUSED, gpointer user_data) { - request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); + request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data)); } @@ -188,26 +187,25 @@ service_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data) { - GError * error = NULL; - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - - if (error != NULL) { - g_error ("Error creating proxy: %s", error->message); - g_error_free (error); - return; - } - - /* we want to change the primary device changes */ - g_signal_connect (priv->proxy, - "g-properties-changed", - G_CALLBACK (receive_properties_changed), - user_data); - - /* get the initial state */ - request_device_list (self); + GError * error = NULL; + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + + if (error != NULL) + { + g_error ("Error creating proxy: %s", error->message); + g_error_free (error); + return; + } + + /* we want to change the primary device changes */ + g_signal_connect (priv->proxy, "g-properties-changed", + G_CALLBACK (receive_properties_changed), user_data); + + /* get the initial state */ + request_device_list (self); } static void @@ -216,16 +214,16 @@ gsd_appeared_callback (GDBusConnection *connection, const gchar *name_owner, gpointer user_data) { - IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); - IndicatorPowerDbusListenerPrivate * priv = self->priv; - - g_dbus_proxy_new (connection, - G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, - NULL, - name, - GSD_POWER_DBUS_PATH, - GSD_POWER_DBUS_INTERFACE, - priv->cancellable, - service_proxy_cb, - self); + IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data); + IndicatorPowerDbusListenerPrivate * priv = self->priv; + + g_dbus_proxy_new (connection, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + NULL, + name, + GSD_POWER_DBUS_PATH, + GSD_POWER_DBUS_INTERFACE, + priv->cancellable, + service_proxy_cb, + self); } diff --git a/src/dbus-listener.h b/src/dbus-listener.h index d1ab993..b07c73f 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -55,9 +55,9 @@ typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPriv */ struct _IndicatorPowerDbusListenerClass { - GObjectClass parent_class; + GObjectClass parent_class; - void (* devices_enumerated) (IndicatorPowerDbusListener*, GSList * devices); + void (* devices_enumerated) (IndicatorPowerDbusListener*, GSList * devices); }; /** @@ -67,8 +67,8 @@ struct _IndicatorPowerDbusListenerClass */ struct _IndicatorPowerDbusListener { - GObject parent; - IndicatorPowerDbusListenerPrivate * priv; + GObject parent; + IndicatorPowerDbusListenerPrivate * priv; }; /*** -- cgit v1.2.3 From 66ba8edb03428310abe360f443849cb8e57c3480 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 1 Jun 2012 14:48:05 -0500 Subject: In indicator-power.c's put_primary_device(), remove dead code. --- src/indicator-power.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index d5735ee..33f49e7 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -440,32 +440,24 @@ get_primary_device (GSList * devices) static void put_primary_device (IndicatorPower *self, IndicatorPowerDevice *device) { - GIcon *device_gicon; - gchar *short_details = NULL; - gchar *details = NULL; - gchar *accessible_name = NULL; IndicatorPowerPrivate * priv = self->priv; /* set icon */ - device_gicon = indicator_power_device_get_gicon (device); + GIcon * 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)); - - /* get the device name */ - //device_name = device_kind_to_localised_string (kind); - /* get the description */ + gchar * short_details; + gchar * details; + gchar * accessible_name; indicator_power_device_get_time_details (device, &short_details, &details, &accessible_name); - - gtk_label_set_label (GTK_LABEL (priv->label), - short_details); + gtk_label_set_label (GTK_LABEL (priv->label), short_details); set_accessible_desc (self, accessible_name); - - g_free (short_details); - g_free (details); g_free (accessible_name); + g_free (details); + g_free (short_details); } void -- cgit v1.2.3 From 15d6b254b2a9fcdca5fd76c8f58c8a0274e63322 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:30:34 -0500 Subject: move the header files from noinst_HEADERS to libpower_la_SOURCES --- src/Makefile.am | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 73c30a0..b2cf3df 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,16 +10,14 @@ powerlib_LTLIBRARIES = libpower.la libpower_la_SOURCES = \ dbus-listener.c \ + dbus-listener.h \ device.c \ + device.h \ + indicator-power.h \ indicator-power.c CLEANFILES += .libs/*.gcda .libs/*.gcno *.gcda *.gcno -noinst_HEADERS = \ - dbus-listener.h \ - device.h \ - indicator-power.h - libpower_la_CFLAGS = \ $(UPOWER_CFLAGS) \ $(INDICATOR_CFLAGS) \ -- cgit v1.2.3 From f2310de23a3012fd579a659a99b09b030e9df108 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:39:51 -0500 Subject: add a GTK-Doc signal comment block for indictor-power-dbus-listener's enumerated signal --- src/dbus-listener.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 7f399f4..9143a25 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -64,13 +64,20 @@ indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass object_class->dispose = indicator_power_dbus_listener_dispose; object_class->finalize = indicator_power_dbus_listener_finalize; - /* signals */ + /** + * IndicatorPowerDbusListener::indicator-power-dbus-listener-devices-enumerated: + * + * @listener: the IndicatorPowerDbusListener + * @devices: a GSList of #IndicatorPowerDevice objects. (transfer none) + * + * This is emitted each time a new set of devices is enumerated over the bus. + */ signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED, G_TYPE_FROM_CLASS(klass), 0, G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated), NULL, NULL, g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER, G_TYPE_NONE); + G_TYPE_NONE, 1, G_TYPE_POINTER); } /* Initialize an instance */ -- cgit v1.2.3 From 79d37d6827bdfd9b1a94ce6918157ce475bc012a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:42:23 -0500 Subject: add g_bus_unwatch_name() to watcher's dispose() method --- src/dbus-listener.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 9143a25..7e3d7b5 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -112,6 +112,12 @@ indicator_power_dbus_listener_dispose (GObject *object) g_clear_object (&priv->proxy); g_clear_object (&priv->cancellable); + if (priv->watcher_id) + { + g_bus_unwatch_name (priv->watcher_id); + priv->watcher_id = 0; + } + G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object); } -- cgit v1.2.3 From 5cac4ebb939cde67c78cf8aa59d27beafd187ac8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:43:11 -0500 Subject: remove trailing whitespace --- src/dbus-listener.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 7e3d7b5..4923fe1 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -154,7 +154,7 @@ get_devices_cb (GObject * source_object, gsize i; GVariant * devices_variant = g_variant_get_child_value (devices_container, 0); const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0; - + for (i=0; i Date: Wed, 6 Jun 2012 14:47:08 -0500 Subject: if self->cancellable is non-NULL in dispose(), pass it to g_cancellable_cancel() before clearing the listener's reference --- src/dbus-listener.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 4923fe1..34df925 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -110,7 +110,12 @@ indicator_power_dbus_listener_dispose (GObject *object) IndicatorPowerDbusListenerPrivate * priv = self->priv; g_clear_object (&priv->proxy); - g_clear_object (&priv->cancellable); + + if (priv->cancellable != NULL) + { + g_cancellable_cancel (priv->cancellable); + g_clear_object (&priv->cancellable); + } if (priv->watcher_id) { -- cgit v1.2.3 From 65f33de3763fc9540f994abf29ca05f7714f81ee Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:50:39 -0500 Subject: simplify the devices-enumerated signal's name --- src/dbus-listener.c | 2 +- src/dbus-listener.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dbus-listener.c b/src/dbus-listener.c index 34df925..b1f64a7 100644 --- a/src/dbus-listener.c +++ b/src/dbus-listener.c @@ -65,7 +65,7 @@ indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass object_class->finalize = indicator_power_dbus_listener_finalize; /** - * IndicatorPowerDbusListener::indicator-power-dbus-listener-devices-enumerated: + * IndicatorPowerDbusListener::devices-enumerated: * * @listener: the IndicatorPowerDbusListener * @devices: a GSList of #IndicatorPowerDevice objects. (transfer none) diff --git a/src/dbus-listener.h b/src/dbus-listener.h index b07c73f..8a166bf 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -47,7 +47,7 @@ typedef struct _IndicatorPowerDbusListenerPrivate IndicatorPowerDbusListenerPriv #define GSD_POWER_DBUS_PATH GSD_PATH "/Power" /* signals */ -#define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "indicator-power-dbus-listener-devices-enumerated" +#define INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED "devices-enumerated" /** * IndicatorPowerDbusListenerClass: -- cgit v1.2.3 From 42caee4de28551962e0e26dcda12df318a52d215 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 14:51:22 -0500 Subject: remove prototype for indicator_power_dbus_listener_new() since it's not needed/used --- src/dbus-listener.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/dbus-listener.h b/src/dbus-listener.h index 8a166bf..e4d006b 100644 --- a/src/dbus-listener.h +++ b/src/dbus-listener.h @@ -77,9 +77,6 @@ struct _IndicatorPowerDbusListener GType indicator_power_dbus_listener_get_type (void); -IndicatorPowerDbusListener* indicator_power_dbus_listener_new (void); - - G_END_DECLS #endif -- 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(-) 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(+) 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(+) 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(+) 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(-) 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 bca7ca967fbc3ba1ba2085385601bb949768885b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:22:38 -0500 Subject: simplify the Device properties' name strings --- src/device.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/device.h b/src/device.h index c7ba638..3f7bbee 100644 --- a/src/device.h +++ b/src/device.h @@ -40,11 +40,11 @@ typedef struct _IndicatorPowerDevice IndicatorPowerDevice; typedef struct _IndicatorPowerDeviceClass IndicatorPowerDeviceClass; 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_PERCENTAGE "indicator-power-device-percentage" -#define INDICATOR_POWER_DEVICE_TIME "indicator-power-device-time" +#define INDICATOR_POWER_DEVICE_KIND "kind" +#define INDICATOR_POWER_DEVICE_STATE "state" +#define INDICATOR_POWER_DEVICE_OBJECT_PATH "object-path" +#define INDICATOR_POWER_DEVICE_PERCENTAGE "percentage" +#define INDICATOR_POWER_DEVICE_TIME "time" /** * IndicatorPowerDeviceClass: -- cgit v1.2.3 From 137b2e1d0d3b6902186024c0a0a7fd17ac02707c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 6 Jun 2012 15:24:53 -0500 Subject: remove superfluous #include --- src/indicator-power.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/indicator-power.h b/src/indicator-power.h index 69ed888..a696b40 100644 --- a/src/indicator-power.h +++ b/src/indicator-power.h @@ -26,8 +26,6 @@ with this program. If not, see . #include #include -#include "device.h" - G_BEGIN_DECLS #define INDICATOR_POWER_TYPE (indicator_power_get_type ()) -- 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(-) 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(-) 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 From 867f9fe6955aed2b63bf671f85ae403a6152db74 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 7 Jun 2012 10:36:13 -0500 Subject: remove some dead code. --- src/indicator-power.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 33f49e7..f3a7235 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -180,14 +180,6 @@ spawn_command_line_async (const char * command) g_clear_error (&err); } -static void -show_info_cb (GtkMenuItem *item, - gpointer data) -{ - /*TODO: show the statistics of the specific device*/ - spawn_command_line_async ("gnome-power-statistics"); -} - static void option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self) { @@ -195,13 +187,6 @@ option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self) gtk_check_menu_item_get_active(item)); } -static void -show_preferences_cb (GtkMenuItem *item, - gpointer data) -{ - spawn_command_line_async ("gnome-control-center power"); -} - /* ensure that the entry is using self's accessible description */ static void refresh_entry_accessible_desc (IndicatorPower * self, IndicatorObjectEntry * entry) @@ -289,8 +274,8 @@ menu_add_device (GtkMenu * menu, const IndicatorPowerDevice * device) gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); added = TRUE; - g_signal_connect (G_OBJECT (item), "activate", - G_CALLBACK (show_info_cb), NULL); + g_signal_connect_swapped (G_OBJECT (item), "activate", + G_CALLBACK (spawn_command_line_async), "gnome-power-statistics"); g_free (short_details); g_free (details); @@ -356,8 +341,8 @@ build_menu (IndicatorPower *self) item = gtk_image_menu_item_new_with_label (_("Power Settings…")); image = gtk_image_new_from_icon_name (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); - g_signal_connect (G_OBJECT (item), "activate", - G_CALLBACK (show_preferences_cb), NULL); + g_signal_connect_swapped (G_OBJECT (item), "activate", + G_CALLBACK (spawn_command_line_async), "gnome-control-center power"); gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); } -- cgit v1.2.3 From 2343edb9f2a49354ab3d46a2fec85b840daf0709 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 3 Jul 2012 17:20:35 -0500 Subject: disable test-dbus-listener for now --- tests/Makefile.am | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index b6b543b..0c60247 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -45,11 +45,11 @@ test_device_SOURCES = test-device.cc test_device_LDADD = $(TEST_LIBS) test_device_CPPFLAGS = $(TEST_CPPFLAGS) -TESTS += test-dbus-listener -check_PROGRAMS += test-dbus-listener -test_dbus_listener_SOURCES = test-dbus-listener.cc -test_dbus_listener_LDADD = $(TEST_LIBS) -test_dbus_listener_CPPFLAGS = $(TEST_CPPFLAGS) +#TESTS += test-dbus-listener +#check_PROGRAMS += test-dbus-listener +#test_dbus_listener_SOURCES = test-dbus-listener.cc +#test_dbus_listener_LDADD = $(TEST_LIBS) +#test_dbus_listener_CPPFLAGS = $(TEST_CPPFLAGS) TESTS += test-indicator check_PROGRAMS += test-indicator -- cgit v1.2.3