[nautilus] Migrate accels file to use XDG user config dir



commit f60ee692c98dc88c657343407fb13d43c28830ba
Author: Holger Berndt <hb gnome org>
Date:   Mon May 14 22:10:24 2012 +0200

    Migrate accels file to use XDG user config dir
    
    https://bugzilla.gnome.org/show_bug.cgi?id=646584

 libnautilus-private/nautilus-file-utilities.c |   23 +++--
 libnautilus-private/nautilus-file-utilities.h |    3 +
 libnautilus-private/nautilus-ui-utilities.c   |   35 ++++++++
 libnautilus-private/nautilus-ui-utilities.h   |    3 +
 src/nautilus-application.c                    |  109 ++++++++++++++++++++++++-
 src/nautilus-view.c                           |   46 +----------
 6 files changed, 164 insertions(+), 55 deletions(-)
---
diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c
index 16ec80e..db7c1fb 100644
--- a/libnautilus-private/nautilus-file-utilities.c
+++ b/libnautilus-private/nautilus-file-utilities.c
@@ -121,20 +121,25 @@ nautilus_get_user_directory (void)
  * Get the path for the filename containing nautilus accelerator map.
  * The filename need not exist.
  *
- * Return value: the filename path, or NULL if the home directory could not be found
+ * Return value: the filename path
  **/
 char *
 nautilus_get_accel_map_file (void)
 {
-	const gchar *override;
-
-	override = g_getenv ("GNOME22_USER_DIR");
+	return g_build_filename (g_get_user_config_dir (), "nautilus", "accels", NULL);
+}
 
-	if (override) {
-		return g_build_filename (override, "accels/nautilus", NULL);
-	} else {
-		return g_build_filename (g_get_home_dir (), ".gnome2/accels/nautilus", NULL);
-	}
+/**
+ * nautilus_get_scripts_directory_path:
+ *
+ * Get the path for the directory containing nautilus scripts.
+ *
+ * Return value: the directory path containing nautilus scripts
+ **/
+char *
+nautilus_get_scripts_directory_path (void)
+{
+	return g_build_filename (g_get_user_data_dir (), "nautilus", "scripts", NULL);
 }
 
 typedef struct {
diff --git a/libnautilus-private/nautilus-file-utilities.h b/libnautilus-private/nautilus-file-utilities.h
index 0736ab6..3d1b41c 100644
--- a/libnautilus-private/nautilus-file-utilities.h
+++ b/libnautilus-private/nautilus-file-utilities.h
@@ -70,6 +70,9 @@ char *   nautilus_ensure_unique_file_name            (const char *directory_uri,
 
 GFile *  nautilus_find_existing_uri_in_hierarchy     (GFile *location);
 
+char * nautilus_get_accel_map_file (void);
+char * nautilus_get_scripts_directory_path (void);
+
 GHashTable * nautilus_trashed_files_get_original_directories (GList *files,
 							      GList **unhandled_files);
 void nautilus_restore_files_from_trash (GList *files,
diff --git a/libnautilus-private/nautilus-ui-utilities.c b/libnautilus-private/nautilus-ui-utilities.c
index 8b96b69..69fbdb1 100644
--- a/libnautilus-private/nautilus-ui-utilities.c
+++ b/libnautilus-private/nautilus-ui-utilities.c
@@ -130,3 +130,38 @@ nautilus_ui_get_menu_icon (const char *icon_name)
 
 	return pixbuf;
 }
+
+char *
+nautilus_escape_action_name (const char *action_name,
+			     const char *prefix)
+{
+	GString *s;
+
+	if (action_name == NULL) {
+		return NULL;
+	}
+
+	s = g_string_new (prefix);
+
+	while (*action_name != 0) {
+		switch (*action_name) {
+		case '\\':
+			g_string_append (s, "\\\\");
+			break;
+		case '/':
+			g_string_append (s, "\\s");
+			break;
+		case '&':
+			g_string_append (s, "\\a");
+			break;
+		case '"':
+			g_string_append (s, "\\q");
+			break;
+		default:
+			g_string_append_c (s, *action_name);
+		}
+
+		action_name ++;
+	}
+	return g_string_free (s, FALSE);
+}
diff --git a/libnautilus-private/nautilus-ui-utilities.h b/libnautilus-private/nautilus-ui-utilities.h
index 3aeaa09..642aa72 100644
--- a/libnautilus-private/nautilus-ui-utilities.h
+++ b/libnautilus-private/nautilus-ui-utilities.h
@@ -38,4 +38,7 @@ GtkAction * nautilus_action_from_menu_item         (NautilusMenuItem  *item);
 
 GdkPixbuf * nautilus_ui_get_menu_icon              (const char        *icon_name);
 
+char * nautilus_escape_action_name (const char *action_name, const char *prefix);
+
+
 #endif /* NAUTILUS_UI_UTILITIES_H */
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index f98700f..e746518 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -1137,19 +1137,120 @@ queue_accel_map_save_callback (GtkAccelMap *object, gchar *accel_path,
 }
 
 static void
+update_accel_due_to_scripts_migration (gpointer data, const gchar *accel_path, guint accel_key,
+		GdkModifierType accel_mods, gboolean changed)
+{
+	char *old_scripts_location_esc = data;
+	char *old_scripts_pt;
+
+	old_scripts_pt = strstr (accel_path, old_scripts_location_esc);
+
+	if (old_scripts_pt != NULL) {
+		/* There's a mention of the deprecated scripts directory in the accel. Remove it, and
+		 * add a migrated one. */
+		char *tmp;
+		char *tmp2;
+		GString *new_accel_path;
+
+		/* base part of accel */
+		tmp = g_strndup (accel_path, old_scripts_pt - accel_path);
+		new_accel_path = g_string_new (tmp);
+		g_free (tmp);
+
+		/* new script directory, escaped */
+		tmp = nautilus_get_scripts_directory_path ();
+		tmp2 = nautilus_escape_action_name (tmp, "");
+		g_free (tmp);
+		g_string_append (new_accel_path, tmp2);
+		g_free (tmp2);
+
+		/* script path relative to scripts directory */
+		g_string_append (new_accel_path, old_scripts_pt + strlen (old_scripts_location_esc));
+
+		/* exchange entry */
+		gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
+		gtk_accel_map_change_entry (new_accel_path->str, accel_key, accel_mods, TRUE);
+
+		g_string_free (new_accel_path, TRUE);
+	}
+}
+
+static void
 init_gtk_accels (void)
 {
 	char *accel_map_filename;
+	gboolean performed_migration = FALSE;
+	char *old_scripts_directory_path = NULL;
 
-	/* load accelerator map, and register save callback */
 	accel_map_filename = nautilus_get_accel_map_file ();
-	if (accel_map_filename) {
-		gtk_accel_map_load (accel_map_filename);
-		g_free (accel_map_filename);
+
+	/* If the accel map file doesn't exist, try to migrate from
+	 * former locations. */
+	if (!g_file_test (accel_map_filename, G_FILE_TEST_IS_REGULAR)) {
+		char *old_accel_map_filename;
+		const gchar *override;
+
+		override = g_getenv ("GNOME22_USER_DIR");
+		if (override) {
+			old_accel_map_filename = g_build_filename (override,
+					"accels", "nautilus", NULL);
+			old_scripts_directory_path = g_build_filename (override,
+				       "nautilus-scripts",
+				       NULL);
+		} else {
+			old_accel_map_filename = g_build_filename (g_get_home_dir (),
+					".gnome2", "accels", "nautilus", NULL);
+			old_scripts_directory_path = g_build_filename (g_get_home_dir (),
+								       ".gnome2",
+								       "nautilus-scripts",
+								       NULL);
+		}
+
+		if (g_file_test (old_accel_map_filename, G_FILE_TEST_IS_REGULAR)) {
+			char *parent_dir;
+
+			parent_dir = g_path_get_dirname (accel_map_filename);
+			if (g_mkdir_with_parents (parent_dir, 0700) == 0) {
+				GFile *accel_map_file;
+				GFile *old_accel_map_file;
+
+				accel_map_file = g_file_new_for_path (accel_map_filename);
+				old_accel_map_file = g_file_new_for_path (old_accel_map_filename);
+
+				/* If the move fails, it's safer to not read any accel map
+				 * on startup instead of reading the old one and possibly
+				 * being stuck with it. */
+				performed_migration = g_file_move (old_accel_map_file, accel_map_file, 0, NULL, NULL, NULL, NULL);
+
+				g_object_unref (accel_map_file);
+				g_object_unref (old_accel_map_file);
+			}
+			g_free (parent_dir);
+		}
+
+		g_free (old_accel_map_filename);
+	}
+
+	/* load accelerator map, and register save callback */
+	gtk_accel_map_load (accel_map_filename);
+	g_free (accel_map_filename);
+
+	if (performed_migration) {
+		/* Migrate accels pointing to scripts */
+		char *old_scripts_location_esc;
+
+		old_scripts_location_esc = nautilus_escape_action_name (old_scripts_directory_path, "");
+		gtk_accel_map_foreach (old_scripts_location_esc, update_accel_due_to_scripts_migration);
+		g_free (old_scripts_location_esc);
+		/* save map immediately */
+		save_of_accel_map_requested = TRUE;
+		nautilus_application_save_accel_map (NULL);
 	}
 
 	g_signal_connect (gtk_accel_map_get (), "changed",
 			  G_CALLBACK (queue_accel_map_save_callback), NULL);
+
+	g_free (old_scripts_directory_path);
 }
 
 static void
diff --git a/src/nautilus-view.c b/src/nautilus-view.c
index 2c4d4e9..b9c8b0f 100644
--- a/src/nautilus-view.c
+++ b/src/nautilus-view.c
@@ -2239,10 +2239,7 @@ set_up_scripts_directory_global (void)
 		return TRUE;
 	}
 
-	scripts_directory_path = g_build_filename (g_get_user_data_dir (),
-						   "nautilus",
-						   "scripts",
-						   NULL);
+	scripts_directory_path = nautilus_get_scripts_directory_path ();
 
 	override = g_getenv ("GNOME22_USER_DIR");
 
@@ -4319,41 +4316,6 @@ open_with_launch_application_callback (GtkAction *action,
 }
 
 static char *
-escape_action_name (const char *action_name,
-		    const char *prefix)
-{
-	GString *s;
-
-	if (action_name == NULL) {
-		return NULL;
-	}
-	
-	s = g_string_new (prefix);
-
-	while (*action_name != 0) {
-		switch (*action_name) {
-		case '\\':
-			g_string_append (s, "\\\\");
-			break;
-		case '/':
-			g_string_append (s, "\\s");
-			break;
-		case '&':
-			g_string_append (s, "\\a");
-			break;
-		case '"':
-			g_string_append (s, "\\q");
-			break;
-		default:
-			g_string_append_c (s, *action_name);
-		}
-
-		action_name ++;
-	}
-	return g_string_free (s, FALSE);
-}
-
-static char *
 escape_action_path (const char *action_path)
 {
 	GString *s;
@@ -4402,7 +4364,7 @@ add_submenu (GtkUIManager *ui_manager,
 	GtkAction *action;
 	
 	if (parent_path != NULL) {
-		action_name = escape_action_name (uri, "submenu_");
+		action_name = nautilus_escape_action_name (uri, "submenu_");
 		submenu_name = g_path_get_basename (uri);
 		escaped_submenu_name = escape_action_path (submenu_name);
 		escaped_label = eel_str_double_underscores (label);
@@ -5282,7 +5244,7 @@ add_script_to_scripts_menus (NautilusView *directory_view,
 
 	launch_parameters = script_launch_parameters_new (file, directory_view);
 
-	action_name = escape_action_name (uri, "script_");
+	action_name = nautilus_escape_action_name (uri, "script_");
 	escaped_label = eel_str_double_underscores (name);
 
 	action = gtk_action_new (action_name,
@@ -5531,7 +5493,7 @@ add_template_to_templates_menus (NautilusView *directory_view,
 	uri = nautilus_file_get_uri (file);
 	tip = g_strdup_printf (_("Create a new document from template \"%s\""), name);
 
-	action_name = escape_action_name (uri, "template_");
+	action_name = nautilus_escape_action_name (uri, "template_");
 	escaped_label = eel_str_double_underscores (name);
 	
 	parameters = create_template_parameters_new (file, directory_view);



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