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>
50 #include "gdk/gdkkeysyms.h"
51 #include "gtkbutton.h"
53 #include "gtkfilesel.h"
58 #include "gtklistitem.h"
60 #include "gtkscrolledwindow.h"
62 #include "gtksignal.h"
65 #include "gtkmenuitem.h"
66 #include "gtkoptionmenu.h"
68 #include "gtkdialog.h"
69 #include "gtkmessagedialog.h"
72 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
79 #define mkdir(p,m) _mkdir(p)
81 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
84 #endif /* G_OS_WIN32 */
86 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
88 #define DIR_LIST_WIDTH 180
89 #define DIR_LIST_HEIGHT 180
90 #define FILE_LIST_WIDTH 180
91 #define FILE_LIST_HEIGHT 180
93 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
94 * in here, since the rest of the code in the file does require some
99 # define MAXPATHLEN PATH_MAX
101 # define MAXPATHLEN 2048
105 /* I've put this here so it doesn't get confused with the
106 * file completion interface */
107 typedef struct _HistoryCallbackArg HistoryCallbackArg;
109 struct _HistoryCallbackArg
112 GtkWidget *menu_item;
116 typedef struct _CompletionState CompletionState;
117 typedef struct _CompletionDir CompletionDir;
118 typedef struct _CompletionDirSent CompletionDirSent;
119 typedef struct _CompletionDirEntry CompletionDirEntry;
120 typedef struct _CompletionUserDir CompletionUserDir;
121 typedef struct _PossibleCompletion PossibleCompletion;
123 /* Non-external file completion decls and structures */
125 /* A contant telling PRCS how many directories to cache. Its actually
126 * kept in a list, so the geometry isn't important. */
127 #define CMPL_DIRECTORY_CACHE_SIZE 10
129 /* A constant used to determine whether a substring was an exact
130 * match by first_diff_index()
132 #define PATTERN_MATCH -1
133 /* The arguments used by all fnmatch() calls below
135 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
137 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
138 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
140 /* This structure contains all the useful information about a directory
141 * for the purposes of filename completion. These structures are cached
142 * in the CompletionState struct. CompletionDir's are reference counted.
144 struct _CompletionDirSent
151 struct _CompletionDirEntry *entries;
154 struct _CompletionDir
156 CompletionDirSent *sent;
161 struct _CompletionDir *cmpl_parent;
166 /* This structure contains pairs of directory entry names with a flag saying
167 * whether or not they are a valid directory. NOTE: This information is used
168 * to provide the caller with information about whether to update its completions
169 * or try to open a file. Since directories are cached by the directory mtime,
170 * a symlink which points to an invalid file (which will not be a directory),
171 * will not be reevaluated if that file is created, unless the containing
172 * directory is touched. I consider this case to be worth ignoring (josh).
174 struct _CompletionDirEntry
180 struct _CompletionUserDir
186 struct _PossibleCompletion
188 /* accessible fields, all are accessed externally by functions
192 gint is_a_completion;
193 gboolean is_directory;
200 struct _CompletionState
202 gint last_valid_char;
204 gint updated_text_len;
205 gint updated_text_alloc;
206 gboolean re_complete;
208 gchar *user_dir_name_buffer;
209 gint user_directories_len;
211 gchar *last_completion_text;
213 gint user_completion_index; /* if >= 0, currently completing ~user */
215 struct _CompletionDir *completion_dir; /* directory completing from */
216 struct _CompletionDir *active_completion_dir;
218 struct _PossibleCompletion the_completion;
220 struct _CompletionDir *reference_dir; /* initial directory */
222 GList* directory_storage;
223 GList* directory_sent_storage;
225 struct _CompletionUserDir *user_directories;
234 /* File completion functions which would be external, were they used
235 * outside of this file.
238 static CompletionState* cmpl_init_state (void);
239 static void cmpl_free_state (CompletionState *cmpl_state);
240 static gint cmpl_state_okay (CompletionState* cmpl_state);
241 static gchar* cmpl_strerror (gint);
243 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
244 gchar **remaining_text,
245 CompletionState *cmpl_state);
247 /* Returns a name for consideration, possibly a completion, this name
248 * will be invalid after the next call to cmpl_next_completion.
250 static char* cmpl_this_completion (PossibleCompletion*);
252 /* True if this completion matches the given text. Otherwise, this
253 * output can be used to have a list of non-completions.
255 static gint cmpl_is_a_completion (PossibleCompletion*);
257 /* True if the completion is a directory
259 static gboolean cmpl_is_directory (PossibleCompletion*);
261 /* Obtains the next completion, or NULL
263 static PossibleCompletion* cmpl_next_completion (CompletionState*);
265 /* Updating completions: the return value of cmpl_updated_text() will
266 * be text_to_complete completed as much as possible after the most
267 * recent call to cmpl_completion_matches. For the present
268 * application, this is the suggested replacement for the user's input
269 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
272 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
274 /* After updating, to see if the completion was a directory, call
275 * this. If it was, you should consider re-calling completion_matches.
277 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
279 /* Current location: if using file completion, return the current
280 * directory, from which file completion begins. More specifically,
281 * the cwd concatenated with all exact completions up to the last
282 * directory delimiter('/').
284 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
286 /* backing up: if cmpl_completion_matches returns NULL, you may query
287 * the index of the last completable character into cmpl_updated_text.
289 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
291 /* When the user selects a non-directory, call cmpl_completion_fullname
292 * to get the full name of the selected file.
294 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
297 /* Directory operations. */
298 static CompletionDir* open_ref_dir (gchar* text_to_complete,
299 gchar** remaining_text,
300 CompletionState* cmpl_state);
301 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
302 static gboolean check_dir (gchar *dir_name,
304 gboolean *stat_subdirs);
306 static CompletionDir* open_dir (gchar* dir_name,
307 CompletionState* cmpl_state);
309 static CompletionDir* open_user_dir (gchar* text_to_complete,
310 CompletionState *cmpl_state);
312 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
313 CompletionState *cmpl_state);
314 static CompletionDirSent* open_new_dir (gchar* dir_name,
316 gboolean stat_subdirs);
317 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
318 static gint correct_parent (CompletionDir* cmpl_dir,
320 static gchar* find_parent_dir_fullname (gchar* dirname);
321 static CompletionDir* attach_dir (CompletionDirSent* sent,
323 CompletionState *cmpl_state);
324 static void free_dir_sent (CompletionDirSent* sent);
325 static void free_dir (CompletionDir *dir);
326 static void prune_memory_usage(CompletionState *cmpl_state);
328 /* Completion operations */
329 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
330 CompletionState *cmpl_state);
331 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
332 static CompletionDir* find_completion_dir(gchar* text_to_complete,
333 gchar** remaining_text,
334 CompletionState* cmpl_state);
335 static PossibleCompletion* append_completion_text(gchar* text,
336 CompletionState* cmpl_state);
338 static gint get_pwdb(CompletionState* cmpl_state);
339 static gint compare_user_dir(const void* a, const void* b);
341 static gint first_diff_index(gchar* pat, gchar* text);
342 static gint compare_cmpl_dir(const void* a, const void* b);
343 static void update_cmpl(PossibleCompletion* poss,
344 CompletionState* cmpl_state);
346 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
347 static void gtk_file_selection_set_property (GObject *object,
351 static void gtk_file_selection_get_property (GObject *object,
355 static void gtk_file_selection_init (GtkFileSelection *filesel);
356 static void gtk_file_selection_destroy (GtkObject *object);
357 static gint gtk_file_selection_key_press (GtkWidget *widget,
360 static gint gtk_file_selection_insert_text (GtkWidget *widget,
361 const gchar *new_text,
362 gint new_text_length,
366 static void gtk_file_selection_file_button (GtkWidget *widget,
369 GdkEventButton *bevent,
372 static void gtk_file_selection_dir_button (GtkWidget *widget,
375 GdkEventButton *bevent,
378 static void gtk_file_selection_populate (GtkFileSelection *fs,
381 static void gtk_file_selection_abort (GtkFileSelection *fs);
383 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
386 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
387 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
388 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
392 static GtkWindowClass *parent_class = NULL;
394 /* Saves errno when something cmpl does fails. */
395 static gint cmpl_errno;
399 * Take the path currently in the file selection
400 * entry field and translate as necessary from
401 * a WIN32 style to CYGWIN32 style path. For
402 * instance translate:
403 * x:\somepath\file.jpg
405 * //x/somepath/file.jpg
407 * Replace the path in the selection text field.
408 * Return a boolean value concerning whether a
409 * translation had to be made.
412 translate_win32_path (GtkFileSelection *filesel)
418 * Retrieve the current path
420 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
423 * Translate only if this looks like a DOS-ish
424 * path... First handle any drive letters.
426 if (isalpha (path[0]) && (path[1] == ':')) {
428 * This part kind of stinks... It isn't possible
429 * to know if there is enough space in the current
430 * string for the extra character required in this
431 * conversion. Assume that there isn't enough space
432 * and use the set function on the text field to
433 * set the newly created string.
435 gchar *newPath = g_strdup_printf ("//%c/%s", path[0], (path + 3));
436 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
443 * Now, replace backslashes with forward slashes
446 if (strchr (path, '\\'))
449 for (index = 0; path[index] != '\0'; index++)
450 if (path[index] == '\\')
461 gtk_file_selection_get_type (void)
463 static GtkType file_selection_type = 0;
465 if (!file_selection_type)
467 static const GtkTypeInfo filesel_info =
470 sizeof (GtkFileSelection),
471 sizeof (GtkFileSelectionClass),
472 (GtkClassInitFunc) gtk_file_selection_class_init,
473 (GtkObjectInitFunc) gtk_file_selection_init,
474 /* reserved_1 */ NULL,
475 /* reserved_2 */ NULL,
476 (GtkClassInitFunc) NULL,
479 file_selection_type = gtk_type_unique (GTK_TYPE_DIALOG, &filesel_info);
482 return file_selection_type;
486 gtk_file_selection_class_init (GtkFileSelectionClass *class)
488 GObjectClass *gobject_class;
489 GtkObjectClass *object_class;
491 gobject_class = (GObjectClass*) class;
492 object_class = (GtkObjectClass*) class;
494 parent_class = gtk_type_class (GTK_TYPE_DIALOG);
496 gobject_class->set_property = gtk_file_selection_set_property;
497 gobject_class->get_property = gtk_file_selection_get_property;
499 g_object_class_install_property (gobject_class,
501 g_param_spec_string ("filename",
503 _("The currently selected filename."),
505 G_PARAM_READABLE | G_PARAM_WRITABLE));
506 g_object_class_install_property (gobject_class,
508 g_param_spec_boolean ("show_fileops",
509 _("Show file operations"),
510 _("Whether buttons for creating/manipulating files should be displayed."),
514 object_class->destroy = gtk_file_selection_destroy;
517 static void gtk_file_selection_set_property (GObject *object,
522 GtkFileSelection *filesel;
524 filesel = GTK_FILE_SELECTION (object);
529 gtk_file_selection_set_filename (filesel,
530 g_value_get_string (value));
533 case PROP_SHOW_FILEOPS:
534 if (g_value_get_boolean (value))
535 gtk_file_selection_show_fileop_buttons (filesel);
537 gtk_file_selection_hide_fileop_buttons (filesel);
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 static void gtk_file_selection_get_property (GObject *object,
550 GtkFileSelection *filesel;
552 filesel = GTK_FILE_SELECTION (object);
557 g_value_set_string (value,
558 gtk_file_selection_get_filename(filesel));
561 case PROP_SHOW_FILEOPS:
562 /* This is a little bit hacky, but doing otherwise would require
563 * adding a field to the object.
565 g_value_set_boolean (value, (filesel->fileop_c_dir &&
566 filesel->fileop_del_file &&
567 filesel->fileop_ren_file));
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577 gtk_file_selection_init (GtkFileSelection *filesel)
579 GtkWidget *entry_vbox;
581 GtkWidget *list_hbox;
582 GtkWidget *confirm_area;
583 GtkWidget *pulldown_hbox;
584 GtkWidget *scrolled_win;
588 char *file_title [2];
590 dialog = GTK_DIALOG (filesel);
592 filesel->cmpl_state = cmpl_init_state ();
594 /* The dialog-sized vertical box */
595 filesel->main_vbox = dialog->vbox;
596 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
598 /* The horizontal box containing create, rename etc. buttons */
599 filesel->button_area = gtk_hbutton_box_new ();
600 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
601 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
602 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
604 gtk_widget_show (filesel->button_area);
606 gtk_file_selection_show_fileop_buttons (filesel);
608 /* hbox for pulldown menu */
609 pulldown_hbox = gtk_hbox_new (TRUE, 5);
610 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
611 gtk_widget_show (pulldown_hbox);
614 filesel->history_pulldown = gtk_option_menu_new ();
615 gtk_widget_show (filesel->history_pulldown);
616 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
619 /* The horizontal box containing the directory and file listboxes */
620 list_hbox = gtk_hbox_new (FALSE, 5);
621 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
622 gtk_widget_show (list_hbox);
624 /* The directories clist */
625 dir_title[0] = _("Directories");
627 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
628 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
629 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
630 (GtkSignalFunc) gtk_file_selection_dir_button,
632 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->dir_list), 0, TRUE);
633 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
635 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
636 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
637 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
638 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
639 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
640 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
641 gtk_widget_show (filesel->dir_list);
642 gtk_widget_show (scrolled_win);
644 /* The files clist */
645 file_title[0] = _("Files");
646 file_title[1] = NULL;
647 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
648 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
649 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
650 (GtkSignalFunc) gtk_file_selection_file_button,
652 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->file_list), 0, TRUE);
653 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
655 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
656 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
657 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
658 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
659 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
660 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
661 gtk_widget_show (filesel->file_list);
662 gtk_widget_show (scrolled_win);
664 /* action area for packing buttons into. */
665 filesel->action_area = gtk_hbox_new (TRUE, 0);
666 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
668 gtk_widget_show (filesel->action_area);
670 /* The OK/Cancel button area */
671 confirm_area = dialog->action_area;
674 filesel->ok_button = gtk_dialog_add_button (dialog,
678 gtk_widget_grab_default (filesel->ok_button);
680 /* The Cancel button */
681 filesel->cancel_button = gtk_dialog_add_button (dialog,
682 GTK_STOCK_BUTTON_CANCEL,
683 GTK_RESPONSE_CANCEL);
685 /* The selection entry widget */
686 entry_vbox = gtk_vbox_new (FALSE, 2);
687 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
688 gtk_widget_show (entry_vbox);
690 filesel->selection_text = label = gtk_label_new ("");
691 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
692 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
693 gtk_widget_show (label);
695 filesel->selection_entry = gtk_entry_new ();
696 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
697 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
698 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "insert_text",
699 (GtkSignalFunc) gtk_file_selection_insert_text, NULL);
700 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
701 (GtkSignalFunc) gtk_widget_grab_default,
702 GTK_OBJECT (filesel->ok_button));
703 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
704 (GtkSignalFunc) gtk_button_clicked,
705 GTK_OBJECT (filesel->ok_button));
706 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
707 gtk_widget_show (filesel->selection_entry);
709 if (!cmpl_state_okay (filesel->cmpl_state))
713 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
715 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
719 gtk_file_selection_populate (filesel, "", FALSE);
722 gtk_widget_grab_focus (filesel->selection_entry);
726 gtk_file_selection_new (const gchar *title)
728 GtkFileSelection *filesel;
730 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
731 gtk_window_set_title (GTK_WINDOW (filesel), title);
732 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
734 return GTK_WIDGET (filesel);
738 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
740 g_return_if_fail (filesel != NULL);
741 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
743 /* delete, create directory, and rename */
744 if (!filesel->fileop_c_dir)
746 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
747 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
748 (GtkSignalFunc) gtk_file_selection_create_dir,
750 gtk_box_pack_start (GTK_BOX (filesel->button_area),
751 filesel->fileop_c_dir, TRUE, TRUE, 0);
752 gtk_widget_show (filesel->fileop_c_dir);
755 if (!filesel->fileop_del_file)
757 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
758 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
759 (GtkSignalFunc) gtk_file_selection_delete_file,
761 gtk_box_pack_start (GTK_BOX (filesel->button_area),
762 filesel->fileop_del_file, TRUE, TRUE, 0);
763 gtk_widget_show (filesel->fileop_del_file);
766 if (!filesel->fileop_ren_file)
768 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
769 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
770 (GtkSignalFunc) gtk_file_selection_rename_file,
772 gtk_box_pack_start (GTK_BOX (filesel->button_area),
773 filesel->fileop_ren_file, TRUE, TRUE, 0);
774 gtk_widget_show (filesel->fileop_ren_file);
776 g_object_notify (G_OBJECT (filesel), "show_fileops");
777 gtk_widget_queue_resize (GTK_WIDGET (filesel));
781 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
783 g_return_if_fail (filesel != NULL);
784 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
786 if (filesel->fileop_ren_file)
788 gtk_widget_destroy (filesel->fileop_ren_file);
789 filesel->fileop_ren_file = NULL;
792 if (filesel->fileop_del_file)
794 gtk_widget_destroy (filesel->fileop_del_file);
795 filesel->fileop_del_file = NULL;
798 if (filesel->fileop_c_dir)
800 gtk_widget_destroy (filesel->fileop_c_dir);
801 filesel->fileop_c_dir = NULL;
803 g_object_notify (G_OBJECT (filesel), "show_fileops");
809 gtk_file_selection_set_filename (GtkFileSelection *filesel,
810 const gchar *filename)
813 const char *name, *last_slash;
815 g_return_if_fail (filesel != NULL);
816 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
817 g_return_if_fail (filename != NULL);
819 last_slash = strrchr (filename, G_DIR_SEPARATOR);
828 buf = g_strdup (filename);
829 buf[last_slash - filename + 1] = 0;
830 name = last_slash + 1;
833 gtk_file_selection_populate (filesel, buf, FALSE);
835 if (filesel->selection_entry)
836 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
838 g_object_notify (G_OBJECT (filesel), "filename");
842 * gtk_file_selection_get_filename:
843 * @filesel: a #GtkFileSelection
845 * This function returns the selected filename in the C runtime's
846 * multibyte string encoding, which may or may not be the same as that
847 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
848 * The returned string points to a statically allocated buffer and
849 * should be copied if you plan to keep it around.
851 * Return value: currently-selected filename in locale's encoding
853 G_CONST_RETURN gchar*
854 gtk_file_selection_get_filename (GtkFileSelection *filesel)
856 static gchar nothing[2] = "";
857 static gchar something[MAXPATHLEN*2];
861 g_return_val_if_fail (filesel != NULL, nothing);
862 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
865 translate_win32_path (filesel);
867 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
870 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
873 strncpy (something, sys_filename, sizeof (something));
874 g_free (sys_filename);
882 gtk_file_selection_complete (GtkFileSelection *filesel,
883 const gchar *pattern)
885 g_return_if_fail (filesel != NULL);
886 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
887 g_return_if_fail (pattern != NULL);
889 if (filesel->selection_entry)
890 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
891 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
895 gtk_file_selection_destroy (GtkObject *object)
897 GtkFileSelection *filesel;
899 HistoryCallbackArg *callback_arg;
901 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
903 filesel = GTK_FILE_SELECTION (object);
905 if (filesel->fileop_dialog)
907 gtk_widget_destroy (filesel->fileop_dialog);
908 filesel->fileop_dialog = NULL;
911 if (filesel->history_list)
913 list = filesel->history_list;
916 callback_arg = list->data;
917 g_free (callback_arg->directory);
918 g_free (callback_arg);
921 g_list_free (filesel->history_list);
922 filesel->history_list = NULL;
925 if (filesel->cmpl_state)
927 cmpl_free_state (filesel->cmpl_state);
928 filesel->cmpl_state = NULL;
931 GTK_OBJECT_CLASS (parent_class)->destroy (object);
934 /* Begin file operations callbacks */
937 gtk_file_selection_fileop_error (GtkFileSelection *fs,
938 gchar *error_message)
942 g_return_if_fail (error_message != NULL);
945 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
946 GTK_DIALOG_DESTROY_WITH_PARENT,
949 "%s", error_message);
951 /* yes, we free it */
952 g_free (error_message);
954 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
956 gtk_signal_connect_object (GTK_OBJECT (dialog), "response",
957 (GtkSignalFunc) gtk_widget_destroy,
960 gtk_widget_show (dialog);
964 gtk_file_selection_fileop_destroy (GtkWidget *widget,
967 GtkFileSelection *fs = data;
969 g_return_if_fail (fs != NULL);
970 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
972 fs->fileop_dialog = NULL;
977 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
980 GtkFileSelection *fs = data;
984 gchar *sys_full_path;
986 GError *error = NULL;
987 CompletionState *cmpl_state;
989 g_return_if_fail (fs != NULL);
990 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
992 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
993 cmpl_state = (CompletionState*) fs->cmpl_state;
994 path = cmpl_reference_position (cmpl_state);
996 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
997 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1000 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1001 buf = g_strdup_printf (_("The directory name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1003 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n%s"), dirname, error->message,
1004 _("You probably used symbols not allowed in filenames."));
1005 gtk_file_selection_fileop_error (fs, buf);
1006 g_error_free (error);
1010 if (mkdir (sys_full_path, 0755) < 0)
1012 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n"), dirname,
1013 g_strerror (errno));
1014 gtk_file_selection_fileop_error (fs, buf);
1019 g_free (sys_full_path);
1021 gtk_widget_destroy (fs->fileop_dialog);
1022 gtk_file_selection_populate (fs, "", FALSE);
1026 gtk_file_selection_create_dir (GtkWidget *widget,
1029 GtkFileSelection *fs = data;
1035 g_return_if_fail (fs != NULL);
1036 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1038 if (fs->fileop_dialog)
1042 dialog = gtk_dialog_new ();
1043 fs->fileop_dialog = dialog;
1044 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1045 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1047 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
1048 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1050 /* If file dialog is grabbed, grab option dialog */
1051 /* When option dialog is closed, file dialog will be grabbed again */
1052 if (GTK_WINDOW (fs)->modal)
1053 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1055 vbox = gtk_vbox_new (FALSE, 0);
1056 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1057 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1059 gtk_widget_show( vbox);
1061 label = gtk_label_new_with_mnemonic (_("_Directory name:"));
1062 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1063 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1064 gtk_widget_show (label);
1066 /* The directory entry widget */
1067 fs->fileop_entry = gtk_entry_new ();
1068 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1069 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1071 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1072 gtk_widget_show (fs->fileop_entry);
1075 button = gtk_button_new_with_label (_("Create"));
1076 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1077 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
1079 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1080 button, TRUE, TRUE, 0);
1081 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1082 gtk_widget_show (button);
1084 button = gtk_button_new_with_label (_("Cancel"));
1085 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1086 (GtkSignalFunc) gtk_widget_destroy,
1088 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1089 button, TRUE, TRUE, 0);
1090 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1091 gtk_widget_grab_default (button);
1092 gtk_widget_show (button);
1094 gtk_widget_show (dialog);
1098 gtk_file_selection_delete_file_confirmed (GtkWidget *widget,
1101 GtkFileSelection *fs = data;
1102 CompletionState *cmpl_state;
1105 gchar *sys_full_path;
1106 GError *error = NULL;
1109 g_return_if_fail (fs != NULL);
1110 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1112 cmpl_state = (CompletionState*) fs->cmpl_state;
1113 path = cmpl_reference_position (cmpl_state);
1115 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1116 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1119 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1120 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1123 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1124 fs->fileop_file, error->message,
1125 _("It probably contains symbols not allowed in filenames."));
1127 gtk_file_selection_fileop_error (fs, buf);
1128 g_error_free (error);
1132 if (unlink (sys_full_path) < 0)
1134 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1135 fs->fileop_file, g_strerror (errno));
1136 gtk_file_selection_fileop_error (fs, buf);
1141 g_free (sys_full_path);
1143 gtk_widget_destroy (fs->fileop_dialog);
1144 gtk_file_selection_populate (fs, "", FALSE);
1148 gtk_file_selection_delete_file (GtkWidget *widget,
1151 GtkFileSelection *fs = data;
1159 g_return_if_fail (fs != NULL);
1160 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1162 if (fs->fileop_dialog)
1165 #ifdef G_WITH_CYGWIN
1166 translate_win32_path (fs);
1169 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1170 if (strlen (filename) < 1)
1173 fs->fileop_file = filename;
1176 fs->fileop_dialog = dialog = gtk_dialog_new ();
1177 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1178 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1180 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1181 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1183 /* If file dialog is grabbed, grab option dialog */
1184 /* When option dialog is closed, file dialog will be grabbed again */
1185 if (GTK_WINDOW (fs)->modal)
1186 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1188 vbox = gtk_vbox_new (FALSE, 0);
1189 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1190 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1192 gtk_widget_show (vbox);
1194 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1195 label = gtk_label_new (buf);
1196 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1197 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1198 gtk_widget_show (label);
1202 button = gtk_button_new_with_label (_("Delete"));
1203 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1204 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1206 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1207 button, TRUE, TRUE, 0);
1208 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1209 gtk_widget_show (button);
1211 button = gtk_button_new_with_label (_("Cancel"));
1212 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1213 (GtkSignalFunc) gtk_widget_destroy,
1215 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1216 button, TRUE, TRUE, 0);
1217 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1218 gtk_widget_grab_default (button);
1219 gtk_widget_show (button);
1221 gtk_widget_show (dialog);
1226 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1229 GtkFileSelection *fs = data;
1233 gchar *new_filename;
1234 gchar *old_filename;
1235 gchar *sys_new_filename;
1236 gchar *sys_old_filename;
1237 CompletionState *cmpl_state;
1238 GError *error = NULL;
1240 g_return_if_fail (fs != NULL);
1241 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1243 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1244 cmpl_state = (CompletionState*) fs->cmpl_state;
1245 path = cmpl_reference_position (cmpl_state);
1247 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1248 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1250 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1253 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1254 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1256 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1257 new_filename, error->message,
1258 _("You probably used symbols not allowed in filenames."));
1259 gtk_file_selection_fileop_error (fs, buf);
1260 g_error_free (error);
1264 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1267 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1268 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1270 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1271 old_filename, error->message,
1272 _("It probably contains symbols not allowed in filenames."));
1273 gtk_file_selection_fileop_error (fs, buf);
1274 g_error_free (error);
1278 if (rename (sys_old_filename, sys_new_filename) < 0)
1280 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1281 sys_old_filename, sys_new_filename,
1282 g_strerror (errno));
1283 gtk_file_selection_fileop_error (fs, buf);
1287 g_free (sys_old_filename);
1290 g_free (new_filename);
1291 g_free (old_filename);
1292 g_free (sys_new_filename);
1294 gtk_widget_destroy (fs->fileop_dialog);
1295 gtk_file_selection_populate (fs, "", FALSE);
1299 gtk_file_selection_rename_file (GtkWidget *widget,
1302 GtkFileSelection *fs = data;
1309 g_return_if_fail (fs != NULL);
1310 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1312 if (fs->fileop_dialog)
1315 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1316 if (strlen (fs->fileop_file) < 1)
1320 fs->fileop_dialog = dialog = gtk_dialog_new ();
1321 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1322 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1324 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1325 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1327 /* If file dialog is grabbed, grab option dialog */
1328 /* When option dialog closed, file dialog will be grabbed again */
1329 if (GTK_WINDOW (fs)->modal)
1330 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1332 vbox = gtk_vbox_new (FALSE, 0);
1333 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1334 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1336 gtk_widget_show(vbox);
1338 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1339 label = gtk_label_new(buf);
1340 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1341 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1342 gtk_widget_show (label);
1345 /* New filename entry */
1346 fs->fileop_entry = gtk_entry_new ();
1347 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1349 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1350 gtk_widget_show (fs->fileop_entry);
1352 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1353 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1354 0, strlen (fs->fileop_file));
1357 button = gtk_button_new_with_label (_("Rename"));
1358 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1359 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1361 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1362 button, TRUE, TRUE, 0);
1363 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1364 gtk_widget_show (button);
1366 button = gtk_button_new_with_label (_("Cancel"));
1367 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1368 (GtkSignalFunc) gtk_widget_destroy,
1370 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1371 button, TRUE, TRUE, 0);
1372 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1373 gtk_widget_grab_default (button);
1374 gtk_widget_show (button);
1376 gtk_widget_show (dialog);
1380 gtk_file_selection_insert_text (GtkWidget *widget,
1381 const gchar *new_text,
1382 gint new_text_length,
1388 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1393 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "insert_text");
1403 gtk_file_selection_key_press (GtkWidget *widget,
1407 GtkFileSelection *fs;
1410 g_return_val_if_fail (widget != NULL, FALSE);
1411 g_return_val_if_fail (event != NULL, FALSE);
1413 if (event->keyval == GDK_Tab)
1415 fs = GTK_FILE_SELECTION (user_data);
1416 #ifdef G_WITH_CYGWIN
1417 translate_win32_path (fs);
1419 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1421 text = g_strdup (text);
1423 gtk_file_selection_populate (fs, text, TRUE);
1427 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1437 gtk_file_selection_history_callback (GtkWidget *widget,
1440 GtkFileSelection *fs = data;
1441 HistoryCallbackArg *callback_arg;
1444 g_return_if_fail (fs != NULL);
1445 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1447 list = fs->history_list;
1450 callback_arg = list->data;
1452 if (callback_arg->menu_item == widget)
1454 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1463 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1464 gchar *current_directory)
1466 HistoryCallbackArg *callback_arg;
1467 GtkWidget *menu_item;
1473 g_return_if_fail (fs != NULL);
1474 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1475 g_return_if_fail (current_directory != NULL);
1477 list = fs->history_list;
1479 if (fs->history_menu)
1482 callback_arg = list->data;
1483 g_free (callback_arg->directory);
1484 g_free (callback_arg);
1487 g_list_free (fs->history_list);
1488 fs->history_list = NULL;
1490 gtk_widget_destroy (fs->history_menu);
1493 fs->history_menu = gtk_menu_new ();
1495 current_dir = g_strdup (current_directory);
1497 dir_len = strlen (current_dir);
1499 for (i = dir_len; i >= 0; i--)
1501 /* the i == dir_len is to catch the full path for the first
1503 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1505 /* another small hack to catch the full path */
1507 current_dir[i + 1] = '\0';
1508 #ifdef G_WITH_CYGWIN
1509 if (!strcmp (current_dir, "//"))
1512 menu_item = gtk_menu_item_new_with_label (current_dir);
1514 callback_arg = g_new (HistoryCallbackArg, 1);
1515 callback_arg->menu_item = menu_item;
1517 /* since the autocompletion gets confused if you don't
1518 * supply a trailing '/' on a dir entry, set the full
1519 * (current) path to "" which just refreshes the filesel */
1522 callback_arg->directory = g_strdup ("");
1526 callback_arg->directory = g_strdup (current_dir);
1529 fs->history_list = g_list_append (fs->history_list, callback_arg);
1531 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1532 (GtkSignalFunc) gtk_file_selection_history_callback,
1534 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1535 gtk_widget_show (menu_item);
1539 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1541 g_free (current_dir);
1545 gtk_file_selection_file_button (GtkWidget *widget,
1548 GdkEventButton *bevent,
1551 GtkFileSelection *fs = NULL;
1552 gchar *filename, *temp = NULL;
1554 g_return_if_fail (GTK_IS_CLIST (widget));
1557 g_return_if_fail (fs != NULL);
1558 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1560 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1561 filename = g_strdup (temp);
1563 #ifdef G_WITH_CYGWIN
1564 /* Check to see if the selection was a drive selector */
1565 if (isalpha (filename[0]) && (filename[1] == ':')) {
1566 /* It is... map it to a CYGWIN32 drive */
1567 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1569 filename = temp_filename;
1571 #endif /* G_WITH_CYGWIN */
1576 switch (bevent->type)
1578 case GDK_2BUTTON_PRESS:
1579 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1583 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1587 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1594 gtk_file_selection_dir_button (GtkWidget *widget,
1597 GdkEventButton *bevent,
1600 GtkFileSelection *fs = NULL;
1601 gchar *filename, *temp = NULL;
1603 g_return_if_fail (GTK_IS_CLIST (widget));
1605 fs = GTK_FILE_SELECTION (user_data);
1606 g_return_if_fail (fs != NULL);
1607 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1609 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1610 filename = g_strdup (temp);
1615 switch (bevent->type)
1617 case GDK_2BUTTON_PRESS:
1618 gtk_file_selection_populate (fs, filename, FALSE);
1622 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1626 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1632 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1635 win32_gtk_add_drives_to_dir_list (GtkWidget *the_dir_list)
1637 gchar *text[2], *textPtr;
1639 char volumeNameBuf[128];
1640 char formatBuffer[128];
1644 /* Get the Drives string */
1645 GetLogicalDriveStrings (sizeof (buffer), buffer);
1647 /* Add the drives as necessary */
1649 while (*textPtr != '\0') {
1650 /* Get the volume information for this drive */
1651 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1653 /* Ignore floppies (?) */
1654 DWORD maxComponentLength, flags;
1656 GetVolumeInformation (textPtr,
1657 volumeNameBuf, sizeof(volumeNameBuf),
1658 NULL, &maxComponentLength,
1660 /* Build the actual displayable string */
1662 sprintf (formatBuffer, "%c:\\", toupper(textPtr[0]));
1663 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1664 if (strlen (volumeNameBuf) > 0)
1665 sprintf (formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1667 /* Add to the list */
1668 text[0] = formatBuffer;
1669 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1671 textPtr += (strlen (textPtr) + 1);
1677 gtk_file_selection_populate (GtkFileSelection *fs,
1681 CompletionState *cmpl_state;
1682 PossibleCompletion* poss;
1684 gchar* rem_path = rel_path;
1687 gint did_recurse = FALSE;
1688 gint possible_count = 0;
1689 gint selection_index = -1;
1691 g_return_if_fail (fs != NULL);
1692 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1694 cmpl_state = (CompletionState*) fs->cmpl_state;
1695 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1697 if (!cmpl_state_okay (cmpl_state))
1699 /* Something went wrong. */
1700 gtk_file_selection_abort (fs);
1704 g_assert (cmpl_state->reference_dir);
1706 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1707 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1708 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1709 gtk_clist_clear (GTK_CLIST (fs->file_list));
1711 /* Set the dir_list to include ./ and ../ */
1713 text[0] = "." G_DIR_SEPARATOR_S;
1714 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1716 text[0] = ".." G_DIR_SEPARATOR_S;
1717 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1721 if (cmpl_is_a_completion (poss))
1723 possible_count += 1;
1725 filename = cmpl_this_completion (poss);
1729 if (cmpl_is_directory (poss))
1731 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1732 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1733 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1737 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1741 poss = cmpl_next_completion (cmpl_state);
1744 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1745 /* For Windows, add drives as potential selections */
1746 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1749 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1750 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1752 /* File lists are set. */
1754 g_assert (cmpl_state->reference_dir);
1759 /* User is trying to complete filenames, so advance the user's input
1760 * string to the updated_text, which is the common leading substring
1761 * of all possible completions, and if its a directory attempt
1762 * attempt completions in it. */
1764 if (cmpl_updated_text (cmpl_state)[0])
1767 if (cmpl_updated_dir (cmpl_state))
1769 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1773 gtk_file_selection_populate (fs, dir_name, TRUE);
1779 if (fs->selection_entry)
1780 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1781 cmpl_updated_text (cmpl_state));
1786 selection_index = cmpl_last_valid_char (cmpl_state) -
1787 (strlen (rel_path) - strlen (rem_path));
1788 if (fs->selection_entry)
1789 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1794 if (fs->selection_entry)
1795 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1800 if (fs->selection_entry)
1801 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1803 if (fs->selection_entry)
1805 sel_text = g_strconcat (_("Selection: "),
1806 cmpl_reference_position (cmpl_state),
1809 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1813 if (fs->history_pulldown)
1815 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1822 gtk_file_selection_abort (GtkFileSelection *fs)
1826 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1828 /* BEEP gdk_beep(); */
1830 if (fs->selection_entry)
1831 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1834 /**********************************************************************/
1835 /* External Interface */
1836 /**********************************************************************/
1838 /* The four completion state selectors
1841 cmpl_updated_text (CompletionState *cmpl_state)
1843 return cmpl_state->updated_text;
1847 cmpl_updated_dir (CompletionState *cmpl_state)
1849 return cmpl_state->re_complete;
1853 cmpl_reference_position (CompletionState *cmpl_state)
1855 return cmpl_state->reference_dir->fullname;
1859 cmpl_last_valid_char (CompletionState *cmpl_state)
1861 return cmpl_state->last_valid_char;
1865 cmpl_completion_fullname (gchar *text,
1866 CompletionState *cmpl_state)
1868 static char nothing[2] = "";
1870 if (!cmpl_state_okay (cmpl_state))
1874 else if (g_path_is_absolute (text))
1876 strcpy (cmpl_state->updated_text, text);
1879 else if (text[0] == '~')
1884 dir = open_user_dir (text, cmpl_state);
1888 /* spencer says just return ~something, so
1889 * for now just do it. */
1890 strcpy (cmpl_state->updated_text, text);
1895 strcpy (cmpl_state->updated_text, dir->fullname);
1897 slash = strchr (text, G_DIR_SEPARATOR);
1900 strcat (cmpl_state->updated_text, slash);
1906 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1907 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1908 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1909 strcat (cmpl_state->updated_text, text);
1912 return cmpl_state->updated_text;
1915 /* The three completion selectors
1918 cmpl_this_completion (PossibleCompletion* pc)
1924 cmpl_is_directory (PossibleCompletion* pc)
1926 return pc->is_directory;
1930 cmpl_is_a_completion (PossibleCompletion* pc)
1932 return pc->is_a_completion;
1935 /**********************************************************************/
1936 /* Construction, deletion */
1937 /**********************************************************************/
1939 static CompletionState*
1940 cmpl_init_state (void)
1942 gchar *sys_getcwd_buf;
1944 CompletionState *new_state;
1946 new_state = g_new (CompletionState, 1);
1948 /* g_get_current_dir() returns a string in the "system" charset */
1949 sys_getcwd_buf = g_get_current_dir ();
1950 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
1951 g_free (sys_getcwd_buf);
1955 new_state->reference_dir = NULL;
1956 new_state->completion_dir = NULL;
1957 new_state->active_completion_dir = NULL;
1958 new_state->directory_storage = NULL;
1959 new_state->directory_sent_storage = NULL;
1960 new_state->last_valid_char = 0;
1961 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1962 new_state->updated_text_alloc = MAXPATHLEN;
1963 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1964 new_state->the_completion.text_alloc = MAXPATHLEN;
1965 new_state->user_dir_name_buffer = NULL;
1966 new_state->user_directories = NULL;
1968 new_state->reference_dir = open_dir (utf8_cwd, new_state);
1970 if (!new_state->reference_dir)
1972 /* Directories changing from underneath us, grumble */
1973 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
1982 cmpl_free_dir_list (GList* dp0)
1988 free_dir (dp->data);
1996 cmpl_free_dir_sent_list (GList* dp0)
2002 free_dir_sent (dp->data);
2010 cmpl_free_state (CompletionState* cmpl_state)
2012 g_return_if_fail (cmpl_state != NULL);
2014 cmpl_free_dir_list (cmpl_state->directory_storage);
2015 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2017 if (cmpl_state->user_dir_name_buffer)
2018 g_free (cmpl_state->user_dir_name_buffer);
2019 if (cmpl_state->user_directories)
2020 g_free (cmpl_state->user_directories);
2021 if (cmpl_state->the_completion.text)
2022 g_free (cmpl_state->the_completion.text);
2023 if (cmpl_state->updated_text)
2024 g_free (cmpl_state->updated_text);
2026 g_free (cmpl_state);
2030 free_dir (CompletionDir* dir)
2032 g_free (dir->fullname);
2037 free_dir_sent (CompletionDirSent* sent)
2040 for (i = 0; i < sent->entry_count; i++)
2041 g_free (sent->entries[i].entry_name);
2042 g_free (sent->entries);
2047 prune_memory_usage (CompletionState *cmpl_state)
2049 GList* cdsl = cmpl_state->directory_sent_storage;
2050 GList* cdl = cmpl_state->directory_storage;
2054 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2059 cmpl_free_dir_sent_list (cdsl->next);
2063 cmpl_state->directory_storage = NULL;
2066 if (cdl->data == cmpl_state->reference_dir)
2067 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2069 free_dir (cdl->data);
2076 /**********************************************************************/
2077 /* The main entrances. */
2078 /**********************************************************************/
2080 static PossibleCompletion*
2081 cmpl_completion_matches (gchar *text_to_complete,
2082 gchar **remaining_text,
2083 CompletionState *cmpl_state)
2086 PossibleCompletion *poss;
2088 prune_memory_usage (cmpl_state);
2090 g_assert (text_to_complete != NULL);
2092 cmpl_state->user_completion_index = -1;
2093 cmpl_state->last_completion_text = text_to_complete;
2094 cmpl_state->the_completion.text[0] = 0;
2095 cmpl_state->last_valid_char = 0;
2096 cmpl_state->updated_text_len = -1;
2097 cmpl_state->updated_text[0] = 0;
2098 cmpl_state->re_complete = FALSE;
2101 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2103 if (text_to_complete[0] == '~' && !first_slash)
2105 /* Text starts with ~ and there is no slash, show all the
2106 * home directory completions.
2108 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2110 update_cmpl (poss, cmpl_state);
2115 cmpl_state->reference_dir =
2116 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2118 if (!cmpl_state->reference_dir)
2121 cmpl_state->completion_dir =
2122 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2124 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2126 if (!cmpl_state->completion_dir)
2129 cmpl_state->completion_dir->cmpl_index = -1;
2130 cmpl_state->completion_dir->cmpl_parent = NULL;
2131 cmpl_state->completion_dir->cmpl_text = *remaining_text;
2133 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2135 cmpl_state->reference_dir = cmpl_state->completion_dir;
2137 poss = attempt_file_completion (cmpl_state);
2139 update_cmpl (poss, cmpl_state);
2144 static PossibleCompletion*
2145 cmpl_next_completion (CompletionState* cmpl_state)
2147 PossibleCompletion* poss = NULL;
2149 cmpl_state->the_completion.text[0] = 0;
2152 if (cmpl_state->user_completion_index >= 0)
2153 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2155 poss = attempt_file_completion (cmpl_state);
2157 poss = attempt_file_completion (cmpl_state);
2160 update_cmpl (poss, cmpl_state);
2165 /**********************************************************************/
2166 /* Directory Operations */
2167 /**********************************************************************/
2169 /* Open the directory where completion will begin from, if possible. */
2170 static CompletionDir*
2171 open_ref_dir (gchar *text_to_complete,
2172 gchar **remaining_text,
2173 CompletionState *cmpl_state)
2176 CompletionDir *new_dir;
2178 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2180 #ifdef G_WITH_CYGWIN
2181 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2184 sprintf (root_dir, "//%c", text_to_complete[2]);
2186 new_dir = open_dir (root_dir, cmpl_state);
2189 *remaining_text = text_to_complete + 4;
2197 else if (text_to_complete[0] == '~')
2199 new_dir = open_user_dir (text_to_complete, cmpl_state);
2204 *remaining_text = first_slash + 1;
2206 *remaining_text = text_to_complete + strlen (text_to_complete);
2214 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2216 gchar *tmp = g_strdup (text_to_complete);
2220 while (*p && *p != '*' && *p != '?')
2224 p = strrchr (tmp, G_DIR_SEPARATOR);
2232 new_dir = open_dir (tmp, cmpl_state);
2235 *remaining_text = text_to_complete +
2236 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2240 /* If no possible candidates, use the cwd */
2241 gchar *sys_curdir = g_get_current_dir ();
2242 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2244 g_free (sys_curdir);
2246 new_dir = open_dir (utf8_curdir, cmpl_state);
2249 *remaining_text = text_to_complete;
2251 g_free (utf8_curdir);
2258 *remaining_text = text_to_complete;
2260 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2265 new_dir->cmpl_index = -1;
2266 new_dir->cmpl_parent = NULL;
2274 /* open a directory by user name */
2275 static CompletionDir*
2276 open_user_dir (gchar *text_to_complete,
2277 CompletionState *cmpl_state)
2279 CompletionDir *result;
2283 g_assert (text_to_complete && text_to_complete[0] == '~');
2285 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2288 cmp_len = first_slash - text_to_complete - 1;
2290 cmp_len = strlen (text_to_complete + 1);
2295 gchar *homedir = g_get_home_dir ();
2296 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2301 result = open_dir (utf8_homedir, cmpl_state);
2305 g_free (utf8_homedir);
2310 gchar* copy = g_new (char, cmp_len + 1);
2314 strncpy (copy, text_to_complete + 1, cmp_len);
2316 pwd = getpwnam (copy);
2323 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2324 result = open_dir (utf8_dir, cmpl_state);
2332 /* open a directory relative the the current relative directory */
2333 static CompletionDir*
2334 open_relative_dir (gchar *dir_name,
2336 CompletionState *cmpl_state)
2338 CompletionDir *result;
2341 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2342 g_string_assign (path, dir->fullname);
2344 if (dir->fullname_len > 1
2345 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2346 g_string_append_c (path, G_DIR_SEPARATOR);
2347 g_string_append (path, dir_name);
2349 result = open_dir (path->str, cmpl_state);
2351 g_string_free (path, TRUE);
2356 /* after the cache lookup fails, really open a new directory */
2357 static CompletionDirSent*
2358 open_new_dir (gchar *dir_name,
2360 gboolean stat_subdirs)
2362 CompletionDirSent *sent;
2364 struct dirent *dirent_ptr;
2365 gint entry_count = 0;
2368 struct stat ent_sbuf;
2370 gchar *sys_dir_name;
2372 sent = g_new (CompletionDirSent, 1);
2373 sent->mtime = sbuf->st_mtime;
2374 sent->inode = sbuf->st_ino;
2375 sent->device = sbuf->st_dev;
2377 path = g_string_sized_new (2*MAXPATHLEN + 10);
2379 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2382 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2386 directory = opendir (sys_dir_name);
2390 g_free (sys_dir_name);
2394 while ((dirent_ptr = readdir (directory)) != NULL)
2397 sent->entries = g_new (CompletionDirEntry, entry_count);
2398 sent->entry_count = entry_count;
2400 rewinddir (directory);
2402 for (i = 0; i < entry_count; i += 1)
2404 dirent_ptr = readdir (directory);
2409 closedir (directory);
2410 g_free (sys_dir_name);
2414 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent_ptr->d_name, -1, NULL, NULL, NULL);
2415 if (!g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2417 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent_ptr->d_name);
2422 g_string_assign (path, sys_dir_name);
2423 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2425 g_string_append_c (path, G_DIR_SEPARATOR);
2427 g_string_append (path, dirent_ptr->d_name);
2431 /* Here we know path->str is a "system charset" string */
2432 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2433 sent->entries[n_entries].is_dir = TRUE;
2435 /* stat may fail, and we don't mind, since it could be a
2436 * dangling symlink. */
2437 sent->entries[n_entries].is_dir = FALSE;
2440 sent->entries[n_entries].is_dir = 1;
2442 sent->entry_count = n_entries;
2444 g_free (sys_dir_name);
2445 g_string_free (path, TRUE);
2446 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2448 closedir (directory);
2453 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2456 check_dir (gchar *dir_name,
2457 struct stat *result,
2458 gboolean *stat_subdirs)
2460 /* A list of directories that we know only contain other directories.
2461 * Trying to stat every file in these directories would be very
2468 struct stat statbuf;
2469 } no_stat_dirs[] = {
2470 { "/afs", FALSE, { 0 } },
2471 { "/net", FALSE, { 0 } }
2474 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
2475 static gboolean initialized = FALSE;
2476 gchar *sys_dir_name;
2482 for (i = 0; i < n_no_stat_dirs; i++)
2484 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2485 no_stat_dirs[i].present = TRUE;
2489 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2492 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2496 if (stat (sys_dir_name, result) < 0)
2498 g_free (sys_dir_name);
2502 g_free (sys_dir_name);
2504 *stat_subdirs = TRUE;
2505 for (i = 0; i < n_no_stat_dirs; i++)
2507 if (no_stat_dirs[i].present &&
2508 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2509 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2511 *stat_subdirs = FALSE;
2521 /* open a directory by absolute pathname */
2522 static CompletionDir*
2523 open_dir (gchar *dir_name,
2524 CompletionState *cmpl_state)
2527 gboolean stat_subdirs;
2528 CompletionDirSent *sent;
2531 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2532 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2535 cdsl = cmpl_state->directory_sent_storage;
2541 if (sent->inode == sbuf.st_ino &&
2542 sent->mtime == sbuf.st_mtime &&
2543 sent->device == sbuf.st_dev)
2544 return attach_dir (sent, dir_name, cmpl_state);
2549 stat_subdirs = TRUE;
2552 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
2556 cmpl_state->directory_sent_storage =
2557 g_list_prepend (cmpl_state->directory_sent_storage, sent);
2559 return attach_dir (sent, dir_name, cmpl_state);
2565 static CompletionDir*
2566 attach_dir (CompletionDirSent *sent,
2568 CompletionState *cmpl_state)
2570 CompletionDir* new_dir;
2572 new_dir = g_new (CompletionDir, 1);
2574 cmpl_state->directory_storage =
2575 g_list_prepend (cmpl_state->directory_storage, new_dir);
2577 new_dir->sent = sent;
2578 new_dir->fullname = g_strdup (dir_name);
2579 new_dir->fullname_len = strlen (dir_name);
2585 correct_dir_fullname (CompletionDir* cmpl_dir)
2587 gint length = strlen (cmpl_dir->fullname);
2588 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2589 gchar *sys_filename;
2592 /* Does it end with /. (\.) ? */
2594 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2596 /* Is it just the root directory (on a drive) ? */
2597 if (cmpl_dir->fullname + length - 2 == first_slash)
2599 cmpl_dir->fullname[length - 1] = 0;
2600 cmpl_dir->fullname_len = length - 1;
2605 cmpl_dir->fullname[length - 2] = 0;
2609 /* Ends with /./ (\.\)? */
2610 else if (length >= 3 &&
2611 strcmp (cmpl_dir->fullname + length - 3,
2612 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2613 cmpl_dir->fullname[length - 2] = 0;
2615 /* Ends with /.. (\..) ? */
2616 else if (length >= 3 &&
2617 strcmp (cmpl_dir->fullname + length - 3,
2618 G_DIR_SEPARATOR_S "..") == 0)
2620 /* Is it just /.. (X:\..)? */
2621 if (cmpl_dir->fullname + length - 3 == first_slash)
2623 cmpl_dir->fullname[length - 2] = 0;
2624 cmpl_dir->fullname_len = length - 2;
2628 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2631 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2635 if (stat (sys_filename, &sbuf) < 0)
2637 g_free (sys_filename);
2641 g_free (sys_filename);
2643 cmpl_dir->fullname[length - 3] = 0;
2645 if (!correct_parent (cmpl_dir, &sbuf))
2649 /* Ends with /../ (\..\)? */
2650 else if (length >= 4 &&
2651 strcmp (cmpl_dir->fullname + length - 4,
2652 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2654 /* Is it just /../ (X:\..\)? */
2655 if (cmpl_dir->fullname + length - 4 == first_slash)
2657 cmpl_dir->fullname[length - 3] = 0;
2658 cmpl_dir->fullname_len = length - 3;
2662 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2665 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2669 if (stat (sys_filename, &sbuf) < 0)
2671 g_free (sys_filename);
2675 g_free (sys_filename);
2677 cmpl_dir->fullname[length - 4] = 0;
2679 if (!correct_parent (cmpl_dir, &sbuf))
2683 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
2689 correct_parent (CompletionDir *cmpl_dir,
2696 gchar *sys_filename;
2699 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2700 g_assert (last_slash);
2701 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2703 /* Clever (?) way to check for top-level directory that works also on
2704 * Win32, where there is a drive letter and colon prefixed...
2706 if (last_slash != first_slash)
2716 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2719 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2721 last_slash[0] = G_DIR_SEPARATOR;
2725 if (stat (sys_filename, &parbuf) < 0)
2727 g_free (sys_filename);
2730 last_slash[0] = G_DIR_SEPARATOR;
2733 g_free (sys_filename);
2735 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2736 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2737 /* it wasn't a link */
2743 last_slash[0] = G_DIR_SEPARATOR;
2745 /* it was a link, have to figure it out the hard way */
2747 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
2752 g_free (cmpl_dir->fullname);
2754 cmpl_dir->fullname = new_name;
2763 find_parent_dir_fullname (gchar* dirname)
2765 gchar *sys_orig_dir;
2770 sys_orig_dir = g_get_current_dir ();
2771 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
2774 g_free (sys_orig_dir);
2775 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2779 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
2781 g_free (sys_dirname);
2782 g_free (sys_orig_dir);
2786 g_free (sys_dirname);
2788 sys_cwd = g_get_current_dir ();
2789 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
2792 if (chdir (sys_orig_dir) != 0)
2795 g_free (sys_orig_dir);
2799 g_free (sys_orig_dir);
2805 /**********************************************************************/
2806 /* Completion Operations */
2807 /**********************************************************************/
2811 static PossibleCompletion*
2812 attempt_homedir_completion (gchar *text_to_complete,
2813 CompletionState *cmpl_state)
2817 if (!cmpl_state->user_dir_name_buffer &&
2818 !get_pwdb (cmpl_state))
2820 length = strlen (text_to_complete) - 1;
2822 cmpl_state->user_completion_index += 1;
2824 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2826 index = first_diff_index (text_to_complete + 1,
2827 cmpl_state->user_directories
2828 [cmpl_state->user_completion_index].login);
2835 if (cmpl_state->last_valid_char < (index + 1))
2836 cmpl_state->last_valid_char = index + 1;
2837 cmpl_state->user_completion_index += 1;
2841 cmpl_state->the_completion.is_a_completion = 1;
2842 cmpl_state->the_completion.is_directory = TRUE;
2844 append_completion_text ("~", cmpl_state);
2846 append_completion_text (cmpl_state->
2847 user_directories[cmpl_state->user_completion_index].login,
2850 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
2853 if (text_to_complete[1]
2854 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2856 cmpl_state->user_completion_index = -1;
2861 cmpl_state->user_completion_index += 1;
2862 cmpl_state->the_completion.is_a_completion = 1;
2863 cmpl_state->the_completion.is_directory = TRUE;
2865 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
2871 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2872 #define FOLD(c) (tolower(c))
2877 /* returns the index (>= 0) of the first differing character,
2878 * PATTERN_MATCH if the completion matches */
2880 first_diff_index (gchar *pat,
2885 while (*pat && *text && FOLD (*text) == FOLD (*pat))
2895 return PATTERN_MATCH;
2898 static PossibleCompletion*
2899 append_completion_text (gchar *text,
2900 CompletionState *cmpl_state)
2904 if (!cmpl_state->the_completion.text)
2907 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
2909 if (cmpl_state->the_completion.text_alloc > len)
2911 strcat (cmpl_state->the_completion.text, text);
2912 return &cmpl_state->the_completion;
2918 cmpl_state->the_completion.text_alloc = i;
2920 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
2922 if (!cmpl_state->the_completion.text)
2926 strcat (cmpl_state->the_completion.text, text);
2927 return &cmpl_state->the_completion;
2931 static CompletionDir*
2932 find_completion_dir (gchar *text_to_complete,
2933 gchar **remaining_text,
2934 CompletionState *cmpl_state)
2936 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2937 CompletionDir* dir = cmpl_state->reference_dir;
2938 CompletionDir* next;
2939 *remaining_text = text_to_complete;
2943 gint len = first_slash - *remaining_text;
2945 gchar *found_name = NULL; /* Quiet gcc */
2947 gchar* pat_buf = g_new (gchar, len + 1);
2949 strncpy (pat_buf, *remaining_text, len);
2952 for (i = 0; i < dir->sent->entry_count; i += 1)
2954 if (dir->sent->entries[i].is_dir &&
2955 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
2956 FNMATCH_FLAGS)!= FNM_NOMATCH)
2966 found_name = dir->sent->entries[i].entry_name;
2973 /* Perhaps we are trying to open an automount directory */
2974 found_name = pat_buf;
2977 next = open_relative_dir (found_name, dir, cmpl_state);
2985 next->cmpl_parent = dir;
2989 if (!correct_dir_fullname (dir))
2995 *remaining_text = first_slash + 1;
2996 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3005 update_cmpl (PossibleCompletion *poss,
3006 CompletionState *cmpl_state)
3010 if (!poss || !cmpl_is_a_completion (poss))
3013 cmpl_len = strlen (cmpl_this_completion (poss));
3015 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3017 cmpl_state->updated_text =
3018 (gchar*)g_realloc (cmpl_state->updated_text,
3019 cmpl_state->updated_text_alloc);
3020 cmpl_state->updated_text_alloc = 2*cmpl_len;
3023 if (cmpl_state->updated_text_len < 0)
3025 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3026 cmpl_state->updated_text_len = cmpl_len;
3027 cmpl_state->re_complete = cmpl_is_directory (poss);
3029 else if (cmpl_state->updated_text_len == 0)
3031 cmpl_state->re_complete = FALSE;
3036 first_diff_index (cmpl_state->updated_text,
3037 cmpl_this_completion (poss));
3039 cmpl_state->re_complete = FALSE;
3041 if (first_diff == PATTERN_MATCH)
3044 if (first_diff > cmpl_state->updated_text_len)
3045 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3047 cmpl_state->updated_text_len = first_diff;
3048 cmpl_state->updated_text[first_diff] = 0;
3052 static PossibleCompletion*
3053 attempt_file_completion (CompletionState *cmpl_state)
3055 gchar *pat_buf, *first_slash;
3056 CompletionDir *dir = cmpl_state->active_completion_dir;
3058 dir->cmpl_index += 1;
3060 if (dir->cmpl_index == dir->sent->entry_count)
3062 if (dir->cmpl_parent == NULL)
3064 cmpl_state->active_completion_dir = NULL;
3070 cmpl_state->active_completion_dir = dir->cmpl_parent;
3072 return attempt_file_completion (cmpl_state);
3076 g_assert (dir->cmpl_text);
3078 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3082 gint len = first_slash - dir->cmpl_text;
3084 pat_buf = g_new (gchar, len + 1);
3085 strncpy (pat_buf, dir->cmpl_text, len);
3090 gint len = strlen (dir->cmpl_text);
3092 pat_buf = g_new (gchar, len + 2);
3093 strcpy (pat_buf, dir->cmpl_text);
3094 /* Don't append a * if the user entered one herself.
3095 * This way one can complete *.h and don't get matches
3096 * on any .help files, for instance.
3098 if (strchr (pat_buf, '*') == NULL)
3099 strcpy (pat_buf + len, "*");
3104 if (dir->sent->entries[dir->cmpl_index].is_dir)
3106 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3107 FNMATCH_FLAGS) != FNM_NOMATCH)
3109 CompletionDir* new_dir;
3111 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3120 new_dir->cmpl_parent = dir;
3122 new_dir->cmpl_index = -1;
3123 new_dir->cmpl_text = first_slash + 1;
3125 cmpl_state->active_completion_dir = new_dir;
3128 return attempt_file_completion (cmpl_state);
3133 return attempt_file_completion (cmpl_state);
3139 return attempt_file_completion (cmpl_state);
3144 if (dir->cmpl_parent != NULL)
3146 append_completion_text (dir->fullname +
3147 strlen (cmpl_state->completion_dir->fullname) + 1,
3149 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3152 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3154 cmpl_state->the_completion.is_a_completion =
3155 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3156 FNMATCH_FLAGS) != FNM_NOMATCH;
3158 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3159 if (dir->sent->entries[dir->cmpl_index].is_dir)
3160 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3163 return &cmpl_state->the_completion;
3170 get_pwdb (CompletionState* cmpl_state)
3172 struct passwd *pwd_ptr;
3175 gint len = 0, i, count = 0;
3177 if (cmpl_state->user_dir_name_buffer)
3181 while ((pwd_ptr = getpwent ()) != NULL)
3183 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3184 len += strlen (utf8);
3186 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3187 len += strlen (utf8);
3195 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3196 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3197 cmpl_state->user_directories_len = count;
3199 buf_ptr = cmpl_state->user_dir_name_buffer;
3201 for (i = 0; i < count; i += 1)
3203 pwd_ptr = getpwent ();
3210 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3211 strcpy (buf_ptr, utf8);
3214 cmpl_state->user_directories[i].login = buf_ptr;
3216 buf_ptr += strlen (buf_ptr);
3219 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3220 strcpy (buf_ptr, utf8);
3223 cmpl_state->user_directories[i].homedir = buf_ptr;
3225 buf_ptr += strlen (buf_ptr);
3229 qsort (cmpl_state->user_directories,
3230 cmpl_state->user_directories_len,
3231 sizeof (CompletionUserDir),
3240 if (cmpl_state->user_dir_name_buffer)
3241 g_free (cmpl_state->user_dir_name_buffer);
3242 if (cmpl_state->user_directories)
3243 g_free (cmpl_state->user_directories);
3245 cmpl_state->user_dir_name_buffer = NULL;
3246 cmpl_state->user_directories = NULL;
3252 compare_user_dir (const void *a,
3255 return strcmp ((((CompletionUserDir*)a))->login,
3256 (((CompletionUserDir*)b))->login);
3262 compare_cmpl_dir (const void *a,
3265 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3266 return strcmp ((((CompletionDirEntry*)a))->entry_name,
3267 (((CompletionDirEntry*)b))->entry_name);
3269 return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
3270 (((CompletionDirEntry*)b))->entry_name);
3275 cmpl_state_okay (CompletionState* cmpl_state)
3277 return cmpl_state && cmpl_state->reference_dir;
3281 cmpl_strerror (gint err)
3283 if (err == CMPL_ERRNO_TOO_LONG)
3284 return _("Name too long");
3285 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3286 return _("Couldn't convert filename");
3288 return g_strerror (err);