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