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 */
58 #include "gdk/gdkkeysyms.h"
59 #include "gtkbutton.h"
60 #include "gtkcellrenderertext.h"
62 #include "gtkfilesel.h"
66 #include "gtkliststore.h"
68 #include "gtkprivate.h"
69 #include "gtkscrolledwindow.h"
71 #include "gtktreeselection.h"
72 #include "gtktreeview.h"
75 #include "gtkmenuitem.h"
76 #include "gtkoptionmenu.h"
77 #include "gtkdialog.h"
78 #include "gtkmessagedialog.h"
81 #include "gtkeventbox.h"
84 #include "gtkhpaned.h"
89 #define mkdir(p,m) _mkdir(p)
91 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
93 #endif /* G_OS_WIN32 */
96 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
99 #define DIR_LIST_WIDTH 180
100 #define DIR_LIST_HEIGHT 180
101 #define FILE_LIST_WIDTH 180
102 #define FILE_LIST_HEIGHT 180
104 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
105 * in here, since the rest of the code in the file does require some
110 # define MAXPATHLEN PATH_MAX
112 # define MAXPATHLEN 2048
116 /* I've put this here so it doesn't get confused with the
117 * file completion interface */
118 typedef struct _HistoryCallbackArg HistoryCallbackArg;
120 struct _HistoryCallbackArg
123 GtkWidget *menu_item;
127 typedef struct _CompletionState CompletionState;
128 typedef struct _CompletionDir CompletionDir;
129 typedef struct _CompletionDirSent CompletionDirSent;
130 typedef struct _CompletionDirEntry CompletionDirEntry;
131 typedef struct _CompletionUserDir CompletionUserDir;
132 typedef struct _PossibleCompletion PossibleCompletion;
134 /* Non-external file completion decls and structures */
136 /* A contant telling PRCS how many directories to cache. Its actually
137 * kept in a list, so the geometry isn't important. */
138 #define CMPL_DIRECTORY_CACHE_SIZE 10
140 /* A constant used to determine whether a substring was an exact
141 * match by first_diff_index()
143 #define PATTERN_MATCH -1
144 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
145 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
147 /* This structure contains all the useful information about a directory
148 * for the purposes of filename completion. These structures are cached
149 * in the CompletionState struct. CompletionDir's are reference counted.
151 struct _CompletionDirSent
158 struct _CompletionDirEntry *entries;
161 struct _CompletionDir
163 CompletionDirSent *sent;
168 struct _CompletionDir *cmpl_parent;
173 /* This structure contains pairs of directory entry names with a flag saying
174 * whether or not they are a valid directory. NOTE: This information is used
175 * to provide the caller with information about whether to update its completions
176 * or try to open a file. Since directories are cached by the directory mtime,
177 * a symlink which points to an invalid file (which will not be a directory),
178 * will not be reevaluated if that file is created, unless the containing
179 * directory is touched. I consider this case to be worth ignoring (josh).
181 struct _CompletionDirEntry
188 struct _CompletionUserDir
194 struct _PossibleCompletion
196 /* accessible fields, all are accessed externally by functions
200 gint is_a_completion;
201 gboolean is_directory;
208 struct _CompletionState
210 gint last_valid_char;
212 gint updated_text_len;
213 gint updated_text_alloc;
214 gboolean re_complete;
216 gchar *user_dir_name_buffer;
217 gint user_directories_len;
219 gchar *last_completion_text;
221 gint user_completion_index; /* if >= 0, currently completing ~user */
223 struct _CompletionDir *completion_dir; /* directory completing from */
224 struct _CompletionDir *active_completion_dir;
226 struct _PossibleCompletion the_completion;
228 struct _CompletionDir *reference_dir; /* initial directory */
230 GList* directory_storage;
231 GList* directory_sent_storage;
233 struct _CompletionUserDir *user_directories;
251 /* File completion functions which would be external, were they used
252 * outside of this file.
255 static CompletionState* cmpl_init_state (void);
256 static void cmpl_free_state (CompletionState *cmpl_state);
257 static gint cmpl_state_okay (CompletionState* cmpl_state);
258 static const gchar* cmpl_strerror (gint);
260 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
261 gchar **remaining_text,
262 CompletionState *cmpl_state);
264 /* Returns a name for consideration, possibly a completion, this name
265 * will be invalid after the next call to cmpl_next_completion.
267 static char* cmpl_this_completion (PossibleCompletion*);
269 /* True if this completion matches the given text. Otherwise, this
270 * output can be used to have a list of non-completions.
272 static gint cmpl_is_a_completion (PossibleCompletion*);
274 /* True if the completion is a directory
276 static gboolean cmpl_is_directory (PossibleCompletion*);
278 /* Obtains the next completion, or NULL
280 static PossibleCompletion* cmpl_next_completion (CompletionState*);
282 /* Updating completions: the return value of cmpl_updated_text() will
283 * be text_to_complete completed as much as possible after the most
284 * recent call to cmpl_completion_matches. For the present
285 * application, this is the suggested replacement for the user's input
286 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
289 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
291 /* After updating, to see if the completion was a directory, call
292 * this. If it was, you should consider re-calling completion_matches.
294 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
296 /* Current location: if using file completion, return the current
297 * directory, from which file completion begins. More specifically,
298 * the cwd concatenated with all exact completions up to the last
299 * directory delimiter('/').
301 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
303 /* backing up: if cmpl_completion_matches returns NULL, you may query
304 * the index of the last completable character into cmpl_updated_text.
306 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
308 /* When the user selects a non-directory, call cmpl_completion_fullname
309 * to get the full name of the selected file.
311 static const gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
314 /* Directory operations. */
315 static CompletionDir* open_ref_dir (gchar* text_to_complete,
316 gchar** remaining_text,
317 CompletionState* cmpl_state);
318 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
319 static gboolean check_dir (gchar *dir_name,
321 gboolean *stat_subdirs);
323 static CompletionDir* open_dir (gchar* dir_name,
324 CompletionState* cmpl_state);
326 static CompletionDir* open_user_dir (const gchar* text_to_complete,
327 CompletionState *cmpl_state);
329 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
330 CompletionState *cmpl_state);
331 static CompletionDirSent* open_new_dir (gchar* dir_name,
333 gboolean stat_subdirs);
334 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
335 static gint correct_parent (CompletionDir* cmpl_dir,
338 static gchar* find_parent_dir_fullname (gchar* dirname);
340 static CompletionDir* attach_dir (CompletionDirSent* sent,
342 CompletionState *cmpl_state);
343 static void free_dir_sent (CompletionDirSent* sent);
344 static void free_dir (CompletionDir *dir);
345 static void prune_memory_usage(CompletionState *cmpl_state);
347 /* Completion operations */
349 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
350 CompletionState *cmpl_state);
352 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
353 static CompletionDir* find_completion_dir(gchar* text_to_complete,
354 gchar** remaining_text,
355 CompletionState* cmpl_state);
356 static PossibleCompletion* append_completion_text(gchar* text,
357 CompletionState* cmpl_state);
359 static gint get_pwdb(CompletionState* cmpl_state);
360 static gint compare_user_dir(const void* a, const void* b);
362 static gint first_diff_index(gchar* pat, gchar* text);
363 static gint compare_cmpl_dir(const void* a, const void* b);
364 static void update_cmpl(PossibleCompletion* poss,
365 CompletionState* cmpl_state);
367 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
368 static void gtk_file_selection_set_property (GObject *object,
372 static void gtk_file_selection_get_property (GObject *object,
376 static void gtk_file_selection_init (GtkFileSelection *filesel);
377 static void gtk_file_selection_finalize (GObject *object);
378 static void gtk_file_selection_destroy (GtkObject *object);
379 static void gtk_file_selection_map (GtkWidget *widget);
380 static gint gtk_file_selection_key_press (GtkWidget *widget,
383 static gint gtk_file_selection_insert_text (GtkWidget *widget,
384 const gchar *new_text,
385 gint new_text_length,
388 static void gtk_file_selection_update_fileops (GtkFileSelection *filesel);
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 * /cygdrive/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)
447 gchar newPath[MAX_PATH];
450 * Retrieve the current path
452 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
454 cygwin_conv_to_posix_path (path, newPath);
455 updated = (strcmp (path, newPath) != 0);
458 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
465 gtk_file_selection_get_type (void)
467 static GType file_selection_type = 0;
469 if (!file_selection_type)
471 static const GTypeInfo filesel_info =
473 sizeof (GtkFileSelectionClass),
474 NULL, /* base_init */
475 NULL, /* base_finalize */
476 (GClassInitFunc) gtk_file_selection_class_init,
477 NULL, /* class_finalize */
478 NULL, /* class_data */
479 sizeof (GtkFileSelection),
481 (GInstanceInitFunc) gtk_file_selection_init,
484 file_selection_type =
485 g_type_register_static (GTK_TYPE_DIALOG, "GtkFileSelection",
489 return file_selection_type;
493 gtk_file_selection_class_init (GtkFileSelectionClass *class)
495 GObjectClass *gobject_class;
496 GtkObjectClass *object_class;
497 GtkWidgetClass *widget_class;
499 gobject_class = (GObjectClass*) class;
500 object_class = (GtkObjectClass*) class;
501 widget_class = (GtkWidgetClass*) class;
503 parent_class = g_type_class_peek_parent (class);
505 gobject_class->finalize = gtk_file_selection_finalize;
506 gobject_class->set_property = gtk_file_selection_set_property;
507 gobject_class->get_property = gtk_file_selection_get_property;
509 g_object_class_install_property (gobject_class,
511 g_param_spec_string ("filename",
513 _("The currently selected filename"),
515 G_PARAM_READABLE | G_PARAM_WRITABLE));
516 g_object_class_install_property (gobject_class,
518 g_param_spec_boolean ("show_fileops",
519 _("Show file operations"),
520 _("Whether buttons for creating/manipulating files should be displayed"),
524 g_object_class_install_property (gobject_class,
525 PROP_SELECT_MULTIPLE,
526 g_param_spec_boolean ("select_multiple",
527 _("Select multiple"),
528 _("Whether to allow multiple files to be selected"),
532 object_class->destroy = gtk_file_selection_destroy;
533 widget_class->map = gtk_file_selection_map;
536 static void gtk_file_selection_set_property (GObject *object,
541 GtkFileSelection *filesel;
543 filesel = GTK_FILE_SELECTION (object);
548 gtk_file_selection_set_filename (filesel,
549 g_value_get_string (value));
551 case PROP_SHOW_FILEOPS:
552 if (g_value_get_boolean (value))
553 gtk_file_selection_show_fileop_buttons (filesel);
555 gtk_file_selection_hide_fileop_buttons (filesel);
557 case PROP_SELECT_MULTIPLE:
558 gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
566 static void gtk_file_selection_get_property (GObject *object,
571 GtkFileSelection *filesel;
573 filesel = GTK_FILE_SELECTION (object);
578 g_value_set_string (value,
579 gtk_file_selection_get_filename(filesel));
582 case PROP_SHOW_FILEOPS:
583 /* This is a little bit hacky, but doing otherwise would require
584 * adding a field to the object.
586 g_value_set_boolean (value, (filesel->fileop_c_dir &&
587 filesel->fileop_del_file &&
588 filesel->fileop_ren_file));
590 case PROP_SELECT_MULTIPLE:
591 g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
594 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
600 grab_default (GtkWidget *widget)
602 gtk_widget_grab_default (widget);
607 gtk_file_selection_init (GtkFileSelection *filesel)
609 GtkWidget *entry_vbox;
611 GtkWidget *list_hbox, *list_container;
612 GtkWidget *confirm_area;
613 GtkWidget *pulldown_hbox;
614 GtkWidget *scrolled_win;
620 GtkTreeViewColumn *column;
622 gtk_widget_push_composite_child ();
624 dialog = GTK_DIALOG (filesel);
626 filesel->cmpl_state = cmpl_init_state ();
628 /* The dialog-sized vertical box */
629 filesel->main_vbox = dialog->vbox;
630 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
632 /* The horizontal box containing create, rename etc. buttons */
633 filesel->button_area = gtk_hbutton_box_new ();
634 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
635 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
636 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
638 gtk_widget_show (filesel->button_area);
640 gtk_file_selection_show_fileop_buttons (filesel);
642 /* hbox for pulldown menu */
643 pulldown_hbox = gtk_hbox_new (TRUE, 5);
644 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
645 gtk_widget_show (pulldown_hbox);
648 filesel->history_pulldown = gtk_option_menu_new ();
649 gtk_widget_show (filesel->history_pulldown);
650 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
653 /* The horizontal box containing the directory and file listboxes */
655 spacer = gtk_hbox_new (FALSE, 0);
656 gtk_widget_set_size_request (spacer, -1, 5);
657 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
658 gtk_widget_show (spacer);
660 list_hbox = gtk_hbox_new (FALSE, 5);
661 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
662 gtk_widget_show (list_hbox);
664 list_container = g_object_new (GTK_TYPE_HPANED,
670 list_container = list_hbox;
672 spacer = gtk_hbox_new (FALSE, 0);
673 gtk_widget_set_size_request (spacer, -1, 5);
674 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
675 gtk_widget_show (spacer);
677 /* The directories list */
679 model = gtk_list_store_new (1, G_TYPE_STRING);
680 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
681 g_object_unref (model);
683 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
684 gtk_cell_renderer_text_new (),
687 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
688 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
689 gtk_widget_show (label);
690 gtk_tree_view_column_set_widget (column, label);
691 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
692 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
694 gtk_widget_set_size_request (filesel->dir_list,
695 DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
696 g_signal_connect (filesel->dir_list, "row_activated",
697 G_CALLBACK (gtk_file_selection_dir_activate), filesel);
699 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
701 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
702 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
703 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
704 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
705 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
706 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
707 if (GTK_IS_PANED (list_container))
708 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
710 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
711 gtk_widget_show (filesel->dir_list);
712 gtk_widget_show (scrolled_win);
715 model = gtk_list_store_new (1, G_TYPE_STRING);
716 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
717 g_object_unref (model);
719 column = gtk_tree_view_column_new_with_attributes (_("Files"),
720 gtk_cell_renderer_text_new (),
723 label = gtk_label_new_with_mnemonic (_("_Files"));
724 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
725 gtk_widget_show (label);
726 gtk_tree_view_column_set_widget (column, label);
727 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
728 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
730 gtk_widget_set_size_request (filesel->file_list,
731 FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
732 g_signal_connect (filesel->file_list, "row_activated",
733 G_CALLBACK (gtk_file_selection_file_activate), filesel);
734 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
735 G_CALLBACK (gtk_file_selection_file_changed), filesel);
737 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
739 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
740 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
741 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
742 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
743 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
744 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
745 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
746 gtk_widget_show (filesel->file_list);
747 gtk_widget_show (scrolled_win);
749 /* action area for packing buttons into. */
750 filesel->action_area = gtk_hbox_new (TRUE, 0);
751 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
753 gtk_widget_show (filesel->action_area);
755 /* The OK/Cancel button area */
756 confirm_area = dialog->action_area;
758 /* The Cancel button */
759 filesel->cancel_button = gtk_dialog_add_button (dialog,
761 GTK_RESPONSE_CANCEL);
763 filesel->ok_button = gtk_dialog_add_button (dialog,
767 gtk_widget_grab_default (filesel->ok_button);
769 /* The selection entry widget */
770 entry_vbox = gtk_vbox_new (FALSE, 2);
771 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
772 gtk_widget_show (entry_vbox);
774 eventbox = gtk_event_box_new ();
775 filesel->selection_text = label = gtk_label_new ("");
776 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
777 gtk_container_add (GTK_CONTAINER (eventbox), label);
778 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
779 gtk_widget_show (label);
780 gtk_widget_show (eventbox);
782 filesel->selection_entry = gtk_entry_new ();
783 g_signal_connect (filesel->selection_entry, "key_press_event",
784 G_CALLBACK (gtk_file_selection_key_press), filesel);
785 g_signal_connect (filesel->selection_entry, "insert_text",
786 G_CALLBACK (gtk_file_selection_insert_text), NULL);
787 g_signal_connect_swapped (filesel->selection_entry, "changed",
788 G_CALLBACK (gtk_file_selection_update_fileops), filesel);
789 g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
790 G_CALLBACK (grab_default),
792 g_signal_connect_swapped (filesel->selection_entry, "activate",
793 G_CALLBACK (gtk_button_clicked),
796 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
797 gtk_widget_show (filesel->selection_entry);
799 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
800 filesel->selection_entry);
802 if (!cmpl_state_okay (filesel->cmpl_state))
806 sprintf (err_buf, _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
808 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
812 gtk_file_selection_populate (filesel, "", FALSE, TRUE);
815 gtk_widget_grab_focus (filesel->selection_entry);
817 gtk_widget_pop_composite_child ();
821 uri_list_extract_first_uri (const gchar* uri_list)
825 g_return_val_if_fail (uri_list != NULL, NULL);
828 /* We don't actually try to validate the URI according to RFC
829 * 2396, or even check for allowed characters - we just ignore
830 * comments and trim whitespace off the ends. We also
831 * allow LF delimination as well as the specified CRLF.
833 * We do allow comments like specified in RFC 2483.
839 while (g_ascii_isspace(*p))
843 while (*q && (*q != '\n') && (*q != '\r'))
849 while (q > p && g_ascii_isspace (*q))
853 return g_strndup (p, q - p + 1);
856 p = strchr (p, '\n');
864 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
868 if (response_id == GTK_RESPONSE_YES)
870 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
872 gtk_file_selection_set_filename (fs, filename);
875 gtk_widget_destroy (dialog);
880 filenames_dropped (GtkWidget *widget,
881 GdkDragContext *context,
884 GtkSelectionData *selection_data,
889 char *filename = NULL;
891 char this_hostname[257];
893 GError *error = NULL;
895 if (!selection_data->data)
898 uri = uri_list_extract_first_uri ((char *)selection_data->data);
903 filename = g_filename_from_uri (uri, &hostname, &error);
908 g_warning ("Error getting dropped filename: %s\n",
910 g_error_free (error);
914 res = gethostname (this_hostname, 256);
915 this_hostname[256] = 0;
917 if ((hostname == NULL) ||
918 (res == 0 && strcmp (hostname, this_hostname) == 0) ||
919 (strcmp (hostname, "localhost") == 0))
920 gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
925 gchar *filename_utf8;
927 /* Conversion back to UTF-8 should always succeed for the result
928 * of g_filename_from_uri()
930 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
931 g_assert (filename_utf8);
933 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
934 GTK_DIALOG_DESTROY_WITH_PARENT,
935 GTK_MESSAGE_QUESTION,
937 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
938 "Are you sure that you want to select it?"), filename_utf8, hostname);
939 g_free (filename_utf8);
941 g_object_set_data_full (G_OBJECT (dialog), "gtk-fs-dnd-filename", g_strdup (filename), g_free);
943 g_signal_connect_data (dialog, "response",
944 (GCallback) dnd_really_drop,
947 gtk_widget_show (dialog);
965 filenames_drag_get (GtkWidget *widget,
966 GdkDragContext *context,
967 GtkSelectionData *selection_data,
970 GtkFileSelection *filesel)
978 file = gtk_file_selection_get_filename (filesel);
982 if (info == TARGET_URILIST)
984 res = gethostname (hostname, 256);
987 uri_list = g_filename_to_uri (file, (!res)?hostname:NULL, &error);
990 g_warning ("Error getting filename: %s\n",
992 g_error_free (error);
996 gtk_selection_data_set (selection_data,
997 selection_data->target, 8,
998 (void *)uri_list, strlen((char *)uri_list));
1003 gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
1004 g_assert (filename_utf8);
1005 gtk_selection_data_set_text (selection_data, filename_utf8, -1);
1006 g_free (filename_utf8);
1012 file_selection_setup_dnd (GtkFileSelection *filesel)
1014 GtkWidget *eventbox;
1015 static const GtkTargetEntry drop_types[] = {
1016 { "text/uri-list", 0, TARGET_URILIST}
1018 static gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
1019 static const GtkTargetEntry drag_types[] = {
1020 { "text/uri-list", 0, TARGET_URILIST},
1021 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
1024 { "COMPOUND_TEXT", 0, 0 }
1026 static gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
1028 gtk_drag_dest_set (GTK_WIDGET (filesel),
1029 GTK_DEST_DEFAULT_ALL,
1030 drop_types, n_drop_types,
1033 g_signal_connect (filesel, "drag_data_received",
1034 G_CALLBACK (filenames_dropped), NULL);
1036 eventbox = gtk_widget_get_parent (filesel->selection_text);
1037 gtk_drag_source_set (eventbox,
1039 drag_types, n_drag_types,
1042 g_signal_connect (eventbox, "drag_data_get",
1043 G_CALLBACK (filenames_drag_get), filesel);
1047 gtk_file_selection_new (const gchar *title)
1049 GtkFileSelection *filesel;
1051 filesel = g_object_new (GTK_TYPE_FILE_SELECTION, NULL);
1052 gtk_window_set_title (GTK_WINDOW (filesel), title);
1053 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1055 file_selection_setup_dnd (filesel);
1057 return GTK_WIDGET (filesel);
1061 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1063 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1065 /* delete, create directory, and rename */
1066 if (!filesel->fileop_c_dir)
1068 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1069 g_signal_connect (filesel->fileop_c_dir, "clicked",
1070 G_CALLBACK (gtk_file_selection_create_dir),
1072 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1073 filesel->fileop_c_dir, TRUE, TRUE, 0);
1074 gtk_widget_show (filesel->fileop_c_dir);
1077 if (!filesel->fileop_del_file)
1079 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1080 g_signal_connect (filesel->fileop_del_file, "clicked",
1081 G_CALLBACK (gtk_file_selection_delete_file),
1083 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1084 filesel->fileop_del_file, TRUE, TRUE, 0);
1085 gtk_widget_show (filesel->fileop_del_file);
1088 if (!filesel->fileop_ren_file)
1090 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1091 g_signal_connect (filesel->fileop_ren_file, "clicked",
1092 G_CALLBACK (gtk_file_selection_rename_file),
1094 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1095 filesel->fileop_ren_file, TRUE, TRUE, 0);
1096 gtk_widget_show (filesel->fileop_ren_file);
1099 gtk_file_selection_update_fileops (filesel);
1101 g_object_notify (G_OBJECT (filesel), "show_fileops");
1105 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1107 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1109 if (filesel->fileop_ren_file)
1111 gtk_widget_destroy (filesel->fileop_ren_file);
1112 filesel->fileop_ren_file = NULL;
1115 if (filesel->fileop_del_file)
1117 gtk_widget_destroy (filesel->fileop_del_file);
1118 filesel->fileop_del_file = NULL;
1121 if (filesel->fileop_c_dir)
1123 gtk_widget_destroy (filesel->fileop_c_dir);
1124 filesel->fileop_c_dir = NULL;
1126 g_object_notify (G_OBJECT (filesel), "show_fileops");
1132 * gtk_file_selection_set_filename:
1133 * @filesel: a #GtkFileSelection.
1134 * @filename: a string to set as the default file name.
1136 * Sets a default path for the file requestor. If @filename includes a
1137 * directory path, then the requestor will open with that path as its
1138 * current working directory.
1140 * The encoding of @filename is the on-disk encoding, which
1141 * may not be UTF-8. See g_filename_from_utf8().
1144 gtk_file_selection_set_filename (GtkFileSelection *filesel,
1145 const gchar *filename)
1148 const char *name, *last_slash;
1149 char *filename_utf8;
1151 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1152 g_return_if_fail (filename != NULL);
1154 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1155 g_return_if_fail (filename_utf8 != NULL);
1157 last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1161 buf = g_strdup ("");
1162 name = filename_utf8;
1166 buf = g_strdup (filename_utf8);
1167 buf[last_slash - filename_utf8 + 1] = 0;
1168 name = last_slash + 1;
1171 gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1173 if (filesel->selection_entry)
1174 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1176 g_object_notify (G_OBJECT (filesel), "filename");
1178 g_free (filename_utf8);
1182 * gtk_file_selection_get_filename:
1183 * @filesel: a #GtkFileSelection
1185 * This function returns the selected filename in the on-disk encoding
1186 * (see g_filename_from_utf8()), which may or may not be the same as that
1187 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1188 * The returned string points to a statically allocated buffer and
1189 * should be copied if you plan to keep it around.
1191 * If no file is selected then the selected directory path is returned.
1193 * Return value: currently-selected filename in the on-disk encoding.
1195 G_CONST_RETURN gchar*
1196 gtk_file_selection_get_filename (GtkFileSelection *filesel)
1198 static const gchar nothing[2] = "";
1199 static gchar something[MAXPATHLEN*2];
1203 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1205 #ifdef G_WITH_CYGWIN
1206 translate_win32_path (filesel);
1208 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1211 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
1214 strncpy (something, sys_filename, sizeof (something));
1215 g_free (sys_filename);
1223 gtk_file_selection_complete (GtkFileSelection *filesel,
1224 const gchar *pattern)
1226 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1227 g_return_if_fail (pattern != NULL);
1229 if (filesel->selection_entry)
1230 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1231 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1235 gtk_file_selection_destroy (GtkObject *object)
1237 GtkFileSelection *filesel;
1239 HistoryCallbackArg *callback_arg;
1241 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1243 filesel = GTK_FILE_SELECTION (object);
1245 if (filesel->fileop_dialog)
1247 gtk_widget_destroy (filesel->fileop_dialog);
1248 filesel->fileop_dialog = NULL;
1251 if (filesel->history_list)
1253 list = filesel->history_list;
1256 callback_arg = list->data;
1257 g_free (callback_arg->directory);
1258 g_free (callback_arg);
1261 g_list_free (filesel->history_list);
1262 filesel->history_list = NULL;
1265 if (filesel->cmpl_state)
1267 cmpl_free_state (filesel->cmpl_state);
1268 filesel->cmpl_state = NULL;
1271 if (filesel->selected_names)
1273 free_selected_names (filesel->selected_names);
1274 filesel->selected_names = NULL;
1277 if (filesel->last_selected)
1279 g_free (filesel->last_selected);
1280 filesel->last_selected = NULL;
1283 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1287 gtk_file_selection_map (GtkWidget *widget)
1289 GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1291 /* Refresh the contents */
1292 gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1294 GTK_WIDGET_CLASS (parent_class)->map (widget);
1298 gtk_file_selection_finalize (GObject *object)
1300 GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1302 g_free (filesel->fileop_file);
1304 G_OBJECT_CLASS (parent_class)->finalize (object);
1307 /* Begin file operations callbacks */
1310 gtk_file_selection_fileop_error (GtkFileSelection *fs,
1311 gchar *error_message)
1315 g_return_if_fail (error_message != NULL);
1318 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1319 GTK_DIALOG_DESTROY_WITH_PARENT,
1322 "%s", error_message);
1324 /* yes, we free it */
1325 g_free (error_message);
1327 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1329 g_signal_connect_swapped (dialog, "response",
1330 G_CALLBACK (gtk_widget_destroy),
1333 gtk_widget_show (dialog);
1337 gtk_file_selection_fileop_destroy (GtkWidget *widget,
1340 GtkFileSelection *fs = data;
1342 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1344 fs->fileop_dialog = NULL;
1348 entry_is_empty (GtkEntry *entry)
1350 const gchar *text = gtk_entry_get_text (entry);
1352 return *text == '\0';
1356 gtk_file_selection_fileop_entry_changed (GtkEntry *entry,
1359 gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1363 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1366 GtkFileSelection *fs = data;
1367 const gchar *dirname;
1370 gchar *sys_full_path;
1372 GError *error = NULL;
1373 CompletionState *cmpl_state;
1375 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1377 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1378 cmpl_state = (CompletionState*) fs->cmpl_state;
1379 path = cmpl_reference_position (cmpl_state);
1381 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1382 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1385 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1386 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1388 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname, error->message,
1389 _("You probably used symbols not allowed in filenames."));
1390 gtk_file_selection_fileop_error (fs, buf);
1391 g_error_free (error);
1395 if (mkdir (sys_full_path, 0755) < 0)
1397 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname,
1398 g_strerror (errno));
1399 gtk_file_selection_fileop_error (fs, buf);
1404 g_free (sys_full_path);
1406 gtk_widget_destroy (fs->fileop_dialog);
1407 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1411 gtk_file_selection_create_dir (GtkWidget *widget,
1414 GtkFileSelection *fs = data;
1420 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1422 if (fs->fileop_dialog)
1426 dialog = gtk_dialog_new ();
1427 fs->fileop_dialog = dialog;
1428 g_signal_connect (dialog, "destroy",
1429 G_CALLBACK (gtk_file_selection_fileop_destroy),
1431 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1432 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1433 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1435 /* If file dialog is grabbed, grab option dialog */
1436 /* When option dialog is closed, file dialog will be grabbed again */
1437 if (GTK_WINDOW (fs)->modal)
1438 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1440 vbox = gtk_vbox_new (FALSE, 0);
1441 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1442 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1444 gtk_widget_show( vbox);
1446 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1447 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1448 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1449 gtk_widget_show (label);
1451 /* The directory entry widget */
1452 fs->fileop_entry = gtk_entry_new ();
1453 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1454 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1456 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1457 gtk_widget_show (fs->fileop_entry);
1460 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1461 g_signal_connect_swapped (button, "clicked",
1462 G_CALLBACK (gtk_widget_destroy),
1464 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1465 button, TRUE, TRUE, 0);
1466 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1467 gtk_widget_grab_default (button);
1468 gtk_widget_show (button);
1470 gtk_widget_grab_focus (fs->fileop_entry);
1472 button = gtk_button_new_with_mnemonic (_("C_reate"));
1473 gtk_widget_set_sensitive (button, FALSE);
1474 g_signal_connect (button, "clicked",
1475 G_CALLBACK (gtk_file_selection_create_dir_confirmed),
1477 g_signal_connect (fs->fileop_entry, "changed",
1478 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1481 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1482 button, TRUE, TRUE, 0);
1483 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1484 gtk_widget_show (button);
1486 gtk_widget_show (dialog);
1490 gtk_file_selection_delete_file_response (GtkDialog *dialog,
1494 GtkFileSelection *fs = data;
1495 CompletionState *cmpl_state;
1498 gchar *sys_full_path;
1499 GError *error = NULL;
1502 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1504 if (response_id != GTK_RESPONSE_OK)
1506 gtk_widget_destroy (GTK_WIDGET (dialog));
1510 cmpl_state = (CompletionState*) fs->cmpl_state;
1511 path = cmpl_reference_position (cmpl_state);
1513 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1514 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1517 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1518 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1521 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1522 fs->fileop_file, error->message,
1523 _("It probably contains symbols not allowed in filenames."));
1525 gtk_file_selection_fileop_error (fs, buf);
1526 g_error_free (error);
1530 if (unlink (sys_full_path) < 0)
1532 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1533 fs->fileop_file, g_strerror (errno));
1534 gtk_file_selection_fileop_error (fs, buf);
1539 g_free (sys_full_path);
1541 gtk_widget_destroy (fs->fileop_dialog);
1542 gtk_file_selection_populate (fs, "", FALSE, TRUE);
1546 gtk_file_selection_delete_file (GtkWidget *widget,
1549 GtkFileSelection *fs = data;
1551 const gchar *filename;
1553 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1555 if (fs->fileop_dialog)
1558 #ifdef G_WITH_CYGWIN
1559 translate_win32_path (fs);
1562 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1563 if (strlen (filename) < 1)
1566 g_free (fs->fileop_file);
1567 fs->fileop_file = g_strdup (filename);
1570 fs->fileop_dialog = dialog =
1571 gtk_message_dialog_new (GTK_WINDOW (fs),
1572 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1573 GTK_MESSAGE_QUESTION,
1575 _("Really delete file \"%s\" ?"), filename);
1577 g_signal_connect (dialog, "destroy",
1578 G_CALLBACK (gtk_file_selection_fileop_destroy),
1580 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1581 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1584 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1585 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1586 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1589 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1591 g_signal_connect (dialog, "response",
1592 G_CALLBACK (gtk_file_selection_delete_file_response),
1595 gtk_widget_show (dialog);
1599 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1602 GtkFileSelection *fs = data;
1606 gchar *new_filename;
1607 gchar *old_filename;
1608 gchar *sys_new_filename;
1609 gchar *sys_old_filename;
1610 CompletionState *cmpl_state;
1611 GError *error = NULL;
1613 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1615 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1616 cmpl_state = (CompletionState*) fs->cmpl_state;
1617 path = cmpl_reference_position (cmpl_state);
1619 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1620 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1622 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1625 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1626 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1628 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1629 new_filename, error->message,
1630 _("You probably used symbols not allowed in filenames."));
1631 gtk_file_selection_fileop_error (fs, buf);
1632 g_error_free (error);
1636 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1639 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1640 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1642 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1643 old_filename, error->message,
1644 _("It probably contains symbols not allowed in filenames."));
1645 gtk_file_selection_fileop_error (fs, buf);
1646 g_error_free (error);
1650 if (rename (sys_old_filename, sys_new_filename) < 0)
1652 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1653 sys_old_filename, sys_new_filename,
1654 g_strerror (errno));
1655 gtk_file_selection_fileop_error (fs, buf);
1659 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1660 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1663 g_free (sys_old_filename);
1666 g_free (new_filename);
1667 g_free (old_filename);
1668 g_free (sys_new_filename);
1670 gtk_widget_destroy (fs->fileop_dialog);
1674 gtk_file_selection_rename_file (GtkWidget *widget,
1677 GtkFileSelection *fs = data;
1684 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1686 if (fs->fileop_dialog)
1689 g_free (fs->fileop_file);
1690 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1691 if (strlen (fs->fileop_file) < 1)
1695 fs->fileop_dialog = dialog = gtk_dialog_new ();
1696 g_signal_connect (dialog, "destroy",
1697 G_CALLBACK (gtk_file_selection_fileop_destroy),
1699 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1700 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1701 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1703 /* If file dialog is grabbed, grab option dialog */
1704 /* When option dialog closed, file dialog will be grabbed again */
1705 if (GTK_WINDOW (fs)->modal)
1706 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1708 vbox = gtk_vbox_new (FALSE, 0);
1709 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1710 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1712 gtk_widget_show(vbox);
1714 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1715 label = gtk_label_new (buf);
1716 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1717 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1718 gtk_widget_show (label);
1721 /* New filename entry */
1722 fs->fileop_entry = gtk_entry_new ();
1723 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1725 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1726 gtk_widget_show (fs->fileop_entry);
1728 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1729 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1730 0, strlen (fs->fileop_file));
1733 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1734 g_signal_connect_swapped (button, "clicked",
1735 G_CALLBACK (gtk_widget_destroy),
1737 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1738 button, TRUE, TRUE, 0);
1739 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1740 gtk_widget_grab_default (button);
1741 gtk_widget_show (button);
1743 gtk_widget_grab_focus (fs->fileop_entry);
1745 button = gtk_button_new_with_mnemonic (_("_Rename"));
1746 g_signal_connect (button, "clicked",
1747 G_CALLBACK (gtk_file_selection_rename_file_confirmed),
1749 g_signal_connect (fs->fileop_entry, "changed",
1750 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1753 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1754 button, TRUE, TRUE, 0);
1755 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1756 gtk_widget_show (button);
1758 gtk_widget_show (dialog);
1762 gtk_file_selection_insert_text (GtkWidget *widget,
1763 const gchar *new_text,
1764 gint new_text_length,
1770 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1774 gdk_display_beep (gtk_widget_get_display (widget));
1775 g_signal_stop_emission_by_name (widget, "insert_text");
1785 gtk_file_selection_update_fileops (GtkFileSelection *fs)
1789 if (!fs->selection_entry)
1792 sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1794 if (fs->fileop_del_file)
1795 gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1797 if (fs->fileop_ren_file)
1798 gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1802 gtk_file_selection_key_press (GtkWidget *widget,
1806 GtkFileSelection *fs;
1809 g_return_val_if_fail (widget != NULL, FALSE);
1810 g_return_val_if_fail (event != NULL, FALSE);
1812 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1813 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1815 fs = GTK_FILE_SELECTION (user_data);
1816 #ifdef G_WITH_CYGWIN
1817 translate_win32_path (fs);
1819 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1821 gtk_file_selection_populate (fs, text, TRUE, TRUE);
1832 gtk_file_selection_history_callback (GtkWidget *widget,
1835 GtkFileSelection *fs = data;
1836 HistoryCallbackArg *callback_arg;
1839 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1841 list = fs->history_list;
1844 callback_arg = list->data;
1846 if (callback_arg->menu_item == widget)
1848 gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1857 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1858 gchar *current_directory)
1860 HistoryCallbackArg *callback_arg;
1861 GtkWidget *menu_item;
1867 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1868 g_return_if_fail (current_directory != NULL);
1870 list = fs->history_list;
1872 if (fs->history_menu)
1875 callback_arg = list->data;
1876 g_free (callback_arg->directory);
1877 g_free (callback_arg);
1880 g_list_free (fs->history_list);
1881 fs->history_list = NULL;
1883 gtk_widget_destroy (fs->history_menu);
1886 fs->history_menu = gtk_menu_new ();
1888 current_dir = g_strdup (current_directory);
1890 dir_len = strlen (current_dir);
1892 for (i = dir_len; i >= 0; i--)
1894 /* the i == dir_len is to catch the full path for the first
1896 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1898 /* another small hack to catch the full path */
1900 current_dir[i + 1] = '\0';
1901 #ifdef G_WITH_CYGWIN
1902 if (!strcmp (current_dir, "//"))
1905 menu_item = gtk_menu_item_new_with_label (current_dir);
1907 callback_arg = g_new (HistoryCallbackArg, 1);
1908 callback_arg->menu_item = menu_item;
1910 /* since the autocompletion gets confused if you don't
1911 * supply a trailing '/' on a dir entry, set the full
1912 * (current) path to "" which just refreshes the filesel */
1915 callback_arg->directory = g_strdup ("");
1919 callback_arg->directory = g_strdup (current_dir);
1922 fs->history_list = g_list_append (fs->history_list, callback_arg);
1924 g_signal_connect (menu_item, "activate",
1925 G_CALLBACK (gtk_file_selection_history_callback),
1927 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1928 gtk_widget_show (menu_item);
1932 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1934 g_free (current_dir);
1938 get_real_filename (gchar *filename,
1941 #ifdef G_WITH_CYGWIN
1942 /* Check to see if the selection was a drive selector */
1943 if (isalpha (filename[0]) && (filename[1] == ':'))
1945 gchar temp_filename[MAX_PATH];
1948 cygwin_conv_to_posix_path (filename, temp_filename);
1950 /* we need trailing '/'. */
1951 len = strlen (temp_filename);
1952 if (len > 0 && temp_filename[len-1] != '/')
1954 temp_filename[len] = '/';
1955 temp_filename[len+1] = '\0';
1961 return g_strdup (temp_filename);
1963 #endif /* G_WITH_CYGWIN */
1968 gtk_file_selection_file_activate (GtkTreeView *tree_view,
1970 GtkTreeViewColumn *column,
1973 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1974 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1978 gtk_tree_model_get_iter (model, &iter, path);
1979 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
1980 filename = get_real_filename (filename, TRUE);
1981 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1982 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1988 gtk_file_selection_dir_activate (GtkTreeView *tree_view,
1990 GtkTreeViewColumn *column,
1993 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1994 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1998 gtk_tree_model_get_iter (model, &iter, path);
1999 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
2000 filename = get_real_filename (filename, TRUE);
2001 gtk_file_selection_populate (fs, filename, FALSE, FALSE);
2005 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2008 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
2012 char formatBuffer[128];
2015 /* Get the drives string */
2016 GetLogicalDriveStrings (sizeof (buffer), buffer);
2018 /* Add the drives as necessary */
2020 while (*textPtr != '\0')
2022 /* Ignore floppies (?) */
2023 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
2025 /* Build the actual displayable string */
2026 sprintf (formatBuffer, "%c:\\", toupper (textPtr[0]));
2028 /* Add to the list */
2029 gtk_list_store_append (model, &iter);
2030 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
2032 textPtr += (strlen (textPtr) + 1);
2038 escape_underscores (const gchar *str)
2040 GString *result = g_string_new (NULL);
2044 g_string_append_c (result, '_');
2046 g_string_append_c (result, *str);
2050 return g_string_free (result, FALSE);
2054 gtk_file_selection_populate (GtkFileSelection *fs,
2056 gboolean try_complete,
2057 gboolean reset_entry)
2059 CompletionState *cmpl_state;
2060 PossibleCompletion* poss;
2062 GtkListStore *dir_model;
2063 GtkListStore *file_model;
2065 gchar* rem_path = rel_path;
2067 gint did_recurse = FALSE;
2068 gint possible_count = 0;
2069 gint selection_index = -1;
2071 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
2073 cmpl_state = (CompletionState*) fs->cmpl_state;
2074 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2076 if (!cmpl_state_okay (cmpl_state))
2078 /* Something went wrong. */
2079 gtk_file_selection_abort (fs);
2083 g_assert (cmpl_state->reference_dir);
2085 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2086 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2088 gtk_list_store_clear (dir_model);
2089 gtk_list_store_clear (file_model);
2091 /* Set the dir list to include ./ and ../ */
2092 gtk_list_store_append (dir_model, &iter);
2093 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2094 gtk_list_store_append (dir_model, &iter);
2095 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2099 if (cmpl_is_a_completion (poss))
2101 possible_count += 1;
2103 filename = cmpl_this_completion (poss);
2105 if (cmpl_is_directory (poss))
2107 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2108 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2110 gtk_list_store_append (dir_model, &iter);
2111 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2116 gtk_list_store_append (file_model, &iter);
2117 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2121 poss = cmpl_next_completion (cmpl_state);
2124 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2125 /* For Windows, add drives as potential selections */
2126 win32_gtk_add_drives_to_dir_list (dir_model);
2129 /* File lists are set. */
2131 g_assert (cmpl_state->reference_dir);
2136 /* User is trying to complete filenames, so advance the user's input
2137 * string to the updated_text, which is the common leading substring
2138 * of all possible completions, and if its a directory attempt
2139 * attempt completions in it. */
2141 if (cmpl_updated_text (cmpl_state)[0])
2144 if (cmpl_updated_dir (cmpl_state))
2146 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2150 gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2156 if (fs->selection_entry)
2157 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2158 cmpl_updated_text (cmpl_state));
2163 selection_index = cmpl_last_valid_char (cmpl_state) -
2164 (strlen (rel_path) - strlen (rem_path));
2165 if (fs->selection_entry)
2166 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2169 else if (reset_entry)
2171 if (fs->selection_entry)
2172 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2177 if (fs->selection_entry)
2178 gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry),
2181 if (fs->selection_entry)
2183 char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2184 sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2187 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2191 if (fs->history_pulldown)
2193 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2200 gtk_file_selection_abort (GtkFileSelection *fs)
2204 sprintf (err_buf, _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2206 /* BEEP gdk_beep(); */
2208 if (fs->selection_entry)
2209 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2213 * gtk_file_selection_set_select_multiple:
2214 * @filesel: a #GtkFileSelection
2215 * @select_multiple: whether or not the user is allowed to select multiple
2216 * files in the file list.
2218 * Sets whether the user is allowed to select multiple files in the file list.
2219 * Use gtk_file_selection_get_selections () to get the list of selected files.
2222 gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2223 gboolean select_multiple)
2225 GtkTreeSelection *sel;
2226 GtkSelectionMode mode;
2228 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2230 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2232 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2234 if (mode != gtk_tree_selection_get_mode (sel))
2236 gtk_tree_selection_set_mode (sel, mode);
2238 g_object_notify (G_OBJECT (filesel), "select-multiple");
2243 * gtk_file_selection_get_select_multiple:
2244 * @filesel: a #GtkFileSelection
2246 * Determines whether or not the user is allowed to select multiple files in
2247 * the file list. See gtk_file_selection_set_select_multiple().
2249 * Return value: %TRUE if the user is allowed to select multiple files in the
2253 gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2255 GtkTreeSelection *sel;
2257 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2259 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2260 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2264 multiple_changed_foreach (GtkTreeModel *model,
2269 GPtrArray *names = data;
2272 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2274 g_ptr_array_add (names, filename);
2278 free_selected_names (GPtrArray *names)
2282 for (i = 0; i < names->len; i++)
2283 g_free (g_ptr_array_index (names, i));
2285 g_ptr_array_free (names, TRUE);
2289 gtk_file_selection_file_changed (GtkTreeSelection *selection,
2292 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2293 GPtrArray *new_names;
2298 new_names = g_ptr_array_sized_new (8);
2300 gtk_tree_selection_selected_foreach (selection,
2301 multiple_changed_foreach,
2304 /* nothing selected */
2305 if (new_names->len == 0)
2307 g_ptr_array_free (new_names, TRUE);
2309 if (fs->selected_names != NULL)
2311 free_selected_names (fs->selected_names);
2312 fs->selected_names = NULL;
2315 goto maybe_clear_entry;
2318 if (new_names->len != 1)
2320 GPtrArray *old_names = fs->selected_names;
2322 if (old_names != NULL)
2324 /* A common case is selecting a range of files from top to bottom,
2325 * so quickly check for that to avoid looping over the entire list
2327 if (compare_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2328 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2329 index = new_names->len - 1;
2332 gint i = 0, j = 0, cmp;
2334 /* do a quick diff, stopping at the first file not in the
2337 while (i < old_names->len && j < new_names->len)
2339 cmp = compare_filenames (g_ptr_array_index (old_names, i),
2340 g_ptr_array_index (new_names, j));
2357 /* we ran off the end of the old list */
2358 if (index == -1 && i < new_names->len)
2364 /* A phantom anchor still exists at the point where the last item
2365 * was selected, which is used for subsequent range selections.
2366 * So search up from there.
2368 if (fs->last_selected &&
2369 compare_filenames (fs->last_selected,
2370 g_ptr_array_index (new_names, 0)) == 0)
2371 index = new_names->len - 1;
2379 if (fs->selected_names != NULL)
2380 free_selected_names (fs->selected_names);
2382 fs->selected_names = new_names;
2386 if (fs->last_selected != NULL)
2387 g_free (fs->last_selected);
2389 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2390 filename = get_real_filename (fs->last_selected, FALSE);
2392 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2394 if (filename != fs->last_selected)
2402 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2403 if ((entry != NULL) && (fs->last_selected != NULL) &&
2404 (compare_filenames (entry, fs->last_selected) == 0))
2405 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2409 * gtk_file_selection_get_selections:
2410 * @filesel: a #GtkFileSelection
2412 * Retrieves the list of file selections the user has made in the dialog box.
2413 * This function is intended for use when the user can select multiple files
2414 * in the file list. The first file in the list is equivalent to what
2415 * gtk_file_selection_get_filename() would return.
2417 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2418 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2419 * g_filename_to_utf8() on each string.
2421 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2422 * g_strfreev() to free it.
2425 gtk_file_selection_get_selections (GtkFileSelection *filesel)
2429 gchar *filename, *dirname;
2430 gchar *current, *buf;
2432 gboolean unselected_entry;
2434 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2436 filename = g_strdup (gtk_file_selection_get_filename (filesel));
2438 if (strlen (filename) == 0)
2444 names = filesel->selected_names;
2447 selections = g_new (gchar *, names->len + 2);
2449 selections = g_new (gchar *, 2);
2452 unselected_entry = TRUE;
2456 dirname = g_path_get_dirname (filename);
2458 for (i = 0; i < names->len; i++)
2460 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2462 current = g_build_filename (dirname, buf, NULL);
2465 selections[count++] = current;
2467 if (unselected_entry && compare_filenames (current, filename) == 0)
2468 unselected_entry = FALSE;
2474 if (unselected_entry)
2475 selections[count++] = filename;
2479 selections[count] = NULL;
2484 /**********************************************************************/
2485 /* External Interface */
2486 /**********************************************************************/
2488 /* The four completion state selectors
2491 cmpl_updated_text (CompletionState *cmpl_state)
2493 return cmpl_state->updated_text;
2497 cmpl_updated_dir (CompletionState *cmpl_state)
2499 return cmpl_state->re_complete;
2503 cmpl_reference_position (CompletionState *cmpl_state)
2505 return cmpl_state->reference_dir->fullname;
2509 cmpl_last_valid_char (CompletionState *cmpl_state)
2511 return cmpl_state->last_valid_char;
2515 cmpl_completion_fullname (const gchar *text,
2516 CompletionState *cmpl_state)
2518 static const char nothing[2] = "";
2520 if (!cmpl_state_okay (cmpl_state))
2524 else if (g_path_is_absolute (text))
2526 strcpy (cmpl_state->updated_text, text);
2529 else if (text[0] == '~')
2534 dir = open_user_dir (text, cmpl_state);
2538 /* spencer says just return ~something, so
2539 * for now just do it. */
2540 strcpy (cmpl_state->updated_text, text);
2545 strcpy (cmpl_state->updated_text, dir->fullname);
2547 slash = strchr (text, G_DIR_SEPARATOR);
2550 strcat (cmpl_state->updated_text, slash);
2556 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
2557 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
2558 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
2559 strcat (cmpl_state->updated_text, text);
2562 return cmpl_state->updated_text;
2565 /* The three completion selectors
2568 cmpl_this_completion (PossibleCompletion* pc)
2574 cmpl_is_directory (PossibleCompletion* pc)
2576 return pc->is_directory;
2580 cmpl_is_a_completion (PossibleCompletion* pc)
2582 return pc->is_a_completion;
2585 /**********************************************************************/
2586 /* Construction, deletion */
2587 /**********************************************************************/
2589 static CompletionState*
2590 cmpl_init_state (void)
2592 gchar *sys_getcwd_buf;
2594 CompletionState *new_state;
2596 new_state = g_new (CompletionState, 1);
2598 /* g_get_current_dir() returns a string in the "system" charset */
2599 sys_getcwd_buf = g_get_current_dir ();
2600 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
2601 g_free (sys_getcwd_buf);
2605 new_state->reference_dir = NULL;
2606 new_state->completion_dir = NULL;
2607 new_state->active_completion_dir = NULL;
2608 new_state->directory_storage = NULL;
2609 new_state->directory_sent_storage = NULL;
2610 new_state->last_valid_char = 0;
2611 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2612 new_state->updated_text_alloc = MAXPATHLEN;
2613 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2614 new_state->the_completion.text_alloc = MAXPATHLEN;
2615 new_state->user_dir_name_buffer = NULL;
2616 new_state->user_directories = NULL;
2618 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2620 if (!new_state->reference_dir)
2622 /* Directories changing from underneath us, grumble */
2623 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2632 cmpl_free_dir_list (GList* dp0)
2638 free_dir (dp->data);
2646 cmpl_free_dir_sent_list (GList* dp0)
2652 free_dir_sent (dp->data);
2660 cmpl_free_state (CompletionState* cmpl_state)
2662 g_return_if_fail (cmpl_state != NULL);
2664 cmpl_free_dir_list (cmpl_state->directory_storage);
2665 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2667 if (cmpl_state->user_dir_name_buffer)
2668 g_free (cmpl_state->user_dir_name_buffer);
2669 if (cmpl_state->user_directories)
2670 g_free (cmpl_state->user_directories);
2671 if (cmpl_state->the_completion.text)
2672 g_free (cmpl_state->the_completion.text);
2673 if (cmpl_state->updated_text)
2674 g_free (cmpl_state->updated_text);
2676 g_free (cmpl_state);
2680 free_dir (CompletionDir* dir)
2682 g_free (dir->cmpl_text);
2683 g_free (dir->fullname);
2688 free_dir_sent (CompletionDirSent* sent)
2691 for (i = 0; i < sent->entry_count; i++)
2693 g_free (sent->entries[i].entry_name);
2694 g_free (sent->entries[i].sort_key);
2696 g_free (sent->entries);
2701 prune_memory_usage (CompletionState *cmpl_state)
2703 GList* cdsl = cmpl_state->directory_sent_storage;
2704 GList* cdl = cmpl_state->directory_storage;
2708 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2713 cmpl_free_dir_sent_list (cdsl->next);
2717 cmpl_state->directory_storage = NULL;
2720 if (cdl->data == cmpl_state->reference_dir)
2721 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2723 free_dir (cdl->data);
2730 /**********************************************************************/
2731 /* The main entrances. */
2732 /**********************************************************************/
2734 static PossibleCompletion*
2735 cmpl_completion_matches (gchar *text_to_complete,
2736 gchar **remaining_text,
2737 CompletionState *cmpl_state)
2740 PossibleCompletion *poss;
2742 prune_memory_usage (cmpl_state);
2744 g_assert (text_to_complete != NULL);
2746 cmpl_state->user_completion_index = -1;
2747 cmpl_state->last_completion_text = text_to_complete;
2748 cmpl_state->the_completion.text[0] = 0;
2749 cmpl_state->last_valid_char = 0;
2750 cmpl_state->updated_text_len = -1;
2751 cmpl_state->updated_text[0] = 0;
2752 cmpl_state->re_complete = FALSE;
2755 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2757 if (text_to_complete[0] == '~' && !first_slash)
2759 /* Text starts with ~ and there is no slash, show all the
2760 * home directory completions.
2762 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2764 update_cmpl (poss, cmpl_state);
2769 cmpl_state->reference_dir =
2770 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2772 if (!cmpl_state->reference_dir)
2775 cmpl_state->completion_dir =
2776 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2778 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2780 if (!cmpl_state->completion_dir)
2783 cmpl_state->completion_dir->cmpl_index = -1;
2784 cmpl_state->completion_dir->cmpl_parent = NULL;
2785 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2787 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2789 cmpl_state->reference_dir = cmpl_state->completion_dir;
2791 poss = attempt_file_completion (cmpl_state);
2793 update_cmpl (poss, cmpl_state);
2798 static PossibleCompletion*
2799 cmpl_next_completion (CompletionState* cmpl_state)
2801 PossibleCompletion* poss = NULL;
2803 cmpl_state->the_completion.text[0] = 0;
2806 if (cmpl_state->user_completion_index >= 0)
2807 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2809 poss = attempt_file_completion (cmpl_state);
2811 poss = attempt_file_completion (cmpl_state);
2814 update_cmpl (poss, cmpl_state);
2819 /**********************************************************************/
2820 /* Directory Operations */
2821 /**********************************************************************/
2823 /* Open the directory where completion will begin from, if possible. */
2824 static CompletionDir*
2825 open_ref_dir (gchar *text_to_complete,
2826 gchar **remaining_text,
2827 CompletionState *cmpl_state)
2830 CompletionDir *new_dir;
2832 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2834 #ifdef G_WITH_CYGWIN
2835 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2838 sprintf (root_dir, "//%c", text_to_complete[2]);
2840 new_dir = open_dir (root_dir, cmpl_state);
2843 *remaining_text = text_to_complete + 4;
2851 else if (text_to_complete[0] == '~')
2853 new_dir = open_user_dir (text_to_complete, cmpl_state);
2858 *remaining_text = first_slash + 1;
2860 *remaining_text = text_to_complete + strlen (text_to_complete);
2868 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2870 gchar *tmp = g_strdup (text_to_complete);
2874 while (*p && *p != '*' && *p != '?')
2878 p = strrchr (tmp, G_DIR_SEPARATOR);
2886 new_dir = open_dir (tmp, cmpl_state);
2889 *remaining_text = text_to_complete +
2890 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2894 /* If no possible candidates, use the cwd */
2895 gchar *sys_curdir = g_get_current_dir ();
2896 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2898 g_free (sys_curdir);
2900 new_dir = open_dir (utf8_curdir, cmpl_state);
2903 *remaining_text = text_to_complete;
2905 g_free (utf8_curdir);
2912 *remaining_text = text_to_complete;
2914 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2919 new_dir->cmpl_index = -1;
2920 new_dir->cmpl_parent = NULL;
2928 /* open a directory by user name */
2929 static CompletionDir*
2930 open_user_dir (const gchar *text_to_complete,
2931 CompletionState *cmpl_state)
2933 CompletionDir *result;
2937 g_assert (text_to_complete && text_to_complete[0] == '~');
2939 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2942 cmp_len = first_slash - text_to_complete - 1;
2944 cmp_len = strlen (text_to_complete + 1);
2949 const gchar *homedir = g_get_home_dir ();
2950 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2953 result = open_dir (utf8_homedir, cmpl_state);
2957 g_free (utf8_homedir);
2962 gchar* copy = g_new (char, cmp_len + 1);
2966 strncpy (copy, text_to_complete + 1, cmp_len);
2968 pwd = getpwnam (copy);
2975 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
2976 result = open_dir (utf8_dir, cmpl_state);
2984 /* open a directory relative the the current relative directory */
2985 static CompletionDir*
2986 open_relative_dir (gchar *dir_name,
2988 CompletionState *cmpl_state)
2990 CompletionDir *result;
2993 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2994 g_string_assign (path, dir->fullname);
2996 if (dir->fullname_len > 1
2997 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2998 g_string_append_c (path, G_DIR_SEPARATOR);
2999 g_string_append (path, dir_name);
3001 result = open_dir (path->str, cmpl_state);
3003 g_string_free (path, TRUE);
3008 /* after the cache lookup fails, really open a new directory */
3009 static CompletionDirSent*
3010 open_new_dir (gchar *dir_name,
3012 gboolean stat_subdirs)
3014 CompletionDirSent *sent;
3017 GError *error = NULL;
3018 gint entry_count = 0;
3021 struct stat ent_sbuf;
3023 gchar *sys_dir_name;
3025 sent = g_new (CompletionDirSent, 1);
3026 sent->mtime = sbuf->st_mtime;
3027 sent->inode = sbuf->st_ino;
3028 sent->device = sbuf->st_dev;
3030 path = g_string_sized_new (2*MAXPATHLEN + 10);
3032 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3035 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3039 directory = g_dir_open (sys_dir_name, 0, &error);
3042 cmpl_errno = error->code; /* ??? */
3043 g_free (sys_dir_name);
3047 while ((dirent = g_dir_read_name (directory)) != NULL)
3050 entry_count += 2; /* For ".",".." */
3052 sent->entries = g_new (CompletionDirEntry, entry_count);
3053 sent->entry_count = entry_count;
3055 g_dir_rewind (directory);
3057 for (i = 0; i < entry_count; i += 1)
3059 GError *error = NULL;
3067 dirent = g_dir_read_name (directory);
3068 if (!dirent) /* Directory changed */
3072 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3073 if (sent->entries[n_entries].entry_name == NULL
3074 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3076 gchar *escaped_str = g_strescape (dirent, NULL);
3077 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3078 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3080 error->message ? error->message : _("Invalid Utf-8"));
3081 g_free (escaped_str);
3082 g_clear_error (&error);
3085 g_clear_error (&error);
3087 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3089 g_string_assign (path, sys_dir_name);
3090 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3092 g_string_append_c (path, G_DIR_SEPARATOR);
3094 g_string_append (path, dirent);
3098 /* Here we know path->str is a "system charset" string */
3099 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3100 sent->entries[n_entries].is_dir = TRUE;
3102 /* stat may fail, and we don't mind, since it could be a
3103 * dangling symlink. */
3104 sent->entries[n_entries].is_dir = FALSE;
3107 sent->entries[n_entries].is_dir = 1;
3111 sent->entry_count = n_entries;
3113 g_free (sys_dir_name);
3114 g_string_free (path, TRUE);
3115 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3117 g_dir_close (directory);
3122 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3125 check_dir (gchar *dir_name,
3126 struct stat *result,
3127 gboolean *stat_subdirs)
3129 /* A list of directories that we know only contain other directories.
3130 * Trying to stat every file in these directories would be very
3137 struct stat statbuf;
3138 } no_stat_dirs[] = {
3139 { "/afs", FALSE, { 0 } },
3140 { "/net", FALSE, { 0 } }
3143 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3144 static gboolean initialized = FALSE;
3145 gchar *sys_dir_name;
3151 for (i = 0; i < n_no_stat_dirs; i++)
3153 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3154 no_stat_dirs[i].present = TRUE;
3158 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3161 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3165 if (stat (sys_dir_name, result) < 0)
3167 g_free (sys_dir_name);
3171 g_free (sys_dir_name);
3173 *stat_subdirs = TRUE;
3174 for (i = 0; i < n_no_stat_dirs; i++)
3176 if (no_stat_dirs[i].present &&
3177 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3178 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3180 *stat_subdirs = FALSE;
3190 /* open a directory by absolute pathname */
3191 static CompletionDir*
3192 open_dir (gchar *dir_name,
3193 CompletionState *cmpl_state)
3196 gboolean stat_subdirs;
3197 CompletionDirSent *sent;
3200 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3201 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3204 cdsl = cmpl_state->directory_sent_storage;
3210 if (sent->inode == sbuf.st_ino &&
3211 sent->mtime == sbuf.st_mtime &&
3212 sent->device == sbuf.st_dev)
3213 return attach_dir (sent, dir_name, cmpl_state);
3218 stat_subdirs = TRUE;
3221 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3225 cmpl_state->directory_sent_storage =
3226 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3228 return attach_dir (sent, dir_name, cmpl_state);
3234 static CompletionDir*
3235 attach_dir (CompletionDirSent *sent,
3237 CompletionState *cmpl_state)
3239 CompletionDir* new_dir;
3241 new_dir = g_new (CompletionDir, 1);
3243 cmpl_state->directory_storage =
3244 g_list_prepend (cmpl_state->directory_storage, new_dir);
3246 new_dir->sent = sent;
3247 new_dir->fullname = g_strdup (dir_name);
3248 new_dir->fullname_len = strlen (dir_name);
3249 new_dir->cmpl_text = NULL;
3255 correct_dir_fullname (CompletionDir* cmpl_dir)
3257 gint length = strlen (cmpl_dir->fullname);
3258 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3259 gchar *sys_filename;
3262 /* Does it end with /. (\.) ? */
3264 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3266 /* Is it just the root directory (on a drive) ? */
3267 if (cmpl_dir->fullname + length - 2 == first_slash)
3269 cmpl_dir->fullname[length - 1] = 0;
3270 cmpl_dir->fullname_len = length - 1;
3275 cmpl_dir->fullname[length - 2] = 0;
3279 /* Ends with /./ (\.\)? */
3280 else if (length >= 3 &&
3281 strcmp (cmpl_dir->fullname + length - 3,
3282 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3283 cmpl_dir->fullname[length - 2] = 0;
3285 /* Ends with /.. (\..) ? */
3286 else if (length >= 3 &&
3287 strcmp (cmpl_dir->fullname + length - 3,
3288 G_DIR_SEPARATOR_S "..") == 0)
3290 /* Is it just /.. (X:\..)? */
3291 if (cmpl_dir->fullname + length - 3 == first_slash)
3293 cmpl_dir->fullname[length - 2] = 0;
3294 cmpl_dir->fullname_len = length - 2;
3298 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3301 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3305 if (stat (sys_filename, &sbuf) < 0)
3307 g_free (sys_filename);
3311 g_free (sys_filename);
3313 cmpl_dir->fullname[length - 3] = 0;
3315 if (!correct_parent (cmpl_dir, &sbuf))
3319 /* Ends with /../ (\..\)? */
3320 else if (length >= 4 &&
3321 strcmp (cmpl_dir->fullname + length - 4,
3322 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3324 /* Is it just /../ (X:\..\)? */
3325 if (cmpl_dir->fullname + length - 4 == first_slash)
3327 cmpl_dir->fullname[length - 3] = 0;
3328 cmpl_dir->fullname_len = length - 3;
3332 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3335 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3339 if (stat (sys_filename, &sbuf) < 0)
3341 g_free (sys_filename);
3345 g_free (sys_filename);
3347 cmpl_dir->fullname[length - 4] = 0;
3349 if (!correct_parent (cmpl_dir, &sbuf))
3353 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3359 correct_parent (CompletionDir *cmpl_dir,
3366 gchar *sys_filename;
3369 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3370 g_assert (last_slash);
3371 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3373 /* Clever (?) way to check for top-level directory that works also on
3374 * Win32, where there is a drive letter and colon prefixed...
3376 if (last_slash != first_slash)
3386 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3389 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3391 last_slash[0] = G_DIR_SEPARATOR;
3395 if (stat (sys_filename, &parbuf) < 0)
3397 g_free (sys_filename);
3400 last_slash[0] = G_DIR_SEPARATOR;
3403 g_free (sys_filename);
3405 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3406 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3407 /* it wasn't a link */
3413 last_slash[0] = G_DIR_SEPARATOR;
3415 /* it was a link, have to figure it out the hard way */
3417 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3422 g_free (cmpl_dir->fullname);
3424 cmpl_dir->fullname = new_name;
3433 find_parent_dir_fullname (gchar* dirname)
3435 gchar *sys_orig_dir;
3440 sys_orig_dir = g_get_current_dir ();
3441 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3444 g_free (sys_orig_dir);
3445 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3449 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3452 chdir (sys_orig_dir);
3453 g_free (sys_dirname);
3454 g_free (sys_orig_dir);
3457 g_free (sys_dirname);
3459 sys_cwd = g_get_current_dir ();
3460 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3463 if (chdir (sys_orig_dir) != 0)
3466 g_free (sys_orig_dir);
3470 g_free (sys_orig_dir);
3476 /**********************************************************************/
3477 /* Completion Operations */
3478 /**********************************************************************/
3482 static PossibleCompletion*
3483 attempt_homedir_completion (gchar *text_to_complete,
3484 CompletionState *cmpl_state)
3488 if (!cmpl_state->user_dir_name_buffer &&
3489 !get_pwdb (cmpl_state))
3491 length = strlen (text_to_complete) - 1;
3493 cmpl_state->user_completion_index += 1;
3495 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3497 index = first_diff_index (text_to_complete + 1,
3498 cmpl_state->user_directories
3499 [cmpl_state->user_completion_index].login);
3506 if (cmpl_state->last_valid_char < (index + 1))
3507 cmpl_state->last_valid_char = index + 1;
3508 cmpl_state->user_completion_index += 1;
3512 cmpl_state->the_completion.is_a_completion = 1;
3513 cmpl_state->the_completion.is_directory = TRUE;
3515 append_completion_text ("~", cmpl_state);
3517 append_completion_text (cmpl_state->
3518 user_directories[cmpl_state->user_completion_index].login,
3521 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3524 if (text_to_complete[1]
3525 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3527 cmpl_state->user_completion_index = -1;
3532 cmpl_state->user_completion_index += 1;
3533 cmpl_state->the_completion.is_a_completion = 1;
3534 cmpl_state->the_completion.is_directory = TRUE;
3536 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3542 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3543 #define FOLD(c) (tolower(c))
3548 /* returns the index (>= 0) of the first differing character,
3549 * PATTERN_MATCH if the completion matches */
3551 first_diff_index (gchar *pat,
3556 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3566 return PATTERN_MATCH;
3569 static PossibleCompletion*
3570 append_completion_text (gchar *text,
3571 CompletionState *cmpl_state)
3575 if (!cmpl_state->the_completion.text)
3578 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3580 if (cmpl_state->the_completion.text_alloc > len)
3582 strcat (cmpl_state->the_completion.text, text);
3583 return &cmpl_state->the_completion;
3589 cmpl_state->the_completion.text_alloc = i;
3591 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3593 if (!cmpl_state->the_completion.text)
3597 strcat (cmpl_state->the_completion.text, text);
3598 return &cmpl_state->the_completion;
3602 static CompletionDir*
3603 find_completion_dir (gchar *text_to_complete,
3604 gchar **remaining_text,
3605 CompletionState *cmpl_state)
3607 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3608 CompletionDir* dir = cmpl_state->reference_dir;
3609 CompletionDir* next;
3610 *remaining_text = text_to_complete;
3614 gint len = first_slash - *remaining_text;
3616 gchar *found_name = NULL; /* Quiet gcc */
3618 gchar* pat_buf = g_new (gchar, len + 1);
3620 strncpy (pat_buf, *remaining_text, len);
3623 for (i = 0; i < dir->sent->entry_count; i += 1)
3625 if (dir->sent->entries[i].is_dir &&
3626 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name))
3636 found_name = dir->sent->entries[i].entry_name;
3643 /* Perhaps we are trying to open an automount directory */
3644 found_name = pat_buf;
3647 next = open_relative_dir (found_name, dir, cmpl_state);
3655 next->cmpl_parent = dir;
3659 if (!correct_dir_fullname (dir))
3665 *remaining_text = first_slash + 1;
3666 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3675 update_cmpl (PossibleCompletion *poss,
3676 CompletionState *cmpl_state)
3680 if (!poss || !cmpl_is_a_completion (poss))
3683 cmpl_len = strlen (cmpl_this_completion (poss));
3685 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3687 cmpl_state->updated_text =
3688 (gchar*)g_realloc (cmpl_state->updated_text,
3689 cmpl_state->updated_text_alloc);
3690 cmpl_state->updated_text_alloc = 2*cmpl_len;
3693 if (cmpl_state->updated_text_len < 0)
3695 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3696 cmpl_state->updated_text_len = cmpl_len;
3697 cmpl_state->re_complete = cmpl_is_directory (poss);
3699 else if (cmpl_state->updated_text_len == 0)
3701 cmpl_state->re_complete = FALSE;
3706 first_diff_index (cmpl_state->updated_text,
3707 cmpl_this_completion (poss));
3709 cmpl_state->re_complete = FALSE;
3711 if (first_diff == PATTERN_MATCH)
3714 if (first_diff > cmpl_state->updated_text_len)
3715 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3717 cmpl_state->updated_text_len = first_diff;
3718 cmpl_state->updated_text[first_diff] = 0;
3722 static PossibleCompletion*
3723 attempt_file_completion (CompletionState *cmpl_state)
3725 gchar *pat_buf, *first_slash;
3726 CompletionDir *dir = cmpl_state->active_completion_dir;
3728 dir->cmpl_index += 1;
3730 if (dir->cmpl_index == dir->sent->entry_count)
3732 if (dir->cmpl_parent == NULL)
3734 cmpl_state->active_completion_dir = NULL;
3740 cmpl_state->active_completion_dir = dir->cmpl_parent;
3742 return attempt_file_completion (cmpl_state);
3746 g_assert (dir->cmpl_text);
3748 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3752 gint len = first_slash - dir->cmpl_text;
3754 pat_buf = g_new (gchar, len + 1);
3755 strncpy (pat_buf, dir->cmpl_text, len);
3760 gint len = strlen (dir->cmpl_text);
3762 pat_buf = g_new (gchar, len + 2);
3763 strcpy (pat_buf, dir->cmpl_text);
3764 /* Don't append a * if the user entered one herself.
3765 * This way one can complete *.h and don't get matches
3766 * on any .help files, for instance.
3768 if (strchr (pat_buf, '*') == NULL)
3769 strcpy (pat_buf + len, "*");
3774 if (dir->sent->entries[dir->cmpl_index].is_dir)
3776 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name))
3778 CompletionDir* new_dir;
3780 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3789 new_dir->cmpl_parent = dir;
3791 new_dir->cmpl_index = -1;
3792 new_dir->cmpl_text = g_strdup (first_slash + 1);
3794 cmpl_state->active_completion_dir = new_dir;
3797 return attempt_file_completion (cmpl_state);
3802 return attempt_file_completion (cmpl_state);
3808 return attempt_file_completion (cmpl_state);
3813 if (dir->cmpl_parent != NULL)
3815 append_completion_text (dir->fullname +
3816 strlen (cmpl_state->completion_dir->fullname) + 1,
3818 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3821 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3823 cmpl_state->the_completion.is_a_completion =
3824 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name);
3826 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3827 if (dir->sent->entries[dir->cmpl_index].is_dir)
3828 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3831 return &cmpl_state->the_completion;
3838 get_pwdb (CompletionState* cmpl_state)
3840 struct passwd *pwd_ptr;
3843 gint len = 0, i, count = 0;
3845 if (cmpl_state->user_dir_name_buffer)
3849 while ((pwd_ptr = getpwent ()) != NULL)
3851 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3852 len += strlen (utf8);
3854 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3855 len += strlen (utf8);
3863 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3864 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3865 cmpl_state->user_directories_len = count;
3867 buf_ptr = cmpl_state->user_dir_name_buffer;
3869 for (i = 0; i < count; i += 1)
3871 pwd_ptr = getpwent ();
3878 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3879 strcpy (buf_ptr, utf8);
3882 cmpl_state->user_directories[i].login = buf_ptr;
3884 buf_ptr += strlen (buf_ptr);
3887 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3888 strcpy (buf_ptr, utf8);
3891 cmpl_state->user_directories[i].homedir = buf_ptr;
3893 buf_ptr += strlen (buf_ptr);
3897 qsort (cmpl_state->user_directories,
3898 cmpl_state->user_directories_len,
3899 sizeof (CompletionUserDir),
3908 if (cmpl_state->user_dir_name_buffer)
3909 g_free (cmpl_state->user_dir_name_buffer);
3910 if (cmpl_state->user_directories)
3911 g_free (cmpl_state->user_directories);
3913 cmpl_state->user_dir_name_buffer = NULL;
3914 cmpl_state->user_directories = NULL;
3920 compare_user_dir (const void *a,
3923 return strcmp ((((CompletionUserDir*)a))->login,
3924 (((CompletionUserDir*)b))->login);
3930 compare_cmpl_dir (const void *a,
3934 return strcmp (((CompletionDirEntry*)a)->sort_key,
3935 (((CompletionDirEntry*)b))->sort_key);
3939 cmpl_state_okay (CompletionState* cmpl_state)
3941 return cmpl_state && cmpl_state->reference_dir;
3945 cmpl_strerror (gint err)
3947 if (err == CMPL_ERRNO_TOO_LONG)
3948 return _("Name too long");
3949 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3950 return _("Couldn't convert filename");
3952 return g_strerror (err);