[gnome-power-manager] Switch to using GIcons internally so we can support icon themes in the future



commit 055383dfa52056e2eba4ded75e5a7e15aad0f8f5
Author: Richard Hughes <richard hughsie com>
Date:   Wed Mar 31 17:06:33 2010 +0100

    Switch to using GIcons internally so we can support icon themes in the future

 src/gpm-engine.c     |   31 ++++++++++++-------------
 src/gpm-engine.h     |    2 +-
 src/gpm-manager.c    |   59 ++++++++++++++++++++++++++++++++++++-------------
 src/gpm-statistics.c |   12 ++++++---
 src/gpm-tray-icon.c  |   15 ++++++++----
 src/gpm-tray-icon.h  |    2 +-
 src/gpm-upower.c     |    7 ++++-
 src/gpm-upower.h     |    2 +-
 8 files changed, 84 insertions(+), 46 deletions(-)
---
diff --git a/src/gpm-engine.c b/src/gpm-engine.c
index f71dc6e..d221ed7 100644
--- a/src/gpm-engine.c
+++ b/src/gpm-engine.c
@@ -51,7 +51,7 @@ struct GpmEnginePrivate
 	GPtrArray		*array;
 	GpmPhone		*phone;
 	GpmIconPolicy		 icon_policy;
-	gchar			*previous_icon;
+	GIcon			*previous_icon;
 	gchar			*previous_summary;
 
 	gboolean		 use_time_primary;
@@ -271,7 +271,7 @@ gpm_engine_get_summary (GpmEngine *engine)
  *
  * Returns the icon
  **/
-static gchar *
+static GIcon *
 gpm_engine_get_icon_priv (GpmEngine *engine, UpDeviceKind device_kind, GpmEngineWarning warning, gboolean use_state)
 {
 	guint i;
@@ -321,10 +321,10 @@ gpm_engine_get_icon_priv (GpmEngine *engine, UpDeviceKind device_kind, GpmEngine
  *
  * Returns the icon
  **/
-gchar *
+GIcon *
 gpm_engine_get_icon (GpmEngine *engine)
 {
-	gchar *icon = NULL;
+	GIcon *icon = NULL;
 
 	g_return_val_if_fail (GPM_IS_ENGINE (engine), NULL);
 
@@ -412,7 +412,7 @@ gpm_engine_get_icon (GpmEngine *engine)
 static gboolean
 gpm_engine_recalculate_state_icon (GpmEngine *engine)
 {
-	gchar *icon;
+	GIcon *icon;
 
 	g_return_val_if_fail (engine != NULL, FALSE);
 	g_return_val_if_fail (GPM_IS_ENGINE (engine), FALSE);
@@ -427,31 +427,29 @@ gpm_engine_recalculate_state_icon (GpmEngine *engine)
 		egg_debug ("** EMIT: icon-changed: none");
 		g_signal_emit (engine, signals [ICON_CHANGED], 0, NULL);
 
-		g_free (engine->priv->previous_icon);
+		g_object_unref (engine->priv->previous_icon);
 		engine->priv->previous_icon = NULL;
 		return TRUE;
 	}
 
 	/* no icon before, now icon */
 	if (engine->priv->previous_icon == NULL) {
-		egg_debug ("** EMIT: icon-changed: %s", icon);
 		g_signal_emit (engine, signals [ICON_CHANGED], 0, icon);
-		engine->priv->previous_icon = icon;
+		engine->priv->previous_icon = g_object_ref (icon);
 		return TRUE;
 	}
 
 	/* icon before, now different */
-	if (strcmp (engine->priv->previous_icon, icon) != 0) {
-		g_free (engine->priv->previous_icon);
-		engine->priv->previous_icon = icon;
-		egg_debug ("** EMIT: icon-changed: %s", icon);
+	if (!g_icon_equal (engine->priv->previous_icon, icon)) {
+		g_object_unref (engine->priv->previous_icon);
+		engine->priv->previous_icon = g_object_ref (icon);
 		g_signal_emit (engine, signals [ICON_CHANGED], 0, icon);
 		return TRUE;
 	}
 
 	egg_debug ("no change");
 	/* nothing to do */
-	g_free (icon);
+	g_object_unref (icon);
 	return FALSE;
 }
 
@@ -1152,8 +1150,8 @@ gpm_engine_class_init (GpmEngineClass *klass)
 			      G_TYPE_FROM_CLASS (object_class),
 			      G_SIGNAL_RUN_LAST,
 			      G_STRUCT_OFFSET (GpmEngineClass, icon_changed),
-			      NULL, NULL, g_cclosure_marshal_VOID__STRING,
-			      G_TYPE_NONE, 1, G_TYPE_STRING);
+			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
+			      G_TYPE_NONE, 1, G_TYPE_ICON);
 	signals [SUMMARY_CHANGED] =
 		g_signal_new ("summary-changed",
 			      G_TYPE_FROM_CLASS (object_class),
@@ -1240,7 +1238,8 @@ gpm_engine_finalize (GObject *object)
 	g_object_unref (engine->priv->phone);
 	g_object_unref (engine->priv->battery_composite);
 
-	g_free (engine->priv->previous_icon);
+	if (engine->priv->previous_icon != NULL)
+		g_object_unref (engine->priv->previous_icon);
 	g_free (engine->priv->previous_summary);
 
 	G_OBJECT_CLASS (gpm_engine_parent_class)->finalize (object);
diff --git a/src/gpm-engine.h b/src/gpm-engine.h
index e158283..e420866 100644
--- a/src/gpm-engine.h
+++ b/src/gpm-engine.h
@@ -70,7 +70,7 @@ typedef struct
 
 GType		 gpm_engine_get_type		(void);
 GpmEngine	*gpm_engine_new			(void);
-gchar		*gpm_engine_get_icon		(GpmEngine	*engine);
+GIcon		*gpm_engine_get_icon		(GpmEngine	*engine);
 gchar		*gpm_engine_get_summary		(GpmEngine	*engine);
 GPtrArray	*gpm_engine_get_devices		(GpmEngine	*engine);
 
diff --git a/src/gpm-manager.c b/src/gpm-manager.c
index d12bd80..667c9ae 100644
--- a/src/gpm-manager.c
+++ b/src/gpm-manager.c
@@ -460,12 +460,33 @@ gpm_manager_notification_closed_cb (NotifyNotification *notification, NotifyNoti
 }
 
 /**
+ * gpm_manager_get_icon_name:
+ **/
+static const gchar *
+gpm_manager_get_icon_name (GIcon *icon)
+{
+	const gchar* const *icon_names;
+	const gchar *icon_name = NULL;
+
+	/* no icon */
+	if (icon == NULL)
+		goto out;
+
+	/* just use the first icon */
+	icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+	if (icon_names != NULL)
+		icon_name = icon_names[0];
+out:
+	return icon_name;
+}
+
+/**
  * gpm_manager_notify:
  **/
 static gboolean
 gpm_manager_notify (GpmManager *manager, NotifyNotification **notification_class,
 		    const gchar *title, const gchar *message,
-		    guint timeout, const gchar *icon, NotifyUrgency urgency)
+		    guint timeout, const gchar *icon_name, NotifyUrgency urgency)
 {
 	gboolean ret;
 	GError *error = NULL;
@@ -477,9 +498,9 @@ gpm_manager_notify (GpmManager *manager, NotifyNotification **notification_class
 
 	/* if the status icon is hidden, don't point at it */
 	if (gtk_status_icon_is_embedded (manager->priv->status_icon))
-		notification = notify_notification_new_with_status_icon (title, message, icon, manager->priv->status_icon);
+		notification = notify_notification_new_with_status_icon (title, message, icon_name, manager->priv->status_icon);
 	else
-		notification = notify_notification_new (title, message, icon, NULL);
+		notification = notify_notification_new (title, message, icon_name, NULL);
 	notify_notification_set_timeout (notification, timeout);
 	notify_notification_set_urgency (notification, urgency);
 	g_signal_connect (notification, "closed", G_CALLBACK (gpm_manager_notification_closed_cb), notification_class);
@@ -1241,7 +1262,7 @@ gpm_manager_engine_perhaps_recall_cb (GpmEngine *engine, UpDevice *device, gchar
  * gpm_manager_engine_icon_changed_cb:
  */
 static void
-gpm_manager_engine_icon_changed_cb (GpmEngine  *engine, gchar *icon, GpmManager *manager)
+gpm_manager_engine_icon_changed_cb (GpmEngine  *engine, GIcon *icon, GpmManager *manager)
 {
 	gpm_tray_icon_set_icon (manager->priv->tray_icon, icon);
 }
@@ -1355,7 +1376,7 @@ gpm_manager_engine_discharging_cb (GpmEngine *engine, UpDevice *device, GpmManag
 	gdouble percentage;
 	gint64 time_to_empty;
 	gchar *remaining_text = NULL;
-	gchar *icon = NULL;
+	GIcon *icon = NULL;
 	const gchar *kind_desc;
 
 	/* only action this if specified in gconf */
@@ -1409,9 +1430,10 @@ gpm_manager_engine_discharging_cb (GpmEngine *engine, UpDevice *device, GpmManag
 	icon = gpm_upower_get_device_icon (device);
 	/* show the notification */
 	gpm_manager_notify (manager, &manager->priv->notification_discharging, title, message, GPM_MANAGER_NOTIFY_TIMEOUT_LONG,
-			    icon, NOTIFY_URGENCY_NORMAL);
+			    gpm_manager_get_icon_name (icon), NOTIFY_URGENCY_NORMAL);
 out:
-	g_free (icon);
+	if (icon != NULL)
+		g_object_unref (icon);
 	g_free (remaining_text);
 	return;
 }
@@ -1452,7 +1474,7 @@ gpm_manager_engine_charge_low_cb (GpmEngine *engine, UpDevice *device, GpmManage
 	const gchar *title = NULL;
 	gchar *message = NULL;
 	gchar *remaining_text;
-	gchar *icon = NULL;
+	GIcon *icon = NULL;
 	UpDeviceKind kind;
 	gdouble percentage;
 	gint64 time_to_empty;
@@ -1529,10 +1551,12 @@ gpm_manager_engine_charge_low_cb (GpmEngine *engine, UpDevice *device, GpmManage
 
 	/* get correct icon */
 	icon = gpm_upower_get_device_icon (device);
-	gpm_manager_notify (manager, &manager->priv->notification_warning_low, title, message, GPM_MANAGER_NOTIFY_TIMEOUT_LONG, icon, NOTIFY_URGENCY_NORMAL);
+	gpm_manager_notify (manager, &manager->priv->notification_warning_low, title, message,
+			    GPM_MANAGER_NOTIFY_TIMEOUT_LONG, gpm_manager_get_icon_name (icon), NOTIFY_URGENCY_NORMAL);
 	gpm_manager_play (manager, GPM_MANAGER_SOUND_BATTERY_CAUTION, TRUE);
 out:
-	g_free (icon);
+	if (icon != NULL)
+		g_object_unref (icon);
 	g_free (message);
 }
 
@@ -1545,7 +1569,7 @@ gpm_manager_engine_charge_critical_cb (GpmEngine *engine, UpDevice *device, GpmM
 	const gchar *title = NULL;
 	gchar *message = NULL;
 	gchar *action;
-	gchar *icon = NULL;
+	GIcon *icon = NULL;
 	UpDeviceKind kind;
 	gdouble percentage;
 	gint64 time_to_empty;
@@ -1652,7 +1676,8 @@ gpm_manager_engine_charge_critical_cb (GpmEngine *engine, UpDevice *device, GpmM
 
 	/* get correct icon */
 	icon = gpm_upower_get_device_icon (device);
-	gpm_manager_notify (manager, &manager->priv->notification_warning_low, title, message, GPM_MANAGER_NOTIFY_TIMEOUT_NEVER, icon, NOTIFY_URGENCY_CRITICAL);
+	gpm_manager_notify (manager, &manager->priv->notification_warning_low, title, message,
+			    GPM_MANAGER_NOTIFY_TIMEOUT_NEVER, gpm_manager_get_icon_name (icon), NOTIFY_URGENCY_CRITICAL);
 
 	switch (kind) {
 
@@ -1669,7 +1694,8 @@ gpm_manager_engine_charge_critical_cb (GpmEngine *engine, UpDevice *device, GpmM
 		gpm_manager_play (manager, GPM_MANAGER_SOUND_BATTERY_LOW, TRUE);
 	}
 out:
-	g_free (icon);
+	if (icon != NULL)
+		g_object_unref (icon);
 	g_free (message);
 }
 
@@ -1682,7 +1708,7 @@ gpm_manager_engine_charge_action_cb (GpmEngine *engine, UpDevice *device, GpmMan
 	const gchar *title = NULL;
 	gchar *action;
 	gchar *message = NULL;
-	gchar *icon = NULL;
+	GIcon *icon = NULL;
 	UpDeviceKind kind;
 	GpmActionPolicy policy;
 
@@ -1778,10 +1804,11 @@ gpm_manager_engine_charge_action_cb (GpmEngine *engine, UpDevice *device, GpmMan
 	icon = gpm_upower_get_device_icon (device);
 	gpm_manager_notify (manager, &manager->priv->notification_warning_low,
 			    title, message, GPM_MANAGER_NOTIFY_TIMEOUT_NEVER,
-			    icon, NOTIFY_URGENCY_CRITICAL);
+			    gpm_manager_get_icon_name (icon), NOTIFY_URGENCY_CRITICAL);
 	gpm_manager_play (manager, GPM_MANAGER_SOUND_BATTERY_LOW, TRUE);
 out:
-	g_free (icon);
+	if (icon != NULL)
+		g_object_unref (icon);
 	g_free (message);
 }
 
diff --git a/src/gpm-statistics.c b/src/gpm-statistics.c
index b655049..2023b5e 100644
--- a/src/gpm-statistics.c
+++ b/src/gpm-statistics.c
@@ -152,7 +152,7 @@ gpm_stats_add_devices_columns (GtkTreeView *treeview)
 	renderer = gtk_cell_renderer_pixbuf_new ();
 	g_object_set (renderer, "stock-size", GTK_ICON_SIZE_DIALOG, NULL);
 	column = gtk_tree_view_column_new_with_attributes (_("Image"), renderer,
-							   "icon-name", GPM_DEVICES_COLUMN_ICON, NULL);
+							   "gicon", GPM_DEVICES_COLUMN_ICON, NULL);
 	gtk_tree_view_append_column (treeview, column);
 
 	/* column for text */
@@ -1146,7 +1146,7 @@ gpm_stats_add_device (UpDevice *device)
 	const gchar *id;
 	GtkTreeIter iter;
 	const gchar *text;
-	const gchar *icon;
+	GIcon *icon;
 	UpDeviceKind kind;
 
 	/* get device properties */
@@ -1163,6 +1163,7 @@ gpm_stats_add_device (UpDevice *device)
 			    GPM_DEVICES_COLUMN_ID, id,
 			    GPM_DEVICES_COLUMN_TEXT, text,
 			    GPM_DEVICES_COLUMN_ICON, icon, -1);
+	g_object_unref (icon);
 }
 
 /**
@@ -1612,7 +1613,7 @@ main (int argc, char *argv[])
 
 	/* create list stores */
 	list_store_info = gtk_list_store_new (GPM_INFO_COLUMN_LAST, G_TYPE_STRING, G_TYPE_STRING);
-	list_store_devices = gtk_list_store_new (GPM_DEVICES_COLUMN_LAST, G_TYPE_STRING,
+	list_store_devices = gtk_list_store_new (GPM_DEVICES_COLUMN_LAST, G_TYPE_ICON,
 						 G_TYPE_STRING, G_TYPE_STRING);
 	list_store_wakeups = gtk_list_store_new (GPM_WAKEUPS_COLUMN_LAST, G_TYPE_STRING,
 						 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
@@ -1772,12 +1773,15 @@ main (int argc, char *argv[])
 	ret = up_wakeups_get_has_capability (wakeups);
 	if (ret) {
 		GtkTreeIter iter;
+		GIcon *icon;
+		icon = g_themed_icon_new ("computer");
 		gtk_list_store_append (list_store_devices, &iter);
 		gtk_list_store_set (list_store_devices, &iter,
 				    GPM_DEVICES_COLUMN_ID, "wakeups",
 				    /* TRANSLATORS: the icon for the CPU */
 				    GPM_DEVICES_COLUMN_TEXT, _("Processor"),
-				    GPM_DEVICES_COLUMN_ICON, "computer", -1);
+				    GPM_DEVICES_COLUMN_ICON, icon, -1);
+		g_object_unref (icon);
 	}
 
 	/* set the correct focus on the last device */
diff --git a/src/gpm-tray-icon.c b/src/gpm-tray-icon.c
index 64fc257..6e783ac 100644
--- a/src/gpm-tray-icon.c
+++ b/src/gpm-tray-icon.c
@@ -116,14 +116,16 @@ gpm_tray_icon_get_status_icon (GpmTrayIcon *icon)
  * Loads a pixmap from disk, and sets as the tooltip icon
  **/
 gboolean
-gpm_tray_icon_set_icon (GpmTrayIcon *icon, const gchar *filename)
+gpm_tray_icon_set_icon (GpmTrayIcon *icon, GIcon *gicon)
 {
+	gchar *filename = NULL;
 	g_return_val_if_fail (icon != NULL, FALSE);
 	g_return_val_if_fail (GPM_IS_TRAY_ICON (icon), FALSE);
 
-	if (filename != NULL) {
+	if (gicon != NULL) {
+		filename = g_icon_to_string (gicon);
 		egg_debug ("Setting icon to %s", filename);
-		gtk_status_icon_set_from_icon_name (icon->priv->status_icon, filename);
+		gtk_status_icon_set_from_gicon (icon->priv->status_icon, gicon);
 
 		/* make sure that we are visible */
 		gpm_tray_icon_show (icon, TRUE);
@@ -134,6 +136,7 @@ gpm_tray_icon_set_icon (GpmTrayIcon *icon, const gchar *filename)
 		/* make sure that we are hidden */
 		gpm_tray_icon_show (icon, FALSE);
 	}
+	g_free (filename);
 	return TRUE;
 }
 
@@ -200,6 +203,7 @@ gpm_tray_icon_add_device (GpmTrayIcon *icon, GtkMenu *menu, const GPtrArray *arr
 	UpDevice *device;
 	UpDeviceKind kind_tmp;
 	gdouble percentage;
+	GIcon *gicon = NULL;
 
 	/* find type */
 	for (i=0;i<array->len;i++) {
@@ -245,8 +249,8 @@ gpm_tray_icon_add_device (GpmTrayIcon *icon, GtkMenu *menu, const GPtrArray *arr
 		gtk_box_pack_start (GTK_BOX (hbox), label_percentage, FALSE, FALSE, 3);
 
 		/* generate the image */
-		icon_name = gpm_upower_get_device_icon (device);
-		image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
+		gicon = gpm_upower_get_device_icon (device);
+		image = gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_MENU);
 		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
 		gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
 
@@ -258,6 +262,7 @@ gpm_tray_icon_add_device (GpmTrayIcon *icon, GtkMenu *menu, const GPtrArray *arr
 
 		g_free (icon_name);
 		g_free (percentage_str);
+		g_object_unref (gicon);
 	}
 	return added;
 }
diff --git a/src/gpm-tray-icon.h b/src/gpm-tray-icon.h
index 5ff828f..56f281d 100644
--- a/src/gpm-tray-icon.h
+++ b/src/gpm-tray-icon.h
@@ -55,7 +55,7 @@ GpmTrayIcon	*gpm_tray_icon_new			(void);
 gboolean	 gpm_tray_icon_set_tooltip		(GpmTrayIcon	*icon,
 							 const gchar	*tooltip);
 gboolean	 gpm_tray_icon_set_icon			(GpmTrayIcon	*icon,
-							 const gchar	*filename);
+							 GIcon		*gicon);
 GtkStatusIcon	*gpm_tray_icon_get_status_icon		(GpmTrayIcon	*icon);
 
 G_END_DECLS
diff --git a/src/gpm-upower.c b/src/gpm-upower.c
index c82f29a..8c00102 100644
--- a/src/gpm-upower.c
+++ b/src/gpm-upower.c
@@ -72,7 +72,7 @@ gpm_upower_get_device_icon_index (UpDevice *device)
  * Need to free the return value
  *
  **/
-gchar *
+GIcon *
 gpm_upower_get_device_icon (UpDevice *device)
 {
 	gchar *filename = NULL;
@@ -82,6 +82,7 @@ gpm_upower_get_device_icon (UpDevice *device)
 	UpDeviceState state;
 	gboolean is_present;
 	gdouble percentage;
+	GIcon *icon = NULL;
 
 	g_return_val_if_fail (device != NULL, NULL);
 
@@ -171,7 +172,9 @@ gpm_upower_get_device_icon (UpDevice *device)
 	}
 
 	egg_debug ("got filename: %s", filename);
-	return filename;
+	icon = g_themed_icon_new (filename);
+	g_free (filename);
+	return icon;
 }
 
 /**
diff --git a/src/gpm-upower.h b/src/gpm-upower.h
index c1a9c03..4bd70ed 100644
--- a/src/gpm-upower.h
+++ b/src/gpm-upower.h
@@ -32,7 +32,7 @@ const gchar	*gpm_device_kind_to_localised_string	(UpDeviceKind	 kind,
 const gchar	*gpm_device_kind_to_icon		(UpDeviceKind	 kind);
 const gchar	*gpm_device_technology_to_localised_string (UpDeviceTechnology technology_enum);
 const gchar	*gpm_device_state_to_localised_string	(UpDeviceState	 state);
-gchar		*gpm_upower_get_device_icon		(UpDevice	*device);
+GIcon		*gpm_upower_get_device_icon		(UpDevice	*device);
 gchar		*gpm_upower_get_device_summary		(UpDevice	*device);
 gchar		*gpm_upower_get_device_description	(UpDevice	*device);
 



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