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);
576 grab_default (GtkWidget *widget)
578 gtk_widget_grab_default (widget);
583 gtk_file_selection_init (GtkFileSelection *filesel)
585 GtkWidget *entry_vbox;
587 GtkWidget *list_hbox;
588 GtkWidget *confirm_area;
589 GtkWidget *pulldown_hbox;
590 GtkWidget *scrolled_win;
594 char *file_title [2];
596 dialog = GTK_DIALOG (filesel);
598 filesel->cmpl_state = cmpl_init_state ();
600 /* The dialog-sized vertical box */
601 filesel->main_vbox = dialog->vbox;
602 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
604 /* The horizontal box containing create, rename etc. buttons */
605 filesel->button_area = gtk_hbutton_box_new ();
606 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
607 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
608 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
610 gtk_widget_show (filesel->button_area);
612 gtk_file_selection_show_fileop_buttons (filesel);
614 /* hbox for pulldown menu */
615 pulldown_hbox = gtk_hbox_new (TRUE, 5);
616 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
617 gtk_widget_show (pulldown_hbox);
620 filesel->history_pulldown = gtk_option_menu_new ();
621 gtk_widget_show (filesel->history_pulldown);
622 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
625 /* The horizontal box containing the directory and file listboxes */
626 list_hbox = gtk_hbox_new (FALSE, 5);
627 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
628 gtk_widget_show (list_hbox);
630 /* The directories clist */
631 dir_title[0] = _("Directories");
633 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
634 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
635 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
636 (GtkSignalFunc) gtk_file_selection_dir_button,
638 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->dir_list), 0, TRUE);
639 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
641 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
642 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
643 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
644 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
645 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
646 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
647 gtk_widget_show (filesel->dir_list);
648 gtk_widget_show (scrolled_win);
650 /* The files clist */
651 file_title[0] = _("Files");
652 file_title[1] = NULL;
653 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
654 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
655 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
656 (GtkSignalFunc) gtk_file_selection_file_button,
658 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->file_list), 0, TRUE);
659 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
661 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
662 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
663 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
664 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
665 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
666 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
667 gtk_widget_show (filesel->file_list);
668 gtk_widget_show (scrolled_win);
670 /* action area for packing buttons into. */
671 filesel->action_area = gtk_hbox_new (TRUE, 0);
672 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
674 gtk_widget_show (filesel->action_area);
676 /* The OK/Cancel button area */
677 confirm_area = dialog->action_area;
680 filesel->ok_button = gtk_dialog_add_button (dialog,
684 gtk_widget_grab_default (filesel->ok_button);
686 /* The Cancel button */
687 filesel->cancel_button = gtk_dialog_add_button (dialog,
689 GTK_RESPONSE_CANCEL);
691 /* The selection entry widget */
692 entry_vbox = gtk_vbox_new (FALSE, 2);
693 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
694 gtk_widget_show (entry_vbox);
696 filesel->selection_text = label = gtk_label_new ("");
697 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
698 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
699 gtk_widget_show (label);
701 filesel->selection_entry = gtk_entry_new ();
702 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
703 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
704 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "insert_text",
705 (GtkSignalFunc) gtk_file_selection_insert_text, NULL);
706 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
707 (GtkSignalFunc) grab_default,
708 GTK_OBJECT (filesel->ok_button));
709 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
710 (GtkSignalFunc) gtk_button_clicked,
711 GTK_OBJECT (filesel->ok_button));
712 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
713 gtk_widget_show (filesel->selection_entry);
715 if (!cmpl_state_okay (filesel->cmpl_state))
719 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
721 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
725 gtk_file_selection_populate (filesel, "", FALSE);
728 gtk_widget_grab_focus (filesel->selection_entry);
732 gtk_file_selection_new (const gchar *title)
734 GtkFileSelection *filesel;
736 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
737 gtk_window_set_title (GTK_WINDOW (filesel), title);
738 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
740 return GTK_WIDGET (filesel);
744 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
746 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
748 /* delete, create directory, and rename */
749 if (!filesel->fileop_c_dir)
751 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
752 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
753 (GtkSignalFunc) gtk_file_selection_create_dir,
755 gtk_box_pack_start (GTK_BOX (filesel->button_area),
756 filesel->fileop_c_dir, TRUE, TRUE, 0);
757 gtk_widget_show (filesel->fileop_c_dir);
760 if (!filesel->fileop_del_file)
762 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
763 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
764 (GtkSignalFunc) gtk_file_selection_delete_file,
766 gtk_box_pack_start (GTK_BOX (filesel->button_area),
767 filesel->fileop_del_file, TRUE, TRUE, 0);
768 gtk_widget_show (filesel->fileop_del_file);
771 if (!filesel->fileop_ren_file)
773 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
774 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
775 (GtkSignalFunc) gtk_file_selection_rename_file,
777 gtk_box_pack_start (GTK_BOX (filesel->button_area),
778 filesel->fileop_ren_file, TRUE, TRUE, 0);
779 gtk_widget_show (filesel->fileop_ren_file);
781 g_object_notify (G_OBJECT (filesel), "show_fileops");
782 gtk_widget_queue_resize (GTK_WIDGET (filesel));
786 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
788 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
790 if (filesel->fileop_ren_file)
792 gtk_widget_destroy (filesel->fileop_ren_file);
793 filesel->fileop_ren_file = NULL;
796 if (filesel->fileop_del_file)
798 gtk_widget_destroy (filesel->fileop_del_file);
799 filesel->fileop_del_file = NULL;
802 if (filesel->fileop_c_dir)
804 gtk_widget_destroy (filesel->fileop_c_dir);
805 filesel->fileop_c_dir = NULL;
807 g_object_notify (G_OBJECT (filesel), "show_fileops");
813 gtk_file_selection_set_filename (GtkFileSelection *filesel,
814 const gchar *filename)
817 const char *name, *last_slash;
819 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
820 g_return_if_fail (filename != NULL);
822 last_slash = strrchr (filename, G_DIR_SEPARATOR);
831 buf = g_strdup (filename);
832 buf[last_slash - filename + 1] = 0;
833 name = last_slash + 1;
836 gtk_file_selection_populate (filesel, buf, FALSE);
838 if (filesel->selection_entry)
839 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
841 g_object_notify (G_OBJECT (filesel), "filename");
845 * gtk_file_selection_get_filename:
846 * @filesel: a #GtkFileSelection
848 * This function returns the selected filename in the C runtime's
849 * multibyte string encoding, which may or may not be the same as that
850 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
851 * The returned string points to a statically allocated buffer and
852 * should be copied if you plan to keep it around.
854 * Return value: currently-selected filename in locale's encoding
856 G_CONST_RETURN gchar*
857 gtk_file_selection_get_filename (GtkFileSelection *filesel)
859 static gchar nothing[2] = "";
860 static gchar something[MAXPATHLEN*2];
864 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
867 translate_win32_path (filesel);
869 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
872 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
875 strncpy (something, sys_filename, sizeof (something));
876 g_free (sys_filename);
884 gtk_file_selection_complete (GtkFileSelection *filesel,
885 const gchar *pattern)
887 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
888 g_return_if_fail (pattern != NULL);
890 if (filesel->selection_entry)
891 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
892 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
896 gtk_file_selection_destroy (GtkObject *object)
898 GtkFileSelection *filesel;
900 HistoryCallbackArg *callback_arg;
902 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
904 filesel = GTK_FILE_SELECTION (object);
906 if (filesel->fileop_dialog)
908 gtk_widget_destroy (filesel->fileop_dialog);
909 filesel->fileop_dialog = NULL;
912 if (filesel->history_list)
914 list = filesel->history_list;
917 callback_arg = list->data;
918 g_free (callback_arg->directory);
919 g_free (callback_arg);
922 g_list_free (filesel->history_list);
923 filesel->history_list = NULL;
926 if (filesel->cmpl_state)
928 cmpl_free_state (filesel->cmpl_state);
929 filesel->cmpl_state = NULL;
932 GTK_OBJECT_CLASS (parent_class)->destroy (object);
935 /* Begin file operations callbacks */
938 gtk_file_selection_fileop_error (GtkFileSelection *fs,
939 gchar *error_message)
943 g_return_if_fail (error_message != NULL);
946 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
947 GTK_DIALOG_DESTROY_WITH_PARENT,
950 "%s", error_message);
952 /* yes, we free it */
953 g_free (error_message);
955 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
957 gtk_signal_connect_object (GTK_OBJECT (dialog), "response",
958 (GtkSignalFunc) gtk_widget_destroy,
961 gtk_widget_show (dialog);
965 gtk_file_selection_fileop_destroy (GtkWidget *widget,
968 GtkFileSelection *fs = data;
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 (GTK_IS_FILE_SELECTION (fs));
991 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
992 cmpl_state = (CompletionState*) fs->cmpl_state;
993 path = cmpl_reference_position (cmpl_state);
995 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
996 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
999 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1000 buf = g_strdup_printf (_("The directory name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1002 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n%s"), dirname, error->message,
1003 _("You probably used symbols not allowed in filenames."));
1004 gtk_file_selection_fileop_error (fs, buf);
1005 g_error_free (error);
1009 if (mkdir (sys_full_path, 0755) < 0)
1011 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n"), dirname,
1012 g_strerror (errno));
1013 gtk_file_selection_fileop_error (fs, buf);
1018 g_free (sys_full_path);
1020 gtk_widget_destroy (fs->fileop_dialog);
1021 gtk_file_selection_populate (fs, "", FALSE);
1025 gtk_file_selection_create_dir (GtkWidget *widget,
1028 GtkFileSelection *fs = data;
1034 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1036 if (fs->fileop_dialog)
1040 dialog = gtk_dialog_new ();
1041 fs->fileop_dialog = dialog;
1042 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1043 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1045 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
1046 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1048 /* If file dialog is grabbed, grab option dialog */
1049 /* When option dialog is closed, file dialog will be grabbed again */
1050 if (GTK_WINDOW (fs)->modal)
1051 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1053 vbox = gtk_vbox_new (FALSE, 0);
1054 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1055 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1057 gtk_widget_show( vbox);
1059 label = gtk_label_new_with_mnemonic (_("_Directory name:"));
1060 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1061 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1062 gtk_widget_show (label);
1064 /* The directory entry widget */
1065 fs->fileop_entry = gtk_entry_new ();
1066 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1067 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1069 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1070 gtk_widget_show (fs->fileop_entry);
1073 button = gtk_button_new_with_label (_("Create"));
1074 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1075 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
1077 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1078 button, TRUE, TRUE, 0);
1079 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1080 gtk_widget_show (button);
1082 button = gtk_button_new_with_label (_("Cancel"));
1083 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1084 (GtkSignalFunc) gtk_widget_destroy,
1086 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1087 button, TRUE, TRUE, 0);
1088 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1089 gtk_widget_grab_default (button);
1090 gtk_widget_show (button);
1092 gtk_widget_show (dialog);
1096 gtk_file_selection_delete_file_confirmed (GtkWidget *widget,
1099 GtkFileSelection *fs = data;
1100 CompletionState *cmpl_state;
1103 gchar *sys_full_path;
1104 GError *error = NULL;
1107 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1109 cmpl_state = (CompletionState*) fs->cmpl_state;
1110 path = cmpl_reference_position (cmpl_state);
1112 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1113 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1116 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1117 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1120 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1121 fs->fileop_file, error->message,
1122 _("It probably contains symbols not allowed in filenames."));
1124 gtk_file_selection_fileop_error (fs, buf);
1125 g_error_free (error);
1129 if (unlink (sys_full_path) < 0)
1131 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1132 fs->fileop_file, g_strerror (errno));
1133 gtk_file_selection_fileop_error (fs, buf);
1138 g_free (sys_full_path);
1140 gtk_widget_destroy (fs->fileop_dialog);
1141 gtk_file_selection_populate (fs, "", FALSE);
1145 gtk_file_selection_delete_file (GtkWidget *widget,
1148 GtkFileSelection *fs = data;
1156 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1158 if (fs->fileop_dialog)
1161 #ifdef G_WITH_CYGWIN
1162 translate_win32_path (fs);
1165 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1166 if (strlen (filename) < 1)
1169 fs->fileop_file = filename;
1172 fs->fileop_dialog = dialog = gtk_dialog_new ();
1173 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1174 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1176 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1177 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1179 /* If file dialog is grabbed, grab option dialog */
1180 /* When option dialog is closed, file dialog will be grabbed again */
1181 if (GTK_WINDOW (fs)->modal)
1182 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1184 vbox = gtk_vbox_new (FALSE, 0);
1185 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1186 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1188 gtk_widget_show (vbox);
1190 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1191 label = gtk_label_new (buf);
1192 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1193 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1194 gtk_widget_show (label);
1198 button = gtk_button_new_with_label (_("Delete"));
1199 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1200 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1202 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1203 button, TRUE, TRUE, 0);
1204 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1205 gtk_widget_show (button);
1207 button = gtk_button_new_with_label (_("Cancel"));
1208 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1209 (GtkSignalFunc) gtk_widget_destroy,
1211 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1212 button, TRUE, TRUE, 0);
1213 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1214 gtk_widget_grab_default (button);
1215 gtk_widget_show (button);
1217 gtk_widget_show (dialog);
1222 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1225 GtkFileSelection *fs = data;
1229 gchar *new_filename;
1230 gchar *old_filename;
1231 gchar *sys_new_filename;
1232 gchar *sys_old_filename;
1233 CompletionState *cmpl_state;
1234 GError *error = NULL;
1236 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1238 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1239 cmpl_state = (CompletionState*) fs->cmpl_state;
1240 path = cmpl_reference_position (cmpl_state);
1242 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1243 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1245 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1248 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1249 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1251 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1252 new_filename, error->message,
1253 _("You probably used symbols not allowed in filenames."));
1254 gtk_file_selection_fileop_error (fs, buf);
1255 g_error_free (error);
1259 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1262 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1263 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1265 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1266 old_filename, error->message,
1267 _("It probably contains symbols not allowed in filenames."));
1268 gtk_file_selection_fileop_error (fs, buf);
1269 g_error_free (error);
1273 if (rename (sys_old_filename, sys_new_filename) < 0)
1275 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1276 sys_old_filename, sys_new_filename,
1277 g_strerror (errno));
1278 gtk_file_selection_fileop_error (fs, buf);
1282 g_free (sys_old_filename);
1285 g_free (new_filename);
1286 g_free (old_filename);
1287 g_free (sys_new_filename);
1289 gtk_widget_destroy (fs->fileop_dialog);
1290 gtk_file_selection_populate (fs, "", FALSE);
1294 gtk_file_selection_rename_file (GtkWidget *widget,
1297 GtkFileSelection *fs = data;
1304 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1306 if (fs->fileop_dialog)
1309 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1310 if (strlen (fs->fileop_file) < 1)
1314 fs->fileop_dialog = dialog = gtk_dialog_new ();
1315 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1316 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1318 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1319 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1321 /* If file dialog is grabbed, grab option dialog */
1322 /* When option dialog closed, file dialog will be grabbed again */
1323 if (GTK_WINDOW (fs)->modal)
1324 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1326 vbox = gtk_vbox_new (FALSE, 0);
1327 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1328 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1330 gtk_widget_show(vbox);
1332 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1333 label = gtk_label_new(buf);
1334 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1335 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1336 gtk_widget_show (label);
1339 /* New filename entry */
1340 fs->fileop_entry = gtk_entry_new ();
1341 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1343 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1344 gtk_widget_show (fs->fileop_entry);
1346 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1347 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1348 0, strlen (fs->fileop_file));
1351 button = gtk_button_new_with_label (_("Rename"));
1352 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1353 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1355 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1356 button, TRUE, TRUE, 0);
1357 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1358 gtk_widget_show (button);
1360 button = gtk_button_new_with_label (_("Cancel"));
1361 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1362 (GtkSignalFunc) gtk_widget_destroy,
1364 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1365 button, TRUE, TRUE, 0);
1366 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1367 gtk_widget_grab_default (button);
1368 gtk_widget_show (button);
1370 gtk_widget_show (dialog);
1374 gtk_file_selection_insert_text (GtkWidget *widget,
1375 const gchar *new_text,
1376 gint new_text_length,
1382 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1387 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "insert_text");
1397 gtk_file_selection_key_press (GtkWidget *widget,
1401 GtkFileSelection *fs;
1404 g_return_val_if_fail (widget != NULL, FALSE);
1405 g_return_val_if_fail (event != NULL, FALSE);
1407 if (event->keyval == GDK_Tab ||
1408 event->keyval == GDK_ISO_Left_Tab ||
1409 event->keyval == GDK_KP_Tab)
1411 fs = GTK_FILE_SELECTION (user_data);
1412 #ifdef G_WITH_CYGWIN
1413 translate_win32_path (fs);
1415 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1417 text = g_strdup (text);
1419 gtk_file_selection_populate (fs, text, TRUE);
1431 gtk_file_selection_history_callback (GtkWidget *widget,
1434 GtkFileSelection *fs = data;
1435 HistoryCallbackArg *callback_arg;
1438 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1440 list = fs->history_list;
1443 callback_arg = list->data;
1445 if (callback_arg->menu_item == widget)
1447 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1456 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1457 gchar *current_directory)
1459 HistoryCallbackArg *callback_arg;
1460 GtkWidget *menu_item;
1466 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1467 g_return_if_fail (current_directory != NULL);
1469 list = fs->history_list;
1471 if (fs->history_menu)
1474 callback_arg = list->data;
1475 g_free (callback_arg->directory);
1476 g_free (callback_arg);
1479 g_list_free (fs->history_list);
1480 fs->history_list = NULL;
1482 gtk_widget_destroy (fs->history_menu);
1485 fs->history_menu = gtk_menu_new ();
1487 current_dir = g_strdup (current_directory);
1489 dir_len = strlen (current_dir);
1491 for (i = dir_len; i >= 0; i--)
1493 /* the i == dir_len is to catch the full path for the first
1495 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1497 /* another small hack to catch the full path */
1499 current_dir[i + 1] = '\0';
1500 #ifdef G_WITH_CYGWIN
1501 if (!strcmp (current_dir, "//"))
1504 menu_item = gtk_menu_item_new_with_label (current_dir);
1506 callback_arg = g_new (HistoryCallbackArg, 1);
1507 callback_arg->menu_item = menu_item;
1509 /* since the autocompletion gets confused if you don't
1510 * supply a trailing '/' on a dir entry, set the full
1511 * (current) path to "" which just refreshes the filesel */
1514 callback_arg->directory = g_strdup ("");
1518 callback_arg->directory = g_strdup (current_dir);
1521 fs->history_list = g_list_append (fs->history_list, callback_arg);
1523 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1524 (GtkSignalFunc) gtk_file_selection_history_callback,
1526 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1527 gtk_widget_show (menu_item);
1531 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1533 g_free (current_dir);
1537 gtk_file_selection_file_button (GtkWidget *widget,
1540 GdkEventButton *bevent,
1543 GtkFileSelection *fs = NULL;
1544 gchar *filename, *temp = NULL;
1546 g_return_if_fail (GTK_IS_CLIST (widget));
1549 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1551 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1552 filename = g_strdup (temp);
1554 #ifdef G_WITH_CYGWIN
1555 /* Check to see if the selection was a drive selector */
1556 if (isalpha (filename[0]) && (filename[1] == ':'))
1558 /* It is... map it to a CYGWIN32 drive */
1559 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1561 filename = temp_filename;
1563 #endif /* G_WITH_CYGWIN */
1568 switch (bevent->type)
1570 case GDK_2BUTTON_PRESS:
1571 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1575 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1579 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1586 gtk_file_selection_dir_button (GtkWidget *widget,
1589 GdkEventButton *bevent,
1592 GtkFileSelection *fs = NULL;
1593 gchar *filename, *temp = NULL;
1595 g_return_if_fail (GTK_IS_CLIST (widget));
1597 fs = GTK_FILE_SELECTION (user_data);
1598 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1600 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1601 filename = g_strdup (temp);
1606 switch (bevent->type)
1608 case GDK_2BUTTON_PRESS:
1609 gtk_file_selection_populate (fs, filename, FALSE);
1613 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1617 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1623 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1626 win32_gtk_add_drives_to_dir_list (GtkWidget *the_dir_list)
1628 gchar *text[2], *textPtr;
1630 char volumeNameBuf[128];
1631 char formatBuffer[128];
1635 /* Get the Drives string */
1636 GetLogicalDriveStrings (sizeof (buffer), buffer);
1638 /* Add the drives as necessary */
1640 while (*textPtr != '\0') {
1641 /* Get the volume information for this drive */
1642 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1644 /* Ignore floppies (?) */
1645 DWORD maxComponentLength, flags;
1647 GetVolumeInformation (textPtr,
1648 volumeNameBuf, sizeof(volumeNameBuf),
1649 NULL, &maxComponentLength,
1651 /* Build the actual displayable string */
1653 sprintf (formatBuffer, "%c:\\", toupper(textPtr[0]));
1654 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1655 if (strlen (volumeNameBuf) > 0)
1656 sprintf (formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1658 /* Add to the list */
1659 text[0] = formatBuffer;
1660 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1662 textPtr += (strlen (textPtr) + 1);
1668 gtk_file_selection_populate (GtkFileSelection *fs,
1672 CompletionState *cmpl_state;
1673 PossibleCompletion* poss;
1675 gchar* rem_path = rel_path;
1678 gint did_recurse = FALSE;
1679 gint possible_count = 0;
1680 gint selection_index = -1;
1682 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1684 cmpl_state = (CompletionState*) fs->cmpl_state;
1685 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1687 if (!cmpl_state_okay (cmpl_state))
1689 /* Something went wrong. */
1690 gtk_file_selection_abort (fs);
1694 g_assert (cmpl_state->reference_dir);
1696 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1697 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1698 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1699 gtk_clist_clear (GTK_CLIST (fs->file_list));
1701 /* Set the dir_list to include ./ and ../ */
1703 text[0] = "." G_DIR_SEPARATOR_S;
1704 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1706 text[0] = ".." G_DIR_SEPARATOR_S;
1707 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1711 if (cmpl_is_a_completion (poss))
1713 possible_count += 1;
1715 filename = cmpl_this_completion (poss);
1719 if (cmpl_is_directory (poss))
1721 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1722 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1723 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1727 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1731 poss = cmpl_next_completion (cmpl_state);
1734 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1735 /* For Windows, add drives as potential selections */
1736 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1739 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1740 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1742 /* File lists are set. */
1744 g_assert (cmpl_state->reference_dir);
1749 /* User is trying to complete filenames, so advance the user's input
1750 * string to the updated_text, which is the common leading substring
1751 * of all possible completions, and if its a directory attempt
1752 * attempt completions in it. */
1754 if (cmpl_updated_text (cmpl_state)[0])
1757 if (cmpl_updated_dir (cmpl_state))
1759 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1763 gtk_file_selection_populate (fs, dir_name, TRUE);
1769 if (fs->selection_entry)
1770 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1771 cmpl_updated_text (cmpl_state));
1776 selection_index = cmpl_last_valid_char (cmpl_state) -
1777 (strlen (rel_path) - strlen (rem_path));
1778 if (fs->selection_entry)
1779 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1784 if (fs->selection_entry)
1785 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1790 if (fs->selection_entry)
1791 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1793 if (fs->selection_entry)
1795 sel_text = g_strconcat (_("Selection: "),
1796 cmpl_reference_position (cmpl_state),
1799 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1803 if (fs->history_pulldown)
1805 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1812 gtk_file_selection_abort (GtkFileSelection *fs)
1816 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1818 /* BEEP gdk_beep(); */
1820 if (fs->selection_entry)
1821 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1824 /**********************************************************************/
1825 /* External Interface */
1826 /**********************************************************************/
1828 /* The four completion state selectors
1831 cmpl_updated_text (CompletionState *cmpl_state)
1833 return cmpl_state->updated_text;
1837 cmpl_updated_dir (CompletionState *cmpl_state)
1839 return cmpl_state->re_complete;
1843 cmpl_reference_position (CompletionState *cmpl_state)
1845 return cmpl_state->reference_dir->fullname;
1849 cmpl_last_valid_char (CompletionState *cmpl_state)
1851 return cmpl_state->last_valid_char;
1855 cmpl_completion_fullname (gchar *text,
1856 CompletionState *cmpl_state)
1858 static char nothing[2] = "";
1860 if (!cmpl_state_okay (cmpl_state))
1864 else if (g_path_is_absolute (text))
1866 strcpy (cmpl_state->updated_text, text);
1869 else if (text[0] == '~')
1874 dir = open_user_dir (text, cmpl_state);
1878 /* spencer says just return ~something, so
1879 * for now just do it. */
1880 strcpy (cmpl_state->updated_text, text);
1885 strcpy (cmpl_state->updated_text, dir->fullname);
1887 slash = strchr (text, G_DIR_SEPARATOR);
1890 strcat (cmpl_state->updated_text, slash);
1896 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1897 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1898 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1899 strcat (cmpl_state->updated_text, text);
1902 return cmpl_state->updated_text;
1905 /* The three completion selectors
1908 cmpl_this_completion (PossibleCompletion* pc)
1914 cmpl_is_directory (PossibleCompletion* pc)
1916 return pc->is_directory;
1920 cmpl_is_a_completion (PossibleCompletion* pc)
1922 return pc->is_a_completion;
1925 /**********************************************************************/
1926 /* Construction, deletion */
1927 /**********************************************************************/
1929 static CompletionState*
1930 cmpl_init_state (void)
1932 gchar *sys_getcwd_buf;
1934 CompletionState *new_state;
1936 new_state = g_new (CompletionState, 1);
1938 /* g_get_current_dir() returns a string in the "system" charset */
1939 sys_getcwd_buf = g_get_current_dir ();
1940 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
1941 g_free (sys_getcwd_buf);
1945 new_state->reference_dir = NULL;
1946 new_state->completion_dir = NULL;
1947 new_state->active_completion_dir = NULL;
1948 new_state->directory_storage = NULL;
1949 new_state->directory_sent_storage = NULL;
1950 new_state->last_valid_char = 0;
1951 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1952 new_state->updated_text_alloc = MAXPATHLEN;
1953 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1954 new_state->the_completion.text_alloc = MAXPATHLEN;
1955 new_state->user_dir_name_buffer = NULL;
1956 new_state->user_directories = NULL;
1958 new_state->reference_dir = open_dir (utf8_cwd, new_state);
1960 if (!new_state->reference_dir)
1962 /* Directories changing from underneath us, grumble */
1963 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
1972 cmpl_free_dir_list (GList* dp0)
1978 free_dir (dp->data);
1986 cmpl_free_dir_sent_list (GList* dp0)
1992 free_dir_sent (dp->data);
2000 cmpl_free_state (CompletionState* cmpl_state)
2002 g_return_if_fail (cmpl_state != NULL);
2004 cmpl_free_dir_list (cmpl_state->directory_storage);
2005 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2007 if (cmpl_state->user_dir_name_buffer)
2008 g_free (cmpl_state->user_dir_name_buffer);
2009 if (cmpl_state->user_directories)
2010 g_free (cmpl_state->user_directories);
2011 if (cmpl_state->the_completion.text)
2012 g_free (cmpl_state->the_completion.text);
2013 if (cmpl_state->updated_text)
2014 g_free (cmpl_state->updated_text);
2016 g_free (cmpl_state);
2020 free_dir (CompletionDir* dir)
2022 g_free (dir->fullname);
2027 free_dir_sent (CompletionDirSent* sent)
2030 for (i = 0; i < sent->entry_count; i++)
2031 g_free (sent->entries[i].entry_name);
2032 g_free (sent->entries);
2037 prune_memory_usage (CompletionState *cmpl_state)
2039 GList* cdsl = cmpl_state->directory_sent_storage;
2040 GList* cdl = cmpl_state->directory_storage;
2044 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2049 cmpl_free_dir_sent_list (cdsl->next);
2053 cmpl_state->directory_storage = NULL;
2056 if (cdl->data == cmpl_state->reference_dir)
2057 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2059 free_dir (cdl->data);
2066 /**********************************************************************/
2067 /* The main entrances. */
2068 /**********************************************************************/
2070 static PossibleCompletion*
2071 cmpl_completion_matches (gchar *text_to_complete,
2072 gchar **remaining_text,
2073 CompletionState *cmpl_state)
2076 PossibleCompletion *poss;
2078 prune_memory_usage (cmpl_state);
2080 g_assert (text_to_complete != NULL);
2082 cmpl_state->user_completion_index = -1;
2083 cmpl_state->last_completion_text = text_to_complete;
2084 cmpl_state->the_completion.text[0] = 0;
2085 cmpl_state->last_valid_char = 0;
2086 cmpl_state->updated_text_len = -1;
2087 cmpl_state->updated_text[0] = 0;
2088 cmpl_state->re_complete = FALSE;
2091 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2093 if (text_to_complete[0] == '~' && !first_slash)
2095 /* Text starts with ~ and there is no slash, show all the
2096 * home directory completions.
2098 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2100 update_cmpl (poss, cmpl_state);
2105 cmpl_state->reference_dir =
2106 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2108 if (!cmpl_state->reference_dir)
2111 cmpl_state->completion_dir =
2112 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2114 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2116 if (!cmpl_state->completion_dir)
2119 cmpl_state->completion_dir->cmpl_index = -1;
2120 cmpl_state->completion_dir->cmpl_parent = NULL;
2121 cmpl_state->completion_dir->cmpl_text = *remaining_text;
2123 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2125 cmpl_state->reference_dir = cmpl_state->completion_dir;
2127 poss = attempt_file_completion (cmpl_state);
2129 update_cmpl (poss, cmpl_state);
2134 static PossibleCompletion*
2135 cmpl_next_completion (CompletionState* cmpl_state)
2137 PossibleCompletion* poss = NULL;
2139 cmpl_state->the_completion.text[0] = 0;
2142 if (cmpl_state->user_completion_index >= 0)
2143 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2145 poss = attempt_file_completion (cmpl_state);
2147 poss = attempt_file_completion (cmpl_state);
2150 update_cmpl (poss, cmpl_state);
2155 /**********************************************************************/
2156 /* Directory Operations */
2157 /**********************************************************************/
2159 /* Open the directory where completion will begin from, if possible. */
2160 static CompletionDir*
2161 open_ref_dir (gchar *text_to_complete,
2162 gchar **remaining_text,
2163 CompletionState *cmpl_state)
2166 CompletionDir *new_dir;
2168 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2170 #ifdef G_WITH_CYGWIN
2171 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2174 sprintf (root_dir, "//%c", text_to_complete[2]);
2176 new_dir = open_dir (root_dir, cmpl_state);
2179 *remaining_text = text_to_complete + 4;
2187 else if (text_to_complete[0] == '~')
2189 new_dir = open_user_dir (text_to_complete, cmpl_state);
2194 *remaining_text = first_slash + 1;
2196 *remaining_text = text_to_complete + strlen (text_to_complete);
2204 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2206 gchar *tmp = g_strdup (text_to_complete);
2210 while (*p && *p != '*' && *p != '?')
2214 p = strrchr (tmp, G_DIR_SEPARATOR);
2222 new_dir = open_dir (tmp, cmpl_state);
2225 *remaining_text = text_to_complete +
2226 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2230 /* If no possible candidates, use the cwd */
2231 gchar *sys_curdir = g_get_current_dir ();
2232 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2234 g_free (sys_curdir);
2236 new_dir = open_dir (utf8_curdir, cmpl_state);
2239 *remaining_text = text_to_complete;
2241 g_free (utf8_curdir);
2248 *remaining_text = text_to_complete;
2250 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2255 new_dir->cmpl_index = -1;
2256 new_dir->cmpl_parent = NULL;
2264 /* open a directory by user name */
2265 static CompletionDir*
2266 open_user_dir (gchar *text_to_complete,
2267 CompletionState *cmpl_state)
2269 CompletionDir *result;
2273 g_assert (text_to_complete && text_to_complete[0] == '~');
2275 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2278 cmp_len = first_slash - text_to_complete - 1;
2280 cmp_len = strlen (text_to_complete + 1);
2285 gchar *homedir = g_get_home_dir ();
2286 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2291 result = open_dir (utf8_homedir, cmpl_state);
2295 g_free (utf8_homedir);
2300 gchar* copy = g_new (char, cmp_len + 1);
2304 strncpy (copy, text_to_complete + 1, cmp_len);
2306 pwd = getpwnam (copy);
2313 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2314 result = open_dir (utf8_dir, cmpl_state);
2322 /* open a directory relative the the current relative directory */
2323 static CompletionDir*
2324 open_relative_dir (gchar *dir_name,
2326 CompletionState *cmpl_state)
2328 CompletionDir *result;
2331 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2332 g_string_assign (path, dir->fullname);
2334 if (dir->fullname_len > 1
2335 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2336 g_string_append_c (path, G_DIR_SEPARATOR);
2337 g_string_append (path, dir_name);
2339 result = open_dir (path->str, cmpl_state);
2341 g_string_free (path, TRUE);
2346 /* after the cache lookup fails, really open a new directory */
2347 static CompletionDirSent*
2348 open_new_dir (gchar *dir_name,
2350 gboolean stat_subdirs)
2352 CompletionDirSent *sent;
2354 struct dirent *dirent_ptr;
2355 gint entry_count = 0;
2358 struct stat ent_sbuf;
2360 gchar *sys_dir_name;
2362 sent = g_new (CompletionDirSent, 1);
2363 sent->mtime = sbuf->st_mtime;
2364 sent->inode = sbuf->st_ino;
2365 sent->device = sbuf->st_dev;
2367 path = g_string_sized_new (2*MAXPATHLEN + 10);
2369 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2372 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2376 directory = opendir (sys_dir_name);
2380 g_free (sys_dir_name);
2384 while ((dirent_ptr = readdir (directory)) != NULL)
2387 sent->entries = g_new (CompletionDirEntry, entry_count);
2388 sent->entry_count = entry_count;
2390 rewinddir (directory);
2392 for (i = 0; i < entry_count; i += 1)
2394 dirent_ptr = readdir (directory);
2399 closedir (directory);
2400 g_free (sys_dir_name);
2404 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent_ptr->d_name, -1, NULL, NULL, NULL);
2405 if (!g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2407 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent_ptr->d_name);
2411 g_string_assign (path, sys_dir_name);
2412 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2414 g_string_append_c (path, G_DIR_SEPARATOR);
2416 g_string_append (path, dirent_ptr->d_name);
2420 /* Here we know path->str is a "system charset" string */
2421 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2422 sent->entries[n_entries].is_dir = TRUE;
2424 /* stat may fail, and we don't mind, since it could be a
2425 * dangling symlink. */
2426 sent->entries[n_entries].is_dir = FALSE;
2429 sent->entries[n_entries].is_dir = 1;
2433 sent->entry_count = n_entries;
2435 g_free (sys_dir_name);
2436 g_string_free (path, TRUE);
2437 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2439 closedir (directory);
2444 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2447 check_dir (gchar *dir_name,
2448 struct stat *result,
2449 gboolean *stat_subdirs)
2451 /* A list of directories that we know only contain other directories.
2452 * Trying to stat every file in these directories would be very
2459 struct stat statbuf;
2460 } no_stat_dirs[] = {
2461 { "/afs", FALSE, { 0 } },
2462 { "/net", FALSE, { 0 } }
2465 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
2466 static gboolean initialized = FALSE;
2467 gchar *sys_dir_name;
2473 for (i = 0; i < n_no_stat_dirs; i++)
2475 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2476 no_stat_dirs[i].present = TRUE;
2480 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2483 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2487 if (stat (sys_dir_name, result) < 0)
2489 g_free (sys_dir_name);
2493 g_free (sys_dir_name);
2495 *stat_subdirs = TRUE;
2496 for (i = 0; i < n_no_stat_dirs; i++)
2498 if (no_stat_dirs[i].present &&
2499 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2500 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2502 *stat_subdirs = FALSE;
2512 /* open a directory by absolute pathname */
2513 static CompletionDir*
2514 open_dir (gchar *dir_name,
2515 CompletionState *cmpl_state)
2518 gboolean stat_subdirs;
2519 CompletionDirSent *sent;
2522 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2523 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2526 cdsl = cmpl_state->directory_sent_storage;
2532 if (sent->inode == sbuf.st_ino &&
2533 sent->mtime == sbuf.st_mtime &&
2534 sent->device == sbuf.st_dev)
2535 return attach_dir (sent, dir_name, cmpl_state);
2540 stat_subdirs = TRUE;
2543 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
2547 cmpl_state->directory_sent_storage =
2548 g_list_prepend (cmpl_state->directory_sent_storage, sent);
2550 return attach_dir (sent, dir_name, cmpl_state);
2556 static CompletionDir*
2557 attach_dir (CompletionDirSent *sent,
2559 CompletionState *cmpl_state)
2561 CompletionDir* new_dir;
2563 new_dir = g_new (CompletionDir, 1);
2565 cmpl_state->directory_storage =
2566 g_list_prepend (cmpl_state->directory_storage, new_dir);
2568 new_dir->sent = sent;
2569 new_dir->fullname = g_strdup (dir_name);
2570 new_dir->fullname_len = strlen (dir_name);
2576 correct_dir_fullname (CompletionDir* cmpl_dir)
2578 gint length = strlen (cmpl_dir->fullname);
2579 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2580 gchar *sys_filename;
2583 /* Does it end with /. (\.) ? */
2585 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2587 /* Is it just the root directory (on a drive) ? */
2588 if (cmpl_dir->fullname + length - 2 == first_slash)
2590 cmpl_dir->fullname[length - 1] = 0;
2591 cmpl_dir->fullname_len = length - 1;
2596 cmpl_dir->fullname[length - 2] = 0;
2600 /* Ends with /./ (\.\)? */
2601 else if (length >= 3 &&
2602 strcmp (cmpl_dir->fullname + length - 3,
2603 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2604 cmpl_dir->fullname[length - 2] = 0;
2606 /* Ends with /.. (\..) ? */
2607 else if (length >= 3 &&
2608 strcmp (cmpl_dir->fullname + length - 3,
2609 G_DIR_SEPARATOR_S "..") == 0)
2611 /* Is it just /.. (X:\..)? */
2612 if (cmpl_dir->fullname + length - 3 == first_slash)
2614 cmpl_dir->fullname[length - 2] = 0;
2615 cmpl_dir->fullname_len = length - 2;
2619 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2622 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2626 if (stat (sys_filename, &sbuf) < 0)
2628 g_free (sys_filename);
2632 g_free (sys_filename);
2634 cmpl_dir->fullname[length - 3] = 0;
2636 if (!correct_parent (cmpl_dir, &sbuf))
2640 /* Ends with /../ (\..\)? */
2641 else if (length >= 4 &&
2642 strcmp (cmpl_dir->fullname + length - 4,
2643 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2645 /* Is it just /../ (X:\..\)? */
2646 if (cmpl_dir->fullname + length - 4 == first_slash)
2648 cmpl_dir->fullname[length - 3] = 0;
2649 cmpl_dir->fullname_len = length - 3;
2653 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2656 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2660 if (stat (sys_filename, &sbuf) < 0)
2662 g_free (sys_filename);
2666 g_free (sys_filename);
2668 cmpl_dir->fullname[length - 4] = 0;
2670 if (!correct_parent (cmpl_dir, &sbuf))
2674 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
2680 correct_parent (CompletionDir *cmpl_dir,
2687 gchar *sys_filename;
2690 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2691 g_assert (last_slash);
2692 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2694 /* Clever (?) way to check for top-level directory that works also on
2695 * Win32, where there is a drive letter and colon prefixed...
2697 if (last_slash != first_slash)
2707 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2710 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2712 last_slash[0] = G_DIR_SEPARATOR;
2716 if (stat (sys_filename, &parbuf) < 0)
2718 g_free (sys_filename);
2721 last_slash[0] = G_DIR_SEPARATOR;
2724 g_free (sys_filename);
2726 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2727 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2728 /* it wasn't a link */
2734 last_slash[0] = G_DIR_SEPARATOR;
2736 /* it was a link, have to figure it out the hard way */
2738 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
2743 g_free (cmpl_dir->fullname);
2745 cmpl_dir->fullname = new_name;
2754 find_parent_dir_fullname (gchar* dirname)
2756 gchar *sys_orig_dir;
2761 sys_orig_dir = g_get_current_dir ();
2762 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
2765 g_free (sys_orig_dir);
2766 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2770 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
2772 g_free (sys_dirname);
2773 g_free (sys_orig_dir);
2777 g_free (sys_dirname);
2779 sys_cwd = g_get_current_dir ();
2780 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
2783 if (chdir (sys_orig_dir) != 0)
2786 g_free (sys_orig_dir);
2790 g_free (sys_orig_dir);
2796 /**********************************************************************/
2797 /* Completion Operations */
2798 /**********************************************************************/
2802 static PossibleCompletion*
2803 attempt_homedir_completion (gchar *text_to_complete,
2804 CompletionState *cmpl_state)
2808 if (!cmpl_state->user_dir_name_buffer &&
2809 !get_pwdb (cmpl_state))
2811 length = strlen (text_to_complete) - 1;
2813 cmpl_state->user_completion_index += 1;
2815 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2817 index = first_diff_index (text_to_complete + 1,
2818 cmpl_state->user_directories
2819 [cmpl_state->user_completion_index].login);
2826 if (cmpl_state->last_valid_char < (index + 1))
2827 cmpl_state->last_valid_char = index + 1;
2828 cmpl_state->user_completion_index += 1;
2832 cmpl_state->the_completion.is_a_completion = 1;
2833 cmpl_state->the_completion.is_directory = TRUE;
2835 append_completion_text ("~", cmpl_state);
2837 append_completion_text (cmpl_state->
2838 user_directories[cmpl_state->user_completion_index].login,
2841 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
2844 if (text_to_complete[1]
2845 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2847 cmpl_state->user_completion_index = -1;
2852 cmpl_state->user_completion_index += 1;
2853 cmpl_state->the_completion.is_a_completion = 1;
2854 cmpl_state->the_completion.is_directory = TRUE;
2856 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
2862 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2863 #define FOLD(c) (tolower(c))
2868 /* returns the index (>= 0) of the first differing character,
2869 * PATTERN_MATCH if the completion matches */
2871 first_diff_index (gchar *pat,
2876 while (*pat && *text && FOLD (*text) == FOLD (*pat))
2886 return PATTERN_MATCH;
2889 static PossibleCompletion*
2890 append_completion_text (gchar *text,
2891 CompletionState *cmpl_state)
2895 if (!cmpl_state->the_completion.text)
2898 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
2900 if (cmpl_state->the_completion.text_alloc > len)
2902 strcat (cmpl_state->the_completion.text, text);
2903 return &cmpl_state->the_completion;
2909 cmpl_state->the_completion.text_alloc = i;
2911 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
2913 if (!cmpl_state->the_completion.text)
2917 strcat (cmpl_state->the_completion.text, text);
2918 return &cmpl_state->the_completion;
2922 static CompletionDir*
2923 find_completion_dir (gchar *text_to_complete,
2924 gchar **remaining_text,
2925 CompletionState *cmpl_state)
2927 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2928 CompletionDir* dir = cmpl_state->reference_dir;
2929 CompletionDir* next;
2930 *remaining_text = text_to_complete;
2934 gint len = first_slash - *remaining_text;
2936 gchar *found_name = NULL; /* Quiet gcc */
2938 gchar* pat_buf = g_new (gchar, len + 1);
2940 strncpy (pat_buf, *remaining_text, len);
2943 for (i = 0; i < dir->sent->entry_count; i += 1)
2945 if (dir->sent->entries[i].is_dir &&
2946 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
2947 FNMATCH_FLAGS)!= FNM_NOMATCH)
2957 found_name = dir->sent->entries[i].entry_name;
2964 /* Perhaps we are trying to open an automount directory */
2965 found_name = pat_buf;
2968 next = open_relative_dir (found_name, dir, cmpl_state);
2976 next->cmpl_parent = dir;
2980 if (!correct_dir_fullname (dir))
2986 *remaining_text = first_slash + 1;
2987 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
2996 update_cmpl (PossibleCompletion *poss,
2997 CompletionState *cmpl_state)
3001 if (!poss || !cmpl_is_a_completion (poss))
3004 cmpl_len = strlen (cmpl_this_completion (poss));
3006 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3008 cmpl_state->updated_text =
3009 (gchar*)g_realloc (cmpl_state->updated_text,
3010 cmpl_state->updated_text_alloc);
3011 cmpl_state->updated_text_alloc = 2*cmpl_len;
3014 if (cmpl_state->updated_text_len < 0)
3016 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3017 cmpl_state->updated_text_len = cmpl_len;
3018 cmpl_state->re_complete = cmpl_is_directory (poss);
3020 else if (cmpl_state->updated_text_len == 0)
3022 cmpl_state->re_complete = FALSE;
3027 first_diff_index (cmpl_state->updated_text,
3028 cmpl_this_completion (poss));
3030 cmpl_state->re_complete = FALSE;
3032 if (first_diff == PATTERN_MATCH)
3035 if (first_diff > cmpl_state->updated_text_len)
3036 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3038 cmpl_state->updated_text_len = first_diff;
3039 cmpl_state->updated_text[first_diff] = 0;
3043 static PossibleCompletion*
3044 attempt_file_completion (CompletionState *cmpl_state)
3046 gchar *pat_buf, *first_slash;
3047 CompletionDir *dir = cmpl_state->active_completion_dir;
3049 dir->cmpl_index += 1;
3051 if (dir->cmpl_index == dir->sent->entry_count)
3053 if (dir->cmpl_parent == NULL)
3055 cmpl_state->active_completion_dir = NULL;
3061 cmpl_state->active_completion_dir = dir->cmpl_parent;
3063 return attempt_file_completion (cmpl_state);
3067 g_assert (dir->cmpl_text);
3069 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3073 gint len = first_slash - dir->cmpl_text;
3075 pat_buf = g_new (gchar, len + 1);
3076 strncpy (pat_buf, dir->cmpl_text, len);
3081 gint len = strlen (dir->cmpl_text);
3083 pat_buf = g_new (gchar, len + 2);
3084 strcpy (pat_buf, dir->cmpl_text);
3085 /* Don't append a * if the user entered one herself.
3086 * This way one can complete *.h and don't get matches
3087 * on any .help files, for instance.
3089 if (strchr (pat_buf, '*') == NULL)
3090 strcpy (pat_buf + len, "*");
3095 if (dir->sent->entries[dir->cmpl_index].is_dir)
3097 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3098 FNMATCH_FLAGS) != FNM_NOMATCH)
3100 CompletionDir* new_dir;
3102 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3111 new_dir->cmpl_parent = dir;
3113 new_dir->cmpl_index = -1;
3114 new_dir->cmpl_text = first_slash + 1;
3116 cmpl_state->active_completion_dir = new_dir;
3119 return attempt_file_completion (cmpl_state);
3124 return attempt_file_completion (cmpl_state);
3130 return attempt_file_completion (cmpl_state);
3135 if (dir->cmpl_parent != NULL)
3137 append_completion_text (dir->fullname +
3138 strlen (cmpl_state->completion_dir->fullname) + 1,
3140 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3143 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3145 cmpl_state->the_completion.is_a_completion =
3146 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3147 FNMATCH_FLAGS) != FNM_NOMATCH;
3149 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3150 if (dir->sent->entries[dir->cmpl_index].is_dir)
3151 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3154 return &cmpl_state->the_completion;
3161 get_pwdb (CompletionState* cmpl_state)
3163 struct passwd *pwd_ptr;
3166 gint len = 0, i, count = 0;
3168 if (cmpl_state->user_dir_name_buffer)
3172 while ((pwd_ptr = getpwent ()) != NULL)
3174 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3175 len += strlen (utf8);
3177 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3178 len += strlen (utf8);
3186 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3187 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3188 cmpl_state->user_directories_len = count;
3190 buf_ptr = cmpl_state->user_dir_name_buffer;
3192 for (i = 0; i < count; i += 1)
3194 pwd_ptr = getpwent ();
3201 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3202 strcpy (buf_ptr, utf8);
3205 cmpl_state->user_directories[i].login = buf_ptr;
3207 buf_ptr += strlen (buf_ptr);
3210 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3211 strcpy (buf_ptr, utf8);
3214 cmpl_state->user_directories[i].homedir = buf_ptr;
3216 buf_ptr += strlen (buf_ptr);
3220 qsort (cmpl_state->user_directories,
3221 cmpl_state->user_directories_len,
3222 sizeof (CompletionUserDir),
3231 if (cmpl_state->user_dir_name_buffer)
3232 g_free (cmpl_state->user_dir_name_buffer);
3233 if (cmpl_state->user_directories)
3234 g_free (cmpl_state->user_directories);
3236 cmpl_state->user_dir_name_buffer = NULL;
3237 cmpl_state->user_directories = NULL;
3243 compare_user_dir (const void *a,
3246 return strcmp ((((CompletionUserDir*)a))->login,
3247 (((CompletionUserDir*)b))->login);
3253 compare_cmpl_dir (const void *a,
3256 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3257 return strcmp ((((CompletionDirEntry*)a))->entry_name,
3258 (((CompletionDirEntry*)b))->entry_name);
3260 return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
3261 (((CompletionDirEntry*)b))->entry_name);
3266 cmpl_state_okay (CompletionState* cmpl_state)
3268 return cmpl_state && cmpl_state->reference_dir;
3272 cmpl_strerror (gint err)
3274 if (err == CMPL_ERRNO_TOO_LONG)
3275 return _("Name too long");
3276 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3277 return _("Couldn't convert filename");
3279 return g_strerror (err);