gnome-scan r669 - in trunk: . modules/gsane



Author: bersace
Date: Fri Dec 12 20:46:35 2008
New Revision: 669
URL: http://svn.gnome.org/viewvc/gnome-scan?rev=669&view=rev

Log:
Instanciate OptionHandlers.

Modified:
   trunk/ChangeLog
   trunk/modules/gsane/gsane-scanner.c

Modified: trunk/modules/gsane/gsane-scanner.c
==============================================================================
--- trunk/modules/gsane/gsane-scanner.c	(original)
+++ trunk/modules/gsane/gsane-scanner.c	Fri Dec 12 20:46:35 2008
@@ -31,17 +31,26 @@
 #include <gnome-scan-module-helper.h>
 #include <sane/sane.h>
 #include "gsane-common.h"
+#include "gsane-option-manager.h"
+#include "gsane-option-handler.h"
 #include "gsane-scanner.h"
 
 #define GSANE_SCANNER_GET_PRIVATE(o)	(G_TYPE_INSTANCE_GET_PRIVATE ((o), GSANE_TYPE_SCANNER, GSaneScannerPrivate))
 
 struct _GSaneScannerPrivate
 {
+	/* properties */
 	gchar* sane_id;
 	gchar* sane_type;
 
-	gboolean probe_done;
+	/* fields */
+	GHashTable* option_handlers;
+
+	/* SANE */
 	SANE_Handle handle;
+
+	/* status */
+	gboolean probe_done;
 };
 
 enum {
@@ -68,7 +77,7 @@
 }
 
 gboolean
-gsane_scanner_sane_status_is_good(GSaneScanner *self, const gchar* operation, SANE_Status status)
+gsane_scanner_check_sane_status(GSaneScanner *self, const gchar* operation, SANE_Status status)
 {
 	/* TODO: update node status, handle error/warning */
 	if (status != SANE_STATUS_GOOD) {
@@ -79,22 +88,80 @@
 	return status == SANE_STATUS_GOOD;
 }
 
+static void
+gsane_scanner_handle_sane_option(GSaneScanner*self, SANE_Int n, const SANE_Option_Descriptor* desc, const gchar* group)
+{
+	/* retrieve handler type */
+	GSaneOptionManager* mngr = gsane_option_manager_new();
+	GType handler_type = gsane_option_manager_get_handler_type(mngr, desc);
+	if (handler_type == G_TYPE_INVALID)
+		return;
+
+	GSaneOptionHandlerClass* handler_class = g_type_class_ref(handler_type);
+	GSaneOptionHandler* handler = NULL;
+	const gchar* key;
+
+	if (handler_class->unique) {
+		key = g_type_name(handler_type);
+		handler = g_hash_table_lookup(self->priv->option_handlers, key);
+	}
+	else {
+		key = desc->name;
+	}
+
+	if (handler == NULL) {
+		handler = gsane_option_handler_new(handler_type, GNOME_SCAN_SCANNER(self), self->priv->handle);
+		g_hash_table_insert(self->priv->option_handlers, g_strdup(key), handler);
+	}
+
+	/* pass SANE option to handler */
+	gsane_option_handler_handle_option(handler, n, desc, group);
+
+	g_type_class_unref(handler_class);
+}
+
 /* GThreadFunc */
 static void*
 gsane_scanner_probe_options(GSaneScanner *self)
 {
 	SANE_Status status;
+	const SANE_Option_Descriptor* desc;
 	SANE_Int count;
+	const gchar* group = NULL;
+	gchar* devname;
+	gint n;
 
+	g_object_get(self, "name", &devname, NULL);
 	status = sane_open(self->priv->sane_id, &self->priv->handle);
 	gchar *operation = g_strdup_printf("sane_open(%s)", self->priv->sane_id);
-	if (!gsane_scanner_sane_status_is_good(self, operation, status))
+	if (!gsane_scanner_check_sane_status(self, operation, status)) {
+		g_free(operation);
 		goto end;
-
-	g_free(operation);
+	}
 
 	sane_control_option(self->priv->handle, 0, SANE_ACTION_GET_VALUE, &count,NULL);
-	g_debug("Devices %s : %i options", self->priv->sane_id, count);
+	g_debug("Device %s : %i SANE options", self->priv->sane_id, count);
+
+	for (n = 1; n < count; n++) {
+		desc = sane_get_option_descriptor(self->priv->handle, n);
+		switch(desc->type) {
+		case SANE_TYPE_GROUP:
+			g_debug("Group %s", dgettext("sane-backends", desc->title));
+			group = desc->title;
+			break;
+		case SANE_TYPE_BUTTON:
+			g_debug("Ignoring button %s.", desc->name);
+			break;
+		default:
+			if (SANE_OPTION_IS_SETTABLE(desc->cap)) {
+				gsane_scanner_handle_sane_option(self, n, desc, group);
+			}
+			else {
+				g_debug("Ignoring sensor : %s", desc->name);
+			}
+			break;
+		}
+	}
 
 	gnome_scan_node_update_status(GNOME_SCAN_NODE(self), GNOME_SCAN_STATUS_UNCONFIGURED, NULL);
 
@@ -125,7 +192,9 @@
 gsane_scanner_init(GSaneScanner *self)
 {
 	self->priv = GSANE_SCANNER_GET_PRIVATE(self);
-	self->priv->probe_done = FALSE;
+	self->priv->option_handlers	= g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)gsane_option_handler_destroy);
+	self->priv->handle		= NULL;
+	self->priv->probe_done		= FALSE;
 }
 
 static void
@@ -134,6 +203,7 @@
 	GSaneScanner *self = GSANE_SCANNER(object);
 	self->priv->sane_id = (self->priv->sane_id == NULL ? NULL : g_free(self->priv->sane_id), NULL);
 	self->priv->sane_type = (self->priv->sane_type == NULL ? NULL : g_free(self->priv->sane_type), NULL);
+	g_hash_table_destroy(self->priv->option_handlers);
 }
 
 static void



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