Use the GtkTreeSortable of the filesystem model

Previously, there was a GtkTreeSortModel wrapped around the filesystem
model to make it sortable. As the new implementation implements the
GtkTreeSortable interface, we can use this instead.
This commit is contained in:
Benjamin Otte 2009-06-30 23:17:26 +02:00
parent 3e385ffd07
commit 105e6fbc11
2 changed files with 34 additions and 68 deletions

View File

@ -878,14 +878,11 @@ store_selection_foreach (GtkTreeModel *model,
gpointer data) gpointer data)
{ {
GtkFileChooserDefault *impl; GtkFileChooserDefault *impl;
GtkTreeIter child_iter;
GFile *file; GFile *file;
impl = GTK_FILE_CHOOSER_DEFAULT (data); impl = GTK_FILE_CHOOSER_DEFAULT (data);
gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter); file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
pending_select_files_add (impl, file); pending_select_files_add (impl, file);
} }
@ -949,9 +946,6 @@ gtk_file_chooser_default_finalize (GObject *object)
if (impl->browse_files_model) if (impl->browse_files_model)
g_object_unref (impl->browse_files_model); g_object_unref (impl->browse_files_model);
if (impl->sort_model)
g_object_unref (impl->sort_model);
search_clear_model (impl, FALSE); search_clear_model (impl, FALSE);
recent_clear_model (impl, FALSE); recent_clear_model (impl, FALSE);
@ -5972,6 +5966,9 @@ set_sort_column (GtkFileChooserDefault *impl)
GtkTreeSortable *sortable; GtkTreeSortable *sortable;
sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view))); sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
/* can happen when we're still populating the model */
if (sortable == NULL)
return;
gtk_tree_sortable_set_sort_column_id (sortable, gtk_tree_sortable_set_sort_column_id (sortable,
impl->sort_column, impl->sort_column,
@ -6323,27 +6320,15 @@ load_set_model (GtkFileChooserDefault *impl)
profile_start ("start", NULL); profile_start ("start", NULL);
g_assert (impl->browse_files_model != NULL); g_assert (impl->browse_files_model != NULL);
g_assert (impl->sort_model == NULL);
profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
g_signal_connect (impl->sort_model, "sort-column-changed",
G_CALLBACK (list_sort_column_changed_cb), impl);
profile_msg (" gtk_tree_view_set_model start", NULL); profile_msg (" gtk_tree_view_set_model start", NULL);
gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
GTK_TREE_MODEL (impl->sort_model)); GTK_TREE_MODEL (impl->browse_files_model));
gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view)); gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view), gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
MODEL_COL_NAME); MODEL_COL_NAME);
profile_msg (" gtk_tree_view_set_model end", NULL);
set_sort_column (impl); set_sort_column (impl);
profile_msg (" gtk_tree_view_set_model end", NULL);
impl->list_sort_ascending = TRUE; impl->list_sort_ascending = TRUE;
profile_end ("end", NULL); profile_end ("end", NULL);
@ -6409,12 +6394,12 @@ browse_files_select_first_row (GtkFileChooserDefault *impl)
GtkTreeIter dummy_iter; GtkTreeIter dummy_iter;
GtkTreeModel *tree_model; GtkTreeModel *tree_model;
if (!impl->sort_model)
return;
path = gtk_tree_path_new_from_indices (0, -1); path = gtk_tree_path_new_from_indices (0, -1);
tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)); tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
if (!tree_model)
return;
/* If the list is empty, do nothing. */ /* If the list is empty, do nothing. */
if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path)) if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE); gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
@ -6614,7 +6599,6 @@ pending_select_files_process (GtkFileChooserDefault *impl)
{ {
g_assert (impl->load_state == LOAD_FINISHED); g_assert (impl->load_state == LOAD_FINISHED);
g_assert (impl->browse_files_model != NULL); g_assert (impl->browse_files_model != NULL);
g_assert (impl->sort_model != NULL);
if (impl->pending_select_files) if (impl->pending_select_files)
{ {
@ -6689,12 +6673,6 @@ stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
impl->browse_files_model = NULL; impl->browse_files_model = NULL;
} }
if (impl->sort_model)
{
g_object_unref (impl->sort_model);
impl->sort_model = NULL;
}
gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL); gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
} }
@ -6898,13 +6876,23 @@ set_list_model (GtkFileChooserDefault *impl,
G_TYPE_STRING, G_TYPE_STRING,
PANGO_TYPE_ELLIPSIZE_MODE); PANGO_TYPE_ELLIPSIZE_MODE);
_gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
profile_msg (" set sort function", NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
set_sort_column (impl);
impl->list_sort_ascending = TRUE;
g_signal_connect (impl->browse_files_model, "sort-column-changed",
G_CALLBACK (list_sort_column_changed_cb), impl);
load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */ load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
g_signal_connect (impl->browse_files_model, "finished-loading", g_signal_connect (impl->browse_files_model, "finished-loading",
G_CALLBACK (browse_files_model_finished_loading_cb), impl); G_CALLBACK (browse_files_model_finished_loading_cb), impl);
_gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
install_list_model_filter (impl); install_list_model_filter (impl);
profile_end ("end", NULL); profile_end ("end", NULL);
@ -6984,18 +6972,12 @@ update_chooser_entry (GtkFileChooserDefault *impl)
} }
else if (closure.num_selected == 1) else if (closure.num_selected == 1)
{ {
GtkTreeIter child_iter;
if (impl->operation_mode == OPERATION_MODE_BROWSE) if (impl->operation_mode == OPERATION_MODE_BROWSE)
{ {
GFileInfo *info; GFileInfo *info;
gboolean change_entry; gboolean change_entry;
gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
&child_iter,
&closure.first_selected_iter);
info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
/* If the cursor moved to the row of the newly created folder, /* If the cursor moved to the row of the newly created folder,
* retrieving info will return NULL. * retrieving info will return NULL.
@ -7411,12 +7393,10 @@ select_func (GtkFileSystemModel *model,
{ {
GtkFileChooserDefault *impl = user_data; GtkFileChooserDefault *impl = user_data;
GtkTreeSelection *selection; GtkTreeSelection *selection;
GtkTreeIter sorted_iter;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view)); selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter); gtk_tree_selection_select_iter (selection, iter);
gtk_tree_selection_select_iter (selection, &sorted_iter);
} }
static gboolean static gboolean
@ -7480,21 +7460,18 @@ gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
{ {
GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser); GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view); GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
GtkTreeIter iter, sorted_iter; GtkTreeIter iter;
if (!impl->browse_files_model) if (!impl->browse_files_model)
return; return;
if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model, if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
&iter, &iter,
file)) file))
return; return;
gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model,
&sorted_iter,
&iter);
gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view), gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
&sorted_iter); &iter);
} }
static gboolean static gboolean
@ -7538,7 +7515,7 @@ gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
} }
if (impl->select_multiple) if (impl->select_multiple)
gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model), gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
maybe_select, impl); maybe_select, impl);
} }
@ -7654,13 +7631,11 @@ get_files_foreach (GtkTreeModel *model,
struct get_files_closure *info; struct get_files_closure *info;
GFile *file; GFile *file;
GtkFileSystemModel *fs_model; GtkFileSystemModel *fs_model;
GtkTreeIter sel_iter;
info = data; info = data;
fs_model = info->impl->browse_files_model; fs_model = info->impl->browse_files_model;
gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
file = _gtk_file_system_model_get_file (fs_model, &sel_iter); file = _gtk_file_system_model_get_file (fs_model, iter);
if (!file) if (!file)
return; /* We are on the editable row */ return; /* We are on the editable row */
@ -8206,13 +8181,10 @@ switch_folder_foreach_cb (GtkTreeModel *model,
gpointer data) gpointer data)
{ {
struct switch_folder_closure *closure; struct switch_folder_closure *closure;
GtkTreeIter child_iter;
closure = data; closure = data;
gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter); closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
closure->num_selected++; closure->num_selected++;
} }
@ -8247,7 +8219,7 @@ get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
gboolean *had_selection) gboolean *had_selection)
{ {
GtkTreeSelection *selection; GtkTreeSelection *selection;
GtkTreeIter iter, child_iter; GtkTreeIter iter;
GFileInfo *info; GFileInfo *info;
g_assert (!impl->select_multiple); g_assert (!impl->select_multiple);
@ -8260,11 +8232,7 @@ get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
*had_selection = TRUE; *had_selection = TRUE;
gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
&child_iter,
&iter);
info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
return info; return info;
} }

View File

@ -232,8 +232,6 @@ struct _GtkFileChooserDefault
*/ */
GtkTreeModel *shortcuts_combo_filter_model; GtkTreeModel *shortcuts_combo_filter_model;
GtkTreeModelSort *sort_model;
/* Handles */ /* Handles */
GSList *loading_shortcuts; GSList *loading_shortcuts;
GSList *reload_icon_cancellables; GSList *reload_icon_cancellables;