[nautilus-actions] Review pre- and post- I/O providers write operations



commit 20d92e5f4b87ebd7ed7838923c9764e79bd4d6b0
Author: pierre <pierre vfedora10 virtuals pwi>
Date:   Tue Aug 10 17:11:12 2010 +0200

    Review pre- and post- I/O providers write operations
    
    Assure that type and version are correctly written.

 ChangeLog                         |   11 +++++
 TODO                              |    4 --
 src/core/na-object-action.c       |   71 +++++++++++++++++++++--------------
 src/core/na-object-item-factory.c |    5 ++-
 src/io-desktop/nadp-writer.c      |   74 ++++++++++++++++++++++---------------
 src/io-gconf/nagp-writer.c        |   55 +++++++++++++++++++--------
 src/io-xml/naxml-writer.c         |   59 ++++++++++++++++++++---------
 7 files changed, 180 insertions(+), 99 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5c79afd..7b13ad7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2010-08-09 Pierre Wieser <pwieser trychlos org>
 
+	Review pre- and post- write operations in I/O providers.
+
+	* src/core/na-object-action.c: Cleanup the code.
+
+	* src/core/na-object-item-factory.c:
+	NAFO_DATA_IVERSION becomes unwritable.
+
+	* src/io-desktop/nadp-writer.c:
+	* src/io-gconf/nagp-writer.c:
+	* src/io-xml/naxml-writer.c: Cleanup the code.
+
 	Immediately reacts to a status change in I/O providers.
 
 	* src/core/na-ipivot-consumer.c:
diff --git a/TODO b/TODO
index 537cf63..adfa6a5 100644
--- a/TODO
+++ b/TODO
@@ -16,10 +16,6 @@ TODO
 
 - 2010-08-09: in v1.x until ??, %f used to be the basename!!
 
-- "write even if default" was added for version only
-  but version should not been written in desktop files (not in the spec)
-  so version should be explictely written (as type)
-
 - an empty list is not a default value
   e.g. basenames default value = [*]
   after deleting this default value in the UI, we should write basenames=[] in the I/O provider
diff --git a/src/core/na-object-action.c b/src/core/na-object-action.c
index 96ce4a1..fda2eb9 100644
--- a/src/core/na-object-action.c
+++ b/src/core/na-object-action.c
@@ -87,9 +87,11 @@ static guint        ifactory_object_write_done( NAIFactoryObject *instance, cons
 static void         icontext_iface_init( NAIContextInterface *iface );
 static gboolean     icontext_is_candidate( NAIContext *object, guint target, GList *selection );
 
-static void         convert_v1_to_v2( NAIFactoryObject *instance );
-static void         deals_with_toolbar_label( NAIFactoryObject *instance );
-static void         deals_with_version( NAIFactoryObject *instance );
+static void         read_done_convert_v1_to_v2( NAIFactoryObject *instance );
+static void         read_done_deals_with_toolbar_label( NAIFactoryObject *instance );
+static void         read_done_deals_with_version( NAIFactoryObject *instance );
+
+static guint        write_done_write_profiles( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages );
 
 static gboolean     object_object_is_valid( const NAObjectAction *action );
 static gboolean     is_valid_label( const NAObjectAction *action );
@@ -342,13 +344,13 @@ ifactory_object_read_done( NAIFactoryObject *instance, const NAIFactoryProvider
 
 	/* may attach a new profile if we detect a pre-v2 action
 	 */
-	convert_v1_to_v2( instance );
+	read_done_convert_v1_to_v2( instance );
 
 	/* deals with obsoleted data, i.e. data which may have been written in the past
 	 * but are no long written by now
 	 */
-	deals_with_toolbar_label( instance );
-	deals_with_version( instance );
+	read_done_deals_with_toolbar_label( instance );
+	read_done_deals_with_version( instance );
 
 	/* prepare the context after the reading
 	 */
@@ -372,27 +374,11 @@ ifactory_object_write_start( NAIFactoryObject *instance, const NAIFactoryProvide
 static guint
 ifactory_object_write_done( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages )
 {
-	static const gchar *thisfn = "na_object_action_ifactory_object_write_done";
 	guint code;
-	GSList *children_slist, *ic;
-	NAObjectProfile *profile;
-
-	code = NA_IIO_PROVIDER_CODE_OK;
-
-	if( NA_IS_OBJECT_ACTION( instance )){
-		children_slist = na_object_get_items_slist( instance );
-
-		for( ic = children_slist ; ic && code == NA_IIO_PROVIDER_CODE_OK ; ic = ic->next ){
-			profile = NA_OBJECT_PROFILE( na_object_get_item( instance, ic->data ));
 
-			if( profile ){
-				code = na_ifactory_provider_write_item( writer, writer_data, NA_IFACTORY_OBJECT( profile ), messages );
+	g_return_val_if_fail( NA_IS_OBJECT_ACTION( instance ), NA_IIO_PROVIDER_CODE_PROGRAM_ERROR );
 
-			} else {
-				g_warning( "%s: profile not found: %s", thisfn, ( const gchar * ) ic->data );
-			}
-		}
-	}
+	code = write_done_write_profiles( instance, writer, writer_data, messages );
 
 	return( code );
 }
@@ -420,9 +406,9 @@ icontext_is_candidate( NAIContext *object, guint target, GList *selection )
  *  -> move obsoleted data to a new profile, updating the version string
  */
 static void
-convert_v1_to_v2( NAIFactoryObject *instance )
+read_done_convert_v1_to_v2( NAIFactoryObject *instance )
 {
-	static const gchar *thisfn = "na_object_action_convert_v1_to_v2";
+	static const gchar *thisfn = "na_object_action_read_done_convert_v1_to_v2";
 	gboolean is_pre_v2;
 	GList *to_move;
 	NADataDef *def;
@@ -478,7 +464,7 @@ convert_v1_to_v2( NAIFactoryObject *instance )
  * if toolbar-same-label is true, then ensure that this is actually true
  */
 static void
-deals_with_toolbar_label( NAIFactoryObject *instance )
+read_done_deals_with_toolbar_label( NAIFactoryObject *instance )
 {
 	gchar *toolbar_label;
 	gchar *action_label;
@@ -508,7 +494,7 @@ deals_with_toolbar_label( NAIFactoryObject *instance )
  *    so we just force a version number to 2
  */
 static void
-deals_with_version( NAIFactoryObject *instance )
+read_done_deals_with_version( NAIFactoryObject *instance )
 {
 	guint version_uint;
 	gchar *version_str;
@@ -530,6 +516,35 @@ deals_with_version( NAIFactoryObject *instance )
 	}
 }
 
+/*
+ * write the profiles of the action
+ * note that subitems string list has been rebuilt on write_start
+ */
+static guint
+write_done_write_profiles( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages )
+{
+	static const gchar *thisfn = "na_object_action_write_done_write_profiles";
+	guint code;
+	GSList *children_slist, *ic;
+	NAObjectProfile *profile;
+
+	code = NA_IIO_PROVIDER_CODE_OK;
+	children_slist = na_object_get_items_slist( instance );
+
+	for( ic = children_slist ; ic && code == NA_IIO_PROVIDER_CODE_OK ; ic = ic->next ){
+		profile = NA_OBJECT_PROFILE( na_object_get_item( instance, ic->data ));
+
+		if( profile ){
+			code = na_ifactory_provider_write_item( writer, writer_data, NA_IFACTORY_OBJECT( profile ), messages );
+
+		} else {
+			g_warning( "%s: profile not found: %s", thisfn, ( const gchar * ) ic->data );
+		}
+	}
+
+	return( code );
+}
+
 static gboolean
 object_object_is_valid( const NAObjectAction *action )
 {
diff --git a/src/core/na-object-item-factory.c b/src/core/na-object-item-factory.c
index 23ab42a..7062b1e 100644
--- a/src/core/na-object-item-factory.c
+++ b/src/core/na-object-item-factory.c
@@ -391,10 +391,13 @@ NADataDef data_def_item [] = {
 	 * do not have this key.
 	 * Only when version will need to be incremented again, we will write a version
 	 * key in .desktop files (probably 1 or 2), and increment this iversion to 4.
+	 * As this version number will not be the same in .desktop and in GConf, it
+	 * is marked as not automatically writable: it has to be written specifically
+	 * by each i/o provider.
 	 */
 	{ NAFO_DATA_IVERSION,
 				TRUE,
-				TRUE,
+				FALSE,
 				TRUE,
 				N_( "Version of the format" ),
 				N_( "The version of the configuration format that will be used to manage backward compatibility." ),
diff --git a/src/io-desktop/nadp-writer.c b/src/io-desktop/nadp-writer.c
index b74ad55..a432319 100644
--- a/src/io-desktop/nadp-writer.c
+++ b/src/io-desktop/nadp-writer.c
@@ -68,6 +68,9 @@ static guint           write_item( const NAIIOProvider *provider, const NAObject
 
 static void            desktop_weak_notify( NadpDesktopFile *ndf, GObject *item );
 
+static void            write_start_write_type( NadpDesktopFile *ndp, NAObjectItem *item );
+static void            write_done_write_subitems_list( NadpDesktopFile *ndp, NAObjectItem *item );
+
 static ExportFormatFn *find_export_format_fn( GQuark format );
 
 /*
@@ -452,16 +455,22 @@ nadp_writer_ifactory_provider_write_start( const NAIFactoryProvider *provider, v
 							const NAIFactoryObject *object, GSList **messages  )
 {
 	if( NA_IS_OBJECT_ITEM( object )){
-		nadp_desktop_file_set_string(
-				NADP_DESKTOP_FILE( writer_data ),
-				NADP_GROUP_DESKTOP,
-				NADP_KEY_TYPE,
-				NA_IS_OBJECT_ACTION( object ) ? NADP_VALUE_TYPE_ACTION : NADP_VALUE_TYPE_MENU );
+		write_start_write_type( NADP_DESKTOP_FILE( writer_data ), NA_OBJECT_ITEM( object ));
 	}
 
 	return( NA_IIO_PROVIDER_CODE_OK );
 }
 
+static void
+write_start_write_type( NadpDesktopFile *ndp, NAObjectItem *item )
+{
+	nadp_desktop_file_set_string(
+			ndp,
+			NADP_GROUP_DESKTOP,
+			NADP_KEY_TYPE,
+			NA_IS_OBJECT_ACTION( item ) ? NADP_VALUE_TYPE_ACTION : NADP_VALUE_TYPE_MENU );
+}
+
 /*
  * when writing to .desktop file a profile which has both a path and parameters,
  * then concatenate these two fields to the 'Exec' key
@@ -561,36 +570,41 @@ guint
 nadp_writer_ifactory_provider_write_done( const NAIFactoryProvider *provider, void *writer_data,
 							const NAIFactoryObject *object, GSList **messages  )
 {
-	static const gchar *thisfn = "nadp_writer_ifactory_provider_write_done";
+	if( NA_IS_OBJECT_ITEM( object )){
+		write_done_write_subitems_list( NADP_DESKTOP_FILE( writer_data ), NA_OBJECT_ITEM( object ));
+	}
+
+	return( NA_IIO_PROVIDER_CODE_OK );
+}
+
+static void
+write_done_write_subitems_list( NadpDesktopFile *ndp, NAObjectItem *item )
+{
+	static const gchar *thisfn = "nadp_writer_write_done_write_subitems_list";
 	GSList *subitems;
-	GSList *profile_groups;
-	GSList *ip;
+	GSList *profile_groups, *ip;
 
-	if( NA_IS_OBJECT_ITEM( object )){
-		subitems = na_object_get_items_slist( object );
-		na_core_utils_slist_dump( thisfn, subitems );
-
-		nadp_desktop_file_set_string_list(
-				NADP_DESKTOP_FILE( writer_data ),
-				NADP_GROUP_DESKTOP,
-				NA_IS_OBJECT_ACTION( object ) ? NADP_KEY_PROFILES : NADP_KEY_ITEMS_LIST,
-				subitems );
-
-		profile_groups = nadp_desktop_file_get_profiles( NADP_DESKTOP_FILE( writer_data ));
-		na_core_utils_slist_dump( thisfn, profile_groups );
-
-		for( ip = profile_groups ; ip ; ip = ip->next ){
-			if( na_core_utils_slist_count( subitems, ( const gchar * ) ip->data ) == 0 ){
-				g_debug( "%s: deleting (removed) profile %s", thisfn, ( const gchar * ) ip->data );
-				nadp_desktop_file_remove_profile( NADP_DESKTOP_FILE( writer_data ), ( const gchar * ) ip->data );
-			}
-		}
+	subitems = na_object_get_items_slist( item );
+	na_core_utils_slist_dump( thisfn, subitems );
+
+	nadp_desktop_file_set_string_list(
+			ndp,
+			NADP_GROUP_DESKTOP,
+			NA_IS_OBJECT_ACTION( item ) ? NADP_KEY_PROFILES : NADP_KEY_ITEMS_LIST,
+			subitems );
+
+	profile_groups = nadp_desktop_file_get_profiles( ndp );
+	na_core_utils_slist_dump( thisfn, profile_groups );
 
-		na_core_utils_slist_free( profile_groups );
-		na_core_utils_slist_free( subitems );
+	for( ip = profile_groups ; ip ; ip = ip->next ){
+		if( na_core_utils_slist_count( subitems, ( const gchar * ) ip->data ) == 0 ){
+			g_debug( "%s: deleting (removed) profile %s", thisfn, ( const gchar * ) ip->data );
+			nadp_desktop_file_remove_profile( ndp, ( const gchar * ) ip->data );
+		}
 	}
 
-	return( NA_IIO_PROVIDER_CODE_OK );
+	na_core_utils_slist_free( profile_groups );
+	na_core_utils_slist_free( subitems );
 }
 
 static ExportFormatFn *
diff --git a/src/io-gconf/nagp-writer.c b/src/io-gconf/nagp-writer.c
index 79a6c14..eb1c61b 100644
--- a/src/io-gconf/nagp-writer.c
+++ b/src/io-gconf/nagp-writer.c
@@ -46,6 +46,9 @@
 #include "nagp-writer.h"
 #include "nagp-keys.h"
 
+static void write_start_write_type( NagpGConfProvider *provider, NAObjectItem *item );
+static void write_start_write_version( NagpGConfProvider *provider, NAObjectItem *item );
+
 /*
  * API function: should only be called through NAIIOProvider interface
  */
@@ -223,28 +226,46 @@ guint
 nagp_writer_write_start( const NAIFactoryProvider *writer, void *writer_data,
 							const NAIFactoryObject *object, GSList **messages  )
 {
-	GConfClient *gconf;
-	gchar *id;
-	gchar *path;
-	guint iversion;
-
 	if( NA_IS_OBJECT_ITEM( object )){
-		id = na_object_get_id( object );
-		gconf = NAGP_GCONF_PROVIDER( writer )->private->gconf;
+		write_start_write_type( NAGP_GCONF_PROVIDER( writer ), NA_OBJECT_ITEM( object ));
+		write_start_write_version( NAGP_GCONF_PROVIDER( writer ), NA_OBJECT_ITEM( object ));
+	}
 
-		path = g_strdup_printf( "%s/%s/%s", NAGP_CONFIGURATIONS_PATH, id, NAGP_ENTRY_TYPE );
-		na_gconf_utils_write_string( gconf, path, NA_IS_OBJECT_ACTION( object ) ? NAGP_VALUE_TYPE_ACTION : NAGP_VALUE_TYPE_MENU, NULL );
-		g_free( path );
+	return( NA_IIO_PROVIDER_CODE_OK );
+}
 
-		path = g_strdup_printf( "%s/%s/%s", NAGP_CONFIGURATIONS_PATH, id, NAGP_ENTRY_IVERSION );
-		iversion = na_object_get_iversion( object );
-		na_gconf_utils_write_int( gconf, path, iversion, NULL );
-		g_free( path );
+static void
+write_start_write_type( NagpGConfProvider *provider, NAObjectItem *item )
+{
+	gchar *id, *path;
 
-		g_free( id );
-	}
+	id = na_object_get_id( item );
+	path = g_strdup_printf( "%s/%s/%s", NAGP_CONFIGURATIONS_PATH, id, NAGP_ENTRY_TYPE );
 
-	return( NA_IIO_PROVIDER_CODE_OK );
+	na_gconf_utils_write_string(
+			provider->private->gconf,
+			path,
+			NA_IS_OBJECT_ACTION( item ) ? NAGP_VALUE_TYPE_ACTION : NAGP_VALUE_TYPE_MENU,
+			NULL );
+
+	g_free( path );
+	g_free( id );
+}
+
+static void
+write_start_write_version( NagpGConfProvider *provider, NAObjectItem *item )
+{
+	gchar *id, *path;
+	guint iversion;
+
+	id = na_object_get_id( item );
+	path = g_strdup_printf( "%s/%s/%s", NAGP_CONFIGURATIONS_PATH, id, NAGP_ENTRY_IVERSION );
+
+	iversion = na_object_get_iversion( item );
+	na_gconf_utils_write_int( provider->private->gconf, path, iversion, NULL );
+
+	g_free( path );
+	g_free( id );
 }
 
 guint
diff --git a/src/io-xml/naxml-writer.c b/src/io-xml/naxml-writer.c
index 56c8391..7b8469f 100644
--- a/src/io-xml/naxml-writer.c
+++ b/src/io-xml/naxml-writer.c
@@ -99,6 +99,9 @@ static void            instance_init( GTypeInstance *instance, gpointer klass );
 static void            instance_dispose( GObject *object );
 static void            instance_finalize( GObject *object );
 
+static void            write_start_write_type( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups );
+static void            write_start_write_version( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups );
+
 static void            write_data_schema_v1( NAXMLWriter *writer, const NAObjectId *object, const NADataBoxed *boxed, const NADataDef *def );
 static void            write_data_schema_v1_element( NAXMLWriter *writer, const NADataDef *def );
 static void            write_type_schema_v1( NAXMLWriter *writer, const NAObjectItem *object, const NADataDef *def, const gchar *value );
@@ -114,7 +117,6 @@ static xmlDocPtr       build_xml_doc( NAXMLWriter *writer );
 static ExportFormatFn *find_export_format_fn( GQuark format );
 static gchar          *get_output_fname( const NAObjectItem *item, const gchar *folder, GQuark format );
 static void            output_xml_to_file( const gchar *xml, const gchar *filename, GSList **msg );
-static void            write_type( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups );
 static guint           writer_to_buffer( NAXMLWriter *writer );
 
 static ExportFormatFn st_export_format_fn[] = {
@@ -400,12 +402,48 @@ naxml_writer_write_start( const NAIFactoryProvider *provider, void *writer_data,
 		}
 
 		groups = na_ifactory_object_get_data_groups( object );
-		write_type( writer, NA_OBJECT_ITEM( object ), groups );
+		write_start_write_type( writer, NA_OBJECT_ITEM( object ), groups );
+		write_start_write_version( writer, NA_OBJECT_ITEM( object ), groups );
 	}
 
 	return( NA_IIO_PROVIDER_CODE_OK );
 }
 
+/* at end of write_start (list_node already created)
+ * explicitly write the 'Type' node
+ */
+static void
+write_start_write_type( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups )
+{
+	const NADataDef *def;
+	const gchar *svalue;
+
+	writer->private->schema_node = NULL;
+	writer->private->locale_node = NULL;
+	def = na_data_def_get_data_def( groups, NA_FACTORY_OBJECT_ITEM_GROUP, NAFO_DATA_TYPE );
+	svalue = NA_IS_OBJECT_ACTION( object ) ? NAGP_VALUE_TYPE_ACTION : NAGP_VALUE_TYPE_MENU;
+
+	( *writer->private->fn_str->write_type_fn )( writer, object, def, svalue );
+}
+
+static void
+write_start_write_version( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups )
+{
+	const NADataDef *def;
+	guint iversion;
+	gchar *svalue;
+
+	writer->private->schema_node = NULL;
+	writer->private->locale_node = NULL;
+	def = na_data_def_get_data_def( groups, NA_FACTORY_OBJECT_ITEM_GROUP, NAFO_DATA_IVERSION );
+	iversion = na_object_get_iversion( object );
+	svalue = g_strdup_printf( "%d", iversion );
+
+	( *writer->private->fn_str->write_type_fn )( writer, object, def, svalue );
+
+	g_free( svalue );
+}
+
 guint
 naxml_writer_write_data( const NAIFactoryProvider *provider, void *writer_data, const NAIFactoryObject *object, const NADataBoxed *boxed, GSList **messages )
 {
@@ -810,23 +848,6 @@ output_xml_to_file( const gchar *xml, const gchar *filename, GSList **msg )
 	g_object_unref( file );
 }
 
-/* at end of write_start (list_node already created)
- * explicitly write the 'Type' node
- */
-static void
-write_type( NAXMLWriter *writer, NAObjectItem *object, const NADataGroup *groups )
-{
-	const NADataDef *def;
-	const gchar *svalue;
-
-	writer->private->schema_node = NULL;
-	writer->private->locale_node = NULL;
-	def = na_data_def_get_data_def( groups, NA_FACTORY_OBJECT_ITEM_GROUP, NAFO_DATA_TYPE );
-	svalue = NA_IS_OBJECT_ACTION( object ) ? NAGP_VALUE_TYPE_ACTION : NAGP_VALUE_TYPE_MENU;
-
-	( *writer->private->fn_str->write_type_fn )( writer, object, def, svalue );
-}
-
 static guint
 writer_to_buffer( NAXMLWriter *writer )
 {



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