[Nautilus-list] PATCH: enable renaming of .desktop files



The attached patch is a cleaned up version of my earlier patch.

It allows you to rename .desktop files by setting the 'Name' value
within the .desktop file, instead of renaming the actual file system
name.

- Frank



diff -u -r libnautilus-private.original/nautilus-desktop-file-loader.c libnautilus-private/nautilus-desktop-file-loader.c
--- libnautilus-private.original/nautilus-desktop-file-loader.c	Wed Jan  9 21:29:28 2002
+++ libnautilus-private/nautilus-desktop-file-loader.c	Wed Jan  9 22:09:41 2002
@@ -164,116 +164,12 @@
         return df;
 }
 
-static GnomeVFSResult
-write_all (GnomeVFSHandle *handle,
-	   gconstpointer buffer,
-	   GnomeVFSFileSize bytes)
-{
-	GnomeVFSFileSize bytes_written;
-	GnomeVFSResult result;
-
-	while (bytes > 0) {
-		result = gnome_vfs_write (handle, buffer,
-					  bytes, &bytes_written);
-		if (result != GNOME_VFS_OK) {
-			return result;
-		}
-		bytes -= bytes_written;
-	}
-	
-	return GNOME_VFS_OK;
-}
-
-GnomeVFSResult
-nautilus_desktop_file_save (NautilusDesktopFile *df,
-			    const char *uri)
-{
-	NautilusDesktopFileAddition *addition;
-	GnomeVFSHandle *handle;
-	GnomeVFSResult result;
-	char *old_val;
-	GList *list;
-	gint i;
-
-	g_return_val_if_fail (df != NULL, FALSE);
-	g_return_val_if_fail (df->lines != NULL, FALSE);
-
-	result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_WRITE);
-	if (result != GNOME_VFS_OK) {
-		return result;
-	}
-
-	for (i = 0; df->lines[i] != NULL; i++) {
-		gboolean handled_line;
-
-		handled_line = FALSE;
-		for (list = df->addition_list; list; list = list->next) {
-			addition = (NautilusDesktopFileAddition *)list->data;
-			
-			if (addition->section->start_line[0] == df->lines[i]) {
-				addition->saving_section = TRUE;
-			}
-
-			if (addition->saving_section && df->lines[i][0] == '[') {
-				addition->saving_section = FALSE;
-			}
-
-			if (addition->saving_section &&
-			    (strncmp (addition->name, df->lines[i], strlen (addition->name)) == 0)) {
-				old_val = strstr (df->lines[i], "=");
-				if (old_val == NULL) {
-					continue;
-				}
-				result = write_all (handle,
-						    df->lines[i],
-						    old_val + 1 - df->lines[i]);
-				if (result != GNOME_VFS_OK) {
-					gnome_vfs_close (handle);
-					return result;
-				}
-				result = write_all (handle,
-						    addition->value,
-						    strlen (addition->value));
-				if (result != GNOME_VFS_OK) {
-					gnome_vfs_close (handle);
-					return result;
-				}
-				result = write_all (handle, "\n", 1);
-				if (result != GNOME_VFS_OK) {
-					gnome_vfs_close (handle);
-					return result;
-				}
-
-				handled_line = TRUE;
-			}
-		}
-		if (handled_line == FALSE) {
-			result = write_all (handle,
-					    df->lines[i],
-					    strlen (df->lines[i]));
-			if (result != GNOME_VFS_OK) {
-				gnome_vfs_close (handle);
-				return result;
-			}
-			result = write_all (handle, "\n", 1);
-			if (result != GNOME_VFS_OK) {
-				gnome_vfs_close (handle);
-				return result;
-			}
-		}
-	}
-
-	gnome_vfs_close (handle);
-	return GNOME_VFS_OK;
-}
-
 static void
 destroy_foreach (gpointer key, gpointer value, gpointer data)
 {
         section_free (value);
 }
 
-
 void
 nautilus_desktop_file_free (NautilusDesktopFile *df)
 {
@@ -481,6 +377,112 @@
         }
 }
 
+
+static GnomeVFSResult
+write_all (GnomeVFSHandle *handle,
+	   gconstpointer buffer,
+	   GnomeVFSFileSize bytes)
+{
+	GnomeVFSFileSize bytes_written;
+	GnomeVFSResult result;
+
+	while (bytes > 0) {
+		result = gnome_vfs_write (handle, buffer,
+					  bytes, &bytes_written);
+		if (result != GNOME_VFS_OK) {
+			return result;
+		}
+		bytes -= bytes_written;
+	}
+	
+	return GNOME_VFS_OK;
+}
+
+GnomeVFSResult
+nautilus_desktop_file_save (NautilusDesktopFile *df,
+			    const char *uri)
+{
+	NautilusDesktopFileAddition *addition;
+	GnomeVFSHandle *handle;
+	GnomeVFSResult result;
+	char *old_val;
+	GList *list;
+	gint i;
+
+	g_return_val_if_fail (df != NULL, FALSE);
+	g_return_val_if_fail (df->lines != NULL, FALSE);
+
+	result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_WRITE);
+	if (result != GNOME_VFS_OK) {
+		return result;
+	}
+
+	for (i = 0; df->lines[i] != NULL; i++) {
+		gboolean handled_line;
+
+		handled_line = FALSE;
+		for (list = df->addition_list; list; list = list->next) {
+			addition = (NautilusDesktopFileAddition *)list->data;
+			
+			if (addition->section->start_line[0] == df->lines[i]) {
+				addition->saving_section = TRUE;
+			}
+
+			if (addition->saving_section && df->lines[i][0] == '[') {
+				addition->saving_section = FALSE;
+			}
+
+			if (addition->saving_section && key_equal (addition->name, df->lines[i])) {
+
+				old_val = strstr (df->lines[i], "=");
+				if (old_val == NULL) {
+					continue;
+				}
+				
+				result = write_all (handle,
+						    df->lines[i],
+						    old_val + 1 - df->lines[i]);
+				if (result != GNOME_VFS_OK) {
+					gnome_vfs_close (handle);
+					return result;
+				}
+				result = write_all (handle,
+						    addition->value,
+						    strlen (addition->value));
+				if (result != GNOME_VFS_OK) {
+					gnome_vfs_close (handle);
+					return result;
+				}
+				result = write_all (handle, "\n", 1);
+				if (result != GNOME_VFS_OK) {
+					gnome_vfs_close (handle);
+					return result;
+				}
+
+				handled_line = TRUE;
+			}
+		}
+		if (handled_line == FALSE) {
+			result = write_all (handle,
+					    df->lines[i],
+					    strlen (df->lines[i]));
+			if (result != GNOME_VFS_OK) {
+				gnome_vfs_close (handle);
+				return result;
+			}
+			result = write_all (handle, "\n", 1);
+			if (result != GNOME_VFS_OK) {
+				gnome_vfs_close (handle);
+				return result;
+			}
+		}
+	}
+
+	gnome_vfs_close (handle);
+	return GNOME_VFS_OK;
+}
+
+
 static NautilusDesktopFileSection*
 section_new  (const char *name,
               char **start_line)
@@ -1160,3 +1162,68 @@
 	
 	return TRUE; 
 }
+
+
+gboolean
+nautilus_desktop_file_set_locale_string (NautilusDesktopFile *df,
+				  	 const char *section,
+				  	 const char *keyname,
+				  	 const char *value)
+{
+	NautilusDesktopFileSection *sect;
+	NautilusDesktopFileAddition *addition;
+        char *lang;
+        char *lang_country;
+	char *local_keyname;
+	const char *strval;
+
+        get_locale (&lang, &lang_country);
+
+	/* FIXME - we need to try de_DE.ENCODING in addition to what
+	 * we are trying here.
+	 */
+	
+        /* Try "Foo[de_DE]" */
+        if (lang_country) {
+                local_keyname = g_strconcat (keyname, "[", lang_country, "]", NULL);
+                strval = get_keyval (df, section, local_keyname);
+                
+		if (strval != NULL) {
+                        goto done;
+		} else {
+			g_free (local_keyname);
+		}
+        }
+
+        /* Try "Foo[de]" */
+        if (lang) {
+                local_keyname = g_strconcat (keyname, "[", lang, "]", NULL);
+                strval = get_keyval (df, section, local_keyname);
+                
+		if (strval != NULL) {
+			goto done;
+		} else {
+			g_free (local_keyname);
+		}
+        }
+
+        /* If none of the above work fall back to not localized */
+	local_keyname = g_strdup (keyname);
+        
+ done:
+        g_free (lang);
+        g_free (lang_country);
+        
+	sect = get_section (df, section);
+	if (sect == NULL) {
+		g_free (local_keyname);
+		return FALSE;
+	}
+	
+	addition = addition_new (sect, local_keyname, value);
+	df->addition_list = g_list_append (df->addition_list, addition);
+	
+	g_free (local_keyname);
+	
+	return TRUE;
+}
Only in libnautilus-private: nautilus-desktop-file-loader.c.orig
diff -u -r libnautilus-private.original/nautilus-desktop-file-loader.h libnautilus-private/nautilus-desktop-file-loader.h
--- libnautilus-private.original/nautilus-desktop-file-loader.h	Wed Jan  9 21:29:28 2002
+++ libnautilus-private/nautilus-desktop-file-loader.h	Wed Jan  9 21:41:55 2002
@@ -77,6 +77,10 @@
 						  const char           *section,
 						  const char           *keyname,
 						  const char           *value);
+gboolean nautilus_desktop_file_set_locale_string (NautilusDesktopFile  *df,
+						  const char           *section,
+						  const char           *keyname,
+						  const char           *value);
 
 
 /* Some getters and setters are missing, they should be added as needed */
diff -u -r libnautilus-private.original/nautilus-file.c libnautilus-private/nautilus-file.c
--- libnautilus-private.original/nautilus-file.c	Wed Jan  9 21:29:28 2002
+++ libnautilus-private/nautilus-file.c	Wed Jan  9 21:35:29 2002
@@ -34,6 +34,7 @@
 #include "nautilus-global-preferences.h"
 #include "nautilus-lib-self-check-functions.h"
 #include "nautilus-link.h"
+#include "nautilus-link-desktop-file.h"
 #include "nautilus-metadata.h"
 #include "nautilus-trash-directory.h"
 #include "nautilus-trash-file.h"
@@ -782,10 +783,6 @@
 	if (nautilus_file_is_self_owned (file)) {
 		return FALSE;
 	}
-
-	if (nautilus_file_is_mime_type (file, "application/x-gnome-app-info")) {
-		return FALSE;
-	}
 	
 	can_rename = TRUE;
 	uri = nautilus_file_get_uri (file);
@@ -1000,6 +997,21 @@
 	g_return_if_fail (new_name != NULL);
 	g_return_if_fail (callback != NULL);
 
+	/* Can't rename a file that's already gone.
+	 * We need to check this here because there may be a new
+	 * file with the same name.
+	 */
+	if (nautilus_file_is_gone (file)) {
+	       	/* Claim that something changed even if the rename
+		 * failed. This makes it easier for some clients who
+		 * see the "reverting" to the old name as "changing
+		 * back".
+		 */
+		nautilus_file_changed (file);
+		(* callback) (file, GNOME_VFS_ERROR_NOT_FOUND, callback_data);
+		return;
+	}
+
 	/* FIXME: Rename GMC URLs on the local file system by setting
 	 * their metadata only. This leaves no way to rename them for
 	 * real.
@@ -1021,27 +1033,29 @@
 		}
 	}
 	
-	 /* Make return an error for incoming names containing path separators. */
-	 if (strstr (new_name, "/") != NULL) {
-		(* callback) (file, GNOME_VFS_ERROR_NOT_PERMITTED, callback_data);
-		return;
+	/* rename a .desktop file by setting the text value, not by renaming the actual file */
+	if (nautilus_file_is_mime_type (file, "application/x-gnome-app-info")) {
+		uri = nautilus_file_get_uri (file);
+		path = gnome_vfs_get_local_path_from_uri (uri);
+		g_free (uri);		
+		
+		if (path != NULL) {
+			nautilus_link_desktop_file_local_set_text (path, new_name);
+
+			g_free (path);
+
+			(* callback) (file, GNOME_VFS_OK, callback_data);
+			nautilus_file_changed (file);
+			return;
+		}
 	}
 	
-	/* Can't rename a file that's already gone.
-	 * We need to check this here because there may be a new
-	 * file with the same name.
-	 */
-	if (nautilus_file_is_gone (file)) {
-	       	/* Claim that something changed even if the rename
-		 * failed. This makes it easier for some clients who
-		 * see the "reverting" to the old name as "changing
-		 * back".
-		 */
-		nautilus_file_changed (file);
-		(* callback) (file, GNOME_VFS_ERROR_NOT_FOUND, callback_data);
+	/* Make return an error for incoming names containing path separators. */
+	if (strstr (new_name, "/") != NULL) {
+		(* callback) (file, GNOME_VFS_ERROR_NOT_PERMITTED, callback_data);
 		return;
 	}
-
+	
 	/* Test the name-hasn't-changed case explicitly, for two reasons.
 	 * (1) gnome_vfs_async_xfer returns an error if new & old are same.
 	 * (2) We don't want to send file-changed signal if nothing changed.
Only in libnautilus-private: nautilus-file.c.orig
diff -u -r libnautilus-private.original/nautilus-link-desktop-file.c libnautilus-private/nautilus-link-desktop-file.c
--- libnautilus-private.original/nautilus-link-desktop-file.c	Wed Jan  9 21:29:28 2002
+++ libnautilus-private/nautilus-link-desktop-file.c	Wed Jan  9 21:49:01 2002
@@ -49,6 +49,7 @@
 #define NAUTILUS_LINK_MOUNT_TAG 	"FSDevice"
 #define NAUTILUS_LINK_HOME_TAG 		"X-nautilus-home"
 
+
 static const char *
 get_tag (NautilusLinkType type)
 {
@@ -68,13 +69,13 @@
 }
 
 static gchar *
-slurp_key_string (const char *path,
-		  const char *keyname,
-                  gboolean    localize)
+get_key_string (const char *path,
+		const char *keyname,
+                gboolean    localize)
 {
 	NautilusDesktopFile *desktop_file = NULL;
 	gchar *text;
-	gboolean set;
+	gboolean get;
 	GnomeVFSResult result;
 	gchar *uri;
 
@@ -92,12 +93,12 @@
 	}
 
         if (localize) {
-                set = nautilus_desktop_file_get_locale_string (desktop_file,
+                get = nautilus_desktop_file_get_locale_string (desktop_file,
 							       "Desktop Entry",
 							       keyname,
 							       &text);
 	} else {
-                set = nautilus_desktop_file_get_string (desktop_file,
+                get = nautilus_desktop_file_get_string (desktop_file,
 							"Desktop Entry",
 							keyname,
 							&text);
@@ -105,13 +106,61 @@
 	
 	nautilus_desktop_file_free (desktop_file);
 	
-	if (set == FALSE) {
+	if (get == FALSE) {
 		return NULL;
 	}
 
 	return text;
 }
 
+static gboolean
+set_key_string (const char *path,
+		const char *keyname,
+		const char *value,
+                gboolean    localize)
+{
+	NautilusDesktopFile *desktop_file = NULL;
+	gboolean set;
+	GnomeVFSResult result;
+	gchar *uri;
+
+	uri = gnome_vfs_get_uri_from_local_path (path);
+	if (uri == NULL) {
+		return FALSE;
+	}
+
+	result = nautilus_desktop_file_load (uri, &desktop_file);
+
+	if (result != GNOME_VFS_OK) {
+		g_free (uri);
+		return FALSE;
+	}
+
+        if (localize) {
+                set = nautilus_desktop_file_set_locale_string (desktop_file,
+							       "Desktop Entry",
+							       keyname,
+							       value);
+	} else {
+                set = nautilus_desktop_file_set_string (desktop_file,
+							"Desktop Entry",
+							keyname,
+							value);
+	}
+	
+	result = nautilus_desktop_file_save (desktop_file, uri);
+
+	nautilus_desktop_file_free (desktop_file);
+	
+	g_free (uri);
+	
+	if (result != GNOME_VFS_OK || !set) {
+		return FALSE;
+	}
+	
+	return TRUE;
+}
+
 gboolean
 nautilus_link_desktop_file_local_create (const char        *directory_path,
 					 const char        *name,
@@ -213,10 +262,18 @@
 	return TRUE;
 }
 
+gboolean
+nautilus_link_desktop_file_local_set_text (const char *path,
+					   const char *text)
+{
+	return set_key_string (path, "Name", text, TRUE);
+}
+
+
 char *
 nautilus_link_desktop_file_local_get_text (const char *path)
 {
-	return slurp_key_string (path, "Name", TRUE);
+	return get_key_string (path, "Name", TRUE);
 }
 
 char *
@@ -231,14 +288,14 @@
 	gchar *type;
 	gchar *retval;
 
-	type = slurp_key_string (path, "Type", FALSE);
+	type = get_key_string (path, "Type", FALSE);
 	retval = NULL;
 	if (type == NULL) {
 		return NULL;
 	}
 
 	if (strcmp (type, "Application") == 0) {
-		retval = slurp_key_string (path, "Comment", TRUE);
+		retval = get_key_string (path, "Comment", TRUE);
 	}
 	
 	g_free (type);
@@ -253,7 +310,7 @@
 	gchar *type;
 	NautilusLinkType retval;
 
-	type = slurp_key_string (path, "Type", FALSE);
+	type = get_key_string (path, "Type", FALSE);
 
 	if (type == NULL) {
 		return NAUTILUS_LINK_GENERIC;
diff -u -r libnautilus-private.original/nautilus-link-desktop-file.h libnautilus-private/nautilus-link-desktop-file.h
--- libnautilus-private.original/nautilus-link-desktop-file.h	Wed Jan  9 21:29:28 2002
+++ libnautilus-private/nautilus-link-desktop-file.h	Wed Jan  9 21:31:36 2002
@@ -37,6 +37,8 @@
 									      NautilusLinkType   type);
 gboolean         nautilus_link_desktop_file_local_set_icon                   (const char        *path,
 									      const char        *icon_name);
+gboolean	 nautilus_link_desktop_file_local_set_text		     (const char	*path,
+									      const char	*text);
 char *           nautilus_link_desktop_file_local_get_text                   (const char        *path);
 char *           nautilus_link_desktop_file_local_get_additional_text        (const char        *path);
 NautilusLinkType nautilus_link_desktop_file_local_get_link_type              (const char        *path);


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