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 /* Things common to Open and Save mode */
91 GtkFileSystemModel *tree_model;
92 GtkListStore *shortcuts_model;
93 GtkFileSystemModel *list_model;
94 GtkTreeModelSort *sort_model;
96 GtkFileFilter *current_filter;
100 gboolean has_desktop;
106 guint volumes_changed_id;
107 guint bookmarks_changed_id;
109 GtkFilePath *current_volume_path;
110 GtkFilePath *current_folder;
111 GtkFilePath *preview_path;
113 GtkWidget *new_folder_button;
115 GtkWidget *preview_frame;
117 GtkWidget *filter_combo;
118 GtkWidget *tree_scrollwin;
120 GtkWidget *shortcuts_scrollwin;
121 GtkWidget *shortcuts_tree;
122 GtkWidget *add_bookmark_button;
123 GtkWidget *remove_bookmark_button;
124 GtkWidget *list_scrollwin;
126 GtkWidget *preview_widget;
127 GtkWidget *extra_widget;
130 GtkTreeViewColumn *list_name_column;
131 GtkCellRenderer *list_name_renderer;
133 /* Things for Save mode */
135 GtkWidget *save_widgets;
138 GtkWidget *save_folder_combo;
142 guint folder_mode : 1;
143 guint local_only : 1;
144 guint preview_widget_active : 1;
145 guint select_multiple : 1;
146 guint show_hidden : 1;
147 guint list_sort_ascending : 1;
148 guint changing_folder : 1;
159 static guint signals[LAST_SIGNAL] = { 0 };
161 /* Column numbers for the shortcuts tree. Keep these in sync with create_shortcuts_model() */
163 SHORTCUTS_COL_PIXBUF,
166 SHORTCUTS_COL_REMOVABLE,
167 SHORTCUTS_COL_PIXBUF_VISIBLE,
168 SHORTCUTS_COL_NUM_COLUMNS
171 /* Column numbers for the file list */
176 FILE_LIST_COL_NUM_COLUMNS
179 /* Identifiers for target types */
184 /* Target types for DnD in the shortcuts list */
185 static GtkTargetEntry shortcuts_targets[] = {
186 { "text/uri-list", 0, TEXT_URI_LIST }
189 static const int num_shortcuts_targets = sizeof (shortcuts_targets) / sizeof (shortcuts_targets[0]);
191 /* Interesting places in the shortcuts bar */
201 /* Standard icon size */
202 /* FIXME: maybe this should correspond to the font size in the tree views... */
205 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
206 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
207 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
209 static GObject* gtk_file_chooser_default_constructor (GType type,
210 guint n_construct_properties,
211 GObjectConstructParam *construct_params);
212 static void gtk_file_chooser_default_finalize (GObject *object);
213 static void gtk_file_chooser_default_set_property (GObject *object,
217 static void gtk_file_chooser_default_get_property (GObject *object,
221 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
223 static void gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
224 const GtkFilePath *path);
225 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
226 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
228 static void gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
229 const GtkFilePath *path);
230 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
231 const GtkFilePath *path);
232 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
233 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
234 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
235 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
236 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
237 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
238 GtkFileFilter *filter);
239 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
240 GtkFileFilter *filter);
241 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
242 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
243 const GtkFilePath *path,
245 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
246 const GtkFilePath *path,
248 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
250 static void location_popup_handler (GtkFileChooserDefault *impl);
251 static void up_folder_handler (GtkFileChooserDefault *impl);
252 static void home_folder_handler (GtkFileChooserDefault *impl);
254 static void set_current_filter (GtkFileChooserDefault *impl,
255 GtkFileFilter *filter);
256 static void check_preview_change (GtkFileChooserDefault *impl);
258 static void filter_combo_changed (GtkComboBox *combo_box,
259 GtkFileChooserDefault *impl);
260 static void tree_selection_changed (GtkTreeSelection *tree_selection,
261 GtkFileChooserDefault *impl);
263 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
265 GtkTreeViewColumn *column,
266 GtkFileChooserDefault *impl);
267 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
270 gboolean path_currently_selected,
273 static void list_selection_changed (GtkTreeSelection *tree_selection,
274 GtkFileChooserDefault *impl);
275 static void list_row_activated (GtkTreeView *tree_view,
277 GtkTreeViewColumn *column,
278 GtkFileChooserDefault *impl);
280 static void path_bar_clicked (GtkPathBar *path_bar,
281 GtkFilePath *file_path,
282 GtkFileChooserDefault *impl);
284 static void add_bookmark_button_clicked_cb (GtkButton *button,
285 GtkFileChooserDefault *impl);
286 static void remove_bookmark_button_clicked_cb (GtkButton *button,
287 GtkFileChooserDefault *impl);
289 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
290 GtkCellRenderer *cell,
291 GtkTreeModel *tree_model,
294 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
295 GtkCellRenderer *cell,
296 GtkTreeModel *tree_model,
299 static void list_name_data_func (GtkTreeViewColumn *tree_column,
300 GtkCellRenderer *cell,
301 GtkTreeModel *tree_model,
305 static void list_size_data_func (GtkTreeViewColumn *tree_column,
306 GtkCellRenderer *cell,
307 GtkTreeModel *tree_model,
311 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
312 GtkCellRenderer *cell,
313 GtkTreeModel *tree_model,
317 static GObjectClass *parent_class;
320 _gtk_file_chooser_default_get_type (void)
322 static GType file_chooser_default_type = 0;
324 if (!file_chooser_default_type)
326 static const GTypeInfo file_chooser_default_info =
328 sizeof (GtkFileChooserDefaultClass),
329 NULL, /* base_init */
330 NULL, /* base_finalize */
331 (GClassInitFunc) gtk_file_chooser_default_class_init,
332 NULL, /* class_finalize */
333 NULL, /* class_data */
334 sizeof (GtkFileChooserDefault),
336 (GInstanceInitFunc) gtk_file_chooser_default_init,
339 static const GInterfaceInfo file_chooser_info =
341 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
342 NULL, /* interface_finalize */
343 NULL /* interface_data */
346 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
347 &file_chooser_default_info, 0);
348 g_type_add_interface_static (file_chooser_default_type,
349 GTK_TYPE_FILE_CHOOSER,
353 return file_chooser_default_type;
357 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
359 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
360 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
361 GtkBindingSet *binding_set;
363 parent_class = g_type_class_peek_parent (class);
365 gobject_class->finalize = gtk_file_chooser_default_finalize;
366 gobject_class->constructor = gtk_file_chooser_default_constructor;
367 gobject_class->set_property = gtk_file_chooser_default_set_property;
368 gobject_class->get_property = gtk_file_chooser_default_get_property;
370 widget_class->show_all = gtk_file_chooser_default_show_all;
372 signals[LOCATION_POPUP] =
373 _gtk_binding_signal_new ("location-popup",
374 G_OBJECT_CLASS_TYPE (class),
375 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
376 G_CALLBACK (location_popup_handler),
378 _gtk_marshal_VOID__VOID,
381 _gtk_binding_signal_new ("up-folder",
382 G_OBJECT_CLASS_TYPE (class),
383 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
384 G_CALLBACK (up_folder_handler),
386 _gtk_marshal_VOID__VOID,
388 signals[HOME_FOLDER] =
389 _gtk_binding_signal_new ("home-folder",
390 G_OBJECT_CLASS_TYPE (class),
391 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
392 G_CALLBACK (home_folder_handler),
394 _gtk_marshal_VOID__VOID,
397 binding_set = gtk_binding_set_by_class (class);
399 gtk_binding_entry_add_signal (binding_set,
400 GDK_l, GDK_CONTROL_MASK,
404 gtk_binding_entry_add_signal (binding_set,
405 GDK_Up, GDK_MOD1_MASK,
408 gtk_binding_entry_add_signal (binding_set,
409 GDK_KP_Up, GDK_MOD1_MASK,
413 gtk_binding_entry_add_signal (binding_set,
414 GDK_Home, GDK_MOD1_MASK,
417 gtk_binding_entry_add_signal (binding_set,
418 GDK_KP_Home, GDK_MOD1_MASK,
422 _gtk_file_chooser_install_properties (gobject_class);
426 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
428 iface->select_path = gtk_file_chooser_default_select_path;
429 iface->unselect_path = gtk_file_chooser_default_unselect_path;
430 iface->select_all = gtk_file_chooser_default_select_all;
431 iface->unselect_all = gtk_file_chooser_default_unselect_all;
432 iface->get_paths = gtk_file_chooser_default_get_paths;
433 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
434 iface->get_file_system = gtk_file_chooser_default_get_file_system;
435 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
436 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
437 iface->set_current_name = gtk_file_chooser_default_set_current_name;
438 iface->add_filter = gtk_file_chooser_default_add_filter;
439 iface->remove_filter = gtk_file_chooser_default_remove_filter;
440 iface->list_filters = gtk_file_chooser_default_list_filters;
441 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
442 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
443 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
447 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
449 impl->folder_mode = FALSE;
450 impl->local_only = TRUE;
451 impl->preview_widget_active = TRUE;
452 impl->select_multiple = FALSE;
453 impl->show_hidden = FALSE;
455 gtk_box_set_spacing (GTK_BOX (impl), 12);
459 gtk_file_chooser_default_finalize (GObject *object)
461 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
464 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
465 impl->volumes_changed_id = 0;
466 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
467 impl->bookmarks_changed_id = 0;
468 g_object_unref (impl->file_system);
470 for (l = impl->filters; l; l = l->next)
472 GtkFileFilter *filter;
474 filter = GTK_FILE_FILTER (l->data);
475 g_object_unref (filter);
477 g_slist_free (impl->filters);
479 if (impl->current_filter)
480 g_object_unref (impl->current_filter);
482 if (impl->current_volume_path)
483 gtk_file_path_free (impl->current_volume_path);
485 if (impl->current_folder)
486 gtk_file_path_free (impl->current_folder);
488 if (impl->preview_path)
489 gtk_file_path_free (impl->preview_path);
491 G_OBJECT_CLASS (parent_class)->finalize (object);
494 /* Shows an error dialog set as transient for the specified window */
496 error_message_with_parent (GtkWindow *parent,
501 dialog = gtk_message_dialog_new (parent,
502 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
507 gtk_dialog_run (GTK_DIALOG (dialog));
508 gtk_widget_destroy (dialog);
511 /* Shows an error dialog for the file chooser */
513 error_message (GtkFileChooserDefault *impl,
518 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
519 if (!GTK_WIDGET_TOPLEVEL (toplevel))
522 error_message_with_parent (toplevel ? GTK_WINDOW (toplevel) : NULL,
526 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
528 error_dialog (GtkFileChooserDefault *impl,
530 const GtkFilePath *path,
535 text = g_strdup_printf (msg,
536 gtk_file_path_get_string (path),
538 error_message (impl, text);
540 g_error_free (error);
543 /* Displays an error message about not being able to get information for a file.
544 * Frees the GError as well.
547 error_getting_info_dialog (GtkFileChooserDefault *impl,
548 const GtkFilePath *path,
552 _("Could not retrieve information about %s:\n%s"),
556 /* Shows an error dialog about not being able to add a bookmark */
558 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
559 const GtkFilePath *path,
563 _("Could not add a bookmark for %s:\n%s"),
567 /* Shows an error dialog about not being able to compose a filename */
569 error_building_filename_dialog (GtkFileChooserDefault *impl,
570 const GtkFilePath *base_path,
571 const char *file_part,
576 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
577 gtk_file_path_get_string (base_path),
580 error_message (impl, msg);
582 g_error_free (error);
586 update_preview_widget_visibility (GtkFileChooserDefault *impl)
588 if (impl->preview_widget_active && impl->preview_widget)
589 gtk_widget_show (impl->preview_frame);
591 gtk_widget_hide (impl->preview_frame);
595 set_preview_widget (GtkFileChooserDefault *impl,
596 GtkWidget *preview_widget)
598 if (preview_widget == impl->preview_widget)
601 if (impl->preview_widget)
602 gtk_container_remove (GTK_CONTAINER (impl->preview_frame),
603 impl->preview_widget);
605 impl->preview_widget = preview_widget;
606 if (impl->preview_widget)
608 gtk_widget_show_all (impl->preview_widget);
609 gtk_container_add (GTK_CONTAINER (impl->preview_frame),
610 impl->preview_widget);
613 update_preview_widget_visibility (impl);
616 /* Clears the selection in the shortcuts tree */
618 shortcuts_unselect_all (GtkFileChooserDefault *impl)
620 GtkTreeSelection *selection;
622 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
623 gtk_tree_selection_unselect_all (selection);
626 /* Convenience function to get the display name and icon info for a path */
628 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
630 GtkFilePath *parent_path;
631 GtkFileFolder *parent_folder;
634 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
637 parent_folder = gtk_file_system_get_folder (file_system, parent_path,
638 GTK_FILE_INFO_DISPLAY_NAME
642 | GTK_FILE_INFO_IS_FOLDER,
644 gtk_file_path_free (parent_path);
649 info = gtk_file_folder_get_info (parent_folder, path, error);
650 g_object_unref (parent_folder);
655 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
656 * inserts a volume. A position of -1 indicates the end of the tree.
659 shortcuts_insert_path (GtkFileChooserDefault *impl,
662 GtkFileSystemVolume *volume,
663 const GtkFilePath *path,
676 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
677 pixbuf = gtk_file_system_volume_render_icon (impl->file_system,
687 info = get_file_info (impl->file_system, path, error);
691 data = gtk_file_path_copy (path);
694 label_copy = g_strdup (label);
696 label_copy = g_strdup (gtk_file_info_get_display_name (info));
698 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
700 gtk_file_info_free (info);
704 gtk_list_store_append (impl->shortcuts_model, &iter);
706 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
708 gtk_list_store_set (impl->shortcuts_model, &iter,
709 SHORTCUTS_COL_PIXBUF, pixbuf,
710 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
711 SHORTCUTS_COL_NAME, label_copy,
712 SHORTCUTS_COL_PATH, data,
713 SHORTCUTS_COL_REMOVABLE, removable,
719 g_object_unref (pixbuf);
724 /* Appends an item for the user's home directory to the shortcuts model */
726 shortcuts_append_home (GtkFileChooserDefault *impl)
729 GtkFilePath *home_path;
732 home = g_get_home_dir ();
733 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
736 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
738 error_getting_info_dialog (impl, home_path, error);
740 gtk_file_path_free (home_path);
743 /* Appends the ~/Desktop directory to the shortcuts model */
745 shortcuts_append_desktop (GtkFileChooserDefault *impl)
750 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
751 path = gtk_file_system_filename_to_path (impl->file_system, name);
754 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, FALSE, NULL);
755 /* We do not actually pop up an error dialog if there is no desktop directory
756 * because some people may really not want to have one.
759 gtk_file_path_free (path);
762 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
764 shortcuts_append_paths (GtkFileChooserDefault *impl,
771 for (; paths; paths = paths->next)
779 /* NULL GError, but we don't really want to show error boxes here */
781 if (shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, TRUE, NULL))
788 /* Returns the index for the corresponding item in the shortcuts bar */
790 shortcuts_get_index (GtkFileChooserDefault *impl,
791 ShortcutsIndex where)
797 if (where == SHORTCUTS_HOME)
800 n += impl->has_home ? 1 : 0;
802 if (where == SHORTCUTS_DESKTOP)
805 n += impl->has_desktop ? 1 : 0;
807 if (where == SHORTCUTS_VOLUMES)
810 n += impl->num_volumes;
812 if (where == SHORTCUTS_SHORTCUTS)
815 n += impl->num_shortcuts;
817 if (where == SHORTCUTS_SEPARATOR)
822 if (where == SHORTCUTS_BOOKMARKS)
825 g_assert_not_reached ();
832 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
834 /* Removes the specified number of rows from the shortcuts list */
836 shortcuts_remove_rows (GtkFileChooserDefault *impl,
839 RemoveFunc remove_fn)
843 path = gtk_tree_path_new_from_indices (start_row, -1);
845 for (; n_rows; n_rows--)
850 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
851 g_assert_not_reached ();
855 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
856 (* remove_fn) (impl, data);
859 gtk_list_store_remove (impl->shortcuts_model, &iter);
862 gtk_tree_path_free (path);
865 /* Used from shortcuts_remove_rows() */
867 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
869 GtkFileSystemVolume *volume;
872 gtk_file_system_volume_free (impl->file_system, volume);
875 /* Adds all the file system volumes to the shortcuts model */
877 shortcuts_add_volumes (GtkFileChooserDefault *impl)
883 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
884 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
885 impl->num_volumes = 0;
887 list = gtk_file_system_list_volumes (impl->file_system);
891 for (l = list; l; l = l->next)
893 GtkFileSystemVolume *volume;
897 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
901 impl->num_volumes = n;
906 /* Used from shortcuts_remove_rows() */
908 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
913 gtk_file_path_free (path);
916 /* Updates the list of bookmarks */
918 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
922 shortcuts_remove_rows (impl,
923 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS),
927 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
928 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
929 gtk_file_paths_free (bookmarks);
932 /* Appends the bookmarks separator node and the bookmarks from the file system. */
934 shortcuts_append_bookmarks (GtkFileChooserDefault *impl)
938 gtk_list_store_append (impl->shortcuts_model, &iter);
939 gtk_list_store_set (impl->shortcuts_model, &iter,
940 SHORTCUTS_COL_PIXBUF, NULL,
941 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
942 SHORTCUTS_COL_NAME, NULL,
943 SHORTCUTS_COL_PATH, NULL,
945 shortcuts_add_bookmarks (impl);
948 /* Creates the GtkTreeStore used as the shortcuts model */
950 create_shortcuts_model (GtkFileChooserDefault *impl)
952 if (impl->shortcuts_model)
953 g_object_unref (impl->shortcuts_model);
955 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
956 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
957 GDK_TYPE_PIXBUF, /* pixbuf */
958 G_TYPE_STRING, /* name */
959 G_TYPE_POINTER, /* path or volume */
960 G_TYPE_BOOLEAN, /* removable */
961 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
963 if (impl->file_system)
965 shortcuts_append_home (impl);
966 shortcuts_append_desktop (impl);
967 shortcuts_add_volumes (impl);
968 shortcuts_append_bookmarks (impl);
971 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->shortcuts_tree), GTK_TREE_MODEL (impl->shortcuts_model));
974 /* Callback used when the "New Folder" toolbar button is clicked */
976 new_folder_button_clicked (GtkButton *button,
977 GtkFileChooserDefault *impl)
982 /* FIXME: this doesn't work for folder mode, just for file mode */
984 _gtk_file_system_model_add_editable (impl->list_model, &iter);
985 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
987 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->list_model), &iter);
988 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->list),
990 impl->list_name_column,
994 /* Callback used from the text cell renderer when the new folder is named */
996 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
998 const gchar *new_text,
999 GtkFileChooserDefault *impl)
1002 GtkFilePath *file_path;
1004 _gtk_file_system_model_remove_editable (impl->list_model);
1005 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1008 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1011 error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1016 if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1018 _("Could not create folder %s:\n%s"),
1021 gtk_file_path_free (file_path);
1023 /* FIXME: scroll to the new folder and select it */
1026 /* Callback used from the text cell renderer when the new folder edition gets
1030 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1031 GtkFileChooserDefault *impl)
1033 _gtk_file_system_model_remove_editable (impl->list_model);
1034 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1037 /* Creates the widgets for the filter combo box */
1039 filter_create (GtkFileChooserDefault *impl)
1041 impl->filter_combo = gtk_combo_box_new_text ();
1042 g_signal_connect (impl->filter_combo, "changed",
1043 G_CALLBACK (filter_combo_changed), impl);
1045 return impl->filter_combo;
1049 button_new (GtkFileChooserDefault *impl,
1051 const char *stock_id,
1061 button = gtk_button_new ();
1062 hbox = gtk_hbox_new (FALSE, 2);
1063 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1065 gtk_container_add (GTK_CONTAINER (button), align);
1066 gtk_container_add (GTK_CONTAINER (align), hbox);
1067 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1069 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1071 widget = gtk_label_new_with_mnemonic (text);
1072 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1073 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1075 gtk_widget_set_sensitive (button, sensitive);
1076 g_signal_connect (button, "clicked", callback, impl);
1078 gtk_widget_show_all (align);
1081 gtk_widget_show (button);
1086 /* Creates the widgets for the folder tree */
1088 create_folder_tree (GtkFileChooserDefault *impl)
1090 GtkTreeSelection *selection;
1092 /* Scrolled window */
1094 impl->tree_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1095 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
1096 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1097 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
1099 if (impl->folder_mode)
1100 gtk_widget_show (impl->tree_scrollwin);
1104 impl->tree = gtk_tree_view_new ();
1105 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->tree), FALSE);
1107 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
1108 g_signal_connect (selection, "changed",
1109 G_CALLBACK (tree_selection_changed), impl);
1111 gtk_container_add (GTK_CONTAINER (impl->tree_scrollwin), impl->tree);
1112 gtk_widget_show (impl->tree);
1116 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->tree), 0,
1118 gtk_cell_renderer_text_new (),
1119 tree_name_data_func, impl, NULL);
1120 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->tree),
1121 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1123 return impl->tree_scrollwin;
1126 /* Tries to add a bookmark from a path name */
1128 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1129 const GtkFilePath *path)
1135 info = get_file_info (impl->file_system, path, &error);
1138 error_getting_info_dialog (impl, path, error);
1139 else if (!gtk_file_info_get_is_folder (info))
1143 msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1144 gtk_file_path_get_string (path));
1145 error_message (impl, msg);
1151 if (!gtk_file_system_add_bookmark (impl->file_system, path, &error))
1152 error_could_not_add_bookmark_dialog (impl, path, error);
1156 /* Callback used when the "Add bookmark" button is clicked */
1158 add_bookmark_button_clicked_cb (GtkButton *button,
1159 GtkFileChooserDefault *impl)
1161 shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1164 /* Callback used when the "Remove bookmark" button is clicked */
1166 remove_bookmark_button_clicked_cb (GtkButton *button,
1167 GtkFileChooserDefault *impl)
1169 GtkTreeSelection *selection;
1175 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1177 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1179 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1180 SHORTCUTS_COL_PATH, &path,
1181 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1184 g_assert_not_reached ();
1189 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1191 _("Could not remove bookmark for %s:\n%s"),
1197 /* Sensitize the "add bookmark" button if the current folder is not in the
1198 * bookmarks list, or de-sensitize it otherwise.
1201 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1210 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1214 separator_idx = shortcuts_get_index (impl, SHORTCUTS_SEPARATOR);
1215 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1223 if (i == separator_idx)
1226 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1228 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1230 GtkFileSystemVolume *volume;
1231 GtkFilePath *base_path;
1234 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1236 exists = strcmp (gtk_file_path_get_string (impl->current_folder),
1237 gtk_file_path_get_string (base_path)) == 0;
1249 if (path && gtk_file_path_compare (path, impl->current_folder) == 0)
1256 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
1259 gtk_widget_set_sensitive (impl->add_bookmark_button, !exists);
1262 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1263 * bookmark row is selected in the shortcuts tree.
1266 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1268 GtkTreeSelection *selection;
1270 gboolean removable = FALSE;
1272 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1274 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1275 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1276 SHORTCUTS_COL_REMOVABLE, &removable,
1279 gtk_widget_set_sensitive (impl->remove_bookmark_button, removable);
1282 /* Converts raw selection data from text/uri-list to a list of strings */
1284 split_uris (const char *data)
1287 const char *p, *start;
1293 for (p = start; *p != 0; p++)
1294 if (*p == '\r' && *(p + 1) == '\n')
1298 name = g_strndup (start, p - start);
1299 uris = g_slist_prepend (uris, name);
1305 uris = g_slist_reverse (uris);
1309 /* Callback used when we get the drag data for the bookmarks list. We add the
1310 * received URIs as bookmarks if they are folders.
1313 shortcuts_drag_data_received_cb (GtkWidget *widget,
1314 GdkDragContext *context,
1317 GtkSelectionData *selection_data,
1322 GtkFileChooserDefault *impl;
1325 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1327 uris = split_uris (selection_data->data);
1329 for (l = uris; l; l = l->next)
1335 path = gtk_file_system_uri_to_path (impl->file_system, uri);
1339 shortcuts_add_bookmark_from_path (impl, path);
1340 gtk_file_path_free (path);
1346 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1348 error_message (impl, msg);
1355 g_slist_free (uris);
1358 /* Callback used when the selection in the shortcuts tree changes */
1360 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
1361 GtkFileChooserDefault *impl)
1363 bookmarks_check_remove_sensitivity (impl);
1366 /* Creates the widgets for the shortcuts and bookmarks tree */
1368 shortcuts_tree_create (GtkFileChooserDefault *impl)
1370 GtkTreeSelection *selection;
1371 GtkTreeViewColumn *column;
1372 GtkCellRenderer *renderer;
1374 /* Scrolled window */
1376 impl->shortcuts_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1377 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
1378 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1379 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
1381 gtk_widget_show (impl->shortcuts_scrollwin);
1385 impl->shortcuts_tree = gtk_tree_view_new ();
1386 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->shortcuts_tree), FALSE);
1388 gtk_drag_dest_set (impl->shortcuts_tree,
1389 GTK_DEST_DEFAULT_ALL,
1391 num_shortcuts_targets,
1394 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
1395 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1396 gtk_tree_selection_set_select_function (selection,
1397 shortcuts_select_func,
1400 g_signal_connect (selection, "changed",
1401 G_CALLBACK (shortcuts_selection_changed_cb), impl);
1403 g_signal_connect (impl->shortcuts_tree, "row-activated",
1404 G_CALLBACK (shortcuts_row_activated_cb), impl);
1406 g_signal_connect (impl->shortcuts_tree, "drag-data-received",
1407 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1409 gtk_container_add (GTK_CONTAINER (impl->shortcuts_scrollwin), impl->shortcuts_tree);
1410 gtk_widget_show (impl->shortcuts_tree);
1414 create_shortcuts_model (impl);
1418 column = gtk_tree_view_column_new ();
1419 gtk_tree_view_column_set_title (column, _("Folder"));
1421 renderer = gtk_cell_renderer_pixbuf_new ();
1422 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1423 gtk_tree_view_column_set_attributes (column, renderer,
1424 "pixbuf", SHORTCUTS_COL_PIXBUF,
1425 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
1428 renderer = _gtk_cell_renderer_sep_text_new ();
1429 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1430 gtk_tree_view_column_set_attributes (column, renderer,
1431 "text", SHORTCUTS_COL_NAME,
1434 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->shortcuts_tree), column);
1436 return impl->shortcuts_scrollwin;
1439 /* Creates the widgets for the shortcuts/bookmarks pane */
1441 shortcuts_pane_create (GtkFileChooserDefault *impl,
1442 GtkSizeGroup *size_group)
1448 vbox = gtk_vbox_new (FALSE, 6);
1449 gtk_widget_show (vbox);
1451 /* Shortcuts tree */
1453 widget = shortcuts_tree_create (impl);
1454 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1456 /* Box for buttons */
1458 hbox = gtk_hbox_new (TRUE, 6);
1459 gtk_size_group_add_widget (size_group, hbox);
1460 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1461 gtk_widget_show (hbox);
1463 /* Add bookmark button */
1465 impl->add_bookmark_button = button_new (impl,
1470 G_CALLBACK (add_bookmark_button_clicked_cb));
1471 gtk_box_pack_start (GTK_BOX (hbox), impl->add_bookmark_button, TRUE, TRUE, 0);
1473 /* Remove bookmark button */
1475 impl->remove_bookmark_button = button_new (impl,
1480 G_CALLBACK (remove_bookmark_button_clicked_cb));
1481 gtk_box_pack_start (GTK_BOX (hbox), impl->remove_bookmark_button, TRUE, TRUE, 0);
1486 /* Creates the widgets for the file list */
1488 create_file_list (GtkFileChooserDefault *impl)
1490 GtkTreeSelection *selection;
1491 GtkTreeViewColumn *column;
1492 GtkCellRenderer *renderer;
1494 /* Scrolled window */
1496 impl->list_scrollwin = gtk_scrolled_window_new (NULL, NULL);
1497 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
1498 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1499 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
1501 if (!impl->folder_mode)
1502 gtk_widget_show (impl->list_scrollwin);
1504 /* Tree/list view */
1506 impl->list = gtk_tree_view_new ();
1507 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->list), TRUE);
1508 gtk_container_add (GTK_CONTAINER (impl->list_scrollwin), impl->list);
1509 g_signal_connect (impl->list, "row_activated",
1510 G_CALLBACK (list_row_activated), impl);
1511 gtk_widget_show (impl->list);
1513 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1514 g_signal_connect (selection, "changed",
1515 G_CALLBACK (list_selection_changed), impl);
1517 /* Filename column */
1519 impl->list_name_column = gtk_tree_view_column_new ();
1520 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1521 gtk_tree_view_column_set_title (impl->list_name_column, _("File name"));
1522 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1524 renderer = gtk_cell_renderer_pixbuf_new ();
1525 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1526 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1527 list_icon_data_func, impl, NULL);
1529 impl->list_name_renderer = gtk_cell_renderer_text_new ();
1530 g_signal_connect (impl->list_name_renderer, "edited",
1531 G_CALLBACK (renderer_edited_cb), impl);
1532 g_signal_connect (impl->list_name_renderer, "editing-canceled",
1533 G_CALLBACK (renderer_editing_canceled_cb), impl);
1534 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1535 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1536 list_name_data_func, impl, NULL);
1538 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), impl->list_name_column);
1542 column = gtk_tree_view_column_new ();
1543 gtk_tree_view_column_set_title (column, _("Size"));
1545 renderer = gtk_cell_renderer_text_new ();
1546 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1547 gtk_tree_view_column_set_cell_data_func (column, renderer,
1548 list_size_data_func, impl, NULL);
1549 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1550 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
1552 /* Modification time column */
1554 column = gtk_tree_view_column_new ();
1555 gtk_tree_view_column_set_title (column, _("Modified"));
1557 renderer = gtk_cell_renderer_text_new ();
1558 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1559 gtk_tree_view_column_set_cell_data_func (column, renderer,
1560 list_mtime_data_func, impl, NULL);
1561 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1562 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
1564 return impl->list_scrollwin;
1568 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1573 hbox = gtk_hbox_new (FALSE, 12);
1574 gtk_widget_show (hbox);
1578 widget = filter_create (impl);
1579 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1584 /* Creates the widgets for the files/folders pane */
1586 file_pane_create (GtkFileChooserDefault *impl,
1587 GtkSizeGroup *size_group)
1593 vbox = gtk_vbox_new (FALSE, 6);
1594 gtk_widget_show (vbox);
1596 /* The path bar and 'Create Folder' button */
1597 hbox = gtk_hbox_new (FALSE, 12);
1598 gtk_widget_show (hbox);
1599 impl->path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
1600 g_signal_connect (impl->path_bar, "path_clicked", G_CALLBACK (path_bar_clicked), impl);
1601 gtk_widget_show_all (impl->path_bar);
1602 gtk_box_pack_start (GTK_BOX (hbox), impl->path_bar, TRUE, TRUE, 0);
1605 impl->new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
1606 g_signal_connect (impl->new_folder_button, "clicked",
1607 G_CALLBACK (new_folder_button_clicked), impl);
1608 gtk_box_pack_end (GTK_BOX (hbox), impl->new_folder_button, FALSE, FALSE, 0);
1610 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
1612 gtk_widget_show (impl->new_folder_button);
1613 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1615 /* Box for lists and preview */
1617 hbox = gtk_hbox_new (FALSE, 12);
1618 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1619 gtk_widget_show (hbox);
1623 widget = create_folder_tree (impl);
1624 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1628 widget = create_file_list (impl);
1629 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1633 impl->preview_frame = gtk_frame_new (_("Preview"));
1634 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_frame, FALSE, FALSE, 0);
1635 /* Don't show preview frame initially */
1637 /* Filename entry and filter combo */
1638 hbox = gtk_hbox_new (FALSE, 0);
1639 gtk_size_group_add_widget (size_group, hbox);
1640 widget = create_filename_entry_and_filter_combo (impl);
1641 gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1642 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1643 gtk_widget_show (hbox);
1647 /* Callback used when the "Browse for more folders" expander is toggled */
1649 expander_activate_cb (GtkExpander *expander,
1650 GtkFileChooserDefault *impl)
1654 active = gtk_expander_get_expanded (expander);
1657 gtk_widget_show (impl->hpaned);
1659 gtk_widget_hide (impl->hpaned);
1662 /* Creates the widgets specific to Save mode */
1664 save_widgets_create (GtkFileChooserDefault *impl)
1668 GtkWidget *alignment;
1670 table = gtk_table_new (3, 2, FALSE);
1671 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1672 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1676 widget = gtk_label_new_with_mnemonic (_("_Name:"));
1677 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
1678 gtk_table_attach (GTK_TABLE (table), widget,
1682 gtk_widget_show (widget);
1684 impl->entry = _gtk_file_chooser_entry_new ();
1685 gtk_entry_set_activates_default (GTK_ENTRY (impl->entry), TRUE);
1686 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->entry), impl->file_system);
1687 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->entry), impl->current_folder);
1689 gtk_table_attach (GTK_TABLE (table), impl->entry,
1691 GTK_EXPAND | GTK_FILL, 0,
1693 gtk_widget_show (impl->entry);
1694 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->entry);
1698 widget = gtk_label_new_with_mnemonic (_("Save in _Folder:"));
1699 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
1700 gtk_table_attach (GTK_TABLE (table), widget,
1704 gtk_widget_show (widget);
1706 /* FIXME: create the combo */
1710 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
1711 gtk_table_attach (GTK_TABLE (table), alignment,
1715 gtk_widget_show (alignment);
1717 widget = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
1718 gtk_container_add (GTK_CONTAINER (alignment), widget);
1719 gtk_widget_show (widget);
1720 g_signal_connect (widget, "activate",
1721 G_CALLBACK (expander_activate_cb),
1727 /* Creates the main hpaned with the widgets shared by Open and Save mode */
1729 main_paned_create (GtkFileChooserDefault *impl)
1733 GtkSizeGroup *size_group;
1735 /* size group is used by the [+][-] buttons, as well as the filter. */
1736 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
1740 hpaned = gtk_hpaned_new ();
1741 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
1743 /* Shortcuts pane */
1745 widget = shortcuts_pane_create (impl, size_group);
1746 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
1748 /* File/folder pane */
1750 widget = file_pane_create (impl, size_group);
1751 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
1757 gtk_file_chooser_default_constructor (GType type,
1758 guint n_construct_properties,
1759 GObjectConstructParam *construct_params)
1761 GtkFileChooserDefault *impl;
1763 GtkWidget *show_widget;
1765 object = parent_class->constructor (type,
1766 n_construct_properties,
1768 impl = GTK_FILE_CHOOSER_DEFAULT (object);
1770 g_assert (impl->file_system);
1772 gtk_widget_push_composite_child ();
1774 /* Widgets for Save mode */
1776 impl->save_widgets = save_widgets_create (impl);
1777 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
1779 /* Widgets for Open and Save mode */
1781 impl->hpaned = main_paned_create (impl);
1782 gtk_box_pack_start (GTK_BOX (impl), impl->hpaned, TRUE, TRUE, 0);
1784 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
1785 show_widget = impl->hpaned;
1787 show_widget = impl->save_widgets;
1789 gtk_widget_show (show_widget);
1791 gtk_widget_pop_composite_child ();
1796 /* Sets the extra_widget by packing it in the appropriate place */
1798 set_extra_widget (GtkFileChooserDefault *impl,
1799 GtkWidget *extra_widget)
1801 if (extra_widget == impl->extra_widget)
1804 if (impl->extra_widget)
1805 gtk_container_remove (GTK_CONTAINER (impl), impl->extra_widget);
1807 impl->extra_widget = extra_widget;
1808 if (impl->extra_widget)
1810 gtk_widget_show_all (impl->extra_widget);
1811 gtk_box_pack_end (GTK_BOX (impl), impl->extra_widget, FALSE, FALSE, 0);
1816 volumes_changed_cb (GtkFileSystem *file_system,
1817 GtkFileChooserDefault *impl)
1819 shortcuts_add_volumes (impl);
1822 /* Callback used when the set of bookmarks changes in the file system */
1824 bookmarks_changed_cb (GtkFileSystem *file_system,
1825 GtkFileChooserDefault *impl)
1827 shortcuts_add_bookmarks (impl);
1829 bookmarks_check_add_sensitivity (impl);
1830 bookmarks_check_remove_sensitivity (impl);
1833 /* Sets the file chooser to multiple selection mode */
1835 set_select_multiple (GtkFileChooserDefault *impl,
1836 gboolean select_multiple,
1837 gboolean property_notify)
1839 GtkTreeSelection *selection;
1840 GtkSelectionMode mode;
1842 if (select_multiple == impl->select_multiple)
1845 impl->select_multiple = select_multiple;
1847 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
1849 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
1850 gtk_tree_selection_set_mode (selection, mode);
1852 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1853 gtk_tree_selection_set_mode (selection, mode);
1855 g_object_notify (G_OBJECT (impl), "select-multiple");
1857 /* FIXME #132255: See note in check_preview_change() */
1858 check_preview_change (impl);
1862 set_file_system_backend (GtkFileChooserDefault *impl,
1863 const char *backend)
1865 if (impl->file_system)
1867 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
1868 impl->volumes_changed_id = 0;
1869 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
1870 impl->bookmarks_changed_id = 0;
1871 g_object_unref (impl->file_system);
1874 impl->file_system = NULL;
1876 impl->file_system = _gtk_file_system_create (backend);
1878 if (!impl->file_system)
1880 #if defined (G_OS_UNIX)
1881 impl->file_system = gtk_file_system_unix_new ();
1882 #elif defined (G_OS_WIN32)
1883 impl->file_system = gtk_file_system_win32_new ();
1885 #error "No default filesystem implementation on the platform"
1889 if (impl->file_system)
1891 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
1892 G_CALLBACK (volumes_changed_cb),
1894 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
1895 G_CALLBACK (bookmarks_changed_cb),
1901 gtk_file_chooser_default_set_property (GObject *object,
1903 const GValue *value,
1907 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
1911 case GTK_FILE_CHOOSER_PROP_ACTION:
1912 impl->action = g_value_get_enum (value);
1913 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1915 gtk_widget_hide (impl->hpaned);
1916 gtk_widget_show (impl->save_widgets);
1917 gtk_widget_show (impl->new_folder_button);
1919 if (impl->select_multiple)
1921 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
1922 "Re-setting to single selection mode.");
1923 set_select_multiple (impl, FALSE, TRUE);
1928 gtk_widget_hide (impl->save_widgets);
1929 gtk_widget_show (impl->hpaned);
1931 if (impl->folder_mode)
1932 gtk_widget_show (impl->new_folder_button);
1934 gtk_widget_hide (impl->new_folder_button);
1938 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
1939 set_file_system_backend (impl, g_value_get_string (value));
1941 case GTK_FILE_CHOOSER_PROP_FILTER:
1942 set_current_filter (impl, g_value_get_object (value));
1944 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
1946 gboolean folder_mode = g_value_get_boolean (value);
1947 if (folder_mode != impl->folder_mode)
1949 impl->folder_mode = folder_mode;
1950 if (impl->folder_mode)
1952 gtk_widget_hide (impl->list_scrollwin);
1953 gtk_widget_show (impl->tree_scrollwin);
1954 gtk_widget_show (impl->new_folder_button);
1958 gtk_widget_hide (impl->tree_scrollwin);
1959 gtk_widget_show (impl->list_scrollwin);
1960 gtk_widget_hide (impl->new_folder_button);
1965 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1966 impl->local_only = g_value_get_boolean (value);
1968 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1969 set_preview_widget (impl, g_value_get_object (value));
1971 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1972 impl->preview_widget_active = g_value_get_boolean (value);
1973 update_preview_widget_visibility (impl);
1975 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1976 set_extra_widget (impl, g_value_get_object (value));
1978 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1980 gboolean select_multiple = g_value_get_boolean (value);
1981 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
1983 g_warning ("Multiple selection mode is not allowed in Save mode");
1987 set_select_multiple (impl, select_multiple, FALSE);
1990 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1992 gboolean show_hidden = g_value_get_boolean (value);
1993 if (show_hidden != impl->show_hidden)
1995 impl->show_hidden = show_hidden;
1996 _gtk_file_system_model_set_show_hidden (impl->tree_model, show_hidden);
1997 _gtk_file_system_model_set_show_hidden (impl->list_model, show_hidden);
2002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2008 gtk_file_chooser_default_get_property (GObject *object,
2013 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2017 case GTK_FILE_CHOOSER_PROP_ACTION:
2018 g_value_set_enum (value, impl->action);
2020 case GTK_FILE_CHOOSER_PROP_FILTER:
2021 g_value_set_object (value, impl->current_filter);
2023 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
2024 g_value_set_boolean (value, impl->folder_mode);
2026 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2027 g_value_set_boolean (value, impl->local_only);
2029 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2030 g_value_set_object (value, impl->preview_widget);
2032 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2033 g_value_set_boolean (value, impl->preview_widget_active);
2035 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2036 g_value_set_object (value, impl->extra_widget);
2038 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2039 g_value_set_boolean (value, impl->select_multiple);
2041 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2042 g_value_set_boolean (value, impl->show_hidden);
2045 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2050 /* We override show-all since we have internal widgets that
2051 * shouldn't be shown when you call show_all(), like the filter
2055 gtk_file_chooser_default_show_all (GtkWidget *widget)
2057 gtk_widget_show (widget);
2061 expand_and_select_func (GtkFileSystemModel *model,
2066 GtkFileChooserDefault *impl = user_data;
2067 GtkTreeView *tree_view;
2069 if (model == impl->tree_model)
2070 tree_view = GTK_TREE_VIEW (impl->tree);
2072 tree_view = GTK_TREE_VIEW (impl->list);
2074 gtk_tree_view_expand_to_path (tree_view, path);
2075 gtk_tree_view_expand_row (tree_view, path, FALSE);
2076 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
2077 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), path, NULL, TRUE, 0.3, 0.5);
2081 list_model_filter_func (GtkFileSystemModel *model,
2083 const GtkFileInfo *file_info,
2086 GtkFileChooserDefault *impl = user_data;
2087 GtkFileFilterInfo filter_info;
2088 GtkFileFilterFlags needed;
2091 if (!impl->current_filter)
2094 if (gtk_file_info_get_is_folder (file_info))
2097 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
2099 needed = gtk_file_filter_get_needed (impl->current_filter);
2101 filter_info.display_name = gtk_file_info_get_display_name (file_info);
2102 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
2104 if (needed & GTK_FILE_FILTER_FILENAME)
2106 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
2107 if (filter_info.filename)
2108 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
2111 filter_info.filename = NULL;
2113 if (needed & GTK_FILE_FILTER_URI)
2115 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
2116 if (filter_info.filename)
2117 filter_info.contains |= GTK_FILE_FILTER_URI;
2120 filter_info.uri = NULL;
2122 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2124 if (filter_info.filename)
2125 g_free ((gchar *)filter_info.filename);
2126 if (filter_info.uri)
2127 g_free ((gchar *)filter_info.uri);
2133 install_list_model_filter (GtkFileChooserDefault *impl)
2135 if (impl->current_filter)
2136 _gtk_file_system_model_set_filter (impl->list_model,
2137 list_model_filter_func,
2141 #define COMPARE_DIRECTORIES \
2142 GtkFileChooserDefault *impl = user_data; \
2143 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->list_model, a); \
2144 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->list_model, b); \
2145 gboolean dir_a, dir_b; \
2148 dir_a = gtk_file_info_get_is_folder (info_a); \
2150 return impl->list_sort_ascending ? -1 : 1; \
2153 dir_b = gtk_file_info_get_is_folder (info_b); \
2155 return impl->list_sort_ascending ? 1 : -1; \
2157 if (dir_a != dir_b) \
2158 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2160 /* Sort callback for the filename column */
2162 name_sort_func (GtkTreeModel *model,
2167 COMPARE_DIRECTORIES;
2169 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2172 /* Sort callback for the size column */
2174 size_sort_func (GtkTreeModel *model,
2179 COMPARE_DIRECTORIES;
2182 gint64 size_a = gtk_file_info_get_size (info_a);
2183 gint64 size_b = gtk_file_info_get_size (info_b);
2185 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2189 /* Sort callback for the mtime column */
2191 mtime_sort_func (GtkTreeModel *model,
2196 COMPARE_DIRECTORIES;
2199 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2200 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2202 return ta > tb ? -1 : (ta == tb ? 0 : 1);
2206 /* Callback used when the sort column changes. We cache the sort order for use
2207 * in name_sort_func().
2210 list_sort_column_changed_cb (GtkTreeSortable *sortable,
2211 GtkFileChooserDefault *impl)
2213 GtkSortType sort_type;
2215 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2216 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2219 /* Gets rid of the old list model and creates a new one for the current folder */
2221 set_list_model (GtkFileChooserDefault *impl)
2223 if (impl->list_model)
2225 g_object_unref (impl->list_model);
2226 g_object_unref (impl->sort_model);
2229 impl->list_model = _gtk_file_system_model_new (impl->file_system,
2230 impl->current_folder, 0,
2232 _gtk_file_system_model_set_show_hidden (impl->list_model, impl->show_hidden);
2233 install_list_model_filter (impl);
2235 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->list_model));
2236 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2237 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2238 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2239 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2240 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2241 impl->list_sort_ascending = TRUE;
2243 g_signal_connect (impl->sort_model, "sort_column_changed",
2244 G_CALLBACK (list_sort_column_changed_cb), impl);
2246 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->list),
2247 GTK_TREE_MODEL (impl->sort_model));
2248 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->list));
2249 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->list),
2250 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2253 /* Gets rid of the old folder tree model and creates a new one for the volume
2254 * corresponding to the specified path.
2257 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2259 GtkFileSystemVolume *volume;
2260 GtkFilePath *base_path, *parent_path;
2264 volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2267 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2269 if (base_path == NULL)
2271 base_path = gtk_file_path_copy (path);
2272 while (gtk_file_system_get_parent (impl->file_system,
2276 parent_path != NULL)
2278 gtk_file_path_free (base_path);
2279 base_path = parent_path;
2283 if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2286 if (impl->tree_model)
2287 g_object_unref (impl->tree_model);
2289 impl->current_volume_path = gtk_file_path_copy (base_path);
2291 impl->tree_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2292 GTK_FILE_INFO_DISPLAY_NAME);
2293 _gtk_file_system_model_set_show_files (impl->tree_model, FALSE);
2294 _gtk_file_system_model_set_show_hidden (impl->tree_model, impl->show_hidden);
2296 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->tree),
2297 GTK_TREE_MODEL (impl->tree_model));
2301 gtk_file_path_free (base_path);
2303 gtk_file_system_volume_free (impl->file_system, volume);
2307 update_chooser_entry (GtkFileChooserDefault *impl)
2309 GtkTreeSelection *selection;
2310 const GtkFileInfo *info;
2312 GtkTreeIter child_iter;
2314 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
2317 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2319 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2322 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2326 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
2328 if (!gtk_file_info_get_is_folder (info))
2329 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry),
2330 gtk_file_info_get_display_name (info));
2334 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
2335 const GtkFilePath *path)
2337 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2339 if (impl->current_folder)
2340 gtk_file_path_free (impl->current_folder);
2342 impl->current_folder = gtk_file_path_copy (path);
2344 /* Change the current folder label */
2345 gtk_path_bar_set_path (GTK_PATH_BAR (impl->path_bar), path, impl->file_system, NULL);
2347 /* Update the folder tree */
2349 if (!impl->changing_folder)
2351 impl->changing_folder = TRUE;
2352 set_tree_model (impl, impl->current_folder);
2353 _gtk_file_system_model_path_do (impl->tree_model, path,
2354 expand_and_select_func, impl);
2355 impl->changing_folder = FALSE;
2358 /* Notify the save entry */
2360 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->entry), impl->current_folder);
2362 /* Create a new list model */
2363 set_list_model (impl);
2365 /* Refresh controls */
2367 shortcuts_unselect_all (impl);
2369 g_signal_emit_by_name (impl, "current-folder-changed", 0);
2371 update_chooser_entry (impl);
2372 check_preview_change (impl);
2373 bookmarks_check_add_sensitivity (impl);
2375 g_signal_emit_by_name (impl, "selection-changed", 0);
2378 static GtkFilePath *
2379 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2381 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2383 return gtk_file_path_copy (impl->current_folder);
2387 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2390 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2392 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
2394 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry), name);
2398 select_func (GtkFileSystemModel *model,
2403 GtkFileChooserDefault *impl = user_data;
2404 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
2405 GtkTreePath *sorted_path;
2407 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2408 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2409 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), sorted_path, NULL, TRUE, 0.3, 0.0);
2410 gtk_tree_path_free (sorted_path);
2414 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
2415 const GtkFilePath *path)
2417 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2418 GtkFilePath *parent_path;
2422 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2424 error_getting_info_dialog (impl, path, error);
2430 _gtk_file_chooser_set_current_folder_path (chooser, path);
2434 _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2435 gtk_file_path_free (parent_path);
2436 _gtk_file_system_model_path_do (impl->list_model, path,
2442 unselect_func (GtkFileSystemModel *model,
2447 GtkFileChooserDefault *impl = user_data;
2448 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
2449 GtkTreePath *sorted_path;
2451 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2453 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2455 gtk_tree_path_free (sorted_path);
2459 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
2460 const GtkFilePath *path)
2462 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2464 _gtk_file_system_model_path_do (impl->list_model, path,
2465 unselect_func, impl);
2469 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2471 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2472 if (impl->select_multiple)
2474 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2475 gtk_tree_selection_select_all (selection);
2480 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2482 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2483 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2485 gtk_tree_selection_unselect_all (selection);
2488 struct get_paths_closure {
2489 GtkFileChooserDefault *impl;
2491 GtkFilePath *path_from_entry;
2495 get_paths_foreach (GtkTreeModel *model,
2500 struct get_paths_closure *info;
2501 const GtkFilePath *file_path;
2502 GtkFileSystemModel *fs_model;
2503 GtkTreeIter sel_iter;
2507 if (info->impl->folder_mode)
2509 fs_model = info->impl->tree_model;
2514 fs_model = info->impl->list_model;
2515 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
2518 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
2520 if (!info->path_from_entry
2521 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
2522 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
2526 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
2528 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2529 struct get_paths_closure info;
2533 info.path_from_entry = NULL;
2535 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2537 GtkFileChooserEntry *chooser_entry;
2538 const GtkFilePath *folder_path;
2539 const gchar *file_part;
2541 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->entry);
2542 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
2543 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
2545 if (file_part != NULL && file_part[0] != '\0')
2547 GtkFilePath *selected;
2548 GError *error = NULL;
2550 selected = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
2554 error_building_filename_dialog (impl, folder_path, file_part, error);
2558 info.path_from_entry = selected;
2562 if (!info.path_from_entry || impl->select_multiple)
2564 GtkTreeSelection *selection;
2568 if (impl->folder_mode)
2570 if (impl->tree_model)
2571 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
2575 if (impl->sort_model)
2576 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2580 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
2583 if (info.path_from_entry)
2584 info.result = g_slist_prepend (info.result, info.path_from_entry);
2586 return g_slist_reverse (info.result);
2589 static GtkFilePath *
2590 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
2592 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2594 if (impl->preview_path)
2595 return gtk_file_path_copy (impl->preview_path);
2600 static GtkFileSystem *
2601 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
2603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2605 return impl->file_system;
2608 /* Shows or hides the filter widgets */
2610 toolbar_show_filters (GtkFileChooserDefault *impl,
2614 gtk_widget_show (impl->filter_combo);
2616 gtk_widget_hide (impl->filter_combo);
2620 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
2621 GtkFileFilter *filter)
2623 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2626 if (g_slist_find (impl->filters, filter))
2628 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
2632 g_object_ref (filter);
2633 gtk_object_sink (GTK_OBJECT (filter));
2634 impl->filters = g_slist_append (impl->filters, filter);
2636 name = gtk_file_filter_get_name (filter);
2638 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
2640 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
2642 if (!g_slist_find (impl->filters, impl->current_filter))
2643 set_current_filter (impl, filter);
2645 toolbar_show_filters (impl, TRUE);
2649 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
2650 GtkFileFilter *filter)
2652 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2653 GtkTreeModel *model;
2657 filter_index = g_slist_index (impl->filters, filter);
2659 if (filter_index < 0)
2661 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
2665 impl->filters = g_slist_remove (impl->filters, filter);
2667 if (filter == impl->current_filter)
2670 set_current_filter (impl, impl->filters->data);
2672 set_current_filter (impl, NULL);
2675 /* Remove row from the combo box */
2676 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
2677 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
2678 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
2680 g_object_unref (filter);
2683 toolbar_show_filters (impl, FALSE);
2687 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
2689 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2691 return g_slist_copy (impl->filters);
2694 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
2696 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
2699 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
2703 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
2704 const GtkFilePath *path,
2707 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2711 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
2713 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
2716 impl->num_shortcuts++;
2722 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
2723 const GtkFilePath *path,
2726 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2731 if (impl->num_shortcuts == 0)
2734 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
2735 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
2736 g_assert_not_reached ();
2738 for (i = 0; i < impl->num_shortcuts; i++)
2740 GtkFilePath *shortcut;
2742 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
2743 g_assert (shortcut != NULL);
2745 if (gtk_file_path_compare (shortcut, path) == 0)
2747 /* The other columns are freed by the GtkTreeStore */
2748 gtk_file_path_free (shortcut);
2749 gtk_list_store_remove (impl->shortcuts_model, &iter);
2750 impl->num_shortcuts--;
2754 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2755 g_assert_not_reached ();
2761 GTK_FILE_CHOOSER_ERROR,
2762 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
2763 _("shortcut %s does not exist"),
2764 gtk_file_path_get_string (path));
2770 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
2772 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2778 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
2779 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
2780 g_assert_not_reached ();
2784 for (i = 0; i < impl->num_shortcuts; i++)
2786 GtkFilePath *shortcut;
2788 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
2789 g_assert (shortcut != NULL);
2791 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
2793 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2794 g_assert_not_reached ();
2797 return g_slist_reverse (list);
2801 set_current_filter (GtkFileChooserDefault *impl,
2802 GtkFileFilter *filter)
2804 if (impl->current_filter != filter)
2808 /* If we have filters, new filter must be one of them
2810 filter_index = g_slist_index (impl->filters, filter);
2811 if (impl->filters && filter_index < 0)
2814 if (impl->current_filter)
2815 g_object_unref (impl->current_filter);
2816 impl->current_filter = filter;
2817 if (impl->current_filter)
2819 g_object_ref (impl->current_filter);
2820 gtk_object_sink (GTK_OBJECT (filter));
2824 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
2827 install_list_model_filter (impl);
2829 g_object_notify (G_OBJECT (impl), "filter");
2834 open_and_close (GtkTreeView *tree_view,
2835 GtkTreePath *target_path)
2837 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2841 path = gtk_tree_path_new ();
2842 gtk_tree_path_append_index (path, 0);
2844 gtk_tree_model_get_iter (model, &iter, path);
2848 if (gtk_tree_path_is_ancestor (path, target_path) ||
2849 gtk_tree_path_compare (path, target_path) == 0)
2851 GtkTreeIter child_iter;
2852 gtk_tree_view_expand_row (tree_view, path, FALSE);
2853 if (gtk_tree_model_iter_children (model, &child_iter, &iter))
2856 gtk_tree_path_down (path);
2861 gtk_tree_view_collapse_row (tree_view, path);
2865 GtkTreeIter parent_iter;
2866 GtkTreeIter next_iter;
2869 if (gtk_tree_model_iter_next (model, &next_iter))
2872 gtk_tree_path_next (path);
2876 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
2880 gtk_tree_path_up (path);
2887 gtk_tree_path_free (path);
2891 filter_combo_changed (GtkComboBox *combo_box,
2892 GtkFileChooserDefault *impl)
2894 gint new_index = gtk_combo_box_get_active (combo_box);
2895 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
2897 set_current_filter (impl, new_filter);
2901 check_preview_change (GtkFileChooserDefault *impl)
2903 const GtkFilePath *new_path = NULL;
2905 /* FIXME #132255: Fixing preview for multiple selection involves getting the
2906 * full selection and diffing to find out what the most recently selected file
2907 * is; there is logic in GtkFileSelection that probably can be
2910 if (impl->sort_model && !impl->select_multiple)
2912 GtkTreeSelection *selection;
2915 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
2916 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
2918 GtkTreeIter child_iter;
2920 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2921 &child_iter, &iter);
2923 new_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
2927 if (new_path != impl->preview_path &&
2928 !(new_path && impl->preview_path &&
2929 gtk_file_path_compare (new_path, impl->preview_path) == 0))
2931 if (impl->preview_path)
2932 gtk_file_path_free (impl->preview_path);
2935 impl->preview_path = gtk_file_path_copy (new_path);
2937 impl->preview_path = NULL;
2939 g_signal_emit_by_name (impl, "update-preview");
2944 tree_selection_changed (GtkTreeSelection *selection,
2945 GtkFileChooserDefault *impl)
2948 const GtkFilePath *file_path;
2951 /* FIXME #132255: Fixing this for multiple selection involves getting the full
2952 * selection and diffing to find out what the most recently selected file is;
2953 * there is logic in GtkFileSelection that probably can be copied;
2954 * check_preview_change() is similar.
2956 if (impl->select_multiple
2957 || !gtk_tree_selection_get_selected (selection, NULL, &iter))
2960 file_path = _gtk_file_system_model_get_path (impl->tree_model, &iter);
2961 if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
2964 /* Close the tree up to only the parents of the newly selected
2965 * node and it's immediate children are visible.
2967 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->tree_model), &iter);
2968 open_and_close (GTK_TREE_VIEW (impl->tree), path);
2969 gtk_tree_path_free (path);
2971 if (!impl->changing_folder)
2972 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
2975 /* Activates a volume by mounting it if necessary and then switching to its
2979 shortcuts_activate_volume (GtkFileChooserDefault *impl,
2980 GtkFileSystemVolume *volume)
2984 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
2989 if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
2993 msg = g_strdup_printf ("Could not mount %s:\n%s",
2994 gtk_file_system_volume_get_display_name (impl->file_system, volume),
2996 error_message (impl, msg);
2998 g_error_free (error);
3004 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
3005 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
3006 gtk_file_path_free (path);
3009 /* Callback used when a row in the shortcuts list is activated */
3011 shortcuts_row_activated_cb (GtkTreeView *tree_view,
3013 GtkTreeViewColumn *column,
3014 GtkFileChooserDefault *impl)
3017 int selected, start_row;
3020 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3023 selected = *gtk_tree_path_get_indices (path);
3025 if (selected == shortcuts_get_index (impl, SHORTCUTS_SEPARATOR))
3028 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
3030 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
3031 if (selected >= start_row && selected < start_row + impl->num_volumes)
3033 GtkFileSystemVolume *volume;
3036 shortcuts_activate_volume (impl, volume);
3040 GtkFilePath *file_path;
3043 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3048 shortcuts_select_func (GtkTreeSelection *selection,
3049 GtkTreeModel *model,
3051 gboolean path_currently_selected,
3054 GtkFileChooserDefault *impl = data;
3056 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
3060 list_selection_changed (GtkTreeSelection *selection,
3061 GtkFileChooserDefault *impl)
3063 /* See if we are in the new folder editable row for Save mode */
3064 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3066 GtkTreeSelection *selection;
3067 GtkTreeIter iter, child_iter;
3068 const GtkFileInfo *info;
3070 g_assert (!impl->select_multiple);
3071 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
3072 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3075 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3079 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
3081 return; /* We are on the editable row for New Folder */
3084 update_chooser_entry (impl);
3085 check_preview_change (impl);
3087 g_signal_emit_by_name (impl, "selection-changed", 0);
3090 /* Callback used when a row in the file list is activated */
3092 list_row_activated (GtkTreeView *tree_view,
3094 GtkTreeViewColumn *column,
3095 GtkFileChooserDefault *impl)
3097 GtkTreeIter iter, child_iter;
3098 const GtkFileInfo *info;
3100 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
3103 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
3105 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
3107 if (gtk_file_info_get_is_folder (info))
3109 const GtkFilePath *file_path;
3111 file_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
3112 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3117 g_signal_emit_by_name (impl, "file-activated");
3121 path_bar_clicked (GtkPathBar *path_bar,
3122 GtkFilePath *file_path,
3123 GtkFileChooserDefault *impl)
3125 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3128 static const GtkFileInfo *
3129 get_list_file_info (GtkFileChooserDefault *impl,
3132 GtkTreeIter child_iter;
3134 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3138 return _gtk_file_system_model_get_info (impl->list_model, &child_iter);
3142 tree_name_data_func (GtkTreeViewColumn *tree_column,
3143 GtkCellRenderer *cell,
3144 GtkTreeModel *tree_model,
3148 GtkFileChooserDefault *impl = data;
3149 const GtkFileInfo *info = _gtk_file_system_model_get_info (impl->tree_model, iter);
3154 "text", gtk_file_info_get_display_name (info),
3160 list_icon_data_func (GtkTreeViewColumn *tree_column,
3161 GtkCellRenderer *cell,
3162 GtkTreeModel *tree_model,
3166 GtkFileChooserDefault *impl = data;
3167 GtkTreeIter child_iter;
3168 const GtkFilePath *path;
3171 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3174 path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
3178 /* FIXME: NULL GError */
3179 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3185 g_object_unref (pixbuf);
3188 /* Sets a cellrenderer's text, making it bold if the GtkFileInfo is a folder */
3190 set_cell_text_bold_if_folder (const GtkFileInfo *info, GtkCellRenderer *cell, const char *text)
3194 "weight", gtk_file_info_get_is_folder (info) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
3199 list_name_data_func (GtkTreeViewColumn *tree_column,
3200 GtkCellRenderer *cell,
3201 GtkTreeModel *tree_model,
3205 GtkFileChooserDefault *impl = data;
3206 const GtkFileInfo *info = get_list_file_info (impl, iter);
3211 "text", _("Type name of new folder"),
3216 set_cell_text_bold_if_folder (info, cell, gtk_file_info_get_display_name (info));
3221 list_size_data_func (GtkTreeViewColumn *tree_column,
3222 GtkCellRenderer *cell,
3223 GtkTreeModel *tree_model,
3227 GtkFileChooserDefault *impl = data;
3228 const GtkFileInfo *info = get_list_file_info (impl, iter);
3232 if (!info || gtk_file_info_get_is_folder (info))
3235 size = gtk_file_info_get_size (info);
3237 if (size < (gint64)1024)
3238 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3239 else if (size < (gint64)1024*1024)
3240 str = g_strdup_printf (_("%.1f K"), size / (1024.));
3241 else if (size < (gint64)1024*1024*1024)
3242 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3244 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3254 /* Tree column data callback for the file list; fetches the mtime of a file */
3256 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3257 GtkCellRenderer *cell,
3258 GtkTreeModel *tree_model,
3262 GtkFileChooserDefault *impl;
3263 const GtkFileInfo *info;
3264 GtkFileTime time_mtime, time_now;
3271 info = get_list_file_info (impl, iter);
3280 time_mtime = gtk_file_info_get_modification_time (info);
3281 g_date_set_time (&mtime, (GTime) time_mtime);
3283 time_now = (GTime ) time (NULL);
3284 g_date_set_time (&now, (GTime) time_now);
3286 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3289 strcpy (buf, _("Today"));
3290 else if (days_diff == 1)
3291 strcpy (buf, _("Yesterday"));
3296 if (days_diff > 1 && days_diff < 7)
3297 format = "%A"; /* Days from last week */
3299 format = _("%d/%b/%Y"); /* Any other date */
3301 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3302 strcpy (buf, _("Unknown"));
3305 set_cell_text_bold_if_folder (info, cell, buf);
3309 _gtk_file_chooser_default_new (const char *file_system)
3311 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3312 "file-system-backend", file_system,
3317 location_entry_create (GtkFileChooserDefault *impl)
3321 entry = _gtk_file_chooser_entry_new ();
3322 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
3323 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
3324 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
3326 return GTK_WIDGET (entry);
3330 update_from_entry (GtkFileChooserDefault *impl,
3332 GtkFileChooserEntry *chooser_entry)
3334 const GtkFilePath *folder_path;
3335 const char *file_part;
3337 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3338 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3340 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
3342 error_message_with_parent (parent,
3343 _("Cannot change to the folder you specified as it is an invalid path."));
3347 if (file_part[0] == '\0')
3349 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), folder_path);
3354 GtkFileFolder *folder = NULL;
3355 GtkFilePath *subfolder_path = NULL;
3356 GtkFileInfo *info = NULL;
3359 /* If the file part is non-empty, we need to figure out if it refers to a
3360 * folder within folder. We could optimize the case here where the folder
3361 * is already loaded for one of our tree models.
3365 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
3369 error_getting_info_dialog (impl, folder_path, error);
3374 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
3376 if (!subfolder_path)
3380 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
3381 gtk_file_path_get_string (folder_path),
3384 error_message (impl, msg);
3386 g_object_unref (folder);
3391 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
3396 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3398 g_object_unref (folder);
3399 gtk_file_path_free (subfolder_path);
3403 error_getting_info_dialog (impl, subfolder_path, error);
3404 g_object_unref (folder);
3405 gtk_file_path_free (subfolder_path);
3409 if (gtk_file_info_get_is_folder (info))
3410 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3412 _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3414 g_object_unref (folder);
3415 gtk_file_path_free (subfolder_path);
3416 gtk_file_info_free (info);
3421 location_popup_handler (GtkFileChooserDefault *impl)
3424 GtkWidget *toplevel;
3431 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
3432 if (!GTK_WIDGET_TOPLEVEL (toplevel))
3435 dialog = gtk_dialog_new_with_buttons (_("Open Location"),
3436 GTK_WINDOW (toplevel),
3437 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
3438 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
3439 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3441 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
3443 hbox = gtk_hbox_new (FALSE, 12);
3444 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
3446 label = gtk_label_new_with_mnemonic (_("_Location:"));
3447 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3449 entry = location_entry_create (impl);
3450 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3451 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
3455 gtk_widget_show_all (dialog);
3456 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
3457 update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry));
3459 gtk_widget_destroy (dialog);
3462 /* Handler for the "up-folder" keybinding signal */
3464 up_folder_handler (GtkFileChooserDefault *impl)
3466 GtkFilePath *parent_path;
3470 if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
3472 if (parent_path) /* If we were on a root, parent_path will be NULL */
3474 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
3475 gtk_file_path_free (parent_path);
3480 _("Could not go to the parent folder of %s:\n%s"),
3481 impl->current_folder,
3485 /* Handler for the "home-folder" keybinding signal */
3487 home_folder_handler (GtkFileChooserDefault *impl)
3491 /* Should we pull this information from impl->has_home and the shortcuts data
3492 * instead? Sounds like a bit of overkill...
3495 home = g_get_home_dir ();
3496 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home);