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