]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
filechooserentry: Get rid of unused member variable
[~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 "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkentry.h"
30 #include "gtkfilesystemmodel.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_EXPLICIT_COMPLETION
52 } LoadCompleteAction;
53
54 typedef enum
55 {
56   REFRESH_OK,
57   REFRESH_INVALID_INPUT,
58   REFRESH_INCOMPLETE_HOSTNAME,
59   REFRESH_NONEXISTENT,
60   REFRESH_NOT_LOCAL
61 } RefreshStatus;
62
63 struct _GtkFileChooserEntry
64 {
65   GtkEntry parent_instance;
66
67   GtkFileChooserAction action;
68
69   GFile *base_folder;
70   GFile *current_folder_file;
71   gchar *dir_part;
72   gchar *file_part;
73   gint file_part_pos;
74
75   LoadCompleteAction load_complete_action;
76
77   GtkTreeModel *completion_store;
78
79   guint current_folder_loaded : 1;
80   guint eat_tabs       : 1;
81   guint local_only     : 1;
82 };
83
84 enum
85 {
86   DISPLAY_NAME_COLUMN,
87   FULL_PATH_COLUMN,
88   FILE_COLUMN,
89   N_COLUMNS
90 };
91
92 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
93 static void     gtk_file_chooser_entry_dispose        (GObject          *object);
94 static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
95 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
96                                                         GdkEventKey *event);
97 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
98                                                         GdkEventFocus   *event);
99
100 #ifdef G_OS_WIN32
101 static gint     insert_text_callback      (GtkFileChooserEntry *widget,
102                                            const gchar         *new_text,
103                                            gint                 new_text_length,
104                                            gint                *position,
105                                            gpointer             user_data);
106 static void     delete_text_callback      (GtkFileChooserEntry *widget,
107                                            gint                 start_pos,
108                                            gint                 end_pos,
109                                            gpointer             user_data);
110 #endif
111
112 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
113                                            GtkTreeModel        *model,
114                                            GtkTreeIter         *iter,
115                                            GtkFileChooserEntry *chooser_entry);
116 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
117                                            const char          *key,
118                                            GtkTreeIter         *iter,
119                                            gpointer             data);
120
121 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
122                                                            const char          *text);
123 static void finished_loading_cb (GtkFileSystemModel  *model,
124                                  GError              *error,
125                                  GtkFileChooserEntry *chooser_entry);
126
127 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
128
129 static char *
130 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
131 {
132   GtkEditable *editable = GTK_EDITABLE (chooser_entry);
133   int start, end;
134
135   gtk_editable_get_selection_bounds (editable, &start, &end);
136   return gtk_editable_get_chars (editable, 0, MIN (start, end));
137 }
138
139 static void
140 gtk_file_chooser_entry_dispatch_properties_changed (GObject     *object,
141                                                     guint        n_pspecs,
142                                                     GParamSpec **pspecs)
143 {
144   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
145   guint i;
146
147   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
148
149   /* What we are after: The text in front of the cursor was modified.
150    * Unfortunately, there's no other way to catch this. */
151
152   for (i = 0; i < n_pspecs; i++)
153     {
154       if (pspecs[i]->name == I_("cursor-position") ||
155           pspecs[i]->name == I_("selection-bound") ||
156           pspecs[i]->name == I_("text"))
157         {
158           char *text;
159
160           chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
161
162           text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
163           refresh_current_folder_and_file_part (chooser_entry, text);
164           g_free (text);
165
166           break;
167         }
168     }
169 }
170
171 static void
172 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
173 {
174   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
176
177   gobject_class->finalize = gtk_file_chooser_entry_finalize;
178   gobject_class->dispose = gtk_file_chooser_entry_dispose;
179   gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
180
181   widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
182   widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
183   widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
184 }
185
186 static void
187 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
188 {
189   GtkEntryCompletion *comp;
190   GtkCellRenderer *cell;
191
192   chooser_entry->local_only = TRUE;
193   chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
194
195   g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
196
197   comp = gtk_entry_completion_new ();
198   gtk_entry_completion_set_popup_single_match (comp, FALSE);
199   /* see docs for gtk_entry_completion_set_text_column() */
200   g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
201
202   gtk_entry_completion_set_match_func (comp,
203                                        completion_match_func,
204                                        chooser_entry,
205                                        NULL);
206
207   cell = gtk_cell_renderer_text_new ();
208   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
209                               cell, TRUE);
210   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
211                                  cell,
212                                  "text", DISPLAY_NAME_COLUMN);
213
214   g_signal_connect (comp, "match-selected",
215                     G_CALLBACK (match_selected_callback), chooser_entry);
216
217   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
218   g_object_unref (comp);
219
220 #ifdef G_OS_WIN32
221   g_signal_connect (chooser_entry, "insert-text",
222                     G_CALLBACK (insert_text_callback), NULL);
223   g_signal_connect (chooser_entry, "delete-text",
224                     G_CALLBACK (delete_text_callback), NULL);
225 #endif
226 }
227
228 static void
229 gtk_file_chooser_entry_finalize (GObject *object)
230 {
231   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
232
233   g_object_unref (chooser_entry->base_folder);
234
235   if (chooser_entry->current_folder_file)
236     g_object_unref (chooser_entry->current_folder_file);
237
238   g_free (chooser_entry->dir_part);
239   g_free (chooser_entry->file_part);
240
241   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
242 }
243
244 static void
245 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
246 {
247   if (chooser_entry->current_folder_file)
248     {
249       g_object_unref (chooser_entry->current_folder_file);
250       chooser_entry->current_folder_file = NULL;
251     }
252 }
253
254 static void
255 gtk_file_chooser_entry_dispose (GObject *object)
256 {
257   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
258
259   discard_loading_and_current_folder_file (chooser_entry);
260
261   if (chooser_entry->completion_store)
262     {
263       g_object_unref (chooser_entry->completion_store);
264       chooser_entry->completion_store = NULL;
265     }
266
267   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
268 }
269
270 /* Match functions for the GtkEntryCompletion */
271 static gboolean
272 match_selected_callback (GtkEntryCompletion  *completion,
273                          GtkTreeModel        *model,
274                          GtkTreeIter         *iter,
275                          GtkFileChooserEntry *chooser_entry)
276 {
277   char *path;
278   
279   gtk_tree_model_get (model, iter,
280                       FULL_PATH_COLUMN, &path,
281                       -1);
282
283   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
284                             0,
285                             gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
286   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
287                             path,
288                             0,
289                             NULL); 
290
291   g_free (path);
292
293   return TRUE;
294 }
295
296 /* Match function for the GtkEntryCompletion */
297 static gboolean
298 completion_match_func (GtkEntryCompletion *comp,
299                        const char         *key_unused,
300                        GtkTreeIter        *iter,
301                        gpointer            data)
302 {
303   GtkFileChooserEntry *chooser_entry;
304   char *name = NULL;
305   gboolean result;
306   char *norm_file_part;
307   char *norm_name;
308
309   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
310
311   /* We ignore the key because it is the contents of the entry.  Instead, we
312    * just use our precomputed file_part.
313    */
314   if (!chooser_entry->file_part)
315     {
316       return FALSE;
317     }
318
319   gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
320   if (!name)
321     {
322       return FALSE; /* Uninitialized row, ugh */
323     }
324
325   /* If we have an empty file_part, then we're at the root of a directory.  In
326    * that case, we want to match all non-dot files.  We might want to match
327    * dot_files too if show_hidden is TRUE on the fileselector in the future.
328    */
329   /* Additionally, support for gnome .hidden files would be sweet, too */
330   if (chooser_entry->file_part[0] == '\000')
331     {
332       if (name[0] == '.')
333         result = FALSE;
334       else
335         result = TRUE;
336       g_free (name);
337
338       return result;
339     }
340
341
342   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
343   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
344
345 #ifdef G_PLATFORM_WIN32
346   {
347     gchar *temp;
348
349     temp = norm_file_part;
350     norm_file_part = g_utf8_casefold (norm_file_part, -1);
351     g_free (temp);
352
353     temp = norm_name;
354     norm_name = g_utf8_casefold (norm_name, -1);
355     g_free (temp);
356   }
357 #endif
358
359   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
360
361   g_free (norm_file_part);
362   g_free (norm_name);
363   g_free (name);
364   
365   return result;
366 }
367
368 static void
369 clear_completions (GtkFileChooserEntry *chooser_entry)
370 {
371   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
372 }
373
374 static void
375 beep (GtkFileChooserEntry *chooser_entry)
376 {
377   gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
378 }
379
380 static gboolean
381 is_valid_scheme_character (char c)
382 {
383   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
384 }
385
386 static gboolean
387 has_uri_scheme (const char *str)
388 {
389   const char *p;
390
391   p = str;
392
393   if (!is_valid_scheme_character (*p))
394     return FALSE;
395
396   do
397     p++;
398   while (is_valid_scheme_character (*p));
399
400   return (strncmp (p, "://", 3) == 0);
401 }
402
403 static GFile *
404 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
405                                     const gchar         *str)
406 {
407   GFile *file;
408
409   if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
410     file = g_file_parse_name (str);
411   else
412     file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
413
414   return file;
415 }
416
417 static GFile *
418 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
419                                          const char *         text)
420 {
421   GFile *file, *parent;
422
423   file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
424
425   if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
426     return file;
427
428   parent = g_file_get_parent (file);
429   g_object_unref (file);
430
431   return parent;
432 }
433
434 static gboolean
435 gtk_file_chooser_entry_parse (GtkFileChooserEntry  *chooser_entry,
436                               const gchar          *str,
437                               GFile               **folder,
438                               gchar               **file_part,
439                               GError              **error)
440 {
441   GFile *file;
442   gboolean result = FALSE;
443   gboolean is_dir = FALSE;
444   gchar *last_slash = NULL;
445
446   if (str && *str)
447     is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
448
449   last_slash = strrchr (str, G_DIR_SEPARATOR);
450
451   file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
452
453   if (g_file_equal (chooser_entry->base_folder, file))
454     {
455       /* this is when user types '.', could be the
456        * beginning of a hidden file, ./ or ../
457        */
458       *folder = g_object_ref (file);
459       *file_part = g_strdup (str);
460       result = TRUE;
461     }
462   else if (is_dir)
463     {
464       /* it's a dir, or at least it ends with the dir separator */
465       *folder = g_object_ref (file);
466       *file_part = g_strdup ("");
467       result = TRUE;
468     }
469   else
470     {
471       GFile *parent_file;
472
473       parent_file = g_file_get_parent (file);
474
475       if (!parent_file)
476         {
477           g_set_error (error,
478                        GTK_FILE_CHOOSER_ERROR,
479                        GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
480                        "Could not get parent file");
481           *folder = NULL;
482           *file_part = NULL;
483         }
484       else
485         {
486           *folder = parent_file;
487           result = TRUE;
488
489           if (last_slash)
490             *file_part = g_strdup (last_slash + 1);
491           else
492             *file_part = g_strdup (str);
493         }
494     }
495
496   g_object_unref (file);
497
498   return result;
499 }
500
501 /* Finds a common prefix based on the contents of the entry
502  * and mandatorily appends it
503  */
504 static void
505 explicitly_complete (GtkFileChooserEntry *chooser_entry)
506 {
507   clear_completions (chooser_entry);
508
509   if (chooser_entry->completion_store)
510     {
511       char *completion, *text;
512       gsize completion_len, text_len;
513       
514       text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
515       text_len = strlen (text);
516       completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
517       completion_len = completion ? strlen (completion) : 0;
518
519       if (completion_len > text_len)
520         {
521           GtkEditable *editable = GTK_EDITABLE (chooser_entry);
522           int pos = gtk_editable_get_position (editable);
523
524           gtk_editable_insert_text (editable,
525                                     completion + text_len,
526                                     completion_len - text_len,
527                                     &pos);
528           gtk_editable_set_position (editable, pos);
529           return;
530         }
531     }
532
533   beep (chooser_entry);
534 }
535
536 static void
537 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
538 {
539   GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
540   _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
541 }
542
543 static void
544 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
545 {
546   if (chooser_entry->current_folder_loaded)
547     explicitly_complete (chooser_entry);
548   else
549     chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
550 }
551
552 static gboolean
553 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
554                                         GdkEventKey *event)
555 {
556   GtkFileChooserEntry *chooser_entry;
557   GtkEditable *editable;
558   GdkModifierType state;
559   gboolean control_pressed;
560
561   chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
562   editable = GTK_EDITABLE (widget);
563
564   if (!chooser_entry->eat_tabs)
565     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
566
567   control_pressed = FALSE;
568
569   if (gtk_get_current_event_state (&state))
570     {
571       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
572         control_pressed = TRUE;
573     }
574
575   /* This is a bit evil -- it makes Tab never leave the entry. It basically
576    * makes it 'safe' for people to hit. */
577   if (event->keyval == GDK_KEY_Tab && !control_pressed)
578     {
579       gint start, end;
580
581       gtk_editable_get_selection_bounds (editable, &start, &end);
582       
583       if (start != end)
584         gtk_editable_set_position (editable, MAX (start, end));
585       else
586         start_explicit_completion (chooser_entry);
587
588       return TRUE;
589      }
590
591   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
592
593 }
594
595 static gboolean
596 gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
597                                         GdkEventFocus *event)
598 {
599   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
600
601   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
602  
603   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
604 }
605
606 static void
607 discard_completion_store (GtkFileChooserEntry *chooser_entry)
608 {
609   if (!chooser_entry->completion_store)
610     return;
611
612   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
613   gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
614   g_object_unref (chooser_entry->completion_store);
615   chooser_entry->completion_store = NULL;
616 }
617
618 static gboolean
619 completion_store_set (GtkFileSystemModel  *model,
620                       GFile               *file,
621                       GFileInfo           *info,
622                       int                  column,
623                       GValue              *value,
624                       gpointer             data)
625 {
626   GtkFileChooserEntry *chooser_entry = data;
627
628   const char *prefix = "";
629   const char *suffix = "";
630
631   switch (column)
632     {
633     case FILE_COLUMN:
634       g_value_set_object (value, file);
635       break;
636     case FULL_PATH_COLUMN:
637       prefix = chooser_entry->dir_part;
638       /* fall through */
639     case DISPLAY_NAME_COLUMN:
640       if (_gtk_file_info_consider_as_directory (info))
641         suffix = G_DIR_SEPARATOR_S;
642
643       g_value_take_string (value, g_strconcat (
644               prefix,
645               g_file_info_get_display_name (info),
646               suffix,
647               NULL));
648       break;
649     default:
650       g_assert_not_reached ();
651       break;
652     }
653
654   return TRUE;
655 }
656
657 /* Fills the completion store from the contents of the current folder */
658 static void
659 populate_completion_store (GtkFileChooserEntry *chooser_entry)
660 {
661   discard_completion_store (chooser_entry);
662
663   chooser_entry->completion_store = GTK_TREE_MODEL (
664       _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
665                                                 "standard::name,standard::display-name,standard::type",
666                                                 completion_store_set,
667                                                 chooser_entry,
668                                                 N_COLUMNS,
669                                                 G_TYPE_STRING,
670                                                 G_TYPE_STRING,
671                                                 G_TYPE_FILE));
672   g_signal_connect (chooser_entry->completion_store, "finished-loading",
673                     G_CALLBACK (finished_loading_cb), chooser_entry);
674
675   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
676                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
677
678   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
679                                   chooser_entry->completion_store);
680 }
681
682 /* When we finish loading the current folder, this function should get called to
683  * perform the deferred explicit completion.
684  */
685 static void
686 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
687 {
688   switch (chooser_entry->load_complete_action)
689     {
690     case LOAD_COMPLETE_NOTHING:
691       break;
692
693     case LOAD_COMPLETE_EXPLICIT_COMPLETION:
694       explicitly_complete (chooser_entry);
695       break;
696
697     default:
698       g_assert_not_reached ();
699     }
700
701 }
702
703 /* Callback when the current folder finishes loading */
704 static void
705 finished_loading_cb (GtkFileSystemModel  *model,
706                      GError              *error,
707                      GtkFileChooserEntry *chooser_entry)
708 {
709   GtkEntryCompletion *completion;
710
711   chooser_entry->current_folder_loaded = TRUE;
712
713   if (error)
714     {
715       LoadCompleteAction old_load_complete_action;
716
717       old_load_complete_action = chooser_entry->load_complete_action;
718
719       discard_completion_store (chooser_entry);
720       clear_completions (chooser_entry);
721
722       if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
723         {
724           /* Since this came from explicit user action (Tab completion), we'll present errors visually */
725
726           beep (chooser_entry);
727         }
728
729       return;
730     }
731
732   perform_load_complete_action (chooser_entry);
733
734   gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
735
736   completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
737   gtk_entry_completion_set_inline_completion (completion, TRUE);
738   gtk_entry_completion_complete (completion);
739   gtk_entry_completion_insert_prefix (completion);
740 }
741
742 static RefreshStatus
743 reload_current_folder (GtkFileChooserEntry *chooser_entry,
744                        GFile               *folder_file)
745 {
746   g_assert (folder_file != NULL);
747
748   if (chooser_entry->current_folder_file
749       && g_file_equal (folder_file, chooser_entry->current_folder_file))
750     return REFRESH_OK;
751
752   if (chooser_entry->current_folder_file)
753     {
754       discard_loading_and_current_folder_file (chooser_entry);
755     }
756   
757   if (chooser_entry->local_only
758       && !g_file_is_native (folder_file))
759     return REFRESH_NOT_LOCAL;
760
761   chooser_entry->current_folder_file = g_object_ref (folder_file);
762   chooser_entry->current_folder_loaded = FALSE;
763
764   populate_completion_store (chooser_entry);
765
766   return REFRESH_OK;
767 }
768
769 static RefreshStatus
770 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
771                                       const gchar *        text)
772 {
773   GFile *folder_file;
774   gchar *file_part;
775   gsize total_len, file_part_len;
776   gint file_part_pos;
777   GError *error;
778   RefreshStatus result;
779
780   error = NULL;
781   if (!gtk_file_chooser_entry_parse (chooser_entry,
782                                      text, &folder_file, &file_part, &error))
783     {
784       folder_file = g_object_ref (chooser_entry->base_folder);
785
786       if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
787         result = REFRESH_NONEXISTENT;
788       else
789         result = REFRESH_INVALID_INPUT;
790
791       if (error)
792         g_error_free (error);
793
794       file_part = g_strdup ("");
795       file_part_pos = -1;
796     }
797   else
798     {
799       g_assert (folder_file != NULL);
800
801       file_part_len = strlen (file_part);
802       total_len = strlen (text);
803       if (total_len > file_part_len)
804         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
805       else
806         file_part_pos = 0;
807
808       result = REFRESH_OK;
809     }
810
811   g_free (chooser_entry->file_part);
812   g_free (chooser_entry->dir_part);
813
814   chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
815   chooser_entry->file_part = file_part;
816   chooser_entry->file_part_pos = file_part_pos;
817
818   if (result == REFRESH_OK)
819     {
820       result = reload_current_folder (chooser_entry, folder_file);
821     }
822   else
823     {
824       discard_loading_and_current_folder_file (chooser_entry);
825     }
826
827   if (folder_file)
828     g_object_unref (folder_file);
829
830   g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
831             ((result == REFRESH_OK)
832              && (chooser_entry->current_folder_file != NULL))
833             /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
834             || ((result != REFRESH_OK)
835                 && (chooser_entry->current_folder_file == NULL)));
836
837   return result;
838 }
839
840 #ifdef G_OS_WIN32
841 static gint
842 insert_text_callback (GtkFileChooserEntry *chooser_entry,
843                       const gchar         *new_text,
844                       gint                 new_text_length,
845                       gint                *position,
846                       gpointer             user_data)
847 {
848   const gchar *colon = memchr (new_text, ':', new_text_length);
849   gint i;
850
851   /* Disallow these characters altogether */
852   for (i = 0; i < new_text_length; i++)
853     {
854       if (new_text[i] == '<' ||
855           new_text[i] == '>' ||
856           new_text[i] == '"' ||
857           new_text[i] == '|' ||
858           new_text[i] == '*' ||
859           new_text[i] == '?')
860         break;
861     }
862
863   if (i < new_text_length ||
864       /* Disallow entering text that would cause a colon to be anywhere except
865        * after a drive letter.
866        */
867       (colon != NULL &&
868        *position + (colon - new_text) != 1) ||
869       (new_text_length > 0 &&
870        *position <= 1 &&
871        gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
872        gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
873     {
874       gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
875       g_signal_stop_emission_by_name (chooser_entry, "insert_text");
876       return FALSE;
877     }
878
879   return TRUE;
880 }
881
882 static void
883 delete_text_callback (GtkFileChooserEntry *chooser_entry,
884                       gint                 start_pos,
885                       gint                 end_pos,
886                       gpointer             user_data)
887 {
888   /* If deleting a drive letter, delete the colon, too */
889   if (start_pos == 0 && end_pos == 1 &&
890       gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
891       gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
892     {
893       g_signal_handlers_block_by_func (chooser_entry,
894                                        G_CALLBACK (delete_text_callback),
895                                        user_data);
896       gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
897       g_signal_handlers_unblock_by_func (chooser_entry,
898                                          G_CALLBACK (delete_text_callback),
899                                          user_data);
900     }
901 }
902 #endif
903
904 /**
905  * _gtk_file_chooser_entry_new:
906  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
907  *
908  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
909  * is an internal implementation widget for the GTK+ file chooser
910  * which is an entry with completion with respect to a
911  * #GtkFileSystem object.
912  *
913  * Return value: the newly created #GtkFileChooserEntry
914  **/
915 GtkWidget *
916 _gtk_file_chooser_entry_new (gboolean       eat_tabs)
917 {
918   GtkFileChooserEntry *chooser_entry;
919
920   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
921   chooser_entry->eat_tabs = (eat_tabs != FALSE);
922
923   return GTK_WIDGET (chooser_entry);
924 }
925
926 /**
927  * _gtk_file_chooser_entry_set_base_folder:
928  * @chooser_entry: a #GtkFileChooserEntry
929  * @file: file for a folder in the chooser entries current file system.
930  *
931  * Sets the folder with respect to which completions occur.
932  **/
933 void
934 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
935                                          GFile               *file)
936 {
937   if (file)
938     g_object_ref (file);
939   else
940     file = g_file_new_for_path (g_get_home_dir ());
941
942   if (g_file_equal (chooser_entry->base_folder, file))
943     {
944       g_object_unref (file);
945       return;
946     }
947
948   if (chooser_entry->base_folder)
949     g_object_unref (chooser_entry->base_folder);
950
951   chooser_entry->base_folder = file;
952
953   clear_completions (chooser_entry);
954 }
955
956 /**
957  * _gtk_file_chooser_entry_get_current_folder:
958  * @chooser_entry: a #GtkFileChooserEntry
959  *
960  * Gets the current folder for the #GtkFileChooserEntry. If the
961  * user has only entered a filename, this will be in the base folder
962  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
963  * user has entered a relative or absolute path, then it will
964  * be different.  If the user has entered unparsable text, or text which
965  * the entry cannot handle, this will return %NULL.
966  *
967  * Return value: the file for the current folder - you must g_object_unref()
968  *   the value after use.
969  **/
970 GFile *
971 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
972 {
973   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
974
975   return gtk_file_chooser_get_directory_for_text (chooser_entry,
976                                                   gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
977 }
978
979 /**
980  * _gtk_file_chooser_entry_get_file_part:
981  * @chooser_entry: a #GtkFileChooserEntry
982  *
983  * Gets the non-folder portion of whatever the user has entered
984  * into the file selector. What is returned is a UTF-8 string,
985  * and if a filename path is needed, g_file_get_child_for_display_name()
986  * must be used
987   *
988  * Return value: the entered filename - this value is owned by the
989  *  chooser entry and must not be modified or freed.
990  **/
991 const gchar *
992 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
993 {
994   const char *last_slash, *text;
995
996   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
997
998   text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
999   last_slash = strrchr (text, G_DIR_SEPARATOR);
1000   if (last_slash)
1001     return last_slash + 1;
1002   else
1003     return text;
1004 }
1005
1006 /**
1007  * _gtk_file_chooser_entry_set_action:
1008  * @chooser_entry: a #GtkFileChooserEntry
1009  * @action: the action which is performed by the file selector using this entry
1010  *
1011  * Sets action which is performed by the file selector using this entry. 
1012  * The #GtkFileChooserEntry will use different completion strategies for 
1013  * different actions.
1014  **/
1015 void
1016 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1017                                     GtkFileChooserAction action)
1018 {
1019   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1020   
1021   if (chooser_entry->action != action)
1022     {
1023       GtkEntryCompletion *comp;
1024
1025       chooser_entry->action = action;
1026
1027       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1028
1029       /* FIXME: do we need to actually set the following? */
1030
1031       switch (action)
1032         {
1033         case GTK_FILE_CHOOSER_ACTION_OPEN:
1034         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1035           gtk_entry_completion_set_popup_single_match (comp, FALSE);
1036           break;
1037         case GTK_FILE_CHOOSER_ACTION_SAVE:
1038         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1039           gtk_entry_completion_set_popup_single_match (comp, TRUE);
1040           break;
1041         }
1042     }
1043 }
1044
1045
1046 /**
1047  * _gtk_file_chooser_entry_get_action:
1048  * @chooser_entry: a #GtkFileChooserEntry
1049  *
1050  * Gets the action for this entry. 
1051  *
1052  * Returns: the action
1053  **/
1054 GtkFileChooserAction
1055 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1056 {
1057   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1058                         GTK_FILE_CHOOSER_ACTION_OPEN);
1059   
1060   return chooser_entry->action;
1061 }
1062
1063 gboolean
1064 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1065                                        GFile               *file)
1066 {
1067   GtkTreeIter iter;
1068   GFileInfo *info;
1069
1070   if (chooser_entry->completion_store == NULL ||
1071       !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1072                                                  &iter,
1073                                                  file))
1074     return FALSE;
1075
1076   info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1077                                           &iter);
1078
1079   return _gtk_file_info_consider_as_directory (info);
1080 }
1081
1082
1083 /*
1084  * _gtk_file_chooser_entry_select_filename:
1085  * @chooser_entry: a #GtkFileChooserEntry
1086  *
1087  * Selects the filename (without the extension) for user edition.
1088  */
1089 void
1090 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1091 {
1092   const gchar *str, *ext;
1093   glong len = -1;
1094
1095   if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1096     {
1097       str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1098       ext = g_strrstr (str, ".");
1099
1100       if (ext)
1101        len = g_utf8_pointer_to_offset (str, ext);
1102     }
1103
1104   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1105 }
1106
1107 void
1108 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1109                                         gboolean             local_only)
1110 {
1111   chooser_entry->local_only = local_only;
1112   clear_completions (chooser_entry);
1113 }
1114
1115 gboolean
1116 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1117 {
1118   return chooser_entry->local_only;
1119 }