gtkfilechooserwidget.c 278 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 35 36 37
#include "gtkentry.h"
#include "gtkexpander.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 "gtkwindowgroup.h"
Matthias Clasen's avatar
Matthias Clasen committed
69
#include "gtkintl.h"
70 71
#include "gtkshow.h"
#include "gtkmain.h"
72
#include "gtkscrollable.h"
73
#include "gtkpopover.h"
74
#include "gtkrevealer.h"
75
#include "gtkspinner.h"
76 77 78
#include "gtkseparator.h"
#include "gtkmodelbutton.h"
#include "gtkgesturelongpress.h"
79
#include "gtkdebug.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 100 101 102
 * @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.
 */


103 104 105 106
/* Values for GtkSelection-related "info" fields */
#define SELECTION_TEXT 0
#define SELECTION_URI  1

107 108 109
/* 150 mseconds of delay */
#define LOCATION_CHANGED_TIMEOUT 150

110 111 112 113 114 115 116 117
/* Profiling stuff */
#undef PROFILE_FILE_CHOOSER
#ifdef PROFILE_FILE_CHOOSER


#ifndef F_OK 
#define F_OK 0
#endif
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132
#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)
133
{
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  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

160
enum {
161 162
  PROP_SEARCH_MODE = 1,
  PROP_SUBTITLE
163
};
164 165

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
166 167 168 169
  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 */
170 171 172
} LoadState;

typedef enum {
Matthias Clasen's avatar
Matthias Clasen committed
173 174
  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 */
175 176 177 178 179 180 181 182 183 184
} ReloadState;

typedef enum {
  LOCATION_MODE_PATH_BAR,
  LOCATION_MODE_FILENAME_ENTRY
} LocationMode;

typedef enum {
  OPERATION_MODE_BROWSE,
  OPERATION_MODE_SEARCH,
185
  OPERATION_MODE_ENTER_LOCATION,
186
  OPERATION_MODE_OTHER_LOCATIONS,
187 188 189 190 191 192 193 194
  OPERATION_MODE_RECENT
} OperationMode;

typedef enum {
  STARTUP_MODE_RECENT,
  STARTUP_MODE_CWD
} StartupMode;

195 196 197 198 199 200 201 202 203 204
typedef enum {
  CLOCK_FORMAT_24,
  CLOCK_FORMAT_12
} ClockFormat;

typedef enum {
  DATE_FORMAT_REGULAR,
  DATE_FORMAT_WITH_TIME
} DateFormat;

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

  GtkWidget *browse_files_popover;
224 225 226 227 228 229
  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;
230
  GtkWidget *rename_file_item;
231
  GtkWidget *trash_file_item;
232
  GtkWidget *delete_file_item;
233 234
  GtkWidget *sort_directories_item;
  GtkWidget *show_time_item;
235

236 237 238
  GtkWidget *browse_new_folder_button;
  GtkSizeGroup *browse_path_bar_size_group;
  GtkWidget *browse_path_bar;
239 240 241 242
  GtkWidget *new_folder_name_entry;
  GtkWidget *new_folder_create_button;
  GtkWidget *new_folder_error_label;
  GtkWidget *new_folder_popover;
243 244 245 246 247
  GtkWidget *rename_file_name_entry;
  GtkWidget *rename_file_rename_button;
  GtkWidget *rename_file_error_label;
  GtkWidget *rename_file_popover;
  GFile *rename_file_source_file;
248

249 250
  GtkGesture *long_press_gesture;

251 252 253 254
  GtkFileSystemModel *browse_files_model;
  char *browse_files_last_selected_name;

  GtkWidget *places_sidebar;
255
  GtkWidget *places_view;
256 257 258 259
  StartupMode startup_mode;

  /* OPERATION_MODE_SEARCH */
  GtkWidget *search_entry;
260 261
  GtkWidget *search_spinner;
  guint show_progress_timeout;
262 263 264
  GtkSearchEngine *search_engine;
  GtkQuery *search_query;
  GtkFileSystemModel *search_model;
265
  GtkFileSystemModel *model_for_search;
266 267 268 269 270 271

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

272
  GtkWidget *extra_and_filters;
273 274 275 276 277 278 279 280 281 282 283 284
  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;

285 286
  GtkWidget *external_entry;

287 288 289 290 291 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
  /* 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;
321
  GtkTreeViewColumn *list_time_column;
322
  GtkCellRenderer *list_date_renderer;
323
  GtkCellRenderer *list_time_renderer;
324
  GtkTreeViewColumn *list_size_column;
325
  GtkCellRenderer *list_size_renderer;
326
  GtkTreeViewColumn *list_location_column;
327
  GtkCellRenderer *list_location_renderer;
328

329 330
  guint location_changed_id;

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

  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," \
386
                         "standard::content-type,time::modified,time::access," \
387 388
                         "access::can-rename,access::can-delete,access::can-trash," \
                         "standard::target-uri"
389 390 391 392
enum {
  /* the first 3 must be these due to settings caching sort column */
  MODEL_COL_NAME,
  MODEL_COL_SIZE,
393
  MODEL_COL_TIME,
394 395 396 397 398 399
  MODEL_COL_FILE,
  MODEL_COL_NAME_COLLATED,
  MODEL_COL_IS_FOLDER,
  MODEL_COL_IS_SENSITIVE,
  MODEL_COL_SURFACE,
  MODEL_COL_SIZE_TEXT,
400
  MODEL_COL_DATE_TEXT,
401
  MODEL_COL_TIME_TEXT,
402
  MODEL_COL_LOCATION_TEXT,
403 404 405 406 407
  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
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
#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 */
423 424 425 426

/* Identifiers for target types */
enum {
  GTK_TREE_MODEL_ROW,
427
};
428

429 430 431 432 433 434 435 436 437 438 439 440
#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);

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

Matthias Clasen's avatar
Matthias Clasen committed
464 465 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
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);
491
static gboolean       gtk_file_chooser_widget_add_shortcut_folder    (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
492 493
                                                                       GFile             *file,
                                                                       GError           **error);
494
static gboolean       gtk_file_chooser_widget_remove_shortcut_folder (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
495 496
                                                                       GFile             *file,
                                                                       GError           **error);
497 498 499
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
500 501
                                                                       gint                *default_width,
                                                                       gint                *default_height);
502 503 504 505 506 507
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
508
                                     const gchar           *path);
509 510 511 512 513 514 515
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
516
                                     gint                   bookmark_index);
517 518 519
static void show_hidden_handler     (GtkFileChooserWidget *impl);
static void search_shortcut_handler (GtkFileChooserWidget *impl);
static void recent_shortcut_handler (GtkFileChooserWidget *impl);
520
static void places_shortcut_handler (GtkFileChooserWidget *impl);
521 522 523
static void update_appearance       (GtkFileChooserWidget *impl);

static void operation_mode_set (GtkFileChooserWidget *impl, OperationMode mode);
524
static void location_mode_set  (GtkFileChooserWidget *impl, LocationMode new_mode);
525 526

static void set_current_filter   (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
527
                                  GtkFileFilter         *filter);
528 529 530
static void check_preview_change (GtkFileChooserWidget *impl);

static void filter_combo_changed       (GtkComboBox           *combo_box,
Matthias Clasen's avatar
Matthias Clasen committed
531
                                        GtkFileChooserWidget *impl);
532 533

static gboolean list_select_func   (GtkTreeSelection      *selection,
Matthias Clasen's avatar
Matthias Clasen committed
534 535 536 537
                                    GtkTreeModel          *model,
                                    GtkTreePath           *path,
                                    gboolean               path_currently_selected,
                                    gpointer               data);
538 539

static void list_selection_changed     (GtkTreeSelection      *tree_selection,
Matthias Clasen's avatar
Matthias Clasen committed
540
                                        GtkFileChooserWidget  *impl);
541
static void list_row_activated         (GtkTreeView           *tree_view,
Matthias Clasen's avatar
Matthias Clasen committed
542 543 544
                                        GtkTreePath           *path,
                                        GtkTreeViewColumn     *column,
                                        GtkFileChooserWidget  *impl);
545 546
static void list_cursor_changed        (GtkTreeView           *treeview,
                                        GtkFileChooserWidget  *impl);
547 548

static void path_bar_clicked (GtkPathBar            *path_bar,
Matthias Clasen's avatar
Matthias Clasen committed
549 550
                              GFile                 *file,
                              GFile                 *child,
551 552 553 554 555 556 557 558 559 560 561 562 563
                              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);

564 565 566
static GSList  *get_selected_files           (GtkFileChooserWidget *impl);
static GSList  *get_selected_infos           (GtkFileChooserWidget *impl);

567 568 569 570
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
571
                                              gboolean               remove_from_treeview);
572
static void     search_entry_activate_cb     (GtkFileChooserWidget *impl);
573
static void     search_entry_stop_cb         (GtkFileChooserWidget *impl);
574 575
static void     settings_load                (GtkFileChooserWidget *impl);

576 577 578
static void     show_filters                 (GtkFileChooserWidget *impl,
                                              gboolean               show);

579
static gboolean recent_files_setting_is_enabled (GtkFileChooserWidget *impl);
580 581 582 583 584 585 586 587
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);

588 589 590 591 592
static void     clear_model_cache            (GtkFileChooserWidget *impl,
                                              gint                  column);
static void     set_model_filter             (GtkFileChooserWidget *impl,
                                              GtkFileFilter        *filter);

593

594

595
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserWidget, gtk_file_chooser_widget, GTK_TYPE_BOX,
596
                         G_ADD_PRIVATE (GtkFileChooserWidget)
Matthias Clasen's avatar
Matthias Clasen committed
597 598 599 600
                         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
601 602

static void
603
gtk_file_chooser_widget_iface_init (GtkFileChooserIface *iface)
Owen Taylor's avatar
Owen Taylor committed
604
{
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  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
623

624 625 626 627 628 629 630
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
631

632 633 634 635 636 637 638
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
639 640 641
}

static void
642
pending_select_files_add (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
643
                          GFile                 *file)
Owen Taylor's avatar
Owen Taylor committed
644
{
645
  GtkFileChooserWidgetPrivate *priv = impl->priv;
646

647 648
  priv->pending_select_files =
    g_slist_prepend (priv->pending_select_files, g_object_ref (file));
649 650
}

651 652
static void
gtk_file_chooser_widget_finalize (GObject *object)
653
{
654 655
  GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (object);
  GtkFileChooserWidgetPrivate *priv = impl->priv;
656

657 658 659
  if (priv->location_changed_id > 0)
    g_source_remove (priv->location_changed_id);

660 661 662
  unset_file_system_backend (impl);

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

664
  g_slist_free_full (priv->filters, g_object_unref);
665

666 667
  if (priv->current_filter)
    g_object_unref (priv->current_filter);
668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
  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);
685
  g_clear_object (&impl->priv->model_for_search);
686 687 688 689 690 691 692 693 694

  /* 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
695 696
}

697
/* Shows an error dialog set as transient for the specified window */
Owen Taylor's avatar
Owen Taylor committed
698
static void
699
error_message_with_parent (GtkWindow  *parent,
Matthias Clasen's avatar
Matthias Clasen committed
700 701
                           const char *msg,
                           const char *detail)
Owen Taylor's avatar
Owen Taylor committed
702
{
703
  GtkWidget *dialog;
704

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

  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
738 739
               const char            *msg,
               const char            *detail)
740 741 742 743 744 745 746
{
  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
747 748 749
              const char            *msg,
              GFile                 *file,
              GError                *error)
750 751
{
  if (error)
752
    {
753 754 755 756
      char *uri = NULL;
      char *text;

      if (file)
Matthias Clasen's avatar
Matthias Clasen committed
757
        uri = g_file_get_uri (file);
758 759 760 761 762
      text = g_strdup_printf (msg, uri);
      error_message (impl, text, error->message);
      g_free (text);
      g_free (uri);
      g_error_free (error);
763
    }
Owen Taylor's avatar
Owen Taylor committed
764 765
}

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

/* 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
782 783
                                                 GFile                 *file,
                                                 GError                *error)
784 785
{
  error_dialog (impl,
Matthias Clasen's avatar
Matthias Clasen committed
786
                _("The folder could not be created, as a file with the same "
787 788
                  "name already exists.  Try using a different name for the "
                  "folder, or rename the file first."),
Matthias Clasen's avatar
Matthias Clasen committed
789
                file, error);
790 791 792 793
}

static void
error_with_file_under_nonfolder (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
794
                                 GFile *parent_file)
795 796 797 798 799
{
  GError *error;

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

  error_dialog (impl,
Matthias Clasen's avatar
Matthias Clasen committed
803 804
                _("Cannot create a file under %s as it is not a folder"),
                parent_file, error);
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"), 
Matthias Clasen's avatar
Matthias Clasen committed
832
                NULL, error);
833 834 835 836 837
}

/* 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
838 839
                              GFile                 *file,
                              GError                *error)
840 841
{
  error_dialog (impl, _("The folder contents could not be displayed"),
Matthias Clasen's avatar
Matthias Clasen committed
842
                file, error);
843 844
}

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

853 854 855 856 857
static void
error_trashing_file (GtkFileChooserWidget *impl,
                     GFile                *file,
                     GError               *error)
{
Piotr Drąg's avatar
Piotr Drąg committed
858
  error_dialog (impl, _("The file could not be moved to the Trash"), file, error);
859 860
}

861 862 863
/* 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
864 865
                                 GFile                 *file,
                                 gboolean               clear_entry)
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 904 905
{
  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
906 907 908 909 910 911 912
        {
          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);
        }
913 914 915 916
    }
  else
    {
      if (priv->preview_label)
Matthias Clasen's avatar
Matthias Clasen committed
917 918 919 920
        {
          gtk_widget_destroy (priv->preview_label);
          priv->preview_label = NULL;
        }
921 922 923 924 925 926 927 928 929 930 931 932 933
    }

  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
934
                    GtkWidget             *preview_widget)
935 936 937 938 939 940 941 942
{
  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
943
                          priv->preview_widget);
944 945 946 947 948 949 950

  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
951 952
                             priv->preview_widget,
                             (priv->use_preview_label && priv->preview_label) ? 1 : 0);
953 954 955 956 957
    }

  update_preview_widget_visibility (impl);
}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
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;
976 977
  GtkWidget *error_label;
  GtkWidget *button;
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 1005 1006
};

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

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

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

static void
Matthias Clasen's avatar
Matthias Clasen committed
1024 1025 1026 1027
check_valid_child_name (GtkFileChooserWidget *impl,
                        GFile                *parent,
                        const gchar          *name,
                        gboolean              is_folder,
1028
                        GFile                *original,
Matthias Clasen's avatar
Matthias Clasen committed
1029 1030
                        GtkWidget            *error_label,
                        GtkWidget            *button)
1031 1032 1033
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1034
  gtk_widget_set_sensitive (button, FALSE);
1035 1036

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

1055
      gtk_label_set_text (GTK_LABEL (error_label), "");
1056

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

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

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

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

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

Matthias Clasen's avatar
Matthias Clasen committed
1114 1115 1116 1117
  check_valid_child_name (impl,
                          priv->current_folder,
                          gtk_entry_get_text (entry),
                          TRUE,
1118
                          NULL,
Matthias Clasen's avatar
Matthias Clasen committed
1119 1120
                          priv->new_folder_error_label,
                          priv->new_folder_create_button);
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 1147 1148
}

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

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
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
1159 1160 1161
                            GtkTreePath  *path,
                            GtkTreeIter  *iter,
                            gpointer      data)
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
{
  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
1187 1188 1189
                 gint                  *num_selected,
                 gboolean              *all_files,
                 gboolean              *all_folders)
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
{
  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
1202 1203
                                       selection_check_foreach_cb,
                                       &closure);
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 1229 1230

  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
1231 1232 1233 1234
places_sidebar_open_location_cb (GtkPlacesSidebar     *sidebar,
                                 GFile                *location,
                                 GtkPlacesOpenFlags    open_flags,
                                 GtkFileChooserWidget *impl)
1235 1236 1237 1238
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  gboolean clear_entry;

1239
  /* In the Save modes, we want to preserve what the user typed in the filename
1240 1241 1242 1243
   * 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
1244
           || priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1245 1246 1247 1248
    clear_entry = TRUE;
  else
    clear_entry = FALSE;

1249 1250
  location_mode_set (impl, LOCATION_MODE_PATH_BAR);

1251 1252 1253 1254 1255 1256 1257 1258 1259
  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
1260 1261 1262
                                      const char       *primary,
                                      const char       *secondary,
                                      GtkFileChooserWidget *impl)
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
{
  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
1275 1276 1277 1278
           || event->keyval == GDK_KEY_KP_Right
           || event->keyval == GDK_KEY_Left
           || event->keyval == GDK_KEY_KP_Left)
          && (event->state & modifiers) == 0);
1279 1280 1281
}

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

1287 1288 1289
  if (impl->priv->operation_mode == OPERATION_MODE_SEARCH)
    return FALSE;

1290
  no_text_input_mask =
1291
    gtk_widget_get_modifier_mask (GTK_WIDGET (impl), GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
1292 1293 1294

  if ((event->keyval == GDK_KEY_slash
       || event->keyval == GDK_KEY_KP_Divide
1295
       || event->keyval == GDK_KEY_period
1296 1297 1298 1299
#ifdef G_OS_UNIX
       || event->keyval == GDK_KEY_asciitilde
#endif
       ) && !(event->state & no_text_input_mask))
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    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
1311 1312
                                 GdkEventKey *event,
                                 gpointer     data)
1313 1314 1315 1316
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) data;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

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

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

  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
1338
           priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
    {
      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
1352 1353
            {
              gtk_window_activate_default (window);
1354

Matthias Clasen's avatar
Matthias Clasen committed
1355 1356
              return TRUE;
            }
1357 1358 1359
        }
    }

1360 1361 1362 1363 1364 1365 1366
  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;
    }

1367 1368 1369
  return FALSE;
}

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

1377
  if (should_trigger_location_entry (impl, event))
1378
    {
1379 1380 1381 1382 1383 1384
      if (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
          priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
        {
          location_popup_handler (impl, event->string);
          return TRUE;
        }
1385
    }
1386
  else if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), (GdkEvent *)event))
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    {
      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;
}

1399 1400 1401 1402 1403
/* 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
1404 1405 1406
                         GtkTreePath  *path,
                         GtkTreeIter  *iter,
                         gpointer      data)
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
{
  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
1423 1424 1425
add_to_shortcuts_cb (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       data)
1426
{
1427
  GtkFileChooserWidget *impl = data;
1428 1429 1430 1431 1432 1433
  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
1434 1435
                                       add_bookmark_foreach_cb,
                                       impl);
1436 1437
}

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
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,
1455
                                   _("Are you sure you want to permanently delete “%s”?"),
1456
                                   name);
1457 1458
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            _("If you delete an item, it will be permanently lost."));
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
  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);
}

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495