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;
229 /* File completion functions which would be external, were they used
230 * outside of this file.
233 static CompletionState* cmpl_init_state (void);
234 static void cmpl_free_state (CompletionState *cmpl_state);
235 static gint cmpl_state_okay (CompletionState* cmpl_state);
236 static gchar* cmpl_strerror (gint);
238 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
239 gchar **remaining_text,
240 CompletionState *cmpl_state);
242 /* Returns a name for consideration, possibly a completion, this name
243 * will be invalid after the next call to cmpl_next_completion.
245 static char* cmpl_this_completion (PossibleCompletion*);
247 /* True if this completion matches the given text. Otherwise, this
248 * output can be used to have a list of non-completions.
250 static gint cmpl_is_a_completion (PossibleCompletion*);
252 /* True if the completion is a directory
254 static gboolean cmpl_is_directory (PossibleCompletion*);
256 /* Obtains the next completion, or NULL
258 static PossibleCompletion* cmpl_next_completion (CompletionState*);
260 /* Updating completions: the return value of cmpl_updated_text() will
261 * be text_to_complete completed as much as possible after the most
262 * recent call to cmpl_completion_matches. For the present
263 * application, this is the suggested replacement for the user's input
264 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
267 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
269 /* After updating, to see if the completion was a directory, call
270 * this. If it was, you should consider re-calling completion_matches.
272 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
274 /* Current location: if using file completion, return the current
275 * directory, from which file completion begins. More specifically,
276 * the cwd concatenated with all exact completions up to the last
277 * directory delimiter('/').
279 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
281 /* backing up: if cmpl_completion_matches returns NULL, you may query
282 * the index of the last completable character into cmpl_updated_text.
284 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
286 /* When the user selects a non-directory, call cmpl_completion_fullname
287 * to get the full name of the selected file.
289 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
292 /* Directory operations. */
293 static CompletionDir* open_ref_dir (gchar* text_to_complete,
294 gchar** remaining_text,
295 CompletionState* cmpl_state);
296 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
297 static gboolean check_dir (gchar *dir_name,
299 gboolean *stat_subdirs);
301 static CompletionDir* open_dir (gchar* dir_name,
302 CompletionState* cmpl_state);
304 static CompletionDir* open_user_dir (gchar* text_to_complete,
305 CompletionState *cmpl_state);
307 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
308 CompletionState *cmpl_state);
309 static CompletionDirSent* open_new_dir (gchar* dir_name,
311 gboolean stat_subdirs);
312 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
313 static gint correct_parent (CompletionDir* cmpl_dir,
315 static gchar* find_parent_dir_fullname (gchar* dirname);
316 static CompletionDir* attach_dir (CompletionDirSent* sent,
318 CompletionState *cmpl_state);
319 static void free_dir_sent (CompletionDirSent* sent);
320 static void free_dir (CompletionDir *dir);
321 static void prune_memory_usage(CompletionState *cmpl_state);
323 /* Completion operations */
324 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
325 CompletionState *cmpl_state);
326 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
327 static CompletionDir* find_completion_dir(gchar* text_to_complete,
328 gchar** remaining_text,
329 CompletionState* cmpl_state);
330 static PossibleCompletion* append_completion_text(gchar* text,
331 CompletionState* cmpl_state);
333 static gint get_pwdb(CompletionState* cmpl_state);
334 static gint compare_user_dir(const void* a, const void* b);
336 static gint first_diff_index(gchar* pat, gchar* text);
337 static gint compare_cmpl_dir(const void* a, const void* b);
338 static void update_cmpl(PossibleCompletion* poss,
339 CompletionState* cmpl_state);
341 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
342 static void gtk_file_selection_init (GtkFileSelection *filesel);
343 static void gtk_file_selection_destroy (GtkObject *object);
344 static gint gtk_file_selection_key_press (GtkWidget *widget,
347 static gint gtk_file_selection_insert_text (GtkWidget *widget,
348 const gchar *new_text,
349 gint new_text_length,
353 static void gtk_file_selection_file_button (GtkWidget *widget,
356 GdkEventButton *bevent,
359 static void gtk_file_selection_dir_button (GtkWidget *widget,
362 GdkEventButton *bevent,
365 static void gtk_file_selection_populate (GtkFileSelection *fs,
368 static void gtk_file_selection_abort (GtkFileSelection *fs);
370 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
373 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
374 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
375 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
379 static GtkWindowClass *parent_class = NULL;
381 /* Saves errno when something cmpl does fails. */
382 static gint cmpl_errno;
386 * Take the path currently in the file selection
387 * entry field and translate as necessary from
388 * a WIN32 style to CYGWIN32 style path. For
389 * instance translate:
390 * x:\somepath\file.jpg
392 * //x/somepath/file.jpg
394 * Replace the path in the selection text field.
395 * Return a boolean value concerning whether a
396 * translation had to be made.
399 translate_win32_path (GtkFileSelection *filesel)
405 * Retrieve the current path
407 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
410 * Translate only if this looks like a DOS-ish
411 * path... First handle any drive letters.
413 if (isalpha (path[0]) && (path[1] == ':')) {
415 * This part kind of stinks... It isn't possible
416 * to know if there is enough space in the current
417 * string for the extra character required in this
418 * conversion. Assume that there isn't enough space
419 * and use the set function on the text field to
420 * set the newly created string.
422 gchar *newPath = g_strdup_printf ("//%c/%s", path[0], (path + 3));
423 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
430 * Now, replace backslashes with forward slashes
433 if (strchr (path, '\\'))
436 for (index = 0; path[index] != '\0'; index++)
437 if (path[index] == '\\')
448 gtk_file_selection_get_type (void)
450 static GtkType file_selection_type = 0;
452 if (!file_selection_type)
454 static const GtkTypeInfo filesel_info =
457 sizeof (GtkFileSelection),
458 sizeof (GtkFileSelectionClass),
459 (GtkClassInitFunc) gtk_file_selection_class_init,
460 (GtkObjectInitFunc) gtk_file_selection_init,
461 /* reserved_1 */ NULL,
462 /* reserved_2 */ NULL,
463 (GtkClassInitFunc) NULL,
466 file_selection_type = gtk_type_unique (GTK_TYPE_DIALOG, &filesel_info);
469 return file_selection_type;
473 gtk_file_selection_class_init (GtkFileSelectionClass *class)
475 GtkObjectClass *object_class;
477 object_class = (GtkObjectClass*) class;
479 parent_class = gtk_type_class (GTK_TYPE_DIALOG);
481 object_class->destroy = gtk_file_selection_destroy;
485 gtk_file_selection_init (GtkFileSelection *filesel)
487 GtkWidget *entry_vbox;
489 GtkWidget *list_hbox;
490 GtkWidget *confirm_area;
491 GtkWidget *pulldown_hbox;
492 GtkWidget *scrolled_win;
496 char *file_title [2];
498 dialog = GTK_DIALOG (filesel);
500 filesel->cmpl_state = cmpl_init_state ();
502 /* The dialog-sized vertical box */
503 filesel->main_vbox = dialog->vbox;
504 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
506 /* The horizontal box containing create, rename etc. buttons */
507 filesel->button_area = gtk_hbutton_box_new ();
508 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
509 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
510 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
512 gtk_widget_show (filesel->button_area);
514 gtk_file_selection_show_fileop_buttons (filesel);
516 /* hbox for pulldown menu */
517 pulldown_hbox = gtk_hbox_new (TRUE, 5);
518 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
519 gtk_widget_show (pulldown_hbox);
522 filesel->history_pulldown = gtk_option_menu_new ();
523 gtk_widget_show (filesel->history_pulldown);
524 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
527 /* The horizontal box containing the directory and file listboxes */
528 list_hbox = gtk_hbox_new (FALSE, 5);
529 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
530 gtk_widget_show (list_hbox);
532 /* The directories clist */
533 dir_title[0] = _("Directories");
535 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
536 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
537 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
538 (GtkSignalFunc) gtk_file_selection_dir_button,
540 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->dir_list), 0, TRUE);
541 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
543 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
544 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
545 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
546 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
547 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
548 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
549 gtk_widget_show (filesel->dir_list);
550 gtk_widget_show (scrolled_win);
552 /* The files clist */
553 file_title[0] = _("Files");
554 file_title[1] = NULL;
555 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
556 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
557 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
558 (GtkSignalFunc) gtk_file_selection_file_button,
560 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->file_list), 0, TRUE);
561 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
563 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
564 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
565 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
566 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
567 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
568 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
569 gtk_widget_show (filesel->file_list);
570 gtk_widget_show (scrolled_win);
572 /* action area for packing buttons into. */
573 filesel->action_area = gtk_hbox_new (TRUE, 0);
574 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
576 gtk_widget_show (filesel->action_area);
578 /* The OK/Cancel button area */
579 confirm_area = dialog->action_area;
582 filesel->ok_button = gtk_dialog_add_button (dialog,
586 gtk_widget_grab_default (filesel->ok_button);
588 /* The Cancel button */
589 filesel->cancel_button = gtk_dialog_add_button (dialog,
590 GTK_STOCK_BUTTON_CANCEL,
591 GTK_RESPONSE_CANCEL);
593 /* The selection entry widget */
594 entry_vbox = gtk_vbox_new (FALSE, 2);
595 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
596 gtk_widget_show (entry_vbox);
598 filesel->selection_text = label = gtk_label_new ("");
599 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
600 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
601 gtk_widget_show (label);
603 filesel->selection_entry = gtk_entry_new ();
604 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
605 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
606 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "insert_text",
607 (GtkSignalFunc) gtk_file_selection_insert_text, NULL);
608 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
609 (GtkSignalFunc) gtk_widget_grab_default,
610 GTK_OBJECT (filesel->ok_button));
611 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
612 (GtkSignalFunc) gtk_button_clicked,
613 GTK_OBJECT (filesel->ok_button));
614 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
615 gtk_widget_show (filesel->selection_entry);
617 if (!cmpl_state_okay (filesel->cmpl_state))
621 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
623 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
627 gtk_file_selection_populate (filesel, "", FALSE);
630 gtk_widget_grab_focus (filesel->selection_entry);
634 gtk_file_selection_new (const gchar *title)
636 GtkFileSelection *filesel;
638 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
639 gtk_window_set_title (GTK_WINDOW (filesel), title);
640 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
642 return GTK_WIDGET (filesel);
646 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
648 g_return_if_fail (filesel != NULL);
649 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
651 /* delete, create directory, and rename */
652 if (!filesel->fileop_c_dir)
654 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
655 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
656 (GtkSignalFunc) gtk_file_selection_create_dir,
658 gtk_box_pack_start (GTK_BOX (filesel->button_area),
659 filesel->fileop_c_dir, TRUE, TRUE, 0);
660 gtk_widget_show (filesel->fileop_c_dir);
663 if (!filesel->fileop_del_file)
665 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
666 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
667 (GtkSignalFunc) gtk_file_selection_delete_file,
669 gtk_box_pack_start (GTK_BOX (filesel->button_area),
670 filesel->fileop_del_file, TRUE, TRUE, 0);
671 gtk_widget_show (filesel->fileop_del_file);
674 if (!filesel->fileop_ren_file)
676 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
677 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
678 (GtkSignalFunc) gtk_file_selection_rename_file,
680 gtk_box_pack_start (GTK_BOX (filesel->button_area),
681 filesel->fileop_ren_file, TRUE, TRUE, 0);
682 gtk_widget_show (filesel->fileop_ren_file);
685 gtk_widget_queue_resize (GTK_WIDGET (filesel));
689 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
691 g_return_if_fail (filesel != NULL);
692 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
694 if (filesel->fileop_ren_file)
696 gtk_widget_destroy (filesel->fileop_ren_file);
697 filesel->fileop_ren_file = NULL;
700 if (filesel->fileop_del_file)
702 gtk_widget_destroy (filesel->fileop_del_file);
703 filesel->fileop_del_file = NULL;
706 if (filesel->fileop_c_dir)
708 gtk_widget_destroy (filesel->fileop_c_dir);
709 filesel->fileop_c_dir = NULL;
716 gtk_file_selection_set_filename (GtkFileSelection *filesel,
717 const gchar *filename)
720 const char *name, *last_slash;
722 g_return_if_fail (filesel != NULL);
723 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
724 g_return_if_fail (filename != NULL);
726 last_slash = strrchr (filename, G_DIR_SEPARATOR);
735 buf = g_strdup (filename);
736 buf[last_slash - filename + 1] = 0;
737 name = last_slash + 1;
740 gtk_file_selection_populate (filesel, buf, FALSE);
742 if (filesel->selection_entry)
743 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
748 gtk_file_selection_get_filename (GtkFileSelection *filesel)
750 static gchar nothing[2] = "";
751 static gchar something[MAXPATHLEN*2];
755 g_return_val_if_fail (filesel != NULL, nothing);
756 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
759 translate_win32_path (filesel);
761 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
764 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
767 strncpy (something, sys_filename, sizeof (something));
768 g_free (sys_filename);
776 gtk_file_selection_complete (GtkFileSelection *filesel,
777 const gchar *pattern)
779 g_return_if_fail (filesel != NULL);
780 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
781 g_return_if_fail (pattern != NULL);
783 if (filesel->selection_entry)
784 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
785 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
789 gtk_file_selection_destroy (GtkObject *object)
791 GtkFileSelection *filesel;
793 HistoryCallbackArg *callback_arg;
795 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
797 filesel = GTK_FILE_SELECTION (object);
799 if (filesel->fileop_dialog)
801 gtk_widget_destroy (filesel->fileop_dialog);
802 filesel->fileop_dialog = NULL;
805 if (filesel->history_list)
807 list = filesel->history_list;
810 callback_arg = list->data;
811 g_free (callback_arg->directory);
812 g_free (callback_arg);
815 g_list_free (filesel->history_list);
816 filesel->history_list = NULL;
819 if (filesel->cmpl_state)
821 cmpl_free_state (filesel->cmpl_state);
822 filesel->cmpl_state = NULL;
825 GTK_OBJECT_CLASS (parent_class)->destroy (object);
828 /* Begin file operations callbacks */
831 gtk_file_selection_fileop_error (GtkFileSelection *fs,
832 gchar *error_message)
836 g_return_if_fail (error_message != NULL);
839 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
840 GTK_DIALOG_DESTROY_WITH_PARENT,
843 "%s", error_message);
845 /* yes, we free it */
846 g_free (error_message);
848 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
850 gtk_signal_connect_object (GTK_OBJECT (dialog), "response",
851 (GtkSignalFunc) gtk_widget_destroy,
854 gtk_widget_show (dialog);
858 gtk_file_selection_fileop_destroy (GtkWidget *widget,
861 GtkFileSelection *fs = data;
863 g_return_if_fail (fs != NULL);
864 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
866 fs->fileop_dialog = NULL;
871 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
874 GtkFileSelection *fs = data;
878 gchar *sys_full_path;
880 GError *error = NULL;
881 CompletionState *cmpl_state;
883 g_return_if_fail (fs != NULL);
884 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
886 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
887 cmpl_state = (CompletionState*) fs->cmpl_state;
888 path = cmpl_reference_position (cmpl_state);
890 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
891 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
894 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
895 buf = g_strdup_printf (_("The directory name \"%s\" contains symbols that are not allowed in filenames"), dirname);
897 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n%s"), dirname, error->message,
898 _("You probably used symbols not allowed in filenames."));
899 gtk_file_selection_fileop_error (fs, buf);
900 g_error_free (error);
904 if (mkdir (sys_full_path, 0755) < 0)
906 buf = g_strdup_printf (_("Error creating directory \"%s\": %s\n"), dirname,
908 gtk_file_selection_fileop_error (fs, buf);
913 g_free (sys_full_path);
915 gtk_widget_destroy (fs->fileop_dialog);
916 gtk_file_selection_populate (fs, "", FALSE);
920 gtk_file_selection_create_dir (GtkWidget *widget,
923 GtkFileSelection *fs = data;
929 g_return_if_fail (fs != NULL);
930 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
932 if (fs->fileop_dialog)
936 dialog = gtk_dialog_new ();
937 fs->fileop_dialog = dialog;
938 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
939 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
941 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
942 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
944 /* If file dialog is grabbed, grab option dialog */
945 /* When option dialog is closed, file dialog will be grabbed again */
946 if (GTK_WINDOW (fs)->modal)
947 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
949 vbox = gtk_vbox_new (FALSE, 0);
950 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
951 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
953 gtk_widget_show( vbox);
955 label = gtk_label_new (_("Directory name:"));
956 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
957 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
958 gtk_widget_show (label);
960 /* The directory entry widget */
961 fs->fileop_entry = gtk_entry_new ();
962 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
964 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
965 gtk_widget_show (fs->fileop_entry);
968 button = gtk_button_new_with_label (_("Create"));
969 gtk_signal_connect (GTK_OBJECT (button), "clicked",
970 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
972 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
973 button, TRUE, TRUE, 0);
974 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
975 gtk_widget_show (button);
977 button = gtk_button_new_with_label (_("Cancel"));
978 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
979 (GtkSignalFunc) gtk_widget_destroy,
981 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
982 button, TRUE, TRUE, 0);
983 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
984 gtk_widget_grab_default (button);
985 gtk_widget_show (button);
987 gtk_widget_show (dialog);
991 gtk_file_selection_delete_file_confirmed (GtkWidget *widget,
994 GtkFileSelection *fs = data;
995 CompletionState *cmpl_state;
998 gchar *sys_full_path;
999 GError *error = NULL;
1002 g_return_if_fail (fs != NULL);
1003 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1005 cmpl_state = (CompletionState*) fs->cmpl_state;
1006 path = cmpl_reference_position (cmpl_state);
1008 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1009 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1012 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1013 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1016 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1017 fs->fileop_file, error->message,
1018 _("It probably contains symbols not allowed in filenames."));
1020 gtk_file_selection_fileop_error (fs, buf);
1021 g_error_free (error);
1025 if (unlink (sys_full_path) < 0)
1027 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1028 fs->fileop_file, g_strerror (errno));
1029 gtk_file_selection_fileop_error (fs, buf);
1034 g_free (sys_full_path);
1036 gtk_widget_destroy (fs->fileop_dialog);
1037 gtk_file_selection_populate (fs, "", FALSE);
1041 gtk_file_selection_delete_file (GtkWidget *widget,
1044 GtkFileSelection *fs = data;
1052 g_return_if_fail (fs != NULL);
1053 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1055 if (fs->fileop_dialog)
1058 #ifdef G_WITH_CYGWIN
1059 translate_win32_path (fs);
1062 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1063 if (strlen (filename) < 1)
1066 fs->fileop_file = filename;
1069 fs->fileop_dialog = dialog = gtk_dialog_new ();
1070 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1071 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1073 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1074 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1076 /* If file dialog is grabbed, grab option dialog */
1077 /* When option dialog is closed, file dialog will be grabbed again */
1078 if (GTK_WINDOW (fs)->modal)
1079 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1081 vbox = gtk_vbox_new (FALSE, 0);
1082 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1083 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1085 gtk_widget_show (vbox);
1087 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1088 label = gtk_label_new (buf);
1089 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1090 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1091 gtk_widget_show (label);
1095 button = gtk_button_new_with_label (_("Delete"));
1096 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1097 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1099 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1100 button, TRUE, TRUE, 0);
1101 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1102 gtk_widget_show (button);
1104 button = gtk_button_new_with_label (_("Cancel"));
1105 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1106 (GtkSignalFunc) gtk_widget_destroy,
1108 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1109 button, TRUE, TRUE, 0);
1110 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1111 gtk_widget_grab_default (button);
1112 gtk_widget_show (button);
1114 gtk_widget_show (dialog);
1119 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1122 GtkFileSelection *fs = data;
1126 gchar *new_filename;
1127 gchar *old_filename;
1128 gchar *sys_new_filename;
1129 gchar *sys_old_filename;
1130 CompletionState *cmpl_state;
1131 GError *error = NULL;
1133 g_return_if_fail (fs != NULL);
1134 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1136 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1137 cmpl_state = (CompletionState*) fs->cmpl_state;
1138 path = cmpl_reference_position (cmpl_state);
1140 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1141 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1143 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1146 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1147 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1149 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1150 new_filename, error->message,
1151 _("You probably used symbols not allowed in filenames."));
1152 gtk_file_selection_fileop_error (fs, buf);
1153 g_error_free (error);
1157 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1160 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1161 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1163 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1164 old_filename, error->message,
1165 _("It probably contains symbols not allowed in filenames."));
1166 gtk_file_selection_fileop_error (fs, buf);
1167 g_error_free (error);
1171 if (rename (sys_old_filename, sys_new_filename) < 0)
1173 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1174 sys_old_filename, sys_new_filename,
1175 g_strerror (errno));
1176 gtk_file_selection_fileop_error (fs, buf);
1180 g_free (sys_old_filename);
1183 g_free (new_filename);
1184 g_free (old_filename);
1185 g_free (sys_new_filename);
1187 gtk_widget_destroy (fs->fileop_dialog);
1188 gtk_file_selection_populate (fs, "", FALSE);
1192 gtk_file_selection_rename_file (GtkWidget *widget,
1195 GtkFileSelection *fs = data;
1202 g_return_if_fail (fs != NULL);
1203 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1205 if (fs->fileop_dialog)
1208 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1209 if (strlen (fs->fileop_file) < 1)
1213 fs->fileop_dialog = dialog = gtk_dialog_new ();
1214 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1215 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1217 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1218 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1220 /* If file dialog is grabbed, grab option dialog */
1221 /* When option dialog closed, file dialog will be grabbed again */
1222 if (GTK_WINDOW (fs)->modal)
1223 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1225 vbox = gtk_vbox_new (FALSE, 0);
1226 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1227 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1229 gtk_widget_show(vbox);
1231 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1232 label = gtk_label_new(buf);
1233 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1234 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1235 gtk_widget_show (label);
1238 /* New filename entry */
1239 fs->fileop_entry = gtk_entry_new ();
1240 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1242 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1243 gtk_widget_show (fs->fileop_entry);
1245 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1246 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1247 0, strlen (fs->fileop_file));
1250 button = gtk_button_new_with_label (_("Rename"));
1251 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1252 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1254 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1255 button, TRUE, TRUE, 0);
1256 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1257 gtk_widget_show (button);
1259 button = gtk_button_new_with_label (_("Cancel"));
1260 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1261 (GtkSignalFunc) gtk_widget_destroy,
1263 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1264 button, TRUE, TRUE, 0);
1265 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1266 gtk_widget_grab_default (button);
1267 gtk_widget_show (button);
1269 gtk_widget_show (dialog);
1273 gtk_file_selection_insert_text (GtkWidget *widget,
1274 const gchar *new_text,
1275 gint new_text_length,
1281 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1286 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "insert_text");
1296 gtk_file_selection_key_press (GtkWidget *widget,
1300 GtkFileSelection *fs;
1303 g_return_val_if_fail (widget != NULL, FALSE);
1304 g_return_val_if_fail (event != NULL, FALSE);
1306 if (event->keyval == GDK_Tab)
1308 fs = GTK_FILE_SELECTION (user_data);
1309 #ifdef G_WITH_CYGWIN
1310 translate_win32_path (fs);
1312 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1314 text = g_strdup (text);
1316 gtk_file_selection_populate (fs, text, TRUE);
1320 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1330 gtk_file_selection_history_callback (GtkWidget *widget,
1333 GtkFileSelection *fs = data;
1334 HistoryCallbackArg *callback_arg;
1337 g_return_if_fail (fs != NULL);
1338 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1340 list = fs->history_list;
1343 callback_arg = list->data;
1345 if (callback_arg->menu_item == widget)
1347 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1356 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1357 gchar *current_directory)
1359 HistoryCallbackArg *callback_arg;
1360 GtkWidget *menu_item;
1366 g_return_if_fail (fs != NULL);
1367 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1368 g_return_if_fail (current_directory != NULL);
1370 list = fs->history_list;
1372 if (fs->history_menu)
1375 callback_arg = list->data;
1376 g_free (callback_arg->directory);
1377 g_free (callback_arg);
1380 g_list_free (fs->history_list);
1381 fs->history_list = NULL;
1383 gtk_widget_destroy (fs->history_menu);
1386 fs->history_menu = gtk_menu_new ();
1388 current_dir = g_strdup (current_directory);
1390 dir_len = strlen (current_dir);
1392 for (i = dir_len; i >= 0; i--)
1394 /* the i == dir_len is to catch the full path for the first
1396 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1398 /* another small hack to catch the full path */
1400 current_dir[i + 1] = '\0';
1401 #ifdef G_WITH_CYGWIN
1402 if (!strcmp (current_dir, "//"))
1405 menu_item = gtk_menu_item_new_with_label (current_dir);
1407 callback_arg = g_new (HistoryCallbackArg, 1);
1408 callback_arg->menu_item = menu_item;
1410 /* since the autocompletion gets confused if you don't
1411 * supply a trailing '/' on a dir entry, set the full
1412 * (current) path to "" which just refreshes the filesel */
1415 callback_arg->directory = g_strdup ("");
1419 callback_arg->directory = g_strdup (current_dir);
1422 fs->history_list = g_list_append (fs->history_list, callback_arg);
1424 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1425 (GtkSignalFunc) gtk_file_selection_history_callback,
1427 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1428 gtk_widget_show (menu_item);
1432 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1434 g_free (current_dir);
1438 gtk_file_selection_file_button (GtkWidget *widget,
1441 GdkEventButton *bevent,
1444 GtkFileSelection *fs = NULL;
1445 gchar *filename, *temp = NULL;
1447 g_return_if_fail (GTK_IS_CLIST (widget));
1450 g_return_if_fail (fs != NULL);
1451 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1453 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1454 filename = g_strdup (temp);
1456 #ifdef G_WITH_CYGWIN
1457 /* Check to see if the selection was a drive selector */
1458 if (isalpha (filename[0]) && (filename[1] == ':')) {
1459 /* It is... map it to a CYGWIN32 drive */
1460 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1462 filename = temp_filename;
1464 #endif /* G_WITH_CYGWIN */
1469 switch (bevent->type)
1471 case GDK_2BUTTON_PRESS:
1472 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1476 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1480 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1487 gtk_file_selection_dir_button (GtkWidget *widget,
1490 GdkEventButton *bevent,
1493 GtkFileSelection *fs = NULL;
1494 gchar *filename, *temp = NULL;
1496 g_return_if_fail (GTK_IS_CLIST (widget));
1498 fs = GTK_FILE_SELECTION (user_data);
1499 g_return_if_fail (fs != NULL);
1500 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1502 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1503 filename = g_strdup (temp);
1508 switch (bevent->type)
1510 case GDK_2BUTTON_PRESS:
1511 gtk_file_selection_populate (fs, filename, FALSE);
1515 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1519 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1525 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1528 win32_gtk_add_drives_to_dir_list (GtkWidget *the_dir_list)
1530 gchar *text[2], *textPtr;
1532 char volumeNameBuf[128];
1533 char formatBuffer[128];
1537 /* Get the Drives string */
1538 GetLogicalDriveStrings (sizeof (buffer), buffer);
1540 /* Add the drives as necessary */
1542 while (*textPtr != '\0') {
1543 /* Get the volume information for this drive */
1544 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1546 /* Ignore floppies (?) */
1547 DWORD maxComponentLength, flags;
1549 GetVolumeInformation (textPtr,
1550 volumeNameBuf, sizeof(volumeNameBuf),
1551 NULL, &maxComponentLength,
1553 /* Build the actual displayable string */
1555 sprintf (formatBuffer, "%c:\\", toupper(textPtr[0]));
1556 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1557 if (strlen (volumeNameBuf) > 0)
1558 sprintf (formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1560 /* Add to the list */
1561 text[0] = formatBuffer;
1562 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1564 textPtr += (strlen (textPtr) + 1);
1570 gtk_file_selection_populate (GtkFileSelection *fs,
1574 CompletionState *cmpl_state;
1575 PossibleCompletion* poss;
1577 gchar* rem_path = rel_path;
1580 gint did_recurse = FALSE;
1581 gint possible_count = 0;
1582 gint selection_index = -1;
1584 g_return_if_fail (fs != NULL);
1585 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1587 cmpl_state = (CompletionState*) fs->cmpl_state;
1588 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1590 if (!cmpl_state_okay (cmpl_state))
1592 /* Something went wrong. */
1593 gtk_file_selection_abort (fs);
1597 g_assert (cmpl_state->reference_dir);
1599 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1600 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1601 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1602 gtk_clist_clear (GTK_CLIST (fs->file_list));
1604 /* Set the dir_list to include ./ and ../ */
1606 text[0] = "." G_DIR_SEPARATOR_S;
1607 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1609 text[0] = ".." G_DIR_SEPARATOR_S;
1610 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1614 if (cmpl_is_a_completion (poss))
1616 possible_count += 1;
1618 filename = cmpl_this_completion (poss);
1622 if (cmpl_is_directory (poss))
1624 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1625 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1626 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1630 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1634 poss = cmpl_next_completion (cmpl_state);
1637 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1638 /* For Windows, add drives as potential selections */
1639 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1642 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1643 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1645 /* File lists are set. */
1647 g_assert (cmpl_state->reference_dir);
1652 /* User is trying to complete filenames, so advance the user's input
1653 * string to the updated_text, which is the common leading substring
1654 * of all possible completions, and if its a directory attempt
1655 * attempt completions in it. */
1657 if (cmpl_updated_text (cmpl_state)[0])
1660 if (cmpl_updated_dir (cmpl_state))
1662 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1666 gtk_file_selection_populate (fs, dir_name, TRUE);
1672 if (fs->selection_entry)
1673 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1674 cmpl_updated_text (cmpl_state));
1679 selection_index = cmpl_last_valid_char (cmpl_state) -
1680 (strlen (rel_path) - strlen (rem_path));
1681 if (fs->selection_entry)
1682 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1687 if (fs->selection_entry)
1688 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1693 if (fs->selection_entry)
1694 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1696 if (fs->selection_entry)
1698 sel_text = g_strconcat (_("Selection: "),
1699 cmpl_reference_position (cmpl_state),
1702 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1706 if (fs->history_pulldown)
1708 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1715 gtk_file_selection_abort (GtkFileSelection *fs)
1719 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1721 /* BEEP gdk_beep(); */
1723 if (fs->selection_entry)
1724 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1727 /**********************************************************************/
1728 /* External Interface */
1729 /**********************************************************************/
1731 /* The four completion state selectors
1734 cmpl_updated_text (CompletionState *cmpl_state)
1736 return cmpl_state->updated_text;
1740 cmpl_updated_dir (CompletionState *cmpl_state)
1742 return cmpl_state->re_complete;
1746 cmpl_reference_position (CompletionState *cmpl_state)
1748 return cmpl_state->reference_dir->fullname;
1752 cmpl_last_valid_char (CompletionState *cmpl_state)
1754 return cmpl_state->last_valid_char;
1758 cmpl_completion_fullname (gchar *text,
1759 CompletionState *cmpl_state)
1761 static char nothing[2] = "";
1763 if (!cmpl_state_okay (cmpl_state))
1767 else if (g_path_is_absolute (text))
1769 strcpy (cmpl_state->updated_text, text);
1772 else if (text[0] == '~')
1777 dir = open_user_dir (text, cmpl_state);
1781 /* spencer says just return ~something, so
1782 * for now just do it. */
1783 strcpy (cmpl_state->updated_text, text);
1788 strcpy (cmpl_state->updated_text, dir->fullname);
1790 slash = strchr (text, G_DIR_SEPARATOR);
1793 strcat (cmpl_state->updated_text, slash);
1799 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1800 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1801 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1802 strcat (cmpl_state->updated_text, text);
1805 return cmpl_state->updated_text;
1808 /* The three completion selectors
1811 cmpl_this_completion (PossibleCompletion* pc)
1817 cmpl_is_directory (PossibleCompletion* pc)
1819 return pc->is_directory;
1823 cmpl_is_a_completion (PossibleCompletion* pc)
1825 return pc->is_a_completion;
1828 /**********************************************************************/
1829 /* Construction, deletion */
1830 /**********************************************************************/
1832 static CompletionState*
1833 cmpl_init_state (void)
1835 gchar *sys_getcwd_buf;
1837 CompletionState *new_state;
1839 new_state = g_new (CompletionState, 1);
1841 /* g_get_current_dir() returns a string in the "system" charset */
1842 sys_getcwd_buf = g_get_current_dir ();
1843 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
1844 g_free (sys_getcwd_buf);
1848 new_state->reference_dir = NULL;
1849 new_state->completion_dir = NULL;
1850 new_state->active_completion_dir = NULL;
1851 new_state->directory_storage = NULL;
1852 new_state->directory_sent_storage = NULL;
1853 new_state->last_valid_char = 0;
1854 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1855 new_state->updated_text_alloc = MAXPATHLEN;
1856 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1857 new_state->the_completion.text_alloc = MAXPATHLEN;
1858 new_state->user_dir_name_buffer = NULL;
1859 new_state->user_directories = NULL;
1861 new_state->reference_dir = open_dir (utf8_cwd, new_state);
1863 if (!new_state->reference_dir)
1865 /* Directories changing from underneath us, grumble */
1866 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
1875 cmpl_free_dir_list (GList* dp0)
1881 free_dir (dp->data);
1889 cmpl_free_dir_sent_list (GList* dp0)
1895 free_dir_sent (dp->data);
1903 cmpl_free_state (CompletionState* cmpl_state)
1905 g_return_if_fail (cmpl_state != NULL);
1907 cmpl_free_dir_list (cmpl_state->directory_storage);
1908 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1910 if (cmpl_state->user_dir_name_buffer)
1911 g_free (cmpl_state->user_dir_name_buffer);
1912 if (cmpl_state->user_directories)
1913 g_free (cmpl_state->user_directories);
1914 if (cmpl_state->the_completion.text)
1915 g_free (cmpl_state->the_completion.text);
1916 if (cmpl_state->updated_text)
1917 g_free (cmpl_state->updated_text);
1919 g_free (cmpl_state);
1923 free_dir (CompletionDir* dir)
1925 g_free (dir->fullname);
1930 free_dir_sent (CompletionDirSent* sent)
1933 for (i = 0; i < sent->entry_count; i++)
1934 g_free (sent->entries[i].entry_name);
1935 g_free (sent->entries);
1940 prune_memory_usage (CompletionState *cmpl_state)
1942 GList* cdsl = cmpl_state->directory_sent_storage;
1943 GList* cdl = cmpl_state->directory_storage;
1947 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1952 cmpl_free_dir_sent_list (cdsl->next);
1956 cmpl_state->directory_storage = NULL;
1959 if (cdl->data == cmpl_state->reference_dir)
1960 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
1962 free_dir (cdl->data);
1969 /**********************************************************************/
1970 /* The main entrances. */
1971 /**********************************************************************/
1973 static PossibleCompletion*
1974 cmpl_completion_matches (gchar *text_to_complete,
1975 gchar **remaining_text,
1976 CompletionState *cmpl_state)
1979 PossibleCompletion *poss;
1981 prune_memory_usage (cmpl_state);
1983 g_assert (text_to_complete != NULL);
1985 cmpl_state->user_completion_index = -1;
1986 cmpl_state->last_completion_text = text_to_complete;
1987 cmpl_state->the_completion.text[0] = 0;
1988 cmpl_state->last_valid_char = 0;
1989 cmpl_state->updated_text_len = -1;
1990 cmpl_state->updated_text[0] = 0;
1991 cmpl_state->re_complete = FALSE;
1994 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
1996 if (text_to_complete[0] == '~' && !first_slash)
1998 /* Text starts with ~ and there is no slash, show all the
1999 * home directory completions.
2001 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2003 update_cmpl (poss, cmpl_state);
2008 cmpl_state->reference_dir =
2009 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2011 if (!cmpl_state->reference_dir)
2014 cmpl_state->completion_dir =
2015 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2017 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2019 if (!cmpl_state->completion_dir)
2022 cmpl_state->completion_dir->cmpl_index = -1;
2023 cmpl_state->completion_dir->cmpl_parent = NULL;
2024 cmpl_state->completion_dir->cmpl_text = *remaining_text;
2026 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2028 cmpl_state->reference_dir = cmpl_state->completion_dir;
2030 poss = attempt_file_completion (cmpl_state);
2032 update_cmpl (poss, cmpl_state);
2037 static PossibleCompletion*
2038 cmpl_next_completion (CompletionState* cmpl_state)
2040 PossibleCompletion* poss = NULL;
2042 cmpl_state->the_completion.text[0] = 0;
2045 if (cmpl_state->user_completion_index >= 0)
2046 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2048 poss = attempt_file_completion (cmpl_state);
2050 poss = attempt_file_completion (cmpl_state);
2053 update_cmpl (poss, cmpl_state);
2058 /**********************************************************************/
2059 /* Directory Operations */
2060 /**********************************************************************/
2062 /* Open the directory where completion will begin from, if possible. */
2063 static CompletionDir*
2064 open_ref_dir (gchar *text_to_complete,
2065 gchar **remaining_text,
2066 CompletionState *cmpl_state)
2069 CompletionDir *new_dir;
2071 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2073 #ifdef G_WITH_CYGWIN
2074 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2077 sprintf (root_dir, "//%c", text_to_complete[2]);
2079 new_dir = open_dir (root_dir, cmpl_state);
2082 *remaining_text = text_to_complete + 4;
2090 else if (text_to_complete[0] == '~')
2092 new_dir = open_user_dir (text_to_complete, cmpl_state);
2097 *remaining_text = first_slash + 1;
2099 *remaining_text = text_to_complete + strlen (text_to_complete);
2107 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2109 gchar *tmp = g_strdup (text_to_complete);
2113 while (*p && *p != '*' && *p != '?')
2117 p = strrchr (tmp, G_DIR_SEPARATOR);
2125 new_dir = open_dir (tmp, cmpl_state);
2128 *remaining_text = text_to_complete +
2129 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2133 /* If no possible candidates, use the cwd */
2134 gchar *sys_curdir = g_get_current_dir ();
2135 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2137 g_free (sys_curdir);
2139 new_dir = open_dir (utf8_curdir, cmpl_state);
2142 *remaining_text = text_to_complete;
2144 g_free (utf8_curdir);
2151 *remaining_text = text_to_complete;
2153 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2158 new_dir->cmpl_index = -1;
2159 new_dir->cmpl_parent = NULL;
2167 /* open a directory by user name */
2168 static CompletionDir*
2169 open_user_dir (gchar *text_to_complete,
2170 CompletionState *cmpl_state)
2172 CompletionDir *result;
2176 g_assert (text_to_complete && text_to_complete[0] == '~');
2178 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2181 cmp_len = first_slash - text_to_complete - 1;
2183 cmp_len = strlen (text_to_complete + 1);
2188 gchar *homedir = g_get_home_dir ();
2189 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2194 result = open_dir (utf8_homedir, cmpl_state);
2198 g_free (utf8_homedir);
2203 gchar* copy = g_new (char, cmp_len + 1);
2207 strncpy (copy, text_to_complete + 1, cmp_len);
2209 pwd = getpwnam (copy);
2216 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2217 result = open_dir (utf8_dir, cmpl_state);
2225 /* open a directory relative the the current relative directory */
2226 static CompletionDir*
2227 open_relative_dir (gchar *dir_name,
2229 CompletionState *cmpl_state)
2231 CompletionDir *result;
2234 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2235 g_string_assign (path, dir->fullname);
2237 if (dir->fullname_len > 1
2238 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2239 g_string_append_c (path, G_DIR_SEPARATOR);
2240 g_string_append (path, dir_name);
2242 result = open_dir (path->str, cmpl_state);
2244 g_string_free (path, TRUE);
2249 /* after the cache lookup fails, really open a new directory */
2250 static CompletionDirSent*
2251 open_new_dir (gchar *dir_name,
2253 gboolean stat_subdirs)
2255 CompletionDirSent *sent;
2257 struct dirent *dirent_ptr;
2258 gint entry_count = 0;
2261 struct stat ent_sbuf;
2263 gchar *sys_dir_name;
2265 sent = g_new (CompletionDirSent, 1);
2266 sent->mtime = sbuf->st_mtime;
2267 sent->inode = sbuf->st_ino;
2268 sent->device = sbuf->st_dev;
2270 path = g_string_sized_new (2*MAXPATHLEN + 10);
2272 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2275 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2279 directory = opendir (sys_dir_name);
2283 g_free (sys_dir_name);
2287 while ((dirent_ptr = readdir (directory)) != NULL)
2290 sent->entries = g_new (CompletionDirEntry, entry_count);
2291 sent->entry_count = entry_count;
2293 rewinddir (directory);
2295 for (i = 0; i < entry_count; i += 1)
2297 dirent_ptr = readdir (directory);
2302 closedir (directory);
2303 g_free (sys_dir_name);
2307 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent_ptr->d_name, -1, NULL, NULL, NULL);
2308 if (!g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2310 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent_ptr->d_name);
2315 g_string_assign (path, sys_dir_name);
2316 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2318 g_string_append_c (path, G_DIR_SEPARATOR);
2320 g_string_append (path, dirent_ptr->d_name);
2324 /* Here we know path->str is a "system charset" string */
2325 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2326 sent->entries[n_entries].is_dir = TRUE;
2328 /* stat may fail, and we don't mind, since it could be a
2329 * dangling symlink. */
2330 sent->entries[n_entries].is_dir = FALSE;
2333 sent->entries[n_entries].is_dir = 1;
2335 sent->entry_count = n_entries;
2337 g_free (sys_dir_name);
2338 g_string_free (path, TRUE);
2339 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2341 closedir (directory);
2346 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2349 check_dir (gchar *dir_name,
2350 struct stat *result,
2351 gboolean *stat_subdirs)
2353 /* A list of directories that we know only contain other directories.
2354 * Trying to stat every file in these directories would be very
2361 struct stat statbuf;
2362 } no_stat_dirs[] = {
2363 { "/afs", FALSE, { 0 } },
2364 { "/net", FALSE, { 0 } }
2367 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
2368 static gboolean initialized = FALSE;
2369 gchar *sys_dir_name;
2375 for (i = 0; i < n_no_stat_dirs; i++)
2377 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2378 no_stat_dirs[i].present = TRUE;
2382 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2385 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2389 if (stat (sys_dir_name, result) < 0)
2391 g_free (sys_dir_name);
2395 g_free (sys_dir_name);
2397 *stat_subdirs = TRUE;
2398 for (i = 0; i < n_no_stat_dirs; i++)
2400 if (no_stat_dirs[i].present &&
2401 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2402 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2404 *stat_subdirs = FALSE;
2414 /* open a directory by absolute pathname */
2415 static CompletionDir*
2416 open_dir (gchar *dir_name,
2417 CompletionState *cmpl_state)
2420 gboolean stat_subdirs;
2421 CompletionDirSent *sent;
2424 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2425 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2428 cdsl = cmpl_state->directory_sent_storage;
2434 if (sent->inode == sbuf.st_ino &&
2435 sent->mtime == sbuf.st_mtime &&
2436 sent->device == sbuf.st_dev)
2437 return attach_dir (sent, dir_name, cmpl_state);
2442 stat_subdirs = TRUE;
2445 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
2449 cmpl_state->directory_sent_storage =
2450 g_list_prepend (cmpl_state->directory_sent_storage, sent);
2452 return attach_dir (sent, dir_name, cmpl_state);
2458 static CompletionDir*
2459 attach_dir (CompletionDirSent *sent,
2461 CompletionState *cmpl_state)
2463 CompletionDir* new_dir;
2465 new_dir = g_new (CompletionDir, 1);
2467 cmpl_state->directory_storage =
2468 g_list_prepend (cmpl_state->directory_storage, new_dir);
2470 new_dir->sent = sent;
2471 new_dir->fullname = g_strdup (dir_name);
2472 new_dir->fullname_len = strlen (dir_name);
2478 correct_dir_fullname (CompletionDir* cmpl_dir)
2480 gint length = strlen (cmpl_dir->fullname);
2481 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2482 gchar *sys_filename;
2485 /* Does it end with /. (\.) ? */
2487 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2489 /* Is it just the root directory (on a drive) ? */
2490 if (cmpl_dir->fullname + length - 2 == first_slash)
2492 cmpl_dir->fullname[length - 1] = 0;
2493 cmpl_dir->fullname_len = length - 1;
2498 cmpl_dir->fullname[length - 2] = 0;
2502 /* Ends with /./ (\.\)? */
2503 else if (length >= 3 &&
2504 strcmp (cmpl_dir->fullname + length - 3,
2505 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2506 cmpl_dir->fullname[length - 2] = 0;
2508 /* Ends with /.. (\..) ? */
2509 else if (length >= 3 &&
2510 strcmp (cmpl_dir->fullname + length - 3,
2511 G_DIR_SEPARATOR_S "..") == 0)
2513 /* Is it just /.. (X:\..)? */
2514 if (cmpl_dir->fullname + length - 3 == first_slash)
2516 cmpl_dir->fullname[length - 2] = 0;
2517 cmpl_dir->fullname_len = length - 2;
2521 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2524 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2528 if (stat (sys_filename, &sbuf) < 0)
2530 g_free (sys_filename);
2534 g_free (sys_filename);
2536 cmpl_dir->fullname[length - 3] = 0;
2538 if (!correct_parent (cmpl_dir, &sbuf))
2542 /* Ends with /../ (\..\)? */
2543 else if (length >= 4 &&
2544 strcmp (cmpl_dir->fullname + length - 4,
2545 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2547 /* Is it just /../ (X:\..\)? */
2548 if (cmpl_dir->fullname + length - 4 == first_slash)
2550 cmpl_dir->fullname[length - 3] = 0;
2551 cmpl_dir->fullname_len = length - 3;
2555 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2558 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2562 if (stat (sys_filename, &sbuf) < 0)
2564 g_free (sys_filename);
2568 g_free (sys_filename);
2570 cmpl_dir->fullname[length - 4] = 0;
2572 if (!correct_parent (cmpl_dir, &sbuf))
2576 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
2582 correct_parent (CompletionDir *cmpl_dir,
2589 gchar *sys_filename;
2592 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2593 g_assert (last_slash);
2594 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
2596 /* Clever (?) way to check for top-level directory that works also on
2597 * Win32, where there is a drive letter and colon prefixed...
2599 if (last_slash != first_slash)
2609 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
2612 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2614 last_slash[0] = G_DIR_SEPARATOR;
2618 if (stat (sys_filename, &parbuf) < 0)
2620 g_free (sys_filename);
2623 last_slash[0] = G_DIR_SEPARATOR;
2626 g_free (sys_filename);
2628 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2629 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2630 /* it wasn't a link */
2636 last_slash[0] = G_DIR_SEPARATOR;
2638 /* it was a link, have to figure it out the hard way */
2640 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
2645 g_free (cmpl_dir->fullname);
2647 cmpl_dir->fullname = new_name;
2656 find_parent_dir_fullname (gchar* dirname)
2658 gchar *sys_orig_dir;
2663 sys_orig_dir = g_get_current_dir ();
2664 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
2667 g_free (sys_orig_dir);
2668 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2672 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
2674 g_free (sys_dirname);
2675 g_free (sys_orig_dir);
2679 g_free (sys_dirname);
2681 sys_cwd = g_get_current_dir ();
2682 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
2685 if (chdir (sys_orig_dir) != 0)
2688 g_free (sys_orig_dir);
2692 g_free (sys_orig_dir);
2698 /**********************************************************************/
2699 /* Completion Operations */
2700 /**********************************************************************/
2704 static PossibleCompletion*
2705 attempt_homedir_completion (gchar *text_to_complete,
2706 CompletionState *cmpl_state)
2710 if (!cmpl_state->user_dir_name_buffer &&
2711 !get_pwdb (cmpl_state))
2713 length = strlen (text_to_complete) - 1;
2715 cmpl_state->user_completion_index += 1;
2717 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2719 index = first_diff_index (text_to_complete + 1,
2720 cmpl_state->user_directories
2721 [cmpl_state->user_completion_index].login);
2728 if (cmpl_state->last_valid_char < (index + 1))
2729 cmpl_state->last_valid_char = index + 1;
2730 cmpl_state->user_completion_index += 1;
2734 cmpl_state->the_completion.is_a_completion = 1;
2735 cmpl_state->the_completion.is_directory = TRUE;
2737 append_completion_text ("~", cmpl_state);
2739 append_completion_text (cmpl_state->
2740 user_directories[cmpl_state->user_completion_index].login,
2743 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
2746 if (text_to_complete[1]
2747 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2749 cmpl_state->user_completion_index = -1;
2754 cmpl_state->user_completion_index += 1;
2755 cmpl_state->the_completion.is_a_completion = 1;
2756 cmpl_state->the_completion.is_directory = TRUE;
2758 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
2764 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2765 #define FOLD(c) (tolower(c))
2770 /* returns the index (>= 0) of the first differing character,
2771 * PATTERN_MATCH if the completion matches */
2773 first_diff_index (gchar *pat,
2778 while (*pat && *text && FOLD (*text) == FOLD (*pat))
2788 return PATTERN_MATCH;
2791 static PossibleCompletion*
2792 append_completion_text (gchar *text,
2793 CompletionState *cmpl_state)
2797 if (!cmpl_state->the_completion.text)
2800 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
2802 if (cmpl_state->the_completion.text_alloc > len)
2804 strcat (cmpl_state->the_completion.text, text);
2805 return &cmpl_state->the_completion;
2811 cmpl_state->the_completion.text_alloc = i;
2813 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
2815 if (!cmpl_state->the_completion.text)
2819 strcat (cmpl_state->the_completion.text, text);
2820 return &cmpl_state->the_completion;
2824 static CompletionDir*
2825 find_completion_dir (gchar *text_to_complete,
2826 gchar **remaining_text,
2827 CompletionState *cmpl_state)
2829 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2830 CompletionDir* dir = cmpl_state->reference_dir;
2831 CompletionDir* next;
2832 *remaining_text = text_to_complete;
2836 gint len = first_slash - *remaining_text;
2838 gchar *found_name = NULL; /* Quiet gcc */
2840 gchar* pat_buf = g_new (gchar, len + 1);
2842 strncpy (pat_buf, *remaining_text, len);
2845 for (i = 0; i < dir->sent->entry_count; i += 1)
2847 if (dir->sent->entries[i].is_dir &&
2848 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
2849 FNMATCH_FLAGS)!= FNM_NOMATCH)
2859 found_name = dir->sent->entries[i].entry_name;
2866 /* Perhaps we are trying to open an automount directory */
2867 found_name = pat_buf;
2870 next = open_relative_dir (found_name, dir, cmpl_state);
2878 next->cmpl_parent = dir;
2882 if (!correct_dir_fullname (dir))
2888 *remaining_text = first_slash + 1;
2889 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
2898 update_cmpl (PossibleCompletion *poss,
2899 CompletionState *cmpl_state)
2903 if (!poss || !cmpl_is_a_completion (poss))
2906 cmpl_len = strlen (cmpl_this_completion (poss));
2908 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
2910 cmpl_state->updated_text =
2911 (gchar*)g_realloc (cmpl_state->updated_text,
2912 cmpl_state->updated_text_alloc);
2913 cmpl_state->updated_text_alloc = 2*cmpl_len;
2916 if (cmpl_state->updated_text_len < 0)
2918 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
2919 cmpl_state->updated_text_len = cmpl_len;
2920 cmpl_state->re_complete = cmpl_is_directory (poss);
2922 else if (cmpl_state->updated_text_len == 0)
2924 cmpl_state->re_complete = FALSE;
2929 first_diff_index (cmpl_state->updated_text,
2930 cmpl_this_completion (poss));
2932 cmpl_state->re_complete = FALSE;
2934 if (first_diff == PATTERN_MATCH)
2937 if (first_diff > cmpl_state->updated_text_len)
2938 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
2940 cmpl_state->updated_text_len = first_diff;
2941 cmpl_state->updated_text[first_diff] = 0;
2945 static PossibleCompletion*
2946 attempt_file_completion (CompletionState *cmpl_state)
2948 gchar *pat_buf, *first_slash;
2949 CompletionDir *dir = cmpl_state->active_completion_dir;
2951 dir->cmpl_index += 1;
2953 if (dir->cmpl_index == dir->sent->entry_count)
2955 if (dir->cmpl_parent == NULL)
2957 cmpl_state->active_completion_dir = NULL;
2963 cmpl_state->active_completion_dir = dir->cmpl_parent;
2965 return attempt_file_completion (cmpl_state);
2969 g_assert (dir->cmpl_text);
2971 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
2975 gint len = first_slash - dir->cmpl_text;
2977 pat_buf = g_new (gchar, len + 1);
2978 strncpy (pat_buf, dir->cmpl_text, len);
2983 gint len = strlen (dir->cmpl_text);
2985 pat_buf = g_new (gchar, len + 2);
2986 strcpy (pat_buf, dir->cmpl_text);
2987 /* Don't append a * if the user entered one herself.
2988 * This way one can complete *.h and don't get matches
2989 * on any .help files, for instance.
2991 if (strchr (pat_buf, '*') == NULL)
2992 strcpy (pat_buf + len, "*");
2997 if (dir->sent->entries[dir->cmpl_index].is_dir)
2999 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3000 FNMATCH_FLAGS) != FNM_NOMATCH)
3002 CompletionDir* new_dir;
3004 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3013 new_dir->cmpl_parent = dir;
3015 new_dir->cmpl_index = -1;
3016 new_dir->cmpl_text = first_slash + 1;
3018 cmpl_state->active_completion_dir = new_dir;
3021 return attempt_file_completion (cmpl_state);
3026 return attempt_file_completion (cmpl_state);
3032 return attempt_file_completion (cmpl_state);
3037 if (dir->cmpl_parent != NULL)
3039 append_completion_text (dir->fullname +
3040 strlen (cmpl_state->completion_dir->fullname) + 1,
3042 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3045 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3047 cmpl_state->the_completion.is_a_completion =
3048 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3049 FNMATCH_FLAGS) != FNM_NOMATCH;
3051 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3052 if (dir->sent->entries[dir->cmpl_index].is_dir)
3053 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3056 return &cmpl_state->the_completion;
3063 get_pwdb (CompletionState* cmpl_state)
3065 struct passwd *pwd_ptr;
3068 gint len = 0, i, count = 0;
3070 if (cmpl_state->user_dir_name_buffer)
3074 while ((pwd_ptr = getpwent ()) != NULL)
3076 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3077 len += strlen (utf8);
3079 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3080 len += strlen (utf8);
3088 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3089 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3090 cmpl_state->user_directories_len = count;
3092 buf_ptr = cmpl_state->user_dir_name_buffer;
3094 for (i = 0; i < count; i += 1)
3096 pwd_ptr = getpwent ();
3103 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3104 strcpy (buf_ptr, utf8);
3107 cmpl_state->user_directories[i].login = buf_ptr;
3109 buf_ptr += strlen (buf_ptr);
3112 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3113 strcpy (buf_ptr, utf8);
3116 cmpl_state->user_directories[i].homedir = buf_ptr;
3118 buf_ptr += strlen (buf_ptr);
3122 qsort (cmpl_state->user_directories,
3123 cmpl_state->user_directories_len,
3124 sizeof (CompletionUserDir),
3133 if (cmpl_state->user_dir_name_buffer)
3134 g_free (cmpl_state->user_dir_name_buffer);
3135 if (cmpl_state->user_directories)
3136 g_free (cmpl_state->user_directories);
3138 cmpl_state->user_dir_name_buffer = NULL;
3139 cmpl_state->user_directories = NULL;
3145 compare_user_dir (const void *a,
3148 return strcmp ((((CompletionUserDir*)a))->login,
3149 (((CompletionUserDir*)b))->login);
3155 compare_cmpl_dir (const void *a,
3158 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3159 return strcmp ((((CompletionDirEntry*)a))->entry_name,
3160 (((CompletionDirEntry*)b))->entry_name);
3162 return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
3163 (((CompletionDirEntry*)b))->entry_name);
3168 cmpl_state_okay (CompletionState* cmpl_state)
3170 return cmpl_state && cmpl_state->reference_dir;
3174 cmpl_strerror (gint err)
3176 if (err == CMPL_ERRNO_TOO_LONG)
3177 return _("Name too long");
3178 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3179 return _("Couldn't convert filename");
3181 return g_strerror (err);