[gtk/matthiasc/for-master-2: 4/16] Read bookmarks async
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/matthiasc/for-master-2: 4/16] Read bookmarks async
- Date: Wed, 5 Feb 2020 06:04:00 +0000 (UTC)
commit 1e6171a4a700b0c16b2f705e87b49a2fb9b50d67
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Feb 4 17:54:30 2020 +0100
Read bookmarks async
Do not block the main thread while reading the bookmarks file.
This speeds up initial setup of a GtkPlacesSidebar.
gtk/gtkbookmarksmanager.c | 69 ++++++++++++++++++++++++++++++++++-------------
1 file changed, 51 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtkbookmarksmanager.c b/gtk/gtkbookmarksmanager.c
index 87f1f708a7..a944e91294 100644
--- a/gtk/gtkbookmarksmanager.c
+++ b/gtk/gtkbookmarksmanager.c
@@ -85,17 +85,12 @@ get_bookmarks_file (void)
}
static GSList *
-read_bookmarks (GFile *file)
+parse_bookmarks (const char *contents)
{
- gchar *contents;
gchar **lines, *space;
GSList *bookmarks = NULL;
gint i;
- if (!g_file_load_contents (file, NULL, &contents,
- NULL, NULL, NULL))
- return NULL;
-
lines = g_strsplit (contents, "\n", -1);
for (i = 0; lines[i]; i++)
@@ -122,11 +117,59 @@ read_bookmarks (GFile *file)
bookmarks = g_slist_reverse (bookmarks);
g_strfreev (lines);
+
+ return bookmarks;
+}
+
+static GSList *
+read_bookmarks (GFile *file)
+{
+ gchar *contents;
+ GSList *bookmarks = NULL;
+
+ if (!g_file_load_contents (file, NULL, &contents,
+ NULL, NULL, NULL))
+ return NULL;
+
+ bookmarks = parse_bookmarks (contents);
+
g_free (contents);
return bookmarks;
}
+static void
+notify_changed (GtkBookmarksManager *manager)
+{
+ if (manager->changed_func)
+ manager->changed_func (manager->changed_func_data);
+}
+
+static void
+read_bookmarks_finish (GObject *source,
+ GAsyncResult *result,
+ gpointer data)
+{
+ GFile *file = G_FILE (source);
+ GtkBookmarksManager *manager = data;
+ char *contents = NULL;
+ GError *error = NULL;
+
+ if (!g_file_load_contents_finish (file, result, &contents, NULL, NULL, &error))
+ {
+ g_warning ("Failed to load '%s': %s", g_file_peek_path (file), error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
+ manager->bookmarks = parse_bookmarks (contents);
+
+ g_free (contents);
+
+ notify_changed (manager);
+}
+
static void
save_bookmarks (GFile *bookmarks_file,
GSList *bookmarks)
@@ -181,13 +224,6 @@ save_bookmarks (GFile *bookmarks_file,
g_string_free (contents, TRUE);
}
-static void
-notify_changed (GtkBookmarksManager *manager)
-{
- if (manager->changed_func)
- manager->changed_func (manager->changed_func_data);
-}
-
static void
bookmarks_file_changed (GFileMonitor *monitor,
GFile *file,
@@ -203,9 +239,7 @@ bookmarks_file_changed (GFileMonitor *monitor,
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_DELETED:
- g_slist_free_full (manager->bookmarks, _gtk_bookmark_free);
- manager->bookmarks = read_bookmarks (file);
- notify_changed (manager);
+ g_file_load_contents_async (file, NULL, read_bookmarks_finish, manager);
break;
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
@@ -234,8 +268,7 @@ _gtk_bookmarks_manager_new (GtkBookmarksChangedFunc changed_func, gpointer chang
manager->changed_func_data = changed_func_data;
bookmarks_file = get_bookmarks_file ();
- manager->bookmarks = read_bookmarks (bookmarks_file);
- if (!manager->bookmarks)
+ if (!g_file_query_exists (bookmarks_file, NULL))
{
GFile *legacy_bookmarks_file;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]