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>
45 #include <glib.h> /* Include early to get G_OS_WIN32 and
48 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
53 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
55 #include <winsock.h> /* For gethostname */
60 #include "gdk/gdkkeysyms.h"
61 #include "gtkbutton.h"
62 #include "gtkcellrenderertext.h"
64 #include "gtkfilesel.h"
68 #include "gtkliststore.h"
70 #include "gtkscrolledwindow.h"
72 #include "gtksignal.h"
73 #include "gtktreeselection.h"
74 #include "gtktreeview.h"
77 #include "gtkmenuitem.h"
78 #include "gtkoptionmenu.h"
79 #include "gtkdialog.h"
80 #include "gtkmessagedialog.h"
83 #include "gtkeventbox.h"
85 #define WANT_HPANAED 1
86 #include "gtkhpaned.h"
91 #define mkdir(p,m) _mkdir(p)
93 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
95 #endif /* G_OS_WIN32 */
97 #define DIR_LIST_WIDTH 180
98 #define DIR_LIST_HEIGHT 180
99 #define FILE_LIST_WIDTH 180
100 #define FILE_LIST_HEIGHT 180
102 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
103 * in here, since the rest of the code in the file does require some
108 # define MAXPATHLEN PATH_MAX
110 # define MAXPATHLEN 2048
114 /* I've put this here so it doesn't get confused with the
115 * file completion interface */
116 typedef struct _HistoryCallbackArg HistoryCallbackArg;
118 struct _HistoryCallbackArg
121 GtkWidget *menu_item;
125 typedef struct _CompletionState CompletionState;
126 typedef struct _CompletionDir CompletionDir;
127 typedef struct _CompletionDirSent CompletionDirSent;
128 typedef struct _CompletionDirEntry CompletionDirEntry;
129 typedef struct _CompletionUserDir CompletionUserDir;
130 typedef struct _PossibleCompletion PossibleCompletion;
132 /* Non-external file completion decls and structures */
134 /* A contant telling PRCS how many directories to cache. Its actually
135 * kept in a list, so the geometry isn't important. */
136 #define CMPL_DIRECTORY_CACHE_SIZE 10
138 /* A constant used to determine whether a substring was an exact
139 * match by first_diff_index()
141 #define PATTERN_MATCH -1
142 /* The arguments used by all fnmatch() calls below
144 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
146 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
147 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
149 /* This structure contains all the useful information about a directory
150 * for the purposes of filename completion. These structures are cached
151 * in the CompletionState struct. CompletionDir's are reference counted.
153 struct _CompletionDirSent
160 struct _CompletionDirEntry *entries;
163 struct _CompletionDir
165 CompletionDirSent *sent;
170 struct _CompletionDir *cmpl_parent;
175 /* This structure contains pairs of directory entry names with a flag saying
176 * whether or not they are a valid directory. NOTE: This information is used
177 * to provide the caller with information about whether to update its completions
178 * or try to open a file. Since directories are cached by the directory mtime,
179 * a symlink which points to an invalid file (which will not be a directory),
180 * will not be reevaluated if that file is created, unless the containing
181 * directory is touched. I consider this case to be worth ignoring (josh).
183 struct _CompletionDirEntry
189 struct _CompletionUserDir
195 struct _PossibleCompletion
197 /* accessible fields, all are accessed externally by functions
201 gint is_a_completion;
202 gboolean is_directory;
209 struct _CompletionState
211 gint last_valid_char;
213 gint updated_text_len;
214 gint updated_text_alloc;
215 gboolean re_complete;
217 gchar *user_dir_name_buffer;
218 gint user_directories_len;
220 gchar *last_completion_text;
222 gint user_completion_index; /* if >= 0, currently completing ~user */
224 struct _CompletionDir *completion_dir; /* directory completing from */
225 struct _CompletionDir *active_completion_dir;
227 struct _PossibleCompletion the_completion;
229 struct _CompletionDir *reference_dir; /* initial directory */
231 GList* directory_storage;
232 GList* directory_sent_storage;
234 struct _CompletionUserDir *user_directories;
252 /* File completion functions which would be external, were they used
253 * outside of this file.
256 static CompletionState* cmpl_init_state (void);
257 static void cmpl_free_state (CompletionState *cmpl_state);
258 static gint cmpl_state_okay (CompletionState* cmpl_state);
259 static const gchar* cmpl_strerror (gint);
261 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
262 gchar **remaining_text,
263 CompletionState *cmpl_state);
265 /* Returns a name for consideration, possibly a completion, this name
266 * will be invalid after the next call to cmpl_next_completion.
268 static char* cmpl_this_completion (PossibleCompletion*);
270 /* True if this completion matches the given text. Otherwise, this
271 * output can be used to have a list of non-completions.
273 static gint cmpl_is_a_completion (PossibleCompletion*);
275 /* True if the completion is a directory
277 static gboolean cmpl_is_directory (PossibleCompletion*);
279 /* Obtains the next completion, or NULL
281 static PossibleCompletion* cmpl_next_completion (CompletionState*);
283 /* Updating completions: the return value of cmpl_updated_text() will
284 * be text_to_complete completed as much as possible after the most
285 * recent call to cmpl_completion_matches. For the present
286 * application, this is the suggested replacement for the user's input
287 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
290 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
292 /* After updating, to see if the completion was a directory, call
293 * this. If it was, you should consider re-calling completion_matches.
295 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
297 /* Current location: if using file completion, return the current
298 * directory, from which file completion begins. More specifically,
299 * the cwd concatenated with all exact completions up to the last
300 * directory delimiter('/').
302 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
304 /* backing up: if cmpl_completion_matches returns NULL, you may query
305 * the index of the last completable character into cmpl_updated_text.
307 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
309 /* When the user selects a non-directory, call cmpl_completion_fullname
310 * to get the full name of the selected file.
312 static gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
315 /* Directory operations. */
316 static CompletionDir* open_ref_dir (gchar* text_to_complete,
317 gchar** remaining_text,
318 CompletionState* cmpl_state);
319 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
320 static gboolean check_dir (gchar *dir_name,
322 gboolean *stat_subdirs);
324 static CompletionDir* open_dir (gchar* dir_name,
325 CompletionState* cmpl_state);
327 static CompletionDir* open_user_dir (const gchar* text_to_complete,
328 CompletionState *cmpl_state);
330 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
331 CompletionState *cmpl_state);
332 static CompletionDirSent* open_new_dir (gchar* dir_name,
334 gboolean stat_subdirs);
335 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
336 static gint correct_parent (CompletionDir* cmpl_dir,
339 static gchar* find_parent_dir_fullname (gchar* dirname);
341 static CompletionDir* attach_dir (CompletionDirSent* sent,
343 CompletionState *cmpl_state);
344 static void free_dir_sent (CompletionDirSent* sent);
345 static void free_dir (CompletionDir *dir);
346 static void prune_memory_usage(CompletionState *cmpl_state);
348 /* Completion operations */
350 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
351 CompletionState *cmpl_state);
353 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
354 static CompletionDir* find_completion_dir(gchar* text_to_complete,
355 gchar** remaining_text,
356 CompletionState* cmpl_state);
357 static PossibleCompletion* append_completion_text(gchar* text,
358 CompletionState* cmpl_state);
360 static gint get_pwdb(CompletionState* cmpl_state);
361 static gint compare_user_dir(const void* a, const void* b);
363 static gint first_diff_index(gchar* pat, gchar* text);
364 static gint compare_cmpl_dir(const void* a, const void* b);
365 static void update_cmpl(PossibleCompletion* poss,
366 CompletionState* cmpl_state);
368 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
369 static void gtk_file_selection_set_property (GObject *object,
373 static void gtk_file_selection_get_property (GObject *object,
377 static void gtk_file_selection_init (GtkFileSelection *filesel);
378 static void gtk_file_selection_finalize (GObject *object);
379 static void gtk_file_selection_destroy (GtkObject *object);
380 static void gtk_file_selection_map (GtkWidget *widget);
381 static gint gtk_file_selection_key_press (GtkWidget *widget,
384 static gint gtk_file_selection_insert_text (GtkWidget *widget,
385 const gchar *new_text,
386 gint new_text_length,
390 static void gtk_file_selection_file_activate (GtkTreeView *tree_view,
392 GtkTreeViewColumn *column,
394 static void gtk_file_selection_file_changed (GtkTreeSelection *selection,
396 static void gtk_file_selection_dir_activate (GtkTreeView *tree_view,
398 GtkTreeViewColumn *column,
401 static void gtk_file_selection_populate (GtkFileSelection *fs,
403 gboolean try_complete,
404 gboolean reset_entry);
405 static void gtk_file_selection_abort (GtkFileSelection *fs);
407 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
410 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
411 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
412 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
414 static void free_selected_names (GPtrArray *names);
416 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
417 #define compare_filenames(a, b) strcmp(a, b)
419 #define compare_filenames(a, b) g_ascii_strcasecmp(a, b)
423 static GtkWindowClass *parent_class = NULL;
425 /* Saves errno when something cmpl does fails. */
426 static gint cmpl_errno;
430 * Take the path currently in the file selection
431 * entry field and translate as necessary from
432 * a WIN32 style to CYGWIN32 style path. For
433 * instance translate:
434 * x:\somepath\file.jpg
436 * //x/somepath/file.jpg
438 * Replace the path in the selection text field.
439 * Return a boolean value concerning whether a
440 * translation had to be made.
443 translate_win32_path (GtkFileSelection *filesel)
449 * Retrieve the current path
451 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
454 * Translate only if this looks like a DOS-ish
455 * path... First handle any drive letters.
457 if (isalpha (path[0]) && (path[1] == ':')) {
459 * This part kind of stinks... It isn't possible
460 * to know if there is enough space in the current
461 * string for the extra character required in this
462 * conversion. Assume that there isn't enough space
463 * and use the set function on the text field to
464 * set the newly created string.
466 gchar *newPath = g_strdup_printf ("//%c/%s", path[0], (path + 3));
467 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
474 * Now, replace backslashes with forward slashes
477 if (strchr (path, '\\'))
480 for (index = 0; path[index] != '\0'; index++)
481 if (path[index] == '\\')
492 gtk_file_selection_get_type (void)
494 static GtkType file_selection_type = 0;
496 if (!file_selection_type)
498 static const GtkTypeInfo filesel_info =
501 sizeof (GtkFileSelection),
502 sizeof (GtkFileSelectionClass),
503 (GtkClassInitFunc) gtk_file_selection_class_init,
504 (GtkObjectInitFunc) gtk_file_selection_init,
505 /* reserved_1 */ NULL,
506 /* reserved_2 */ NULL,
507 (GtkClassInitFunc) NULL,
510 file_selection_type = gtk_type_unique (GTK_TYPE_DIALOG, &filesel_info);
513 return file_selection_type;
517 gtk_file_selection_class_init (GtkFileSelectionClass *class)
519 GObjectClass *gobject_class;
520 GtkObjectClass *object_class;
521 GtkWidgetClass *widget_class;
523 gobject_class = (GObjectClass*) class;
524 object_class = (GtkObjectClass*) class;
525 widget_class = (GtkWidgetClass*) class;
527 parent_class = gtk_type_class (GTK_TYPE_DIALOG);
529 gobject_class->finalize = gtk_file_selection_finalize;
530 gobject_class->set_property = gtk_file_selection_set_property;
531 gobject_class->get_property = gtk_file_selection_get_property;
533 g_object_class_install_property (gobject_class,
535 g_param_spec_string ("filename",
537 _("The currently selected filename."),
539 G_PARAM_READABLE | G_PARAM_WRITABLE));
540 g_object_class_install_property (gobject_class,
542 g_param_spec_boolean ("show_fileops",
543 _("Show file operations"),
544 _("Whether buttons for creating/manipulating files should be displayed."),
548 g_object_class_install_property (gobject_class,
549 PROP_SELECT_MULTIPLE,
550 g_param_spec_boolean ("select_multiple",
551 _("Select multiple"),
552 _("Whether to allow multiple files to be selected."),
556 object_class->destroy = gtk_file_selection_destroy;
557 widget_class->map = gtk_file_selection_map;
560 static void gtk_file_selection_set_property (GObject *object,
565 GtkFileSelection *filesel;
567 filesel = GTK_FILE_SELECTION (object);
572 gtk_file_selection_set_filename (filesel,
573 g_value_get_string (value));
575 case PROP_SHOW_FILEOPS:
576 if (g_value_get_boolean (value))
577 gtk_file_selection_show_fileop_buttons (filesel);
579 gtk_file_selection_hide_fileop_buttons (filesel);
581 case PROP_SELECT_MULTIPLE:
582 gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590 static void gtk_file_selection_get_property (GObject *object,
595 GtkFileSelection *filesel;
597 filesel = GTK_FILE_SELECTION (object);
602 g_value_set_string (value,
603 gtk_file_selection_get_filename(filesel));
606 case PROP_SHOW_FILEOPS:
607 /* This is a little bit hacky, but doing otherwise would require
608 * adding a field to the object.
610 g_value_set_boolean (value, (filesel->fileop_c_dir &&
611 filesel->fileop_del_file &&
612 filesel->fileop_ren_file));
614 case PROP_SELECT_MULTIPLE:
615 g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
618 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
624 grab_default (GtkWidget *widget)
626 gtk_widget_grab_default (widget);
631 gtk_file_selection_init (GtkFileSelection *filesel)
633 GtkWidget *entry_vbox;
635 GtkWidget *list_hbox, *list_container;
636 GtkWidget *confirm_area;
637 GtkWidget *pulldown_hbox;
638 GtkWidget *scrolled_win;
643 GtkTreeViewColumn *column;
645 gtk_widget_push_composite_child ();
647 dialog = GTK_DIALOG (filesel);
649 filesel->cmpl_state = cmpl_init_state ();
651 /* The dialog-sized vertical box */
652 filesel->main_vbox = dialog->vbox;
653 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
655 /* The horizontal box containing create, rename etc. buttons */
656 filesel->button_area = gtk_hbutton_box_new ();
657 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
658 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
659 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
661 gtk_widget_show (filesel->button_area);
663 gtk_file_selection_show_fileop_buttons (filesel);
665 /* hbox for pulldown menu */
666 pulldown_hbox = gtk_hbox_new (TRUE, 5);
667 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
668 gtk_widget_show (pulldown_hbox);
671 filesel->history_pulldown = gtk_option_menu_new ();
672 gtk_widget_show (filesel->history_pulldown);
673 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
676 /* The horizontal box containing the directory and file listboxes */
677 list_hbox = gtk_hbox_new (FALSE, 5);
678 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
679 gtk_widget_show (list_hbox);
681 list_container = g_object_new (GTK_TYPE_HPANED,
686 list_container = list_hbox;
688 /* The directories list */
690 model = gtk_list_store_new (1, G_TYPE_STRING);
691 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
692 g_object_unref (model);
694 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
695 gtk_cell_renderer_text_new (),
698 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
699 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
700 gtk_widget_show (label);
701 gtk_tree_view_column_set_widget (column, label);
702 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
703 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
705 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
706 g_signal_connect (filesel->dir_list, "row_activated",
707 G_CALLBACK (gtk_file_selection_dir_activate), filesel);
709 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
711 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
712 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
713 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
714 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
715 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
716 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
717 if (GTK_IS_PANED (list_container))
718 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
720 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
721 gtk_widget_show (filesel->dir_list);
722 gtk_widget_show (scrolled_win);
725 model = gtk_list_store_new (1, G_TYPE_STRING);
726 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
727 g_object_unref (model);
729 column = gtk_tree_view_column_new_with_attributes (_("Files"),
730 gtk_cell_renderer_text_new (),
733 label = gtk_label_new_with_mnemonic (_("_Files"));
734 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
735 gtk_widget_show (label);
736 gtk_tree_view_column_set_widget (column, label);
737 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
738 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
740 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
741 g_signal_connect (filesel->file_list, "row_activated",
742 G_CALLBACK (gtk_file_selection_file_activate), filesel);
743 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
744 G_CALLBACK (gtk_file_selection_file_changed), filesel);
746 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
748 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
749 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
750 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
751 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
752 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
753 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
754 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
755 gtk_widget_show (filesel->file_list);
756 gtk_widget_show (scrolled_win);
758 /* action area for packing buttons into. */
759 filesel->action_area = gtk_hbox_new (TRUE, 0);
760 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
762 gtk_widget_show (filesel->action_area);
764 /* The OK/Cancel button area */
765 confirm_area = dialog->action_area;
767 /* The Cancel button */
768 filesel->cancel_button = gtk_dialog_add_button (dialog,
770 GTK_RESPONSE_CANCEL);
772 filesel->ok_button = gtk_dialog_add_button (dialog,
776 gtk_widget_grab_default (filesel->ok_button);
778 /* The selection entry widget */
779 entry_vbox = gtk_vbox_new (FALSE, 2);
780 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
781 gtk_widget_show (entry_vbox);
783 eventbox = gtk_event_box_new ();
784 filesel->selection_text = label = gtk_label_new ("");
785 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
786 gtk_container_add (GTK_CONTAINER (eventbox), label);
787 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
788 gtk_widget_show (label);
789 gtk_widget_show (eventbox);
791 filesel->selection_entry = gtk_entry_new ();
792 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
793 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
794 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "insert_text",
795 (GtkSignalFunc) gtk_file_selection_insert_text, NULL);
796 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
797 (GtkSignalFunc) grab_default,
798 GTK_OBJECT (filesel->ok_button));
799 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
800 (GtkSignalFunc) gtk_button_clicked,
801 GTK_OBJECT (filesel->ok_button));
802 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
803 gtk_widget_show (filesel->selection_entry);
805 if (!cmpl_state_okay (filesel->cmpl_state))
809 sprintf (err_buf, _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
811 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
815 gtk_file_selection_populate (filesel, "", FALSE, TRUE);
818 gtk_widget_grab_focus (filesel->selection_entry);
820 gtk_widget_pop_composite_child ();
824 uri_list_extract_first_uri (const gchar* uri_list)
828 g_return_val_if_fail (uri_list != NULL, NULL);
831 /* We don't actually try to validate the URI according to RFC
832 * 2396, or even check for allowed characters - we just ignore
833 * comments and trim whitespace off the ends. We also
834 * allow LF delimination as well as the specified CRLF.
836 * We do allow comments like specified in RFC 2483.
842 while (g_ascii_isspace(*p))
846 while (*q && (*q != '\n') && (*q != '\r'))
852 while (q > p && g_ascii_isspace (*q))
856 return g_strndup (p, q - p + 1);
859 p = strchr (p, '\n');
867 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
871 if (response_id == GTK_RESPONSE_YES)
873 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
875 gtk_file_selection_set_filename (fs, filename);
878 gtk_widget_destroy (dialog);
883 filenames_dropped (GtkWidget *widget,
884 GdkDragContext *context,
887 GtkSelectionData *selection_data,
892 char *filename = NULL;
894 char this_hostname[257];
896 GError *error = NULL;
898 if (!selection_data->data)
901 uri = uri_list_extract_first_uri ((char *)selection_data->data);
906 filename = g_filename_from_uri (uri, &hostname, &error);
911 g_warning ("Error getting dropped filename: %s\n",
913 g_error_free (error);
917 res = gethostname (this_hostname, 256);
918 this_hostname[256] = 0;
920 if ((hostname == NULL) ||
921 (res == 0 && strcmp (hostname, this_hostname) == 0) ||
922 (strcmp (hostname, "localhost") == 0))
923 gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
929 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
930 GTK_DIALOG_DESTROY_WITH_PARENT,
931 GTK_MESSAGE_QUESTION,
933 _("The file \"%s\" resides on another machine (called %s) and may not be availible to this program.\n"
934 "Are you sure that you want to select it?"), filename, hostname);
936 g_object_set_data_full (G_OBJECT (dialog), "gtk-fs-dnd-filename", g_strdup (filename), g_free);
938 g_signal_connect_data (dialog, "response",
939 (GCallback) dnd_really_drop,
942 gtk_widget_show (dialog);
960 filenames_drag_get (GtkWidget *widget,
961 GdkDragContext *context,
962 GtkSelectionData *selection_data,
965 GtkFileSelection *filesel)
973 file = gtk_file_selection_get_filename (filesel);
977 if (info == TARGET_URILIST)
979 res = gethostname (hostname, 256);
982 uri_list = g_filename_to_uri (file, (!res)?hostname:NULL, &error);
985 g_warning ("Error getting filename: %s\n",
987 g_error_free (error);
991 gtk_selection_data_set (selection_data,
992 selection_data->target, 8,
993 (void *)uri_list, strlen((char *)uri_list));
998 g_print ("Setting text: '%s'\n", file);
999 gtk_selection_data_set_text (selection_data, file, -1);
1005 file_selection_setup_dnd (GtkFileSelection *filesel)
1007 GtkWidget *eventbox;
1008 static GtkTargetEntry drop_types[] = {
1009 { "text/uri-list", 0, TARGET_URILIST}
1011 static gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
1012 static GtkTargetEntry drag_types[] = {
1013 { "text/uri-list", 0, TARGET_URILIST},
1014 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
1017 { "COMPOUND_TEXT", 0, 0 }
1019 static gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
1021 gtk_drag_dest_set (GTK_WIDGET (filesel),
1022 GTK_DEST_DEFAULT_ALL,
1023 drop_types, n_drop_types,
1026 gtk_signal_connect (GTK_OBJECT(filesel), "drag_data_received",
1027 GTK_SIGNAL_FUNC(filenames_dropped), NULL);
1029 eventbox = gtk_widget_get_parent (filesel->selection_text);
1030 gtk_drag_source_set (eventbox,
1032 drag_types, n_drag_types,
1035 gtk_signal_connect (GTK_OBJECT (eventbox),
1037 GTK_SIGNAL_FUNC (filenames_drag_get),
1042 gtk_file_selection_new (const gchar *title)
1044 GtkFileSelection *filesel;
1046 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
1047 gtk_window_set_title (GTK_WINDOW (filesel), title);
1048 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1050 file_selection_setup_dnd (filesel);
1052 return GTK_WIDGET (filesel);
1056 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1058 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1060 /* delete, create directory, and rename */
1061 if (!filesel->fileop_c_dir)
1063 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1064 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
1065 (GtkSignalFunc) gtk_file_selection_create_dir,
1066 (gpointer) filesel);
1067 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1068 filesel->fileop_c_dir, TRUE, TRUE, 0);
1069 gtk_widget_show (filesel->fileop_c_dir);
1072 if (!filesel->fileop_del_file)
1074 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1075 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
1076 (GtkSignalFunc) gtk_file_selection_delete_file,
1077 (gpointer) filesel);
1078 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1079 filesel->fileop_del_file, TRUE, TRUE, 0);
1080 gtk_widget_show (filesel->fileop_del_file);
1083 if (!filesel->fileop_ren_file)
1085 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1086 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
1087 (GtkSignalFunc) gtk_file_selection_rename_file,
1088 (gpointer) filesel);
1089 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1090 filesel->fileop_ren_file, TRUE, TRUE, 0);
1091 gtk_widget_show (filesel->fileop_ren_file);
1093 g_object_notify (G_OBJECT (filesel), "show_fileops");
1094 gtk_widget_queue_resize (GTK_WIDGET (filesel));
1098 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1100 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1102 if (filesel->fileop_ren_file)
1104 gtk_widget_destroy (filesel->fileop_ren_file);
1105 filesel->fileop_ren_file = NULL;
1108 if (filesel->fileop_del_file)
1110 gtk_widget_destroy (filesel->fileop_del_file);
1111 filesel->fileop_del_file = NULL;
1114 if (filesel->fileop_c_dir)
1116 gtk_widget_destroy (filesel->fileop_c_dir);
1117 filesel->fileop_c_dir = NULL;
1119 g_object_notify (G_OBJECT (filesel), "show_fileops");
1125 gtk_file_selection_set_filename (GtkFileSelection *filesel,
1126 const gchar *filename)
1129 const char *name, *last_slash;
1131 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1132 g_return_if_fail (filename != NULL);
1134 last_slash = strrchr (filename, G_DIR_SEPARATOR);
1138 buf = g_strdup ("");
1143 buf = g_strdup (filename);
1144 buf[last_slash - filename + 1] = 0;
1145 name = last_slash + 1;
1148 gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1150 if (filesel->selection_entry)
1151 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1153 g_object_notify (G_OBJECT (filesel), "filename");
1157 * gtk_file_selection_get_filename:
1158 * @filesel: a #GtkFileSelection
1160 * This function returns the selected filename in encoding of
1161 * g_filename_from_utf8(), which may or may not be the same as that
1162 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1163 * The returned string points to a statically allocated buffer and
1164 * should be copied if you plan to keep it around.
1166 * Return value: currently-selected filename in locale's encoding
1168 G_CONST_RETURN gchar*
1169 gtk_file_selection_get_filename (GtkFileSelection *filesel)
1171 static gchar nothing[2] = "";
1172 static gchar something[MAXPATHLEN*2];
1176 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1178 #ifdef G_WITH_CYGWIN
1179 translate_win32_path (filesel);
1181 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1184 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
1187 strncpy (something, sys_filename, sizeof (something));
1188 g_free (sys_filename);
1196 gtk_file_selection_complete (GtkFileSelection *filesel,
1197 const gchar *pattern)
1199 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1200 g_return_if_fail (pattern != NULL);
1202 if (filesel->selection_entry)
1203 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1204 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1208 gtk_file_selection_destroy (GtkObject *object)
1210 GtkFileSelection *filesel;
1212 HistoryCallbackArg *callback_arg;
1214 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1216 filesel = GTK_FILE_SELECTION (object);
1218 if (filesel->fileop_dialog)
1220 gtk_widget_destroy (filesel->fileop_dialog);
1221 filesel->fileop_dialog = NULL;
1224 if (filesel->history_list)
1226 list = filesel->history_list;
1229 callback_arg = list->data;
1230 g_free (callback_arg->directory);
1231 g_free (callback_arg);
1234 g_list_free (filesel->history_list);
1235 filesel->history_list = NULL;
1238 if (filesel->cmpl_state)
1240 cmpl_free_state (filesel->cmpl_state);
1241 filesel->cmpl_state = NULL;
1244 if (filesel->selected_names)
1246 free_selected_names (filesel->selected_names);
1247 filesel->selected_names = NULL;
1250 if (filesel->last_selected)
1252 g_free (filesel->last_selected);
1253 filesel->last_selected = NULL;
1256 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1260 gtk_file_selection_map (GtkWidget *widget)
1262 GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1264 /* Refresh the contents */
1265 gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1267 GTK_WIDGET_CLASS (parent_class)->map (widget);
1271 gtk_file_selection_finalize (GObject *object)
1273 GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1275 g_free (filesel->fileop_file);
1277 G_OBJECT_CLASS (parent_class)->finalize (object);
1280 /* Begin file operations callbacks */
1283 gtk_file_selection_fileop_error (GtkFileSelection *fs,
1284 gchar *error_message)
1288 g_return_if_fail (error_message != NULL);
1291 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1292 GTK_DIALOG_DESTROY_WITH_PARENT,
1295 "%s", error_message);
1297 /* yes, we free it */
1298 g_free (error_message);
1300 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1302 gtk_signal_connect_object (GTK_OBJECT (dialog), "response",
1303 (GtkSignalFunc) gtk_widget_destroy,
1306 gtk_widget_show (dialog);
1310 gtk_file_selection_fileop_destroy (GtkWidget *widget,
1313 GtkFileSelection *fs = data;
1315 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1317 fs->fileop_dialog = NULL;
1322 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1325 GtkFileSelection *fs = data;
1326 const gchar *dirname;
1329 gchar *sys_full_path;
1331 GError *error = NULL;
1332 CompletionState *cmpl_state;
1334 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1336 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1337 cmpl_state = (CompletionState*) fs->cmpl_state;
1338 path = cmpl_reference_position (cmpl_state);
1340 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1341 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1344 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1345 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1347 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname, error->message,
1348 _("You probably used symbols not allowed in filenames."));
1349 gtk_file_selection_fileop_error (fs, buf);
1350 g_error_free (error);
1354 if (mkdir (sys_full_path, 0755) < 0)
1356 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname,
1357 g_strerror (errno));
1358 gtk_file_selection_fileop_error (fs, buf);
1363 g_free (sys_full_path);
1365 gtk_widget_destroy (fs->fileop_dialog);
1366 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1370 gtk_file_selection_create_dir (GtkWidget *widget,
1373 GtkFileSelection *fs = data;
1379 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1381 if (fs->fileop_dialog)
1385 dialog = gtk_dialog_new ();
1386 fs->fileop_dialog = dialog;
1387 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1388 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1390 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1391 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1392 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1394 /* If file dialog is grabbed, grab option dialog */
1395 /* When option dialog is closed, file dialog will be grabbed again */
1396 if (GTK_WINDOW (fs)->modal)
1397 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1399 vbox = gtk_vbox_new (FALSE, 0);
1400 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1401 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1403 gtk_widget_show( vbox);
1405 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1406 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1407 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1408 gtk_widget_show (label);
1410 /* The directory entry widget */
1411 fs->fileop_entry = gtk_entry_new ();
1412 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1413 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1415 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1416 gtk_widget_show (fs->fileop_entry);
1419 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1420 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1421 (GtkSignalFunc) gtk_widget_destroy,
1423 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1424 button, TRUE, TRUE, 0);
1425 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1426 gtk_widget_grab_default (button);
1427 gtk_widget_show (button);
1429 gtk_widget_grab_focus (fs->fileop_entry);
1431 button = gtk_button_new_with_label (_("Create"));
1432 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1433 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
1435 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1436 button, TRUE, TRUE, 0);
1437 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1438 gtk_widget_show (button);
1440 gtk_widget_show (dialog);
1444 gtk_file_selection_delete_file_response (GtkDialog *dialog,
1448 GtkFileSelection *fs = data;
1449 CompletionState *cmpl_state;
1452 gchar *sys_full_path;
1453 GError *error = NULL;
1456 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1458 if (response_id != GTK_RESPONSE_OK)
1460 gtk_widget_destroy (GTK_WIDGET (dialog));
1464 cmpl_state = (CompletionState*) fs->cmpl_state;
1465 path = cmpl_reference_position (cmpl_state);
1467 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1468 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1471 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1472 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1475 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1476 fs->fileop_file, error->message,
1477 _("It probably contains symbols not allowed in filenames."));
1479 gtk_file_selection_fileop_error (fs, buf);
1480 g_error_free (error);
1484 if (unlink (sys_full_path) < 0)
1486 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1487 fs->fileop_file, g_strerror (errno));
1488 gtk_file_selection_fileop_error (fs, buf);
1493 g_free (sys_full_path);
1495 gtk_widget_destroy (fs->fileop_dialog);
1496 gtk_file_selection_populate (fs, "", FALSE, TRUE);
1500 gtk_file_selection_delete_file (GtkWidget *widget,
1503 GtkFileSelection *fs = data;
1505 const gchar *filename;
1507 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1509 if (fs->fileop_dialog)
1512 #ifdef G_WITH_CYGWIN
1513 translate_win32_path (fs);
1516 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1517 if (strlen (filename) < 1)
1520 g_free (fs->fileop_file);
1521 fs->fileop_file = g_strdup (filename);
1524 fs->fileop_dialog = dialog =
1525 gtk_message_dialog_new (GTK_WINDOW (fs),
1526 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1527 GTK_MESSAGE_QUESTION,
1529 _("Really delete file \"%s\" ?"), filename);
1531 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1532 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1534 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1535 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1538 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1539 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1540 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1543 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1545 g_signal_connect (G_OBJECT (dialog), "response",
1546 G_CALLBACK (gtk_file_selection_delete_file_response),
1549 gtk_widget_show (dialog);
1553 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1556 GtkFileSelection *fs = data;
1560 gchar *new_filename;
1561 gchar *old_filename;
1562 gchar *sys_new_filename;
1563 gchar *sys_old_filename;
1564 CompletionState *cmpl_state;
1565 GError *error = NULL;
1567 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1569 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1570 cmpl_state = (CompletionState*) fs->cmpl_state;
1571 path = cmpl_reference_position (cmpl_state);
1573 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1574 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1576 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1579 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1580 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1582 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1583 new_filename, error->message,
1584 _("You probably used symbols not allowed in filenames."));
1585 gtk_file_selection_fileop_error (fs, buf);
1586 g_error_free (error);
1590 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1593 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1594 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1596 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1597 old_filename, error->message,
1598 _("It probably contains symbols not allowed in filenames."));
1599 gtk_file_selection_fileop_error (fs, buf);
1600 g_error_free (error);
1604 if (rename (sys_old_filename, sys_new_filename) < 0)
1606 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1607 sys_old_filename, sys_new_filename,
1608 g_strerror (errno));
1609 gtk_file_selection_fileop_error (fs, buf);
1613 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1614 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1617 g_free (sys_old_filename);
1620 g_free (new_filename);
1621 g_free (old_filename);
1622 g_free (sys_new_filename);
1624 gtk_widget_destroy (fs->fileop_dialog);
1628 gtk_file_selection_rename_file (GtkWidget *widget,
1631 GtkFileSelection *fs = data;
1638 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1640 if (fs->fileop_dialog)
1643 g_free (fs->fileop_file);
1644 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1645 if (strlen (fs->fileop_file) < 1)
1649 fs->fileop_dialog = dialog = gtk_dialog_new ();
1650 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1651 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1653 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1654 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1655 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1657 /* If file dialog is grabbed, grab option dialog */
1658 /* When option dialog closed, file dialog will be grabbed again */
1659 if (GTK_WINDOW (fs)->modal)
1660 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1662 vbox = gtk_vbox_new (FALSE, 0);
1663 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1664 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1666 gtk_widget_show(vbox);
1668 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1669 label = gtk_label_new (buf);
1670 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1671 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1672 gtk_widget_show (label);
1675 /* New filename entry */
1676 fs->fileop_entry = gtk_entry_new ();
1677 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1679 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1680 gtk_widget_show (fs->fileop_entry);
1682 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1683 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1684 0, strlen (fs->fileop_file));
1687 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1688 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1689 (GtkSignalFunc) gtk_widget_destroy,
1691 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1692 button, TRUE, TRUE, 0);
1693 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1694 gtk_widget_grab_default (button);
1695 gtk_widget_show (button);
1697 gtk_widget_grab_focus (fs->fileop_entry);
1699 button = gtk_button_new_with_label (_("Rename"));
1700 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1701 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1703 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1704 button, TRUE, TRUE, 0);
1705 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1706 gtk_widget_show (button);
1708 gtk_widget_show (dialog);
1712 gtk_file_selection_insert_text (GtkWidget *widget,
1713 const gchar *new_text,
1714 gint new_text_length,
1720 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1725 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "insert_text");
1735 gtk_file_selection_key_press (GtkWidget *widget,
1739 GtkFileSelection *fs;
1742 g_return_val_if_fail (widget != NULL, FALSE);
1743 g_return_val_if_fail (event != NULL, FALSE);
1745 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1746 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1748 fs = GTK_FILE_SELECTION (user_data);
1749 #ifdef G_WITH_CYGWIN
1750 translate_win32_path (fs);
1752 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1754 gtk_file_selection_populate (fs, text, TRUE, TRUE);
1765 gtk_file_selection_history_callback (GtkWidget *widget,
1768 GtkFileSelection *fs = data;
1769 HistoryCallbackArg *callback_arg;
1772 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1774 list = fs->history_list;
1777 callback_arg = list->data;
1779 if (callback_arg->menu_item == widget)
1781 gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1790 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1791 gchar *current_directory)
1793 HistoryCallbackArg *callback_arg;
1794 GtkWidget *menu_item;
1800 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1801 g_return_if_fail (current_directory != NULL);
1803 list = fs->history_list;
1805 if (fs->history_menu)
1808 callback_arg = list->data;
1809 g_free (callback_arg->directory);
1810 g_free (callback_arg);
1813 g_list_free (fs->history_list);
1814 fs->history_list = NULL;
1816 gtk_widget_destroy (fs->history_menu);
1819 fs->history_menu = gtk_menu_new ();
1821 current_dir = g_strdup (current_directory);
1823 dir_len = strlen (current_dir);
1825 for (i = dir_len; i >= 0; i--)
1827 /* the i == dir_len is to catch the full path for the first
1829 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1831 /* another small hack to catch the full path */
1833 current_dir[i + 1] = '\0';
1834 #ifdef G_WITH_CYGWIN
1835 if (!strcmp (current_dir, "//"))
1838 menu_item = gtk_menu_item_new_with_label (current_dir);
1840 callback_arg = g_new (HistoryCallbackArg, 1);
1841 callback_arg->menu_item = menu_item;
1843 /* since the autocompletion gets confused if you don't
1844 * supply a trailing '/' on a dir entry, set the full
1845 * (current) path to "" which just refreshes the filesel */
1848 callback_arg->directory = g_strdup ("");
1852 callback_arg->directory = g_strdup (current_dir);
1855 fs->history_list = g_list_append (fs->history_list, callback_arg);
1857 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1858 (GtkSignalFunc) gtk_file_selection_history_callback,
1860 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1861 gtk_widget_show (menu_item);
1865 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1867 g_free (current_dir);
1871 get_real_filename (gchar *filename,
1874 #ifdef G_WITH_CYGWIN
1875 /* Check to see if the selection was a drive selector */
1876 if (isalpha (filename[0]) && (filename[1] == ':'))
1878 /* It is... map it to a CYGWIN32 drive */
1879 gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
1884 return temp_filename;
1888 #endif /* G_WITH_CYGWIN */
1892 gtk_file_selection_file_activate (GtkTreeView *tree_view,
1894 GtkTreeViewColumn *column,
1897 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1898 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1902 gtk_tree_model_get_iter (model, &iter, path);
1903 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
1904 filename = get_real_filename (filename, TRUE);
1906 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1907 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1913 gtk_file_selection_dir_activate (GtkTreeView *tree_view,
1915 GtkTreeViewColumn *column,
1918 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1919 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1923 gtk_tree_model_get_iter (model, &iter, path);
1924 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
1925 gtk_file_selection_populate (fs, filename, FALSE, FALSE);
1929 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1932 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
1936 char formatBuffer[128];
1939 /* Get the drives string */
1940 GetLogicalDriveStrings (sizeof (buffer), buffer);
1942 /* Add the drives as necessary */
1944 while (*textPtr != '\0')
1946 /* Ignore floppies (?) */
1947 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1949 /* Build the actual displayable string */
1950 sprintf (formatBuffer, "%c:\\", toupper (textPtr[0]));
1952 /* Add to the list */
1953 gtk_list_store_append (model, &iter);
1954 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
1956 textPtr += (strlen (textPtr) + 1);
1962 gtk_file_selection_populate (GtkFileSelection *fs,
1964 gboolean try_complete,
1965 gboolean reset_entry)
1967 CompletionState *cmpl_state;
1968 PossibleCompletion* poss;
1970 GtkListStore *dir_model;
1971 GtkListStore *file_model;
1973 gchar* rem_path = rel_path;
1975 gint did_recurse = FALSE;
1976 gint possible_count = 0;
1977 gint selection_index = -1;
1979 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1981 cmpl_state = (CompletionState*) fs->cmpl_state;
1982 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1984 if (!cmpl_state_okay (cmpl_state))
1986 /* Something went wrong. */
1987 gtk_file_selection_abort (fs);
1991 g_assert (cmpl_state->reference_dir);
1993 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
1994 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
1996 gtk_list_store_clear (dir_model);
1997 gtk_list_store_clear (file_model);
1999 /* Set the dir list to include ./ and ../ */
2000 gtk_list_store_append (dir_model, &iter);
2001 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2002 gtk_list_store_append (dir_model, &iter);
2003 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2007 if (cmpl_is_a_completion (poss))
2009 possible_count += 1;
2011 filename = cmpl_this_completion (poss);
2013 if (cmpl_is_directory (poss))
2015 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2016 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2018 gtk_list_store_append (dir_model, &iter);
2019 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2024 gtk_list_store_append (file_model, &iter);
2025 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2029 poss = cmpl_next_completion (cmpl_state);
2032 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2033 /* For Windows, add drives as potential selections */
2034 win32_gtk_add_drives_to_dir_list (dir_model);
2037 /* File lists are set. */
2039 g_assert (cmpl_state->reference_dir);
2044 /* User is trying to complete filenames, so advance the user's input
2045 * string to the updated_text, which is the common leading substring
2046 * of all possible completions, and if its a directory attempt
2047 * attempt completions in it. */
2049 if (cmpl_updated_text (cmpl_state)[0])
2052 if (cmpl_updated_dir (cmpl_state))
2054 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2058 gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2064 if (fs->selection_entry)
2065 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2066 cmpl_updated_text (cmpl_state));
2071 selection_index = cmpl_last_valid_char (cmpl_state) -
2072 (strlen (rel_path) - strlen (rem_path));
2073 if (fs->selection_entry)
2074 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2077 else if (reset_entry)
2079 if (fs->selection_entry)
2080 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2085 if (fs->selection_entry)
2086 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
2088 if (fs->selection_entry)
2090 sel_text = g_strconcat (_("Selection: "),
2091 cmpl_reference_position (cmpl_state),
2094 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
2098 if (fs->history_pulldown)
2100 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2107 gtk_file_selection_abort (GtkFileSelection *fs)
2111 sprintf (err_buf, _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2113 /* BEEP gdk_beep(); */
2115 if (fs->selection_entry)
2116 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2120 * gtk_file_selection_set_select_multiple:
2121 * @filesel: a #GtkFileSelection
2122 * @select_multiple: whether or not the user is allowed to select multiple
2123 * files in the file list.
2125 * Sets whether the user is allowed to select multiple files in the file list.
2126 * Use gtk_file_selection_get_selections () to get the list of selected files.
2129 gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2130 gboolean select_multiple)
2132 GtkTreeSelection *sel;
2133 GtkSelectionMode mode;
2135 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2137 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2139 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2141 if (mode != gtk_tree_selection_get_mode (sel))
2143 gtk_tree_selection_set_mode (sel, mode);
2145 g_object_notify (G_OBJECT (filesel), "select-multiple");
2150 * gtk_file_selection_get_select_multiple:
2151 * @filesel: a #GtkFileSelection
2153 * Determines whether or not the user is allowed to select multiple files in
2154 * the file list. See gtk_file_selection_set_select_multiple().
2156 * Return value: %TRUE if the user is allowed to select multiple files in the
2160 gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2162 GtkTreeSelection *sel;
2164 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2166 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2167 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2171 multiple_changed_foreach (GtkTreeModel *model,
2176 GPtrArray *names = data;
2179 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2181 g_ptr_array_add (names, filename);
2185 free_selected_names (GPtrArray *names)
2189 for (i = 0; i < names->len; i++)
2190 g_free (g_ptr_array_index (names, i));
2192 g_ptr_array_free (names, TRUE);
2196 gtk_file_selection_file_changed (GtkTreeSelection *selection,
2199 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2200 GPtrArray *new_names;
2205 new_names = g_ptr_array_sized_new (8);
2207 gtk_tree_selection_selected_foreach (selection,
2208 multiple_changed_foreach,
2211 /* nothing selected */
2212 if (new_names->len == 0)
2214 g_ptr_array_free (new_names, TRUE);
2216 if (fs->selected_names != NULL)
2218 free_selected_names (fs->selected_names);
2219 fs->selected_names = NULL;
2222 goto maybe_clear_entry;
2225 if (new_names->len != 1)
2227 GPtrArray *old_names = fs->selected_names;
2229 if (old_names != NULL)
2231 /* A common case is selecting a range of files from top to bottom,
2232 * so quickly check for that to avoid looping over the entire list
2234 if (compare_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2235 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2236 index = new_names->len - 1;
2239 gint i = 0, j = 0, cmp;
2241 /* do a quick diff, stopping at the first file not in the
2244 while (i < old_names->len && j < new_names->len)
2246 cmp = compare_filenames (g_ptr_array_index (old_names, i),
2247 g_ptr_array_index (new_names, j));
2264 /* we ran off the end of the old list */
2265 if (index == -1 && i < new_names->len)
2271 /* A phantom anchor still exists at the point where the last item
2272 * was selected, which is used for subsequent range selections.
2273 * So search up from there.
2275 if (compare_filenames (fs->last_selected,
2276 g_ptr_array_index (new_names, 0)) == 0)
2277 index = new_names->len - 1;
2285 if (fs->selected_names != NULL)
2286 free_selected_names (fs->selected_names);
2288 fs->selected_names = new_names;
2292 if (fs->last_selected != NULL)
2293 g_free (fs->last_selected);
2295 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2296 filename = get_real_filename (fs->last_selected, FALSE);
2298 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2300 if (filename != fs->last_selected)
2308 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2309 if ((entry != NULL) && (fs->last_selected != NULL) &&
2310 (compare_filenames (entry, fs->last_selected) == 0))
2311 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2315 * gtk_file_selection_get_selections:
2316 * @filesel: a #GtkFileSelection
2318 * Retrieves the list of file selections the user has made in the dialog box.
2319 * This function is intended for use when the user can select multiple files
2320 * in the file list. The first file in the list is equivalent to what
2321 * gtk_file_selection_get_filename() would return.
2323 * The filenames are in the encoding of g_filename_from_utf8, which may or may
2324 * not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2325 * g_filename_to_utf8() on each string.
2327 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2328 * g_strfreev() to free it.
2331 gtk_file_selection_get_selections (GtkFileSelection *filesel)
2335 gchar *filename, *dirname;
2336 gchar *current, *buf;
2339 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2341 filename = g_strdup (gtk_file_selection_get_filename (filesel));
2343 if (strlen (filename) == 0)
2349 names = filesel->selected_names;
2352 selections = g_new (gchar *, names->len + 2);
2354 selections = g_new (gchar *, 2);
2357 selections[count++] = filename;
2361 dirname = g_path_get_dirname (filename);
2363 for (i = 0; i < names->len; i++)
2365 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2367 current = g_build_filename (dirname, buf, NULL);
2370 if (compare_filenames (current, filename) != 0)
2371 selections[count++] = current;
2377 selections[count] = NULL;
2382 /**********************************************************************/
2383 /* External Interface */
2384 /**********************************************************************/
2386 /* The four completion state selectors
2389 cmpl_updated_text (CompletionState *cmpl_state)
2391 return cmpl_state->updated_text;
2395 cmpl_updated_dir (CompletionState *cmpl_state)
2397 return cmpl_state->re_complete;
2401 cmpl_reference_position (CompletionState *cmpl_state)
2403 return cmpl_state->reference_dir->fullname;
2407 cmpl_last_valid_char (CompletionState *cmpl_state)
2409 return cmpl_state->last_valid_char;
2413 cmpl_completion_fullname (const gchar *text,
2414 CompletionState *cmpl_state)
2416 static char nothing[2] = "";
2418 if (!cmpl_state_okay (cmpl_state))
2422 else if (g_path_is_absolute (text))
2424 strcpy (cmpl_state->updated_text, text);
2427 else if (text[0] == '~')
2432 dir = open_user_dir (text, cmpl_state);
2436 /* spencer says just return ~something, so
2437 * for now just do it. */
2438 strcpy (cmpl_state->updated_text, text);
2443 strcpy (cmpl_state->updated_text, dir->fullname);
2445 slash = strchr (text, G_DIR_SEPARATOR);
2448 strcat (cmpl_state->updated_text, slash);
2454 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
2455 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
2456 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
2457 strcat (cmpl_state->updated_text, text);
2460 return cmpl_state->updated_text;
2463 /* The three completion selectors
2466 cmpl_this_completion (PossibleCompletion* pc)
2472 cmpl_is_directory (PossibleCompletion* pc)
2474 return pc->is_directory;
2478 cmpl_is_a_completion (PossibleCompletion* pc)
2480 return pc->is_a_completion;
2483 /**********************************************************************/
2484 /* Construction, deletion */
2485 /**********************************************************************/
2487 static CompletionState*
2488 cmpl_init_state (void)
2490 gchar *sys_getcwd_buf;
2492 CompletionState *new_state;
2494 new_state = g_new (CompletionState, 1);
2496 /* g_get_current_dir() returns a string in the "system" charset */
2497 sys_getcwd_buf = g_get_current_dir ();
2498 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
2499 g_free (sys_getcwd_buf);
2503 new_state->reference_dir = NULL;
2504 new_state->completion_dir = NULL;
2505 new_state->active_completion_dir = NULL;
2506 new_state->directory_storage = NULL;
2507 new_state->directory_sent_storage = NULL;
2508 new_state->last_valid_char = 0;
2509 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2510 new_state->updated_text_alloc = MAXPATHLEN;
2511 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2512 new_state->the_completion.text_alloc = MAXPATHLEN;
2513 new_state->user_dir_name_buffer = NULL;
2514 new_state->user_directories = NULL;
2516 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2518 if (!new_state->reference_dir)
2520 /* Directories changing from underneath us, grumble */
2521 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2530 cmpl_free_dir_list (GList* dp0)
2536 free_dir (dp->data);
2544 cmpl_free_dir_sent_list (GList* dp0)
2550 free_dir_sent (dp->data);
2558 cmpl_free_state (CompletionState* cmpl_state)
2560 g_return_if_fail (cmpl_state != NULL);
2562 cmpl_free_dir_list (cmpl_state->directory_storage);
2563 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2565 if (cmpl_state->user_dir_name_buffer)
2566 g_free (cmpl_state->user_dir_name_buffer);
2567 if (cmpl_state->user_directories)
2568 g_free (cmpl_state->user_directories);
2569 if (cmpl_state->the_completion.text)
2570 g_free (cmpl_state->the_completion.text);
2571 if (cmpl_state->updated_text)
2572 g_free (cmpl_state->updated_text);
2574 g_free (cmpl_state);
2578 free_dir (CompletionDir* dir)
2580 g_free (dir->cmpl_text);
2581 g_free (dir->fullname);
2586 free_dir_sent (CompletionDirSent* sent)
2589 for (i = 0; i < sent->entry_count; i++)
2590 g_free (sent->entries[i].entry_name);
2591 g_free (sent->entries);
2596 prune_memory_usage (CompletionState *cmpl_state)
2598 GList* cdsl = cmpl_state->directory_sent_storage;
2599 GList* cdl = cmpl_state->directory_storage;
2603 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2608 cmpl_free_dir_sent_list (cdsl->next);
2612 cmpl_state->directory_storage = NULL;
2615 if (cdl->data == cmpl_state->reference_dir)
2616 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2618 free_dir (cdl->data);
2625 /**********************************************************************/
2626 /* The main entrances. */
2627 /**********************************************************************/
2629 static PossibleCompletion*
2630 cmpl_completion_matches (gchar *text_to_complete,
2631 gchar **remaining_text,
2632 CompletionState *cmpl_state)
2635 PossibleCompletion *poss;
2637 prune_memory_usage (cmpl_state);
2639 g_assert (text_to_complete != NULL);
2641 cmpl_state->user_completion_index = -1;
2642 cmpl_state->last_completion_text = text_to_complete;
2643 cmpl_state->the_completion.text[0] = 0;
2644 cmpl_state->last_valid_char = 0;
2645 cmpl_state->updated_text_len = -1;
2646 cmpl_state->updated_text[0] = 0;
2647 cmpl_state->re_complete = FALSE;
2650 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2652 if (text_to_complete[0] == '~' && !first_slash)
2654 /* Text starts with ~ and there is no slash, show all the
2655 * home directory completions.
2657 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2659 update_cmpl (poss, cmpl_state);
2664 cmpl_state->reference_dir =
2665 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2667 if (!cmpl_state->reference_dir)
2670 cmpl_state->completion_dir =
2671 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2673 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2675 if (!cmpl_state->completion_dir)
2678 cmpl_state->completion_dir->cmpl_index = -1;
2679 cmpl_state->completion_dir->cmpl_parent = NULL;
2680 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2682 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2684 cmpl_state->reference_dir = cmpl_state->completion_dir;
2686 poss = attempt_file_completion (cmpl_state);
2688 update_cmpl (poss, cmpl_state);
2693 static PossibleCompletion*
2694 cmpl_next_completion (CompletionState* cmpl_state)
2696 PossibleCompletion* poss = NULL;
2698 cmpl_state->the_completion.text[0] = 0;
2701 if (cmpl_state->user_completion_index >= 0)
2702 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2704 poss = attempt_file_completion (cmpl_state);
2706 poss = attempt_file_completion (cmpl_state);
2709 update_cmpl (poss, cmpl_state);
2714 /**********************************************************************/
2715 /* Directory Operations */
2716 /**********************************************************************/
2718 /* Open the directory where completion will begin from, if possible. */
2719 static CompletionDir*
2720 open_ref_dir (gchar *text_to_complete,
2721 gchar **remaining_text,
2722 CompletionState *cmpl_state)
2725 CompletionDir *new_dir;
2727 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2729 #ifdef G_WITH_CYGWIN
2730 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2733 sprintf (root_dir, "//%c", text_to_complete[2]);
2735 new_dir = open_dir (root_dir, cmpl_state);
2738 *remaining_text = text_to_complete + 4;
2746 else if (text_to_complete[0] == '~')
2748 new_dir = open_user_dir (text_to_complete, cmpl_state);
2753 *remaining_text = first_slash + 1;
2755 *remaining_text = text_to_complete + strlen (text_to_complete);
2763 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2765 gchar *tmp = g_strdup (text_to_complete);
2769 while (*p && *p != '*' && *p != '?')
2773 p = strrchr (tmp, G_DIR_SEPARATOR);
2781 new_dir = open_dir (tmp, cmpl_state);
2784 *remaining_text = text_to_complete +
2785 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2789 /* If no possible candidates, use the cwd */
2790 gchar *sys_curdir = g_get_current_dir ();
2791 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2793 g_free (sys_curdir);
2795 new_dir = open_dir (utf8_curdir, cmpl_state);
2798 *remaining_text = text_to_complete;
2800 g_free (utf8_curdir);
2807 *remaining_text = text_to_complete;
2809 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2814 new_dir->cmpl_index = -1;
2815 new_dir->cmpl_parent = NULL;
2823 /* open a directory by user name */
2824 static CompletionDir*
2825 open_user_dir (const gchar *text_to_complete,
2826 CompletionState *cmpl_state)
2828 CompletionDir *result;
2832 g_assert (text_to_complete && text_to_complete[0] == '~');
2834 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2837 cmp_len = first_slash - text_to_complete - 1;
2839 cmp_len = strlen (text_to_complete + 1);
2844 const gchar *homedir = g_get_home_dir ();
2845 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2848 result = open_dir (utf8_homedir, cmpl_state);
2852 g_free (utf8_homedir);
2857 gchar* copy = g_new (char, cmp_len + 1);
2861 strncpy (copy, text_to_complete + 1, cmp_len);
2863 pwd = getpwnam (copy);
2870 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2871 result = open_dir (utf8_dir, cmpl_state);
2879 /* open a directory relative the the current relative directory */
2880 static CompletionDir*
2881 open_relative_dir (gchar *dir_name,
2883 CompletionState *cmpl_state)
2885 CompletionDir *result;
2888 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2889 g_string_assign (path, dir->fullname);
2891 if (dir->fullname_len > 1
2892 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2893 g_string_append_c (path, G_DIR_SEPARATOR);
2894 g_string_append (path, dir_name);
2896 result = open_dir (path->str, cmpl_state);
2898 g_string_free (path, TRUE);
2903 /* after the cache lookup fails, really open a new directory */
2904 static CompletionDirSent*
2905 open_new_dir (gchar *dir_name,
2907 gboolean stat_subdirs)
2909 CompletionDirSent *sent;
2913 gint entry_count = 0;
2916 struct stat ent_sbuf;
2918 gchar *sys_dir_name;
2920 sent = g_new (CompletionDirSent, 1);
2921 sent->mtime = sbuf->st_mtime;
2922 sent->inode = sbuf->st_ino;
2923 sent->device = sbuf->st_dev;
2925 path = g_string_sized_new (2*MAXPATHLEN + 10);
2927 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
2930 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
2934 directory = g_dir_open (sys_dir_name, 0, &error);
2937 cmpl_errno = error->code; /* ??? */
2938 g_free (sys_dir_name);
2942 while ((dirent = g_dir_read_name (directory)) != NULL)
2945 sent->entries = g_new (CompletionDirEntry, entry_count);
2946 sent->entry_count = entry_count;
2948 g_dir_rewind (directory);
2950 for (i = 0; i < entry_count; i += 1)
2952 dirent = g_dir_read_name (directory);
2956 g_warning ("Failure reading folder '%s'", sys_dir_name);
2957 g_dir_close (directory);
2958 g_free (sys_dir_name);
2962 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, NULL);
2963 if (sent->entries[n_entries].entry_name == NULL
2964 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
2966 g_warning (_("The filename %s couldn't be converted to UTF-8. Try setting the environment variable G_BROKEN_FILENAMES."), dirent);
2970 g_string_assign (path, sys_dir_name);
2971 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2973 g_string_append_c (path, G_DIR_SEPARATOR);
2975 g_string_append (path, dirent);
2979 /* Here we know path->str is a "system charset" string */
2980 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
2981 sent->entries[n_entries].is_dir = TRUE;
2983 /* stat may fail, and we don't mind, since it could be a
2984 * dangling symlink. */
2985 sent->entries[n_entries].is_dir = FALSE;
2988 sent->entries[n_entries].is_dir = 1;
2992 sent->entry_count = n_entries;
2994 g_free (sys_dir_name);
2995 g_string_free (path, TRUE);
2996 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
2998 g_dir_close (directory);
3003 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3006 check_dir (gchar *dir_name,
3007 struct stat *result,
3008 gboolean *stat_subdirs)
3010 /* A list of directories that we know only contain other directories.
3011 * Trying to stat every file in these directories would be very
3018 struct stat statbuf;
3019 } no_stat_dirs[] = {
3020 { "/afs", FALSE, { 0 } },
3021 { "/net", FALSE, { 0 } }
3024 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3025 static gboolean initialized = FALSE;
3026 gchar *sys_dir_name;
3032 for (i = 0; i < n_no_stat_dirs; i++)
3034 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3035 no_stat_dirs[i].present = TRUE;
3039 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3042 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3046 if (stat (sys_dir_name, result) < 0)
3048 g_free (sys_dir_name);
3052 g_free (sys_dir_name);
3054 *stat_subdirs = TRUE;
3055 for (i = 0; i < n_no_stat_dirs; i++)
3057 if (no_stat_dirs[i].present &&
3058 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3059 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3061 *stat_subdirs = FALSE;
3071 /* open a directory by absolute pathname */
3072 static CompletionDir*
3073 open_dir (gchar *dir_name,
3074 CompletionState *cmpl_state)
3077 gboolean stat_subdirs;
3078 CompletionDirSent *sent;
3081 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3082 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3085 cdsl = cmpl_state->directory_sent_storage;
3091 if (sent->inode == sbuf.st_ino &&
3092 sent->mtime == sbuf.st_mtime &&
3093 sent->device == sbuf.st_dev)
3094 return attach_dir (sent, dir_name, cmpl_state);
3099 stat_subdirs = TRUE;
3102 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3106 cmpl_state->directory_sent_storage =
3107 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3109 return attach_dir (sent, dir_name, cmpl_state);
3115 static CompletionDir*
3116 attach_dir (CompletionDirSent *sent,
3118 CompletionState *cmpl_state)
3120 CompletionDir* new_dir;
3122 new_dir = g_new (CompletionDir, 1);
3124 cmpl_state->directory_storage =
3125 g_list_prepend (cmpl_state->directory_storage, new_dir);
3127 new_dir->sent = sent;
3128 new_dir->fullname = g_strdup (dir_name);
3129 new_dir->fullname_len = strlen (dir_name);
3130 new_dir->cmpl_text = NULL;
3136 correct_dir_fullname (CompletionDir* cmpl_dir)
3138 gint length = strlen (cmpl_dir->fullname);
3139 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3140 gchar *sys_filename;
3143 /* Does it end with /. (\.) ? */
3145 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3147 /* Is it just the root directory (on a drive) ? */
3148 if (cmpl_dir->fullname + length - 2 == first_slash)
3150 cmpl_dir->fullname[length - 1] = 0;
3151 cmpl_dir->fullname_len = length - 1;
3156 cmpl_dir->fullname[length - 2] = 0;
3160 /* Ends with /./ (\.\)? */
3161 else if (length >= 3 &&
3162 strcmp (cmpl_dir->fullname + length - 3,
3163 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3164 cmpl_dir->fullname[length - 2] = 0;
3166 /* Ends with /.. (\..) ? */
3167 else if (length >= 3 &&
3168 strcmp (cmpl_dir->fullname + length - 3,
3169 G_DIR_SEPARATOR_S "..") == 0)
3171 /* Is it just /.. (X:\..)? */
3172 if (cmpl_dir->fullname + length - 3 == first_slash)
3174 cmpl_dir->fullname[length - 2] = 0;
3175 cmpl_dir->fullname_len = length - 2;
3179 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3182 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3186 if (stat (sys_filename, &sbuf) < 0)
3188 g_free (sys_filename);
3192 g_free (sys_filename);
3194 cmpl_dir->fullname[length - 3] = 0;
3196 if (!correct_parent (cmpl_dir, &sbuf))
3200 /* Ends with /../ (\..\)? */
3201 else if (length >= 4 &&
3202 strcmp (cmpl_dir->fullname + length - 4,
3203 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3205 /* Is it just /../ (X:\..\)? */
3206 if (cmpl_dir->fullname + length - 4 == first_slash)
3208 cmpl_dir->fullname[length - 3] = 0;
3209 cmpl_dir->fullname_len = length - 3;
3213 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3216 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3220 if (stat (sys_filename, &sbuf) < 0)
3222 g_free (sys_filename);
3226 g_free (sys_filename);
3228 cmpl_dir->fullname[length - 4] = 0;
3230 if (!correct_parent (cmpl_dir, &sbuf))
3234 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3240 correct_parent (CompletionDir *cmpl_dir,
3247 gchar *sys_filename;
3250 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3251 g_assert (last_slash);
3252 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3254 /* Clever (?) way to check for top-level directory that works also on
3255 * Win32, where there is a drive letter and colon prefixed...
3257 if (last_slash != first_slash)
3267 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3270 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3272 last_slash[0] = G_DIR_SEPARATOR;
3276 if (stat (sys_filename, &parbuf) < 0)
3278 g_free (sys_filename);
3281 last_slash[0] = G_DIR_SEPARATOR;
3284 g_free (sys_filename);
3286 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3287 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3288 /* it wasn't a link */
3294 last_slash[0] = G_DIR_SEPARATOR;
3296 /* it was a link, have to figure it out the hard way */
3298 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3303 g_free (cmpl_dir->fullname);
3305 cmpl_dir->fullname = new_name;
3314 find_parent_dir_fullname (gchar* dirname)
3316 gchar *sys_orig_dir;
3321 sys_orig_dir = g_get_current_dir ();
3322 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3325 g_free (sys_orig_dir);
3326 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3330 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3332 g_free (sys_dirname);
3333 g_free (sys_orig_dir);
3337 g_free (sys_dirname);
3339 sys_cwd = g_get_current_dir ();
3340 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3343 if (chdir (sys_orig_dir) != 0)
3346 g_free (sys_orig_dir);
3350 g_free (sys_orig_dir);
3356 /**********************************************************************/
3357 /* Completion Operations */
3358 /**********************************************************************/
3362 static PossibleCompletion*
3363 attempt_homedir_completion (gchar *text_to_complete,
3364 CompletionState *cmpl_state)
3368 if (!cmpl_state->user_dir_name_buffer &&
3369 !get_pwdb (cmpl_state))
3371 length = strlen (text_to_complete) - 1;
3373 cmpl_state->user_completion_index += 1;
3375 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3377 index = first_diff_index (text_to_complete + 1,
3378 cmpl_state->user_directories
3379 [cmpl_state->user_completion_index].login);
3386 if (cmpl_state->last_valid_char < (index + 1))
3387 cmpl_state->last_valid_char = index + 1;
3388 cmpl_state->user_completion_index += 1;
3392 cmpl_state->the_completion.is_a_completion = 1;
3393 cmpl_state->the_completion.is_directory = TRUE;
3395 append_completion_text ("~", cmpl_state);
3397 append_completion_text (cmpl_state->
3398 user_directories[cmpl_state->user_completion_index].login,
3401 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3404 if (text_to_complete[1]
3405 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3407 cmpl_state->user_completion_index = -1;
3412 cmpl_state->user_completion_index += 1;
3413 cmpl_state->the_completion.is_a_completion = 1;
3414 cmpl_state->the_completion.is_directory = TRUE;
3416 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3422 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3423 #define FOLD(c) (tolower(c))
3428 /* returns the index (>= 0) of the first differing character,
3429 * PATTERN_MATCH if the completion matches */
3431 first_diff_index (gchar *pat,
3436 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3446 return PATTERN_MATCH;
3449 static PossibleCompletion*
3450 append_completion_text (gchar *text,
3451 CompletionState *cmpl_state)
3455 if (!cmpl_state->the_completion.text)
3458 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3460 if (cmpl_state->the_completion.text_alloc > len)
3462 strcat (cmpl_state->the_completion.text, text);
3463 return &cmpl_state->the_completion;
3469 cmpl_state->the_completion.text_alloc = i;
3471 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3473 if (!cmpl_state->the_completion.text)
3477 strcat (cmpl_state->the_completion.text, text);
3478 return &cmpl_state->the_completion;
3482 static CompletionDir*
3483 find_completion_dir (gchar *text_to_complete,
3484 gchar **remaining_text,
3485 CompletionState *cmpl_state)
3487 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3488 CompletionDir* dir = cmpl_state->reference_dir;
3489 CompletionDir* next;
3490 *remaining_text = text_to_complete;
3494 gint len = first_slash - *remaining_text;
3496 gchar *found_name = NULL; /* Quiet gcc */
3498 gchar* pat_buf = g_new (gchar, len + 1);
3500 strncpy (pat_buf, *remaining_text, len);
3503 for (i = 0; i < dir->sent->entry_count; i += 1)
3505 if (dir->sent->entries[i].is_dir &&
3506 fnmatch (pat_buf, dir->sent->entries[i].entry_name,
3507 FNMATCH_FLAGS)!= FNM_NOMATCH)
3517 found_name = dir->sent->entries[i].entry_name;
3524 /* Perhaps we are trying to open an automount directory */
3525 found_name = pat_buf;
3528 next = open_relative_dir (found_name, dir, cmpl_state);
3536 next->cmpl_parent = dir;
3540 if (!correct_dir_fullname (dir))
3546 *remaining_text = first_slash + 1;
3547 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3556 update_cmpl (PossibleCompletion *poss,
3557 CompletionState *cmpl_state)
3561 if (!poss || !cmpl_is_a_completion (poss))
3564 cmpl_len = strlen (cmpl_this_completion (poss));
3566 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3568 cmpl_state->updated_text =
3569 (gchar*)g_realloc (cmpl_state->updated_text,
3570 cmpl_state->updated_text_alloc);
3571 cmpl_state->updated_text_alloc = 2*cmpl_len;
3574 if (cmpl_state->updated_text_len < 0)
3576 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3577 cmpl_state->updated_text_len = cmpl_len;
3578 cmpl_state->re_complete = cmpl_is_directory (poss);
3580 else if (cmpl_state->updated_text_len == 0)
3582 cmpl_state->re_complete = FALSE;
3587 first_diff_index (cmpl_state->updated_text,
3588 cmpl_this_completion (poss));
3590 cmpl_state->re_complete = FALSE;
3592 if (first_diff == PATTERN_MATCH)
3595 if (first_diff > cmpl_state->updated_text_len)
3596 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3598 cmpl_state->updated_text_len = first_diff;
3599 cmpl_state->updated_text[first_diff] = 0;
3603 static PossibleCompletion*
3604 attempt_file_completion (CompletionState *cmpl_state)
3606 gchar *pat_buf, *first_slash;
3607 CompletionDir *dir = cmpl_state->active_completion_dir;
3609 dir->cmpl_index += 1;
3611 if (dir->cmpl_index == dir->sent->entry_count)
3613 if (dir->cmpl_parent == NULL)
3615 cmpl_state->active_completion_dir = NULL;
3621 cmpl_state->active_completion_dir = dir->cmpl_parent;
3623 return attempt_file_completion (cmpl_state);
3627 g_assert (dir->cmpl_text);
3629 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3633 gint len = first_slash - dir->cmpl_text;
3635 pat_buf = g_new (gchar, len + 1);
3636 strncpy (pat_buf, dir->cmpl_text, len);
3641 gint len = strlen (dir->cmpl_text);
3643 pat_buf = g_new (gchar, len + 2);
3644 strcpy (pat_buf, dir->cmpl_text);
3645 /* Don't append a * if the user entered one herself.
3646 * This way one can complete *.h and don't get matches
3647 * on any .help files, for instance.
3649 if (strchr (pat_buf, '*') == NULL)
3650 strcpy (pat_buf + len, "*");
3655 if (dir->sent->entries[dir->cmpl_index].is_dir)
3657 if (fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3658 FNMATCH_FLAGS) != FNM_NOMATCH)
3660 CompletionDir* new_dir;
3662 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3671 new_dir->cmpl_parent = dir;
3673 new_dir->cmpl_index = -1;
3674 new_dir->cmpl_text = g_strdup (first_slash + 1);
3676 cmpl_state->active_completion_dir = new_dir;
3679 return attempt_file_completion (cmpl_state);
3684 return attempt_file_completion (cmpl_state);
3690 return attempt_file_completion (cmpl_state);
3695 if (dir->cmpl_parent != NULL)
3697 append_completion_text (dir->fullname +
3698 strlen (cmpl_state->completion_dir->fullname) + 1,
3700 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3703 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3705 cmpl_state->the_completion.is_a_completion =
3706 fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
3707 FNMATCH_FLAGS) != FNM_NOMATCH;
3709 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3710 if (dir->sent->entries[dir->cmpl_index].is_dir)
3711 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3714 return &cmpl_state->the_completion;
3721 get_pwdb (CompletionState* cmpl_state)
3723 struct passwd *pwd_ptr;
3726 gint len = 0, i, count = 0;
3728 if (cmpl_state->user_dir_name_buffer)
3732 while ((pwd_ptr = getpwent ()) != NULL)
3734 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3735 len += strlen (utf8);
3737 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3738 len += strlen (utf8);
3746 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3747 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3748 cmpl_state->user_directories_len = count;
3750 buf_ptr = cmpl_state->user_dir_name_buffer;
3752 for (i = 0; i < count; i += 1)
3754 pwd_ptr = getpwent ();
3761 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3762 strcpy (buf_ptr, utf8);
3765 cmpl_state->user_directories[i].login = buf_ptr;
3767 buf_ptr += strlen (buf_ptr);
3770 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3771 strcpy (buf_ptr, utf8);
3774 cmpl_state->user_directories[i].homedir = buf_ptr;
3776 buf_ptr += strlen (buf_ptr);
3780 qsort (cmpl_state->user_directories,
3781 cmpl_state->user_directories_len,
3782 sizeof (CompletionUserDir),
3791 if (cmpl_state->user_dir_name_buffer)
3792 g_free (cmpl_state->user_dir_name_buffer);
3793 if (cmpl_state->user_directories)
3794 g_free (cmpl_state->user_directories);
3796 cmpl_state->user_dir_name_buffer = NULL;
3797 cmpl_state->user_directories = NULL;
3803 compare_user_dir (const void *a,
3806 return strcmp ((((CompletionUserDir*)a))->login,
3807 (((CompletionUserDir*)b))->login);
3813 compare_cmpl_dir (const void *a,
3816 return compare_filenames ((((CompletionDirEntry*)a))->entry_name,
3817 (((CompletionDirEntry*)b))->entry_name);
3821 cmpl_state_okay (CompletionState* cmpl_state)
3823 return cmpl_state && cmpl_state->reference_dir;
3827 cmpl_strerror (gint err)
3829 if (err == CMPL_ERRNO_TOO_LONG)
3830 return _("Name too long");
3831 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3832 return _("Couldn't convert filename");
3834 return g_strerror (err);