[nautilus-actions] Handle key files



commit 7748a0895a61ec5217a9f12653114ee42b163fe1
Author: Pierre Wieser <pwieser trychlos org>
Date:   Tue Dec 1 22:12:28 2009 +0100

    Handle key files
    
    No more use EggDesktopFile class as it seems rather read-oriented,
    and it appears simpler to direcly handle the key file itself.

 ChangeLog                                          |   23 +
 nautilus-actions/io-provider-desktop/Makefile.am   |    2 -
 .../io-provider-desktop/egg-desktop-file.c         | 1510 --------------------
 .../io-provider-desktop/egg-desktop-file.h         |  160 ---
 .../io-provider-desktop/nadp-desktop-file.c        |  306 ++++-
 .../io-provider-desktop/nadp-desktop-file.h        |   14 +-
 .../io-provider-desktop/nadp-desktop-provider.c    |    7 +
 .../io-provider-desktop/nadp-desktop-provider.h    |   10 -
 nautilus-actions/io-provider-desktop/nadp-read.c   |   28 +-
 nautilus-actions/io-provider-desktop/nadp-write.c  |   70 +-
 nautilus-actions/private/na-object-item-priv.h     |    2 +-
 11 files changed, 391 insertions(+), 1741 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 88c5e43..286f701 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2009-12-01 Pierre Wieser <pwieser trychlos org>
+
+	* nautilus-actions/private/na-object-item-priv.h: Fix typo.
+
+	* nautilus-actions/io-provider-desktop/nadp-desktop-provider.h:
+	* nautilus-actions/io-provider-desktop/nadp-desktop-provider.c:
+	Move NadpDesktopProviderPrivate to C source.
+
+	* nautilus-actions/io-provider-desktop/nadp-write.c:
+	Remove EggDesktopFile class.
+	(nadp_iio_provider_write_item, nadp_iio_provider_delete_item):
+	Check that item is not read-only before trying to write or delete.
+
+	* nautilus-actions/io-provider-desktop/Makefile.am:
+	Remove EggDesktopFile files.
+
+	* nautilus-actions/io-provider-desktop/nadp-desktop-file.c:
+	* nautilus-actions/io-provider-desktop/nadp-desktop-file.h:
+	Handle key files.
+
+	* nautilus-actions/io-provider-desktop/nadp-read.c:
+	Test for writability status of the readen item.
+
 2009-11-30 Pierre Wieser <pwieser trychlos org>
 
 	* nautilus-actions/io-provider-desktop/nadp-desktop-file.c:
diff --git a/nautilus-actions/io-provider-desktop/Makefile.am b/nautilus-actions/io-provider-desktop/Makefile.am
index 17c28de..662fa06 100644
--- a/nautilus-actions/io-provider-desktop/Makefile.am
+++ b/nautilus-actions/io-provider-desktop/Makefile.am
@@ -37,8 +37,6 @@ AM_CPPFLAGS += \
 	$(NULL)
 
 libna_io_provider_desktop_la_SOURCES = \
-	egg-desktop-file.c											\
-	egg-desktop-file.h											\
 	nadp-desktop-file.c											\
 	nadp-desktop-file.h											\
 	nadp-desktop-provider.c										\
diff --git a/nautilus-actions/io-provider-desktop/nadp-desktop-file.c b/nautilus-actions/io-provider-desktop/nadp-desktop-file.c
index 1311b60..122d30c 100644
--- a/nautilus-actions/io-provider-desktop/nadp-desktop-file.c
+++ b/nautilus-actions/io-provider-desktop/nadp-desktop-file.c
@@ -32,6 +32,9 @@
 #include <config.h>
 #endif
 
+#include <gio/gio.h>
+#include <string.h>
+
 #include "nadp-desktop-file.h"
 #include "nadp-utils.h"
 
@@ -44,18 +47,22 @@ struct NadpDesktopFileClassPrivate {
 /* private instance data
  */
 struct NadpDesktopFilePrivate {
-	gboolean        dispose_has_run;
-	gchar          *id;
-	EggDesktopFile *edf;
+	gboolean   dispose_has_run;
+	gchar     *id;
+	gchar     *path;
+	GKeyFile  *key_file;
 };
 
 static GObjectClass *st_parent_class = NULL;
 
-static GType register_type( void );
-static void  class_init( NadpDesktopFileClass *klass );
-static void  instance_init( GTypeInstance *instance, gpointer klass );
-static void  instance_dispose( GObject *object );
-static void  instance_finalize( GObject *object );
+static GType            register_type( void );
+static void             class_init( NadpDesktopFileClass *klass );
+static void             instance_init( GTypeInstance *instance, gpointer klass );
+static void             instance_dispose( GObject *object );
+static void             instance_finalize( GObject *object );
+
+static NadpDesktopFile *ndf_new( const gchar *path );
+static gboolean         check_key_file( NadpDesktopFile *ndf );
 
 GType
 nadp_desktop_file_get_type( void )
@@ -124,6 +131,7 @@ instance_init( GTypeInstance *instance, gpointer klass )
 	self->private = g_new0( NadpDesktopFilePrivate, 1 );
 
 	self->private->dispose_has_run = FALSE;
+	self->private->key_file = g_key_file_new();
 }
 
 static void
@@ -140,11 +148,6 @@ instance_dispose( GObject *object )
 
 		self->private->dispose_has_run = TRUE;
 
-		if( self->private->edf ){
-			egg_desktop_file_free( self->private->edf );
-			self->private->edf = NULL;
-		}
-
 		/* chain up to the parent class */
 		if( G_OBJECT_CLASS( st_parent_class )->dispose ){
 			G_OBJECT_CLASS( st_parent_class )->dispose( object );
@@ -165,6 +168,16 @@ instance_finalize( GObject *object )
 		self->private->id = NULL;
 	}
 
+	if( self->private->path ){
+		g_free( self->private->path );
+		self->private->path = NULL;
+	}
+
+	if( self->private->key_file ){
+		g_key_file_free( self->private->key_file );
+		self->private->key_file = NULL;
+	}
+
 	g_free( self->private );
 
 	/* chain call to parent class */
@@ -173,39 +186,146 @@ instance_finalize( GObject *object )
 	}
 }
 
+/*
+ * ndf_new:
+ * @path: the full pathname of a .desktop file.
+ *
+ * Retuns: a newly allocated #NadpDesktopFile object.
+ */
+static NadpDesktopFile *
+ndf_new( const gchar *path )
+{
+	NadpDesktopFile *ndf;
+
+	ndf = g_object_new( NADP_DESKTOP_FILE_TYPE, NULL );
+
+	ndf->private->id = nadp_utils_path2id( path );
+	ndf->private->path = g_strdup( path );
+
+	return( ndf );
+}
+
+/**
+ * nadp_desktop_file_new_for_write:
+ * @path: the full pathname of a .desktop file.
+ *
+ * Retuns: a newly allocated #NadpDesktopFile object.
+ */
+NadpDesktopFile *
+nadp_desktop_file_new_for_write( const gchar *path )
+{
+	static const gchar *thisfn = "nadp_desktop_file_new_for_write";
+	NadpDesktopFile *ndf;
+
+	ndf = NULL;
+	g_debug( "%s: path=%s", thisfn, path );
+	g_return_val_if_fail( path && g_utf8_strlen( path, -1 ) && g_path_is_absolute( path ), ndf );
+
+	ndf = ndf_new( path );
+
+	g_key_file_set_string( ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TYPE, G_KEY_FILE_DESKTOP_TYPE_APPLICATION );
+
+	return( ndf );
+}
+
 /**
  * nadp_desktop_file_new_from_path:
  * @path: the full pathname of a .desktop file.
  *
  * Retuns: a newly allocated #NadpDesktopFile object.
+ *
+ * Key file has been loaded, and first validity checks made.
  */
 NadpDesktopFile *
 nadp_desktop_file_new_from_path( const gchar *path )
 {
 	static const gchar *thisfn = "nadp_desktop_file_new_from_path";
 	NadpDesktopFile *ndf;
-	EggDesktopFile *edf;
 	GError *error;
 
 	ndf = NULL;
 	g_debug( "%s: path=%s", thisfn, path );
-	g_return_val_if_fail( path && g_utf8_strlen( path, -1 ), ndf );
+	g_return_val_if_fail( path && g_utf8_strlen( path, -1 ) && g_path_is_absolute( path ), ndf );
+
+	ndf = ndf_new( path );
 
 	error = NULL;
-	edf = egg_desktop_file_new( path, &error );
+	g_key_file_load_from_file( ndf->private->key_file, path, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error );
 	if( error ){
 		g_warning( "%s: %s: %s", thisfn, path, error->message );
 		g_error_free( error );
+		g_object_unref( ndf );
 		return( NULL );
 	}
 
-	ndf = g_object_new( NADP_DESKTOP_FILE_TYPE, NULL );
-	ndf->private->edf = edf;
-	ndf->private->id = nadp_utils_path2id( path );
+	if( !check_key_file( ndf )){
+		g_object_unref( ndf );
+		return( NULL );
+	}
 
 	return( ndf );
 }
 
+static gboolean
+check_key_file( NadpDesktopFile *ndf )
+{
+	static const gchar *thisfn = "nadp_desktop_file_check_key_file";
+	gboolean ret;
+	gchar *start_group;
+	gchar *type;
+	GError *error;
+
+	ret = TRUE;
+
+	/* start group must be 'Desktop Entry' */
+	start_group = g_key_file_get_start_group( ndf->private->key_file );
+	if( !strcmp( start_group, G_KEY_FILE_DESKTOP_GROUP )){
+		g_warning( "%s: %s: invalid start group, found %s, waited for %s",
+				thisfn, ndf->private->path, start_group, G_KEY_FILE_DESKTOP_GROUP );
+		ret = FALSE;
+	}
+	g_free( start_group );
+
+	/* Type is required
+	 * only deal with 'Application' type */
+	if( ret ){
+		error = NULL;
+		type = g_key_file_get_string( ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TYPE, &error );
+		if( error ){
+			g_warning( "%s: %s: %s", thisfn, ndf->private->path, error->message );
+			g_error_free( error );
+			ret = FALSE;
+		} else if( !strcmp( type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION )){
+			ret = FALSE;
+		}
+		g_free( type );
+	}
+
+	return( ret );
+}
+
+/**
+ * nadp_desktop_file_get_key_file_path:
+ * @ndf: the #NadpDesktopFile instance.
+ *
+ * Returns: the full pathname of the key file, as a newly allocated
+ * string which should be g_free() by the caller.
+ */
+gchar *
+nadp_desktop_file_get_key_file_path( const NadpDesktopFile *ndf )
+{
+	gchar *path;
+
+	path = NULL;
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), path );
+
+	if( !ndf->private->dispose_has_run ){
+		path = g_strdup( ndf->private->path );
+	}
+
+	return( path );
+}
+
 /**
  * nadp_desktop_file_get_id:
  * @ndf: the #NadpDesktopFile instance.
@@ -222,27 +342,157 @@ nadp_desktop_file_get_id( const NadpDesktopFile *ndf )
 	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), id );
 
 	if( !ndf->private->dispose_has_run ){
-		id = ndf->private->id;
+		id = g_strdup( ndf->private->id );
 	}
 
 	return( id );
 }
 
 /**
- * nadp_desktop_file_get_egg_desktop_file:
+ * nadp_desktop_file_get_label:
+ * @ndf: the #NadpDesktopFile instance.
+ *
+ * Returns: the label of the action, as a newly allocated string which
+ * should be g_free() by the caller.
+ */
+gchar *
+nadp_desktop_file_get_label( const NadpDesktopFile *ndf )
+{
+	gchar *label;
+
+	label = NULL;
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), label );
+
+	if( !ndf->private->dispose_has_run ){
+		label = g_key_file_get_locale_string(
+				ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL );
+	}
+
+	return( label );
+}
+
+/**
+ * nadp_desktop_file_get_tooltip:
  * @ndf: the #NadpDesktopFile instance.
+ *
+ * Returns: the tooltip of the action, as a newly allocated string which
+ * should be g_free() by the caller.
  */
-EggDesktopFile *
-nadp_desktop_file_get_egg_desktop_file( const NadpDesktopFile *ndf )
+gchar *
+nadp_desktop_file_get_tooltip( const NadpDesktopFile *ndf )
 {
-	EggDesktopFile *edf;
+	gchar *tooltip;
+
+	tooltip = NULL;
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), tooltip );
+
+	if( !ndf->private->dispose_has_run ){
+		tooltip = g_key_file_get_locale_string(
+				ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL );
+	}
+
+	return( tooltip );
+}
 
-	edf = NULL;
-	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), edf );
+/**
+ * nadp_desktop_file_set_label:
+ * @ndf: the #NadpDesktopFile instance.
+ * @label: the label to be set.
+ *
+ * Sets the label.
+ */
+void
+nadp_desktop_file_set_label( NadpDesktopFile *ndf, const gchar *label )
+{
+	g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
 
 	if( !ndf->private->dispose_has_run ){
-		edf = ndf->private->edf;
+		g_key_file_set_locale_string(
+				ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, label );
+	}
+}
+
+/**
+ * nadp_desktop_file_set_tooltip:
+ * @ndf: the #NadpDesktopFile instance.
+ * @tooltip: the tooltip to be set.
+ *
+ * Sets the tooltip.
+ */
+void
+nadp_desktop_file_set_tooltip( NadpDesktopFile *ndf, const gchar *tooltip )
+{
+	g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
+
+	if( !ndf->private->dispose_has_run ){
+		g_key_file_set_locale_string(
+				ndf->private->key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, tooltip );
+	}
+}
+
+/**
+ * nadp_desktop_file_write:
+ * @ndf: the #NadpDesktopFile instance.
+ *
+ * Writes the key file to the disk.
+ *
+ * Returns: %TRUE if write is ok, %FALSE else.
+ */
+gboolean
+nadp_desktop_file_write( NadpDesktopFile *ndf )
+{
+	static const gchar *thisfn = "nadp_desktop_file_write";
+	gboolean ret;
+	gchar *data;
+	GFile *file;
+	GFileOutputStream *stream;
+	GError *error;
+
+	ret = FALSE;
+	error = NULL;
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), ret );
+
+	if( !ndf->private->dispose_has_run ){
+
+		data = g_key_file_to_data( ndf->private->key_file, NULL, NULL );
+		file = g_file_new_for_path( ndf->private->path );
+
+		stream = g_file_replace( file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error );
+		if( error ){
+			g_warning( "%s: g_file_replace: %s", thisfn, error->message );
+			g_error_free( error );
+			if( stream ){
+				g_object_unref( stream );
+			}
+			g_object_unref( file );
+			g_free( data );
+			return( FALSE );
+		}
+
+		g_output_stream_write( G_OUTPUT_STREAM( stream ), data, g_utf8_strlen( data, -1 ), NULL, &error );
+		if( error ){
+			g_warning( "%s: g_output_stream_write: %s", thisfn, error->message );
+			g_error_free( error );
+			g_object_unref( stream );
+			g_object_unref( file );
+			g_free( data );
+			return( FALSE );
+		}
+
+		g_output_stream_close( G_OUTPUT_STREAM( stream ), NULL, &error );
+		if( error ){
+			g_warning( "%s: g_output_stream_close: %s", thisfn, error->message );
+			g_error_free( error );
+			g_object_unref( stream );
+			g_object_unref( file );
+			g_free( data );
+			return( FALSE );
+		}
+
+		g_object_unref( stream );
+		g_object_unref( file );
+		g_free( data );
 	}
 
-	return( edf );
+	return( TRUE );
 }
diff --git a/nautilus-actions/io-provider-desktop/nadp-desktop-file.h b/nautilus-actions/io-provider-desktop/nadp-desktop-file.h
index 3a7face..ecaab64 100644
--- a/nautilus-actions/io-provider-desktop/nadp-desktop-file.h
+++ b/nautilus-actions/io-provider-desktop/nadp-desktop-file.h
@@ -44,8 +44,6 @@
 #include <glib.h>
 #include <glib-object.h>
 
-#include "egg-desktop-file.h"
-
 G_BEGIN_DECLS
 
 #define NADP_DESKTOP_FILE_TYPE					( nadp_desktop_file_get_type())
@@ -73,11 +71,19 @@ typedef struct {
 
 GType            nadp_desktop_file_get_type( void );
 
+NadpDesktopFile *nadp_desktop_file_new_for_write( const gchar *path );
 NadpDesktopFile *nadp_desktop_file_new_from_path( const gchar *path );
 
-gchar           *nadp_desktop_file_get_id              ( const NadpDesktopFile *ndf );
+gchar           *nadp_desktop_file_get_key_file_path( const NadpDesktopFile *ndf );
+
+gchar           *nadp_desktop_file_get_id( const NadpDesktopFile *ndf );
+gchar           *nadp_desktop_file_get_label( const NadpDesktopFile *ndf );
+gchar           *nadp_desktop_file_get_tooltip( const NadpDesktopFile *ndf );
+
+void             nadp_desktop_file_set_label( NadpDesktopFile *ndf, const gchar *label );
+void             nadp_desktop_file_set_tooltip( NadpDesktopFile *ndf, const gchar *tooltip );
 
-EggDesktopFile  *nadp_desktop_file_get_egg_desktop_file( const NadpDesktopFile *ndf );
+gboolean         nadp_desktop_file_write( NadpDesktopFile *ndf );
 
 G_END_DECLS
 
diff --git a/nautilus-actions/io-provider-desktop/nadp-desktop-provider.c b/nautilus-actions/io-provider-desktop/nadp-desktop-provider.c
index 82b4bdd..8410378 100644
--- a/nautilus-actions/io-provider-desktop/nadp-desktop-provider.c
+++ b/nautilus-actions/io-provider-desktop/nadp-desktop-provider.c
@@ -46,6 +46,13 @@ struct NadpDesktopProviderClassPrivate {
 	void *empty;						/* so that gcc -pedantic is happy */
 };
 
+/* private instance data
+ */
+typedef struct NadpDesktopProviderPrivate {
+	gboolean dispose_has_run;
+}
+	NadpDesktopProviderPrivate;
+
 static GType         st_module_type = 0;
 static GObjectClass *st_parent_class = NULL;
 
diff --git a/nautilus-actions/io-provider-desktop/nadp-desktop-provider.h b/nautilus-actions/io-provider-desktop/nadp-desktop-provider.h
index 8c068ef..79c4af9 100644
--- a/nautilus-actions/io-provider-desktop/nadp-desktop-provider.h
+++ b/nautilus-actions/io-provider-desktop/nadp-desktop-provider.h
@@ -46,8 +46,6 @@
 
 #include <glib-object.h>
 
-#include "egg-desktop-file.h"
-
 G_BEGIN_DECLS
 
 #define NADP_DESKTOP_PROVIDER_TYPE					( nadp_desktop_provider_get_type())
@@ -57,14 +55,6 @@ G_BEGIN_DECLS
 #define NADP_IS_DESKTOP_PROVIDER_CLASS( klass )		( G_TYPE_CHECK_CLASS_TYPE(( klass ), NADP_DESKTOP_PROVIDER_TYPE ))
 #define NADP_DESKTOP_PROVIDER_GET_CLASS( object )	( G_TYPE_INSTANCE_GET_CLASS(( object ), NADP_DESKTOP_PROVIDER_TYPE, NadpDesktopProviderClass ))
 
-/* private instance data
- */
-typedef struct NadpDesktopProviderPrivate {
-	gboolean        dispose_has_run;
-	EggDesktopFile *egg_desktop_file;
-}
-	NadpDesktopProviderPrivate;
-
 typedef struct {
 	GObject                     parent;
 	NadpDesktopProviderPrivate *private;
diff --git a/nautilus-actions/io-provider-desktop/nadp-read.c b/nautilus-actions/io-provider-desktop/nadp-read.c
index 8cd7cb9..062c562 100644
--- a/nautilus-actions/io-provider-desktop/nadp-read.c
+++ b/nautilus-actions/io-provider-desktop/nadp-read.c
@@ -52,6 +52,7 @@ static gboolean        is_already_loaded( const NadpDesktopProvider *provider, G
 static GList          *desktop_path_from_id( const NadpDesktopProvider *provider, GList *files, const gchar *dir, const gchar *id );
 static NAObjectAction *action_from_desktop_path( const NadpDesktopProvider *provider, DesktopPath *dps, GSList **messages );
 static void            read_action_properties( const NadpDesktopProvider *provider, NAObjectAction *action, NadpDesktopFile *ndf, GSList **messages );
+static void            read_item_properties( const NadpDesktopProvider *provider, NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages );
 static void            free_desktop_paths( GList *paths );
 
 /*
@@ -130,6 +131,7 @@ get_list_of_desktop_files( const NadpDesktopProvider *provider, GList **files, c
 
 	error = NULL;
 	dir_handle = NULL;
+
 	/* do not warn when the directory just doesn't exist
 	 */
 	if( g_file_test( dir, G_FILE_TEST_IS_DIR )){
@@ -226,23 +228,39 @@ action_from_desktop_path( const NadpDesktopProvider *provider, DesktopPath *dps,
 static void
 read_action_properties( const NadpDesktopProvider *provider, NAObjectAction *action, NadpDesktopFile *ndf, GSList **messages )
 {
-	static const gchar *thisfn = "nadp_action_item_properties";
-	EggDesktopFile *edf;
+	read_item_properties( provider, NA_OBJECT_ITEM( action ), ndf, messages );
+}
+
+static void
+read_item_properties( const NadpDesktopProvider *provider, NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages )
+{
+	static const gchar *thisfn = "nadp_read_read_item_properties";
 	gchar *id;
 	gchar *label;
+	gchar *tooltip;
+	gchar *path;
+	gboolean writable;
 
-	edf = nadp_desktop_file_get_egg_desktop_file( ndf );
 	id = nadp_desktop_file_get_id( ndf );
 
-	label = ( gchar * ) egg_desktop_file_get_name( edf );
+	label = ( gchar * ) nadp_desktop_file_get_label( ndf );
 	if( !label || !g_utf8_strlen( label, -1 )){
 		g_warning( "%s: id=%s, label not found or empty", thisfn, id );
 		g_free( label );
 		label = g_strdup( "" );
 	}
-	na_object_set_label( action, label );
+	na_object_set_label( item, label );
 	g_free( label );
 
+	tooltip = ( gchar * ) nadp_desktop_file_get_tooltip( ndf );
+	na_object_set_tooltip( item, tooltip );
+	g_free( tooltip );
+
+	path = nadp_desktop_file_get_key_file_path( ndf );
+	writable = nadp_utils_is_writable_file( path );
+	g_free( path );
+	na_object_set_readonly( item, !writable );
+
 	g_free( id );
 }
 
diff --git a/nautilus-actions/io-provider-desktop/nadp-write.c b/nautilus-actions/io-provider-desktop/nadp-write.c
index c65e96e..da3cfdc 100644
--- a/nautilus-actions/io-provider-desktop/nadp-write.c
+++ b/nautilus-actions/io-provider-desktop/nadp-write.c
@@ -40,7 +40,7 @@
 #include "nadp-utils.h"
 #include "nadp-xdg-data-dirs.h"
 
-static guint write_item( const NAIIOProvider *provider, const NAObjectItem *item, const gchar *path, GSList **messages );
+static guint write_item( const NAIIOProvider *provider, const NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages );
 
 /*
  * NadpDesktopProvider is willing to write if user data dir exists (or
@@ -88,8 +88,7 @@ nadp_iio_provider_is_writable( const NAIIOProvider *provider, const NAObjectItem
 {
 	gboolean writable;
 	NadpDesktopFile *ndf;
-	EggDesktopFile *edf;
-	const char *path;
+	gchar *path;
 
 	writable = FALSE;
 	g_return_val_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ), writable );
@@ -99,9 +98,9 @@ nadp_iio_provider_is_writable( const NAIIOProvider *provider, const NAObjectItem
 
 	if( ndf ){
 		g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), writable );
-		edf = nadp_desktop_file_get_egg_desktop_file( ndf );
-		path = egg_desktop_file_get_source( edf );
+		path = nadp_desktop_file_get_key_file_path( ndf );
 		writable = nadp_utils_is_writable_file( path );
+		g_free( path );
 
 	} else {
 		writable = nadp_iio_provider_is_willing_to_write( provider );
@@ -113,9 +112,9 @@ nadp_iio_provider_is_writable( const NAIIOProvider *provider, const NAObjectItem
 guint
 nadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages )
 {
+	static const gchar *thisfn = "nadp_iio_provider_write_item";
 	guint ret;
 	NadpDesktopFile *ndf;
-	EggDesktopFile *edf;
 	gchar *path;
 	gchar *userdir;
 	gchar *id;
@@ -125,12 +124,16 @@ nadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem
 	g_return_val_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ), ret );
 	g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), ret );
 
+	if( na_object_is_readonly( item )){
+		g_warning( "%s: item=%p is read-only", thisfn, ( void * ) item );
+		return( NA_IIO_PROVIDER_NOT_WRITABLE );
+	}
+
 	ndf = ( NadpDesktopFile * ) g_object_get_data( G_OBJECT( item ), "nadp-desktop-file" );
 
+	/* write into the current key file and write it to current path */
 	if( ndf ){
 		g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), ret );
-		edf = nadp_desktop_file_get_egg_desktop_file( ndf );
-		path = ( gchar * ) egg_desktop_file_get_source( edf );
 
 	} else {
 		userdir = nadp_xdg_data_dirs_get_user_dir( NADP_DESKTOP_PROVIDER( provider ), messages );
@@ -141,47 +144,72 @@ nadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem
 		g_free( bname );
 		g_free( userdir );
 
-		ret = write_item( provider, item, path, messages );
-
-		if( ret == NA_IIO_PROVIDER_WRITE_OK ){
-			ndf = nadp_desktop_file_new_from_path( path );
-			g_object_set_data( G_OBJECT( item ), "nadp-desktop-file", ndf );
-			g_object_weak_ref( G_OBJECT( item ), ( GWeakNotify ) g_object_unref, ndf );
-		}
+		ndf = nadp_desktop_file_new_for_write( path );
+		g_object_set_data( G_OBJECT( item ), "nadp-desktop-file", ndf );
+		g_object_weak_ref( G_OBJECT( item ), ( GWeakNotify ) g_object_unref, ndf );
 
 		g_free( path );
 	}
 
+	ret = write_item( provider, item, ndf, messages );
+
 	return( ret );
 }
 
+/*
+ *
+ */
 static guint
-write_item( const NAIIOProvider *provider, const NAObjectItem *item, const gchar *path, GSList **messages )
+write_item( const NAIIOProvider *provider, const NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages )
 {
-	return( NA_IIO_PROVIDER_WRITE_OK );
+	guint ret;
+	gchar *label;
+	gchar *tooltip;
+
+	ret = NA_IIO_PROVIDER_WRITE_OK;
+
+	label = na_object_get_label( item );
+	nadp_desktop_file_set_label( ndf, label );
+	g_free( label );
+
+	tooltip = na_object_get_tooltip( item );
+	nadp_desktop_file_set_tooltip( ndf, tooltip );
+	g_free( tooltip );
+
+	if( !nadp_desktop_file_write( ndf )){
+		ret = NA_IIO_PROVIDER_WRITE_ERROR;
+	}
+
+	return( ret );
 }
 
 guint
 nadp_iio_provider_delete_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages )
 {
+	static const gchar *thisfn = "nadp_iio_provider_delete_item";
 	guint ret;
 	NadpDesktopFile *ndf;
-	EggDesktopFile *edf;
-	const char *path;
+	gchar *path;
 
 	ret = NA_IIO_PROVIDER_NOT_WILLING_TO_WRITE;
 	g_return_val_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ), ret );
 	g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), ret );
 
+	if( na_object_is_readonly( item )){
+		g_warning( "%s: item=%p is read-only", thisfn, ( void * ) item );
+		return( NA_IIO_PROVIDER_NOT_WRITABLE );
+	}
+
 	ndf = ( NadpDesktopFile * ) g_object_get_data( G_OBJECT( item ), "nadp-desktop-file" );
 
 	if( ndf ){
 		g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), ret );
-		edf = nadp_desktop_file_get_egg_desktop_file( ndf );
-		path = egg_desktop_file_get_source( edf );
+		path = nadp_desktop_file_get_key_file_path( ndf );
 		if( nadp_utils_delete_file( path )){
 			ret = NA_IIO_PROVIDER_WRITE_OK;
 		}
+		g_free( path );
+
 	} else {
 		ret = NA_IIO_PROVIDER_WRITE_OK;
 	}
diff --git a/nautilus-actions/private/na-object-item-priv.h b/nautilus-actions/private/na-object-item-priv.h
index d1d43b7..3ef1277 100644
--- a/nautilus-actions/private/na-object-item-priv.h
+++ b/nautilus-actions/private/na-object-item-priv.h
@@ -58,7 +58,7 @@ struct NAObjectItemPrivate {
 	 * subsystem
 	 * defaults to FALSE unless a write has already returned an error
 	 */
-	gboolean read_only;
+	gboolean       read_only;
 
 	/* the original provider
 	 * required to be able to edit/delete the item



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