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