[Xfce4-commits] [xfce/xfce4-power-manager] 07/64: Change battery icons to reflect its status

noreply at xfce.org noreply at xfce.org
Mon May 26 06:00:02 CEST 2014


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-power-manager.

commit 70e71be089406f7ab97ba5913f90e6912d380faf
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Thu May 1 16:04:39 2014 +0300

    Change battery icons to reflect its status
---
 common/xfpm-power-common.c             |  110 +++++++++++++++++++++++++++++
 common/xfpm-power-common.h             |    8 ++-
 panel-plugins/battery/battery-button.c |  120 ++++++++++++++++++++++----------
 src/xfpm-battery.c                     |   27 -------
 4 files changed, 200 insertions(+), 65 deletions(-)

diff --git a/common/xfpm-power-common.c b/common/xfpm-power-common.c
index 30988ff..0d321b9 100644
--- a/common/xfpm-power-common.c
+++ b/common/xfpm-power-common.c
@@ -197,3 +197,113 @@ xfpm_battery_get_time_string (guint seconds)
 			    minutes, ngettext ("minute", "minutes", minutes));
     return timestring;
 }
+
+gchar *
+xfpm_battery_get_icon_prefix_device_enum_type (UpDeviceKind type)
+{
+    if ( type == UP_DEVICE_KIND_BATTERY )
+    {
+	return g_strdup (XFPM_PRIMARY_ICON_PREFIX);
+    }
+    else if ( type == UP_DEVICE_KIND_UPS )
+    {
+	return g_strdup (XFPM_UPS_ICON_PREFIX);
+    }
+    else if ( type == UP_DEVICE_KIND_MOUSE )
+    {
+	return g_strdup (XFPM_MOUSE_ICON_PREFIX);
+    }
+    else if ( type == UP_DEVICE_KIND_KEYBOARD )
+    {
+	return g_strdup (XFPM_KBD_ICON_PREFIX);
+    }
+    else if ( type == UP_DEVICE_KIND_PHONE )
+    {
+	return g_strdup (XFPM_PHONE_ICON_PREFIX);
+    }
+
+    return g_strdup (XFPM_PRIMARY_ICON_PREFIX);
+}
+
+gchar*
+get_device_icon_name (UpClient *upower, UpDevice *device)
+{
+    gchar *icon_name = NULL, *icon_prefix;
+    guint type = 0, state = 0;
+    gboolean on_battery, battery_low = FALSE;
+    gboolean present;
+    gdouble percentage;
+
+    /* hack, this depends on XFPM_DEVICE_TYPE_* being in sync with UP_DEVICE_KIND_* */
+    g_object_get (device,
+		  "kind", &type,
+		  "state", &state,
+		  "is-present", &present,
+		  "percentage", &percentage,
+		   NULL);
+
+    g_object_get (upower,
+                  "on-battery", &on_battery,
+#if UP_CHECK_VERSION(0, 99, 0)
+		  "OnLowBattery", &battery_low,
+#endif
+		  NULL);
+
+    icon_prefix = xfpm_battery_get_icon_prefix_device_enum_type (type);
+
+    if ( type == UP_DEVICE_KIND_LINE_POWER )
+    {
+	if ( !on_battery )
+	{
+	    icon_name = g_strdup_printf ("%s", XFPM_AC_ADAPTER_ICON);
+	}
+	else if ( battery_low )
+	{
+	    icon_name = g_strdup_printf ("%s100", icon_prefix);
+	}
+	else
+	{
+	    icon_name = g_strdup_printf ("%s100", icon_prefix);
+	}
+    }
+    else if ( type == UP_DEVICE_KIND_BATTERY || type == UP_DEVICE_KIND_UPS )
+    {
+	if (!present)
+	{
+	    icon_name = g_strdup_printf ("%s%s", icon_prefix, "missing");
+	}
+	else if (state == UP_DEVICE_STATE_FULLY_CHARGED )
+	{
+	    icon_name = g_strdup_printf ("%s%s", icon_prefix, on_battery ? "100" : "charged");
+	}
+	else if ( state == UP_DEVICE_STATE_CHARGING || state == UP_DEVICE_STATE_PENDING_CHARGE)
+	{
+	    icon_name = g_strdup_printf ("%s%s-%s", icon_prefix, 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", icon_prefix, xfpm_battery_get_icon_index (type, percentage));
+	}
+	else if ( state == UP_DEVICE_STATE_EMPTY)
+	{
+	    icon_name = g_strdup_printf ("%s%s", icon_prefix, on_battery ? "000" : "000-charging");
+	}
+    }
+    else
+    {
+	if ( !present || state == UP_DEVICE_STATE_EMPTY )
+	{
+	    icon_name = g_strdup_printf ("%s000", icon_prefix);
+	}
+	else if ( state == UP_DEVICE_STATE_FULLY_CHARGED )
+	{
+	    icon_name = g_strdup_printf ("%s100", icon_prefix);
+	}
+	else if ( state == UP_DEVICE_STATE_DISCHARGING || state == UP_DEVICE_STATE_CHARGING )
+	{
+	    icon_name = g_strdup_printf ("%s%s", icon_prefix, xfpm_battery_get_icon_index (type, percentage));
+	}
+    }
+
+    return icon_name;
+}
diff --git a/common/xfpm-power-common.h b/common/xfpm-power-common.h
index 708ddd5..920dff4 100644
--- a/common/xfpm-power-common.h
+++ b/common/xfpm-power-common.h
@@ -47,8 +47,12 @@ const gchar	*xfpm_power_translate_technology	(guint value);
 
 const gchar	*xfpm_power_get_icon_name		(guint device_type);
 
-const gchar * G_GNUC_CONST xfpm_battery_get_icon_index (UpDeviceKind type, guint percent);
+const gchar *G_GNUC_CONST xfpm_battery_get_icon_index (UpDeviceKind type, guint percent);
 
-gchar * xfpm_battery_get_time_string (guint seconds);
+gchar *xfpm_battery_get_time_string (guint seconds);
+
+gchar *xfpm_battery_get_icon_prefix_device_enum_type (UpDeviceKind type);
+
+gchar *get_device_icon_name (UpClient *upower, UpDevice *device);
 
 #endif /* XFPM_UPOWER_COMMON */
diff --git a/panel-plugins/battery/battery-button.c b/panel-plugins/battery/battery-button.c
index 41566dc..d037363 100644
--- a/panel-plugins/battery/battery-button.c
+++ b/panel-plugins/battery/battery-button.c
@@ -79,6 +79,7 @@ enum
 };
 
 static gchar* get_device_description (UpClient *upower, UpDevice *device);
+static GtkTreeIter* find_device_in_tree (BatteryButton *button, const gchar *object_path);
 
 G_DEFINE_TYPE (BatteryButton, battery_button, GTK_TYPE_BUTTON)
 
@@ -340,7 +341,6 @@ get_device_description (UpClient *upower, UpDevice *device)
 {
     gchar *tip = NULL;
     gchar *est_time_str = NULL;
-    gchar *power_status = NULL;
     guint type = 0, state = 0;
     gchar *model = NULL, *vendor = NULL;
     gboolean on_battery;
@@ -458,7 +458,6 @@ get_device_description (UpClient *upower, UpDevice *device)
 			       percentage);
     }
 
-    g_free (power_status);
     return tip;
 }
 
@@ -492,7 +491,9 @@ device_changed_cb (UpDevice *device, BatteryButton *button)
 {
     GtkTreeIter *iter;
     const gchar *object_path = up_device_get_object_path(device);
-    gchar *details;
+    gchar *details, *icon_name;
+    GdkPixbuf *pix;
+    guint type;
 
     TRACE("entering for %s", object_path);
 
@@ -501,13 +502,45 @@ device_changed_cb (UpDevice *device, BatteryButton *button)
     if (iter == NULL)
 	return;
 
+    icon_name = get_device_icon_name (button->priv->upower, device);
     details = get_device_description(button->priv->upower, device);
 
+    pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+				    icon_name,
+				    48,
+				    GTK_ICON_LOOKUP_USE_BUILTIN,
+				    NULL);
+
     gtk_list_store_set (GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(button->priv->treeview))), iter,
 			COL_NAME, details,
+			COL_ICON, pix,
 			-1);
 
     gtk_tree_iter_free (iter);
+
+    g_object_get (device,
+		  "kind", &type,
+		   NULL);
+
+    /* update the panel icon */
+    if (type == UP_DEVICE_KIND_LINE_POWER)
+    {
+	if ( g_strcmp0(icon_name, XFPM_AC_ADAPTER_ICON) == 0 )
+	{
+	    gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), pix);
+	}
+	else
+	{
+	    GdkPixbuf *tmp = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						       XFPM_BATTERY_ICON,
+						       48,
+						       GTK_ICON_LOOKUP_USE_BUILTIN,
+						       NULL);
+
+	    gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), tmp);
+	    g_object_unref (tmp);
+	}
+    }
 }
 
 static void
@@ -517,7 +550,7 @@ battery_button_add_device (UpDevice *device, BatteryButton *button)
     GtkTreeIter iter, *device_iter;
     GdkPixbuf *pix;
     guint type = 0;
-    gchar *details;
+    gchar *details, *icon_name;
     const gchar *object_path = up_device_get_object_path(device);
     gulong signal_id;
 
@@ -534,18 +567,17 @@ battery_button_add_device (UpDevice *device, BatteryButton *button)
 		  "kind", &type,
 		   NULL);
 
+    icon_name = get_device_icon_name (button->priv->upower, device);
+    details = get_device_description(button->priv->upower, device);
+
     list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (button->priv->treeview)));
 
     pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-				    xfpm_power_get_icon_name (type),
+				    icon_name,
 				    48,
 				    GTK_ICON_LOOKUP_USE_BUILTIN,
 				    NULL);
 
-    details = get_device_description(button->priv->upower, device);
-
-    DBG("device %s : %s", xfpm_power_get_icon_name (type), details);
-
 #if UP_CHECK_VERSION(0, 99, 0)
     signal_id = g_signal_connect (device, "notify", G_CALLBACK (device_changed_cb), button);
 #else
@@ -556,6 +588,23 @@ battery_button_add_device (UpDevice *device, BatteryButton *button)
     {
 	/* The PC's plugged in status shows up first */
 	gtk_list_store_prepend (list_store, &iter);
+
+	/* update the panel icon */
+	if ( g_strcmp0(icon_name, XFPM_AC_ADAPTER_ICON) == 0 )
+	{
+	    gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), pix);
+	}
+	else
+	{
+	    GdkPixbuf *tmp = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						       XFPM_BATTERY_ICON,
+						       48,
+						       GTK_ICON_LOOKUP_USE_BUILTIN,
+						       NULL);
+
+	    gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), tmp);
+	    g_object_unref (tmp);
+	}
     }
     else
     {
@@ -777,36 +826,10 @@ destroy_popup (BatteryButton *button)
 }
 
 static gboolean
-battery_button_set_icon (BatteryButton *button, gint width)
-{
-    GdkPixbuf *pixbuf;
-    const gchar *icon_name;
-
-    icon_name = XFPM_AC_ADAPTER_ICON;
-
-    pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-                                       icon_name,
-                                       width,
-                                       GTK_ICON_LOOKUP_FORCE_SIZE,
-                                       NULL);
-
-    if ( pixbuf )
-    {
-        gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), pixbuf);
-        g_object_unref (pixbuf);
-        return TRUE;
-    }
-    return FALSE;
-}
-
-static gboolean
 battery_button_size_changed_cb (XfcePanelPlugin *plugin, gint size, BatteryButton *button)
 {
-    gint width = size -2 - 2* MAX(gtk_widget_get_style(GTK_WIDGET(button))->xthickness,
-				  gtk_widget_get_style(GTK_WIDGET(button))->xthickness);
-
     gtk_widget_set_size_request (GTK_WIDGET(plugin), size, size);
-    return battery_button_set_icon (button, width);
+    return TRUE;
 }
 
 static void
@@ -826,6 +849,9 @@ help_cb (GtkMenuItem *menuitem, gpointer user_data)
 void battery_button_show (BatteryButton *button)
 {
     GtkWidget *mi;
+    GdkPixbuf *pix;
+    gboolean on_battery;
+    const gchar *icon_name;
 
     g_return_if_fail (BATTERY_IS_BUTTON (button));
 
@@ -834,6 +860,28 @@ void battery_button_show (BatteryButton *button)
     button->priv->image = gtk_image_new ();
     gtk_container_add (GTK_CONTAINER (button), button->priv->image);
 
+    g_object_get (button->priv->upower,
+                  "on-battery", &on_battery,
+		  NULL);
+
+    if ( on_battery )
+    {
+	icon_name = XFPM_BATTERY_ICON;
+    }
+    else
+    {
+	icon_name = XFPM_AC_ADAPTER_ICON;
+    }
+
+    pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+				    icon_name,
+				    48,
+				    GTK_ICON_LOOKUP_USE_BUILTIN,
+				    NULL);
+
+    gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), pix);
+    g_object_unref (pix);
+
     /* help dialog */
     mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_HELP, NULL);
     gtk_widget_set_sensitive (mi, TRUE);
diff --git a/src/xfpm-battery.c b/src/xfpm-battery.c
index 1a3c322..0f7d5c2 100644
--- a/src/xfpm-battery.c
+++ b/src/xfpm-battery.c
@@ -700,33 +700,6 @@ xfpm_battery_finalize (GObject *object)
     G_OBJECT_CLASS (xfpm_battery_parent_class)->finalize (object);
 }
 
-static gchar *
-xfpm_battery_get_icon_prefix_device_enum_type (UpDeviceKind type)
-{
-    if ( type == UP_DEVICE_KIND_BATTERY )
-    {
-	return g_strdup (XFPM_PRIMARY_ICON_PREFIX);
-    }
-    else if ( type == UP_DEVICE_KIND_UPS )
-    {
-	return g_strdup (XFPM_UPS_ICON_PREFIX);
-    }
-    else if ( type == UP_DEVICE_KIND_MOUSE )
-    {
-	return g_strdup (XFPM_MOUSE_ICON_PREFIX);
-    }
-    else if ( type == UP_DEVICE_KIND_KEYBOARD )
-    {
-	return g_strdup (XFPM_KBD_ICON_PREFIX);
-    }
-    else if ( type == UP_DEVICE_KIND_PHONE )
-    {
-	return g_strdup (XFPM_PHONE_ICON_PREFIX);
-    }
-
-    return g_strdup (XFPM_PRIMARY_ICON_PREFIX);
-}
-
 static const gchar *
 xfpm_battery_get_name (UpDeviceKind type)
 {

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list