file chooser: Set fixed sort functions for recent and search

This commit is contained in:
Matthias Clasen 2015-07-02 22:35:49 -07:00
parent 0ac17418dd
commit 31a022175f

View File

@ -3585,33 +3585,35 @@ gtk_file_chooser_widget_unmap (GtkWidget *widget)
GTK_WIDGET_CLASS (gtk_file_chooser_widget_parent_class)->unmap (widget); GTK_WIDGET_CLASS (gtk_file_chooser_widget_parent_class)->unmap (widget);
} }
#define COMPARE_DIRECTORIES \
GtkFileChooserWidget *impl = user_data; \
GtkFileChooserWidgetPrivate *priv = impl->priv; \
GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
gboolean dir_a, dir_b; \
\
dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
\
if (priv->sort_directories_first && dir_a != dir_b) \
return priv->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
/* Sort callback for the filename column */
static gint static gint
name_sort_func (GtkTreeModel *model, compare_directory (GtkFileSystemModel *model,
GtkTreeIter *a, GtkTreeIter *a,
GtkTreeIter *b, GtkTreeIter *b,
gpointer user_data) GtkFileChooserWidget *impl)
{ {
COMPARE_DIRECTORIES; GtkFileChooserWidgetPrivate *priv = impl->priv;
else gboolean dir_a, dir_b;
dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (model, a, MODEL_COL_IS_FOLDER));
dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (model, b, MODEL_COL_IS_FOLDER));
if (priv->sort_directories_first && dir_a != dir_b)
return priv->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1);
return 0;
}
static gint
compare_name (GtkFileSystemModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
GtkFileChooserWidget *impl)
{ {
const char *key_a, *key_b; const char *key_a, *key_b;
gint result; gint result;
key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED)); key_a = g_value_get_string (_gtk_file_system_model_get_value (model, a, MODEL_COL_NAME_COLLATED));
key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED)); key_b = g_value_get_string (_gtk_file_system_model_get_value (model, b, MODEL_COL_NAME_COLLATED));
if (key_a && key_b) if (key_a && key_b)
result = strcmp (key_a, key_b); result = strcmp (key_a, key_b);
@ -3624,6 +3626,66 @@ name_sort_func (GtkTreeModel *model,
return result; return result;
} }
static gint
compare_size (GtkFileSystemModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
GtkFileChooserWidget *impl)
{
gint64 size_a, size_b;
size_a = g_value_get_int64 (_gtk_file_system_model_get_value (model, a, MODEL_COL_SIZE));
size_b = g_value_get_int64 (_gtk_file_system_model_get_value (model, b, MODEL_COL_SIZE));
return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
}
static gint
compare_time (GtkFileSystemModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
GtkFileChooserWidget *impl)
{
glong ta, tb;
ta = g_value_get_long (_gtk_file_system_model_get_value (model, a, MODEL_COL_TIME));
tb = g_value_get_long (_gtk_file_system_model_get_value (model, b, MODEL_COL_TIME));
return ta < tb ? -1 : (ta == tb ? 0 : 1);
}
static gint
compare_location (GtkFileSystemModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
GtkFileChooserWidget *impl)
{
const char *key_a, *key_b;
key_a = g_value_get_string (_gtk_file_system_model_get_value (model, a, MODEL_COL_LOCATION_TEXT));
key_b = g_value_get_string (_gtk_file_system_model_get_value (model, b, MODEL_COL_LOCATION_TEXT));
return g_strcmp0 (key_a, key_b);
}
/* Sort callback for the filename column */
static gint
name_sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);
GtkFileChooserWidget *impl = user_data;
gint result;
result = compare_directory (fs_model, a, b, impl);
if (result == 0)
result = compare_name (fs_model, a, b, impl);
return result;
} }
/* Sort callback for the size column */ /* Sort callback for the size column */
@ -3633,16 +3695,16 @@ size_sort_func (GtkTreeModel *model,
GtkTreeIter *b, GtkTreeIter *b,
gpointer user_data) gpointer user_data)
{ {
COMPARE_DIRECTORIES; GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);
else GtkFileChooserWidget *impl = user_data;
{ gint result;
gint64 size_a, size_b;
size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE)); result = compare_directory (fs_model, a, b, impl);
size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1); if (result == 0)
} result = compare_size (fs_model, a, b, impl);
return result;
} }
/* Sort callback for the time column */ /* Sort callback for the time column */
@ -3652,34 +3714,58 @@ time_sort_func (GtkTreeModel *model,
GtkTreeIter *b, GtkTreeIter *b,
gpointer user_data) gpointer user_data)
{ {
COMPARE_DIRECTORIES; GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);
else GtkFileChooserWidget *impl = user_data;
{ gint result;
glong ta, tb;
ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_TIME)); result = compare_directory (fs_model, a, b, impl);
tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_TIME));
return ta < tb ? -1 : (ta == tb ? 0 : 1); if (result == 0)
} result = compare_time (fs_model, a, b, impl);
return result;
} }
static gint static gint
location_sort_func (GtkTreeModel *model, recent_sort_func (GtkTreeModel *model,
GtkTreeIter *a, GtkTreeIter *a,
GtkTreeIter *b, GtkTreeIter *b,
gpointer user_data) gpointer user_data)
{ {
COMPARE_DIRECTORIES; GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);
else GtkFileChooserWidget *impl = user_data;
{ gint result;
const char *key_a, *key_b;
key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_LOCATION_TEXT)); result = compare_time (fs_model, a, b, impl);
key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_LOCATION_TEXT));
return g_strcmp0 (key_a, key_b); if (result == 0)
result = compare_name (fs_model, a, b, impl);
if (result == 0)
result = compare_location (fs_model, a, b, impl);
return result;
} }
static gint
search_sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);
GtkFileChooserWidget *impl = user_data;
gint result;
result = compare_location (fs_model, a, b, impl);
if (result == 0)
result = compare_name (fs_model, a, b, impl);
if (result == 0)
result = compare_time (fs_model, a, b, impl);
return result;
} }
/* Callback used when the sort column changes. We cache the sort order for use /* Callback used when the sort column changes. We cache the sort order for use
@ -4497,7 +4583,6 @@ set_list_model (GtkFileChooserWidget *impl,
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL); gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL); gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_TIME, time_sort_func, impl, NULL); gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_TIME, time_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), MODEL_COL_LOCATION_TEXT, location_sort_func, impl, NULL);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), NULL, NULL, NULL); gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->browse_files_model), NULL, NULL, NULL);
set_sort_column (impl); set_sort_column (impl);
priv->list_sort_ascending = TRUE; priv->list_sort_ascending = TRUE;
@ -6575,24 +6660,8 @@ search_setup_model (GtkFileChooserWidget *impl)
impl, impl,
MODEL_COLUMN_TYPES); MODEL_COLUMN_TYPES);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->search_model),
MODEL_COL_NAME,
name_sort_func,
impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->search_model),
MODEL_COL_TIME,
time_sort_func,
impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->search_model),
MODEL_COL_SIZE,
size_sort_func,
impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->search_model),
MODEL_COL_LOCATION_TEXT,
location_sort_func,
impl, NULL);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->search_model), gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->search_model),
location_sort_func, search_sort_func,
impl, NULL); impl, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->search_model), gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->search_model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
@ -6604,7 +6673,12 @@ search_setup_model (GtkFileChooserWidget *impl)
*/ */
gtk_tree_view_set_model (GTK_TREE_VIEW (priv->browse_files_tree_view), gtk_tree_view_set_model (GTK_TREE_VIEW (priv->browse_files_tree_view),
GTK_TREE_MODEL (priv->search_model)); GTK_TREE_MODEL (priv->search_model));
file_list_set_sort_column_ids (impl);
gtk_tree_view_column_set_sort_column_id (priv->list_name_column, -1);
gtk_tree_view_column_set_sort_column_id (priv->list_time_column, -1);
gtk_tree_view_column_set_sort_column_id (priv->list_size_column, -1);
gtk_tree_view_column_set_sort_column_id (priv->list_location_column, -1);
gtk_tree_view_column_set_visible (priv->list_location_column, TRUE); gtk_tree_view_column_set_visible (priv->list_location_column, TRUE);
gtk_tree_view_column_set_title (priv->list_time_column, _("Modified")); gtk_tree_view_column_set_title (priv->list_time_column, _("Modified"));
gtk_tree_view_columns_autosize (GTK_TREE_VIEW (priv->browse_files_tree_view)); gtk_tree_view_columns_autosize (GTK_TREE_VIEW (priv->browse_files_tree_view));
@ -6782,7 +6856,7 @@ recent_setup_model (GtkFileChooserWidget *impl)
_gtk_file_system_model_set_filter (priv->recent_model, _gtk_file_system_model_set_filter (priv->recent_model,
priv->current_filter); priv->current_filter);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->recent_model), gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (priv->recent_model),
time_sort_func, recent_sort_func,
impl, NULL); impl, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->recent_model), gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->recent_model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,