diff --git a/fiv-browser.c b/fiv-browser.c index 4248417..69f5dff 100644 --- a/fiv-browser.c +++ b/fiv-browser.c @@ -1698,18 +1698,18 @@ on_model_files_changed(FivIoModel *model, FivBrowser *self) if (self->selected) selected_uri = g_strdup(self->selected->uri); - // TODO(p): Later implement arguments. + // TODO(p): Later implement arguments of this FivIoModel signal. + // Or ensure somehow else that thumbnails won't be reloaded unnecessarily. thumbnailers_abort(self); g_array_set_size(self->entries, 0); g_array_set_size(self->layouted_rows, 0); - GPtrArray *files = fiv_io_model_get_files(self->model); - for (guint i = 0; i < files->len; i++) { + gsize len = 0; + const FivIoModelEntry *files = fiv_io_model_get_files(self->model, &len); + for (gsize i = 0; i < len; i++) { g_array_append_val(self->entries, - ((Entry) {.thumbnail = NULL, .uri = files->pdata[i]})); - files->pdata[i] = NULL; + ((Entry) {.thumbnail = NULL, .uri = g_strdup(files[i].uri)})); } - g_ptr_array_free(files, TRUE); fiv_browser_select(self, selected_uri); g_free(selected_uri); diff --git a/fiv-io.c b/fiv-io.c index 1b35a4e..a1ec9f6 100644 --- a/fiv-io.c +++ b/fiv-io.c @@ -2795,14 +2795,8 @@ fiv_io_deserialize(GBytes *bytes) #include -typedef struct { - gchar *uri; ///< GIO URI - gchar *collate_key; ///< Collate key for the filename - gint64 mtime_msec; ///< Modification time in milliseconds -} ModelEntry; - static void -model_entry_finalize(ModelEntry *entry) +model_entry_finalize(FivIoModelEntry *entry) { g_free(entry->uri); g_free(entry->collate_key); @@ -2867,8 +2861,9 @@ model_supports(FivIoModel *self, const gchar *filename) } static inline int -model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1, - const ModelEntry *entry2, GFile *file2) +model_compare_entries(FivIoModel *self, + const FivIoModelEntry *entry1, GFile *file1, + const FivIoModelEntry *entry2, GFile *file2) { if (g_file_has_prefix(file1, file2)) return +1; @@ -2894,8 +2889,8 @@ model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1, static gint model_compare(gconstpointer a, gconstpointer b, gpointer user_data) { - const ModelEntry *entry1 = a; - const ModelEntry *entry2 = b; + const FivIoModelEntry *entry1 = a; + const FivIoModelEntry *entry2 = b; GFile *file1 = g_file_new_for_uri(entry1->uri); GFile *file2 = g_file_new_for_uri(entry2->uri); int result = model_compare_entries(user_data, entry1, file1, entry2, file2); @@ -2939,7 +2934,7 @@ model_reload(FivIoModel *self, GError **error) if (self->filtering && g_file_info_get_is_hidden(info)) continue; - ModelEntry entry = {.uri = g_file_get_uri(child)}; + FivIoModelEntry entry = {.uri = g_file_get_uri(child)}; GDateTime *mtime = g_file_info_get_modification_date_time(info); if (mtime) { entry.mtime_msec = g_date_time_to_unix(mtime) * 1000 + @@ -3076,8 +3071,8 @@ fiv_io_model_init(FivIoModel *self) self->supported_globs = extract_mime_globs((const char **) types); g_strfreev(types); - self->files = g_array_new(FALSE, TRUE, sizeof(ModelEntry)); - self->subdirs = g_array_new(FALSE, TRUE, sizeof(ModelEntry)); + self->files = g_array_new(FALSE, TRUE, sizeof(FivIoModelEntry)); + self->subdirs = g_array_new(FALSE, TRUE, sizeof(FivIoModelEntry)); g_array_set_clear_func( self->subdirs, (GDestroyNotify) model_entry_finalize); g_array_set_clear_func( @@ -3107,24 +3102,18 @@ fiv_io_model_get_location(FivIoModel *self) return self->directory; } -GPtrArray * -fiv_io_model_get_files(FivIoModel *self) +const FivIoModelEntry * +fiv_io_model_get_files(FivIoModel *self, gsize *len) { - GPtrArray *a = g_ptr_array_new_full(self->files->len, g_free); - for (guint i = 0; i < self->files->len; i++) - g_ptr_array_add( - a, g_strdup(g_array_index(self->files, ModelEntry, i).uri)); - return a; + *len = self->files->len; + return (const FivIoModelEntry *) self->files->data; } -GPtrArray * -fiv_io_model_get_subdirectories(FivIoModel *self) +const FivIoModelEntry * +fiv_io_model_get_subdirs(FivIoModel *self, gsize *len) { - GPtrArray *a = g_ptr_array_new_full(self->subdirs->len, g_free); - for (guint i = 0; i < self->subdirs->len; i++) - g_ptr_array_add( - a, g_strdup(g_array_index(self->subdirs, ModelEntry, i).uri)); - return a; + *len = self->subdirs->len; + return (const FivIoModelEntry *) self->subdirs->data; } // --- Export ------------------------------------------------------------------ diff --git a/fiv-io.h b/fiv-io.h index 8586065..ed25d67 100644 --- a/fiv-io.h +++ b/fiv-io.h @@ -122,8 +122,14 @@ gboolean fiv_io_model_open(FivIoModel *self, GFile *directory, GError **error); /// There is no ownership transfer, and the object may be NULL. GFile *fiv_io_model_get_location(FivIoModel *self); -GPtrArray *fiv_io_model_get_files(FivIoModel *self); -GPtrArray *fiv_io_model_get_subdirectories(FivIoModel *self); +typedef struct { + gchar *uri; ///< GIO URI + gchar *collate_key; ///< Collate key for the filename + gint64 mtime_msec; ///< Modification time in milliseconds +} FivIoModelEntry; + +const FivIoModelEntry *fiv_io_model_get_files(FivIoModel *self, gsize *len); +const FivIoModelEntry *fiv_io_model_get_subdirs(FivIoModel *self, gsize *len); // --- Export ------------------------------------------------------------------ diff --git a/fiv-sidebar.c b/fiv-sidebar.c index e4bbd5d..bc96d03 100644 --- a/fiv-sidebar.c +++ b/fiv-sidebar.c @@ -241,14 +241,15 @@ update_location(FivSidebar *self) if ((row = create_row(self, location, "circle-filled-symbolic"))) gtk_container_add(GTK_CONTAINER(self->listbox), row); - GPtrArray *subdirs = fiv_io_model_get_subdirectories(self->model); - for (guint i = 0; i < subdirs->len; i++) { - GFile *file = g_file_new_for_uri(subdirs->pdata[i]); + gsize len = 0; + const FivIoModelEntry *subdirs = + fiv_io_model_get_subdirs(self->model, &len); + for (gsize i = 0; i < len; i++) { + GFile *file = g_file_new_for_uri(subdirs[i].uri); if ((row = create_row(self, file, "go-down-symbolic"))) gtk_container_add(GTK_CONTAINER(self->listbox), row); g_object_unref(file); } - g_ptr_array_free(subdirs, TRUE); } static void diff --git a/fiv.c b/fiv.c index 524070b..acf35ea 100644 --- a/fiv.c +++ b/fiv.c @@ -702,8 +702,13 @@ on_model_files_changed(FivIoModel *model, G_GNUC_UNUSED gpointer user_data) { g_return_if_fail(model == g.model); + gsize len = 0; + const FivIoModelEntry *files = fiv_io_model_get_files(g.model, &len); g_ptr_array_free(g.files, TRUE); - g.files = fiv_io_model_get_files(g.model); + g.files = g_ptr_array_new_full(len, g_free); + for (gsize i = 0; i < len; i++) + g_ptr_array_add(g.files, g_strdup(files[i].uri)); + update_files_index(); gtk_widget_set_sensitive(