Use natural sort order

This is exposed in GLib through collate key construction.
This commit is contained in:
Přemysl Eric Janouch 2022-02-20 10:50:08 +01:00
parent fbf26a7d66
commit 6c748439ed
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 9 additions and 12 deletions

View File

@ -2711,6 +2711,7 @@ fiv_io_open_from_data(
typedef struct _ModelEntry { typedef struct _ModelEntry {
gchar *uri; ///< GIO URI gchar *uri; ///< GIO URI
gchar *collate_key; ///< Collate key for the filename
gint64 mtime_msec; ///< Modification time in milliseconds gint64 mtime_msec; ///< Modification time in milliseconds
} ModelEntry; } ModelEntry;
@ -2718,6 +2719,7 @@ static void
model_entry_finalize(ModelEntry *entry) model_entry_finalize(ModelEntry *entry)
{ {
g_free(entry->uri); g_free(entry->uri);
g_free(entry->collate_key);
} }
struct _FivIoModel { struct _FivIoModel {
@ -2778,17 +2780,6 @@ model_supports(FivIoModel *self, const gchar *filename)
return FALSE; return FALSE;
} }
static inline int
model_compare_name(GFile *location1, GFile *location2)
{
gchar *name1 = g_file_get_parse_name(location1);
gchar *name2 = g_file_get_parse_name(location2);
int result = g_utf8_collate(name1, name2);
g_free(name1);
g_free(name2);
return result;
}
static inline int static inline int
model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1, model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1,
const ModelEntry *entry2, GFile *file2) const ModelEntry *entry2, GFile *file2)
@ -2809,7 +2800,7 @@ model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1,
// Fall-through // Fall-through
case FIV_IO_MODEL_SORT_NAME: case FIV_IO_MODEL_SORT_NAME:
case FIV_IO_MODEL_SORT_COUNT: case FIV_IO_MODEL_SORT_COUNT:
result = model_compare_name(file1, file2); result = strcmp(entry1->collate_key, entry2->collate_key);
} }
return self->sort_descending ? -result : +result; return self->sort_descending ? -result : +result;
} }
@ -2870,6 +2861,12 @@ model_reload(FivIoModel *self, GError **error)
g_date_time_unref(mtime); g_date_time_unref(mtime);
} }
gchar *parse_name = g_file_get_parse_name(child);
// TODO(p): Make it possible to use g_utf8_collate_key() instead,
// which does not use natural sorting.
entry.collate_key = g_utf8_collate_key_for_filename(parse_name, -1);
g_free(parse_name);
const char *name = g_file_info_get_name(info); const char *name = g_file_info_get_name(info);
if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY) { if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY) {
entry.uri = g_file_get_uri(child); entry.uri = g_file_get_uri(child);