1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gtkalignment.h"
22 #include "gtkbutton.h"
23 #include "gtkcellrendererpixbuf.h"
24 #include "gtkcellrendererseptext.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkcombobox.h"
28 #include "gtkfilechooserdefault.h"
29 #include "gtkfilechooserentry.h"
30 #include "gtkfilechooserutils.h"
31 #include "gtkfilechooser.h"
32 #include "gtkfilesystemmodel.h"
35 #include "gtkhpaned.h"
36 #include "gtkicontheme.h"
40 #include "gtkmenuitem.h"
41 #include "gtkmessagedialog.h"
42 #include "gtkpathbar.h"
43 #include "gtkprivate.h"
44 #include "gtkscrolledwindow.h"
45 #include "gtksizegroup.h"
48 #include "gtktreeview.h"
49 #include "gtktreemodelsort.h"
50 #include "gtktreeselection.h"
51 #include "gtktreestore.h"
52 #include "gtktypebuiltins.h"
55 #if defined (G_OS_UNIX)
56 #include "gtkfilesystemunix.h"
57 #elif defined (G_OS_WIN32)
58 #include "gtkfilesystemwin32.h"
64 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
66 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
67 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
68 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
70 struct _GtkFileChooserDefaultClass
72 GtkVBoxClass parent_class;
75 struct _GtkFileChooserDefault
77 GtkVBox parent_instance;
79 GtkFileSystem *file_system;
80 GtkFileSystemModel *tree_model;
81 GtkTreeStore *shortcuts_model;
82 GtkFileSystemModel *list_model;
83 GtkTreeModelSort *sort_model;
85 GtkFileChooserAction action;
87 GtkFileFilter *current_filter;
97 guint volumes_changed_id;
99 guint bookmarks_changed_id;
101 GtkFilePath *current_volume_path;
102 GtkFilePath *current_folder;
103 GtkFilePath *preview_path;
105 GtkWidget *up_button;
106 GtkWidget *new_folder_button;
108 GtkWidget *preview_frame;
110 GtkWidget *filter_combo;
111 GtkWidget *folder_label;
112 GtkWidget *tree_scrollwin;
114 GtkWidget *shortcuts_scrollwin;
115 GtkWidget *shortcuts_tree;
116 GtkWidget *add_bookmark_button;
117 GtkWidget *remove_bookmark_button;
118 GtkWidget *list_scrollwin;
121 GtkWidget *preview_widget;
122 GtkWidget *extra_widget;
125 GtkTreeViewColumn *list_name_column;
126 GtkCellRenderer *list_name_renderer;
128 guint folder_mode : 1;
129 guint local_only : 1;
130 guint preview_widget_active : 1;
131 guint select_multiple : 1;
132 guint show_hidden : 1;
133 guint list_sort_ascending : 1;
134 guint changing_folder : 1;
137 /* Column numbers for the shortcuts tree. Keep these in sync with create_shortcuts_model() */
139 SHORTCUTS_COL_PIXBUF,
142 SHORTCUTS_COL_REMOVABLE,
143 SHORTCUTS_COL_NUM_COLUMNS
146 /* Column numbers for the file list */
151 FILE_LIST_COL_NUM_COLUMNS
154 /* Identifiers for target types */
159 /* Target types for DnD in the shortcuts list */
160 static GtkTargetEntry shortcuts_targets[] = {
161 { "text/uri-list", 0, TEXT_URI_LIST }
164 static const int num_shortcuts_targets = sizeof (shortcuts_targets) / sizeof (shortcuts_targets[0]);
166 /* Interesting places in the shortcuts bar */
176 /* Standard icon size */
177 /* FIXME: maybe this should correspond to the font size in the tree views... */
180 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
181 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
182 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
184 static GObject* gtk_file_chooser_default_constructor (GType type,
185 guint n_construct_properties,
186 GObjectConstructParam *construct_params);
187 static void gtk_file_chooser_default_finalize (GObject *object);
188 static void gtk_file_chooser_default_set_property (GObject *object,
192 static void gtk_file_chooser_default_get_property (GObject *object,
196 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
198 static void gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
199 const GtkFilePath *path);
200 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
201 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
203 static void gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
204 const GtkFilePath *path);
205 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
206 const GtkFilePath *path);
207 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
208 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
209 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
210 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
211 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
212 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
213 GtkFileFilter *filter);
214 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
215 GtkFileFilter *filter);
216 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
217 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
218 const GtkFilePath *path,
220 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
221 const GtkFilePath *path,
223 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
225 static void set_current_filter (GtkFileChooserDefault *impl,
226 GtkFileFilter *filter);
227 static void check_preview_change (GtkFileChooserDefault *impl);
229 static void filter_combo_changed (GtkComboBox *combo_box,
230 GtkFileChooserDefault *impl);
231 static void tree_selection_changed (GtkTreeSelection *tree_selection,
232 GtkFileChooserDefault *impl);
234 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
236 GtkTreeViewColumn *column,
237 GtkFileChooserDefault *impl);
238 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
241 gboolean path_currentlny_selected,
244 static void list_selection_changed (GtkTreeSelection *tree_selection,
245 GtkFileChooserDefault *impl);
246 static void list_row_activated (GtkTreeView *tree_view,
248 GtkTreeViewColumn *column,
249 GtkFileChooserDefault *impl);
250 static void entry_activate (GtkEntry *entry,
251 GtkFileChooserDefault *impl);
253 static void add_bookmark_button_clicked_cb (GtkButton *button,
254 GtkFileChooserDefault *impl);
255 static void remove_bookmark_button_clicked_cb (GtkButton *button,
256 GtkFileChooserDefault *impl);
258 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
259 GtkCellRenderer *cell,
260 GtkTreeModel *tree_model,
263 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
264 GtkCellRenderer *cell,
265 GtkTreeModel *tree_model,
268 static void list_name_data_func (GtkTreeViewColumn *tree_column,
269 GtkCellRenderer *cell,
270 GtkTreeModel *tree_model,
274 static void list_size_data_func (GtkTreeViewColumn *tree_column,
275 GtkCellRenderer *cell,
276 GtkTreeModel *tree_model,
280 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
281 GtkCellRenderer *cell,
282 GtkTreeModel *tree_model,
286 static GObjectClass *parent_class;
289 _gtk_file_chooser_default_get_type (void)
291 static GType file_chooser_default_type = 0;
293 if (!file_chooser_default_type)
295 static const GTypeInfo file_chooser_default_info =
297 sizeof (GtkFileChooserDefaultClass),
298 NULL, /* base_init */
299 NULL, /* base_finalize */
300 (GClassInitFunc) gtk_file_chooser_default_class_init,
301 NULL, /* class_finalize */
302 NULL, /* class_data */
303 sizeof (GtkFileChooserDefault),
305 (GInstanceInitFunc) gtk_file_chooser_default_init,
308 static const GInterfaceInfo file_chooser_info =
310 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
311 NULL, /* interface_finalize */
312 NULL /* interface_data */
315 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
316 &file_chooser_default_info, 0);
317 g_type_add_interface_static (file_chooser_default_type,
318 GTK_TYPE_FILE_CHOOSER,
322 return file_chooser_default_type;
326 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
328 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
329 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
331 parent_class = g_type_class_peek_parent (class);
333 gobject_class->finalize = gtk_file_chooser_default_finalize;
334 gobject_class->constructor = gtk_file_chooser_default_constructor;
335 gobject_class->set_property = gtk_file_chooser_default_set_property;
336 gobject_class->get_property = gtk_file_chooser_default_get_property;
338 widget_class->show_all = gtk_file_chooser_default_show_all;
340 _gtk_file_chooser_install_properties (gobject_class);
344 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
346 iface->select_path = gtk_file_chooser_default_select_path;
347 iface->unselect_path = gtk_file_chooser_default_unselect_path;
348 iface->select_all = gtk_file_chooser_default_select_all;
349 iface->unselect_all = gtk_file_chooser_default_unselect_all;
350 iface->get_paths = gtk_file_chooser_default_get_paths;
351 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
352 iface->get_file_system = gtk_file_chooser_default_get_file_system;
353 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
354 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
355 iface->set_current_name = gtk_file_chooser_default_set_current_name;
356 iface->add_filter = gtk_file_chooser_default_add_filter;
357 iface->remove_filter = gtk_file_chooser_default_remove_filter;
358 iface->list_filters = gtk_file_chooser_default_list_filters;
359 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
360 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
361 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
365 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
367 impl->folder_mode = FALSE;
368 impl->local_only = TRUE;
369 impl->preview_widget_active = TRUE;
370 impl->select_multiple = FALSE;
371 impl->show_hidden = FALSE;
373 gtk_box_set_spacing (GTK_BOX (impl), 12);
377 gtk_file_chooser_default_finalize (GObject *object)
379 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
382 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
383 impl->volumes_changed_id = 0;
384 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
385 impl->bookmarks_changed_id = 0;
386 g_object_unref (impl->file_system);
388 for (l = impl->filters; l; l = l->next)
390 GtkFileFilter *filter;
392 filter = GTK_FILE_FILTER (l->data);
393 g_object_unref (filter);
395 g_slist_free (impl->filters);
397 if (impl->current_filter)
398 g_object_unref (impl->current_filter);
400 if (impl->current_volume_path)
401 gtk_file_path_free (impl->current_volume_path);
403 if (impl->current_folder)
404 gtk_file_path_free (impl->current_folder);
406 if (impl->preview_path)
407 gtk_file_path_free (impl->preview_path);
409 G_OBJECT_CLASS (parent_class)->finalize (object);
412 /* Shows an error dialog */
414 error_message (GtkFileChooserDefault *impl,
420 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
421 if (!GTK_WIDGET_TOPLEVEL (toplevel))
424 dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
425 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
430 gtk_dialog_run (GTK_DIALOG (dialog));
431 gtk_widget_destroy (dialog);
434 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
436 error_dialog (GtkFileChooserDefault *impl,
438 const GtkFilePath *path,
443 text = g_strdup_printf (msg,
444 gtk_file_path_get_string (path),
446 error_message (impl, text);
448 g_error_free (error);
451 /* Displays an error message about not being able to get information for a file.
452 * Frees the GError as well.
455 error_getting_info_dialog (GtkFileChooserDefault *impl,
456 const GtkFilePath *path,
460 _("Could not retrieve information about %s:\n%s"),
464 /* Shows an error dialog about not being able to add a bookmark */
466 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
467 const GtkFilePath *path,
471 _("Could not add a bookmark for %s:\n%s"),
475 /* Shows an error dialog about not being able to compose a filename */
477 error_building_filename_dialog (GtkFileChooserDefault *impl,
478 const GtkFilePath *base_path,
479 const char *file_part,
484 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
485 gtk_file_path_get_string (base_path),
488 error_message (impl, msg);
490 g_error_free (error);
494 update_preview_widget_visibility (GtkFileChooserDefault *impl)
496 if (impl->preview_widget_active && impl->preview_widget)
497 gtk_widget_show (impl->preview_frame);
499 gtk_widget_hide (impl->preview_frame);
503 set_preview_widget (GtkFileChooserDefault *impl,
504 GtkWidget *preview_widget)
506 if (preview_widget == impl->preview_widget)
509 if (impl->preview_widget)
510 gtk_container_remove (GTK_CONTAINER (impl->preview_frame),
511 impl->preview_widget);
513 impl->preview_widget = preview_widget;
514 if (impl->preview_widget)
516 gtk_widget_show_all (impl->preview_widget);
517 gtk_container_add (GTK_CONTAINER (impl->preview_frame),
518 impl->preview_widget);
521 update_preview_widget_visibility (impl);
524 /* Clears the selection in the shortcuts tree */
526 shortcuts_unselect_all (GtkFileChooserDefault *impl)
528 GtkTreeSelection *selection;
530 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
531 gtk_tree_selection_unselect_all (selection);
534 /* Convenience function to get the display name and icon info for a path */
536 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
538 GtkFilePath *parent_path;
539 GtkFileFolder *parent_folder;
542 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
545 parent_folder = gtk_file_system_get_folder (file_system, parent_path,
546 GTK_FILE_INFO_DISPLAY_NAME
550 | GTK_FILE_INFO_IS_FOLDER,
552 gtk_file_path_free (parent_path);
557 info = gtk_file_folder_get_info (parent_folder, path, error);
558 g_object_unref (parent_folder);
563 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
564 * inserts a volume. A position of -1 indicates the end of the tree.
567 shortcuts_insert_path (GtkFileChooserDefault *impl,
570 GtkFileSystemVolume *volume,
571 const GtkFilePath *path,
584 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
585 pixbuf = gtk_file_system_volume_render_icon (impl->file_system,
595 info = get_file_info (impl->file_system, path, error);
599 data = gtk_file_path_copy (path);
602 label_copy = g_strdup (label);
604 label_copy = g_strdup (gtk_file_info_get_display_name (info));
606 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
608 gtk_file_info_free (info);
611 gtk_tree_store_insert (impl->shortcuts_model, &iter, NULL, pos);
613 gtk_tree_store_set (impl->shortcuts_model, &iter,
614 SHORTCUTS_COL_PIXBUF, pixbuf,
615 SHORTCUTS_COL_NAME, label_copy,
616 SHORTCUTS_COL_PATH, data,
617 SHORTCUTS_COL_REMOVABLE, removable,
623 g_object_unref (pixbuf);
628 /* Appends an item for the user's home directory to the shortcuts model */
630 shortcuts_append_home (GtkFileChooserDefault *impl)
633 GtkFilePath *home_path;
636 home = g_get_home_dir ();
637 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
640 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
642 error_getting_info_dialog (impl, home_path, error);
644 gtk_file_path_free (home_path);
647 /* Appends the ~/Desktop directory to the shortcuts model */
649 shortcuts_append_desktop (GtkFileChooserDefault *impl)
654 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
655 path = gtk_file_system_filename_to_path (impl->file_system, name);
658 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, FALSE, NULL);
659 /* We do not actually pop up an error dialog if there is no desktop directory
660 * because some people may really not want to have one.
663 gtk_file_path_free (path);
666 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
668 shortcuts_append_paths (GtkFileChooserDefault *impl,
675 for (; paths; paths = paths->next)
683 /* NULL GError, but we don't really want to show error boxes here */
685 if (shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, TRUE, NULL))
692 /* Returns the index for the corresponding item in the shortcuts bar */
694 shortcuts_get_index (GtkFileChooserDefault *impl,
695 ShortcutsIndex where)
701 if (where == SHORTCUTS_HOME)
704 n += impl->has_home ? 1 : 0;
706 if (where == SHORTCUTS_DESKTOP)
709 n += impl->has_desktop ? 1 : 0;
711 if (where == SHORTCUTS_VOLUMES)
714 n += impl->num_volumes;
716 if (where == SHORTCUTS_SHORTCUTS)
719 n += impl->num_shortcuts;
721 if (where == SHORTCUTS_SEPARATOR)
726 if (where == SHORTCUTS_BOOKMARKS)
729 g_assert_not_reached ();
736 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
738 /* Removes the specified number of rows from the shortcuts list */
740 shortcuts_remove_rows (GtkFileChooserDefault *impl,
743 RemoveFunc remove_fn)
747 path = gtk_tree_path_new_from_indices (start_row, -1);
749 for (; n_rows; n_rows--)
754 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
755 g_assert_not_reached ();
759 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
760 (* remove_fn) (impl, data);
763 gtk_tree_store_remove (impl->shortcuts_model, &iter);
766 gtk_tree_path_free (path);
769 /* Used from shortcuts_remove_rows() */
771 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
773 GtkFileSystemVolume *volume;
776 gtk_file_system_volume_free (impl->file_system, volume);
779 /* Adds all the file system volumes to the shortcuts model */
781 shortcuts_add_volumes (GtkFileChooserDefault *impl)
787 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
788 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
789 impl->num_volumes = 0;
791 list = gtk_file_system_list_volumes (impl->file_system);
795 for (l = list; l; l = l->next)
797 GtkFileSystemVolume *volume;
801 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
805 impl->num_volumes = n;
810 /* Used from shortcuts_remove_rows() */
812 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
817 gtk_file_path_free (path);
820 /* Updates the list of bookmarks */
822 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
826 shortcuts_remove_rows (impl,
827 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS),
831 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
832 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
833 gtk_file_paths_free (bookmarks);
836 /* Appends the bookmarks separator node and the bookmarks from the file system. */
838 shortcuts_append_bookmarks (GtkFileChooserDefault *impl)
842 gtk_tree_store_append (impl->shortcuts_model, &iter, NULL);
843 gtk_tree_store_set (impl->shortcuts_model, &iter,
844 SHORTCUTS_COL_PIXBUF, NULL,
845 SHORTCUTS_COL_NAME, NULL,
846 SHORTCUTS_COL_PATH, NULL,
848 shortcuts_add_bookmarks (impl);
851 /* Creates the GtkTreeStore used as the shortcuts model */
853 create_shortcuts_model (GtkFileChooserDefault *impl)
855 if (impl->shortcuts_model)
856 g_object_unref (impl->shortcuts_model);
858 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
859 impl->shortcuts_model = gtk_tree_store_new (SHORTCUTS_COL_NUM_COLUMNS,
860 GDK_TYPE_PIXBUF, /* pixbuf */
861 G_TYPE_STRING, /* name */
862 G_TYPE_POINTER, /* path or volume */
863 G_TYPE_BOOLEAN); /* removable */
865 if (impl->file_system)
867 shortcuts_append_home (impl);
868 shortcuts_append_desktop (impl);
869 shortcuts_add_volumes (impl);
870 shortcuts_append_bookmarks (impl);
873 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->shortcuts_tree), GTK_TREE_MODEL (impl->shortcuts_model));
876 /* Callback used when the "Up" toolbar button is clicked */
878 up_button_clicked_cb (GtkButton *button,
879 GtkFileChooserDefault *impl)
881 GtkFilePath *parent_path;
885 if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
887 if (parent_path) /* If we were on a root, parent_path will be NULL */
889 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
890 gtk_file_path_free (parent_path);
895 _("Could not go to the parent folder of %s:\n%s"),
896 impl->current_folder,
900 /* Callback used when the "New Folder" toolbar button is clicked */
902 new_folder_button_clicked (GtkButton *button,
903 GtkFileChooserDefault *impl)
908 /* FIXME: this doesn't work for folder mode, just for file mode */
910 _gtk_file_system_model_add_editable (impl->list_model, &iter);
911 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
913 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->list_model), &iter);
914 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->list),
916 impl->list_name_column,
920 /* Callback used from the text cell renderer when the new folder is named */
922 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
924 const gchar *new_text,
925 GtkFileChooserDefault *impl)
928 GtkFilePath *file_path;
930 _gtk_file_system_model_remove_editable (impl->list_model);
931 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
934 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
937 error_building_filename_dialog (impl, impl->current_folder, new_text, error);
942 if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
944 _("Could not create folder %s:\n%s"),
947 gtk_file_path_free (file_path);
949 /* FIXME: scroll to the new folder and select it */
952 /* Callback used from the text cell renderer when the new folder edition gets
956 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
957 GtkFileChooserDefault *impl)
959 _gtk_file_system_model_remove_editable (impl->list_model);
960 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
963 /* Creates the widgets for the filter combo box */
965 filter_create (GtkFileChooserDefault *impl)
967 impl->filter_combo = gtk_combo_box_new_text ();
968 g_signal_connect (impl->filter_combo, "changed",
969 G_CALLBACK (filter_combo_changed), impl);
971 return impl->filter_combo;
975 button_new (GtkFileChooserDefault *impl,
977 const char *stock_id,
986 button = gtk_button_new ();
988 hbox = gtk_hbox_new (FALSE, 2);
989 gtk_container_add (GTK_CONTAINER (button), hbox);
991 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
992 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
994 widget = gtk_label_new (text);
995 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
997 gtk_widget_set_sensitive (button, sensitive);
998 g_signal_connect (button, "clicked", callback, impl);
1000 gtk_widget_show_all (hbox);
1003 gtk_widget_show (button);
1008 /* Creates the widgets for the current folder indicator */
1010 current_folder_create (GtkFileChooserDefault *impl)
1014 hbox = gtk_hbox_new (FALSE, 12);
1015 gtk_widget_show (hbox);
1019 impl->up_button = button_new (impl,
1024 G_CALLBACK (up_button_clicked_cb));
1025 gtk_box_pack_start (GTK_BOX (hbox), impl->up_button, FALSE, FALSE, 0);
1027 /* Current folder label */
1029 impl->folder_label = gtk_label_new (NULL);
1030 gtk_misc_set_alignment (GTK_MISC (impl->folder_label), 0.0, 0.5);
1031 gtk_box_pack_start (GTK_BOX (hbox), impl->folder_label, FALSE, FALSE, 0);
1032 gtk_widget_show (impl->folder_label);
1034 /* New folder button for save mode */
1036 impl->new_folder_button = gtk_button_new_from_stock (GTK_STOCK_NEW);
1037 g_signal_connect (impl->new_folder_button, "clicked",
1038 G_CALLBACK (new_folder_button_clicked), impl);
1039 gtk_box_pack_end (GTK_BOX (hbox), impl->new_folder_button, FALSE, FALSE, 0);
1041 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1042 gtk_widget_show (impl->new_folder_button);
1047 /* Sets the sensitivity of the toolbar buttons */
1049 toolbar_check_sensitivity (GtkFileChooserDefault *impl)
1051 GtkFilePath *parent_path;
1052 gboolean has_parent;
1056 /* I don't think we need to check GError here, do we? */
1057 if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, NULL))
1061 gtk_file_path_free (parent_path);
1066 gtk_widget_set_sensitive (GTK_WIDGET (impl->up_button), has_parent);
1069 /* Creates the widgets for the folder tree */
1071 create_folder_tree (GtkFileChooserDefault *impl)
1073 GtkTreeSelection *selection;
1075 /* Scrolled window */
1077 impl->tree_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1078 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
1079 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1080 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
1082 if (impl->folder_mode)
1083 gtk_widget_show (impl->tree_scrollwin);
1087 impl->tree = gtk_tree_view_new ();
1088 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->tree), FALSE);
1090 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
1091 g_signal_connect (selection, "changed",
1092 G_CALLBACK (tree_selection_changed), impl);
1094 gtk_container_add (GTK_CONTAINER (impl->tree_scrollwin), impl->tree);
1095 gtk_widget_show (impl->tree);
1099 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->tree), 0,
1101 gtk_cell_renderer_text_new (),
1102 tree_name_data_func, impl, NULL);
1103 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->tree),
1104 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1106 return impl->tree_scrollwin;
1109 /* Tries to add a bookmark from a path name */
1111 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1112 const GtkFilePath *path)
1118 info = get_file_info (impl->file_system, path, &error);
1121 error_getting_info_dialog (impl, path, error);
1122 else if (!gtk_file_info_get_is_folder (info))
1126 msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1127 gtk_file_path_get_string (path));
1128 error_message (impl, msg);
1134 if (!gtk_file_system_add_bookmark (impl->file_system, path, &error))
1135 error_could_not_add_bookmark_dialog (impl, path, error);
1139 /* Callback used when the "Add bookmark" button is clicked */
1141 add_bookmark_button_clicked_cb (GtkButton *button,
1142 GtkFileChooserDefault *impl)
1144 shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1147 /* Callback used when the "Remove bookmark" button is clicked */
1149 remove_bookmark_button_clicked_cb (GtkButton *button,
1150 GtkFileChooserDefault *impl)
1152 GtkTreeSelection *selection;
1158 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1160 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1162 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1163 SHORTCUTS_COL_PATH, &path,
1164 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1167 g_assert_not_reached ();
1172 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1174 _("Could not remove bookmark for %s:\n%s"),
1180 /* Sensitize the "add bookmark" button if the current folder is not in the
1181 * bookmarks list, or de-sensitize it otherwise.
1184 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1193 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1197 separator_idx = shortcuts_get_index (impl, SHORTCUTS_SEPARATOR);
1198 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1206 if (i == separator_idx)
1209 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1211 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1213 GtkFileSystemVolume *volume;
1214 GtkFilePath *base_path;
1217 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1219 exists = strcmp (gtk_file_path_get_string (impl->current_folder),
1220 gtk_file_path_get_string (base_path)) == 0;
1232 if (path && gtk_file_path_compare (path, impl->current_folder) == 0)
1239 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
1242 gtk_widget_set_sensitive (impl->add_bookmark_button, !exists);
1245 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1246 * bookmark row is selected in the shortcuts tree.
1249 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1251 GtkTreeSelection *selection;
1253 gboolean removable = FALSE;
1255 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1257 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1258 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1259 SHORTCUTS_COL_REMOVABLE, &removable,
1262 gtk_widget_set_sensitive (impl->remove_bookmark_button, removable);
1265 /* Converts raw selection data from text/uri-list to a list of strings */
1267 split_uris (const char *data)
1270 const char *p, *start;
1276 for (p = start; *p != 0; p++)
1277 if (*p == '\r' && *(p + 1) == '\n')
1281 name = g_strndup (start, p - start);
1282 uris = g_slist_prepend (uris, name);
1288 uris = g_slist_reverse (uris);
1292 /* Callback used when we get the drag data for the bookmarks list. We add the
1293 * received URIs as bookmarks if they are folders.
1296 shortcuts_drag_data_received_cb (GtkWidget *widget,
1297 GdkDragContext *context,
1300 GtkSelectionData *selection_data,
1305 GtkFileChooserDefault *impl;
1308 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1310 uris = split_uris (selection_data->data);
1312 for (l = uris; l; l = l->next)
1318 path = gtk_file_system_uri_to_path (impl->file_system, uri);
1322 shortcuts_add_bookmark_from_path (impl, path);
1323 gtk_file_path_free (path);
1329 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1331 error_message (impl, msg);
1338 g_slist_free (uris);
1341 /* Callback used when the selection in the shortcuts tree changes */
1343 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
1344 GtkFileChooserDefault *impl)
1346 bookmarks_check_remove_sensitivity (impl);
1349 /* Creates the widgets for the shortcuts and bookmarks tree */
1351 shortcuts_tree_create (GtkFileChooserDefault *impl)
1353 GtkTreeSelection *selection;
1354 GtkTreeViewColumn *column;
1355 GtkCellRenderer *renderer;
1357 /* Scrolled window */
1359 impl->shortcuts_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1360 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
1361 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1362 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
1364 gtk_widget_show (impl->shortcuts_scrollwin);
1368 impl->shortcuts_tree = gtk_tree_view_new ();
1369 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->shortcuts_tree), FALSE);
1371 gtk_drag_dest_set (impl->shortcuts_tree,
1372 GTK_DEST_DEFAULT_ALL,
1374 num_shortcuts_targets,
1377 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1378 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1379 gtk_tree_selection_set_select_function (selection,
1380 shortcuts_select_func,
1383 g_signal_connect (selection, "changed",
1384 G_CALLBACK (shortcuts_selection_changed_cb), impl);
1386 g_signal_connect (impl->shortcuts_tree, "row-activated",
1387 G_CALLBACK (shortcuts_row_activated_cb), impl);
1389 g_signal_connect (impl->shortcuts_tree, "drag-data-received",
1390 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1392 gtk_container_add (GTK_CONTAINER (impl->shortcuts_scrollwin), impl->shortcuts_tree);
1393 gtk_widget_show (impl->shortcuts_tree);
1397 create_shortcuts_model (impl);
1401 column = gtk_tree_view_column_new ();
1402 gtk_tree_view_column_set_title (column, _("Folder"));
1404 renderer = gtk_cell_renderer_pixbuf_new ();
1405 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1406 gtk_tree_view_column_set_attributes (column, renderer,
1407 "pixbuf", SHORTCUTS_COL_PIXBUF,
1410 renderer = _gtk_cell_renderer_sep_text_new ();
1411 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1412 gtk_tree_view_column_set_attributes (column, renderer,
1413 "text", SHORTCUTS_COL_NAME,
1416 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->shortcuts_tree), column);
1418 return impl->shortcuts_scrollwin;
1421 /* Creates the widgets for the shortcuts/bookmarks pane */
1423 shortcuts_pane_create (GtkFileChooserDefault *impl)
1429 vbox = gtk_vbox_new (FALSE, 6);
1430 gtk_widget_show (vbox);
1432 /* Shortcuts tree */
1434 widget = shortcuts_tree_create (impl);
1435 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1437 /* Box for buttons */
1439 hbox = gtk_hbox_new (TRUE, 6);
1440 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1441 gtk_widget_show (hbox);
1443 /* Add bookmark button */
1445 impl->add_bookmark_button = button_new (impl,
1450 G_CALLBACK (add_bookmark_button_clicked_cb));
1451 gtk_box_pack_start (GTK_BOX (hbox), impl->add_bookmark_button, TRUE, TRUE, 0);
1453 /* Remove bookmark button */
1455 impl->remove_bookmark_button = button_new (impl,
1460 G_CALLBACK (remove_bookmark_button_clicked_cb));
1461 gtk_box_pack_start (GTK_BOX (hbox), impl->remove_bookmark_button, TRUE, TRUE, 0);
1466 /* Creates the widgets for the file list */
1468 create_file_list (GtkFileChooserDefault *impl)
1470 GtkTreeSelection *selection;
1471 GtkTreeViewColumn *column;
1472 GtkCellRenderer *renderer;
1474 /* Scrolled window */
1476 impl->list_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1477 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
1478 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1479 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
1481 if (!impl->folder_mode)
1482 gtk_widget_show (impl->list_scrollwin);
1484 /* Tree/list view */
1486 impl->list = gtk_tree_view_new ();
1487 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->list), TRUE);
1488 gtk_container_add (GTK_CONTAINER (impl->list_scrollwin), impl->list);
1489 g_signal_connect (impl->list, "row_activated",
1490 G_CALLBACK (list_row_activated), impl);
1491 gtk_widget_show (impl->list);
1493 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1494 g_signal_connect (selection, "changed",
1495 G_CALLBACK (list_selection_changed), impl);
1497 /* Filename column */
1499 impl->list_name_column = gtk_tree_view_column_new ();
1500 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1501 gtk_tree_view_column_set_title (impl->list_name_column, _("File name"));
1502 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1504 renderer = gtk_cell_renderer_pixbuf_new ();
1505 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1506 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1507 list_icon_data_func, impl, NULL);
1509 impl->list_name_renderer = gtk_cell_renderer_text_new ();
1510 g_signal_connect (impl->list_name_renderer, "edited",
1511 G_CALLBACK (renderer_edited_cb), impl);
1512 g_signal_connect (impl->list_name_renderer, "editing-canceled",
1513 G_CALLBACK (renderer_editing_canceled_cb), impl);
1514 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1515 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1516 list_name_data_func, impl, NULL);
1518 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), impl->list_name_column);
1522 column = gtk_tree_view_column_new ();
1523 gtk_tree_view_column_set_title (column, _("Size"));
1525 renderer = gtk_cell_renderer_text_new ();
1526 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1527 gtk_tree_view_column_set_cell_data_func (column, renderer,
1528 list_size_data_func, impl, NULL);
1529 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1530 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
1532 /* Modification time column */
1534 column = gtk_tree_view_column_new ();
1535 gtk_tree_view_column_set_title (column, _("Modified"));
1537 renderer = gtk_cell_renderer_text_new ();
1538 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1539 gtk_tree_view_column_set_cell_data_func (column, renderer,
1540 list_mtime_data_func, impl, NULL);
1541 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1542 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
1544 return impl->list_scrollwin;
1547 /* Creates the widgets for the files/folders pane */
1549 file_pane_create (GtkFileChooserDefault *impl)
1555 vbox = gtk_vbox_new (FALSE, 6);
1556 gtk_widget_show (vbox);
1558 /* Current folder indicator */
1560 widget = current_folder_create (impl);
1561 impl->path_bar = g_object_new (gtk_path_bar_get_type (), NULL);
1562 gtk_widget_show_all (impl->path_bar);
1564 gtk_box_pack_start (GTK_BOX (vbox), impl->path_bar, FALSE, FALSE, 0);
1566 gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
1569 /* Box for lists and preview */
1571 hbox = gtk_hbox_new (FALSE, 12);
1572 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1573 gtk_widget_show (hbox);
1577 widget = create_folder_tree (impl);
1578 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1582 widget = create_file_list (impl);
1583 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1587 impl->preview_frame = gtk_frame_new (_("Preview"));
1588 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_frame, FALSE, FALSE, 0);
1589 /* Don't show preview frame initially */
1595 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1600 hbox = gtk_hbox_new (FALSE, 12);
1601 gtk_widget_show (hbox);
1603 /* Label and entry */
1605 widget = gtk_label_new_with_mnemonic (_("_Filename:"));
1606 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1607 gtk_widget_show (widget);
1609 impl->entry = _gtk_file_chooser_entry_new ();
1610 gtk_entry_set_activates_default (GTK_ENTRY (impl->entry), TRUE);
1611 g_signal_connect (impl->entry, "activate",
1612 G_CALLBACK (entry_activate), impl);
1613 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->entry),
1616 gtk_box_pack_start (GTK_BOX (hbox), impl->entry, TRUE, TRUE, 0);
1617 gtk_widget_show (impl->entry);
1619 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->entry);
1623 widget = filter_create (impl);
1624 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1630 gtk_file_chooser_default_constructor (GType type,
1631 guint n_construct_properties,
1632 GObjectConstructParam *construct_params)
1634 GtkFileChooserDefault *impl;
1639 GtkWidget *entry_widget;
1641 object = parent_class->constructor (type,
1642 n_construct_properties,
1644 impl = GTK_FILE_CHOOSER_DEFAULT (object);
1646 g_assert (impl->file_system);
1648 gtk_widget_push_composite_child ();
1652 hpaned = gtk_hpaned_new ();
1653 gtk_box_pack_start (GTK_BOX (impl), hpaned, TRUE, TRUE, 0);
1654 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
1655 gtk_widget_show (hpaned);
1657 /* Shortcuts pane */
1659 widget = shortcuts_pane_create (impl);
1660 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
1662 /* File/folder pane */
1664 widget = file_pane_create (impl);
1665 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
1667 /* Filename entry and filter combo */
1669 entry_widget = create_filename_entry_and_filter_combo (impl);
1670 gtk_box_pack_start (GTK_BOX (impl), entry_widget, FALSE, FALSE, 0);
1672 /* Make the entry the first widget in the focus chain
1674 focus_chain = g_list_append (NULL, entry_widget);
1675 focus_chain = g_list_append (focus_chain, hpaned);
1676 gtk_container_set_focus_chain (GTK_CONTAINER (impl), focus_chain);
1677 g_list_free (focus_chain);
1679 gtk_widget_pop_composite_child ();
1684 /* Sets the extra_widget by packing it in the appropriate place */
1686 set_extra_widget (GtkFileChooserDefault *impl,
1687 GtkWidget *extra_widget)
1689 if (extra_widget == impl->extra_widget)
1692 if (impl->extra_widget)
1693 gtk_container_remove (GTK_CONTAINER (impl), impl->extra_widget);
1695 impl->extra_widget = extra_widget;
1696 if (impl->extra_widget)
1698 gtk_widget_show_all (impl->extra_widget);
1699 gtk_box_pack_end (GTK_BOX (impl), impl->extra_widget, FALSE, FALSE, 0);
1704 volumes_changed_cb (GtkFileSystem *file_system,
1705 GtkFileChooserDefault *impl)
1707 shortcuts_add_volumes (impl);
1710 /* Callback used when the set of bookmarks changes in the file system */
1712 bookmarks_changed_cb (GtkFileSystem *file_system,
1713 GtkFileChooserDefault *impl)
1715 shortcuts_add_bookmarks (impl);
1717 bookmarks_check_add_sensitivity (impl);
1718 bookmarks_check_remove_sensitivity (impl);
1721 /* Sets the file chooser to multiple selection mode */
1723 set_select_multiple (GtkFileChooserDefault *impl,
1724 gboolean select_multiple,
1725 gboolean property_notify)
1727 GtkTreeSelection *selection;
1728 GtkSelectionMode mode;
1730 if (select_multiple == impl->select_multiple)
1733 impl->select_multiple = select_multiple;
1735 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
1737 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
1738 gtk_tree_selection_set_mode (selection, mode);
1740 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1741 gtk_tree_selection_set_mode (selection, mode);
1743 g_object_notify (G_OBJECT (impl), "select-multiple");
1745 /* FIXME #132255: See note in check_preview_change() */
1746 check_preview_change (impl);
1750 set_file_system_backend (GtkFileChooserDefault *impl,
1751 const char *backend)
1753 if (impl->file_system)
1755 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
1756 impl->volumes_changed_id = 0;
1757 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
1758 impl->bookmarks_changed_id = 0;
1759 g_object_unref (impl->file_system);
1762 impl->file_system = NULL;
1764 impl->file_system = _gtk_file_system_create (backend);
1766 if (!impl->file_system)
1768 #if defined (G_OS_UNIX)
1769 impl->file_system = gtk_file_system_unix_new ();
1770 #elif defined (G_OS_WIN32)
1771 impl->file_system = gtk_file_system_win32_new ();
1773 #error "No default filesystem implementation on the platform"
1777 if (impl->file_system)
1779 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
1780 G_CALLBACK (volumes_changed_cb),
1782 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
1783 G_CALLBACK (bookmarks_changed_cb),
1789 gtk_file_chooser_default_set_property (GObject *object,
1791 const GValue *value,
1795 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
1799 case GTK_FILE_CHOOSER_PROP_ACTION:
1800 impl->action = g_value_get_enum (value);
1801 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1803 gtk_widget_show (impl->new_folder_button);
1805 if (impl->select_multiple)
1807 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
1808 "Re-setting to single selection mode.");
1809 set_select_multiple (impl, FALSE, TRUE);
1813 gtk_widget_hide (impl->new_folder_button);
1816 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
1817 set_file_system_backend (impl, g_value_get_string (value));
1819 case GTK_FILE_CHOOSER_PROP_FILTER:
1820 set_current_filter (impl, g_value_get_object (value));
1822 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
1824 gboolean folder_mode = g_value_get_boolean (value);
1825 if (folder_mode != impl->folder_mode)
1827 impl->folder_mode = folder_mode;
1828 if (impl->folder_mode)
1830 gtk_widget_hide (impl->list_scrollwin);
1831 gtk_widget_show (impl->tree_scrollwin);
1835 gtk_widget_hide (impl->tree_scrollwin);
1836 gtk_widget_show (impl->list_scrollwin);
1841 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1842 impl->local_only = g_value_get_boolean (value);
1844 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1845 set_preview_widget (impl, g_value_get_object (value));
1847 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1848 impl->preview_widget_active = g_value_get_boolean (value);
1849 update_preview_widget_visibility (impl);
1851 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1852 set_extra_widget (impl, g_value_get_object (value));
1854 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1856 gboolean select_multiple = g_value_get_boolean (value);
1857 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
1859 g_warning ("Multiple selection mode is not allowed in Save mode");
1863 set_select_multiple (impl, select_multiple, FALSE);
1866 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1868 gboolean show_hidden = g_value_get_boolean (value);
1869 if (show_hidden != impl->show_hidden)
1871 impl->show_hidden = show_hidden;
1872 _gtk_file_system_model_set_show_hidden (impl->tree_model, show_hidden);
1873 _gtk_file_system_model_set_show_hidden (impl->list_model, show_hidden);
1878 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1884 gtk_file_chooser_default_get_property (GObject *object,
1889 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
1893 case GTK_FILE_CHOOSER_PROP_ACTION:
1894 g_value_set_enum (value, impl->action);
1896 case GTK_FILE_CHOOSER_PROP_FILTER:
1897 g_value_set_object (value, impl->current_filter);
1899 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
1900 g_value_set_boolean (value, impl->folder_mode);
1902 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1903 g_value_set_boolean (value, impl->local_only);
1905 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1906 g_value_set_object (value, impl->preview_widget);
1908 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1909 g_value_set_boolean (value, impl->preview_widget_active);
1911 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1912 g_value_set_object (value, impl->extra_widget);
1914 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1915 g_value_set_boolean (value, impl->select_multiple);
1917 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1918 g_value_set_boolean (value, impl->show_hidden);
1921 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1926 /* We override show-all since we have internal widgets that
1927 * shouldn't be shown when you call show_all(), like the filter
1931 gtk_file_chooser_default_show_all (GtkWidget *widget)
1933 gtk_widget_show (widget);
1937 expand_and_select_func (GtkFileSystemModel *model,
1942 GtkFileChooserDefault *impl = user_data;
1943 GtkTreeView *tree_view;
1945 if (model == impl->tree_model)
1946 tree_view = GTK_TREE_VIEW (impl->tree);
1948 tree_view = GTK_TREE_VIEW (impl->list);
1950 gtk_tree_view_expand_to_path (tree_view, path);
1951 gtk_tree_view_expand_row (tree_view, path, FALSE);
1952 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
1953 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), path, NULL, TRUE, 0.3, 0.5);
1957 list_model_filter_func (GtkFileSystemModel *model,
1959 const GtkFileInfo *file_info,
1962 GtkFileChooserDefault *impl = user_data;
1963 GtkFileFilterInfo filter_info;
1964 GtkFileFilterFlags needed;
1967 if (!impl->current_filter)
1970 if (gtk_file_info_get_is_folder (file_info))
1973 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
1975 needed = gtk_file_filter_get_needed (impl->current_filter);
1977 filter_info.display_name = gtk_file_info_get_display_name (file_info);
1978 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
1980 if (needed & GTK_FILE_FILTER_FILENAME)
1982 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
1983 if (filter_info.filename)
1984 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
1987 filter_info.filename = NULL;
1989 if (needed & GTK_FILE_FILTER_URI)
1991 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
1992 if (filter_info.filename)
1993 filter_info.contains |= GTK_FILE_FILTER_URI;
1996 filter_info.uri = NULL;
1998 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2000 if (filter_info.filename)
2001 g_free ((gchar *)filter_info.filename);
2002 if (filter_info.uri)
2003 g_free ((gchar *)filter_info.uri);
2009 install_list_model_filter (GtkFileChooserDefault *impl)
2011 if (impl->current_filter)
2012 _gtk_file_system_model_set_filter (impl->list_model,
2013 list_model_filter_func,
2017 #define COMPARE_DIRECTORIES \
2018 GtkFileChooserDefault *impl = user_data; \
2019 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->list_model, a); \
2020 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->list_model, b); \
2021 gboolean dir_a, dir_b; \
2024 dir_a = gtk_file_info_get_is_folder (info_a); \
2026 return impl->list_sort_ascending ? -1 : 1; \
2029 dir_b = gtk_file_info_get_is_folder (info_b); \
2031 return impl->list_sort_ascending ? 1 : -1; \
2033 if (dir_a != dir_b) \
2034 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2036 /* Sort callback for the filename column */
2038 name_sort_func (GtkTreeModel *model,
2043 COMPARE_DIRECTORIES;
2045 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2048 /* Sort callback for the size column */
2050 size_sort_func (GtkTreeModel *model,
2055 COMPARE_DIRECTORIES;
2058 gint64 size_a = gtk_file_info_get_size (info_a);
2059 gint64 size_b = gtk_file_info_get_size (info_b);
2061 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2065 /* Sort callback for the mtime column */
2067 mtime_sort_func (GtkTreeModel *model,
2072 COMPARE_DIRECTORIES;
2075 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2076 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2078 return ta > tb ? -1 : (ta == tb ? 0 : 1);
2082 /* Callback used when the sort column changes. We cache the sort order for use
2083 * in name_sort_func().
2086 list_sort_column_changed_cb (GtkTreeSortable *sortable,
2087 GtkFileChooserDefault *impl)
2089 GtkSortType sort_type;
2091 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2092 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2095 /* Gets rid of the old list model and creates a new one for the current folder */
2097 set_list_model (GtkFileChooserDefault *impl)
2099 if (impl->list_model)
2101 g_object_unref (impl->list_model);
2102 g_object_unref (impl->sort_model);
2105 impl->list_model = _gtk_file_system_model_new (impl->file_system,
2106 impl->current_folder, 0,
2108 _gtk_file_system_model_set_show_hidden (impl->list_model, impl->show_hidden);
2109 install_list_model_filter (impl);
2111 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->list_model));
2112 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2113 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2114 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2115 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2116 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2117 impl->list_sort_ascending = TRUE;
2119 g_signal_connect (impl->sort_model, "sort_column_changed",
2120 G_CALLBACK (list_sort_column_changed_cb), impl);
2122 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->list),
2123 GTK_TREE_MODEL (impl->sort_model));
2124 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->list));
2125 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->list),
2126 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2129 /* Gets rid of the old folder tree model and creates a new one for the volume
2130 * corresponding to the specified path.
2133 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2135 GtkFileSystemVolume *volume;
2136 GtkFilePath *base_path, *parent_path;
2140 volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2143 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2145 if (base_path == NULL)
2147 base_path = gtk_file_path_copy (path);
2148 while (gtk_file_system_get_parent (impl->file_system,
2152 parent_path != NULL)
2154 gtk_file_path_free (base_path);
2155 base_path = parent_path;
2159 if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2162 if (impl->tree_model)
2163 g_object_unref (impl->tree_model);
2165 impl->current_volume_path = gtk_file_path_copy (base_path);
2167 impl->tree_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2168 GTK_FILE_INFO_DISPLAY_NAME);
2169 _gtk_file_system_model_set_show_files (impl->tree_model, FALSE);
2170 _gtk_file_system_model_set_show_hidden (impl->tree_model, impl->show_hidden);
2172 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->tree),
2173 GTK_TREE_MODEL (impl->tree_model));
2177 gtk_file_path_free (base_path);
2179 gtk_file_system_volume_free (impl->file_system, volume);
2183 update_chooser_entry (GtkFileChooserDefault *impl)
2185 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2186 const GtkFileInfo *info;
2188 GtkTreeIter child_iter;
2190 /* FIXME #132255: Fixing this for multiple selection involves getting the full
2191 * selection and diffing to find out what the most recently selected file is;
2192 * there is logic in GtkFileSelection that probably can be copied;
2193 * check_preview_change() is similar.
2195 if (impl->select_multiple
2196 || !gtk_tree_selection_get_selected (selection, NULL, &iter))
2199 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2203 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
2205 if (!gtk_file_info_get_is_folder (info))
2206 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry),
2207 gtk_file_info_get_display_name (info));
2211 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
2212 const GtkFilePath *path)
2214 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2217 if (impl->current_folder)
2218 gtk_file_path_free (impl->current_folder);
2220 impl->current_folder = gtk_file_path_copy (path);
2222 /* Change the current folder label */
2224 str = g_strdup_printf (_("Current folder: %s"), gtk_file_path_get_string (path));
2225 gtk_label_set_text (GTK_LABEL (impl->folder_label), str);
2227 gtk_path_bar_set_path (GTK_PATH_BAR (impl->path_bar), gtk_file_path_get_string (path));
2231 /* Update the folder tree */
2233 if (!impl->changing_folder)
2235 impl->changing_folder = TRUE;
2236 set_tree_model (impl, impl->current_folder);
2237 _gtk_file_system_model_path_do (impl->tree_model, path,
2238 expand_and_select_func, impl);
2239 impl->changing_folder = FALSE;
2242 /* Notify the location entry */
2244 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->entry), impl->current_folder);
2246 /* Create a new list model */
2247 set_list_model (impl);
2249 /* Refresh controls */
2251 shortcuts_unselect_all (impl);
2252 toolbar_check_sensitivity (impl);
2254 g_signal_emit_by_name (impl, "current-folder-changed", 0);
2256 update_chooser_entry (impl);
2257 check_preview_change (impl);
2258 bookmarks_check_add_sensitivity (impl);
2260 g_signal_emit_by_name (impl, "selection-changed", 0);
2263 static GtkFilePath *
2264 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2266 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2268 return gtk_file_path_copy (impl->current_folder);
2272 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2275 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2277 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry), name);
2281 select_func (GtkFileSystemModel *model,
2286 GtkFileChooserDefault *impl = user_data;
2287 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
2288 GtkTreePath *sorted_path;
2290 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2291 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2292 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), sorted_path, NULL, TRUE, 0.3, 0.0);
2293 gtk_tree_path_free (sorted_path);
2297 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
2298 const GtkFilePath *path)
2300 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2301 GtkFilePath *parent_path;
2305 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2307 error_getting_info_dialog (impl, path, error);
2313 _gtk_file_chooser_set_current_folder_path (chooser, path);
2317 _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2318 gtk_file_path_free (parent_path);
2319 _gtk_file_system_model_path_do (impl->list_model, path,
2325 unselect_func (GtkFileSystemModel *model,
2330 GtkFileChooserDefault *impl = user_data;
2331 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
2332 GtkTreePath *sorted_path;
2334 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2336 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2338 gtk_tree_path_free (sorted_path);
2342 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
2343 const GtkFilePath *path)
2345 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2347 _gtk_file_system_model_path_do (impl->list_model, path,
2348 unselect_func, impl);
2352 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2354 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2355 if (impl->select_multiple)
2357 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2358 gtk_tree_selection_select_all (selection);
2363 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2365 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2366 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2368 gtk_tree_selection_unselect_all (selection);
2371 struct get_paths_closure {
2372 GtkFileChooserDefault *impl;
2374 GtkFilePath *path_from_entry;
2378 get_paths_foreach (GtkTreeModel *model,
2383 struct get_paths_closure *info;
2384 const GtkFilePath *file_path;
2385 GtkFileSystemModel *fs_model;
2386 GtkTreeIter sel_iter;
2390 if (info->impl->folder_mode)
2392 fs_model = info->impl->tree_model;
2397 fs_model = info->impl->list_model;
2398 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
2401 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
2403 if (!info->path_from_entry
2404 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
2405 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
2409 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
2411 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2412 GtkFileChooserEntry *chooser_entry;
2413 const GtkFilePath *folder_path;
2414 const gchar *file_part;
2415 struct get_paths_closure info;
2419 info.path_from_entry = NULL;
2421 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->entry);
2422 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
2423 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
2425 if (file_part != NULL && file_part[0] != '\0')
2427 GtkFilePath *selected;
2428 GError *error = NULL;
2430 selected = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
2434 error_building_filename_dialog (impl, folder_path, file_part, error);
2438 info.path_from_entry = selected;
2441 if (!info.path_from_entry || impl->select_multiple)
2443 GtkTreeSelection *selection;
2447 if (impl->folder_mode)
2449 if (impl->tree_model)
2450 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
2454 if (impl->sort_model)
2455 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2459 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
2462 if (info.path_from_entry)
2463 info.result = g_slist_prepend (info.result, info.path_from_entry);
2465 return g_slist_reverse (info.result);
2468 static GtkFilePath *
2469 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
2471 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2473 if (impl->preview_path)
2474 return gtk_file_path_copy (impl->preview_path);
2479 static GtkFileSystem *
2480 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
2482 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2484 return impl->file_system;
2487 /* Shows or hides the filter widgets */
2489 toolbar_show_filters (GtkFileChooserDefault *impl,
2493 gtk_widget_show (impl->filter_combo);
2495 gtk_widget_hide (impl->filter_combo);
2499 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
2500 GtkFileFilter *filter)
2502 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2505 if (g_slist_find (impl->filters, filter))
2507 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
2511 g_object_ref (filter);
2512 gtk_object_sink (GTK_OBJECT (filter));
2513 impl->filters = g_slist_append (impl->filters, filter);
2515 name = gtk_file_filter_get_name (filter);
2517 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
2519 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
2521 if (!g_slist_find (impl->filters, impl->current_filter))
2522 set_current_filter (impl, filter);
2524 toolbar_show_filters (impl, TRUE);
2528 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
2529 GtkFileFilter *filter)
2531 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2532 GtkTreeModel *model;
2536 filter_index = g_slist_index (impl->filters, filter);
2538 if (filter_index < 0)
2540 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
2544 impl->filters = g_slist_remove (impl->filters, filter);
2546 if (filter == impl->current_filter)
2549 set_current_filter (impl, impl->filters->data);
2551 set_current_filter (impl, NULL);
2554 /* Remove row from the combo box */
2555 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
2556 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
2557 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
2559 g_object_unref (filter);
2562 toolbar_show_filters (impl, FALSE);
2566 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
2568 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2570 return g_slist_copy (impl->filters);
2573 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
2575 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
2578 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
2582 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
2583 const GtkFilePath *path,
2586 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2590 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
2592 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
2595 impl->num_shortcuts++;
2601 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
2602 const GtkFilePath *path,
2605 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2610 if (impl->num_shortcuts == 0)
2613 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
2614 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
2615 g_assert_not_reached ();
2617 for (i = 0; i < impl->num_shortcuts; i++)
2619 GtkFilePath *shortcut;
2621 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
2622 g_assert (shortcut != NULL);
2624 if (gtk_file_path_compare (shortcut, path) == 0)
2626 /* The other columns are freed by the GtkTreeStore */
2627 gtk_file_path_free (shortcut);
2628 gtk_tree_store_remove (impl->shortcuts_model, &iter);
2629 impl->num_shortcuts--;
2633 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2634 g_assert_not_reached ();
2640 GTK_FILE_CHOOSER_ERROR,
2641 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
2642 _("shortcut %s does not exist"),
2643 gtk_file_path_get_string (path));
2649 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
2651 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2657 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
2658 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
2659 g_assert_not_reached ();
2663 for (i = 0; i < impl->num_shortcuts; i++)
2665 GtkFilePath *shortcut;
2667 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
2668 g_assert (shortcut != NULL);
2670 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
2672 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2673 g_assert_not_reached ();
2676 return g_slist_reverse (list);
2680 set_current_filter (GtkFileChooserDefault *impl,
2681 GtkFileFilter *filter)
2683 if (impl->current_filter != filter)
2687 /* If we have filters, new filter must be one of them
2689 filter_index = g_slist_index (impl->filters, filter);
2690 if (impl->filters && filter_index < 0)
2693 if (impl->current_filter)
2694 g_object_unref (impl->current_filter);
2695 impl->current_filter = filter;
2696 if (impl->current_filter)
2698 g_object_ref (impl->current_filter);
2699 gtk_object_sink (GTK_OBJECT (filter));
2703 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
2706 install_list_model_filter (impl);
2708 g_object_notify (G_OBJECT (impl), "filter");
2713 open_and_close (GtkTreeView *tree_view,
2714 GtkTreePath *target_path)
2716 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2720 path = gtk_tree_path_new ();
2721 gtk_tree_path_append_index (path, 0);
2723 gtk_tree_model_get_iter (model, &iter, path);
2727 if (gtk_tree_path_is_ancestor (path, target_path) ||
2728 gtk_tree_path_compare (path, target_path) == 0)
2730 GtkTreeIter child_iter;
2731 gtk_tree_view_expand_row (tree_view, path, FALSE);
2732 if (gtk_tree_model_iter_children (model, &child_iter, &iter))
2735 gtk_tree_path_down (path);
2740 gtk_tree_view_collapse_row (tree_view, path);
2744 GtkTreeIter parent_iter;
2745 GtkTreeIter next_iter;
2748 if (gtk_tree_model_iter_next (model, &next_iter))
2751 gtk_tree_path_next (path);
2755 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
2759 gtk_tree_path_up (path);
2766 gtk_tree_path_free (path);
2770 filter_combo_changed (GtkComboBox *combo_box,
2771 GtkFileChooserDefault *impl)
2773 gint new_index = gtk_combo_box_get_active (combo_box);
2774 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
2776 set_current_filter (impl, new_filter);
2780 check_preview_change (GtkFileChooserDefault *impl)
2782 const GtkFilePath *new_path = NULL;
2784 /* FIXME #132255: Fixing preview for multiple selection involves getting the
2785 * full selection and diffing to find out what the most recently selected file
2786 * is; there is logic in GtkFileSelection that probably can be
2787 * copied. update_chooser_entry() is similar.
2789 if (impl->sort_model && !impl->select_multiple)
2791 GtkTreeSelection *selection;
2794 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2795 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
2797 GtkTreeIter child_iter;
2799 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2800 &child_iter, &iter);
2802 new_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
2806 if (new_path != impl->preview_path &&
2807 !(new_path && impl->preview_path &&
2808 gtk_file_path_compare (new_path, impl->preview_path) == 0))
2810 if (impl->preview_path)
2811 gtk_file_path_free (impl->preview_path);
2814 impl->preview_path = gtk_file_path_copy (new_path);
2816 impl->preview_path = NULL;
2818 g_signal_emit_by_name (impl, "update-preview");
2823 tree_selection_changed (GtkTreeSelection *selection,
2824 GtkFileChooserDefault *impl)
2827 const GtkFilePath *file_path;
2830 /* FIXME #132255: Fixing this for multiple selection involves getting the full
2831 * selection and diffing to find out what the most recently selected file is;
2832 * there is logic in GtkFileSelection that probably can be copied;
2833 * check_preview_change() is similar.
2835 if (impl->select_multiple
2836 || !gtk_tree_selection_get_selected (selection, NULL, &iter))
2839 file_path = _gtk_file_system_model_get_path (impl->tree_model, &iter);
2840 if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
2843 /* Close the tree up to only the parents of the newly selected
2844 * node and it's immediate children are visible.
2846 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->tree_model), &iter);
2847 open_and_close (GTK_TREE_VIEW (impl->tree), path);
2848 gtk_tree_path_free (path);
2850 if (!impl->changing_folder)
2851 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
2854 /* Activates a volume by mounting it if necessary and then switching to its
2858 shortcuts_activate_volume (GtkFileChooserDefault *impl,
2859 GtkFileSystemVolume *volume)
2863 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
2868 if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
2872 msg = g_strdup_printf ("Could not mount %s:\n%s",
2873 gtk_file_system_volume_get_display_name (impl->file_system, volume),
2875 error_message (impl, msg);
2877 g_error_free (error);
2883 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2884 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
2885 gtk_file_path_free (path);
2888 /* Callback used when a row in the shortcuts list is activated */
2890 shortcuts_row_activated_cb (GtkTreeView *tree_view,
2892 GtkTreeViewColumn *column,
2893 GtkFileChooserDefault *impl)
2896 int selected, start_row;
2899 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
2902 selected = *gtk_tree_path_get_indices (path);
2904 if (selected == shortcuts_get_index (impl, SHORTCUTS_SEPARATOR))
2907 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
2909 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2910 if (selected >= start_row && selected < start_row + impl->num_volumes)
2912 GtkFileSystemVolume *volume;
2915 shortcuts_activate_volume (impl, volume);
2919 GtkFilePath *file_path;
2922 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
2927 shortcuts_select_func (GtkTreeSelection *selection,
2928 GtkTreeModel *model,
2930 gboolean path_currently_selected,
2933 GtkFileChooserDefault *impl = data;
2935 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
2939 list_selection_changed (GtkTreeSelection *selection,
2940 GtkFileChooserDefault *impl)
2942 /* See if we are in the new folder editable row for Save mode */
2943 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2945 GtkTreeSelection *selection;
2946 GtkTreeIter iter, child_iter;
2947 const GtkFileInfo *info;
2949 g_assert (!impl->select_multiple);
2950 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2951 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2954 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2958 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
2960 return; /* We are on the editable row for New Folder */
2963 update_chooser_entry (impl);
2964 check_preview_change (impl);
2966 g_signal_emit_by_name (impl, "selection-changed", 0);
2969 /* Callback used when a row in the file list is activated */
2971 list_row_activated (GtkTreeView *tree_view,
2973 GtkTreeViewColumn *column,
2974 GtkFileChooserDefault *impl)
2976 GtkTreeIter iter, child_iter;
2977 const GtkFileInfo *info;
2979 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
2982 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
2984 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
2986 if (gtk_file_info_get_is_folder (info))
2988 const GtkFilePath *file_path;
2990 file_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
2991 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
2996 g_signal_emit_by_name (impl, "file-activated");
3000 entry_activate (GtkEntry *entry,
3001 GtkFileChooserDefault *impl)
3003 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
3004 const GtkFilePath *folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3005 const gchar *file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3006 GtkFilePath *new_folder = NULL;
3009 return; /* The entry got a nonexistent path */
3011 if (file_part[0] == '\0')
3013 if (gtk_file_path_compare (impl->current_folder, folder_path) != 0)
3014 new_folder = gtk_file_path_copy (folder_path);
3020 GtkFileFolder *folder = NULL;
3021 GtkFilePath *subfolder_path = NULL;
3022 GtkFileInfo *info = NULL;
3025 /* If the file part is non-empty, we need to figure out if it
3026 * refers to a folder within folder. We could optimize the case
3027 * here where the folder is already loaded for one of our tree models.
3031 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
3035 error_getting_info_dialog (impl, folder_path, error);
3040 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
3042 if (!subfolder_path)
3046 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
3047 gtk_file_path_get_string (folder_path),
3050 error_message (impl, msg);
3052 g_object_unref (folder);
3057 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
3061 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3063 g_object_unref (folder);
3064 gtk_file_path_free (subfolder_path);
3067 error_getting_info_dialog (impl, subfolder_path, error);
3068 g_object_unref (folder);
3069 gtk_file_path_free (subfolder_path);
3073 if (gtk_file_info_get_is_folder (info))
3074 new_folder = gtk_file_path_copy (subfolder_path);
3076 g_object_unref (folder);
3077 gtk_file_path_free (subfolder_path);
3078 gtk_file_info_free (info);
3083 g_signal_stop_emission_by_name (entry, "activate");
3085 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), new_folder);
3086 _gtk_file_chooser_entry_set_file_part (chooser_entry, "");
3088 gtk_file_path_free (new_folder);
3092 static const GtkFileInfo *
3093 get_list_file_info (GtkFileChooserDefault *impl,
3096 GtkTreeIter child_iter;
3098 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3102 return _gtk_file_system_model_get_info (impl->list_model, &child_iter);
3106 tree_name_data_func (GtkTreeViewColumn *tree_column,
3107 GtkCellRenderer *cell,
3108 GtkTreeModel *tree_model,
3112 GtkFileChooserDefault *impl = data;
3113 const GtkFileInfo *info = _gtk_file_system_model_get_info (impl->tree_model, iter);
3118 "text", gtk_file_info_get_display_name (info),
3124 list_icon_data_func (GtkTreeViewColumn *tree_column,
3125 GtkCellRenderer *cell,
3126 GtkTreeModel *tree_model,
3130 GtkFileChooserDefault *impl = data;
3131 GtkTreeIter child_iter;
3132 const GtkFilePath *path;
3135 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3138 path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
3142 /* FIXME: NULL GError */
3143 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3149 g_object_unref (pixbuf);
3152 const GtkFileInfo *info = get_list_file_info (impl, iter);
3156 GtkWidget *widget = GTK_TREE_VIEW_COLUMN (tree_column)->tree_view;
3157 GdkPixbuf *pixbuf = gtk_file_info_render_icon (info, widget, ICON_SIZE);
3164 g_object_unref (pixbuf);
3169 /* Sets a cellrenderer's text, making it bold if the GtkFileInfo is a folder */
3171 set_cell_text_bold_if_folder (const GtkFileInfo *info, GtkCellRenderer *cell, const char *text)
3175 "weight", gtk_file_info_get_is_folder (info) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
3180 list_name_data_func (GtkTreeViewColumn *tree_column,
3181 GtkCellRenderer *cell,
3182 GtkTreeModel *tree_model,
3186 GtkFileChooserDefault *impl = data;
3187 const GtkFileInfo *info = get_list_file_info (impl, iter);
3192 "text", _("Type name of new folder"),
3197 set_cell_text_bold_if_folder (info, cell, gtk_file_info_get_display_name (info));
3202 list_size_data_func (GtkTreeViewColumn *tree_column,
3203 GtkCellRenderer *cell,
3204 GtkTreeModel *tree_model,
3208 GtkFileChooserDefault *impl = data;
3209 const GtkFileInfo *info = get_list_file_info (impl, iter);
3213 if (!info || gtk_file_info_get_is_folder (info))
3216 size = gtk_file_info_get_size (info);
3218 if (size < (gint64)1024)
3219 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3220 else if (size < (gint64)1024*1024)
3221 str = g_strdup_printf (_("%.1f K"), size / (1024.));
3222 else if (size < (gint64)1024*1024*1024)
3223 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3225 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3235 /* Tree column data callback for the file list; fetches the mtime of a file */
3237 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3238 GtkCellRenderer *cell,
3239 GtkTreeModel *tree_model,
3243 GtkFileChooserDefault *impl;
3244 const GtkFileInfo *info;
3245 GtkFileTime time_mtime, time_now;
3252 info = get_list_file_info (impl, iter);
3261 time_mtime = gtk_file_info_get_modification_time (info);
3262 g_date_set_time (&mtime, (GTime) time_mtime);
3264 time_now = (GTime ) time (NULL);
3265 g_date_set_time (&now, (GTime) time_now);
3267 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3270 strcpy (buf, _("Today"));
3271 else if (days_diff == 1)
3272 strcpy (buf, _("Yesterday"));
3277 if (days_diff > 1 && days_diff < 7)
3278 format = "%A"; /* Days from last week */
3280 format = _("%d/%b/%Y"); /* Any other date */
3282 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3283 strcpy (buf, _("Unknown"));
3286 set_cell_text_bold_if_folder (info, cell, buf);
3290 _gtk_file_chooser_default_new (const char *file_system)
3292 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3293 "file-system-backend", file_system,