gtkfilechooserwidget.c 277 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 33
#include "gtkcelllayout.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkcheckmenuitem.h"
#include "gtkclipboard.h"
#include "gtkcomboboxtext.h"
#include "gtkentry.h"
34
#include "gtkstack.h"
35 36 37
#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 62 63 64 65 66
#include "gtkseparatormenuitem.h"
#include "gtksettings.h"
#include "gtksizegroup.h"
#include "gtksizerequest.h"
#include "gtktooltip.h"
#include "gtktreednd.h"
#include "gtktreeprivate.h"
#include "gtktreeselection.h"
#include "gtkbox.h"
67
#include "gtkwindowgroup.h"
Matthias Clasen's avatar
Matthias Clasen committed
68
#include "gtkintl.h"
69 70
#include "gtkshow.h"
#include "gtkmain.h"
71
#include "gtkscrollable.h"
72
#include "gtkpopover.h"
73
#include "gtkrevealer.h"
74
#include "gtkspinner.h"
75 76 77
#include "gtkseparator.h"
#include "gtkmodelbutton.h"
#include "gtkgesturelongpress.h"
78
#include "gtkdebug.h"
79

80
#include <cairo-gobject.h>
81

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

Matthias Clasen's avatar
Matthias Clasen committed
89 90
/**
 * SECTION:gtkfilechooserwidget
Matthias Clasen's avatar
Matthias Clasen committed
91
 * @Short_description: A file chooser widget
Matthias Clasen's avatar
Matthias Clasen committed
92 93 94 95 96 97 98 99 100 101
 * @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.
 */


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

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

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


#ifndef F_OK 
#define F_OK 0
#endif
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131
#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)
132
{
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
  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

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

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

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

typedef enum {
  LOCATION_MODE_PATH_BAR,
  LOCATION_MODE_FILENAME_ENTRY
} LocationMode;

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

typedef enum {
  STARTUP_MODE_RECENT,
  STARTUP_MODE_CWD
} StartupMode;

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

typedef enum {
  DATE_FORMAT_REGULAR,
  DATE_FORMAT_WITH_TIME
} DateFormat;

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

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

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

248 249
  GtkGesture *long_press_gesture;

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

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

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

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

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

284 285
  GtkWidget *external_entry;

286 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
  /* 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;
320
  GtkTreeViewColumn *list_time_column;
321
  GtkCellRenderer *list_date_renderer;
322
  GtkCellRenderer *list_time_renderer;
323
  GtkTreeViewColumn *list_size_column;
324
  GtkCellRenderer *list_size_renderer;
325
  GtkTreeViewColumn *list_location_column;
326
  GtkCellRenderer *list_location_renderer;
327

328 329
  guint location_changed_id;

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

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

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

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

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

Matthias Clasen's avatar
Matthias Clasen committed
463 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
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);
490
static gboolean       gtk_file_chooser_widget_add_shortcut_folder    (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
491 492
                                                                       GFile             *file,
                                                                       GError           **error);
493
static gboolean       gtk_file_chooser_widget_remove_shortcut_folder (GtkFileChooser    *chooser,
Matthias Clasen's avatar
Matthias Clasen committed
494 495
                                                                       GFile             *file,
                                                                       GError           **error);
496 497 498
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
499 500
                                                                       gint                *default_width,
                                                                       gint                *default_height);
501 502 503 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);

static void add_selection_to_recent_list (GtkFileChooserWidget *impl);

static void location_popup_handler  (GtkFileChooserWidget *impl,
Matthias Clasen's avatar
Matthias Clasen committed
507
                                     const gchar           *path);
508 509 510 511 512 513 514
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
515
                                     gint                   bookmark_index);
516 517 518
static void show_hidden_handler     (GtkFileChooserWidget *impl);
static void search_shortcut_handler (GtkFileChooserWidget *impl);
static void recent_shortcut_handler (GtkFileChooserWidget *impl);
519
static void places_shortcut_handler (GtkFileChooserWidget *impl);
520 521 522
static void update_appearance       (GtkFileChooserWidget *impl);

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

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

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

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

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

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

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

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

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

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

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

592

593

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

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

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

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

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

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

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

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

659 660 661
  unset_file_system_backend (impl);

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

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

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

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

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

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

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

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

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

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

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

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

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

  error_dialog (impl,
Matthias Clasen's avatar
Matthias Clasen committed
802 803
                _("Cannot create a file under %s as it is not a folder"),
                parent_file, error);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
}

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
828
                                GError                *error)
829 830
{
  error_dialog (impl, _("Invalid file name"), 
Matthias Clasen's avatar
Matthias Clasen committed
831
                NULL, error);
832 833 834 835 836
}

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

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

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

860 861 862
/* 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
863 864
                                 GFile                 *file,
                                 gboolean               clear_entry)
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
{
  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
905 906 907 908 909 910 911
        {
          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);
        }
912 913 914 915
    }
  else
    {
      if (priv->preview_label)
Matthias Clasen's avatar
Matthias Clasen committed
916 917 918 919
        {
          gtk_widget_destroy (priv->preview_label);
          priv->preview_label = NULL;
        }
920 921 922 923 924 925 926 927 928 929 930 931 932
    }

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

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

  update_preview_widget_visibility (impl);
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
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;
975 976
  GtkWidget *error_label;
  GtkWidget *button;
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
};

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

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

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

1033
  gtk_widget_set_sensitive (button, FALSE);
1034 1035

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

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

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

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

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

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

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

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

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

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
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
1158 1159 1160
                            GtkTreePath  *path,
                            GtkTreeIter  *iter,
                            gpointer      data)
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
{
  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
1186 1187 1188
                 gint                  *num_selected,
                 gboolean              *all_files,
                 gboolean              *all_folders)
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
{
  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
1201 1202
                                       selection_check_foreach_cb,
                                       &closure);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

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

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

1248 1249
  location_mode_set (impl, LOCATION_MODE_PATH_BAR);

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

static gboolean
1281 1282
should_trigger_location_entry (GtkWidget   *widget,
                               GdkEventKey *event)
1283 1284 1285 1286 1287 1288 1289 1290
{
  GdkModifierType no_text_input_mask;

  no_text_input_mask =
    gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);

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

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

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

  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
1334
           priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
    {
      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
1348 1349
            {
              gtk_window_activate_default (window);
1350

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

  return FALSE;
}

1359 1360 1361 1362 1363 1364 1365
static gboolean
gtk_file_chooser_widget_key_press_event (GtkWidget   *widget,
                                         GdkEventKey *event)
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) widget;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

1366 1367
  if (should_trigger_location_entry (widget, event))
    {
1368 1369 1370 1371 1372 1373
      if (priv->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
          priv->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
        {
          location_popup_handler (impl, event->string);
          return TRUE;
        }
1374
    }
1375
  else if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), (GdkEvent *)event))
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    {
      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;
}

1388 1389 1390 1391 1392
/* 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
1393 1394 1395
                         GtkTreePath  *path,
                         GtkTreeIter  *iter,
                         gpointer      data)
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
{
  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
1412 1413 1414
add_to_shortcuts_cb (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       data)
1415
{
1416
  GtkFileChooserWidget *impl = data;
1417 1418 1419 1420 1421 1422
  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
1423 1424
                                       add_bookmark_foreach_cb,
                                       impl);
1425 1426
}

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
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,
1444
                                   _("Are you sure you want to permanently delete “%s”?"),
1445
                                   name);
1446 1447
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            _("If you delete an item, it will be permanently lost."));
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
  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);
}

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
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);
    }
}

1489
static void
1490 1491 1492
delete_file_cb (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       data)
1493
{
1494
  GtkFileChooserWidget *impl = data;
1495 1496 1497 1498
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
Matthias Clasen's avatar