From 858276bc0254d2589b9acd77f06c2457af9cb4c8 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Wed, 5 Apr 2023 13:21:40 +0200 Subject: Whitespace fix --- src/testing.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/testing.c b/src/testing.c index 6c5e8c4..0d002a1 100644 --- a/src/testing.c +++ b/src/testing.c @@ -185,7 +185,7 @@ on_bus_changed(IndicatorPowerService * service, IndicatorPowerTesting * self) { GObject * bus = NULL; - g_object_get(service, "bus", &bus, NULL); + g_object_get(service, "bus", &bus, NULL); set_bus(self, G_DBUS_CONNECTION(bus)); g_clear_object(&bus); } @@ -296,7 +296,7 @@ indicator_power_testing_init (IndicatorPowerTesting * self) G_CALLBACK(on_mock_battery_minutes_left_changed), self); /* Mock Battery */ - + p->battery_mock = indicator_power_device_new("/some/path", UP_DEVICE_KIND_BATTERY, 50.0, -- cgit v1.2.3 From 490ec9d3410c535b6f1e915ae52d983730bcb93e Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Wed, 5 Apr 2023 10:13:54 +0200 Subject: CMakeLists.txt: Fix minimum requred CMake warning --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 869d4ae..1a9ee67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ -project(ayatana-indicator-power C CXX) cmake_minimum_required(VERSION 3.13) +project(ayatana-indicator-power C CXX) if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) SET(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "..." FORCE) -- cgit v1.2.3 From eeea7eab9aa5877f8e00cf3993ed088abd498427 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Wed, 5 Apr 2023 10:20:47 +0200 Subject: src/service.c: Use level or progress IDO as needed --- src/service.c | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/service.c b/src/service.c index 3914e3f..32f8788 100644 --- a/src/service.c +++ b/src/service.c @@ -504,17 +504,6 @@ static GMenuModel * create_devices_section (IndicatorPowerService * self, int profile) { GMenu * menu = g_menu_new (); - - if (self->priv->primary_device != NULL) - { - GMenuItem * item = g_menu_item_new (_("Charge level"), NULL); - g_menu_item_set_attribute (item, "x-ayatana-type", "s", "org.ayatana.indicator.progress"); - guint16 battery_level = (guint16)(indicator_power_device_get_percentage (self->priv->primary_device) + 0.5); - g_menu_item_set_attribute (item, "x-ayatana-progress", "q", battery_level); - g_menu_append_item (menu, item); - g_object_unref (item); - } - GList * l; for (l=self->priv->devices; l!=NULL; l=l->next) @@ -524,19 +513,38 @@ create_devices_section (IndicatorPowerService * self, int profile) if (kind != UP_DEVICE_KIND_LINE_POWER) { - if (device == self->priv->primary_device) + gchar *sLabel = NULL; + + if (kind == UP_DEVICE_KIND_BATTERY) + { + sLabel = g_strdup (_("Charge level")); + } + else { - continue; + sLabel = indicator_power_device_get_readable_text (device); } - char * label = indicator_power_device_get_readable_text (device); - GMenuItem * item = g_menu_item_new (label, NULL); - g_free (label); - g_menu_item_set_attribute (item, "x-ayatana-type", "s", "org.ayatana.indicator.progress"); + gchar *sIndicator = NULL; + gchar *sProperty = NULL; + + if (!ayatana_common_utils_is_lomiri()) + { + sIndicator = "org.ayatana.indicator.level"; + sProperty = "x-ayatana-level"; + } + else + { + sIndicator = "org.ayatana.indicator.progress"; + sProperty = "x-ayatana-progress"; + } + + GMenuItem * item = g_menu_item_new (sLabel, NULL); + g_free (sLabel); + g_menu_item_set_attribute (item, "x-ayatana-type", "s", sIndicator); guint16 battery_level = (guint16)(indicator_power_device_get_percentage (device) + 0.5); - g_menu_item_set_attribute (item, "x-ayatana-progress", "q", battery_level); + g_menu_item_set_attribute (item, sProperty, "q", battery_level); - if (profile != PROFILE_PHONE) + if (!ayatana_common_utils_is_lomiri()) { GIcon * icon = indicator_power_device_get_gicon (device, FALSE); @@ -553,10 +561,7 @@ create_devices_section (IndicatorPowerService * self, int profile) g_object_unref (icon); } - if (profile == PROFILE_DESKTOP) - { - g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s", indicator_power_device_get_object_path (device)); - } + g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s", indicator_power_device_get_object_path (device)); } g_menu_append_item (menu, item); -- cgit v1.2.3 From 906c021e2ebd927b512c9d52c03c17e70c01f065 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Wed, 5 Apr 2023 13:25:10 +0200 Subject: Use the device model name in the menu --- src/device-provider-upower.c | 14 ++++++ src/device.c | 62 +++++++++++++++++++++----- src/device.h | 7 ++- src/service.c | 12 ++++- src/testing.c | 3 ++ tests/test-device.cc | 103 +++++++++++++++++++++++-------------------- tests/test-notify.cc | 5 ++- tests/test-service.cc | 28 ++++++------ 8 files changed, 158 insertions(+), 76 deletions(-) diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index f9b5e18..dbbe5f1 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -1,8 +1,10 @@ /* * Copyright 2013 Canonical Ltd. + * Copyright 2023 Robert Tari * * Authors: * Charles Kerr + * Robert Tari * * 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 @@ -108,6 +110,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) else { guint32 kind = 0; + gchar *model; guint32 state = 0; gdouble percentage = 0; gint64 time_to_empty = 0; @@ -119,6 +122,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) GVariant * dict = g_variant_get_child_value (response, 0); g_variant_lookup (dict, "Type", "u", &kind); + g_variant_lookup (dict, "Model", "s", &model); g_variant_lookup (dict, "State", "u", &state); g_variant_lookup (dict, "Percentage", "d", &percentage); g_variant_lookup (dict, "TimeToEmpty", "x", &time_to_empty); @@ -129,6 +133,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) if ((device = g_hash_table_lookup (p->devices, data->path))) { g_object_set (device, INDICATOR_POWER_DEVICE_KIND, (gint)kind, + INDICATOR_POWER_DEVICE_MODEL, model, INDICATOR_POWER_DEVICE_STATE, (gint)state, INDICATOR_POWER_DEVICE_OBJECT_PATH, data->path, INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, @@ -140,6 +145,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) { device = indicator_power_device_new (data->path, kind, + model, percentage, state, (time_t)time, @@ -344,6 +350,14 @@ on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, NULL); changed = TRUE; } + else if (!g_strcmp0(key, "Model")) + { + const gchar *s = g_variant_get_string(value, NULL); + g_object_set(device, + INDICATOR_POWER_DEVICE_MODEL, s, + NULL); + changed = TRUE; + } else if (!g_strcmp0(key, "State")) { const guint32 u = g_variant_get_uint32(value); diff --git a/src/device.c b/src/device.c index a1c4d14..a373981 100644 --- a/src/device.c +++ b/src/device.c @@ -35,6 +35,7 @@ License along with this library. If not, see struct _IndicatorPowerDevicePrivate { UpDeviceKind kind; + gchar *model; UpDeviceState state; gchar * object_path; gdouble percentage; @@ -52,6 +53,7 @@ struct _IndicatorPowerDevicePrivate enum { PROP_0, PROP_KIND, + PROP_MODEL, PROP_STATE, PROP_OBJECT_PATH, PROP_PERCENTAGE, @@ -91,6 +93,12 @@ indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) UP_DEVICE_KIND_UNKNOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_MODEL] = g_param_spec_string (INDICATOR_POWER_DEVICE_MODEL, + "model", + "The device's model", + NULL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_STATE] = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState", @@ -135,6 +143,7 @@ indicator_power_device_init (IndicatorPowerDevice *self) priv = indicator_power_device_get_instance_private(self); priv->kind = UP_DEVICE_KIND_UNKNOWN; + priv->model = NULL; priv->state = UP_DEVICE_STATE_UNKNOWN; priv->object_path = NULL; priv->percentage = 0.0; @@ -182,6 +191,10 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) g_value_set_int (value, priv->kind); break; + case PROP_MODEL: + g_value_set_string (value, priv->model); + break; + case PROP_STATE: g_value_set_int (value, priv->state); break; @@ -220,6 +233,11 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp p->kind = (UpDeviceKind) g_value_get_int (value); break; + case PROP_MODEL: + g_free (p->model); + p->model = g_value_dup_string (value); + break; + case PROP_STATE: p->state = (UpDeviceState) g_value_get_int (value); break; @@ -280,6 +298,16 @@ indicator_power_device_get_kind (const IndicatorPowerDevice * device) return device->priv->kind; } +const gchar * +indicator_power_device_get_model (const IndicatorPowerDevice * device) +{ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); + /* LCOV_EXCL_STOP */ + + return device->priv->model; +} + UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device) { @@ -880,16 +908,25 @@ time_is_relevant (const IndicatorPowerDevice * device) */ static char * get_menuitem_text (const IndicatorPowerDevice * device, - gboolean accessible) + gboolean accessible, gboolean bModelName) { char * str = NULL; const IndicatorPowerDevicePrivate * p = device->priv; - const char * kind_str = device_kind_to_localised_string (p->kind); + const char * sLabel = NULL; + + if (bModelName) + { + sLabel = p->model; + } + else + { + sLabel = device_kind_to_localised_string (p->kind); + } if (p->state == UP_DEVICE_STATE_FULLY_CHARGED) { /* TRANSLATORS: example: "battery (charged)" */ - str = g_strdup_printf (_("%s (charged)"), kind_str); + str = g_strdup_printf (_("%s (charged)"), sLabel); } else { @@ -906,11 +943,11 @@ get_menuitem_text (const IndicatorPowerDevice * device, if (time_str && *time_str) { /* TRANSLATORS: example: "battery (time remaining)" */ - str = g_strdup_printf (_("%s (%s)"), kind_str, time_str); + str = g_strdup_printf (_("%s (%s)"), sLabel, time_str); } else { - str = g_strdup (kind_str); + str = g_strdup (sLabel); } g_free (time_str); @@ -920,11 +957,11 @@ get_menuitem_text (const IndicatorPowerDevice * device, } char * -indicator_power_device_get_readable_text (const IndicatorPowerDevice * device) +indicator_power_device_get_readable_text (const IndicatorPowerDevice * device, gboolean bModelName) { g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); - return get_menuitem_text (device, FALSE); + return get_menuitem_text (device, FALSE, bModelName); } char * @@ -932,7 +969,7 @@ indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device) { g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL); - return get_menuitem_text (device, TRUE); + return get_menuitem_text (device, TRUE, FALSE); } /** @@ -1020,6 +1057,7 @@ indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device IndicatorPowerDevice * indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, + const gchar * model, gdouble percentage, UpDeviceState state, time_t timestamp, @@ -1027,6 +1065,7 @@ indicator_power_device_new (const gchar * object_path, { GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_MODEL, model, INDICATOR_POWER_DEVICE_STATE, state, INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, @@ -1039,9 +1078,10 @@ indicator_power_device_new (const gchar * object_path, IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant * v) { - g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdutb)")), NULL); + g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(sussdutb)")), NULL); UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; + const gchar *model = NULL; UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; const gchar * icon = NULL; const gchar * object_path = NULL; @@ -1049,9 +1089,10 @@ indicator_power_device_new_from_variant (GVariant * v) guint64 time = 0; gboolean power_supply = FALSE; - g_variant_get (v, "(&su&sdutb)", + g_variant_get (v, "(&sus&sdutb)", &object_path, &kind, + &model, &icon, &percentage, &state, @@ -1060,6 +1101,7 @@ indicator_power_device_new_from_variant (GVariant * v) return indicator_power_device_new (object_path, kind, + model, percentage, state, (time_t)time, diff --git a/src/device.h b/src/device.h index fa41fe0..d9e9264 100644 --- a/src/device.h +++ b/src/device.h @@ -42,6 +42,7 @@ typedef struct _IndicatorPowerDeviceClass IndicatorPowerDeviceClass; typedef struct _IndicatorPowerDevicePrivate IndicatorPowerDevicePrivate; #define INDICATOR_POWER_DEVICE_KIND "kind" +#define INDICATOR_POWER_DEVICE_MODEL "model" #define INDICATOR_POWER_DEVICE_STATE "state" #define INDICATOR_POWER_DEVICE_OBJECT_PATH "object-path" #define INDICATOR_POWER_DEVICE_PERCENTAGE "percentage" @@ -125,6 +126,7 @@ GType indicator_power_device_get_type (void); IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, + const gchar * model, gdouble percentage, UpDeviceState state, time_t time, @@ -132,12 +134,13 @@ IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path, /** * Convenience wrapper around indicator_power_device_new() - * @variant holds the same args as indicator_power_device_new() in "(susdut)" + * @variant holds the same args as indicator_power_device_new() in "(sussdut)" */ IndicatorPowerDevice* indicator_power_device_new_from_variant (GVariant * variant); UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device); +const gchar * indicator_power_device_get_model (const IndicatorPowerDevice * device); UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device); const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device); gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device); @@ -148,7 +151,7 @@ GStrv indicator_power_device_get_icon_names (const IndicatorPower GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device, gboolean panel); -char * indicator_power_device_get_readable_text (const IndicatorPowerDevice * device); +char * indicator_power_device_get_readable_text (const IndicatorPowerDevice * device, gboolean bModelName); char * indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device); diff --git a/src/service.c b/src/service.c index 32f8788..bdf4caa 100644 --- a/src/service.c +++ b/src/service.c @@ -517,11 +517,18 @@ create_devices_section (IndicatorPowerService * self, int profile) if (kind == UP_DEVICE_KIND_BATTERY) { - sLabel = g_strdup (_("Charge level")); + if (!ayatana_common_utils_is_lomiri()) + { + sLabel = indicator_power_device_get_readable_text (device, FALSE); + } + else + { + sLabel = g_strdup (_("Charge level")); + } } else { - sLabel = indicator_power_device_get_readable_text (device); + sLabel = indicator_power_device_get_readable_text (device, TRUE); } gchar *sIndicator = NULL; @@ -1418,6 +1425,7 @@ create_totalled_battery_device (const GList * devices) device = indicator_power_device_new (NULL, UP_DEVICE_KIND_BATTERY, + NULL, percent, state, time_left, diff --git a/src/testing.c b/src/testing.c index 0d002a1..3633cfe 100644 --- a/src/testing.c +++ b/src/testing.c @@ -1,5 +1,6 @@ /* * Copyright 2014 Canonical Ltd. + * Copyright 2023 Robert Tari * * 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 @@ -15,6 +16,7 @@ * * Authors: * Charles Kerr + * Robert Tari */ #include "dbus-shared.h" @@ -299,6 +301,7 @@ indicator_power_testing_init (IndicatorPowerTesting * self) p->battery_mock = indicator_power_device_new("/some/path", UP_DEVICE_KIND_BATTERY, + "Some Model", 50.0, UP_DEVICE_STATE_DISCHARGING, 60*30, diff --git a/tests/test-device.cc b/tests/test-device.cc index 7154fcd..f2d4b8f 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -1,6 +1,6 @@ /* * Copyright 2012-2016 Canonical Ltd. - * Copyright 2022 Robert Tari + * Copyright 2022-2023 Robert Tari * * 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 @@ -83,7 +83,7 @@ class DeviceTest : public ::testing::Test void check_label (const IndicatorPowerDevice * device, const char * expected_label) { - char * label = indicator_power_device_get_readable_text (device); + char * label = indicator_power_device_get_readable_text (device, FALSE); EXPECT_STREQ (expected_label, label); g_free (label); } @@ -205,6 +205,7 @@ TEST_F(DeviceTest, New) { IndicatorPowerDevice * device = indicator_power_device_new ("/object/path", UP_DEVICE_KIND_BATTERY, + "Some Model", 50.0, UP_DEVICE_STATE_CHARGING, 30, @@ -212,6 +213,7 @@ TEST_F(DeviceTest, New) ASSERT_TRUE (device != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_STREQ ("Some Model", indicator_power_device_get_model(device)); ASSERT_EQ (UP_DEVICE_STATE_CHARGING, indicator_power_device_get_state(device)); ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device)); ASSERT_EQ (50, int(indicator_power_device_get_percentage(device))); @@ -224,9 +226,10 @@ TEST_F(DeviceTest, New) TEST_F(DeviceTest, NewFromVariant) { - auto variant = g_variant_new("(susdutb)", + auto variant = g_variant_new("(sussdutb)", "/object/path", guint32(UP_DEVICE_KIND_BATTERY), + "Some Model", "icon", 50.0, guint32(UP_DEVICE_STATE_CHARGING), @@ -237,6 +240,7 @@ TEST_F(DeviceTest, NewFromVariant) ASSERT_TRUE (device != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_STREQ ("Some Model", indicator_power_device_get_model(device)); ASSERT_EQ (UP_DEVICE_STATE_CHARGING, indicator_power_device_get_state(device)); ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device)); ASSERT_EQ (50, int(indicator_power_device_get_percentage(device))); @@ -841,8 +845,10 @@ namespace { std::ostringstream o; const auto path = indicator_power_device_get_object_path(device); + const gchar *model = indicator_power_device_get_model(device); o << kind2str(indicator_power_device_get_kind(device)) + << ' ' << (model ? model : "noname") << ' ' << state2str(indicator_power_device_get_state(device)) << ' ' << indicator_power_device_get_time(device)<<'m' << ' ' << int(ceil(indicator_power_device_get_percentage(device)))<<'%' @@ -855,14 +861,15 @@ namespace IndicatorPowerDevice* str2device(const std::string& str) { auto tokens = g_strsplit(str.c_str(), " ", 0); - g_assert(6u == g_strv_length(tokens)); + g_assert(7u == g_strv_length(tokens)); const auto kind = str2kind(tokens[0]); - const auto state = str2state(tokens[1]); - const time_t time = atoi(tokens[2]); - const double pct = strtod(tokens[3],nullptr); - const char* path = !g_strcmp0(tokens[4],"nopath") ? nullptr : tokens[4]; - const gboolean power_supply = atoi(tokens[5]); - auto ret = indicator_power_device_new(path, kind, pct, state, time, power_supply); + const char* model = !g_strcmp0(tokens[1],"noname") ? nullptr : tokens[1]; + const auto state = str2state(tokens[2]); + const time_t time = atoi(tokens[3]); + const double pct = strtod(tokens[4],nullptr); + const char* path = !g_strcmp0(tokens[5],"nopath") ? nullptr : tokens[5]; + const gboolean power_supply = atoi(tokens[6]); + auto ret = indicator_power_device_new(path, kind, model, pct, state, time, power_supply); g_strfreev(tokens); return ret; } @@ -885,100 +892,100 @@ TEST_F(DeviceTest, ChoosePrimary) } tests[] = { { "one discharging battery", - "battery discharging 10m 60% bat01 1", - { "battery discharging 10m 60% bat01 1" } + "battery noname discharging 10m 60% bat01 1", + { "battery noname discharging 10m 60% bat01 1" } }, { "merge two discharging batteries", - "battery discharging 20m 70% nopath 1", - { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1" } + "battery noname discharging 20m 70% nopath 1", + { "battery noname discharging 10m 60% bat01 1", "battery noname discharging 20m 80% bat02 1" } }, { "merge two other discharging batteries", - "battery discharging 30m 90% nopath 1", - { "battery discharging 20m 80% bat01 1", "battery discharging 30m 100% bat02 1" } + "battery noname discharging 30m 90% nopath 1", + { "battery noname discharging 20m 80% bat01 1", "battery noname discharging 30m 100% bat02 1" } }, { "merge three discharging batteries", - "battery discharging 30m 80% nopath 1", - { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1", "battery discharging 30m 100% bat03 1" } + "battery noname discharging 30m 80% nopath 1", + { "battery noname discharging 10m 60% bat01 1", "battery noname discharging 20m 80% bat02 1", "battery noname discharging 30m 100% bat03 1" } }, { "one charging battery", - "battery charging 10m 60% bat01 1", - { "battery charging 10m 60% bat01 1" } + "battery noname charging 10m 60% bat01 1", + { "battery noname charging 10m 60% bat01 1" } }, { "merge two charging batteries", - "battery charging 20m 70% nopath 1", - { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1" } + "battery noname charging 20m 70% nopath 1", + { "battery noname charging 10m 60% bat01 1", "battery noname charging 20m 80% bat02 1" } }, { "merge two other charging batteries", - "battery charging 30m 90% nopath 1", - { "battery charging 20m 80% bat01 1", "battery charging 30m 100% bat02 1" } + "battery noname charging 30m 90% nopath 1", + { "battery noname charging 20m 80% bat01 1", "battery noname charging 30m 100% bat02 1" } }, { "merge three charging batteries", - "battery charging 30m 80% nopath 1", - { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1", "battery charging 30m 100% bat03 1" } + "battery noname charging 30m 80% nopath 1", + { "battery noname charging 10m 60% bat01 1", "battery noname charging 20m 80% bat02 1", "battery noname charging 30m 100% bat03 1" } }, { "one charged battery", - "battery charged 0m 100% bat01 1", - { "battery charged 0m 100% bat01 1" } + "battery noname charged 0m 100% bat01 1", + { "battery noname charged 0m 100% bat01 1" } }, { "merge one charged, one discharging", - "battery discharging 10m 80% nopath 1", - { "battery charged 0m 100% bat01 1", "battery discharging 10m 60% bat02 1" } + "battery noname discharging 10m 80% nopath 1", + { "battery noname charged 0m 100% bat01 1", "battery noname discharging 10m 60% bat02 1" } }, { "merged one charged, one charging", - "battery charging 10m 80% nopath 1", - { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1" } + "battery noname charging 10m 80% nopath 1", + { "battery noname charged 0m 100% bat01 1", "battery noname charging 10m 60% bat02 1" } }, { "merged one charged, one charging, one discharging", - "battery discharging 10m 74% nopath 1", - { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1", "battery discharging 10m 60% bat03 1" } + "battery noname discharging 10m 74% nopath 1", + { "battery noname charged 0m 100% bat01 1", "battery noname charging 10m 60% bat02 1", "battery noname discharging 10m 60% bat03 1" } }, { "one discharging mouse and one discharging battery. ignore mouse because it doesn't supply the power", - "battery discharging 10m 60% bat01 1", - { "battery discharging 10m 60% bat01 1", "mouse discharging 20m 80% mouse01 0" } + "battery noname discharging 10m 60% bat01 1", + { "battery noname discharging 10m 60% bat01 1", "mouse noname discharging 20m 80% mouse01 0" } }, { "one discharging mouse and a different discharging battery. ignore mouse because it doesn't supply the power", - "battery discharging 30m 100% bat01 1", - { "battery discharging 30m 100% bat01 1", "mouse discharging 20m 80% mouse01 0" } + "battery noname discharging 30m 100% bat01 1", + { "battery noname discharging 30m 100% bat01 1", "mouse noname discharging 20m 80% mouse01 0" } }, { "everything comes before power lines #1", - "battery discharging 10m 60% bat01 1", - { "battery discharging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" } + "battery noname discharging 10m 60% bat01 1", + { "battery noname discharging 10m 60% bat01 1", "line-power noname unknown 0m 0% lp01 1" } }, { "everything comes before power lines #2", - "battery charging 10m 60% bat01 1", - { "battery charging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" } + "battery noname charging 10m 60% bat01 1", + { "battery noname charging 10m 60% bat01 1", "line-power noname unknown 0m 0% lp01 1" } }, { "everything comes before power lines #3 except that the mouse doesn't supply the power", - "line-power unknown 0m 0% lp01 1", - { "mouse discharging 20m 80% mouse01 0", "line-power unknown 0m 0% lp01 1" } + "line-power noname unknown 0m 0% lp01 1", + { "mouse noname discharging 20m 80% mouse01 0", "line-power noname unknown 0m 0% lp01 1" } }, { // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 "don't select a device with unknown state when we have another device with a known state...", - "battery charged 0m 100% bat01 1", - { "battery charged 0m 100% bat01 1", "phone unknown 0m 61% phone01 1" } + "battery noname charged 0m 100% bat01 1", + { "battery noname charged 0m 100% bat01 1", "phone noname unknown 0m 61% phone01 1" } }, { // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 "...but do select the unknown state device if nothing else is available", - "phone unknown 0m 61% phone01 1", - { "phone unknown 0m 61% phone01 1" } + "phone noname unknown 0m 61% phone01 1", + { "phone noname unknown 0m 61% phone01 1" } } }; diff --git a/tests/test-notify.cc b/tests/test-notify.cc index 815b5d7..12a80fb 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -1,6 +1,6 @@ /* * Copyright 2014-2016 Canonical Ltd. - * Copyright 2021-2022 Robert Tari + * Copyright 2021-2023 Robert Tari * * 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 @@ -236,6 +236,7 @@ TEST_F(NotifyFixture, PercentageToLevel) { auto battery = indicator_power_device_new ("/object/path", UP_DEVICE_KIND_BATTERY, + "Some Model", 50.0, UP_DEVICE_STATE_DISCHARGING, 30, @@ -315,6 +316,7 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) { auto battery = indicator_power_device_new ("/object/path", UP_DEVICE_KIND_BATTERY, + "Some Model", 50.0, UP_DEVICE_STATE_DISCHARGING, 30, @@ -388,6 +390,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) auto battery = indicator_power_device_new ("/object/path", UP_DEVICE_KIND_BATTERY, + "Some Model", percent_low + 1.0, UP_DEVICE_STATE_DISCHARGING, 30, diff --git a/tests/test-service.cc b/tests/test-service.cc index b2d4dc4..3db4a0e 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -1,8 +1,10 @@ /* * Copyright 2013 Canonical Ltd. + * Copyright 2023 Robert Tari * * Authors: * Charles Kerr + * Robert Tari * * 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 @@ -70,12 +72,12 @@ class IndicatorTest : public ::testing::Test ac_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/line_power_AC", - UP_DEVICE_KIND_LINE_POWER, + UP_DEVICE_KIND_LINE_POWER, "Some Model", 0.0, UP_DEVICE_STATE_UNKNOWN, 0); battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", - UP_DEVICE_KIND_BATTERY, + UP_DEVICE_KIND_BATTERY, "Some Model", 52.871712, UP_DEVICE_STATE_DISCHARGING, 8834); } @@ -247,7 +249,7 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) NULL); IndicatorPowerDevice * bad_battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", - UP_DEVICE_KIND_BATTERY, + UP_DEVICE_KIND_BATTERY, "Some Model", 53, UP_DEVICE_STATE_CHARGING, 0); GSList * devices = NULL; @@ -270,34 +272,34 @@ TEST_F(IndicatorTest, OtherDevices) 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, + "/org/freedesktop/UPower/devices/mouse", UP_DEVICE_KIND_MOUSE, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS, + "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD, + "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA, + "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE, + "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR, + "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR, "Some Model", 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, + "/org/freedesktop/UPower/devices/media_player", UP_DEVICE_KIND_MEDIA_PLAYER, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET, + "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER, + "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( - "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN, + "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN, "Some Model", 0, UP_DEVICE_STATE_UNKNOWN, 0)); indicator_power_set_devices (power, devices); -- cgit v1.2.3 From 8f7c0cdbfd3ac5166a9628f917a0184fe5a44628 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Wed, 5 Apr 2023 13:51:53 +0200 Subject: Do not use charge-aware icons in the menu --- src/device.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/device.h | 2 +- src/service.c | 4 ++-- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/device.c b/src/device.c index a373981..6e79557 100644 --- a/src/device.c +++ b/src/device.c @@ -436,6 +436,42 @@ device_kind_to_string (UpDeviceKind kind) } } +static const char *device_kind_to_icon_name (UpDeviceKind kind) +{ + switch (kind) + { + case UP_DEVICE_KIND_LINE_POWER: return "ac-adapter"; + case UP_DEVICE_KIND_BATTERY: return "battery"; + case UP_DEVICE_KIND_UPS: return "gpm-ups-charged"; + case UP_DEVICE_KIND_MONITOR: return "display"; + case UP_DEVICE_KIND_MOUSE: return "mouse"; + case UP_DEVICE_KIND_KEYBOARD: return "keyboard"; + case UP_DEVICE_KIND_PDA: return "pda"; + case UP_DEVICE_KIND_PHONE: return "phone"; + case UP_DEVICE_KIND_MEDIA_PLAYER: return "multimedia-player"; + case UP_DEVICE_KIND_TABLET: return "tablet"; + case UP_DEVICE_KIND_COMPUTER: return "computer"; + case UP_DEVICE_KIND_GAMING_INPUT: return "input-gaming"; + case UP_DEVICE_KIND_PEN: return "input-tablet"; + case UP_DEVICE_KIND_TOUCHPAD: return "input-touchpad"; + case UP_DEVICE_KIND_MODEM: return "modem"; + case UP_DEVICE_KIND_NETWORK: return "network-wireless"; + case UP_DEVICE_KIND_HEADSET: return "audio-headset"; + case UP_DEVICE_KIND_SPEAKERS: return "audio-speakers"; + case UP_DEVICE_KIND_HEADPHONES: return "audio-headphones"; + case UP_DEVICE_KIND_VIDEO: return "camera-video"; + case UP_DEVICE_KIND_OTHER_AUDIO: return "audio-card"; + case UP_DEVICE_KIND_REMOTE_CONTROL: return "remote-control"; + case UP_DEVICE_KIND_PRINTER: return "printer"; + case UP_DEVICE_KIND_SCANNER: return "scanner"; + case UP_DEVICE_KIND_CAMERA: return "camera-photo"; + case UP_DEVICE_KIND_WEARABLE: return "wearable"; + case UP_DEVICE_KIND_TOY: return "toy"; + case UP_DEVICE_KIND_BLUETOOTH_GENERIC: return "blueman-device"; + default: return "unknown"; + } +} + /** indicator_power_device_get_icon_names: @device: #IndicatorPowerDevice from which to generate the icon names @@ -595,6 +631,7 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device, gboo indicator_power_device_get_gicon: @device: #IndicatorPowerDevice to generate the icon names from @panel: Whether to prefer panel icons + @bShowCharge: Whether to return a charge-aware icon A convenience function to call g_themed_icon_new_from_names() with the names returned by indicator_power_device_get_icon_names() @@ -602,11 +639,23 @@ indicator_power_device_get_icon_names (const IndicatorPowerDevice * device, gboo Return value: (transfer full): A themed GIcon */ GIcon * -indicator_power_device_get_gicon (const IndicatorPowerDevice * device, gboolean panel) +indicator_power_device_get_gicon (const IndicatorPowerDevice * device, gboolean panel, gboolean bShowCharge) { - GStrv names = indicator_power_device_get_icon_names (device, panel); - GIcon * icon = g_themed_icon_new_from_names (names, -1); - g_strfreev (names); + GIcon * icon = NULL; + + if (!bShowCharge) + { + const UpDeviceKind nKind = indicator_power_device_get_kind (device); + const gchar *sIcon = device_kind_to_icon_name (nKind); + icon = g_themed_icon_new_with_default_fallbacks (sIcon); + } + else + { + GStrv names = indicator_power_device_get_icon_names (device, panel); + icon = g_themed_icon_new_from_names (names, -1); + g_strfreev (names); + } + return icon; } diff --git a/src/device.h b/src/device.h index d9e9264..169305b 100644 --- a/src/device.h +++ b/src/device.h @@ -148,7 +148,7 @@ time_t indicator_power_device_get_time (const IndicatorPower gboolean indicator_power_device_get_power_supply (const IndicatorPowerDevice * device); GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device, gboolean panel); -GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device, gboolean panel); +GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device, gboolean panel, gboolean bShowCharge); char * indicator_power_device_get_readable_text (const IndicatorPowerDevice * device, gboolean bModelName); diff --git a/src/service.c b/src/service.c index bdf4caa..a7b3f10 100644 --- a/src/service.c +++ b/src/service.c @@ -466,7 +466,7 @@ create_header_state (IndicatorPowerService * self) g_free (title); } - if ((icon = indicator_power_device_get_gicon (p->primary_device, TRUE))) + if ((icon = indicator_power_device_get_gicon (p->primary_device, TRUE, TRUE))) { GVariant * serialized_icon = g_icon_serialize (icon); @@ -553,7 +553,7 @@ create_devices_section (IndicatorPowerService * self, int profile) if (!ayatana_common_utils_is_lomiri()) { - GIcon * icon = indicator_power_device_get_gicon (device, FALSE); + GIcon * icon = indicator_power_device_get_gicon (device, FALSE, FALSE); if (icon) { -- cgit v1.2.3