gtkfilechooserentry.c 52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* GTK - The GIMP Toolkit
 * gtkfilechooserentry.c: Entry with filename completion
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21
#include "config.h"
22 23
#include <string.h>

24
#include "gtkalignment.h"
25 26
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"
27 28
#include "gtkentry.h"
#include "gtkfilechooserentry.h"
29
#include "gtklabel.h"
30
#include "gtkmain.h"
31
#include "gtkwindow.h"
32
#include "gtkintl.h"
33
#include "gtkalias.h"
34

35 36 37 38 39 40 41 42 43 44 45
typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;

#define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
#define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
#define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))

struct _GtkFileChooserEntryClass
{
  GtkEntryClass parent_class;
};

46 47 48 49
/* Action to take when the current folder finishes loading (for explicit or automatic completion) */
typedef enum {
  LOAD_COMPLETE_NOTHING,
  LOAD_COMPLETE_AUTOCOMPLETE,
50
  LOAD_COMPLETE_EXPLICIT_COMPLETION
51 52
} LoadCompleteAction;

53 54 55 56
struct _GtkFileChooserEntry
{
  GtkEntry parent_instance;

57 58
  GtkFileChooserAction action;

59
  GtkFileSystem *file_system;
60 61
  GFile *base_folder;
  GFile *current_folder_file;
62
  gchar *file_part;
63
  gint file_part_pos;
64

65
  /* Folder being loaded or already loaded */
66 67
  GtkFolder *current_folder;
  GCancellable *load_folder_cancellable;
68

69 70
  LoadCompleteAction load_complete_action;

71 72
  GtkListStore *completion_store;

73 74
  guint start_autocompletion_idle_id;

75 76
  GtkWidget *completion_feedback_window;
  GtkWidget *completion_feedback_label;
77
  guint completion_feedback_timeout_id;
78

79
  guint has_completion : 1;
80
  guint in_change      : 1;
81
  guint eat_tabs       : 1;
82 83
};

84 85 86
enum
{
  DISPLAY_NAME_COLUMN,
87
  FILE_COLUMN,
88 89 90
  N_COLUMNS
};

91 92
#define COMPLETION_FEEDBACK_TIMEOUT_MS 2000

Matthias Clasen's avatar
Matthias Clasen committed
93
static void     gtk_file_chooser_entry_iface_init     (GtkEditableClass *iface);
94 95

static void     gtk_file_chooser_entry_finalize       (GObject          *object);
96
static void     gtk_file_chooser_entry_dispose        (GObject          *object);
97
static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
98
static void     gtk_file_chooser_entry_unmap          (GtkWidget        *widget);
99 100
static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
						       GtkDirectionType  direction);
101 102
static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
							GdkEventFocus   *event);
103
static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
104 105 106 107
static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
						       const gchar *new_text,
						       gint         new_text_length,
						       gint        *position);
108 109 110 111 112 113 114 115
static void     gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
						       gint         start_pos,
						       gint         end_pos);
static void     gtk_file_chooser_entry_set_position (GtkEditable *editable,
						     gint         position);
static void     gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
							     gint         start_pos,
							     gint         end_pos);
116 117 118 119 120 121 122 123 124 125 126 127 128

#ifdef G_OS_WIN32
static gint     insert_text_callback      (GtkFileChooserEntry *widget,
					   const gchar         *new_text,
					   gint                 new_text_length,
					   gint                *position,
					   gpointer             user_data);
static void     delete_text_callback      (GtkFileChooserEntry *widget,
					   gint                 start_pos,
					   gint                 end_pos,
					   gpointer             user_data);
#endif

129 130 131 132 133 134 135 136
static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
					   GtkTreeModel        *model,
					   GtkTreeIter         *iter,
					   GtkFileChooserEntry *chooser_entry);
static gboolean completion_match_func     (GtkEntryCompletion  *comp,
					   const char          *key,
					   GtkTreeIter         *iter,
					   gpointer             data);
137 138
static char    *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
						GFile               *file,
139
						gchar               *display_name);
140

141 142 143 144 145 146 147
typedef enum {
  REFRESH_UP_TO_CURSOR_POSITION,
  REFRESH_WHOLE_TEXT
} RefreshMode;

static void refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
						  RefreshMode refresh_mode);
148
static void finished_loading_cb (GFile         *file,
149 150
				 gpointer       data);
static void autocomplete (GtkFileChooserEntry *chooser_entry);
151
static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
152
static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
153 154
static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
					const gchar         *feedback);
155

Owen Taylor's avatar
Owen Taylor committed
156
static GtkEditableClass *parent_editable_iface;
157

Matthias Clasen's avatar
Matthias Clasen committed
158 159
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
Matthias Clasen's avatar
Matthias Clasen committed
160
						gtk_file_chooser_entry_iface_init))
161 162

static void
Matthias Clasen's avatar
Matthias Clasen committed
163
_gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
164 165 166
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
167
  GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
168 169

  gobject_class->finalize = gtk_file_chooser_entry_finalize;
170
  gobject_class->dispose = gtk_file_chooser_entry_dispose;
171

172
  widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
173
  widget_class->unmap = gtk_file_chooser_entry_unmap;
174
  widget_class->focus = gtk_file_chooser_entry_focus;
175
  widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
176 177

  entry_class->activate = gtk_file_chooser_entry_activate;
178 179 180 181 182 183 184 185
}

static void
gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
{
  parent_editable_iface = g_type_interface_peek_parent (iface);

  iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
186 187 188
  iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
  iface->set_position = gtk_file_chooser_entry_set_position;
  iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
189 190 191
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
192
_gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
193
{
194
  GtkEntryCompletion *comp;
195
  GtkCellRenderer *cell;
196

197 198
  g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);

199
  comp = gtk_entry_completion_new ();
200
  gtk_entry_completion_set_popup_single_match (comp, FALSE);
201

202 203 204 205
  gtk_entry_completion_set_match_func (comp,
				       completion_match_func,
				       chooser_entry,
				       NULL);
206 207 208 209 210 211 212 213

  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
                              cell, TRUE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
                                 cell,
                                 "text", 0);

214
  g_signal_connect (comp, "match_selected",
215
		    G_CALLBACK (match_selected_callback), chooser_entry);
216 217

  gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
218
  g_object_unref (comp);
219

220 221 222 223 224 225
#ifdef G_OS_WIN32
  g_signal_connect (chooser_entry, "insert_text",
		    G_CALLBACK (insert_text_callback), NULL);
  g_signal_connect (chooser_entry, "delete_text",
		    G_CALLBACK (delete_text_callback), NULL);
#endif
226 227 228 229 230 231 232
}

static void
gtk_file_chooser_entry_finalize (GObject *object)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);

233 234 235 236 237 238 239 240 241
  if (chooser_entry->base_folder)
    g_object_unref (chooser_entry->base_folder);

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

  if (chooser_entry->current_folder_file)
    g_object_unref (chooser_entry->current_folder_file);

242 243
  g_free (chooser_entry->file_part);

Matthias Clasen's avatar
Matthias Clasen committed
244
  G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
245 246 247 248 249 250 251
}

static void
gtk_file_chooser_entry_dispose (GObject *object)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);

252 253
  remove_completion_feedback (chooser_entry);

254 255 256 257 258 259
  if (chooser_entry->start_autocompletion_idle_id != 0)
    {
      g_source_remove (chooser_entry->start_autocompletion_idle_id);
      chooser_entry->start_autocompletion_idle_id = 0;
    }

260
  if (chooser_entry->completion_store)
261 262 263 264 265
    {
      g_object_unref (chooser_entry->completion_store);
      chooser_entry->completion_store = NULL;
    }

266
  if (chooser_entry->load_folder_cancellable)
267
    {
268 269
      g_cancellable_cancel (chooser_entry->load_folder_cancellable);
      chooser_entry->load_folder_cancellable = NULL;
270
    }
271

272
  if (chooser_entry->current_folder)
273 274
    {
      g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
275
					    G_CALLBACK (finished_loading_cb), chooser_entry);
276
      g_object_unref (chooser_entry->current_folder);
277
      chooser_entry->current_folder = NULL;
278 279
    }

280
  if (chooser_entry->file_system)
281 282 283 284
    {
      g_object_unref (chooser_entry->file_system);
      chooser_entry->file_system = NULL;
    }
285

Matthias Clasen's avatar
Matthias Clasen committed
286
  G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
287 288
}

289 290 291 292 293 294 295 296
/* Match functions for the GtkEntryCompletion */
static gboolean
match_selected_callback (GtkEntryCompletion  *completion,
			 GtkTreeModel        *model,
			 GtkTreeIter         *iter,
			 GtkFileChooserEntry *chooser_entry)
{
  char *display_name;
297
  GFile *file;
298
  gint pos;
299 300 301
  
  gtk_tree_model_get (model, iter,
		      DISPLAY_NAME_COLUMN, &display_name,
302
		      FILE_COLUMN, &file,
303 304
		      -1);

305
  if (!display_name || !file)
306
    {
307
      /* these shouldn't complain if passed NULL */
308
      g_object_unref (file);
309
      g_free (display_name);
310
      return FALSE;
311
    }
312

313
  display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
314

315
  pos = chooser_entry->file_part_pos;
316 317 318 319 320 321 322 323 324 325

  /* We don't set in_change here as we want to update the current_folder
   * variable */
  gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
			    pos, -1);
  gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
			    display_name, -1, 
			    &pos);
  gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);

326
  g_object_unref (file);
327 328
  g_free (display_name);

329 330 331
  return TRUE;
}

332 333 334
/* Match function for the GtkEntryCompletion */
static gboolean
completion_match_func (GtkEntryCompletion *comp,
335
		       const char         *key_unused,
336 337 338 339 340 341
		       GtkTreeIter        *iter,
		       gpointer            data)
{
  GtkFileChooserEntry *chooser_entry;
  char *name;
  gboolean result;
342 343
  char *norm_file_part;
  char *norm_name;
344 345 346

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

347 348 349 350
  /* We ignore the key because it is the contents of the entry.  Instead, we
   * just use our precomputed file_part.
   */
  if (!chooser_entry->file_part)
351 352 353 354
    {
      return FALSE;
    }

355
  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
356
  if (!name)
357 358 359
    {
      return FALSE; /* Uninitialized row, ugh */
    }
360

361
  /* If we have an empty file_part, then we're at the root of a directory.  In
362 363
   * that case, we want to match all non-dot files.  We might want to match
   * dot_files too if show_hidden is TRUE on the fileselector in the future.
364
   */
365
  /* Additionally, support for gnome .hidden files would be sweet, too */
366
  if (chooser_entry->file_part[0] == '\000')
367
    {
368 369 370 371 372
      if (name[0] == '.')
	result = FALSE;
      else
	result = TRUE;
      g_free (name);
373

374 375
      return result;
    }
376 377


378 379
  norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
  norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
380

381 382 383 384 385 386 387 388 389 390 391 392 393 394
#ifdef G_PLATFORM_WIN32
  {
    gchar *temp;

    temp = norm_file_part;
    norm_file_part = g_utf8_casefold (norm_file_part, -1);
    g_free (temp);

    temp = norm_name;
    norm_name = g_utf8_casefold (norm_name, -1);
    g_free (temp);
  }
#endif

395
  result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
396

397 398
  g_free (norm_file_part);
  g_free (norm_name);
399
  g_free (name);
400
  
401 402 403
  return result;
}

404 405 406 407 408
static void
clear_completions (GtkFileChooserEntry *chooser_entry)
{
  chooser_entry->has_completion = FALSE;
  chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
409 410

  remove_completion_feedback (chooser_entry);
411 412
}

413 414 415
static void
beep (GtkFileChooserEntry *chooser_entry)
{
416
  gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
417 418
}

419 420
/* This function will append a directory separator to paths to
 * display_name iff the path associated with it is a directory.
421
 * maybe_append_separator_to_file will g_free the display_name and
422 423
 * return a new one if needed.  Otherwise, it will return the old one.
 * You should be safe calling
424
 *
425
 * display_name = maybe_append_separator_to_file (entry, file, display_name);
426 427 428 429
 * ...
 * g_free (display_name);
 */
static char *
430 431
maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
				GFile               *file,
432 433
				gchar               *display_name)
{
434
  if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
435
    {
436 437
      GFileInfo *info;

438
      info = _gtk_folder_get_info (chooser_entry->current_folder, file);
439 440 441

      if (info)
	{
442
	  if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
443 444
	    {
	      gchar *tmp = display_name;
445
	      display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
446 447
	      g_free (tmp);
	    }
448 449

	  g_object_unref (info);
450 451 452 453 454 455
	}
    }

  return display_name;
}

456 457 458 459 460 461 462 463 464 465 466 467
static char *
trim_dir_separator_suffix (const char *str)
{
  int len;

  len = strlen (str);
  if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
    return g_strndup (str, len - 1);
  else
    return g_strdup (str);
}

468 469 470 471
/* Determines if the completion model has entries with a common prefix relative
 * to the current contents of the entry.  Also, if there's one and only one such
 * path, stores it in unique_path_ret.
 */
472
static gboolean
473 474
find_common_prefix (GtkFileChooserEntry *chooser_entry,
		    gchar               **common_prefix_ret,
475
		    GFile               **unique_file_ret,
476
		    gboolean             *is_complete_not_unique_ret,
477
		    gboolean             *prefix_expands_the_file_part_ret,
478
		    GError              **error)
479
{
480
  GtkEditable *editable;
481
  GtkTreeIter iter;
482
  gboolean parsed;
483
  gboolean valid;
484
  char *text_up_to_cursor;
485
  GFile *parsed_folder_file;
486
  char *parsed_file_part;
487

488
  *common_prefix_ret = NULL;
489
  *unique_file_ret = NULL;
490
  *is_complete_not_unique_ret = FALSE;
491
  *prefix_expands_the_file_part_ret = FALSE;
492

493 494 495 496
  editable = GTK_EDITABLE (chooser_entry);

  text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));

497 498 499 500 501 502
  parsed = _gtk_file_system_parse (chooser_entry->file_system,
				   chooser_entry->base_folder,
				   text_up_to_cursor,
				   &parsed_folder_file,
				   &parsed_file_part,
				   error);
503 504 505 506

  g_free (text_up_to_cursor);

  if (!parsed)
507
    return FALSE;
508

509 510 511
  g_assert (parsed_folder_file != NULL
	    && chooser_entry->current_folder != NULL
	    && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
512

513
  g_object_unref (parsed_folder_file);
514 515 516

  /* First pass: find the common prefix */

517
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
518 519 520 521

  while (valid)
    {
      gchar *display_name;
522
      GFile *file;
523 524 525 526

      gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
			  &iter,
			  DISPLAY_NAME_COLUMN, &display_name,
527
			  FILE_COLUMN, &file,
528 529
			  -1);

530
      if (g_str_has_prefix (display_name, parsed_file_part))
531
	{
532
	  if (!*common_prefix_ret)
533
	    {
534
	      *common_prefix_ret = trim_dir_separator_suffix (display_name);
535
	      *unique_file_ret = g_object_ref (file);
536 537 538
	    }
	  else
	    {
539
	      gchar *p = *common_prefix_ret;
540
	      const gchar *q = display_name;
541

542 543 544 545 546
	      while (*p && *p == *q)
		{
		  p++;
		  q++;
		}
547

548 549
	      *p = '\0';

550 551 552 553 554
	      if (*unique_file_ret)
		{
		  g_object_unref (*unique_file_ret);
		  *unique_file_ret = NULL;
		}
555 556 557 558
	    }
	}

      g_free (display_name);
559
      g_object_unref (file);
560
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
561
    }
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  /* Second pass: see if the prefix we found is a complete match */

  if (*common_prefix_ret != NULL)
    {
      valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);

      while (valid)
	{
	  gchar *display_name;
	  int len;

	  gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
			      &iter,
			      DISPLAY_NAME_COLUMN, &display_name,
			      -1);
	  len = strlen (display_name);
	  g_assert (len > 0);

	  if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
	    len--;

	  if (strncmp (*common_prefix_ret, display_name, len) == 0)
	    *is_complete_not_unique_ret = TRUE;

	  g_free (display_name);
	  valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
	}
590 591 592 593

      /* Finally:  Did we generate a new completion, or was the user's input already completed as far as it could go? */

      *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
594 595
    }

596
  g_free (parsed_file_part);
597 598

  return TRUE;
599 600
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
static gboolean
char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
{
  int cursor_pos;
  gboolean result;

  result = FALSE;

  cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
  if (cursor_pos < GTK_ENTRY (chooser_entry)->text_length)
    {
      char *next_char_str;

      next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
      if (G_IS_DIR_SEPARATOR (*next_char_str))
	result = TRUE;

      g_free (next_char_str);
    }

  return result;
}

624
typedef enum {
625 626 627
  INVALID_INPUT,		/* what the user typed is bogus */
  NO_MATCH,			/* no matches based on what the user typed */
  NOTHING_INSERTED_COMPLETE,	/* what the user typed is already completed as far as it will go */
628
  NOTHING_INSERTED_UNIQUE,	/* what the user typed is already completed, and is a unique match */
629 630 631
  COMPLETED,			/* completion inserted (ambiguous suffix) */
  COMPLETED_UNIQUE,		/* completion inserted, and it is a complete name and a unique match */
  COMPLETE_BUT_NOT_UNIQUE	/* completion inserted, it is a complete name but not unique */
632 633
} CommonPrefixResult;

634
/* Finds a common prefix based on the contents of the entry and mandatorily appends it */
635
static CommonPrefixResult
636
append_common_prefix (GtkFileChooserEntry *chooser_entry,
637 638
		      gboolean             highlight,
		      gboolean             show_errors)
639 640
{
  gchar *common_prefix;
641
  GFile *unique_file;
642
  gboolean is_complete_not_unique;
643
  gboolean prefix_expands_the_file_part;
644 645 646
  GError *error;
  CommonPrefixResult result;
  gboolean have_result;
647

648 649
  clear_completions (chooser_entry);

650 651 652
  if (chooser_entry->completion_store == NULL)
    return NO_MATCH;

653
  error = NULL;
654
  if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
655 656 657 658
    {
      if (show_errors)
	{
	  beep (chooser_entry);
659
	  pop_up_completion_feedback (chooser_entry, _("Invalid path"));
660 661 662 663 664 665 666 667
	}

      g_error_free (error);

      return INVALID_INPUT;
    }

  have_result = FALSE;
668

669
  if (unique_file)
670
    {
671
      if (!char_after_cursor_is_directory_separator (chooser_entry))
672 673
	common_prefix = maybe_append_separator_to_file (chooser_entry,
							unique_file,
674 675
							common_prefix);

676
      g_object_unref (unique_file);
677 678

      if (common_prefix)
679 680 681 682 683 684
	{
	  if (prefix_expands_the_file_part)
	    result = COMPLETED_UNIQUE;
	  else
	    result = NOTHING_INSERTED_UNIQUE;
	}
685 686 687 688
      else
	result = INVALID_INPUT;

      have_result = TRUE;
689
    }
690 691
  else
    {
692 693 694 695 696
      if (is_complete_not_unique)
	{
	  result = COMPLETE_BUT_NOT_UNIQUE;
	  have_result = TRUE;
	}
697
    }
698 699 700

  if (common_prefix)
    {
701
      gint cursor_pos;
702 703 704
      gint common_prefix_len;
      gint pos;

705
      cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
706 707
      common_prefix_len = g_utf8_strlen (common_prefix, -1);

708
      pos = chooser_entry->file_part_pos;
709

710 711 712 713 714 715 716 717 718
      if (prefix_expands_the_file_part)
	{
	  chooser_entry->in_change = TRUE;
	  gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
				    pos, cursor_pos);
	  gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
				    common_prefix, -1, 
				    &pos);
	  chooser_entry->in_change = FALSE;
719

720 721 722 723
	  if (highlight)
	    {
	      gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
					  cursor_pos,
724
					  pos); /* equivalent to cursor_pos + common_prefix_len); */
725 726 727 728
	      chooser_entry->has_completion = TRUE;
	    }
	  else
	    gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
729
	}
730
      else if (!have_result)
731 732 733 734
	{
	  result = NOTHING_INSERTED_COMPLETE;
	  have_result = TRUE;
	}
735

736
      g_free (common_prefix);
737 738 739 740

      if (have_result)
	return result;
      else
741
	return COMPLETED;
742 743 744 745 746 747
    }
  else
    {
      if (have_result)
	return result;
      else
748
	return NO_MATCH;
749
    }
750 751
}

752 753 754 755 756
static void
gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
				       const gchar *new_text,
				       gint         new_text_length,
				       gint        *position)
757
{
758 759 760
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
  gint old_text_len;
  gint insert_pos;
761

762 763
  old_text_len = GTK_ENTRY (chooser_entry)->text_length;
  insert_pos = *position;
764

765
  parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
766

767 768
  if (chooser_entry->in_change)
    return;
769

770 771
  remove_completion_feedback (chooser_entry);

772 773
  if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
       || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
774 775
      && insert_pos == old_text_len)
    install_start_autocompletion_idle (chooser_entry);
776 777
}

778 779 780 781 782 783 784 785 786
static void
clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
{
  if (chooser_entry->in_change)
    return;

  clear_completions (chooser_entry);
}

787 788 789 790
static void
gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
				       gint         start_pos,
				       gint         end_pos)
791
{
792 793 794
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

  parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
795

796
  clear_completions_if_not_in_change (chooser_entry);
797 798
}

799
static void
800 801
gtk_file_chooser_entry_set_position (GtkEditable *editable,
				     gint         position)
802
{
803 804 805 806
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

  parent_editable_iface->set_position (editable, position);

807
  clear_completions_if_not_in_change (chooser_entry);
808 809
}

810
static void
811 812 813
gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
					     gint         start_pos,
					     gint         end_pos)
814
{
815 816
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);

817 818
  parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);

819
  clear_completions_if_not_in_change (chooser_entry);
820 821
}

822 823 824 825 826 827 828
static void
gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
  _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
}

829 830 831 832 833 834 835 836 837 838
static void
gtk_file_chooser_entry_unmap (GtkWidget *widget)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);

  remove_completion_feedback (chooser_entry);

  GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static gboolean
completion_feedback_window_expose_event_cb (GtkWidget      *widget,
					    GdkEventExpose *event,
					    gpointer        data)
{
  /* Stolen from gtk_tooltip_paint_window() */

  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

  gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
		      chooser_entry->completion_feedback_window->window,
		      GTK_STATE_NORMAL,
		      GTK_SHADOW_OUT,
		      NULL,
		      chooser_entry->completion_feedback_window,
		      "tooltip",
		      0, 0,
		      chooser_entry->completion_feedback_window->allocation.width,
		      chooser_entry->completion_feedback_window->allocation.height);

  return FALSE;
}

862 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
static void
set_invisible_mouse_cursor (GdkWindow *window)
{
  /* Stolen from gtkentry.c:set_invisible_cursor() */
  /* FIXME: implement a stupid public gdk_window_set_invisible_mouse_cursor() */

  GdkBitmap *empty_bitmap;
  GdkCursor *cursor;
  GdkColor useless;
  char invisible_cursor_bits[] = { 0x0 };

  useless.red = useless.green = useless.blue = 0;
  useless.pixel = 0;

  empty_bitmap = gdk_bitmap_create_from_data (window,
					      invisible_cursor_bits,
					      1, 1);

  cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
				       empty_bitmap,
				       &useless,
				       &useless, 0, 0);

  gdk_window_set_cursor (window, cursor);

  gdk_cursor_unref (cursor);

  g_object_unref (empty_bitmap);
}

static void
completion_feedback_window_realize_cb (GtkWidget *widget,
				       gpointer data)
{
  /* We hide the mouse cursor inside the completion feedback window, since
   * GtkEntry hides the cursor when the user types.  We don't want the cursor to
   * come back if the completion feedback ends up where the mouse is.
   */
  set_invisible_mouse_cursor (widget->window);
}

903 904 905 906 907
static void
create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* Stolen from gtk_tooltip_init() */

908 909
  GtkWidget *alignment;

910 911 912 913 914 915 916
  chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
  gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
			    GDK_WINDOW_TYPE_HINT_TOOLTIP);
  gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
  gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
  gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");

917 918 919 920 921 922 923 924 925
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
			     chooser_entry->completion_feedback_window->style->ythickness,
			     chooser_entry->completion_feedback_window->style->ythickness,
			     chooser_entry->completion_feedback_window->style->xthickness,
			     chooser_entry->completion_feedback_window->style->xthickness);
  gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
  gtk_widget_show (alignment);

926 927
  g_signal_connect (chooser_entry->completion_feedback_window, "expose_event",
		    G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
928 929 930
  g_signal_connect (chooser_entry->completion_feedback_window, "realize",
		    G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
  /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
931 932

  chooser_entry->completion_feedback_label = gtk_label_new (NULL);
933 934
  gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
  gtk_widget_show (chooser_entry->completion_feedback_label);
935 936
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950
static gboolean
completion_feedback_timeout_cb (gpointer data)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

  chooser_entry->completion_feedback_timeout_id = 0;

  remove_completion_feedback (chooser_entry);
  return FALSE;
}

static void
install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
{
951 952
  if (chooser_entry->completion_feedback_timeout_id != 0)
    g_source_remove (chooser_entry->completion_feedback_timeout_id);
953 954 955 956 957 958

  chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
									   completion_feedback_timeout_cb,
									   chooser_entry);
}

959
/* Gets the x position of the text cursor in the entry, in widget coordinates */
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
static void
get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
		    gint                *x_ret)
{
  /* FIXME: see the docs for gtk_entry_get_layout_offsets().  As an exercise for
   * the reader, you have to implement support for the entry's scroll offset and
   * RTL layouts and all the fancy Pango stuff.
   */

  PangoLayout *layout;
  gint layout_x, layout_y;
  gint layout_index;
  PangoRectangle strong_pos;

  layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));

  gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);

  layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
						       GTK_ENTRY (chooser_entry)->current_pos);

  pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);

  *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
}

986 987 988 989 990 991 992
static void
show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
{
  /* More or less stolen from gtk_tooltip_position() */

  GtkRequisition feedback_req;
  gint entry_x, entry_y;
993
  gint cursor_x;
994
  GtkAllocation *entry_allocation;
995
  int feedback_x, feedback_y;
996 997 998 999 1000 1001

  gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);

  gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
  entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);

1002
  get_entry_cursor_x (chooser_entry, &cursor_x);
1003

1004 1005
  /* FIXME: fit to the screen if we bump on the screen's edge */
  feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1006 1007
  feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;

1008
  gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1009
  gtk_widget_show (chooser_entry->completion_feedback_window);
1010

1011
  install_completion_feedback_timer (chooser_entry);
1012 1013
}

1014 1015 1016 1017
static void
pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
			    const gchar         *feedback)
{
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  if (chooser_entry->completion_feedback_window == NULL)
    create_completion_feedback_window (chooser_entry);

  gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);

  show_completion_feedback_window (chooser_entry);
}

static void
remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
{
1029 1030 1031
  if (chooser_entry->completion_feedback_window)
    gtk_widget_destroy (chooser_entry->completion_feedback_window);

1032 1033
  chooser_entry->completion_feedback_window = NULL;
  chooser_entry->completion_feedback_label = NULL;
1034

1035 1036 1037 1038 1039
  if (chooser_entry->completion_feedback_timeout_id != 0)
    {
      g_source_remove (chooser_entry->completion_feedback_timeout_id);
      chooser_entry->completion_feedback_timeout_id = 0;
    }
1040 1041
}

1042 1043 1044
static void
explicitly_complete (GtkFileChooserEntry *chooser_entry)
{
1045 1046
  CommonPrefixResult result;

1047
  g_assert (chooser_entry->current_folder != NULL);
1048
  g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1049 1050 1051

  /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
   *
1052
   * - If there is a common prefix, insert it (done)
1053
   * - If there is no common prefix, pop up the suggestion window
1054
   * - If there are no matches at all, beep and bring up a tooltip (done)
1055 1056
   * - If the suggestion window is already up, scroll it
   */
1057 1058 1059 1060
  result = append_common_prefix (chooser_entry, FALSE, TRUE);

  switch (result)
    {
1061 1062
    case INVALID_INPUT:
      /* We already beeped in append_common_prefix(); do nothing here */
1063 1064
      break;

1065
    case NO_MATCH:
1066
      beep (chooser_entry);
1067
      pop_up_completion_feedback (chooser_entry, _("No match"));
1068 1069
      break;

1070 1071 1072 1073
    case NOTHING_INSERTED_COMPLETE:
      /* FIXME: pop up the suggestion window or scroll it */
      break;

1074 1075 1076 1077
    case NOTHING_INSERTED_UNIQUE:
      pop_up_completion_feedback (chooser_entry, _("Sole completion"));
      break;

1078 1079 1080 1081 1082
    case COMPLETED:
      /* Nothing to do */
      break;

    case COMPLETED_UNIQUE:
1083
      /* Nothing to do */
1084 1085
      break;

1086 1087
    case COMPLETE_BUT_NOT_UNIQUE:
      pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1088 1089 1090 1091 1092
      break;

    default:
      g_assert_not_reached ();
    }
1093 1094 1095 1096 1097
}

static void
start_explicit_completion (GtkFileChooserEntry *chooser_entry)
{
1098
  refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1099

1100
  if (!chooser_entry->current_folder_file)
1101 1102 1103
    {
      /* Here, no folder path means we couldn't parse what the user typed. */

1104
      beep (chooser_entry);
1105
      pop_up_completion_feedback (chooser_entry, _("Invalid path"));
1106 1107 1108 1109 1110 1111

      chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
      return;
    }

  if (chooser_entry->current_folder
1112
      && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1113 1114 1115 1116 1117 1118
    {
      explicitly_complete (chooser_entry);
    }
  else
    {
      chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1119

1120
      pop_up_completion_feedback (chooser_entry, _("Completing..."));
1121 1122 1123
    }
}

1124 1125 1126 1127
static gboolean
gtk_file_chooser_entry_focus (GtkWidget        *widget,
			      GtkDirectionType  direction)
{
1128 1129 1130
  GtkFileChooserEntry *chooser_entry;
  GtkEditable *editable;
  GtkEntry *entry;
1131
  GdkModifierType state;
1132 1133 1134 1135 1136
  gboolean control_pressed;

  chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
  editable = GTK_EDITABLE (widget);
  entry = GTK_ENTRY (widget);
1137

1138
  if (!chooser_entry->eat_tabs)
Matthias Clasen's avatar
Matthias Clasen committed
1139
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1140

1141 1142
  control_pressed = FALSE;

1143
  if (gtk_get_current_event_state (&state))
1144
    {
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
      if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
	control_pressed = TRUE;
    }

  /* This is a bit evil -- it makes Tab never leave the entry. It basically
   * makes it 'safe' for people to hit. */
  if ((direction == GTK_DIR_TAB_FORWARD) &&
      (GTK_WIDGET_HAS_FOCUS (widget)) &&
      (! control_pressed))
    {
1155
      if (chooser_entry->has_completion)
1156
	gtk_editable_set_position (editable, entry->text_length);
1157
      else
1158
	start_explicit_completion (chooser_entry);
1159

1160 1161 1162
      return TRUE;
    }
  else
Matthias Clasen's avatar
Matthias Clasen committed
1163
    return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1164 1165
}

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
static gboolean
gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
					GdkEventFocus *event)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);

  chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
 
  return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
}

1177
static void
1178
commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1179 1180 1181
{
  if (chooser_entry->has_completion)
    {
1182 1183
      gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
				 GTK_ENTRY (chooser_entry)->text_length);
1184
    }
1185 1186

  refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1187 1188 1189 1190 1191 1192
}

static void
gtk_file_chooser_entry_activate (GtkEntry *entry)
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1193

1194
  commit_completion_and_refresh (chooser_entry);
Matthias Clasen's avatar
Matthias Clasen committed
1195
  GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1196 1197
}

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
static void
discard_completion_store (GtkFileChooserEntry *chooser_entry)
{
  if (!chooser_entry->completion_store)
    return;

  gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
  g_object_unref (chooser_entry->completion_store);
  chooser_entry->completion_store = NULL;
}

1209 1210 1211 1212
/* Fills the completion store from the contents of the current folder */
static void
populate_completion_store (GtkFileChooserEntry *chooser_entry)
{
1213
  GSList *files;
1214 1215
  GSList *tmp_list;

1216
  discard_completion_store (chooser_entry);
1217

1218
  files = _gtk_folder_list_children (chooser_entry->current_folder);
1219

1220 1221
  chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
							G_TYPE_STRING,
1222
							G_TYPE_FILE);
1223

1224
  for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1225
    {
1226 1227 1228 1229
      GFileInfo *info;
      GFile *file;

      file = tmp_list->data;
1230

1231
      info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1232 1233 1234

      if (info)
	{
1235
	  gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1236 1237
	  GtkTreeIter iter;

1238
          display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
1239 1240 1241 1242

	  gtk_list_store_append (chooser_entry->completion_store, &iter);
	  gtk_list_store_set (chooser_entry->completion_store, &iter,
			      DISPLAY_NAME_COLUMN, display_name,
1243
			      FILE_COLUMN, file,
1244 1245
			      -1);

1246
	  g_object_unref (info);
1247 1248 1249 1250
          g_free (display_name);
	}
    }

1251 1252
  g_slist_foreach (files, (GFunc) g_object_unref, NULL);
  g_slist_free (files);
1253 1254 1255

  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
					DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1256 1257 1258

  gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
				  GTK_TREE_MODEL (chooser_entry->completion_store));
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
}

/* When we finish loading the current folder, this function should get called to
 * perform the deferred autocompletion or explicit completion.
 */
static void
perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
{
  switch (chooser_entry->load_complete_action)
    {
    case LOAD_COMPLETE_NOTHING:
      break;

    case LOAD_COMPLETE_AUTOCOMPLETE:
      autocomplete (chooser_entry);
      break;

1276 1277
    case LOAD_COMPLETE_EXPLICIT_COMPLETION:
      explicitly_complete (chooser_entry);
1278 1279 1280 1281 1282 1283 1284 1285 1286
      break;

    default:
      g_assert_not_reached ();
    }

  chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
}

1287 1288 1289 1290 1291
static void
finish_folder_load (GtkFileChooserEntry *chooser_entry)
{
  populate_completion_store (chooser_entry);
  perform_load_complete_action (chooser_entry);
1292 1293

  gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1294 1295
}

1296 1297
/* Callback when the current folder finishes loading */
static void
1298 1299
finished_loading_cb (GFile    *file,
		     gpointer  data)
1300 1301 1302
{
  GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);

1303
  finish_folder_load (chooser_entry);
1304 1305 1306
}

/* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1307
static void
1308 1309 1310 1311
load_directory_get_folder_callback (GCancellable  *cancellable,
				    GtkFolder     *folder,
				    const GError  *error,
				    gpointer       data)
1312
{
1313
  gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1314 1315
  GtkFileChooserEntry *chooser_entry = data;

1316
  if (cancellable != chooser_entry->load_folder_cancellable)
1317 1318
    goto out;

1319
  chooser_entry->load_folder_cancellable = NULL;
1320

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
  if (error)
    {
      LoadCompleteAction old_load_complete_action;

      old_load_complete_action = chooser_entry->load_complete_action;

      clear_completions (chooser_entry);

      if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
	{
	  /* Since this came from explicit user action (Tab completion), we'll present errors visually */

	  beep (chooser_entry);
1334
	  pop_up_completion_feedback (chooser_entry, error->message);
1335
	}
1336

1337 1338 1339 1340 1341
      if (chooser_entry->current_folder)
	{
	  g_object_unref (chooser_entry->current_folder);
	  chooser_entry->current_folder = NULL;
	}
1342
    }
1343

1344 1345 1346
  if (cancelled || error)
    goto out;

1347
  g_assert (folder != NULL);
1348
  chooser_entry->current_folder = g_object_ref (folder);
1349

1350 1351
  discard_completion_store (chooser_entry);

1352
  if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))