[nautilus-actions] Remove key from desktop file when it is no more set



commit 925711524255c19a5cfb88b2200598f223a2b18b
Author: Pierre Wieser <pwieser trychlos org>
Date:   Mon Mar 29 06:56:16 2010 +0200

    Remove key from desktop file when it is no more set

 ChangeLog                          |    7 +++++
 src/io-desktop/nadp-desktop-file.c |   48 ++++++++++++++++++++++++++++++++++++
 src/io-desktop/nadp-desktop-file.h |    2 +
 src/io-desktop/nadp-writer.c       |   33 +++++++++++++++---------
 4 files changed, 77 insertions(+), 13 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 01203ab..bbe9e14 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -125,6 +125,13 @@
 
 2009-03-29 Pierre Wieser <pwieser trychlos org>
 
+	* src/io-desktop/nadp-desktop-file.c:
+	* src/io-desktop/nadp-desktop-file.h (nadp_desktop_file_remove_key):
+	New function.
+
+	* src/io-desktop/nadp-writer.c (nadp_iio_provider_write_item):
+	Remove key when it is no more set.
+
 	* src/io-desktop/nadp-keys.h: Remove unused keys.
 
 	* src/io-desktop/nadp-reader.c (nadp_reader_ifactory_provider_read_done):
diff --git a/src/io-desktop/nadp-desktop-file.c b/src/io-desktop/nadp-desktop-file.c
index c15588f..4a9aed4 100644
--- a/src/io-desktop/nadp-desktop-file.c
+++ b/src/io-desktop/nadp-desktop-file.c
@@ -468,6 +468,47 @@ nadp_desktop_file_get_profiles( const NadpDesktopFile *ndf )
 }
 
 /**
+ * nadp_desktop_file_remove_key:
+ * @ndf: this #NadpDesktopFile instance.
+ * @group: the group.
+ * @key: the key.
+ *
+ * Removes the specified key.
+ *
+ * Note that this doesn't work very well for localized keys, as we only
+ * remove a key which has the exact same label that the provided one.
+ * So we'd have to remove:
+ * - key
+ * - key[en_US.UTF-8]
+ * - key[en_US]
+ * - key[en]
+ */
+void
+nadp_desktop_file_remove_key( const NadpDesktopFile *ndf, const gchar *group, const gchar *key )
+{
+	char **locales;
+	char **iloc;
+	gchar *locale_key;
+
+	g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
+
+	if( !ndf->private->dispose_has_run ){
+
+		g_key_file_remove_key( ndf->private->key_file, group, key, NULL );
+
+		locales = ( char ** ) g_get_language_names();
+		iloc = locales;
+
+		while( *iloc ){
+			locale_key = g_strdup_printf( "%s[%s]", key, *iloc );
+			g_key_file_remove_key( ndf->private->key_file, group, locale_key, NULL );
+			g_free( locale_key );
+			iloc++;
+		}
+	}
+}
+
+/**
  * nadp_desktop_file_get_boolean:
  * @ndf: this #NadpDesktopFile instance.
  * @group: the searched group.
@@ -755,6 +796,13 @@ nadp_desktop_file_set_locale_string( const NadpDesktopFile *ndf, const gchar *gr
 	if( !ndf->private->dispose_has_run ){
 
 		locales = ( char ** ) g_get_language_names();
+		/*
+		en_US.UTF-8
+		en_US
+		en.UTF-8
+		en
+		C
+		*/
 		g_key_file_set_locale_string( ndf->private->key_file, group, key, locales[0], value );
 	}
 }
diff --git a/src/io-desktop/nadp-desktop-file.h b/src/io-desktop/nadp-desktop-file.h
index 6df3436..f42ce44 100644
--- a/src/io-desktop/nadp-desktop-file.h
+++ b/src/io-desktop/nadp-desktop-file.h
@@ -84,6 +84,8 @@ gchar           *nadp_desktop_file_get_file_type    ( const NadpDesktopFile *ndf
 gchar           *nadp_desktop_file_get_id           ( const NadpDesktopFile *ndf );
 GSList          *nadp_desktop_file_get_profiles     ( const NadpDesktopFile *ndf );
 
+void             nadp_desktop_file_remove_key       ( const NadpDesktopFile *ndf, const gchar *group, const gchar *key );
+
 gboolean         nadp_desktop_file_get_boolean      ( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, gboolean *key_found, gboolean default_value );
 gchar           *nadp_desktop_file_get_locale_string( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, gboolean *key_found, const gchar *default_value );
 gchar           *nadp_desktop_file_get_string       ( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, gboolean *key_found, const gchar *default_value );
diff --git a/src/io-desktop/nadp-writer.c b/src/io-desktop/nadp-writer.c
index 1be0b42..5db875c 100644
--- a/src/io-desktop/nadp-writer.c
+++ b/src/io-desktop/nadp-writer.c
@@ -170,6 +170,12 @@ nadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem
  * actually writes the item to the existing NadpDesktopFile
  * as we have choosen to take advantage of data factory management system
  * we do not need to enumerate each and every elementary data
+ *
+ * As we want keep comments between through multiple updates, we cannot
+ * just delete the .desktop file and recreate it as we are doing for GConf.
+ * Instead of that, we delete each group before updating it, then deleting
+ * last groups (not updated ones) at end.
+ * -> as a side effect, we lose comments inside of groups :(
  */
 static guint
 write_item( const NAIIOProvider *provider, const NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages )
@@ -348,20 +354,20 @@ nadp_writer_ifactory_provider_write_data(
 
 	code = NA_IIO_PROVIDER_CODE_OK;
 	ndf = NADP_DESKTOP_FILE( writer_data );
+	def = na_data_boxed_get_data_def( boxed );
 
-	if( na_data_boxed_is_set( boxed )){
-		def = na_data_boxed_get_data_def( boxed );
+	if( def->desktop_entry && strlen( def->desktop_entry )){
 
-		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 );
 
-			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 );
+		}
 
-			} else {
-				group_name = g_strdup( NADP_GROUP_DESKTOP );
-			}
+		if( na_data_boxed_is_set( boxed )){
 
 			switch( def->type ){
 
@@ -398,10 +404,11 @@ nadp_writer_ifactory_provider_write_data(
 					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 );
+		} else {
+			nadp_desktop_file_remove_key( ndf, group_name, def->desktop_entry );
 		}
+
+		g_free( group_name );
 	}
 
 	return( code );



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