[nautilus-actions] Rewrite action input/output



commit aad6ff5aa1b4b1145eded737960526a7c4d65ad9
Author: Pierre Wieser <pwieser trychlos org>
Date:   Sat Mar 6 15:18:08 2010 +0100

    Rewrite action input/output

 ChangeLog                           |   15 +++
 src/core/na-object-action-factory.c |    4 +-
 src/core/na-object-action.c         |  174 +++++++++++++++++++++++------------
 src/io-gconf/nagp-reader.c          |   43 ++-------
 src/io-gconf/nagp-writer.c          |   98 ++++----------------
 5 files changed, 164 insertions(+), 170 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index a799113..81036ef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 2009-03-05 Pierre Wieser <pwieser trychlos org>
 
+	* src/core/na-object-action-factory.c:
+	data_def_action_v1 is now available from outside.
+
+	* src/core/na-object-action.c
+	(ifactory_object_read_done, ifactory_object_write_done):
+	Rewrite input/output functions.
+
+	* src/io-gconf/nagp-reader.c
+	(nagp_reader_read_data): Simplify the code.
+
+	* src/io-gconf/nagp-writer.c
+	(nagp_writer_write_start): No more write type as this is a normal data.
+	(nagp_writer_write_data): Rewrite profile writing.
+	(nagp_writer_write_done): Profile writing is now driven by NAObjectAction.
+
 	* src/nact/nact-tree-model-dnd.c: Cleanup the code.
 
 2009-03-05 Pierre Wieser <pwieser trychlos org>
diff --git a/src/core/na-object-action-factory.c b/src/core/na-object-action-factory.c
index 24aa605..f91bd28 100644
--- a/src/core/na-object-action-factory.c
+++ b/src/core/na-object-action-factory.c
@@ -162,7 +162,7 @@ static NADataDef data_def_action [] = {
  * these are obsoleted since 1.9 (which was a non-official version)
  * readable but non writable, no default
  */
-static NADataDef data_def_obsoleted_action [] = {
+NADataDef data_def_action_v1 [] = {
 
 	{ NAFO_DATA_PATH,
 				TRUE,
@@ -297,7 +297,7 @@ NADataGroup action_data_groups [] = {
 	{ NA_FACTORY_OBJECT_ID_GROUP,         data_def_id },
 	{ NA_FACTORY_OBJECT_ITEM_GROUP,       data_def_item },
 	{ NA_FACTORY_OBJECT_ACTION_GROUP,     data_def_action },
-	{ NA_FACTORY_OBSOLETED_ACTION_GROUP,  data_def_obsoleted_action },
+	{ NA_FACTORY_OBSOLETED_ACTION_GROUP,  data_def_action_v1 },
 	{ NA_FACTORY_OBJECT_CONDITIONS_GROUP, NULL },
 	{ NULL }
 };
diff --git a/src/core/na-object-action.c b/src/core/na-object-action.c
index c2ba957..77dc25b 100644
--- a/src/core/na-object-action.c
+++ b/src/core/na-object-action.c
@@ -34,6 +34,7 @@
 
 #include <glib/gi18n.h>
 #include <string.h>
+#include <stdlib.h>
 
 #include <api/na-iio-provider.h>
 #include <api/na-object-api.h>
@@ -53,18 +54,11 @@ struct NAObjectActionPrivate {
 	gboolean dispose_has_run;
 };
 
-/* while iterating when searching for obsoleted boxed
- */
-typedef struct {
-	NAObjectProfile *profile;
-	GList           *moved;
-}
-	IterForObsoletedParms;
-
 /* i18n: default label for a new action */
 #define NEW_NAUTILUS_ACTION				N_( "New Nautilus action" )
 
-extern NADataGroup action_data_groups [];		/* defined in na-item-action-factory.c */
+extern NADataGroup action_data_groups [];		/* defined in na-object-action-factory.c */
+extern NADataDef   data_def_action_v1 [];		/* defined in na-object-action-factory.c */
 
 static NAObjectItemClass *st_parent_class = NULL;
 
@@ -89,7 +83,8 @@ static void         ifactory_object_read_done( NAIFactoryObject *instance, const
 static guint        ifactory_object_write_start( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages );
 static guint        ifactory_object_write_done( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages );
 
-static gboolean     check_for_obsoleted_iter( const NAIFactoryObject *object, NADataBoxed *boxed, IterForObsoletedParms *parms );
+static void         convert_pre_v2_action( NAIFactoryObject *instance );
+static void         deals_with_toolbar_label( NAIFactoryObject *instance );
 
 static gboolean     object_object_is_valid( const NAObjectAction *action );
 static gboolean     is_valid_label( const NAObjectAction *action );
@@ -324,48 +319,28 @@ ifactory_object_read_start( NAIFactoryObject *instance, const NAIFactoryProvider
 {
 }
 
+/*
+ * at this time, we don't yet have readen the profiles as this will be
+ * done in ifactory_provider_read_done - we so just be able to deal with
+ * action-specific properties (not check for profiles consistency)
+ */
 static void
 ifactory_object_read_done( NAIFactoryObject *instance, const NAIFactoryProvider *reader, void *reader_data, GSList **messages )
 {
-	IterForObsoletedParms parms;
-	GList *ibox;
-	gchar *label;
-
 	g_debug( "na_object_action_ifactory_object_read_done: instance=%p", ( void * ) instance );
 
-	/* do we have a pre-v2 action ?
-	 *  i.e. an action without profile, with some data in its body
-	 *  -> do we have read some obsoleted data which are now in the profile
+	/* may attach a new profile if we detect a pre-v2 action
 	 */
-	parms.profile = na_object_profile_new();
-	parms.moved = NULL;
-
-	na_factory_object_iter_on_boxed( instance, ( NAFactoryObjectIterBoxedFn ) check_for_obsoleted_iter, &parms );
-
-	if( parms.moved ){
-		na_object_set_id( parms.profile, "profile-pre-v2" );
-		na_object_set_label( parms.profile, _( "Profile automatically created from pre-v2 action" ));
-		na_object_attach_profile( instance, parms.profile );
-
-		for( ibox = parms.moved ; ibox ; ibox = ibox->next ){
-			na_factory_object_move_boxed( NA_IFACTORY_OBJECT( parms.profile ), instance, NA_DATA_BOXED( ibox->data ));
-		}
-
-	} else {
-		g_object_unref( parms.profile );
-	}
+	convert_pre_v2_action( instance );
 
-	/* set action defaults
+	/* deals with obsoleted data, i.e. data which may have been written in the past
+	 * but are no long written by now
 	 */
-	na_factory_object_set_defaults( instance );
+	deals_with_toolbar_label( instance );
 
-	/* if toolbar-same-label is true, then ensure that this is actually true
+	/* last, set other action defaults
 	 */
-	if( na_object_is_toolbar_same_label( instance )){
-		label = na_object_get_label( instance );
-		na_object_set_toolbar_label( instance, label );
-		g_free( label );
-	}
+	na_factory_object_set_defaults( instance );
 }
 
 static guint
@@ -379,30 +354,115 @@ ifactory_object_write_start( NAIFactoryObject *instance, const NAIFactoryProvide
 static guint
 ifactory_object_write_done( NAIFactoryObject *instance, const NAIFactoryProvider *writer, void *writer_data, GSList **messages )
 {
-	return( NA_IIO_PROVIDER_CODE_OK );
+	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 );
+
+			} else {
+				g_warning( "%s: profile not found: %s", thisfn, ( const gchar * ) ic->data );
+			}
+		}
+	}
+
+	return( code );
 }
 
-static gboolean
-check_for_obsoleted_iter( const NAIFactoryObject *object, NADataBoxed *boxed, IterForObsoletedParms *parms )
+/*
+ * do we have a pre-v2 action ?
+ *  it may be identified by an version = "1.x"
+ *  or by any data found in data_def_action_v1 (defined in na-object-action-factory.c)
+ *  -> move obsoleted data to a new profile
+ */
+static void
+convert_pre_v2_action( NAIFactoryObject *instance )
 {
-	NADataDef *action_def = na_data_boxed_get_data_def( boxed );
+	gboolean is_pre_v2;
+	GList *to_move;
+	NADataDef *def;
+	NADataBoxed *boxed;
+	gchar *version;
+	GList *ibox;
+	NAObjectProfile *profile;
 
-	/* if property is obsoleted in an action
-	 */
-	if( action_def->readable && !action_def->writable ){
-		NADataDef *profile_def = na_factory_object_get_data_def( NA_IFACTORY_OBJECT( parms->profile ), action_def->name );
+	is_pre_v2 = FALSE;
+	to_move = NULL;
 
-		/* but the property exists in the profile
-		 */
-		if( profile_def && profile_def->readable && profile_def->writable){
-			g_debug( "na_object_action_check_for_obsoleted_iter: " \
+	def = data_def_action_v1;
+	while( def->name ){
+		boxed = na_ifactory_object_get_data_boxed( instance , def->name );
+		if( boxed ){
+			g_debug( "na_object_action_convert_pre_v2_action: " \
 					 "boxed=%p (%s) marked to be moved from action body to profile",
-							 ( void * ) boxed, action_def->name );
-			parms->moved =g_list_prepend( parms->moved, boxed );
+							 ( void * ) boxed, def->name );
+			to_move =g_list_prepend( to_move, boxed );
 		}
+		def++;
+	}
+
+	if( to_move ){
+		is_pre_v2 = TRUE;
+
+	} else {
+		version = na_object_get_version( instance );
+		if( version && strlen( version ) && atoi( version ) < 2 ){
+			is_pre_v2 = TRUE;
+		}
+		g_free( version );
+	}
+
+	if( is_pre_v2 ){
+		profile = na_object_profile_new();
+		na_object_set_id( profile, "profile-pre-v2" );
+		na_object_set_label( profile, _( "Profile automatically created from pre-v2 action" ));
+		na_object_attach_profile( instance, profile );
+
+		if( to_move ){
+			for( ibox = to_move ; ibox ; ibox = ibox->next ){
+				na_factory_object_move_boxed(
+						NA_IFACTORY_OBJECT( profile ), instance, NA_DATA_BOXED( ibox->data ));
+			}
+		}
+
+		na_factory_object_set_defaults( NA_IFACTORY_OBJECT( profile ));
+	}
+}
+
+/*
+ * if toolbar-same-label is true, then ensure that this is actually true
+ */
+static void
+deals_with_toolbar_label( NAIFactoryObject *instance )
+{
+	gchar *toolbar_label;
+	gchar *action_label;
+	gboolean same_label;
+
+	toolbar_label = na_object_get_toolbar_label( instance );
+	action_label = na_object_get_label( instance );
+
+	if( !toolbar_label || !g_utf8_strlen( toolbar_label, -1 )){
+		na_object_set_toolbar_label( instance, action_label );
+		na_object_set_toolbar_same_label( instance, TRUE );
+
+	} else {
+		same_label = ( g_utf8_collate( action_label, toolbar_label ) == 0 );
+		na_object_set_toolbar_same_label( instance, same_label );
 	}
 
-	return( FALSE );
+	g_free( action_label );
+	g_free( toolbar_label );
 }
 
 static gboolean
diff --git a/src/io-gconf/nagp-reader.c b/src/io-gconf/nagp-reader.c
index 22a849c..02c56da 100644
--- a/src/io-gconf/nagp-reader.c
+++ b/src/io-gconf/nagp-reader.c
@@ -54,12 +54,10 @@ typedef struct {
 
 static NAObjectItem *read_item( NagpGConfProvider *provider, const gchar *path, GSList **messages );
 
-static NADataBoxed  *read_data_item_properties( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *reader_data, const NADataDef *def );
-static NADataBoxed  *read_data_profile_properties( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *reader_data, const NADataDef *def );
-static void          read_done_item( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *data, GSList **messages );
-static void          read_done_action( const NAIFactoryProvider *provider, NAObjectAction *action, ReaderData *data, GSList **messages );
+static void          read_done_item_is_writable( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *data, GSList **messages );
+static void          read_done_action_load_profiles_from_list( const NAIFactoryProvider *provider, NAObjectAction *action, ReaderData *data, GSList **messages );
 static void          read_done_action_load_profile( const NAIFactoryProvider *provider, ReaderData *data, const gchar *path, GSList **messages );
-static void          read_done_profile( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *data, GSList **messages );
+static void          read_done_profile_attach_profile( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *data, GSList **messages );
 
 static NADataBoxed  *get_boxed_from_path( const NagpGConfProvider *provider, const gchar *path, ReaderData *reader_data, const NADataDef *def );
 static gboolean      is_key_writable( NagpGConfProvider *gconf, const gchar *key );
@@ -186,31 +184,12 @@ nagp_reader_read_data( const NAIFactoryProvider *provider, void *reader_data, co
 		return( NULL );
 	}
 
-	boxed = NULL;
-
-	if( NA_IS_OBJECT_ITEM( object )){
-		boxed = read_data_item_properties( provider, NA_OBJECT_ITEM( object ), ( ReaderData * ) reader_data, def );
-	}
-
-	if( NA_IS_OBJECT_PROFILE( object )){
-		boxed = read_data_profile_properties( provider, NA_OBJECT_PROFILE( object ), ( ReaderData * ) reader_data, def );
-	}
+	boxed = get_boxed_from_path(
+			NAGP_GCONF_PROVIDER( provider ), (( ReaderData * ) reader_data )->path, reader_data, def );
 
 	return( boxed );
 }
 
-static NADataBoxed *
-read_data_item_properties( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *reader_data, const NADataDef *def )
-{
-	return( get_boxed_from_path( NAGP_GCONF_PROVIDER( provider ), reader_data->path, reader_data, def ));
-}
-
-static NADataBoxed *
-read_data_profile_properties( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *reader_data, const NADataDef *def )
-{
-	return( get_boxed_from_path( NAGP_GCONF_PROVIDER( provider ), reader_data->path, reader_data, def ));
-}
-
 void
 nagp_reader_read_done( const NAIFactoryProvider *provider, void *reader_data, const NAIFactoryObject *object, GSList **messages  )
 {
@@ -227,22 +206,22 @@ nagp_reader_read_done( const NAIFactoryProvider *provider, void *reader_data, co
 			( void * ) messages );
 
 	if( NA_IS_OBJECT_ITEM( object )){
-		read_done_item( provider, NA_OBJECT_ITEM( object ), ( ReaderData * ) reader_data, messages );
+		read_done_item_is_writable( provider, NA_OBJECT_ITEM( object ), ( ReaderData * ) reader_data, messages );
 	}
 
 	if( NA_IS_OBJECT_ACTION( object )){
-		read_done_action( provider, NA_OBJECT_ACTION( object ), ( ReaderData * ) reader_data, messages );
+		read_done_action_load_profiles_from_list( provider, NA_OBJECT_ACTION( object ), ( ReaderData * ) reader_data, messages );
 	}
 
 	if( NA_IS_OBJECT_PROFILE( object )){
-		read_done_profile( provider, NA_OBJECT_PROFILE( object ), ( ReaderData * ) reader_data, messages );
+		read_done_profile_attach_profile( provider, NA_OBJECT_PROFILE( object ), ( ReaderData * ) reader_data, messages );
 	}
 
 	g_debug( "quitting nagp_read_done for %s at %p", G_OBJECT_TYPE_NAME( object ), ( void * ) object );
 }
 
 static void
-read_done_item( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *data, GSList **messages )
+read_done_item_is_writable( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderData *data, GSList **messages )
 {
 	GSList *entries, *ie;
 	gboolean writable;
@@ -265,7 +244,7 @@ read_done_item( const NAIFactoryProvider *provider, NAObjectItem *item, ReaderDa
 }
 
 static void
-read_done_action( const NAIFactoryProvider *provider, NAObjectAction *action, ReaderData *data, GSList **messages )
+read_done_action_load_profiles_from_list( const NAIFactoryProvider *provider, NAObjectAction *action, ReaderData *data, GSList **messages )
 {
 	GSList *order;
 	GSList *list_profiles;
@@ -321,7 +300,7 @@ read_done_action_load_profile( const NAIFactoryProvider *provider, ReaderData *d
 }
 
 static void
-read_done_profile( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *data, GSList **messages )
+read_done_profile_attach_profile( const NAIFactoryProvider *provider, NAObjectProfile *profile, ReaderData *data, GSList **messages )
 {
 	na_object_attach_profile( data->parent, profile );
 }
diff --git a/src/io-gconf/nagp-writer.c b/src/io-gconf/nagp-writer.c
index 42070c3..80a5305 100644
--- a/src/io-gconf/nagp-writer.c
+++ b/src/io-gconf/nagp-writer.c
@@ -46,11 +46,6 @@
 #include "nagp-writer.h"
 #include "nagp-keys.h"
 
-typedef struct {
-	gchar *parent_id;
-}
-	WriterData;
-
 /*
  * API function: should only be called through NAIIOProvider interface
  */
@@ -113,7 +108,6 @@ nagp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem
 	static const gchar *thisfn = "nagp_gconf_provider_iio_provider_write_item";
 	NagpGConfProvider *self;
 	guint ret;
-	WriterData *data;
 
 	g_debug( "%s: provider=%p (%s), item=%p (%s), messages=%p",
 			thisfn,
@@ -136,12 +130,7 @@ nagp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem
 	ret = nagp_iio_provider_delete_item( provider, item, messages );
 
 	if( ret == NA_IIO_PROVIDER_CODE_OK ){
-
-		data = g_new0( WriterData, 1 );
-
-		na_ifactory_provider_write_item( NA_IFACTORY_PROVIDER( provider ), data, NA_IFACTORY_OBJECT( item ), messages );
-
-		g_free( data );
+		na_ifactory_provider_write_item( NA_IFACTORY_PROVIDER( provider ), NULL, NA_IFACTORY_OBJECT( item ), messages );
 	}
 
 	gconf_client_suggest_sync( self->private->gconf, NULL );
@@ -218,38 +207,7 @@ guint
 nagp_writer_write_start( const NAIFactoryProvider *writer, void *writer_data,
 							const NAIFactoryObject *object, GSList **messages  )
 {
-	guint code;
-	gchar *id, *parent_path, *path;
-	gchar *msg;
-
-	code = NA_IIO_PROVIDER_CODE_OK;
-
-	if( NA_IS_OBJECT_ITEM( object )){
-		g_return_val_if_fail(
-				(( WriterData * ) writer_data )->parent_id == NULL,
-				NA_IIO_PROVIDER_CODE_PROGRAM_ERROR );
-
-		id = na_object_get_id( object );
-		parent_path = gconf_concat_dir_and_key( NAGP_CONFIGURATIONS_PATH, id );
-		path = gconf_concat_dir_and_key( parent_path, NAGP_ENTRY_TYPE );
-
-		msg = NULL;
-		na_gconf_utils_write_string(
-				NAGP_GCONF_PROVIDER( writer )->private->gconf,
-				path,
-				NA_IS_OBJECT_ACTION( object ) ? NAGP_VALUE_TYPE_ACTION : NAGP_VALUE_TYPE_MENU,
-				&msg );
-		if( msg ){
-			*messages = g_slist_append( *messages, msg );
-			code = NA_IIO_PROVIDER_CODE_WRITE_ERROR;
-		}
-
-		g_free( path );
-		g_free( parent_path );
-		g_free( id );
-	}
-
-	return( code );
+	return( NA_IIO_PROVIDER_CODE_OK );
 }
 
 guint
@@ -260,8 +218,8 @@ nagp_writer_write_data( const NAIFactoryProvider *provider, void *writer_data,
 	static const gchar *thisfn = "nagp_writer_write_data";
 	guint code;
 	NADataDef *def;
-	gchar *id;
-	gchar *parent_path, *dir_path, *path;
+	gchar *this_id;
+	gchar *this_path, *path;
 	gchar *msg;
 	gchar *str_value;
 	gboolean bool_value;
@@ -275,16 +233,21 @@ nagp_writer_write_data( const NAIFactoryProvider *provider, void *writer_data,
 	code = NA_IIO_PROVIDER_CODE_OK;
 	def = na_data_boxed_get_data_def( boxed );
 
-	id = na_object_get_id( object );
+	if( NA_IS_OBJECT_PROFILE( object )){
+		NAObjectItem *parent = NA_OBJECT_ITEM( na_object_get_parent( object ));
+		gchar *parent_id = na_object_get_id( parent );
+		gchar *id = na_object_get_id( object );
+		this_id = gconf_concat_dir_and_key( parent_id, id );
+		g_free( id );
+		g_free( parent_id );
 
-	parent_path = gconf_concat_dir_and_key( NAGP_CONFIGURATIONS_PATH,
-			(( WriterData * ) writer_data )->parent_id ?
-					(( WriterData * ) writer_data )->parent_id : id );
+	} else {
+		this_id = na_object_get_id( object );
 
-	dir_path = (( WriterData * ) writer_data )->parent_id ?
-					gconf_concat_dir_and_key( parent_path, id ) : g_strdup( parent_path );
+	}
 
-	path = gconf_concat_dir_and_key( dir_path, def->gconf_entry );
+	this_path = gconf_concat_dir_and_key( NAGP_CONFIGURATIONS_PATH, this_id );
+	path = gconf_concat_dir_and_key( this_path, def->gconf_entry );
 
 	gconf = NAGP_GCONF_PROVIDER( provider )->private->gconf;
 
@@ -353,8 +316,8 @@ nagp_writer_write_data( const NAIFactoryProvider *provider, void *writer_data,
 
 	g_free( msg );
 	g_free( path );
-	g_free( parent_path );
-	g_free( id );
+	g_free( this_path );
+	g_free( this_id );
 
 	return( code );
 }
@@ -364,28 +327,5 @@ nagp_writer_write_done( const NAIFactoryProvider *writer, void *writer_data,
 									const NAIFactoryObject *object,
 									GSList **messages  )
 {
-	guint code;
-	GSList *children_slist, *ic;
-	WriterData *data;
-	NAObjectProfile *profile;
-
-	code = NA_IIO_PROVIDER_CODE_OK;
-
-	if( NA_IS_OBJECT_ACTION( object )){
-		children_slist = na_object_get_items_slist( object );
-
-		for( ic = children_slist ; ic && code == NA_IIO_PROVIDER_CODE_OK ; ic = ic->next ){
-			data = g_new0( WriterData, 1 );
-			data->parent_id = na_object_get_id( object );
-
-			profile = NA_OBJECT_PROFILE( na_object_get_item( object, ic->data ));
-
-			code = na_ifactory_provider_write_item( writer, data, NA_IFACTORY_OBJECT( profile ), messages );
-
-			g_free( data->parent_id );
-			g_free( data );
-		}
-	}
-
-	return( code );
+	return( NA_IIO_PROVIDER_CODE_OK );
 }



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