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