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 "gdk/gdkkeysyms.h"
22 #include "gtkalignment.h"
23 #include "gtkbindings.h"
24 #include "gtkbutton.h"
25 #include "gtkcellrendererpixbuf.h"
26 #include "gtkcellrendererseptext.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcombobox.h"
30 #include "gtkexpander.h"
31 #include "gtkfilechooserdefault.h"
32 #include "gtkfilechooserentry.h"
33 #include "gtkfilechooserutils.h"
34 #include "gtkfilechooser.h"
35 #include "gtkfilesystemmodel.h"
38 #include "gtkhpaned.h"
39 #include "gtkicontheme.h"
43 #include "gtkmarshalers.h"
44 #include "gtkmenuitem.h"
45 #include "gtkmessagedialog.h"
46 #include "gtkpathbar.h"
47 #include "gtkprivate.h"
48 #include "gtkscrolledwindow.h"
49 #include "gtksizegroup.h"
52 #include "gtktreeview.h"
53 #include "gtktreemodelsort.h"
54 #include "gtktreeselection.h"
55 #include "gtktreestore.h"
56 #include "gtktypebuiltins.h"
59 #if defined (G_OS_UNIX)
60 #include "gtkfilesystemunix.h"
61 #elif defined (G_OS_WIN32)
62 #include "gtkfilesystemwin32.h"
68 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
70 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
71 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
72 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
74 struct _GtkFileChooserDefaultClass
76 GtkVBoxClass parent_class;
79 struct _GtkFileChooserDefault
81 GtkVBox parent_instance;
83 GtkFileChooserAction action;
85 GtkFileSystem *file_system;
87 /* Save mode widgets */
88 GtkWidget *save_widgets;
90 GtkWidget *save_file_name_entry;
91 GtkWidget *save_folder_label;
92 GtkWidget *save_folder_combo;
93 GtkWidget *save_extra_align;
94 GtkWidget *save_expander;
96 /* The file browsing widgets */
97 GtkWidget *browse_widgets;
98 GtkWidget *browse_shortcuts_tree_view;
99 GtkWidget *browse_shortcuts_swin;
100 GtkWidget *browse_shortcuts_add_button;
101 GtkWidget *browse_shortcuts_remove_button;
102 GtkWidget *browse_files_swin;
103 GtkWidget *browse_files_tree_view;
104 GtkWidget *browse_directories_swin;
105 GtkWidget *browse_directories_tree_view;
106 GtkWidget *browse_new_folder_button;
107 GtkWidget *browse_path_bar;
108 GtkWidget *browse_extra_align;
109 GtkTreeModel *browse_shortcuts_model;
110 GtkFileSystemModel *browse_files_model;
111 GtkFileSystemModel *browse_directories_model;
113 GtkWidget *filter_combo;
114 GtkWidget *preview_widget;
115 GtkWidget *extra_widget;
117 GtkListStore *shortcuts_model;
118 GtkTreeModelSort *sort_model;
120 GtkFileFilter *current_filter;
124 gboolean has_desktop;
130 guint volumes_changed_id;
131 guint bookmarks_changed_id;
133 GtkFilePath *current_volume_path;
134 GtkFilePath *current_folder;
135 GtkFilePath *preview_path;
137 GtkWidget *preview_frame;
139 GtkTreeViewColumn *list_name_column;
140 GtkCellRenderer *list_name_renderer;
145 guint folder_mode : 1;
146 guint local_only : 1;
147 guint preview_widget_active : 1;
148 guint select_multiple : 1;
149 guint show_hidden : 1;
150 guint list_sort_ascending : 1;
151 guint changing_folder : 1;
162 static guint signals[LAST_SIGNAL] = { 0 };
164 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
166 SHORTCUTS_COL_PIXBUF,
169 SHORTCUTS_COL_REMOVABLE,
170 SHORTCUTS_COL_PIXBUF_VISIBLE,
171 SHORTCUTS_COL_NUM_COLUMNS
174 /* Column numbers for the file list */
179 FILE_LIST_COL_NUM_COLUMNS
182 /* Identifiers for target types */
187 /* Target types for DnD in the shortcuts list */
188 static GtkTargetEntry shortcuts_targets[] = {
189 { "text/uri-list", 0, TEXT_URI_LIST }
192 static const int num_shortcuts_targets = sizeof (shortcuts_targets) / sizeof (shortcuts_targets[0]);
194 /* Interesting places in the shortcuts bar */
204 /* Standard icon size */
205 /* FIXME: maybe this should correspond to the font size in the tree views... */
208 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
209 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
210 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
212 static GObject* gtk_file_chooser_default_constructor (GType type,
213 guint n_construct_properties,
214 GObjectConstructParam *construct_params);
215 static void gtk_file_chooser_default_finalize (GObject *object);
216 static void gtk_file_chooser_default_set_property (GObject *object,
220 static void gtk_file_chooser_default_get_property (GObject *object,
224 static void gtk_file_chooser_default_dispose (GObject *object);
225 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
227 static void gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
228 const GtkFilePath *path);
229 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
230 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
232 static void gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
233 const GtkFilePath *path);
234 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
235 const GtkFilePath *path);
236 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
237 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
238 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
239 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
240 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
241 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
242 GtkFileFilter *filter);
243 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
244 GtkFileFilter *filter);
245 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
246 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
247 const GtkFilePath *path,
249 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
250 const GtkFilePath *path,
252 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
254 static void location_popup_handler (GtkFileChooserDefault *impl);
255 static void up_folder_handler (GtkFileChooserDefault *impl);
256 static void home_folder_handler (GtkFileChooserDefault *impl);
257 static void update_appearance (GtkFileChooserDefault *impl);
259 static void set_current_filter (GtkFileChooserDefault *impl,
260 GtkFileFilter *filter);
261 static void check_preview_change (GtkFileChooserDefault *impl);
263 static void filter_combo_changed (GtkComboBox *combo_box,
264 GtkFileChooserDefault *impl);
265 static void tree_selection_changed (GtkTreeSelection *tree_selection,
266 GtkFileChooserDefault *impl);
268 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
270 GtkTreeViewColumn *column,
271 GtkFileChooserDefault *impl);
272 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
275 gboolean path_currently_selected,
278 static void list_selection_changed (GtkTreeSelection *tree_selection,
279 GtkFileChooserDefault *impl);
280 static void list_row_activated (GtkTreeView *tree_view,
282 GtkTreeViewColumn *column,
283 GtkFileChooserDefault *impl);
285 static void path_bar_clicked (GtkPathBar *path_bar,
286 GtkFilePath *file_path,
287 GtkFileChooserDefault *impl);
289 static void add_bookmark_button_clicked_cb (GtkButton *button,
290 GtkFileChooserDefault *impl);
291 static void remove_bookmark_button_clicked_cb (GtkButton *button,
292 GtkFileChooserDefault *impl);
294 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
295 GtkCellRenderer *cell,
296 GtkTreeModel *tree_model,
299 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
300 GtkCellRenderer *cell,
301 GtkTreeModel *tree_model,
304 static void list_name_data_func (GtkTreeViewColumn *tree_column,
305 GtkCellRenderer *cell,
306 GtkTreeModel *tree_model,
310 static void list_size_data_func (GtkTreeViewColumn *tree_column,
311 GtkCellRenderer *cell,
312 GtkTreeModel *tree_model,
316 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
317 GtkCellRenderer *cell,
318 GtkTreeModel *tree_model,
322 static GObjectClass *parent_class;
325 _gtk_file_chooser_default_get_type (void)
327 static GType file_chooser_default_type = 0;
329 if (!file_chooser_default_type)
331 static const GTypeInfo file_chooser_default_info =
333 sizeof (GtkFileChooserDefaultClass),
334 NULL, /* base_init */
335 NULL, /* base_finalize */
336 (GClassInitFunc) gtk_file_chooser_default_class_init,
337 NULL, /* class_finalize */
338 NULL, /* class_data */
339 sizeof (GtkFileChooserDefault),
341 (GInstanceInitFunc) gtk_file_chooser_default_init,
344 static const GInterfaceInfo file_chooser_info =
346 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
347 NULL, /* interface_finalize */
348 NULL /* interface_data */
351 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
352 &file_chooser_default_info, 0);
353 g_type_add_interface_static (file_chooser_default_type,
354 GTK_TYPE_FILE_CHOOSER,
358 return file_chooser_default_type;
362 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
364 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
365 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
366 GtkBindingSet *binding_set;
368 parent_class = g_type_class_peek_parent (class);
370 gobject_class->finalize = gtk_file_chooser_default_finalize;
371 gobject_class->constructor = gtk_file_chooser_default_constructor;
372 gobject_class->set_property = gtk_file_chooser_default_set_property;
373 gobject_class->get_property = gtk_file_chooser_default_get_property;
374 gobject_class->dispose = gtk_file_chooser_default_dispose;
376 widget_class->show_all = gtk_file_chooser_default_show_all;
378 signals[LOCATION_POPUP] =
379 _gtk_binding_signal_new ("location-popup",
380 G_OBJECT_CLASS_TYPE (class),
381 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
382 G_CALLBACK (location_popup_handler),
384 _gtk_marshal_VOID__VOID,
387 _gtk_binding_signal_new ("up-folder",
388 G_OBJECT_CLASS_TYPE (class),
389 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
390 G_CALLBACK (up_folder_handler),
392 _gtk_marshal_VOID__VOID,
394 signals[HOME_FOLDER] =
395 _gtk_binding_signal_new ("home-folder",
396 G_OBJECT_CLASS_TYPE (class),
397 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
398 G_CALLBACK (home_folder_handler),
400 _gtk_marshal_VOID__VOID,
403 binding_set = gtk_binding_set_by_class (class);
405 gtk_binding_entry_add_signal (binding_set,
406 GDK_l, GDK_CONTROL_MASK,
410 gtk_binding_entry_add_signal (binding_set,
411 GDK_Up, GDK_MOD1_MASK,
414 gtk_binding_entry_add_signal (binding_set,
415 GDK_KP_Up, GDK_MOD1_MASK,
419 gtk_binding_entry_add_signal (binding_set,
420 GDK_Home, GDK_MOD1_MASK,
423 gtk_binding_entry_add_signal (binding_set,
424 GDK_KP_Home, GDK_MOD1_MASK,
428 _gtk_file_chooser_install_properties (gobject_class);
432 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
434 iface->select_path = gtk_file_chooser_default_select_path;
435 iface->unselect_path = gtk_file_chooser_default_unselect_path;
436 iface->select_all = gtk_file_chooser_default_select_all;
437 iface->unselect_all = gtk_file_chooser_default_unselect_all;
438 iface->get_paths = gtk_file_chooser_default_get_paths;
439 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
440 iface->get_file_system = gtk_file_chooser_default_get_file_system;
441 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
442 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
443 iface->set_current_name = gtk_file_chooser_default_set_current_name;
444 iface->add_filter = gtk_file_chooser_default_add_filter;
445 iface->remove_filter = gtk_file_chooser_default_remove_filter;
446 iface->list_filters = gtk_file_chooser_default_list_filters;
447 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
448 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
449 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
453 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
455 impl->folder_mode = FALSE;
456 impl->local_only = TRUE;
457 impl->preview_widget_active = TRUE;
458 impl->select_multiple = FALSE;
459 impl->show_hidden = FALSE;
461 gtk_box_set_spacing (GTK_BOX (impl), 12);
465 gtk_file_chooser_default_finalize (GObject *object)
467 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
470 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
471 impl->volumes_changed_id = 0;
472 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
473 impl->bookmarks_changed_id = 0;
474 g_object_unref (impl->file_system);
476 for (l = impl->filters; l; l = l->next)
478 GtkFileFilter *filter;
480 filter = GTK_FILE_FILTER (l->data);
481 g_object_unref (filter);
483 g_slist_free (impl->filters);
485 if (impl->current_filter)
486 g_object_unref (impl->current_filter);
488 if (impl->current_volume_path)
489 gtk_file_path_free (impl->current_volume_path);
491 if (impl->current_folder)
492 gtk_file_path_free (impl->current_folder);
494 if (impl->preview_path)
495 gtk_file_path_free (impl->preview_path);
497 G_OBJECT_CLASS (parent_class)->finalize (object);
500 /* Shows an error dialog set as transient for the specified window */
502 error_message_with_parent (GtkWindow *parent,
507 dialog = gtk_message_dialog_new (parent,
508 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
513 gtk_dialog_run (GTK_DIALOG (dialog));
514 gtk_widget_destroy (dialog);
517 /* Shows an error dialog for the file chooser */
519 error_message (GtkFileChooserDefault *impl,
524 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
525 if (!GTK_WIDGET_TOPLEVEL (toplevel))
528 error_message_with_parent (toplevel ? GTK_WINDOW (toplevel) : NULL,
532 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
534 error_dialog (GtkFileChooserDefault *impl,
536 const GtkFilePath *path,
541 text = g_strdup_printf (msg,
542 gtk_file_path_get_string (path),
544 error_message (impl, text);
546 g_error_free (error);
549 /* Displays an error message about not being able to get information for a file.
550 * Frees the GError as well.
553 error_getting_info_dialog (GtkFileChooserDefault *impl,
554 const GtkFilePath *path,
558 _("Could not retrieve information about %s:\n%s"),
562 /* Shows an error dialog about not being able to add a bookmark */
564 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
565 const GtkFilePath *path,
569 _("Could not add a bookmark for %s:\n%s"),
573 /* Shows an error dialog about not being able to compose a filename */
575 error_building_filename_dialog (GtkFileChooserDefault *impl,
576 const GtkFilePath *base_path,
577 const char *file_part,
582 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
583 gtk_file_path_get_string (base_path),
586 error_message (impl, msg);
588 g_error_free (error);
592 update_preview_widget_visibility (GtkFileChooserDefault *impl)
594 if (impl->preview_widget_active && impl->preview_widget)
595 gtk_widget_show (impl->preview_frame);
597 gtk_widget_hide (impl->preview_frame);
601 set_preview_widget (GtkFileChooserDefault *impl,
602 GtkWidget *preview_widget)
604 if (preview_widget == impl->preview_widget)
607 if (impl->preview_widget)
608 gtk_container_remove (GTK_CONTAINER (impl->preview_frame),
609 impl->preview_widget);
611 impl->preview_widget = preview_widget;
612 if (impl->preview_widget)
614 gtk_widget_show_all (impl->preview_widget);
615 gtk_container_add (GTK_CONTAINER (impl->preview_frame),
616 impl->preview_widget);
619 update_preview_widget_visibility (impl);
622 /* Clears the selection in the shortcuts tree */
624 shortcuts_unselect_all (GtkFileChooserDefault *impl)
626 GtkTreeSelection *selection;
628 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
629 gtk_tree_selection_unselect_all (selection);
632 /* Convenience function to get the display name and icon info for a path */
634 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
636 GtkFilePath *parent_path;
637 GtkFileFolder *parent_folder;
640 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
643 parent_folder = gtk_file_system_get_folder (file_system, parent_path,
644 GTK_FILE_INFO_DISPLAY_NAME
648 | GTK_FILE_INFO_IS_FOLDER,
650 gtk_file_path_free (parent_path);
655 info = gtk_file_folder_get_info (parent_folder, path, error);
656 g_object_unref (parent_folder);
661 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
662 * inserts a volume. A position of -1 indicates the end of the tree.
665 shortcuts_insert_path (GtkFileChooserDefault *impl,
668 GtkFileSystemVolume *volume,
669 const GtkFilePath *path,
682 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
683 pixbuf = gtk_file_system_volume_render_icon (impl->file_system,
693 info = get_file_info (impl->file_system, path, error);
697 data = gtk_file_path_copy (path);
700 label_copy = g_strdup (label);
702 label_copy = g_strdup (gtk_file_info_get_display_name (info));
704 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
706 gtk_file_info_free (info);
710 gtk_list_store_append (impl->shortcuts_model, &iter);
712 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
714 gtk_list_store_set (impl->shortcuts_model, &iter,
715 SHORTCUTS_COL_PIXBUF, pixbuf,
716 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
717 SHORTCUTS_COL_NAME, label_copy,
718 SHORTCUTS_COL_PATH, data,
719 SHORTCUTS_COL_REMOVABLE, removable,
725 g_object_unref (pixbuf);
730 /* Appends an item for the user's home directory to the shortcuts model */
732 shortcuts_append_home (GtkFileChooserDefault *impl)
735 GtkFilePath *home_path;
738 home = g_get_home_dir ();
739 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
742 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
744 error_getting_info_dialog (impl, home_path, error);
746 gtk_file_path_free (home_path);
749 /* Appends the ~/Desktop directory to the shortcuts model */
751 shortcuts_append_desktop (GtkFileChooserDefault *impl)
756 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
757 path = gtk_file_system_filename_to_path (impl->file_system, name);
760 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
761 /* We do not actually pop up an error dialog if there is no desktop directory
762 * because some people may really not want to have one.
765 gtk_file_path_free (path);
768 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
770 shortcuts_append_paths (GtkFileChooserDefault *impl,
777 for (; paths; paths = paths->next)
785 /* NULL GError, but we don't really want to show error boxes here */
787 if (shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, TRUE, NULL))
794 /* Returns the index for the corresponding item in the shortcuts bar */
796 shortcuts_get_index (GtkFileChooserDefault *impl,
797 ShortcutsIndex where)
803 if (where == SHORTCUTS_HOME)
806 n += impl->has_home ? 1 : 0;
808 if (where == SHORTCUTS_DESKTOP)
811 n += impl->has_desktop ? 1 : 0;
813 if (where == SHORTCUTS_VOLUMES)
816 n += impl->num_volumes;
818 if (where == SHORTCUTS_SHORTCUTS)
821 n += impl->num_shortcuts;
823 if (where == SHORTCUTS_SEPARATOR)
826 /* If there are no bookmarks there won't be a separator */
827 n += impl->num_shortcuts > 0 ? 1 : 0;
829 if (where == SHORTCUTS_BOOKMARKS)
832 g_assert_not_reached ();
839 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
841 /* Removes the specified number of rows from the shortcuts list */
843 shortcuts_remove_rows (GtkFileChooserDefault *impl,
846 RemoveFunc remove_fn)
850 path = gtk_tree_path_new_from_indices (start_row, -1);
852 for (; n_rows; n_rows--)
857 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
858 g_assert_not_reached ();
862 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
863 (* remove_fn) (impl, data);
866 gtk_list_store_remove (impl->shortcuts_model, &iter);
869 gtk_tree_path_free (path);
872 /* Used from shortcuts_remove_rows() */
874 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
876 GtkFileSystemVolume *volume;
879 gtk_file_system_volume_free (impl->file_system, volume);
882 /* Adds all the file system volumes to the shortcuts model */
884 shortcuts_add_volumes (GtkFileChooserDefault *impl)
890 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
891 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
892 impl->num_volumes = 0;
894 list = gtk_file_system_list_volumes (impl->file_system);
898 for (l = list; l; l = l->next)
900 GtkFileSystemVolume *volume;
904 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
908 impl->num_volumes = n;
913 /* Used from shortcuts_remove_rows() */
915 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
920 gtk_file_path_free (path);
923 /* Inserts the bookmarks separator node */
925 shortcuts_insert_separator (GtkFileChooserDefault *impl)
929 gtk_list_store_insert (impl->shortcuts_model, &iter,
930 shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
931 gtk_list_store_set (impl->shortcuts_model, &iter,
932 SHORTCUTS_COL_PIXBUF, NULL,
933 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
934 SHORTCUTS_COL_NAME, NULL,
935 SHORTCUTS_COL_PATH, NULL,
939 /* Creates the GtkTreeStore used as the shortcuts model */
940 /* Updates the list of bookmarks */
942 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
946 if (impl->num_bookmarks > 0)
948 shortcuts_remove_rows (impl,
949 shortcuts_get_index (impl, SHORTCUTS_SEPARATOR),
950 impl->num_bookmarks + 1,
955 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
956 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
957 gtk_file_paths_free (bookmarks);
959 if (impl->num_bookmarks > 0)
961 shortcuts_insert_separator (impl);
966 shortcuts_model_create (GtkFileChooserDefault *impl)
968 if (impl->shortcuts_model)
969 g_object_unref (impl->shortcuts_model);
971 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
972 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
973 GDK_TYPE_PIXBUF, /* pixbuf */
974 G_TYPE_STRING, /* name */
975 G_TYPE_POINTER, /* path or volume */
976 G_TYPE_BOOLEAN, /* removable */
977 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
979 if (impl->file_system)
981 shortcuts_append_home (impl);
982 shortcuts_append_desktop (impl);
983 shortcuts_add_volumes (impl);
984 shortcuts_add_bookmarks (impl);
987 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), GTK_TREE_MODEL (impl->shortcuts_model));
990 /* Callback used when the "New Folder" toolbar button is clicked */
992 new_folder_button_clicked (GtkButton *button,
993 GtkFileChooserDefault *impl)
998 /* FIXME: this doesn't work for folder mode, just for file mode */
1000 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1001 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1003 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1004 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1006 impl->list_name_column,
1010 /* Callback used from the text cell renderer when the new folder is named */
1012 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1014 const gchar *new_text,
1015 GtkFileChooserDefault *impl)
1018 GtkFilePath *file_path;
1020 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1021 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1024 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1027 error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1032 if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1034 _("Could not create folder %s:\n%s"),
1037 gtk_file_path_free (file_path);
1039 /* FIXME: scroll to the new folder and select it */
1042 /* Callback used from the text cell renderer when the new folder edition gets
1046 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1047 GtkFileChooserDefault *impl)
1049 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1050 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1053 /* Creates the widgets for the filter combo box */
1055 filter_create (GtkFileChooserDefault *impl)
1057 impl->filter_combo = gtk_combo_box_new_text ();
1058 g_signal_connect (impl->filter_combo, "changed",
1059 G_CALLBACK (filter_combo_changed), impl);
1061 return impl->filter_combo;
1065 button_new (GtkFileChooserDefault *impl,
1067 const char *stock_id,
1077 button = gtk_button_new ();
1078 hbox = gtk_hbox_new (FALSE, 2);
1079 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1081 gtk_container_add (GTK_CONTAINER (button), align);
1082 gtk_container_add (GTK_CONTAINER (align), hbox);
1083 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1085 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1087 widget = gtk_label_new_with_mnemonic (text);
1088 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1089 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1091 gtk_widget_set_sensitive (button, sensitive);
1092 g_signal_connect (button, "clicked", callback, impl);
1094 gtk_widget_show_all (align);
1097 gtk_widget_show (button);
1102 /* Creates the widgets for the folder tree */
1104 create_folder_tree (GtkFileChooserDefault *impl)
1106 GtkTreeSelection *selection;
1108 /* Scrolled window */
1110 impl->browse_directories_swin = gtk_scrolled_window_new (NULL, NULL);
1111 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1112 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1113 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1115 if (impl->folder_mode)
1116 gtk_widget_show (impl->browse_directories_swin);
1120 impl->browse_directories_tree_view = gtk_tree_view_new ();
1121 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_directories_tree_view), FALSE);
1123 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
1124 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1127 num_shortcuts_targets,
1130 g_signal_connect (selection, "changed",
1131 G_CALLBACK (tree_selection_changed), impl);
1133 gtk_container_add (GTK_CONTAINER (impl->browse_directories_swin), impl->browse_directories_tree_view);
1134 gtk_widget_show (impl->browse_directories_tree_view);
1138 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->browse_directories_tree_view), 0,
1140 gtk_cell_renderer_text_new (),
1141 tree_name_data_func, impl, NULL);
1142 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1143 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1145 return impl->browse_directories_swin;
1148 /* Returns whether a path is already present in the shortcuts list */
1150 shortcut_exists (GtkFileChooserDefault *impl,
1151 const GtkFilePath *path)
1160 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1164 separator_idx = shortcuts_get_index (impl, SHORTCUTS_SEPARATOR);
1165 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1173 if (i == separator_idx)
1176 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1178 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1180 GtkFileSystemVolume *volume;
1181 GtkFilePath *base_path;
1184 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1186 exists = strcmp (gtk_file_path_get_string (path),
1187 gtk_file_path_get_string (base_path)) == 0;
1195 GtkFilePath *model_path;
1199 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1206 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
1212 /* Tries to add a bookmark from a path name */
1214 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1215 const GtkFilePath *path)
1220 if (shortcut_exists (impl, path))
1224 info = get_file_info (impl->file_system, path, &error);
1227 error_getting_info_dialog (impl, path, error);
1228 else if (!gtk_file_info_get_is_folder (info))
1232 msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1233 gtk_file_path_get_string (path));
1234 error_message (impl, msg);
1240 if (!gtk_file_system_add_bookmark (impl->file_system, path, &error))
1241 error_could_not_add_bookmark_dialog (impl, path, error);
1246 add_bookmark_foreach_cb (GtkTreeModel *model,
1251 GtkFileChooserDefault *impl;
1252 GtkFileSystemModel *fs_model;
1253 GtkTreeIter child_iter;
1254 const GtkFilePath *file_path;
1256 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1258 if (impl->folder_mode)
1260 fs_model = impl->browse_directories_model;
1265 fs_model = impl->browse_files_model;
1266 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1269 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &child_iter);
1270 shortcuts_add_bookmark_from_path (impl, file_path);
1273 /* Callback used when the "Add bookmark" button is clicked */
1275 add_bookmark_button_clicked_cb (GtkButton *button,
1276 GtkFileChooserDefault *impl)
1278 GtkWidget *tree_view;
1279 GtkTreeSelection *selection;
1281 if (impl->folder_mode)
1282 tree_view = impl->browse_directories_tree_view;
1284 tree_view = impl->browse_files_tree_view;
1286 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1287 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1288 shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1290 gtk_tree_selection_selected_foreach (selection,
1291 add_bookmark_foreach_cb,
1295 /* Callback used when the "Remove bookmark" button is clicked */
1297 remove_bookmark_button_clicked_cb (GtkButton *button,
1298 GtkFileChooserDefault *impl)
1300 GtkTreeSelection *selection;
1306 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1309 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1311 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1312 SHORTCUTS_COL_PATH, &path,
1313 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1316 g_assert_not_reached ();
1321 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1323 _("Could not remove bookmark for %s:\n%s"),
1329 struct is_folders_foreach_closure {
1330 GtkFileChooserDefault *impl;
1331 gboolean all_folders;
1334 /* Used from gtk_tree_selection_selected_foreach() */
1336 is_folders_foreach_cb (GtkTreeModel *model,
1341 struct is_folders_foreach_closure *closure;
1342 GtkTreeIter child_iter;
1343 const GtkFileInfo *info;
1347 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1349 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1350 closure->all_folders &= gtk_file_info_get_is_folder (info);
1353 /* Returns whether the selected items in the file list are all folders */
1355 selection_is_folders (GtkFileChooserDefault *impl)
1357 struct is_folders_foreach_closure closure;
1358 GtkTreeSelection *selection;
1360 g_assert (!impl->folder_mode);
1362 closure.impl = impl;
1363 closure.all_folders = TRUE;
1365 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1366 gtk_tree_selection_selected_foreach (selection,
1367 is_folders_foreach_cb,
1370 return closure.all_folders;
1373 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1374 * if there are no selected items *and* the current folder is not in the
1375 * bookmarks list. De-sensitize the button otherwise.
1378 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1380 GtkWidget *tree_view;
1381 GtkTreeSelection *selection;
1384 /* Check selection */
1386 if (impl->folder_mode)
1387 tree_view = impl->browse_directories_tree_view;
1389 tree_view = impl->browse_files_tree_view;
1391 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1393 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1394 active = !shortcut_exists (impl, impl->current_folder);
1396 active = (impl->folder_mode || selection_is_folders (impl));
1398 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1401 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1402 * bookmark row is selected in the shortcuts tree.
1405 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1407 GtkTreeSelection *selection;
1409 gboolean removable = FALSE;
1411 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1413 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1414 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1415 SHORTCUTS_COL_REMOVABLE, &removable,
1418 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1421 /* Converts raw selection data from text/uri-list to a list of strings */
1423 split_uris (const char *data)
1426 const char *p, *start;
1432 for (p = start; *p != 0; p++)
1433 if (*p == '\r' && *(p + 1) == '\n')
1437 name = g_strndup (start, p - start);
1438 uris = g_slist_prepend (uris, name);
1444 uris = g_slist_reverse (uris);
1448 /* Callback used when we get the drag data for the bookmarks list. We add the
1449 * received URIs as bookmarks if they are folders.
1452 shortcuts_drag_data_received_cb (GtkWidget *widget,
1453 GdkDragContext *context,
1456 GtkSelectionData *selection_data,
1461 GtkFileChooserDefault *impl;
1464 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1466 uris = split_uris (selection_data->data);
1468 for (l = uris; l; l = l->next)
1474 path = gtk_file_system_uri_to_path (impl->file_system, uri);
1478 shortcuts_add_bookmark_from_path (impl, path);
1479 gtk_file_path_free (path);
1485 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1487 error_message (impl, msg);
1494 g_slist_free (uris);
1497 /* Callback used when the selection in the shortcuts tree changes */
1499 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
1500 GtkFileChooserDefault *impl)
1502 bookmarks_check_remove_sensitivity (impl);
1505 /* Creates the widgets for the shortcuts and bookmarks tree */
1507 shortcuts_list_create (GtkFileChooserDefault *impl)
1509 GtkTreeSelection *selection;
1510 GtkTreeViewColumn *column;
1511 GtkCellRenderer *renderer;
1513 /* Scrolled window */
1515 impl->browse_shortcuts_swin = gtk_scrolled_window_new (NULL, NULL);
1516 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1517 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1518 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1520 gtk_widget_show (impl->browse_shortcuts_swin);
1524 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
1525 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
1527 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
1528 GTK_DEST_DEFAULT_ALL,
1530 num_shortcuts_targets,
1533 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1534 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1535 gtk_tree_selection_set_select_function (selection,
1536 shortcuts_select_func,
1539 g_signal_connect (selection, "changed",
1540 G_CALLBACK (shortcuts_selection_changed_cb), impl);
1542 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
1543 G_CALLBACK (shortcuts_row_activated_cb), impl);
1545 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
1546 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1548 gtk_container_add (GTK_CONTAINER (impl->browse_shortcuts_swin), impl->browse_shortcuts_tree_view);
1549 gtk_widget_show (impl->browse_shortcuts_tree_view);
1553 shortcuts_model_create (impl);
1557 column = gtk_tree_view_column_new ();
1558 gtk_tree_view_column_set_title (column, _("Folder"));
1560 renderer = gtk_cell_renderer_pixbuf_new ();
1561 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1562 gtk_tree_view_column_set_attributes (column, renderer,
1563 "pixbuf", SHORTCUTS_COL_PIXBUF,
1564 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
1567 renderer = _gtk_cell_renderer_sep_text_new ();
1568 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1569 gtk_tree_view_column_set_attributes (column, renderer,
1570 "text", SHORTCUTS_COL_NAME,
1573 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
1575 return impl->browse_shortcuts_swin;
1578 /* Creates the widgets for the shortcuts/bookmarks pane */
1580 shortcuts_pane_create (GtkFileChooserDefault *impl,
1581 GtkSizeGroup *size_group)
1587 vbox = gtk_vbox_new (FALSE, 6);
1588 gtk_widget_show (vbox);
1590 /* Shortcuts tree */
1592 widget = shortcuts_list_create (impl);
1593 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1595 /* Box for buttons */
1597 hbox = gtk_hbox_new (TRUE, 6);
1598 gtk_size_group_add_widget (size_group, hbox);
1599 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1600 gtk_widget_show (hbox);
1602 /* Add bookmark button */
1604 impl->browse_shortcuts_add_button = button_new (impl,
1609 G_CALLBACK (add_bookmark_button_clicked_cb));
1610 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
1612 /* Remove bookmark button */
1614 impl->browse_shortcuts_remove_button = button_new (impl,
1619 G_CALLBACK (remove_bookmark_button_clicked_cb));
1620 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
1625 /* Creates the widgets for the file list */
1627 create_file_list (GtkFileChooserDefault *impl)
1629 GtkTreeSelection *selection;
1630 GtkTreeViewColumn *column;
1631 GtkCellRenderer *renderer;
1633 /* Scrolled window */
1635 impl->browse_files_swin = gtk_scrolled_window_new (NULL, NULL);
1636 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1637 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1638 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1640 if (!impl->folder_mode)
1641 gtk_widget_show (impl->browse_files_swin);
1643 /* Tree/list view */
1645 impl->browse_files_tree_view = gtk_tree_view_new ();
1646 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
1647 gtk_container_add (GTK_CONTAINER (impl->browse_files_swin), impl->browse_files_tree_view);
1648 g_signal_connect (impl->browse_files_tree_view, "row_activated",
1649 G_CALLBACK (list_row_activated), impl);
1650 gtk_widget_show (impl->browse_files_tree_view);
1652 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1653 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
1656 num_shortcuts_targets,
1659 g_signal_connect (selection, "changed",
1660 G_CALLBACK (list_selection_changed), impl);
1662 /* Filename column */
1664 impl->list_name_column = gtk_tree_view_column_new ();
1665 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1666 gtk_tree_view_column_set_title (impl->list_name_column, _("File name"));
1667 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1669 renderer = gtk_cell_renderer_pixbuf_new ();
1670 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1671 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1672 list_icon_data_func, impl, NULL);
1674 impl->list_name_renderer = gtk_cell_renderer_text_new ();
1675 g_signal_connect (impl->list_name_renderer, "edited",
1676 G_CALLBACK (renderer_edited_cb), impl);
1677 g_signal_connect (impl->list_name_renderer, "editing-canceled",
1678 G_CALLBACK (renderer_editing_canceled_cb), impl);
1679 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1680 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1681 list_name_data_func, impl, NULL);
1683 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
1687 column = gtk_tree_view_column_new ();
1688 gtk_tree_view_column_set_title (column, _("Size"));
1690 renderer = gtk_cell_renderer_text_new ();
1691 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1692 gtk_tree_view_column_set_cell_data_func (column, renderer,
1693 list_size_data_func, impl, NULL);
1694 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1695 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1697 /* Modification time column */
1699 column = gtk_tree_view_column_new ();
1700 gtk_tree_view_column_set_title (column, _("Modified"));
1702 renderer = gtk_cell_renderer_text_new ();
1703 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1704 gtk_tree_view_column_set_cell_data_func (column, renderer,
1705 list_mtime_data_func, impl, NULL);
1706 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1707 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1709 return impl->browse_files_swin;
1713 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1718 hbox = gtk_hbox_new (FALSE, 12);
1719 gtk_widget_show (hbox);
1723 widget = filter_create (impl);
1724 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1729 /* Creates the widgets for the files/folders pane */
1731 file_pane_create (GtkFileChooserDefault *impl,
1732 GtkSizeGroup *size_group)
1738 vbox = gtk_vbox_new (FALSE, 6);
1739 gtk_widget_show (vbox);
1741 /* The path bar and 'Create Folder' button */
1742 hbox = gtk_hbox_new (FALSE, 12);
1743 gtk_widget_show (hbox);
1744 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
1745 g_signal_connect (impl->browse_path_bar, "path_clicked", G_CALLBACK (path_bar_clicked), impl);
1746 gtk_widget_show_all (impl->browse_path_bar);
1747 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
1750 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
1751 g_signal_connect (impl->browse_new_folder_button, "clicked",
1752 G_CALLBACK (new_folder_button_clicked), impl);
1753 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
1754 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1756 /* Box for lists and preview */
1758 hbox = gtk_hbox_new (FALSE, 12);
1759 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1760 gtk_widget_show (hbox);
1764 widget = create_folder_tree (impl);
1765 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1769 widget = create_file_list (impl);
1770 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1774 impl->preview_frame = gtk_frame_new (_("Preview"));
1775 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_frame, FALSE, FALSE, 0);
1776 /* Don't show preview frame initially */
1778 /* Filename entry and filter combo */
1779 hbox = gtk_hbox_new (FALSE, 0);
1780 gtk_size_group_add_widget (size_group, hbox);
1781 widget = create_filename_entry_and_filter_combo (impl);
1782 gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1783 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1784 gtk_widget_show (hbox);
1788 /* Callback used when the "Browse for more folders" expander is toggled */
1790 expander_changed_cb (GtkExpander *expander,
1792 GtkFileChooserDefault *impl)
1796 active = gtk_expander_get_expanded (expander);
1797 update_appearance (impl);
1800 /* Creates the widgets specific to Save mode */
1802 save_widgets_create (GtkFileChooserDefault *impl)
1807 GtkWidget *alignment;
1809 vbox = gtk_vbox_new (FALSE, 12);
1811 table = gtk_table_new (2, 2, FALSE);
1812 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
1813 gtk_widget_show (table);
1814 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1815 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1819 widget = gtk_label_new_with_mnemonic (_("_Name:"));
1820 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
1821 gtk_table_attach (GTK_TABLE (table), widget,
1825 gtk_widget_show (widget);
1827 impl->save_file_name_entry = gtk_entry_new ();
1828 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 25);
1829 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
1830 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
1832 GTK_EXPAND | GTK_FILL, 0,
1834 gtk_widget_show (impl->save_file_name_entry);
1835 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
1838 impl->save_folder_label = gtk_label_new_with_mnemonic (_("Save in _Folder:"));
1839 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
1840 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
1844 gtk_widget_show (impl->save_folder_label);
1846 /* FIXME: create the combo */
1849 impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
1850 gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
1853 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
1854 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
1856 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
1857 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
1858 g_signal_connect (impl->save_expander, "notify::expanded",
1859 G_CALLBACK (expander_changed_cb),
1861 gtk_widget_show_all (alignment);
1866 /* Creates the main hpaned with the widgets shared by Open and Save mode */
1868 browse_widgets_create (GtkFileChooserDefault *impl)
1873 GtkSizeGroup *size_group;
1875 /* size group is used by the [+][-] buttons and the filter combo */
1876 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
1877 vbox = gtk_vbox_new (FALSE, 12);
1880 hpaned = gtk_hpaned_new ();
1881 gtk_widget_show (hpaned);
1882 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
1883 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
1885 widget = shortcuts_pane_create (impl, size_group);
1886 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
1887 widget = file_pane_create (impl, size_group);
1888 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
1890 /* Alignment to hold custom widget */
1891 impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
1892 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
1898 gtk_file_chooser_default_constructor (GType type,
1899 guint n_construct_properties,
1900 GObjectConstructParam *construct_params)
1902 GtkFileChooserDefault *impl;
1905 object = parent_class->constructor (type,
1906 n_construct_properties,
1908 impl = GTK_FILE_CHOOSER_DEFAULT (object);
1910 g_assert (impl->file_system);
1912 gtk_widget_push_composite_child ();
1914 /* Widgets for Save mode */
1915 impl->save_widgets = save_widgets_create (impl);
1916 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
1918 /* The browse widgets */
1919 impl->browse_widgets = browse_widgets_create (impl);
1920 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
1922 gtk_widget_pop_composite_child ();
1923 update_appearance (impl);
1928 /* Sets the extra_widget by packing it in the appropriate place */
1930 set_extra_widget (GtkFileChooserDefault *impl,
1931 GtkWidget *extra_widget)
1935 g_object_ref (extra_widget);
1936 /* FIXME: is this right ? */
1937 gtk_widget_show (extra_widget);
1940 if (impl->extra_widget)
1941 g_object_unref (impl->extra_widget);
1943 impl->extra_widget = extra_widget;
1947 volumes_changed_cb (GtkFileSystem *file_system,
1948 GtkFileChooserDefault *impl)
1950 shortcuts_add_volumes (impl);
1953 /* Callback used when the set of bookmarks changes in the file system */
1955 bookmarks_changed_cb (GtkFileSystem *file_system,
1956 GtkFileChooserDefault *impl)
1958 shortcuts_add_bookmarks (impl);
1960 bookmarks_check_add_sensitivity (impl);
1961 bookmarks_check_remove_sensitivity (impl);
1964 /* Sets the file chooser to multiple selection mode */
1966 set_select_multiple (GtkFileChooserDefault *impl,
1967 gboolean select_multiple,
1968 gboolean property_notify)
1970 GtkTreeSelection *selection;
1971 GtkSelectionMode mode;
1973 if (select_multiple == impl->select_multiple)
1976 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
1978 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
1979 gtk_tree_selection_set_mode (selection, mode);
1981 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1982 gtk_tree_selection_set_mode (selection, mode);
1984 impl->select_multiple = select_multiple;
1985 g_object_notify (G_OBJECT (impl), "select-multiple");
1987 /* FIXME #132255: See note in check_preview_change() */
1988 check_preview_change (impl);
1992 set_file_system_backend (GtkFileChooserDefault *impl,
1993 const char *backend)
1995 if (impl->file_system)
1997 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
1998 impl->volumes_changed_id = 0;
1999 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
2000 impl->bookmarks_changed_id = 0;
2001 g_object_unref (impl->file_system);
2004 impl->file_system = NULL;
2006 impl->file_system = _gtk_file_system_create (backend);
2008 if (!impl->file_system)
2010 #if defined (G_OS_UNIX)
2011 impl->file_system = gtk_file_system_unix_new ();
2012 #elif defined (G_OS_WIN32)
2013 impl->file_system = gtk_file_system_win32_new ();
2015 #error "No default filesystem implementation on the platform"
2019 if (impl->file_system)
2021 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
2022 G_CALLBACK (volumes_changed_cb),
2024 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
2025 G_CALLBACK (bookmarks_changed_cb),
2030 /* This function is basically a do_all function.
2032 * It sets the visibility on all the widgets based on the current state, and
2033 * moves the custom_widget if needed.
2036 update_appearance (GtkFileChooserDefault *impl)
2040 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2042 GtkWidget *top_level;
2044 gtk_widget_show (impl->save_widgets);
2046 top_level = gtk_widget_get_toplevel (GTK_WIDGET (impl));
2048 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
2050 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
2051 /*gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);*/
2052 gtk_widget_show (impl->browse_widgets);
2053 if (GTK_IS_WINDOW (top_level))
2054 gtk_window_set_resizable (GTK_WINDOW (top_level), TRUE);
2058 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
2059 /*gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);*/
2060 gtk_widget_hide (impl->browse_widgets);
2061 gtk_widget_queue_resize (top_level);
2063 if (GTK_IS_WINDOW (top_level))
2064 gtk_window_set_resizable (GTK_WINDOW (top_level), FALSE);
2065 if (GTK_IS_WINDOW (top_level))
2066 gtk_window_set_resizable (GTK_WINDOW (top_level), TRUE);
2070 gtk_widget_show (impl->browse_new_folder_button);
2072 if (impl->select_multiple)
2074 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
2075 "Re-setting to single selection mode.");
2076 set_select_multiple (impl, FALSE, TRUE);
2079 else /* GTK_FILE_CHOOSER_ACTION_OPEN */
2081 gtk_widget_hide (impl->save_widgets);
2082 gtk_widget_show (impl->browse_widgets);
2084 if (impl->folder_mode)
2085 gtk_widget_show (impl->browse_new_folder_button);
2087 gtk_widget_hide (impl->browse_new_folder_button);
2090 if (impl->folder_mode)
2092 gtk_widget_hide (impl->browse_files_swin);
2093 gtk_widget_show (impl->browse_directories_swin);
2097 gtk_widget_hide (impl->browse_directories_swin);
2098 gtk_widget_show (impl->browse_files_swin);
2101 if (impl->extra_widget)
2104 GtkWidget *unused_align;
2106 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2108 align = impl->save_extra_align;
2109 unused_align = impl->browse_extra_align;
2113 align = impl->browse_extra_align;
2114 unused_align = impl->save_extra_align;
2117 /* We own a ref on extra_widget, so it's safe to do this */
2118 child = GTK_BIN (unused_align)->child;
2120 gtk_container_remove (GTK_CONTAINER (unused_align), child);
2122 child = GTK_BIN (align)->child;
2123 if (child && child != impl->extra_widget)
2125 gtk_container_remove (GTK_CONTAINER (align), child);
2126 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2128 else if (child == NULL)
2130 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2133 gtk_widget_show (align);
2134 gtk_widget_hide (unused_align);
2138 child = GTK_BIN (impl->browse_extra_align)->child;
2140 gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
2142 child = GTK_BIN (impl->save_extra_align)->child;
2144 gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
2146 gtk_widget_hide (impl->save_extra_align);
2147 gtk_widget_hide (impl->browse_extra_align);
2152 gtk_file_chooser_default_set_property (GObject *object,
2154 const GValue *value,
2158 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2162 case GTK_FILE_CHOOSER_PROP_ACTION:
2164 GtkFileChooserAction action = g_value_get_enum (value);
2166 if (action != impl->action)
2168 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
2170 g_warning ("Multiple selection mode is not allowed in Save mode");
2171 set_select_multiple (impl, FALSE, TRUE);
2173 impl->action = action;
2174 update_appearance (impl);
2178 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
2179 set_file_system_backend (impl, g_value_get_string (value));
2181 case GTK_FILE_CHOOSER_PROP_FILTER:
2182 set_current_filter (impl, g_value_get_object (value));
2184 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
2186 gboolean folder_mode = g_value_get_boolean (value);
2187 if (folder_mode != impl->folder_mode)
2189 impl->folder_mode = folder_mode;
2190 update_appearance (impl);
2194 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2195 impl->local_only = g_value_get_boolean (value);
2197 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2198 set_preview_widget (impl, g_value_get_object (value));
2200 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2201 impl->preview_widget_active = g_value_get_boolean (value);
2202 update_preview_widget_visibility (impl);
2204 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2205 set_extra_widget (impl, g_value_get_object (value));
2206 update_appearance (impl);
2208 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2210 gboolean select_multiple = g_value_get_boolean (value);
2211 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
2213 g_warning ("Multiple selection mode is not allowed in Save mode");
2217 set_select_multiple (impl, select_multiple, FALSE);
2220 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2222 gboolean show_hidden = g_value_get_boolean (value);
2223 if (show_hidden != impl->show_hidden)
2225 impl->show_hidden = show_hidden;
2226 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2228 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_files_model),
2234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2240 gtk_file_chooser_default_get_property (GObject *object,
2245 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2249 case GTK_FILE_CHOOSER_PROP_ACTION:
2250 g_value_set_enum (value, impl->action);
2252 case GTK_FILE_CHOOSER_PROP_FILTER:
2253 g_value_set_object (value, impl->current_filter);
2255 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
2256 g_value_set_boolean (value, impl->folder_mode);
2258 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2259 g_value_set_boolean (value, impl->local_only);
2261 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2262 g_value_set_object (value, impl->preview_widget);
2264 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2265 g_value_set_boolean (value, impl->preview_widget_active);
2267 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2268 g_value_set_object (value, impl->extra_widget);
2270 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2271 g_value_set_boolean (value, impl->select_multiple);
2273 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2274 g_value_set_boolean (value, impl->show_hidden);
2277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2284 gtk_file_chooser_default_dispose (GObject *object)
2286 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
2288 if (impl->extra_widget)
2290 g_object_unref (impl->extra_widget);
2291 impl->extra_widget = NULL;
2293 G_OBJECT_CLASS (parent_class)->dispose (object);
2296 /* We override show-all since we have internal widgets that
2297 * shouldn't be shown when you call show_all(), like the filter
2301 gtk_file_chooser_default_show_all (GtkWidget *widget)
2303 gtk_widget_show (widget);
2307 expand_and_select_func (GtkFileSystemModel *model,
2312 GtkFileChooserDefault *impl = user_data;
2313 GtkTreeView *tree_view;
2315 if (model == impl->browse_directories_model)
2316 tree_view = GTK_TREE_VIEW (impl->browse_directories_tree_view);
2318 tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2320 gtk_tree_view_expand_to_path (tree_view, path);
2321 gtk_tree_view_expand_row (tree_view, path, FALSE);
2322 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
2323 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), path, NULL, TRUE, 0.3, 0.5);
2327 list_model_filter_func (GtkFileSystemModel *model,
2329 const GtkFileInfo *file_info,
2332 GtkFileChooserDefault *impl = user_data;
2333 GtkFileFilterInfo filter_info;
2334 GtkFileFilterFlags needed;
2337 if (!impl->current_filter)
2340 if (gtk_file_info_get_is_folder (file_info))
2343 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
2345 needed = gtk_file_filter_get_needed (impl->current_filter);
2347 filter_info.display_name = gtk_file_info_get_display_name (file_info);
2348 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
2350 if (needed & GTK_FILE_FILTER_FILENAME)
2352 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
2353 if (filter_info.filename)
2354 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
2357 filter_info.filename = NULL;
2359 if (needed & GTK_FILE_FILTER_URI)
2361 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
2362 if (filter_info.filename)
2363 filter_info.contains |= GTK_FILE_FILTER_URI;
2366 filter_info.uri = NULL;
2368 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2370 if (filter_info.filename)
2371 g_free ((gchar *)filter_info.filename);
2372 if (filter_info.uri)
2373 g_free ((gchar *)filter_info.uri);
2379 install_list_model_filter (GtkFileChooserDefault *impl)
2381 if (impl->current_filter)
2382 _gtk_file_system_model_set_filter (impl->browse_files_model,
2383 list_model_filter_func,
2387 #define COMPARE_DIRECTORIES \
2388 GtkFileChooserDefault *impl = user_data; \
2389 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
2390 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
2391 gboolean dir_a, dir_b; \
2394 dir_a = gtk_file_info_get_is_folder (info_a); \
2396 return impl->list_sort_ascending ? -1 : 1; \
2399 dir_b = gtk_file_info_get_is_folder (info_b); \
2401 return impl->list_sort_ascending ? 1 : -1; \
2403 if (dir_a != dir_b) \
2404 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2406 /* Sort callback for the filename column */
2408 name_sort_func (GtkTreeModel *model,
2413 COMPARE_DIRECTORIES;
2415 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2418 /* Sort callback for the size column */
2420 size_sort_func (GtkTreeModel *model,
2425 COMPARE_DIRECTORIES;
2428 gint64 size_a = gtk_file_info_get_size (info_a);
2429 gint64 size_b = gtk_file_info_get_size (info_b);
2431 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2435 /* Sort callback for the mtime column */
2437 mtime_sort_func (GtkTreeModel *model,
2442 COMPARE_DIRECTORIES;
2445 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2446 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2448 return ta > tb ? -1 : (ta == tb ? 0 : 1);
2452 /* Callback used when the sort column changes. We cache the sort order for use
2453 * in name_sort_func().
2456 list_sort_column_changed_cb (GtkTreeSortable *sortable,
2457 GtkFileChooserDefault *impl)
2459 GtkSortType sort_type;
2461 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2462 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2465 /* Gets rid of the old list model and creates a new one for the current folder */
2467 set_list_model (GtkFileChooserDefault *impl)
2469 if (impl->browse_files_model)
2471 g_object_unref (impl->browse_files_model);
2472 g_object_unref (impl->sort_model);
2475 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
2476 impl->current_folder, 0,
2478 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
2479 install_list_model_filter (impl);
2481 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
2482 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2483 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2484 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2485 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2486 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2487 impl->list_sort_ascending = TRUE;
2489 g_signal_connect (impl->sort_model, "sort_column_changed",
2490 G_CALLBACK (list_sort_column_changed_cb), impl);
2492 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
2493 GTK_TREE_MODEL (impl->sort_model));
2494 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
2495 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
2496 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2499 /* Gets rid of the old folder tree model and creates a new one for the volume
2500 * corresponding to the specified path.
2503 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2505 GtkFileSystemVolume *volume;
2506 GtkFilePath *base_path, *parent_path;
2510 volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2513 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2515 if (base_path == NULL)
2517 base_path = gtk_file_path_copy (path);
2518 while (gtk_file_system_get_parent (impl->file_system,
2522 parent_path != NULL)
2524 gtk_file_path_free (base_path);
2525 base_path = parent_path;
2529 if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2532 if (impl->browse_directories_model)
2533 g_object_unref (impl->browse_directories_model);
2535 impl->current_volume_path = gtk_file_path_copy (base_path);
2537 impl->browse_directories_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2538 GTK_FILE_INFO_DISPLAY_NAME);
2539 _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2541 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2544 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_directories_tree_view),
2545 GTK_TREE_MODEL (impl->browse_directories_model));
2549 gtk_file_path_free (base_path);
2551 gtk_file_system_volume_free (impl->file_system, volume);
2555 update_chooser_entry (GtkFileChooserDefault *impl)
2557 GtkTreeSelection *selection;
2558 const GtkFileInfo *info;
2560 GtkTreeIter child_iter;
2562 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
2565 g_assert (!impl->select_multiple);
2566 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2568 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2571 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2575 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
2577 if (!gtk_file_info_get_is_folder (info))
2578 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
2579 gtk_file_info_get_display_name (info));
2583 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
2584 const GtkFilePath *path)
2586 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2588 if (impl->current_folder)
2589 gtk_file_path_free (impl->current_folder);
2591 impl->current_folder = gtk_file_path_copy (path);
2593 /* Change the current folder label */
2594 gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, impl->file_system, NULL);
2596 /* Update the folder tree */
2598 if (!impl->changing_folder)
2600 impl->changing_folder = TRUE;
2601 set_tree_model (impl, impl->current_folder);
2602 _gtk_file_system_model_path_do (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2603 path, expand_and_select_func, impl);
2604 impl->changing_folder = FALSE;
2607 /* Create a new list model */
2608 set_list_model (impl);
2610 /* Refresh controls */
2612 shortcuts_unselect_all (impl);
2614 g_signal_emit_by_name (impl, "current-folder-changed", 0);
2616 check_preview_change (impl);
2617 bookmarks_check_add_sensitivity (impl);
2619 g_signal_emit_by_name (impl, "selection-changed", 0);
2622 static GtkFilePath *
2623 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2625 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2627 return gtk_file_path_copy (impl->current_folder);
2631 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2634 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2636 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
2638 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
2642 select_func (GtkFileSystemModel *model,
2647 GtkFileChooserDefault *impl = user_data;
2648 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2649 GtkTreePath *sorted_path;
2651 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2652 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2653 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), sorted_path, NULL, TRUE, 0.3, 0.0);
2654 gtk_tree_path_free (sorted_path);
2658 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
2659 const GtkFilePath *path)
2661 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2662 GtkFilePath *parent_path;
2666 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2668 error_getting_info_dialog (impl, path, error);
2674 _gtk_file_chooser_set_current_folder_path (chooser, path);
2678 _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2679 gtk_file_path_free (parent_path);
2680 _gtk_file_system_model_path_do (impl->browse_files_model, path,
2686 unselect_func (GtkFileSystemModel *model,
2691 GtkFileChooserDefault *impl = user_data;
2692 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2693 GtkTreePath *sorted_path;
2695 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2697 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2699 gtk_tree_path_free (sorted_path);
2703 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
2704 const GtkFilePath *path)
2706 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2708 _gtk_file_system_model_path_do (impl->browse_files_model, path,
2709 unselect_func, impl);
2713 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2715 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2716 if (impl->select_multiple)
2718 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2719 gtk_tree_selection_select_all (selection);
2724 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2726 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2727 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2729 gtk_tree_selection_unselect_all (selection);
2732 struct get_paths_closure {
2733 GtkFileChooserDefault *impl;
2735 GtkFilePath *path_from_entry;
2739 get_paths_foreach (GtkTreeModel *model,
2744 struct get_paths_closure *info;
2745 const GtkFilePath *file_path;
2746 GtkFileSystemModel *fs_model;
2747 GtkTreeIter sel_iter;
2751 if (info->impl->folder_mode)
2753 fs_model = info->impl->browse_directories_model;
2758 fs_model = info->impl->browse_files_model;
2759 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
2762 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &sel_iter);
2764 if (!info->path_from_entry
2765 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
2766 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
2770 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
2772 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2773 struct get_paths_closure info;
2777 info.path_from_entry = NULL;
2779 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2781 const char *filename;
2783 filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
2785 if (filename != NULL && filename[0] != '\0')
2787 GtkFilePath *selected;
2788 GError *error = NULL;
2790 selected = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
2794 error_building_filename_dialog (impl, impl->current_folder, filename, error);
2798 info.path_from_entry = selected;
2802 if (!info.path_from_entry || impl->select_multiple)
2804 GtkTreeSelection *selection;
2808 if (impl->folder_mode)
2810 if (impl->browse_directories_model)
2811 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
2815 if (impl->sort_model)
2816 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2820 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
2823 if (info.path_from_entry)
2824 info.result = g_slist_prepend (info.result, info.path_from_entry);
2826 return g_slist_reverse (info.result);
2829 static GtkFilePath *
2830 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
2832 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2834 if (impl->preview_path)
2835 return gtk_file_path_copy (impl->preview_path);
2840 static GtkFileSystem *
2841 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
2843 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2845 return impl->file_system;
2848 /* Shows or hides the filter widgets */
2850 toolbar_show_filters (GtkFileChooserDefault *impl,
2854 gtk_widget_show (impl->filter_combo);
2856 gtk_widget_hide (impl->filter_combo);
2860 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
2861 GtkFileFilter *filter)
2863 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2866 if (g_slist_find (impl->filters, filter))
2868 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
2872 g_object_ref (filter);
2873 gtk_object_sink (GTK_OBJECT (filter));
2874 impl->filters = g_slist_append (impl->filters, filter);
2876 name = gtk_file_filter_get_name (filter);
2878 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
2880 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
2882 if (!g_slist_find (impl->filters, impl->current_filter))
2883 set_current_filter (impl, filter);
2885 toolbar_show_filters (impl, TRUE);
2889 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
2890 GtkFileFilter *filter)
2892 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2893 GtkTreeModel *model;
2897 filter_index = g_slist_index (impl->filters, filter);
2899 if (filter_index < 0)
2901 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
2905 impl->filters = g_slist_remove (impl->filters, filter);
2907 if (filter == impl->current_filter)
2910 set_current_filter (impl, impl->filters->data);
2912 set_current_filter (impl, NULL);
2915 /* Remove row from the combo box */
2916 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
2917 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
2918 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
2920 g_object_unref (filter);
2923 toolbar_show_filters (impl, FALSE);
2927 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
2929 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2931 return g_slist_copy (impl->filters);
2934 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
2936 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
2939 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
2943 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
2944 const GtkFilePath *path,
2947 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2951 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
2953 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
2956 impl->num_shortcuts++;
2962 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
2963 const GtkFilePath *path,
2966 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2971 if (impl->num_shortcuts == 0)
2974 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
2975 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
2976 g_assert_not_reached ();
2978 for (i = 0; i < impl->num_shortcuts; i++)
2980 GtkFilePath *shortcut;
2982 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
2983 g_assert (shortcut != NULL);
2985 if (gtk_file_path_compare (shortcut, path) == 0)
2987 /* The other columns are freed by the GtkTreeStore */
2988 gtk_file_path_free (shortcut);
2989 gtk_list_store_remove (impl->shortcuts_model, &iter);
2990 impl->num_shortcuts--;
2994 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2995 g_assert_not_reached ();
3001 GTK_FILE_CHOOSER_ERROR,
3002 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
3003 _("shortcut %s does not exist"),
3004 gtk_file_path_get_string (path));
3010 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
3012 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3018 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3019 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3020 g_assert_not_reached ();
3024 for (i = 0; i < impl->num_shortcuts; i++)
3026 GtkFilePath *shortcut;
3028 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3029 g_assert (shortcut != NULL);
3031 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
3033 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3034 g_assert_not_reached ();
3037 return g_slist_reverse (list);
3041 set_current_filter (GtkFileChooserDefault *impl,
3042 GtkFileFilter *filter)
3044 if (impl->current_filter != filter)
3048 /* If we have filters, new filter must be one of them
3050 filter_index = g_slist_index (impl->filters, filter);
3051 if (impl->filters && filter_index < 0)
3054 if (impl->current_filter)
3055 g_object_unref (impl->current_filter);
3056 impl->current_filter = filter;
3057 if (impl->current_filter)
3059 g_object_ref (impl->current_filter);
3060 gtk_object_sink (GTK_OBJECT (filter));
3064 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
3067 install_list_model_filter (impl);
3069 g_object_notify (G_OBJECT (impl), "filter");
3074 open_and_close (GtkTreeView *tree_view,
3075 GtkTreePath *target_path)
3077 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
3081 path = gtk_tree_path_new ();
3082 gtk_tree_path_append_index (path, 0);
3084 gtk_tree_model_get_iter (model, &iter, path);
3088 if (gtk_tree_path_is_ancestor (path, target_path) ||
3089 gtk_tree_path_compare (path, target_path) == 0)
3091 GtkTreeIter child_iter;
3092 gtk_tree_view_expand_row (tree_view, path, FALSE);
3093 if (gtk_tree_model_iter_children (model, &child_iter, &iter))
3096 gtk_tree_path_down (path);
3101 gtk_tree_view_collapse_row (tree_view, path);
3105 GtkTreeIter parent_iter;
3106 GtkTreeIter next_iter;
3109 if (gtk_tree_model_iter_next (model, &next_iter))
3112 gtk_tree_path_next (path);
3116 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
3120 gtk_tree_path_up (path);
3127 gtk_tree_path_free (path);
3131 filter_combo_changed (GtkComboBox *combo_box,
3132 GtkFileChooserDefault *impl)
3134 gint new_index = gtk_combo_box_get_active (combo_box);
3135 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
3137 set_current_filter (impl, new_filter);
3141 check_preview_change (GtkFileChooserDefault *impl)
3143 const GtkFilePath *new_path = NULL;
3145 /* FIXME #132255: Fixing preview for multiple selection involves getting the
3146 * full selection and diffing to find out what the most recently selected file
3147 * is; there is logic in GtkFileSelection that probably can be
3150 if (impl->sort_model && !impl->select_multiple)
3152 GtkTreeSelection *selection;
3155 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3156 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
3158 GtkTreeIter child_iter;
3160 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3161 &child_iter, &iter);
3163 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3167 if (new_path != impl->preview_path &&
3168 !(new_path && impl->preview_path &&
3169 gtk_file_path_compare (new_path, impl->preview_path) == 0))
3171 if (impl->preview_path)
3172 gtk_file_path_free (impl->preview_path);
3175 impl->preview_path = gtk_file_path_copy (new_path);
3177 impl->preview_path = NULL;
3179 g_signal_emit_by_name (impl, "update-preview");
3184 tree_selection_changed (GtkTreeSelection *selection,
3185 GtkFileChooserDefault *impl)
3188 const GtkFilePath *file_path;
3191 /* FIXME #132255: Fixing this for multiple selection involves getting the full
3192 * selection and diffing to find out what the most recently selected file is;
3193 * there is logic in GtkFileSelection that probably can be copied;
3194 * check_preview_change() is similar.
3196 if (impl->select_multiple
3197 || !gtk_tree_selection_get_selected (selection, NULL, &iter))
3200 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3202 if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
3205 /* Close the tree up to only the parents of the newly selected
3206 * node and it's immediate children are visible.
3208 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_directories_model), &iter);
3209 open_and_close (GTK_TREE_VIEW (impl->browse_directories_tree_view), path);
3210 gtk_tree_path_free (path);
3212 if (!impl->changing_folder)
3213 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3216 /* Activates a volume by mounting it if necessary and then switching to its
3220 shortcuts_activate_volume (GtkFileChooserDefault *impl,
3221 GtkFileSystemVolume *volume)
3225 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
3230 if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
3234 msg = g_strdup_printf ("Could not mount %s:\n%s",
3235 gtk_file_system_volume_get_display_name (impl->file_system, volume),
3237 error_message (impl, msg);
3239 g_error_free (error);
3245 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
3246 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
3247 gtk_file_path_free (path);
3250 /* Callback used when a row in the shortcuts list is activated */
3252 shortcuts_row_activated_cb (GtkTreeView *tree_view,
3254 GtkTreeViewColumn *column,
3255 GtkFileChooserDefault *impl)
3258 int selected, start_row;
3261 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3264 selected = *gtk_tree_path_get_indices (path);
3266 if (selected == shortcuts_get_index (impl, SHORTCUTS_SEPARATOR))
3269 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
3271 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
3272 if (selected >= start_row && selected < start_row + impl->num_volumes)
3274 GtkFileSystemVolume *volume;
3277 shortcuts_activate_volume (impl, volume);
3281 GtkFilePath *file_path;
3284 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3289 shortcuts_select_func (GtkTreeSelection *selection,
3290 GtkTreeModel *model,
3292 gboolean path_currently_selected,
3295 GtkFileChooserDefault *impl = data;
3297 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
3301 list_selection_changed (GtkTreeSelection *selection,
3302 GtkFileChooserDefault *impl)
3304 /* See if we are in the new folder editable row for Save mode */
3305 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3307 GtkTreeSelection *selection;
3308 GtkTreeIter iter, child_iter;
3309 const GtkFileInfo *info;
3311 g_assert (!impl->select_multiple);
3312 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3313 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3316 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3320 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3322 return; /* We are on the editable row for New Folder */
3325 update_chooser_entry (impl);
3326 check_preview_change (impl);
3327 bookmarks_check_add_sensitivity (impl);
3329 g_signal_emit_by_name (impl, "selection-changed", 0);
3332 /* Callback used when a row in the file list is activated */
3334 list_row_activated (GtkTreeView *tree_view,
3336 GtkTreeViewColumn *column,
3337 GtkFileChooserDefault *impl)
3339 GtkTreeIter iter, child_iter;
3340 const GtkFileInfo *info;
3342 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
3345 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
3347 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3349 if (gtk_file_info_get_is_folder (info))
3351 const GtkFilePath *file_path;
3353 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3354 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3359 g_signal_emit_by_name (impl, "file-activated");
3363 path_bar_clicked (GtkPathBar *path_bar,
3364 GtkFilePath *file_path,
3365 GtkFileChooserDefault *impl)
3367 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3370 static const GtkFileInfo *
3371 get_list_file_info (GtkFileChooserDefault *impl,
3374 GtkTreeIter child_iter;
3376 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3380 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3384 tree_name_data_func (GtkTreeViewColumn *tree_column,
3385 GtkCellRenderer *cell,
3386 GtkTreeModel *tree_model,
3390 GtkFileChooserDefault *impl = data;
3391 const GtkFileInfo *info;
3393 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3399 "text", gtk_file_info_get_display_name (info),
3405 list_icon_data_func (GtkTreeViewColumn *tree_column,
3406 GtkCellRenderer *cell,
3407 GtkTreeModel *tree_model,
3411 GtkFileChooserDefault *impl = data;
3412 GtkTreeIter child_iter;
3413 const GtkFilePath *path;
3416 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3419 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3423 /* FIXME: NULL GError */
3424 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3430 g_object_unref (pixbuf);
3433 /* Sets a cellrenderer's text, making it bold if the GtkFileInfo is a folder */
3435 set_cell_text_bold_if_folder (const GtkFileInfo *info, GtkCellRenderer *cell, const char *text)
3439 "weight", gtk_file_info_get_is_folder (info) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
3444 list_name_data_func (GtkTreeViewColumn *tree_column,
3445 GtkCellRenderer *cell,
3446 GtkTreeModel *tree_model,
3450 GtkFileChooserDefault *impl = data;
3451 const GtkFileInfo *info = get_list_file_info (impl, iter);
3456 "text", _("Type name of new folder"),
3461 set_cell_text_bold_if_folder (info, cell, gtk_file_info_get_display_name (info));
3466 list_size_data_func (GtkTreeViewColumn *tree_column,
3467 GtkCellRenderer *cell,
3468 GtkTreeModel *tree_model,
3472 GtkFileChooserDefault *impl = data;
3473 const GtkFileInfo *info = get_list_file_info (impl, iter);
3477 if (!info || gtk_file_info_get_is_folder (info))
3480 size = gtk_file_info_get_size (info);
3482 if (size < (gint64)1024)
3483 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3484 else if (size < (gint64)1024*1024)
3485 str = g_strdup_printf (_("%.1f K"), size / (1024.));
3486 else if (size < (gint64)1024*1024*1024)
3487 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3489 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3499 /* Tree column data callback for the file list; fetches the mtime of a file */
3501 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3502 GtkCellRenderer *cell,
3503 GtkTreeModel *tree_model,
3507 GtkFileChooserDefault *impl;
3508 const GtkFileInfo *info;
3509 GtkFileTime time_mtime, time_now;
3516 info = get_list_file_info (impl, iter);
3525 time_mtime = gtk_file_info_get_modification_time (info);
3526 g_date_set_time (&mtime, (GTime) time_mtime);
3528 time_now = (GTime ) time (NULL);
3529 g_date_set_time (&now, (GTime) time_now);
3531 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3534 strcpy (buf, _("Today"));
3535 else if (days_diff == 1)
3536 strcpy (buf, _("Yesterday"));
3541 if (days_diff > 1 && days_diff < 7)
3542 format = "%A"; /* Days from last week */
3544 format = _("%d/%b/%Y"); /* Any other date */
3546 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3547 strcpy (buf, _("Unknown"));
3550 set_cell_text_bold_if_folder (info, cell, buf);
3554 _gtk_file_chooser_default_new (const char *file_system)
3556 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3557 "file-system-backend", file_system,
3562 location_entry_create (GtkFileChooserDefault *impl)
3566 entry = _gtk_file_chooser_entry_new ();
3567 /* Pick a good width for the entry */
3568 gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
3569 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
3570 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
3571 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
3573 return GTK_WIDGET (entry);
3577 update_from_entry (GtkFileChooserDefault *impl,
3579 GtkFileChooserEntry *chooser_entry)
3581 const GtkFilePath *folder_path;
3582 const char *file_part;
3584 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3585 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3587 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
3589 error_message_with_parent (parent,
3590 _("Cannot change to the folder you specified as it is an invalid path."));
3594 if (file_part[0] == '\0')
3596 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), folder_path);
3601 GtkFileFolder *folder = NULL;
3602 GtkFilePath *subfolder_path = NULL;
3603 GtkFileInfo *info = NULL;
3606 /* If the file part is non-empty, we need to figure out if it refers to a
3607 * folder within folder. We could optimize the case here where the folder
3608 * is already loaded for one of our tree models.
3612 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
3616 error_getting_info_dialog (impl, folder_path, error);
3621 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
3623 if (!subfolder_path)
3627 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
3628 gtk_file_path_get_string (folder_path),
3631 error_message (impl, msg);
3633 g_object_unref (folder);
3638 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
3643 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3645 g_object_unref (folder);
3646 gtk_file_path_free (subfolder_path);
3650 error_getting_info_dialog (impl, subfolder_path, error);
3651 g_object_unref (folder);
3652 gtk_file_path_free (subfolder_path);
3656 if (gtk_file_info_get_is_folder (info))
3657 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3659 _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3661 g_object_unref (folder);
3662 gtk_file_path_free (subfolder_path);
3663 gtk_file_info_free (info);
3668 location_popup_handler (GtkFileChooserDefault *impl)
3671 GtkWidget *toplevel;
3678 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
3679 if (!GTK_WIDGET_TOPLEVEL (toplevel))
3682 dialog = gtk_dialog_new_with_buttons (_("Open Location"),
3683 GTK_WINDOW (toplevel),
3684 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
3685 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3686 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
3688 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
3689 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
3690 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
3691 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
3693 hbox = gtk_hbox_new (FALSE, 12);
3694 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
3695 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
3697 label = gtk_label_new_with_mnemonic (_("_Location:"));
3698 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3700 entry = location_entry_create (impl);
3701 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3702 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
3706 gtk_widget_show_all (dialog);
3707 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
3708 update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry));
3710 gtk_widget_destroy (dialog);
3713 /* Handler for the "up-folder" keybinding signal */
3715 up_folder_handler (GtkFileChooserDefault *impl)
3717 GtkFilePath *parent_path;
3721 if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
3723 if (parent_path) /* If we were on a root, parent_path will be NULL */
3725 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
3726 gtk_file_path_free (parent_path);
3731 _("Could not go to the parent folder of %s:\n%s"),
3732 impl->current_folder,
3736 /* Handler for the "home-folder" keybinding signal */
3738 home_folder_handler (GtkFileChooserDefault *impl)
3742 /* Should we pull this information from impl->has_home and the shortcuts data
3743 * instead? Sounds like a bit of overkill...
3746 home = g_get_home_dir ();
3747 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home);