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,
688 GTK_STOCK_BUTTON_CANCEL,
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 (filesel != NULL);
747 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
749 /* delete, create directory, and rename */
750 if (!filesel->fileop_c_dir)
752 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
753 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
754 (GtkSignalFunc) gtk_file_selection_create_dir,
756 gtk_box_pack_start (GTK_BOX (filesel->button_area),
757 filesel->fileop_c_dir, TRUE, TRUE, 0);
758 gtk_widget_show (filesel->fileop_c_dir);
761 if (!filesel->fileop_del_file)
763 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
764 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
765 (GtkSignalFunc) gtk_file_selection_delete_file,
767 gtk_box_pack_start (GTK_BOX (filesel->button_area),
768 filesel->fileop_del_file, TRUE, TRUE, 0);
769 gtk_widget_show (filesel->fileop_del_file);
772 if (!filesel->fileop_ren_file)
774 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
775 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
776 (GtkSignalFunc) gtk_file_selection_rename_file,
778 gtk_box_pack_start (GTK_BOX (filesel->button_area),
779 filesel->fileop_ren_file, TRUE, TRUE, 0);
780 gtk_widget_show (filesel->fileop_ren_file);
782 g_object_notify (G_OBJECT (filesel), "show_fileops");
783 gtk_widget_queue_resize (GTK_WIDGET (filesel));
787 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
789 g_return_if_fail (filesel != NULL);
790 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
792 if (filesel->fileop_ren_file)
794 gtk_widget_destroy (filesel->fileop_ren_file);
795 filesel->fileop_ren_file = NULL;
798 if (filesel->fileop_del_file)
800 gtk_widget_destroy (filesel->fileop_del_file);
801 filesel->fileop_del_file = NULL;
804 if (filesel->fileop_c_dir)
806 gtk_widget_destroy (filesel->fileop_c_dir);
807 filesel->fileop_c_dir = NULL;
809 g_object_notify (G_OBJECT (filesel), "show_fileops");
815 gtk_file_selection_set_filename (GtkFileSelection *filesel,
816 const gchar *filename)
819 const char *name, *last_slash;
821 g_return_if_fail (filesel != NULL);
822 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
823 g_return_if_fail (filename != NULL);
825 last_slash = strrchr (filename, G_DIR_SEPARATOR);
834 buf = g_strdup (filename);
835 buf[last_slash - filename + 1] = 0;
836 name = last_slash + 1;
839 gtk_file_selection_populate (filesel, buf, FALSE);
841 if (filesel->selection_entry)
842 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
844 g_object_notify (G_OBJECT (filesel), "filename");
848 * gtk_file_selection_get_filename:
849 * @filesel: a #GtkFileSelection
851 * This function returns the selected filename in the C runtime's
852 * multibyte string encoding, which may or may not be the same as that
853 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
854 * The returned string points to a statically allocated buffer and
855 * should be copied if you plan to keep it around.
857 * Return value: currently-selected filename in locale's encoding
859 G_CONST_RETURN gchar*
860 gtk_file_selection_get_filename (GtkFileSelection *filesel)
862 static gchar nothing[2] = "";
863 static gchar something[MAXPATHLEN*2];
867 g_return_val_if_fail (filesel != NULL, nothing);
868 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
871 translate_win32_path (filesel);
873 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
876 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
879 strncpy (something, sys_filename, sizeof (something));
880 g_free (sys_filename);
888 gtk_file_selection_complete (GtkFileSelection *filesel,
889 const gchar *pattern)
891 g_return_if_fail (filesel != NULL);
892 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
893 g_return_if_fail (pattern != NULL);
895 if (filesel->selection_entry)
896 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
897 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
901 gtk_file_selection_destroy (GtkObject *object)
903 GtkFileSelection *filesel;
905 HistoryCallbackArg *callback_arg;
907 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
909 filesel = GTK_FILE_SELECTION (object);
911 if (filesel->fileop_dialog)
913 gtk_widget_destroy (filesel->fileop_dialog);
914 filesel->fileop_dialog = NULL;
917 if (filesel->history_list)
919 list = filesel->history_list;
922 callback_arg = list->data;
923 g_free (callback_arg->directory);
924 g_free (callback_arg);
927 g_list_free (filesel->history_list);
928 filesel->history_list = NULL;
931 if (filesel->cmpl_state)
933 cmpl_free_state (filesel->cmpl_state);
934 filesel->cmpl_state = NULL;
937 GTK_OBJECT_CLASS (parent_class)->destroy (object);
940 /* Begin file operations callbacks */
943 gtk_file_selection_fileop_error (GtkFileSelection *fs,
944 gchar *error_message)
948 g_return_if_fail (error_message != NULL);
951 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
952 GTK_DIALOG_DESTROY_WITH_PARENT,
955 "%s", error_message);
957 /* yes, we free it */
958 g_free (error_message);
960 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
962 gtk_signal_connect_object (GTK_OBJECT (dialog), "response",
963 (GtkSignalFunc) gtk_widget_destroy,
966 gtk_widget_show (dialog);
970 gtk_file_selection_fileop_destroy (GtkWidget *widget,
973 GtkFileSelection *fs = data;
975 g_return_if_fail (fs != NULL);
976 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
978 fs->fileop_dialog = NULL;
983 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
986 GtkFileSelection *fs = data;
990 gchar *sys_full_path;
992 GError *error = NULL;
993 CompletionState *cmpl_state;
995 g_return_if_fail (fs != NULL);
996 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
998 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
999 cmpl_state = (CompletionState*) fs->cmpl_state;
1000 path = cmpl_reference_position (cmpl_state);
1002 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1003 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1006 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1007 buf = g_strdup_printf (_("The directory name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1009 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n%s"), dirname, error->message,
1010 _("You probably used symbols not allowed in filenames."));
1011 gtk_file_selection_fileop_error (fs, buf);
1012 g_error_free (error);
1016 if (mkdir (sys_full_path, 0755) < 0)
1018 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n"), dirname,
1019 g_strerror (errno));
1020 gtk_file_selection_fileop_error (fs, buf);
1025 g_free (sys_full_path);
1027 gtk_widget_destroy (fs->fileop_dialog);
1028 gtk_file_selection_populate (fs, "", FALSE);
1032 gtk_file_selection_create_dir (GtkWidget *widget,
1035 GtkFileSelection *fs = data;
1041 g_return_if_fail (fs != NULL);
1042 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1044 if (fs->fileop_dialog)
1048 dialog = gtk_dialog_new ();
1049 fs->fileop_dialog = dialog;
1050 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1051 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1053 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
1054 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1056 /* If file dialog is grabbed, grab option dialog */
1057 /* When option dialog is closed, file dialog will be grabbed again */
1058 if (GTK_WINDOW (fs)->modal)
1059 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1061 vbox = gtk_vbox_new (FALSE, 0);
1062 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1063 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1065 gtk_widget_show( vbox);
1067 label = gtk_label_new_with_mnemonic (_("_Directory name:"));
1068 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1069 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1070 gtk_widget_show (label);
1072 /* The directory entry widget */
1073 fs->fileop_entry = gtk_entry_new ();
1074 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1075 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1077 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1078 gtk_widget_show (fs->fileop_entry);
1081 button = gtk_button_new_with_label (_("Create"));
1082 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1083 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
1085 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1086 button, TRUE, TRUE, 0);
1087 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1088 gtk_widget_show (button);
1090 button = gtk_button_new_with_label (_("Cancel"));
1091 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1092 (GtkSignalFunc) gtk_widget_destroy,
1094 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1095 button, TRUE, TRUE, 0);
1096 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1097 gtk_widget_grab_default (button);
1098 gtk_widget_show (button);
1100 gtk_widget_show (dialog);
1104 gtk_file_selection_delete_file_confirmed (GtkWidget *widget,
1107 GtkFileSelection *fs = data;
1108 CompletionState *cmpl_state;
1111 gchar *sys_full_path;
1112 GError *error = NULL;
1115 g_return_if_fail (fs != NULL);
1116 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1118 cmpl_state = (CompletionState*) fs->cmpl_state;
1119 path = cmpl_reference_position (cmpl_state);
1121 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1122 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1125 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1126 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1129 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1130 fs->fileop_file, error->message,
1131 _("It probably contains symbols not allowed in filenames."));
1133 gtk_file_selection_fileop_error (fs, buf);
1134 g_error_free (error);
1138 if (unlink (sys_full_path) < 0)
1140 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1141 fs->fileop_file, g_strerror (errno));
1142 gtk_file_selection_fileop_error (fs, buf);
1147 g_free (sys_full_path);
1149 gtk_widget_destroy (fs->fileop_dialog);
1150 gtk_file_selection_populate (fs, "", FALSE);
1154 gtk_file_selection_delete_file (GtkWidget *widget,
1157 GtkFileSelection *fs = data;
1165 g_return_if_fail (fs != NULL);
1166 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1168 if (fs->fileop_dialog)
1171 #ifdef G_WITH_CYGWIN
1172 translate_win32_path (fs);
1175 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1176 if (strlen (filename) < 1)
1179 fs->fileop_file = filename;
1182 fs->fileop_dialog = dialog = gtk_dialog_new ();
1183 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1184 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1186 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1187 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1189 /* If file dialog is grabbed, grab option dialog */
1190 /* When option dialog is closed, file dialog will be grabbed again */
1191 if (GTK_WINDOW (fs)->modal)
1192 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1194 vbox = gtk_vbox_new (FALSE, 0);
1195 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1196 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1198 gtk_widget_show (vbox);
1200 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1201 label = gtk_label_new (buf);
1202 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1203 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1204 gtk_widget_show (label);
1208 button = gtk_button_new_with_label (_("Delete"));
1209 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1210 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1212 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1213 button, TRUE, TRUE, 0);
1214 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1215 gtk_widget_show (button);
1217 button = gtk_button_new_with_label (_("Cancel"));
1218 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1219 (GtkSignalFunc) gtk_widget_destroy,
1221 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1222 button, TRUE, TRUE, 0);
1223 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1224 gtk_widget_grab_default (button);
1225 gtk_widget_show (button);
1227 gtk_widget_show (dialog);
1232 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1235 GtkFileSelection *fs = data;
1239 gchar *new_filename;
1240 gchar *old_filename;
1241 gchar *sys_new_filename;
1242 gchar *sys_old_filename;
1243 CompletionState *cmpl_state;
1244 GError *error = NULL;
1246 g_return_if_fail (fs != NULL);
1247 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1249 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1250 cmpl_state = (CompletionState*) fs->cmpl_state;
1251 path = cmpl_reference_position (cmpl_state);
1253 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1254 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1256 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1259 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1260 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1262 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1263 new_filename, error->message,
1264 _("You probably used symbols not allowed in filenames."));
1265 gtk_file_selection_fileop_error (fs, buf);
1266 g_error_free (error);
1270 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1273 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1274 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1276 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1277 old_filename, error->message,
1278 _("It probably contains symbols not allowed in filenames."));
1279 gtk_file_selection_fileop_error (fs, buf);
1280 g_error_free (error);
1284 if (rename (sys_old_filename, sys_new_filename) < 0)
1286 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1287 sys_old_filename, sys_new_filename,
1288 g_strerror (errno));
1289 gtk_file_selection_fileop_error (fs, buf);
1293 g_free (sys_old_filename);
1296 g_free (new_filename);
1297 g_free (old_filename);
1298 g_free (sys_new_filename);
1300 gtk_widget_destroy (fs->fileop_dialog);
1301 gtk_file_selection_populate (fs, "", FALSE);
1305 gtk_file_selection_rename_file (GtkWidget *widget,
1308 GtkFileSelection *fs = data;
1315 g_return_if_fail (fs != NULL);
1316 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1318 if (fs->fileop_dialog)
1321 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1322 if (strlen (fs->fileop_file) < 1)
1326 fs->fileop_dialog = dialog = gtk_dialog_new ();
1327 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1328 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1330 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1331 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1333 /* If file dialog is grabbed, grab option dialog */
1334 /* When option dialog closed, file dialog will be grabbed again */
1335 if (GTK_WINDOW (fs)->modal)
1336 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1338 vbox = gtk_vbox_new (FALSE, 0);
1339 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1340 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1342 gtk_widget_show(vbox);
1344 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1345 label = gtk_label_new(buf);
1346 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1347 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1348 gtk_widget_show (label);
1351 /* New filename entry */
1352 fs->fileop_entry = gtk_entry_new ();
1353 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1355 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1356 gtk_widget_show (fs->fileop_entry);
1358 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1359 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1360 0, strlen (fs->fileop_file));
1363 button = gtk_button_new_with_label (_("Rename"));
1364 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1365 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1367 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1368 button, TRUE, TRUE, 0);
1369 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1370 gtk_widget_show (button);
1372 button = gtk_button_new_with_label (_("Cancel"));
1373 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1374 (GtkSignalFunc) gtk_widget_destroy,
1376 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1377 button, TRUE, TRUE, 0);
1378 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1379 gtk_widget_grab_default (button);
1380 gtk_widget_show (button);
1382 gtk_widget_show (dialog);
1386 gtk_file_selection_insert_text (GtkWidget *widget,
1387 const gchar *new_text,
1388 gint new_text_length,
1394 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1399 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "insert_text");
1409 gtk_file_selection_key_press (GtkWidget *widget,
1413 GtkFileSelection *fs;
1416 g_return_val_if_fail (widget != NULL, FALSE);
1417 g_return_val_if_fail (event != NULL, FALSE);
1419 if (event->keyval == GDK_Tab)
1421 fs = GTK_FILE_SELECTION (user_data);
1422 #ifdef G_WITH_CYGWIN
1423 translate_win32_path (fs);
1425 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1427 text = g_strdup (text);
1429 gtk_file_selection_populate (fs, text, TRUE);
1433 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1443 gtk_file_selection_history_callback (GtkWidget *widget,
1446 GtkFileSelection *fs = data;
1447 HistoryCallbackArg *callback_arg;
1450 g_return_if_fail (fs != NULL);
1451 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1453 list = fs->history_list;
1456 callback_arg = list->data;
1458 if (callback_arg->menu_item == widget)
1460 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1469 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1470 gchar *current_directory)
1472 HistoryCallbackArg *callback_arg;
1473 GtkWidget *menu_item;
1479 g_return_if_fail (fs != NULL);
1480 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1481 g_return_if_fail (current_directory != NULL);
1483 list = fs->history_list;
1485 if (fs->history_menu)
1488 callback_arg = list->data;
1489 g_free (callback_arg->directory);
1490 g_free (callback_arg);
1493 g_list_free (fs->history_list);
1494 fs->history_list = NULL;
1496 gtk_widget_destroy (fs->history_menu);
1499 fs->history_menu = gtk_menu_new ();
1501 current_dir = g_strdup (current_directory);
1503 dir_len = strlen (current_dir);
1505 for (i = dir_len; i >= 0; i--)
1507 /* the i == dir_len is to catch the full path for the first
1509 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1511 /* another small hack to catch the full path */
1513 current_dir[i + 1] = '\0';
1514 #ifdef G_WITH_CYGWIN
1515 if (!strcmp (current_dir, "//"))
1518 menu_item = gtk_menu_item_new_with_label (current_dir);
1520 callback_arg = g_new (HistoryCallbackArg, 1);
1521 callback_arg->menu_item = menu_item;
1523 /* since the autocompletion gets confused if you don't
1524 * supply a trailing '/' on a dir entry, set the full
1525 * (current) path to "" which just refreshes the filesel */
1528 callback_arg->directory = g_strdup ("");
1532 callback_arg->directory = g_strdup (current_dir);
1535 fs->history_list = g_list_append (fs->history_list, callback_arg);
1537 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1538 (GtkSignalFunc) gtk_file_selection_history_callback,
1540 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1541 gtk_widget_show (menu_item);
1545 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1547 g_free (current_dir);
1551 gtk_file_selection_file_button (GtkWidget *widget,
1554 GdkEventButton *bevent,
1557 GtkFileSelection *fs = NULL;
1558 gchar *filename, *temp = NULL;
1560 g_return_if_fail (GTK_IS_CLIST (widget));
1563 g_return_if_fail (fs != NULL);
1564 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1566 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1567 filename = g_strdup (temp);
1569 #ifdef G_WITH_CYGWIN
1570 /* Check to see if the selection was a drive selector */
1571 if (isalpha (filename[0]) && (filename[1] == ':')) {
1572 /* It is... map it to a CYGWIN32 drive */
1573 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1575 filename = temp_filename;
1577 #endif /* G_WITH_CYGWIN */
1582 switch (bevent->type)
1584 case GDK_2BUTTON_PRESS:
1585 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1589 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1593 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1600 gtk_file_selection_dir_button (GtkWidget *widget,
1603 GdkEventButton *bevent,
1606 GtkFileSelection *fs = NULL;
1607 gchar *filename, *temp = NULL;
1609 g_return_if_fail (GTK_IS_CLIST (widget));
1611 fs = GTK_FILE_SELECTION (user_data);
1612 g_return_if_fail (fs != NULL);
1613 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1615 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1616 filename = g_strdup (temp);
1621 switch (bevent->type)
1623 case GDK_2BUTTON_PRESS:
1624 gtk_file_selection_populate (fs, filename, FALSE);
1628 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1632 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1638 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1641 win32_gtk_add_drives_to_dir_list (GtkWidget *the_dir_list)
1643 gchar *text[2], *textPtr;
1645 char volumeNameBuf[128];
1646 char formatBuffer[128];
1650 /* Get the Drives string */
1651 GetLogicalDriveStrings (sizeof (buffer), buffer);
1653 /* Add the drives as necessary */
1655 while (*textPtr != '\0') {
1656 /* Get the volume information for this drive */
1657 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1659 /* Ignore floppies (?) */
1660 DWORD maxComponentLength, flags;
1662 GetVolumeInformation (textPtr,
1663 volumeNameBuf, sizeof(volumeNameBuf),
1664 NULL, &maxComponentLength,
1666 /* Build the actual displayable string */
1668 sprintf (formatBuffer, "%c:\\", toupper(textPtr[0]));
1669 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1670 if (strlen (volumeNameBuf) > 0)
1671 sprintf (formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1673 /* Add to the list */
1674 text[0] = formatBuffer;
1675 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1677 textPtr += (strlen (textPtr) + 1);
1683 gtk_file_selection_populate (GtkFileSelection *fs,
1687 CompletionState *cmpl_state;
1688 PossibleCompletion* poss;
1690 gchar* rem_path = rel_path;
1693 gint did_recurse = FALSE;
1694 gint possible_count = 0;
1695 gint selection_index = -1;
1697 g_return_if_fail (fs != NULL);
1698 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1700 cmpl_state = (CompletionState*) fs->cmpl_state;
1701 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1703 if (!cmpl_state_okay (cmpl_state))
1705 /* Something went wrong. */
1706 gtk_file_selection_abort (fs);
1710 g_assert (cmpl_state->reference_dir);
1712 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1713 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1714 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1715 gtk_clist_clear (GTK_CLIST (fs->file_list));
1717 /* Set the dir_list to include ./ and ../ */
1719 text[0] = "." G_DIR_SEPARATOR_S;
1720 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1722 text[0] = ".." G_DIR_SEPARATOR_S;
1723 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1727 if (cmpl_is_a_completion (poss))
1729 possible_count += 1;
1731 filename = cmpl_this_completion (poss);
1735 if (cmpl_is_directory (poss))
1737 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1738 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1739 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1743 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1747 poss = cmpl_next_completion (cmpl_state);
1750 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1751 /* For Windows, add drives as potential selections */
1752 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1755 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1756 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1758 /* File lists are set. */
1760 g_assert (cmpl_state->reference_dir);
1765 /* User is trying to complete filenames, so advance the user's input
1766 * string to the updated_text, which is the common leading substring
1767 * of all possible completions, and if its a directory attempt
1768 * attempt completions in it. */
1770 if (cmpl_updated_text (cmpl_state)[0])
1773 if (cmpl_updated_dir (cmpl_state))
1775 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1779 gtk_file_selection_populate (fs, dir_name, TRUE);
1785 if (fs->selection_entry)
1786 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1787 cmpl_updated_text (cmpl_state));
1792 selection_index = cmpl_last_valid_char (cmpl_state) -
1793 (strlen (rel_path) - strlen (rem_path));
1794 if (fs->selection_entry)
1795 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1800 if (fs->selection_entry)
1801 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1806 if (fs->selection_entry)
1807 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1809 if (fs->selection_entry)
1811 sel_text = g_strconcat (_("Selection: "),
1812 cmpl_reference_position (cmpl_state),
1815 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1819 if (fs->history_pulldown)
1821 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1828 gtk_file_selection_abort (GtkFileSelection *fs)
1832 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1834 /* BEEP gdk_beep(); */
1836 if (fs->selection_entry)
1837 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1840 /**********************************************************************/
1841 /* External Interface */
1842 /**********************************************************************/
1844 /* The four completion state selectors
1847 cmpl_updated_text (CompletionState *cmpl_state)
1849 return cmpl_state->updated_text;
1853 cmpl_updated_dir (CompletionState *cmpl_state)
1855 return cmpl_state->re_complete;
1859 cmpl_reference_position (CompletionState *cmpl_state)
1861 return cmpl_state->reference_dir->fullname;
1865 cmpl_last_valid_char (CompletionState *cmpl_state)
1867 return cmpl_state->last_valid_char;
1871 cmpl_completion_fullname (gchar *text,
1872 CompletionState *cmpl_state)
1874 static char nothing[2] = "";
1876 if (!cmpl_state_okay (cmpl_state))
1880 else if (g_path_is_absolute (text))
1882 strcpy (cmpl_state->updated_text, text);
1885 else if (text[0] == '~')
1890 dir = open_user_dir (text, cmpl_state);
1894 /* spencer says just return ~something, so
1895 * for now just do it. */
1896 strcpy (cmpl_state->updated_text, text);
1901 strcpy (cmpl_state->updated_text, dir->fullname);
1903 slash = strchr (text, G_DIR_SEPARATOR);
1906 strcat (cmpl_state->updated_text, slash);
1912 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1913 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1914 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1915 strcat (cmpl_state->updated_text, text);
1918 return cmpl_state->updated_text;
1921 /* The three completion selectors
1924 cmpl_this_completion (PossibleCompletion* pc)
1930 cmpl_is_directory (PossibleCompletion* pc)
1932 return pc->is_directory;
1936 cmpl_is_a_completion (PossibleCompletion* pc)
1938 return pc->is_a_completion;
1941 /**********************************************************************/
1942 /* Construction, deletion */
1943 /**********************************************************************/
1945 static CompletionState*
1946 cmpl_init_state (void)
1948 gchar *sys_getcwd_buf;
1950 CompletionState *new_state;
1952 new_state = g_new (CompletionState, 1);
1954 /* g_get_current_dir() returns a string in the "system" charset */
1955 sys_getcwd_buf = g_get_current_dir ();
1956 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
1957 g_free (sys_getcwd_buf);
1961 new_state->reference_dir = NULL;
1962 new_state->completion_dir = NULL;
1963 new_state->active_completion_dir = NULL;
1964 new_state->directory_storage = NULL;
1965 new_state->directory_sent_storage = NULL;
1966 new_state->last_valid_char = 0;
1967 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1968 new_state->updated_text_alloc = MAXPATHLEN;
1969 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1970 new_state->the_completion.text_alloc = MAXPATHLEN;
1971 new_state->user_dir_name_buffer = NULL;
1972 new_state->user_directories = NULL;
1974 new_state->reference_dir = open_dir (utf8_cwd, new_state);
1976 if (!new_state->reference_dir)
1978 /* Directories changing from underneath us, grumble */
1979 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
1988 cmpl_free_dir_list (GList* dp0)
1994 free_dir (dp->data);
2002 cmpl_free_dir_sent_list (GList* dp0)
2008 free_dir_sent (dp->data);
2016 cmpl_free_state (CompletionState* cmpl_state)
2018 g_return_if_fail (cmpl_state != NULL);
2020 cmpl_free_dir_list (cmpl_state->directory_storage);
2021 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2023 if (cmpl_state->user_dir_name_buffer)
2024 g_free (cmpl_state->user_dir_name_buffer);
2025 if (cmpl_state->user_directories)
2026 g_free (cmpl_state->user_directories);
2027 if (cmpl_state->the_completion.text)
2028 g_free (cmpl_state->the_completion.text);
2029 if (cmpl_state->updated_text)
2030 g_free (cmpl_state->updated_text);
2032 g_free (cmpl_state);
2036 free_dir (CompletionDir* dir)
2038 g_free (dir->fullname);
2043 free_dir_sent (CompletionDirSent* sent)
2046 for (i = 0; i < sent->entry_count; i++)
2047 g_free (sent->entries[i].entry_name);
2048 g_free (sent->entries);
2053 prune_memory_usage (CompletionState *cmpl_state)
2055 GList* cdsl = cmpl_state->directory_sent_storage;
2056 GList* cdl = cmpl_state->directory_storage;
2060 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2065 cmpl_free_dir_sent_list (cdsl->next);
2069 cmpl_state->directory_storage = NULL;
2072 if (cdl->data == cmpl_state->reference_dir)
2073 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2075 free_dir (cdl->data);
2082 /**********************************************************************/
2083 /* The main entrances. */
2084 /**********************************************************************/
2086 static PossibleCompletion*
2087 cmpl_completion_matches (gchar *text_to_complete,
2088 gchar **remaining_text,
2089 CompletionState *cmpl_state)
2092 PossibleCompletion *poss;
2094 prune_memory_usage (cmpl_state);
2096 g_assert (text_to_complete != NULL);
2098 cmpl_state->user_completion_index = -1;
2099 cmpl_state->last_completion_text = text_to_complete;
2100 cmpl_state->the_completion.text[0] = 0;
2101 cmpl_state->last_valid_char = 0;
2102 cmpl_state->updated_text_len = -1;
2103 cmpl_state->updated_text[0] = 0;
2104 cmpl_state->re_complete = FALSE;
2107 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2109 if (text_to_complete[0] == '~' && !first_slash)
2111 /* Text starts with ~ and there is no slash, show all the
2112 * home directory completions.
2114 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2116 update_cmpl (poss, cmpl_state);
2121 cmpl_state->reference_dir =
2122 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2124 if (!cmpl_state->reference_dir)
2127 cmpl_state->completion_dir =
2128 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2130 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2132 if (!cmpl_state->completion_dir)
2135 cmpl_state->completion_dir->cmpl_index = -1;
2136 cmpl_state->completion_dir->cmpl_parent = NULL;
2137 cmpl_state->completion_dir->cmpl_text = *remaining_text;
2139 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2141 cmpl_state->reference_dir = cmpl_state->completion_dir;
2143 poss = attempt_file_completion (cmpl_state);
2145 update_cmpl (poss, cmpl_state);
2150 static PossibleCompletion*
2151 cmpl_next_completion (CompletionState* cmpl_state)
2153 PossibleCompletion* poss = NULL;
2155 cmpl_state->the_completion.text[0] = 0;
2158 if (cmpl_state->user_completion_index >= 0)
2159 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2161 poss = attempt_file_completion (cmpl_state);
2163 poss = attempt_file_completion (cmpl_state);
2166 update_cmpl (poss, cmpl_state);
2171 /**********************************************************************/
2172 /* Directory Operations */
2173 /**********************************************************************/
2175 /* Open the directory where completion will begin from, if possible. */
2176 static CompletionDir*
2177 open_ref_dir (gchar *text_to_complete,
2178 gchar **remaining_text,
2179 CompletionState *cmpl_state)
2182 CompletionDir *new_dir;
2184 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2186 #ifdef G_WITH_CYGWIN
2187 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2190 sprintf (root_dir, "//%c", text_to_complete[2]);
2192 new_dir = open_dir (root_dir, cmpl_state);
2195 *remaining_text = text_to_complete + 4;
2203 else if (text_to_complete[0] == '~')
2205 new_dir = open_user_dir (text_to_complete, cmpl_state);
2210 *remaining_text = first_slash + 1;
2212 *remaining_text = text_to_complete + strlen (text_to_complete);
2220 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2222 gchar *tmp = g_strdup (text_to_complete);
2226 while (*p && *p != '*' && *p != '?')
2230 p = strrchr (tmp, G_DIR_SEPARATOR);
2238 new_dir = open_dir (tmp, cmpl_state);
2241 *remaining_text = text_to_complete +
2242 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2246 /* If no possible candidates, use the cwd */
2247 gchar *sys_curdir = g_get_current_dir ();
2248 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2250 g_free (sys_curdir);
2252 new_dir = open_dir (utf8_curdir, cmpl_state);
2255 *remaining_text = text_to_complete;
2257 g_free (utf8_curdir);
2264 *remaining_text = text_to_complete;
2266 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2271 new_dir->cmpl_index = -1;
2272 new_dir->cmpl_parent = NULL;
2280 /* open a directory by user name */
2281 static CompletionDir*
2282 open_user_dir (gchar *text_to_complete,
2283 CompletionState *cmpl_state)
2285 CompletionDir *result;
2289 g_assert (text_to_complete && text_to_complete[0] == '~');
2291 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2294 cmp_len = first_slash - text_to_complete - 1;
2296 cmp_len = strlen (text_to_complete + 1);
2301 gchar *homedir = g_get_home_dir ();
2302 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2307 result = open_dir (utf8_homedir, cmpl_state);
2311 g_free (utf8_homedir);
2316 gchar* copy = g_new (char, cmp_len + 1);
2320 strncpy (copy, text_to_complete + 1, cmp_len);
2322 pwd = getpwnam (copy);
2329 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2330 result = open_dir (utf8_dir, cmpl_state);
2338 /* open a directory relative the the current relative directory */
2339 static CompletionDir*
2340 open_relative_dir (gchar *dir_name,
2342 CompletionState *cmpl_state)
2344 CompletionDir *result;
2347 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2348 g_string_assign (path, dir->fullname);
2350 if (dir->fullname_len > 1
2351 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2352 g_string_append_c (path, G_DIR_SEPARATOR);
2353 g_string_append (path, dir_name);
2355 result = open_dir (path->str, cmpl_state);
2357 g_string_free (path, TRUE);
2362 /* after the cache lookup fails, really open a new directory */
2363 static CompletionDirSent*
2364 open_new_dir (gchar *dir_name,
2366 gboolean stat_subdirs)
2368 CompletionDirSent *sent;
2370 struct dirent *dirent_ptr;
2371 gint entry_count = 0;
2374 struct stat ent_sbuf;
2376 gchar *sys_dir_name;
2378 sent = g_new (CompletionDirSent, 1);
2379 sent->mtime = sbuf->st_mtime;
2380 sent->inode = sbuf->st_ino;
2381 sent->device = sbuf->st_dev;
2383 path = g_string_sized_new (2*MAXPATHLEN + 10);
2385 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2388 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2392 directory = opendir (sys_dir_name);
2396 g_free (sys_dir_name);
2400 while ((dirent_ptr = readdir (directory)) != NULL)
2403 sent->entries = g_new (CompletionDirEntry, entry_count);
2404 sent->entry_count = entry_count;
2406 rewinddir (directory);
2408 for (i = 0; i < entry_count; i += 1)
2410 dirent_ptr = readdir (directory);
2415 closedir (directory);
2416 g_free (sys_dir_name);
2420 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent_ptr->d_name, -1, NULL, NULL, NULL);
2421 if (!g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2423 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent_ptr->d_name);
2427 g_string_assign (path, sys_dir_name);
2428 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2430 g_string_append_c (path, G_DIR_SEPARATOR);
2432 g_string_append (path, dirent_ptr->d_name);
2436 /* Here we know path->str is a "system charset" string */
2437 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2438 sent->entries[n_entries].is_dir = TRUE;
2440 /* stat may fail, and we don't mind, since it could be a
2441 * dangling symlink. */
2442 sent->entries[n_entries].is_dir = FALSE;
2445 sent->entries[n_entries].is_dir = 1;
2449 sent->entry_count = n_entries;
2451 g_free (sys_dir_name);
2452 g_string_free (path, TRUE);
2453 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2455 closedir (directory);
2460 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2463 check_dir (gchar *dir_name,
2464 struct stat *result,
2465 gboolean *stat_subdirs)
2467 /* A list of directories that we know only contain other directories.
2468 * Trying to stat every file in these directories would be very
2475 struct stat statbuf;
2476 } no_stat_dirs[] = {
2477 { "/afs", FALSE, { 0 } },
2478 { "/net", FALSE, { 0 } }
2481 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
2482 static gboolean initialized = FALSE;
2483 gchar *sys_dir_name;
2489 for (i = 0; i < n_no_stat_dirs; i++)
2491 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2492 no_stat_dirs[i].present = TRUE;
2496 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2499 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2503 if (stat (sys_dir_name, result) < 0)
2505 g_free (sys_dir_name);
2509 g_free (sys_dir_name);
2511 *stat_subdirs = TRUE;
2512 for (i = 0; i < n_no_stat_dirs; i++)
2514 if (no_stat_dirs[i].present &&
2515 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2516 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2518 *stat_subdirs = FALSE;
2528 /* open a directory by absolute pathname */
2529 static CompletionDir*
2530 open_dir (gchar *dir_name,
2531 CompletionState *cmpl_state)
2534 gboolean stat_subdirs;
2535 CompletionDirSent *sent;
2538 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2539 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2542 cdsl = cmpl_state->directory_sent_storage;
2548 if (sent->inode == sbuf.st_ino &&
2549 sent->mtime == sbuf.st_mtime &&
2550 sent->device == sbuf.st_dev)
2551 return attach_dir (sent, dir_name, cmpl_state);
2556 stat_subdirs = TRUE;
2559 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
2563 cmpl_state->directory_sent_storage =
2564 g_list_prepend (cmpl_state->directory_sent_storage, sent);
2566 return attach_dir (sent, dir_name, cmpl_state);
2572 static CompletionDir*
2573 attach_dir (CompletionDirSent *sent,
2575 CompletionState *cmpl_state)
2577 CompletionDir* new_dir;
2579 new_dir = g_new (CompletionDir, 1);
2581 cmpl_state->directory_storage =
2582 g_list_prepend (cmpl_state->directory_storage, new_dir);
2584 new_dir->sent = sent;
2585 new_dir->fullname = g_strdup (dir_name);
2586 new_dir->fullname_len = strlen (dir_name);
2592 correct_dir_fullname (CompletionDir* cmpl_dir)
2594 gint length = strlen (cmpl_dir->fullname);
2595 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2596 gchar *sys_filename;
2599 /* Does it end with /. (\.) ? */
2601 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2603 /* Is it just the root directory (on a drive) ? */
2604 if (cmpl_dir->fullname + length - 2 == first_slash)
2606 cmpl_dir->fullname[length - 1] = 0;
2607 cmpl_dir->fullname_len = length - 1;
2612 cmpl_dir->fullname[length - 2] = 0;
2616 /* Ends with /./ (\.\)? */
2617 else if (length >= 3 &&
2618 strcmp (cmpl_dir->fullname + length - 3,
2619 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2620 cmpl_dir->fullname[length - 2] = 0;
2622 /* Ends with /.. (\..) ? */
2623 else if (length >= 3 &&
2624 strcmp (cmpl_dir->fullname + length - 3,
2625 G_DIR_SEPARATOR_S "..") == 0)
2627 /* Is it just /.. (X:\..)? */
2628 if (cmpl_dir->fullname + length - 3 == first_slash)
2630 cmpl_dir->fullname[length - 2] = 0;
2631 cmpl_dir->fullname_len = length - 2;
2635 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2638 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2642 if (stat (sys_filename, &sbuf) < 0)
2644 g_free (sys_filename);
2648 g_free (sys_filename);
2650 cmpl_dir->fullname[length - 3] = 0;
2652 if (!correct_parent (cmpl_dir, &sbuf))
2656 /* Ends with /../ (\..\)? */
2657 else if (length >= 4 &&
2658 strcmp (cmpl_dir->fullname + length - 4,
2659 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2661 /* Is it just /../ (X:\..\)? */
2662 if (cmpl_dir->fullname + length - 4 == first_slash)
2664 cmpl_dir->fullname[length - 3] = 0;
2665 cmpl_dir->fullname_len = length - 3;
2669 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2672 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2676 if (stat (sys_filename, &sbuf) < 0)
2678 g_free (sys_filename);
2682 g_free (sys_filename);
2684 cmpl_dir->fullname[length - 4] = 0;
2686 if (!correct_parent (cmpl_dir, &sbuf))
2690 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
2696 correct_parent (CompletionDir *cmpl_dir,
2703 gchar *sys_filename;
2706 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2707 g_assert (last_slash);
2708 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2710 /* Clever (?) way to check for top-level directory that works also on
2711 * Win32, where there is a drive letter and colon prefixed...
2713 if (last_slash != first_slash)
2723 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2726 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2728 last_slash[0] = G_DIR_SEPARATOR;
2732 if (stat (sys_filename, &parbuf) < 0)
2734 g_free (sys_filename);
2737 last_slash[0] = G_DIR_SEPARATOR;
2740 g_free (sys_filename);
2742 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2743 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2744 /* it wasn't a link */
2750 last_slash[0] = G_DIR_SEPARATOR;
2752 /* it was a link, have to figure it out the hard way */
2754 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
2759 g_free (cmpl_dir->fullname);
2761 cmpl_dir->fullname = new_name;
2770 find_parent_dir_fullname (gchar* dirname)
2772 gchar *sys_orig_dir;
2777 sys_orig_dir = g_get_current_dir ();
2778 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
2781 g_free (sys_orig_dir);
2782 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2786 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
2788 g_free (sys_dirname);
2789 g_free (sys_orig_dir);
2793 g_free (sys_dirname);
2795 sys_cwd = g_get_current_dir ();
2796 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
2799 if (chdir (sys_orig_dir) != 0)
2802 g_free (sys_orig_dir);
2806 g_free (sys_orig_dir);
2812 /**********************************************************************/
2813 /* Completion Operations */
2814 /**********************************************************************/
2818 static PossibleCompletion*
2819 attempt_homedir_completion (gchar *text_to_complete,
2820 CompletionState *cmpl_state)
2824 if (!cmpl_state->user_dir_name_buffer &&
2825 !get_pwdb (cmpl_state))
2827 length = strlen (text_to_complete) - 1;
2829 cmpl_state->user_completion_index += 1;
2831 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2833 index = first_diff_index (text_to_complete + 1,
2834 cmpl_state->user_directories
2835 [cmpl_state->user_completion_index].login);
2842 if (cmpl_state->last_valid_char < (index + 1))
2843 cmpl_state->last_valid_char = index + 1;
2844 cmpl_state->user_completion_index += 1;
2848 cmpl_state->the_completion.is_a_completion = 1;
2849 cmpl_state->the_completion.is_directory = TRUE;
2851 append_completion_text ("~", cmpl_state);
2853 append_completion_text (cmpl_state->
2854 user_directories[cmpl_state->user_completion_index].login,
2857 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
2860 if (text_to_complete[1]
2861 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2863 cmpl_state->user_completion_index = -1;
2868 cmpl_state->user_completion_index += 1;
2869 cmpl_state->the_completion.is_a_completion = 1;
2870 cmpl_state->the_completion.is_directory = TRUE;
2872 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
2878 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2879 #define FOLD(c) (tolower(c))
2884 /* returns the index (>= 0) of the first differing character,
2885 * PATTERN_MATCH if the completion matches */
2887 first_diff_index (gchar *pat,
2892 while (*pat && *text && FOLD (*text) == FOLD (*pat))
2902 return PATTERN_MATCH;
2905 static PossibleCompletion*
2906 append_completion_text (gchar *text,
2907 CompletionState *cmpl_state)
2911 if (!cmpl_state->the_completion.text)
2914 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
2916 if (cmpl_state->the_completion.text_alloc > len)
2918 strcat (cmpl_state->the_completion.text, text);
2919 return &cmpl_state->the_completion;
2925 cmpl_state->the_completion.text_alloc = i;
2927 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
2929 if (!cmpl_state->the_completion.text)
2933 strcat (cmpl_state->the_completion.text, text);
2934 return &cmpl_state->the_completion;
2938 static CompletionDir*
2939 find_completion_dir (gchar *text_to_complete,
2940 gchar **remaining_text,
2941 CompletionState *cmpl_state)
2943 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2944 CompletionDir* dir = cmpl_state->reference_dir;
2945 CompletionDir* next;
2946 *remaining_text = text_to_complete;
2950 gint len = first_slash - *remaining_text;
2952 gchar *found_name = NULL; /* Quiet gcc */
2954 gchar* pat_buf = g_new (gchar, len + 1);
2956 strncpy (pat_buf, *remaining_text, len);
2959 for (i = 0; i < dir->sent->entry_count; i += 1)
2961 if (dir->sent->entries[i].is_dir &&
2962 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
2963 FNMATCH_FLAGS)!= FNM_NOMATCH)
2973 found_name = dir->sent->entries[i].entry_name;
2980 /* Perhaps we are trying to open an automount directory */
2981 found_name = pat_buf;
2984 next = open_relative_dir (found_name, dir, cmpl_state);
2992 next->cmpl_parent = dir;
2996 if (!correct_dir_fullname (dir))
3002 *remaining_text = first_slash + 1;
3003 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3012 update_cmpl (PossibleCompletion *poss,
3013 CompletionState *cmpl_state)
3017 if (!poss || !cmpl_is_a_completion (poss))
3020 cmpl_len = strlen (cmpl_this_completion (poss));
3022 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3024 cmpl_state->updated_text =
3025 (gchar*)g_realloc (cmpl_state->updated_text,
3026 cmpl_state->updated_text_alloc);
3027 cmpl_state->updated_text_alloc = 2*cmpl_len;
3030 if (cmpl_state->updated_text_len < 0)
3032 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3033 cmpl_state->updated_text_len = cmpl_len;
3034 cmpl_state->re_complete = cmpl_is_directory (poss);
3036 else if (cmpl_state->updated_text_len == 0)
3038 cmpl_state->re_complete = FALSE;
3043 first_diff_index (cmpl_state->updated_text,
3044 cmpl_this_completion (poss));
3046 cmpl_state->re_complete = FALSE;
3048 if (first_diff == PATTERN_MATCH)
3051 if (first_diff > cmpl_state->updated_text_len)
3052 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3054 cmpl_state->updated_text_len = first_diff;
3055 cmpl_state->updated_text[first_diff] = 0;
3059 static PossibleCompletion*
3060 attempt_file_completion (CompletionState *cmpl_state)
3062 gchar *pat_buf, *first_slash;
3063 CompletionDir *dir = cmpl_state->active_completion_dir;
3065 dir->cmpl_index += 1;
3067 if (dir->cmpl_index == dir->sent->entry_count)
3069 if (dir->cmpl_parent == NULL)
3071 cmpl_state->active_completion_dir = NULL;
3077 cmpl_state->active_completion_dir = dir->cmpl_parent;
3079 return attempt_file_completion (cmpl_state);
3083 g_assert (dir->cmpl_text);
3085 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3089 gint len = first_slash - dir->cmpl_text;
3091 pat_buf = g_new (gchar, len + 1);
3092 strncpy (pat_buf, dir->cmpl_text, len);
3097 gint len = strlen (dir->cmpl_text);
3099 pat_buf = g_new (gchar, len + 2);
3100 strcpy (pat_buf, dir->cmpl_text);
3101 /* Don't append a * if the user entered one herself.
3102 * This way one can complete *.h and don't get matches
3103 * on any .help files, for instance.
3105 if (strchr (pat_buf, '*') == NULL)
3106 strcpy (pat_buf + len, "*");
3111 if (dir->sent->entries[dir->cmpl_index].is_dir)
3113 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3114 FNMATCH_FLAGS) != FNM_NOMATCH)
3116 CompletionDir* new_dir;
3118 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3127 new_dir->cmpl_parent = dir;
3129 new_dir->cmpl_index = -1;
3130 new_dir->cmpl_text = first_slash + 1;
3132 cmpl_state->active_completion_dir = new_dir;
3135 return attempt_file_completion (cmpl_state);
3140 return attempt_file_completion (cmpl_state);
3146 return attempt_file_completion (cmpl_state);
3151 if (dir->cmpl_parent != NULL)
3153 append_completion_text (dir->fullname +
3154 strlen (cmpl_state->completion_dir->fullname) + 1,
3156 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3159 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3161 cmpl_state->the_completion.is_a_completion =
3162 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3163 FNMATCH_FLAGS) != FNM_NOMATCH;
3165 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3166 if (dir->sent->entries[dir->cmpl_index].is_dir)
3167 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3170 return &cmpl_state->the_completion;
3177 get_pwdb (CompletionState* cmpl_state)
3179 struct passwd *pwd_ptr;
3182 gint len = 0, i, count = 0;
3184 if (cmpl_state->user_dir_name_buffer)
3188 while ((pwd_ptr = getpwent ()) != NULL)
3190 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3191 len += strlen (utf8);
3193 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3194 len += strlen (utf8);
3202 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3203 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3204 cmpl_state->user_directories_len = count;
3206 buf_ptr = cmpl_state->user_dir_name_buffer;
3208 for (i = 0; i < count; i += 1)
3210 pwd_ptr = getpwent ();
3217 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3218 strcpy (buf_ptr, utf8);
3221 cmpl_state->user_directories[i].login = buf_ptr;
3223 buf_ptr += strlen (buf_ptr);
3226 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3227 strcpy (buf_ptr, utf8);
3230 cmpl_state->user_directories[i].homedir = buf_ptr;
3232 buf_ptr += strlen (buf_ptr);
3236 qsort (cmpl_state->user_directories,
3237 cmpl_state->user_directories_len,
3238 sizeof (CompletionUserDir),
3247 if (cmpl_state->user_dir_name_buffer)
3248 g_free (cmpl_state->user_dir_name_buffer);
3249 if (cmpl_state->user_directories)
3250 g_free (cmpl_state->user_directories);
3252 cmpl_state->user_dir_name_buffer = NULL;
3253 cmpl_state->user_directories = NULL;
3259 compare_user_dir (const void *a,
3262 return strcmp ((((CompletionUserDir*)a))->login,
3263 (((CompletionUserDir*)b))->login);
3269 compare_cmpl_dir (const void *a,
3272 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3273 return strcmp ((((CompletionDirEntry*)a))->entry_name,
3274 (((CompletionDirEntry*)b))->entry_name);
3276 return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
3277 (((CompletionDirEntry*)b))->entry_name);
3282 cmpl_state_okay (CompletionState* cmpl_state)
3284 return cmpl_state && cmpl_state->reference_dir;
3288 cmpl_strerror (gint err)
3290 if (err == CMPL_ERRNO_TOO_LONG)
3291 return _("Name too long");
3292 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3293 return _("Couldn't convert filename");
3295 return g_strerror (err);