[nautilus-actions] Write data into desktop files



commit 92d9f19d53726da0dc100b0b199ff9329e12904a
Author: Pierre Wieser <pwieser trychlos org>
Date:   Sun Mar 28 12:19:24 2010 +0200

    Write data into desktop files

 ChangeLog                              |   18 ++++++
 src/core/na-object-item-factory.c      |   13 +++--
 src/io-desktop/nadp-desktop-provider.c |    4 +-
 src/io-desktop/nadp-keys.c             |   42 --------------
 src/io-desktop/nadp-keys.h             |    9 ---
 src/io-desktop/nadp-reader.c           |   37 ++++++++----
 src/io-desktop/nadp-writer.c           |   98 ++++++++++++++++++++++++++++++++
 src/io-desktop/nadp-writer.h           |   12 +++-
 8 files changed, 159 insertions(+), 74 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index da9fbcb..01f0395 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -131,6 +131,24 @@
 	* src/core/na-factory-object.c (set_defaults_iter):
 	Only set a default value when the box has not been yet allocated.
 
+	* src/core/na-object-item-factory.c:
+	NAFO_DATA_TYPE is no more automatically readen nor written.
+
+	* src/io-desktop/nadp-desktop-provider.c (ifactory_provider_iface_init):
+	Implement data writing.
+
+	* src/io-desktop/nadp-keys.c:
+	* src/io-desktop/nadp-keys.h (nadp_keys_get_group_and_key):
+	Removed function.
+
+	* src/io-desktop/nadp-reader.c
+	(item_from_desktop_path): Test default value first.
+	(nadp_reader_ifactory_provider_read_data): Use new desktop_entry field.
+
+	* src/io-desktop/nadp-writer.c:
+	* src/io-desktop/nadp-writer.h (nadp_writer_ifactory_provider_write_start,
+	nadp_writer_ifactory_provider_write_data): New functions.
+
 	* src/io-gconf/nagp-writer.c (nagp_writer_write_start):
 	Explicitely write item type.
 
diff --git a/src/core/na-object-item-factory.c b/src/core/na-object-item-factory.c
index ea27d53..3f1581d 100644
--- a/src/core/na-object-item-factory.c
+++ b/src/core/na-object-item-factory.c
@@ -42,12 +42,15 @@ NADataDef data_def_item [] = {
 
 	/* this data is marked as non readable as it has to be readen specifically
 	 * in order to be able to create the corresponding NAObjectItem-derived object
-	 * it is nonetheless marked writable
+	 * it is not writable as different I/O providers have different values for it
+	 * it is not instantiated
+	 * it is just left here to be able to define the corresponding GConf schema
+	 * and to export it as XML
 	 */
 	{ NAFO_DATA_TYPE,
 				FALSE,
-				TRUE,
-				TRUE,
+				FALSE,
+				FALSE,
 				N_( "Type of the item" ),
 				N_( "Defines if the item is an action or a menu. Possible values are :\n" \
 					"- 'Action',\n" \
@@ -55,12 +58,12 @@ NADataDef data_def_item [] = {
 					"The value is case sensitive and must not be localized." ),
 				NAFD_TYPE_STRING,
 				"Action",
-				TRUE,
+				FALSE,
 				FALSE,
 				FALSE,
 				FALSE,
 				"type",
-				"Type",
+				NULL,
 				0,
 				NULL,
 				0,
diff --git a/src/io-desktop/nadp-desktop-provider.c b/src/io-desktop/nadp-desktop-provider.c
index d1e7faa..64a791a 100644
--- a/src/io-desktop/nadp-desktop-provider.c
+++ b/src/io-desktop/nadp-desktop-provider.c
@@ -226,8 +226,8 @@ ifactory_provider_iface_init( NAIFactoryProviderInterface *iface )
 	iface->read_start = NULL;
 	iface->read_data = nadp_reader_ifactory_provider_read_data;
 	iface->read_done = nadp_reader_ifactory_provider_read_done;
-	iface->write_start = NULL;
-	iface->write_data = NULL;
+	iface->write_start = nadp_writer_ifactory_provider_write_start;
+	iface->write_data = nadp_writer_ifactory_provider_write_data;
 	iface->write_done = NULL;
 }
 
diff --git a/src/io-desktop/nadp-keys.c b/src/io-desktop/nadp-keys.c
index 965dff2..7f9a6d6 100644
--- a/src/io-desktop/nadp-keys.c
+++ b/src/io-desktop/nadp-keys.c
@@ -32,46 +32,4 @@
 #include <config.h>
 #endif
 
-#include <string.h>
-
-#include <api/na-ifactory-object-data.h>
-
 #include "nadp-keys.h"
-
-static NadpIdKey id_key [] = {
-	{ NAFO_DATA_LABEL,   NADP_GROUP_DESKTOP, NADP_KEY_NAME },
-	{ NAFO_DATA_TOOLTIP, NADP_GROUP_DESKTOP, NADP_KEY_TOOLTIP },
-	{ NAFO_DATA_ICON,    NADP_GROUP_DESKTOP, NADP_KEY_ICON },
-	{ NULL }
-};
-
-/**
- * nadp_keys_get_group_and_key:
- * @iddef:
- *
- * Set: the group and the key to be used for this @iddef.
- *
- * Returns: %TRUE if the data has been found, %FALSE else.
- */
-gboolean
-nadp_keys_get_group_and_key( const NADataDef *def, gchar **group, gchar **key )
-{
-	gboolean found;
-	int i;
-
-	found = FALSE;
-	*group = NULL;
-	*key = NULL;
-
-	for( i = 0 ; id_key[i].data_id && !found ; ++i ){
-
-		if( !strcmp( id_key[i].data_id, def->name )){
-
-			*group = g_strdup( id_key[i].group );
-			*key = g_strdup( id_key[i].key );
-			found = TRUE;
-		}
-	}
-
-	return( found );
-}
diff --git a/src/io-desktop/nadp-keys.h b/src/io-desktop/nadp-keys.h
index 8d4acfe..0cce79f 100644
--- a/src/io-desktop/nadp-keys.h
+++ b/src/io-desktop/nadp-keys.h
@@ -92,15 +92,6 @@ G_BEGIN_DECLS
 #define NADP_VALUE_CAPABILITY_EXECUTABLE			"Executable"
 #define NADP_VALUE_CAPABILITY_LOCAL					"Local"
 
-typedef struct {
-	gchar *data_id;
-	gchar *group;
-	gchar *key;
-}
-	NadpIdKey;
-
-gboolean nadp_keys_get_group_and_key( const NADataDef *iddef, gchar **group, gchar **key );
-
 G_END_DECLS
 
 #endif /* __NADP_KEYS_H__ */
diff --git a/src/io-desktop/nadp-reader.c b/src/io-desktop/nadp-reader.c
index 5f7fe45..0fc44fe 100644
--- a/src/io-desktop/nadp-reader.c
+++ b/src/io-desktop/nadp-reader.c
@@ -336,18 +336,18 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 	NADataBoxed *boxed;
 	gboolean found;
 	NadpReaderData *nrd;
-	gchar *group, *key;
+	gchar *group, *id;
 	gchar *msg;
 	gchar *str_value;
 	gboolean bool_value;
 	GSList *slist_value;
 	guint uint_value;
 
-	/*g_debug( "%s: reader=%p (%s), reader_data=%p, def=%p, messages=%p",
+	/*g_debug( "%s: reader=%p (%s), reader_data=%p, def=%p (%s), messages=%p",
 			thisfn,
 			( void * ) reader, G_OBJECT_TYPE_NAME( reader ),
 			( void * ) reader_data,
-			( void * ) def,
+			( void * ) def, def->name,
 			( void * ) messages );*/
 
 	g_return_val_if_fail( NA_IS_IFACTORY_PROVIDER( reader ), NULL );
@@ -361,12 +361,22 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 		nrd = ( NadpReaderData * ) reader_data;
 		g_return_val_if_fail( NADP_IS_DESKTOP_FILE( nrd->ndf ), NULL );
 
-		if( nadp_keys_get_group_and_key( def, &group, &key )){
+		if( def->desktop_entry ){
+
+			if( NA_IS_OBJECT_ITEM( object )){
+				group = g_strdup( NADP_GROUP_DESKTOP );
+
+			} else {
+				g_return_val_if_fail( NA_IS_OBJECT_PROFILE( object ), NULL );
+				id = na_object_get_id( object );
+				group = g_strdup_printf( "%s %s", NADP_GROUP_PROFILE, id );
+				g_free( id );
+			}
 
 			switch( def->type ){
 
 				case NAFD_TYPE_LOCALE_STRING:
-					str_value = nadp_desktop_file_get_locale_string( nrd->ndf, group, key, &found, def->default_value );
+					str_value = nadp_desktop_file_get_locale_string( nrd->ndf, group, def->desktop_entry, &found, def->default_value );
 					if( str_value && found ){
 						boxed = na_data_boxed_new( def );
 						na_data_boxed_set_from_void( boxed, str_value );
@@ -375,7 +385,7 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 					break;
 
 				case NAFD_TYPE_STRING:
-					str_value = nadp_desktop_file_get_string( nrd->ndf, group, key, &found, def->default_value );
+					str_value = nadp_desktop_file_get_string( nrd->ndf, group, def->desktop_entry, &found, def->default_value );
 					if( str_value && found ){
 						boxed = na_data_boxed_new( def );
 						na_data_boxed_set_from_void( boxed, str_value );
@@ -384,7 +394,8 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 					break;
 
 				case NAFD_TYPE_BOOLEAN:
-					bool_value = nadp_desktop_file_get_boolean( nrd->ndf, group, key, &found, na_core_utils_boolean_from_string( def->default_value ));
+					bool_value = nadp_desktop_file_get_boolean( nrd->ndf, group, def->desktop_entry, &found, na_core_utils_boolean_from_string( def->default_value ));
+					g_debug( "%s: name=%s, found=%s, value=%s", thisfn, def->name, found ? "True":"False", bool_value ? "True":"False" );
 					if( found ){
 						boxed = na_data_boxed_new( def );
 						na_data_boxed_set_from_void( boxed, GUINT_TO_POINTER( bool_value ));
@@ -392,7 +403,7 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 					break;
 
 				case NAFD_TYPE_STRING_LIST:
-					slist_value = nadp_desktop_file_get_string_list( nrd->ndf, group, key, &found, def->default_value );
+					slist_value = nadp_desktop_file_get_string_list( nrd->ndf, group, def->desktop_entry, &found, def->default_value );
 					if( slist_value && found ){
 						boxed = na_data_boxed_new( def );
 						na_data_boxed_set_from_void( boxed, slist_value );
@@ -401,7 +412,7 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 					break;
 
 				case NAFD_TYPE_UINT:
-					uint_value = nadp_desktop_file_get_uint( nrd->ndf, group, key, &found, atoi( def->default_value ));
+					uint_value = nadp_desktop_file_get_uint( nrd->ndf, group, def->desktop_entry, &found, atoi( def->default_value ));
 					if( found ){
 						boxed = na_data_boxed_new( def );
 						na_data_boxed_set_from_void( boxed, GUINT_TO_POINTER( uint_value ));
@@ -414,8 +425,6 @@ nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void
 					*messages = g_slist_append( *messages, msg );
 			}
 
-			/*g_debug( "%s: group=%s, key=%s", thisfn, group, key );*/
-			g_free( key );
 			g_free( group );
 		}
 	}
@@ -498,7 +507,6 @@ read_done_load_profiles( const NAIFactoryProvider *provider, NAObjectAction *act
 	 * this is mandatory for pre-2.29 actions which introduced order of profiles
 	 */
 	for( ip = list_profiles ; ip ; ip = ip->next ){
-		g_debug( "nadp_reader_read_done_load_profiles: loading profile=%s", ( gchar * ) ip->data );
 		read_done_action_load_profile( provider, reader_data, ( const gchar * ) ip->data, messages );
 	}
 }
@@ -506,8 +514,11 @@ read_done_load_profiles( const NAIFactoryProvider *provider, NAObjectAction *act
 static void
 read_done_action_load_profile( const NAIFactoryProvider *provider, NadpReaderData *reader_data, const gchar *profile_id, GSList **messages )
 {
-	NAObjectProfile *profile = na_object_profile_new();
+	NAObjectProfile *profile;
+
+	g_debug( "nadp_reader_read_done_action_load_profile: loading profile=%s", profile_id );
 
+	profile = na_object_profile_new();
 	na_object_set_id( profile, profile_id );
 
 	na_ifactory_provider_read_item(
diff --git a/src/io-desktop/nadp-writer.c b/src/io-desktop/nadp-writer.c
index af68faf..f5348e0 100644
--- a/src/io-desktop/nadp-writer.c
+++ b/src/io-desktop/nadp-writer.c
@@ -33,13 +33,16 @@
 #endif
 
 #include <errno.h>
+#include <string.h>
 
 #include <api/na-core-utils.h>
+#include <api/na-data-types.h>
 #include <api/na-object-api.h>
 #include <api/na-ifactory-provider.h>
 
 #include "nadp-desktop-file.h"
 #include "nadp-desktop-provider.h"
+#include "nadp-keys.h"
 #include "nadp-writer.h"
 #include "nadp-xdg-dirs.h"
 
@@ -308,3 +311,98 @@ nadp_writer_desktop_is_writable( const NAIIOProvider *provider, const NAObjectIt
 	return( writable );
 }
 #endif
+
+guint
+nadp_writer_ifactory_provider_write_start( const NAIFactoryProvider *provider, void *writer_data,
+							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 );
+	}
+
+	return( NA_IIO_PROVIDER_CODE_OK );
+}
+
+guint
+nadp_writer_ifactory_provider_write_data(
+				const NAIFactoryProvider *provider, void *writer_data, const NAIFactoryObject *object,
+				const NADataBoxed *boxed, GSList **messages )
+{
+	static const gchar *thisfn = "nadp_writer_ifactory_provider_write_data";
+	NadpDesktopFile *ndf;
+	guint code;
+	NADataDef *def;
+	gchar *profile_id;
+	gchar *group_name;
+	gchar *str_value;
+	gboolean bool_value;
+	GSList *slist_value;
+	guint uint_value;
+
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( writer_data ), NA_IIO_PROVIDER_CODE_PROGRAM_ERROR );
+	/*g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/
+
+	code = NA_IIO_PROVIDER_CODE_OK;
+	ndf = NADP_DESKTOP_FILE( writer_data );
+
+	if( na_data_boxed_is_set( boxed )){
+		def = na_data_boxed_get_data_def( boxed );
+
+		if( def->desktop_entry && strlen( def->desktop_entry )){
+
+			if( NA_IS_OBJECT_PROFILE( object )){
+				profile_id = na_object_get_id( object );
+				group_name = g_strdup_printf( "%s %s", NADP_GROUP_PROFILE, profile_id );
+				g_free( profile_id );
+
+			} else {
+				group_name = g_strdup( NADP_GROUP_DESKTOP );
+			}
+
+			switch( def->type ){
+
+				case NAFD_TYPE_STRING:
+					str_value = na_data_boxed_get_as_string( boxed );
+					nadp_desktop_file_set_string( ndf, group_name, def->desktop_entry, str_value );
+					g_free( str_value );
+					break;
+
+				case NAFD_TYPE_LOCALE_STRING:
+					str_value = na_data_boxed_get_as_string( boxed );
+					nadp_desktop_file_set_locale_string( ndf, group_name, def->desktop_entry, str_value );
+					g_free( str_value );
+					break;
+
+				case NAFD_TYPE_BOOLEAN:
+					bool_value = GPOINTER_TO_UINT( na_data_boxed_get_as_void( boxed ));
+					nadp_desktop_file_set_boolean( ndf, group_name, def->desktop_entry, bool_value );
+					break;
+
+				case NAFD_TYPE_STRING_LIST:
+					slist_value = ( GSList * ) na_data_boxed_get_as_void( boxed );
+					nadp_desktop_file_set_string_list( ndf, group_name, def->desktop_entry, slist_value );
+					na_core_utils_slist_free( slist_value );
+					break;
+
+				case NAFD_TYPE_UINT:
+					uint_value = GPOINTER_TO_UINT( na_data_boxed_get_as_void( boxed ));
+					nadp_desktop_file_set_uint( ndf, group_name, def->desktop_entry, uint_value );
+					break;
+
+				default:
+					g_warning( "%s: unknown type=%u for %s", thisfn, def->type, def->name );
+					code = NA_IIO_PROVIDER_CODE_PROGRAM_ERROR;
+			}
+
+			/*g_debug( "%s: gconf=%p, code=%u, path=%s", thisfn, ( void * ) gconf, code, path );*/
+
+			g_free( group_name );
+		}
+	}
+
+	return( code );
+}
diff --git a/src/io-desktop/nadp-writer.h b/src/io-desktop/nadp-writer.h
index eacad68..291d3d4 100644
--- a/src/io-desktop/nadp-writer.h
+++ b/src/io-desktop/nadp-writer.h
@@ -36,12 +36,18 @@
 G_BEGIN_DECLS
 
 gboolean nadp_iio_provider_is_willing_to_write( const NAIIOProvider *provider );
+gboolean nadp_iio_provider_is_able_to_write   ( const NAIIOProvider *provider );
 
-gboolean nadp_iio_provider_is_able_to_write( const NAIIOProvider *provider );
+guint    nadp_iio_provider_write_item ( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages );
+guint    nadp_iio_provider_delete_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages );
 
-guint    nadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages );
+guint    nadp_writer_ifactory_provider_write_start(
+				const NAIFactoryProvider *provider, void *writer_data, const NAIFactoryObject *object,
+				GSList **messages  );
 
-guint    nadp_iio_provider_delete_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages );
+guint    nadp_writer_ifactory_provider_write_data(
+				const NAIFactoryProvider *provider, void *writer_data, const NAIFactoryObject *object,
+				const NADataBoxed *boxed, GSList **messages );
 
 G_END_DECLS
 



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