? =
Index: configure.in
===================================================================
RCS file: /cvs/gnome/gnome-utils/configure.in,v
retrieving revision 1.358
diff --unified -p -r1.358 configure.in
--- configure.in	11 Oct 2004 23:18:03 -0000	1.358
+++ configure.in	20 Oct 2004 14:30:09 -0000
@@ -48,6 +48,29 @@ PKG_CHECK_MODULES(LIBGLADE, libglade-2.0
 AC_SUBST(LIBGLADE_CFLAGS)
 AC_SUBST(LIBGLADE_LIBS)
 
+dnl ****************************************
+dnl HAL for floppy detection in gfloppy
+dnl ****************************************
+AC_ARG_ENABLE(hal,
+AC_HELP_STRING([--enable-hal],[use hal, if available]),
+[case "${enableval}" in
+yes) ENABLE_HAL=yes ;;
+no)  ENABLE_HAL=no ;;
+*) AC_MSG_ERROR(bad value ${enableval} for --enable-hal) ;;
+esac],
+[ENABLE_HAL=yes]) dnl Default value
+
+if test "x$ENABLE_HAL" = "xyes"; then
+	PKG_CHECK_MODULES(HAL, hal >= 0.4.0,
+		AC_DEFINE(USE_HAL, 1, [defined if using libhal]),
+		[ USE_HAL=""])
+else
+	USE_HAL=""
+fi
+
+AC_SUBST(HAL_CFLAGS)
+AC_SUBST(HAL_LIBS)
+
 dnl *****************************
 dnl popt is needed for most utils
 dnl *****************************
Index: gfloppy/src/Makefile.am
===================================================================
RCS file: /cvs/gnome/gfloppy/src/Makefile.am,v
retrieving revision 1.24
diff --unified -p -r1.24 Makefile.am
--- gfloppy/src/Makefile.am	22 Mar 2004 10:20:05 -0000	1.24
+++ gfloppy/src/Makefile.am	20 Oct 2004 14:30:09 -0000
@@ -5,7 +5,8 @@ INCLUDES = \
         -DGLADEDIR=\""$(gladedir)"\"           	\
         -DDATADIR=\""$(datadir)"\"		\
 	$(GNOME_UTILS_CFLAGS)			\
-	$(LIBGLADE_CFLAGS)
+	$(LIBGLADE_CFLAGS)			\
+	$(HAL_CFLAGS)
 
 bin_PROGRAMS = gfloppy
 
@@ -24,7 +25,7 @@ gfloppy_SOURCES = \
 	fdformat.c \
 	badblocks.c
 
-gfloppy_LDADD = $(GNOME_UTILS_LIBS) ${LIBGLADE_LIBS} -lext2fs
+gfloppy_LDADD = $(GNOME_UTILS_LIBS) ${LIBGLADE_LIBS} $(HAL_LIBS) -lext2fs
 
 gfloppy_LDFLAGS = -export-dynamic
 
Index: gfloppy/src/main.c
===================================================================
RCS file: /cvs/gnome/gfloppy/src/main.c,v
retrieving revision 1.52
diff --unified -p -r1.52 main.c
--- gfloppy/src/main.c	12 Feb 2004 14:17:41 -0000	1.52
+++ gfloppy/src/main.c	20 Oct 2004 14:30:09 -0000
@@ -31,6 +31,10 @@
 #include <sys/types.h>
 #include <errno.h>
 
+#ifdef USE_HAL
+#  include <hal/libhal.h>
+#endif
+
 #include "gfloppy.h"
 #include "gfloppy-config.h"
 #include "progress.h"
@@ -50,6 +54,13 @@ gboolean prefer_mkdosfs_backend;
 static GList *valid_devices = NULL;
 
 
+typedef struct _FloppyStatus
+{
+	GFloppyStatus status;
+	gchar *device_path;
+} FloppyStatus;
+
+
 static GFloppyFormattingMode get_selected_formatting_mode ();
 static void type_option_changed_cb     (GtkOptionMenu   *option_menu,
 					gpointer         data);
@@ -58,6 +69,18 @@ static void set_widget_sensitive_from  (
 					gpointer         data);
 */
 
+static void 
+floppy_status_free (FloppyStatus *floppies, gint num_floppies)
+{
+	gint i;
+
+	for (i = 0; i < num_floppies; i++) {
+		g_free (floppies[i].device_path);
+	}
+
+	g_free (floppies);
+}
+
 static char *
 get_mformat_device (char *device)
 {
@@ -107,6 +130,52 @@ start_format (void)
 	setup_progress_and_run (&floppy, toplevel);
 }
 
+#ifdef USE_HAL
+
+#define HAL_FLOPPY_KEY   "storage.drive_type"
+#define HAL_FLOPPY_VALUE "floppy"
+
+#define HAL_FLOPPY_DEV_KEY "block.device"
+
+static FloppyStatus *
+get_hal_floppies (gint *num_devices, gint *num_ok, LibHalContext *ctx)
+{
+	gint              i;
+	gchar           **device_list;
+	FloppyStatus *floppies;
+
+	device_list = hal_manager_find_device_string_match (ctx,
+							    HAL_FLOPPY_KEY, 
+							    HAL_FLOPPY_VALUE, 
+							    num_devices); 
+
+	floppies = NULL;
+	
+	*num_ok = 0;
+
+	if (*num_devices != 0) {
+		floppies = g_new0 (FloppyStatus, *num_devices);
+
+		for (i = 0; i < *num_devices; i++) {
+			floppies[i].device_path = 
+				hal_device_get_property_string (ctx, 
+								device_list[i], 
+								HAL_FLOPPY_DEV_KEY);
+
+			floppies[i].status = test_floppy_device (floppies[i].device_path);
+
+			if (floppies[i].status == GFLOPPY_DEVICE_OK)
+				(*num_ok)++;
+		}
+		
+		hal_free_string_array (device_list);
+	}
+
+	return floppies;
+}
+
+#endif
+
 #include <stdlib.h>
 
 static void
@@ -147,7 +216,7 @@ init_commands (void)
 }
 
 static void
-init_devices (void)
+init_devices (gpointer ctx)
 {
 	GtkWidget *device_option;
 	GtkWidget *device_entry;
@@ -188,30 +257,53 @@ init_devices (void)
 		/* Device is okay */
 		ok_devices_present = 1;
 	} else {
-		GFloppyStatus status[NUM_DEVICES_TO_CHECK];
-		char device[32];
+		gint num_devices_to_check = NUM_DEVICES_TO_CHECK;
+
 		gint i;
+		FloppyStatus *floppies;
+
+#ifdef USE_HAL
+		gint num_hal_floppies_ok;
+
+
+		if (ctx != NULL) {
+			floppies = get_hal_floppies (&num_devices_to_check, 
+						     &num_hal_floppies_ok, 
+						     (LibHalContext *) ctx);
+
+			ok_devices_present += num_hal_floppies_ok;
 
-		for (i = 0; i < NUM_DEVICES_TO_CHECK; i++) {
-			sprintf (device,"/dev/floppy/%d", i);
-			status[i] = test_floppy_device (device);
-
-			if (status[i] == GFLOPPY_NO_DEVICE && !devfs_mode) {
-				sprintf (device,"/dev/fd%d", i);
-				status[i] = test_floppy_device (device);
+			goto FloppiesFound;
+		}
+
+		/* If hal didn't initialize properly, fall back to the manual way */
+#endif 
+
+		floppies = g_new0 (FloppyStatus, num_devices_to_check);
+
+		for (i = 0; i < num_devices_to_check; i++) {
+			floppies[i].device_path = g_new0 (gchar, 32);
+			sprintf (floppies[i].device_path,"/dev/floppy/%d", i);
+			floppies[i].status = test_floppy_device (floppies[i].device_path);
+
+			if (floppies[i].status == GFLOPPY_NO_DEVICE && !devfs_mode) {
+				sprintf (floppies[i].device_path,"/dev/fd%d", i);
+				floppies[i].status = test_floppy_device (floppies[i].device_path);
 			} else
 				devfs_mode = TRUE;
 
-			if (status[i] == GFLOPPY_DEVICE_OK) 
+			if (floppies[i].status == GFLOPPY_DEVICE_OK) 
 				ok_devices_present++;
 		}
 
+	FloppiesFound:
+
 		if (ok_devices_present == 0) {
 			GtkWidget *toplevel;
 			GtkWidget *dialog;
 
 			/* FIXME: Let's assume that there's only a problem with /dev/fd0 */
-			switch (status[0]) {
+			switch (floppies[0].status) {
 			case GFLOPPY_NO_DEVICE:
 				msg = g_strdup_printf (_("Unable to open any device, formatting cannot continue."));
 				break;
@@ -236,22 +328,23 @@ init_devices (void)
 			gtk_widget_destroy (dialog);
 			exit (1);
 		} else if (ok_devices_present == 1) {
-			for (i = 0; i < NUM_DEVICES_TO_CHECK; i++)
-				if (status[i] == GFLOPPY_DEVICE_OK) {
-					floppy.device = g_strdup_printf ("%s%d", (devfs_mode ? "/dev/floppy/" : "/dev/fd"), i);
+			for (i = 0; i < num_devices_to_check; i++)
+				if (floppies[i].status == GFLOPPY_DEVICE_OK) {
+					floppy.device = g_strdup (floppies[i].device_path);
 					break;
 				}
 		} else {
-			for (i = 0; i < NUM_DEVICES_TO_CHECK; i++)
-				if (status[i] == GFLOPPY_DEVICE_OK) {
-					char *d = g_strdup_printf ("%s%d", (devfs_mode ? "/dev/floppy/" : "/dev/fd"), i);
-
-					valid_devices = g_list_append (valid_devices, d);
+			for (i = 0; i < num_devices_to_check; i++)
+				if (floppies[i].status == GFLOPPY_DEVICE_OK) {
+					valid_devices = g_list_append (valid_devices, 
+								       g_strdup (floppies[i].device_path));
 
 					if (floppy.device == NULL)
-						floppy.device = g_strdup (d);
+						floppy.device = g_strdup (floppies[i].device_path);
 				}
 		}
+
+		floppy_status_free (floppies, num_devices_to_check);
 	}
 
 	floppy.mdevice = g_strdup (get_mformat_device (floppy.device));
@@ -467,6 +560,10 @@ main (int argc, char *argv[])
 	GSList *l;
         gint base_size;
 
+
+	/* This is for the libhal context, should we need it */
+	gpointer ctx;
+
 	struct poptOption gfloppy_opts[] = {
 		{"device", '\0', POPT_ARG_STRING, NULL, 0, NULL, NULL},
 		{NULL, '\0', 0, NULL, 0, NULL, NULL}
@@ -486,6 +583,10 @@ main (int argc, char *argv[])
                             GNOME_PARAM_POPT_FLAGS, 0,
                             GNOME_PARAM_APP_DATADIR,DATADIR,NULL);
 
+#ifdef USE_HAL
+	ctx = (gpointer) hal_initialize (NULL, FALSE);
+#endif
+
         icon_theme = gnome_icon_theme_new ();
         icon_path = gnome_icon_theme_lookup_icon (icon_theme, "gnome-dev-floppy", 24.0,
                                                               &icon_data, &base_size);
@@ -503,7 +604,7 @@ main (int argc, char *argv[])
 	if (xml == NULL)
 		g_error ("Cannot load/find gfloppy2.glade");
 
-	init_devices ();
+	init_devices (ctx);
 
 	/* We get the different widgets */
 	
@@ -611,6 +712,10 @@ main (int argc, char *argv[])
 	gfloppy_config_save (&config, client);
 
 	g_object_unref (G_OBJECT (client));
+
+#ifdef USE_HAL
+	hal_shutdown (ctx);
+#endif
 
 	return 0;
 }
