[nautilus-actions] An helper for importing a list of URIs



commit fce4386298cad97e0056c705d3c4b2aad689873f
Author: pierre <pierre vfedora10 virtuals pwi>
Date:   Mon Mar 1 16:18:18 2010 +0100

    An helper for importing a list of URIs
    
    Define a new NAIImporterListParms parameters structure.
    Define a new na_importer_import_from_list() function.

 ChangeLog              |    5 ++
 src/api/na-iimporter.h |   19 ++++++++-
 src/core/na-importer.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/core/na-importer.h |    4 +-
 4 files changed, 128 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4f4dc06..a9b6c60 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2009-03-01 Pierre Wieser <pwieser trychlos org>
 
+	* src/api/na-iimporter.h (NAIImporterListParms): New structure.
+
+	* src/core/na-importer.c:
+	* src/core/na-importer.h (na_importer_import_from_list): New function.
+
 	* src/api/na-iimporter.h:
 	* src/core/na-iimporter.c:
 	* src/core/na-importer-ask.c:
diff --git a/src/api/na-iimporter.h b/src/api/na-iimporter.h
index ed85dba..f0f94a3 100644
--- a/src/api/na-iimporter.h
+++ b/src/api/na-iimporter.h
@@ -54,6 +54,7 @@ G_BEGIN_DECLS
 
 typedef struct NAIImporter                 NAIImporter;
 typedef struct NAIImporterUriParms         NAIImporterUriParms;
+typedef struct NAIImporterListParms        NAIImporterListParms;
 
 typedef struct NAIImporterInterfacePrivate NAIImporterInterfacePrivate;
 
@@ -113,9 +114,10 @@ enum {
  * same id than the currently being imported one, or %NULL if the
  * imported id will be unique
  */
-typedef NAObjectItem * ( *NAIImporterCheckFn )( const NAObjectItem *, void *fn_data );
+typedef NAObjectItem * ( *NAIImporterCheckFn )( const NAObjectItem *, const void *fn_data );
 
 /* parameters via a structure
+ * ... when importing a single uri
  */
 struct NAIImporterUriParms {
 	guint              version;			/* i 1: version of this structure */
@@ -130,6 +132,21 @@ struct NAIImporterUriParms {
 										 *       but shouldn't reinitialize it. */
 };
 
+/* ... when importing a list of uris
+ */
+struct NAIImporterListParms {
+	guint              version;			/* i 1: version of this structure */
+	GSList            *uris;			/* i 1: list of uris of the files to be imported */
+	guint              mode;			/* i 1: import mode */
+	GtkWindow         *window;			/* i 1: a window which will act as a parent of the ask dialog */
+	GList             *imported;		/*  o1: list of eventually imported NAObjectItem-derived objects */
+	NAIImporterCheckFn check_fn;		/* i 1: a function to check the existance of each imported item */
+	void              *check_fn_data;	/* i 1: data function */
+	GSList            *messages;		/* io1: a #GSList list of localized strings;
+										 *       the provider may append messages to this list,
+										 *       but shouldn't reinitialize it. */
+};
+
 GType na_iimporter_get_type( void );
 
 guint na_iimporter_ask_user( const NAIImporter *importer, const NAIImporterUriParms *parms, const NAObjectItem *existing );
diff --git a/src/core/na-importer.c b/src/core/na-importer.c
index cba0613..783c77f 100644
--- a/src/core/na-importer.c
+++ b/src/core/na-importer.c
@@ -32,13 +32,19 @@
 #include <config.h>
 #endif
 
+#include <string.h>
+
+#include <api/na-object-api.h>
+
 #include "na-importer.h"
 
 extern gboolean iimporter_initialized;		/* defined in na-iimporter.c */
 extern gboolean iimporter_finalized;		/* defined in na-iimporter.c */
 
+static NAObjectItem *is_importing_already_exists( const NAObjectItem *importing, const NAIImporterListParms *parms );
+
 /**
- * na_importer_import:
+ * na_importer_import_from_uri:
  * @pivot: the #NAPivot pivot for this application.
  * @parms: a #NAIImporterUriParms structure.
  *
@@ -51,12 +57,15 @@ na_importer_import_from_uri( const NAPivot *pivot, NAIImporterUriParms *parms )
 	GList *modules, *im;
 	guint code;
 
+	g_return_val_if_fail( NA_IS_PIVOT( pivot ), IMPORTER_CODE_PROGRAM_ERROR );
+
 	g_debug( "%s: pivot=%p, parms=%p", thisfn, ( void * ) pivot, ( void * ) parms );
 
-	code = IMPORTER_CODE_NOT_WILLING_TO;
+	code = IMPORTER_CODE_PROGRAM_ERROR;
 
 	if( iimporter_initialized && !iimporter_finalized ){
 
+		code = IMPORTER_CODE_NOT_WILLING_TO;
 		modules = na_pivot_get_providers( pivot, NA_IIMPORTER_TYPE );
 
 		for( im = modules ; im && code == IMPORTER_CODE_NOT_WILLING_TO ; im = im->next ){
@@ -71,3 +80,94 @@ na_importer_import_from_uri( const NAPivot *pivot, NAIImporterUriParms *parms )
 
 	return( code );
 }
+
+/**
+ * na_importer_import_from_list:
+ * @pivot: the #NAPivot pivot for this application.
+ * @parms: a #NAIImporterListParms structure.
+ *
+ * Imports a list of URIs.
+ *
+ * Each successfully imported #NAObjectItem-derived object is added to
+ * the 'imported' list of the structure, in the same order than the URIs.
+ *
+ * Messages which may be generated by each individual import operation
+ * are added to the 'messages' member of @parms.
+ *
+ * Returns: the last import operation code.
+ */
+guint
+na_importer_import_from_list( const NAPivot *pivot, NAIImporterListParms *parms )
+{
+	static const gchar *thisfn = "na_importer_import_from_list";
+	GSList *iuri;
+	NAIImporterUriParms uri_parms;
+	guint code;
+
+	g_return_val_if_fail( NA_IS_PIVOT( pivot ), IMPORTER_CODE_PROGRAM_ERROR );
+
+	g_debug( "%s: pivot=%p, parms=%p", thisfn, ( void * ) pivot, ( void * ) parms );
+
+	code = IMPORTER_CODE_PROGRAM_ERROR;
+	parms->imported = NULL;
+
+	if( iimporter_initialized && !iimporter_finalized ){
+
+		code = IMPORTER_CODE_NOT_WILLING_TO;
+
+		uri_parms.version = 1;
+		uri_parms.mode = parms->mode;
+		uri_parms.window = parms->window;
+		uri_parms.check_fn = ( NAIImporterCheckFn ) is_importing_already_exists;
+		uri_parms.check_fn_data = parms;
+		uri_parms.messages = parms->messages;
+
+		for( iuri = parms->uris ; iuri ; iuri = iuri->next ){
+
+			uri_parms.uri = ( gchar * ) iuri->data;
+			uri_parms.imported = NULL;
+
+			code = na_importer_import_from_uri( pivot, &uri_parms );
+
+			if( uri_parms.imported ){
+				parms->imported = g_list_prepend( parms->imported, uri_parms.imported );
+			}
+		}
+
+		parms->imported = g_list_reverse( parms->imported );
+	}
+
+	return( code );
+}
+
+static NAObjectItem *
+is_importing_already_exists( const NAObjectItem *importing, const NAIImporterListParms *parms )
+{
+	NAObjectItem *exists;
+	GList *ip;
+
+	exists = NULL;
+	gchar *importing_id = na_object_get_id( importing );
+	g_debug( "na_importer_is_importing_already_exists: importing_id=%s", importing_id );
+
+	/* is the importing item already in the current importation list ?
+	 */
+	for( ip = parms->imported ; ip && !exists ; ip = ip->next ){
+		gchar *id = na_object_get_id( ip->data );
+		if( !strcmp( importing_id, id )){
+			exists = NA_OBJECT_ITEM( ip->data );
+		}
+		g_free( id );
+	}
+
+	g_free( importing_id );
+
+	/* if not found in our current importation list,
+	 * then check the via provided function and data
+	 */
+	if( !exists ){
+		exists = parms->check_fn( importing, parms->check_fn_data );
+	}
+
+	return( exists );
+}
diff --git a/src/core/na-importer.h b/src/core/na-importer.h
index 6d696f8..12b6e9d 100644
--- a/src/core/na-importer.h
+++ b/src/core/na-importer.h
@@ -44,7 +44,9 @@
 
 G_BEGIN_DECLS
 
-guint na_importer_import_from_uri( const NAPivot *pivot, NAIImporterUriParms *parms );
+guint na_importer_import_from_uri ( const NAPivot *pivot, NAIImporterUriParms *parms );
+
+guint na_importer_import_from_list( const NAPivot *pivot, NAIImporterListParms *parms );
 
 G_END_DECLS
 



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