diff options
-rw-r--r-- | data/com.canonical.indicator.power.Battery.xml | 4 | ||||
-rw-r--r-- | src/notifier.c | 81 | ||||
-rw-r--r-- | src/notifier.h | 15 | ||||
-rw-r--r-- | tests/test-notify.cc | 20 |
4 files changed, 73 insertions, 47 deletions
diff --git a/data/com.canonical.indicator.power.Battery.xml b/data/com.canonical.indicator.power.Battery.xml index d2c8a2d..eca4524 100644 --- a/data/com.canonical.indicator.power.Battery.xml +++ b/data/com.canonical.indicator.power.Battery.xml @@ -3,10 +3,10 @@ <node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd"> <interface name="com.canonical.indicator.power.Battery"> - <property name="PowerLevel" type="i" access="read"> + <property name="PowerLevel" type="s" access="read"> <doc:doc> <doc:description> - <doc:para>The battery's power level. 0==No Low Battery, 1==Low, 2==Very Low, 3==Critical</doc:para> + <doc:para>The battery's power level. Possible values: 'ok', 'low', 'very_low', 'critical'</doc:para> </doc:description> </doc:doc> </property> diff --git a/src/notifier.c b/src/notifier.c index 60799f9..7add139 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -27,6 +27,15 @@ #define HINT_INTERACTIVE "x-canonical-switch-to-application" +typedef enum +{ + POWER_LEVEL_OK, + POWER_LEVEL_LOW, + POWER_LEVEL_VERY_LOW, + POWER_LEVEL_CRITICAL +} +PowerLevel; + /** *** GObject Properties **/ @@ -75,6 +84,48 @@ G_DEFINE_TYPE_WITH_PRIVATE(IndicatorPowerNotifier, #define get_priv(o) ((priv_t*)indicator_power_notifier_get_instance_private(o)) /*** +**** +***/ + +static const char * +power_level_to_dbus_string (const PowerLevel power_level) +{ + switch (power_level) + { + case POWER_LEVEL_LOW: return "low"; + case POWER_LEVEL_VERY_LOW: return "very_low"; + case POWER_LEVEL_CRITICAL: return "critical"; + default: return "ok"; + } +} + +PowerLevel +get_battery_power_level (IndicatorPowerDevice * battery) +{ + static const double percent_critical = 2.0; + static const double percent_very_low = 5.0; + static const double percent_low = 10.0; + gdouble p; + PowerLevel ret; + + g_return_val_if_fail(battery != NULL, POWER_LEVEL_OK); + g_return_val_if_fail(indicator_power_device_get_kind(battery) == UP_DEVICE_KIND_BATTERY, POWER_LEVEL_OK); + + p = indicator_power_device_get_percentage(battery); + + if (p <= percent_critical) + ret = POWER_LEVEL_CRITICAL; + else if (p <= percent_very_low) + ret = POWER_LEVEL_VERY_LOW; + else if (p <= percent_low) + ret = POWER_LEVEL_LOW; + else + ret = POWER_LEVEL_OK; + + return ret; +} + +/*** **** Notifications ***/ @@ -164,7 +215,7 @@ on_battery_property_changed (IndicatorPowerNotifier * self) g_return_if_fail(INDICATOR_IS_POWER_DEVICE(p->battery)); old_power_level = p->power_level; - new_power_level = indicator_power_notifier_get_power_level (p->battery); + new_power_level = get_battery_power_level (p->battery); old_discharging = p->discharging; new_discharging = indicator_power_device_get_state(p->battery) == UP_DEVICE_STATE_DISCHARGING; @@ -182,7 +233,7 @@ on_battery_property_changed (IndicatorPowerNotifier * self) notification_clear (self); } - dbus_battery_set_power_level (p->dbus_battery, new_power_level); + dbus_battery_set_power_level (p->dbus_battery, power_level_to_dbus_string (new_power_level)); p->discharging = new_discharging; } @@ -326,7 +377,7 @@ indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, { g_signal_handlers_disconnect_by_data (p->battery, self); g_clear_object (&p->battery); - dbus_battery_set_power_level (p->dbus_battery, POWER_LEVEL_OK); + dbus_battery_set_power_level (p->dbus_battery, power_level_to_dbus_string (POWER_LEVEL_OK)); notification_clear (self); } @@ -384,28 +435,8 @@ indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, } } -PowerLevel +const char * indicator_power_notifier_get_power_level (IndicatorPowerDevice * battery) { - static const double percent_critical = 2.0; - static const double percent_very_low = 5.0; - static const double percent_low = 10.0; - gdouble p; - PowerLevel ret; - - g_return_val_if_fail(battery != NULL, POWER_LEVEL_OK); - g_return_val_if_fail(indicator_power_device_get_kind(battery) == UP_DEVICE_KIND_BATTERY, POWER_LEVEL_OK); - - p = indicator_power_device_get_percentage(battery); - - if (p <= percent_critical) - ret = POWER_LEVEL_CRITICAL; - else if (p <= percent_very_low) - ret = POWER_LEVEL_VERY_LOW; - else if (p <= percent_low) - ret = POWER_LEVEL_LOW; - else - ret = POWER_LEVEL_OK; - - return ret; + return power_level_to_dbus_string (get_battery_power_level (battery)); } diff --git a/src/notifier.h b/src/notifier.h index c23c585..18e25d7 100644 --- a/src/notifier.h +++ b/src/notifier.h @@ -35,15 +35,6 @@ G_BEGIN_DECLS typedef struct _IndicatorPowerNotifier IndicatorPowerNotifier; typedef struct _IndicatorPowerNotifierClass IndicatorPowerNotifierClass; -typedef enum -{ - POWER_LEVEL_OK, - POWER_LEVEL_LOW, - POWER_LEVEL_VERY_LOW, - POWER_LEVEL_CRITICAL -} -PowerLevel; - /** * The Indicator Power Notifier. */ @@ -72,7 +63,11 @@ void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, void indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, IndicatorPowerDevice * battery); -PowerLevel indicator_power_notifier_get_power_level (IndicatorPowerDevice * battery); +#define POWER_LEVEL_STR_OK "ok" +#define POWER_LEVEL_STR_LOW "low" +#define POWER_LEVEL_STR_VERY_LOW "very_low" +#define POWER_LEVEL_STR_CRITICAL "critical" +const char * indicator_power_notifier_get_power_level (IndicatorPowerDevice * battery); G_END_DECLS diff --git a/tests/test-notify.cc b/tests/test-notify.cc index a8d66d3..b5166a0 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -200,13 +200,13 @@ TEST_F(NotifyFixture, PercentageToLevel) const auto level = indicator_power_notifier_get_power_level(battery); if (i <= percent_critical) - EXPECT_EQ (POWER_LEVEL_CRITICAL, level); + EXPECT_STREQ (POWER_LEVEL_STR_CRITICAL, level); else if (i <= percent_very_low) - EXPECT_EQ (POWER_LEVEL_VERY_LOW, level); + EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, level); else if (i <= percent_low) - EXPECT_EQ (POWER_LEVEL_LOW, level); + EXPECT_STREQ (POWER_LEVEL_STR_LOW, level); else - EXPECT_EQ (POWER_LEVEL_OK, level); + EXPECT_STREQ (POWER_LEVEL_STR_OK, level); } g_object_unref (battery); @@ -227,7 +227,7 @@ namespace struct ChangedParams { - int32_t power_level = POWER_LEVEL_OK; + std::string power_level = POWER_LEVEL_STR_OK; bool is_warning = false; uint32_t fields = 0; }; @@ -245,8 +245,8 @@ namespace g_return_if_fail (g_variant_is_of_type (dict, G_VARIANT_TYPE_DICTIONARY)); auto changed_params = static_cast<ChangedParams*>(gchanged_params); - gint32 power_level; - if (g_variant_lookup (dict, "PowerLevel", "i", &power_level, nullptr)) + const char * power_level; + if (g_variant_lookup (dict, "PowerLevel", "&s", &power_level, nullptr)) { changed_params->power_level = power_level; changed_params->fields |= FIELD_POWER_LEVEL; @@ -362,7 +362,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) // test setup case wait_msec(); - EXPECT_EQ (0, changed_params.power_level); + EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); // change the percent past the 'low' threshold and confirm that // a) the power level changes @@ -379,7 +379,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) set_battery_percentage (battery, percent_very_low); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL, changed_params.fields); - EXPECT_EQ (POWER_LEVEL_VERY_LOW, changed_params.power_level); + EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, changed_params.power_level.c_str()); // ...and that the warning is taken down if the battery is plugged back in... changed_params = ChangedParams(); @@ -400,7 +400,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) set_battery_percentage (battery, percent_low+1); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); - EXPECT_EQ (POWER_LEVEL_OK, changed_params.power_level); + EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); EXPECT_FALSE (changed_params.is_warning); // cleanup |