gtkfilechooserwidget.c 279 KB
Newer Older
1
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
Cody Russell's avatar
Cody Russell committed
2
/* GTK - The GIMP Toolkit
Owen Taylor's avatar
Owen Taylor committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * gtkfilechooserwidget.c: Embeddable file selector widget
 * Copyright (C) 2003, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Owen Taylor's avatar
Owen Taylor committed
18 19
 */

20
#include "config.h"
Matthias Clasen's avatar
Matthias Clasen committed
21

Owen Taylor's avatar
Owen Taylor committed
22
#include "gtkfilechooserwidget.h"
23
#include "gtkfilechooserwidgetprivate.h"
24 25

#include "gtkbindings.h"
26
#include "gtkbutton.h"
27 28 29 30 31 32
#include "gtkcelllayout.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkcheckmenuitem.h"
#include "gtkclipboard.h"
#include "gtkcomboboxtext.h"
33
#include "gtkdragsource.h"
34
#include "gtkdragdest.h"
35 36 37 38
#include "gtkentry.h"
#include "gtkexpander.h"
#include "gtkfilechooserprivate.h"
#include "gtkfilechooserdialog.h"
39
#include "gtkfilechooserembed.h"
40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "gtkfilechooserentry.h"
#include "gtkfilechooserutils.h"
#include "gtkfilechooser.h"
#include "gtkfilesystem.h"
#include "gtkfilesystemmodel.h"
#include "gtkgrid.h"
#include "gtkicontheme.h"
#include "gtklabel.h"
#include "gtkmarshalers.h"
#include "gtkmessagedialog.h"
#include "gtkmountoperation.h"
#include "gtkpaned.h"
#include "gtkpathbar.h"
#include "gtkplacessidebar.h"
54
#include "gtkplacessidebarprivate.h"
55
#include "gtkplacesviewprivate.h"
56 57
#include "gtkprivate.h"
#include "gtkrecentmanager.h"
58
#include "gtksearchentry.h"
59 60 61 62
#include "gtkseparatormenuitem.h"
#include "gtksettings.h"
#include "gtksizegroup.h"
#include "gtksizerequest.h"
63
#include "gtkstack.h"
64 65 66 67 68
#include "gtktooltip.h"
#include "gtktreednd.h"
#include "gtktreeprivate.h"
#include "gtktreeselection.h"
#include "gtkbox.h"
69
#include "gtkwindowgroup.h"
70
#include "gtkintl.h"
71 72
#include "gtkshow.h"
#include "gtkmain.h"
73
#include "gtkscrollable.h"
74
#include "gtkpopover.h"
75
#include "gtkrevealer.h"
76
#include "gtkspinner.h"
77 78 79
#include "gtkseparator.h"
#include "gtkmodelbutton.h"
#include "gtkgesturelongpress.h"
80
#include "gtkdebug.h"
81

82
#include <cairo-gobject.h>
83

84 85 86 87 88 89 90
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef G_OS_WIN32
#include <io.h>
#endif

Matthias Clasen's avatar
Matthias Clasen committed
91 92
/**
 * SECTION:gtkfilechooserwidget
Matthias Clasen's avatar
Matthias Clasen committed
93
 * @Short_description: A file chooser widget
Matthias Clasen's avatar
Matthias Clasen committed
94 95 96 97 98 99 100
 * @Title: GtkFileChooserWidget
 * @See_also: #GtkFileChooserDialog
 *
 * #GtkFileChooserWidget is a widget for choosing files.
 * It exposes the #GtkFileChooser interface, and you should
 * use the methods of this interface to interact with the
 * widget.
101 102 103 104
 *
 * # CSS nodes
 *
 * GtkFileChooserWidget has a single CSS node with name filechooser.
Matthias Clasen's avatar
Matthias Clasen committed
105 106 107
 */


108 109 110 111
/* Values for GtkSelection-related "info" fields */
#define SELECTION_TEXT 0
#define SELECTION_URI  1

112 113 114
/* 150 mseconds of delay */
#define LOCATION_CHANGED_TIMEOUT 150

115 116 117 118 119 120 121 122
/* Profiling stuff */
#undef PROFILE_FILE_CHOOSER
#ifdef PROFILE_FILE_CHOOSER


#ifndef F_OK 
#define F_OK 0
#endif
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137
#define PROFILE_INDENT 4

static int profile_indent;

static void
profile_add_indent (int indent)
{
  profile_indent += indent;
  if (profile_indent < 0)
    g_error ("You screwed up your indentation");
}

void
_gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
138
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  char *str;

  if (indent < 0)
    profile_add_indent (indent);

  if (profile_indent == 0)
    str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
  else
    str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");

  access (str, F_OK);
  g_free (str);

  if (indent > 0)
    profile_add_indent (indent);
}

#define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
#define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
#define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
#else
#define profile_start(x, y)
#define profile_end(x, y)
#define profile_msg(x, y)
#endif

165
enum {
166 167
  PROP_SEARCH_MODE = 1,
  PROP_SUBTITLE
168
};
169 170

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
171 172 173 174
  LOAD_EMPTY,                   /* There is no model */
  LOAD_PRELOAD,                 /* Model is loading and a timer is running; model isn't inserted into the tree yet */
  LOAD_LOADING,                 /* Timeout expired, model is inserted into the tree, but not fully loaded yet */
  LOAD_FINISHED                 /* Model is fully loaded and inserted into the tree */
175 176 177
} LoadState;

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
178 179
  RELOAD_EMPTY,                 /* No folder has been set */
  RELOAD_HAS_FOLDER             /* We have a folder, although it may not be completely loaded yet; no need to reload */
180 181 182 183 184 185 186 187 188 189
} ReloadState;

typedef enum {
  LOCATION_MODE_PATH_BAR,
  LOCATION_MODE_FILENAME_ENTRY
} LocationMode;

typedef enum {
  OPERATION_MODE_BROWSE,
  OPERATION_MODE_SEARCH,
190
  OPERATION_MODE_ENTER_LOCATION,
191
  OPERATION_MODE_OTHER_LOCATIONS,
192 193 194 195 196 197 198 199
  OPERATION_MODE_RECENT
} OperationMode;

typedef enum {
  STARTUP_MODE_RECENT,
  STARTUP_MODE_CWD
} StartupMode;

200 201 202 203 204 205 206 207 208 209
typedef enum {
  CLOCK_FORMAT_24,
  CLOCK_FORMAT_12
} ClockFormat;

typedef enum {
  DATE_FORMAT_REGULAR,
  DATE_FORMAT_WITH_TIME
} DateFormat;

210 211 212 213 214 215 216 217 218 219 220
struct _GtkFileChooserWidgetPrivate {
  GtkFileChooserAction action;

  GtkFileSystem *file_system;

  /* Save mode widgets */
  GtkWidget *save_widgets;
  GtkWidget *save_widgets_table;

  /* The file browsing widgets */
  GtkWidget *browse_widgets_hpaned;
221
  GtkWidget *browse_header_revealer;
222
  GtkWidget *browse_header_stack;
223
  GtkWidget *browse_files_stack;
224
  GtkWidget *browse_files_swin;
225
  GtkWidget *browse_files_tree_view;
226
  GtkWidget *remote_warning_bar;
227 228

  GtkWidget *browse_files_popover;
229 230 231 232 233 234
  GtkWidget *add_shortcut_item;
  GtkWidget *hidden_files_item;
  GtkWidget *size_column_item;
  GtkWidget *copy_file_location_item;
  GtkWidget *visit_file_item;
  GtkWidget *open_folder_item;
235
  GtkWidget *rename_file_item;
236
  GtkWidget *trash_file_item;
237
  GtkWidget *delete_file_item;
238 239
  GtkWidget *sort_directories_item;
  GtkWidget *show_time_item;
240

241 242 243
  GtkWidget *browse_new_folder_button;
  GtkSizeGroup *browse_path_bar_size_group;
  GtkWidget *browse_path_bar;
244 245 246 247
  GtkWidget *new_folder_name_entry;
  GtkWidget *new_folder_create_button;
  GtkWidget *new_folder_error_label;
  GtkWidget *new_folder_popover;
248 249 250 251 252
  GtkWidget *rename_file_name_entry;
  GtkWidget *rename_file_rename_button;
  GtkWidget *rename_file_error_label;
  GtkWidget *rename_file_popover;
  GFile *rename_file_source_file;
253

254 255
  GtkGesture *long_press_gesture;

256 257 258 259
  GtkFileSystemModel *browse_files_model;
  char *browse_files_last_selected_name;

  GtkWidget *places_sidebar;
260
  GtkWidget *places_view;
261 262 263 264
  StartupMode startup_mode;

  /* OPERATION_MODE_SEARCH */
  GtkWidget *search_entry;
265 266
  GtkWidget *search_spinner;
  guint show_progress_timeout;
267 268 269
  GtkSearchEngine *search_engine;
  GtkQuery *search_query;
  GtkFileSystemModel *search_model;
270
  GtkFileSystemModel *model_for_search;
271 272 273 274 275 276

  /* OPERATION_MODE_RECENT */
  GtkRecentManager *recent_manager;
  GtkFileSystemModel *recent_model;
  guint load_recent_id;

277
  GtkWidget *extra_and_filters;
278 279 280 281 282 283 284 285 286 287 288 289
  GtkWidget *filter_combo_hbox;
  GtkWidget *filter_combo;
  GtkWidget *preview_box;
  GtkWidget *preview_label;
  GtkWidget *preview_widget;
  GtkWidget *extra_align;
  GtkWidget *extra_widget;

  GtkWidget *location_entry_box;
  GtkWidget *location_entry;
  LocationMode location_mode;

290 291
  GtkWidget *external_entry;

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  /* Handles */
  GCancellable *file_list_drag_data_received_cancellable;
  GCancellable *update_current_folder_cancellable;
  GCancellable *should_respond_get_info_cancellable;
  GCancellable *file_exists_get_info_cancellable;

  LoadState load_state;
  ReloadState reload_state;
  guint load_timeout_id;

  OperationMode operation_mode;

  GSList *pending_select_files;

  GtkFileFilter *current_filter;
  GSList *filters;

  GtkBookmarksManager *bookmarks_manager;

  int num_volumes;
  int num_shortcuts;
  int num_bookmarks;

  gulong volumes_changed_id;
  gulong bookmarks_changed_id;

  GFile *current_volume_file;
  GFile *current_folder;
  GFile *preview_file;
  char *preview_display_name;

  GtkTreeViewColumn *list_name_column;
  GtkCellRenderer *list_name_renderer;
  GtkCellRenderer *list_pixbuf_renderer;
326
  GtkTreeViewColumn *list_time_column;
327
  GtkCellRenderer *list_date_renderer;
328
  GtkCellRenderer *list_time_renderer;
329
  GtkTreeViewColumn *list_size_column;
330
  GtkCellRenderer *list_size_renderer;
331
  GtkTreeViewColumn *list_location_column;
332
  GtkCellRenderer *list_location_renderer;
333

334 335
  guint location_changed_id;

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  gulong settings_signal_id;
  int icon_size;

  GSource *focus_entry_idle;

  gulong toplevel_set_focus_id;
  GtkWidget *toplevel_last_focus_widget;

  gint sort_column;
  GtkSortType sort_order;

  /* Flags */

  guint local_only : 1;
  guint preview_widget_active : 1;
  guint use_preview_label : 1;
  guint select_multiple : 1;
  guint show_hidden : 1;
354
  guint show_hidden_set : 1;
355
  guint sort_directories_first : 1;
356
  guint show_time : 1;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  guint do_overwrite_confirmation : 1;
  guint list_sort_ascending : 1;
  guint shortcuts_current_folder_active : 1;
  guint show_size_column : 1;
  guint create_folders : 1;
  guint auto_selecting_first_row : 1;
};

#define MAX_LOADING_TIME 500

#define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")

/* Signal IDs */
enum {
  LOCATION_POPUP,
  LOCATION_POPUP_ON_PASTE,
  UP_FOLDER,
  DOWN_FOLDER,
  HOME_FOLDER,
  DESKTOP_FOLDER,
  QUICK_BOOKMARK,
  LOCATION_TOGGLE_POPUP,
  SHOW_HIDDEN,
  SEARCH_SHORTCUT,
  RECENT_SHORTCUT,
382
  PLACES_SHORTCUT,
383 384 385 386 387 388 389 390

  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

#define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
                         "standard::is-hidden,standard::is-backup,standard::size," \
391
                         "standard::content-type,time::modified,time::access," \
392 393
                         "access::can-rename,access::can-delete,access::can-trash," \
                         "standard::target-uri"
394 395 396 397
enum {
  /* the first 3 must be these due to settings caching sort column */
  MODEL_COL_NAME,
  MODEL_COL_SIZE,
398
  MODEL_COL_TIME,
399 400 401 402 403 404
  MODEL_COL_FILE,
  MODEL_COL_NAME_COLLATED,
  MODEL_COL_IS_FOLDER,
  MODEL_COL_IS_SENSITIVE,
  MODEL_COL_SURFACE,
  MODEL_COL_SIZE_TEXT,
405
  MODEL_COL_DATE_TEXT,
406
  MODEL_COL_TIME_TEXT,
407
  MODEL_COL_LOCATION_TEXT,
408 409 410 411 412
  MODEL_COL_ELLIPSIZE,
  MODEL_COL_NUM_COLUMNS
};

/* This list of types is passed to _gtk_file_system_model_new*() */
Matthias Clasen's avatar
Matthias Clasen committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
#define MODEL_COLUMN_TYPES                                      \
        MODEL_COL_NUM_COLUMNS,                                  \
        G_TYPE_STRING,            /* MODEL_COL_NAME */          \
        G_TYPE_INT64,             /* MODEL_COL_SIZE */          \
        G_TYPE_LONG,              /* MODEL_COL_TIME */          \
        G_TYPE_FILE,              /* MODEL_COL_FILE */          \
        G_TYPE_STRING,            /* MODEL_COL_NAME_COLLATED */ \
        G_TYPE_BOOLEAN,           /* MODEL_COL_IS_FOLDER */     \
        G_TYPE_BOOLEAN,           /* MODEL_COL_IS_SENSITIVE */  \
        CAIRO_GOBJECT_TYPE_SURFACE,  /* MODEL_COL_SURFACE */    \
        G_TYPE_STRING,            /* MODEL_COL_SIZE_TEXT */     \
        G_TYPE_STRING,            /* MODEL_COL_DATE_TEXT */     \
        G_TYPE_STRING,            /* MODEL_COL_TIME_TEXT */     \
        G_TYPE_STRING,            /* MODEL_COL_LOCATION_TEXT */ \
        PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
428 429 430 431

/* Identifiers for target types */
enum {
  GTK_TREE_MODEL_ROW,
432
};
433

434 435 436 437 438 439 440 441 442 443 444 445
#define DEFAULT_RECENT_FILES_LIMIT 50

/* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE 16

#define PREVIEW_HBOX_SPACING 12
#define NUM_LINES 45
#define NUM_CHARS 60

static void gtk_file_chooser_widget_iface_init       (GtkFileChooserIface        *iface);
static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface   *iface);

446
static void     gtk_file_chooser_widget_constructed  (GObject               *object);
447
static void     gtk_file_chooser_widget_finalize     (GObject               *object);
448
static void     gtk_file_chooser_widget_set_property (GObject               *object,
Matthias Clasen's avatar
Matthias Clasen committed
449 450 451
                                                       guint                  prop_id,
                                                       const GValue          *value,
                                                       GParamSpec            *pspec);
452
static void     gtk_file_chooser_widget_get_property (GObject               *object,
Matthias Clasen's avatar
Matthias Clasen committed
453 454 455
                                                       guint                  prop_id,
                                                       GValue                *value,
                                                       GParamSpec            *pspec);
456 457 458 459 460 461
static void     gtk_file_chooser_widget_dispose      (GObject               *object);
static void     gtk_file_chooser_widget_show_all       (GtkWidget             *widget);
static void     gtk_file_chooser_widget_realize        (GtkWidget             *widget);
static void     gtk_file_chooser_widget_map            (GtkWidget             *widget);
static void     gtk_file_chooser_widget_unmap          (GtkWidget             *widget);
static void     gtk_file_chooser_widget_hierarchy_changed (GtkWidget          *widget,
Matthias Clasen's avatar
Matthias Clasen committed
462
                                                            GtkWidget          *previous_toplevel);
463 464
static void     gtk_file_chooser_widget_style_updated  (GtkWidget             *widget);
static void     gtk_file_chooser_widget_screen_changed (GtkWidget             *widget,
465
                                                        GdkScreen             *previous_screen);
466 467
static gboolean gtk_file_chooser_widget_key_press_event (GtkWidget            *widget,
                                                         GdkEventKey          *event);
468

Matthias Clasen's avatar
Matthias Clasen committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static gboolean       gtk_file_chooser_widget_set_current_folder           (GtkFileChooser    *chooser,
                                                                            GFile             *folder,
                                                                            GError           **error);
static gboolean       gtk_file_chooser_widget_update_current_folder        (GtkFileChooser    *chooser,
                                                                            GFile             *folder,
                                                                            gboolean           keep_trail,
                                                                            gboolean           clear_entry,
                                                                            GError           **error);
static GFile *        gtk_file_chooser_widget_get_current_folder           (GtkFileChooser    *chooser);
static void           gtk_file_chooser_widget_set_current_name             (GtkFileChooser    *chooser,
                                                                            const gchar       *name);
static gchar *        gtk_file_chooser_widget_get_current_name             (GtkFileChooser    *chooser);
static gboolean       gtk_file_chooser_widget_select_file                  (GtkFileChooser    *chooser,
                                                                            GFile             *file,
                                                                            GError           **error);
static void           gtk_file_chooser_widget_unselect_file                (GtkFileChooser    *chooser,
                                                                            GFile             *file);
static void           gtk_file_chooser_widget_select_all                   (GtkFileChooser    *chooser);
static void           gtk_file_chooser_widget_unselect_all                 (GtkFileChooser    *chooser);
static GSList *       gtk_file_chooser_widget_get_files                    (GtkFileChooser    *chooser);
static GFile *        gtk_file_chooser_widget_get_preview_file             (GtkFileChooser    *chooser);
static GtkFileSystem *gtk_file_chooser_widget_get_file_system              (GtkFileChooser    *chooser);
static void           gtk_file_chooser_widget_add_filter                   (GtkFileChooser    *chooser,
                                                                            GtkFileFilter     *filter);
static void           gtk_file_chooser_widget_remove_filter                (GtkFileChooser    *chooser,
                                                                            GtkFileFilter     *filter);
static GSList *       gtk_file_chooser_widget_list_filters                 (GtkFileChooser    *chooser);
496
static gboolean       gtk_file_chooser_widget_add_shortcut_folder    (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
497 498
                                                                       GFile             *file,
                                                                       GError           **error);
499
static gboolean       gtk_file_chooser_widget_remove_shortcut_folder (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
500 501
                                                                       GFile             *file,
                                                                       GError           **error);
502 503 504
static GSList *       gtk_file_chooser_widget_list_shortcut_folders  (GtkFileChooser    *chooser);

static void           gtk_file_chooser_widget_get_default_size       (GtkFileChooserEmbed *chooser_embed,
Matthias Clasen's avatar
Matthias Clasen committed
505 506
                                                                       gint                *default_width,
                                                                       gint                *default_height);
507 508 509 510 511 512
static gboolean       gtk_file_chooser_widget_should_respond         (GtkFileChooserEmbed *chooser_embed);
static void           gtk_file_chooser_widget_initial_focus          (GtkFileChooserEmbed *chooser_embed);

static void add_selection_to_recent_list (GtkFileChooserWidget *impl);

static void location_popup_handler  (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
513
                                     const gchar           *path);
514 515 516 517 518 519 520
static void location_popup_on_paste_handler (GtkFileChooserWidget *impl);
static void location_toggle_popup_handler   (GtkFileChooserWidget *impl);
static void up_folder_handler       (GtkFileChooserWidget *impl);
static void down_folder_handler     (GtkFileChooserWidget *impl);
static void home_folder_handler     (GtkFileChooserWidget *impl);
static void desktop_folder_handler  (GtkFileChooserWidget *impl);
static void quick_bookmark_handler  (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
521
                                     gint                   bookmark_index);
522 523 524
static void show_hidden_handler     (GtkFileChooserWidget *impl);
static void search_shortcut_handler (GtkFileChooserWidget *impl);
static void recent_shortcut_handler (GtkFileChooserWidget *impl);
525
static void places_shortcut_handler (GtkFileChooserWidget *impl);
526 527 528
static void update_appearance       (GtkFileChooserWidget *impl);

static void operation_mode_set (GtkFileChooserWidget *impl, OperationMode mode);
529
static void location_mode_set  (GtkFileChooserWidget *impl, LocationMode new_mode);
530 531

static void set_current_filter   (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
532
                                  GtkFileFilter         *filter);
533 534 535
static void check_preview_change (GtkFileChooserWidget *impl);

static void filter_combo_changed       (GtkComboBox           *combo_box,
Matthias Clasen's avatar
Matthias Clasen committed
536
                                        GtkFileChooserWidget *impl);
537 538

static gboolean list_select_func   (GtkTreeSelection      *selection,
Matthias Clasen's avatar
Matthias Clasen committed
539 540 541 542
                                    GtkTreeModel          *model,
                                    GtkTreePath           *path,
                                    gboolean               path_currently_selected,
                                    gpointer               data);
543 544

static void list_selection_changed     (GtkTreeSelection      *tree_selection,
Matthias Clasen's avatar
Matthias Clasen committed
545
                                        GtkFileChooserWidget  *impl);
546
static void list_row_activated         (GtkTreeView           *tree_view,
Matthias Clasen's avatar
Matthias Clasen committed
547 548 549
                                        GtkTreePath           *path,
                                        GtkTreeViewColumn     *column,
                                        GtkFileChooserWidget  *impl);
550 551
static void list_cursor_changed        (GtkTreeView           *treeview,
                                        GtkFileChooserWidget  *impl);
552 553

static void path_bar_clicked (GtkPathBar            *path_bar,
Matthias Clasen's avatar
Matthias Clasen committed
554 555
                              GFile                 *file,
                              GFile                 *child,
556 557 558 559 560 561 562 563 564 565 566 567 568
                              gboolean               child_is_hidden,
                              GtkFileChooserWidget *impl);

static void update_cell_renderer_attributes (GtkFileChooserWidget *impl);

static void load_remove_timer (GtkFileChooserWidget *impl, LoadState new_load_state);
static void browse_files_center_selected_row (GtkFileChooserWidget *impl);

static void location_switch_to_path_bar (GtkFileChooserWidget *impl);

static void stop_loading_and_clear_list_model (GtkFileChooserWidget *impl,
                                               gboolean remove_from_treeview);

569 570 571
static GSList  *get_selected_files           (GtkFileChooserWidget *impl);
static GSList  *get_selected_infos           (GtkFileChooserWidget *impl);

572 573 574 575
static void     search_setup_widgets         (GtkFileChooserWidget *impl);
static void     search_stop_searching        (GtkFileChooserWidget *impl,
                                              gboolean               remove_query);
static void     search_clear_model           (GtkFileChooserWidget *impl, 
Matthias Clasen's avatar
Matthias Clasen committed
576
                                              gboolean               remove_from_treeview);
577
static void     search_entry_activate_cb     (GtkFileChooserWidget *impl);
578
static void     search_entry_stop_cb         (GtkFileChooserWidget *impl);
579 580
static void     settings_load                (GtkFileChooserWidget *impl);

581 582 583
static void     show_filters                 (GtkFileChooserWidget *impl,
                                              gboolean               show);

584
static gboolean recent_files_setting_is_enabled (GtkFileChooserWidget *impl);
585 586 587 588 589 590 591 592
static void     recent_start_loading         (GtkFileChooserWidget *impl);
static void     recent_stop_loading          (GtkFileChooserWidget *impl);
static void     recent_clear_model           (GtkFileChooserWidget *impl,
                                              gboolean               remove_from_treeview);
static gboolean recent_should_respond        (GtkFileChooserWidget *impl);
static void     set_file_system_backend      (GtkFileChooserWidget *impl);
static void     unset_file_system_backend    (GtkFileChooserWidget *impl);

593 594 595 596
static void     clear_model_cache            (GtkFileChooserWidget *impl,
                                              gint                  column);
static void     set_model_filter             (GtkFileChooserWidget *impl,
                                              GtkFileFilter        *filter);
597
static void     switch_to_home_dir           (GtkFileChooserWidget *impl);
598

599

600

601
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserWidget, gtk_file_chooser_widget, GTK_TYPE_BOX,
602
                         G_ADD_PRIVATE (GtkFileChooserWidget)
Matthias Clasen's avatar
Matthias Clasen committed
603 604 605 606
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
                                                gtk_file_chooser_widget_iface_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
                                                gtk_file_chooser_embed_default_iface_init));
Owen Taylor's avatar
Owen Taylor committed
607 608

static void
609
gtk_file_chooser_widget_iface_init (GtkFileChooserIface *iface)
Owen Taylor's avatar
Owen Taylor committed
610
{
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  iface->select_file = gtk_file_chooser_widget_select_file;
  iface->unselect_file = gtk_file_chooser_widget_unselect_file;
  iface->select_all = gtk_file_chooser_widget_select_all;
  iface->unselect_all = gtk_file_chooser_widget_unselect_all;
  iface->get_files = gtk_file_chooser_widget_get_files;
  iface->get_preview_file = gtk_file_chooser_widget_get_preview_file;
  iface->get_file_system = gtk_file_chooser_widget_get_file_system;
  iface->set_current_folder = gtk_file_chooser_widget_set_current_folder;
  iface->get_current_folder = gtk_file_chooser_widget_get_current_folder;
  iface->set_current_name = gtk_file_chooser_widget_set_current_name;
  iface->get_current_name = gtk_file_chooser_widget_get_current_name;
  iface->add_filter = gtk_file_chooser_widget_add_filter;
  iface->remove_filter = gtk_file_chooser_widget_remove_filter;
  iface->list_filters = gtk_file_chooser_widget_list_filters;
  iface->add_shortcut_folder = gtk_file_chooser_widget_add_shortcut_folder;
  iface->remove_shortcut_folder = gtk_file_chooser_widget_remove_shortcut_folder;
  iface->list_shortcut_folders = gtk_file_chooser_widget_list_shortcut_folders;
}
Owen Taylor's avatar
Owen Taylor committed
629

630 631 632 633 634 635 636
static void
gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
{
  iface->get_default_size = gtk_file_chooser_widget_get_default_size;
  iface->should_respond = gtk_file_chooser_widget_should_respond;
  iface->initial_focus = gtk_file_chooser_widget_initial_focus;
}
Owen Taylor's avatar
Owen Taylor committed
637

638 639 640 641 642 643 644
static void
pending_select_files_free (GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

  g_slist_free_full (priv->pending_select_files, g_object_unref);
  priv->pending_select_files = NULL;
Owen Taylor's avatar
Owen Taylor committed
645 646 647
}

static void
648
pending_select_files_add (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
649
                          GFile                 *file)
Owen Taylor's avatar
Owen Taylor committed
650
{
651
  GtkFileChooserWidgetPrivate *priv = impl->priv;
652

653 654
  priv->pending_select_files =
    g_slist_prepend (priv->pending_select_files, g_object_ref (file));
655 656
}

657 658
static void
gtk_file_chooser_widget_finalize (GObject *object)
659
{
660 661
  GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (object);
  GtkFileChooserWidgetPrivate *priv = impl->priv;
662

663 664 665
  if (priv->location_changed_id > 0)
    g_source_remove (priv->location_changed_id);

666 667 668
  unset_file_system_backend (impl);

  g_free (priv->browse_files_last_selected_name);
Owen Taylor's avatar
Owen Taylor committed
669

670
  g_slist_free_full (priv->filters, g_object_unref);
671

672 673
  if (priv->current_filter)
    g_object_unref (priv->current_filter);
674

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
  if (priv->current_volume_file)
    g_object_unref (priv->current_volume_file);

  if (priv->current_folder)
    g_object_unref (priv->current_folder);

  if (priv->preview_file)
    g_object_unref (priv->preview_file);

  if (priv->browse_path_bar_size_group)
    g_object_unref (priv->browse_path_bar_size_group);

  /* Free all the Models we have */
  stop_loading_and_clear_list_model (impl, FALSE);
  search_clear_model (impl, FALSE);
  recent_clear_model (impl, FALSE);
691
  g_clear_object (&impl->priv->model_for_search);
692 693 694 695 696 697 698 699 700

  /* stopping the load above should have cleared this */
  g_assert (priv->load_timeout_id == 0);

  g_free (priv->preview_display_name);

  impl->priv = NULL;

  G_OBJECT_CLASS (gtk_file_chooser_widget_parent_class)->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
701 702
}

703
/* Shows an error dialog set as transient for the specified window */
Owen Taylor's avatar
Owen Taylor committed
704
static void
705
error_message_with_parent (GtkWindow  *parent,
Matthias Clasen's avatar
Matthias Clasen committed
706 707
                           const char *msg,
                           const char *detail)
Owen Taylor's avatar
Owen Taylor committed
708
{
709
  GtkWidget *dialog;
710

711
  dialog = gtk_message_dialog_new (parent,
Matthias Clasen's avatar
Matthias Clasen committed
712 713 714 715 716
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_ERROR,
                                   GTK_BUTTONS_OK,
                                   "%s",
                                   msg);
717
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
Matthias Clasen's avatar
Matthias Clasen committed
718
                                            "%s", detail);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

  if (parent && gtk_window_has_group (parent))
    gtk_window_group_add_window (gtk_window_get_group (parent),
                                 GTK_WINDOW (dialog));

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel;

  toplevel = gtk_widget_get_toplevel (widget);
  if (!gtk_widget_is_toplevel (toplevel))
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

/* Shows an error dialog for the file chooser */
static void
error_message (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
744 745
               const char            *msg,
               const char            *detail)
746 747 748 749 750 751 752
{
  error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
}

/* Shows a simple error dialog relative to a path.  Frees the GError as well. */
static void
error_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
753 754
              const char            *msg,
              GError                *error)
755 756
{
  if (error)
757
    {
758
      error_message (impl, msg, error->message);
759
      g_error_free (error);
760
    }
Owen Taylor's avatar
Owen Taylor committed
761 762
}

763
/* Shows an error dialog about not being able to create a folder */
Owen Taylor's avatar
Owen Taylor committed
764
static void
765
error_creating_folder_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
766 767
                              GFile                 *file,
                              GError                *error)
Owen Taylor's avatar
Owen Taylor committed
768
{
769
  error_dialog (impl, 
Matthias Clasen's avatar
Matthias Clasen committed
770
                _("The folder could not be created"), 
771
                error);
772 773 774 775 776 777 778
}

/* Shows an error about not being able to create a folder because a file with
 * the same name is already there.
 */
static void
error_creating_folder_over_existing_file_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
779 780
                                                 GFile                 *file,
                                                 GError                *error)
781 782
{
  error_dialog (impl,
Matthias Clasen's avatar
Matthias Clasen committed
783
                _("The folder could not be created, as a file with the same "
784 785
                  "name already exists.  Try using a different name for the "
                  "folder, or rename the file first."),
786
                error);
787 788 789 790
}

static void
error_with_file_under_nonfolder (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
791
                                 GFile *parent_file)
792 793
{
  GError *error;
794
  char *uri, *msg;
795 796 797

  error = NULL;
  g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
Matthias Clasen's avatar
Matthias Clasen committed
798
                       _("You need to choose a valid filename."));
799

800 801 802 803 804
  uri = g_file_get_uri (parent_file);
  msg = g_strdup_printf (_("Cannot create a file under %s as it is not a folder"), uri);
  g_free (uri);
  error_dialog (impl, msg, error);
  g_free (msg);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
}

static void
error_filename_to_long_dialog (GtkFileChooserWidget *impl)
{
  error_message (impl,
                 _("Cannot create file as the filename is too long"),
                 _("Try using a shorter name."));
}

/* Shows an error about not being able to select a folder because a file with
 * the same name is already there.
 */
static void
error_selecting_folder_over_existing_file_dialog (GtkFileChooserWidget *impl)
{
  error_message (impl,
                 _("You may only select folders"),
                 _("The item that you selected is not a folder try using a different item."));
}

/* Shows an error dialog about not being able to create a filename */
static void
error_building_filename_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
829
                                GError                *error)
830
{
831
  error_dialog (impl, _("Invalid file name"), error);
832 833 834 835 836
}

/* Shows an error dialog when we cannot switch to a folder */
static void
error_changing_folder_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
837 838
                              GFile                 *file,
                              GError                *error)
839
{
840
  error_dialog (impl, _("The folder contents could not be displayed"), error);
841 842
}

843 844 845 846 847
static void
error_deleting_file (GtkFileChooserWidget *impl,
                     GFile                *file,
                     GError               *error)
{
848
  error_dialog (impl, _("The file could not be deleted"), error);
849 850
}

851 852 853 854 855
static void
error_trashing_file (GtkFileChooserWidget *impl,
                     GFile                *file,
                     GError               *error)
{
856
  error_dialog (impl, _("The file could not be moved to the Trash"), error);
857 858
}

859 860 861
/* Changes folders, displaying an error dialog if this fails */
static gboolean
change_folder_and_display_error (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
862 863
                                 GFile                 *file,
                                 gboolean               clear_entry)
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
{
  GError *error;
  gboolean result;

  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  /* We copy the path because of this case:
   *
   * list_row_activated()
   *   fetches path from model; path belongs to the model (*)
   *   calls change_folder_and_display_error()
   *     calls gtk_file_chooser_set_current_folder_file()
   *       changing folders fails, sets model to NULL, thus freeing the path in (*)
   */

  error = NULL;
  result = gtk_file_chooser_widget_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);

  if (!result)
    error_changing_folder_dialog (impl, file, error);

  return result;
}

static void
emit_default_size_changed (GtkFileChooserWidget *impl)
{
  profile_msg ("    emit default-size-changed start", NULL);
  g_signal_emit_by_name (impl, "default-size-changed");
  profile_msg ("    emit default-size-changed end", NULL);
}

static void
update_preview_widget_visibility (GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

  if (priv->use_preview_label)
    {
      if (!priv->preview_label)
Matthias Clasen's avatar
Matthias Clasen committed
904 905 906 907 908 909 910
        {
          priv->preview_label = gtk_label_new (priv->preview_display_name);
          gtk_box_pack_start (GTK_BOX (priv->preview_box), priv->preview_label, FALSE, FALSE, 0);
          gtk_box_reorder_child (GTK_BOX (priv->preview_box), priv->preview_label, 0);
          gtk_label_set_ellipsize (GTK_LABEL (priv->preview_label), PANGO_ELLIPSIZE_MIDDLE);
          gtk_widget_show (priv->preview_label);
        }
911 912 913 914
    }
  else
    {
      if (priv->preview_label)
Matthias Clasen's avatar
Matthias Clasen committed
915 916 917 918
        {
          gtk_widget_destroy (priv->preview_label);
          priv->preview_label = NULL;
        }
919 920 921 922 923 924 925 926 927 928 929 930 931
    }

  if (priv->preview_widget_active && priv->preview_widget)
    gtk_widget_show (priv->preview_box);
  else
    gtk_widget_hide (priv->preview_box);

  if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
    emit_default_size_changed (impl);
}

static void
set_preview_widget (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
932
                    GtkWidget             *preview_widget)
933 934 935 936 937 938 939 940
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

  if (preview_widget == priv->preview_widget)
    return;

  if (priv->preview_widget)
    gtk_container_remove (GTK_CONTAINER (priv->preview_box),
Matthias Clasen's avatar
Matthias Clasen committed
941
                          priv->preview_widget);
942 943 944 945 946 947 948

  priv->preview_widget = preview_widget;
  if (priv->preview_widget)
    {
      gtk_widget_show (priv->preview_widget);
      gtk_box_pack_start (GTK_BOX (priv->preview_box), priv->preview_widget, TRUE, TRUE, 0);
      gtk_box_reorder_child (GTK_BOX (priv->preview_box),
Matthias Clasen's avatar
Matthias Clasen committed
949 950
                             priv->preview_widget,
                             (priv->use_preview_label && priv->preview_label) ? 1 : 0);
951 952 953 954 955
    }

  update_preview_widget_visibility (impl);
}

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
static void
new_folder_popover_active (GtkWidget            *button,
                           GParamSpec           *pspec,
                           GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

  gtk_entry_set_text (GTK_ENTRY (priv->new_folder_name_entry), "");
  gtk_widget_set_sensitive (priv->new_folder_create_button, FALSE);
  gtk_label_set_text (GTK_LABEL (priv->new_folder_error_label), "");
}

struct FileExistsData
{
  GtkFileChooserWidget *impl;
  gboolean file_exists_and_is_not_folder;
  GFile *parent_file;
  GFile *file;
974 975
  GtkWidget *error_label;
  GtkWidget *button;
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
};

static void
name_exists_get_info_cb (GCancellable *cancellable,
                         GFileInfo    *info,
                         const GError *error,
                         gpointer      user_data)
{
  struct FileExistsData *data = user_data;
  GtkFileChooserWidget *impl = data->impl;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

  if (cancellable != priv->file_exists_get_info_cancellable)
    goto out;

  priv->file_exists_get_info_cancellable = NULL;

  if (g_cancellable_is_cancelled (cancellable))
    goto out;

  if (info != NULL)
    {
      const gchar *msg;

      if (_gtk_file_info_consider_as_directory (info))
        msg = _("A folder with that name already exists");
      else
        msg = _("A file with that name already exists");

1005 1006
      gtk_widget_set_sensitive (data->button, FALSE);
      gtk_label_set_text (GTK_LABEL (data->error_label), msg);
1007 1008 1009
    }
  else
    {
1010
      gtk_widget_set_sensitive (data->button, TRUE);
1011
      /* Don't clear the label here, it may contain a warning */
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    }

out:
  g_object_unref (impl);
  g_object_unref (data->file);
  g_free (data);
  g_object_unref (cancellable);
}

static void
1022 1023 1024 1025
check_valid_child_name (GtkFileChooserWidget *impl,
                        GFile                *parent,
                        const gchar          *name,
                        gboolean              is_folder,
1026
                        GFile                *original,
1027 1028
                        GtkWidget            *error_label,
                        GtkWidget            *button)
1029 1030 1031
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1032
  gtk_widget_set_sensitive (button, FALSE);
1033 1034

  if (name[0] == '\0')
1035
    gtk_label_set_text (GTK_LABEL (error_label), "");
1036
  else if (strcmp (name, ".") == 0)
1037
    gtk_label_set_text (GTK_LABEL (error_label),
1038 1039
                        is_folder ? _("A folder cannot be called “.”")
                                  : _("A file cannot be called “.”"));
1040
  else if (strcmp (name, "..") == 0)
1041
    gtk_label_set_text (GTK_LABEL (error_label),
1042 1043
                        is_folder ? _("A folder cannot be called “..”")
                                  : _("A file cannot be called “..”"));
1044
  else if (strchr (name, '/') != NULL)
1045
    gtk_label_set_text (GTK_LABEL (error_label),
1046 1047
                        is_folder ? _("Folder names cannot contain “/”")
                                  : _("File names cannot contain “/”"));
1048 1049 1050 1051 1052
  else
    {
      GFile *file;
      GError *error = NULL;

1053
      gtk_label_set_text (GTK_LABEL (error_label), "");
1054

1055
      file = g_file_get_child_for_display_name (parent, name, &error);
1056 1057
      if (file == NULL)
        {
1058
          gtk_label_set_text (GTK_LABEL (error_label), error->message);
1059 1060
          g_error_free (error);
        }
1061 1062 1063 1064 1065
      else if (original && g_file_equal (original, file))
        {
          gtk_widget_set_sensitive (button, TRUE);
          g_object_unref (file);
        }
1066 1067 1068 1069
      else
        {
          struct FileExistsData *data;

1070 1071
          /* Warn the user about questionable names that are technically valid */
          if (g_ascii_isspace (name[0]))
1072
            gtk_label_set_text (GTK_LABEL (error_label),
1073 1074
                                is_folder ? _("Folder names should not begin with a space")
                                          : _("File names should not begin with a space"));
1075 1076

          else if (g_ascii_isspace (name[strlen (name) - 1]))
1077
            gtk_label_set_text (GTK_LABEL (error_label),
1078 1079
                                is_folder ? _("Folder names should not end with a space")
                                          : _("File names should not end with a space"));
1080
          else if (name[0] == '.')
1081
            gtk_label_set_text (GTK_LABEL (error_label),
1082 1083
                                is_folder ? _("Folder names starting with a “.” are hidden")
                                          : _("File names starting with a “.” are hidden"));
1084 1085 1086 1087

          data = g_new0 (struct FileExistsData, 1);
          data->impl = g_object_ref (impl);
          data->file = g_object_ref (file);
1088 1089
          data->error_label = error_label;
          data->button = button;
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

          if (priv->file_exists_get_info_cancellable)
            g_cancellable_cancel (priv->file_exists_get_info_cancellable);

          priv->file_exists_get_info_cancellable =
            _gtk_file_system_get_info (priv->file_system,
                                       file,
                                       "standard::type",
                                       name_exists_get_info_cb,
                                       data);

          g_object_unref (file);
        }
    }
}

static void
new_folder_name_changed (GtkEntry             *entry,
                         GtkFileChooserWidget *impl)
{
1110 1111
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1112 1113 1114 1115
  check_valid_child_name (impl,
                          priv->current_folder,
                          gtk_entry_get_text (entry),
                          TRUE,
1116
                          NULL,
1117 1118
                          priv->new_folder_error_label,
                          priv->new_folder_create_button);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
}

static void
new_folder_create_clicked (GtkButton            *button,
                           GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GError *error = NULL;
  GFile *file;
  const gchar *name;

  name = gtk_entry_get_text (GTK_ENTRY (priv->new_folder_name_entry));
  file = g_file_get_child_for_display_name (priv->current_folder, name, &error);

  gtk_widget_hide (priv->new_folder_popover);

  if (file)
    {
      if (g_file_make_directory (file, NULL, &error))
        change_folder_and_display_error (impl, file, FALSE);
      else
        error_creating_folder_dialog (impl, file, error);
      g_object_unref (file);
    }
  else
    error_creating_folder_dialog (impl, file, error);
}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
struct selection_check_closure {
  GtkFileChooserWidget *impl;
  int num_selected;
  gboolean all_files;
  gboolean all_folders;
};

/* Used from gtk_tree_selection_selected_foreach() */
static void
selection_check_foreach_cb (GtkTreeModel *model,
Matthias Clasen's avatar
Matthias Clasen committed
1157 1158 1159
                            GtkTreePath  *path,
                            GtkTreeIter  *iter,
                            gpointer      data)
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
{
  struct selection_check_closure *closure;
  gboolean is_folder;
  GFile *file;

  gtk_tree_model_get (model, iter,
                      MODEL_COL_FILE, &file,
                      MODEL_COL_IS_FOLDER, &is_folder,
                      -1);

  if (file == NULL)
    return;

  g_object_unref (file);

  closure = data;
  closure->num_selected++;

  closure->all_folders = closure->all_folders && is_folder;
  closure->all_files = closure->all_files && !is_folder;
}

/* Checks whether the selected items in the file list are all files or all folders */
static void
selection_check (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
1185 1186 1187
                 gint                  *num_selected,
                 gboolean              *all_files,
                 gboolean              *all_folders)
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  struct selection_check_closure closure;
  GtkTreeSelection *selection;

  closure.impl = impl;
  closure.num_selected = 0;
  closure.all_files = TRUE;
  closure.all_folders = TRUE;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
  gtk_tree_selection_selected_foreach (selection,
Matthias Clasen's avatar
Matthias Clasen committed
1200 1201
                                       selection_check_foreach_cb,
                                       &closure);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

  g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));

  if (num_selected)
    *num_selected = closure.num_selected;

  if (all_files)
    *all_files = closure.all_files;

  if (all_folders)
    *all_folders = closure.all_folders;
}

static gboolean
file_is_recent_uri (GFile *file)
{
  GFile *recent;
  gboolean same;

  recent = g_file_new_for_uri ("recent:///");
  same = g_file_equal (file, recent);
  g_object_unref (recent);

  return same;
}

static void
1229 1230 1231 1232
places_sidebar_open_location_cb (GtkPlacesSidebar     *sidebar,
                                 GFile                *location,
                                 GtkPlacesOpenFlags    open_flags,
                                 GtkFileChooserWidget *impl)
1233 1234 1235 1236
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  gboolean clear_entry;

1237
  /* In the Save modes, we want to preserve what the user typed in the filename
1238 1239 1240 1241
   * entry, so that he may choose another folder without erasing his typed name.
   */
  if (priv->location_entry
      && !(priv->action == GTK_FILE_CHOOSER_ACTION_SAVE
Matthias Clasen's avatar
Matthias Clasen committed
1242
           || priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1243 1244 1245 1246
    clear_entry = TRUE;
  else
    clear_entry = FALSE;

1247 1248
  location_mode_set (impl, LOCATION_MODE_PATH_BAR);

1249 1250 1251 1252 1253 1254 1255 1256 1257
  if (file_is_recent_uri (location))
    operation_mode_set (impl, OPERATION_MODE_RECENT);
  else
    change_folder_and_display_error (impl, location, clear_entry);
}

/* Callback used when the places sidebar needs us to display an error message */
static void
places_sidebar_show_error_message_cb (GtkPlacesSidebar *sidebar,
Matthias Clasen's avatar
Matthias Clasen committed
1258 1259 1260
                                      const char       *primary,
                                      const char       *secondary,
                                      GtkFileChooserWidget *impl)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
{
  error_message (impl, primary, secondary);
}

static gboolean
key_is_left_or_right (GdkEventKey *event)
{
  guint modifiers;

  modifiers = gtk_accelerator_get_default_mod_mask ();

  return ((event->keyval == GDK_KEY_Right
Matthias Clasen's avatar
Matthias Clasen committed
1273 1274 1275 1276
           || event->keyval == GDK_KEY_KP_Right
           || event->keyval == GDK_KEY_Left
           || event->keyval == GDK_KEY_KP_Left)
          && (event->state & modifiers) == 0);
1277 1278 1279
}

static gboolean
1280 1281
should_trigger_location_entry (GtkFileChooserWidget *impl,
                               GdkEventKey          *event)
1282 1283 1284
{
  GdkModifierType no_text_input_mask;

1285 1286 1287
  if (impl->priv->operation_mode == OPERATION_MODE_SEARCH)
    return FALSE;

1288
  no_text_input_mask =
1289
    gtk_widget_get_modifier_mask (GTK_WIDGET (impl), GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
1290 1291 1292

  if ((event->keyval == GDK_KEY_slash
       || event->keyval == GDK_KEY_KP_Divide
1293
       || event->keyval == GDK_KEY_period
1294 1295 1296 1297
#ifdef G_OS_UNIX
       || event->keyval == GDK_KEY_asciitilde
#endif
       ) && !(event->state & no_text_input_mask))
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    return TRUE;

  return FALSE;
}

/* Handles key press events on the file list, so that we can trap Enter to
 * activate the default button on our own.  Also, checks to see if “/” has been
 * pressed.
 */
static gboolean
browse_files_key_press_event_cb (GtkWidget   *widget,
Matthias Clasen's avatar
Matthias Clasen committed
1309 1310
                                 GdkEventKey *event,
                                 gpointer     data)
1311 1312 1313 1314
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) data;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1315
  if (should_trigger_location_entry (impl, event) &&
1316 1317
      (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
       priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER))
1318 1319 1320 1321 1322 1323 1324
    {
      location_popup_handler (impl, event->string);
      return TRUE;
    }

  if (key_is_left_or_right (event))
    {
1325 1326
      if (gtk_widget_child_focus (priv->places_sidebar, GTK_DIR_LEFT))
        return TRUE;
1327 1328 1329 1330 1331 1332 1333 1334 1335
    }

  if ((event->keyval == GDK_KEY_Return
       || event->keyval == GDK_KEY_ISO_Enter
       || event->keyval == GDK_KEY_KP_Enter
       || event->keyval == GDK_KEY_space
       || event->keyval == GDK_KEY_KP_Space)
      && !(event->state & gtk_accelerator_get_default_mod_mask ())
      && !(priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
Matthias Clasen's avatar
Matthias Clasen committed
1336
           priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    {
      GtkWindow *window;

      window = get_toplevel (widget);
      if (window)
        {
          GtkWidget *default_widget, *focus_widget;

          default_widget = gtk_window_get_default_widget (window);
          focus_widget = gtk_window_get_focus (window);

          if (widget != default_widget &&
              !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
Matthias Clasen's avatar
Matthias Clasen committed
1350 1351
            {
              gtk_window_activate_default (window);
1352

Matthias Clasen's avatar
Matthias Clasen committed
1353 1354
              return TRUE;
            }
1355 1356 1357
        }
    }

1358 1359 1360 1361 1362 1363 1364
  if (event->keyval == GDK_KEY_Escape &&
      priv->operation_mode == OPERATION_MODE_SEARCH)
    {
      gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), (GdkEvent *)event);
      return TRUE;
    }

1365 1366 1367
  return FALSE;
}

1368 1369 1370 1371 1372 1373 1374
static gboolean
gtk_file_chooser_widget_key_press_event (GtkWidget   *widget,
                                         GdkEventKey *event)
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) widget;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1375
  if (should_trigger_location_entry (impl, event))
1376
    {
1377 1378 1379 1380 1381 1382
      if (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
          priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
        {
          location_popup_handler (impl, event->string);
          return TRUE;
        }
1383
    }
1384
  else if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), (GdkEvent *)event))
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    {
      if (priv->operation_mode != OPERATION_MODE_SEARCH)
        operation_mode_set (impl, OPERATION_MODE_SEARCH);
      return TRUE;
    }

  if (GTK_WIDGET_CLASS (gtk_file_chooser_widget_parent_class)->key_press_event (widget, event))
    return TRUE;

  return FALSE;
}

1397 1398 1399 1400 1401
/* Callback used from gtk_tree_selection_selected_foreach(); adds a bookmark for
 * each selected item in the file list.
 */
static void
add_bookmark_foreach_cb (GtkTreeModel *model,
Matthias Clasen's avatar
Matthias Clasen committed
1402 1403 1404
                         GtkTreePath  *path,
                         GtkTreeIter  *iter,
                         gpointer      data)
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) data;
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GFile *file;

  gtk_tree_model_get (model, iter,
                      MODEL_COL_FILE, &file,
                      -1);

  _gtk_bookmarks_manager_insert_bookmark (priv->bookmarks_manager, file, 0, NULL); /* NULL-GError */

  g_object_unref (file);
}

/* Callback used when the "Add to Bookmarks" menu item is activated */
static void
1421 1422 1423
add_to_shortcuts_cb (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       data)
1424
{
1425
  GtkFileChooserWidget *impl = data;
1426 1427 1428 1429 1430 1431
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));

  gtk_tree_selection_selected_foreach (selection,
Matthias Clasen's avatar
Matthias Clasen committed
1432 1433
                                       add_bookmark_foreach_cb,
                                       impl);
1434 1435
}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
static gboolean
confirm_delete (GtkFileChooserWidget *impl,
                GFileInfo            *info)
{
  GtkWindow *toplevel;
  GtkWidget *dialog;
  gint response;
  const gchar *name;

  name = g_file_info_get_display_name (info);

  toplevel = get_toplevel (GTK_WIDGET (impl));

  dialog = gtk_message_dialog_new (toplevel,
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_QUESTION,
                                   GTK_BUTTONS_NONE,
1453
                                   _("Are you sure you want to permanently delete “%s”?"),
1454
                                   name);
1455 1456
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            _("If you delete an item, it will be permanently lost."));
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Cancel"), GTK_RESPONSE_CANCEL);
  gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Delete"), GTK_RESPONSE_ACCEPT);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_ACCEPT,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
G_GNUC_END_IGNORE_DEPRECATIONS
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);

  if (gtk_window_has_group (toplevel))
    gtk_window_group_add_window (gtk_window_get_group (toplevel), GTK_WINDOW (dialog));

  response = gtk_dialog_run (GTK_DIALOG (dialog));

  gtk_widget_destroy (dialog);

  return (response == GTK_RESPONSE_ACCEPT);
}

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static void
delete_selected_cb (GtkTreeModel *model,
                    GtkTreePath  *path,
                    GtkTreeIter  *iter,
                    gpointer      data)
{
  GtkFileChooserWidget *impl = data;
  GFile *file;
  GFileInfo *info;
  GError *error = NULL;

  file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
  info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (model), iter);

  if (confirm_delete (impl, info))
    {
      if (!g_file_delete (file, NULL, &error))
        error_deleting_file (impl, file, error);
    }
}

1498
static void
1499 1500 1501
delete_file_cb (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       data)
1502
{
1503
  GtkFileChooserWidget *impl = data;
1504 1505 1506 1507
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
1508 1509
  gtk_tree_selection_selected_foreach (selection, delete_selected_cb, impl);
}
1510

1511 1512 1513 1514 1515 1516 1517 1518 1519
static void
trash_selected_cb (GtkTreeModel *model,
                   GtkTreePath  *path,
                   GtkTreeIter  *iter,
                   gpointer      data)
{
  GtkFileChooserWidget *impl = data;
  GFile *file;
  GError *error = NULL;
1520

1521
  file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
1522

1523 1524
  if (!g_file_trash (file, NULL, &error))
    error_trashing_file (impl, file, error);
1525 1526
}

1527

1528
static void
1529 1530 1531
trash_file_cb (GSimpleAction *action,
               GVariant      *parameter,
               gpointer       data)
1532
{
1533
  GtkFileChooserWidget *impl = data;
1534 1535 1536 1537
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
1538
  gtk_tree_selection_selected_foreach (selection, trash_selected_cb, impl);
1539 1540
}

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static void
rename_file_name_changed (GtkEntry             *entry,
                          GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GFileType file_type;

  file_type = g_file_query_file_type (priv->rename_file_source_file,
                                      G_FILE_QUERY_INFO_NONE, NULL);

1551 1552 1553 1554
  check_valid_child_name (impl,
                          priv->current_folder,
                          gtk_entry_get_text (entry),
                          file_type == G_FILE_TYPE_DIRECTORY,
1555
                          priv->rename_file_source_file,
1556 1557
                          priv->rename_file_error_label,
                          priv->rename_file_rename_button);
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
}

static void
rename_file_end (GtkPopover           *popover,
                 GtkFileChooserWidget *impl)
{
  g_object_unref (impl->priv->rename_file_source_file);
}

static void
rename_file_rename_clicked (GtkButton            *button,
                            GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GFile *dest;
  const gchar* new_name;

  gtk_widget_hide (priv->rename_file_popover);

  new_name = gtk_entry_get_text (GTK_ENTRY (priv->rename_file_name_entry));
  dest = g_file_get_parent (priv->rename_file_source_file);

  if (dest)
    {
      GFile *child;
      GError *error = NULL;

      child = g_file_get_child (dest, new_name);
      if (child)
        {
          if (!g_file_move (priv->rename_file_source_file, child, G_FILE_COPY_NONE,
                            NULL, NULL, NULL, &error))
1590
            error_dialog (impl, _("The file could not be renamed"), error);
1591 1592 1593 1594 1595 1596 1597 1598 1599

          g_object_unref (child);
        }

      g_object_unref (dest);
    }
}

static void
1600 1601 1602 1603
rename_selected_cb (GtkTreeModel *model,
                    GtkTreePath  *path,
                    GtkTreeIter  *iter,
                    gpointer      data)
1604
{
1605
  GtkFileChooserWidget *impl = data;
1606 1607 1608 1609
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GdkRectangle rect;
  gchar *filename;

1610 1611 1612
  gtk_tree_model_get (model, iter,
                      MODEL_COL_FILE, &priv->rename_file_source_file,
                      -1);
1613

1614 1615
  gtk_tree_view_get_cell_area (GTK_TREE_VIEW (priv->browse_files_tree_view),
                               path, priv->list_name_column, &rect);
1616

1617 1618
  gtk_tree_view_convert_tree_to_widget_coords (GTK_TREE_VIEW (priv->browse_files_tree_view),
                                               rect.x, rect.y, &rect.x, &rect.y);
1619

1620 1621 1622
  filename = g_file_get_basename (priv->rename_file_source_file);
  gtk_entry_set_text (GTK_ENTRY(priv->rename_file_name_entry), filename);
  g_free (filename);
1623

1624 1625 1626 1627 1628 1629
  gtk_popover_set_pointing_to (GTK_POPOVER (priv->rename_file_popover), &rect);
  gtk_widget_show (priv->rename_file_popover);
  gtk_widget_grab_focus (priv->rename_file_popover);
}

static void
1630 1631 1632
rename_file_cb (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       data)
1633
{
1634
  GtkFileChooserWidget *impl = data;
1635 1636
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;
1637

1638 1639
  /* insensitive until we change the name */
  gtk_widget_set_sensitive (priv->rename_file_rename_button, FALSE);
1640

1641 1642
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
  gtk_tree_selection_selected_foreach (selection, rename_selected_cb, impl);
1643 1644
}

164