1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdefault.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmenuitem.h"
55 #include "gtkmessagedialog.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkscrolledwindow.h"
60 #include "gtkseparatormenuitem.h"
61 #include "gtksizegroup.h"
64 #include "gtktreednd.h"
65 #include "gtktreeprivate.h"
66 #include "gtktreeselection.h"
67 #include "gtktypebuiltins.h"
70 #if defined (G_OS_UNIX)
71 #include "gtkfilesystemunix.h"
72 #elif defined (G_OS_WIN32)
73 #include "gtkfilesystemwin32.h"
92 #undef PROFILE_FILE_CHOOSER
93 #ifdef PROFILE_FILE_CHOOSER
100 #define PROFILE_INDENT 4
101 static int profile_indent;
104 profile_add_indent (int indent)
106 profile_indent += indent;
107 if (profile_indent < 0)
108 g_error ("You screwed up your indentation");
112 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
117 profile_add_indent (indent);
119 if (profile_indent == 0)
120 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
122 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
128 profile_add_indent (indent);
131 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
132 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
133 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
135 #define profile_start(x, y)
136 #define profile_end(x, y)
137 #define profile_msg(x, y)
142 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
144 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
146 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
148 #define MAX_LOADING_TIME 500
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
168 static guint signals[LAST_SIGNAL] = { 0 };
170 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
172 SHORTCUTS_COL_PIXBUF,
175 SHORTCUTS_COL_IS_VOLUME,
176 SHORTCUTS_COL_REMOVABLE,
177 SHORTCUTS_COL_PIXBUF_VISIBLE,
178 SHORTCUTS_COL_HANDLE,
179 SHORTCUTS_COL_NUM_COLUMNS
182 /* Column numbers for the file list */
187 FILE_LIST_COL_NUM_COLUMNS
190 /* Identifiers for target types */
196 /* Target types for dragging from the shortcuts list */
197 static const GtkTargetEntry shortcuts_source_targets[] = {
198 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
201 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
202 / sizeof (shortcuts_source_targets[0]));
204 /* Target types for dropping into the shortcuts list */
205 static const GtkTargetEntry shortcuts_dest_targets[] = {
206 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
207 { "text/uri-list", 0, TEXT_URI_LIST }
210 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
211 / sizeof (shortcuts_dest_targets[0]));
213 /* Target types for DnD from the file list */
214 static const GtkTargetEntry file_list_source_targets[] = {
215 { "text/uri-list", 0, TEXT_URI_LIST }
218 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
219 / sizeof (file_list_source_targets[0]));
221 /* Target types for dropping into the file list */
222 static const GtkTargetEntry file_list_dest_targets[] = {
223 { "text/uri-list", 0, TEXT_URI_LIST }
226 static const int num_file_list_dest_targets = (sizeof (file_list_dest_targets)
227 / sizeof (file_list_dest_targets[0]));
230 /* Interesting places in the shortcuts bar */
236 SHORTCUTS_BOOKMARKS_SEPARATOR,
238 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
239 SHORTCUTS_CURRENT_FOLDER
242 /* Icon size for if we can't get it from the theme */
243 #define FALLBACK_ICON_SIZE 16
245 #define PREVIEW_HBOX_SPACING 12
249 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
250 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
252 static GObject* gtk_file_chooser_default_constructor (GType type,
253 guint n_construct_properties,
254 GObjectConstructParam *construct_params);
255 static void gtk_file_chooser_default_finalize (GObject *object);
256 static void gtk_file_chooser_default_set_property (GObject *object,
260 static void gtk_file_chooser_default_get_property (GObject *object,
264 static void gtk_file_chooser_default_dispose (GObject *object);
265 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
266 static void gtk_file_chooser_default_map (GtkWidget *widget);
267 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
268 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
269 GtkWidget *previous_toplevel);
270 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
271 GtkStyle *previous_style);
272 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
273 GdkScreen *previous_screen);
275 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
276 const GtkFilePath *path,
278 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
279 const GtkFilePath *path,
282 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
283 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
285 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
286 const GtkFilePath *path,
288 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
289 const GtkFilePath *path);
290 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
291 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
292 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
293 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
294 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
295 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
296 GtkFileFilter *filter);
297 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
298 GtkFileFilter *filter);
299 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
300 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
301 const GtkFilePath *path,
303 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
304 const GtkFilePath *path,
306 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
310 gint *default_height);
311 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
312 gboolean *resize_horizontally,
313 gboolean *resize_vertically);
314 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
315 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
317 static void location_popup_handler (GtkFileChooserDefault *impl,
319 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
320 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
321 static void up_folder_handler (GtkFileChooserDefault *impl);
322 static void down_folder_handler (GtkFileChooserDefault *impl);
323 static void home_folder_handler (GtkFileChooserDefault *impl);
324 static void desktop_folder_handler (GtkFileChooserDefault *impl);
325 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
326 gint bookmark_index);
327 static void update_appearance (GtkFileChooserDefault *impl);
329 static void set_current_filter (GtkFileChooserDefault *impl,
330 GtkFileFilter *filter);
331 static void check_preview_change (GtkFileChooserDefault *impl);
333 static void filter_combo_changed (GtkComboBox *combo_box,
334 GtkFileChooserDefault *impl);
335 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
337 GtkTreeViewColumn *column,
338 GtkFileChooserDefault *impl);
340 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
342 GtkFileChooserDefault *impl);
344 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
347 gboolean path_currently_selected,
349 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
351 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
353 static int shortcuts_get_index (GtkFileChooserDefault *impl,
354 ShortcutsIndex where);
355 static int shortcut_find_position (GtkFileChooserDefault *impl,
356 const GtkFilePath *path);
358 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
360 static gboolean list_select_func (GtkTreeSelection *selection,
363 gboolean path_currently_selected,
366 static void list_selection_changed (GtkTreeSelection *tree_selection,
367 GtkFileChooserDefault *impl);
368 static void list_row_activated (GtkTreeView *tree_view,
370 GtkTreeViewColumn *column,
371 GtkFileChooserDefault *impl);
373 static void select_func (GtkFileSystemModel *model,
378 static void path_bar_clicked (GtkPathBar *path_bar,
379 GtkFilePath *file_path,
380 GtkFilePath *child_path,
381 gboolean child_is_hidden,
382 GtkFileChooserDefault *impl);
384 static void add_bookmark_button_clicked_cb (GtkButton *button,
385 GtkFileChooserDefault *impl);
386 static void remove_bookmark_button_clicked_cb (GtkButton *button,
387 GtkFileChooserDefault *impl);
388 static void save_folder_combo_changed_cb (GtkComboBox *combo,
389 GtkFileChooserDefault *impl);
391 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
392 GtkCellRenderer *cell,
393 GtkTreeModel *tree_model,
396 static void list_name_data_func (GtkTreeViewColumn *tree_column,
397 GtkCellRenderer *cell,
398 GtkTreeModel *tree_model,
402 static void list_size_data_func (GtkTreeViewColumn *tree_column,
403 GtkCellRenderer *cell,
404 GtkTreeModel *tree_model,
408 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
409 GtkCellRenderer *cell,
410 GtkTreeModel *tree_model,
414 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
417 static void load_remove_timer (GtkFileChooserDefault *impl);
418 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
420 static void location_button_toggled_cb (GtkToggleButton *toggle,
421 GtkFileChooserDefault *impl);
422 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
427 /* Drag and drop interface declarations */
430 GtkTreeModelFilter parent;
432 GtkFileChooserDefault *impl;
433 } ShortcutsModelFilter;
436 GtkTreeModelFilterClass parent_class;
437 } ShortcutsModelFilterClass;
439 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
440 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
442 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
444 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
445 _shortcuts_model_filter,
446 GTK_TYPE_TREE_MODEL_FILTER,
447 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
448 shortcuts_model_filter_drag_source_iface_init))
450 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
451 GtkTreeModel *child_model,
456 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
457 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
458 gtk_file_chooser_default_iface_init)
459 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
460 gtk_file_chooser_embed_default_iface_init));
463 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
465 static const guint quick_bookmark_keyvals[10] = {
466 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
468 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
470 GtkBindingSet *binding_set;
473 gobject_class->finalize = gtk_file_chooser_default_finalize;
474 gobject_class->constructor = gtk_file_chooser_default_constructor;
475 gobject_class->set_property = gtk_file_chooser_default_set_property;
476 gobject_class->get_property = gtk_file_chooser_default_get_property;
477 gobject_class->dispose = gtk_file_chooser_default_dispose;
479 widget_class->show_all = gtk_file_chooser_default_show_all;
480 widget_class->map = gtk_file_chooser_default_map;
481 widget_class->unmap = gtk_file_chooser_default_unmap;
482 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
483 widget_class->style_set = gtk_file_chooser_default_style_set;
484 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
486 signals[LOCATION_POPUP] =
487 _gtk_binding_signal_new (I_("location-popup"),
488 G_OBJECT_CLASS_TYPE (class),
489 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
490 G_CALLBACK (location_popup_handler),
492 _gtk_marshal_VOID__STRING,
493 G_TYPE_NONE, 1, G_TYPE_STRING);
494 signals[LOCATION_POPUP_ON_PASTE] =
495 _gtk_binding_signal_new ("location-popup-on-paste",
496 G_OBJECT_CLASS_TYPE (class),
497 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
498 G_CALLBACK (location_popup_on_paste_handler),
500 _gtk_marshal_VOID__VOID,
502 signals[LOCATION_TOGGLE_POPUP] =
503 _gtk_binding_signal_new (I_("location-toggle-popup"),
504 G_OBJECT_CLASS_TYPE (class),
505 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
506 G_CALLBACK (location_toggle_popup_handler),
508 _gtk_marshal_VOID__VOID,
511 _gtk_binding_signal_new (I_("up-folder"),
512 G_OBJECT_CLASS_TYPE (class),
513 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
514 G_CALLBACK (up_folder_handler),
516 _gtk_marshal_VOID__VOID,
518 signals[DOWN_FOLDER] =
519 _gtk_binding_signal_new (I_("down-folder"),
520 G_OBJECT_CLASS_TYPE (class),
521 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
522 G_CALLBACK (down_folder_handler),
524 _gtk_marshal_VOID__VOID,
526 signals[HOME_FOLDER] =
527 _gtk_binding_signal_new (I_("home-folder"),
528 G_OBJECT_CLASS_TYPE (class),
529 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
530 G_CALLBACK (home_folder_handler),
532 _gtk_marshal_VOID__VOID,
534 signals[DESKTOP_FOLDER] =
535 _gtk_binding_signal_new (I_("desktop-folder"),
536 G_OBJECT_CLASS_TYPE (class),
537 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
538 G_CALLBACK (desktop_folder_handler),
540 _gtk_marshal_VOID__VOID,
542 signals[QUICK_BOOKMARK] =
543 _gtk_binding_signal_new (I_("quick-bookmark"),
544 G_OBJECT_CLASS_TYPE (class),
545 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
546 G_CALLBACK (quick_bookmark_handler),
548 _gtk_marshal_VOID__INT,
549 G_TYPE_NONE, 1, G_TYPE_INT);
551 binding_set = gtk_binding_set_by_class (class);
553 gtk_binding_entry_add_signal (binding_set,
554 GDK_l, GDK_CONTROL_MASK,
555 "location-toggle-popup",
558 gtk_binding_entry_add_signal (binding_set,
561 1, G_TYPE_STRING, "/");
562 gtk_binding_entry_add_signal (binding_set,
565 1, G_TYPE_STRING, "/");
568 gtk_binding_entry_add_signal (binding_set,
571 1, G_TYPE_STRING, "~");
574 gtk_binding_entry_add_signal (binding_set,
575 GDK_v, GDK_CONTROL_MASK,
576 "location-popup-on-paste",
579 gtk_binding_entry_add_signal (binding_set,
580 GDK_Up, GDK_MOD1_MASK,
583 gtk_binding_entry_add_signal (binding_set,
587 gtk_binding_entry_add_signal (binding_set,
588 GDK_KP_Up, GDK_MOD1_MASK,
592 gtk_binding_entry_add_signal (binding_set,
593 GDK_Down, GDK_MOD1_MASK,
596 gtk_binding_entry_add_signal (binding_set,
597 GDK_KP_Down, GDK_MOD1_MASK,
601 gtk_binding_entry_add_signal (binding_set,
602 GDK_Home, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set,
606 GDK_KP_Home, GDK_MOD1_MASK,
609 gtk_binding_entry_add_signal (binding_set,
610 GDK_d, GDK_MOD1_MASK,
614 for (i = 0; i < 10; i++)
615 gtk_binding_entry_add_signal (binding_set,
616 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
620 _gtk_file_chooser_install_properties (gobject_class);
624 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
626 iface->select_path = gtk_file_chooser_default_select_path;
627 iface->unselect_path = gtk_file_chooser_default_unselect_path;
628 iface->select_all = gtk_file_chooser_default_select_all;
629 iface->unselect_all = gtk_file_chooser_default_unselect_all;
630 iface->get_paths = gtk_file_chooser_default_get_paths;
631 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
632 iface->get_file_system = gtk_file_chooser_default_get_file_system;
633 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
634 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
635 iface->set_current_name = gtk_file_chooser_default_set_current_name;
636 iface->add_filter = gtk_file_chooser_default_add_filter;
637 iface->remove_filter = gtk_file_chooser_default_remove_filter;
638 iface->list_filters = gtk_file_chooser_default_list_filters;
639 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
640 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
641 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
645 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
647 iface->get_default_size = gtk_file_chooser_default_get_default_size;
648 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
649 iface->should_respond = gtk_file_chooser_default_should_respond;
650 iface->initial_focus = gtk_file_chooser_default_initial_focus;
654 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
656 profile_start ("start", NULL);
657 #ifdef PROFILE_FILE_CHOOSER
658 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
660 impl->local_only = TRUE;
661 impl->preview_widget_active = TRUE;
662 impl->use_preview_label = TRUE;
663 impl->select_multiple = FALSE;
664 impl->show_hidden = FALSE;
665 impl->icon_size = FALLBACK_ICON_SIZE;
666 impl->load_state = LOAD_EMPTY;
667 impl->reload_state = RELOAD_EMPTY;
668 impl->pending_select_paths = NULL;
669 impl->location_mode = LOCATION_MODE_PATH_BAR;
671 gtk_box_set_spacing (GTK_BOX (impl), 12);
673 impl->tooltips = gtk_tooltips_new ();
674 g_object_ref_sink (impl->tooltips);
676 profile_end ("end", NULL);
679 /* Frees the data columns for the specified iter in the shortcuts model*/
681 shortcuts_free_row_data (GtkFileChooserDefault *impl,
686 GtkFileSystemHandle *handle;
688 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
689 SHORTCUTS_COL_DATA, &col_data,
690 SHORTCUTS_COL_IS_VOLUME, &is_volume,
691 SHORTCUTS_COL_HANDLE, &handle,
695 gtk_file_system_cancel_operation (handle);
702 GtkFileSystemVolume *volume;
705 gtk_file_system_volume_free (impl->file_system, volume);
712 gtk_file_path_free (path);
716 /* Frees all the data columns in the shortcuts model */
718 shortcuts_free (GtkFileChooserDefault *impl)
722 if (!impl->shortcuts_model)
725 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
728 shortcuts_free_row_data (impl, &iter);
730 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
732 g_object_unref (impl->shortcuts_model);
733 impl->shortcuts_model = NULL;
737 pending_select_paths_free (GtkFileChooserDefault *impl)
741 for (l = impl->pending_select_paths; l; l = l->next)
746 gtk_file_path_free (path);
749 g_slist_free (impl->pending_select_paths);
750 impl->pending_select_paths = NULL;
754 pending_select_paths_add (GtkFileChooserDefault *impl,
755 const GtkFilePath *path)
757 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
760 /* Used from gtk_tree_selection_selected_foreach() */
762 store_selection_foreach (GtkTreeModel *model,
767 GtkFileChooserDefault *impl;
768 GtkTreeIter child_iter;
769 const GtkFilePath *file_path;
771 impl = GTK_FILE_CHOOSER_DEFAULT (data);
773 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
775 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
776 pending_select_paths_add (impl, file_path);
779 /* Stores the current selection in the list of paths to select; this is used to
780 * preserve the selection when reloading the current folder.
783 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
785 GtkTreeSelection *selection;
787 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
788 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
792 gtk_file_chooser_default_finalize (GObject *object)
794 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
797 if (impl->shortcuts_filter_model)
798 g_object_unref (impl->shortcuts_filter_model);
800 shortcuts_free (impl);
802 g_object_unref (impl->file_system);
804 g_free (impl->browse_files_last_selected_name);
806 for (l = impl->filters; l; l = l->next)
808 GtkFileFilter *filter;
810 filter = GTK_FILE_FILTER (l->data);
811 g_object_unref (filter);
813 g_slist_free (impl->filters);
815 if (impl->current_filter)
816 g_object_unref (impl->current_filter);
818 if (impl->current_volume_path)
819 gtk_file_path_free (impl->current_volume_path);
821 if (impl->current_folder)
822 gtk_file_path_free (impl->current_folder);
824 if (impl->preview_path)
825 gtk_file_path_free (impl->preview_path);
827 load_remove_timer (impl);
829 /* Free all the Models we have */
830 if (impl->browse_files_model)
831 g_object_unref (impl->browse_files_model);
833 if (impl->sort_model)
834 g_object_unref (impl->sort_model);
836 g_free (impl->preview_display_name);
838 g_free (impl->edited_new_text);
840 g_object_unref (impl->tooltips);
842 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
845 /* Shows an error dialog set as transient for the specified window */
847 error_message_with_parent (GtkWindow *parent,
853 dialog = gtk_message_dialog_new (parent,
854 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
859 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
863 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
865 gtk_dialog_run (GTK_DIALOG (dialog));
866 gtk_widget_destroy (dialog);
869 /* Returns a toplevel GtkWindow, or NULL if none */
871 get_toplevel (GtkWidget *widget)
875 toplevel = gtk_widget_get_toplevel (widget);
876 if (!GTK_WIDGET_TOPLEVEL (toplevel))
879 return GTK_WINDOW (toplevel);
882 /* Shows an error dialog for the file chooser */
884 error_message (GtkFileChooserDefault *impl,
888 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
891 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
893 error_dialog (GtkFileChooserDefault *impl,
895 const GtkFilePath *path,
904 uri = gtk_file_system_path_to_uri (impl->file_system, path);
905 text = g_strdup_printf (msg, uri);
906 error_message (impl, text, error->message);
909 g_error_free (error);
913 /* Displays an error message about not being able to get information for a file.
914 * Frees the GError as well.
917 error_getting_info_dialog (GtkFileChooserDefault *impl,
918 const GtkFilePath *path,
922 _("Could not retrieve information about the file"),
926 /* Shows an error dialog about not being able to add a bookmark */
928 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
929 const GtkFilePath *path,
933 _("Could not add a bookmark"),
937 /* Shows an error dialog about not being able to remove a bookmark */
939 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
940 const GtkFilePath *path,
944 _("Could not remove bookmark"),
948 /* Shows an error dialog about not being able to create a folder */
950 error_creating_folder_dialog (GtkFileChooserDefault *impl,
951 const GtkFilePath *path,
955 _("The folder could not be created"),
959 /* Shows an error about not being able to create a folder because a file with
960 * the same name is already there.
963 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
964 const GtkFilePath *path,
968 _("The folder could not be created, as a file with the same name "
969 "already exists. Try using a different name for the folder, "
970 "or rename the file first."),
974 /* Shows an error dialog about not being able to create a filename */
976 error_building_filename_dialog (GtkFileChooserDefault *impl,
977 const GtkFilePath *folder_part,
978 const char *file_part,
981 error_dialog (impl, _("Invalid file name"),
985 /* Shows an error dialog when we cannot switch to a folder */
987 error_changing_folder_dialog (GtkFileChooserDefault *impl,
988 const GtkFilePath *path,
991 error_dialog (impl, _("The folder contents could not be displayed"),
995 /* Changes folders, displaying an error dialog if this fails */
997 change_folder_and_display_error (GtkFileChooserDefault *impl,
998 const GtkFilePath *path)
1002 GtkFilePath *path_copy;
1004 g_return_val_if_fail (path != NULL, FALSE);
1006 profile_start ("start", (char *) path);
1008 /* We copy the path because of this case:
1010 * list_row_activated()
1011 * fetches path from model; path belongs to the model (*)
1012 * calls change_folder_and_display_error()
1013 * calls _gtk_file_chooser_set_current_folder_path()
1014 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1017 path_copy = gtk_file_path_copy (path);
1020 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1023 error_changing_folder_dialog (impl, path_copy, error);
1025 gtk_file_path_free (path_copy);
1027 profile_end ("end", (char *) path);
1033 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1035 if (impl->use_preview_label)
1037 if (!impl->preview_label)
1039 impl->preview_label = gtk_label_new (impl->preview_display_name);
1040 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1041 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1042 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1043 gtk_widget_show (impl->preview_label);
1048 if (impl->preview_label)
1050 gtk_widget_destroy (impl->preview_label);
1051 impl->preview_label = NULL;
1055 if (impl->preview_widget_active && impl->preview_widget)
1056 gtk_widget_show (impl->preview_box);
1058 gtk_widget_hide (impl->preview_box);
1060 g_signal_emit_by_name (impl, "default-size-changed");
1064 set_preview_widget (GtkFileChooserDefault *impl,
1065 GtkWidget *preview_widget)
1067 if (preview_widget == impl->preview_widget)
1070 if (impl->preview_widget)
1071 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1072 impl->preview_widget);
1074 impl->preview_widget = preview_widget;
1075 if (impl->preview_widget)
1077 gtk_widget_show (impl->preview_widget);
1078 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1079 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1080 impl->preview_widget,
1081 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1084 update_preview_widget_visibility (impl);
1087 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1088 struct ReloadIconsData
1090 GtkFileChooserDefault *impl;
1091 GtkTreeRowReference *row_ref;
1095 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1096 const GtkFileInfo *info,
1097 const GError *error,
1103 gboolean cancelled = handle->cancelled;
1104 struct ReloadIconsData *data = user_data;
1106 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1109 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1111 if (cancelled || error)
1114 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1115 data->impl->icon_size, NULL);
1117 path = gtk_tree_row_reference_get_path (data->row_ref);
1118 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1119 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1120 SHORTCUTS_COL_PIXBUF, pixbuf,
1122 gtk_tree_path_free (path);
1125 g_object_unref (pixbuf);
1128 gtk_tree_row_reference_free (data->row_ref);
1129 g_object_unref (data->impl);
1132 g_object_unref (handle);
1136 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1141 profile_start ("start", NULL);
1143 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1146 for (l = impl->reload_icon_handles; l; l = l->next)
1148 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1149 gtk_file_system_cancel_operation (handle);
1151 g_slist_free (impl->reload_icon_handles);
1152 impl->reload_icon_handles = NULL;
1158 gboolean pixbuf_visible;
1161 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1162 SHORTCUTS_COL_DATA, &data,
1163 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1164 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1167 if (pixbuf_visible && data)
1171 GtkFileSystemVolume *volume;
1174 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1175 impl->icon_size, NULL);
1177 gtk_list_store_set (impl->shortcuts_model, &iter,
1178 SHORTCUTS_COL_PIXBUF, pixbuf,
1182 g_object_unref (pixbuf);
1184 else if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1186 const GtkFilePath *path;
1187 struct ReloadIconsData *info;
1188 GtkTreePath *tree_path;
1189 GtkFileSystemHandle *handle;
1193 info = g_new0 (struct ReloadIconsData, 1);
1194 info->impl = g_object_ref (impl);
1195 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1196 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1197 gtk_tree_path_free (tree_path);
1199 handle = gtk_file_system_get_info (impl->file_system, path,
1201 shortcuts_reload_icons_get_info_cb,
1203 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1207 GtkIconTheme *icon_theme;
1209 /* Don't call get_info for remote paths to avoid latency and
1211 * If we switch to a better bookmarks file format (XBEL), we
1212 * should use mime info to get a better icon.
1214 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1215 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1216 impl->icon_size, 0, NULL);
1218 gtk_list_store_set (impl->shortcuts_model, &iter,
1219 SHORTCUTS_COL_PIXBUF, pixbuf,
1223 g_object_unref (pixbuf);
1227 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1231 profile_end ("end", NULL);
1235 shortcuts_find_folder (GtkFileChooserDefault *impl,
1236 GtkFilePath *folder)
1238 GtkTreeSelection *selection;
1242 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1244 g_assert (folder != NULL);
1245 pos = shortcut_find_position (impl, folder);
1248 gtk_tree_selection_unselect_all (selection);
1252 path = gtk_tree_path_new_from_indices (pos, -1);
1253 gtk_tree_selection_select_path (selection, path);
1254 gtk_tree_path_free (path);
1257 /* If a shortcut corresponds to the current folder, selects it */
1259 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1261 shortcuts_find_folder (impl, impl->current_folder);
1264 /* Removes the specified number of rows from the shortcuts list */
1266 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1272 path = gtk_tree_path_new_from_indices (start_row, -1);
1274 for (; n_rows; n_rows--)
1278 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1279 g_assert_not_reached ();
1281 shortcuts_free_row_data (impl, &iter);
1282 gtk_list_store_remove (impl->shortcuts_model, &iter);
1285 gtk_tree_path_free (path);
1289 shortcuts_update_count (GtkFileChooserDefault *impl,
1290 ShortcutsIndex type,
1295 case SHORTCUTS_HOME:
1297 impl->has_home = FALSE;
1299 impl->has_home = TRUE;
1302 case SHORTCUTS_DESKTOP:
1304 impl->has_desktop = FALSE;
1306 impl->has_desktop = TRUE;
1309 case SHORTCUTS_VOLUMES:
1310 impl->num_volumes += value;
1313 case SHORTCUTS_SHORTCUTS:
1314 impl->num_shortcuts += value;
1317 case SHORTCUTS_BOOKMARKS:
1318 impl->num_bookmarks += value;
1321 case SHORTCUTS_CURRENT_FOLDER:
1323 impl->shortcuts_current_folder_active = FALSE;
1325 impl->shortcuts_current_folder_active = TRUE;
1334 struct ShortcutsInsertRequest
1336 GtkFileChooserDefault *impl;
1337 GtkFilePath *parent_path;
1341 GtkTreeRowReference *row_ref;
1342 ShortcutsIndex type;
1348 get_file_info_finished (GtkFileSystemHandle *handle,
1349 const GtkFileInfo *info,
1350 const GError *error,
1354 gboolean cancelled = handle->cancelled;
1355 gboolean is_volume = FALSE;
1359 GtkFileSystemHandle *model_handle;
1360 struct ShortcutsInsertRequest *request = data;
1362 path = gtk_tree_row_reference_get_path (request->row_ref);
1364 /* Handle doesn't exist anymore in the model */
1367 pos = gtk_tree_path_get_indices (path)[0];
1368 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1370 gtk_tree_path_free (path);
1372 /* validate handle, else goto out */
1373 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1374 SHORTCUTS_COL_HANDLE, &model_handle,
1376 if (handle != model_handle)
1379 /* set the handle to NULL in the model (we unref later on) */
1380 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1381 SHORTCUTS_COL_HANDLE, NULL,
1389 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1390 shortcuts_update_count (request->impl, request->type, -1);
1392 if (request->type == SHORTCUTS_HOME)
1394 const char *home = g_get_home_dir ();
1395 GtkFilePath *home_path;
1397 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1398 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1399 gtk_file_path_free (home_path);
1401 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1403 /* Remove the current folder separator */
1404 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1405 shortcuts_remove_rows (request->impl, separator_pos, 1);
1411 if (!request->label_copy)
1412 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1413 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1414 request->impl->icon_size, NULL);
1416 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1417 SHORTCUTS_COL_PIXBUF, pixbuf,
1418 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1419 SHORTCUTS_COL_NAME, request->label_copy,
1420 SHORTCUTS_COL_IS_VOLUME, is_volume,
1421 SHORTCUTS_COL_REMOVABLE, request->removable,
1424 if (request->impl->shortcuts_filter_model)
1425 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_filter_model));
1427 if (request->type == SHORTCUTS_CURRENT_FOLDER
1428 && request->impl->save_folder_combo != NULL)
1430 /* The current folder is updated via _activate_iter(), don't
1431 * have save_folder_combo_changed_cb() call _activate_iter()
1434 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1435 G_CALLBACK (save_folder_combo_changed_cb),
1437 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1438 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1439 G_CALLBACK (save_folder_combo_changed_cb),
1444 g_object_unref (pixbuf);
1447 g_object_unref (request->impl);
1448 gtk_file_path_free (request->parent_path);
1449 gtk_file_path_free (request->path);
1450 gtk_tree_row_reference_free (request->row_ref);
1451 g_free (request->label_copy);
1454 g_object_unref (handle);
1457 /* FIXME: GtkFileSystem needs a function to split a remote path
1458 * into hostname and path components, or maybe just have a
1459 * gtk_file_system_path_get_display_name().
1461 * This function is also used in gtkfilechooserbutton.c
1464 _gtk_file_chooser_label_for_uri (const gchar *uri)
1466 const gchar *path, *start, *end, *p;
1467 gchar *host, *label;
1469 start = strstr (uri, "://");
1471 path = strchr (start, '/');
1477 end = uri + strlen (uri);
1481 /* strip username */
1482 p = strchr (start, '@');
1488 p = strchr (start, ':');
1492 host = g_strndup (start, end - start);
1494 /* Translators: the first string is a path and the second string
1495 * is a hostname. Nautilus and the panel contain the same string
1498 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1505 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1506 * inserts a volume. A position of -1 indicates the end of the tree.
1509 shortcuts_insert_path (GtkFileChooserDefault *impl,
1512 GtkFileSystemVolume *volume,
1513 const GtkFilePath *path,
1516 ShortcutsIndex type)
1519 GdkPixbuf *pixbuf = NULL;
1520 gpointer data = NULL;
1522 GtkIconTheme *icon_theme;
1524 profile_start ("start", is_volume ? "volume" : (char *) path);
1529 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1530 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1531 impl->icon_size, NULL);
1533 else if (gtk_file_system_path_is_local (impl->file_system, path))
1535 struct ShortcutsInsertRequest *request;
1536 GtkFileSystemHandle *handle;
1539 request = g_new0 (struct ShortcutsInsertRequest, 1);
1540 request->impl = g_object_ref (impl);
1541 request->path = gtk_file_path_copy (path);
1542 request->name_only = TRUE;
1543 request->removable = removable;
1545 request->type = type;
1547 request->label_copy = g_strdup (label);
1550 gtk_list_store_append (impl->shortcuts_model, &iter);
1552 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1554 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1555 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1556 gtk_tree_path_free (p);
1558 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1559 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1560 get_file_info_finished, request);
1562 gtk_list_store_set (impl->shortcuts_model, &iter,
1563 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1564 SHORTCUTS_COL_IS_VOLUME, is_volume,
1565 SHORTCUTS_COL_HANDLE, handle,
1568 shortcuts_update_count (impl, type, 1);
1574 /* Don't call get_info for remote paths to avoid latency and
1577 data = gtk_file_path_copy (path);
1579 label_copy = g_strdup (label);
1584 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1586 label_copy = _gtk_file_chooser_label_for_uri (uri);
1591 /* If we switch to a better bookmarks file format (XBEL), we
1592 * should use mime info to get a better icon.
1594 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1595 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1596 impl->icon_size, 0, NULL);
1600 gtk_list_store_append (impl->shortcuts_model, &iter);
1602 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1604 shortcuts_update_count (impl, type, 1);
1606 gtk_list_store_set (impl->shortcuts_model, &iter,
1607 SHORTCUTS_COL_PIXBUF, pixbuf,
1608 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1609 SHORTCUTS_COL_NAME, label_copy,
1610 SHORTCUTS_COL_DATA, data,
1611 SHORTCUTS_COL_IS_VOLUME, is_volume,
1612 SHORTCUTS_COL_REMOVABLE, removable,
1613 SHORTCUTS_COL_HANDLE, NULL,
1616 if (impl->shortcuts_filter_model)
1617 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1619 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1621 /* The current folder is updated via _activate_iter(), don't
1622 * have save_folder_combo_changed_cb() call _activate_iter()
1625 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1626 g_signal_handlers_block_by_func (impl->save_folder_combo,
1627 G_CALLBACK (save_folder_combo_changed_cb),
1629 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1630 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1631 G_CALLBACK (save_folder_combo_changed_cb),
1635 g_free (label_copy);
1638 g_object_unref (pixbuf);
1640 profile_end ("end", NULL);
1643 /* Appends an item for the user's home directory to the shortcuts model */
1645 shortcuts_append_home (GtkFileChooserDefault *impl)
1648 GtkFilePath *home_path;
1650 profile_start ("start", NULL);
1652 home = g_get_home_dir ();
1655 profile_end ("end - no home directory!?", NULL);
1659 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1661 shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1663 gtk_file_path_free (home_path);
1665 profile_end ("end", NULL);
1668 /* Appends the ~/Desktop directory to the shortcuts model */
1670 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1676 profile_start ("start", NULL);
1679 name = _gtk_file_system_win32_get_desktop ();
1681 home = g_get_home_dir ();
1684 profile_end ("end - no home directory!?", NULL);
1688 name = g_build_filename (home, "Desktop", NULL);
1691 path = gtk_file_system_filename_to_path (impl->file_system, name);
1694 shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1695 /* We do not actually pop up an error dialog if there is no desktop directory
1696 * because some people may really not want to have one.
1699 gtk_file_path_free (path);
1701 profile_end ("end", NULL);
1704 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1706 shortcuts_append_paths (GtkFileChooserDefault *impl,
1713 profile_start ("start", NULL);
1715 /* As there is no separator now, we want to start there.
1717 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1720 for (; paths; paths = paths->next)
1726 if (impl->local_only &&
1727 !gtk_file_system_path_is_local (impl->file_system, path))
1730 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1732 /* NULL GError, but we don't really want to show error boxes here */
1733 shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1739 profile_end ("end", NULL);
1741 return num_inserted;
1744 /* Returns the index for the corresponding item in the shortcuts bar */
1746 shortcuts_get_index (GtkFileChooserDefault *impl,
1747 ShortcutsIndex where)
1753 if (where == SHORTCUTS_HOME)
1756 n += impl->has_home ? 1 : 0;
1758 if (where == SHORTCUTS_DESKTOP)
1761 n += impl->has_desktop ? 1 : 0;
1763 if (where == SHORTCUTS_VOLUMES)
1766 n += impl->num_volumes;
1768 if (where == SHORTCUTS_SHORTCUTS)
1771 n += impl->num_shortcuts;
1773 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1776 /* If there are no bookmarks there won't be a separator */
1777 n += (impl->num_bookmarks > 0) ? 1 : 0;
1779 if (where == SHORTCUTS_BOOKMARKS)
1782 n += impl->num_bookmarks;
1784 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1789 if (where == SHORTCUTS_CURRENT_FOLDER)
1792 g_assert_not_reached ();
1799 /* Adds all the file system volumes to the shortcuts model */
1801 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1806 gboolean old_changing_folders;
1808 profile_start ("start", NULL);
1811 old_changing_folders = impl->changing_folder;
1812 impl->changing_folder = TRUE;
1814 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1815 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1816 impl->num_volumes = 0;
1818 list = gtk_file_system_list_volumes (impl->file_system);
1822 for (l = list; l; l = l->next)
1824 GtkFileSystemVolume *volume;
1828 if (impl->local_only)
1830 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
1832 GtkFilePath *base_path;
1834 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1835 if (base_path != NULL)
1837 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1838 gtk_file_path_free (base_path);
1842 gtk_file_system_volume_free (impl->file_system, volume);
1849 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
1853 impl->num_volumes = n;
1854 g_slist_free (list);
1856 if (impl->shortcuts_filter_model)
1857 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1859 impl->changing_folder = old_changing_folders;
1861 profile_end ("end", NULL);
1864 /* Inserts a separator node in the shortcuts list */
1866 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1867 ShortcutsIndex where)
1871 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1873 gtk_list_store_insert (impl->shortcuts_model, &iter,
1874 shortcuts_get_index (impl, where));
1875 gtk_list_store_set (impl->shortcuts_model, &iter,
1876 SHORTCUTS_COL_PIXBUF, NULL,
1877 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1878 SHORTCUTS_COL_NAME, NULL,
1879 SHORTCUTS_COL_DATA, NULL,
1883 /* Updates the list of bookmarks */
1885 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1888 gboolean old_changing_folders;
1890 GtkFilePath *list_selected = NULL;
1891 GtkFilePath *combo_selected = NULL;
1895 profile_start ("start", NULL);
1897 old_changing_folders = impl->changing_folder;
1898 impl->changing_folder = TRUE;
1900 if (shortcuts_get_selected (impl, &iter))
1902 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1904 SHORTCUTS_COL_DATA, &col_data,
1905 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1908 if (col_data && !is_volume)
1909 list_selected = gtk_file_path_copy (col_data);
1912 if (impl->save_folder_combo &&
1913 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1916 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1918 SHORTCUTS_COL_DATA, &col_data,
1919 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1922 if (col_data && !is_volume)
1923 combo_selected = gtk_file_path_copy (col_data);
1926 if (impl->num_bookmarks > 0)
1927 shortcuts_remove_rows (impl,
1928 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1929 impl->num_bookmarks + 1);
1931 impl->num_bookmarks = 0;
1933 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1934 shortcuts_append_paths (impl, bookmarks);
1935 gtk_file_paths_free (bookmarks);
1937 if (impl->num_bookmarks > 0)
1938 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1940 if (impl->shortcuts_filter_model)
1941 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1945 shortcuts_find_folder (impl, list_selected);
1946 gtk_file_path_free (list_selected);
1953 pos = shortcut_find_position (impl, combo_selected);
1955 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1957 gtk_file_path_free (combo_selected);
1960 impl->changing_folder = old_changing_folders;
1962 profile_end ("end", NULL);
1965 /* Appends a separator and a row to the shortcuts list for the current folder */
1967 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1972 g_assert (!impl->shortcuts_current_folder_active);
1976 g_assert (impl->current_folder != NULL);
1978 pos = shortcut_find_position (impl, impl->current_folder);
1981 GtkFileSystemVolume *volume;
1982 GtkFilePath *base_path;
1986 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1990 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1992 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1994 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1999 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
2001 shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2005 shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2007 gtk_file_system_volume_free (impl->file_system, volume);
2011 gtk_file_path_free (base_path);
2013 else if (impl->save_folder_combo != NULL)
2014 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2017 /* Updates the current folder row in the shortcuts model */
2019 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2023 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2025 if (impl->shortcuts_current_folder_active)
2027 shortcuts_remove_rows (impl, pos, 2);
2028 impl->shortcuts_current_folder_active = FALSE;
2031 shortcuts_add_current_folder (impl);
2034 /* Filter function used for the shortcuts filter model */
2036 shortcuts_filter_cb (GtkTreeModel *model,
2040 GtkFileChooserDefault *impl;
2044 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2046 path = gtk_tree_model_get_path (model, iter);
2050 pos = *gtk_tree_path_get_indices (path);
2051 gtk_tree_path_free (path);
2053 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2056 /* Creates the list model for shortcuts */
2058 shortcuts_model_create (GtkFileChooserDefault *impl)
2060 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2061 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2062 GDK_TYPE_PIXBUF, /* pixbuf */
2063 G_TYPE_STRING, /* name */
2064 G_TYPE_POINTER, /* path or volume */
2065 G_TYPE_BOOLEAN, /* is the previous column a volume? */
2066 G_TYPE_BOOLEAN, /* removable */
2067 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2068 G_TYPE_POINTER); /* GtkFileSystemHandle */
2070 if (impl->file_system)
2072 shortcuts_append_home (impl);
2073 shortcuts_append_desktop (impl);
2074 shortcuts_add_volumes (impl);
2077 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
2078 GTK_TREE_MODEL (impl->shortcuts_model),
2081 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2082 shortcuts_filter_cb,
2087 /* Callback used when the "New Folder" button is clicked */
2089 new_folder_button_clicked (GtkButton *button,
2090 GtkFileChooserDefault *impl)
2095 if (!impl->browse_files_model)
2096 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2098 /* Prevent button from being clicked twice */
2099 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2101 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2103 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2104 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2105 path, impl->list_name_column,
2108 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2109 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2111 impl->list_name_column,
2114 gtk_tree_path_free (path);
2118 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2119 const GtkFilePath *path,
2120 const GError *error,
2123 gboolean cancelled = handle->cancelled;
2124 GtkFileChooserDefault *impl = data;
2126 if (!g_slist_find (impl->pending_handles, handle))
2129 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2135 change_folder_and_display_error (impl, path);
2137 error_creating_folder_dialog (impl, path, g_error_copy (error));
2140 g_object_unref (impl);
2141 g_object_unref (handle);
2144 /* Idle handler for creating a new folder after editing its name cell, or for
2145 * canceling the editing.
2148 edited_idle_cb (GtkFileChooserDefault *impl)
2150 GDK_THREADS_ENTER ();
2152 g_source_destroy (impl->edited_idle);
2153 impl->edited_idle = NULL;
2155 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2156 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2158 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2160 if (impl->edited_new_text) /* not cancelled? */
2163 GtkFilePath *file_path;
2166 file_path = gtk_file_system_make_path (impl->file_system,
2167 impl->current_folder,
2168 impl->edited_new_text,
2172 GtkFileSystemHandle *handle;
2174 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2175 edited_idle_create_folder_cb,
2176 g_object_ref (impl));
2177 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2179 gtk_file_path_free (file_path);
2182 error_creating_folder_dialog (impl, file_path, error);
2184 g_free (impl->edited_new_text);
2185 impl->edited_new_text = NULL;
2188 GDK_THREADS_LEAVE ();
2194 queue_edited_idle (GtkFileChooserDefault *impl,
2195 const gchar *new_text)
2197 /* We create the folder in an idle handler so that we don't modify the tree
2201 if (!impl->edited_idle)
2203 impl->edited_idle = g_idle_source_new ();
2204 g_source_set_closure (impl->edited_idle,
2205 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2207 g_source_attach (impl->edited_idle, NULL);
2210 g_free (impl->edited_new_text);
2211 impl->edited_new_text = g_strdup (new_text);
2214 /* Callback used from the text cell renderer when the new folder is named */
2216 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2218 const gchar *new_text,
2219 GtkFileChooserDefault *impl)
2221 /* work around bug #154921 */
2222 g_object_set (cell_renderer_text,
2223 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2224 queue_edited_idle (impl, new_text);
2227 /* Callback used from the text cell renderer when the new folder edition gets
2231 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2232 GtkFileChooserDefault *impl)
2234 /* work around bug #154921 */
2235 g_object_set (cell_renderer_text,
2236 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2237 queue_edited_idle (impl, NULL);
2240 /* Creates the widgets for the filter combo box */
2242 filter_create (GtkFileChooserDefault *impl)
2244 impl->filter_combo = gtk_combo_box_new_text ();
2245 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2247 g_signal_connect (impl->filter_combo, "changed",
2248 G_CALLBACK (filter_combo_changed), impl);
2250 return impl->filter_combo;
2254 button_new (GtkFileChooserDefault *impl,
2256 const char *stock_id,
2264 button = gtk_button_new_with_mnemonic (text);
2265 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2266 gtk_button_set_image (GTK_BUTTON (button), image);
2268 gtk_widget_set_sensitive (button, sensitive);
2269 g_signal_connect (button, "clicked", callback, impl);
2272 gtk_widget_show (button);
2277 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2279 shortcut_find_position (GtkFileChooserDefault *impl,
2280 const GtkFilePath *path)
2284 int current_folder_separator_idx;
2286 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2289 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2292 /* FIXME: is this still needed? */
2293 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2297 for (i = 0; i < current_folder_separator_idx; i++)
2302 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2303 SHORTCUTS_COL_DATA, &col_data,
2304 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2311 GtkFileSystemVolume *volume;
2312 GtkFilePath *base_path;
2316 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2318 exists = base_path && strcmp (gtk_file_path_get_string (path),
2319 gtk_file_path_get_string (base_path)) == 0;
2327 GtkFilePath *model_path;
2329 model_path = col_data;
2331 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2336 if (i < current_folder_separator_idx - 1)
2338 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2339 g_assert_not_reached ();
2346 /* Tries to add a bookmark from a path name */
2348 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2349 const GtkFilePath *path,
2354 g_return_val_if_fail (path != NULL, FALSE);
2356 if (shortcut_find_position (impl, path) != -1)
2360 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2362 error_adding_bookmark_dialog (impl, path, error);
2370 add_bookmark_foreach_cb (GtkTreeModel *model,
2375 GtkFileChooserDefault *impl;
2376 GtkFileSystemModel *fs_model;
2377 GtkTreeIter child_iter;
2378 const GtkFilePath *file_path;
2380 impl = (GtkFileChooserDefault *) data;
2382 fs_model = impl->browse_files_model;
2383 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2385 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2386 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2389 /* Adds a bookmark from the currently selected item in the file list */
2391 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2393 GtkTreeSelection *selection;
2395 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2397 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2398 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2400 gtk_tree_selection_selected_foreach (selection,
2401 add_bookmark_foreach_cb,
2405 /* Callback used when the "Add bookmark" button is clicked */
2407 add_bookmark_button_clicked_cb (GtkButton *button,
2408 GtkFileChooserDefault *impl)
2410 bookmarks_add_selected_folder (impl);
2413 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2414 * returns FALSE if no shortcut is selected.
2417 shortcuts_get_selected (GtkFileChooserDefault *impl,
2420 GtkTreeSelection *selection;
2421 GtkTreeIter parent_iter;
2423 if (!impl->browse_shortcuts_tree_view)
2426 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2428 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2431 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2437 /* Removes the selected bookmarks */
2439 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2447 if (!shortcuts_get_selected (impl, &iter))
2450 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2451 SHORTCUTS_COL_DATA, &col_data,
2452 SHORTCUTS_COL_REMOVABLE, &removable,
2454 g_assert (col_data != NULL);
2462 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2463 error_removing_bookmark_dialog (impl, path, error);
2466 /* Callback used when the "Remove bookmark" button is clicked */
2468 remove_bookmark_button_clicked_cb (GtkButton *button,
2469 GtkFileChooserDefault *impl)
2471 remove_selected_bookmarks (impl);
2474 struct selection_check_closure {
2475 GtkFileChooserDefault *impl;
2478 gboolean all_folders;
2481 /* Used from gtk_tree_selection_selected_foreach() */
2483 selection_check_foreach_cb (GtkTreeModel *model,
2488 struct selection_check_closure *closure;
2489 GtkTreeIter child_iter;
2490 const GtkFileInfo *info;
2494 closure->num_selected++;
2496 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2498 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2499 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2501 closure->all_folders = closure->all_folders && is_folder;
2502 closure->all_files = closure->all_files && !is_folder;
2505 /* Checks whether the selected items in the file list are all files or all folders */
2507 selection_check (GtkFileChooserDefault *impl,
2509 gboolean *all_files,
2510 gboolean *all_folders)
2512 struct selection_check_closure closure;
2513 GtkTreeSelection *selection;
2515 closure.impl = impl;
2516 closure.num_selected = 0;
2517 closure.all_files = TRUE;
2518 closure.all_folders = TRUE;
2520 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2521 gtk_tree_selection_selected_foreach (selection,
2522 selection_check_foreach_cb,
2525 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2528 *num_selected = closure.num_selected;
2531 *all_files = closure.all_files;
2534 *all_folders = closure.all_folders;
2537 struct get_selected_path_closure {
2538 GtkFileChooserDefault *impl;
2539 const GtkFilePath *path;
2543 get_selected_path_foreach_cb (GtkTreeModel *model,
2548 struct get_selected_path_closure *closure;
2549 GtkTreeIter child_iter;
2553 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2554 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2557 /* Returns a selected path from the file list */
2558 static const GtkFilePath *
2559 get_selected_path (GtkFileChooserDefault *impl)
2561 struct get_selected_path_closure closure;
2562 GtkTreeSelection *selection;
2564 closure.impl = impl;
2565 closure.path = NULL;
2567 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2568 gtk_tree_selection_selected_foreach (selection,
2569 get_selected_path_foreach_cb,
2572 return closure.path;
2576 GtkFileChooserDefault *impl;
2578 } UpdateTooltipData;
2581 update_tooltip (GtkTreeModel *model,
2586 UpdateTooltipData *udata = data;
2587 GtkTreeIter child_iter;
2588 const GtkFileInfo *info;
2590 if (udata->tip == NULL)
2592 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2596 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2597 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2598 gtk_file_info_get_display_name (info));
2603 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2604 * if there are no selected items *and* the current folder is not in the
2605 * bookmarks list. De-sensitize the button otherwise.
2608 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2611 gboolean all_folders;
2615 selection_check (impl, &num_selected, NULL, &all_folders);
2617 if (num_selected == 0)
2618 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2619 else if (num_selected == 1)
2621 const GtkFilePath *path;
2623 path = get_selected_path (impl);
2624 active = all_folders && (shortcut_find_position (impl, path) == -1);
2627 active = all_folders;
2629 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2631 if (impl->browse_files_popup_menu_add_shortcut_item)
2632 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2633 (num_selected == 0) ? FALSE : active);
2637 if (num_selected == 0)
2638 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2639 else if (num_selected > 1)
2640 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2643 GtkTreeSelection *selection;
2644 UpdateTooltipData data;
2646 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2649 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2653 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2658 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2659 * bookmark row is selected in the shortcuts tree.
2662 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2665 gboolean removable = FALSE;
2668 if (shortcuts_get_selected (impl, &iter))
2669 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2670 SHORTCUTS_COL_REMOVABLE, &removable,
2671 SHORTCUTS_COL_NAME, &name,
2674 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2680 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2681 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2690 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2693 gboolean removable = FALSE;
2695 if (impl->browse_shortcuts_popup_menu == NULL)
2698 if (shortcuts_get_selected (impl, &iter))
2699 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2700 SHORTCUTS_COL_REMOVABLE, &removable,
2703 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2704 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2707 /* GtkWidget::drag-begin handler for the shortcuts list. */
2709 shortcuts_drag_begin_cb (GtkWidget *widget,
2710 GdkDragContext *context,
2711 GtkFileChooserDefault *impl)
2714 impl->shortcuts_drag_context = g_object_ref (context);
2719 /* Removes the idle handler for outside drags */
2721 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2723 if (!impl->shortcuts_drag_outside_idle)
2726 g_source_destroy (impl->shortcuts_drag_outside_idle);
2727 impl->shortcuts_drag_outside_idle = NULL;
2731 /* GtkWidget::drag-end handler for the shortcuts list. */
2733 shortcuts_drag_end_cb (GtkWidget *widget,
2734 GdkDragContext *context,
2735 GtkFileChooserDefault *impl)
2738 g_object_unref (impl->shortcuts_drag_context);
2740 shortcuts_cancel_drag_outside_idle (impl);
2742 if (!impl->shortcuts_drag_outside)
2745 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2747 impl->shortcuts_drag_outside = FALSE;
2751 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2753 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2754 GdkDragContext *context,
2755 GtkFileChooserDefault *impl)
2757 g_signal_stop_emission_by_name (widget, "drag_data_delete");
2761 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2765 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2768 GtkTreeView *tree_view;
2771 GdkPixmap *row_pixmap;
2776 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2778 /* Find the selected path and get its drag pixmap */
2780 if (!shortcuts_get_selected (impl, &iter))
2781 g_assert_not_reached ();
2783 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2785 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2786 gtk_tree_path_free (path);
2795 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2801 GdkPixmap *composite;
2802 int row_pixmap_width, row_pixmap_height;
2803 int pixbuf_width, pixbuf_height;
2804 int composite_width, composite_height;
2805 int pixbuf_x, pixbuf_y;
2806 GdkGC *gc, *mask_gc;
2808 GdkBitmap *pixbuf_mask;
2810 /* Create pixmap and mask for composite image */
2812 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2813 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2814 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2816 composite_width = MAX (row_pixmap_width, pixbuf_width);
2817 composite_height = MAX (row_pixmap_height, pixbuf_height);
2819 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2821 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2824 pixbuf_x = composite_width - pixbuf_width;
2826 pixbuf_y = (composite_height - pixbuf_height) / 2;
2828 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2829 gc = gdk_gc_new (composite);
2831 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2832 mask_gc = gdk_gc_new (mask);
2834 gdk_gc_set_foreground (mask_gc, &color);
2835 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2838 color.green = 0xffff;
2839 color.blue = 0xffff;
2840 gdk_gc_set_rgb_fg_color (gc, &color);
2841 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2843 /* Composite the row pixmap and the pixbuf */
2845 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2847 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2848 NULL, &pixbuf_mask, 128);
2849 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2852 pixbuf_width, pixbuf_height);
2853 g_object_unref (pixbuf_mask);
2855 gdk_draw_drawable (composite, gc, row_pixmap,
2858 row_pixmap_width, row_pixmap_height);
2860 gdk_gc_set_foreground (mask_gc, &color);
2861 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2863 gdk_draw_pixbuf (composite, gc, pixbuf,
2866 pixbuf_width, pixbuf_height,
2870 g_object_unref (pixbuf);
2871 g_object_unref (row_pixmap);
2873 row_pixmap = composite;
2877 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2879 gtk_tree_view_get_path_at_pos (tree_view,
2880 tree_view->priv->press_start_x,
2881 tree_view->priv->press_start_y,
2887 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2888 gdk_drawable_get_colormap (row_pixmap),
2891 tree_view->priv->press_start_x + 1,
2892 row_pixmap_y + cell_y + 1);
2894 g_object_unref (row_pixmap);
2896 g_object_unref (mask);
2899 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2900 * handler so that we can tell apart the drag_leave event that comes right
2901 * before a drag_drop, from a normal drag_leave. We don't want to set the
2902 * cursor nor the flag in the latter case.
2905 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2907 GDK_THREADS_ENTER ();
2909 shortcuts_drag_set_delete_cursor (impl, TRUE);
2910 impl->shortcuts_drag_outside = TRUE;
2912 shortcuts_cancel_drag_outside_idle (impl);
2914 GDK_THREADS_LEAVE ();
2920 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2924 shortcuts_drag_leave_cb (GtkWidget *widget,
2925 GdkDragContext *context,
2927 GtkFileChooserDefault *impl)
2930 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2932 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2933 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2934 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2936 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2940 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2942 GTK_TREE_VIEW_DROP_BEFORE);
2944 g_signal_stop_emission_by_name (widget, "drag_leave");
2947 /* Computes the appropriate row and position for dropping */
2949 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2953 GtkTreeViewDropPosition *pos)
2955 GtkTreeView *tree_view;
2956 GtkTreeViewColumn *column;
2960 int bookmarks_index;
2962 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2964 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2966 if (!gtk_tree_view_get_path_at_pos (tree_view,
2968 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2974 row = bookmarks_index + impl->num_bookmarks - 1;
2975 *path = gtk_tree_path_new_from_indices (row, -1);
2976 *pos = GTK_TREE_VIEW_DROP_AFTER;
2980 row = *gtk_tree_path_get_indices (*path);
2981 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2982 gtk_tree_path_free (*path);
2984 if (row < bookmarks_index)
2986 row = bookmarks_index;
2987 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2989 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2991 row = bookmarks_index + impl->num_bookmarks - 1;
2992 *pos = GTK_TREE_VIEW_DROP_AFTER;
2996 if (cell_y < cell.height / 2)
2997 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2999 *pos = GTK_TREE_VIEW_DROP_AFTER;
3002 *path = gtk_tree_path_new_from_indices (row, -1);
3005 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3006 * implement the destination side of DnD by hand, due to limitations in
3007 * GtkTreeView's DnD API.
3010 shortcuts_drag_motion_cb (GtkWidget *widget,
3011 GdkDragContext *context,
3015 GtkFileChooserDefault *impl)
3018 GtkTreeViewDropPosition pos;
3019 GdkDragAction action;
3022 if (gtk_drag_get_source_widget (context) == widget)
3024 shortcuts_cancel_drag_outside_idle (impl);
3026 if (impl->shortcuts_drag_outside)
3028 shortcuts_drag_set_delete_cursor (impl, FALSE);
3029 impl->shortcuts_drag_outside = FALSE;
3034 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
3035 action = GDK_ACTION_COPY;
3036 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
3037 action = GDK_ACTION_MOVE;
3044 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3045 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3046 gtk_tree_path_free (path);
3050 g_signal_stop_emission_by_name (widget, "drag_motion");
3054 gdk_drag_status (context, action, time_);
3061 /* GtkWidget::drag-drop handler for the shortcuts list. */
3063 shortcuts_drag_drop_cb (GtkWidget *widget,
3064 GdkDragContext *context,
3068 GtkFileChooserDefault *impl)
3071 shortcuts_cancel_drag_outside_idle (impl);
3074 g_signal_stop_emission_by_name (widget, "drag_drop");
3078 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3080 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3087 uris = g_uri_list_extract_uris (data);
3089 for (i = 0; uris[i]; i++)
3095 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3099 if (shortcuts_add_bookmark_from_path (impl, path, position))
3102 gtk_file_path_free (path);
3108 g_set_error (&error,
3109 GTK_FILE_CHOOSER_ERROR,
3110 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3111 _("Could not add a bookmark for '%s' "
3112 "because it is an invalid path name."),
3114 error_adding_bookmark_dialog (impl, path, error);
3121 /* Reorders the selected bookmark to the specified position */
3123 shortcuts_reorder (GtkFileChooserDefault *impl,
3131 int bookmarks_index;
3132 const GtkFilePath *file_path;
3133 GtkFilePath *file_path_copy;
3137 /* Get the selected path */
3139 if (!shortcuts_get_selected (impl, &iter))
3140 g_assert_not_reached ();
3142 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3143 old_position = *gtk_tree_path_get_indices (path);
3144 gtk_tree_path_free (path);
3146 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3147 old_position -= bookmarks_index;
3148 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3150 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3151 SHORTCUTS_COL_NAME, &name,
3152 SHORTCUTS_COL_DATA, &col_data,
3153 SHORTCUTS_COL_IS_VOLUME, &is_volume,
3155 g_assert (col_data != NULL);
3156 g_assert (!is_volume);
3158 file_path = col_data;
3159 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3161 /* Remove the path from the old position and insert it in the new one */
3163 if (new_position > old_position)
3166 if (old_position == new_position)
3170 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3172 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3173 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3176 error_adding_bookmark_dialog (impl, file_path_copy, error);
3180 gtk_file_path_free (file_path_copy);
3183 /* Callback used when we get the drag data for the bookmarks list. We add the
3184 * received URIs as bookmarks if they are folders.
3187 shortcuts_drag_data_received_cb (GtkWidget *widget,
3188 GdkDragContext *context,
3191 GtkSelectionData *selection_data,
3196 GtkFileChooserDefault *impl;
3197 GtkTreePath *tree_path;
3198 GtkTreeViewDropPosition tree_pos;
3200 int bookmarks_index;
3202 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3204 /* Compute position */
3206 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3208 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3209 position = *gtk_tree_path_get_indices (tree_path);
3210 gtk_tree_path_free (tree_path);
3212 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3215 g_assert (position >= bookmarks_index);
3216 position -= bookmarks_index;
3218 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3219 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3220 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3221 shortcuts_reorder (impl, position);
3223 g_signal_stop_emission_by_name (widget, "drag_data_received");
3226 /* Callback used when the selection in the shortcuts tree changes */
3228 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3229 GtkFileChooserDefault *impl)
3231 bookmarks_check_remove_sensitivity (impl);
3232 shortcuts_check_popup_sensitivity (impl);
3236 shortcuts_row_separator_func (GtkTreeModel *model,
3240 gint column = GPOINTER_TO_INT (data);
3243 gtk_tree_model_get (model, iter, column, &text, -1);
3253 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3254 * keypresses before the TreeView gets them.
3257 tree_view_keybinding_cb (GtkWidget *tree_view,
3259 GtkFileChooserDefault *impl)
3261 if ((event->keyval == GDK_slash
3262 || event->keyval == GDK_KP_Divide
3264 || event->keyval == GDK_asciitilde
3266 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3268 location_popup_handler (impl, event->string);
3275 /* Callback used when the file list's popup menu is detached */
3277 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3280 GtkFileChooserDefault *impl;
3282 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3283 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3285 impl->browse_shortcuts_popup_menu = NULL;
3286 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3287 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3291 remove_shortcut_cb (GtkMenuItem *item,
3292 GtkFileChooserDefault *impl)
3294 remove_selected_bookmarks (impl);
3297 /* Rename the selected bookmark */
3299 rename_selected_bookmark (GtkFileChooserDefault *impl)
3303 GtkTreeViewColumn *column;
3304 GtkCellRenderer *cell;
3307 if (shortcuts_get_selected (impl, &iter))
3309 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3310 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3311 renderers = gtk_tree_view_column_get_cell_renderers (column);
3312 cell = g_list_nth_data (renderers, 1);
3313 g_list_free (renderers);
3314 g_object_set (cell, "editable", TRUE, NULL);
3315 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3316 path, column, cell, TRUE);
3317 gtk_tree_path_free (path);
3322 rename_shortcut_cb (GtkMenuItem *item,
3323 GtkFileChooserDefault *impl)
3325 rename_selected_bookmark (impl);
3328 /* Constructs the popup menu for the file list if needed */
3330 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3334 if (impl->browse_shortcuts_popup_menu)
3337 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3338 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3339 impl->browse_shortcuts_tree_view,
3340 shortcuts_popup_menu_detach_cb);
3342 item = gtk_image_menu_item_new_with_label (_("Remove"));
3343 impl->browse_shortcuts_popup_menu_remove_item = item;
3344 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3345 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3346 g_signal_connect (item, "activate",
3347 G_CALLBACK (remove_shortcut_cb), impl);
3348 gtk_widget_show (item);
3349 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3351 item = gtk_menu_item_new_with_label (_("Rename..."));
3352 impl->browse_shortcuts_popup_menu_rename_item = item;
3353 g_signal_connect (item, "activate",
3354 G_CALLBACK (rename_shortcut_cb), impl);
3355 gtk_widget_show (item);
3356 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3358 shortcuts_check_popup_sensitivity (impl);
3362 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3364 shortcuts_build_popup_menu (impl);
3368 popup_position_func (GtkMenu *menu,
3372 gpointer user_data);
3375 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3376 GdkEventButton *event)
3378 shortcuts_update_popup_menu (impl);
3380 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3381 NULL, NULL, NULL, NULL,
3382 event->button, event->time);
3385 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3387 popup_position_func, impl->browse_shortcuts_tree_view,
3388 0, GDK_CURRENT_TIME);
3389 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3394 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3396 shortcuts_popup_menu_cb (GtkWidget *widget,
3397 GtkFileChooserDefault *impl)
3399 shortcuts_popup_menu (impl, NULL);
3403 /* Callback used when a button is pressed on the shortcuts list.
3404 * We trap button 3 to bring up a popup menu.
3407 shortcuts_button_press_event_cb (GtkWidget *widget,
3408 GdkEventButton *event,
3409 GtkFileChooserDefault *impl)
3411 static gboolean in_press = FALSE;
3417 if (event->button != 3)
3421 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3427 shortcuts_popup_menu (impl, event);
3432 shortcuts_edited (GtkCellRenderer *cell,
3435 GtkFileChooserDefault *impl)
3439 GtkFilePath *shortcut;
3441 g_object_set (cell, "editable", FALSE, NULL);
3443 path = gtk_tree_path_new_from_string (path_string);
3444 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3445 g_assert_not_reached ();
3447 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3448 SHORTCUTS_COL_DATA, &shortcut,
3450 gtk_tree_path_free (path);
3452 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3456 shortcuts_editing_canceled (GtkCellRenderer *cell,
3457 GtkFileChooserDefault *impl)
3459 g_object_set (cell, "editable", FALSE, NULL);
3462 /* Creates the widgets for the shortcuts and bookmarks tree */
3464 shortcuts_list_create (GtkFileChooserDefault *impl)
3467 GtkTreeSelection *selection;
3468 GtkTreeViewColumn *column;
3469 GtkCellRenderer *renderer;
3471 /* Scrolled window */
3473 swin = gtk_scrolled_window_new (NULL, NULL);
3474 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3475 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3476 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3478 gtk_widget_show (swin);
3482 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3483 #ifdef PROFILE_FILE_CHOOSER
3484 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3486 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3487 G_CALLBACK (tree_view_keybinding_cb), impl);
3488 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3489 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3490 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3491 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3492 /* Accessible object name for the file chooser's shortcuts pane */
3493 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3495 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3497 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3499 shortcuts_source_targets,
3500 num_shortcuts_source_targets,
3503 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3504 GTK_DEST_DEFAULT_ALL,
3505 shortcuts_dest_targets,
3506 num_shortcuts_dest_targets,
3507 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3509 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3510 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3511 gtk_tree_selection_set_select_function (selection,
3512 shortcuts_select_func,
3515 g_signal_connect (selection, "changed",
3516 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3518 g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3519 G_CALLBACK (shortcuts_row_activated_cb), impl);
3521 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3522 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3524 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3525 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3526 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3527 G_CALLBACK (shortcuts_drag_end_cb), impl);
3528 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3529 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3531 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3532 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3533 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3534 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3535 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3536 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3537 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3538 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3540 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3541 gtk_widget_show (impl->browse_shortcuts_tree_view);
3545 column = gtk_tree_view_column_new ();
3546 /* Column header for the file chooser's shortcuts pane */
3547 gtk_tree_view_column_set_title (column, _("_Places"));
3549 renderer = gtk_cell_renderer_pixbuf_new ();
3550 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3551 gtk_tree_view_column_set_attributes (column, renderer,
3552 "pixbuf", SHORTCUTS_COL_PIXBUF,
3553 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3556 renderer = gtk_cell_renderer_text_new ();
3557 g_signal_connect (renderer, "edited",
3558 G_CALLBACK (shortcuts_edited), impl);
3559 g_signal_connect (renderer, "editing-canceled",
3560 G_CALLBACK (shortcuts_editing_canceled), impl);
3561 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3562 gtk_tree_view_column_set_attributes (column, renderer,
3563 "text", SHORTCUTS_COL_NAME,
3566 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3567 shortcuts_row_separator_func,
3568 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3571 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3576 /* Creates the widgets for the shortcuts/bookmarks pane */
3578 shortcuts_pane_create (GtkFileChooserDefault *impl,
3579 GtkSizeGroup *size_group)
3585 vbox = gtk_vbox_new (FALSE, 6);
3586 gtk_widget_show (vbox);
3588 /* Shortcuts tree */
3590 widget = shortcuts_list_create (impl);
3591 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3593 /* Box for buttons */
3595 hbox = gtk_hbox_new (TRUE, 6);
3596 gtk_size_group_add_widget (size_group, hbox);
3597 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3598 gtk_widget_show (hbox);
3600 /* Add bookmark button */
3602 impl->browse_shortcuts_add_button = button_new (impl,
3607 G_CALLBACK (add_bookmark_button_clicked_cb));
3608 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3609 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3610 _("Add the selected folder to the Bookmarks"), NULL);
3612 /* Remove bookmark button */
3614 impl->browse_shortcuts_remove_button = button_new (impl,
3619 G_CALLBACK (remove_bookmark_button_clicked_cb));
3620 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3621 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3622 _("Remove the selected bookmark"), NULL);
3627 /* Handles key press events on the file list, so that we can trap Enter to
3628 * activate the default button on our own. Also, checks to see if '/' has been
3629 * pressed. See comment by tree_view_keybinding_cb() for more details.
3632 trap_activate_cb (GtkWidget *widget,
3636 GtkFileChooserDefault *impl;
3639 impl = (GtkFileChooserDefault *) data;
3641 modifiers = gtk_accelerator_get_default_mod_mask ();
3643 if ((event->keyval == GDK_slash
3644 || event->keyval == GDK_KP_Divide
3646 || event->keyval == GDK_asciitilde
3648 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3650 location_popup_handler (impl, event->string);
3654 if ((event->keyval == GDK_Return
3655 || event->keyval == GDK_ISO_Enter
3656 || event->keyval == GDK_KP_Enter
3657 || event->keyval == GDK_space)
3658 && ((event->state & modifiers) == 0)
3659 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3660 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3664 window = get_toplevel (widget);
3666 && widget != window->default_widget
3667 && !(widget == window->focus_widget &&
3668 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3670 gtk_window_activate_default (window);
3678 /* Callback used when the file list's popup menu is detached */
3680 popup_menu_detach_cb (GtkWidget *attach_widget,
3683 GtkFileChooserDefault *impl;
3685 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3686 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3688 impl->browse_files_popup_menu = NULL;
3689 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3690 impl->browse_files_popup_menu_hidden_files_item = NULL;
3693 /* Callback used when the "Add to Bookmarks" menu item is activated */
3695 add_to_shortcuts_cb (GtkMenuItem *item,
3696 GtkFileChooserDefault *impl)
3698 bookmarks_add_selected_folder (impl);
3701 /* Callback used when the "Show Hidden Files" menu item is toggled */
3703 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3704 GtkFileChooserDefault *impl)
3707 "show-hidden", gtk_check_menu_item_get_active (item),
3711 /* Shows an error dialog about not being able to select a dragged file */
3713 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3714 const GtkFilePath *path,
3718 _("Could not select file"),
3723 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3728 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3730 for (i = 1; uris[i]; i++)
3735 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3739 GError *error = NULL;
3741 gtk_file_chooser_default_select_path (chooser, path, &error);
3743 error_selecting_dragged_file_dialog (impl, path, error);
3745 gtk_file_path_free (path);
3750 struct FileListDragData
3752 GtkFileChooserDefault *impl;
3758 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
3759 const GtkFileInfo *info,
3760 const GError *error,
3763 gboolean cancelled = handle->cancelled;
3764 struct FileListDragData *data = user_data;
3765 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3767 if (handle != data->impl->file_list_drag_data_received_handle)
3770 data->impl->file_list_drag_data_received_handle = NULL;
3772 if (cancelled || error)
3775 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3776 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3777 data->uris[1] == 0 && !error &&
3778 gtk_file_info_get_is_folder (info))
3779 change_folder_and_display_error (data->impl, data->path);
3782 GError *error = NULL;
3784 gtk_file_chooser_default_unselect_all (chooser);
3785 gtk_file_chooser_default_select_path (chooser, data->path, &error);
3787 error_selecting_dragged_file_dialog (data->impl, data->path, error);
3789 browse_files_center_selected_row (data->impl);
3792 if (data->impl->select_multiple)
3793 file_list_drag_data_select_uris (data->impl, data->uris);
3796 g_object_unref (data->impl);
3797 g_strfreev (data->uris);
3798 gtk_file_path_free (data->path);
3801 g_object_unref (handle);
3805 file_list_drag_data_received_cb (GtkWidget *widget,
3806 GdkDragContext *context,
3809 GtkSelectionData *selection_data,
3814 GtkFileChooserDefault *impl;
3815 GtkFileChooser *chooser;
3819 GError *error = NULL;
3821 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3822 chooser = GTK_FILE_CHOOSER (data);
3824 /* Parse the text/uri-list string, navigate to the first one */
3825 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
3829 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3833 struct FileListDragData *data;
3835 data = g_new0 (struct FileListDragData, 1);
3836 data->impl = g_object_ref (impl);
3840 if (impl->file_list_drag_data_received_handle)
3841 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
3843 impl->file_list_drag_data_received_handle =
3844 gtk_file_system_get_info (impl->file_system, path,
3845 GTK_FILE_INFO_IS_FOLDER,
3846 file_list_drag_data_received_get_info_cb,
3852 g_set_error (&error,
3853 GTK_FILE_CHOOSER_ERROR,
3854 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3855 _("Could not select file '%s' "
3856 "because it is an invalid path name."),
3858 error_selecting_dragged_file_dialog (impl, NULL, error);
3861 if (impl->select_multiple)
3862 file_list_drag_data_select_uris (impl, uris);
3868 g_signal_stop_emission_by_name (widget, "drag_data_received");
3871 /* Don't do anything with the drag_drop signal */
3873 file_list_drag_drop_cb (GtkWidget *widget,
3874 GdkDragContext *context,
3878 GtkFileChooserDefault *impl)
3880 g_signal_stop_emission_by_name (widget, "drag_drop");
3884 /* Disable the normal tree drag motion handler, it makes it look like you're
3885 dropping the dragged item onto a tree item */
3887 file_list_drag_motion_cb (GtkWidget *widget,
3888 GdkDragContext *context,
3892 GtkFileChooserDefault *impl)
3894 g_signal_stop_emission_by_name (widget, "drag_motion");
3898 /* Constructs the popup menu for the file list if needed */
3900 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3904 if (impl->browse_files_popup_menu)
3907 impl->browse_files_popup_menu = gtk_menu_new ();
3908 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3909 impl->browse_files_tree_view,
3910 popup_menu_detach_cb);
3912 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3913 impl->browse_files_popup_menu_add_shortcut_item = item;
3914 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3915 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3916 gtk_widget_set_sensitive (item, FALSE);
3917 g_signal_connect (item, "activate",
3918 G_CALLBACK (add_to_shortcuts_cb), impl);
3919 gtk_widget_show (item);
3920 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3922 item = gtk_separator_menu_item_new ();
3923 gtk_widget_show (item);
3924 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3926 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3927 impl->browse_files_popup_menu_hidden_files_item = item;
3928 g_signal_connect (item, "toggled",
3929 G_CALLBACK (show_hidden_toggled_cb), impl);
3930 gtk_widget_show (item);
3931 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3934 /* Updates the popup menu for the file list, creating it if necessary */
3936 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3938 file_list_build_popup_menu (impl);
3940 /* The sensitivity of the Add to Bookmarks item is set in
3941 * bookmarks_check_add_sensitivity()
3944 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3945 G_CALLBACK (show_hidden_toggled_cb), impl);
3946 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3948 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3949 G_CALLBACK (show_hidden_toggled_cb), impl);
3953 popup_position_func (GtkMenu *menu,
3959 GtkWidget *widget = GTK_WIDGET (user_data);
3960 GdkScreen *screen = gtk_widget_get_screen (widget);
3963 GdkRectangle monitor;
3965 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3967 gdk_window_get_origin (widget->window, x, y);
3969 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3971 *x += (widget->allocation.width - req.width) / 2;
3972 *y += (widget->allocation.height - req.height) / 2;
3974 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3975 gtk_menu_set_monitor (menu, monitor_num);
3976 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3978 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3979 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3985 file_list_popup_menu (GtkFileChooserDefault *impl,
3986 GdkEventButton *event)
3988 file_list_update_popup_menu (impl);
3990 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3991 NULL, NULL, NULL, NULL,
3992 event->button, event->time);
3995 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3997 popup_position_func, impl->browse_files_tree_view,
3998 0, GDK_CURRENT_TIME);
3999 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4005 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4007 list_popup_menu_cb (GtkWidget *widget,
4008 GtkFileChooserDefault *impl)
4010 file_list_popup_menu (impl, NULL);
4014 /* Callback used when a button is pressed on the file list. We trap button 3 to
4015 * bring up a popup menu.
4018 list_button_press_event_cb (GtkWidget *widget,
4019 GdkEventButton *event,
4020 GtkFileChooserDefault *impl)
4022 static gboolean in_press = FALSE;
4028 if (event->button != 3)
4032 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4035 file_list_popup_menu (impl, event);
4039 /* Creates the widgets for the file list */
4041 create_file_list (GtkFileChooserDefault *impl)
4044 GtkTreeSelection *selection;
4045 GtkTreeViewColumn *column;
4046 GtkCellRenderer *renderer;
4048 /* Scrolled window */
4050 swin = gtk_scrolled_window_new (NULL, NULL);
4051 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4052 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4053 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4056 /* Tree/list view */
4058 impl->browse_files_tree_view = gtk_tree_view_new ();
4059 #ifdef PROFILE_FILE_CHOOSER
4060 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4062 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4063 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4065 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4066 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4068 gtk_drag_dest_set (impl->browse_files_tree_view,
4069 GTK_DEST_DEFAULT_ALL,
4070 file_list_dest_targets,
4071 num_file_list_dest_targets,
4072 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4074 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4075 G_CALLBACK (list_row_activated), impl);
4076 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4077 G_CALLBACK (trap_activate_cb), impl);
4078 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4079 G_CALLBACK (list_popup_menu_cb), impl);
4080 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4081 G_CALLBACK (list_button_press_event_cb), impl);
4083 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4084 G_CALLBACK (file_list_drag_data_received_cb), impl);
4085 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4086 G_CALLBACK (file_list_drag_drop_cb), impl);
4087 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4088 G_CALLBACK (file_list_drag_motion_cb), impl);
4090 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4091 gtk_tree_selection_set_select_function (selection,
4094 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4096 file_list_source_targets,
4097 num_file_list_source_targets,
4100 g_signal_connect (selection, "changed",
4101 G_CALLBACK (list_selection_changed), impl);
4103 /* Filename column */
4105 impl->list_name_column = gtk_tree_view_column_new ();
4106 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4107 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4108 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4109 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4111 renderer = gtk_cell_renderer_pixbuf_new ();
4112 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4113 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4114 list_icon_data_func, impl, NULL);
4116 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4117 g_object_set (impl->list_name_renderer,
4118 "ellipsize", PANGO_ELLIPSIZE_END,
4120 g_signal_connect (impl->list_name_renderer, "edited",
4121 G_CALLBACK (renderer_edited_cb), impl);
4122 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4123 G_CALLBACK (renderer_editing_canceled_cb), impl);
4124 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4125 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4126 list_name_data_func, impl, NULL);
4128 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4132 column = gtk_tree_view_column_new ();
4133 gtk_tree_view_column_set_title (column, _("Size"));
4135 renderer = gtk_cell_renderer_text_new ();
4136 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4137 gtk_tree_view_column_set_cell_data_func (column, renderer,
4138 list_size_data_func, impl, NULL);
4139 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4140 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4142 /* Modification time column */
4144 column = gtk_tree_view_column_new ();
4145 gtk_tree_view_column_set_resizable (column, TRUE);
4146 gtk_tree_view_column_set_title (column, _("Modified"));
4148 renderer = gtk_cell_renderer_text_new ();
4149 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4150 gtk_tree_view_column_set_cell_data_func (column, renderer,
4151 list_mtime_data_func, impl, NULL);
4152 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
4153 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4154 gtk_widget_show_all (swin);
4160 create_path_bar (GtkFileChooserDefault *impl)
4162 GtkWidget *path_bar;
4164 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4165 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4171 set_filter_tooltip (GtkWidget *widget,
4174 GtkTooltips *tooltips = (GtkTooltips *)data;
4176 if (GTK_IS_BUTTON (widget))
4177 gtk_tooltips_set_tip (tooltips, widget,
4178 _("Select which types of files are shown"),
4183 realize_filter_combo (GtkWidget *combo,
4186 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4188 gtk_container_forall (GTK_CONTAINER (combo),
4193 /* Creates the widgets for the files/folders pane */
4195 file_pane_create (GtkFileChooserDefault *impl,
4196 GtkSizeGroup *size_group)
4202 vbox = gtk_vbox_new (FALSE, 6);
4203 gtk_widget_show (vbox);
4205 /* Box for lists and preview */
4207 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4208 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4209 gtk_widget_show (hbox);
4213 widget = create_file_list (impl);
4214 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4218 impl->preview_box = gtk_vbox_new (FALSE, 12);
4219 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4220 /* Don't show preview box initially */
4224 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4226 widget = filter_create (impl);
4228 g_signal_connect (widget, "realize",
4229 G_CALLBACK (realize_filter_combo), impl);
4231 gtk_widget_show (widget);
4232 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4234 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4235 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4240 /* Callback used when the "Browse for more folders" expander is toggled */
4242 expander_changed_cb (GtkExpander *expander,
4244 GtkFileChooserDefault *impl)
4246 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4247 update_appearance (impl);
4250 /* Callback used when the selection changes in the save folder combo box */
4252 save_folder_combo_changed_cb (GtkComboBox *combo,
4253 GtkFileChooserDefault *impl)
4257 if (impl->changing_folder)
4260 if (gtk_combo_box_get_active_iter (combo, &iter))
4261 shortcuts_activate_iter (impl, &iter);
4264 /* Creates the combo box with the save folders */
4266 save_folder_combo_create (GtkFileChooserDefault *impl)
4269 GtkCellRenderer *cell;
4271 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4272 "model", impl->shortcuts_model,
4273 "focus-on-click", FALSE,
4275 gtk_widget_show (combo);
4277 cell = gtk_cell_renderer_pixbuf_new ();
4278 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4279 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4280 "pixbuf", SHORTCUTS_COL_PIXBUF,
4281 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4282 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4285 cell = gtk_cell_renderer_text_new ();
4286 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4287 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4288 "text", SHORTCUTS_COL_NAME,
4289 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4292 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4293 shortcuts_row_separator_func,
4294 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
4297 g_signal_connect (combo, "changed",
4298 G_CALLBACK (save_folder_combo_changed_cb), impl);
4303 /* Creates the widgets specific to Save mode */
4305 save_widgets_create (GtkFileChooserDefault *impl)
4310 GtkWidget *alignment;
4312 if (impl->save_widgets != NULL)
4315 location_switch_to_path_bar (impl);
4317 vbox = gtk_vbox_new (FALSE, 12);
4319 table = gtk_table_new (2, 2, FALSE);
4320 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4321 gtk_widget_show (table);
4322 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4323 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4327 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4328 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4329 gtk_table_attach (GTK_TABLE (table), widget,
4333 gtk_widget_show (widget);
4335 /* Location entry */
4337 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4338 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4340 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4341 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4342 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4344 GTK_EXPAND | GTK_FILL, 0,
4346 gtk_widget_show (impl->location_entry);
4347 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4350 impl->save_folder_label = gtk_label_new (NULL);
4351 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4352 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4356 gtk_widget_show (impl->save_folder_label);
4358 impl->save_folder_combo = save_folder_combo_create (impl);
4359 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4361 GTK_EXPAND | GTK_FILL, GTK_FILL,
4363 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4366 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4367 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4369 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4370 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4371 g_signal_connect (impl->save_expander, "notify::expanded",
4372 G_CALLBACK (expander_changed_cb),
4374 gtk_widget_show_all (alignment);
4376 impl->save_widgets = vbox;
4377 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4378 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4379 gtk_widget_show (impl->save_widgets);
4382 /* Destroys the widgets specific to Save mode */
4384 save_widgets_destroy (GtkFileChooserDefault *impl)
4386 if (impl->save_widgets == NULL)
4389 gtk_widget_destroy (impl->save_widgets);
4390 impl->save_widgets = NULL;
4391 impl->location_entry = NULL;
4392 impl->save_folder_label = NULL;
4393 impl->save_folder_combo = NULL;
4394 impl->save_expander = NULL;
4397 /* Turns on the path bar widget. Can be called even if we are already in that
4401 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4403 if (impl->location_entry)
4405 gtk_widget_destroy (impl->location_entry);
4406 impl->location_entry = NULL;
4409 gtk_widget_hide (impl->location_entry_box);
4412 /* Sets the full path of the current folder as the text in the location entry. */
4414 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4418 if (!impl->current_folder)
4421 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4425 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4428 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4435 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4439 gboolean need_slash;
4442 len = strlen (text);
4443 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4449 slash_text = g_new (char, len + 2);
4450 strcpy (slash_text, text);
4451 slash_text[len] = G_DIR_SEPARATOR;
4452 slash_text[len + 1] = 0;
4458 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4463 /* Turns on the location entry. Can be called even if we are already in that
4467 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4469 if (impl->location_entry)
4470 gtk_widget_destroy (impl->location_entry);
4474 gtk_widget_show (impl->location_entry_box);
4478 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4479 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4481 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4482 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4484 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4485 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4487 /* Configure the entry */
4489 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4493 gtk_widget_show (impl->location_entry);
4494 gtk_widget_grab_focus (impl->location_entry);
4497 /* Sets a new location mode. set_buttons determines whether the toggle button
4498 * for the mode will also be changed.
4501 location_mode_set (GtkFileChooserDefault *impl,
4502 LocationMode new_mode,
4503 gboolean set_button)
4505 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4506 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4508 GtkWindow *toplevel;
4509 GtkWidget *current_focus;
4510 gboolean button_active;
4511 gboolean switch_to_file_list;
4515 case LOCATION_MODE_PATH_BAR:
4516 button_active = FALSE;
4518 /* The location_entry will disappear when we switch to path bar mode. So,
4519 * we'll focus the file list in that case, to avoid having a window with
4520 * no focused widget.
4522 toplevel = get_toplevel (GTK_WIDGET (impl));
4523 switch_to_file_list = FALSE;
4526 current_focus = gtk_window_get_focus (toplevel);
4527 if (!current_focus || current_focus == impl->location_entry)
4528 switch_to_file_list = TRUE;
4531 location_switch_to_path_bar (impl);
4533 if (switch_to_file_list)
4534 gtk_widget_grab_focus (impl->browse_files_tree_view);
4538 case LOCATION_MODE_FILENAME_ENTRY:
4539 button_active = TRUE;
4540 location_switch_to_filename_entry (impl);
4544 g_assert_not_reached ();
4550 g_signal_handlers_block_by_func (impl->location_button,
4551 G_CALLBACK (location_button_toggled_cb), impl);
4553 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4555 g_signal_handlers_unblock_by_func (impl->location_button,
4556 G_CALLBACK (location_button_toggled_cb), impl);
4560 impl->location_mode = new_mode;
4564 toggle_location_mode (GtkFileChooserDefault *impl,
4565 gboolean set_button)
4567 LocationMode new_mode;
4570 new_mode = (impl->location_mode == LOCATION_MODE_PATH_BAR) ?
4571 LOCATION_MODE_FILENAME_ENTRY : LOCATION_MODE_PATH_BAR;
4573 location_mode_set (impl, new_mode, set_button);
4577 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4579 toggle_location_mode (impl, TRUE);
4582 /* Callback used when one of the location mode buttons is toggled */
4584 location_button_toggled_cb (GtkToggleButton *toggle,
4585 GtkFileChooserDefault *impl)
4589 is_active = gtk_toggle_button_get_active (toggle);
4592 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4594 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4596 toggle_location_mode (impl, FALSE);
4599 /* Creates a toggle button for the location entry. */
4601 location_button_create (GtkFileChooserDefault *impl)
4606 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4607 gtk_widget_show (image);
4609 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4613 g_signal_connect (impl->location_button, "toggled",
4614 G_CALLBACK (location_button_toggled_cb), impl);
4616 str = _("Type a file name");
4618 gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4619 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4622 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4624 browse_widgets_create (GtkFileChooserDefault *impl)
4630 GtkSizeGroup *size_group;
4632 /* size group is used by the [+][-] buttons and the filter combo */
4633 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4634 vbox = gtk_vbox_new (FALSE, 12);
4636 /* Location widgets */
4637 hbox = gtk_hbox_new (FALSE, 12);
4638 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4639 gtk_widget_show (hbox);
4641 location_button_create (impl);
4642 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
4646 impl->browse_path_bar = create_path_bar (impl);
4647 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4648 gtk_widget_show_all (impl->browse_path_bar);
4649 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4652 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4653 g_signal_connect (impl->browse_new_folder_button, "clicked",
4654 G_CALLBACK (new_folder_button_clicked), impl);
4655 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4657 /* Box for the location label and entry */
4659 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
4660 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
4662 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4663 gtk_widget_show (impl->location_label);
4664 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4667 hpaned = gtk_hpaned_new ();
4668 gtk_widget_show (hpaned);
4669 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4671 widget = shortcuts_pane_create (impl, size_group);
4672 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4673 widget = file_pane_create (impl, size_group);
4674 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4676 g_object_unref (size_group);
4682 gtk_file_chooser_default_constructor (GType type,
4683 guint n_construct_properties,
4684 GObjectConstructParam *construct_params)
4686 GtkFileChooserDefault *impl;
4689 profile_start ("start", NULL);
4691 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4692 n_construct_properties,
4694 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4696 g_assert (impl->file_system);
4698 gtk_widget_push_composite_child ();
4700 /* Shortcuts model */
4702 shortcuts_model_create (impl);
4704 /* The browse widgets */
4705 impl->browse_widgets = browse_widgets_create (impl);
4706 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4708 /* Alignment to hold extra widget */
4709 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4710 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4712 gtk_widget_pop_composite_child ();
4713 update_appearance (impl);
4715 profile_end ("end", NULL);
4720 /* Sets the extra_widget by packing it in the appropriate place */
4722 set_extra_widget (GtkFileChooserDefault *impl,
4723 GtkWidget *extra_widget)
4727 g_object_ref (extra_widget);
4728 /* FIXME: is this right ? */
4729 gtk_widget_show (extra_widget);
4732 if (impl->extra_widget)
4734 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4735 g_object_unref (impl->extra_widget);
4738 impl->extra_widget = extra_widget;
4739 if (impl->extra_widget)
4741 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4742 gtk_widget_show (impl->extra_align);
4745 gtk_widget_hide (impl->extra_align);
4749 set_local_only (GtkFileChooserDefault *impl,
4750 gboolean local_only)
4752 if (local_only != impl->local_only)
4754 impl->local_only = local_only;
4756 if (impl->shortcuts_model && impl->file_system)
4758 shortcuts_add_volumes (impl);
4759 shortcuts_add_bookmarks (impl);
4763 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4765 /* If we are pointing to a non-local folder, make an effort to change
4766 * back to a local folder, but it's really up to the app to not cause
4767 * such a situation, so we ignore errors.
4769 const gchar *home = g_get_home_dir ();
4770 GtkFilePath *home_path;
4775 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4777 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4779 gtk_file_path_free (home_path);
4785 volumes_changed_cb (GtkFileSystem *file_system,
4786 GtkFileChooserDefault *impl)
4788 shortcuts_add_volumes (impl);
4791 /* Callback used when the set of bookmarks changes in the file system */
4793 bookmarks_changed_cb (GtkFileSystem *file_system,
4794 GtkFileChooserDefault *impl)
4796 shortcuts_add_bookmarks (impl);
4798 bookmarks_check_add_sensitivity (impl);
4799 bookmarks_check_remove_sensitivity (impl);
4800 shortcuts_check_popup_sensitivity (impl);
4803 /* Sets the file chooser to multiple selection mode */
4805 set_select_multiple (GtkFileChooserDefault *impl,
4806 gboolean select_multiple,
4807 gboolean property_notify)
4809 GtkTreeSelection *selection;
4810 GtkSelectionMode mode;
4812 if (select_multiple == impl->select_multiple)
4815 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4817 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4818 gtk_tree_selection_set_mode (selection, mode);
4820 impl->select_multiple = select_multiple;
4821 g_object_notify (G_OBJECT (impl), "select-multiple");
4823 check_preview_change (impl);
4827 set_file_system_backend (GtkFileChooserDefault *impl,
4828 const char *backend)
4830 profile_start ("start for backend", backend ? backend : "default");
4832 if (impl->file_system)
4834 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4835 impl->volumes_changed_id = 0;
4836 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4837 impl->bookmarks_changed_id = 0;
4838 g_object_unref (impl->file_system);
4841 impl->file_system = NULL;
4843 impl->file_system = _gtk_file_system_create (backend);
4846 GtkSettings *settings = gtk_settings_get_default ();
4847 gchar *default_backend = NULL;
4849 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4850 if (default_backend)
4852 impl->file_system = _gtk_file_system_create (default_backend);
4853 g_free (default_backend);
4857 if (!impl->file_system)
4859 #if defined (G_OS_UNIX)
4860 impl->file_system = gtk_file_system_unix_new ();
4861 #elif defined (G_OS_WIN32)
4862 impl->file_system = gtk_file_system_win32_new ();
4864 #error "No default filesystem implementation on the platform"
4868 if (impl->file_system)
4870 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4871 G_CALLBACK (volumes_changed_cb),
4873 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4874 G_CALLBACK (bookmarks_changed_cb),
4878 profile_end ("end", NULL);
4881 /* This function is basically a do_all function.
4883 * It sets the visibility on all the widgets based on the current state, and
4884 * moves the custom_widget if needed.
4887 update_appearance (GtkFileChooserDefault *impl)
4889 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4890 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4894 gtk_widget_hide (impl->location_button);
4895 save_widgets_create (impl);
4897 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4898 text = _("Save in _folder:");
4900 text = _("Create in _folder:");
4902 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4904 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4906 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4907 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4908 gtk_widget_show (impl->browse_widgets);
4912 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4913 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4914 gtk_widget_hide (impl->browse_widgets);
4917 gtk_widget_show (impl->browse_new_folder_button);
4919 if (impl->select_multiple)
4921 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4922 "Re-setting to single selection mode.");
4923 set_select_multiple (impl, FALSE, TRUE);
4926 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4927 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4929 gtk_widget_show (impl->location_button);
4930 save_widgets_destroy (impl);
4931 gtk_widget_show (impl->browse_widgets);
4932 location_mode_set (impl, impl->location_mode, TRUE);
4935 if (impl->location_entry)
4936 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4938 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4939 gtk_widget_hide (impl->browse_new_folder_button);
4941 gtk_widget_show (impl->browse_new_folder_button);
4943 /* This *is* needed; we need to redraw the file list because the "sensitivity"
4944 * of files may change depending whether we are in a file or folder-only mode.
4946 gtk_widget_queue_draw (impl->browse_files_tree_view);
4948 g_signal_emit_by_name (impl, "default-size-changed");
4952 gtk_file_chooser_default_set_property (GObject *object,
4954 const GValue *value,
4958 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4962 case GTK_FILE_CHOOSER_PROP_ACTION:
4964 GtkFileChooserAction action = g_value_get_enum (value);
4966 if (action != impl->action)
4968 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4970 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4971 && impl->select_multiple)
4973 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4974 "this is not allowed in multiple selection mode. Resetting the file chooser "
4975 "to single selection mode.");
4976 set_select_multiple (impl, FALSE, TRUE);
4978 impl->action = action;
4979 update_appearance (impl);
4984 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4985 set_file_system_backend (impl, g_value_get_string (value));
4988 case GTK_FILE_CHOOSER_PROP_FILTER:
4989 set_current_filter (impl, g_value_get_object (value));
4992 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4993 set_local_only (impl, g_value_get_boolean (value));
4996 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4997 set_preview_widget (impl, g_value_get_object (value));
5000 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5001 impl->preview_widget_active = g_value_get_boolean (value);
5002 update_preview_widget_visibility (impl);
5005 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5006 impl->use_preview_label = g_value_get_boolean (value);
5007 update_preview_widget_visibility (impl);
5010 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5011 set_extra_widget (impl, g_value_get_object (value));
5014 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5016 gboolean select_multiple = g_value_get_boolean (value);
5017 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5020 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5021 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5022 "leaving the file chooser in single selection mode.");
5026 set_select_multiple (impl, select_multiple, FALSE);
5030 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5032 gboolean show_hidden = g_value_get_boolean (value);
5033 if (show_hidden != impl->show_hidden)
5035 impl->show_hidden = show_hidden;
5037 if (impl->browse_files_model)
5038 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5043 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5045 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5046 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5051 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5057 gtk_file_chooser_default_get_property (GObject *object,
5062 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5066 case GTK_FILE_CHOOSER_PROP_ACTION:
5067 g_value_set_enum (value, impl->action);
5070 case GTK_FILE_CHOOSER_PROP_FILTER:
5071 g_value_set_object (value, impl->current_filter);
5074 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5075 g_value_set_boolean (value, impl->local_only);
5078 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5079 g_value_set_object (value, impl->preview_widget);
5082 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5083 g_value_set_boolean (value, impl->preview_widget_active);
5086 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5087 g_value_set_boolean (value, impl->use_preview_label);
5090 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5091 g_value_set_object (value, impl->extra_widget);
5094 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5095 g_value_set_boolean (value, impl->select_multiple);
5098 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5099 g_value_set_boolean (value, impl->show_hidden);
5102 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5103 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5112 /* Removes the settings signal handler. It's safe to call multiple times */
5114 remove_settings_signal (GtkFileChooserDefault *impl,
5117 if (impl->settings_signal_id)
5119 GtkSettings *settings;
5121 settings = gtk_settings_get_for_screen (screen);
5122 g_signal_handler_disconnect (settings,
5123 impl->settings_signal_id);
5124 impl->settings_signal_id = 0;
5129 gtk_file_chooser_default_dispose (GObject *object)
5132 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5134 if (impl->extra_widget)
5136 g_object_unref (impl->extra_widget);
5137 impl->extra_widget = NULL;
5140 if (impl->volumes_changed_id > 0)
5142 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5143 impl->volumes_changed_id = 0;
5146 if (impl->bookmarks_changed_id > 0)
5148 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5149 impl->bookmarks_changed_id = 0;
5152 pending_select_paths_free (impl);
5154 /* cancel all pending operations */
5155 if (impl->pending_handles)
5157 for (l = impl->pending_handles; l; l = l->next)
5159 GtkFileSystemHandle *handle =l->data;
5160 gtk_file_system_cancel_operation (handle);
5162 g_slist_free (impl->pending_handles);
5163 impl->pending_handles = NULL;
5166 if (impl->reload_icon_handles)
5168 for (l = impl->reload_icon_handles; l; l = l->next)
5170 GtkFileSystemHandle *handle =l->data;
5171 gtk_file_system_cancel_operation (handle);
5173 g_slist_free (impl->reload_icon_handles);
5174 impl->reload_icon_handles = NULL;
5177 if (impl->loading_shortcuts)
5179 for (l = impl->loading_shortcuts; l; l = l->next)
5181 GtkFileSystemHandle *handle =l->data;
5182 gtk_file_system_cancel_operation (handle);
5184 g_slist_free (impl->loading_shortcuts);
5185 impl->loading_shortcuts = NULL;
5188 if (impl->file_list_drag_data_received_handle)
5190 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5191 impl->file_list_drag_data_received_handle = NULL;
5194 if (impl->update_current_folder_handle)
5196 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5197 impl->update_current_folder_handle = NULL;
5200 if (impl->show_and_select_paths_handle)
5202 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5203 impl->show_and_select_paths_handle = NULL;
5206 if (impl->should_respond_get_info_handle)
5208 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5209 impl->should_respond_get_info_handle = NULL;
5212 if (impl->update_from_entry_handle)
5214 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5215 impl->update_from_entry_handle = NULL;
5218 if (impl->shortcuts_activate_iter_handle)
5220 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5221 impl->shortcuts_activate_iter_handle = NULL;
5224 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5226 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5229 /* We override show-all since we have internal widgets that
5230 * shouldn't be shown when you call show_all(), like the filter
5234 gtk_file_chooser_default_show_all (GtkWidget *widget)
5236 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5238 gtk_widget_show (widget);
5240 if (impl->extra_widget)
5241 gtk_widget_show_all (impl->extra_widget);
5244 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5245 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5248 toplevel_set_focus_cb (GtkWindow *window,
5250 GtkFileChooserDefault *impl)
5252 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5255 /* We monitor the focus widget on our toplevel to be able to know which widget
5256 * was last focused at the time our "should_respond" method gets called.
5259 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5260 GtkWidget *previous_toplevel)
5262 GtkFileChooserDefault *impl;
5263 GtkWidget *toplevel;
5265 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5267 if (previous_toplevel)
5269 g_assert (impl->toplevel_set_focus_id != 0);
5270 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5271 impl->toplevel_set_focus_id = 0;
5272 impl->toplevel_last_focus_widget = NULL;
5275 g_assert (impl->toplevel_set_focus_id == 0);
5277 toplevel = gtk_widget_get_toplevel (widget);
5278 if (GTK_IS_WINDOW (toplevel))
5280 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5281 G_CALLBACK (toplevel_set_focus_cb), impl);
5282 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5286 /* Changes the icons wherever it is needed */
5288 change_icon_theme (GtkFileChooserDefault *impl)
5290 GtkSettings *settings;
5293 profile_start ("start", NULL);
5295 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5297 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5298 impl->icon_size = MAX (width, height);
5300 impl->icon_size = FALLBACK_ICON_SIZE;
5302 shortcuts_reload_icons (impl);
5303 gtk_widget_queue_resize (impl->browse_files_tree_view);
5305 profile_end ("end", NULL);
5308 /* Callback used when a GtkSettings value changes */
5310 settings_notify_cb (GObject *object,
5312 GtkFileChooserDefault *impl)
5316 profile_start ("start", NULL);
5318 name = g_param_spec_get_name (pspec);
5320 if (strcmp (name, "gtk-icon-theme-name") == 0
5321 || strcmp (name, "gtk-icon-sizes") == 0)
5322 change_icon_theme (impl);
5324 profile_end ("end", NULL);
5327 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5331 check_icon_theme (GtkFileChooserDefault *impl)
5333 GtkSettings *settings;
5335 profile_start ("start", NULL);
5337 if (impl->settings_signal_id)
5339 profile_end ("end", NULL);
5343 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5345 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5346 impl->settings_signal_id = g_signal_connect (settings, "notify",
5347 G_CALLBACK (settings_notify_cb), impl);
5349 change_icon_theme (impl);
5352 profile_end ("end", NULL);
5356 gtk_file_chooser_default_style_set (GtkWidget *widget,
5357 GtkStyle *previous_style)
5359 GtkFileChooserDefault *impl;
5361 profile_start ("start", NULL);
5363 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5365 profile_msg (" parent class style_set start", NULL);
5366 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5367 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5368 profile_msg (" parent class style_set end", NULL);
5370 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5371 change_icon_theme (impl);
5373 profile_msg (" emit default-size-changed start", NULL);
5374 g_signal_emit_by_name (widget, "default-size-changed");
5375 profile_msg (" emit default-size-changed end", NULL);
5377 profile_end ("end", NULL);
5381 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5382 GdkScreen *previous_screen)
5384 GtkFileChooserDefault *impl;
5386 profile_start ("start", NULL);
5388 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5390 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5391 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5393 remove_settings_signal (impl, previous_screen);
5394 check_icon_theme (impl);
5396 g_signal_emit_by_name (widget, "default-size-changed");
5398 profile_end ("end", NULL);
5402 get_is_file_filtered (GtkFileChooserDefault *impl,
5403 const GtkFilePath *path,
5404 GtkFileInfo *file_info)
5406 GtkFileFilterInfo filter_info;
5407 GtkFileFilterFlags needed;
5410 if (!impl->current_filter)
5413 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5415 needed = gtk_file_filter_get_needed (impl->current_filter);
5417 filter_info.display_name = gtk_file_info_get_display_name (file_info);
5418 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5420 if (needed & GTK_FILE_FILTER_FILENAME)
5422 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5423 if (filter_info.filename)
5424 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5427 filter_info.filename = NULL;
5429 if (needed & GTK_FILE_FILTER_URI)
5431 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5432 if (filter_info.uri)
5433 filter_info.contains |= GTK_FILE_FILTER_URI;
5436 filter_info.uri = NULL;
5438 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5440 if (filter_info.filename)
5441 g_free ((gchar *)filter_info.filename);
5442 if (filter_info.uri)
5443 g_free ((gchar *)filter_info.uri);
5449 settings_load (GtkFileChooserDefault *impl)
5451 GtkFileChooserSettings *settings;
5452 LocationMode location_mode;
5453 gboolean show_hidden;
5454 gboolean expand_folders;
5456 settings = _gtk_file_chooser_settings_new ();
5458 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5459 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5460 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5462 g_object_unref (settings);
5464 location_mode_set (impl, location_mode, TRUE);
5465 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5466 impl->expand_folders = expand_folders;
5467 if (impl->save_expander)
5468 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5472 settings_save (GtkFileChooserDefault *impl)
5474 GtkFileChooserSettings *settings;
5476 settings = _gtk_file_chooser_settings_new ();
5478 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5479 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5480 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5483 _gtk_file_chooser_settings_save (settings, NULL);
5485 g_object_unref (settings);
5488 /* GtkWidget::map method */
5490 gtk_file_chooser_default_map (GtkWidget *widget)
5492 GtkFileChooserDefault *impl;
5493 char *current_working_dir;
5495 profile_start ("start", NULL);
5497 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5499 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5501 switch (impl->reload_state)
5504 /* The user didn't explicitly give us a folder to display, so we'll use the cwd */
5505 current_working_dir = g_get_current_dir ();
5506 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
5507 g_free (current_working_dir);
5510 case RELOAD_HAS_FOLDER:
5511 /* Nothing; we are already loading or loaded, so we don't need to reload */
5514 case RELOAD_WAS_UNMAPPED:
5515 /* Just reload the current folder; else continue the pending load. */
5516 if (impl->current_folder)
5518 pending_select_paths_store_selection (impl);
5519 change_folder_and_display_error (impl, impl->current_folder);
5524 g_assert_not_reached ();
5527 bookmarks_changed_cb (impl->file_system, impl);
5529 settings_load (impl);
5531 profile_end ("end", NULL);
5534 /* GtkWidget::unmap method */
5536 gtk_file_chooser_default_unmap (GtkWidget *widget)
5538 GtkFileChooserDefault *impl;
5540 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5542 settings_save (impl);
5544 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5546 impl->reload_state = RELOAD_WAS_UNMAPPED;
5550 list_model_filter_func (GtkFileSystemModel *model,
5552 const GtkFileInfo *file_info,
5555 GtkFileChooserDefault *impl = user_data;
5557 if (!impl->current_filter)
5560 if (gtk_file_info_get_is_folder (file_info))
5563 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5567 install_list_model_filter (GtkFileChooserDefault *impl)
5569 GtkFileSystemModelFilter filter;
5572 g_assert (impl->browse_files_model != NULL);
5574 if (impl->current_filter)
5576 filter = list_model_filter_func;
5585 _gtk_file_system_model_set_filter (impl->browse_files_model,
5590 #define COMPARE_DIRECTORIES \
5591 GtkFileChooserDefault *impl = user_data; \
5592 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
5593 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
5594 gboolean dir_a, dir_b; \
5597 dir_a = gtk_file_info_get_is_folder (info_a); \
5599 return impl->list_sort_ascending ? -1 : 1; \
5602 dir_b = gtk_file_info_get_is_folder (info_b); \
5604 return impl->list_sort_ascending ? 1 : -1; \
5606 if (dir_a != dir_b) \
5607 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5609 /* Sort callback for the filename column */
5611 name_sort_func (GtkTreeModel *model,
5616 COMPARE_DIRECTORIES;
5618 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
5621 /* Sort callback for the size column */
5623 size_sort_func (GtkTreeModel *model,
5628 COMPARE_DIRECTORIES;
5631 gint64 size_a = gtk_file_info_get_size (info_a);
5632 gint64 size_b = gtk_file_info_get_size (info_b);
5634 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
5638 /* Sort callback for the mtime column */
5640 mtime_sort_func (GtkTreeModel *model,
5645 COMPARE_DIRECTORIES;
5648 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
5649 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
5651 return ta > tb ? -1 : (ta == tb ? 0 : 1);
5655 /* Callback used when the sort column changes. We cache the sort order for use
5656 * in name_sort_func().
5659 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5660 GtkFileChooserDefault *impl)
5662 GtkSortType sort_type;
5664 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
5665 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5669 set_busy_cursor (GtkFileChooserDefault *impl,
5672 GtkWindow *toplevel;
5673 GdkDisplay *display;
5676 toplevel = get_toplevel (GTK_WIDGET (impl));
5677 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
5680 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
5683 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5687 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
5688 gdk_display_flush (display);
5691 gdk_cursor_unref (cursor);
5694 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5696 load_set_model (GtkFileChooserDefault *impl)
5698 profile_start ("start", NULL);
5700 g_assert (impl->browse_files_model != NULL);
5701 g_assert (impl->sort_model == NULL);
5703 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
5704 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
5705 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
5706 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
5707 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
5708 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
5709 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
5710 impl->list_sort_ascending = TRUE;
5711 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
5713 g_signal_connect (impl->sort_model, "sort_column_changed",
5714 G_CALLBACK (list_sort_column_changed_cb), impl);
5716 profile_msg (" gtk_tree_view_set_model start", NULL);
5717 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5718 GTK_TREE_MODEL (impl->sort_model));
5719 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5720 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5721 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
5722 profile_msg (" gtk_tree_view_set_model end", NULL);
5724 profile_end ("end", NULL);
5727 /* Timeout callback used when the loading timer expires */
5729 load_timeout_cb (gpointer data)
5731 GtkFileChooserDefault *impl;
5733 profile_start ("start", NULL);
5735 impl = GTK_FILE_CHOOSER_DEFAULT (data);
5736 g_assert (impl->load_state == LOAD_PRELOAD);
5737 g_assert (impl->load_timeout_id != 0);
5738 g_assert (impl->browse_files_model != NULL);
5740 impl->load_timeout_id = 0;
5741 impl->load_state = LOAD_LOADING;
5743 load_set_model (impl);
5745 profile_end ("end", NULL);
5750 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
5752 load_setup_timer (GtkFileChooserDefault *impl)
5754 g_assert (impl->load_timeout_id == 0);
5755 g_assert (impl->load_state != LOAD_PRELOAD);
5757 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
5758 impl->load_state = LOAD_PRELOAD;
5761 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5763 load_remove_timer (GtkFileChooserDefault *impl)
5765 if (impl->load_timeout_id != 0)
5767 g_assert (impl->load_state == LOAD_PRELOAD);
5769 g_source_remove (impl->load_timeout_id);
5770 impl->load_timeout_id = 0;
5771 impl->load_state = LOAD_EMPTY;
5774 g_assert (impl->load_state == LOAD_EMPTY ||
5775 impl->load_state == LOAD_LOADING ||
5776 impl->load_state == LOAD_FINISHED);
5779 /* Selects the first row in the file list */
5781 browse_files_select_first_row (GtkFileChooserDefault *impl)
5785 if (!impl->sort_model)
5788 path = gtk_tree_path_new_from_indices (0, -1);
5789 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5790 gtk_tree_path_free (path);
5793 struct center_selected_row_closure {
5794 GtkFileChooserDefault *impl;
5795 gboolean already_centered;
5798 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
5799 * selected row in the tree view.
5802 center_selected_row_foreach_cb (GtkTreeModel *model,
5807 struct center_selected_row_closure *closure;
5810 if (closure->already_centered)
5813 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5814 closure->already_centered = TRUE;
5817 /* Centers the selected row in the tree view */
5819 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5821 struct center_selected_row_closure closure;
5822 GtkTreeSelection *selection;
5824 closure.impl = impl;
5825 closure.already_centered = FALSE;
5827 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5828 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5831 struct ShowAndSelectPathsData
5833 GtkFileChooserDefault *impl;
5838 show_and_select_paths_finished_loading (GtkFileFolder *folder,
5841 gboolean have_hidden;
5842 gboolean have_filtered;
5844 struct ShowAndSelectPathsData *data = user_data;
5846 have_hidden = FALSE;
5847 have_filtered = FALSE;
5849 for (l = data->paths; l; l = l->next)
5851 const GtkFilePath *path;
5857 info = gtk_file_folder_get_info (folder, path, NULL);
5861 have_hidden = gtk_file_info_get_is_hidden (info);
5864 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
5866 gtk_file_info_free (info);
5868 if (have_hidden && have_filtered)
5869 break; /* we now have all the information we need */
5873 g_signal_handlers_disconnect_by_func (folder,
5874 show_and_select_paths_finished_loading,
5877 g_object_unref (folder);
5880 g_object_set (data->impl, "show-hidden", TRUE, NULL);
5883 set_current_filter (data->impl, NULL);
5885 for (l = data->paths; l; l = l->next)
5887 const GtkFilePath *path;
5890 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
5891 select_func, data->impl);
5894 browse_files_center_selected_row (data->impl);
5896 g_object_unref (data->impl);
5897 gtk_file_paths_free (data->paths);
5902 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
5903 GtkFileFolder *folder,
5904 const GError *error,
5907 gboolean cancelled = handle->cancelled;
5908 struct ShowAndSelectPathsData *data = user_data;
5910 if (data->impl->show_and_select_paths_handle != handle)
5913 data->impl->show_and_select_paths_handle = NULL;
5915 if (cancelled || error)
5918 g_object_unref (handle);
5920 if (gtk_file_folder_is_finished_loading (folder))
5921 show_and_select_paths_finished_loading (folder, user_data);
5923 g_signal_connect (folder, "finished-loading",
5924 G_CALLBACK (show_and_select_paths_finished_loading),
5930 g_object_unref (data->impl);
5931 gtk_file_paths_free (data->paths);
5934 g_object_unref (handle);
5938 show_and_select_paths (GtkFileChooserDefault *impl,
5939 const GtkFilePath *parent_path,
5943 struct ShowAndSelectPathsData *info;
5945 profile_start ("start", NULL);
5949 profile_end ("end", NULL);
5953 info = g_new (struct ShowAndSelectPathsData, 1);
5954 info->impl = g_object_ref (impl);
5955 info->paths = gtk_file_paths_copy (paths);
5957 if (impl->show_and_select_paths_handle)
5958 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5960 impl->show_and_select_paths_handle =
5961 gtk_file_system_get_folder (impl->file_system, parent_path,
5962 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
5963 show_and_select_paths_get_folder_cb, info);
5965 profile_end ("end", NULL);
5969 /* Processes the pending operation when a folder is finished loading */
5971 pending_select_paths_process (GtkFileChooserDefault *impl)
5973 g_assert (impl->load_state == LOAD_FINISHED);
5974 g_assert (impl->browse_files_model != NULL);
5975 g_assert (impl->sort_model != NULL);
5977 if (impl->pending_select_paths)
5980 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
5981 pending_select_paths_free (impl);
5982 browse_files_center_selected_row (impl);
5986 /* We only select the first row if the chooser is actually mapped ---
5987 * selecting the first row is to help the user when he is interacting with
5988 * the chooser, but sometimes a chooser works not on behalf of the user,
5989 * but rather on behalf of something else like GtkFileChooserButton. In
5990 * that case, the chooser's selection should be what the caller expects,
5991 * as the user can't see that something else got selected. See bug #165264.
5993 * Also, we don't select the first file if we are not in OPEN mode. Doing
5994 * so would change the contents of the filename entry for SAVE or
5995 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5996 * select a *different* folder from the one into which the user just
5999 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6000 browse_files_select_first_row (impl);
6003 g_assert (impl->pending_select_paths == NULL);
6006 /* Callback used when the file system model finishes loading */
6008 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6009 GtkFileChooserDefault *impl)
6011 profile_start ("start", NULL);
6013 if (impl->load_state == LOAD_PRELOAD)
6015 load_remove_timer (impl);
6016 load_set_model (impl);
6018 else if (impl->load_state == LOAD_LOADING)
6024 /* We can't g_assert_not_reached(), as something other than us may have
6025 * initiated a folder reload. See #165556.
6027 profile_end ("end", NULL);
6031 g_assert (impl->load_timeout_id == 0);
6033 impl->load_state = LOAD_FINISHED;
6035 pending_select_paths_process (impl);
6036 set_busy_cursor (impl, FALSE);
6037 #ifdef PROFILE_FILE_CHOOSER
6038 access ("MARK: *** FINISHED LOADING", F_OK);
6041 profile_end ("end", NULL);
6044 /* Gets rid of the old list model and creates a new one for the current folder */
6046 set_list_model (GtkFileChooserDefault *impl,
6049 g_assert (impl->current_folder != NULL);
6051 profile_start ("start", NULL);
6053 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6055 if (impl->browse_files_model)
6057 g_object_unref (impl->browse_files_model);
6058 impl->browse_files_model = NULL;
6061 if (impl->sort_model)
6063 g_object_unref (impl->sort_model);
6064 impl->sort_model = NULL;
6067 set_busy_cursor (impl, TRUE);
6068 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6070 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6071 impl->current_folder, 0,
6074 if (!impl->browse_files_model)
6076 set_busy_cursor (impl, FALSE);
6077 profile_end ("end", NULL);
6081 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6083 g_signal_connect (impl->browse_files_model, "finished-loading",
6084 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6086 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6088 install_list_model_filter (impl);
6090 profile_end ("end", NULL);
6095 struct update_chooser_entry_selected_foreach_closure {
6097 GtkTreeIter first_selected_iter;
6101 compare_utf8_filenames (const gchar *a,
6104 gchar *a_folded, *b_folded;
6107 a_folded = g_utf8_strdown (a, -1);
6108 b_folded = g_utf8_strdown (b, -1);
6110 retval = strcmp (a_folded, b_folded);
6119 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6124 struct update_chooser_entry_selected_foreach_closure *closure;
6127 closure->num_selected++;
6129 if (closure->num_selected == 1)
6130 closure->first_selected_iter = *iter;
6134 update_chooser_entry (GtkFileChooserDefault *impl)
6136 GtkTreeSelection *selection;
6137 struct update_chooser_entry_selected_foreach_closure closure;
6138 const char *file_part;
6140 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6141 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6142 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6143 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6144 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6147 g_assert (impl->location_entry != NULL);
6149 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6150 closure.num_selected = 0;
6151 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6155 if (closure.num_selected == 0)
6157 goto maybe_clear_entry;
6159 else if (closure.num_selected == 1)
6161 GtkTreeIter child_iter;
6162 const GtkFileInfo *info;
6163 gboolean change_entry;
6165 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6167 &closure.first_selected_iter);
6169 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6171 /* If the cursor moved to the row of the newly created folder,
6172 * retrieving info will return NULL.
6177 g_free (impl->browse_files_last_selected_name);
6178 impl->browse_files_last_selected_name = g_strdup (gtk_file_info_get_display_name (info));
6180 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6181 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6182 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6184 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6187 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6193 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6194 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6196 /* Multiple selection, so just clear the entry. */
6198 g_free (impl->browse_files_last_selected_name);
6199 impl->browse_files_last_selected_name = NULL;
6201 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6207 if (impl->browse_files_last_selected_name)
6209 const char *entry_text;
6211 gboolean clear_entry;
6213 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6214 len = strlen (entry_text);
6217 /* The file chooser entry may have appended a "/" to its text. So
6218 * take it out, and compare the result to the old selection.
6220 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6224 tmp = g_strndup (entry_text, len - 1);
6225 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6229 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6232 clear_entry = FALSE;
6235 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6240 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6241 const GtkFilePath *path,
6244 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
6248 struct UpdateCurrentFolderData
6250 GtkFileChooserDefault *impl;
6252 gboolean keep_trail;
6253 GtkFilePath *original_path;
6254 GError *original_error;
6258 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6259 const GtkFileInfo *info,
6260 const GError *error,
6263 gboolean cancelled = handle->cancelled;
6264 struct UpdateCurrentFolderData *data = user_data;
6265 GtkFileChooserDefault *impl = data->impl;
6267 if (handle != impl->update_current_folder_handle)
6270 impl->update_current_folder_handle = NULL;
6271 impl->reload_state = RELOAD_EMPTY;
6273 set_busy_cursor (impl, FALSE);
6280 GtkFilePath *parent_path;
6282 if (!data->original_path)
6284 data->original_path = gtk_file_path_copy (data->path);
6285 data->original_error = g_error_copy (error);
6288 /* get parent path and try to change the folder to that */
6289 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6290 parent_path != NULL)
6292 gtk_file_path_free (data->path);
6293 data->path = parent_path;
6295 g_object_unref (handle);
6297 /* restart the update current folder operation */
6298 impl->reload_state = RELOAD_HAS_FOLDER;
6300 impl->update_current_folder_handle =
6301 gtk_file_system_get_info (impl->file_system, data->path,
6302 GTK_FILE_INFO_IS_FOLDER,
6303 update_current_folder_get_info_cb,
6306 set_busy_cursor (impl, TRUE);
6312 /* error and bail out */
6313 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6315 gtk_file_path_free (data->original_path);
6321 if (data->original_path)
6323 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6325 gtk_file_path_free (data->original_path);
6328 if (!gtk_file_info_get_is_folder (info))
6331 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6334 if (impl->current_folder != data->path)
6336 if (impl->current_folder)
6337 gtk_file_path_free (impl->current_folder);
6339 impl->current_folder = gtk_file_path_copy (data->path);
6341 impl->reload_state = RELOAD_HAS_FOLDER;
6344 /* Update the widgets that may trigger a folder change themselves. */
6346 if (!impl->changing_folder)
6348 impl->changing_folder = TRUE;
6350 shortcuts_update_current_folder (impl);
6352 impl->changing_folder = FALSE;
6355 /* Set the folder on the save entry */
6357 if (impl->location_entry)
6358 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6359 impl->current_folder);
6361 /* Create a new list model. This is slightly evil; we store the result value
6362 * but perform more actions rather than returning immediately even if it
6363 * generates an error.
6365 set_list_model (impl, NULL);
6367 /* Refresh controls */
6369 shortcuts_find_current_folder (impl);
6371 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6373 check_preview_change (impl);
6374 bookmarks_check_add_sensitivity (impl);
6376 g_signal_emit_by_name (impl, "selection-changed", 0);
6379 gtk_file_path_free (data->path);
6382 g_object_unref (handle);
6386 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6387 const GtkFilePath *path,
6388 gboolean keep_trail,
6391 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6392 struct UpdateCurrentFolderData *data;
6394 profile_start ("start", (char *) path);
6396 g_assert (path != NULL);
6398 if (impl->local_only &&
6399 !gtk_file_system_path_is_local (impl->file_system, path))
6402 GTK_FILE_CHOOSER_ERROR,
6403 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6404 _("Cannot change to folder because it is not local"));
6406 profile_end ("end - not local", (char *) path);
6410 if (impl->update_current_folder_handle)
6411 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6413 /* Test validity of path here. */
6414 data = g_new0 (struct UpdateCurrentFolderData, 1);
6416 data->path = gtk_file_path_copy (path);
6417 data->keep_trail = keep_trail;
6419 impl->reload_state = RELOAD_HAS_FOLDER;
6421 impl->update_current_folder_handle =
6422 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6423 update_current_folder_get_info_cb,
6426 set_busy_cursor (impl, TRUE);
6428 profile_end ("end", NULL);
6432 static GtkFilePath *
6433 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6435 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6437 if (impl->reload_state == RELOAD_EMPTY)
6439 char *current_working_dir;
6442 /* We are unmapped, or we had an error while loading the last folder. We'll return
6443 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6444 * explicitly calls set_current_folder() on us.
6446 current_working_dir = g_get_current_dir ();
6447 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6448 g_free (current_working_dir);
6452 return gtk_file_path_copy (impl->current_folder);
6456 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6459 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6461 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6462 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6464 pending_select_paths_free (impl);
6465 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6469 select_func (GtkFileSystemModel *model,
6474 GtkFileChooserDefault *impl = user_data;
6475 GtkTreeSelection *selection;
6476 GtkTreeIter sorted_iter;
6478 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6480 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6481 gtk_tree_selection_select_iter (selection, &sorted_iter);
6485 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
6486 const GtkFilePath *path,
6489 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6490 GtkFilePath *parent_path;
6493 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6497 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6499 if (impl->load_state == LOAD_EMPTY)
6503 g_assert (impl->current_folder != NULL);
6505 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6508 if (same_path && impl->load_state == LOAD_FINISHED)
6513 paths.data = (gpointer) path;
6516 result = show_and_select_paths (impl, parent_path, &paths, error);
6517 gtk_file_path_free (parent_path);
6521 pending_select_paths_add (impl, path);
6527 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6528 gtk_file_path_free (parent_path);
6532 gtk_file_path_free (parent_path);
6537 unselect_func (GtkFileSystemModel *model,
6542 GtkFileChooserDefault *impl = user_data;
6543 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
6544 GtkTreePath *sorted_path;
6546 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
6548 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
6550 gtk_tree_path_free (sorted_path);
6554 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
6555 const GtkFilePath *path)
6557 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6559 if (!impl->browse_files_model)
6562 _gtk_file_system_model_path_do (impl->browse_files_model, path,
6563 unselect_func, impl);
6567 maybe_select (GtkTreeModel *model,
6572 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
6573 GtkTreeSelection *selection;
6574 const GtkFileInfo *info;
6577 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6579 info = get_list_file_info (impl, iter);
6580 is_folder = gtk_file_info_get_is_folder (info);
6582 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
6583 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
6584 gtk_tree_selection_select_iter (selection, iter);
6586 gtk_tree_selection_unselect_iter (selection, iter);
6592 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
6594 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6595 if (impl->select_multiple)
6596 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
6597 maybe_select, impl);
6601 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
6603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6604 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6606 gtk_tree_selection_unselect_all (selection);
6607 pending_select_paths_free (impl);
6610 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
6612 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
6614 * is_empty_ret - whether the file entry is totally empty
6616 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
6617 * the path will be "$cwd/foobar")
6620 check_save_entry (GtkFileChooserDefault *impl,
6621 GtkFilePath **path_ret,
6622 gboolean *is_well_formed_ret,
6623 gboolean *is_empty_ret,
6624 gboolean *is_file_part_empty_ret,
6625 gboolean *is_folder)
6627 GtkFileChooserEntry *chooser_entry;
6628 const GtkFilePath *current_folder;
6629 const char *file_part;
6633 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6634 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6635 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6636 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6637 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
6639 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
6641 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
6644 *is_well_formed_ret = TRUE;
6645 *is_empty_ret = TRUE;
6646 *is_file_part_empty_ret = TRUE;
6652 *is_empty_ret = FALSE;
6654 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6655 if (!current_folder)
6658 *is_well_formed_ret = FALSE;
6659 *is_file_part_empty_ret = FALSE;
6665 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6667 if (!file_part || file_part[0] == '\0')
6669 *path_ret = gtk_file_path_copy (current_folder);
6670 *is_well_formed_ret = TRUE;
6671 *is_file_part_empty_ret = TRUE;
6677 *is_file_part_empty_ret = FALSE;
6680 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
6684 error_building_filename_dialog (impl, current_folder, file_part, error);
6686 *is_well_formed_ret = FALSE;
6693 *is_well_formed_ret = TRUE;
6694 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
6697 struct get_paths_closure {
6698 GtkFileChooserDefault *impl;
6700 GtkFilePath *path_from_entry;
6704 get_paths_foreach (GtkTreeModel *model,
6709 struct get_paths_closure *info;
6710 const GtkFilePath *file_path;
6711 GtkFileSystemModel *fs_model;
6712 GtkTreeIter sel_iter;
6715 fs_model = info->impl->browse_files_model;
6716 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
6718 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
6720 return; /* We are on the editable row */
6722 if (!info->path_from_entry
6723 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
6724 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
6728 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
6730 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6731 struct get_paths_closure info;
6732 GtkWindow *toplevel;
6733 GtkWidget *current_focus;
6737 info.path_from_entry = NULL;
6739 toplevel = get_toplevel (GTK_WIDGET (impl));
6741 current_focus = gtk_window_get_focus (toplevel);
6743 current_focus = NULL;
6745 if (current_focus == impl->browse_files_tree_view)
6747 GtkTreeSelection *selection;
6751 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6752 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
6754 /* If there is no selection in the file list, we probably have this situation:
6756 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
6757 * 2. He then double-clicked on a folder ("bar") in the file list
6759 * So we want the selection to be "bar/foo.txt". Jump to the case for the
6760 * filename entry to see if that is the case.
6762 if (info.result == NULL && impl->location_entry)
6765 else if (impl->location_entry && current_focus == impl->location_entry)
6767 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
6771 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
6776 if (!is_well_formed)
6779 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6781 gtk_file_path_free (info.path_from_entry);
6785 g_assert (info.path_from_entry != NULL);
6786 info.result = g_slist_prepend (info.result, info.path_from_entry);
6788 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6790 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
6794 /* The focus is on a dialog's action area button or something else */
6795 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6796 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6804 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
6805 * fall back to the current directory */
6806 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
6807 info.result == NULL)
6809 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
6812 return g_slist_reverse (info.result);
6815 static GtkFilePath *
6816 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
6818 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6820 if (impl->preview_path)
6821 return gtk_file_path_copy (impl->preview_path);
6826 static GtkFileSystem *
6827 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
6829 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6831 return impl->file_system;
6834 /* Shows or hides the filter widgets */
6836 show_filters (GtkFileChooserDefault *impl,
6840 gtk_widget_show (impl->filter_combo_hbox);
6842 gtk_widget_hide (impl->filter_combo_hbox);
6846 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
6847 GtkFileFilter *filter)
6849 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6852 if (g_slist_find (impl->filters, filter))
6854 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
6858 g_object_ref_sink (filter);
6859 impl->filters = g_slist_append (impl->filters, filter);
6861 name = gtk_file_filter_get_name (filter);
6863 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
6865 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
6867 if (!g_slist_find (impl->filters, impl->current_filter))
6868 set_current_filter (impl, filter);
6870 show_filters (impl, TRUE);
6874 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
6875 GtkFileFilter *filter)
6877 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6878 GtkTreeModel *model;
6882 filter_index = g_slist_index (impl->filters, filter);
6884 if (filter_index < 0)
6886 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
6890 impl->filters = g_slist_remove (impl->filters, filter);
6892 if (filter == impl->current_filter)
6895 set_current_filter (impl, impl->filters->data);
6897 set_current_filter (impl, NULL);
6900 /* Remove row from the combo box */
6901 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
6902 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
6903 g_assert_not_reached ();
6905 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
6907 g_object_unref (filter);
6910 show_filters (impl, FALSE);
6914 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
6916 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6918 return g_slist_copy (impl->filters);
6921 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
6923 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
6926 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
6929 struct AddShortcutData
6931 GtkFileChooserDefault *impl;
6936 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
6937 const GtkFileInfo *info,
6938 const GError *error,
6942 gboolean cancelled = handle->cancelled;
6943 struct AddShortcutData *data = user_data;
6945 if (!g_slist_find (data->impl->loading_shortcuts, handle))
6948 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
6950 if (cancelled || error || !gtk_file_info_get_is_folder (info))
6953 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
6955 shortcuts_insert_path (data->impl, pos, FALSE, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
6958 g_object_unref (data->impl);
6959 gtk_file_path_free (data->path);
6962 g_object_unref (handle);
6966 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
6967 const GtkFilePath *path,
6970 GtkFileSystemHandle *handle;
6971 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6972 struct AddShortcutData *data;
6976 /* Avoid adding duplicates */
6977 pos = shortcut_find_position (impl, path);
6978 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
6982 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6983 /* translators, "Shortcut" means "Bookmark" here */
6985 GTK_FILE_CHOOSER_ERROR,
6986 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6987 _("Shortcut %s already exists"),
6994 for (l = impl->loading_shortcuts; l; l = l->next)
6996 GtkFileSystemHandle *h = l->data;
6999 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7000 if (p && !gtk_file_path_compare (path, p))
7004 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7006 GTK_FILE_CHOOSER_ERROR,
7007 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7008 _("Shortcut %s already exists"),
7016 data = g_new0 (struct AddShortcutData, 1);
7017 data->impl = g_object_ref (impl);
7018 data->path = gtk_file_path_copy (path);
7020 handle = gtk_file_system_get_info (impl->file_system, path,
7021 GTK_FILE_INFO_IS_FOLDER,
7022 add_shortcut_get_info_cb, data);
7027 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
7028 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
7034 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7035 const GtkFilePath *path,
7038 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7045 for (l = impl->loading_shortcuts; l; l = l->next)
7047 GtkFileSystemHandle *h = l->data;
7050 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7051 if (p && !gtk_file_path_compare (path, p))
7053 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7054 gtk_file_system_cancel_operation (h);
7059 if (impl->num_shortcuts == 0)
7062 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7063 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7064 g_assert_not_reached ();
7066 for (i = 0; i < impl->num_shortcuts; i++)
7070 GtkFilePath *shortcut;
7072 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7073 SHORTCUTS_COL_DATA, &col_data,
7074 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7076 g_assert (col_data != NULL);
7077 g_assert (!is_volume);
7079 shortcut = col_data;
7080 if (gtk_file_path_compare (shortcut, path) == 0)
7082 shortcuts_remove_rows (impl, pos + i, 1);
7083 impl->num_shortcuts--;
7087 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7088 g_assert_not_reached ();
7093 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7094 /* translators, "Shortcut" means "Bookmark" here */
7096 GTK_FILE_CHOOSER_ERROR,
7097 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7098 _("Shortcut %s does not exist"),
7106 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7108 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7114 if (impl->num_shortcuts == 0)
7117 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7118 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7119 g_assert_not_reached ();
7123 for (i = 0; i < impl->num_shortcuts; i++)
7127 GtkFilePath *shortcut;
7129 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7130 SHORTCUTS_COL_DATA, &col_data,
7131 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7133 g_assert (col_data != NULL);
7134 g_assert (!is_volume);
7136 shortcut = col_data;
7137 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7139 if (i != impl->num_shortcuts - 1)
7141 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7142 g_assert_not_reached ();
7146 return g_slist_reverse (list);
7149 /* Guesses a size based upon font sizes */
7151 find_good_size_from_style (GtkWidget *widget,
7155 GtkFileChooserDefault *impl;
7156 gint default_width, default_height;
7162 g_assert (widget->style != NULL);
7163 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7165 screen = gtk_widget_get_screen (widget);
7168 resolution = gdk_screen_get_resolution (screen);
7169 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7173 resolution = 96.0; /* wheeee */
7175 font_size = pango_font_description_get_size (widget->style->font_desc);
7176 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7178 default_width = font_size * NUM_CHARS;
7179 default_height = font_size * NUM_LINES;
7181 if (impl->preview_widget_active && impl->preview_widget)
7183 gtk_widget_size_request (impl->preview_box, &req);
7184 default_width += PREVIEW_HBOX_SPACING + req.width;
7187 if (impl->extra_widget)
7189 gtk_widget_size_request (impl->extra_align, &req);
7190 default_height += GTK_BOX (widget)->spacing + req.height;
7193 gtk_widget_size_request (widget, &req);
7194 default_width = MAX (default_width, req.width);
7195 default_height = MAX (default_height, req.height);
7197 *width = default_width;
7198 *height = default_height;
7202 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7203 gint *default_width,
7204 gint *default_height)
7206 GtkFileChooserDefault *impl;
7208 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7209 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7213 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
7214 gboolean *resize_horizontally,
7215 gboolean *resize_vertically)
7217 GtkFileChooserDefault *impl;
7219 g_return_if_fail (resize_horizontally != NULL);
7220 g_return_if_fail (resize_vertically != NULL);
7222 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7224 *resize_horizontally = TRUE;
7225 *resize_vertically = TRUE;
7227 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7228 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7230 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7232 *resize_horizontally = FALSE;
7233 *resize_vertically = FALSE;
7238 struct switch_folder_closure {
7239 GtkFileChooserDefault *impl;
7240 const GtkFilePath *path;
7244 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7246 switch_folder_foreach_cb (GtkTreeModel *model,
7251 struct switch_folder_closure *closure;
7252 GtkTreeIter child_iter;
7256 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7258 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7259 closure->num_selected++;
7262 /* Changes to the selected folder in the list view */
7264 switch_to_selected_folder (GtkFileChooserDefault *impl)
7266 GtkTreeSelection *selection;
7267 struct switch_folder_closure closure;
7269 /* We do this with foreach() rather than get_selected() as we may be in
7270 * multiple selection mode
7273 closure.impl = impl;
7274 closure.path = NULL;
7275 closure.num_selected = 0;
7277 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7278 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7280 g_assert (closure.path && closure.num_selected == 1);
7282 change_folder_and_display_error (impl, closure.path);
7285 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7288 static const GtkFileInfo *
7289 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7290 gboolean *had_selection)
7292 GtkTreeSelection *selection;
7293 GtkTreeIter iter, child_iter;
7294 const GtkFileInfo *info;
7296 g_assert (!impl->select_multiple);
7297 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7298 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7300 *had_selection = FALSE;
7304 *had_selection = TRUE;
7306 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7310 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7314 /* Gets the display name of the selected file in the file list; assumes single
7315 * selection mode and that something is selected.
7317 static const gchar *
7318 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7320 const GtkFileInfo *info;
7321 gboolean had_selection;
7323 info = get_selected_file_info_from_file_list (impl, &had_selection);
7324 g_assert (had_selection);
7325 g_assert (info != NULL);
7327 return gtk_file_info_get_display_name (info);
7331 add_custom_button_to_dialog (GtkDialog *dialog,
7332 const gchar *mnemonic_label,
7333 const gchar *stock_id,
7338 button = gtk_button_new_with_mnemonic (mnemonic_label);
7339 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7340 gtk_button_set_image (GTK_BUTTON (button),
7341 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7342 gtk_widget_show (button);
7344 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7347 /* Presents an overwrite confirmation dialog; returns whether we should accept
7351 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7352 const gchar *file_part,
7353 const gchar *folder_display_name)
7355 GtkWindow *toplevel;
7359 toplevel = get_toplevel (GTK_WIDGET (impl));
7361 dialog = gtk_message_dialog_new (toplevel,
7362 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7363 GTK_MESSAGE_QUESTION,
7365 _("A file named \"%s\" already exists. Do you want to replace it?"),
7367 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7368 _("The file already exists in \"%s\". Replacing it will "
7369 "overwrite its contents."),
7370 folder_display_name);
7372 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7373 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7374 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7376 if (toplevel->group)
7377 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7379 response = gtk_dialog_run (GTK_DIALOG (dialog));
7381 gtk_widget_destroy (dialog);
7383 return (response == GTK_RESPONSE_ACCEPT);
7386 struct GetDisplayNameData
7388 GtkFileChooserDefault *impl;
7393 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7394 const GtkFileInfo *info,
7395 const GError *error,
7398 gboolean cancelled = handle->cancelled;
7399 gboolean should_respond = FALSE;
7400 struct GetDisplayNameData *data = user_data;
7402 if (handle != data->impl->should_respond_get_info_handle)
7405 data->impl->should_respond_get_info_handle = NULL;
7411 /* Huh? Did the folder disappear? Let the caller deal with it */
7412 should_respond = TRUE;
7414 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7416 set_busy_cursor (data->impl, FALSE);
7418 g_signal_emit_by_name (data->impl, "response-requested");
7421 g_object_unref (data->impl);
7422 g_free (data->file_part);
7425 g_object_unref (handle);
7428 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7429 * should respond. Can get the file part from the file list or the save entry.
7432 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7433 const gchar *file_part,
7434 const GtkFilePath *parent_path)
7436 GtkFileChooserConfirmation conf;
7438 if (!impl->do_overwrite_confirmation)
7441 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7443 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7447 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7449 struct GetDisplayNameData *data;
7451 g_assert (file_part != NULL);
7453 data = g_new0 (struct GetDisplayNameData, 1);
7454 data->impl = g_object_ref (impl);
7455 data->file_part = g_strdup (file_part);
7457 if (impl->should_respond_get_info_handle)
7458 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7460 impl->should_respond_get_info_handle =
7461 gtk_file_system_get_info (impl->file_system, parent_path,
7462 GTK_FILE_INFO_DISPLAY_NAME,
7463 confirmation_confirm_get_info_cb,
7465 set_busy_cursor (data->impl, TRUE);
7469 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7472 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7476 g_assert_not_reached ();
7482 action_create_folder_cb (GtkFileSystemHandle *handle,
7483 const GtkFilePath *path,
7484 const GError *error,
7487 gboolean cancelled = handle->cancelled;
7488 GtkFileChooserDefault *impl = user_data;
7490 if (!g_slist_find (impl->pending_handles, handle))
7493 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
7495 set_busy_cursor (impl, FALSE);
7501 error_creating_folder_dialog (impl, path, g_error_copy (error));
7503 g_signal_emit_by_name (impl, "response-requested");
7506 g_object_unref (impl);
7507 g_object_unref (handle);
7510 struct FileExistsData
7512 GtkFileChooserDefault *impl;
7513 gboolean file_exists_and_is_not_folder;
7514 GtkFilePath *parent_path;
7519 save_entry_get_info_cb (GtkFileSystemHandle *handle,
7520 const GtkFileInfo *info,
7521 const GError *error,
7524 gboolean parent_is_folder;
7525 gboolean cancelled = handle->cancelled;
7526 struct FileExistsData *data = user_data;
7528 if (handle != data->impl->should_respond_get_info_handle)
7531 data->impl->should_respond_get_info_handle = NULL;
7533 set_busy_cursor (data->impl, FALSE);
7539 parent_is_folder = FALSE;
7541 parent_is_folder = gtk_file_info_get_is_folder (info);
7543 if (parent_is_folder)
7545 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7547 if (data->file_exists_and_is_not_folder)
7550 const char *file_part;
7552 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
7553 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
7556 g_signal_emit_by_name (data->impl, "response-requested");
7559 g_signal_emit_by_name (data->impl, "response-requested");
7561 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
7563 GtkFileSystemHandle *handle;
7565 g_object_ref (data->impl);
7566 handle = gtk_file_system_create_folder (data->impl->file_system,
7568 action_create_folder_cb,
7570 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
7571 set_busy_cursor (data->impl, TRUE);
7576 /* This will display an error, which is what we want */
7577 change_folder_and_display_error (data->impl, data->parent_path);
7581 g_object_unref (data->impl);
7582 gtk_file_path_free (data->path);
7583 gtk_file_path_free (data->parent_path);
7586 g_object_unref (handle);
7590 file_exists_get_info_cb (GtkFileSystemHandle *handle,
7591 const GtkFileInfo *info,
7592 const GError *error,
7595 gboolean data_ownership_taken = FALSE;
7596 gboolean cancelled = handle->cancelled;
7597 gboolean file_exists_and_is_not_folder;
7598 struct FileExistsData *data = user_data;
7600 if (handle != data->impl->file_exists_get_info_handle)
7603 data->impl->file_exists_get_info_handle = NULL;
7605 set_busy_cursor (data->impl, FALSE);
7610 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
7612 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
7613 /* user typed a filename; we are done */
7614 g_signal_emit_by_name (data->impl, "response-requested");
7615 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7616 && file_exists_and_is_not_folder)
7618 /* Oops, the user typed the name of an existing path which is not
7621 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
7622 g_error_copy (error));
7626 /* check that everything up to the last component exists */
7628 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
7629 data_ownership_taken = TRUE;
7631 if (data->impl->should_respond_get_info_handle)
7632 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
7634 data->impl->should_respond_get_info_handle =
7635 gtk_file_system_get_info (data->impl->file_system,
7637 GTK_FILE_INFO_IS_FOLDER,
7638 save_entry_get_info_cb,
7640 set_busy_cursor (data->impl, TRUE);
7644 if (!data_ownership_taken)
7646 g_object_unref (data->impl);
7647 gtk_file_path_free (data->path);
7648 gtk_file_path_free (data->parent_path);
7652 g_object_unref (handle);
7656 paste_text_received (GtkClipboard *clipboard,
7658 GtkFileChooserDefault *impl)
7665 path = gtk_file_system_uri_to_path (impl->file_system, text);
7668 if (!g_path_is_absolute (text))
7670 location_popup_handler (impl, text);
7674 path = gtk_file_system_filename_to_path (impl->file_system, text);
7677 location_popup_handler (impl, text);
7682 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
7683 location_popup_handler (impl, text);
7685 gtk_file_path_free (path);
7688 /* Handler for the "location-popup-on-paste" keybinding signal */
7690 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
7692 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
7693 GDK_SELECTION_CLIPBOARD);
7694 gtk_clipboard_request_text (clipboard,
7695 (GtkClipboardTextReceivedFunc) paste_text_received,
7700 /* Implementation for GtkFileChooserEmbed::should_respond() */
7702 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
7704 GtkFileChooserDefault *impl;
7705 GtkWidget *toplevel;
7706 GtkWidget *current_focus;
7708 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7710 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7711 g_assert (GTK_IS_WINDOW (toplevel));
7713 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
7715 if (current_focus == impl->browse_files_tree_view)
7717 /* The following array encodes what we do based on the impl->action and the
7718 * number of files selected.
7721 NOOP, /* Do nothing (don't respond) */
7722 RESPOND, /* Respond immediately */
7723 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
7724 ALL_FILES, /* Respond only if everything selected is a file */
7725 ALL_FOLDERS, /* Respond only if everything selected is a folder */
7726 SAVE_ENTRY, /* Go to the code for handling the save entry */
7727 NOT_REACHED /* Sanity check */
7729 static const ActionToTake what_to_do[4][3] = {
7730 /* 0 selected 1 selected many selected */
7731 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
7732 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
7733 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
7734 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
7738 gboolean all_files, all_folders;
7740 ActionToTake action;
7744 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7746 selection_check (impl, &num_selected, &all_files, &all_folders);
7748 if (num_selected > 2)
7753 action = what_to_do [impl->action] [k];
7763 case RESPOND_OR_SWITCH:
7764 g_assert (num_selected == 1);
7768 switch_to_selected_folder (impl);
7771 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7772 return should_respond_after_confirm_overwrite (impl,
7773 get_display_name_from_file_list (impl),
7774 impl->current_folder);
7788 g_assert_not_reached ();
7791 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
7794 gboolean is_well_formed, is_empty, is_file_part_empty;
7797 GtkFileChooserEntry *entry;
7802 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7803 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7804 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7805 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7806 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7808 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7809 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7811 if (is_empty || !is_well_formed)
7814 g_assert (path != NULL);
7819 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7820 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7822 _gtk_file_chooser_entry_set_file_part (entry, "");
7823 change_folder_and_display_error (impl, path);
7826 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7827 || GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7829 /* The folder already exists, so we do not need to create it.
7830 * Just respond to terminate the dialog.
7836 g_assert_not_reached ();
7842 struct FileExistsData *data;
7844 /* We need to check whether path exists and is not a folder */
7846 data = g_new0 (struct FileExistsData, 1);
7847 data->impl = g_object_ref (impl);
7848 data->path = gtk_file_path_copy (path);
7849 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
7851 if (impl->file_exists_get_info_handle)
7852 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
7854 impl->file_exists_get_info_handle =
7855 gtk_file_system_get_info (impl->file_system, path,
7856 GTK_FILE_INFO_IS_FOLDER,
7857 file_exists_get_info_cb,
7860 set_busy_cursor (impl, TRUE);
7864 g_error_free (error);
7867 gtk_file_path_free (path);
7870 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
7872 /* The focus is on a dialog's action area button, *and* the widget that
7873 * was focused immediately before it is the shortcuts list. Switch to the
7874 * selected shortcut and tell the caller not to respond.
7878 if (shortcuts_get_selected (impl, &iter))
7880 shortcuts_activate_iter (impl, &iter);
7882 gtk_widget_grab_focus (impl->browse_files_tree_view);
7889 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7891 /* The focus is on a dialog's action area button, *and* the widget that
7892 * was focused immediately before it is the file list.
7896 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7898 /* The focus is on a dialog's action area button, *and* the widget that
7899 * was focused immediately before it is the location entry.
7904 /* The focus is on a dialog's action area button or something else */
7905 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7906 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7911 g_assert_not_reached ();
7915 /* Implementation for GtkFileChooserEmbed::initial_focus() */
7917 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
7919 GtkFileChooserDefault *impl;
7922 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7924 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7925 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7927 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
7928 widget = impl->browse_files_tree_view;
7930 widget = impl->location_entry;
7932 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7933 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7934 widget = impl->location_entry;
7937 g_assert_not_reached ();
7941 g_assert (widget != NULL);
7942 gtk_widget_grab_focus (widget);
7946 set_current_filter (GtkFileChooserDefault *impl,
7947 GtkFileFilter *filter)
7949 if (impl->current_filter != filter)
7953 /* NULL filters are allowed to reset to non-filtered status
7955 filter_index = g_slist_index (impl->filters, filter);
7956 if (impl->filters && filter && filter_index < 0)
7959 if (impl->current_filter)
7960 g_object_unref (impl->current_filter);
7961 impl->current_filter = filter;
7962 if (impl->current_filter)
7964 g_object_ref_sink (impl->current_filter);
7968 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
7971 if (impl->browse_files_model)
7972 install_list_model_filter (impl);
7974 g_object_notify (G_OBJECT (impl), "filter");
7979 filter_combo_changed (GtkComboBox *combo_box,
7980 GtkFileChooserDefault *impl)
7982 gint new_index = gtk_combo_box_get_active (combo_box);
7983 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
7985 set_current_filter (impl, new_filter);
7989 check_preview_change (GtkFileChooserDefault *impl)
7991 GtkTreePath *cursor_path;
7992 const GtkFilePath *new_path;
7993 const GtkFileInfo *new_info;
7995 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
7996 if (cursor_path && impl->sort_model)
7999 GtkTreeIter child_iter;
8001 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path))
8002 g_assert_not_reached ();
8004 gtk_tree_path_free (cursor_path);
8006 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8008 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8009 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8017 if (new_path != impl->preview_path &&
8018 !(new_path && impl->preview_path &&
8019 gtk_file_path_compare (new_path, impl->preview_path) == 0))
8021 if (impl->preview_path)
8023 gtk_file_path_free (impl->preview_path);
8024 g_free (impl->preview_display_name);
8029 impl->preview_path = gtk_file_path_copy (new_path);
8030 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
8034 impl->preview_path = NULL;
8035 impl->preview_display_name = NULL;
8038 if (impl->use_preview_label && impl->preview_label)
8039 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
8041 g_signal_emit_by_name (impl, "update-preview");
8046 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
8047 GtkFileSystemVolume *volume,
8048 const GError *error,
8052 gboolean cancelled = handle->cancelled;
8053 GtkFileChooserDefault *impl = data;
8055 if (handle != impl->shortcuts_activate_iter_handle)
8058 impl->shortcuts_activate_iter_handle = NULL;
8060 set_busy_cursor (impl, FALSE);
8069 msg = g_strdup_printf (_("Could not mount %s"),
8070 gtk_file_system_volume_get_display_name (impl->file_system, volume));
8071 error_message (impl, msg, error->message);
8077 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8080 change_folder_and_display_error (impl, path);
8081 gtk_file_path_free (path);
8085 g_object_unref (impl);
8086 g_object_unref (handle);
8090 /* Activates a volume by mounting it if necessary and then switching to its
8094 shortcuts_activate_volume (GtkFileChooserDefault *impl,
8095 GtkFileSystemVolume *volume)
8099 /* We ref the file chooser since volume_mount() may run a main loop, and the
8100 * user could close the file chooser window in the meantime.
8102 g_object_ref (impl);
8104 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
8106 set_busy_cursor (impl, TRUE);
8108 impl->shortcuts_activate_iter_handle =
8109 gtk_file_system_volume_mount (impl->file_system, volume,
8110 shortcuts_activate_volume_mount_cb,
8111 g_object_ref (impl));
8115 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8118 change_folder_and_display_error (impl, path);
8119 gtk_file_path_free (path);
8123 g_object_unref (impl);
8126 /* Opens the folder or volume at the specified iter in the shortcuts model */
8127 struct ShortcutsActivateData
8129 GtkFileChooserDefault *impl;
8134 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
8135 const GtkFileInfo *info,
8136 const GError *error,
8139 gboolean cancelled = handle->cancelled;
8140 struct ShortcutsActivateData *data = user_data;
8142 if (handle != data->impl->shortcuts_activate_iter_handle)
8145 data->impl->shortcuts_activate_iter_handle = NULL;
8150 if (!error && gtk_file_info_get_is_folder (info))
8151 change_folder_and_display_error (data->impl, data->path);
8153 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl), data->path, NULL);
8156 g_object_unref (data->impl);
8157 gtk_file_path_free (data->path);
8160 g_object_unref (handle);
8164 shortcuts_activate_iter (GtkFileChooserDefault *impl,
8170 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
8171 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
8173 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
8174 SHORTCUTS_COL_DATA, &col_data,
8175 SHORTCUTS_COL_IS_VOLUME, &is_volume,
8179 return; /* We are on a separator */
8181 if (impl->shortcuts_activate_iter_handle)
8183 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
8184 impl->shortcuts_activate_iter_handle = NULL;
8189 GtkFileSystemVolume *volume;
8193 shortcuts_activate_volume (impl, volume);
8197 struct ShortcutsActivateData *data;
8199 data = g_new0 (struct ShortcutsActivateData, 1);
8200 data->impl = g_object_ref (impl);
8201 data->path = gtk_file_path_copy (col_data);
8203 impl->shortcuts_activate_iter_handle =
8204 gtk_file_system_get_info (impl->file_system, data->path,
8205 GTK_FILE_INFO_IS_FOLDER,
8206 shortcuts_activate_get_info_cb, data);
8210 /* Callback used when a row in the shortcuts list is activated */
8212 shortcuts_row_activated_cb (GtkTreeView *tree_view,
8214 GtkTreeViewColumn *column,
8215 GtkFileChooserDefault *impl)
8218 GtkTreeIter child_iter;
8220 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
8223 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
8226 shortcuts_activate_iter (impl, &child_iter);
8228 gtk_widget_grab_focus (impl->browse_files_tree_view);
8231 /* Handler for GtkWidget::key-press-event on the shortcuts list */
8233 shortcuts_key_press_event_cb (GtkWidget *widget,
8235 GtkFileChooserDefault *impl)
8239 modifiers = gtk_accelerator_get_default_mod_mask ();
8241 if ((event->keyval == GDK_BackSpace
8242 || event->keyval == GDK_Delete
8243 || event->keyval == GDK_KP_Delete)
8244 && (event->state & modifiers) == 0)
8246 remove_selected_bookmarks (impl);
8250 if ((event->keyval == GDK_F2)
8251 && (event->state & modifiers) == 0)
8253 rename_selected_bookmark (impl);
8261 shortcuts_select_func (GtkTreeSelection *selection,
8262 GtkTreeModel *model,
8264 gboolean path_currently_selected,
8267 GtkFileChooserDefault *impl = data;
8269 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
8273 list_select_func (GtkTreeSelection *selection,
8274 GtkTreeModel *model,
8276 gboolean path_currently_selected,
8279 GtkFileChooserDefault *impl = data;
8281 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8282 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8284 GtkTreeIter iter, child_iter;
8285 const GtkFileInfo *info;
8287 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8290 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8292 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8294 if (info && !gtk_file_info_get_is_folder (info))
8302 list_selection_changed (GtkTreeSelection *selection,
8303 GtkFileChooserDefault *impl)
8305 /* See if we are in the new folder editable row for Save mode */
8306 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8308 const GtkFileInfo *info;
8309 gboolean had_selection;
8311 info = get_selected_file_info_from_file_list (impl, &had_selection);
8313 goto out; /* normal processing */
8316 return; /* We are on the editable row for New Folder */
8321 if (impl->location_entry)
8322 update_chooser_entry (impl);
8323 check_preview_change (impl);
8324 bookmarks_check_add_sensitivity (impl);
8326 g_signal_emit_by_name (impl, "selection-changed", 0);
8329 /* Callback used when a row in the file list is activated */
8331 list_row_activated (GtkTreeView *tree_view,
8333 GtkTreeViewColumn *column,
8334 GtkFileChooserDefault *impl)
8336 GtkTreeIter iter, child_iter;
8337 const GtkFileInfo *info;
8339 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8342 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8344 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8346 if (gtk_file_info_get_is_folder (info))
8348 const GtkFilePath *file_path;
8350 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8351 change_folder_and_display_error (impl, file_path);
8356 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8357 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8358 g_signal_emit_by_name (impl, "file-activated");
8362 path_bar_clicked (GtkPathBar *path_bar,
8363 GtkFilePath *file_path,
8364 GtkFilePath *child_path,
8365 gboolean child_is_hidden,
8366 GtkFileChooserDefault *impl)
8369 pending_select_paths_add (impl, child_path);
8371 if (!change_folder_and_display_error (impl, file_path))
8374 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
8375 * show hidden files so that ".baz" appears in the file list, as it will still
8376 * be shown in the path bar: "/foo/[bar]/.baz"
8378 if (child_is_hidden)
8379 g_object_set (impl, "show-hidden", TRUE, NULL);
8382 static const GtkFileInfo *
8383 get_list_file_info (GtkFileChooserDefault *impl,
8386 GtkTreeIter child_iter;
8388 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8392 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8396 list_icon_data_func (GtkTreeViewColumn *tree_column,
8397 GtkCellRenderer *cell,
8398 GtkTreeModel *tree_model,
8402 GtkFileChooserDefault *impl = data;
8403 GtkTreeIter child_iter;
8404 const GtkFilePath *path;
8406 const GtkFileInfo *info;
8407 gboolean sensitive = TRUE;
8409 profile_start ("start", NULL);
8411 info = get_list_file_info (impl, iter);
8413 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8416 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8424 /* FIXME: NULL GError */
8425 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
8426 impl->icon_size, NULL);
8431 /* We are on the editable row */
8435 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8436 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
8437 sensitive = gtk_file_info_get_is_folder (info);
8441 "sensitive", sensitive,
8445 g_object_unref (pixbuf);
8447 profile_end ("end", NULL);
8451 list_name_data_func (GtkTreeViewColumn *tree_column,
8452 GtkCellRenderer *cell,
8453 GtkTreeModel *tree_model,
8457 GtkFileChooserDefault *impl = data;
8458 const GtkFileInfo *info = get_list_file_info (impl, iter);
8459 gboolean sensitive = TRUE;
8464 "text", _("Type name of new folder"),
8471 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8472 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8474 sensitive = gtk_file_info_get_is_folder (info);
8478 "text", gtk_file_info_get_display_name (info),
8479 "sensitive", sensitive,
8485 list_size_data_func (GtkTreeViewColumn *tree_column,
8486 GtkCellRenderer *cell,
8487 GtkTreeModel *tree_model,
8491 GtkFileChooserDefault *impl = data;
8492 const GtkFileInfo *info = get_list_file_info (impl, iter);
8495 gboolean sensitive = TRUE;
8497 if (!info || gtk_file_info_get_is_folder (info))
8501 "sensitive", sensitive,
8506 size = gtk_file_info_get_size (info);
8508 if (size < (gint64)1024)
8509 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
8510 else if (size < (gint64)1024*1024)
8511 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
8512 else if (size < (gint64)1024*1024*1024)
8513 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
8515 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
8517 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
8518 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8519 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8524 "sensitive", sensitive,
8525 "alignment", PANGO_ALIGN_RIGHT,
8532 /* Tree column data callback for the file list; fetches the mtime of a file */
8534 list_mtime_data_func (GtkTreeViewColumn *tree_column,
8535 GtkCellRenderer *cell,
8536 GtkTreeModel *tree_model,
8540 GtkFileChooserDefault *impl;
8541 const GtkFileInfo *info;
8542 GtkFileTime time_mtime;
8546 gboolean sensitive = TRUE;
8550 info = get_list_file_info (impl, iter);
8560 time_mtime = gtk_file_info_get_modification_time (info);
8562 if (time_mtime == 0)
8563 strcpy (buf, _("Unknown"));
8567 g_date_set_time_t (&mtime, time_mtime);
8568 time_now = time (NULL);
8569 g_date_set_time_t (&now, time_now);
8571 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
8574 strcpy (buf, _("Today"));
8575 else if (days_diff == 1)
8576 strcpy (buf, _("Yesterday"));
8581 if (days_diff > 1 && days_diff < 7)
8582 format = "%A"; /* Days from last week */
8584 format = "%x"; /* Any other date */
8586 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
8587 strcpy (buf, _("Unknown"));
8591 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8592 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8593 sensitive = gtk_file_info_get_is_folder (info);
8597 "sensitive", sensitive,
8602 _gtk_file_chooser_default_new (const char *file_system)
8604 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
8605 "file-system-backend", file_system,
8610 location_set_user_text (GtkFileChooserDefault *impl,
8613 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
8614 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
8618 location_popup_handler (GtkFileChooserDefault *impl,
8621 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8622 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8624 LocationMode new_mode;
8628 /* since the user typed something, we unconditionally want to turn on the entry */
8629 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8631 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8632 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8633 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8634 new_mode = LOCATION_MODE_PATH_BAR;
8637 g_assert_not_reached ();
8641 location_mode_set (impl, new_mode, TRUE);
8642 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
8645 location_set_user_text (impl, path);
8648 location_entry_set_initial_text (impl);
8649 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
8653 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8654 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8656 gtk_widget_grab_focus (impl->location_entry);
8658 location_set_user_text (impl, path);
8661 g_assert_not_reached ();
8664 /* Handler for the "up-folder" keybinding signal */
8666 up_folder_handler (GtkFileChooserDefault *impl)
8668 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
8671 /* Handler for the "down-folder" keybinding signal */
8673 down_folder_handler (GtkFileChooserDefault *impl)
8675 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
8678 /* Switches to the shortcut in the specified index */
8680 switch_to_shortcut (GtkFileChooserDefault *impl,
8685 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8686 g_assert_not_reached ();
8688 shortcuts_activate_iter (impl, &iter);
8691 /* Handler for the "home-folder" keybinding signal */
8693 home_folder_handler (GtkFileChooserDefault *impl)
8696 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
8699 /* Handler for the "desktop-folder" keybinding signal */
8701 desktop_folder_handler (GtkFileChooserDefault *impl)
8703 if (impl->has_desktop)
8704 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
8708 quick_bookmark_handler (GtkFileChooserDefault *impl,
8709 gint bookmark_index)
8714 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
8717 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
8719 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
8720 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
8723 gtk_tree_path_free (path);
8725 switch_to_shortcut (impl, bookmark_pos);
8730 /* Drag and drop interfaces */
8733 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
8738 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
8743 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
8745 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
8748 ShortcutsModelFilter *model;
8752 model = SHORTCUTS_MODEL_FILTER (drag_source);
8754 pos = *gtk_tree_path_get_indices (path);
8755 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
8757 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
8760 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
8762 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
8764 GtkSelectionData *selection_data)
8766 ShortcutsModelFilter *model;
8768 model = SHORTCUTS_MODEL_FILTER (drag_source);
8775 /* Fill the GtkTreeDragSourceIface vtable */
8777 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
8779 iface->row_draggable = shortcuts_model_filter_row_draggable;
8780 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
8784 /* Fill the GtkTreeDragDestIface vtable */
8786 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
8788 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
8789 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
8793 static GtkTreeModel *
8794 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
8795 GtkTreeModel *child_model,
8798 ShortcutsModelFilter *model;
8800 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
8801 "child-model", child_model,
8802 "virtual-root", root,
8807 return GTK_TREE_MODEL (model);