[gnome-power-manager] Show the primary device time remaining as the first entry in the context menu



commit 93da9b4887b3e91a9ece4882261949cfa5eec37e
Author: Richard Hughes <richard hughsie com>
Date:   Wed May 19 17:12:40 2010 +0100

    Show the primary device time remaining as the first entry in the context menu

 src/gpm-engine.c    |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/gpm-engine.h    |    1 +
 src/gpm-tray-icon.c |   38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+), 0 deletions(-)
---
diff --git a/src/gpm-engine.c b/src/gpm-engine.c
index 6efe66a..e71e206 100644
--- a/src/gpm-engine.c
+++ b/src/gpm-engine.c
@@ -962,6 +962,50 @@ gpm_engine_get_devices (GpmEngine *engine)
 }
 
 /**
+ * gpm_engine_get_primary_device:
+ *
+ * Return value: the #UpDevice, free with g_object_unref()
+ **/
+UpDevice *
+gpm_engine_get_primary_device (GpmEngine *engine)
+{
+	guint i;
+	UpDevice *device = NULL;
+	UpDevice *device_tmp;
+	UpDeviceKind kind;
+	UpDeviceState state;
+	gboolean is_present;
+
+	for (i=0; i<engine->priv->array->len; i++) {
+		device_tmp = g_ptr_array_index (engine->priv->array, i);
+
+		/* get device properties */
+		g_object_get (device_tmp,
+			      "kind", &kind,
+			      "state", &state,
+			      "is-present", &is_present,
+			      NULL);
+
+		/* not present */
+		if (!is_present)
+			continue;
+
+		/* not discharging */
+		if (state != UP_DEVICE_STATE_DISCHARGING)
+			continue;
+
+		/* not battery */
+		if (kind != UP_DEVICE_KIND_BATTERY)
+			continue;
+
+		/* use composite device to cope with multiple batteries */
+		device = g_object_ref (gpm_engine_get_composite_device (engine, device_tmp));
+		break;
+	}
+	return device;
+}
+
+/**
  * phone_device_added_cb:
  **/
 static void
diff --git a/src/gpm-engine.h b/src/gpm-engine.h
index e420866..9516ece 100644
--- a/src/gpm-engine.h
+++ b/src/gpm-engine.h
@@ -73,6 +73,7 @@ GpmEngine	*gpm_engine_new			(void);
 GIcon		*gpm_engine_get_icon		(GpmEngine	*engine);
 gchar		*gpm_engine_get_summary		(GpmEngine	*engine);
 GPtrArray	*gpm_engine_get_devices		(GpmEngine	*engine);
+UpDevice	*gpm_engine_get_primary_device	(GpmEngine	*engine);
 
 G_END_DECLS
 
diff --git a/src/gpm-tray-icon.c b/src/gpm-tray-icon.c
index 0653334..fe11f06 100644
--- a/src/gpm-tray-icon.c
+++ b/src/gpm-tray-icon.c
@@ -264,6 +264,33 @@ gpm_tray_icon_add_device (GpmTrayIcon *icon, GtkMenu *menu, const GPtrArray *arr
 }
 
 /**
+ * gpm_tray_icon_add_primary_device:
+ **/
+static void
+gpm_tray_icon_add_primary_device (GpmTrayIcon *icon, GtkMenu *menu, UpDevice *device)
+{
+	GtkWidget *item;
+	gchar *time_str;
+	gchar *string;
+	gint64 time_to_empty = 0;
+
+	/* get details */
+	g_object_get (device,
+		      "time-to-empty", &time_to_empty,
+		      NULL);
+
+	/* convert time to string */
+	time_str = gpm_get_timestring (time_to_empty);
+
+	/* TRANSLATORS: % is a timestring, e.g. "6 hours 10 minutes" */
+	string = g_strdup_printf (_("%s remaining"), time_str);
+	item = gtk_image_menu_item_new_with_label (string);
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+	g_free (time_str);
+	g_free (string);
+}
+
+/**
  * gpm_tray_icon_create_menu:
  *
  * Create the popup menu.
@@ -276,6 +303,15 @@ gpm_tray_icon_create_menu (GpmTrayIcon *icon)
 	GtkWidget *image;
 	guint dev_cnt = 0;
 	GPtrArray *array;
+	UpDevice *device = NULL;
+
+	/* show the primary device time remaining */
+	device = gpm_engine_get_primary_device (icon->priv->engine);
+	if (device != NULL) {
+		gpm_tray_icon_add_primary_device (icon, menu, device);
+		item = gtk_separator_menu_item_new ();
+		gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+	}
 
 	/* add all device types to the drop down menu */
 	array = gpm_engine_get_devices (icon->priv->engine);
@@ -306,6 +342,8 @@ gpm_tray_icon_create_menu (GpmTrayIcon *icon)
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
 
 skip_prefs:
+	if (device != NULL)
+		g_object_unref (device);
 	return menu;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]