--- gnome-utils/ChangeLog	Mon Sep  8 17:22:10 2003
+++ gnome-utils-new/ChangeLog	Fri Nov  7 16:02:37 2003
@@ -1,3 +1,11 @@
+2003-11-07  Vijaykumar Patwari <vijaykumar.patwari@wipro.com>
+	
+	* gfloppy/src/main.c: (on_format_button_clicked) Added 
+	confirmation dialog for	continuing format.
+	(floppy_mounted) To check if floppy is already mounted.
+	(mount_unmount_device) To mount and unmount the device.
+	Fix bug# 125819.
+
 2003-09-08  Glynn Foster  <glynn.foster@sun.com>
 
 	* configure.in: Release 2.4.0
--- gnome-utils/gfloppy/src/main.c	Sat May 31 11:49:31 2003
+++ gnome-utils-new/gfloppy/src/main.c	Fri Nov  7 16:09:51 2003
@@ -29,6 +29,7 @@
 #include <glade/glade.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/poll.h>
 #include <errno.h>
 
 #include "gfloppy.h"
@@ -36,6 +37,7 @@
 #include "progress.h"
 
 #define NUM_DEVICES_TO_CHECK 4
+#define POLLTIMEOUT 15000
 
 extern int errno;
 
@@ -56,6 +58,8 @@ static void type_option_changed_cb     (
 static void set_widget_sensitive_from  (GtkToggleButton *button,
 					gpointer         data);
 */
+static gboolean floppy_mounted (const gchar *mount_point);
+static gboolean mount_unmount_device (gchar *command, gboolean flag);
 
 static void
 devices_option_activated (GtkWidget *menu_item, gchar *device)
@@ -411,17 +415,138 @@ on_help_button_clicked (GtkWidget *widge
 	}
 }
 
+static gboolean
+floppy_mounted (const gchar *mount_point)
+{
+	FILE *fp;
+	struct pollfd fd;
+	gboolean device_mounted = FALSE;
+	gchar *command = "mount";
+	gchar buf[256];
+	gchar *ptr;
+	gchar *p;
+	gint retval;
+                                                                                                                              
+	fp = popen (command, "r");
+	if (!fp) {
+		return FALSE;
+	}
+                                                                                                                              
+	fd.fd = fp->_fileno;
+	fd.events = POLLIN;
+
+	retval = poll (&fd, 1, POLLTIMEOUT);
+                                                                                                                              
+	if ((retval > 0) && !(fd.revents == POLLERR || fd.revents == POLLNVAL)) {
+		while (fgets (buf, sizeof (buf), fp)) {
+			ptr = strstr (buf, mount_point);
+				if (ptr != NULL) {
+					p = ptr + strlen (mount_point);
+					if (*p == ' ' || *p == '\0' || *p == '\n') {
+						device_mounted = TRUE;
+						break;
+					}
+				}
+		}
+	} else 
+		/*FIXME: What error dialog should be thrown?. */
+		g_warning ("Poll Error\n");
+                                                                                                                              
+	pclose (fp);
+	return device_mounted;
+}
+
+static gboolean
+mount_unmount_device (gchar *command, gboolean flag)
+{
+	GtkWidget *error_dialog;
+	FILE *fp;
+	struct pollfd fd;
+	GString *str;
+	gchar buf[512];
+	gchar *b;
+	gint retval;
+                                                                                                                              
+	fp = popen (command, "r");
+	if (!fp) {
+		g_warning ("unable to run command: %s\n", command);
+		return FALSE;
+	}
+                                                                                                                              
+	fd.fd = fp->_fileno;
+	fd.events = POLLIN;
+                                                                                                                              
+	retval = poll (&fd, 1, POLLTIMEOUT);
+
+	if ((retval > 0) && !(fd.revents == POLLERR || fd.revents == POLLNVAL)) {
+		str = g_string_new (NULL);
+		while (fgets (buf, sizeof (buf), fp) != NULL) {
+			b = buf;
+			g_string_append (str, b);
+		}
+	} else {
+		/*FIXME: What error dialog should be thrown?. */
+		g_warning ("Poll Error\n");
+		pclose (fp);
+		return FALSE;
+	}
+                                                                                                                              
+	if ((flag && !floppy_mounted (floppy.device)) || (!flag  && floppy_mounted (floppy.device))) {
+		error_dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
+						       GTK_DIALOG_DESTROY_WITH_PARENT,
+						       GTK_MESSAGE_ERROR,
+						       GTK_BUTTONS_CLOSE,
+						       "%s", str->str);
+		gtk_dialog_run (GTK_DIALOG (error_dialog));
+		gtk_widget_destroy (error_dialog);
+		g_string_free (str, FALSE);
+		pclose (fp);
+		return FALSE;
+	}
+                                                                                                                              
+	g_string_free (str, FALSE);
+	pclose (fp);
+	return TRUE;
+}
+
 void
 on_format_button_clicked (GtkWidget *widget, gpointer user_data)
 {
 	GtkWidget *type_option;
 	GtkWidget *density_option;
 	GtkWidget *volume_name_entry;
+	GtkWidget *dialog;
+	gchar *command;
+	gint response;
+	gboolean unmounted = FALSE;
 
         type_option = glade_xml_get_widget (xml, "type_option");
         density_option = glade_xml_get_widget (xml, "density_option");
 	volume_name_entry = glade_xml_get_widget (xml, "volume_name_entry");
 
+	dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
+					 GTK_DIALOG_DESTROY_WITH_PARENT,
+					 GTK_MESSAGE_QUESTION,
+					 GTK_BUTTONS_YES_NO,
+					 _("Are you sure you want to continue formatting?. All the information saved will be lost."));
+                                                                                                                              
+	response = gtk_dialog_run (GTK_DIALOG (dialog));
+	gtk_widget_destroy (dialog);
+        
+	if (response == GTK_RESPONSE_YES) {
+		if (floppy_mounted (floppy.device)) {
+			command = g_strdup_printf (("umount %s 2>&1"), floppy.device);
+                                                                                                                              
+			if (mount_unmount_device (command, FALSE)) {
+				unmounted = TRUE;
+			} else {
+				g_free (command);
+				return;
+			}
+		}
+	} else
+		return;
+
 	floppy.volume_name = (gchar *) gtk_entry_get_text (GTK_ENTRY (volume_name_entry));
 
 	if (strstr (floppy.volume_name, " ")) {
@@ -451,6 +576,14 @@ on_format_button_clicked (GtkWidget *wid
 	set_floppy_extended_device ();
 
         start_format ();
+
+	if (!floppy_mounted (floppy.device) && unmounted) {
+		command = g_strdup_printf (("mount %s 2>&1"), floppy.device);
+                                                                                                                              
+		mount_unmount_device (command, TRUE);
+                                                                                                                              
+		g_free (command);
+	}
 
         gtk_widget_set_sensitive (toplevel, TRUE);
 }
