1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
45 #include <glib.h> /* Include early to get G_OS_WIN32 etc */
47 #if defined(G_PLATFORM_WIN32)
52 #endif /* G_PLATFORM_WIN32 */
54 #include <winsock.h> /* For gethostname */
57 #include "gdk/gdkkeysyms.h"
58 #include "gtkbutton.h"
59 #include "gtkcellrenderertext.h"
61 #include "gtkfilesel.h"
66 #include "gtkliststore.h"
68 #include "gtkprivate.h"
69 #include "gtkscrolledwindow.h"
71 #include "gtktreeselection.h"
72 #include "gtktreeview.h"
75 #include "gtkmenuitem.h"
76 #include "gtkoptionmenu.h"
77 #include "gtkdialog.h"
78 #include "gtkmessagedialog.h"
80 #include "gtkeventbox.h"
83 #include "gtkhpaned.h"
88 #define mkdir(p,m) _mkdir(p)
90 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
92 #endif /* G_OS_WIN32 */
95 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
98 #define DIR_LIST_WIDTH 180
99 #define DIR_LIST_HEIGHT 180
100 #define FILE_LIST_WIDTH 180
101 #define FILE_LIST_HEIGHT 180
103 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
104 * in here, since the rest of the code in the file does require some
109 # define MAXPATHLEN PATH_MAX
111 # define MAXPATHLEN 2048
115 /* I've put this here so it doesn't get confused with the
116 * file completion interface */
117 typedef struct _HistoryCallbackArg HistoryCallbackArg;
119 struct _HistoryCallbackArg
122 GtkWidget *menu_item;
126 typedef struct _CompletionState CompletionState;
127 typedef struct _CompletionDir CompletionDir;
128 typedef struct _CompletionDirSent CompletionDirSent;
129 typedef struct _CompletionDirEntry CompletionDirEntry;
130 typedef struct _CompletionUserDir CompletionUserDir;
131 typedef struct _PossibleCompletion PossibleCompletion;
133 /* Non-external file completion decls and structures */
135 /* A contant telling PRCS how many directories to cache. Its actually
136 * kept in a list, so the geometry isn't important. */
137 #define CMPL_DIRECTORY_CACHE_SIZE 10
139 /* A constant used to determine whether a substring was an exact
140 * match by first_diff_index()
142 #define PATTERN_MATCH -1
143 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
144 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
146 /* This structure contains all the useful information about a directory
147 * for the purposes of filename completion. These structures are cached
148 * in the CompletionState struct. CompletionDir's are reference counted.
150 struct _CompletionDirSent
152 #ifndef G_PLATFORM_WIN32
159 struct _CompletionDirEntry *entries;
162 struct _CompletionDir
164 CompletionDirSent *sent;
169 struct _CompletionDir *cmpl_parent;
174 /* This structure contains pairs of directory entry names with a flag saying
175 * whether or not they are a valid directory. NOTE: This information is used
176 * to provide the caller with information about whether to update its completions
177 * or try to open a file. Since directories are cached by the directory mtime,
178 * a symlink which points to an invalid file (which will not be a directory),
179 * will not be reevaluated if that file is created, unless the containing
180 * directory is touched. I consider this case to be worth ignoring (josh).
182 struct _CompletionDirEntry
189 struct _CompletionUserDir
195 struct _PossibleCompletion
197 /* accessible fields, all are accessed externally by functions
201 gint is_a_completion;
202 gboolean is_directory;
209 struct _CompletionState
211 gint last_valid_char;
213 gint updated_text_len;
214 gint updated_text_alloc;
215 gboolean re_complete;
217 gchar *user_dir_name_buffer;
218 gint user_directories_len;
220 gchar *last_completion_text;
222 gint user_completion_index; /* if >= 0, currently completing ~user */
224 struct _CompletionDir *completion_dir; /* directory completing from */
225 struct _CompletionDir *active_completion_dir;
227 struct _PossibleCompletion the_completion;
229 struct _CompletionDir *reference_dir; /* initial directory */
231 GList* directory_storage;
232 GList* directory_sent_storage;
234 struct _CompletionUserDir *user_directories;
252 /* File completion functions which would be external, were they used
253 * outside of this file.
256 static CompletionState* cmpl_init_state (void);
257 static void cmpl_free_state (CompletionState *cmpl_state);
258 static gint cmpl_state_okay (CompletionState* cmpl_state);
259 static const gchar* cmpl_strerror (gint);
261 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
262 gchar **remaining_text,
263 CompletionState *cmpl_state);
265 /* Returns a name for consideration, possibly a completion, this name
266 * will be invalid after the next call to cmpl_next_completion.
268 static char* cmpl_this_completion (PossibleCompletion*);
270 /* True if this completion matches the given text. Otherwise, this
271 * output can be used to have a list of non-completions.
273 static gint cmpl_is_a_completion (PossibleCompletion*);
275 /* True if the completion is a directory
277 static gboolean cmpl_is_directory (PossibleCompletion*);
279 /* Obtains the next completion, or NULL
281 static PossibleCompletion* cmpl_next_completion (CompletionState*);
283 /* Updating completions: the return value of cmpl_updated_text() will
284 * be text_to_complete completed as much as possible after the most
285 * recent call to cmpl_completion_matches. For the present
286 * application, this is the suggested replacement for the user's input
287 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
290 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
292 /* After updating, to see if the completion was a directory, call
293 * this. If it was, you should consider re-calling completion_matches.
295 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
297 /* Current location: if using file completion, return the current
298 * directory, from which file completion begins. More specifically,
299 * the cwd concatenated with all exact completions up to the last
300 * directory delimiter('/').
302 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
304 /* backing up: if cmpl_completion_matches returns NULL, you may query
305 * the index of the last completable character into cmpl_updated_text.
307 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
309 /* When the user selects a non-directory, call cmpl_completion_fullname
310 * to get the full name of the selected file.
312 static const gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
315 /* Directory operations. */
316 static CompletionDir* open_ref_dir (gchar* text_to_complete,
317 gchar** remaining_text,
318 CompletionState* cmpl_state);
319 #ifndef G_PLATFORM_WIN32
320 static gboolean check_dir (gchar *dir_name,
322 gboolean *stat_subdirs);
324 static CompletionDir* open_dir (gchar* dir_name,
325 CompletionState* cmpl_state);
327 static CompletionDir* open_user_dir (const gchar* text_to_complete,
328 CompletionState *cmpl_state);
330 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
331 CompletionState *cmpl_state);
332 static CompletionDirSent* open_new_dir (gchar* dir_name,
334 gboolean stat_subdirs);
335 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
336 static gint correct_parent (CompletionDir* cmpl_dir,
338 #ifndef G_PLATFORM_WIN32
339 static gchar* find_parent_dir_fullname (gchar* dirname);
341 static CompletionDir* attach_dir (CompletionDirSent* sent,
343 CompletionState *cmpl_state);
344 static void free_dir_sent (CompletionDirSent* sent);
345 static void free_dir (CompletionDir *dir);
346 static void prune_memory_usage(CompletionState *cmpl_state);
348 /* Completion operations */
350 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
351 CompletionState *cmpl_state);
353 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
354 static CompletionDir* find_completion_dir(gchar* text_to_complete,
355 gchar** remaining_text,
356 CompletionState* cmpl_state);
357 static PossibleCompletion* append_completion_text(gchar* text,
358 CompletionState* cmpl_state);
360 static gint get_pwdb(CompletionState* cmpl_state);
361 static gint compare_user_dir(const void* a, const void* b);
363 static gint first_diff_index(gchar* pat, gchar* text);
364 static gint compare_cmpl_dir(const void* a, const void* b);
365 static void update_cmpl(PossibleCompletion* poss,
366 CompletionState* cmpl_state);
368 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
369 static void gtk_file_selection_set_property (GObject *object,
373 static void gtk_file_selection_get_property (GObject *object,
377 static void gtk_file_selection_init (GtkFileSelection *filesel);
378 static void gtk_file_selection_finalize (GObject *object);
379 static void gtk_file_selection_destroy (GtkObject *object);
380 static void gtk_file_selection_map (GtkWidget *widget);
381 static gint gtk_file_selection_key_press (GtkWidget *widget,
384 static gint gtk_file_selection_insert_text (GtkWidget *widget,
385 const gchar *new_text,
386 gint new_text_length,
389 static void gtk_file_selection_update_fileops (GtkFileSelection *filesel);
391 static void gtk_file_selection_file_activate (GtkTreeView *tree_view,
393 GtkTreeViewColumn *column,
395 static void gtk_file_selection_file_changed (GtkTreeSelection *selection,
397 static void gtk_file_selection_dir_activate (GtkTreeView *tree_view,
399 GtkTreeViewColumn *column,
402 static void gtk_file_selection_populate (GtkFileSelection *fs,
404 gboolean try_complete,
405 gboolean reset_entry);
406 static void gtk_file_selection_abort (GtkFileSelection *fs);
408 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
411 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
412 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
413 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
415 static void free_selected_names (GPtrArray *names);
417 #ifndef G_PLATFORM_WIN32
419 #define compare_utf8_filenames(a, b) strcmp(a, b)
420 #define compare_sys_filenames(a, b) strcmp(a, b)
425 compare_utf8_filenames (const gchar *a,
428 gchar *a_folded, *b_folded;
431 a_folded = g_utf8_strdown (a, -1);
432 b_folded = g_utf8_strdown (b, -1);
434 retval = strcmp (a_folded, b_folded);
443 compare_sys_filenames (const gchar *a,
446 gchar *a_utf8, *b_utf8;
449 a_utf8 = g_filename_to_utf8 (a, -1, NULL, NULL, NULL);
450 b_utf8 = g_filename_to_utf8 (b, -1, NULL, NULL, NULL);
452 retval = compare_utf8_filenames (a_utf8, b_utf8);
462 static GtkWindowClass *parent_class = NULL;
464 /* Saves errno when something cmpl does fails. */
465 static gint cmpl_errno;
469 * Take the path currently in the file selection
470 * entry field and translate as necessary from
471 * a Win32 style to Cygwin style path. For
472 * instance translate:
473 * x:\somepath\file.jpg
475 * /cygdrive/x/somepath/file.jpg
477 * Replace the path in the selection text field.
478 * Return a boolean value concerning whether a
479 * translation had to be made.
482 translate_win32_path (GtkFileSelection *filesel)
486 gchar newPath[MAX_PATH];
489 * Retrieve the current path
491 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
493 cygwin_conv_to_posix_path (path, newPath);
494 updated = (strcmp (path, newPath) != 0);
497 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
504 gtk_file_selection_get_type (void)
506 static GType file_selection_type = 0;
508 if (!file_selection_type)
510 static const GTypeInfo filesel_info =
512 sizeof (GtkFileSelectionClass),
513 NULL, /* base_init */
514 NULL, /* base_finalize */
515 (GClassInitFunc) gtk_file_selection_class_init,
516 NULL, /* class_finalize */
517 NULL, /* class_data */
518 sizeof (GtkFileSelection),
520 (GInstanceInitFunc) gtk_file_selection_init,
523 file_selection_type =
524 g_type_register_static (GTK_TYPE_DIALOG, "GtkFileSelection",
528 return file_selection_type;
532 gtk_file_selection_class_init (GtkFileSelectionClass *class)
534 GObjectClass *gobject_class;
535 GtkObjectClass *object_class;
536 GtkWidgetClass *widget_class;
538 gobject_class = (GObjectClass*) class;
539 object_class = (GtkObjectClass*) class;
540 widget_class = (GtkWidgetClass*) class;
542 parent_class = g_type_class_peek_parent (class);
544 gobject_class->finalize = gtk_file_selection_finalize;
545 gobject_class->set_property = gtk_file_selection_set_property;
546 gobject_class->get_property = gtk_file_selection_get_property;
548 g_object_class_install_property (gobject_class,
550 g_param_spec_string ("filename",
552 _("The currently selected filename"),
554 G_PARAM_READABLE | G_PARAM_WRITABLE));
555 g_object_class_install_property (gobject_class,
557 g_param_spec_boolean ("show_fileops",
558 _("Show file operations"),
559 _("Whether buttons for creating/manipulating files should be displayed"),
563 g_object_class_install_property (gobject_class,
564 PROP_SELECT_MULTIPLE,
565 g_param_spec_boolean ("select_multiple",
566 _("Select multiple"),
567 _("Whether to allow multiple files to be selected"),
571 object_class->destroy = gtk_file_selection_destroy;
572 widget_class->map = gtk_file_selection_map;
575 static void gtk_file_selection_set_property (GObject *object,
580 GtkFileSelection *filesel;
582 filesel = GTK_FILE_SELECTION (object);
587 gtk_file_selection_set_filename (filesel,
588 g_value_get_string (value));
590 case PROP_SHOW_FILEOPS:
591 if (g_value_get_boolean (value))
592 gtk_file_selection_show_fileop_buttons (filesel);
594 gtk_file_selection_hide_fileop_buttons (filesel);
596 case PROP_SELECT_MULTIPLE:
597 gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
600 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605 static void gtk_file_selection_get_property (GObject *object,
610 GtkFileSelection *filesel;
612 filesel = GTK_FILE_SELECTION (object);
617 g_value_set_string (value,
618 gtk_file_selection_get_filename(filesel));
621 case PROP_SHOW_FILEOPS:
622 /* This is a little bit hacky, but doing otherwise would require
623 * adding a field to the object.
625 g_value_set_boolean (value, (filesel->fileop_c_dir &&
626 filesel->fileop_del_file &&
627 filesel->fileop_ren_file));
629 case PROP_SELECT_MULTIPLE:
630 g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
633 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
639 grab_default (GtkWidget *widget)
641 gtk_widget_grab_default (widget);
646 gtk_file_selection_init (GtkFileSelection *filesel)
648 GtkWidget *entry_vbox;
650 GtkWidget *list_hbox, *list_container;
651 GtkWidget *confirm_area;
652 GtkWidget *pulldown_hbox;
653 GtkWidget *scrolled_win;
659 GtkTreeViewColumn *column;
661 gtk_widget_push_composite_child ();
663 dialog = GTK_DIALOG (filesel);
665 filesel->cmpl_state = cmpl_init_state ();
667 /* The dialog-sized vertical box */
668 filesel->main_vbox = dialog->vbox;
669 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
671 /* The horizontal box containing create, rename etc. buttons */
672 filesel->button_area = gtk_hbutton_box_new ();
673 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
674 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
675 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
677 gtk_widget_show (filesel->button_area);
679 gtk_file_selection_show_fileop_buttons (filesel);
681 /* hbox for pulldown menu */
682 pulldown_hbox = gtk_hbox_new (TRUE, 5);
683 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
684 gtk_widget_show (pulldown_hbox);
687 filesel->history_pulldown = gtk_option_menu_new ();
688 gtk_widget_show (filesel->history_pulldown);
689 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
692 /* The horizontal box containing the directory and file listboxes */
694 spacer = gtk_hbox_new (FALSE, 0);
695 gtk_widget_set_size_request (spacer, -1, 5);
696 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
697 gtk_widget_show (spacer);
699 list_hbox = gtk_hbox_new (FALSE, 5);
700 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
701 gtk_widget_show (list_hbox);
703 list_container = g_object_new (GTK_TYPE_HPANED,
709 list_container = list_hbox;
711 spacer = gtk_hbox_new (FALSE, 0);
712 gtk_widget_set_size_request (spacer, -1, 5);
713 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
714 gtk_widget_show (spacer);
716 /* The directories list */
718 model = gtk_list_store_new (1, G_TYPE_STRING);
719 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
720 g_object_unref (model);
722 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
723 gtk_cell_renderer_text_new (),
726 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
727 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
728 gtk_widget_show (label);
729 gtk_tree_view_column_set_widget (column, label);
730 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
731 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
733 gtk_widget_set_size_request (filesel->dir_list,
734 DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
735 g_signal_connect (filesel->dir_list, "row_activated",
736 G_CALLBACK (gtk_file_selection_dir_activate), filesel);
738 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
740 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
741 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
742 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
743 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
744 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
745 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
746 if (GTK_IS_PANED (list_container))
747 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
749 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
750 gtk_widget_show (filesel->dir_list);
751 gtk_widget_show (scrolled_win);
754 model = gtk_list_store_new (1, G_TYPE_STRING);
755 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
756 g_object_unref (model);
758 column = gtk_tree_view_column_new_with_attributes (_("Files"),
759 gtk_cell_renderer_text_new (),
762 label = gtk_label_new_with_mnemonic (_("_Files"));
763 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
764 gtk_widget_show (label);
765 gtk_tree_view_column_set_widget (column, label);
766 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
767 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
769 gtk_widget_set_size_request (filesel->file_list,
770 FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
771 g_signal_connect (filesel->file_list, "row_activated",
772 G_CALLBACK (gtk_file_selection_file_activate), filesel);
773 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
774 G_CALLBACK (gtk_file_selection_file_changed), filesel);
776 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
778 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
779 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
780 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
781 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
782 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
783 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
784 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
785 gtk_widget_show (filesel->file_list);
786 gtk_widget_show (scrolled_win);
788 /* action area for packing buttons into. */
789 filesel->action_area = gtk_hbox_new (TRUE, 0);
790 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
792 gtk_widget_show (filesel->action_area);
794 /* The OK/Cancel button area */
795 confirm_area = dialog->action_area;
797 /* The Cancel button */
798 filesel->cancel_button = gtk_dialog_add_button (dialog,
800 GTK_RESPONSE_CANCEL);
802 filesel->ok_button = gtk_dialog_add_button (dialog,
806 gtk_widget_grab_default (filesel->ok_button);
808 /* The selection entry widget */
809 entry_vbox = gtk_vbox_new (FALSE, 2);
810 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
811 gtk_widget_show (entry_vbox);
813 eventbox = gtk_event_box_new ();
814 filesel->selection_text = label = gtk_label_new ("");
815 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
816 gtk_container_add (GTK_CONTAINER (eventbox), label);
817 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
818 gtk_widget_show (label);
819 gtk_widget_show (eventbox);
821 filesel->selection_entry = gtk_entry_new ();
822 g_signal_connect (filesel->selection_entry, "key_press_event",
823 G_CALLBACK (gtk_file_selection_key_press), filesel);
824 g_signal_connect (filesel->selection_entry, "insert_text",
825 G_CALLBACK (gtk_file_selection_insert_text), NULL);
826 g_signal_connect_swapped (filesel->selection_entry, "changed",
827 G_CALLBACK (gtk_file_selection_update_fileops), filesel);
828 g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
829 G_CALLBACK (grab_default),
831 g_signal_connect_swapped (filesel->selection_entry, "activate",
832 G_CALLBACK (gtk_button_clicked),
835 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
836 gtk_widget_show (filesel->selection_entry);
838 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
839 filesel->selection_entry);
841 if (!cmpl_state_okay (filesel->cmpl_state))
845 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
847 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
851 gtk_file_selection_populate (filesel, "", FALSE, TRUE);
854 gtk_widget_grab_focus (filesel->selection_entry);
856 gtk_widget_pop_composite_child ();
860 uri_list_extract_first_uri (const gchar* uri_list)
864 g_return_val_if_fail (uri_list != NULL, NULL);
867 /* We don't actually try to validate the URI according to RFC
868 * 2396, or even check for allowed characters - we just ignore
869 * comments and trim whitespace off the ends. We also
870 * allow LF delimination as well as the specified CRLF.
872 * We do allow comments like specified in RFC 2483.
878 while (g_ascii_isspace(*p))
882 while (*q && (*q != '\n') && (*q != '\r'))
888 while (q > p && g_ascii_isspace (*q))
892 return g_strndup (p, q - p + 1);
895 p = strchr (p, '\n');
903 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
907 if (response_id == GTK_RESPONSE_YES)
909 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
911 gtk_file_selection_set_filename (fs, filename);
914 gtk_widget_destroy (dialog);
919 filenames_dropped (GtkWidget *widget,
920 GdkDragContext *context,
923 GtkSelectionData *selection_data,
928 char *filename = NULL;
930 char this_hostname[257];
932 GError *error = NULL;
934 if (!selection_data->data)
937 uri = uri_list_extract_first_uri ((char *)selection_data->data);
942 filename = g_filename_from_uri (uri, &hostname, &error);
947 g_warning ("Error getting dropped filename: %s\n",
949 g_error_free (error);
953 res = gethostname (this_hostname, 256);
954 this_hostname[256] = 0;
956 if ((hostname == NULL) ||
957 (res == 0 && strcmp (hostname, this_hostname) == 0) ||
958 (strcmp (hostname, "localhost") == 0))
959 gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
964 gchar *filename_utf8;
966 /* Conversion back to UTF-8 should always succeed for the result
967 * of g_filename_from_uri()
969 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
970 g_assert (filename_utf8);
972 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
973 GTK_DIALOG_DESTROY_WITH_PARENT,
974 GTK_MESSAGE_QUESTION,
976 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
977 "Are you sure that you want to select it?"), filename_utf8, hostname);
978 g_free (filename_utf8);
980 g_object_set_data_full (G_OBJECT (dialog), "gtk-fs-dnd-filename", g_strdup (filename), g_free);
982 g_signal_connect_data (dialog, "response",
983 (GCallback) dnd_really_drop,
986 gtk_widget_show (dialog);
1004 filenames_drag_get (GtkWidget *widget,
1005 GdkDragContext *context,
1006 GtkSelectionData *selection_data,
1009 GtkFileSelection *filesel)
1017 file = gtk_file_selection_get_filename (filesel);
1021 if (info == TARGET_URILIST)
1023 res = gethostname (hostname, 256);
1026 uri_list = g_filename_to_uri (file, (!res)?hostname:NULL, &error);
1029 g_warning ("Error getting filename: %s\n",
1031 g_error_free (error);
1035 gtk_selection_data_set (selection_data,
1036 selection_data->target, 8,
1037 (void *)uri_list, strlen((char *)uri_list));
1042 gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
1043 g_assert (filename_utf8);
1044 gtk_selection_data_set_text (selection_data, filename_utf8, -1);
1045 g_free (filename_utf8);
1051 file_selection_setup_dnd (GtkFileSelection *filesel)
1053 GtkWidget *eventbox;
1054 static const GtkTargetEntry drop_types[] = {
1055 { "text/uri-list", 0, TARGET_URILIST}
1057 static gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
1058 static const GtkTargetEntry drag_types[] = {
1059 { "text/uri-list", 0, TARGET_URILIST},
1060 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
1063 { "COMPOUND_TEXT", 0, 0 }
1065 static gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
1067 gtk_drag_dest_set (GTK_WIDGET (filesel),
1068 GTK_DEST_DEFAULT_ALL,
1069 drop_types, n_drop_types,
1072 g_signal_connect (filesel, "drag_data_received",
1073 G_CALLBACK (filenames_dropped), NULL);
1075 eventbox = gtk_widget_get_parent (filesel->selection_text);
1076 gtk_drag_source_set (eventbox,
1078 drag_types, n_drag_types,
1081 g_signal_connect (eventbox, "drag_data_get",
1082 G_CALLBACK (filenames_drag_get), filesel);
1086 gtk_file_selection_new (const gchar *title)
1088 GtkFileSelection *filesel;
1090 filesel = g_object_new (GTK_TYPE_FILE_SELECTION, NULL);
1091 gtk_window_set_title (GTK_WINDOW (filesel), title);
1092 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1094 file_selection_setup_dnd (filesel);
1096 return GTK_WIDGET (filesel);
1100 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1102 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1104 /* delete, create directory, and rename */
1105 if (!filesel->fileop_c_dir)
1107 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1108 g_signal_connect (filesel->fileop_c_dir, "clicked",
1109 G_CALLBACK (gtk_file_selection_create_dir),
1111 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1112 filesel->fileop_c_dir, TRUE, TRUE, 0);
1113 gtk_widget_show (filesel->fileop_c_dir);
1116 if (!filesel->fileop_del_file)
1118 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1119 g_signal_connect (filesel->fileop_del_file, "clicked",
1120 G_CALLBACK (gtk_file_selection_delete_file),
1122 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1123 filesel->fileop_del_file, TRUE, TRUE, 0);
1124 gtk_widget_show (filesel->fileop_del_file);
1127 if (!filesel->fileop_ren_file)
1129 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1130 g_signal_connect (filesel->fileop_ren_file, "clicked",
1131 G_CALLBACK (gtk_file_selection_rename_file),
1133 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1134 filesel->fileop_ren_file, TRUE, TRUE, 0);
1135 gtk_widget_show (filesel->fileop_ren_file);
1138 gtk_file_selection_update_fileops (filesel);
1140 g_object_notify (G_OBJECT (filesel), "show_fileops");
1144 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1146 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1148 if (filesel->fileop_ren_file)
1150 gtk_widget_destroy (filesel->fileop_ren_file);
1151 filesel->fileop_ren_file = NULL;
1154 if (filesel->fileop_del_file)
1156 gtk_widget_destroy (filesel->fileop_del_file);
1157 filesel->fileop_del_file = NULL;
1160 if (filesel->fileop_c_dir)
1162 gtk_widget_destroy (filesel->fileop_c_dir);
1163 filesel->fileop_c_dir = NULL;
1165 g_object_notify (G_OBJECT (filesel), "show_fileops");
1171 * gtk_file_selection_set_filename:
1172 * @filesel: a #GtkFileSelection.
1173 * @filename: a string to set as the default file name.
1175 * Sets a default path for the file requestor. If @filename includes a
1176 * directory path, then the requestor will open with that path as its
1177 * current working directory.
1179 * This has the consequence that in order to open the requestor with a
1180 * working directory and an empty filename, @filename must have a trailing
1181 * directory separator.
1183 * The encoding of @filename is the on-disk encoding, which
1184 * may not be UTF-8. See g_filename_from_utf8().
1187 gtk_file_selection_set_filename (GtkFileSelection *filesel,
1188 const gchar *filename)
1191 const char *name, *last_slash;
1192 char *filename_utf8;
1194 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1195 g_return_if_fail (filename != NULL);
1197 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1198 g_return_if_fail (filename_utf8 != NULL);
1200 last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1204 buf = g_strdup ("");
1205 name = filename_utf8;
1209 buf = g_strdup (filename_utf8);
1210 buf[last_slash - filename_utf8 + 1] = 0;
1211 name = last_slash + 1;
1214 gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1216 if (filesel->selection_entry)
1217 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1219 g_object_notify (G_OBJECT (filesel), "filename");
1221 g_free (filename_utf8);
1225 * gtk_file_selection_get_filename:
1226 * @filesel: a #GtkFileSelection
1228 * This function returns the selected filename in the on-disk encoding
1229 * (see g_filename_from_utf8()), which may or may not be the same as that
1230 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1231 * The returned string points to a statically allocated buffer and
1232 * should be copied if you plan to keep it around.
1234 * If no file is selected then the selected directory path is returned.
1236 * Return value: currently-selected filename in the on-disk encoding.
1238 G_CONST_RETURN gchar*
1239 gtk_file_selection_get_filename (GtkFileSelection *filesel)
1241 static const gchar nothing[2] = "";
1242 static gchar something[MAXPATHLEN*2];
1246 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1248 #ifdef G_WITH_CYGWIN
1249 translate_win32_path (filesel);
1251 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1254 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
1257 strncpy (something, sys_filename, sizeof (something));
1258 g_free (sys_filename);
1266 gtk_file_selection_complete (GtkFileSelection *filesel,
1267 const gchar *pattern)
1269 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1270 g_return_if_fail (pattern != NULL);
1272 if (filesel->selection_entry)
1273 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1274 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1278 gtk_file_selection_destroy (GtkObject *object)
1280 GtkFileSelection *filesel;
1282 HistoryCallbackArg *callback_arg;
1284 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1286 filesel = GTK_FILE_SELECTION (object);
1288 if (filesel->fileop_dialog)
1290 gtk_widget_destroy (filesel->fileop_dialog);
1291 filesel->fileop_dialog = NULL;
1294 if (filesel->history_list)
1296 list = filesel->history_list;
1299 callback_arg = list->data;
1300 g_free (callback_arg->directory);
1301 g_free (callback_arg);
1304 g_list_free (filesel->history_list);
1305 filesel->history_list = NULL;
1308 if (filesel->cmpl_state)
1310 cmpl_free_state (filesel->cmpl_state);
1311 filesel->cmpl_state = NULL;
1314 if (filesel->selected_names)
1316 free_selected_names (filesel->selected_names);
1317 filesel->selected_names = NULL;
1320 if (filesel->last_selected)
1322 g_free (filesel->last_selected);
1323 filesel->last_selected = NULL;
1326 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1330 gtk_file_selection_map (GtkWidget *widget)
1332 GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1334 /* Refresh the contents */
1335 gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1337 GTK_WIDGET_CLASS (parent_class)->map (widget);
1341 gtk_file_selection_finalize (GObject *object)
1343 GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1345 g_free (filesel->fileop_file);
1347 G_OBJECT_CLASS (parent_class)->finalize (object);
1350 /* Begin file operations callbacks */
1353 gtk_file_selection_fileop_error (GtkFileSelection *fs,
1354 gchar *error_message)
1358 g_return_if_fail (error_message != NULL);
1361 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1362 GTK_DIALOG_DESTROY_WITH_PARENT,
1365 "%s", error_message);
1367 /* yes, we free it */
1368 g_free (error_message);
1370 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1372 g_signal_connect_swapped (dialog, "response",
1373 G_CALLBACK (gtk_widget_destroy),
1376 gtk_widget_show (dialog);
1380 gtk_file_selection_fileop_destroy (GtkWidget *widget,
1383 GtkFileSelection *fs = data;
1385 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1387 fs->fileop_dialog = NULL;
1391 entry_is_empty (GtkEntry *entry)
1393 const gchar *text = gtk_entry_get_text (entry);
1395 return *text == '\0';
1399 gtk_file_selection_fileop_entry_changed (GtkEntry *entry,
1402 gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1406 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1409 GtkFileSelection *fs = data;
1410 const gchar *dirname;
1413 gchar *sys_full_path;
1415 GError *error = NULL;
1416 CompletionState *cmpl_state;
1418 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1420 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1421 cmpl_state = (CompletionState*) fs->cmpl_state;
1422 path = cmpl_reference_position (cmpl_state);
1424 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1425 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1428 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1429 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1431 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname, error->message,
1432 _("You probably used symbols not allowed in filenames."));
1433 gtk_file_selection_fileop_error (fs, buf);
1434 g_error_free (error);
1438 if (mkdir (sys_full_path, 0755) < 0)
1440 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname,
1441 g_strerror (errno));
1442 gtk_file_selection_fileop_error (fs, buf);
1447 g_free (sys_full_path);
1449 gtk_widget_destroy (fs->fileop_dialog);
1450 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1454 gtk_file_selection_create_dir (GtkWidget *widget,
1457 GtkFileSelection *fs = data;
1463 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1465 if (fs->fileop_dialog)
1469 dialog = gtk_dialog_new ();
1470 fs->fileop_dialog = dialog;
1471 g_signal_connect (dialog, "destroy",
1472 G_CALLBACK (gtk_file_selection_fileop_destroy),
1474 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1475 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1476 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1478 /* If file dialog is grabbed, grab option dialog */
1479 /* When option dialog is closed, file dialog will be grabbed again */
1480 if (GTK_WINDOW (fs)->modal)
1481 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1483 vbox = gtk_vbox_new (FALSE, 0);
1484 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1485 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1487 gtk_widget_show( vbox);
1489 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1490 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1491 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1492 gtk_widget_show (label);
1494 /* The directory entry widget */
1495 fs->fileop_entry = gtk_entry_new ();
1496 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1497 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1499 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1500 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1501 gtk_widget_show (fs->fileop_entry);
1504 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1505 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1506 g_signal_connect_swapped (button, "clicked",
1507 G_CALLBACK (gtk_widget_destroy),
1510 gtk_widget_grab_focus (fs->fileop_entry);
1512 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1513 _("C_reate"), GTK_RESPONSE_OK);
1514 gtk_widget_set_sensitive (button, FALSE);
1515 g_signal_connect (button, "clicked",
1516 G_CALLBACK (gtk_file_selection_create_dir_confirmed),
1518 g_signal_connect (fs->fileop_entry, "changed",
1519 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1522 gtk_widget_grab_default (button);
1524 gtk_widget_show (dialog);
1528 gtk_file_selection_delete_file_response (GtkDialog *dialog,
1532 GtkFileSelection *fs = data;
1533 CompletionState *cmpl_state;
1536 gchar *sys_full_path;
1537 GError *error = NULL;
1540 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1542 if (response_id != GTK_RESPONSE_OK)
1544 gtk_widget_destroy (GTK_WIDGET (dialog));
1548 cmpl_state = (CompletionState*) fs->cmpl_state;
1549 path = cmpl_reference_position (cmpl_state);
1551 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1552 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1555 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1556 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1559 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1560 fs->fileop_file, error->message,
1561 _("It probably contains symbols not allowed in filenames."));
1563 gtk_file_selection_fileop_error (fs, buf);
1564 g_error_free (error);
1568 if (unlink (sys_full_path) < 0)
1570 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1571 fs->fileop_file, g_strerror (errno));
1572 gtk_file_selection_fileop_error (fs, buf);
1577 g_free (sys_full_path);
1579 gtk_widget_destroy (fs->fileop_dialog);
1580 gtk_file_selection_populate (fs, "", FALSE, TRUE);
1584 gtk_file_selection_delete_file (GtkWidget *widget,
1587 GtkFileSelection *fs = data;
1589 const gchar *filename;
1591 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1593 if (fs->fileop_dialog)
1596 #ifdef G_WITH_CYGWIN
1597 translate_win32_path (fs);
1600 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1601 if (strlen (filename) < 1)
1604 g_free (fs->fileop_file);
1605 fs->fileop_file = g_strdup (filename);
1608 fs->fileop_dialog = dialog =
1609 gtk_message_dialog_new (GTK_WINDOW (fs),
1610 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1611 GTK_MESSAGE_QUESTION,
1613 _("Really delete file \"%s\" ?"), filename);
1615 g_signal_connect (dialog, "destroy",
1616 G_CALLBACK (gtk_file_selection_fileop_destroy),
1618 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1619 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1622 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1623 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1624 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1627 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1629 g_signal_connect (dialog, "response",
1630 G_CALLBACK (gtk_file_selection_delete_file_response),
1633 gtk_widget_show (dialog);
1637 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1640 GtkFileSelection *fs = data;
1644 gchar *new_filename;
1645 gchar *old_filename;
1646 gchar *sys_new_filename;
1647 gchar *sys_old_filename;
1648 CompletionState *cmpl_state;
1649 GError *error = NULL;
1651 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1653 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1654 cmpl_state = (CompletionState*) fs->cmpl_state;
1655 path = cmpl_reference_position (cmpl_state);
1657 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1658 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1660 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1663 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1664 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1666 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1667 new_filename, error->message,
1668 _("You probably used symbols not allowed in filenames."));
1669 gtk_file_selection_fileop_error (fs, buf);
1670 g_error_free (error);
1674 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1677 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1678 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1680 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1681 old_filename, error->message,
1682 _("It probably contains symbols not allowed in filenames."));
1683 gtk_file_selection_fileop_error (fs, buf);
1684 g_error_free (error);
1688 if (rename (sys_old_filename, sys_new_filename) < 0)
1690 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1691 sys_old_filename, sys_new_filename,
1692 g_strerror (errno));
1693 gtk_file_selection_fileop_error (fs, buf);
1697 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1698 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1701 g_free (sys_old_filename);
1704 g_free (new_filename);
1705 g_free (old_filename);
1706 g_free (sys_new_filename);
1708 gtk_widget_destroy (fs->fileop_dialog);
1712 gtk_file_selection_rename_file (GtkWidget *widget,
1715 GtkFileSelection *fs = data;
1722 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1724 if (fs->fileop_dialog)
1727 g_free (fs->fileop_file);
1728 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1729 if (strlen (fs->fileop_file) < 1)
1733 fs->fileop_dialog = dialog = gtk_dialog_new ();
1734 g_signal_connect (dialog, "destroy",
1735 G_CALLBACK (gtk_file_selection_fileop_destroy),
1737 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1738 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1739 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1741 /* If file dialog is grabbed, grab option dialog */
1742 /* When option dialog closed, file dialog will be grabbed again */
1743 if (GTK_WINDOW (fs)->modal)
1744 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1746 vbox = gtk_vbox_new (FALSE, 0);
1747 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1748 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1750 gtk_widget_show(vbox);
1752 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1753 label = gtk_label_new (buf);
1754 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1755 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1756 gtk_widget_show (label);
1759 /* New filename entry */
1760 fs->fileop_entry = gtk_entry_new ();
1761 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1763 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1764 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1765 gtk_widget_show (fs->fileop_entry);
1767 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1768 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1769 0, strlen (fs->fileop_file));
1772 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1773 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1774 g_signal_connect_swapped (button, "clicked",
1775 G_CALLBACK (gtk_widget_destroy),
1778 gtk_widget_grab_focus (fs->fileop_entry);
1780 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1781 _("_Rename"), GTK_RESPONSE_OK);
1782 g_signal_connect (button, "clicked",
1783 G_CALLBACK (gtk_file_selection_rename_file_confirmed),
1785 g_signal_connect (fs->fileop_entry, "changed",
1786 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1789 gtk_widget_grab_default (button);
1791 gtk_widget_show (dialog);
1795 gtk_file_selection_insert_text (GtkWidget *widget,
1796 const gchar *new_text,
1797 gint new_text_length,
1803 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1807 gdk_display_beep (gtk_widget_get_display (widget));
1808 g_signal_stop_emission_by_name (widget, "insert_text");
1818 gtk_file_selection_update_fileops (GtkFileSelection *fs)
1822 if (!fs->selection_entry)
1825 sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1827 if (fs->fileop_del_file)
1828 gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1830 if (fs->fileop_ren_file)
1831 gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1835 gtk_file_selection_key_press (GtkWidget *widget,
1839 GtkFileSelection *fs;
1842 g_return_val_if_fail (widget != NULL, FALSE);
1843 g_return_val_if_fail (event != NULL, FALSE);
1845 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1846 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1848 fs = GTK_FILE_SELECTION (user_data);
1849 #ifdef G_WITH_CYGWIN
1850 translate_win32_path (fs);
1852 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1854 gtk_file_selection_populate (fs, text, TRUE, TRUE);
1865 gtk_file_selection_history_callback (GtkWidget *widget,
1868 GtkFileSelection *fs = data;
1869 HistoryCallbackArg *callback_arg;
1872 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1874 list = fs->history_list;
1877 callback_arg = list->data;
1879 if (callback_arg->menu_item == widget)
1881 gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1890 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1891 gchar *current_directory)
1893 HistoryCallbackArg *callback_arg;
1894 GtkWidget *menu_item;
1900 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1901 g_return_if_fail (current_directory != NULL);
1903 list = fs->history_list;
1905 if (fs->history_menu)
1908 callback_arg = list->data;
1909 g_free (callback_arg->directory);
1910 g_free (callback_arg);
1913 g_list_free (fs->history_list);
1914 fs->history_list = NULL;
1916 gtk_widget_destroy (fs->history_menu);
1919 fs->history_menu = gtk_menu_new ();
1921 current_dir = g_strdup (current_directory);
1923 dir_len = strlen (current_dir);
1925 for (i = dir_len; i >= 0; i--)
1927 /* the i == dir_len is to catch the full path for the first
1929 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1931 /* another small hack to catch the full path */
1933 current_dir[i + 1] = '\0';
1934 #ifdef G_WITH_CYGWIN
1935 if (!strcmp (current_dir, "//"))
1938 menu_item = gtk_menu_item_new_with_label (current_dir);
1940 callback_arg = g_new (HistoryCallbackArg, 1);
1941 callback_arg->menu_item = menu_item;
1943 /* since the autocompletion gets confused if you don't
1944 * supply a trailing '/' on a dir entry, set the full
1945 * (current) path to "" which just refreshes the filesel */
1948 callback_arg->directory = g_strdup ("");
1952 callback_arg->directory = g_strdup (current_dir);
1955 fs->history_list = g_list_append (fs->history_list, callback_arg);
1957 g_signal_connect (menu_item, "activate",
1958 G_CALLBACK (gtk_file_selection_history_callback),
1960 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1961 gtk_widget_show (menu_item);
1965 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1967 g_free (current_dir);
1971 get_real_filename (gchar *filename,
1974 #ifdef G_WITH_CYGWIN
1975 /* Check to see if the selection was a drive selector */
1976 if (isalpha (filename[0]) && (filename[1] == ':'))
1978 gchar temp_filename[MAX_PATH];
1981 cygwin_conv_to_posix_path (filename, temp_filename);
1983 /* we need trailing '/'. */
1984 len = strlen (temp_filename);
1985 if (len > 0 && temp_filename[len-1] != '/')
1987 temp_filename[len] = '/';
1988 temp_filename[len+1] = '\0';
1994 return g_strdup (temp_filename);
1996 #endif /* G_WITH_CYGWIN */
2001 gtk_file_selection_file_activate (GtkTreeView *tree_view,
2003 GtkTreeViewColumn *column,
2006 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2007 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2011 gtk_tree_model_get_iter (model, &iter, path);
2012 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
2013 filename = get_real_filename (filename, TRUE);
2014 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2015 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
2021 gtk_file_selection_dir_activate (GtkTreeView *tree_view,
2023 GtkTreeViewColumn *column,
2026 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2027 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2031 gtk_tree_model_get_iter (model, &iter, path);
2032 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
2033 filename = get_real_filename (filename, TRUE);
2034 gtk_file_selection_populate (fs, filename, FALSE, FALSE);
2038 #ifdef G_PLATFORM_WIN32
2041 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
2045 char formatBuffer[128];
2048 /* Get the drives string */
2049 GetLogicalDriveStrings (sizeof (buffer), buffer);
2051 /* Add the drives as necessary */
2053 while (*textPtr != '\0')
2055 /* Ignore floppies (?) */
2056 if (GetDriveType (textPtr) != DRIVE_REMOVABLE)
2058 /* Build the actual displayable string */
2059 g_snprintf (formatBuffer, sizeof (formatBuffer), "%c:\\", toupper (textPtr[0]));
2061 /* Add to the list */
2062 gtk_list_store_append (model, &iter);
2063 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
2065 textPtr += (strlen (textPtr) + 1);
2071 escape_underscores (const gchar *str)
2073 GString *result = g_string_new (NULL);
2077 g_string_append_c (result, '_');
2079 g_string_append_c (result, *str);
2083 return g_string_free (result, FALSE);
2087 gtk_file_selection_populate (GtkFileSelection *fs,
2089 gboolean try_complete,
2090 gboolean reset_entry)
2092 CompletionState *cmpl_state;
2093 PossibleCompletion* poss;
2095 GtkListStore *dir_model;
2096 GtkListStore *file_model;
2098 gchar* rem_path = rel_path;
2100 gint did_recurse = FALSE;
2101 gint possible_count = 0;
2102 gint selection_index = -1;
2104 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
2106 cmpl_state = (CompletionState*) fs->cmpl_state;
2107 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2109 if (!cmpl_state_okay (cmpl_state))
2111 /* Something went wrong. */
2112 gtk_file_selection_abort (fs);
2116 g_assert (cmpl_state->reference_dir);
2118 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2119 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2121 gtk_list_store_clear (dir_model);
2122 gtk_list_store_clear (file_model);
2124 /* Set the dir list to include ./ and ../ */
2125 gtk_list_store_append (dir_model, &iter);
2126 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2127 gtk_list_store_append (dir_model, &iter);
2128 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2132 if (cmpl_is_a_completion (poss))
2134 possible_count += 1;
2136 filename = cmpl_this_completion (poss);
2138 if (cmpl_is_directory (poss))
2140 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2141 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2143 gtk_list_store_append (dir_model, &iter);
2144 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2149 gtk_list_store_append (file_model, &iter);
2150 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2154 poss = cmpl_next_completion (cmpl_state);
2157 #ifdef G_PLATFORM_WIN32
2158 /* For Windows, add drives as potential selections */
2159 win32_gtk_add_drives_to_dir_list (dir_model);
2162 /* File lists are set. */
2164 g_assert (cmpl_state->reference_dir);
2169 /* User is trying to complete filenames, so advance the user's input
2170 * string to the updated_text, which is the common leading substring
2171 * of all possible completions, and if its a directory attempt
2172 * attempt completions in it. */
2174 if (cmpl_updated_text (cmpl_state)[0])
2177 if (cmpl_updated_dir (cmpl_state))
2179 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2183 gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2189 if (fs->selection_entry)
2190 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2191 cmpl_updated_text (cmpl_state));
2196 selection_index = cmpl_last_valid_char (cmpl_state) -
2197 (strlen (rel_path) - strlen (rem_path));
2198 if (fs->selection_entry)
2199 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2202 else if (reset_entry)
2204 if (fs->selection_entry)
2205 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2210 if (fs->selection_entry)
2211 gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry),
2214 if (fs->selection_entry)
2216 char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2217 sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2220 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2224 if (fs->history_pulldown)
2226 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2233 gtk_file_selection_abort (GtkFileSelection *fs)
2237 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2239 /* BEEP gdk_beep(); */
2241 if (fs->selection_entry)
2242 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2246 * gtk_file_selection_set_select_multiple:
2247 * @filesel: a #GtkFileSelection
2248 * @select_multiple: whether or not the user is allowed to select multiple
2249 * files in the file list.
2251 * Sets whether the user is allowed to select multiple files in the file list.
2252 * Use gtk_file_selection_get_selections () to get the list of selected files.
2255 gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2256 gboolean select_multiple)
2258 GtkTreeSelection *sel;
2259 GtkSelectionMode mode;
2261 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2263 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2265 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2267 if (mode != gtk_tree_selection_get_mode (sel))
2269 gtk_tree_selection_set_mode (sel, mode);
2271 g_object_notify (G_OBJECT (filesel), "select-multiple");
2276 * gtk_file_selection_get_select_multiple:
2277 * @filesel: a #GtkFileSelection
2279 * Determines whether or not the user is allowed to select multiple files in
2280 * the file list. See gtk_file_selection_set_select_multiple().
2282 * Return value: %TRUE if the user is allowed to select multiple files in the
2286 gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2288 GtkTreeSelection *sel;
2290 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2292 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2293 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2297 multiple_changed_foreach (GtkTreeModel *model,
2302 GPtrArray *names = data;
2305 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2307 g_ptr_array_add (names, filename);
2311 free_selected_names (GPtrArray *names)
2315 for (i = 0; i < names->len; i++)
2316 g_free (g_ptr_array_index (names, i));
2318 g_ptr_array_free (names, TRUE);
2322 gtk_file_selection_file_changed (GtkTreeSelection *selection,
2325 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2326 GPtrArray *new_names;
2331 new_names = g_ptr_array_sized_new (8);
2333 gtk_tree_selection_selected_foreach (selection,
2334 multiple_changed_foreach,
2337 /* nothing selected */
2338 if (new_names->len == 0)
2340 g_ptr_array_free (new_names, TRUE);
2342 if (fs->selected_names != NULL)
2344 free_selected_names (fs->selected_names);
2345 fs->selected_names = NULL;
2348 goto maybe_clear_entry;
2351 if (new_names->len != 1)
2353 GPtrArray *old_names = fs->selected_names;
2355 if (old_names != NULL)
2357 /* A common case is selecting a range of files from top to bottom,
2358 * so quickly check for that to avoid looping over the entire list
2360 if (compare_utf8_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2361 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2362 index = new_names->len - 1;
2365 gint i = 0, j = 0, cmp;
2367 /* do a quick diff, stopping at the first file not in the
2370 while (i < old_names->len && j < new_names->len)
2372 cmp = compare_utf8_filenames (g_ptr_array_index (old_names, i),
2373 g_ptr_array_index (new_names, j));
2390 /* we ran off the end of the old list */
2391 if (index == -1 && i < new_names->len)
2397 /* A phantom anchor still exists at the point where the last item
2398 * was selected, which is used for subsequent range selections.
2399 * So search up from there.
2401 if (fs->last_selected &&
2402 compare_utf8_filenames (fs->last_selected,
2403 g_ptr_array_index (new_names, 0)) == 0)
2404 index = new_names->len - 1;
2412 if (fs->selected_names != NULL)
2413 free_selected_names (fs->selected_names);
2415 fs->selected_names = new_names;
2419 if (fs->last_selected != NULL)
2420 g_free (fs->last_selected);
2422 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2423 filename = get_real_filename (fs->last_selected, FALSE);
2425 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2427 if (filename != fs->last_selected)
2435 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2436 if ((entry != NULL) && (fs->last_selected != NULL) &&
2437 (compare_utf8_filenames (entry, fs->last_selected) == 0))
2438 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2442 * gtk_file_selection_get_selections:
2443 * @filesel: a #GtkFileSelection
2445 * Retrieves the list of file selections the user has made in the dialog box.
2446 * This function is intended for use when the user can select multiple files
2447 * in the file list. The first file in the list is equivalent to what
2448 * gtk_file_selection_get_filename() would return.
2450 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2451 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2452 * g_filename_to_utf8() on each string.
2454 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2455 * g_strfreev() to free it.
2458 gtk_file_selection_get_selections (GtkFileSelection *filesel)
2462 gchar *filename, *dirname;
2463 gchar *current, *buf;
2465 gboolean unselected_entry;
2467 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2469 filename = g_strdup (gtk_file_selection_get_filename (filesel));
2471 if (strlen (filename) == 0)
2477 names = filesel->selected_names;
2480 selections = g_new (gchar *, names->len + 2);
2482 selections = g_new (gchar *, 2);
2485 unselected_entry = TRUE;
2489 dirname = g_path_get_dirname (filename);
2491 for (i = 0; i < names->len; i++)
2493 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2495 current = g_build_filename (dirname, buf, NULL);
2498 selections[count++] = current;
2500 if (unselected_entry && compare_sys_filenames (current, filename) == 0)
2501 unselected_entry = FALSE;
2507 if (unselected_entry)
2508 selections[count++] = filename;
2512 selections[count] = NULL;
2517 /**********************************************************************/
2518 /* External Interface */
2519 /**********************************************************************/
2521 /* The four completion state selectors
2524 cmpl_updated_text (CompletionState *cmpl_state)
2526 return cmpl_state->updated_text;
2530 cmpl_updated_dir (CompletionState *cmpl_state)
2532 return cmpl_state->re_complete;
2536 cmpl_reference_position (CompletionState *cmpl_state)
2538 return cmpl_state->reference_dir->fullname;
2542 cmpl_last_valid_char (CompletionState *cmpl_state)
2544 return cmpl_state->last_valid_char;
2548 cmpl_completion_fullname (const gchar *text,
2549 CompletionState *cmpl_state)
2551 static const char nothing[2] = "";
2553 if (!cmpl_state_okay (cmpl_state))
2557 else if (g_path_is_absolute (text))
2559 strcpy (cmpl_state->updated_text, text);
2562 else if (text[0] == '~')
2567 dir = open_user_dir (text, cmpl_state);
2571 /* spencer says just return ~something, so
2572 * for now just do it. */
2573 strcpy (cmpl_state->updated_text, text);
2578 strcpy (cmpl_state->updated_text, dir->fullname);
2580 slash = strchr (text, G_DIR_SEPARATOR);
2583 strcat (cmpl_state->updated_text, slash);
2589 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
2590 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
2591 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
2592 strcat (cmpl_state->updated_text, text);
2595 return cmpl_state->updated_text;
2598 /* The three completion selectors
2601 cmpl_this_completion (PossibleCompletion* pc)
2607 cmpl_is_directory (PossibleCompletion* pc)
2609 return pc->is_directory;
2613 cmpl_is_a_completion (PossibleCompletion* pc)
2615 return pc->is_a_completion;
2618 /**********************************************************************/
2619 /* Construction, deletion */
2620 /**********************************************************************/
2622 static CompletionState*
2623 cmpl_init_state (void)
2625 gchar *sys_getcwd_buf;
2627 CompletionState *new_state;
2629 new_state = g_new (CompletionState, 1);
2631 /* g_get_current_dir() returns a string in the "system" charset */
2632 sys_getcwd_buf = g_get_current_dir ();
2633 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
2634 g_free (sys_getcwd_buf);
2638 new_state->reference_dir = NULL;
2639 new_state->completion_dir = NULL;
2640 new_state->active_completion_dir = NULL;
2641 new_state->directory_storage = NULL;
2642 new_state->directory_sent_storage = NULL;
2643 new_state->last_valid_char = 0;
2644 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2645 new_state->updated_text_alloc = MAXPATHLEN;
2646 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2647 new_state->the_completion.text_alloc = MAXPATHLEN;
2648 new_state->user_dir_name_buffer = NULL;
2649 new_state->user_directories = NULL;
2651 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2653 if (!new_state->reference_dir)
2655 /* Directories changing from underneath us, grumble */
2656 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2665 cmpl_free_dir_list (GList* dp0)
2671 free_dir (dp->data);
2679 cmpl_free_dir_sent_list (GList* dp0)
2685 free_dir_sent (dp->data);
2693 cmpl_free_state (CompletionState* cmpl_state)
2695 g_return_if_fail (cmpl_state != NULL);
2697 cmpl_free_dir_list (cmpl_state->directory_storage);
2698 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2700 if (cmpl_state->user_dir_name_buffer)
2701 g_free (cmpl_state->user_dir_name_buffer);
2702 if (cmpl_state->user_directories)
2703 g_free (cmpl_state->user_directories);
2704 if (cmpl_state->the_completion.text)
2705 g_free (cmpl_state->the_completion.text);
2706 if (cmpl_state->updated_text)
2707 g_free (cmpl_state->updated_text);
2709 g_free (cmpl_state);
2713 free_dir (CompletionDir* dir)
2715 g_free (dir->cmpl_text);
2716 g_free (dir->fullname);
2721 free_dir_sent (CompletionDirSent* sent)
2724 for (i = 0; i < sent->entry_count; i++)
2726 g_free (sent->entries[i].entry_name);
2727 g_free (sent->entries[i].sort_key);
2729 g_free (sent->entries);
2734 prune_memory_usage (CompletionState *cmpl_state)
2736 GList* cdsl = cmpl_state->directory_sent_storage;
2737 GList* cdl = cmpl_state->directory_storage;
2741 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2746 cmpl_free_dir_sent_list (cdsl->next);
2750 cmpl_state->directory_storage = NULL;
2753 if (cdl->data == cmpl_state->reference_dir)
2754 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2756 free_dir (cdl->data);
2763 /**********************************************************************/
2764 /* The main entrances. */
2765 /**********************************************************************/
2767 static PossibleCompletion*
2768 cmpl_completion_matches (gchar *text_to_complete,
2769 gchar **remaining_text,
2770 CompletionState *cmpl_state)
2775 PossibleCompletion *poss;
2777 prune_memory_usage (cmpl_state);
2779 g_assert (text_to_complete != NULL);
2781 cmpl_state->user_completion_index = -1;
2782 cmpl_state->last_completion_text = text_to_complete;
2783 cmpl_state->the_completion.text[0] = 0;
2784 cmpl_state->last_valid_char = 0;
2785 cmpl_state->updated_text_len = -1;
2786 cmpl_state->updated_text[0] = 0;
2787 cmpl_state->re_complete = FALSE;
2790 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2792 if (text_to_complete[0] == '~' && !first_slash)
2794 /* Text starts with ~ and there is no slash, show all the
2795 * home directory completions.
2797 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2799 update_cmpl (poss, cmpl_state);
2804 cmpl_state->reference_dir =
2805 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2807 if (!cmpl_state->reference_dir)
2810 cmpl_state->completion_dir =
2811 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2813 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2815 if (!cmpl_state->completion_dir)
2818 cmpl_state->completion_dir->cmpl_index = -1;
2819 cmpl_state->completion_dir->cmpl_parent = NULL;
2820 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2822 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2824 cmpl_state->reference_dir = cmpl_state->completion_dir;
2826 poss = attempt_file_completion (cmpl_state);
2828 update_cmpl (poss, cmpl_state);
2833 static PossibleCompletion*
2834 cmpl_next_completion (CompletionState* cmpl_state)
2836 PossibleCompletion* poss = NULL;
2838 cmpl_state->the_completion.text[0] = 0;
2841 if (cmpl_state->user_completion_index >= 0)
2842 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2844 poss = attempt_file_completion (cmpl_state);
2846 poss = attempt_file_completion (cmpl_state);
2849 update_cmpl (poss, cmpl_state);
2854 /**********************************************************************/
2855 /* Directory Operations */
2856 /**********************************************************************/
2858 /* Open the directory where completion will begin from, if possible. */
2859 static CompletionDir*
2860 open_ref_dir (gchar *text_to_complete,
2861 gchar **remaining_text,
2862 CompletionState *cmpl_state)
2865 CompletionDir *new_dir;
2867 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2869 #ifdef G_WITH_CYGWIN
2870 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2873 g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2875 new_dir = open_dir (root_dir, cmpl_state);
2878 *remaining_text = text_to_complete + 4;
2886 else if (text_to_complete[0] == '~')
2888 new_dir = open_user_dir (text_to_complete, cmpl_state);
2893 *remaining_text = first_slash + 1;
2895 *remaining_text = text_to_complete + strlen (text_to_complete);
2903 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2905 gchar *tmp = g_strdup (text_to_complete);
2909 while (*p && *p != '*' && *p != '?')
2913 p = strrchr (tmp, G_DIR_SEPARATOR);
2916 if (p + 1 == g_path_skip_root (tmp))
2920 new_dir = open_dir (tmp, cmpl_state);
2923 *remaining_text = text_to_complete +
2924 ((p == g_path_skip_root (tmp)) ? (p - tmp) : (p + 1 - tmp));
2928 /* If no possible candidates, use the cwd */
2929 gchar *sys_curdir = g_get_current_dir ();
2930 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2932 g_free (sys_curdir);
2934 new_dir = open_dir (utf8_curdir, cmpl_state);
2937 *remaining_text = text_to_complete;
2939 g_free (utf8_curdir);
2946 *remaining_text = text_to_complete;
2948 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2953 new_dir->cmpl_index = -1;
2954 new_dir->cmpl_parent = NULL;
2962 /* open a directory by user name */
2963 static CompletionDir*
2964 open_user_dir (const gchar *text_to_complete,
2965 CompletionState *cmpl_state)
2967 CompletionDir *result;
2971 g_assert (text_to_complete && text_to_complete[0] == '~');
2973 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2976 cmp_len = first_slash - text_to_complete - 1;
2978 cmp_len = strlen (text_to_complete + 1);
2983 const gchar *homedir = g_get_home_dir ();
2984 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2987 result = open_dir (utf8_homedir, cmpl_state);
2991 g_free (utf8_homedir);
2996 gchar* copy = g_new (char, cmp_len + 1);
3000 strncpy (copy, text_to_complete + 1, cmp_len);
3002 pwd = getpwnam (copy);
3009 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
3010 result = open_dir (utf8_dir, cmpl_state);
3018 /* open a directory relative the the current relative directory */
3019 static CompletionDir*
3020 open_relative_dir (gchar *dir_name,
3022 CompletionState *cmpl_state)
3024 CompletionDir *result;
3027 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
3028 g_string_assign (path, dir->fullname);
3030 if (dir->fullname_len > 1
3031 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
3032 g_string_append_c (path, G_DIR_SEPARATOR);
3033 g_string_append (path, dir_name);
3035 result = open_dir (path->str, cmpl_state);
3037 g_string_free (path, TRUE);
3042 /* after the cache lookup fails, really open a new directory */
3043 static CompletionDirSent*
3044 open_new_dir (gchar *dir_name,
3046 gboolean stat_subdirs)
3048 CompletionDirSent *sent;
3051 GError *error = NULL;
3052 gint entry_count = 0;
3055 struct stat ent_sbuf;
3057 gchar *sys_dir_name;
3059 sent = g_new (CompletionDirSent, 1);
3060 #ifndef G_PLATFORM_WIN32
3061 sent->mtime = sbuf->st_mtime;
3062 sent->inode = sbuf->st_ino;
3063 sent->device = sbuf->st_dev;
3065 path = g_string_sized_new (2*MAXPATHLEN + 10);
3067 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3070 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3074 directory = g_dir_open (sys_dir_name, 0, &error);
3077 cmpl_errno = error->code; /* ??? */
3078 g_free (sys_dir_name);
3082 while ((dirent = g_dir_read_name (directory)) != NULL)
3084 entry_count += 2; /* For ".",".." */
3086 sent->entries = g_new (CompletionDirEntry, entry_count);
3087 sent->entry_count = entry_count;
3089 g_dir_rewind (directory);
3091 for (i = 0; i < entry_count; i += 1)
3093 GError *error = NULL;
3101 dirent = g_dir_read_name (directory);
3102 if (!dirent) /* Directory changed */
3106 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3107 if (sent->entries[n_entries].entry_name == NULL
3108 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3110 gchar *escaped_str = g_strescape (dirent, NULL);
3111 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3112 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3114 error->message ? error->message : _("Invalid UTF-8"));
3115 g_free (escaped_str);
3116 g_clear_error (&error);
3119 g_clear_error (&error);
3121 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3123 g_string_assign (path, sys_dir_name);
3124 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3126 g_string_append_c (path, G_DIR_SEPARATOR);
3128 g_string_append (path, dirent);
3132 /* Here we know path->str is a "system charset" string */
3133 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3134 sent->entries[n_entries].is_dir = TRUE;
3136 /* stat may fail, and we don't mind, since it could be a
3137 * dangling symlink. */
3138 sent->entries[n_entries].is_dir = FALSE;
3141 sent->entries[n_entries].is_dir = 1;
3145 sent->entry_count = n_entries;
3147 g_free (sys_dir_name);
3148 g_string_free (path, TRUE);
3149 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3151 g_dir_close (directory);
3156 #ifndef G_PLATFORM_WIN32
3159 check_dir (gchar *dir_name,
3160 struct stat *result,
3161 gboolean *stat_subdirs)
3163 /* A list of directories that we know only contain other directories.
3164 * Trying to stat every file in these directories would be very
3171 struct stat statbuf;
3172 } no_stat_dirs[] = {
3173 { "/afs", FALSE, { 0 } },
3174 { "/net", FALSE, { 0 } }
3177 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3178 static gboolean initialized = FALSE;
3179 gchar *sys_dir_name;
3185 for (i = 0; i < n_no_stat_dirs; i++)
3187 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3188 no_stat_dirs[i].present = TRUE;
3192 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3195 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3199 if (stat (sys_dir_name, result) < 0)
3201 g_free (sys_dir_name);
3205 g_free (sys_dir_name);
3207 *stat_subdirs = TRUE;
3208 for (i = 0; i < n_no_stat_dirs; i++)
3210 if (no_stat_dirs[i].present &&
3211 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3212 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3214 *stat_subdirs = FALSE;
3224 /* open a directory by absolute pathname */
3225 static CompletionDir*
3226 open_dir (gchar *dir_name,
3227 CompletionState *cmpl_state)
3229 #ifndef G_PLATFORM_WIN32
3231 gboolean stat_subdirs;
3234 CompletionDirSent *sent;
3236 #ifndef G_PLATFORM_WIN32
3237 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3240 cdsl = cmpl_state->directory_sent_storage;
3246 if (sent->inode == sbuf.st_ino &&
3247 sent->mtime == sbuf.st_mtime &&
3248 sent->device == sbuf.st_dev)
3249 return attach_dir (sent, dir_name, cmpl_state);
3254 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3256 sent = open_new_dir (dir_name, NULL, TRUE);
3261 cmpl_state->directory_sent_storage =
3262 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3264 return attach_dir (sent, dir_name, cmpl_state);
3270 static CompletionDir*
3271 attach_dir (CompletionDirSent *sent,
3273 CompletionState *cmpl_state)
3275 CompletionDir* new_dir;
3277 new_dir = g_new (CompletionDir, 1);
3279 cmpl_state->directory_storage =
3280 g_list_prepend (cmpl_state->directory_storage, new_dir);
3282 new_dir->sent = sent;
3283 new_dir->fullname = g_strdup (dir_name);
3284 new_dir->fullname_len = strlen (dir_name);
3285 new_dir->cmpl_text = NULL;
3291 correct_dir_fullname (CompletionDir* cmpl_dir)
3293 gint length = strlen (cmpl_dir->fullname);
3294 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3295 gchar *sys_filename;
3298 /* Does it end with /. (\.) ? */
3300 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3302 /* Is it just the root directory (on a drive) ? */
3303 if (cmpl_dir->fullname + length - 2 == first_slash)
3305 cmpl_dir->fullname[length - 1] = 0;
3306 cmpl_dir->fullname_len = length - 1;
3311 cmpl_dir->fullname[length - 2] = 0;
3315 /* Ends with /./ (\.\)? */
3316 else if (length >= 3 &&
3317 strcmp (cmpl_dir->fullname + length - 3,
3318 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3319 cmpl_dir->fullname[length - 2] = 0;
3321 /* Ends with /.. (\..) ? */
3322 else if (length >= 3 &&
3323 strcmp (cmpl_dir->fullname + length - 3,
3324 G_DIR_SEPARATOR_S "..") == 0)
3326 /* Is it just /.. (X:\..)? */
3327 if (cmpl_dir->fullname + length - 3 == first_slash)
3329 cmpl_dir->fullname[length - 2] = 0;
3330 cmpl_dir->fullname_len = length - 2;
3334 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3337 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3341 if (stat (sys_filename, &sbuf) < 0)
3343 g_free (sys_filename);
3347 g_free (sys_filename);
3349 cmpl_dir->fullname[length - 3] = 0;
3351 if (!correct_parent (cmpl_dir, &sbuf))
3355 /* Ends with /../ (\..\)? */
3356 else if (length >= 4 &&
3357 strcmp (cmpl_dir->fullname + length - 4,
3358 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3360 /* Is it just /../ (X:\..\)? */
3361 if (cmpl_dir->fullname + length - 4 == first_slash)
3363 cmpl_dir->fullname[length - 3] = 0;
3364 cmpl_dir->fullname_len = length - 3;
3368 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3371 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3375 if (stat (sys_filename, &sbuf) < 0)
3377 g_free (sys_filename);
3381 g_free (sys_filename);
3383 cmpl_dir->fullname[length - 4] = 0;
3385 if (!correct_parent (cmpl_dir, &sbuf))
3389 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3395 correct_parent (CompletionDir *cmpl_dir,
3401 #ifndef G_PLATFORM_WIN32
3404 gchar *sys_filename;
3407 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3408 g_assert (last_slash);
3409 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3411 /* Clever (?) way to check for top-level directory that works also on
3412 * Win32, where there is a drive letter and colon prefixed...
3414 if (last_slash != first_slash)
3424 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3427 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3429 last_slash[0] = G_DIR_SEPARATOR;
3433 if (stat (sys_filename, &parbuf) < 0)
3435 g_free (sys_filename);
3438 last_slash[0] = G_DIR_SEPARATOR;
3441 g_free (sys_filename);
3443 #ifndef G_PLATFORM_WIN32 /* No inode numbers on Win32 */
3444 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3445 /* it wasn't a link */
3451 last_slash[0] = G_DIR_SEPARATOR;
3453 /* it was a link, have to figure it out the hard way */
3455 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3460 g_free (cmpl_dir->fullname);
3462 cmpl_dir->fullname = new_name;
3468 #ifndef G_PLATFORM_WIN32
3471 find_parent_dir_fullname (gchar* dirname)
3473 gchar *sys_orig_dir;
3478 sys_orig_dir = g_get_current_dir ();
3479 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3482 g_free (sys_orig_dir);
3483 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3487 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3490 chdir (sys_orig_dir);
3491 g_free (sys_dirname);
3492 g_free (sys_orig_dir);
3495 g_free (sys_dirname);
3497 sys_cwd = g_get_current_dir ();
3498 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3501 if (chdir (sys_orig_dir) != 0)
3504 g_free (sys_orig_dir);
3508 g_free (sys_orig_dir);
3514 /**********************************************************************/
3515 /* Completion Operations */
3516 /**********************************************************************/
3520 static PossibleCompletion*
3521 attempt_homedir_completion (gchar *text_to_complete,
3522 CompletionState *cmpl_state)
3526 if (!cmpl_state->user_dir_name_buffer &&
3527 !get_pwdb (cmpl_state))
3529 length = strlen (text_to_complete) - 1;
3531 cmpl_state->user_completion_index += 1;
3533 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3535 index = first_diff_index (text_to_complete + 1,
3536 cmpl_state->user_directories
3537 [cmpl_state->user_completion_index].login);
3544 if (cmpl_state->last_valid_char < (index + 1))
3545 cmpl_state->last_valid_char = index + 1;
3546 cmpl_state->user_completion_index += 1;
3550 cmpl_state->the_completion.is_a_completion = 1;
3551 cmpl_state->the_completion.is_directory = TRUE;
3553 append_completion_text ("~", cmpl_state);
3555 append_completion_text (cmpl_state->
3556 user_directories[cmpl_state->user_completion_index].login,
3559 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3562 if (text_to_complete[1]
3563 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3565 cmpl_state->user_completion_index = -1;
3570 cmpl_state->user_completion_index += 1;
3571 cmpl_state->the_completion.is_a_completion = 1;
3572 cmpl_state->the_completion.is_directory = TRUE;
3574 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3580 #ifdef G_PLATFORM_WIN32
3581 /* FIXME: determine whether we should casefold all Unicode letters
3582 * here, too (and in in first_diff_index() walk through the strings with
3583 * g_utf8_next_char()), or if this folding isn't actually needed at
3586 #define FOLD(c) (tolower(c))
3591 /* returns the index (>= 0) of the first differing character,
3592 * PATTERN_MATCH if the completion matches */
3594 first_diff_index (gchar *pat,
3599 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3609 return PATTERN_MATCH;
3612 static PossibleCompletion*
3613 append_completion_text (gchar *text,
3614 CompletionState *cmpl_state)
3618 if (!cmpl_state->the_completion.text)
3621 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3623 if (cmpl_state->the_completion.text_alloc > len)
3625 strcat (cmpl_state->the_completion.text, text);
3626 return &cmpl_state->the_completion;
3632 cmpl_state->the_completion.text_alloc = i;
3634 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3636 if (!cmpl_state->the_completion.text)
3640 strcat (cmpl_state->the_completion.text, text);
3641 return &cmpl_state->the_completion;
3645 static CompletionDir*
3646 find_completion_dir (gchar *text_to_complete,
3647 gchar **remaining_text,
3648 CompletionState *cmpl_state)
3650 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3651 CompletionDir* dir = cmpl_state->reference_dir;
3652 CompletionDir* next;
3653 *remaining_text = text_to_complete;
3657 gint len = first_slash - *remaining_text;
3659 gchar *found_name = NULL; /* Quiet gcc */
3661 gchar* pat_buf = g_new (gchar, len + 1);
3663 strncpy (pat_buf, *remaining_text, len);
3666 for (i = 0; i < dir->sent->entry_count; i += 1)
3668 if (dir->sent->entries[i].is_dir &&
3669 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name))
3679 found_name = dir->sent->entries[i].entry_name;
3686 /* Perhaps we are trying to open an automount directory */
3687 found_name = pat_buf;
3690 next = open_relative_dir (found_name, dir, cmpl_state);
3698 next->cmpl_parent = dir;
3702 if (!correct_dir_fullname (dir))
3708 *remaining_text = first_slash + 1;
3709 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3718 update_cmpl (PossibleCompletion *poss,
3719 CompletionState *cmpl_state)
3723 if (!poss || !cmpl_is_a_completion (poss))
3726 cmpl_len = strlen (cmpl_this_completion (poss));
3728 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3730 cmpl_state->updated_text_alloc = 2*cmpl_len;
3731 cmpl_state->updated_text =
3732 (gchar*)g_realloc (cmpl_state->updated_text,
3733 cmpl_state->updated_text_alloc);
3736 if (cmpl_state->updated_text_len < 0)
3738 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3739 cmpl_state->updated_text_len = cmpl_len;
3740 cmpl_state->re_complete = cmpl_is_directory (poss);
3742 else if (cmpl_state->updated_text_len == 0)
3744 cmpl_state->re_complete = FALSE;
3749 first_diff_index (cmpl_state->updated_text,
3750 cmpl_this_completion (poss));
3752 cmpl_state->re_complete = FALSE;
3754 if (first_diff == PATTERN_MATCH)
3757 if (first_diff > cmpl_state->updated_text_len)
3758 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3760 cmpl_state->updated_text_len = first_diff;
3761 cmpl_state->updated_text[first_diff] = 0;
3765 static PossibleCompletion*
3766 attempt_file_completion (CompletionState *cmpl_state)
3768 gchar *pat_buf, *first_slash;
3769 CompletionDir *dir = cmpl_state->active_completion_dir;
3771 dir->cmpl_index += 1;
3773 if (dir->cmpl_index == dir->sent->entry_count)
3775 if (dir->cmpl_parent == NULL)
3777 cmpl_state->active_completion_dir = NULL;
3783 cmpl_state->active_completion_dir = dir->cmpl_parent;
3785 return attempt_file_completion (cmpl_state);
3789 g_assert (dir->cmpl_text);
3791 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3795 gint len = first_slash - dir->cmpl_text;
3797 pat_buf = g_new (gchar, len + 1);
3798 strncpy (pat_buf, dir->cmpl_text, len);
3803 gint len = strlen (dir->cmpl_text);
3805 pat_buf = g_new (gchar, len + 2);
3806 strcpy (pat_buf, dir->cmpl_text);
3807 /* Don't append a * if the user entered one herself.
3808 * This way one can complete *.h and don't get matches
3809 * on any .help files, for instance.
3811 if (strchr (pat_buf, '*') == NULL)
3812 strcpy (pat_buf + len, "*");
3817 if (dir->sent->entries[dir->cmpl_index].is_dir)
3819 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name))
3821 CompletionDir* new_dir;
3823 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3832 new_dir->cmpl_parent = dir;
3834 new_dir->cmpl_index = -1;
3835 new_dir->cmpl_text = g_strdup (first_slash + 1);
3837 cmpl_state->active_completion_dir = new_dir;
3840 return attempt_file_completion (cmpl_state);
3845 return attempt_file_completion (cmpl_state);
3851 return attempt_file_completion (cmpl_state);
3856 if (dir->cmpl_parent != NULL)
3858 append_completion_text (dir->fullname +
3859 strlen (cmpl_state->completion_dir->fullname) + 1,
3861 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3864 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3866 cmpl_state->the_completion.is_a_completion =
3867 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name);
3869 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3870 if (dir->sent->entries[dir->cmpl_index].is_dir)
3871 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3874 return &cmpl_state->the_completion;
3881 get_pwdb (CompletionState* cmpl_state)
3883 struct passwd *pwd_ptr;
3886 gint len = 0, i, count = 0;
3888 if (cmpl_state->user_dir_name_buffer)
3892 while ((pwd_ptr = getpwent ()) != NULL)
3894 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3895 len += strlen (utf8);
3897 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3898 len += strlen (utf8);
3906 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3907 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3908 cmpl_state->user_directories_len = count;
3910 buf_ptr = cmpl_state->user_dir_name_buffer;
3912 for (i = 0; i < count; i += 1)
3914 pwd_ptr = getpwent ();
3921 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3922 strcpy (buf_ptr, utf8);
3925 cmpl_state->user_directories[i].login = buf_ptr;
3927 buf_ptr += strlen (buf_ptr);
3930 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3931 strcpy (buf_ptr, utf8);
3934 cmpl_state->user_directories[i].homedir = buf_ptr;
3936 buf_ptr += strlen (buf_ptr);
3940 qsort (cmpl_state->user_directories,
3941 cmpl_state->user_directories_len,
3942 sizeof (CompletionUserDir),
3951 if (cmpl_state->user_dir_name_buffer)
3952 g_free (cmpl_state->user_dir_name_buffer);
3953 if (cmpl_state->user_directories)
3954 g_free (cmpl_state->user_directories);
3956 cmpl_state->user_dir_name_buffer = NULL;
3957 cmpl_state->user_directories = NULL;
3963 compare_user_dir (const void *a,
3966 return strcmp ((((CompletionUserDir*)a))->login,
3967 (((CompletionUserDir*)b))->login);
3973 compare_cmpl_dir (const void *a,
3977 return strcmp (((CompletionDirEntry*)a)->sort_key,
3978 (((CompletionDirEntry*)b))->sort_key);
3982 cmpl_state_okay (CompletionState* cmpl_state)
3984 return cmpl_state && cmpl_state->reference_dir;
3988 cmpl_strerror (gint err)
3990 if (err == CMPL_ERRNO_TOO_LONG)
3991 return _("Name too long");
3992 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3993 return _("Couldn't convert filename");
3995 return g_strerror (err);