[gedit] Support new location of gtk bookmarks file



commit 54d69eb93c36bf71dc2f3d611a48122dde754237
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Apr 30 16:30:05 2012 +0200

    Support new location of gtk bookmarks file
    
    The gtk bookmarks file is now in XDG dir, so let's first try there and
    otherwise fall back to the old location

 plugins/filebrowser/gedit-file-bookmarks-store.c |  139 +++++++++++++---------
 1 files changed, 83 insertions(+), 56 deletions(-)
---
diff --git a/plugins/filebrowser/gedit-file-bookmarks-store.c b/plugins/filebrowser/gedit-file-bookmarks-store.c
index ce45ad6..bbf601f 100644
--- a/plugins/filebrowser/gedit-file-bookmarks-store.c
+++ b/plugins/filebrowser/gedit-file-bookmarks-store.c
@@ -490,80 +490,107 @@ add_bookmark (GeditFileBookmarksStore *model,
 	return ret;
 }
 
-static void
-init_bookmarks (GeditFileBookmarksStore *model)
+static gchar *
+get_bookmarks_file (void)
+{
+	return g_build_filename (g_get_user_config_dir (), "gtk-3.0", "bookmarks", NULL);
+}
+
+static gchar *
+get_legacy_bookmarks_file (void)
+{
+	return g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
+}
+
+static gboolean
+parse_bookmarks_file (GeditFileBookmarksStore *model,
+		      const gchar             *bookmarks,
+		      gboolean                *added)
 {
-	gchar *bookmarks;
 	GError *error = NULL;
 	gchar *contents;
 	gchar **lines;
 	gchar **line;
-	gboolean added = FALSE;
-
-	/* Read the bookmarks file */
-	bookmarks = g_build_filename (g_get_home_dir (),
-				      ".gtk-bookmarks",
-				      NULL);
 
-	if (g_file_get_contents (bookmarks, &contents, NULL, &error))
+	if (!g_file_get_contents (bookmarks, &contents, NULL, &error))
 	{
-		lines = g_strsplit (contents, "\n", 0);
+		/* The bookmarks file doesn't exist (which is perfectly fine) */
+		g_error_free (error);
+
+		return FALSE;
+	}
+
+	lines = g_strsplit (contents, "\n", 0);
 
-		for (line = lines; *line; ++line)
+	for (line = lines; *line; ++line)
+	{
+		if (**line)
 		{
-			if (**line)
+			GFile *location;
+
+			gchar *pos;
+			gchar *name;
+
+			/* CHECK: is this really utf8? */
+			pos = g_utf8_strchr (*line, -1, ' ');
+
+			if (pos != NULL)
+			{
+				*pos = '\0';
+				name = pos + 1;
+			}
+			else
+			{
+				name = NULL;
+			}
+
+			/* the bookmarks file should contain valid
+			 * URIs, but paranoia is good */
+			location = g_file_new_for_uri (*line);
+			if (gedit_utils_is_valid_location (location))
 			{
-				GFile *location;
-
-				gchar *pos;
-				gchar *name;
-
-				/* CHECK: is this really utf8? */
-				pos = g_utf8_strchr (*line, -1, ' ');
-
-				if (pos != NULL)
-				{
-					*pos = '\0';
-					name = pos + 1;
-				}
-				else
-				{
-					name = NULL;
-				}
-
-				/* the bookmarks file should contain valid
-				 * URIs, but paranoia is good */
-				location = g_file_new_for_uri (*line);
-				if (gedit_utils_is_valid_location (location))
-				{
-					added |= add_bookmark (model, name, *line);
-				}
-				g_object_unref (location);
+				*added |= add_bookmark (model, name, *line);
 			}
+			g_object_unref (location);
 		}
+	}
 
-		g_strfreev (lines);
-		g_free (contents);
+	g_strfreev (lines);
+	g_free (contents);
 
-		/* Add a watch */
-		if (model->priv->bookmarks_monitor == NULL)
-		{
-			GFile *file;
+	/* Add a watch */
+	if (model->priv->bookmarks_monitor == NULL)
+	{
+		GFile *file;
 
-			file = g_file_new_for_path (bookmarks);
-			model->priv->bookmarks_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
-			g_object_unref (file);
+		file = g_file_new_for_path (bookmarks);
+		model->priv->bookmarks_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
+		g_object_unref (file);
 
-			g_signal_connect (model->priv->bookmarks_monitor,
-					  "changed",
-					  G_CALLBACK (on_bookmarks_file_changed),
-					  model);
-		}
+		g_signal_connect (model->priv->bookmarks_monitor,
+				  "changed",
+				  G_CALLBACK (on_bookmarks_file_changed),
+				  model);
 	}
-	else
+
+	return TRUE;
+}
+
+static void
+init_bookmarks (GeditFileBookmarksStore *model)
+{
+	gchar *bookmarks;
+	gboolean added = FALSE;
+
+	bookmarks = get_bookmarks_file ();
+
+	if (!parse_bookmarks_file (model, bookmarks, &added))
 	{
-		/* The bookmarks file doesn't exist (which is perfectly fine) */
-		g_error_free (error);
+		g_free (bookmarks);
+
+		/* try the old location (gtk <= 3.4) */
+		bookmarks = get_legacy_bookmarks_file ();
+		parse_bookmarks_file (model, bookmarks, &added);
 	}
 
 	if (added)



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