Sort files and directories by name

This commit is contained in:
Přemysl Eric Janouch 2021-11-21 00:21:52 +01:00
parent 8376ae9c4a
commit 6dd0414d0a
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 45 additions and 12 deletions

View File

@ -649,21 +649,30 @@ fastiv_browser_load(FastivBrowser *self, const char *path)
g_array_set_size(self->entries, 0);
g_array_set_size(self->layouted_rows, 0);
// TODO(p): Use opendir(), in order to get file type directly.
GDir *dir = g_dir_open(path, 0, NULL);
if (!dir)
GFile *file = g_file_new_for_path(path);
GFileEnumerator *enumerator = g_file_enumerate_children(file,
G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
g_object_unref(file);
if (!enumerator)
return;
const char *filename;
while ((filename = g_dir_read_name(dir))) {
if (!strcmp(filename, ".") || !strcmp(filename, ".."))
while (TRUE) {
GFileInfo *info = NULL;
GFile *child = NULL;
if (!g_file_enumerator_iterate(enumerator, &info, &child, NULL, NULL) ||
!info)
break;
if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY)
continue;
gchar *subpath = g_build_filename(path, filename, NULL);
g_array_append_val(
self->entries, ((Entry) {.thumbnail = NULL, .filename = subpath}));
// TODO(p): Support being passed filter/sort functions.
g_file_info_get_name(info);
g_array_append_val(self->entries,
((Entry) {.thumbnail = NULL, .filename = g_file_get_path(child)}));
}
g_dir_close(dir);
g_object_unref(enumerator);
GThreadPool *pool = g_thread_pool_new(
entry_add_thumbnail, NULL, g_get_num_processors(), FALSE, NULL);

View File

@ -105,6 +105,27 @@ create_row(GFile *file, const char *icon_name)
return row;
}
static gint
listbox_sort(
GtkListBoxRow *row1, GtkListBoxRow *row2, G_GNUC_UNUSED gpointer user_data)
{
GFile *location1 =
g_object_get_qdata(G_OBJECT(row1), fastiv_sidebar_location_quark());
GFile *location2 =
g_object_get_qdata(G_OBJECT(row2), fastiv_sidebar_location_quark());
if (g_file_has_prefix(location1, location2))
return +1;
if (g_file_has_prefix(location2, location1))
return -1;
gchar *name1 = g_file_get_parse_name(location1);
gchar *name2 = g_file_get_parse_name(location2);
gint result = g_utf8_collate(name1, name2);
g_free(name1);
g_free(name2);
return result;
}
static void
update_location(FastivSidebar *self, GFile *location)
{
@ -143,8 +164,8 @@ update_location(FastivSidebar *self, GFile *location)
if (!enumerator)
return;
// TODO(p): gtk_list_box_set_sort_func(), gtk_list_box_set_filter_func(),
// or even use a model.
// TODO(p): gtk_list_box_set_filter_func(), or even use a model,
// which could be shared with FastivBrowser.
while (TRUE) {
GFileInfo *info = NULL;
GFile *child = NULL;
@ -377,6 +398,9 @@ fastiv_sidebar_init(FastivSidebar *self)
g_signal_connect(self->listbox, "row-activated",
G_CALLBACK(on_open_breadcrumb), self);
gtk_list_box_set_sort_func(
GTK_LIST_BOX(self->listbox), listbox_sort, self, NULL);
GtkWidget *superbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(
GTK_CONTAINER(superbox), GTK_WIDGET(self->places));