Read bookmarks async

Do not block the main thread while reading the bookmarks file.
This speeds up initial setup of a GtkPlacesSidebar.
This commit is contained in:
Matthias Clasen 2020-02-04 17:54:30 +01:00
parent 85ad943832
commit 1e6171a4a7

View File

@ -85,17 +85,12 @@ get_bookmarks_file (void)
} }
static GSList * static GSList *
read_bookmarks (GFile *file) parse_bookmarks (const char *contents)
{ {
gchar *contents;
gchar **lines, *space; gchar **lines, *space;
GSList *bookmarks = NULL; GSList *bookmarks = NULL;
gint i; gint i;
if (!g_file_load_contents (file, NULL, &contents,
NULL, NULL, NULL))
return NULL;
lines = g_strsplit (contents, "\n", -1); lines = g_strsplit (contents, "\n", -1);
for (i = 0; lines[i]; i++) for (i = 0; lines[i]; i++)
@ -122,11 +117,59 @@ read_bookmarks (GFile *file)
bookmarks = g_slist_reverse (bookmarks); bookmarks = g_slist_reverse (bookmarks);
g_strfreev (lines); 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); g_free (contents);
return bookmarks; 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 static void
save_bookmarks (GFile *bookmarks_file, save_bookmarks (GFile *bookmarks_file,
GSList *bookmarks) GSList *bookmarks)
@ -181,13 +224,6 @@ save_bookmarks (GFile *bookmarks_file,
g_string_free (contents, TRUE); g_string_free (contents, TRUE);
} }
static void
notify_changed (GtkBookmarksManager *manager)
{
if (manager->changed_func)
manager->changed_func (manager->changed_func_data);
}
static void static void
bookmarks_file_changed (GFileMonitor *monitor, bookmarks_file_changed (GFileMonitor *monitor,
GFile *file, GFile *file,
@ -203,9 +239,7 @@ bookmarks_file_changed (GFileMonitor *monitor,
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_CREATED: case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_DELETED: case G_FILE_MONITOR_EVENT_DELETED:
g_slist_free_full (manager->bookmarks, _gtk_bookmark_free); g_file_load_contents_async (file, NULL, read_bookmarks_finish, manager);
manager->bookmarks = read_bookmarks (file);
notify_changed (manager);
break; break;
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED: 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; manager->changed_func_data = changed_func_data;
bookmarks_file = get_bookmarks_file (); bookmarks_file = get_bookmarks_file ();
manager->bookmarks = read_bookmarks (bookmarks_file); if (!g_file_query_exists (bookmarks_file, NULL))
if (!manager->bookmarks)
{ {
GFile *legacy_bookmarks_file; GFile *legacy_bookmarks_file;