diff options
Diffstat (limited to 'thunar/thunar-file.c')
-rw-r--r-- | thunar/thunar-file.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/thunar/thunar-file.c b/thunar/thunar-file.c index 18687ff5..c8ae011f 100644 --- a/thunar/thunar-file.c +++ b/thunar/thunar-file.c @@ -4141,6 +4141,74 @@ thunar_file_destroy (ThunarFile *file) /** + * thunar_file_compare: + * @a : the first #ThunarFile. + * @b : the second #ThunarFile. + * + * Compares items so that directories come before files and ancestors come + * before descendants. + * + * Return value: -1 if @file_a should be sorted before @file_b, 1 if + * @file_b should be sorted before @file_a, 0 if equal. + **/ +gint +thunar_file_compare (ThunarFile *a, + ThunarFile *b) +{ + GFile *file_a; + GFile *file_b; + GFile *parent_a; + GFile *parent_b; + gint ret = 0; + + file_a = thunar_file_get_file (a); + file_b = thunar_file_get_file (b); + + /* check whether the files are equal */ + if (g_file_equal (file_a, file_b)) + return 0; + + /* directories always come first */ + if (thunar_file_get_kind (a) == G_FILE_TYPE_DIRECTORY + && thunar_file_get_kind (b) != G_FILE_TYPE_DIRECTORY) + { + return -1; + } + else if (thunar_file_get_kind (a) != G_FILE_TYPE_DIRECTORY + && thunar_file_get_kind (b) == G_FILE_TYPE_DIRECTORY) + { + return 1; + } + + /* ancestors come first */ + if (g_file_has_prefix (file_b, file_a)) + return -1; + else if (g_file_has_prefix (file_a, file_b)) + return 1; + + parent_a = g_file_get_parent (file_a); + parent_b = g_file_get_parent (file_b); + + if (g_file_equal (parent_a, parent_b)) + { + /* compare siblings by their display name */ + ret = g_utf8_collate (thunar_file_get_display_name (a), + thunar_file_get_display_name (b)); + } + /* again, ancestors come first */ + else if (g_file_has_prefix (file_b, parent_a)) + ret = -1; + else if (g_file_has_prefix (file_a, parent_b)) + ret = 1; + + g_object_unref (parent_a); + g_object_unref (parent_b); + return ret; +} + + + +/** * thunar_file_compare_by_name: * @file_a : the first #ThunarFile. * @file_b : the second #ThunarFile. |