]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
wayland: Remove non-existing gdkscreen-wayland.h from SOURCES
[~andy/gtk] / gtk / gtkfilechooserentry.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserentry.c: Entry with filename completion
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "gtkfilechooserentry.h"
24
25 #include <string.h>
26
27 #include "gtkalignment.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkentry.h"
31 #include "gtklabel.h"
32 #include "gtkmain.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
35 #include "gtkintl.h"
36
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42
43 struct _GtkFileChooserEntryClass
44 {
45   GtkEntryClass parent_class;
46 };
47
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 typedef enum {
50   LOAD_COMPLETE_NOTHING,
51   LOAD_COMPLETE_AUTOCOMPLETE,
52   LOAD_COMPLETE_EXPLICIT_COMPLETION
53 } LoadCompleteAction;
54
55 typedef enum
56 {
57   REFRESH_OK,
58   REFRESH_INVALID_INPUT,
59   REFRESH_INCOMPLETE_HOSTNAME,
60   REFRESH_NONEXISTENT,
61   REFRESH_NOT_LOCAL
62 } RefreshStatus;
63
64 struct _GtkFileChooserEntry
65 {
66   GtkEntry parent_instance;
67
68   GtkFileChooserAction action;
69
70   GtkFileSystem *file_system;
71   GFile *base_folder;
72   GFile *current_folder_file;
73   gchar *file_part;
74   gint file_part_pos;
75
76   /* Folder being loaded or already loaded */
77   GtkFolder *current_folder;
78   GCancellable *load_folder_cancellable;
79
80   LoadCompleteAction load_complete_action;
81
82   GtkListStore *completion_store;
83
84   guint start_autocompletion_idle_id;
85
86   GtkWidget *completion_feedback_window;
87   GtkWidget *completion_feedback_label;
88   guint completion_feedback_timeout_id;
89
90   guint has_completion : 1;
91   guint in_change      : 1;
92   guint eat_tabs       : 1;
93   guint local_only     : 1;
94 };
95
96 enum
97 {
98   DISPLAY_NAME_COLUMN,
99   FILE_COLUMN,
100   N_COLUMNS
101 };
102
103 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
104
105 static void     gtk_file_chooser_entry_iface_init     (GtkEditableInterface *iface);
106
107 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
108 static void     gtk_file_chooser_entry_dispose        (GObject          *object);
109 static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
110 static void     gtk_file_chooser_entry_unmap          (GtkWidget        *widget);
111 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
112                                                         GdkEventKey *event);
113 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
114                                                         GdkEventFocus   *event);
115 static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
116 static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
117                                                        const gchar *new_text,
118                                                        gint         new_text_length,
119                                                        gint        *position);
120 static void     gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
121                                                        gint         start_pos,
122                                                        gint         end_pos);
123 static void     gtk_file_chooser_entry_set_position (GtkEditable *editable,
124                                                      gint         position);
125 static void     gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
126                                                              gint         start_pos,
127                                                              gint         end_pos);
128
129 #ifdef G_OS_WIN32
130 static gint     insert_text_callback      (GtkFileChooserEntry *widget,
131                                            const gchar         *new_text,
132                                            gint                 new_text_length,
133                                            gint                *position,
134                                            gpointer             user_data);
135 static void     delete_text_callback      (GtkFileChooserEntry *widget,
136                                            gint                 start_pos,
137                                            gint                 end_pos,
138                                            gpointer             user_data);
139 #endif
140
141 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
142                                            GtkTreeModel        *model,
143                                            GtkTreeIter         *iter,
144                                            GtkFileChooserEntry *chooser_entry);
145 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
146                                            const char          *key,
147                                            GtkTreeIter         *iter,
148                                            gpointer             data);
149 static char    *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
150                                                 GFile               *file,
151                                                 gchar               *display_name,
152                                                 gboolean            *appended);
153
154 typedef enum {
155   REFRESH_UP_TO_CURSOR_POSITION,
156   REFRESH_WHOLE_TEXT
157 } RefreshMode;
158
159 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
160                                                   RefreshMode refresh_mode);
161 static void finished_loading_cb (GtkFolder *folder,
162                                  gpointer   data);
163 static void autocomplete (GtkFileChooserEntry *chooser_entry);
164 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
165 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
166 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
167                                         const gchar         *feedback);
168
169 static GtkEditableInterface *parent_editable_iface;
170
171 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
172                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
173                                                 gtk_file_chooser_entry_iface_init))
174
175 static void
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
177 {
178   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
180   GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
181
182   gobject_class->finalize = gtk_file_chooser_entry_finalize;
183   gobject_class->dispose = gtk_file_chooser_entry_dispose;
184
185   widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186   widget_class->unmap = gtk_file_chooser_entry_unmap;
187   widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
188   widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
189
190   entry_class->activate = gtk_file_chooser_entry_activate;
191 }
192
193 static void
194 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
195 {
196   parent_editable_iface = g_type_interface_peek_parent (iface);
197
198   iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
199   iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
200   iface->set_position = gtk_file_chooser_entry_set_position;
201   iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
202 }
203
204 static void
205 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
206 {
207   GtkEntryCompletion *comp;
208   GtkCellRenderer *cell;
209
210   chooser_entry->local_only = TRUE;
211
212   g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
213
214   comp = gtk_entry_completion_new ();
215   gtk_entry_completion_set_popup_single_match (comp, FALSE);
216
217   gtk_entry_completion_set_match_func (comp,
218                                        completion_match_func,
219                                        chooser_entry,
220                                        NULL);
221
222   cell = gtk_cell_renderer_text_new ();
223   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
224                               cell, TRUE);
225   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
226                                  cell,
227                                  "text", 0);
228
229   g_signal_connect (comp, "match-selected",
230                     G_CALLBACK (match_selected_callback), chooser_entry);
231
232   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
233   g_object_unref (comp);
234
235 #ifdef G_OS_WIN32
236   g_signal_connect (chooser_entry, "insert-text",
237                     G_CALLBACK (insert_text_callback), NULL);
238   g_signal_connect (chooser_entry, "delete-text",
239                     G_CALLBACK (delete_text_callback), NULL);
240 #endif
241 }
242
243 static void
244 gtk_file_chooser_entry_finalize (GObject *object)
245 {
246   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
247
248   if (chooser_entry->base_folder)
249     g_object_unref (chooser_entry->base_folder);
250
251   if (chooser_entry->current_folder)
252     g_object_unref (chooser_entry->current_folder);
253
254   if (chooser_entry->current_folder_file)
255     g_object_unref (chooser_entry->current_folder_file);
256
257   g_free (chooser_entry->file_part);
258
259   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
260 }
261
262 static void
263 discard_current_folder (GtkFileChooserEntry *chooser_entry)
264 {
265   if (chooser_entry->current_folder)
266     {
267       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
268                                             G_CALLBACK (finished_loading_cb), chooser_entry);
269       g_object_unref (chooser_entry->current_folder);
270       chooser_entry->current_folder = NULL;
271     }
272 }
273
274 static void
275 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
276 {
277   if (chooser_entry->load_folder_cancellable)
278     {
279       g_cancellable_cancel (chooser_entry->load_folder_cancellable);
280       chooser_entry->load_folder_cancellable = NULL;
281     }
282
283   if (chooser_entry->current_folder_file)
284     {
285       g_object_unref (chooser_entry->current_folder_file);
286       chooser_entry->current_folder_file = NULL;
287     }
288 }
289
290 static void
291 gtk_file_chooser_entry_dispose (GObject *object)
292 {
293   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
294
295   remove_completion_feedback (chooser_entry);
296   discard_current_folder (chooser_entry);
297   discard_loading_and_current_folder_file (chooser_entry);
298
299   if (chooser_entry->start_autocompletion_idle_id != 0)
300     {
301       g_source_remove (chooser_entry->start_autocompletion_idle_id);
302       chooser_entry->start_autocompletion_idle_id = 0;
303     }
304
305   if (chooser_entry->completion_store)
306     {
307       g_object_unref (chooser_entry->completion_store);
308       chooser_entry->completion_store = NULL;
309     }
310
311   if (chooser_entry->file_system)
312     {
313       g_object_unref (chooser_entry->file_system);
314       chooser_entry->file_system = NULL;
315     }
316
317   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
318 }
319
320 /* Match functions for the GtkEntryCompletion */
321 static gboolean
322 match_selected_callback (GtkEntryCompletion  *completion,
323                          GtkTreeModel        *model,
324                          GtkTreeIter         *iter,
325                          GtkFileChooserEntry *chooser_entry)
326 {
327   char *display_name;
328   GFile *file;
329   gint pos;
330   gboolean dummy;
331   
332   gtk_tree_model_get (model, iter,
333                       DISPLAY_NAME_COLUMN, &display_name,
334                       FILE_COLUMN, &file,
335                       -1);
336
337   if (!display_name || !file)
338     {
339       if (file)
340         g_object_unref (file);
341
342       g_free (display_name);
343       return FALSE;
344     }
345
346   display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
347
348   pos = chooser_entry->file_part_pos;
349
350   /* We don't set in_change here as we want to update the current_folder
351    * variable */
352   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
353                             pos, -1);
354   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
355                             display_name, -1, 
356                             &pos);
357   gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
358
359   g_object_unref (file);
360   g_free (display_name);
361
362   return TRUE;
363 }
364
365 /* Match function for the GtkEntryCompletion */
366 static gboolean
367 completion_match_func (GtkEntryCompletion *comp,
368                        const char         *key_unused,
369                        GtkTreeIter        *iter,
370                        gpointer            data)
371 {
372   GtkFileChooserEntry *chooser_entry;
373   char *name;
374   gboolean result;
375   char *norm_file_part;
376   char *norm_name;
377
378   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
379
380   /* We ignore the key because it is the contents of the entry.  Instead, we
381    * just use our precomputed file_part.
382    */
383   if (!chooser_entry->file_part)
384     {
385       return FALSE;
386     }
387
388   gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
389   if (!name)
390     {
391       return FALSE; /* Uninitialized row, ugh */
392     }
393
394   /* If we have an empty file_part, then we're at the root of a directory.  In
395    * that case, we want to match all non-dot files.  We might want to match
396    * dot_files too if show_hidden is TRUE on the fileselector in the future.
397    */
398   /* Additionally, support for gnome .hidden files would be sweet, too */
399   if (chooser_entry->file_part[0] == '\000')
400     {
401       if (name[0] == '.')
402         result = FALSE;
403       else
404         result = TRUE;
405       g_free (name);
406
407       return result;
408     }
409
410
411   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
412   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
413
414 #ifdef G_PLATFORM_WIN32
415   {
416     gchar *temp;
417
418     temp = norm_file_part;
419     norm_file_part = g_utf8_casefold (norm_file_part, -1);
420     g_free (temp);
421
422     temp = norm_name;
423     norm_name = g_utf8_casefold (norm_name, -1);
424     g_free (temp);
425   }
426 #endif
427
428   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
429
430   g_free (norm_file_part);
431   g_free (norm_name);
432   g_free (name);
433   
434   return result;
435 }
436
437 static void
438 clear_completions (GtkFileChooserEntry *chooser_entry)
439 {
440   chooser_entry->has_completion = FALSE;
441   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
442
443   remove_completion_feedback (chooser_entry);
444 }
445
446 static void
447 beep (GtkFileChooserEntry *chooser_entry)
448 {
449   gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
450 }
451
452 /* This function will append a directory separator to paths to
453  * display_name iff the path associated with it is a directory.
454  * maybe_append_separator_to_file will g_free the display_name and
455  * return a new one if needed.  Otherwise, it will return the old one.
456  * You should be safe calling
457  *
458  * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
459  * ...
460  * g_free (display_name);
461  */
462 static char *
463 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
464                                 GFile               *file,
465                                 gchar               *display_name,
466                                 gboolean            *appended)
467 {
468   *appended = FALSE;
469
470   if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
471     {
472       GFileInfo *info;
473
474       info = _gtk_folder_get_info (chooser_entry->current_folder, file);
475
476       if (info)
477         {
478           if (_gtk_file_info_consider_as_directory (info))
479             {
480               gchar *tmp = display_name;
481               display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
482               *appended = TRUE;
483               g_free (tmp);
484             }
485
486           g_object_unref (info);
487         }
488     }
489
490   return display_name;
491 }
492
493 static char *
494 trim_dir_separator_suffix (const char *str)
495 {
496   int len;
497
498   len = strlen (str);
499   if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
500     return g_strndup (str, len - 1);
501   else
502     return g_strdup (str);
503 }
504
505 /* Determines if the completion model has entries with a common prefix relative
506  * to the current contents of the entry.  Also, if there's one and only one such
507  * path, stores it in unique_path_ret.
508  */
509 static gboolean
510 find_common_prefix (GtkFileChooserEntry *chooser_entry,
511                     gchar               **common_prefix_ret,
512                     GFile               **unique_file_ret,
513                     gboolean             *is_complete_not_unique_ret,
514                     gboolean             *prefix_expands_the_file_part_ret,
515                     GError              **error)
516 {
517   GtkEditable *editable;
518   GtkTreeIter iter;
519   gboolean parsed;
520   gboolean valid;
521   char *text_up_to_cursor;
522   GFile *parsed_folder_file;
523   char *parsed_file_part;
524
525   *common_prefix_ret = NULL;
526   *unique_file_ret = NULL;
527   *is_complete_not_unique_ret = FALSE;
528   *prefix_expands_the_file_part_ret = FALSE;
529
530   editable = GTK_EDITABLE (chooser_entry);
531
532   text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
533
534   parsed = _gtk_file_system_parse (chooser_entry->file_system,
535                                    chooser_entry->base_folder,
536                                    text_up_to_cursor,
537                                    &parsed_folder_file,
538                                    &parsed_file_part,
539                                    error);
540
541   g_free (text_up_to_cursor);
542
543   if (!parsed)
544     return FALSE;
545
546   g_assert (parsed_folder_file != NULL
547             && chooser_entry->current_folder != NULL
548             && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
549
550   g_object_unref (parsed_folder_file);
551
552   /* First pass: find the common prefix */
553
554   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
555
556   while (valid)
557     {
558       gchar *display_name;
559       GFile *file;
560
561       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
562                           &iter,
563                           DISPLAY_NAME_COLUMN, &display_name,
564                           FILE_COLUMN, &file,
565                           -1);
566
567       if (g_str_has_prefix (display_name, parsed_file_part))
568         {
569           if (!*common_prefix_ret)
570             {
571               *common_prefix_ret = trim_dir_separator_suffix (display_name);
572               *unique_file_ret = g_object_ref (file);
573             }
574           else
575             {
576               gchar *p = *common_prefix_ret;
577               const gchar *q = display_name;
578
579               while (*p && *p == *q)
580                 {
581                   p++;
582                   q++;
583                 }
584
585               *p = '\0';
586
587               if (*unique_file_ret)
588                 {
589                   g_object_unref (*unique_file_ret);
590                   *unique_file_ret = NULL;
591                 }
592             }
593         }
594
595       g_free (display_name);
596       g_object_unref (file);
597       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
598     }
599
600   /* Second pass: see if the prefix we found is a complete match */
601
602   if (*common_prefix_ret != NULL)
603     {
604       valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
605
606       while (valid)
607         {
608           gchar *display_name;
609           int len;
610
611           gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
612                               &iter,
613                               DISPLAY_NAME_COLUMN, &display_name,
614                               -1);
615           len = strlen (display_name);
616           g_assert (len > 0);
617
618           if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
619             len--;
620
621           if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
622             *is_complete_not_unique_ret = TRUE;
623
624           g_free (display_name);
625           valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
626         }
627
628       /* Finally:  Did we generate a new completion, or was the user's input already completed as far as it could go? */
629
630       *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
631     }
632
633   g_free (parsed_file_part);
634
635   return TRUE;
636 }
637
638 static gboolean
639 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
640 {
641   int cursor_pos;
642   gboolean result;
643
644   result = FALSE;
645
646   cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
647   if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
648     {
649       char *next_char_str;
650
651       next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
652       if (G_IS_DIR_SEPARATOR (*next_char_str))
653         result = TRUE;
654
655       g_free (next_char_str);
656     }
657
658   return result;
659 }
660
661 typedef enum {
662   INVALID_INPUT,                /* what the user typed is bogus */
663   NO_MATCH,                     /* no matches based on what the user typed */
664   NOTHING_INSERTED_COMPLETE,    /* what the user typed is already completed as far as it will go */
665   NOTHING_INSERTED_UNIQUE,      /* what the user typed is already completed, and is a unique match */
666   COMPLETED,                    /* completion inserted (ambiguous suffix) */
667   COMPLETED_UNIQUE,             /* completion inserted, and it is a complete name and a unique match */
668   COMPLETE_BUT_NOT_UNIQUE       /* completion inserted, it is a complete name but not unique */
669 } CommonPrefixResult;
670
671 /* Finds a common prefix based on the contents of the entry
672  * and mandatorily appends it
673  */
674 static CommonPrefixResult
675 append_common_prefix (GtkFileChooserEntry *chooser_entry,
676                       gboolean             highlight,
677                       gboolean             show_errors)
678 {
679   gchar *common_prefix;
680   GFile *unique_file;
681   gboolean is_complete_not_unique;
682   gboolean prefix_expands_the_file_part;
683   GError *error;
684   CommonPrefixResult result = NO_MATCH;
685   gboolean have_result;
686
687   clear_completions (chooser_entry);
688
689   if (chooser_entry->completion_store == NULL)
690     return NO_MATCH;
691
692   error = NULL;
693   if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
694     {
695       /* If the user types an incomplete hostname ("http://foo" without
696        * a slash after that), it's not an error.  We just don't want to
697        * pop up a meaningless completion window in that state.
698        */
699       if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
700           && show_errors)
701         {
702           beep (chooser_entry);
703           pop_up_completion_feedback (chooser_entry, _("Invalid path"));
704         }
705
706       g_error_free (error);
707
708       return INVALID_INPUT;
709     }
710
711   have_result = FALSE;
712
713   if (unique_file)
714     {
715       if (!char_after_cursor_is_directory_separator (chooser_entry))
716         {
717           gboolean appended;
718
719           common_prefix = maybe_append_separator_to_file (chooser_entry,
720                                                           unique_file,
721                                                           common_prefix,
722                                                           &appended);
723           if (appended)
724             prefix_expands_the_file_part = TRUE;
725         }
726
727       g_object_unref (unique_file);
728
729       if (prefix_expands_the_file_part)
730         result = COMPLETED_UNIQUE;
731       else
732         result = NOTHING_INSERTED_UNIQUE;
733
734       have_result = TRUE;
735     }
736   else
737     {
738       if (is_complete_not_unique)
739         {
740           result = COMPLETE_BUT_NOT_UNIQUE;
741           have_result = TRUE;
742         }
743     }
744
745   if (common_prefix)
746     {
747       gint cursor_pos;
748       gint pos;
749
750       cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
751
752       pos = chooser_entry->file_part_pos;
753
754       if (prefix_expands_the_file_part)
755         {
756           chooser_entry->in_change = TRUE;
757           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
758                                     pos, cursor_pos);
759           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
760                                     common_prefix, -1,
761                                     &pos);
762           chooser_entry->in_change = FALSE;
763
764           if (highlight)
765             {
766               /* equivalent to cursor_pos + common_prefix_len); */
767               gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
768                                           cursor_pos,
769                                           pos);
770               chooser_entry->has_completion = TRUE;
771             }
772           else
773             gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
774         }
775       else if (!have_result)
776         {
777           result = NOTHING_INSERTED_COMPLETE;
778           have_result = TRUE;
779         }
780
781       g_free (common_prefix);
782
783       if (have_result)
784         return result;
785       else
786         return COMPLETED;
787     }
788   else
789     {
790       if (have_result)
791         return result;
792       else
793         return NO_MATCH;
794     }
795 }
796
797 static void
798 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
799                                        const gchar *new_text,
800                                        gint         new_text_length,
801                                        gint        *position)
802 {
803   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
804   gint old_text_len;
805   gint insert_pos;
806
807   old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
808   insert_pos = *position;
809
810   parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
811
812   if (chooser_entry->in_change)
813     return;
814
815   remove_completion_feedback (chooser_entry);
816
817   if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
818        || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
819       && insert_pos == old_text_len)
820     install_start_autocompletion_idle (chooser_entry);
821 }
822
823 static void
824 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
825 {
826   if (chooser_entry->in_change)
827     return;
828
829   clear_completions (chooser_entry);
830 }
831
832 static void
833 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
834                                        gint         start_pos,
835                                        gint         end_pos)
836 {
837   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
838
839   parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
840
841   clear_completions_if_not_in_change (chooser_entry);
842 }
843
844 static void
845 gtk_file_chooser_entry_set_position (GtkEditable *editable,
846                                      gint         position)
847 {
848   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
849
850   parent_editable_iface->set_position (editable, position);
851
852   clear_completions_if_not_in_change (chooser_entry);
853 }
854
855 static void
856 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
857                                              gint         start_pos,
858                                              gint         end_pos)
859 {
860   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
861
862   parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
863
864   clear_completions_if_not_in_change (chooser_entry);
865 }
866
867 static void
868 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
869 {
870   GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
871   _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
872 }
873
874 static void
875 gtk_file_chooser_entry_unmap (GtkWidget *widget)
876 {
877   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
878
879   remove_completion_feedback (chooser_entry);
880
881   GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
882 }
883
884 static gboolean
885 completion_feedback_window_draw_cb (GtkWidget *widget,
886                                     cairo_t   *cr,
887                                     gpointer   data)
888 {
889   /* Stolen from gtk_tooltip_paint_window() */
890
891   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
892   GtkStyleContext *context;
893
894   context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
895
896   gtk_render_background (context, cr, 0, 0,
897                          gtk_widget_get_allocated_width (widget),
898                          gtk_widget_get_allocated_height (widget));
899   gtk_render_frame (context, cr, 0, 0,
900                     gtk_widget_get_allocated_width (widget),
901                     gtk_widget_get_allocated_height (widget));
902
903   return FALSE;
904 }
905
906 static void
907 set_invisible_mouse_cursor (GdkWindow *window)
908 {
909   GdkDisplay *display;
910   GdkCursor *cursor;
911
912   display = gdk_window_get_display (window);
913   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
914
915   gdk_window_set_cursor (window, cursor);
916
917   g_object_unref (cursor);
918 }
919
920 static void
921 completion_feedback_window_realize_cb (GtkWidget *widget,
922                                        gpointer data)
923 {
924   /* We hide the mouse cursor inside the completion feedback window, since
925    * GtkEntry hides the cursor when the user types.  We don't want the cursor to
926    * come back if the completion feedback ends up where the mouse is.
927    */
928   set_invisible_mouse_cursor (gtk_widget_get_window (widget));
929 }
930
931 static void
932 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
933 {
934   /* Stolen from gtk_tooltip_init() */
935
936   GtkStyleContext *context;
937   GtkWidget *alignment;
938   GtkBorder padding, border;
939
940   chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
941   gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
942                             GDK_WINDOW_TYPE_HINT_TOOLTIP);
943   gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
944   gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
945   gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
946
947   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
948   context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
949   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
950
951   gtk_style_context_get_padding (context, 0, &padding);
952   gtk_style_context_get_border (context, 0, &border);
953
954   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
955                              border.top + padding.top,
956                              border.bottom + padding.bottom,
957                              border.left + padding.left,
958                              border.right + padding.right);
959   gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
960   gtk_widget_show (alignment);
961
962   g_signal_connect (chooser_entry->completion_feedback_window, "draw",
963                     G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
964   g_signal_connect (chooser_entry->completion_feedback_window, "realize",
965                     G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
966   /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
967
968   chooser_entry->completion_feedback_label = gtk_label_new (NULL);
969   gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
970   gtk_widget_show (chooser_entry->completion_feedback_label);
971 }
972
973 static gboolean
974 completion_feedback_timeout_cb (gpointer data)
975 {
976   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
977
978   chooser_entry->completion_feedback_timeout_id = 0;
979
980   remove_completion_feedback (chooser_entry);
981   return FALSE;
982 }
983
984 static void
985 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
986 {
987   if (chooser_entry->completion_feedback_timeout_id != 0)
988     g_source_remove (chooser_entry->completion_feedback_timeout_id);
989
990   chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
991                                                                            completion_feedback_timeout_cb,
992                                                                            chooser_entry);
993 }
994
995 /* Gets the x position of the text cursor in the entry, in widget coordinates */
996 static void
997 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
998                     gint                *x_ret)
999 {
1000   /* FIXME: see the docs for gtk_entry_get_layout_offsets().  As an exercise for
1001    * the reader, you have to implement support for the entry's scroll offset and
1002    * RTL layouts and all the fancy Pango stuff.
1003    */
1004
1005   PangoLayout *layout;
1006   gint layout_x, layout_y;
1007   gint layout_index;
1008   PangoRectangle strong_pos;
1009   gint start_pos, end_pos;
1010
1011   layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1012
1013   gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1014
1015   gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1016   layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1017                                                        end_pos);
1018
1019
1020   pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1021
1022   *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1023 }
1024
1025 static void
1026 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1027 {
1028   /* More or less stolen from gtk_tooltip_position() */
1029
1030   GtkRequisition feedback_req;
1031   GtkWidget *widget = GTK_WIDGET (chooser_entry);
1032   gint entry_x, entry_y;
1033   gint cursor_x;
1034   GtkAllocation entry_allocation;
1035   int feedback_x, feedback_y;
1036
1037   gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1038                                  &feedback_req, NULL);
1039
1040   gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1041   gtk_widget_get_allocation (widget, &entry_allocation);
1042
1043   get_entry_cursor_x (chooser_entry, &cursor_x);
1044
1045   /* FIXME: fit to the screen if we bump on the screen's edge */
1046   /* cheap "half M-width", use height as approximation of character em-size */
1047   feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1048   feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1049
1050   gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1051   gtk_widget_show (chooser_entry->completion_feedback_window);
1052
1053   install_completion_feedback_timer (chooser_entry);
1054 }
1055
1056 static void
1057 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1058                             const gchar         *feedback)
1059 {
1060   if (chooser_entry->completion_feedback_window == NULL)
1061     create_completion_feedback_window (chooser_entry);
1062
1063   gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1064
1065   show_completion_feedback_window (chooser_entry);
1066 }
1067
1068 static void
1069 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1070 {
1071   if (chooser_entry->completion_feedback_window)
1072     gtk_widget_destroy (chooser_entry->completion_feedback_window);
1073
1074   chooser_entry->completion_feedback_window = NULL;
1075   chooser_entry->completion_feedback_label = NULL;
1076
1077   if (chooser_entry->completion_feedback_timeout_id != 0)
1078     {
1079       g_source_remove (chooser_entry->completion_feedback_timeout_id);
1080       chooser_entry->completion_feedback_timeout_id = 0;
1081     }
1082 }
1083
1084 static void
1085 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1086 {
1087   CommonPrefixResult result;
1088
1089   g_assert (chooser_entry->current_folder != NULL);
1090   g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1091
1092   /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1093    *
1094    * - If there is a common prefix, insert it (done)
1095    * - If there is no common prefix, pop up the suggestion window
1096    * - If there are no matches at all, beep and bring up a tooltip (done)
1097    * - If the suggestion window is already up, scroll it
1098    */
1099   result = append_common_prefix (chooser_entry, FALSE, TRUE);
1100
1101   switch (result)
1102     {
1103     case INVALID_INPUT:
1104       /* We already beeped in append_common_prefix(); do nothing here */
1105       break;
1106
1107     case NO_MATCH:
1108       beep (chooser_entry);
1109       /* translators: this text is shown when there are no completions 
1110        * for something the user typed in a file chooser entry
1111        */
1112       pop_up_completion_feedback (chooser_entry, _("No match"));
1113       break;
1114
1115     case NOTHING_INSERTED_COMPLETE:
1116       /* FIXME: pop up the suggestion window or scroll it */
1117       break;
1118
1119     case NOTHING_INSERTED_UNIQUE:
1120       /* translators: this text is shown when there is exactly one completion 
1121        * for something the user typed in a file chooser entry
1122        */
1123       pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1124       break;
1125
1126     case COMPLETED:
1127       /* Nothing to do */
1128       break;
1129
1130     case COMPLETED_UNIQUE:
1131       /* Nothing to do */
1132       break;
1133
1134     case COMPLETE_BUT_NOT_UNIQUE:
1135       /* translators: this text is shown when the text in a file chooser
1136        * entry is a complete filename, but could be continued to find
1137        * a longer match
1138        */
1139       pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1140       break;
1141
1142     default:
1143       g_assert_not_reached ();
1144     }
1145 }
1146
1147 static void
1148 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1149 {
1150   RefreshStatus status;
1151   gboolean is_error;
1152   char *feedback_msg;
1153
1154   status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1155
1156   is_error = FALSE;
1157
1158   switch (status)
1159     {
1160     case REFRESH_OK:
1161       g_assert (chooser_entry->current_folder_file != NULL);
1162
1163       if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1164         explicitly_complete (chooser_entry);
1165       else
1166         {
1167           chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1168
1169           /* Translators: this text is shown while the system is searching
1170            * for possible completions for filenames in a file chooser entry. */
1171           pop_up_completion_feedback (chooser_entry, _("Completing..."));
1172         }
1173
1174       break;
1175
1176     case REFRESH_INVALID_INPUT:
1177       is_error = TRUE;
1178       /* Translators: this is shown in the feedback for Tab-completion in a file
1179        * chooser's text entry, when the user enters an invalid path. */
1180       feedback_msg = _("Invalid path");
1181       break;
1182
1183     case REFRESH_INCOMPLETE_HOSTNAME:
1184       is_error = TRUE;
1185
1186       if (chooser_entry->local_only)
1187         {
1188           /* hostnames in a local_only file chooser?  user error */
1189
1190           /* Translators: this is shown in the feedback for Tab-completion in a
1191            * file chooser's text entry when the user enters something like
1192            * "sftp://blahblah" in an app that only supports local filenames. */
1193           feedback_msg = _("Only local files may be selected");
1194         }
1195       else
1196         {
1197           /* Another option is to complete the hostname based on the remote volumes that are mounted */
1198
1199           /* Translators: this is shown in the feedback for Tab-completion in a
1200            * file chooser's text entry when the user hasn't entered the first '/'
1201            * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1202           feedback_msg = _("Incomplete hostname; end it with '/'");
1203         }
1204
1205       break;
1206
1207     case REFRESH_NONEXISTENT:
1208       is_error = TRUE;
1209
1210       /* Translators: this is shown in the feedback for Tab-completion in a file
1211        * chooser's text entry when the user enters a path that does not exist
1212        * and then hits Tab */
1213       feedback_msg = _("Path does not exist");
1214       break;
1215
1216     case REFRESH_NOT_LOCAL:
1217       is_error = TRUE;
1218       feedback_msg = _("Only local files may be selected");
1219       break;
1220
1221     default:
1222       g_assert_not_reached ();
1223       return;
1224     }
1225
1226   if (is_error)
1227     {
1228       g_assert (chooser_entry->current_folder_file == NULL);
1229
1230       beep (chooser_entry);
1231       pop_up_completion_feedback (chooser_entry, feedback_msg);
1232       chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1233     }
1234 }
1235
1236 static gboolean
1237 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1238                                         GdkEventKey *event)
1239 {
1240   GtkFileChooserEntry *chooser_entry;
1241   GtkEditable *editable;
1242   GtkEntry *entry;
1243   GdkModifierType state;
1244   gboolean control_pressed;
1245
1246   chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1247   editable = GTK_EDITABLE (widget);
1248   entry = GTK_ENTRY (widget);
1249
1250   if (!chooser_entry->eat_tabs)
1251     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1252
1253   control_pressed = FALSE;
1254
1255   if (gtk_get_current_event_state (&state))
1256     {
1257       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1258         control_pressed = TRUE;
1259     }
1260
1261   /* This is a bit evil -- it makes Tab never leave the entry. It basically
1262    * makes it 'safe' for people to hit. */
1263   if (event->keyval == GDK_KEY_Tab && !control_pressed)
1264     {
1265       if (chooser_entry->has_completion)
1266         gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1267       else
1268         start_explicit_completion (chooser_entry);
1269
1270       return TRUE;
1271      }
1272
1273   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1274
1275 }
1276
1277 static gboolean
1278 gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
1279                                         GdkEventFocus *event)
1280 {
1281   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1282
1283   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1284  
1285   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1286 }
1287
1288 static void
1289 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1290 {
1291   if (chooser_entry->has_completion)
1292     {
1293       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1294                                  gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1295     }
1296
1297   /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1298   refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1299 }
1300
1301 static void
1302 gtk_file_chooser_entry_activate (GtkEntry *entry)
1303 {
1304   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1305
1306   commit_completion_and_refresh (chooser_entry);
1307   GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1308 }
1309
1310 static void
1311 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1312 {
1313   if (!chooser_entry->completion_store)
1314     return;
1315
1316   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1317   g_object_unref (chooser_entry->completion_store);
1318   chooser_entry->completion_store = NULL;
1319 }
1320
1321 /* Fills the completion store from the contents of the current folder */
1322 static void
1323 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1324 {
1325   GSList *files;
1326   GSList *tmp_list;
1327
1328   discard_completion_store (chooser_entry);
1329
1330   files = _gtk_folder_list_children (chooser_entry->current_folder);
1331
1332   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1333                                                         G_TYPE_STRING,
1334                                                         G_TYPE_FILE);
1335
1336   for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1337     {
1338       GFileInfo *info;
1339       GFile *file;
1340
1341       file = tmp_list->data;
1342
1343       info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1344
1345       if (info)
1346         {
1347           gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1348           GtkTreeIter iter;
1349           gboolean dummy;
1350
1351           display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1352
1353           gtk_list_store_append (chooser_entry->completion_store, &iter);
1354           gtk_list_store_set (chooser_entry->completion_store, &iter,
1355                               DISPLAY_NAME_COLUMN, display_name,
1356                               FILE_COLUMN, file,
1357                               -1);
1358
1359           g_object_unref (info);
1360           g_free (display_name);
1361         }
1362     }
1363
1364   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1365   g_slist_free (files);
1366
1367   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1368                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1369
1370   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1371                                   GTK_TREE_MODEL (chooser_entry->completion_store));
1372 }
1373
1374 /* When we finish loading the current folder, this function should get called to
1375  * perform the deferred autocompletion or explicit completion.
1376  */
1377 static void
1378 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1379 {
1380   switch (chooser_entry->load_complete_action)
1381     {
1382     case LOAD_COMPLETE_NOTHING:
1383       break;
1384
1385     case LOAD_COMPLETE_AUTOCOMPLETE:
1386       autocomplete (chooser_entry);
1387       break;
1388
1389     case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1390       explicitly_complete (chooser_entry);
1391       break;
1392
1393     default:
1394       g_assert_not_reached ();
1395     }
1396
1397   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1398 }
1399
1400 static void
1401 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1402 {
1403   populate_completion_store (chooser_entry);
1404   perform_load_complete_action (chooser_entry);
1405
1406   gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1407 }
1408
1409 /* Callback when the current folder finishes loading */
1410 static void
1411 finished_loading_cb (GtkFolder *folder,
1412                      gpointer   data)
1413 {
1414   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1415
1416   finish_folder_load (chooser_entry);
1417 }
1418
1419 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1420 static void
1421 load_directory_get_folder_callback (GCancellable  *cancellable,
1422                                     GtkFolder     *folder,
1423                                     const GError  *error,
1424                                     gpointer       data)
1425 {
1426   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1427   GtkFileChooserEntry *chooser_entry = data;
1428
1429   if (cancellable != chooser_entry->load_folder_cancellable)
1430     goto out;
1431
1432   chooser_entry->load_folder_cancellable = NULL;
1433
1434   if (error)
1435     {
1436       LoadCompleteAction old_load_complete_action;
1437
1438       old_load_complete_action = chooser_entry->load_complete_action;
1439
1440       discard_completion_store (chooser_entry);
1441       clear_completions (chooser_entry);
1442
1443       if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1444         {
1445           /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1446
1447           beep (chooser_entry);
1448           pop_up_completion_feedback (chooser_entry, error->message);
1449         }
1450
1451       discard_current_folder (chooser_entry);
1452     }
1453
1454   if (cancelled || error)
1455     goto out;
1456
1457   g_assert (folder != NULL);
1458   chooser_entry->current_folder = g_object_ref (folder);
1459
1460   discard_completion_store (chooser_entry);
1461
1462   if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1463     finish_folder_load (chooser_entry);
1464   else
1465     g_signal_connect (chooser_entry->current_folder, "finished-loading",
1466                       G_CALLBACK (finished_loading_cb), chooser_entry);
1467
1468 out:
1469   g_object_unref (chooser_entry);
1470   g_object_unref (cancellable);
1471 }
1472
1473 static RefreshStatus
1474 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1475 {
1476   if (chooser_entry->file_system == NULL)
1477     return REFRESH_OK;
1478
1479   g_assert (chooser_entry->current_folder_file != NULL);
1480   g_assert (chooser_entry->current_folder == NULL);
1481   g_assert (chooser_entry->load_folder_cancellable == NULL);
1482
1483   if (chooser_entry->local_only
1484       && !g_file_is_native (chooser_entry->current_folder_file))
1485     {
1486       g_object_unref (chooser_entry->current_folder_file);
1487       chooser_entry->current_folder_file = NULL;
1488
1489       return REFRESH_NOT_LOCAL;
1490     }
1491
1492   chooser_entry->load_folder_cancellable =
1493     _gtk_file_system_get_folder (chooser_entry->file_system,
1494                                  chooser_entry->current_folder_file,
1495                                 "standard::name,standard::display-name,standard::type",
1496                                  load_directory_get_folder_callback,
1497                                  g_object_ref (chooser_entry));
1498
1499   return REFRESH_OK;
1500 }
1501
1502 static RefreshStatus
1503 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1504                        GFile               *folder_file,
1505                        gboolean             force_reload)
1506 {
1507   gboolean reload = FALSE;
1508
1509   g_assert (folder_file != NULL);
1510
1511   if (chooser_entry->current_folder_file)
1512     {
1513       if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1514              && chooser_entry->load_folder_cancellable))
1515           || force_reload)
1516         {
1517           reload = TRUE;
1518
1519           discard_current_folder (chooser_entry);
1520           discard_loading_and_current_folder_file (chooser_entry);
1521
1522           chooser_entry->current_folder_file = g_object_ref (folder_file);
1523         }
1524     }
1525   else
1526     {
1527       chooser_entry->current_folder_file = g_object_ref (folder_file);
1528       reload = TRUE;
1529     }
1530
1531   if (reload)
1532     return start_loading_current_folder (chooser_entry);
1533   else
1534     return REFRESH_OK;
1535 }
1536
1537 static RefreshStatus
1538 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1539                                       RefreshMode          refresh_mode)
1540 {
1541   GtkEditable *editable;
1542   gint end_pos;
1543   gchar *text;
1544   GFile *folder_file;
1545   gchar *file_part;
1546   gsize total_len, file_part_len;
1547   gint file_part_pos;
1548   GError *error;
1549   RefreshStatus result;
1550
1551   editable = GTK_EDITABLE (chooser_entry);
1552
1553   switch (refresh_mode)
1554     {
1555     case REFRESH_UP_TO_CURSOR_POSITION:
1556       end_pos = gtk_editable_get_position (editable);
1557       break;
1558
1559     case REFRESH_WHOLE_TEXT:
1560       end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1561       break;
1562
1563     default:
1564       g_assert_not_reached ();
1565       return REFRESH_INVALID_INPUT;
1566     }
1567
1568   text = gtk_editable_get_chars (editable, 0, end_pos);
1569
1570   error = NULL;
1571   if (!chooser_entry->file_system ||
1572       !chooser_entry->base_folder ||
1573       !_gtk_file_system_parse (chooser_entry->file_system,
1574                                chooser_entry->base_folder, text,
1575                                &folder_file, &file_part, &error))
1576     {
1577       if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1578         {
1579           folder_file = NULL;
1580           result = REFRESH_INCOMPLETE_HOSTNAME;
1581         }
1582       else
1583         {
1584           folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1585
1586           if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1587             result = REFRESH_NONEXISTENT;
1588           else
1589             result = REFRESH_INVALID_INPUT;
1590         }
1591
1592       if (error)
1593         g_error_free (error);
1594
1595       file_part = g_strdup ("");
1596       file_part_pos = -1;
1597     }
1598   else
1599     {
1600       g_assert (folder_file != NULL);
1601
1602       file_part_len = strlen (file_part);
1603       total_len = strlen (text);
1604       if (total_len > file_part_len)
1605         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1606       else
1607         file_part_pos = 0;
1608
1609       result = REFRESH_OK;
1610     }
1611
1612   g_free (text);
1613
1614   g_free (chooser_entry->file_part);
1615
1616   chooser_entry->file_part = file_part;
1617   chooser_entry->file_part_pos = file_part_pos;
1618
1619   if (result == REFRESH_OK)
1620     {
1621       result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1622     }
1623   else
1624     {
1625       discard_current_folder (chooser_entry);
1626       discard_loading_and_current_folder_file (chooser_entry);
1627     }
1628
1629   if (folder_file)
1630     g_object_unref (folder_file);
1631
1632   g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1633             ((result == REFRESH_OK)
1634              && (chooser_entry->current_folder_file != NULL)
1635              && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1636                  || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1637             /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1638             || ((result != REFRESH_OK)
1639                 && (chooser_entry->current_folder_file == NULL)
1640                 && (chooser_entry->load_folder_cancellable == NULL)
1641                 && (chooser_entry->current_folder == NULL)));
1642
1643   return result;
1644 }
1645
1646 static void
1647 autocomplete (GtkFileChooserEntry *chooser_entry)
1648 {
1649   if (!(chooser_entry->current_folder != NULL
1650         && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1651         && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1652     return;
1653
1654   append_common_prefix (chooser_entry, TRUE, FALSE);
1655 }
1656
1657 static void
1658 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1659 {
1660   RefreshStatus status;
1661
1662   status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1663
1664   switch (status)
1665     {
1666     case REFRESH_OK:
1667       g_assert (chooser_entry->current_folder_file != NULL);
1668
1669       if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1670         autocomplete (chooser_entry);
1671       else
1672         chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1673
1674       break;
1675
1676     case REFRESH_INVALID_INPUT:
1677     case REFRESH_INCOMPLETE_HOSTNAME:
1678     case REFRESH_NONEXISTENT:
1679     case REFRESH_NOT_LOCAL:
1680       /* We don't beep or anything, since this is autocompletion - the user
1681        * didn't request any action explicitly.
1682        */
1683       break;
1684
1685     default:
1686       g_assert_not_reached ();
1687     }
1688 }
1689
1690 static gboolean
1691 start_autocompletion_idle_handler (gpointer data)
1692 {
1693   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1694
1695   start_autocompletion (chooser_entry);
1696
1697   chooser_entry->start_autocompletion_idle_id = 0;
1698
1699   return FALSE;
1700 }
1701
1702 static void
1703 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1704 {
1705   if (chooser_entry->start_autocompletion_idle_id != 0)
1706     return;
1707
1708   chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1709 }
1710
1711 #ifdef G_OS_WIN32
1712 static gint
1713 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1714                       const gchar         *new_text,
1715                       gint                 new_text_length,
1716                       gint                *position,
1717                       gpointer             user_data)
1718 {
1719   const gchar *colon = memchr (new_text, ':', new_text_length);
1720   gint i;
1721
1722   /* Disallow these characters altogether */
1723   for (i = 0; i < new_text_length; i++)
1724     {
1725       if (new_text[i] == '<' ||
1726           new_text[i] == '>' ||
1727           new_text[i] == '"' ||
1728           new_text[i] == '|' ||
1729           new_text[i] == '*' ||
1730           new_text[i] == '?')
1731         break;
1732     }
1733
1734   if (i < new_text_length ||
1735       /* Disallow entering text that would cause a colon to be anywhere except
1736        * after a drive letter.
1737        */
1738       (colon != NULL &&
1739        *position + (colon - new_text) != 1) ||
1740       (new_text_length > 0 &&
1741        *position <= 1 &&
1742        gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1743        gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1744     {
1745       gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1746       g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1747       return FALSE;
1748     }
1749
1750   return TRUE;
1751 }
1752
1753 static void
1754 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1755                       gint                 start_pos,
1756                       gint                 end_pos,
1757                       gpointer             user_data)
1758 {
1759   /* If deleting a drive letter, delete the colon, too */
1760   if (start_pos == 0 && end_pos == 1 &&
1761       gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1762       gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1763     {
1764       g_signal_handlers_block_by_func (chooser_entry,
1765                                        G_CALLBACK (delete_text_callback),
1766                                        user_data);
1767       gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1768       g_signal_handlers_unblock_by_func (chooser_entry,
1769                                          G_CALLBACK (delete_text_callback),
1770                                          user_data);
1771     }
1772 }
1773 #endif
1774
1775 /**
1776  * _gtk_file_chooser_entry_new:
1777  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1778  *
1779  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1780  * is an internal implementation widget for the GTK+ file chooser
1781  * which is an entry with completion with respect to a
1782  * #GtkFileSystem object.
1783  *
1784  * Return value: the newly created #GtkFileChooserEntry
1785  **/
1786 GtkWidget *
1787 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1788 {
1789   GtkFileChooserEntry *chooser_entry;
1790
1791   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1792   chooser_entry->eat_tabs = (eat_tabs != FALSE);
1793
1794   return GTK_WIDGET (chooser_entry);
1795 }
1796
1797 /**
1798  * _gtk_file_chooser_entry_set_file_system:
1799  * @chooser_entry: a #GtkFileChooser
1800  * @file_system: an object implementing #GtkFileSystem
1801  *
1802  * Sets the file system for @chooser_entry.
1803  **/
1804 void
1805 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1806                                          GtkFileSystem       *file_system)
1807 {
1808   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1809   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1810
1811   if (file_system != chooser_entry->file_system)
1812     {
1813       if (chooser_entry->file_system)
1814         g_object_unref (chooser_entry->file_system);
1815
1816       chooser_entry->file_system = g_object_ref (file_system);
1817     }
1818 }
1819
1820 /**
1821  * _gtk_file_chooser_entry_set_base_folder:
1822  * @chooser_entry: a #GtkFileChooserEntry
1823  * @file: file for a folder in the chooser entries current file system.
1824  *
1825  * Sets the folder with respect to which completions occur.
1826  **/
1827 void
1828 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1829                                          GFile               *file)
1830 {
1831   if (chooser_entry->base_folder)
1832     g_object_unref (chooser_entry->base_folder);
1833
1834   chooser_entry->base_folder = file;
1835
1836   if (chooser_entry->base_folder)
1837     g_object_ref (chooser_entry->base_folder);
1838
1839   clear_completions (chooser_entry);
1840   _gtk_file_chooser_entry_select_filename (chooser_entry);
1841 }
1842
1843 /**
1844  * _gtk_file_chooser_entry_get_current_folder:
1845  * @chooser_entry: a #GtkFileChooserEntry
1846  *
1847  * Gets the current folder for the #GtkFileChooserEntry. If the
1848  * user has only entered a filename, this will be in the base folder
1849  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1850  * user has entered a relative or absolute path, then it will
1851  * be different.  If the user has entered unparsable text, or text which
1852  * the entry cannot handle, this will return %NULL.
1853  *
1854  * Return value: the file for the current folder - this value is owned by the
1855  *  chooser entry and must not be modified or freed.
1856  **/
1857 GFile *
1858 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1859 {
1860   commit_completion_and_refresh (chooser_entry);
1861   return chooser_entry->current_folder_file;
1862 }
1863
1864 /**
1865  * _gtk_file_chooser_entry_get_file_part:
1866  * @chooser_entry: a #GtkFileChooserEntry
1867  *
1868  * Gets the non-folder portion of whatever the user has entered
1869  * into the file selector. What is returned is a UTF-8 string,
1870  * and if a filename path is needed, g_file_get_child_for_display_name()
1871  * must be used
1872   *
1873  * Return value: the entered filename - this value is owned by the
1874  *  chooser entry and must not be modified or freed.
1875  **/
1876 const gchar *
1877 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1878 {
1879   commit_completion_and_refresh (chooser_entry);
1880   return chooser_entry->file_part;
1881 }
1882
1883 /**
1884  * _gtk_file_chooser_entry_set_file_part:
1885  * @chooser_entry: a #GtkFileChooserEntry
1886  * @file_part: text to display in the entry, in UTF-8
1887  *
1888  * Sets the current text shown in the file chooser entry.
1889  **/
1890 void
1891 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1892                                        const gchar         *file_part)
1893 {
1894   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1895
1896   chooser_entry->in_change = TRUE;
1897   clear_completions (chooser_entry);
1898   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1899   chooser_entry->in_change = FALSE;
1900 }
1901
1902
1903 /**
1904  * _gtk_file_chooser_entry_set_action:
1905  * @chooser_entry: a #GtkFileChooserEntry
1906  * @action: the action which is performed by the file selector using this entry
1907  *
1908  * Sets action which is performed by the file selector using this entry. 
1909  * The #GtkFileChooserEntry will use different completion strategies for 
1910  * different actions.
1911  **/
1912 void
1913 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1914                                     GtkFileChooserAction action)
1915 {
1916   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1917   
1918   if (chooser_entry->action != action)
1919     {
1920       GtkEntryCompletion *comp;
1921
1922       chooser_entry->action = action;
1923
1924       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1925
1926       /* FIXME: do we need to actually set the following? */
1927
1928       switch (action)
1929         {
1930         case GTK_FILE_CHOOSER_ACTION_OPEN:
1931         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1932           gtk_entry_completion_set_popup_single_match (comp, FALSE);
1933           break;
1934         case GTK_FILE_CHOOSER_ACTION_SAVE:
1935         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1936           gtk_entry_completion_set_popup_single_match (comp, TRUE);
1937           break;
1938         }
1939     }
1940 }
1941
1942
1943 /**
1944  * _gtk_file_chooser_entry_get_action:
1945  * @chooser_entry: a #GtkFileChooserEntry
1946  *
1947  * Gets the action for this entry. 
1948  *
1949  * Returns: the action
1950  **/
1951 GtkFileChooserAction
1952 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1953 {
1954   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1955                         GTK_FILE_CHOOSER_ACTION_OPEN);
1956   
1957   return chooser_entry->action;
1958 }
1959
1960 gboolean
1961 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1962                                        GFile               *file)
1963 {
1964   gboolean retval = FALSE;
1965
1966   if (chooser_entry->current_folder)
1967     {
1968       GFileInfo *file_info;
1969
1970       file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1971
1972       if (file_info)
1973         {
1974           retval = _gtk_file_info_consider_as_directory (file_info);
1975           g_object_unref (file_info);
1976         }
1977     }
1978
1979   return retval;
1980 }
1981
1982
1983 /*
1984  * _gtk_file_chooser_entry_select_filename:
1985  * @chooser_entry: a #GtkFileChooserEntry
1986  *
1987  * Selects the filename (without the extension) for user edition.
1988  */
1989 void
1990 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1991 {
1992   const gchar *str, *ext;
1993   glong len = -1;
1994
1995   if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1996     {
1997       str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1998       ext = g_strrstr (str, ".");
1999
2000       if (ext)
2001        len = g_utf8_pointer_to_offset (str, ext);
2002     }
2003
2004   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
2005 }
2006
2007 void
2008 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
2009                                         gboolean             local_only)
2010 {
2011   chooser_entry->local_only = local_only;
2012   clear_completions (chooser_entry);
2013 }
2014
2015 gboolean
2016 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2017 {
2018   return chooser_entry->local_only;
2019 }