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.
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcheckmenuitem.h"
30 #include "gtkclipboard.h"
31 #include "gtkcombobox.h"
33 #include "gtkeventbox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechoosersettings.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystemmodel.h"
45 #include "gtkhpaned.h"
46 #include "gtkiconfactory.h"
47 #include "gtkicontheme.h"
49 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmenuitem.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksizegroup.h"
63 #include "gtktreednd.h"
64 #include "gtktreeprivate.h"
65 #include "gtktreeselection.h"
66 #include "gtktypebuiltins.h"
69 #if defined (G_OS_UNIX)
70 #include "gtkfilesystemunix.h"
71 #elif defined (G_OS_WIN32)
72 #include "gtkfilesystemwin32.h"
91 #undef PROFILE_FILE_CHOOSER
92 #ifdef PROFILE_FILE_CHOOSER
99 #define PROFILE_INDENT 4
100 static int profile_indent;
103 profile_add_indent (int indent)
105 profile_indent += indent;
106 if (profile_indent < 0)
107 g_error ("You screwed up your indentation");
111 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
116 profile_add_indent (indent);
118 if (profile_indent == 0)
119 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
121 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
127 profile_add_indent (indent);
130 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
131 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
132 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
134 #define profile_start(x, y)
135 #define profile_end(x, y)
136 #define profile_msg(x, y)
141 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
143 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
144 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
145 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
147 #define MAX_LOADING_TIME 500
149 struct _GtkFileChooserDefaultClass
151 GtkVBoxClass parent_class;
157 LOCATION_POPUP_ON_PASTE,
166 static guint signals[LAST_SIGNAL] = { 0 };
168 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
170 SHORTCUTS_COL_PIXBUF,
173 SHORTCUTS_COL_IS_VOLUME,
174 SHORTCUTS_COL_REMOVABLE,
175 SHORTCUTS_COL_PIXBUF_VISIBLE,
176 SHORTCUTS_COL_HANDLE,
177 SHORTCUTS_COL_NUM_COLUMNS
180 /* Column numbers for the file list */
185 FILE_LIST_COL_NUM_COLUMNS
188 /* Identifiers for target types */
194 /* Target types for dragging from the shortcuts list */
195 static const GtkTargetEntry shortcuts_source_targets[] = {
196 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
199 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
200 / sizeof (shortcuts_source_targets[0]));
202 /* Target types for dropping into the shortcuts list */
203 static const GtkTargetEntry shortcuts_dest_targets[] = {
204 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
205 { "text/uri-list", 0, TEXT_URI_LIST }
208 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
209 / sizeof (shortcuts_dest_targets[0]));
211 /* Target types for DnD from the file list */
212 static const GtkTargetEntry file_list_source_targets[] = {
213 { "text/uri-list", 0, TEXT_URI_LIST }
216 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
217 / sizeof (file_list_source_targets[0]));
219 /* Target types for dropping into the file list */
220 static const GtkTargetEntry file_list_dest_targets[] = {
221 { "text/uri-list", 0, TEXT_URI_LIST }
224 static const int num_file_list_dest_targets = (sizeof (file_list_dest_targets)
225 / sizeof (file_list_dest_targets[0]));
228 /* Interesting places in the shortcuts bar */
234 SHORTCUTS_BOOKMARKS_SEPARATOR,
236 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
237 SHORTCUTS_CURRENT_FOLDER
240 /* Icon size for if we can't get it from the theme */
241 #define FALLBACK_ICON_SIZE 16
243 #define PREVIEW_HBOX_SPACING 12
247 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
248 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
250 static GObject* gtk_file_chooser_default_constructor (GType type,
251 guint n_construct_properties,
252 GObjectConstructParam *construct_params);
253 static void gtk_file_chooser_default_finalize (GObject *object);
254 static void gtk_file_chooser_default_set_property (GObject *object,
258 static void gtk_file_chooser_default_get_property (GObject *object,
262 static void gtk_file_chooser_default_dispose (GObject *object);
263 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
264 static void gtk_file_chooser_default_map (GtkWidget *widget);
265 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
266 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
267 GtkWidget *previous_toplevel);
268 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
269 GtkStyle *previous_style);
270 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
271 GdkScreen *previous_screen);
273 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
274 const GtkFilePath *path,
276 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
277 const GtkFilePath *path,
280 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
281 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
283 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
284 const GtkFilePath *path,
286 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
287 const GtkFilePath *path);
288 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
289 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
290 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
291 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
292 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
293 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
294 GtkFileFilter *filter);
295 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
296 GtkFileFilter *filter);
297 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
298 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
299 const GtkFilePath *path,
301 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
302 const GtkFilePath *path,
304 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
308 gint *default_height);
309 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
310 gboolean *resize_horizontally,
311 gboolean *resize_vertically);
312 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
313 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
315 static void location_popup_handler (GtkFileChooserDefault *impl,
317 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
318 static void up_folder_handler (GtkFileChooserDefault *impl);
319 static void down_folder_handler (GtkFileChooserDefault *impl);
320 static void home_folder_handler (GtkFileChooserDefault *impl);
321 static void desktop_folder_handler (GtkFileChooserDefault *impl);
322 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
323 gint bookmark_index);
324 static void update_appearance (GtkFileChooserDefault *impl);
326 static void set_current_filter (GtkFileChooserDefault *impl,
327 GtkFileFilter *filter);
328 static void check_preview_change (GtkFileChooserDefault *impl);
330 static void filter_combo_changed (GtkComboBox *combo_box,
331 GtkFileChooserDefault *impl);
332 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
334 GtkTreeViewColumn *column,
335 GtkFileChooserDefault *impl);
337 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
339 GtkFileChooserDefault *impl);
341 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
344 gboolean path_currently_selected,
346 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
348 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
350 static int shortcuts_get_index (GtkFileChooserDefault *impl,
351 ShortcutsIndex where);
352 static int shortcut_find_position (GtkFileChooserDefault *impl,
353 const GtkFilePath *path);
355 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
357 static gboolean list_select_func (GtkTreeSelection *selection,
360 gboolean path_currently_selected,
363 static void list_selection_changed (GtkTreeSelection *tree_selection,
364 GtkFileChooserDefault *impl);
365 static void list_row_activated (GtkTreeView *tree_view,
367 GtkTreeViewColumn *column,
368 GtkFileChooserDefault *impl);
370 static void select_func (GtkFileSystemModel *model,
375 static void path_bar_clicked (GtkPathBar *path_bar,
376 GtkFilePath *file_path,
377 GtkFilePath *child_path,
378 gboolean child_is_hidden,
379 GtkFileChooserDefault *impl);
381 static void add_bookmark_button_clicked_cb (GtkButton *button,
382 GtkFileChooserDefault *impl);
383 static void remove_bookmark_button_clicked_cb (GtkButton *button,
384 GtkFileChooserDefault *impl);
385 static void save_folder_combo_changed_cb (GtkComboBox *combo,
386 GtkFileChooserDefault *impl);
388 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
389 GtkCellRenderer *cell,
390 GtkTreeModel *tree_model,
393 static void list_name_data_func (GtkTreeViewColumn *tree_column,
394 GtkCellRenderer *cell,
395 GtkTreeModel *tree_model,
399 static void list_size_data_func (GtkTreeViewColumn *tree_column,
400 GtkCellRenderer *cell,
401 GtkTreeModel *tree_model,
405 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
406 GtkCellRenderer *cell,
407 GtkTreeModel *tree_model,
411 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
414 static void load_remove_timer (GtkFileChooserDefault *impl);
415 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
417 static void location_button_toggled_cb (GtkToggleButton *toggle,
418 GtkFileChooserDefault *impl);
419 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
424 /* Drag and drop interface declarations */
427 GtkTreeModelFilter parent;
429 GtkFileChooserDefault *impl;
430 } ShortcutsModelFilter;
433 GtkTreeModelFilterClass parent_class;
434 } ShortcutsModelFilterClass;
436 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
437 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
439 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
441 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
442 _shortcuts_model_filter,
443 GTK_TYPE_TREE_MODEL_FILTER,
444 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
445 shortcuts_model_filter_drag_source_iface_init))
447 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
448 GtkTreeModel *child_model,
453 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
454 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
455 gtk_file_chooser_default_iface_init)
456 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
457 gtk_file_chooser_embed_default_iface_init));
460 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
462 static const guint quick_bookmark_keyvals[10] = {
463 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
465 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
466 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
467 GtkBindingSet *binding_set;
470 gobject_class->finalize = gtk_file_chooser_default_finalize;
471 gobject_class->constructor = gtk_file_chooser_default_constructor;
472 gobject_class->set_property = gtk_file_chooser_default_set_property;
473 gobject_class->get_property = gtk_file_chooser_default_get_property;
474 gobject_class->dispose = gtk_file_chooser_default_dispose;
476 widget_class->show_all = gtk_file_chooser_default_show_all;
477 widget_class->map = gtk_file_chooser_default_map;
478 widget_class->unmap = gtk_file_chooser_default_unmap;
479 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
480 widget_class->style_set = gtk_file_chooser_default_style_set;
481 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
483 signals[LOCATION_POPUP] =
484 _gtk_binding_signal_new (I_("location-popup"),
485 G_OBJECT_CLASS_TYPE (class),
486 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
487 G_CALLBACK (location_popup_handler),
489 _gtk_marshal_VOID__STRING,
490 G_TYPE_NONE, 1, G_TYPE_STRING);
491 signals[LOCATION_POPUP_ON_PASTE] =
492 _gtk_binding_signal_new ("location-popup-on-paste",
493 G_OBJECT_CLASS_TYPE (class),
494 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
495 G_CALLBACK (location_popup_on_paste_handler),
497 _gtk_marshal_VOID__VOID,
500 _gtk_binding_signal_new (I_("up-folder"),
501 G_OBJECT_CLASS_TYPE (class),
502 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
503 G_CALLBACK (up_folder_handler),
505 _gtk_marshal_VOID__VOID,
507 signals[DOWN_FOLDER] =
508 _gtk_binding_signal_new (I_("down-folder"),
509 G_OBJECT_CLASS_TYPE (class),
510 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
511 G_CALLBACK (down_folder_handler),
513 _gtk_marshal_VOID__VOID,
515 signals[HOME_FOLDER] =
516 _gtk_binding_signal_new (I_("home-folder"),
517 G_OBJECT_CLASS_TYPE (class),
518 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
519 G_CALLBACK (home_folder_handler),
521 _gtk_marshal_VOID__VOID,
523 signals[DESKTOP_FOLDER] =
524 _gtk_binding_signal_new (I_("desktop-folder"),
525 G_OBJECT_CLASS_TYPE (class),
526 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
527 G_CALLBACK (desktop_folder_handler),
529 _gtk_marshal_VOID__VOID,
531 signals[QUICK_BOOKMARK] =
532 _gtk_binding_signal_new (I_("quick-bookmark"),
533 G_OBJECT_CLASS_TYPE (class),
534 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
535 G_CALLBACK (quick_bookmark_handler),
537 _gtk_marshal_VOID__INT,
538 G_TYPE_NONE, 1, G_TYPE_INT);
540 binding_set = gtk_binding_set_by_class (class);
542 gtk_binding_entry_add_signal (binding_set,
543 GDK_l, GDK_CONTROL_MASK,
545 1, G_TYPE_STRING, "");
547 gtk_binding_entry_add_signal (binding_set,
550 1, G_TYPE_STRING, "/");
551 gtk_binding_entry_add_signal (binding_set,
554 1, G_TYPE_STRING, "/");
557 gtk_binding_entry_add_signal (binding_set,
560 1, G_TYPE_STRING, "~");
563 gtk_binding_entry_add_signal (binding_set,
564 GDK_v, GDK_CONTROL_MASK,
565 "location-popup-on-paste",
568 gtk_binding_entry_add_signal (binding_set,
569 GDK_Up, GDK_MOD1_MASK,
572 gtk_binding_entry_add_signal (binding_set,
576 gtk_binding_entry_add_signal (binding_set,
577 GDK_KP_Up, GDK_MOD1_MASK,
581 gtk_binding_entry_add_signal (binding_set,
582 GDK_Down, GDK_MOD1_MASK,
585 gtk_binding_entry_add_signal (binding_set,
586 GDK_KP_Down, GDK_MOD1_MASK,
590 gtk_binding_entry_add_signal (binding_set,
591 GDK_Home, GDK_MOD1_MASK,
594 gtk_binding_entry_add_signal (binding_set,
595 GDK_KP_Home, GDK_MOD1_MASK,
598 gtk_binding_entry_add_signal (binding_set,
599 GDK_d, GDK_MOD1_MASK,
603 for (i = 0; i < 10; i++)
604 gtk_binding_entry_add_signal (binding_set,
605 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
609 _gtk_file_chooser_install_properties (gobject_class);
611 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
612 P_("Default file chooser backend"),
613 P_("Name of the GtkFileChooser backend to use by default"),
615 GTK_PARAM_READWRITE));
619 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
621 iface->select_path = gtk_file_chooser_default_select_path;
622 iface->unselect_path = gtk_file_chooser_default_unselect_path;
623 iface->select_all = gtk_file_chooser_default_select_all;
624 iface->unselect_all = gtk_file_chooser_default_unselect_all;
625 iface->get_paths = gtk_file_chooser_default_get_paths;
626 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
627 iface->get_file_system = gtk_file_chooser_default_get_file_system;
628 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
629 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
630 iface->set_current_name = gtk_file_chooser_default_set_current_name;
631 iface->add_filter = gtk_file_chooser_default_add_filter;
632 iface->remove_filter = gtk_file_chooser_default_remove_filter;
633 iface->list_filters = gtk_file_chooser_default_list_filters;
634 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
635 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
636 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
640 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
642 iface->get_default_size = gtk_file_chooser_default_get_default_size;
643 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
644 iface->should_respond = gtk_file_chooser_default_should_respond;
645 iface->initial_focus = gtk_file_chooser_default_initial_focus;
649 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
651 profile_start ("start", NULL);
652 #ifdef PROFILE_FILE_CHOOSER
653 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
655 impl->local_only = TRUE;
656 impl->preview_widget_active = TRUE;
657 impl->use_preview_label = TRUE;
658 impl->select_multiple = FALSE;
659 impl->show_hidden = FALSE;
660 impl->icon_size = FALLBACK_ICON_SIZE;
661 impl->load_state = LOAD_EMPTY;
662 impl->reload_state = RELOAD_EMPTY;
663 impl->pending_select_paths = NULL;
664 impl->location_mode = LOCATION_MODE_PATH_BAR;
666 gtk_box_set_spacing (GTK_BOX (impl), 12);
668 impl->tooltips = gtk_tooltips_new ();
669 g_object_ref_sink (impl->tooltips);
671 profile_end ("end", NULL);
674 /* Frees the data columns for the specified iter in the shortcuts model*/
676 shortcuts_free_row_data (GtkFileChooserDefault *impl,
681 GtkFileSystemHandle *handle;
683 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
684 SHORTCUTS_COL_DATA, &col_data,
685 SHORTCUTS_COL_IS_VOLUME, &is_volume,
686 SHORTCUTS_COL_HANDLE, &handle,
690 gtk_file_system_cancel_operation (handle);
697 GtkFileSystemVolume *volume;
700 gtk_file_system_volume_free (impl->file_system, volume);
707 gtk_file_path_free (path);
711 /* Frees all the data columns in the shortcuts model */
713 shortcuts_free (GtkFileChooserDefault *impl)
717 if (!impl->shortcuts_model)
720 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
723 shortcuts_free_row_data (impl, &iter);
725 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
727 g_object_unref (impl->shortcuts_model);
728 impl->shortcuts_model = NULL;
732 pending_select_paths_free (GtkFileChooserDefault *impl)
736 for (l = impl->pending_select_paths; l; l = l->next)
741 gtk_file_path_free (path);
744 g_slist_free (impl->pending_select_paths);
745 impl->pending_select_paths = NULL;
749 pending_select_paths_add (GtkFileChooserDefault *impl,
750 const GtkFilePath *path)
752 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
755 /* Used from gtk_tree_selection_selected_foreach() */
757 store_selection_foreach (GtkTreeModel *model,
762 GtkFileChooserDefault *impl;
763 GtkTreeIter child_iter;
764 const GtkFilePath *file_path;
766 impl = GTK_FILE_CHOOSER_DEFAULT (data);
768 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
770 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
771 pending_select_paths_add (impl, file_path);
774 /* Stores the current selection in the list of paths to select; this is used to
775 * preserve the selection when reloading the current folder.
778 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
780 GtkTreeSelection *selection;
782 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
783 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
787 gtk_file_chooser_default_finalize (GObject *object)
789 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
792 if (impl->shortcuts_filter_model)
793 g_object_unref (impl->shortcuts_filter_model);
795 shortcuts_free (impl);
797 g_object_unref (impl->file_system);
799 g_free (impl->browse_files_last_selected_name);
801 for (l = impl->filters; l; l = l->next)
803 GtkFileFilter *filter;
805 filter = GTK_FILE_FILTER (l->data);
806 g_object_unref (filter);
808 g_slist_free (impl->filters);
810 if (impl->current_filter)
811 g_object_unref (impl->current_filter);
813 if (impl->current_volume_path)
814 gtk_file_path_free (impl->current_volume_path);
816 if (impl->current_folder)
817 gtk_file_path_free (impl->current_folder);
819 if (impl->preview_path)
820 gtk_file_path_free (impl->preview_path);
822 load_remove_timer (impl);
824 /* Free all the Models we have */
825 if (impl->browse_files_model)
826 g_object_unref (impl->browse_files_model);
828 if (impl->sort_model)
829 g_object_unref (impl->sort_model);
831 g_free (impl->preview_display_name);
833 g_free (impl->edited_new_text);
835 g_object_unref (impl->tooltips);
837 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
840 /* Shows an error dialog set as transient for the specified window */
842 error_message_with_parent (GtkWindow *parent,
848 dialog = gtk_message_dialog_new (parent,
849 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
854 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
858 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
860 gtk_dialog_run (GTK_DIALOG (dialog));
861 gtk_widget_destroy (dialog);
864 /* Returns a toplevel GtkWindow, or NULL if none */
866 get_toplevel (GtkWidget *widget)
870 toplevel = gtk_widget_get_toplevel (widget);
871 if (!GTK_WIDGET_TOPLEVEL (toplevel))
874 return GTK_WINDOW (toplevel);
877 /* Shows an error dialog for the file chooser */
879 error_message (GtkFileChooserDefault *impl,
883 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
886 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
888 error_dialog (GtkFileChooserDefault *impl,
890 const GtkFilePath *path,
899 uri = gtk_file_system_path_to_uri (impl->file_system, path);
900 text = g_strdup_printf (msg, uri);
901 error_message (impl, text, error->message);
904 g_error_free (error);
908 /* Displays an error message about not being able to get information for a file.
909 * Frees the GError as well.
912 error_getting_info_dialog (GtkFileChooserDefault *impl,
913 const GtkFilePath *path,
917 _("Could not retrieve information about the file"),
921 /* Shows an error dialog about not being able to add a bookmark */
923 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
924 const GtkFilePath *path,
928 _("Could not add a bookmark"),
932 /* Shows an error dialog about not being able to remove a bookmark */
934 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
935 const GtkFilePath *path,
939 _("Could not remove bookmark"),
943 /* Shows an error dialog about not being able to create a folder */
945 error_creating_folder_dialog (GtkFileChooserDefault *impl,
946 const GtkFilePath *path,
950 _("The folder could not be created"),
954 /* Shows an error about not being able to create a folder because a file with
955 * the same name is already there.
958 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
959 const GtkFilePath *path,
963 _("The folder could not be created, as a file with the same name "
964 "already exists. Try using a different name for the folder, "
965 "or rename the file first."),
969 /* Shows an error dialog about not being able to create a filename */
971 error_building_filename_dialog (GtkFileChooserDefault *impl,
972 const GtkFilePath *folder_part,
973 const char *file_part,
976 error_dialog (impl, _("Invalid file name"),
980 /* Shows an error dialog when we cannot switch to a folder */
982 error_changing_folder_dialog (GtkFileChooserDefault *impl,
983 const GtkFilePath *path,
986 error_dialog (impl, _("The folder contents could not be displayed"),
990 /* Changes folders, displaying an error dialog if this fails */
992 change_folder_and_display_error (GtkFileChooserDefault *impl,
993 const GtkFilePath *path)
997 GtkFilePath *path_copy;
999 g_return_val_if_fail (path != NULL, FALSE);
1001 profile_start ("start", (char *) path);
1003 /* We copy the path because of this case:
1005 * list_row_activated()
1006 * fetches path from model; path belongs to the model (*)
1007 * calls change_folder_and_display_error()
1008 * calls _gtk_file_chooser_set_current_folder_path()
1009 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1012 path_copy = gtk_file_path_copy (path);
1015 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1018 error_changing_folder_dialog (impl, path_copy, error);
1020 gtk_file_path_free (path_copy);
1022 profile_end ("end", (char *) path);
1028 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1030 if (impl->use_preview_label)
1032 if (!impl->preview_label)
1034 impl->preview_label = gtk_label_new (impl->preview_display_name);
1035 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1036 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1037 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1038 gtk_widget_show (impl->preview_label);
1043 if (impl->preview_label)
1045 gtk_widget_destroy (impl->preview_label);
1046 impl->preview_label = NULL;
1050 if (impl->preview_widget_active && impl->preview_widget)
1051 gtk_widget_show (impl->preview_box);
1053 gtk_widget_hide (impl->preview_box);
1055 g_signal_emit_by_name (impl, "default-size-changed");
1059 set_preview_widget (GtkFileChooserDefault *impl,
1060 GtkWidget *preview_widget)
1062 if (preview_widget == impl->preview_widget)
1065 if (impl->preview_widget)
1066 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1067 impl->preview_widget);
1069 impl->preview_widget = preview_widget;
1070 if (impl->preview_widget)
1072 gtk_widget_show (impl->preview_widget);
1073 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1074 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1075 impl->preview_widget,
1076 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1079 update_preview_widget_visibility (impl);
1082 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1083 struct ReloadIconsData
1085 GtkFileChooserDefault *impl;
1086 GtkTreeRowReference *row_ref;
1090 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1091 const GtkFileInfo *info,
1092 const GError *error,
1098 gboolean cancelled = handle->cancelled;
1099 struct ReloadIconsData *data = user_data;
1101 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1104 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1106 if (cancelled || error)
1109 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1110 data->impl->icon_size, NULL);
1112 path = gtk_tree_row_reference_get_path (data->row_ref);
1113 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1114 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1115 SHORTCUTS_COL_PIXBUF, pixbuf,
1117 gtk_tree_path_free (path);
1120 g_object_unref (pixbuf);
1123 gtk_tree_row_reference_free (data->row_ref);
1124 g_object_unref (data->impl);
1127 g_object_unref (handle);
1131 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1136 profile_start ("start", NULL);
1138 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1141 for (l = impl->reload_icon_handles; l; l = l->next)
1143 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1144 gtk_file_system_cancel_operation (handle);
1146 g_slist_free (impl->reload_icon_handles);
1147 impl->reload_icon_handles = NULL;
1153 gboolean pixbuf_visible;
1156 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1157 SHORTCUTS_COL_DATA, &data,
1158 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1159 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1162 if (pixbuf_visible && data)
1166 GtkFileSystemVolume *volume;
1169 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1170 impl->icon_size, NULL);
1172 gtk_list_store_set (impl->shortcuts_model, &iter,
1173 SHORTCUTS_COL_PIXBUF, pixbuf,
1177 g_object_unref (pixbuf);
1179 else if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1181 const GtkFilePath *path;
1182 struct ReloadIconsData *info;
1183 GtkTreePath *tree_path;
1184 GtkFileSystemHandle *handle;
1188 info = g_new0 (struct ReloadIconsData, 1);
1189 info->impl = g_object_ref (impl);
1190 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1191 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1192 gtk_tree_path_free (tree_path);
1194 handle = gtk_file_system_get_info (impl->file_system, path,
1196 shortcuts_reload_icons_get_info_cb,
1198 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1202 GtkIconTheme *icon_theme;
1204 /* Don't call get_info for remote paths to avoid latency and
1206 * If we switch to a better bookmarks file format (XBEL), we
1207 * should use mime info to get a better icon.
1209 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1210 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1211 impl->icon_size, 0, NULL);
1213 gtk_list_store_set (impl->shortcuts_model, &iter,
1214 SHORTCUTS_COL_PIXBUF, pixbuf,
1218 g_object_unref (pixbuf);
1222 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1226 profile_end ("end", NULL);
1230 shortcuts_find_folder (GtkFileChooserDefault *impl,
1231 GtkFilePath *folder)
1233 GtkTreeSelection *selection;
1237 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1239 g_assert (folder != NULL);
1240 pos = shortcut_find_position (impl, folder);
1243 gtk_tree_selection_unselect_all (selection);
1247 path = gtk_tree_path_new_from_indices (pos, -1);
1248 gtk_tree_selection_select_path (selection, path);
1249 gtk_tree_path_free (path);
1252 /* If a shortcut corresponds to the current folder, selects it */
1254 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1256 shortcuts_find_folder (impl, impl->current_folder);
1259 /* Removes the specified number of rows from the shortcuts list */
1261 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1267 path = gtk_tree_path_new_from_indices (start_row, -1);
1269 for (; n_rows; n_rows--)
1273 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1274 g_assert_not_reached ();
1276 shortcuts_free_row_data (impl, &iter);
1277 gtk_list_store_remove (impl->shortcuts_model, &iter);
1280 gtk_tree_path_free (path);
1284 shortcuts_update_count (GtkFileChooserDefault *impl,
1285 ShortcutsIndex type,
1290 case SHORTCUTS_HOME:
1292 impl->has_home = FALSE;
1294 impl->has_home = TRUE;
1297 case SHORTCUTS_DESKTOP:
1299 impl->has_desktop = FALSE;
1301 impl->has_desktop = TRUE;
1304 case SHORTCUTS_VOLUMES:
1305 impl->num_volumes += value;
1308 case SHORTCUTS_SHORTCUTS:
1309 impl->num_shortcuts += value;
1312 case SHORTCUTS_BOOKMARKS:
1313 impl->num_bookmarks += value;
1316 case SHORTCUTS_CURRENT_FOLDER:
1318 impl->shortcuts_current_folder_active = FALSE;
1320 impl->shortcuts_current_folder_active = TRUE;
1329 struct ShortcutsInsertRequest
1331 GtkFileChooserDefault *impl;
1332 GtkFilePath *parent_path;
1336 GtkTreeRowReference *row_ref;
1337 ShortcutsIndex type;
1343 get_file_info_finished (GtkFileSystemHandle *handle,
1344 const GtkFileInfo *info,
1345 const GError *error,
1349 gboolean cancelled = handle->cancelled;
1350 gboolean is_volume = FALSE;
1354 GtkFileSystemHandle *model_handle;
1355 struct ShortcutsInsertRequest *request = data;
1357 path = gtk_tree_row_reference_get_path (request->row_ref);
1359 /* Handle doesn't exist anymore in the model */
1362 pos = gtk_tree_path_get_indices (path)[0];
1363 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1365 gtk_tree_path_free (path);
1367 /* validate handle, else goto out */
1368 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1369 SHORTCUTS_COL_HANDLE, &model_handle,
1371 if (handle != model_handle)
1374 /* set the handle to NULL in the model (we unref later on) */
1375 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1376 SHORTCUTS_COL_HANDLE, NULL,
1384 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1385 shortcuts_update_count (request->impl, request->type, -1);
1387 if (request->type == SHORTCUTS_HOME)
1389 const char *home = g_get_home_dir ();
1390 GtkFilePath *home_path;
1392 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1393 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1394 gtk_file_path_free (home_path);
1396 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1398 /* Remove the current folder separator */
1399 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1400 shortcuts_remove_rows (request->impl, separator_pos, 1);
1406 if (!request->label_copy)
1407 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1408 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1409 request->impl->icon_size, NULL);
1411 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1412 SHORTCUTS_COL_PIXBUF, pixbuf,
1413 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1414 SHORTCUTS_COL_NAME, request->label_copy,
1415 SHORTCUTS_COL_IS_VOLUME, is_volume,
1416 SHORTCUTS_COL_REMOVABLE, request->removable,
1419 if (request->impl->shortcuts_filter_model)
1420 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_filter_model));
1422 if (request->type == SHORTCUTS_CURRENT_FOLDER
1423 && request->impl->save_folder_combo != NULL)
1425 /* The current folder is updated via _activate_iter(), don't
1426 * have save_folder_combo_changed_cb() call _activate_iter()
1429 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1430 G_CALLBACK (save_folder_combo_changed_cb),
1432 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1433 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1434 G_CALLBACK (save_folder_combo_changed_cb),
1439 g_object_unref (pixbuf);
1442 g_object_unref (request->impl);
1443 gtk_file_path_free (request->parent_path);
1444 gtk_file_path_free (request->path);
1445 gtk_tree_row_reference_free (request->row_ref);
1446 g_free (request->label_copy);
1449 g_object_unref (handle);
1452 /* FIXME: GtkFileSystem needs a function to split a remote path
1453 * into hostname and path components, or maybe just have a
1454 * gtk_file_system_path_get_display_name().
1456 * This function is also used in gtkfilechooserbutton.c
1459 _gtk_file_chooser_label_for_uri (const gchar *uri)
1461 const gchar *path, *start, *end, *p;
1462 gchar *host, *label;
1464 start = strstr (uri, "://");
1466 path = strchr (start, '/');
1472 end = uri + strlen (uri);
1476 /* strip username */
1477 p = strchr (start, '@');
1483 p = strchr (start, ':');
1487 host = g_strndup (start, end - start);
1489 /* Translators: the first string is a path and the second string
1490 * is a hostname. Nautilus and the panel contain the same string
1493 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1500 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1501 * inserts a volume. A position of -1 indicates the end of the tree.
1504 shortcuts_insert_path (GtkFileChooserDefault *impl,
1507 GtkFileSystemVolume *volume,
1508 const GtkFilePath *path,
1511 ShortcutsIndex type)
1514 GdkPixbuf *pixbuf = NULL;
1515 gpointer data = NULL;
1517 GtkIconTheme *icon_theme;
1519 profile_start ("start", is_volume ? "volume" : (char *) path);
1524 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1525 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1526 impl->icon_size, NULL);
1528 else if (gtk_file_system_path_is_local (impl->file_system, path))
1530 struct ShortcutsInsertRequest *request;
1531 GtkFileSystemHandle *handle;
1534 request = g_new0 (struct ShortcutsInsertRequest, 1);
1535 request->impl = g_object_ref (impl);
1536 request->path = gtk_file_path_copy (path);
1537 request->name_only = TRUE;
1538 request->removable = removable;
1540 request->type = type;
1542 request->label_copy = g_strdup (label);
1545 gtk_list_store_append (impl->shortcuts_model, &iter);
1547 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1549 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1550 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1551 gtk_tree_path_free (p);
1553 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1554 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1555 get_file_info_finished, request);
1557 gtk_list_store_set (impl->shortcuts_model, &iter,
1558 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1559 SHORTCUTS_COL_IS_VOLUME, is_volume,
1560 SHORTCUTS_COL_HANDLE, handle,
1563 shortcuts_update_count (impl, type, 1);
1569 /* Don't call get_info for remote paths to avoid latency and
1572 data = gtk_file_path_copy (path);
1574 label_copy = g_strdup (label);
1579 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1581 label_copy = _gtk_file_chooser_label_for_uri (uri);
1586 /* If we switch to a better bookmarks file format (XBEL), we
1587 * should use mime info to get a better icon.
1589 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1590 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1591 impl->icon_size, 0, NULL);
1595 gtk_list_store_append (impl->shortcuts_model, &iter);
1597 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1599 shortcuts_update_count (impl, type, 1);
1601 gtk_list_store_set (impl->shortcuts_model, &iter,
1602 SHORTCUTS_COL_PIXBUF, pixbuf,
1603 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1604 SHORTCUTS_COL_NAME, label_copy,
1605 SHORTCUTS_COL_DATA, data,
1606 SHORTCUTS_COL_IS_VOLUME, is_volume,
1607 SHORTCUTS_COL_REMOVABLE, removable,
1608 SHORTCUTS_COL_HANDLE, NULL,
1611 if (impl->shortcuts_filter_model)
1612 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1614 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1616 /* The current folder is updated via _activate_iter(), don't
1617 * have save_folder_combo_changed_cb() call _activate_iter()
1620 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1621 g_signal_handlers_block_by_func (impl->save_folder_combo,
1622 G_CALLBACK (save_folder_combo_changed_cb),
1624 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1625 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1626 G_CALLBACK (save_folder_combo_changed_cb),
1630 g_free (label_copy);
1633 g_object_unref (pixbuf);
1635 profile_end ("end", NULL);
1638 /* Appends an item for the user's home directory to the shortcuts model */
1640 shortcuts_append_home (GtkFileChooserDefault *impl)
1643 GtkFilePath *home_path;
1645 profile_start ("start", NULL);
1647 home = g_get_home_dir ();
1650 profile_end ("end - no home directory!?", NULL);
1654 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1656 shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1658 gtk_file_path_free (home_path);
1660 profile_end ("end", NULL);
1663 /* Appends the ~/Desktop directory to the shortcuts model */
1665 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1671 profile_start ("start", NULL);
1674 name = _gtk_file_system_win32_get_desktop ();
1676 home = g_get_home_dir ();
1679 profile_end ("end - no home directory!?", NULL);
1683 name = g_build_filename (home, "Desktop", NULL);
1686 path = gtk_file_system_filename_to_path (impl->file_system, name);
1689 shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1690 /* We do not actually pop up an error dialog if there is no desktop directory
1691 * because some people may really not want to have one.
1694 gtk_file_path_free (path);
1696 profile_end ("end", NULL);
1699 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1701 shortcuts_append_paths (GtkFileChooserDefault *impl,
1708 profile_start ("start", NULL);
1710 /* As there is no separator now, we want to start there.
1712 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1715 for (; paths; paths = paths->next)
1721 if (impl->local_only &&
1722 !gtk_file_system_path_is_local (impl->file_system, path))
1725 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1727 /* NULL GError, but we don't really want to show error boxes here */
1728 shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1734 profile_end ("end", NULL);
1736 return num_inserted;
1739 /* Returns the index for the corresponding item in the shortcuts bar */
1741 shortcuts_get_index (GtkFileChooserDefault *impl,
1742 ShortcutsIndex where)
1748 if (where == SHORTCUTS_HOME)
1751 n += impl->has_home ? 1 : 0;
1753 if (where == SHORTCUTS_DESKTOP)
1756 n += impl->has_desktop ? 1 : 0;
1758 if (where == SHORTCUTS_VOLUMES)
1761 n += impl->num_volumes;
1763 if (where == SHORTCUTS_SHORTCUTS)
1766 n += impl->num_shortcuts;
1768 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1771 /* If there are no bookmarks there won't be a separator */
1772 n += (impl->num_bookmarks > 0) ? 1 : 0;
1774 if (where == SHORTCUTS_BOOKMARKS)
1777 n += impl->num_bookmarks;
1779 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1784 if (where == SHORTCUTS_CURRENT_FOLDER)
1787 g_assert_not_reached ();
1794 /* Adds all the file system volumes to the shortcuts model */
1796 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1801 gboolean old_changing_folders;
1803 profile_start ("start", NULL);
1806 old_changing_folders = impl->changing_folder;
1807 impl->changing_folder = TRUE;
1809 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1810 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1811 impl->num_volumes = 0;
1813 list = gtk_file_system_list_volumes (impl->file_system);
1817 for (l = list; l; l = l->next)
1819 GtkFileSystemVolume *volume;
1823 if (impl->local_only)
1825 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
1827 GtkFilePath *base_path;
1829 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1830 if (base_path != NULL)
1832 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1833 gtk_file_path_free (base_path);
1837 gtk_file_system_volume_free (impl->file_system, volume);
1844 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
1848 impl->num_volumes = n;
1849 g_slist_free (list);
1851 if (impl->shortcuts_filter_model)
1852 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1854 impl->changing_folder = old_changing_folders;
1856 profile_end ("end", NULL);
1859 /* Inserts a separator node in the shortcuts list */
1861 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1862 ShortcutsIndex where)
1866 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1868 gtk_list_store_insert (impl->shortcuts_model, &iter,
1869 shortcuts_get_index (impl, where));
1870 gtk_list_store_set (impl->shortcuts_model, &iter,
1871 SHORTCUTS_COL_PIXBUF, NULL,
1872 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1873 SHORTCUTS_COL_NAME, NULL,
1874 SHORTCUTS_COL_DATA, NULL,
1878 /* Updates the list of bookmarks */
1880 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1883 gboolean old_changing_folders;
1885 GtkFilePath *list_selected = NULL;
1886 GtkFilePath *combo_selected = NULL;
1890 profile_start ("start", NULL);
1892 old_changing_folders = impl->changing_folder;
1893 impl->changing_folder = TRUE;
1895 if (shortcuts_get_selected (impl, &iter))
1897 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1899 SHORTCUTS_COL_DATA, &col_data,
1900 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1903 if (col_data && !is_volume)
1904 list_selected = gtk_file_path_copy (col_data);
1907 if (impl->save_folder_combo &&
1908 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1911 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1913 SHORTCUTS_COL_DATA, &col_data,
1914 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1917 if (col_data && !is_volume)
1918 combo_selected = gtk_file_path_copy (col_data);
1921 if (impl->num_bookmarks > 0)
1922 shortcuts_remove_rows (impl,
1923 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1924 impl->num_bookmarks + 1);
1926 impl->num_bookmarks = 0;
1928 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1929 shortcuts_append_paths (impl, bookmarks);
1930 gtk_file_paths_free (bookmarks);
1932 if (impl->num_bookmarks > 0)
1933 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1935 if (impl->shortcuts_filter_model)
1936 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1940 shortcuts_find_folder (impl, list_selected);
1941 gtk_file_path_free (list_selected);
1948 pos = shortcut_find_position (impl, combo_selected);
1950 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1952 gtk_file_path_free (combo_selected);
1955 impl->changing_folder = old_changing_folders;
1957 profile_end ("end", NULL);
1960 /* Appends a separator and a row to the shortcuts list for the current folder */
1962 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1967 g_assert (!impl->shortcuts_current_folder_active);
1971 g_assert (impl->current_folder != NULL);
1973 pos = shortcut_find_position (impl, impl->current_folder);
1976 GtkFileSystemVolume *volume;
1977 GtkFilePath *base_path;
1981 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1985 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1987 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1989 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1994 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1996 shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2000 shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2002 gtk_file_system_volume_free (impl->file_system, volume);
2006 gtk_file_path_free (base_path);
2008 else if (impl->save_folder_combo != NULL)
2009 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2012 /* Updates the current folder row in the shortcuts model */
2014 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2018 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2020 if (impl->shortcuts_current_folder_active)
2022 shortcuts_remove_rows (impl, pos, 2);
2023 impl->shortcuts_current_folder_active = FALSE;
2026 shortcuts_add_current_folder (impl);
2029 /* Filter function used for the shortcuts filter model */
2031 shortcuts_filter_cb (GtkTreeModel *model,
2035 GtkFileChooserDefault *impl;
2039 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2041 path = gtk_tree_model_get_path (model, iter);
2045 pos = *gtk_tree_path_get_indices (path);
2046 gtk_tree_path_free (path);
2048 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2051 /* Creates the list model for shortcuts */
2053 shortcuts_model_create (GtkFileChooserDefault *impl)
2055 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2056 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2057 GDK_TYPE_PIXBUF, /* pixbuf */
2058 G_TYPE_STRING, /* name */
2059 G_TYPE_POINTER, /* path or volume */
2060 G_TYPE_BOOLEAN, /* is the previous column a volume? */
2061 G_TYPE_BOOLEAN, /* removable */
2062 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2063 G_TYPE_POINTER); /* GtkFileSystemHandle */
2065 if (impl->file_system)
2067 shortcuts_append_home (impl);
2068 shortcuts_append_desktop (impl);
2069 shortcuts_add_volumes (impl);
2072 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
2073 GTK_TREE_MODEL (impl->shortcuts_model),
2076 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2077 shortcuts_filter_cb,
2082 /* Callback used when the "New Folder" button is clicked */
2084 new_folder_button_clicked (GtkButton *button,
2085 GtkFileChooserDefault *impl)
2090 if (!impl->browse_files_model)
2091 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2093 /* Prevent button from being clicked twice */
2094 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2096 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2098 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2099 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2100 path, impl->list_name_column,
2103 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2104 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2106 impl->list_name_column,
2109 gtk_tree_path_free (path);
2113 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2114 const GtkFilePath *path,
2115 const GError *error,
2118 gboolean cancelled = handle->cancelled;
2119 GtkFileChooserDefault *impl = data;
2121 if (!g_slist_find (impl->pending_handles, handle))
2124 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2130 change_folder_and_display_error (impl, path);
2132 error_creating_folder_dialog (impl, path, g_error_copy (error));
2135 g_object_unref (impl);
2136 g_object_unref (handle);
2139 /* Idle handler for creating a new folder after editing its name cell, or for
2140 * canceling the editing.
2143 edited_idle_cb (GtkFileChooserDefault *impl)
2145 GDK_THREADS_ENTER ();
2147 g_source_destroy (impl->edited_idle);
2148 impl->edited_idle = NULL;
2150 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2151 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2153 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2155 if (impl->edited_new_text) /* not cancelled? */
2158 GtkFilePath *file_path;
2161 file_path = gtk_file_system_make_path (impl->file_system,
2162 impl->current_folder,
2163 impl->edited_new_text,
2167 GtkFileSystemHandle *handle;
2169 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2170 edited_idle_create_folder_cb,
2171 g_object_ref (impl));
2172 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2174 gtk_file_path_free (file_path);
2177 error_creating_folder_dialog (impl, file_path, error);
2179 g_free (impl->edited_new_text);
2180 impl->edited_new_text = NULL;
2183 GDK_THREADS_LEAVE ();
2189 queue_edited_idle (GtkFileChooserDefault *impl,
2190 const gchar *new_text)
2192 /* We create the folder in an idle handler so that we don't modify the tree
2196 if (!impl->edited_idle)
2198 impl->edited_idle = g_idle_source_new ();
2199 g_source_set_closure (impl->edited_idle,
2200 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2202 g_source_attach (impl->edited_idle, NULL);
2205 g_free (impl->edited_new_text);
2206 impl->edited_new_text = g_strdup (new_text);
2209 /* Callback used from the text cell renderer when the new folder is named */
2211 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2213 const gchar *new_text,
2214 GtkFileChooserDefault *impl)
2216 /* work around bug #154921 */
2217 g_object_set (cell_renderer_text,
2218 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2219 queue_edited_idle (impl, new_text);
2222 /* Callback used from the text cell renderer when the new folder edition gets
2226 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2227 GtkFileChooserDefault *impl)
2229 /* work around bug #154921 */
2230 g_object_set (cell_renderer_text,
2231 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2232 queue_edited_idle (impl, NULL);
2235 /* Creates the widgets for the filter combo box */
2237 filter_create (GtkFileChooserDefault *impl)
2239 impl->filter_combo = gtk_combo_box_new_text ();
2240 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2242 g_signal_connect (impl->filter_combo, "changed",
2243 G_CALLBACK (filter_combo_changed), impl);
2245 return impl->filter_combo;
2249 button_new (GtkFileChooserDefault *impl,
2251 const char *stock_id,
2259 button = gtk_button_new_with_mnemonic (text);
2260 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2261 gtk_button_set_image (GTK_BUTTON (button), image);
2263 gtk_widget_set_sensitive (button, sensitive);
2264 g_signal_connect (button, "clicked", callback, impl);
2267 gtk_widget_show (button);
2272 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2274 shortcut_find_position (GtkFileChooserDefault *impl,
2275 const GtkFilePath *path)
2279 int current_folder_separator_idx;
2281 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2284 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2287 /* FIXME: is this still needed? */
2288 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2292 for (i = 0; i < current_folder_separator_idx; i++)
2297 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2298 SHORTCUTS_COL_DATA, &col_data,
2299 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2306 GtkFileSystemVolume *volume;
2307 GtkFilePath *base_path;
2311 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2313 exists = base_path && strcmp (gtk_file_path_get_string (path),
2314 gtk_file_path_get_string (base_path)) == 0;
2322 GtkFilePath *model_path;
2324 model_path = col_data;
2326 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2331 if (i < current_folder_separator_idx - 1)
2333 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2334 g_assert_not_reached ();
2341 /* Tries to add a bookmark from a path name */
2343 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2344 const GtkFilePath *path,
2349 g_return_val_if_fail (path != NULL, FALSE);
2351 if (shortcut_find_position (impl, path) != -1)
2355 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2357 error_adding_bookmark_dialog (impl, path, error);
2365 add_bookmark_foreach_cb (GtkTreeModel *model,
2370 GtkFileChooserDefault *impl;
2371 GtkFileSystemModel *fs_model;
2372 GtkTreeIter child_iter;
2373 const GtkFilePath *file_path;
2375 impl = (GtkFileChooserDefault *) data;
2377 fs_model = impl->browse_files_model;
2378 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2380 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2381 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2384 /* Adds a bookmark from the currently selected item in the file list */
2386 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2388 GtkTreeSelection *selection;
2390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2392 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2393 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2395 gtk_tree_selection_selected_foreach (selection,
2396 add_bookmark_foreach_cb,
2400 /* Callback used when the "Add bookmark" button is clicked */
2402 add_bookmark_button_clicked_cb (GtkButton *button,
2403 GtkFileChooserDefault *impl)
2405 bookmarks_add_selected_folder (impl);
2408 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2409 * returns FALSE if no shortcut is selected.
2412 shortcuts_get_selected (GtkFileChooserDefault *impl,
2415 GtkTreeSelection *selection;
2416 GtkTreeIter parent_iter;
2418 if (!impl->browse_shortcuts_tree_view)
2421 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2423 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2426 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2432 /* Removes the selected bookmarks */
2434 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2442 if (!shortcuts_get_selected (impl, &iter))
2445 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2446 SHORTCUTS_COL_DATA, &col_data,
2447 SHORTCUTS_COL_REMOVABLE, &removable,
2449 g_assert (col_data != NULL);
2457 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2458 error_removing_bookmark_dialog (impl, path, error);
2461 /* Callback used when the "Remove bookmark" button is clicked */
2463 remove_bookmark_button_clicked_cb (GtkButton *button,
2464 GtkFileChooserDefault *impl)
2466 remove_selected_bookmarks (impl);
2469 struct selection_check_closure {
2470 GtkFileChooserDefault *impl;
2473 gboolean all_folders;
2476 /* Used from gtk_tree_selection_selected_foreach() */
2478 selection_check_foreach_cb (GtkTreeModel *model,
2483 struct selection_check_closure *closure;
2484 GtkTreeIter child_iter;
2485 const GtkFileInfo *info;
2489 closure->num_selected++;
2491 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2493 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2494 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2496 closure->all_folders = closure->all_folders && is_folder;
2497 closure->all_files = closure->all_files && !is_folder;
2500 /* Checks whether the selected items in the file list are all files or all folders */
2502 selection_check (GtkFileChooserDefault *impl,
2504 gboolean *all_files,
2505 gboolean *all_folders)
2507 struct selection_check_closure closure;
2508 GtkTreeSelection *selection;
2510 closure.impl = impl;
2511 closure.num_selected = 0;
2512 closure.all_files = TRUE;
2513 closure.all_folders = TRUE;
2515 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2516 gtk_tree_selection_selected_foreach (selection,
2517 selection_check_foreach_cb,
2520 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2523 *num_selected = closure.num_selected;
2526 *all_files = closure.all_files;
2529 *all_folders = closure.all_folders;
2532 struct get_selected_path_closure {
2533 GtkFileChooserDefault *impl;
2534 const GtkFilePath *path;
2538 get_selected_path_foreach_cb (GtkTreeModel *model,
2543 struct get_selected_path_closure *closure;
2544 GtkTreeIter child_iter;
2548 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2549 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2552 /* Returns a selected path from the file list */
2553 static const GtkFilePath *
2554 get_selected_path (GtkFileChooserDefault *impl)
2556 struct get_selected_path_closure closure;
2557 GtkTreeSelection *selection;
2559 closure.impl = impl;
2560 closure.path = NULL;
2562 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2563 gtk_tree_selection_selected_foreach (selection,
2564 get_selected_path_foreach_cb,
2567 return closure.path;
2571 GtkFileChooserDefault *impl;
2573 } UpdateTooltipData;
2576 update_tooltip (GtkTreeModel *model,
2581 UpdateTooltipData *udata = data;
2582 GtkTreeIter child_iter;
2583 const GtkFileInfo *info;
2585 if (udata->tip == NULL)
2587 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2591 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2592 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2593 gtk_file_info_get_display_name (info));
2598 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2599 * if there are no selected items *and* the current folder is not in the
2600 * bookmarks list. De-sensitize the button otherwise.
2603 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2606 gboolean all_folders;
2610 selection_check (impl, &num_selected, NULL, &all_folders);
2612 if (num_selected == 0)
2613 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2614 else if (num_selected == 1)
2616 const GtkFilePath *path;
2618 path = get_selected_path (impl);
2619 active = all_folders && (shortcut_find_position (impl, path) == -1);
2622 active = all_folders;
2624 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2626 if (impl->browse_files_popup_menu_add_shortcut_item)
2627 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2628 (num_selected == 0) ? FALSE : active);
2632 if (num_selected == 0)
2633 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2634 else if (num_selected > 1)
2635 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2638 GtkTreeSelection *selection;
2639 UpdateTooltipData data;
2641 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2644 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2648 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2653 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2654 * bookmark row is selected in the shortcuts tree.
2657 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2660 gboolean removable = FALSE;
2663 if (shortcuts_get_selected (impl, &iter))
2664 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2665 SHORTCUTS_COL_REMOVABLE, &removable,
2666 SHORTCUTS_COL_NAME, &name,
2669 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2675 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2676 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2685 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2688 gboolean removable = FALSE;
2690 if (impl->browse_shortcuts_popup_menu == NULL)
2693 if (shortcuts_get_selected (impl, &iter))
2694 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2695 SHORTCUTS_COL_REMOVABLE, &removable,
2698 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2699 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2702 /* GtkWidget::drag-begin handler for the shortcuts list. */
2704 shortcuts_drag_begin_cb (GtkWidget *widget,
2705 GdkDragContext *context,
2706 GtkFileChooserDefault *impl)
2709 impl->shortcuts_drag_context = g_object_ref (context);
2714 /* Removes the idle handler for outside drags */
2716 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2718 if (!impl->shortcuts_drag_outside_idle)
2721 g_source_destroy (impl->shortcuts_drag_outside_idle);
2722 impl->shortcuts_drag_outside_idle = NULL;
2726 /* GtkWidget::drag-end handler for the shortcuts list. */
2728 shortcuts_drag_end_cb (GtkWidget *widget,
2729 GdkDragContext *context,
2730 GtkFileChooserDefault *impl)
2733 g_object_unref (impl->shortcuts_drag_context);
2735 shortcuts_cancel_drag_outside_idle (impl);
2737 if (!impl->shortcuts_drag_outside)
2740 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2742 impl->shortcuts_drag_outside = FALSE;
2746 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2748 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2749 GdkDragContext *context,
2750 GtkFileChooserDefault *impl)
2752 g_signal_stop_emission_by_name (widget, "drag_data_delete");
2756 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2760 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2763 GtkTreeView *tree_view;
2766 GdkPixmap *row_pixmap;
2771 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2773 /* Find the selected path and get its drag pixmap */
2775 if (!shortcuts_get_selected (impl, &iter))
2776 g_assert_not_reached ();
2778 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2780 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2781 gtk_tree_path_free (path);
2790 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2796 GdkPixmap *composite;
2797 int row_pixmap_width, row_pixmap_height;
2798 int pixbuf_width, pixbuf_height;
2799 int composite_width, composite_height;
2800 int pixbuf_x, pixbuf_y;
2801 GdkGC *gc, *mask_gc;
2803 GdkBitmap *pixbuf_mask;
2805 /* Create pixmap and mask for composite image */
2807 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2808 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2809 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2811 composite_width = MAX (row_pixmap_width, pixbuf_width);
2812 composite_height = MAX (row_pixmap_height, pixbuf_height);
2814 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2816 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2819 pixbuf_x = composite_width - pixbuf_width;
2821 pixbuf_y = (composite_height - pixbuf_height) / 2;
2823 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2824 gc = gdk_gc_new (composite);
2826 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2827 mask_gc = gdk_gc_new (mask);
2829 gdk_gc_set_foreground (mask_gc, &color);
2830 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2833 color.green = 0xffff;
2834 color.blue = 0xffff;
2835 gdk_gc_set_rgb_fg_color (gc, &color);
2836 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2838 /* Composite the row pixmap and the pixbuf */
2840 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2842 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2843 NULL, &pixbuf_mask, 128);
2844 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2847 pixbuf_width, pixbuf_height);
2848 g_object_unref (pixbuf_mask);
2850 gdk_draw_drawable (composite, gc, row_pixmap,
2853 row_pixmap_width, row_pixmap_height);
2855 gdk_gc_set_foreground (mask_gc, &color);
2856 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2858 gdk_draw_pixbuf (composite, gc, pixbuf,
2861 pixbuf_width, pixbuf_height,
2865 g_object_unref (pixbuf);
2866 g_object_unref (row_pixmap);
2868 row_pixmap = composite;
2872 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2874 gtk_tree_view_get_path_at_pos (tree_view,
2875 tree_view->priv->press_start_x,
2876 tree_view->priv->press_start_y,
2882 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2883 gdk_drawable_get_colormap (row_pixmap),
2886 tree_view->priv->press_start_x + 1,
2887 row_pixmap_y + cell_y + 1);
2889 g_object_unref (row_pixmap);
2891 g_object_unref (mask);
2894 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2895 * handler so that we can tell apart the drag_leave event that comes right
2896 * before a drag_drop, from a normal drag_leave. We don't want to set the
2897 * cursor nor the flag in the latter case.
2900 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2902 GDK_THREADS_ENTER ();
2904 shortcuts_drag_set_delete_cursor (impl, TRUE);
2905 impl->shortcuts_drag_outside = TRUE;
2907 shortcuts_cancel_drag_outside_idle (impl);
2909 GDK_THREADS_LEAVE ();
2915 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2919 shortcuts_drag_leave_cb (GtkWidget *widget,
2920 GdkDragContext *context,
2922 GtkFileChooserDefault *impl)
2925 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2927 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2928 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2929 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2931 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2935 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2937 GTK_TREE_VIEW_DROP_BEFORE);
2939 g_signal_stop_emission_by_name (widget, "drag_leave");
2942 /* Computes the appropriate row and position for dropping */
2944 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2948 GtkTreeViewDropPosition *pos)
2950 GtkTreeView *tree_view;
2951 GtkTreeViewColumn *column;
2955 int bookmarks_index;
2957 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2959 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2961 if (!gtk_tree_view_get_path_at_pos (tree_view,
2963 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2969 row = bookmarks_index + impl->num_bookmarks - 1;
2970 *path = gtk_tree_path_new_from_indices (row, -1);
2971 *pos = GTK_TREE_VIEW_DROP_AFTER;
2975 row = *gtk_tree_path_get_indices (*path);
2976 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2977 gtk_tree_path_free (*path);
2979 if (row < bookmarks_index)
2981 row = bookmarks_index;
2982 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2984 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2986 row = bookmarks_index + impl->num_bookmarks - 1;
2987 *pos = GTK_TREE_VIEW_DROP_AFTER;
2991 if (cell_y < cell.height / 2)
2992 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2994 *pos = GTK_TREE_VIEW_DROP_AFTER;
2997 *path = gtk_tree_path_new_from_indices (row, -1);
3000 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3001 * implement the destination side of DnD by hand, due to limitations in
3002 * GtkTreeView's DnD API.
3005 shortcuts_drag_motion_cb (GtkWidget *widget,
3006 GdkDragContext *context,
3010 GtkFileChooserDefault *impl)
3013 GtkTreeViewDropPosition pos;
3014 GdkDragAction action;
3017 if (gtk_drag_get_source_widget (context) == widget)
3019 shortcuts_cancel_drag_outside_idle (impl);
3021 if (impl->shortcuts_drag_outside)
3023 shortcuts_drag_set_delete_cursor (impl, FALSE);
3024 impl->shortcuts_drag_outside = FALSE;
3029 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
3030 action = GDK_ACTION_COPY;
3031 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
3032 action = GDK_ACTION_MOVE;
3039 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3040 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3041 gtk_tree_path_free (path);
3045 g_signal_stop_emission_by_name (widget, "drag_motion");
3049 gdk_drag_status (context, action, time_);
3056 /* GtkWidget::drag-drop handler for the shortcuts list. */
3058 shortcuts_drag_drop_cb (GtkWidget *widget,
3059 GdkDragContext *context,
3063 GtkFileChooserDefault *impl)
3066 shortcuts_cancel_drag_outside_idle (impl);
3069 g_signal_stop_emission_by_name (widget, "drag_drop");
3073 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3075 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3082 uris = g_uri_list_extract_uris (data);
3084 for (i = 0; uris[i]; i++)
3090 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3094 if (shortcuts_add_bookmark_from_path (impl, path, position))
3097 gtk_file_path_free (path);
3103 g_set_error (&error,
3104 GTK_FILE_CHOOSER_ERROR,
3105 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3106 _("Could not add a bookmark for '%s' "
3107 "because it is an invalid path name."),
3109 error_adding_bookmark_dialog (impl, path, error);
3116 /* Reorders the selected bookmark to the specified position */
3118 shortcuts_reorder (GtkFileChooserDefault *impl,
3126 int bookmarks_index;
3127 const GtkFilePath *file_path;
3128 GtkFilePath *file_path_copy;
3132 /* Get the selected path */
3134 if (!shortcuts_get_selected (impl, &iter))
3135 g_assert_not_reached ();
3137 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3138 old_position = *gtk_tree_path_get_indices (path);
3139 gtk_tree_path_free (path);
3141 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3142 old_position -= bookmarks_index;
3143 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3145 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3146 SHORTCUTS_COL_NAME, &name,
3147 SHORTCUTS_COL_DATA, &col_data,
3148 SHORTCUTS_COL_IS_VOLUME, &is_volume,
3150 g_assert (col_data != NULL);
3151 g_assert (!is_volume);
3153 file_path = col_data;
3154 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3156 /* Remove the path from the old position and insert it in the new one */
3158 if (new_position > old_position)
3161 if (old_position == new_position)
3165 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3167 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3168 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3171 error_adding_bookmark_dialog (impl, file_path_copy, error);
3175 gtk_file_path_free (file_path_copy);
3178 /* Callback used when we get the drag data for the bookmarks list. We add the
3179 * received URIs as bookmarks if they are folders.
3182 shortcuts_drag_data_received_cb (GtkWidget *widget,
3183 GdkDragContext *context,
3186 GtkSelectionData *selection_data,
3191 GtkFileChooserDefault *impl;
3192 GtkTreePath *tree_path;
3193 GtkTreeViewDropPosition tree_pos;
3195 int bookmarks_index;
3197 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3199 /* Compute position */
3201 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3203 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3204 position = *gtk_tree_path_get_indices (tree_path);
3205 gtk_tree_path_free (tree_path);
3207 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3210 g_assert (position >= bookmarks_index);
3211 position -= bookmarks_index;
3213 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3214 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3215 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3216 shortcuts_reorder (impl, position);
3218 g_signal_stop_emission_by_name (widget, "drag_data_received");
3221 /* Callback used when the selection in the shortcuts tree changes */
3223 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3224 GtkFileChooserDefault *impl)
3226 bookmarks_check_remove_sensitivity (impl);
3227 shortcuts_check_popup_sensitivity (impl);
3231 shortcuts_row_separator_func (GtkTreeModel *model,
3235 gint column = GPOINTER_TO_INT (data);
3238 gtk_tree_model_get (model, iter, column, &text, -1);
3248 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3249 * keypresses before the TreeView gets them.
3252 tree_view_keybinding_cb (GtkWidget *tree_view,
3254 GtkFileChooserDefault *impl)
3256 if ((event->keyval == GDK_slash
3257 || event->keyval == GDK_KP_Divide
3259 || event->keyval == GDK_asciitilde
3261 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3263 location_popup_handler (impl, event->string);
3270 /* Callback used when the file list's popup menu is detached */
3272 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3275 GtkFileChooserDefault *impl;
3277 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3278 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3280 impl->browse_shortcuts_popup_menu = NULL;
3281 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3282 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3286 remove_shortcut_cb (GtkMenuItem *item,
3287 GtkFileChooserDefault *impl)
3289 remove_selected_bookmarks (impl);
3292 /* Rename the selected bookmark */
3294 rename_selected_bookmark (GtkFileChooserDefault *impl)
3298 GtkTreeViewColumn *column;
3299 GtkCellRenderer *cell;
3302 if (shortcuts_get_selected (impl, &iter))
3304 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3305 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3306 renderers = gtk_tree_view_column_get_cell_renderers (column);
3307 cell = g_list_nth_data (renderers, 1);
3308 g_list_free (renderers);
3309 g_object_set (cell, "editable", TRUE, NULL);
3310 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3311 path, column, cell, TRUE);
3312 gtk_tree_path_free (path);
3317 rename_shortcut_cb (GtkMenuItem *item,
3318 GtkFileChooserDefault *impl)
3320 rename_selected_bookmark (impl);
3323 /* Constructs the popup menu for the file list if needed */
3325 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3329 if (impl->browse_shortcuts_popup_menu)
3332 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3333 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3334 impl->browse_shortcuts_tree_view,
3335 shortcuts_popup_menu_detach_cb);
3337 item = gtk_image_menu_item_new_with_label (_("Remove"));
3338 impl->browse_shortcuts_popup_menu_remove_item = item;
3339 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3340 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3341 g_signal_connect (item, "activate",
3342 G_CALLBACK (remove_shortcut_cb), impl);
3343 gtk_widget_show (item);
3344 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3346 item = gtk_menu_item_new_with_label (_("Rename..."));
3347 impl->browse_shortcuts_popup_menu_rename_item = item;
3348 g_signal_connect (item, "activate",
3349 G_CALLBACK (rename_shortcut_cb), impl);
3350 gtk_widget_show (item);
3351 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3353 shortcuts_check_popup_sensitivity (impl);
3357 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3359 shortcuts_build_popup_menu (impl);
3363 popup_position_func (GtkMenu *menu,
3367 gpointer user_data);
3370 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3371 GdkEventButton *event)
3373 shortcuts_update_popup_menu (impl);
3375 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3376 NULL, NULL, NULL, NULL,
3377 event->button, event->time);
3380 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3382 popup_position_func, impl->browse_shortcuts_tree_view,
3383 0, GDK_CURRENT_TIME);
3384 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3389 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3391 shortcuts_popup_menu_cb (GtkWidget *widget,
3392 GtkFileChooserDefault *impl)
3394 shortcuts_popup_menu (impl, NULL);
3398 /* Callback used when a button is pressed on the shortcuts list.
3399 * We trap button 3 to bring up a popup menu.
3402 shortcuts_button_press_event_cb (GtkWidget *widget,
3403 GdkEventButton *event,
3404 GtkFileChooserDefault *impl)
3406 static gboolean in_press = FALSE;
3412 if (event->button != 3)
3416 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3422 shortcuts_popup_menu (impl, event);
3427 shortcuts_edited (GtkCellRenderer *cell,
3430 GtkFileChooserDefault *impl)
3434 GtkFilePath *shortcut;
3436 g_object_set (cell, "editable", FALSE, NULL);
3438 path = gtk_tree_path_new_from_string (path_string);
3439 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3440 g_assert_not_reached ();
3442 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3443 SHORTCUTS_COL_DATA, &shortcut,
3445 gtk_tree_path_free (path);
3447 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3451 shortcuts_editing_canceled (GtkCellRenderer *cell,
3452 GtkFileChooserDefault *impl)
3454 g_object_set (cell, "editable", FALSE, NULL);
3457 /* Creates the widgets for the shortcuts and bookmarks tree */
3459 shortcuts_list_create (GtkFileChooserDefault *impl)
3462 GtkTreeSelection *selection;
3463 GtkTreeViewColumn *column;
3464 GtkCellRenderer *renderer;
3466 /* Scrolled window */
3468 swin = gtk_scrolled_window_new (NULL, NULL);
3469 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3470 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3471 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3473 gtk_widget_show (swin);
3477 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3478 #ifdef PROFILE_FILE_CHOOSER
3479 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3481 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3482 G_CALLBACK (tree_view_keybinding_cb), impl);
3483 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3484 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3485 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3486 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3487 /* Accessible object name for the file chooser's shortcuts pane */
3488 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3490 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3492 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3494 shortcuts_source_targets,
3495 num_shortcuts_source_targets,
3498 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3499 GTK_DEST_DEFAULT_ALL,
3500 shortcuts_dest_targets,
3501 num_shortcuts_dest_targets,
3502 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3504 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3505 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3506 gtk_tree_selection_set_select_function (selection,
3507 shortcuts_select_func,
3510 g_signal_connect (selection, "changed",
3511 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3513 g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3514 G_CALLBACK (shortcuts_row_activated_cb), impl);
3516 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3517 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3519 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3520 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3521 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3522 G_CALLBACK (shortcuts_drag_end_cb), impl);
3523 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3524 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3526 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3527 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3528 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3529 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3530 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3531 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3532 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3533 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3535 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3536 gtk_widget_show (impl->browse_shortcuts_tree_view);
3540 column = gtk_tree_view_column_new ();
3541 /* Column header for the file chooser's shortcuts pane */
3542 gtk_tree_view_column_set_title (column, _("_Places"));
3544 renderer = gtk_cell_renderer_pixbuf_new ();
3545 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3546 gtk_tree_view_column_set_attributes (column, renderer,
3547 "pixbuf", SHORTCUTS_COL_PIXBUF,
3548 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3551 renderer = gtk_cell_renderer_text_new ();
3552 g_signal_connect (renderer, "edited",
3553 G_CALLBACK (shortcuts_edited), impl);
3554 g_signal_connect (renderer, "editing-canceled",
3555 G_CALLBACK (shortcuts_editing_canceled), impl);
3556 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3557 gtk_tree_view_column_set_attributes (column, renderer,
3558 "text", SHORTCUTS_COL_NAME,
3561 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3562 shortcuts_row_separator_func,
3563 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3566 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3571 /* Creates the widgets for the shortcuts/bookmarks pane */
3573 shortcuts_pane_create (GtkFileChooserDefault *impl,
3574 GtkSizeGroup *size_group)
3580 vbox = gtk_vbox_new (FALSE, 6);
3581 gtk_widget_show (vbox);
3583 /* Shortcuts tree */
3585 widget = shortcuts_list_create (impl);
3586 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3588 /* Box for buttons */
3590 hbox = gtk_hbox_new (TRUE, 6);
3591 gtk_size_group_add_widget (size_group, hbox);
3592 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3593 gtk_widget_show (hbox);
3595 /* Add bookmark button */
3597 impl->browse_shortcuts_add_button = button_new (impl,
3602 G_CALLBACK (add_bookmark_button_clicked_cb));
3603 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3604 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3605 _("Add the selected folder to the Bookmarks"), NULL);
3607 /* Remove bookmark button */
3609 impl->browse_shortcuts_remove_button = button_new (impl,
3614 G_CALLBACK (remove_bookmark_button_clicked_cb));
3615 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3616 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3617 _("Remove the selected bookmark"), NULL);
3622 /* Handles key press events on the file list, so that we can trap Enter to
3623 * activate the default button on our own. Also, checks to see if '/' has been
3624 * pressed. See comment by tree_view_keybinding_cb() for more details.
3627 trap_activate_cb (GtkWidget *widget,
3631 GtkFileChooserDefault *impl;
3634 impl = (GtkFileChooserDefault *) data;
3636 modifiers = gtk_accelerator_get_default_mod_mask ();
3638 if ((event->keyval == GDK_slash
3639 || event->keyval == GDK_KP_Divide
3641 || event->keyval == GDK_asciitilde
3643 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3645 location_popup_handler (impl, event->string);
3649 if ((event->keyval == GDK_Return
3650 || event->keyval == GDK_ISO_Enter
3651 || event->keyval == GDK_KP_Enter
3652 || event->keyval == GDK_space)
3653 && ((event->state & modifiers) == 0)
3654 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3655 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3659 window = get_toplevel (widget);
3661 && widget != window->default_widget
3662 && !(widget == window->focus_widget &&
3663 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3665 gtk_window_activate_default (window);
3673 /* Callback used when the file list's popup menu is detached */
3675 popup_menu_detach_cb (GtkWidget *attach_widget,
3678 GtkFileChooserDefault *impl;
3680 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3681 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3683 impl->browse_files_popup_menu = NULL;
3684 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3685 impl->browse_files_popup_menu_hidden_files_item = NULL;
3688 /* Callback used when the "Add to Bookmarks" menu item is activated */
3690 add_to_shortcuts_cb (GtkMenuItem *item,
3691 GtkFileChooserDefault *impl)
3693 bookmarks_add_selected_folder (impl);
3696 /* Callback used when the "Show Hidden Files" menu item is toggled */
3698 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3699 GtkFileChooserDefault *impl)
3702 "show-hidden", gtk_check_menu_item_get_active (item),
3706 /* Shows an error dialog about not being able to select a dragged file */
3708 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3709 const GtkFilePath *path,
3713 _("Could not select file"),
3718 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3723 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3725 for (i = 1; uris[i]; i++)
3730 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3734 GError *error = NULL;
3736 gtk_file_chooser_default_select_path (chooser, path, &error);
3738 error_selecting_dragged_file_dialog (impl, path, error);
3740 gtk_file_path_free (path);
3745 struct FileListDragData
3747 GtkFileChooserDefault *impl;
3753 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
3754 const GtkFileInfo *info,
3755 const GError *error,
3758 gboolean cancelled = handle->cancelled;
3759 struct FileListDragData *data = user_data;
3760 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3762 if (handle != data->impl->file_list_drag_data_received_handle)
3765 data->impl->file_list_drag_data_received_handle = NULL;
3767 if (cancelled || error)
3770 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3771 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3772 data->uris[1] == 0 && !error &&
3773 gtk_file_info_get_is_folder (info))
3774 change_folder_and_display_error (data->impl, data->path);
3777 GError *error = NULL;
3779 gtk_file_chooser_default_unselect_all (chooser);
3780 gtk_file_chooser_default_select_path (chooser, data->path, &error);
3782 error_selecting_dragged_file_dialog (data->impl, data->path, error);
3784 browse_files_center_selected_row (data->impl);
3787 if (data->impl->select_multiple)
3788 file_list_drag_data_select_uris (data->impl, data->uris);
3791 g_object_unref (data->impl);
3792 g_strfreev (data->uris);
3793 gtk_file_path_free (data->path);
3796 g_object_unref (handle);
3800 file_list_drag_data_received_cb (GtkWidget *widget,
3801 GdkDragContext *context,
3804 GtkSelectionData *selection_data,
3809 GtkFileChooserDefault *impl;
3810 GtkFileChooser *chooser;
3814 GError *error = NULL;
3816 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3817 chooser = GTK_FILE_CHOOSER (data);
3819 /* Parse the text/uri-list string, navigate to the first one */
3820 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
3824 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3828 struct FileListDragData *data;
3830 data = g_new0 (struct FileListDragData, 1);
3831 data->impl = g_object_ref (impl);
3835 if (impl->file_list_drag_data_received_handle)
3836 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
3838 impl->file_list_drag_data_received_handle =
3839 gtk_file_system_get_info (impl->file_system, path,
3840 GTK_FILE_INFO_IS_FOLDER,
3841 file_list_drag_data_received_get_info_cb,
3847 g_set_error (&error,
3848 GTK_FILE_CHOOSER_ERROR,
3849 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3850 _("Could not select file '%s' "
3851 "because it is an invalid path name."),
3853 error_selecting_dragged_file_dialog (impl, NULL, error);
3856 if (impl->select_multiple)
3857 file_list_drag_data_select_uris (impl, uris);
3863 g_signal_stop_emission_by_name (widget, "drag_data_received");
3866 /* Don't do anything with the drag_drop signal */
3868 file_list_drag_drop_cb (GtkWidget *widget,
3869 GdkDragContext *context,
3873 GtkFileChooserDefault *impl)
3875 g_signal_stop_emission_by_name (widget, "drag_drop");
3879 /* Disable the normal tree drag motion handler, it makes it look like you're
3880 dropping the dragged item onto a tree item */
3882 file_list_drag_motion_cb (GtkWidget *widget,
3883 GdkDragContext *context,
3887 GtkFileChooserDefault *impl)
3889 g_signal_stop_emission_by_name (widget, "drag_motion");
3893 /* Constructs the popup menu for the file list if needed */
3895 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3899 if (impl->browse_files_popup_menu)
3902 impl->browse_files_popup_menu = gtk_menu_new ();
3903 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3904 impl->browse_files_tree_view,
3905 popup_menu_detach_cb);
3907 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3908 impl->browse_files_popup_menu_add_shortcut_item = item;
3909 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3910 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3911 gtk_widget_set_sensitive (item, FALSE);
3912 g_signal_connect (item, "activate",
3913 G_CALLBACK (add_to_shortcuts_cb), impl);
3914 gtk_widget_show (item);
3915 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3917 item = gtk_separator_menu_item_new ();
3918 gtk_widget_show (item);
3919 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3921 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3922 impl->browse_files_popup_menu_hidden_files_item = item;
3923 g_signal_connect (item, "toggled",
3924 G_CALLBACK (show_hidden_toggled_cb), impl);
3925 gtk_widget_show (item);
3926 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3929 /* Updates the popup menu for the file list, creating it if necessary */
3931 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3933 file_list_build_popup_menu (impl);
3935 /* The sensitivity of the Add to Bookmarks item is set in
3936 * bookmarks_check_add_sensitivity()
3939 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3940 G_CALLBACK (show_hidden_toggled_cb), impl);
3941 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3943 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3944 G_CALLBACK (show_hidden_toggled_cb), impl);
3948 popup_position_func (GtkMenu *menu,
3954 GtkWidget *widget = GTK_WIDGET (user_data);
3955 GdkScreen *screen = gtk_widget_get_screen (widget);
3958 GdkRectangle monitor;
3960 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3962 gdk_window_get_origin (widget->window, x, y);
3964 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3966 *x += (widget->allocation.width - req.width) / 2;
3967 *y += (widget->allocation.height - req.height) / 2;
3969 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3970 gtk_menu_set_monitor (menu, monitor_num);
3971 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3973 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3974 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3980 file_list_popup_menu (GtkFileChooserDefault *impl,
3981 GdkEventButton *event)
3983 file_list_update_popup_menu (impl);
3985 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3986 NULL, NULL, NULL, NULL,
3987 event->button, event->time);
3990 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3992 popup_position_func, impl->browse_files_tree_view,
3993 0, GDK_CURRENT_TIME);
3994 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4000 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4002 list_popup_menu_cb (GtkWidget *widget,
4003 GtkFileChooserDefault *impl)
4005 file_list_popup_menu (impl, NULL);
4009 /* Callback used when a button is pressed on the file list. We trap button 3 to
4010 * bring up a popup menu.
4013 list_button_press_event_cb (GtkWidget *widget,
4014 GdkEventButton *event,
4015 GtkFileChooserDefault *impl)
4017 static gboolean in_press = FALSE;
4023 if (event->button != 3)
4027 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4030 file_list_popup_menu (impl, event);
4034 /* Creates the widgets for the file list */
4036 create_file_list (GtkFileChooserDefault *impl)
4039 GtkTreeSelection *selection;
4040 GtkTreeViewColumn *column;
4041 GtkCellRenderer *renderer;
4043 /* Scrolled window */
4045 swin = gtk_scrolled_window_new (NULL, NULL);
4046 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4047 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4048 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4051 /* Tree/list view */
4053 impl->browse_files_tree_view = gtk_tree_view_new ();
4054 #ifdef PROFILE_FILE_CHOOSER
4055 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4057 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4058 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4060 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4061 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4063 gtk_drag_dest_set (impl->browse_files_tree_view,
4064 GTK_DEST_DEFAULT_ALL,
4065 file_list_dest_targets,
4066 num_file_list_dest_targets,
4067 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4069 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4070 G_CALLBACK (list_row_activated), impl);
4071 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4072 G_CALLBACK (trap_activate_cb), impl);
4073 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4074 G_CALLBACK (list_popup_menu_cb), impl);
4075 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4076 G_CALLBACK (list_button_press_event_cb), impl);
4078 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4079 G_CALLBACK (file_list_drag_data_received_cb), impl);
4080 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4081 G_CALLBACK (file_list_drag_drop_cb), impl);
4082 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4083 G_CALLBACK (file_list_drag_motion_cb), impl);
4085 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4086 gtk_tree_selection_set_select_function (selection,
4089 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4091 file_list_source_targets,
4092 num_file_list_source_targets,
4095 g_signal_connect (selection, "changed",
4096 G_CALLBACK (list_selection_changed), impl);
4098 /* Filename column */
4100 impl->list_name_column = gtk_tree_view_column_new ();
4101 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4102 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4103 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4104 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4106 renderer = gtk_cell_renderer_pixbuf_new ();
4107 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4108 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4109 list_icon_data_func, impl, NULL);
4111 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4112 g_object_set (impl->list_name_renderer,
4113 "ellipsize", PANGO_ELLIPSIZE_END,
4115 g_signal_connect (impl->list_name_renderer, "edited",
4116 G_CALLBACK (renderer_edited_cb), impl);
4117 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4118 G_CALLBACK (renderer_editing_canceled_cb), impl);
4119 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4120 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4121 list_name_data_func, impl, NULL);
4123 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4127 column = gtk_tree_view_column_new ();
4128 gtk_tree_view_column_set_title (column, _("Size"));
4130 renderer = gtk_cell_renderer_text_new ();
4131 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4132 gtk_tree_view_column_set_cell_data_func (column, renderer,
4133 list_size_data_func, impl, NULL);
4134 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4135 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4137 /* Modification time column */
4139 column = gtk_tree_view_column_new ();
4140 gtk_tree_view_column_set_resizable (column, TRUE);
4141 gtk_tree_view_column_set_title (column, _("Modified"));
4143 renderer = gtk_cell_renderer_text_new ();
4144 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4145 gtk_tree_view_column_set_cell_data_func (column, renderer,
4146 list_mtime_data_func, impl, NULL);
4147 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
4148 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4149 gtk_widget_show_all (swin);
4155 create_path_bar (GtkFileChooserDefault *impl)
4157 GtkWidget *path_bar;
4159 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4160 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4166 set_filter_tooltip (GtkWidget *widget,
4169 GtkTooltips *tooltips = (GtkTooltips *)data;
4171 if (GTK_IS_BUTTON (widget))
4172 gtk_tooltips_set_tip (tooltips, widget,
4173 _("Select which types of files are shown"),
4178 realize_filter_combo (GtkWidget *combo,
4181 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4183 gtk_container_forall (GTK_CONTAINER (combo),
4188 /* Creates the widgets for the files/folders pane */
4190 file_pane_create (GtkFileChooserDefault *impl,
4191 GtkSizeGroup *size_group)
4197 vbox = gtk_vbox_new (FALSE, 6);
4198 gtk_widget_show (vbox);
4200 /* Box for lists and preview */
4202 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4203 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4204 gtk_widget_show (hbox);
4208 widget = create_file_list (impl);
4209 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4213 impl->preview_box = gtk_vbox_new (FALSE, 12);
4214 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4215 /* Don't show preview box initially */
4219 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4221 widget = filter_create (impl);
4223 g_signal_connect (widget, "realize",
4224 G_CALLBACK (realize_filter_combo), impl);
4226 gtk_widget_show (widget);
4227 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4229 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4230 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4235 /* Callback used when the "Browse for more folders" expander is toggled */
4237 expander_changed_cb (GtkExpander *expander,
4239 GtkFileChooserDefault *impl)
4241 update_appearance (impl);
4244 /* Callback used when the selection changes in the save folder combo box */
4246 save_folder_combo_changed_cb (GtkComboBox *combo,
4247 GtkFileChooserDefault *impl)
4251 if (impl->changing_folder)
4254 if (gtk_combo_box_get_active_iter (combo, &iter))
4255 shortcuts_activate_iter (impl, &iter);
4258 /* Creates the combo box with the save folders */
4260 save_folder_combo_create (GtkFileChooserDefault *impl)
4263 GtkCellRenderer *cell;
4265 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4266 "model", impl->shortcuts_model,
4267 "focus-on-click", FALSE,
4269 gtk_widget_show (combo);
4271 cell = gtk_cell_renderer_pixbuf_new ();
4272 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4273 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4274 "pixbuf", SHORTCUTS_COL_PIXBUF,
4275 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4276 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4279 cell = gtk_cell_renderer_text_new ();
4280 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4281 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4282 "text", SHORTCUTS_COL_NAME,
4283 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4286 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4287 shortcuts_row_separator_func,
4288 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
4291 g_signal_connect (combo, "changed",
4292 G_CALLBACK (save_folder_combo_changed_cb), impl);
4297 /* Creates the widgets specific to Save mode */
4299 save_widgets_create (GtkFileChooserDefault *impl)
4304 GtkWidget *alignment;
4306 if (impl->save_widgets != NULL)
4309 location_switch_to_path_bar (impl);
4311 vbox = gtk_vbox_new (FALSE, 12);
4313 table = gtk_table_new (2, 2, FALSE);
4314 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4315 gtk_widget_show (table);
4316 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4317 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4321 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4322 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4323 gtk_table_attach (GTK_TABLE (table), widget,
4327 gtk_widget_show (widget);
4329 /* Location entry */
4331 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4332 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4334 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4335 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4336 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4338 GTK_EXPAND | GTK_FILL, 0,
4340 gtk_widget_show (impl->location_entry);
4341 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4344 impl->save_folder_label = gtk_label_new (NULL);
4345 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4346 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4350 gtk_widget_show (impl->save_folder_label);
4352 impl->save_folder_combo = save_folder_combo_create (impl);
4353 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4355 GTK_EXPAND | GTK_FILL, GTK_FILL,
4357 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4360 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4361 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4363 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4364 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4365 g_signal_connect (impl->save_expander, "notify::expanded",
4366 G_CALLBACK (expander_changed_cb),
4368 gtk_widget_show_all (alignment);
4370 impl->save_widgets = vbox;
4371 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4372 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4373 gtk_widget_show (impl->save_widgets);
4376 /* Destroys the widgets specific to Save mode */
4378 save_widgets_destroy (GtkFileChooserDefault *impl)
4380 if (impl->save_widgets == NULL)
4383 gtk_widget_destroy (impl->save_widgets);
4384 impl->save_widgets = NULL;
4385 impl->location_entry = NULL;
4386 impl->save_folder_label = NULL;
4387 impl->save_folder_combo = NULL;
4388 impl->save_expander = NULL;
4391 /* Turns on the path bar widget. Can be called even if we are already in that
4395 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4397 if (impl->location_entry)
4399 gtk_widget_destroy (impl->location_entry);
4400 impl->location_entry = NULL;
4403 gtk_widget_hide (impl->location_entry_box);
4406 /* Sets the full path of the current folder as the text in the location entry. */
4408 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4412 if (!impl->current_folder)
4415 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4419 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4422 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4429 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4433 gboolean need_slash;
4436 len = strlen (text);
4437 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4443 slash_text = g_new (char, len + 2);
4444 strcpy (slash_text, text);
4445 slash_text[len] = G_DIR_SEPARATOR;
4446 slash_text[len + 1] = 0;
4452 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4457 /* Turns on the location entry. Can be called even if we are already in that
4461 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4463 if (impl->location_entry)
4464 gtk_widget_destroy (impl->location_entry);
4468 gtk_widget_show (impl->location_entry_box);
4472 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4473 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4475 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4476 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4478 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4479 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4481 /* Configure the entry */
4483 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4487 gtk_widget_show (impl->location_entry);
4488 gtk_widget_grab_focus (impl->location_entry);
4491 /* Sets a new location mode. set_buttons determines whether the toggle button
4492 * for the mode will also be changed.
4495 location_mode_set (GtkFileChooserDefault *impl,
4496 LocationMode new_mode,
4497 gboolean set_button)
4499 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4500 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4502 GtkWindow *toplevel;
4503 GtkWidget *current_focus;
4504 gboolean button_active;
4505 gboolean switch_to_file_list;
4509 case LOCATION_MODE_PATH_BAR:
4510 button_active = FALSE;
4512 /* The location_entry will disappear when we switch to path bar mode. So,
4513 * we'll focus the file list in that case, to avoid having a window with
4514 * no focused widget.
4516 toplevel = get_toplevel (GTK_WIDGET (impl));
4517 switch_to_file_list = FALSE;
4520 current_focus = gtk_window_get_focus (toplevel);
4521 if (!current_focus || current_focus == impl->location_entry)
4522 switch_to_file_list = TRUE;
4525 location_switch_to_path_bar (impl);
4527 if (switch_to_file_list)
4528 gtk_widget_grab_focus (impl->browse_files_tree_view);
4532 case LOCATION_MODE_FILENAME_ENTRY:
4533 button_active = TRUE;
4534 location_switch_to_filename_entry (impl);
4538 g_assert_not_reached ();
4544 g_signal_handlers_block_by_func (impl->location_button,
4545 G_CALLBACK (location_button_toggled_cb), impl);
4547 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4549 g_signal_handlers_unblock_by_func (impl->location_button,
4550 G_CALLBACK (location_button_toggled_cb), impl);
4554 impl->location_mode = new_mode;
4557 /* Callback used when one of the location mode buttons is toggled */
4559 location_button_toggled_cb (GtkToggleButton *toggle,
4560 GtkFileChooserDefault *impl)
4563 LocationMode new_mode;
4565 is_active = gtk_toggle_button_get_active (toggle);
4569 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4570 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4574 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4575 new_mode = LOCATION_MODE_PATH_BAR;
4578 location_mode_set (impl, new_mode, FALSE);
4581 /* Creates a toggle button for the location entry. */
4583 location_button_create (GtkFileChooserDefault *impl)
4588 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4589 gtk_widget_show (image);
4591 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4595 g_signal_connect (impl->location_button, "toggled",
4596 G_CALLBACK (location_button_toggled_cb), impl);
4598 str = _("Type a file name");
4600 gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4601 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4604 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4606 browse_widgets_create (GtkFileChooserDefault *impl)
4612 GtkSizeGroup *size_group;
4614 /* size group is used by the [+][-] buttons and the filter combo */
4615 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4616 vbox = gtk_vbox_new (FALSE, 12);
4618 /* Location widgets */
4619 hbox = gtk_hbox_new (FALSE, 12);
4620 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4621 gtk_widget_show (hbox);
4623 location_button_create (impl);
4624 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
4628 impl->browse_path_bar = create_path_bar (impl);
4629 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4630 gtk_widget_show_all (impl->browse_path_bar);
4631 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4634 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4635 g_signal_connect (impl->browse_new_folder_button, "clicked",
4636 G_CALLBACK (new_folder_button_clicked), impl);
4637 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4639 /* Box for the location label and entry */
4641 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
4642 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
4644 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4645 gtk_widget_show (impl->location_label);
4646 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4649 hpaned = gtk_hpaned_new ();
4650 gtk_widget_show (hpaned);
4651 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
4652 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4654 widget = shortcuts_pane_create (impl, size_group);
4655 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4656 widget = file_pane_create (impl, size_group);
4657 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4659 g_object_unref (size_group);
4665 gtk_file_chooser_default_constructor (GType type,
4666 guint n_construct_properties,
4667 GObjectConstructParam *construct_params)
4669 GtkFileChooserDefault *impl;
4672 profile_start ("start", NULL);
4674 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4675 n_construct_properties,
4677 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4679 g_assert (impl->file_system);
4681 gtk_widget_push_composite_child ();
4683 /* Shortcuts model */
4685 shortcuts_model_create (impl);
4687 /* The browse widgets */
4688 impl->browse_widgets = browse_widgets_create (impl);
4689 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4691 /* Alignment to hold extra widget */
4692 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4693 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4695 gtk_widget_pop_composite_child ();
4696 update_appearance (impl);
4698 profile_end ("end", NULL);
4703 /* Sets the extra_widget by packing it in the appropriate place */
4705 set_extra_widget (GtkFileChooserDefault *impl,
4706 GtkWidget *extra_widget)
4710 g_object_ref (extra_widget);
4711 /* FIXME: is this right ? */
4712 gtk_widget_show (extra_widget);
4715 if (impl->extra_widget)
4717 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4718 g_object_unref (impl->extra_widget);
4721 impl->extra_widget = extra_widget;
4722 if (impl->extra_widget)
4724 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4725 gtk_widget_show (impl->extra_align);
4728 gtk_widget_hide (impl->extra_align);
4732 set_local_only (GtkFileChooserDefault *impl,
4733 gboolean local_only)
4735 if (local_only != impl->local_only)
4737 impl->local_only = local_only;
4739 if (impl->shortcuts_model && impl->file_system)
4741 shortcuts_add_volumes (impl);
4742 shortcuts_add_bookmarks (impl);
4746 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4748 /* If we are pointing to a non-local folder, make an effort to change
4749 * back to a local folder, but it's really up to the app to not cause
4750 * such a situation, so we ignore errors.
4752 const gchar *home = g_get_home_dir ();
4753 GtkFilePath *home_path;
4758 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4760 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4762 gtk_file_path_free (home_path);
4768 volumes_changed_cb (GtkFileSystem *file_system,
4769 GtkFileChooserDefault *impl)
4771 shortcuts_add_volumes (impl);
4774 /* Callback used when the set of bookmarks changes in the file system */
4776 bookmarks_changed_cb (GtkFileSystem *file_system,
4777 GtkFileChooserDefault *impl)
4779 shortcuts_add_bookmarks (impl);
4781 bookmarks_check_add_sensitivity (impl);
4782 bookmarks_check_remove_sensitivity (impl);
4783 shortcuts_check_popup_sensitivity (impl);
4786 /* Sets the file chooser to multiple selection mode */
4788 set_select_multiple (GtkFileChooserDefault *impl,
4789 gboolean select_multiple,
4790 gboolean property_notify)
4792 GtkTreeSelection *selection;
4793 GtkSelectionMode mode;
4795 if (select_multiple == impl->select_multiple)
4798 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4800 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4801 gtk_tree_selection_set_mode (selection, mode);
4803 impl->select_multiple = select_multiple;
4804 g_object_notify (G_OBJECT (impl), "select-multiple");
4806 check_preview_change (impl);
4810 set_file_system_backend (GtkFileChooserDefault *impl,
4811 const char *backend)
4813 profile_start ("start for backend", backend ? backend : "default");
4815 if (impl->file_system)
4817 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4818 impl->volumes_changed_id = 0;
4819 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4820 impl->bookmarks_changed_id = 0;
4821 g_object_unref (impl->file_system);
4824 impl->file_system = NULL;
4826 impl->file_system = _gtk_file_system_create (backend);
4829 GtkSettings *settings = gtk_settings_get_default ();
4830 gchar *default_backend = NULL;
4832 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4833 if (default_backend)
4835 impl->file_system = _gtk_file_system_create (default_backend);
4836 g_free (default_backend);
4840 if (!impl->file_system)
4842 #if defined (G_OS_UNIX)
4843 impl->file_system = gtk_file_system_unix_new ();
4844 #elif defined (G_OS_WIN32)
4845 impl->file_system = gtk_file_system_win32_new ();
4847 #error "No default filesystem implementation on the platform"
4851 if (impl->file_system)
4853 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4854 G_CALLBACK (volumes_changed_cb),
4856 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4857 G_CALLBACK (bookmarks_changed_cb),
4861 profile_end ("end", NULL);
4864 /* This function is basically a do_all function.
4866 * It sets the visibility on all the widgets based on the current state, and
4867 * moves the custom_widget if needed.
4870 update_appearance (GtkFileChooserDefault *impl)
4872 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4873 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4877 gtk_widget_hide (impl->location_button);
4878 save_widgets_create (impl);
4880 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4881 text = _("Save in _folder:");
4883 text = _("Create in _folder:");
4885 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4887 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4889 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4890 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4891 gtk_widget_show (impl->browse_widgets);
4895 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4896 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4897 gtk_widget_hide (impl->browse_widgets);
4900 gtk_widget_show (impl->browse_new_folder_button);
4902 if (impl->select_multiple)
4904 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4905 "Re-setting to single selection mode.");
4906 set_select_multiple (impl, FALSE, TRUE);
4909 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4910 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4912 gtk_widget_show (impl->location_button);
4913 save_widgets_destroy (impl);
4914 gtk_widget_show (impl->browse_widgets);
4915 location_mode_set (impl, impl->location_mode, TRUE);
4918 if (impl->location_entry)
4919 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4921 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4922 gtk_widget_hide (impl->browse_new_folder_button);
4924 gtk_widget_show (impl->browse_new_folder_button);
4926 /* This *is* needed; we need to redraw the file list because the "sensitivity"
4927 * of files may change depending whether we are in a file or folder-only mode.
4929 gtk_widget_queue_draw (impl->browse_files_tree_view);
4931 g_signal_emit_by_name (impl, "default-size-changed");
4935 gtk_file_chooser_default_set_property (GObject *object,
4937 const GValue *value,
4941 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4945 case GTK_FILE_CHOOSER_PROP_ACTION:
4947 GtkFileChooserAction action = g_value_get_enum (value);
4949 if (action != impl->action)
4951 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4953 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4954 && impl->select_multiple)
4956 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4957 "this is not allowed in multiple selection mode. Resetting the file chooser "
4958 "to single selection mode.");
4959 set_select_multiple (impl, FALSE, TRUE);
4961 impl->action = action;
4962 update_appearance (impl);
4967 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4968 set_file_system_backend (impl, g_value_get_string (value));
4971 case GTK_FILE_CHOOSER_PROP_FILTER:
4972 set_current_filter (impl, g_value_get_object (value));
4975 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4976 set_local_only (impl, g_value_get_boolean (value));
4979 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4980 set_preview_widget (impl, g_value_get_object (value));
4983 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4984 impl->preview_widget_active = g_value_get_boolean (value);
4985 update_preview_widget_visibility (impl);
4988 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4989 impl->use_preview_label = g_value_get_boolean (value);
4990 update_preview_widget_visibility (impl);
4993 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4994 set_extra_widget (impl, g_value_get_object (value));
4997 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4999 gboolean select_multiple = g_value_get_boolean (value);
5000 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5003 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5004 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5005 "leaving the file chooser in single selection mode.");
5009 set_select_multiple (impl, select_multiple, FALSE);
5013 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5015 gboolean show_hidden = g_value_get_boolean (value);
5016 if (show_hidden != impl->show_hidden)
5018 impl->show_hidden = show_hidden;
5020 if (impl->browse_files_model)
5021 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5026 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5028 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5029 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5034 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5040 gtk_file_chooser_default_get_property (GObject *object,
5045 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5049 case GTK_FILE_CHOOSER_PROP_ACTION:
5050 g_value_set_enum (value, impl->action);
5053 case GTK_FILE_CHOOSER_PROP_FILTER:
5054 g_value_set_object (value, impl->current_filter);
5057 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5058 g_value_set_boolean (value, impl->local_only);
5061 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5062 g_value_set_object (value, impl->preview_widget);
5065 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5066 g_value_set_boolean (value, impl->preview_widget_active);
5069 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5070 g_value_set_boolean (value, impl->use_preview_label);
5073 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5074 g_value_set_object (value, impl->extra_widget);
5077 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5078 g_value_set_boolean (value, impl->select_multiple);
5081 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5082 g_value_set_boolean (value, impl->show_hidden);
5085 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5086 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5095 /* Removes the settings signal handler. It's safe to call multiple times */
5097 remove_settings_signal (GtkFileChooserDefault *impl,
5100 if (impl->settings_signal_id)
5102 GtkSettings *settings;
5104 settings = gtk_settings_get_for_screen (screen);
5105 g_signal_handler_disconnect (settings,
5106 impl->settings_signal_id);
5107 impl->settings_signal_id = 0;
5112 gtk_file_chooser_default_dispose (GObject *object)
5115 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5117 if (impl->extra_widget)
5119 g_object_unref (impl->extra_widget);
5120 impl->extra_widget = NULL;
5123 if (impl->volumes_changed_id > 0)
5125 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5126 impl->volumes_changed_id = 0;
5129 if (impl->bookmarks_changed_id > 0)
5131 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5132 impl->bookmarks_changed_id = 0;
5135 pending_select_paths_free (impl);
5137 /* cancel all pending operations */
5138 if (impl->pending_handles)
5140 for (l = impl->pending_handles; l; l = l->next)
5142 GtkFileSystemHandle *handle =l->data;
5143 gtk_file_system_cancel_operation (handle);
5145 g_slist_free (impl->pending_handles);
5146 impl->pending_handles = NULL;
5149 if (impl->reload_icon_handles)
5151 for (l = impl->reload_icon_handles; l; l = l->next)
5153 GtkFileSystemHandle *handle =l->data;
5154 gtk_file_system_cancel_operation (handle);
5156 g_slist_free (impl->reload_icon_handles);
5157 impl->reload_icon_handles = NULL;
5160 if (impl->loading_shortcuts)
5162 for (l = impl->loading_shortcuts; l; l = l->next)
5164 GtkFileSystemHandle *handle =l->data;
5165 gtk_file_system_cancel_operation (handle);
5167 g_slist_free (impl->loading_shortcuts);
5168 impl->loading_shortcuts = NULL;
5171 if (impl->file_list_drag_data_received_handle)
5173 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5174 impl->file_list_drag_data_received_handle = NULL;
5177 if (impl->update_current_folder_handle)
5179 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5180 impl->update_current_folder_handle = NULL;
5183 if (impl->show_and_select_paths_handle)
5185 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5186 impl->show_and_select_paths_handle = NULL;
5189 if (impl->should_respond_get_info_handle)
5191 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5192 impl->should_respond_get_info_handle = NULL;
5195 if (impl->update_from_entry_handle)
5197 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5198 impl->update_from_entry_handle = NULL;
5201 if (impl->shortcuts_activate_iter_handle)
5203 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5204 impl->shortcuts_activate_iter_handle = NULL;
5207 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5209 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5212 /* We override show-all since we have internal widgets that
5213 * shouldn't be shown when you call show_all(), like the filter
5217 gtk_file_chooser_default_show_all (GtkWidget *widget)
5219 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5221 gtk_widget_show (widget);
5223 if (impl->extra_widget)
5224 gtk_widget_show_all (impl->extra_widget);
5227 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5228 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5231 toplevel_set_focus_cb (GtkWindow *window,
5233 GtkFileChooserDefault *impl)
5235 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5238 /* We monitor the focus widget on our toplevel to be able to know which widget
5239 * was last focused at the time our "should_respond" method gets called.
5242 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5243 GtkWidget *previous_toplevel)
5245 GtkFileChooserDefault *impl;
5246 GtkWidget *toplevel;
5248 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5250 if (previous_toplevel)
5252 g_assert (impl->toplevel_set_focus_id != 0);
5253 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5254 impl->toplevel_set_focus_id = 0;
5255 impl->toplevel_last_focus_widget = NULL;
5258 g_assert (impl->toplevel_set_focus_id == 0);
5260 toplevel = gtk_widget_get_toplevel (widget);
5261 if (GTK_IS_WINDOW (toplevel))
5263 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5264 G_CALLBACK (toplevel_set_focus_cb), impl);
5265 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5269 /* Changes the icons wherever it is needed */
5271 change_icon_theme (GtkFileChooserDefault *impl)
5273 GtkSettings *settings;
5276 profile_start ("start", NULL);
5278 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5280 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5281 impl->icon_size = MAX (width, height);
5283 impl->icon_size = FALLBACK_ICON_SIZE;
5285 shortcuts_reload_icons (impl);
5286 gtk_widget_queue_resize (impl->browse_files_tree_view);
5288 profile_end ("end", NULL);
5291 /* Callback used when a GtkSettings value changes */
5293 settings_notify_cb (GObject *object,
5295 GtkFileChooserDefault *impl)
5299 profile_start ("start", NULL);
5301 name = g_param_spec_get_name (pspec);
5303 if (strcmp (name, "gtk-icon-theme-name") == 0
5304 || strcmp (name, "gtk-icon-sizes") == 0)
5305 change_icon_theme (impl);
5307 profile_end ("end", NULL);
5310 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5314 check_icon_theme (GtkFileChooserDefault *impl)
5316 GtkSettings *settings;
5318 profile_start ("start", NULL);
5320 if (impl->settings_signal_id)
5322 profile_end ("end", NULL);
5326 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5328 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5329 impl->settings_signal_id = g_signal_connect (settings, "notify",
5330 G_CALLBACK (settings_notify_cb), impl);
5332 change_icon_theme (impl);
5335 profile_end ("end", NULL);
5339 gtk_file_chooser_default_style_set (GtkWidget *widget,
5340 GtkStyle *previous_style)
5342 GtkFileChooserDefault *impl;
5344 profile_start ("start", NULL);
5346 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5348 profile_msg (" parent class style_set start", NULL);
5349 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5350 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5351 profile_msg (" parent class style_set end", NULL);
5353 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5354 change_icon_theme (impl);
5356 profile_msg (" emit default-size-changed start", NULL);
5357 g_signal_emit_by_name (widget, "default-size-changed");
5358 profile_msg (" emit default-size-changed end", NULL);
5360 profile_end ("end", NULL);
5364 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5365 GdkScreen *previous_screen)
5367 GtkFileChooserDefault *impl;
5369 profile_start ("start", NULL);
5371 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5373 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5374 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5376 remove_settings_signal (impl, previous_screen);
5377 check_icon_theme (impl);
5379 g_signal_emit_by_name (widget, "default-size-changed");
5381 profile_end ("end", NULL);
5385 get_is_file_filtered (GtkFileChooserDefault *impl,
5386 const GtkFilePath *path,
5387 GtkFileInfo *file_info)
5389 GtkFileFilterInfo filter_info;
5390 GtkFileFilterFlags needed;
5393 if (!impl->current_filter)
5396 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5398 needed = gtk_file_filter_get_needed (impl->current_filter);
5400 filter_info.display_name = gtk_file_info_get_display_name (file_info);
5401 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5403 if (needed & GTK_FILE_FILTER_FILENAME)
5405 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5406 if (filter_info.filename)
5407 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5410 filter_info.filename = NULL;
5412 if (needed & GTK_FILE_FILTER_URI)
5414 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5415 if (filter_info.uri)
5416 filter_info.contains |= GTK_FILE_FILTER_URI;
5419 filter_info.uri = NULL;
5421 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5423 if (filter_info.filename)
5424 g_free ((gchar *)filter_info.filename);
5425 if (filter_info.uri)
5426 g_free ((gchar *)filter_info.uri);
5432 settings_load (GtkFileChooserDefault *impl)
5434 GtkFileChooserSettings *settings;
5435 LocationMode location_mode;
5436 gboolean show_hidden;
5438 settings = _gtk_file_chooser_settings_new ();
5440 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5441 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5443 g_object_unref (settings);
5445 location_mode_set (impl, location_mode, TRUE);
5446 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5450 settings_save (GtkFileChooserDefault *impl)
5452 GtkFileChooserSettings *settings;
5454 settings = _gtk_file_chooser_settings_new ();
5456 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5457 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5460 _gtk_file_chooser_settings_save (settings, NULL);
5462 g_object_unref (settings);
5465 /* GtkWidget::map method */
5467 gtk_file_chooser_default_map (GtkWidget *widget)
5469 GtkFileChooserDefault *impl;
5470 char *current_working_dir;
5472 profile_start ("start", NULL);
5474 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5476 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5478 switch (impl->reload_state)
5481 /* The user didn't explicitly give us a folder to display, so we'll use the cwd */
5482 current_working_dir = g_get_current_dir ();
5483 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
5484 g_free (current_working_dir);
5487 case RELOAD_HAS_FOLDER:
5488 /* Nothing; we are already loading or loaded, so we don't need to reload */
5491 case RELOAD_WAS_UNMAPPED:
5492 /* Just reload the current folder; else continue the pending load. */
5493 if (impl->current_folder)
5495 pending_select_paths_store_selection (impl);
5496 change_folder_and_display_error (impl, impl->current_folder);
5501 g_assert_not_reached ();
5504 bookmarks_changed_cb (impl->file_system, impl);
5506 settings_load (impl);
5508 profile_end ("end", NULL);
5511 /* GtkWidget::unmap method */
5513 gtk_file_chooser_default_unmap (GtkWidget *widget)
5515 GtkFileChooserDefault *impl;
5517 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5519 settings_save (impl);
5521 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5523 impl->reload_state = RELOAD_WAS_UNMAPPED;
5527 list_model_filter_func (GtkFileSystemModel *model,
5529 const GtkFileInfo *file_info,
5532 GtkFileChooserDefault *impl = user_data;
5534 if (!impl->current_filter)
5537 if (gtk_file_info_get_is_folder (file_info))
5540 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5544 install_list_model_filter (GtkFileChooserDefault *impl)
5546 GtkFileSystemModelFilter filter;
5549 g_assert (impl->browse_files_model != NULL);
5551 if (impl->current_filter)
5553 filter = list_model_filter_func;
5562 _gtk_file_system_model_set_filter (impl->browse_files_model,
5567 #define COMPARE_DIRECTORIES \
5568 GtkFileChooserDefault *impl = user_data; \
5569 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
5570 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
5571 gboolean dir_a, dir_b; \
5574 dir_a = gtk_file_info_get_is_folder (info_a); \
5576 return impl->list_sort_ascending ? -1 : 1; \
5579 dir_b = gtk_file_info_get_is_folder (info_b); \
5581 return impl->list_sort_ascending ? 1 : -1; \
5583 if (dir_a != dir_b) \
5584 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5586 /* Sort callback for the filename column */
5588 name_sort_func (GtkTreeModel *model,
5593 COMPARE_DIRECTORIES;
5595 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
5598 /* Sort callback for the size column */
5600 size_sort_func (GtkTreeModel *model,
5605 COMPARE_DIRECTORIES;
5608 gint64 size_a = gtk_file_info_get_size (info_a);
5609 gint64 size_b = gtk_file_info_get_size (info_b);
5611 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
5615 /* Sort callback for the mtime column */
5617 mtime_sort_func (GtkTreeModel *model,
5622 COMPARE_DIRECTORIES;
5625 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
5626 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
5628 return ta > tb ? -1 : (ta == tb ? 0 : 1);
5632 /* Callback used when the sort column changes. We cache the sort order for use
5633 * in name_sort_func().
5636 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5637 GtkFileChooserDefault *impl)
5639 GtkSortType sort_type;
5641 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
5642 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5646 set_busy_cursor (GtkFileChooserDefault *impl,
5649 GtkWindow *toplevel;
5650 GdkDisplay *display;
5653 toplevel = get_toplevel (GTK_WIDGET (impl));
5654 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
5657 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
5660 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5664 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
5665 gdk_display_flush (display);
5668 gdk_cursor_unref (cursor);
5671 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5673 load_set_model (GtkFileChooserDefault *impl)
5675 profile_start ("start", NULL);
5677 g_assert (impl->browse_files_model != NULL);
5678 g_assert (impl->sort_model == NULL);
5680 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
5681 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
5682 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
5683 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
5684 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
5685 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
5686 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
5687 impl->list_sort_ascending = TRUE;
5688 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
5690 g_signal_connect (impl->sort_model, "sort_column_changed",
5691 G_CALLBACK (list_sort_column_changed_cb), impl);
5693 profile_msg (" gtk_tree_view_set_model start", NULL);
5694 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5695 GTK_TREE_MODEL (impl->sort_model));
5696 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5697 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5698 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
5699 profile_msg (" gtk_tree_view_set_model end", NULL);
5701 profile_end ("end", NULL);
5704 /* Timeout callback used when the loading timer expires */
5706 load_timeout_cb (gpointer data)
5708 GtkFileChooserDefault *impl;
5710 profile_start ("start", NULL);
5712 GDK_THREADS_ENTER ();
5714 impl = GTK_FILE_CHOOSER_DEFAULT (data);
5715 g_assert (impl->load_state == LOAD_PRELOAD);
5716 g_assert (impl->load_timeout_id != 0);
5717 g_assert (impl->browse_files_model != NULL);
5719 impl->load_timeout_id = 0;
5720 impl->load_state = LOAD_LOADING;
5722 load_set_model (impl);
5724 GDK_THREADS_LEAVE ();
5726 profile_end ("end", NULL);
5731 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
5733 load_setup_timer (GtkFileChooserDefault *impl)
5735 g_assert (impl->load_timeout_id == 0);
5736 g_assert (impl->load_state != LOAD_PRELOAD);
5738 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
5739 impl->load_state = LOAD_PRELOAD;
5742 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5744 load_remove_timer (GtkFileChooserDefault *impl)
5746 if (impl->load_timeout_id != 0)
5748 g_assert (impl->load_state == LOAD_PRELOAD);
5750 g_source_remove (impl->load_timeout_id);
5751 impl->load_timeout_id = 0;
5752 impl->load_state = LOAD_EMPTY;
5755 g_assert (impl->load_state == LOAD_EMPTY ||
5756 impl->load_state == LOAD_LOADING ||
5757 impl->load_state == LOAD_FINISHED);
5760 /* Selects the first row in the file list */
5762 browse_files_select_first_row (GtkFileChooserDefault *impl)
5766 if (!impl->sort_model)
5769 path = gtk_tree_path_new_from_indices (0, -1);
5770 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5771 gtk_tree_path_free (path);
5774 struct center_selected_row_closure {
5775 GtkFileChooserDefault *impl;
5776 gboolean already_centered;
5779 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
5780 * selected row in the tree view.
5783 center_selected_row_foreach_cb (GtkTreeModel *model,
5788 struct center_selected_row_closure *closure;
5791 if (closure->already_centered)
5794 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5795 closure->already_centered = TRUE;
5798 /* Centers the selected row in the tree view */
5800 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5802 struct center_selected_row_closure closure;
5803 GtkTreeSelection *selection;
5805 closure.impl = impl;
5806 closure.already_centered = FALSE;
5808 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5809 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5812 struct ShowAndSelectPathsData
5814 GtkFileChooserDefault *impl;
5819 show_and_select_paths_finished_loading (GtkFileFolder *folder,
5822 gboolean have_hidden;
5823 gboolean have_filtered;
5825 struct ShowAndSelectPathsData *data = user_data;
5827 have_hidden = FALSE;
5828 have_filtered = FALSE;
5830 for (l = data->paths; l; l = l->next)
5832 const GtkFilePath *path;
5838 info = gtk_file_folder_get_info (folder, path, NULL);
5842 have_hidden = gtk_file_info_get_is_hidden (info);
5845 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
5847 gtk_file_info_free (info);
5849 if (have_hidden && have_filtered)
5850 break; /* we now have all the information we need */
5854 g_signal_handlers_disconnect_by_func (folder,
5855 show_and_select_paths_finished_loading,
5858 g_object_unref (folder);
5861 g_object_set (data->impl, "show-hidden", TRUE, NULL);
5864 set_current_filter (data->impl, NULL);
5866 for (l = data->paths; l; l = l->next)
5868 const GtkFilePath *path;
5871 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
5872 select_func, data->impl);
5875 browse_files_center_selected_row (data->impl);
5877 g_object_unref (data->impl);
5878 gtk_file_paths_free (data->paths);
5883 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
5884 GtkFileFolder *folder,
5885 const GError *error,
5888 gboolean cancelled = handle->cancelled;
5889 struct ShowAndSelectPathsData *data = user_data;
5891 if (data->impl->show_and_select_paths_handle != handle)
5894 data->impl->show_and_select_paths_handle = NULL;
5896 if (cancelled || error)
5899 g_object_unref (handle);
5901 if (gtk_file_folder_is_finished_loading (folder))
5902 show_and_select_paths_finished_loading (folder, user_data);
5904 g_signal_connect (folder, "finished-loading",
5905 G_CALLBACK (show_and_select_paths_finished_loading),
5911 g_object_unref (data->impl);
5912 gtk_file_paths_free (data->paths);
5915 g_object_unref (handle);
5919 show_and_select_paths (GtkFileChooserDefault *impl,
5920 const GtkFilePath *parent_path,
5924 struct ShowAndSelectPathsData *info;
5926 profile_start ("start", NULL);
5930 profile_end ("end", NULL);
5934 info = g_new (struct ShowAndSelectPathsData, 1);
5935 info->impl = g_object_ref (impl);
5936 info->paths = gtk_file_paths_copy (paths);
5938 if (impl->show_and_select_paths_handle)
5939 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5941 impl->show_and_select_paths_handle =
5942 gtk_file_system_get_folder (impl->file_system, parent_path,
5943 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
5944 show_and_select_paths_get_folder_cb, info);
5946 profile_end ("end", NULL);
5950 /* Processes the pending operation when a folder is finished loading */
5952 pending_select_paths_process (GtkFileChooserDefault *impl)
5954 g_assert (impl->load_state == LOAD_FINISHED);
5955 g_assert (impl->browse_files_model != NULL);
5956 g_assert (impl->sort_model != NULL);
5958 if (impl->pending_select_paths)
5961 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
5962 pending_select_paths_free (impl);
5963 browse_files_center_selected_row (impl);
5967 /* We only select the first row if the chooser is actually mapped ---
5968 * selecting the first row is to help the user when he is interacting with
5969 * the chooser, but sometimes a chooser works not on behalf of the user,
5970 * but rather on behalf of something else like GtkFileChooserButton. In
5971 * that case, the chooser's selection should be what the caller expects,
5972 * as the user can't see that something else got selected. See bug #165264.
5974 * Also, we don't select the first file if we are not in OPEN mode. Doing
5975 * so would change the contents of the filename entry for SAVE or
5976 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5977 * select a *different* folder from the one into which the user just
5980 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5981 browse_files_select_first_row (impl);
5984 g_assert (impl->pending_select_paths == NULL);
5987 /* Callback used when the file system model finishes loading */
5989 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
5990 GtkFileChooserDefault *impl)
5992 profile_start ("start", NULL);
5994 if (impl->load_state == LOAD_PRELOAD)
5996 load_remove_timer (impl);
5997 load_set_model (impl);
5999 else if (impl->load_state == LOAD_LOADING)
6005 /* We can't g_assert_not_reached(), as something other than us may have
6006 * initiated a folder reload. See #165556.
6008 profile_end ("end", NULL);
6012 g_assert (impl->load_timeout_id == 0);
6014 impl->load_state = LOAD_FINISHED;
6016 pending_select_paths_process (impl);
6017 set_busy_cursor (impl, FALSE);
6018 #ifdef PROFILE_FILE_CHOOSER
6019 access ("MARK: *** FINISHED LOADING", F_OK);
6022 profile_end ("end", NULL);
6025 /* Gets rid of the old list model and creates a new one for the current folder */
6027 set_list_model (GtkFileChooserDefault *impl,
6030 g_assert (impl->current_folder != NULL);
6032 profile_start ("start", NULL);
6034 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6036 if (impl->browse_files_model)
6038 g_object_unref (impl->browse_files_model);
6039 impl->browse_files_model = NULL;
6042 if (impl->sort_model)
6044 g_object_unref (impl->sort_model);
6045 impl->sort_model = NULL;
6048 set_busy_cursor (impl, TRUE);
6049 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6051 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6052 impl->current_folder, 0,
6055 if (!impl->browse_files_model)
6057 set_busy_cursor (impl, FALSE);
6058 profile_end ("end", NULL);
6062 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6064 g_signal_connect (impl->browse_files_model, "finished-loading",
6065 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6067 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6069 install_list_model_filter (impl);
6071 profile_end ("end", NULL);
6076 struct update_chooser_entry_selected_foreach_closure {
6078 GtkTreeIter first_selected_iter;
6082 compare_utf8_filenames (const gchar *a,
6085 gchar *a_folded, *b_folded;
6088 a_folded = g_utf8_strdown (a, -1);
6089 b_folded = g_utf8_strdown (b, -1);
6091 retval = strcmp (a_folded, b_folded);
6100 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6105 struct update_chooser_entry_selected_foreach_closure *closure;
6108 closure->num_selected++;
6110 if (closure->num_selected == 1)
6111 closure->first_selected_iter = *iter;
6115 update_chooser_entry (GtkFileChooserDefault *impl)
6117 GtkTreeSelection *selection;
6118 struct update_chooser_entry_selected_foreach_closure closure;
6119 const char *file_part;
6121 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6122 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6123 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6124 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6125 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6128 g_assert (impl->location_entry != NULL);
6130 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6131 closure.num_selected = 0;
6132 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6136 if (closure.num_selected == 0)
6138 goto maybe_clear_entry;
6140 else if (closure.num_selected == 1)
6142 GtkTreeIter child_iter;
6143 const GtkFileInfo *info;
6144 gboolean change_entry;
6146 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6148 &closure.first_selected_iter);
6150 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6152 g_free (impl->browse_files_last_selected_name);
6153 impl->browse_files_last_selected_name = g_strdup (gtk_file_info_get_display_name (info));
6155 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6156 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6157 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6159 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6162 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6168 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6169 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6171 /* Multiple selection, so just clear the entry. */
6173 g_free (impl->browse_files_last_selected_name);
6174 impl->browse_files_last_selected_name = NULL;
6176 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6182 if (impl->browse_files_last_selected_name)
6184 const char *entry_text;
6186 gboolean clear_entry;
6188 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6189 len = strlen (entry_text);
6192 /* The file chooser entry may have appended a "/" to its text. So
6193 * take it out, and compare the result to the old selection.
6195 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6199 tmp = g_strndup (entry_text, len - 1);
6200 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6204 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6207 clear_entry = FALSE;
6210 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6215 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6216 const GtkFilePath *path,
6219 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
6223 struct UpdateCurrentFolderData
6225 GtkFileChooserDefault *impl;
6227 gboolean keep_trail;
6228 GtkFilePath *original_path;
6229 GError *original_error;
6233 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6234 const GtkFileInfo *info,
6235 const GError *error,
6238 gboolean cancelled = handle->cancelled;
6239 struct UpdateCurrentFolderData *data = user_data;
6240 GtkFileChooserDefault *impl = data->impl;
6242 if (handle != impl->update_current_folder_handle)
6245 impl->update_current_folder_handle = NULL;
6246 impl->reload_state = RELOAD_EMPTY;
6248 set_busy_cursor (impl, FALSE);
6255 GtkFilePath *parent_path;
6257 if (!data->original_path)
6259 data->original_path = gtk_file_path_copy (data->path);
6260 data->original_error = g_error_copy (error);
6263 /* get parent path and try to change the folder to that */
6264 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6265 parent_path != NULL)
6267 gtk_file_path_free (data->path);
6268 data->path = parent_path;
6270 g_object_unref (handle);
6272 /* restart the update current folder operation */
6273 impl->reload_state = RELOAD_HAS_FOLDER;
6275 impl->update_current_folder_handle =
6276 gtk_file_system_get_info (impl->file_system, data->path,
6277 GTK_FILE_INFO_IS_FOLDER,
6278 update_current_folder_get_info_cb,
6281 set_busy_cursor (impl, TRUE);
6287 /* error and bail out */
6288 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6290 gtk_file_path_free (data->original_path);
6296 if (data->original_path)
6298 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6300 gtk_file_path_free (data->original_path);
6303 if (!gtk_file_info_get_is_folder (info))
6306 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6309 if (impl->current_folder != data->path)
6311 if (impl->current_folder)
6312 gtk_file_path_free (impl->current_folder);
6314 impl->current_folder = gtk_file_path_copy (data->path);
6316 impl->reload_state = RELOAD_HAS_FOLDER;
6319 /* Update the widgets that may trigger a folder change themselves. */
6321 if (!impl->changing_folder)
6323 impl->changing_folder = TRUE;
6325 shortcuts_update_current_folder (impl);
6327 impl->changing_folder = FALSE;
6330 /* Set the folder on the save entry */
6332 if (impl->location_entry)
6333 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6334 impl->current_folder);
6336 /* Create a new list model. This is slightly evil; we store the result value
6337 * but perform more actions rather than returning immediately even if it
6338 * generates an error.
6340 set_list_model (impl, NULL);
6342 /* Refresh controls */
6344 shortcuts_find_current_folder (impl);
6346 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6348 check_preview_change (impl);
6349 bookmarks_check_add_sensitivity (impl);
6351 g_signal_emit_by_name (impl, "selection-changed", 0);
6354 gtk_file_path_free (data->path);
6357 g_object_unref (handle);
6361 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6362 const GtkFilePath *path,
6363 gboolean keep_trail,
6366 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6367 struct UpdateCurrentFolderData *data;
6369 profile_start ("start", (char *) path);
6371 g_assert (path != NULL);
6373 if (impl->local_only &&
6374 !gtk_file_system_path_is_local (impl->file_system, path))
6377 GTK_FILE_CHOOSER_ERROR,
6378 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6379 _("Cannot change to folder because it is not local"));
6381 profile_end ("end - not local", (char *) path);
6385 if (impl->update_current_folder_handle)
6386 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6388 /* Test validity of path here. */
6389 data = g_new0 (struct UpdateCurrentFolderData, 1);
6391 data->path = gtk_file_path_copy (path);
6392 data->keep_trail = keep_trail;
6394 impl->reload_state = RELOAD_HAS_FOLDER;
6396 impl->update_current_folder_handle =
6397 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6398 update_current_folder_get_info_cb,
6401 set_busy_cursor (impl, TRUE);
6403 profile_end ("end", NULL);
6407 static GtkFilePath *
6408 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6410 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6412 if (impl->reload_state == RELOAD_EMPTY)
6414 char *current_working_dir;
6417 /* We are unmapped, or we had an error while loading the last folder. We'll return
6418 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6419 * explicitly calls set_current_folder() on us.
6421 current_working_dir = g_get_current_dir ();
6422 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6423 g_free (current_working_dir);
6427 return gtk_file_path_copy (impl->current_folder);
6431 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6434 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6436 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6437 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6439 pending_select_paths_free (impl);
6440 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6444 select_func (GtkFileSystemModel *model,
6449 GtkFileChooserDefault *impl = user_data;
6450 GtkTreeSelection *selection;
6451 GtkTreeIter sorted_iter;
6453 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6455 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6456 gtk_tree_selection_select_iter (selection, &sorted_iter);
6460 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
6461 const GtkFilePath *path,
6464 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6465 GtkFilePath *parent_path;
6468 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6472 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6474 if (impl->load_state == LOAD_EMPTY)
6478 g_assert (impl->current_folder != NULL);
6480 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6483 if (same_path && impl->load_state == LOAD_FINISHED)
6488 paths.data = (gpointer) path;
6491 result = show_and_select_paths (impl, parent_path, &paths, error);
6492 gtk_file_path_free (parent_path);
6496 pending_select_paths_add (impl, path);
6502 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6503 gtk_file_path_free (parent_path);
6507 gtk_file_path_free (parent_path);
6512 unselect_func (GtkFileSystemModel *model,
6517 GtkFileChooserDefault *impl = user_data;
6518 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
6519 GtkTreePath *sorted_path;
6521 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
6523 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
6525 gtk_tree_path_free (sorted_path);
6529 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
6530 const GtkFilePath *path)
6532 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6534 if (!impl->browse_files_model)
6537 _gtk_file_system_model_path_do (impl->browse_files_model, path,
6538 unselect_func, impl);
6542 maybe_select (GtkTreeModel *model,
6547 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
6548 GtkTreeSelection *selection;
6549 const GtkFileInfo *info;
6552 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6554 info = get_list_file_info (impl, iter);
6555 is_folder = gtk_file_info_get_is_folder (info);
6557 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
6558 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
6559 gtk_tree_selection_select_iter (selection, iter);
6561 gtk_tree_selection_unselect_iter (selection, iter);
6567 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
6569 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6570 if (impl->select_multiple)
6571 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
6572 maybe_select, impl);
6576 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
6578 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6579 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6581 gtk_tree_selection_unselect_all (selection);
6582 pending_select_paths_free (impl);
6585 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
6587 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
6589 * is_empty_ret - whether the file entry is totally empty
6591 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
6592 * the path will be "$cwd/foobar")
6595 check_save_entry (GtkFileChooserDefault *impl,
6596 GtkFilePath **path_ret,
6597 gboolean *is_well_formed_ret,
6598 gboolean *is_empty_ret,
6599 gboolean *is_file_part_empty_ret,
6600 gboolean *is_folder)
6602 GtkFileChooserEntry *chooser_entry;
6603 const GtkFilePath *current_folder;
6604 const char *file_part;
6608 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6609 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6610 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6611 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6612 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
6614 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
6616 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
6619 *is_well_formed_ret = TRUE;
6620 *is_empty_ret = TRUE;
6621 *is_file_part_empty_ret = TRUE;
6627 *is_empty_ret = FALSE;
6629 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6630 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6632 if (!file_part || file_part[0] == '\0')
6634 *path_ret = gtk_file_path_copy (current_folder);
6635 *is_well_formed_ret = TRUE;
6636 *is_file_part_empty_ret = TRUE;
6642 *is_file_part_empty_ret = FALSE;
6645 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
6649 error_building_filename_dialog (impl, current_folder, file_part, error);
6651 *is_well_formed_ret = FALSE;
6658 *is_well_formed_ret = TRUE;
6659 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
6662 struct get_paths_closure {
6663 GtkFileChooserDefault *impl;
6665 GtkFilePath *path_from_entry;
6669 get_paths_foreach (GtkTreeModel *model,
6674 struct get_paths_closure *info;
6675 const GtkFilePath *file_path;
6676 GtkFileSystemModel *fs_model;
6677 GtkTreeIter sel_iter;
6680 fs_model = info->impl->browse_files_model;
6681 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
6683 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
6685 return; /* We are on the editable row */
6687 if (!info->path_from_entry
6688 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
6689 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
6693 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
6695 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6696 struct get_paths_closure info;
6697 GtkWindow *toplevel;
6698 GtkWidget *current_focus;
6702 info.path_from_entry = NULL;
6704 toplevel = get_toplevel (GTK_WIDGET (impl));
6706 current_focus = gtk_window_get_focus (toplevel);
6708 current_focus = NULL;
6710 if (current_focus == impl->browse_files_tree_view)
6712 GtkTreeSelection *selection;
6716 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6717 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
6719 /* If there is no selection in the file list, we probably have this situation:
6721 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
6722 * 2. He then double-clicked on a folder ("bar") in the file list
6724 * So we want the selection to be "bar/foo.txt". Jump to the case for the
6725 * filename entry to see if that is the case.
6727 if (info.result == NULL && impl->location_entry)
6730 else if (impl->location_entry && current_focus == impl->location_entry)
6732 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
6736 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
6741 if (!is_well_formed)
6744 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6746 gtk_file_path_free (info.path_from_entry);
6750 g_assert (info.path_from_entry != NULL);
6751 info.result = g_slist_prepend (info.result, info.path_from_entry);
6753 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6755 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
6759 /* The focus is on a dialog's action area button or something else */
6760 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6761 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6769 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
6770 * fall back to the current directory */
6771 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
6772 info.result == NULL)
6774 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
6777 return g_slist_reverse (info.result);
6780 static GtkFilePath *
6781 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
6783 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6785 if (impl->preview_path)
6786 return gtk_file_path_copy (impl->preview_path);
6791 static GtkFileSystem *
6792 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
6794 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6796 return impl->file_system;
6799 /* Shows or hides the filter widgets */
6801 show_filters (GtkFileChooserDefault *impl,
6805 gtk_widget_show (impl->filter_combo_hbox);
6807 gtk_widget_hide (impl->filter_combo_hbox);
6811 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
6812 GtkFileFilter *filter)
6814 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6817 if (g_slist_find (impl->filters, filter))
6819 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
6823 g_object_ref_sink (filter);
6824 impl->filters = g_slist_append (impl->filters, filter);
6826 name = gtk_file_filter_get_name (filter);
6828 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
6830 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
6832 if (!g_slist_find (impl->filters, impl->current_filter))
6833 set_current_filter (impl, filter);
6835 show_filters (impl, TRUE);
6839 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
6840 GtkFileFilter *filter)
6842 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6843 GtkTreeModel *model;
6847 filter_index = g_slist_index (impl->filters, filter);
6849 if (filter_index < 0)
6851 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
6855 impl->filters = g_slist_remove (impl->filters, filter);
6857 if (filter == impl->current_filter)
6860 set_current_filter (impl, impl->filters->data);
6862 set_current_filter (impl, NULL);
6865 /* Remove row from the combo box */
6866 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
6867 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
6868 g_assert_not_reached ();
6870 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
6872 g_object_unref (filter);
6875 show_filters (impl, FALSE);
6879 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
6881 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6883 return g_slist_copy (impl->filters);
6886 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
6888 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
6891 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
6894 struct AddShortcutData
6896 GtkFileChooserDefault *impl;
6901 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
6902 const GtkFileInfo *info,
6903 const GError *error,
6907 gboolean cancelled = handle->cancelled;
6908 struct AddShortcutData *data = user_data;
6910 if (!g_slist_find (data->impl->loading_shortcuts, handle))
6913 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
6915 if (cancelled || error || !gtk_file_info_get_is_folder (info))
6918 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
6920 shortcuts_insert_path (data->impl, pos, FALSE, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
6923 g_object_unref (data->impl);
6924 gtk_file_path_free (data->path);
6927 g_object_unref (handle);
6931 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
6932 const GtkFilePath *path,
6935 GtkFileSystemHandle *handle;
6936 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6937 struct AddShortcutData *data;
6941 /* Avoid adding duplicates */
6942 pos = shortcut_find_position (impl, path);
6943 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
6947 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6948 /* translators, "Shortcut" means "Bookmark" here */
6950 GTK_FILE_CHOOSER_ERROR,
6951 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6952 _("Shortcut %s already exists"),
6959 for (l = impl->loading_shortcuts; l; l = l->next)
6961 GtkFileSystemHandle *h = l->data;
6964 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
6965 if (p && !gtk_file_path_compare (path, p))
6969 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6971 GTK_FILE_CHOOSER_ERROR,
6972 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6973 _("Shortcut %s already exists"),
6981 data = g_new0 (struct AddShortcutData, 1);
6982 data->impl = g_object_ref (impl);
6983 data->path = gtk_file_path_copy (path);
6985 handle = gtk_file_system_get_info (impl->file_system, path,
6986 GTK_FILE_INFO_IS_FOLDER,
6987 add_shortcut_get_info_cb, data);
6992 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
6993 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
6999 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7000 const GtkFilePath *path,
7003 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7010 for (l = impl->loading_shortcuts; l; l = l->next)
7012 GtkFileSystemHandle *h = l->data;
7015 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7016 if (p && !gtk_file_path_compare (path, p))
7018 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7019 gtk_file_system_cancel_operation (h);
7024 if (impl->num_shortcuts == 0)
7027 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7028 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7029 g_assert_not_reached ();
7031 for (i = 0; i < impl->num_shortcuts; i++)
7035 GtkFilePath *shortcut;
7037 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7038 SHORTCUTS_COL_DATA, &col_data,
7039 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7041 g_assert (col_data != NULL);
7042 g_assert (!is_volume);
7044 shortcut = col_data;
7045 if (gtk_file_path_compare (shortcut, path) == 0)
7047 shortcuts_remove_rows (impl, pos + i, 1);
7048 impl->num_shortcuts--;
7052 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7053 g_assert_not_reached ();
7058 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7059 /* translators, "Shortcut" means "Bookmark" here */
7061 GTK_FILE_CHOOSER_ERROR,
7062 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7063 _("Shortcut %s does not exist"),
7071 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7073 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7079 if (impl->num_shortcuts == 0)
7082 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7083 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7084 g_assert_not_reached ();
7088 for (i = 0; i < impl->num_shortcuts; i++)
7092 GtkFilePath *shortcut;
7094 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7095 SHORTCUTS_COL_DATA, &col_data,
7096 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7098 g_assert (col_data != NULL);
7099 g_assert (!is_volume);
7101 shortcut = col_data;
7102 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7104 if (i != impl->num_shortcuts - 1)
7106 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7107 g_assert_not_reached ();
7111 return g_slist_reverse (list);
7114 /* Guesses a size based upon font sizes */
7116 find_good_size_from_style (GtkWidget *widget,
7120 GtkFileChooserDefault *impl;
7121 gint default_width, default_height;
7124 GtkRequisition preview_req;
7126 g_assert (widget->style != NULL);
7127 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7129 font_size = pango_font_description_get_size (widget->style->font_desc);
7130 font_size = PANGO_PIXELS (font_size);
7132 default_width = font_size * NUM_CHARS;
7133 default_height = font_size * NUM_LINES;
7135 /* Use at least the requisition size not including the preview widget */
7136 gtk_widget_size_request (widget, &req);
7138 if (impl->preview_widget_active && impl->preview_widget)
7139 gtk_widget_size_request (impl->preview_box, &preview_req);
7141 preview_req.width = 0;
7143 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
7144 default_height = MAX (default_height, req.height);
7146 *width = default_width;
7147 *height = default_height;
7151 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7152 gint *default_width,
7153 gint *default_height)
7155 GtkFileChooserDefault *impl;
7157 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7159 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7161 if (impl->preview_widget_active && impl->preview_widget)
7162 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
7166 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
7167 gboolean *resize_horizontally,
7168 gboolean *resize_vertically)
7170 GtkFileChooserDefault *impl;
7172 g_return_if_fail (resize_horizontally != NULL);
7173 g_return_if_fail (resize_vertically != NULL);
7175 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7177 *resize_horizontally = TRUE;
7178 *resize_vertically = TRUE;
7180 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7181 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7183 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7185 *resize_horizontally = FALSE;
7186 *resize_vertically = FALSE;
7191 struct switch_folder_closure {
7192 GtkFileChooserDefault *impl;
7193 const GtkFilePath *path;
7197 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7199 switch_folder_foreach_cb (GtkTreeModel *model,
7204 struct switch_folder_closure *closure;
7205 GtkTreeIter child_iter;
7209 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7211 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7212 closure->num_selected++;
7215 /* Changes to the selected folder in the list view */
7217 switch_to_selected_folder (GtkFileChooserDefault *impl)
7219 GtkTreeSelection *selection;
7220 struct switch_folder_closure closure;
7222 /* We do this with foreach() rather than get_selected() as we may be in
7223 * multiple selection mode
7226 closure.impl = impl;
7227 closure.path = NULL;
7228 closure.num_selected = 0;
7230 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7231 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7233 g_assert (closure.path && closure.num_selected == 1);
7235 change_folder_and_display_error (impl, closure.path);
7238 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7241 static const GtkFileInfo *
7242 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7243 gboolean *had_selection)
7245 GtkTreeSelection *selection;
7246 GtkTreeIter iter, child_iter;
7247 const GtkFileInfo *info;
7249 g_assert (!impl->select_multiple);
7250 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7251 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7253 *had_selection = FALSE;
7257 *had_selection = TRUE;
7259 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7263 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7267 /* Gets the display name of the selected file in the file list; assumes single
7268 * selection mode and that something is selected.
7270 static const gchar *
7271 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7273 const GtkFileInfo *info;
7274 gboolean had_selection;
7276 info = get_selected_file_info_from_file_list (impl, &had_selection);
7277 g_assert (had_selection);
7278 g_assert (info != NULL);
7280 return gtk_file_info_get_display_name (info);
7284 add_custom_button_to_dialog (GtkDialog *dialog,
7285 const gchar *mnemonic_label,
7286 const gchar *stock_id,
7291 button = gtk_button_new_with_mnemonic (mnemonic_label);
7292 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7293 gtk_button_set_image (GTK_BUTTON (button),
7294 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7295 gtk_widget_show (button);
7297 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7300 /* Presents an overwrite confirmation dialog; returns whether we should accept
7304 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7305 const gchar *file_part,
7306 const gchar *folder_display_name)
7308 GtkWindow *toplevel;
7312 toplevel = get_toplevel (GTK_WIDGET (impl));
7314 dialog = gtk_message_dialog_new (toplevel,
7315 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7316 GTK_MESSAGE_QUESTION,
7318 _("A file named \"%s\" already exists. Do you want to replace it?"),
7320 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7321 _("The file already exists in \"%s\". Replacing it will "
7322 "overwrite its contents."),
7323 folder_display_name);
7325 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7326 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7327 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7329 if (toplevel->group)
7330 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7332 response = gtk_dialog_run (GTK_DIALOG (dialog));
7334 gtk_widget_destroy (dialog);
7336 return (response == GTK_RESPONSE_ACCEPT);
7339 struct GetDisplayNameData
7341 GtkFileChooserDefault *impl;
7346 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7347 const GtkFileInfo *info,
7348 const GError *error,
7351 gboolean cancelled = handle->cancelled;
7352 gboolean should_respond = FALSE;
7353 struct GetDisplayNameData *data = user_data;
7355 if (handle != data->impl->should_respond_get_info_handle)
7358 data->impl->should_respond_get_info_handle = NULL;
7364 /* Huh? Did the folder disappear? Let the caller deal with it */
7365 should_respond = TRUE;
7367 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7369 set_busy_cursor (data->impl, FALSE);
7371 g_signal_emit_by_name (data->impl, "response-requested");
7374 g_object_unref (data->impl);
7375 g_free (data->file_part);
7378 g_object_unref (handle);
7381 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7382 * should respond. Can get the file part from the file list or the save entry.
7385 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7386 const gchar *file_part,
7387 const GtkFilePath *parent_path)
7389 GtkFileChooserConfirmation conf;
7391 if (!impl->do_overwrite_confirmation)
7394 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7396 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7400 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7402 struct GetDisplayNameData *data;
7404 g_assert (file_part != NULL);
7406 data = g_new0 (struct GetDisplayNameData, 1);
7407 data->impl = g_object_ref (impl);
7408 data->file_part = g_strdup (file_part);
7410 if (impl->should_respond_get_info_handle)
7411 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7413 impl->should_respond_get_info_handle =
7414 gtk_file_system_get_info (impl->file_system, parent_path,
7415 GTK_FILE_INFO_DISPLAY_NAME,
7416 confirmation_confirm_get_info_cb,
7418 set_busy_cursor (data->impl, TRUE);
7422 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7425 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7429 g_assert_not_reached ();
7435 action_create_folder_cb (GtkFileSystemHandle *handle,
7436 const GtkFilePath *path,
7437 const GError *error,
7440 gboolean cancelled = handle->cancelled;
7441 GtkFileChooserDefault *impl = user_data;
7443 if (!g_slist_find (impl->pending_handles, handle))
7446 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
7448 set_busy_cursor (impl, FALSE);
7454 error_creating_folder_dialog (impl, path, g_error_copy (error));
7456 g_signal_emit_by_name (impl, "response-requested");
7459 g_object_unref (impl);
7460 g_object_unref (handle);
7463 struct FileExistsData
7465 GtkFileChooserDefault *impl;
7466 gboolean file_exists_and_is_not_folder;
7467 GtkFilePath *parent_path;
7472 save_entry_get_info_cb (GtkFileSystemHandle *handle,
7473 const GtkFileInfo *info,
7474 const GError *error,
7477 gboolean parent_is_folder;
7478 gboolean cancelled = handle->cancelled;
7479 struct FileExistsData *data = user_data;
7481 if (handle != data->impl->should_respond_get_info_handle)
7484 data->impl->should_respond_get_info_handle = NULL;
7486 set_busy_cursor (data->impl, FALSE);
7492 parent_is_folder = FALSE;
7494 parent_is_folder = gtk_file_info_get_is_folder (info);
7496 if (parent_is_folder)
7498 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7500 if (data->file_exists_and_is_not_folder)
7503 const char *file_part;
7505 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
7506 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
7509 g_signal_emit_by_name (data->impl, "response-requested");
7512 g_signal_emit_by_name (data->impl, "response-requested");
7514 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
7516 GtkFileSystemHandle *handle;
7518 g_object_ref (data->impl);
7519 handle = gtk_file_system_create_folder (data->impl->file_system,
7521 action_create_folder_cb,
7523 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
7524 set_busy_cursor (data->impl, TRUE);
7529 /* This will display an error, which is what we want */
7530 change_folder_and_display_error (data->impl, data->parent_path);
7534 g_object_unref (data->impl);
7535 gtk_file_path_free (data->path);
7536 gtk_file_path_free (data->parent_path);
7539 g_object_unref (handle);
7543 file_exists_get_info_cb (GtkFileSystemHandle *handle,
7544 const GtkFileInfo *info,
7545 const GError *error,
7548 gboolean data_ownership_taken = FALSE;
7549 gboolean cancelled = handle->cancelled;
7550 gboolean file_exists_and_is_not_folder;
7551 struct FileExistsData *data = user_data;
7553 if (handle != data->impl->file_exists_get_info_handle)
7556 data->impl->file_exists_get_info_handle = NULL;
7558 set_busy_cursor (data->impl, FALSE);
7563 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
7565 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
7566 /* user typed a filename; we are done */
7567 g_signal_emit_by_name (data->impl, "response-requested");
7568 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7569 && file_exists_and_is_not_folder)
7571 /* Oops, the user typed the name of an existing path which is not
7574 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
7575 g_error_copy (error));
7579 /* check that everything up to the last component exists */
7581 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
7582 data_ownership_taken = TRUE;
7584 if (data->impl->should_respond_get_info_handle)
7585 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
7587 data->impl->should_respond_get_info_handle =
7588 gtk_file_system_get_info (data->impl->file_system,
7590 GTK_FILE_INFO_IS_FOLDER,
7591 save_entry_get_info_cb,
7593 set_busy_cursor (data->impl, TRUE);
7597 if (!data_ownership_taken)
7599 g_object_unref (data->impl);
7600 gtk_file_path_free (data->path);
7601 gtk_file_path_free (data->parent_path);
7605 g_object_unref (handle);
7609 paste_text_received (GtkClipboard *clipboard,
7611 GtkFileChooserDefault *impl)
7618 path = gtk_file_system_uri_to_path (impl->file_system, text);
7621 if (!g_path_is_absolute (text))
7623 location_popup_handler (impl, text);
7627 path = gtk_file_system_filename_to_path (impl->file_system, text);
7630 location_popup_handler (impl, text);
7635 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
7636 location_popup_handler (impl, text);
7638 gtk_file_path_free (path);
7641 /* Handler for the "location-popup-on-paste" keybinding signal */
7643 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
7645 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
7646 GDK_SELECTION_CLIPBOARD);
7647 gtk_clipboard_request_text (clipboard,
7648 (GtkClipboardTextReceivedFunc) paste_text_received,
7653 /* Implementation for GtkFileChooserEmbed::should_respond() */
7655 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
7657 GtkFileChooserDefault *impl;
7658 GtkWidget *toplevel;
7659 GtkWidget *current_focus;
7661 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7663 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7664 g_assert (GTK_IS_WINDOW (toplevel));
7666 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
7668 if (current_focus == impl->browse_files_tree_view)
7670 /* The following array encodes what we do based on the impl->action and the
7671 * number of files selected.
7674 NOOP, /* Do nothing (don't respond) */
7675 RESPOND, /* Respond immediately */
7676 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
7677 ALL_FILES, /* Respond only if everything selected is a file */
7678 ALL_FOLDERS, /* Respond only if everything selected is a folder */
7679 SAVE_ENTRY, /* Go to the code for handling the save entry */
7680 NOT_REACHED /* Sanity check */
7682 static const ActionToTake what_to_do[4][3] = {
7683 /* 0 selected 1 selected many selected */
7684 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
7685 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
7686 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
7687 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
7691 gboolean all_files, all_folders;
7693 ActionToTake action;
7697 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7699 selection_check (impl, &num_selected, &all_files, &all_folders);
7701 if (num_selected > 2)
7706 action = what_to_do [impl->action] [k];
7716 case RESPOND_OR_SWITCH:
7717 g_assert (num_selected == 1);
7721 switch_to_selected_folder (impl);
7724 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7725 return should_respond_after_confirm_overwrite (impl,
7726 get_display_name_from_file_list (impl),
7727 impl->current_folder);
7741 g_assert_not_reached ();
7744 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
7747 gboolean is_well_formed, is_empty, is_file_part_empty;
7750 GtkFileChooserEntry *entry;
7755 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7756 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7757 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7758 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7759 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7761 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7762 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7764 if (is_empty || !is_well_formed)
7767 g_assert (path != NULL);
7772 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7773 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7775 _gtk_file_chooser_entry_set_file_part (entry, "");
7776 change_folder_and_display_error (impl, path);
7779 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7780 || GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7782 /* The folder already exists, so we do not need to create it.
7783 * Just respond to terminate the dialog.
7789 g_assert_not_reached ();
7795 struct FileExistsData *data;
7797 /* We need to check whether path exists and is not a folder */
7799 data = g_new0 (struct FileExistsData, 1);
7800 data->impl = g_object_ref (impl);
7801 data->path = gtk_file_path_copy (path);
7802 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
7804 if (impl->file_exists_get_info_handle)
7805 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
7807 impl->file_exists_get_info_handle =
7808 gtk_file_system_get_info (impl->file_system, path,
7809 GTK_FILE_INFO_IS_FOLDER,
7810 file_exists_get_info_cb,
7813 set_busy_cursor (impl, TRUE);
7817 g_error_free (error);
7820 gtk_file_path_free (path);
7823 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
7825 /* The focus is on a dialog's action area button, *and* the widget that
7826 * was focused immediately before it is the shortcuts list. Switch to the
7827 * selected shortcut and tell the caller not to respond.
7831 if (shortcuts_get_selected (impl, &iter))
7833 shortcuts_activate_iter (impl, &iter);
7835 gtk_widget_grab_focus (impl->browse_files_tree_view);
7842 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7844 /* The focus is on a dialog's action area button, *and* the widget that
7845 * was focused immediately before it is the file list.
7849 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7851 /* The focus is on a dialog's action area button, *and* the widget that
7852 * was focused immediately before it is the location entry.
7857 /* The focus is on a dialog's action area button or something else */
7858 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7859 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7864 g_assert_not_reached ();
7868 /* Implementation for GtkFileChooserEmbed::initial_focus() */
7870 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
7872 GtkFileChooserDefault *impl;
7875 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7877 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7878 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7880 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
7881 widget = impl->browse_files_tree_view;
7883 widget = impl->location_entry;
7885 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7886 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7887 widget = impl->location_entry;
7890 g_assert_not_reached ();
7894 g_assert (widget != NULL);
7895 gtk_widget_grab_focus (widget);
7899 set_current_filter (GtkFileChooserDefault *impl,
7900 GtkFileFilter *filter)
7902 if (impl->current_filter != filter)
7906 /* NULL filters are allowed to reset to non-filtered status
7908 filter_index = g_slist_index (impl->filters, filter);
7909 if (impl->filters && filter && filter_index < 0)
7912 if (impl->current_filter)
7913 g_object_unref (impl->current_filter);
7914 impl->current_filter = filter;
7915 if (impl->current_filter)
7917 g_object_ref_sink (impl->current_filter);
7921 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
7924 if (impl->browse_files_model)
7925 install_list_model_filter (impl);
7927 g_object_notify (G_OBJECT (impl), "filter");
7932 filter_combo_changed (GtkComboBox *combo_box,
7933 GtkFileChooserDefault *impl)
7935 gint new_index = gtk_combo_box_get_active (combo_box);
7936 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
7938 set_current_filter (impl, new_filter);
7942 check_preview_change (GtkFileChooserDefault *impl)
7944 GtkTreePath *cursor_path;
7945 const GtkFilePath *new_path;
7946 const GtkFileInfo *new_info;
7948 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
7949 if (cursor_path && impl->sort_model)
7952 GtkTreeIter child_iter;
7954 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path))
7955 g_assert_not_reached ();
7957 gtk_tree_path_free (cursor_path);
7959 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
7961 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
7962 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7970 if (new_path != impl->preview_path &&
7971 !(new_path && impl->preview_path &&
7972 gtk_file_path_compare (new_path, impl->preview_path) == 0))
7974 if (impl->preview_path)
7976 gtk_file_path_free (impl->preview_path);
7977 g_free (impl->preview_display_name);
7982 impl->preview_path = gtk_file_path_copy (new_path);
7983 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
7987 impl->preview_path = NULL;
7988 impl->preview_display_name = NULL;
7991 if (impl->use_preview_label && impl->preview_label)
7992 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
7994 g_signal_emit_by_name (impl, "update-preview");
7999 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
8000 GtkFileSystemVolume *volume,
8001 const GError *error,
8005 gboolean cancelled = handle->cancelled;
8006 GtkFileChooserDefault *impl = data;
8008 if (handle != impl->shortcuts_activate_iter_handle)
8011 impl->shortcuts_activate_iter_handle = NULL;
8013 set_busy_cursor (impl, FALSE);
8022 msg = g_strdup_printf (_("Could not mount %s"),
8023 gtk_file_system_volume_get_display_name (impl->file_system, volume));
8024 error_message (impl, msg, error->message);
8030 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8033 change_folder_and_display_error (impl, path);
8034 gtk_file_path_free (path);
8038 g_object_unref (impl);
8039 g_object_unref (handle);
8043 /* Activates a volume by mounting it if necessary and then switching to its
8047 shortcuts_activate_volume (GtkFileChooserDefault *impl,
8048 GtkFileSystemVolume *volume)
8052 /* We ref the file chooser since volume_mount() may run a main loop, and the
8053 * user could close the file chooser window in the meantime.
8055 g_object_ref (impl);
8057 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
8059 set_busy_cursor (impl, TRUE);
8061 impl->shortcuts_activate_iter_handle =
8062 gtk_file_system_volume_mount (impl->file_system, volume,
8063 shortcuts_activate_volume_mount_cb,
8064 g_object_ref (impl));
8068 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8071 change_folder_and_display_error (impl, path);
8072 gtk_file_path_free (path);
8076 g_object_unref (impl);
8079 /* Opens the folder or volume at the specified iter in the shortcuts model */
8080 struct ShortcutsActivateData
8082 GtkFileChooserDefault *impl;
8087 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
8088 const GtkFileInfo *info,
8089 const GError *error,
8092 gboolean cancelled = handle->cancelled;
8093 struct ShortcutsActivateData *data = user_data;
8095 if (handle != data->impl->shortcuts_activate_iter_handle)
8098 data->impl->shortcuts_activate_iter_handle = NULL;
8103 if (!error && gtk_file_info_get_is_folder (info))
8104 change_folder_and_display_error (data->impl, data->path);
8106 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl), data->path, NULL);
8109 g_object_unref (data->impl);
8110 gtk_file_path_free (data->path);
8113 g_object_unref (handle);
8117 shortcuts_activate_iter (GtkFileChooserDefault *impl,
8123 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
8124 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
8126 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
8127 SHORTCUTS_COL_DATA, &col_data,
8128 SHORTCUTS_COL_IS_VOLUME, &is_volume,
8132 return; /* We are on a separator */
8134 if (impl->shortcuts_activate_iter_handle)
8136 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
8137 impl->shortcuts_activate_iter_handle = NULL;
8142 GtkFileSystemVolume *volume;
8146 shortcuts_activate_volume (impl, volume);
8150 struct ShortcutsActivateData *data;
8152 data = g_new0 (struct ShortcutsActivateData, 1);
8153 data->impl = g_object_ref (impl);
8154 data->path = gtk_file_path_copy (col_data);
8156 impl->shortcuts_activate_iter_handle =
8157 gtk_file_system_get_info (impl->file_system, data->path,
8158 GTK_FILE_INFO_IS_FOLDER,
8159 shortcuts_activate_get_info_cb, data);
8163 /* Callback used when a row in the shortcuts list is activated */
8165 shortcuts_row_activated_cb (GtkTreeView *tree_view,
8167 GtkTreeViewColumn *column,
8168 GtkFileChooserDefault *impl)
8171 GtkTreeIter child_iter;
8173 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
8176 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
8179 shortcuts_activate_iter (impl, &child_iter);
8181 gtk_widget_grab_focus (impl->browse_files_tree_view);
8184 /* Handler for GtkWidget::key-press-event on the shortcuts list */
8186 shortcuts_key_press_event_cb (GtkWidget *widget,
8188 GtkFileChooserDefault *impl)
8192 modifiers = gtk_accelerator_get_default_mod_mask ();
8194 if ((event->keyval == GDK_BackSpace
8195 || event->keyval == GDK_Delete
8196 || event->keyval == GDK_KP_Delete)
8197 && (event->state & modifiers) == 0)
8199 remove_selected_bookmarks (impl);
8203 if ((event->keyval == GDK_F2)
8204 && (event->state & modifiers) == 0)
8206 rename_selected_bookmark (impl);
8214 shortcuts_select_func (GtkTreeSelection *selection,
8215 GtkTreeModel *model,
8217 gboolean path_currently_selected,
8220 GtkFileChooserDefault *impl = data;
8222 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
8226 list_select_func (GtkTreeSelection *selection,
8227 GtkTreeModel *model,
8229 gboolean path_currently_selected,
8232 GtkFileChooserDefault *impl = data;
8234 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8235 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8237 GtkTreeIter iter, child_iter;
8238 const GtkFileInfo *info;
8240 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8243 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8245 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8247 if (info && !gtk_file_info_get_is_folder (info))
8255 list_selection_changed (GtkTreeSelection *selection,
8256 GtkFileChooserDefault *impl)
8258 /* See if we are in the new folder editable row for Save mode */
8259 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8261 const GtkFileInfo *info;
8262 gboolean had_selection;
8264 info = get_selected_file_info_from_file_list (impl, &had_selection);
8266 goto out; /* normal processing */
8269 return; /* We are on the editable row for New Folder */
8274 update_chooser_entry (impl);
8275 check_preview_change (impl);
8276 bookmarks_check_add_sensitivity (impl);
8278 g_signal_emit_by_name (impl, "selection-changed", 0);
8281 /* Callback used when a row in the file list is activated */
8283 list_row_activated (GtkTreeView *tree_view,
8285 GtkTreeViewColumn *column,
8286 GtkFileChooserDefault *impl)
8288 GtkTreeIter iter, child_iter;
8289 const GtkFileInfo *info;
8291 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8294 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8296 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8298 if (gtk_file_info_get_is_folder (info))
8300 const GtkFilePath *file_path;
8302 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8303 change_folder_and_display_error (impl, file_path);
8308 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8309 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8310 g_signal_emit_by_name (impl, "file-activated");
8314 path_bar_clicked (GtkPathBar *path_bar,
8315 GtkFilePath *file_path,
8316 GtkFilePath *child_path,
8317 gboolean child_is_hidden,
8318 GtkFileChooserDefault *impl)
8321 pending_select_paths_add (impl, child_path);
8323 if (!change_folder_and_display_error (impl, file_path))
8326 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
8327 * show hidden files so that ".baz" appears in the file list, as it will still
8328 * be shown in the path bar: "/foo/[bar]/.baz"
8330 if (child_is_hidden)
8331 g_object_set (impl, "show-hidden", TRUE, NULL);
8334 static const GtkFileInfo *
8335 get_list_file_info (GtkFileChooserDefault *impl,
8338 GtkTreeIter child_iter;
8340 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8344 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8348 list_icon_data_func (GtkTreeViewColumn *tree_column,
8349 GtkCellRenderer *cell,
8350 GtkTreeModel *tree_model,
8354 GtkFileChooserDefault *impl = data;
8355 GtkTreeIter child_iter;
8356 const GtkFilePath *path;
8358 const GtkFileInfo *info;
8359 gboolean sensitive = TRUE;
8361 profile_start ("start", NULL);
8363 info = get_list_file_info (impl, iter);
8365 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8368 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8376 /* FIXME: NULL GError */
8377 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
8378 impl->icon_size, NULL);
8383 /* We are on the editable row */
8387 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8388 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
8389 sensitive = gtk_file_info_get_is_folder (info);
8393 "sensitive", sensitive,
8397 g_object_unref (pixbuf);
8399 profile_end ("end", NULL);
8403 list_name_data_func (GtkTreeViewColumn *tree_column,
8404 GtkCellRenderer *cell,
8405 GtkTreeModel *tree_model,
8409 GtkFileChooserDefault *impl = data;
8410 const GtkFileInfo *info = get_list_file_info (impl, iter);
8411 gboolean sensitive = TRUE;
8416 "text", _("Type name of new folder"),
8423 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8424 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8426 sensitive = gtk_file_info_get_is_folder (info);
8430 "text", gtk_file_info_get_display_name (info),
8431 "sensitive", sensitive,
8437 list_size_data_func (GtkTreeViewColumn *tree_column,
8438 GtkCellRenderer *cell,
8439 GtkTreeModel *tree_model,
8443 GtkFileChooserDefault *impl = data;
8444 const GtkFileInfo *info = get_list_file_info (impl, iter);
8447 gboolean sensitive = TRUE;
8449 if (!info || gtk_file_info_get_is_folder (info))
8453 "sensitive", sensitive,
8458 size = gtk_file_info_get_size (info);
8460 if (size < (gint64)1024)
8461 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
8462 else if (size < (gint64)1024*1024)
8463 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
8464 else if (size < (gint64)1024*1024*1024)
8465 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
8467 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
8469 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
8470 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8471 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8476 "sensitive", sensitive,
8477 "alignment", PANGO_ALIGN_RIGHT,
8484 /* Tree column data callback for the file list; fetches the mtime of a file */
8486 list_mtime_data_func (GtkTreeViewColumn *tree_column,
8487 GtkCellRenderer *cell,
8488 GtkTreeModel *tree_model,
8492 GtkFileChooserDefault *impl;
8493 const GtkFileInfo *info;
8494 GtkFileTime time_mtime;
8498 gboolean sensitive = TRUE;
8502 info = get_list_file_info (impl, iter);
8512 time_mtime = gtk_file_info_get_modification_time (info);
8514 if (time_mtime == 0)
8515 strcpy (buf, _("Unknown"));
8519 g_date_set_time_t (&mtime, time_mtime);
8520 time_now = time (NULL);
8521 g_date_set_time_t (&now, time_now);
8523 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
8526 strcpy (buf, _("Today"));
8527 else if (days_diff == 1)
8528 strcpy (buf, _("Yesterday"));
8533 if (days_diff > 1 && days_diff < 7)
8534 format = "%A"; /* Days from last week */
8536 format = "%x"; /* Any other date */
8538 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
8539 strcpy (buf, _("Unknown"));
8543 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8544 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8545 sensitive = gtk_file_info_get_is_folder (info);
8549 "sensitive", sensitive,
8554 _gtk_file_chooser_default_new (const char *file_system)
8556 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
8557 "file-system-backend", file_system,
8562 location_set_user_text (GtkFileChooserDefault *impl,
8565 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
8566 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
8570 location_popup_handler (GtkFileChooserDefault *impl,
8573 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8574 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8576 LocationMode new_mode;
8580 /* since the user typed something, we unconditionally want to turn on the entry */
8581 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8583 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8584 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8585 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8586 new_mode = LOCATION_MODE_PATH_BAR;
8589 g_assert_not_reached ();
8593 location_mode_set (impl, new_mode, TRUE);
8594 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
8597 location_set_user_text (impl, path);
8600 location_entry_set_initial_text (impl);
8601 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
8605 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8606 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8608 gtk_widget_grab_focus (impl->location_entry);
8610 location_set_user_text (impl, path);
8613 g_assert_not_reached ();
8616 /* Handler for the "up-folder" keybinding signal */
8618 up_folder_handler (GtkFileChooserDefault *impl)
8620 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
8623 /* Handler for the "down-folder" keybinding signal */
8625 down_folder_handler (GtkFileChooserDefault *impl)
8627 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
8630 /* Switches to the shortcut in the specified index */
8632 switch_to_shortcut (GtkFileChooserDefault *impl,
8637 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8638 g_assert_not_reached ();
8640 shortcuts_activate_iter (impl, &iter);
8643 /* Handler for the "home-folder" keybinding signal */
8645 home_folder_handler (GtkFileChooserDefault *impl)
8648 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
8651 /* Handler for the "desktop-folder" keybinding signal */
8653 desktop_folder_handler (GtkFileChooserDefault *impl)
8655 if (impl->has_desktop)
8656 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
8660 quick_bookmark_handler (GtkFileChooserDefault *impl,
8661 gint bookmark_index)
8666 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
8669 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
8671 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
8672 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
8675 gtk_tree_path_free (path);
8677 switch_to_shortcut (impl, bookmark_pos);
8682 /* Drag and drop interfaces */
8685 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
8690 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
8695 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
8697 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
8700 ShortcutsModelFilter *model;
8704 model = SHORTCUTS_MODEL_FILTER (drag_source);
8706 pos = *gtk_tree_path_get_indices (path);
8707 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
8709 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
8712 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
8714 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
8716 GtkSelectionData *selection_data)
8718 ShortcutsModelFilter *model;
8720 model = SHORTCUTS_MODEL_FILTER (drag_source);
8727 /* Fill the GtkTreeDragSourceIface vtable */
8729 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
8731 iface->row_draggable = shortcuts_model_filter_row_draggable;
8732 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
8736 /* Fill the GtkTreeDragDestIface vtable */
8738 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
8740 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
8741 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
8745 static GtkTreeModel *
8746 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
8747 GtkTreeModel *child_model,
8750 ShortcutsModelFilter *model;
8752 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
8753 "child-model", child_model,
8754 "virtual-root", root,
8759 return GTK_TREE_MODEL (model);