Re: [Nautilus-list] icon and metadata questions



Hi Darin,

> 
> These patches both look great! I assume that you need me to apply them, so
> I'll mark your mail message to remind me to get them into the source tree
> soon.

Yes, please do.

> I'd like to understand that feature better. If you do this, can you see the
> "real" name of the desktop file? What happens if you try to rename the
> desktop file inside Nautilus?

When you rename the desktop file inside Nautilus then Nautilus will edit
the file and replace the current Name value with your new value. For
example, if the file is like this:

[Desktop Entry]
Name=My Program
Name[de]=Mein Program

then inside Nautilus the file would show up as "My Program". If you
rename it to "My Cool Program" then my patch would change the Name value
to equal "My Cool Program". It doesn't change the actual file name in
the file system.

My patch takes the current locale into account. So if you are running in
the 'de' locale it will only edit the Name[de] value.

See the attached patch for details. Some comments on what I did:

1. inside nautilus-desktop-file-loader.c I had to move the
nautilus_desktop_file_save and write_all functions so that key_equal
didn't have to be declared forward.

2. I had to use key_equal in the nautilus_desktop_file_save function to
fix a bug that causes the function to change the key for all locales.

3. For some reason I had to declare the set_key_string function forward,
otherwise I got a 'no previous prototype' or some other warning. Not
sure why that is.

Some additional questions:

In nautilus-file.c in the nautius_file_rename function we only check if
the file is gone after doing some other checks. Shouldn't this be the
first thing to check?

> 
> I'm likely to ignore this issue in the crush of just the minimum work
> required to get ported to Gnome 2. Help implementing some solution would be
> welcome.

Maybe I will try something, but there's other things I want do try
first. :)

- 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	Mon Jan  7 14:08:06 2002
+++ libnautilus-private/nautilus-desktop-file-loader.c	Mon Jan  7 17:29:45 2002
@@ -166,116 +166,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)
 {
@@ -483,6 +379,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)
@@ -1162,3 +1164,67 @@
 	
 	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) {
+		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;
+}
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	Mon Jan  7 14:08:07 2002
+++ libnautilus-private/nautilus-desktop-file-loader.h	Mon Jan  7 14:08:33 2002
@@ -78,6 +78,11 @@
 						  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	Mon Jan  7 14:08:10 2002
+++ libnautilus-private/nautilus-file.c	Mon Jan  7 14:11:31 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);
@@ -1018,8 +1015,23 @@
 		}
 	}
 	
-	 /* Make return an error for incoming names containing path separators. */
-	 if (strstr (new_name, "/") != NULL) {
+	/* 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);		
+		
+		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;
+	}
+	
+	/* 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;
 	}
diff -u -r libnautilus-private.original/nautilus-global-preferences.c libnautilus-private/nautilus-global-preferences.c
--- libnautilus-private.original/nautilus-global-preferences.c	Mon Jan  7 14:08:07 2002
+++ libnautilus-private/nautilus-global-preferences.c	Mon Jan  7 14:08:33 2002
@@ -33,6 +33,7 @@
 #include <eel/eel-enumeration.h>
 #include <eel/eel-glib-extensions.h>
 #include <eel/eel-gtk-extensions.h>
+#include <eel/eel-preferences.h>
 #include <eel/eel-smooth-widget.h>
 #include <eel/eel-stock-dialogs.h>
 #include <eel/eel-string.h>
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	Mon Jan  7 14:08:08 2002
+++ libnautilus-private/nautilus-link-desktop-file.c	Mon Jan  7 14:18:48 2002
@@ -49,6 +49,8 @@
 #define NAUTILUS_LINK_MOUNT_TAG 	"FSDevice"
 #define NAUTILUS_LINK_HOME_TAG 		"X-nautilus-home"
 
+gboolean set_key_string (const char *path, const char *keyname,	const char *text, gboolean localize);
+
 static const char *
 get_tag (NautilusLinkType type)
 {
@@ -68,13 +70,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 +94,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,7 +107,7 @@
 	
 	nautilus_desktop_file_free (desktop_file);
 	
-	if (set == FALSE) {
+	if (get == FALSE) {
 		return NULL;
 	}
 
@@ -113,6 +115,53 @@
 }
 
 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) {
+		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,
 					 const char        *image,
@@ -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	Mon Jan  7 14:08:09 2002
+++ libnautilus-private/nautilus-link-desktop-file.h	Mon Jan  7 14:08:33 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]