[totem] Move volume handling for the menus into its own function



commit 2984b6b1807c0072db2d7eca61be03655c21ca25
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Nov 26 16:45:20 2009 +0000

    Move volume handling for the menus into its own function

 src/totem-menu.c |  210 +++++++++++++++++++++++++++++-------------------------
 1 files changed, 112 insertions(+), 98 deletions(-)
---
diff --git a/src/totem-menu.c b/src/totem-menu.c
index 085227d..6149a10 100644
--- a/src/totem-menu.c
+++ b/src/totem-menu.c
@@ -775,119 +775,133 @@ on_play_dvb_activate (GtkAction *action, Totem *totem)
 }
 
 static void
-add_drive_to_menu (GDrive *drive, guint position, Totem *totem)
+add_volume_to_menu (GVolume *volume,
+		    GDrive *drive,
+		    GtkIconTheme *theme,
+		    guint position,
+		    Totem *totem)
 {
-	GtkIconTheme *theme;
-	GList *volumes, *i;
+	char *name, *escaped_name, *label;
+	GtkAction *action;
+	gboolean disabled;
+	GIcon *icon;
+	const char * const *icon_names;
+	const char *icon_name;
+	guint j;
+	char *device_path;
+	GtkWidget *menu_item;
+	char *menu_item_path;
 
-	theme = gtk_icon_theme_get_default ();
+	disabled = FALSE;
 
-	/* Repeat for all the drive's volumes */
-	volumes = g_drive_get_volumes (drive);
-
-	for (i = volumes; i != NULL; i = i->next) {
-		char *name, *escaped_name, *label;
-		GtkAction *action;
-		gboolean disabled;
-		GIcon *icon;
-		const char * const *icon_names;
-		const char *icon_name;
-		guint j;
-		char *device_path;
-		GtkWidget *menu_item;
-		char *menu_item_path;
-
-		disabled = FALSE;
-
-		/* Add devices with blank CDs and audio CDs in them, but disable them */
-		device_path = g_volume_get_identifier (i->data, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
+	/* Add devices with blank CDs and audio CDs in them, but disable them */
+	if (drive != NULL) {
+		device_path = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
 		if (device_path == NULL)
-			continue;
+			return;
+	}
 
-		/* Check whether we have a media... */
-		if (g_drive_has_media (drive) == FALSE) {
-			disabled = TRUE;
-		} else {
-			/* ... Or an audio CD or a blank media */
-			GMount *mount;
-			GFile *root;
-
-			mount = g_volume_get_mount (i->data);
-			if (mount != NULL) {
-				root = g_mount_get_root (mount);
-				g_object_unref (mount);
-
-				if (g_file_has_uri_scheme (root, "burn") != FALSE || g_file_has_uri_scheme (root, "cdda") != FALSE)
-					disabled = TRUE;
-				g_object_unref (root);
-			}
+	/* Check whether we have a media... */
+	if (drive != NULL &&
+	    g_drive_has_media (drive) == FALSE) {
+		disabled = TRUE;
+	} else {
+		/* ... Or an audio CD or a blank media */
+		GMount *mount;
+		GFile *root;
+
+		mount = g_volume_get_mount (volume);
+		if (mount != NULL) {
+			root = g_mount_get_root (mount);
+			g_object_unref (mount);
+
+			if (g_file_has_uri_scheme (root, "burn") != FALSE || g_file_has_uri_scheme (root, "cdda") != FALSE)
+				disabled = TRUE;
+			g_object_unref (root);
 		}
+	}
 
-		/* Work out an icon to display */
-		icon = g_volume_get_icon (i->data);
-		icon_name = NULL;
+	/* Work out an icon to display */
+	icon = g_volume_get_icon (volume);
+	icon_name = NULL;
 
-		if (G_IS_EMBLEMED_ICON (icon) != FALSE) {
-			GIcon *new_icon;
-			new_icon = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
-			g_object_unref (icon);
-			icon = g_object_ref (new_icon);
-		}
+	if (G_IS_EMBLEMED_ICON (icon) != FALSE) {
+		GIcon *new_icon;
+		new_icon = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
+		g_object_unref (icon);
+		icon = g_object_ref (new_icon);
+	}
 
-		if (G_IS_THEMED_ICON (icon)) {
-			icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+	if (G_IS_THEMED_ICON (icon)) {
+		icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
 
-			for (j = 0; icon_names[j] != NULL; j++) {
-				icon_name = icon_names[j];
-				if (gtk_icon_theme_has_icon (theme, icon_name) != FALSE)
-					break;
-			}
+		for (j = 0; icon_names[j] != NULL; j++) {
+			icon_name = icon_names[j];
+			if (gtk_icon_theme_has_icon (theme, icon_name) != FALSE)
+				break;
 		}
+	}
 
-		/* Get the volume's pretty name for the menu label */
-		name = g_volume_get_name (i->data);
-		g_strstrip (name);
-		escaped_name = escape_label_for_menu (name);
-		g_free (name);
-		label = g_strdup_printf (_("Play Disc '%s'"), escaped_name);
-		g_free (escaped_name);
-
-		name = g_strdup_printf (_("device%d"), position);
-
-		action = gtk_action_new (name, label, NULL, NULL);
-		g_object_set (G_OBJECT (action),
-			      "icon-name", icon_name,
-			      "sensitive", !disabled, NULL);
-		gtk_action_group_add_action (totem->devices_action_group, action);
-		g_object_unref (action);
-
-		gtk_ui_manager_add_ui (totem->ui_manager, totem->devices_ui_id,
-			"/tmw-menubar/movie/devices-placeholder", name, name,
-			GTK_UI_MANAGER_MENUITEM, FALSE);
-
-		/* TODO: This can be made cleaner once bug #589842 is fixed */
-		menu_item_path = g_strdup_printf ("/tmw-menubar/movie/devices-placeholder/%s", name);
-		menu_item = gtk_ui_manager_get_widget (totem->ui_manager, menu_item_path);
-		g_free (menu_item_path);
-
-		if (menu_item != NULL)
-			gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menu_item), TRUE);
-
-		g_free (name);
-		g_free (label);
-		g_object_unref (icon);
+	/* Get the volume's pretty name for the menu label */
+	name = g_volume_get_name (volume);
+	g_strstrip (name);
+	escaped_name = escape_label_for_menu (name);
+	g_free (name);
+	label = g_strdup_printf (_("Play Disc '%s'"), escaped_name);
+	g_free (escaped_name);
 
-		if (disabled != FALSE) {
-			g_free (device_path);
-			return;
-		}
+	name = g_strdup_printf (_("device%d"), position);
 
-		g_object_set_data_full (G_OBJECT (action),
-					"device_path", device_path,
-					(GDestroyNotify) g_free);
+	action = gtk_action_new (name, label, NULL, NULL);
+	g_object_set (G_OBJECT (action),
+		      "icon-name", icon_name,
+		      "sensitive", !disabled, NULL);
+	gtk_action_group_add_action (totem->devices_action_group, action);
+	g_object_unref (action);
+
+	gtk_ui_manager_add_ui (totem->ui_manager, totem->devices_ui_id,
+			       "/tmw-menubar/movie/devices-placeholder", name, name,
+			       GTK_UI_MANAGER_MENUITEM, FALSE);
+
+	/* TODO: This can be made cleaner once bug #589842 is fixed */
+	menu_item_path = g_strdup_printf ("/tmw-menubar/movie/devices-placeholder/%s", name);
+	menu_item = gtk_ui_manager_get_widget (totem->ui_manager, menu_item_path);
+	g_free (menu_item_path);
+
+	if (menu_item != NULL)
+		gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menu_item), TRUE);
+
+	g_free (name);
+	g_free (label);
+	g_object_unref (icon);
+
+	if (disabled != FALSE) {
+		g_free (device_path);
+		return;
+	}
+
+	g_object_set_data_full (G_OBJECT (action),
+				"device_path", device_path,
+				(GDestroyNotify) g_free);
+
+	g_signal_connect (G_OBJECT (action), "activate",
+			  G_CALLBACK (on_play_disc_activate), totem);
+}
 
-		g_signal_connect (G_OBJECT (action), "activate",
-				  G_CALLBACK (on_play_disc_activate), totem);
+static void
+add_drive_to_menu (GDrive *drive, guint position, Totem *totem)
+{
+	GtkIconTheme *theme;
+	GList *volumes, *i;
+
+	theme = gtk_icon_theme_get_default ();
+
+	/* Repeat for all the drive's volumes */
+	volumes = g_drive_get_volumes (drive);
+
+	for (i = volumes; i != NULL; i = i->next) {
+		GVolume *volume = i->data;
+		add_volume_to_menu (volume, drive, theme, position, totem);
 	}
 
 	g_list_free (volumes);



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