[brasero] Attempt at fixing #602947 - brasero_drive_get_medium() isn't sync



commit 58efc9386bf594a78a0e83caf38d0b055d4ab154
Author: Philippe Rouquier <bonfire-app wanadoo fr>
Date:   Sun Nov 29 21:18:41 2009 +0100

    Attempt at fixing #602947 -  brasero_drive_get_medium() isn't sync
    A signal is still sent (in the main thread) but the medium becomes available as soon as it's probed
    Also make sure that the first thread started for the BraseroDrive object also checks for the medium inside to do all initialization at once

 libbrasero-media/brasero-drive-priv.h       |    3 +
 libbrasero-media/brasero-drive.c            |   95 ++++++++++++---------------
 libbrasero-media/brasero-medium-selection.c |   21 +++++-
 libbrasero-media/brasero-medium.c           |   15 ++++-
 4 files changed, 76 insertions(+), 58 deletions(-)
---
diff --git a/libbrasero-media/brasero-drive-priv.h b/libbrasero-media/brasero-drive-priv.h
index c6dc1a0..d2a3c52 100644
--- a/libbrasero-media/brasero-drive-priv.h
+++ b/libbrasero-media/brasero-drive-priv.h
@@ -38,6 +38,9 @@ G_BEGIN_DECLS
 gboolean
 brasero_drive_probing (BraseroDrive *drive);
 
+gboolean
+brasero_medium_probing (BraseroMedium *medium);
+
 G_END_DECLS
 
 #endif
diff --git a/libbrasero-media/brasero-drive.c b/libbrasero-media/brasero-drive.c
index 2a02042..61de44a 100644
--- a/libbrasero-media/brasero-drive.c
+++ b/libbrasero-media/brasero-drive.c
@@ -91,7 +91,6 @@ struct _BraseroDrivePrivate
 	guint initial_probe:1;
 	guint initial_probe_cancelled:1;
 
-	guint probed:1;
 	guint has_medium:1;
 	guint probe_cancelled:1;
 
@@ -223,8 +222,6 @@ brasero_drive_cancel_probing (BraseroDrive *drive)
 	}
 	g_mutex_unlock (priv->mutex);
 
-	priv->probed = TRUE;
-
 	if (priv->probe_id) {
 		g_source_remove (priv->probe_id);
 		priv->probe_id = 0;
@@ -712,7 +709,7 @@ brasero_drive_get_medium (BraseroDrive *drive)
 	g_return_val_if_fail (BRASERO_IS_DRIVE (drive), NULL);
 
 	priv = BRASERO_DRIVE_PRIVATE (drive);
-	if (!priv->probed)
+	if (brasero_drive_probing (drive))
 		return NULL;
 
 	return priv->medium;
@@ -844,8 +841,6 @@ brasero_drive_medium_probed (BraseroMedium *medium,
 
 	/* only when it is probed */
 	/* NOTE: BraseroMedium calls GDK_THREADS_ENTER/LEAVE() around g_signal_emit () */
-	priv->probed = TRUE;
-
 	if (brasero_medium_get_status (priv->medium) == BRASERO_MEDIUM_NONE) {
 		g_object_unref (priv->medium);
 		priv->medium = NULL;
@@ -870,43 +865,37 @@ brasero_drive_probing (BraseroDrive *drive)
 	g_return_val_if_fail (BRASERO_IS_DRIVE (drive), FALSE);
 
 	priv = BRASERO_DRIVE_PRIVATE (drive);
-	return priv->probed != TRUE;
+	if (priv->probe != NULL)
+		return TRUE;
+
+	if (priv->medium)
+		return brasero_medium_probing (priv->medium);
+
+	return FALSE;
 }
 
-static gboolean
-brasero_drive_probed_inside (gpointer data)
+static void
+brasero_drive_update_medium (BraseroDrive *drive)
 {
-	BraseroDrive *self;
 	BraseroDrivePrivate *priv;
 
-	self = BRASERO_DRIVE (data);
-	priv = BRASERO_DRIVE_PRIVATE (self);
-
-	if (!g_mutex_trylock (priv->mutex))
-		return TRUE;
-
-	priv->probe_id = 0;
-	g_mutex_unlock (priv->mutex);
-
-	priv->probed = TRUE;
+	priv = BRASERO_DRIVE_PRIVATE (drive);
 
 	if (priv->has_medium) {
 		if (priv->medium) {
 			BRASERO_MEDIA_LOG ("Already a medium. Skipping");
-			return FALSE;
+			return;
 		}
 
 		BRASERO_MEDIA_LOG ("Probing new medium");
-
-		priv->probed = FALSE;
 		priv->medium = g_object_new (BRASERO_TYPE_VOLUME,
-					     "drive", self,
+					     "drive", drive,
 					     NULL);
 
 		g_signal_connect (priv->medium,
 				  "probed",
 				  G_CALLBACK (brasero_drive_medium_probed),
-				  self);
+				  drive);
 	}
 	else if (priv->medium) {
 		BraseroMedium *medium;
@@ -916,14 +905,31 @@ brasero_drive_probed_inside (gpointer data)
 		medium = priv->medium;
 		priv->medium = NULL;
 
-		g_signal_emit (self,
+		g_signal_emit (drive,
 			       drive_signals [MEDIUM_REMOVED],
 			       0,
 			       medium);
 
 		g_object_unref (medium);
 	}
+}
+
+static gboolean
+brasero_drive_probed_inside (gpointer data)
+{
+	BraseroDrive *self;
+	BraseroDrivePrivate *priv;
 
+	self = BRASERO_DRIVE (data);
+	priv = BRASERO_DRIVE_PRIVATE (self);
+
+	if (!g_mutex_trylock (priv->mutex))
+		return TRUE;
+
+	priv->probe_id = 0;
+	g_mutex_unlock (priv->mutex);
+
+	brasero_drive_update_medium (self);
 	return FALSE;
 }
 
@@ -1047,7 +1053,6 @@ brasero_drive_probe_inside (BraseroDrive *drive)
 
 	g_mutex_lock (priv->mutex);
 
-	priv->probed = FALSE;
 	priv->probe_waiting = FALSE;
 	priv->probe_cancelled = FALSE;
 
@@ -1154,7 +1159,7 @@ brasero_drive_reprobe (BraseroDrive *drive)
 	g_return_if_fail (BRASERO_IS_DRIVE (drive));
 
 	priv = BRASERO_DRIVE_PRIVATE (drive);
-
+	
 	if (priv->gdrive) {
 		/* reprobe the contents of the drive system wide */
 		g_drive_poll_for_media (priv->gdrive, NULL, NULL, NULL);
@@ -1179,26 +1184,6 @@ brasero_drive_reprobe (BraseroDrive *drive)
 }
 
 static gboolean
-brasero_drive_probed (gpointer data)
-{
-	BraseroDrivePrivate *priv;
-
-	priv = BRASERO_DRIVE_PRIVATE (data);
-
-	if (!g_mutex_trylock (priv->mutex))
-		return TRUE;
-
-	priv->initial_probe = FALSE;
-	priv->probe_id = 0;
-
-	g_mutex_unlock (priv->mutex);
-
-	brasero_drive_probe_inside (BRASERO_DRIVE (data));
-
-	return FALSE;
-}
-
-static gboolean
 brasero_drive_get_caps_profiles (BraseroDrive *self,
                                  BraseroDeviceHandle *handle,
                                  BraseroScsiErrCode *code)
@@ -1367,7 +1352,7 @@ brasero_drive_probe_thread (gpointer data)
 	while (brasero_spc1_test_unit_ready (handle, &code) != BRASERO_SCSI_OK) {
 		if (code == BRASERO_SCSI_NO_MEDIUM) {
 			BRASERO_MEDIA_LOG ("No medium inserted");
-			break;
+			goto capabilities;
 		}
 
 		if (code != BRASERO_SCSI_NOT_READY) {
@@ -1393,6 +1378,9 @@ brasero_drive_probe_thread (gpointer data)
 	}
 
 	BRASERO_MEDIA_LOG ("Device ready");
+	priv->has_medium = TRUE;
+
+capabilities:
 
 	/* get additional information like the name */
 	res = brasero_spc1_inquiry (handle, &hdr, NULL);
@@ -1434,10 +1422,10 @@ end:
 
 	g_mutex_lock (priv->mutex);
 
-	if (!priv->initial_probe_cancelled)
-		priv->probe_id = g_idle_add (brasero_drive_probed, drive);
+	brasero_drive_update_medium (drive);
 
 	priv->probe = NULL;
+
 	g_cond_broadcast (priv->cond);
 	g_mutex_unlock (priv->mutex);
 
@@ -1507,12 +1495,11 @@ brasero_drive_set_property (GObject *object,
 		brasero_drive_update_gdrive (BRASERO_DRIVE (object), gdrive);
 		break;
 	case PROP_DEVICE:
-		if (!g_value_get_string (value)) {
+		/* The first case is only a fake drive/medium */
+		if (!g_value_get_string (value))
 			priv->medium = g_object_new (BRASERO_TYPE_VOLUME,
 						     "drive", object,
 						     NULL);
-			priv->probed = TRUE;
-		}
 		else
 			brasero_drive_init_real_device (BRASERO_DRIVE (object), g_value_get_string (value));
 		break;
diff --git a/libbrasero-media/brasero-medium-selection.c b/libbrasero-media/brasero-medium-selection.c
index c8a3da1..88db9df 100644
--- a/libbrasero-media/brasero-medium-selection.c
+++ b/libbrasero-media/brasero-medium-selection.c
@@ -647,6 +647,24 @@ brasero_medium_selection_medium_added_cb (BraseroMediumMonitor *monitor,
 
 	priv = BRASERO_MEDIUM_SELECTION_PRIVATE (self);
 
+	model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
+
+	/* Make sure it's not already in our list */
+	if (gtk_tree_model_get_iter_first (model, &iter)) {
+		do {
+			BraseroMedium *tmp;
+
+			tmp = NULL;
+			gtk_tree_model_get (model, &iter,
+					    MEDIUM_COL, &tmp,
+					    -1);
+			if (tmp == medium)
+				return;
+
+		} while (gtk_tree_model_iter_next (model, &iter));
+	}
+
+	/* Make sure it does fit the types of media to display */
 	drive = brasero_medium_get_drive (medium);
 	if ((priv->type & BRASERO_MEDIA_TYPE_CD) == priv->type
 	&& (brasero_medium_get_status (medium) & BRASERO_MEDIUM_CD))
@@ -704,8 +722,6 @@ brasero_medium_selection_medium_added_cb (BraseroMediumMonitor *monitor,
 		}
 	}
 
-	model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
-
 	if (!add) {
 		BraseroMedium *tmp;
 
@@ -825,7 +841,6 @@ brasero_medium_selection_init (BraseroMediumSelection *object)
 					      "medium-removed",
 					      G_CALLBACK (brasero_medium_selection_medium_removed_cb),
 					      object);
-
 	g_object_unref (monitor);
 
 	/* get the list and fill the model */
diff --git a/libbrasero-media/brasero-medium.c b/libbrasero-media/brasero-medium.c
index 4eb8da8..783be95 100644
--- a/libbrasero-media/brasero-medium.c
+++ b/libbrasero-media/brasero-medium.c
@@ -43,6 +43,7 @@
 #include <gdk/gdk.h>
 
 #include "brasero-media-private.h"
+#include "brasero-drive-priv.h"
 
 #include "brasero-medium.h"
 #include "brasero-drive.h"
@@ -2930,6 +2931,17 @@ brasero_medium_init_real (BraseroMedium *object,
 	}
 }
 
+gboolean
+brasero_medium_probing (BraseroMedium *medium)
+{
+	BraseroMediumPrivate *priv;
+
+	g_return_val_if_fail (BRASERO_IS_MEDIUM (medium), FALSE);
+
+	priv = BRASERO_MEDIUM_PRIVATE (medium);
+	return priv->probe != NULL;
+}
+
 static gboolean
 brasero_medium_probed (gpointer data)
 {
@@ -3034,10 +3046,11 @@ brasero_medium_probe_thread (gpointer self)
 
 end:
 
+	g_mutex_lock (priv->mutex);
+
 	if (!priv->probe_cancelled)
 		priv->probe_id = g_idle_add (brasero_medium_probed, self);
 
-	g_mutex_lock (priv->mutex);
 	priv->probe = NULL;
 	g_cond_broadcast (priv->cond);
 	g_mutex_unlock (priv->mutex);



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