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);
1441 gtk_file_selection_history_callback (GtkWidget *widget,
1444 GtkFileSelection *fs = data;
1445 HistoryCallbackArg *callback_arg;
1448 g_return_if_fail (fs != NULL);
1449 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1451 list = fs->history_list;
1454 callback_arg = list->data;
1456 if (callback_arg->menu_item == widget)
1458 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1467 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1468 gchar *current_directory)
1470 HistoryCallbackArg *callback_arg;
1471 GtkWidget *menu_item;
1477 g_return_if_fail (fs != NULL);
1478 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1479 g_return_if_fail (current_directory != NULL);
1481 list = fs->history_list;
1483 if (fs->history_menu)
1486 callback_arg = list->data;
1487 g_free (callback_arg->directory);
1488 g_free (callback_arg);
1491 g_list_free (fs->history_list);
1492 fs->history_list = NULL;
1494 gtk_widget_destroy (fs->history_menu);
1497 fs->history_menu = gtk_menu_new ();
1499 current_dir = g_strdup (current_directory);
1501 dir_len = strlen (current_dir);
1503 for (i = dir_len; i >= 0; i--)
1505 /* the i == dir_len is to catch the full path for the first
1507 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1509 /* another small hack to catch the full path */
1511 current_dir[i + 1] = '\0';
1512 #ifdef G_WITH_CYGWIN
1513 if (!strcmp (current_dir, "//"))
1516 menu_item = gtk_menu_item_new_with_label (current_dir);
1518 callback_arg = g_new (HistoryCallbackArg, 1);
1519 callback_arg->menu_item = menu_item;
1521 /* since the autocompletion gets confused if you don't
1522 * supply a trailing '/' on a dir entry, set the full
1523 * (current) path to "" which just refreshes the filesel */
1526 callback_arg->directory = g_strdup ("");
1530 callback_arg->directory = g_strdup (current_dir);
1533 fs->history_list = g_list_append (fs->history_list, callback_arg);
1535 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1536 (GtkSignalFunc) gtk_file_selection_history_callback,
1538 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1539 gtk_widget_show (menu_item);
1543 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1545 g_free (current_dir);
1549 gtk_file_selection_file_button (GtkWidget *widget,
1552 GdkEventButton *bevent,
1555 GtkFileSelection *fs = NULL;
1556 gchar *filename, *temp = NULL;
1558 g_return_if_fail (GTK_IS_CLIST (widget));
1561 g_return_if_fail (fs != NULL);
1562 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1564 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1565 filename = g_strdup (temp);
1567 #ifdef G_WITH_CYGWIN
1568 /* Check to see if the selection was a drive selector */
1569 if (isalpha (filename[0]) && (filename[1] == ':')) {
1570 /* It is... map it to a CYGWIN32 drive */
1571 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1573 filename = temp_filename;
1575 #endif /* G_WITH_CYGWIN */
1580 switch (bevent->type)
1582 case GDK_2BUTTON_PRESS:
1583 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1587 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1591 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1598 gtk_file_selection_dir_button (GtkWidget *widget,
1601 GdkEventButton *bevent,
1604 GtkFileSelection *fs = NULL;
1605 gchar *filename, *temp = NULL;
1607 g_return_if_fail (GTK_IS_CLIST (widget));
1609 fs = GTK_FILE_SELECTION (user_data);
1610 g_return_if_fail (fs != NULL);
1611 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1613 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1614 filename = g_strdup (temp);
1619 switch (bevent->type)
1621 case GDK_2BUTTON_PRESS:
1622 gtk_file_selection_populate (fs, filename, FALSE);
1626 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1630 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1636 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1639 win32_gtk_add_drives_to_dir_list (GtkWidget *the_dir_list)
1641 gchar *text[2], *textPtr;
1643 char volumeNameBuf[128];
1644 char formatBuffer[128];
1648 /* Get the Drives string */
1649 GetLogicalDriveStrings (sizeof (buffer), buffer);
1651 /* Add the drives as necessary */
1653 while (*textPtr != '\0') {
1654 /* Get the volume information for this drive */
1655 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1657 /* Ignore floppies (?) */
1658 DWORD maxComponentLength, flags;
1660 GetVolumeInformation (textPtr,
1661 volumeNameBuf, sizeof(volumeNameBuf),
1662 NULL, &maxComponentLength,
1664 /* Build the actual displayable string */
1666 sprintf (formatBuffer, "%c:\\", toupper(textPtr[0]));
1667 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1668 if (strlen (volumeNameBuf) > 0)
1669 sprintf (formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1671 /* Add to the list */
1672 text[0] = formatBuffer;
1673 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1675 textPtr += (strlen (textPtr) + 1);
1681 gtk_file_selection_populate (GtkFileSelection *fs,
1685 CompletionState *cmpl_state;
1686 PossibleCompletion* poss;
1688 gchar* rem_path = rel_path;
1691 gint did_recurse = FALSE;
1692 gint possible_count = 0;
1693 gint selection_index = -1;
1695 g_return_if_fail (fs != NULL);
1696 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1698 cmpl_state = (CompletionState*) fs->cmpl_state;
1699 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1701 if (!cmpl_state_okay (cmpl_state))
1703 /* Something went wrong. */
1704 gtk_file_selection_abort (fs);
1708 g_assert (cmpl_state->reference_dir);
1710 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1711 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1712 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1713 gtk_clist_clear (GTK_CLIST (fs->file_list));
1715 /* Set the dir_list to include ./ and ../ */
1717 text[0] = "." G_DIR_SEPARATOR_S;
1718 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1720 text[0] = ".." G_DIR_SEPARATOR_S;
1721 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1725 if (cmpl_is_a_completion (poss))
1727 possible_count += 1;
1729 filename = cmpl_this_completion (poss);
1733 if (cmpl_is_directory (poss))
1735 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1736 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1737 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1741 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1745 poss = cmpl_next_completion (cmpl_state);
1748 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1749 /* For Windows, add drives as potential selections */
1750 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1753 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1754 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1756 /* File lists are set. */
1758 g_assert (cmpl_state->reference_dir);
1763 /* User is trying to complete filenames, so advance the user's input
1764 * string to the updated_text, which is the common leading substring
1765 * of all possible completions, and if its a directory attempt
1766 * attempt completions in it. */
1768 if (cmpl_updated_text (cmpl_state)[0])
1771 if (cmpl_updated_dir (cmpl_state))
1773 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1777 gtk_file_selection_populate (fs, dir_name, TRUE);
1783 if (fs->selection_entry)
1784 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1785 cmpl_updated_text (cmpl_state));
1790 selection_index = cmpl_last_valid_char (cmpl_state) -
1791 (strlen (rel_path) - strlen (rem_path));
1792 if (fs->selection_entry)
1793 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1798 if (fs->selection_entry)
1799 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1804 if (fs->selection_entry)
1805 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1807 if (fs->selection_entry)
1809 sel_text = g_strconcat (_("Selection: "),
1810 cmpl_reference_position (cmpl_state),
1813 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1817 if (fs->history_pulldown)
1819 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1826 gtk_file_selection_abort (GtkFileSelection *fs)
1830 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1832 /* BEEP gdk_beep(); */
1834 if (fs->selection_entry)
1835 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1838 /**********************************************************************/
1839 /* External Interface */
1840 /**********************************************************************/
1842 /* The four completion state selectors
1845 cmpl_updated_text (CompletionState *cmpl_state)
1847 return cmpl_state->updated_text;
1851 cmpl_updated_dir (CompletionState *cmpl_state)
1853 return cmpl_state->re_complete;
1857 cmpl_reference_position (CompletionState *cmpl_state)
1859 return cmpl_state->reference_dir->fullname;
1863 cmpl_last_valid_char (CompletionState *cmpl_state)
1865 return cmpl_state->last_valid_char;
1869 cmpl_completion_fullname (gchar *text,
1870 CompletionState *cmpl_state)
1872 static char nothing[2] = "";
1874 if (!cmpl_state_okay (cmpl_state))
1878 else if (g_path_is_absolute (text))
1880 strcpy (cmpl_state->updated_text, text);
1883 else if (text[0] == '~')
1888 dir = open_user_dir (text, cmpl_state);
1892 /* spencer says just return ~something, so
1893 * for now just do it. */
1894 strcpy (cmpl_state->updated_text, text);
1899 strcpy (cmpl_state->updated_text, dir->fullname);
1901 slash = strchr (text, G_DIR_SEPARATOR);
1904 strcat (cmpl_state->updated_text, slash);
1910 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1911 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1912 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1913 strcat (cmpl_state->updated_text, text);
1916 return cmpl_state->updated_text;
1919 /* The three completion selectors
1922 cmpl_this_completion (PossibleCompletion* pc)
1928 cmpl_is_directory (PossibleCompletion* pc)
1930 return pc->is_directory;
1934 cmpl_is_a_completion (PossibleCompletion* pc)
1936 return pc->is_a_completion;
1939 /**********************************************************************/
1940 /* Construction, deletion */
1941 /**********************************************************************/
1943 static CompletionState*
1944 cmpl_init_state (void)
1946 gchar *sys_getcwd_buf;
1948 CompletionState *new_state;
1950 new_state = g_new (CompletionState, 1);
1952 /* g_get_current_dir() returns a string in the "system" charset */
1953 sys_getcwd_buf = g_get_current_dir ();
1954 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
1955 g_free (sys_getcwd_buf);
1959 new_state->reference_dir = NULL;
1960 new_state->completion_dir = NULL;
1961 new_state->active_completion_dir = NULL;
1962 new_state->directory_storage = NULL;
1963 new_state->directory_sent_storage = NULL;
1964 new_state->last_valid_char = 0;
1965 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1966 new_state->updated_text_alloc = MAXPATHLEN;
1967 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1968 new_state->the_completion.text_alloc = MAXPATHLEN;
1969 new_state->user_dir_name_buffer = NULL;
1970 new_state->user_directories = NULL;
1972 new_state->reference_dir = open_dir (utf8_cwd, new_state);
1974 if (!new_state->reference_dir)
1976 /* Directories changing from underneath us, grumble */
1977 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
1986 cmpl_free_dir_list (GList* dp0)
1992 free_dir (dp->data);
2000 cmpl_free_dir_sent_list (GList* dp0)
2006 free_dir_sent (dp->data);
2014 cmpl_free_state (CompletionState* cmpl_state)
2016 g_return_if_fail (cmpl_state != NULL);
2018 cmpl_free_dir_list (cmpl_state->directory_storage);
2019 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2021 if (cmpl_state->user_dir_name_buffer)
2022 g_free (cmpl_state->user_dir_name_buffer);
2023 if (cmpl_state->user_directories)
2024 g_free (cmpl_state->user_directories);
2025 if (cmpl_state->the_completion.text)
2026 g_free (cmpl_state->the_completion.text);
2027 if (cmpl_state->updated_text)
2028 g_free (cmpl_state->updated_text);
2030 g_free (cmpl_state);
2034 free_dir (CompletionDir* dir)
2036 g_free (dir->fullname);
2041 free_dir_sent (CompletionDirSent* sent)
2044 for (i = 0; i < sent->entry_count; i++)
2045 g_free (sent->entries[i].entry_name);
2046 g_free (sent->entries);
2051 prune_memory_usage (CompletionState *cmpl_state)
2053 GList* cdsl = cmpl_state->directory_sent_storage;
2054 GList* cdl = cmpl_state->directory_storage;
2058 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2063 cmpl_free_dir_sent_list (cdsl->next);
2067 cmpl_state->directory_storage = NULL;
2070 if (cdl->data == cmpl_state->reference_dir)
2071 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2073 free_dir (cdl->data);
2080 /**********************************************************************/
2081 /* The main entrances. */
2082 /**********************************************************************/
2084 static PossibleCompletion*
2085 cmpl_completion_matches (gchar *text_to_complete,
2086 gchar **remaining_text,
2087 CompletionState *cmpl_state)
2090 PossibleCompletion *poss;
2092 prune_memory_usage (cmpl_state);
2094 g_assert (text_to_complete != NULL);
2096 cmpl_state->user_completion_index = -1;
2097 cmpl_state->last_completion_text = text_to_complete;
2098 cmpl_state->the_completion.text[0] = 0;
2099 cmpl_state->last_valid_char = 0;
2100 cmpl_state->updated_text_len = -1;
2101 cmpl_state->updated_text[0] = 0;
2102 cmpl_state->re_complete = FALSE;
2105 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2107 if (text_to_complete[0] == '~' && !first_slash)
2109 /* Text starts with ~ and there is no slash, show all the
2110 * home directory completions.
2112 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2114 update_cmpl (poss, cmpl_state);
2119 cmpl_state->reference_dir =
2120 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2122 if (!cmpl_state->reference_dir)
2125 cmpl_state->completion_dir =
2126 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2128 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2130 if (!cmpl_state->completion_dir)
2133 cmpl_state->completion_dir->cmpl_index = -1;
2134 cmpl_state->completion_dir->cmpl_parent = NULL;
2135 cmpl_state->completion_dir->cmpl_text = *remaining_text;
2137 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2139 cmpl_state->reference_dir = cmpl_state->completion_dir;
2141 poss = attempt_file_completion (cmpl_state);
2143 update_cmpl (poss, cmpl_state);
2148 static PossibleCompletion*
2149 cmpl_next_completion (CompletionState* cmpl_state)
2151 PossibleCompletion* poss = NULL;
2153 cmpl_state->the_completion.text[0] = 0;
2156 if (cmpl_state->user_completion_index >= 0)
2157 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2159 poss = attempt_file_completion (cmpl_state);
2161 poss = attempt_file_completion (cmpl_state);
2164 update_cmpl (poss, cmpl_state);
2169 /**********************************************************************/
2170 /* Directory Operations */
2171 /**********************************************************************/
2173 /* Open the directory where completion will begin from, if possible. */
2174 static CompletionDir*
2175 open_ref_dir (gchar *text_to_complete,
2176 gchar **remaining_text,
2177 CompletionState *cmpl_state)
2180 CompletionDir *new_dir;
2182 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2184 #ifdef G_WITH_CYGWIN
2185 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2188 sprintf (root_dir, "//%c", text_to_complete[2]);
2190 new_dir = open_dir (root_dir, cmpl_state);
2193 *remaining_text = text_to_complete + 4;
2201 else if (text_to_complete[0] == '~')
2203 new_dir = open_user_dir (text_to_complete, cmpl_state);
2208 *remaining_text = first_slash + 1;
2210 *remaining_text = text_to_complete + strlen (text_to_complete);
2218 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2220 gchar *tmp = g_strdup (text_to_complete);
2224 while (*p && *p != '*' && *p != '?')
2228 p = strrchr (tmp, G_DIR_SEPARATOR);
2236 new_dir = open_dir (tmp, cmpl_state);
2239 *remaining_text = text_to_complete +
2240 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2244 /* If no possible candidates, use the cwd */
2245 gchar *sys_curdir = g_get_current_dir ();
2246 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2248 g_free (sys_curdir);
2250 new_dir = open_dir (utf8_curdir, cmpl_state);
2253 *remaining_text = text_to_complete;
2255 g_free (utf8_curdir);
2262 *remaining_text = text_to_complete;
2264 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2269 new_dir->cmpl_index = -1;
2270 new_dir->cmpl_parent = NULL;
2278 /* open a directory by user name */
2279 static CompletionDir*
2280 open_user_dir (gchar *text_to_complete,
2281 CompletionState *cmpl_state)
2283 CompletionDir *result;
2287 g_assert (text_to_complete && text_to_complete[0] == '~');
2289 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2292 cmp_len = first_slash - text_to_complete - 1;
2294 cmp_len = strlen (text_to_complete + 1);
2299 gchar *homedir = g_get_home_dir ();
2300 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2305 result = open_dir (utf8_homedir, cmpl_state);
2309 g_free (utf8_homedir);
2314 gchar* copy = g_new (char, cmp_len + 1);
2318 strncpy (copy, text_to_complete + 1, cmp_len);
2320 pwd = getpwnam (copy);
2327 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2328 result = open_dir (utf8_dir, cmpl_state);
2336 /* open a directory relative the the current relative directory */
2337 static CompletionDir*
2338 open_relative_dir (gchar *dir_name,
2340 CompletionState *cmpl_state)
2342 CompletionDir *result;
2345 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2346 g_string_assign (path, dir->fullname);
2348 if (dir->fullname_len > 1
2349 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2350 g_string_append_c (path, G_DIR_SEPARATOR);
2351 g_string_append (path, dir_name);
2353 result = open_dir (path->str, cmpl_state);
2355 g_string_free (path, TRUE);
2360 /* after the cache lookup fails, really open a new directory */
2361 static CompletionDirSent*
2362 open_new_dir (gchar *dir_name,
2364 gboolean stat_subdirs)
2366 CompletionDirSent *sent;
2368 struct dirent *dirent_ptr;
2369 gint entry_count = 0;
2372 struct stat ent_sbuf;
2374 gchar *sys_dir_name;
2376 sent = g_new (CompletionDirSent, 1);
2377 sent->mtime = sbuf->st_mtime;
2378 sent->inode = sbuf->st_ino;
2379 sent->device = sbuf->st_dev;
2381 path = g_string_sized_new (2*MAXPATHLEN + 10);
2383 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2386 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2390 directory = opendir (sys_dir_name);
2394 g_free (sys_dir_name);
2398 while ((dirent_ptr = readdir (directory)) != NULL)
2401 sent->entries = g_new (CompletionDirEntry, entry_count);
2402 sent->entry_count = entry_count;
2404 rewinddir (directory);
2406 for (i = 0; i < entry_count; i += 1)
2408 dirent_ptr = readdir (directory);
2413 closedir (directory);
2414 g_free (sys_dir_name);
2418 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent_ptr->d_name, -1, NULL, NULL, NULL);
2419 if (!g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2421 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent_ptr->d_name);
2425 g_string_assign (path, sys_dir_name);
2426 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2428 g_string_append_c (path, G_DIR_SEPARATOR);
2430 g_string_append (path, dirent_ptr->d_name);
2434 /* Here we know path->str is a "system charset" string */
2435 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2436 sent->entries[n_entries].is_dir = TRUE;
2438 /* stat may fail, and we don't mind, since it could be a
2439 * dangling symlink. */
2440 sent->entries[n_entries].is_dir = FALSE;
2443 sent->entries[n_entries].is_dir = 1;
2447 sent->entry_count = n_entries;
2449 g_free (sys_dir_name);
2450 g_string_free (path, TRUE);
2451 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2453 closedir (directory);
2458 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2461 check_dir (gchar *dir_name,
2462 struct stat *result,
2463 gboolean *stat_subdirs)
2465 /* A list of directories that we know only contain other directories.
2466 * Trying to stat every file in these directories would be very
2473 struct stat statbuf;
2474 } no_stat_dirs[] = {
2475 { "/afs", FALSE, { 0 } },
2476 { "/net", FALSE, { 0 } }
2479 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
2480 static gboolean initialized = FALSE;
2481 gchar *sys_dir_name;
2487 for (i = 0; i < n_no_stat_dirs; i++)
2489 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2490 no_stat_dirs[i].present = TRUE;
2494 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2497 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2501 if (stat (sys_dir_name, result) < 0)
2503 g_free (sys_dir_name);
2507 g_free (sys_dir_name);
2509 *stat_subdirs = TRUE;
2510 for (i = 0; i < n_no_stat_dirs; i++)
2512 if (no_stat_dirs[i].present &&
2513 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2514 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2516 *stat_subdirs = FALSE;
2526 /* open a directory by absolute pathname */
2527 static CompletionDir*
2528 open_dir (gchar *dir_name,
2529 CompletionState *cmpl_state)
2532 gboolean stat_subdirs;
2533 CompletionDirSent *sent;
2536 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2537 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2540 cdsl = cmpl_state->directory_sent_storage;
2546 if (sent->inode == sbuf.st_ino &&
2547 sent->mtime == sbuf.st_mtime &&
2548 sent->device == sbuf.st_dev)
2549 return attach_dir (sent, dir_name, cmpl_state);
2554 stat_subdirs = TRUE;
2557 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
2561 cmpl_state->directory_sent_storage =
2562 g_list_prepend (cmpl_state->directory_sent_storage, sent);
2564 return attach_dir (sent, dir_name, cmpl_state);
2570 static CompletionDir*
2571 attach_dir (CompletionDirSent *sent,
2573 CompletionState *cmpl_state)
2575 CompletionDir* new_dir;
2577 new_dir = g_new (CompletionDir, 1);
2579 cmpl_state->directory_storage =
2580 g_list_prepend (cmpl_state->directory_storage, new_dir);
2582 new_dir->sent = sent;
2583 new_dir->fullname = g_strdup (dir_name);
2584 new_dir->fullname_len = strlen (dir_name);
2590 correct_dir_fullname (CompletionDir* cmpl_dir)
2592 gint length = strlen (cmpl_dir->fullname);
2593 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2594 gchar *sys_filename;
2597 /* Does it end with /. (\.) ? */
2599 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2601 /* Is it just the root directory (on a drive) ? */
2602 if (cmpl_dir->fullname + length - 2 == first_slash)
2604 cmpl_dir->fullname[length - 1] = 0;
2605 cmpl_dir->fullname_len = length - 1;
2610 cmpl_dir->fullname[length - 2] = 0;
2614 /* Ends with /./ (\.\)? */
2615 else if (length >= 3 &&
2616 strcmp (cmpl_dir->fullname + length - 3,
2617 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2618 cmpl_dir->fullname[length - 2] = 0;
2620 /* Ends with /.. (\..) ? */
2621 else if (length >= 3 &&
2622 strcmp (cmpl_dir->fullname + length - 3,
2623 G_DIR_SEPARATOR_S "..") == 0)
2625 /* Is it just /.. (X:\..)? */
2626 if (cmpl_dir->fullname + length - 3 == first_slash)
2628 cmpl_dir->fullname[length - 2] = 0;
2629 cmpl_dir->fullname_len = length - 2;
2633 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2636 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2640 if (stat (sys_filename, &sbuf) < 0)
2642 g_free (sys_filename);
2646 g_free (sys_filename);
2648 cmpl_dir->fullname[length - 3] = 0;
2650 if (!correct_parent (cmpl_dir, &sbuf))
2654 /* Ends with /../ (\..\)? */
2655 else if (length >= 4 &&
2656 strcmp (cmpl_dir->fullname + length - 4,
2657 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2659 /* Is it just /../ (X:\..\)? */
2660 if (cmpl_dir->fullname + length - 4 == first_slash)
2662 cmpl_dir->fullname[length - 3] = 0;
2663 cmpl_dir->fullname_len = length - 3;
2667 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2670 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2674 if (stat (sys_filename, &sbuf) < 0)
2676 g_free (sys_filename);
2680 g_free (sys_filename);
2682 cmpl_dir->fullname[length - 4] = 0;
2684 if (!correct_parent (cmpl_dir, &sbuf))
2688 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
2694 correct_parent (CompletionDir *cmpl_dir,
2701 gchar *sys_filename;
2704 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2705 g_assert (last_slash);
2706 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2708 /* Clever (?) way to check for top-level directory that works also on
2709 * Win32, where there is a drive letter and colon prefixed...
2711 if (last_slash != first_slash)
2721 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2724 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2726 last_slash[0] = G_DIR_SEPARATOR;
2730 if (stat (sys_filename, &parbuf) < 0)
2732 g_free (sys_filename);
2735 last_slash[0] = G_DIR_SEPARATOR;
2738 g_free (sys_filename);
2740 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2741 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2742 /* it wasn't a link */
2748 last_slash[0] = G_DIR_SEPARATOR;
2750 /* it was a link, have to figure it out the hard way */
2752 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
2757 g_free (cmpl_dir->fullname);
2759 cmpl_dir->fullname = new_name;
2768 find_parent_dir_fullname (gchar* dirname)
2770 gchar *sys_orig_dir;
2775 sys_orig_dir = g_get_current_dir ();
2776 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
2779 g_free (sys_orig_dir);
2780 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2784 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
2786 g_free (sys_dirname);
2787 g_free (sys_orig_dir);
2791 g_free (sys_dirname);
2793 sys_cwd = g_get_current_dir ();
2794 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
2797 if (chdir (sys_orig_dir) != 0)
2800 g_free (sys_orig_dir);
2804 g_free (sys_orig_dir);
2810 /**********************************************************************/
2811 /* Completion Operations */
2812 /**********************************************************************/
2816 static PossibleCompletion*
2817 attempt_homedir_completion (gchar *text_to_complete,
2818 CompletionState *cmpl_state)
2822 if (!cmpl_state->user_dir_name_buffer &&
2823 !get_pwdb (cmpl_state))
2825 length = strlen (text_to_complete) - 1;
2827 cmpl_state->user_completion_index += 1;
2829 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2831 index = first_diff_index (text_to_complete + 1,
2832 cmpl_state->user_directories
2833 [cmpl_state->user_completion_index].login);
2840 if (cmpl_state->last_valid_char < (index + 1))
2841 cmpl_state->last_valid_char = index + 1;
2842 cmpl_state->user_completion_index += 1;
2846 cmpl_state->the_completion.is_a_completion = 1;
2847 cmpl_state->the_completion.is_directory = TRUE;
2849 append_completion_text ("~", cmpl_state);
2851 append_completion_text (cmpl_state->
2852 user_directories[cmpl_state->user_completion_index].login,
2855 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
2858 if (text_to_complete[1]
2859 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2861 cmpl_state->user_completion_index = -1;
2866 cmpl_state->user_completion_index += 1;
2867 cmpl_state->the_completion.is_a_completion = 1;
2868 cmpl_state->the_completion.is_directory = TRUE;
2870 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
2876 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2877 #define FOLD(c) (tolower(c))
2882 /* returns the index (>= 0) of the first differing character,
2883 * PATTERN_MATCH if the completion matches */
2885 first_diff_index (gchar *pat,
2890 while (*pat && *text && FOLD (*text) == FOLD (*pat))
2900 return PATTERN_MATCH;
2903 static PossibleCompletion*
2904 append_completion_text (gchar *text,
2905 CompletionState *cmpl_state)
2909 if (!cmpl_state->the_completion.text)
2912 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
2914 if (cmpl_state->the_completion.text_alloc > len)
2916 strcat (cmpl_state->the_completion.text, text);
2917 return &cmpl_state->the_completion;
2923 cmpl_state->the_completion.text_alloc = i;
2925 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
2927 if (!cmpl_state->the_completion.text)
2931 strcat (cmpl_state->the_completion.text, text);
2932 return &cmpl_state->the_completion;
2936 static CompletionDir*
2937 find_completion_dir (gchar *text_to_complete,
2938 gchar **remaining_text,
2939 CompletionState *cmpl_state)
2941 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2942 CompletionDir* dir = cmpl_state->reference_dir;
2943 CompletionDir* next;
2944 *remaining_text = text_to_complete;
2948 gint len = first_slash - *remaining_text;
2950 gchar *found_name = NULL; /* Quiet gcc */
2952 gchar* pat_buf = g_new (gchar, len + 1);
2954 strncpy (pat_buf, *remaining_text, len);
2957 for (i = 0; i < dir->sent->entry_count; i += 1)
2959 if (dir->sent->entries[i].is_dir &&
2960 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
2961 FNMATCH_FLAGS)!= FNM_NOMATCH)
2971 found_name = dir->sent->entries[i].entry_name;
2978 /* Perhaps we are trying to open an automount directory */
2979 found_name = pat_buf;
2982 next = open_relative_dir (found_name, dir, cmpl_state);
2990 next->cmpl_parent = dir;
2994 if (!correct_dir_fullname (dir))
3000 *remaining_text = first_slash + 1;
3001 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3010 update_cmpl (PossibleCompletion *poss,
3011 CompletionState *cmpl_state)
3015 if (!poss || !cmpl_is_a_completion (poss))
3018 cmpl_len = strlen (cmpl_this_completion (poss));
3020 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3022 cmpl_state->updated_text =
3023 (gchar*)g_realloc (cmpl_state->updated_text,
3024 cmpl_state->updated_text_alloc);
3025 cmpl_state->updated_text_alloc = 2*cmpl_len;
3028 if (cmpl_state->updated_text_len < 0)
3030 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3031 cmpl_state->updated_text_len = cmpl_len;
3032 cmpl_state->re_complete = cmpl_is_directory (poss);
3034 else if (cmpl_state->updated_text_len == 0)
3036 cmpl_state->re_complete = FALSE;
3041 first_diff_index (cmpl_state->updated_text,
3042 cmpl_this_completion (poss));
3044 cmpl_state->re_complete = FALSE;
3046 if (first_diff == PATTERN_MATCH)
3049 if (first_diff > cmpl_state->updated_text_len)
3050 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3052 cmpl_state->updated_text_len = first_diff;
3053 cmpl_state->updated_text[first_diff] = 0;
3057 static PossibleCompletion*
3058 attempt_file_completion (CompletionState *cmpl_state)
3060 gchar *pat_buf, *first_slash;
3061 CompletionDir *dir = cmpl_state->active_completion_dir;
3063 dir->cmpl_index += 1;
3065 if (dir->cmpl_index == dir->sent->entry_count)
3067 if (dir->cmpl_parent == NULL)
3069 cmpl_state->active_completion_dir = NULL;
3075 cmpl_state->active_completion_dir = dir->cmpl_parent;
3077 return attempt_file_completion (cmpl_state);
3081 g_assert (dir->cmpl_text);
3083 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3087 gint len = first_slash - dir->cmpl_text;
3089 pat_buf = g_new (gchar, len + 1);
3090 strncpy (pat_buf, dir->cmpl_text, len);
3095 gint len = strlen (dir->cmpl_text);
3097 pat_buf = g_new (gchar, len + 2);
3098 strcpy (pat_buf, dir->cmpl_text);
3099 /* Don't append a * if the user entered one herself.
3100 * This way one can complete *.h and don't get matches
3101 * on any .help files, for instance.
3103 if (strchr (pat_buf, '*') == NULL)
3104 strcpy (pat_buf + len, "*");
3109 if (dir->sent->entries[dir->cmpl_index].is_dir)
3111 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3112 FNMATCH_FLAGS) != FNM_NOMATCH)
3114 CompletionDir* new_dir;
3116 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3125 new_dir->cmpl_parent = dir;
3127 new_dir->cmpl_index = -1;
3128 new_dir->cmpl_text = first_slash + 1;
3130 cmpl_state->active_completion_dir = new_dir;
3133 return attempt_file_completion (cmpl_state);
3138 return attempt_file_completion (cmpl_state);
3144 return attempt_file_completion (cmpl_state);
3149 if (dir->cmpl_parent != NULL)
3151 append_completion_text (dir->fullname +
3152 strlen (cmpl_state->completion_dir->fullname) + 1,
3154 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3157 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3159 cmpl_state->the_completion.is_a_completion =
3160 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3161 FNMATCH_FLAGS) != FNM_NOMATCH;
3163 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3164 if (dir->sent->entries[dir->cmpl_index].is_dir)
3165 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3168 return &cmpl_state->the_completion;
3175 get_pwdb (CompletionState* cmpl_state)
3177 struct passwd *pwd_ptr;
3180 gint len = 0, i, count = 0;
3182 if (cmpl_state->user_dir_name_buffer)
3186 while ((pwd_ptr = getpwent ()) != NULL)
3188 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3189 len += strlen (utf8);
3191 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3192 len += strlen (utf8);
3200 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3201 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3202 cmpl_state->user_directories_len = count;
3204 buf_ptr = cmpl_state->user_dir_name_buffer;
3206 for (i = 0; i < count; i += 1)
3208 pwd_ptr = getpwent ();
3215 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3216 strcpy (buf_ptr, utf8);
3219 cmpl_state->user_directories[i].login = buf_ptr;
3221 buf_ptr += strlen (buf_ptr);
3224 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3225 strcpy (buf_ptr, utf8);
3228 cmpl_state->user_directories[i].homedir = buf_ptr;
3230 buf_ptr += strlen (buf_ptr);
3234 qsort (cmpl_state->user_directories,
3235 cmpl_state->user_directories_len,
3236 sizeof (CompletionUserDir),
3245 if (cmpl_state->user_dir_name_buffer)
3246 g_free (cmpl_state->user_dir_name_buffer);
3247 if (cmpl_state->user_directories)
3248 g_free (cmpl_state->user_directories);
3250 cmpl_state->user_dir_name_buffer = NULL;
3251 cmpl_state->user_directories = NULL;
3257 compare_user_dir (const void *a,
3260 return strcmp ((((CompletionUserDir*)a))->login,
3261 (((CompletionUserDir*)b))->login);
3267 compare_cmpl_dir (const void *a,
3270 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3271 return strcmp ((((CompletionDirEntry*)a))->entry_name,
3272 (((CompletionDirEntry*)b))->entry_name);
3274 return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
3275 (((CompletionDirEntry*)b))->entry_name);
3280 cmpl_state_okay (CompletionState* cmpl_state)
3282 return cmpl_state && cmpl_state->reference_dir;
3286 cmpl_strerror (gint err)
3288 if (err == CMPL_ERRNO_TOO_LONG)
3289 return _("Name too long");
3290 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3291 return _("Couldn't convert filename");
3293 return g_strerror (err);