Commit bc4ea00e authored by António Fernandes's avatar António Fernandes 🐚 Committed by Carlos Soriano

file: Centralize getting default sort order

nautilus_file_get_default_sort_type() has a misleading name. It doesn't
actually get the default sort type for any folder, but only for certain
special folders.

For regular folders, the default comes from GSettings keys, but this is
implemented in the views instead, with a lot of reduncant code.

So, let's do that in nautilus_file_get_default_sort_type() directly,
and clearly document it.
parent d6effd2d
......@@ -407,21 +407,14 @@ nautilus_canvas_view_get_directory_sort_by (NautilusCanvasView *canvas_view,
static const SortCriterion *
get_default_sort_order (NautilusFile *file)
{
NautilusFileSortType sort_type, default_sort_order;
NautilusFileSortType sort_type;
gboolean reversed;
default_sort_order = g_settings_get_enum (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_ORDER);
reversed = g_settings_get_boolean (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_IN_REVERSE_ORDER);
/* If this is a special folder (e.g. search or recent), override the sort
* order and reversed flag with values appropriate for the folder */
sort_type = nautilus_file_get_default_sort_type (file, &reversed);
if (sort_type == NAUTILUS_FILE_SORT_NONE)
{
sort_type = CLAMP (default_sort_order,
sort_type = CLAMP (sort_type,
NAUTILUS_FILE_SORT_BY_DISPLAY_NAME,
NAUTILUS_FILE_SORT_BY_ATIME);
}
......
......@@ -8813,7 +8813,19 @@ get_attributes_for_default_sort_type (NautilusFile *file,
return retval;
}
/**
* nautilus_file_get_default_sort_type:
* @file: A #NautilusFile representing a location
* @reversed: (out): Location to store whether the order is reversed by default.
*
* Gets which sort order applies by default for the provided locations.
*
* If @file is a location with special needs (e.g. Trash or Recent), the return
* value and @reversed flag are dictated by design. Otherwise, they stem from
* the "default-sort-order" and "default-sort-in-reverse-order" preference keys.
*
* Returns: The default #NautilusFileSortType for this @file.
*/
NautilusFileSortType
nautilus_file_get_default_sort_type (NautilusFile *file,
gboolean *reversed)
......@@ -8821,7 +8833,6 @@ nautilus_file_get_default_sort_type (NautilusFile *file,
NautilusFileSortType retval;
gboolean is_recent, is_download, is_trash, is_search, res;
retval = NAUTILUS_FILE_SORT_NONE;
is_recent = is_download = is_trash = is_search = FALSE;
res = get_attributes_for_default_sort_type (file, &is_recent, &is_download, &is_trash, &is_search);
......@@ -8849,43 +8860,14 @@ nautilus_file_get_default_sort_type (NautilusFile *file,
*reversed = res;
}
}
return retval;
}
const gchar *
nautilus_file_get_default_sort_attribute (NautilusFile *file,
gboolean *reversed)
{
const gchar *retval;
gboolean is_recent, is_download, is_trash, is_search, res;
retval = NULL;
is_download = is_trash = is_search = FALSE;
res = get_attributes_for_default_sort_type (file, &is_recent, &is_download, &is_trash, &is_search);
if (res)
else
{
if (is_recent)
{
retval = g_quark_to_string (attribute_recency_q);
}
else if (is_download)
{
retval = g_quark_to_string (attribute_date_modified_q);
}
else if (is_trash)
{
retval = g_quark_to_string (attribute_trashed_on_q);
}
else if (is_search)
{
retval = g_quark_to_string (attribute_search_relevance_q);
}
retval = g_settings_get_enum (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_ORDER);
if (reversed != NULL)
{
*reversed = res;
*reversed = g_settings_get_boolean (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_IN_REVERSE_ORDER);
}
}
......
......@@ -52,7 +52,8 @@ typedef struct NautilusFile NautilusFile;
typedef enum {
/* These may be set as default-sort-order. When touching this, make sure to
* keep the values in sync with the "org.gnome.nautilus.SortOrder" enum in the
* `data/org.gnome.nautilus.gschema.xml` schemas file.
* `data/org.gnome.nautilus.gschema.xml` schemas file, and the attributes[]
* array in `src/nautilus-list-view.c`.
*/
NAUTILUS_FILE_SORT_NONE = 0, /* Formerly used for "manual" sorting. */
NAUTILUS_FILE_SORT_BY_DISPLAY_NAME = 1,
......@@ -426,8 +427,6 @@ gboolean nautilus_file_is_local_or_fuse (Nautilu
/* Comparing two file objects for sorting */
NautilusFileSortType nautilus_file_get_default_sort_type (NautilusFile *file,
gboolean *reversed);
const gchar * nautilus_file_get_default_sort_attribute (NautilusFile *file,
gboolean *reversed);
int nautilus_file_compare_for_sort (NautilusFile *file_1,
NautilusFile *file_2,
......
......@@ -129,9 +129,11 @@ static const gchar *
get_default_sort_order (NautilusFile *file,
gboolean *reversed)
{
NautilusFileSortType default_sort_order;
gboolean default_sort_reversed;
const gchar *retval;
NautilusFileSortType sort_type;
/* This array makes the #NautilusFileSortType values correspond to the
* respective column attribute.
*/
const char *attributes[] =
{
"name", /* is really "manually" which doesn't apply to lists */
......@@ -140,24 +142,16 @@ get_default_sort_order (NautilusFile *file,
"type",
"date_modified",
"date_accessed",
"starred",
"trashed_on",
"search_relevance",
"recency",
NULL
};
retval = nautilus_file_get_default_sort_attribute (file, reversed);
if (retval == NULL)
{
default_sort_order = g_settings_get_enum (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_ORDER);
default_sort_reversed = g_settings_get_boolean (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_IN_REVERSE_ORDER);
retval = attributes[default_sort_order];
*reversed = default_sort_reversed;
}
sort_type = nautilus_file_get_default_sort_type (file, reversed);
return retval;
return attributes[sort_type];
}
static void
......
......@@ -156,21 +156,13 @@ static const SortConstants *
get_default_sort_order (NautilusFile *file)
{
NautilusFileSortType sort_type;
NautilusFileSortType default_sort_order;
gboolean reversed;
default_sort_order = g_settings_get_enum (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_ORDER);
reversed = g_settings_get_boolean (nautilus_preferences,
NAUTILUS_PREFERENCES_DEFAULT_SORT_IN_REVERSE_ORDER);
/* If this is a special folder (e.g. search or recent), override the sort
* order and reversed flag with values appropriate for the folder */
sort_type = nautilus_file_get_default_sort_type (file, &reversed);
if (sort_type == NAUTILUS_FILE_SORT_NONE)
{
sort_type = CLAMP (default_sort_order,
sort_type = CLAMP (sort_type,
NAUTILUS_FILE_SORT_BY_DISPLAY_NAME,
NAUTILUS_FILE_SORT_BY_ATIME);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment