--- src/file-manager/fm-icon-view.c	2005/08/19 21:17:42	1.1
+++ src/file-manager/fm-icon-view.c	2005/08/20 02:19:13
@@ -79,8 +79,13 @@
 #include <unistd.h>
 #include <esd.h>
 
+#define USE_GST_LAUNCH_AUDIO_PREVIEW 1
+#ifdef USE_GST_LAUNCH_AUDIO_PREVIEW
+#define GST_LAUNCH_PATH "/usr/bin/gst-launch-0.8"
+#else
 #define USE_OLD_AUDIO_PREVIEW 1
 #define READ_CHUNK_SIZE 16384
+#endif
 
 #define POPUP_PATH_ICON_APPEARANCE		"/selection/Icon Appearance Items"
 
@@ -116,6 +121,9 @@ struct FMIconViewDetails
 	NautilusAudioPlayerData *audio_player_data;
 	int audio_preview_timeout;
 	NautilusFile *audio_preview_file;
+#if USE_GST_LAUNCH_AUDIO_PREVIEW
+	pid_t audio_preview_pid;
+#endif
 
 	gboolean filter_by_screen;
 	int num_screens;
@@ -1688,6 +1696,20 @@ band_select_ended_callback (NautilusIcon
 	fm_directory_view_stop_batching_selection_changes (FM_DIRECTORY_VIEW (icon_view));
 }
 
+#if USE_GST_LAUNCH_AUDIO_PREVIEW
+static void
+play_child_watch (GPid pid,
+		  gint status,
+		  gpointer data)
+{
+	FMIconView *icon_view;
+	icon_view = FM_ICON_VIEW (data);
+	if (icon_view->details->audio_preview_pid == (pid_t) pid)
+		icon_view->details->audio_preview_pid = 0;
+	g_spawn_close_pid (pid);
+}
+#endif
+
 /* handle the preview signal by inspecting the mime type.  For now, we only preview local sound files. */
 
 /* here's the timer task that actually plays the file using mpg123, ogg123 or play. */
@@ -1695,7 +1717,46 @@ band_select_ended_callback (NautilusIcon
 static gboolean
 play_file (gpointer callback_data)
 {
-#if USE_OLD_AUDIO_PREVIEW	
+#if USE_GST_LAUNCH_AUDIO_PREVIEW
+	char *file_uri;
+	FMIconView *icon_view;
+	GPtrArray *arg_array;
+	gchar **argv;
+	GPid child_pid;
+
+	icon_view = FM_ICON_VIEW (callback_data);
+	
+	file_uri = nautilus_file_get_uri (
+		icon_view->details->audio_preview_file);
+
+	arg_array = g_ptr_array_sized_new (8);
+	g_ptr_array_add (arg_array, g_strdup (GST_LAUNCH_PATH));
+	g_ptr_array_add (arg_array, g_strdup ("gnomevfssrc"));
+	g_ptr_array_add (arg_array, g_strdup_printf ("location=%s", file_uri));
+	g_ptr_array_add (arg_array, g_strdup ("!"));
+	g_ptr_array_add (arg_array, g_strdup ("spider"));
+	g_ptr_array_add (arg_array, g_strdup ("!"));
+	g_ptr_array_add (arg_array, g_strdup ("audioconvert"));
+	g_ptr_array_add (arg_array, g_strdup ("!"));
+	g_ptr_array_add (arg_array, g_strdup ("audioscale"));
+	g_ptr_array_add (arg_array, g_strdup ("!"));
+	g_ptr_array_add (arg_array, g_strdup ("gconfaudiosink"));
+	g_ptr_array_add (arg_array, NULL);
+	argv = (gchar **) g_ptr_array_free (arg_array, FALSE);
+
+	if (g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, 
+		    NULL, &child_pid, NULL) == TRUE) {
+		g_child_watch_add (child_pid, play_child_watch, icon_view);
+		if (icon_view->details->audio_preview_pid > 0) {
+			kill (icon_view->details->audio_preview_pid, SIGTERM);
+		}
+		icon_view->details->audio_preview_pid = (pid_t) child_pid;
+	}
+	g_strfreev (argv);
+	g_free (file_uri);
+	
+	icon_view->details->audio_preview_timeout = 0;
+#elif USE_OLD_AUDIO_PREVIEW	
 	NautilusFile *file;
 	FMIconView *icon_view;
 	FILE *sound_process;
@@ -1838,7 +1899,11 @@ static void
 preview_audio (FMIconView *icon_view, NautilusFile *file, gboolean start_flag)
 {		
 	/* Stop current audio playback */
-#if USE_OLD_AUDIO_PREVIEW
+#if USE_GST_LAUNCH_AUDIO_PREVIEW
+	if (icon_view->details->audio_preview_pid > 0) {
+		kill (icon_view->details->audio_preview_pid, SIGTERM);
+	}
+#elif USE_OLD_AUDIO_PREVIEW
 	if (audio_preview_pid > 0) {
 		kill (-audio_preview_pid, SIGTERM);
 		waitpid (audio_preview_pid, NULL, 0);
@@ -1858,7 +1923,9 @@ preview_audio (FMIconView *icon_view, Na
 			
 	if (start_flag) {
 		icon_view->details->audio_preview_file = file;
-#if USE_OLD_AUDIO_PREVIEW			
+#if USE_GST_LAUNCH_AUDIO_PREVIEW			
+		icon_view->details->audio_preview_timeout = g_timeout_add (1000, play_file, icon_view);
+#elif USE_OLD_AUDIO_PREVIEW			
 		icon_view->details->audio_preview_timeout = g_timeout_add (1000, play_file, icon_view);
 #else
 		/* FIXME: Need to kill the existing timeout if there is one? */
@@ -1890,6 +1957,9 @@ should_preview_sound (NautilusFile *file
 static inline gboolean
 can_play_sound (void)
 {
+#if USE_GST_LAUNCH_AUDIO_PREVIEW
+	return TRUE;
+#else
 	int open_result;
 
 #if USE_OLD_AUDIO_PREVIEW			
@@ -1908,6 +1978,7 @@ can_play_sound (void)
 	esd_close (open_result);
 
 	return TRUE;
+#endif
 }
 
 static int
