gtkfilechooserwidget.c 283 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
#include "gtkentry.h"
#include "gtkfilechooserprivate.h"
#include "gtkfilechooserdialog.h"
38
#include "gtkfilechooserembed.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52
#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"
53
#include "gtkplacessidebarprivate.h"
54
#include "gtkplacesviewprivate.h"
55 56
#include "gtkprivate.h"
#include "gtkrecentmanager.h"
57
#include "gtksearchentry.h"
58 59 60 61
#include "gtkseparatormenuitem.h"
#include "gtksettings.h"
#include "gtksizegroup.h"
#include "gtksizerequest.h"
62
#include "gtkstack.h"
63 64 65 66 67
#include "gtktooltip.h"
#include "gtktreednd.h"
#include "gtktreeprivate.h"
#include "gtktreeselection.h"
#include "gtkbox.h"
68
#include "gtkcheckbutton.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

81
#include <cairo-gobject.h>
82

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

Matthias Clasen's avatar
Matthias Clasen committed
90 91
/**
 * SECTION:gtkfilechooserwidget
Matthias Clasen's avatar
Matthias Clasen committed
92
 * @Short_description: A file chooser widget
Matthias Clasen's avatar
Matthias Clasen committed
93 94 95 96 97 98 99
 * @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.
100 101 102 103
 *
 * # CSS nodes
 *
 * GtkFileChooserWidget has a single CSS node with name filechooser.
Matthias Clasen's avatar
Matthias Clasen committed
104 105 106
 */


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

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

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


#ifndef F_OK 
#define F_OK 0
#endif
122

123 124 125 126 127 128 129 130 131 132 133 134
#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");
}

135
static void
136
_gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
137
{
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
  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

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

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
170 171 172 173
  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 */
174 175 176
} LoadState;

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
177 178
  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 */
179 180 181 182 183 184 185 186 187 188
} ReloadState;

typedef enum {
  LOCATION_MODE_PATH_BAR,
  LOCATION_MODE_FILENAME_ENTRY
} LocationMode;

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

typedef enum {
  STARTUP_MODE_RECENT,
  STARTUP_MODE_CWD
} StartupMode;

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

typedef enum {
  DATE_FORMAT_REGULAR,
  DATE_FORMAT_WITH_TIME
} DateFormat;

209 210 211 212 213 214 215 216 217 218 219
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;
220
  GtkWidget *browse_header_revealer;
221
  GtkWidget *browse_header_stack;
222
  GtkWidget *browse_files_stack;
223
  GtkWidget *browse_files_swin;
224
  GtkWidget *browse_files_tree_view;
225
  GtkWidget *remote_warning_bar;
226 227

  GtkWidget *browse_files_popover;
228 229 230 231 232 233
  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;
234
  GtkWidget *rename_file_item;
235
  GtkWidget *trash_file_item;
236
  GtkWidget *delete_file_item;
237 238
  GtkWidget *sort_directories_item;
  GtkWidget *show_time_item;
239

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

253 254
  GtkGesture *long_press_gesture;

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

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

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

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

276
  GtkWidget *extra_and_filters;
277 278 279 280 281 282 283 284 285 286 287 288
  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;

289 290
  GtkWidget *external_entry;

291 292 293
  GtkWidget *choice_box;
  GHashTable *choices;

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 326 327
  /* 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;
328
  GtkTreeViewColumn *list_time_column;
329
  GtkCellRenderer *list_date_renderer;
330
  GtkCellRenderer *list_time_renderer;
331
  GtkTreeViewColumn *list_size_column;
332
  GtkCellRenderer *list_size_renderer;
333
  GtkTreeViewColumn *list_location_column;
334
  GtkCellRenderer *list_location_renderer;
335

336 337
  guint location_changed_id;

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  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;
356
  guint show_hidden_set : 1;
357
  guint sort_directories_first : 1;
358
  guint show_time : 1;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  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,
384
  PLACES_SHORTCUT,
385 386 387 388 389 390 391 392

  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," \
393
                         "standard::content-type,time::modified,time::access," \
394 395
                         "access::can-rename,access::can-delete,access::can-trash," \
                         "standard::target-uri"
396 397 398 399
enum {
  /* the first 3 must be these due to settings caching sort column */
  MODEL_COL_NAME,
  MODEL_COL_SIZE,
400
  MODEL_COL_TIME,
401 402 403 404 405 406
  MODEL_COL_FILE,
  MODEL_COL_NAME_COLLATED,
  MODEL_COL_IS_FOLDER,
  MODEL_COL_IS_SENSITIVE,
  MODEL_COL_SURFACE,
  MODEL_COL_SIZE_TEXT,
407
  MODEL_COL_DATE_TEXT,
408
  MODEL_COL_TIME_TEXT,
409
  MODEL_COL_LOCATION_TEXT,
410 411 412 413 414
  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
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
#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 */
430 431 432 433 434 435 436 437 438 439 440 441 442

#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);

443
static void     gtk_file_chooser_widget_constructed  (GObject               *object);
444
static void     gtk_file_chooser_widget_finalize     (GObject               *object);
445
static void     gtk_file_chooser_widget_set_property (GObject               *object,
Matthias Clasen's avatar
Matthias Clasen committed
446 447 448
                                                       guint                  prop_id,
                                                       const GValue          *value,
                                                       GParamSpec            *pspec);
449
static void     gtk_file_chooser_widget_get_property (GObject               *object,
Matthias Clasen's avatar
Matthias Clasen committed
450 451 452
                                                       guint                  prop_id,
                                                       GValue                *value,
                                                       GParamSpec            *pspec);
453 454 455 456 457 458
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
459
                                                            GtkWidget          *previous_toplevel);
460 461
static void     gtk_file_chooser_widget_style_updated  (GtkWidget             *widget);
static void     gtk_file_chooser_widget_screen_changed (GtkWidget             *widget,
462
                                                        GdkScreen             *previous_screen);
463 464
static gboolean gtk_file_chooser_widget_key_press_event (GtkWidget            *widget,
                                                         GdkEventKey          *event);
465

Matthias Clasen's avatar
Matthias Clasen committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
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);
493
static gboolean       gtk_file_chooser_widget_add_shortcut_folder    (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
494 495
                                                                       GFile             *file,
                                                                       GError           **error);
496
static gboolean       gtk_file_chooser_widget_remove_shortcut_folder (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
497 498
                                                                       GFile             *file,
                                                                       GError           **error);
499 500 501
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
502 503
                                                                       gint                *default_width,
                                                                       gint                *default_height);
504 505 506
static gboolean       gtk_file_chooser_widget_should_respond         (GtkFileChooserEmbed *chooser_embed);
static void           gtk_file_chooser_widget_initial_focus          (GtkFileChooserEmbed *chooser_embed);

507 508 509 510 511 512 513 514 515 516 517 518 519 520
static void        gtk_file_chooser_widget_add_choice    (GtkFileChooser  *chooser,
                                                          const char      *id,
                                                          const char      *label,
                                                          const char     **options,
                                                          const char     **option_labels);
static void        gtk_file_chooser_widget_remove_choice (GtkFileChooser  *chooser,
                                                          const char      *id);
static void        gtk_file_chooser_widget_set_choice    (GtkFileChooser  *chooser,
                                                          const char      *id,
                                                          const char      *option);
static const char *gtk_file_chooser_widget_get_choice    (GtkFileChooser  *chooser,
                                                          const char      *id);


521 522 523
static void add_selection_to_recent_list (GtkFileChooserWidget *impl);

static void location_popup_handler  (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
524
                                     const gchar           *path);
525 526 527 528 529 530 531
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
532
                                     gint                   bookmark_index);
533 534 535
static void show_hidden_handler     (GtkFileChooserWidget *impl);
static void search_shortcut_handler (GtkFileChooserWidget *impl);
static void recent_shortcut_handler (GtkFileChooserWidget *impl);
536
static void places_shortcut_handler (GtkFileChooserWidget *impl);
537 538 539
static void update_appearance       (GtkFileChooserWidget *impl);

static void operation_mode_set (GtkFileChooserWidget *impl, OperationMode mode);
540
static void location_mode_set  (GtkFileChooserWidget *impl, LocationMode new_mode);
541 542

static void set_current_filter   (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
543
                                  GtkFileFilter         *filter);
544 545 546
static void check_preview_change (GtkFileChooserWidget *impl);

static void filter_combo_changed       (GtkComboBox           *combo_box,
Matthias Clasen's avatar
Matthias Clasen committed
547
                                        GtkFileChooserWidget *impl);
548 549

static gboolean list_select_func   (GtkTreeSelection      *selection,
Matthias Clasen's avatar
Matthias Clasen committed
550 551 552 553
                                    GtkTreeModel          *model,
                                    GtkTreePath           *path,
                                    gboolean               path_currently_selected,
                                    gpointer               data);
554 555

static void list_selection_changed     (GtkTreeSelection      *tree_selection,
Matthias Clasen's avatar
Matthias Clasen committed
556
                                        GtkFileChooserWidget  *impl);
557
static void list_row_activated         (GtkTreeView           *tree_view,
Matthias Clasen's avatar
Matthias Clasen committed
558 559 560
                                        GtkTreePath           *path,
                                        GtkTreeViewColumn     *column,
                                        GtkFileChooserWidget  *impl);
561 562
static void list_cursor_changed        (GtkTreeView           *treeview,
                                        GtkFileChooserWidget  *impl);
563 564

static void path_bar_clicked (GtkPathBar            *path_bar,
Matthias Clasen's avatar
Matthias Clasen committed
565 566
                              GFile                 *file,
                              GFile                 *child,
567 568 569 570 571 572 573 574 575 576 577 578 579
                              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);

580 581 582
static GSList  *get_selected_files           (GtkFileChooserWidget *impl);
static GSList  *get_selected_infos           (GtkFileChooserWidget *impl);

583 584 585 586
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
587
                                              gboolean               remove_from_treeview);
588
static void     search_entry_activate_cb     (GtkFileChooserWidget *impl);
589
static void     search_entry_stop_cb         (GtkFileChooserWidget *impl);
590 591
static void     settings_load                (GtkFileChooserWidget *impl);

592 593 594
static void     show_filters                 (GtkFileChooserWidget *impl,
                                              gboolean               show);

595
static gboolean recent_files_setting_is_enabled (GtkFileChooserWidget *impl);
596 597 598 599 600 601 602 603
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);

604 605 606 607
static void     clear_model_cache            (GtkFileChooserWidget *impl,
                                              gint                  column);
static void     set_model_filter             (GtkFileChooserWidget *impl,
                                              GtkFileFilter        *filter);
608
static void     switch_to_home_dir           (GtkFileChooserWidget *impl);
609

610

611

612
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserWidget, gtk_file_chooser_widget, GTK_TYPE_BOX,
613
                         G_ADD_PRIVATE (GtkFileChooserWidget)
Matthias Clasen's avatar
Matthias Clasen committed
614 615 616 617
                         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
618 619

static void
620
gtk_file_chooser_widget_iface_init (GtkFileChooserIface *iface)
Owen Taylor's avatar
Owen Taylor committed
621
{
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
  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;
639 640 641 642
  iface->add_choice = gtk_file_chooser_widget_add_choice;
  iface->remove_choice = gtk_file_chooser_widget_remove_choice;
  iface->set_choice = gtk_file_chooser_widget_set_choice;
  iface->get_choice = gtk_file_chooser_widget_get_choice;
643
}
Owen Taylor's avatar
Owen Taylor committed
644

645 646 647 648 649 650 651
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
652

653 654 655 656 657 658 659
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
660 661 662
}

static void
663
pending_select_files_add (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
664
                          GFile                 *file)
Owen Taylor's avatar
Owen Taylor committed
665
{
666
  GtkFileChooserWidgetPrivate *priv = impl->priv;
667

668 669
  priv->pending_select_files =
    g_slist_prepend (priv->pending_select_files, g_object_ref (file));
670 671
}

672 673
static void
gtk_file_chooser_widget_finalize (GObject *object)
674
{
675 676
  GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (object);
  GtkFileChooserWidgetPrivate *priv = impl->priv;
677

678 679 680
  if (priv->choices)
    g_hash_table_unref (priv->choices);

681 682 683
  if (priv->location_changed_id > 0)
    g_source_remove (priv->location_changed_id);

684 685 686
  unset_file_system_backend (impl);

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

688
  g_slist_free_full (priv->filters, g_object_unref);
689

690 691
  if (priv->current_filter)
    g_object_unref (priv->current_filter);
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  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);
709
  g_clear_object (&impl->priv->model_for_search);
710 711 712 713 714 715 716 717 718

  /* 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
719 720
}

721
/* Shows an error dialog set as transient for the specified window */
Owen Taylor's avatar
Owen Taylor committed
722
static void
723
error_message_with_parent (GtkWindow  *parent,
Matthias Clasen's avatar
Matthias Clasen committed
724 725
                           const char *msg,
                           const char *detail)
Owen Taylor's avatar
Owen Taylor committed
726
{
727
  GtkWidget *dialog;
728

729
  dialog = gtk_message_dialog_new (parent,
Matthias Clasen's avatar
Matthias Clasen committed
730 731 732 733 734
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_ERROR,
                                   GTK_BUTTONS_OK,
                                   "%s",
                                   msg);
735
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
Matthias Clasen's avatar
Matthias Clasen committed
736
                                            "%s", detail);
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

  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
762 763
               const char            *msg,
               const char            *detail)
764 765 766 767 768 769 770
{
  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
771 772
              const char            *msg,
              GError                *error)
773 774
{
  if (error)
775
    {
776
      error_message (impl, msg, error->message);
777
      g_error_free (error);
778
    }
Owen Taylor's avatar
Owen Taylor committed
779 780
}

781
/* Shows an error dialog about not being able to create a folder */
Owen Taylor's avatar
Owen Taylor committed
782
static void
783
error_creating_folder_dialog (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
784 785
                              GFile                 *file,
                              GError                *error)
Owen Taylor's avatar
Owen Taylor committed
786
{
787
  error_dialog (impl, 
Matthias Clasen's avatar
Matthias Clasen committed
788
                _("The folder could not be created"), 
789
                error);
790 791 792 793 794 795 796
}

/* 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
797 798
                                                 GFile                 *file,
                                                 GError                *error)
799 800
{
  error_dialog (impl,
Matthias Clasen's avatar
Matthias Clasen committed
801
                _("The folder could not be created, as a file with the same "
802 803
                  "name already exists.  Try using a different name for the "
                  "folder, or rename the file first."),
804
                error);
805 806 807 808
}

static void
error_with_file_under_nonfolder (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
809
                                 GFile *parent_file)
810 811
{
  GError *error;
812
  char *uri, *msg;
813 814 815

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

818 819 820 821 822
  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);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
}

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
847
                                GError                *error)
848
{
849
  error_dialog (impl, _("Invalid file name"), error);
850 851 852 853 854
}

/* 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
855 856
                              GFile                 *file,
                              GError                *error)
857
{
858
  error_dialog (impl, _("The folder contents could not be displayed"), error);
859 860
}

861 862 863 864 865
static void
error_deleting_file (GtkFileChooserWidget *impl,
                     GFile                *file,
                     GError               *error)
{
866
  error_dialog (impl, _("The file could not be deleted"), error);
867 868
}

869 870 871 872 873
static void
error_trashing_file (GtkFileChooserWidget *impl,
                     GFile                *file,
                     GError               *error)
{
874
  error_dialog (impl, _("The file could not be moved to the Trash"), error);
875 876
}

877 878 879
/* 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
880 881
                                 GFile                 *file,
                                 gboolean               clear_entry)
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
{
  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
922 923 924 925 926 927 928
        {
          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);
        }
929 930 931 932
    }
  else
    {
      if (priv->preview_label)
Matthias Clasen's avatar
Matthias Clasen committed
933 934 935 936
        {
          gtk_widget_destroy (priv->preview_label);
          priv->preview_label = NULL;
        }
937 938 939 940 941 942 943 944 945 946 947 948 949
    }

  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
950
                    GtkWidget             *preview_widget)
951 952 953 954 955 956 957 958
{
  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
959
                          priv->preview_widget);
960 961 962 963 964 965 966

  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
967 968
                             priv->preview_widget,
                             (priv->use_preview_label && priv->preview_label) ? 1 : 0);
969 970 971 972 973
    }

  update_preview_widget_visibility (impl);
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
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;
992 993
  GtkWidget *error_label;
  GtkWidget *button;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
};

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");

1023 1024
      gtk_widget_set_sensitive (data->button, FALSE);
      gtk_label_set_text (GTK_LABEL (data->error_label), msg);
1025 1026 1027
    }
  else
    {
1028
      gtk_widget_set_sensitive (data->button, TRUE);
1029
      /* Don't clear the label here, it may contain a warning */
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    }

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

static void
1040 1041 1042 1043
check_valid_child_name (GtkFileChooserWidget *impl,
                        GFile                *parent,
                        const gchar          *name,
                        gboolean              is_folder,
1044
                        GFile                *original,
1045 1046
                        GtkWidget            *error_label,
                        GtkWidget            *button)
1047 1048 1049
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1050
  gtk_widget_set_sensitive (button, FALSE);
1051 1052

  if (name[0] == '\0')
1053
    gtk_label_set_text (GTK_LABEL (error_label), "");
1054
  else if (strcmp (name, ".") == 0)
1055
    gtk_label_set_text (GTK_LABEL (error_label),
1056 1057
                        is_folder ? _("A folder cannot be called “.”")
                                  : _("A file cannot be called “.”"));
1058
  else if (strcmp (name, "..") == 0)
1059
    gtk_label_set_text (GTK_LABEL (error_label),
1060 1061
                        is_folder ? _("A folder cannot be called “..”")
                                  : _("A file cannot be called “..”"));
1062
  else if (strchr (name, '/') != NULL)
1063
    gtk_label_set_text (GTK_LABEL (error_label),
1064 1065
                        is_folder ? _("Folder names cannot contain “/”")
                                  : _("File names cannot contain “/”"));
1066 1067 1068 1069 1070
  else
    {
      GFile *file;
      GError *error = NULL;

1071
      gtk_label_set_text (GTK_LABEL (error_label), "");
1072

1073
      file = g_file_get_child_for_display_name (parent, name, &error);
1074 1075
      if (file == NULL)
        {
1076
          gtk_label_set_text (GTK_LABEL (error_label), error->message);
1077 1078
          g_error_free (error);
        }
1079 1080 1081 1082 1083
      else if (original && g_file_equal (original, file))
        {
          gtk_widget_set_sensitive (button, TRUE);
          g_object_unref (file);
        }
1084 1085 1086 1087
      else
        {
          struct FileExistsData *data;

1088 1089
          /* Warn the user about questionable names that are technically valid */
          if (g_ascii_isspace (name[0]))
1090
            gtk_label_set_text (GTK_LABEL (error_label),
1091 1092
                                is_folder ? _("Folder names should not begin with a space")
                                          : _("File names should not begin with a space"));
1093 1094

          else if (g_ascii_isspace (name[strlen (name) - 1]))
1095
            gtk_label_set_text (GTK_LABEL (error_label),
1096 1097
                                is_folder ? _("Folder names should not end with a space")
                                          : _("File names should not end with a space"));
1098
          else if (name[0] == '.')
1099
            gtk_label_set_text (GTK_LABEL (error_label),
1100 1101
                                is_folder ? _("Folder names starting with a “.” are hidden")
                                          : _("File names starting with a “.” are hidden"));
1102 1103 1104 1105

          data = g_new0 (struct FileExistsData, 1);
          data->impl = g_object_ref (impl);
          data->file = g_object_ref (file);
1106 1107
          data->error_label = error_label;
          data->button = button;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

          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)
{
1128 1129
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1130 1131 1132 1133
  check_valid_child_name (impl,
                          priv->current_folder,
                          gtk_entry_get_text (entry),
                          TRUE,
1134
                          NULL,
1135 1136
                          priv->new_folder_error_label,
                          priv->new_folder_create_button);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
}

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);

1151
  gtk_popover_popdown (GTK_POPOVER (priv->new_folder_popover));
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

  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);
}

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
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
1175 1176 1177
                            GtkTreePath  *path,
                            GtkTreeIter  *iter,
                            gpointer      data)
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
{
  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
1203 1204 1205
                 gint                  *num_selected,
                 gboolean              *all_files,
                 gboolean              *all_folders)
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
{
  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
1218 1219
                                       selection_check_foreach_cb,
                                       &closure);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

  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
1247 1248 1249 1250
places_sidebar_open_location_cb (GtkPlacesSidebar     *sidebar,
                                 GFile                *location,
                                 GtkPlacesOpenFlags    open_flags,
                                 GtkFileChooserWidget *impl)
1251 1252 1253 1254
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  gboolean clear_entry;

1255
  /* In the Save modes, we want to preserve what the user typed in the filename
1256 1257 1258 1259
   * 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
1260
           || priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1261 1262 1263 1264
    clear_entry = TRUE;
  else
    clear_entry = FALSE;

1265 1266
  location_mode_set (impl, LOCATION_MODE_PATH_BAR);

1267 1268 1269 1270 1271 1272 1273 1274 1275
  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
1276 1277 1278
                                      const char       *primary,
                                      const char       *secondary,
                                      GtkFileChooserWidget *impl)
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
{
  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
1291 1292 1293 1294
           || event->keyval == GDK_KEY_KP_Right
           || event->keyval == GDK_KEY_Left
           || event->keyval == GDK_KEY_KP_Left)
          && (event->state & modifiers) == 0);
1295 1296 1297
}

static gboolean
1298 1299
should_trigger_location_entry (GtkFileChooserWidget *impl,
                               GdkEventKey          *event)
1300 1301 1302
{
  GdkModifierType no_text_input_mask;

1303 1304 1305
  if (impl->priv->operation_mode == OPERATION_MODE_SEARCH)
    return FALSE;

1306
  no_text_input_mask =
1307
    gtk_widget_get_modifier_mask (GTK_WIDGET (impl), GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
1308 1309 1310

  if ((event->keyval == GDK_KEY_slash
       || event->keyval == GDK_KEY_KP_Divide
1311
       || event->keyval == GDK_KEY_period
1312 1313 1314 1315
#ifdef G_OS_UNIX
       || event->keyval == GDK_KEY_asciitilde
#endif
       ) && !(event->state & no_text_input_mask))
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    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
1327 1328
                                 GdkEventKey *event,
                                 gpointer     data)
1329 1330 1331 1332
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) data;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1333
  if (should_trigger_location_entry (impl, event) &&
1334 1335
      (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
       priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER))
1336 1337 1338 1339 1340 1341 1342
    {
      location_popup_handler (impl, event->string);
      return TRUE;
    }

  if (key_is_left_or_right (event))
    {
1343 1344
      if (gtk_widget_child_focus (priv->places_sidebar, GTK_DIR_LEFT))
        return TRUE;
1345 1346 1347 1348 1349 1350 1351 1352 1353
    }

  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
1354
           priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    {
      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
1368 1369
            {
              gtk_window_activate_default (window);
1370

Matthias Clasen's avatar
Matthias Clasen committed
1371 1372
              return TRUE;
            }
1373 1374 1375
        }
    }

1376 1377 1378 1379 1380 1381 1382
  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;
    }

1383 1384 1385
  return FALSE;
}

1386 1387 1388 1389 1390 1391 1392
static gboolean
gtk_file_chooser_widget_key_press_event (GtkWidget   *widget,
                                         GdkEventKey *event)
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) widget;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1393
  if (should_trigger_location_entry (impl, event))
1394
    {
1395 1396 1397 1398 1399 1400
      if (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
          priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
        {
          location_popup_handler (impl, event->string);
          return TRUE;
        }
1401
    }
1402
  else if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), (GdkEvent *)event))
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    {
      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;
}

1415 1416 1417 1418 1419
/* 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
1420 1421 1422
                         GtkTreePath  *path,
                         GtkTreeIter  *iter,
                         gpointer      data)
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
{
  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
1439 1440 1441
add_to_shortcuts_cb (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       data)
1442
{
1443
  GtkFileChooserWidget *impl = data;
1444 1445 1446 1447 1448 1449
  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
1450 1451
                                       add_bookmark_foreach_cb,
                                       impl);
1452 1453
}

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
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,
1471
                                   _("Are you sure you want to permanently delete “%s”?"),
1472
                                   name);
1473 1474
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            _("If you delete an item, it will be permanently lost."));
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
  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);
}

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
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);
    }
}

1516
static void
1517 1518 1519
delete_file_cb (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       data)
1520
{
1521
  GtkFileChooserWidget *impl = data;
1522 1523 1524 1525
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
1526 1527
  gtk_tree_selection_selected_foreach (selection, delete_selected_cb, impl);
}
1528

1529 1530 1531 1532 1533 1534 1535 1536 1537
static void
trash_selected_cb (GtkTreeModel *model,
                   GtkTreePath  *path,
                   GtkTreeIter  *iter,
                   gpointer      data)
{
  GtkFileChooserWidget *impl = data;
  GFile *file;
  GError *error = NULL;
1538

1539
  file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
1540

1541 1542
  if (!g_file_trash (file, NULL, &error))
    error_trashing_file (impl, file, error);
1543 1544
}

1545

1546
static void
1547 1548 1549
trash_file_cb (GSimpleAction *action,
               GVariant      *parameter,
               gpointer       data)
1550
{
1551
  GtkFileChooserWidget *impl = data;
1552 1553 1554 1555
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
1556
  gtk_tree_selection_selected_foreach (selection, trash_selected_cb, impl);
1557 1558
}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
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);

1569 1570 1571 1572
  check_valid_child_name (impl,
                          priv->current_folder,
                          gtk_entry_get_text (entry),
                          file_type == G_FILE_TYPE_DIRECTORY,
1573
                          priv->rename_file_source_file,
1574 1575
                          priv->rename_file_error_label,
                          priv->rename_file_rename_button);
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
}

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;

1593
  gtk_popover_popdown (GTK_POPOVER (priv->rename_file_popover));
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607

  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))
1608
            error_dialog (impl, _("The file could not be renamed"), error);
1609 1610 1611 1612 1613 1614 1615 1616 1617

          g_object_unref (child);
        }

      g_object_unref (dest);
    }
}

static void
1618 1619 1620 1621
rename_selected_cb (GtkTreeModel *model,
                    GtkTreePath  *path,
                    GtkTreeIter  *iter,
                    gpointer      data)
1622
{
1623
  GtkFileChooserWidget *impl = data;
1624 1625 1626 1627
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GdkRectangle rect;
  gchar *filename;

1628 1629 1630
  gtk_tree_model_get (model, iter,
                      MODEL_COL_FILE, &priv->rename_file_source_file,
                      -1);
1631

1632 1633
  gtk_tree_view_get_cell_area (GTK_TREE_VIEW (priv->browse_files_tree_view),
                               path, priv->list_name_column, &rect);
1634

1635 1636
  gtk_tree_view_convert_bin_window_to_widget_coords (GTK_TREE_VIEW (priv->browse_files_tree_view),
                                                     rect.x, rect.y, &rect.x, &rect.y);
1637

1638 1639 1640
  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);
1641

1642
  gtk_popover_set_pointing_to (GTK_POPOVER (priv->rename_file_popover), &rect);