gtkfilechooserwidget.c 275 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"
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

79
#include <cairo-gobject.h>
80

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

Matthias Clasen's avatar
Matthias Clasen committed
88 89
/**
 * SECTION:gtkfilechooserwidget
Matthias Clasen's avatar
Matthias Clasen committed
90
 * @Short_description: A file chooser widget
Matthias Clasen's avatar
Matthias Clasen committed
91 92 93 94 95 96 97 98 99 100
 * @Title: GtkFileChooserWidget
 * @See_also: #GtkFileChooserDialog
 *
 * #GtkFileChooserWidget is a widget for choosing files.
 * It exposes the #GtkFileChooser interface, and you should
 * use the methods of this interface to interact with the
 * widget.
 */


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

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

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


#ifndef F_OK 
#define F_OK 0
#endif
116

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

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

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

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

typedef enum {
  LOCATION_MODE_PATH_BAR,
  LOCATION_MODE_FILENAME_ENTRY
} LocationMode;

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

typedef enum {
  STARTUP_MODE_RECENT,
  STARTUP_MODE_CWD
} StartupMode;

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

typedef enum {
  DATE_FORMAT_REGULAR,
  DATE_FORMAT_WITH_TIME
} DateFormat;

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

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

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

246 247
  GtkGesture *long_press_gesture;

248 249 250 251
  GtkFileSystemModel *browse_files_model;
  char *browse_files_last_selected_name;

  GtkWidget *places_sidebar;
252
  GtkWidget *places_view;
253 254 255 256
  StartupMode startup_mode;

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

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

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

282 283
  GtkWidget *external_entry;

284 285 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
  /* 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;
318
  GtkTreeViewColumn *list_time_column;
319
  GtkCellRenderer *list_date_renderer;
320
  GtkCellRenderer *list_time_renderer;
321
  GtkTreeViewColumn *list_size_column;
322
  GtkCellRenderer *list_size_renderer;
323
  GtkTreeViewColumn *list_location_column;
324
  GtkCellRenderer *list_location_renderer;
325

326 327
  guint location_changed_id;

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

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

/* Identifiers for target types */
enum {
  GTK_TREE_MODEL_ROW,
424
};
425

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

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

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

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

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

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

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

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

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

561 562 563
static GSList  *get_selected_files           (GtkFileChooserWidget *impl);
static GSList  *get_selected_infos           (GtkFileChooserWidget *impl);

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

573 574 575
static void     show_filters                 (GtkFileChooserWidget *impl,
                                              gboolean               show);

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

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

590

591

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

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

621 622 623 624 625 626 627
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
628

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

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

644 645
  priv->pending_select_files =
    g_slist_prepend (priv->pending_select_files, g_object_ref (file));
646 647
}

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

654 655 656
  if (priv->location_changed_id > 0)
    g_source_remove (priv->location_changed_id);

657 658 659
  unset_file_system_backend (impl);

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

661
  g_slist_free_full (priv->filters, g_object_unref);
662

663 664
  if (priv->current_filter)
    g_object_unref (priv->current_filter);
665

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

  /* 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
692 693
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  update_preview_widget_visibility (impl);
}

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

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

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

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

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

1031
  gtk_widget_set_sensitive (button, FALSE);
1032 1033

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

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

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

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

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

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

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

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

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

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

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

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

1246 1247
  location_mode_set (impl, LOCATION_MODE_PATH_BAR);

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

static gboolean
1279 1280
should_trigger_location_entry (GtkWidget   *widget,
                               GdkEventKey *event)
1281 1282 1283 1284 1285 1286 1287 1288
{
  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
1289
       || event->keyval == GDK_KEY_period
1290 1291 1292 1293
#ifdef G_OS_UNIX
       || event->keyval == GDK_KEY_asciitilde
#endif
       ) && !(event->state & no_text_input_mask))
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    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
1305 1306
                                 GdkEventKey *event,
                                 gpointer     data)
1307 1308 1309 1310
{
  GtkFileChooserWidget *impl = (GtkFileChooserWidget *) data;
  GtkFileChooserWidgetPrivate *priv = impl->priv;

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

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

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

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

  return FALSE;
}

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

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

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

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

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

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

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
1497 1498
  gtk_tree_selection_selected_foreach (selection, delete_selected_cb, impl);
}
1499

1500 1501 1502 1503 1504 1505 1506 1507 1508
static void
trash_selected_cb (GtkTreeModel *model,
                   GtkTreePath  *path,
                   GtkTreeIter  *iter,
                   gpointer      data)
{
  GtkFileChooserWidget *impl = data;
  GFile *file;
  GError *error = NULL;
1509

1510
  file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
1511

1512 1513
  if (!g_file_trash (file, NULL, &error))
    error_trashing_file (impl, file, error);
1514 1515
}

1516

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

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

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
static void
rename_file_name_changed (GtkEntry             *entry,
                          GtkFileChooserWidget *impl)
{
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GFileType file_type;

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

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

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

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

  gtk_widget_hide (priv->rename_file_popover);

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

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

      child = g_file_get_child (dest, new_name);
      if (child)
        {
          if (!g_file_move (priv->rename_file_source_file, child, G_FILE_COPY_NONE,
                            NULL, NULL, NULL, &error))
            error_dialog (impl, _("The file could not be renamed"), child, error);

          g_object_unref (child);
        }

      g_object_unref (dest);
    }
}

static void
1589 1590 1591 1592
rename_selected_cb (GtkTreeModel *model,
                    GtkTreePath  *path,
                    GtkTreeIter  *iter,
                    gpointer      data)
1593
{
1594
  GtkFileChooserWidget *impl = data;
1595 1596 1597 1598
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GdkRectangle rect;
  gchar *filename;

1599 1600 1601
  gtk_tree_model_get (model, iter,
                      MODEL_COL_FILE, &priv->rename_file_source_file,
                      -1);
1602

1603 1604
  gtk_tree_view_get_cell_area (GTK_TREE_VIEW (priv->browse_files_tree_view),
                               path, priv->list_name_column, &rect);
1605

1606 1607
  gtk_tree_view_convert_tree_to_widget_coords (GTK_TREE_VIEW (priv->browse_files_tree_view),
                                               rect.x, rect.y, &rect.x, &rect.y);
1608

1609 1610 1611
  filename = g_file_get_basename (priv->rename_file_source_file);
  gtk_entry_set_text (GTK_ENTRY(priv->rename_file_name_entry), filename);
  g_free (filename);
1612

1613 1614 1615 1616 1617 1618
  gtk_popover_set_pointing_to (GTK_POPOVER (priv->rename_file_popover), &rect);
  gtk_widget_show (priv->rename_file_popover);
  gtk_widget_grab_focus (priv->rename_file_popover);
}

static void
1619 1620 1621
rename_file_cb (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       data)
1622
{
1623
  GtkFileChooserWidget *impl = data;
1624 1625
  GtkFileChooserWidgetPrivate *priv = impl->priv;
  GtkTreeSelection *selection;
1626

1627 1628
  /* insensitive until we change the name */
  gtk_widget_set_sensitive (priv->rename_file_rename_button, FALSE);
1629

1630 1631
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->browse_files_tree_view));
  gtk_tree_selection_selected_foreach (selection, rename_selected_cb, impl);
1632 1633
}

1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
/* callback used to set data to clipboard */
static void
copy_file_get_cb  (GtkClipboard     *clipboard,
                   GtkSelectionData *selection_data,
                   guint             info,
                   gpointer          data)
{
  GSList *selected_files = data;

  if (selected_files)
    {
      gint num_files = g_slist_length (selected_files);
      gchar **uris;
      gint i;
      GSList *l;

      uris = g_new (gchar *, num_files + 1);
      uris[num_files] = NULL; /* null terminator */

      i = 0;

      for (l = selected_files; l; l = l->next)
        {
          GFile *file = (GFile *) l->data;

Matthias Clasen's avatar
Matthias Clasen committed
1659 1660 1661 1662
          if (info == SELECTION_URI)
            uris[i] = g_file_get_uri (file);
          else /* if (info == SELECTION_TEXT) - let this be the fallback */
            uris[i] = g_file_get_parse_name (file);
1663 1664 1665 1666 1667

          i++;
        }

      if (info == SELECTION_URI)
Matthias Clasen's avatar
Matthias Clasen committed
1668
        gtk_selection_data_set_uris (selection_data, uris);
1669
      else /* if (info == SELECTION_TEXT) - let this be the fallback */
Matthias Clasen's avatar
Matthias Clasen committed
1670 1671 1672 1673 1674
        {
          char *str = g_strjoinv (" ", uris);
          gtk_selection_data_set_text (selection_data, str, -1);
          g_free (str);
        }
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686

      g_strfreev (uris);
    }
}

/* callback used to clear the clipboard data */
static void
copy_file_clear_cb (GtkClipboard *clipboard,
                    gpointer      data)
{
  GSList *selected_files = data;

1687
  g_slist_free_full (selected_files, g_object_unref);
1688 1689 1690 1691
}

/* Callback used when the "Copy file’s location" menu item is activated */
static void
1692 1693 1694
copy_file_location_cb (GSimpleAction *action,
                       GVariant      *parameter,
                       gpointer       data)
1695
{
1696
  GtkFileChooserWidget *impl = data;
1697 1698
  GSList *selected_files = NULL;

1699
  selected_files = get_selected_files (impl);