Commit c5755b02 authored by Simon Steinbeiss's avatar Simon Steinbeiss Committed by Simon Steinbeiß
Browse files

Use more accurate status icons (Fixes #95)

Previously we relied entirely on UPower, now we also check if the icons
used by Gnome are available (battery-level-*).
parent 7d523934
......@@ -24,6 +24,7 @@
G_BEGIN_DECLS
#define XFPM_AC_ADAPTER_ICON "ac-adapter"
#define XFPM_BATTERY_LEVEL_ICON "battery-level"
#define XFPM_COMPUTER_ICON "computer"
#define XFPM_DISPLAY_BRIGHTNESS_ICON "display-brightness-symbolic"
#define XFPM_MOUSE_ICON "input-mouse"
......
......@@ -96,6 +96,33 @@ xfpm_power_translate_technology (guint value)
return _("Unknown");
}
const gchar * G_GNUC_CONST
xfpm_battery_get_icon_index (UpDeviceKind type, guint percent)
{
if (percent < 10)
return "0";
else if (percent < 20)
return "10";
else if (percent < 30)
return "20";
else if (percent < 40)
return "30";
else if (percent < 50)
return "40";
else if (percent < 60)
return "50";
else if (percent < 70)
return "60";
else if (percent < 80)
return "70";
else if (percent < 90)
return "80";
else if (percent < 100)
return "90";
else
return "100";
}
/*
* Taken from gpm
*/
......@@ -160,18 +187,28 @@ is_display_device (UpClient *upower, UpDevice *device)
}
gchar*
get_device_icon_name (UpClient *upower, UpDevice *device)
get_device_panel_icon_name (UpClient *upower, UpDevice *device)
{
return get_device_icon_name (upower, device, TRUE);
}
gchar*
get_device_icon_name (UpClient *upower, UpDevice *device, gboolean is_panel)
{
gchar *icon_name = NULL;
gchar *icon_suffix;
gsize icon_base_length;
gchar *upower_icon;
guint type = 0;
guint type = 0, state = 0;
gdouble percentage;
/* hack, this depends on XFPM_DEVICE_TYPE_* being in sync with UP_DEVICE_KIND_* */
g_object_get (device,
"kind", &type,
"state", &state,
"icon-name", &upower_icon,
"percentage", &percentage,
NULL);
/* Strip away the symbolic suffix for the device icons for the devices tab
......@@ -193,7 +230,14 @@ get_device_icon_name (UpClient *upower, UpDevice *device)
* http://cgit.freedesktop.org/upower/tree/libupower-glib/up-types.h
* because UPower doesn't return device-specific icon-names
*/
if ( type == UP_DEVICE_KIND_UPS )
if ( type == UP_DEVICE_KIND_BATTERY && is_panel )
{
if ( state == UP_DEVICE_STATE_CHARGING || state == UP_DEVICE_STATE_PENDING_CHARGE)
icon_name = g_strdup_printf ("%s-%s-%s", XFPM_BATTERY_LEVEL_ICON, xfpm_battery_get_icon_index (type, percentage), "charging");
else if ( state == UP_DEVICE_STATE_DISCHARGING || state == UP_DEVICE_STATE_PENDING_DISCHARGE)
icon_name = g_strdup_printf ("%s-%s", XFPM_BATTERY_LEVEL_ICON, xfpm_battery_get_icon_index (type, percentage));
}
else if ( type == UP_DEVICE_KIND_UPS )
icon_name = g_strdup (XFPM_UPS_ICON);
else if ( type == UP_DEVICE_KIND_MOUSE )
icon_name = g_strdup (XFPM_MOUSE_ICON);
......
......@@ -43,12 +43,17 @@
#define POLKIT_AUTH_SUSPEND_CONSOLEKIT2 "org.freedesktop.consolekit.system.suspend"
#define POLKIT_AUTH_HIBERNATE_CONSOLEKIT2 "org.freedesktop.consolekit.system.hibernate"
const gchar *xfpm_power_translate_device_type (guint type);
const gchar *xfpm_power_translate_technology (guint value);
gchar *xfpm_battery_get_time_string (guint seconds);
gchar *get_device_icon_name (UpClient *upower,
UpDevice *device);
gchar *get_device_description (UpClient *upower,
UpDevice *device);
const gchar *xfpm_power_translate_device_type (guint type);
const gchar *xfpm_power_translate_technology (guint value);
const gchar *G_GNUC_CONST xfpm_battery_get_icon_index (UpDeviceKind type,
guint percent);
gchar *xfpm_battery_get_time_string (guint seconds);
gchar *get_device_panel_icon_name (UpClient *upower,
UpDevice *device);
gchar *get_device_icon_name (UpClient *upower,
UpDevice *device,
gboolean is_panel);
gchar *get_device_description (UpClient *upower,
UpDevice *device);
#endif /* XFPM_UPOWER_COMMON */
......@@ -79,6 +79,7 @@ struct PowerManagerButtonPrivate
GtkWidget *hbox;
/* Keep track of icon name to redisplay during size changes */
gchar *panel_icon_name;
gchar *panel_fallback_icon_name;
/* Keep track of the last icon size for use during updates */
gint panel_icon_width;
/* Keep track of the tooltip */
......@@ -248,7 +249,10 @@ power_manager_button_set_icon (PowerManagerButton *button)
{
g_return_if_fail (GTK_IS_WIDGET (button->priv->panel_presentation_mode));
gtk_image_set_from_icon_name (GTK_IMAGE (button->priv->panel_icon_image), button->priv->panel_icon_name, GTK_ICON_SIZE_BUTTON);
if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default(), button->priv->panel_icon_name))
gtk_image_set_from_icon_name (GTK_IMAGE (button->priv->panel_icon_image), button->priv->panel_icon_name, GTK_ICON_SIZE_BUTTON);
else
gtk_image_set_from_icon_name (GTK_IMAGE (button->priv->panel_icon_image), button->priv->panel_fallback_icon_name, GTK_ICON_SIZE_BUTTON);
gtk_image_set_pixel_size (GTK_IMAGE (button->priv->panel_icon_image), button->priv->panel_icon_width);
gtk_image_set_pixel_size (GTK_IMAGE (button->priv->panel_presentation_mode), button->priv->panel_icon_width);
......@@ -299,13 +303,13 @@ power_manager_button_set_tooltip (PowerManagerButton *button)
const gchar *
power_manager_button_get_icon_name (PowerManagerButton *button)
{
return button->priv->panel_icon_name;
return button->priv->panel_icon_name;
}
const gchar *
power_manager_button_get_tooltip (PowerManagerButton *button)
{
return button->priv->tooltip;
return button->priv->tooltip;
}
static void
......@@ -462,6 +466,7 @@ power_manager_button_update_device_icon_and_details (PowerManagerButton *button,
const gchar *object_path = up_device_get_object_path(device);
gchar *details;
gchar *icon_name;
gchar *menu_icon_name;
GdkPixbuf *pix = NULL;
XFPM_DEBUG("entering for %s", object_path);
......@@ -476,22 +481,23 @@ power_manager_button_update_device_icon_and_details (PowerManagerButton *button,
battery_device = item->data;
icon_name = get_device_icon_name (button->priv->upower, device);
icon_name = get_device_icon_name (button->priv->upower, device, TRUE);
menu_icon_name = get_device_icon_name (button->priv->upower, device, FALSE);
details = get_device_description (button->priv->upower, device);
/* If UPower doesn't give us an icon, just use the default */
if (g_strcmp0(icon_name, "") == 0)
if (g_strcmp0(menu_icon_name, "") == 0)
{
/* ignore empty icon names */
g_free (icon_name);
icon_name = NULL;
g_free (menu_icon_name);
menu_icon_name = NULL;
}
if (icon_name == NULL)
icon_name = g_strdup (PANEL_DEFAULT_ICON);
if (menu_icon_name == NULL)
menu_icon_name = g_strdup (PANEL_DEFAULT_ICON);
pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
icon_name,
menu_icon_name,
32,
GTK_ICON_LOOKUP_USE_BUILTIN,
NULL);
......@@ -515,8 +521,10 @@ power_manager_button_update_device_icon_and_details (PowerManagerButton *button,
g_free (button->priv->panel_icon_name);
#ifdef XFCE_PLUGIN
button->priv->panel_icon_name = g_strdup_printf ("%s-%s", icon_name, "symbolic");
button->priv->panel_fallback_icon_name = g_strdup_printf ("%s-%s", menu_icon_name, "symbolic");
#else
button->priv->panel_icon_name = g_strdup (icon_name);
button->priv->panel_fallback_icon_name = g_strdup (menu_icon_name);
#endif
power_manager_button_set_icon (button);
/* update the tooltip */
......@@ -525,6 +533,7 @@ power_manager_button_update_device_icon_and_details (PowerManagerButton *button,
power_manager_button_update_label (button, device);
}
g_free (icon_name);
g_free (menu_icon_name);
/* If the menu is being displayed, update it */
if (button->priv->menu && battery_device->menu_item)
......@@ -1023,8 +1032,10 @@ power_manager_button_init (PowerManagerButton *button)
/* Sane defaults for the systray and panel icon */
#ifdef XFCE_PLUGIN
button->priv->panel_icon_name = g_strdup (PANEL_DEFAULT_ICON_SYMBOLIC);
button->priv->panel_fallback_icon_name = g_strdup (PANEL_DEFAULT_ICON_SYMBOLIC);
#else
button->priv->panel_icon_name = g_strdup (PANEL_DEFAULT_ICON);
button->priv->panel_fallback_icon_name = g_strdup (PANEL_DEFAULT_ICON);
#endif
button->priv->panel_icon_width = 24;
......@@ -1053,7 +1064,8 @@ power_manager_button_finalize (GObject *object)
button = POWER_MANAGER_BUTTON (object);
g_free(button->priv->panel_icon_name);
g_free (button->priv->panel_icon_name);
g_free (button->priv->panel_fallback_icon_name);
if (button->priv->set_level_timeout)
{
......@@ -1303,7 +1315,8 @@ power_manager_button_update_label (PowerManagerButton *button, UpDevice *device)
power_manager_button_set_label (button, percentage, time_to_full);
else if (state == UP_DEVICE_STATE_FULLY_CHARGED
|| state == UP_DEVICE_STATE_UNKNOWN
|| g_strcmp0 (button->priv->panel_icon_name, "ac-adapter-symbolic") == 0)
|| g_strcmp0 (button->priv->panel_icon_name, "ac-adapter-symbolic") == 0
|| g_strcmp0 (button->priv->panel_fallback_icon_name, "ac-adapter-symbolic") == 0)
gtk_widget_hide (GTK_WIDGET (button->priv->panel_label));
else
power_manager_button_set_label (button, percentage, time_to_empty);
......
......@@ -1968,7 +1968,7 @@ update_sideview_icon (UpDevice *device)
name = get_device_description (upower, device);
icon_name = get_device_icon_name (upower, device);
icon_name = get_device_icon_name (upower, device, FALSE);
pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
icon_name,
......
......@@ -514,5 +514,5 @@ xfpm_battery_get_icon_name (XfpmBattery *battery)
{
g_return_val_if_fail (XFPM_IS_BATTERY (battery), NULL);
return get_device_icon_name (battery->priv->client, battery->priv->device);
return get_device_icon_name (battery->priv->client, battery->priv->device, TRUE);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment