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 etc */
47 #if defined(G_PLATFORM_WIN32)
52 #endif /* G_PLATFORM_WIN32 */
54 #include <winsock.h> /* For gethostname */
57 #include "gdk/gdkkeysyms.h"
58 #include "gtkbutton.h"
59 #include "gtkcellrenderertext.h"
61 #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 "gtkdialog.h"
77 #include "gtkmessagedialog.h"
79 #include "gtkeventbox.h"
81 #undef GTK_DISABLE_DEPRECATED
82 #include "gtkoptionmenu.h"
83 #define GTK_DISABLE_DEPRECATED
86 #include "gtkhpaned.h"
91 #define mkdir(p,m) _mkdir(p)
93 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
95 #endif /* G_OS_WIN32 */
98 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
101 #define DIR_LIST_WIDTH 180
102 #define DIR_LIST_HEIGHT 180
103 #define FILE_LIST_WIDTH 180
104 #define FILE_LIST_HEIGHT 180
106 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
107 * in here, since the rest of the code in the file does require some
112 # define MAXPATHLEN PATH_MAX
114 # define MAXPATHLEN 2048
118 /* I've put this here so it doesn't get confused with the
119 * file completion interface */
120 typedef struct _HistoryCallbackArg HistoryCallbackArg;
122 struct _HistoryCallbackArg
125 GtkWidget *menu_item;
129 typedef struct _CompletionState CompletionState;
130 typedef struct _CompletionDir CompletionDir;
131 typedef struct _CompletionDirSent CompletionDirSent;
132 typedef struct _CompletionDirEntry CompletionDirEntry;
133 typedef struct _CompletionUserDir CompletionUserDir;
134 typedef struct _PossibleCompletion PossibleCompletion;
136 /* Non-external file completion decls and structures */
138 /* A contant telling PRCS how many directories to cache. Its actually
139 * kept in a list, so the geometry isn't important. */
140 #define CMPL_DIRECTORY_CACHE_SIZE 10
142 /* A constant used to determine whether a substring was an exact
143 * match by first_diff_index()
145 #define PATTERN_MATCH -1
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
155 #ifndef G_PLATFORM_WIN32
162 struct _CompletionDirEntry *entries;
165 struct _CompletionDir
167 CompletionDirSent *sent;
172 struct _CompletionDir *cmpl_parent;
177 /* This structure contains pairs of directory entry names with a flag saying
178 * whether or not they are a valid directory. NOTE: This information is used
179 * to provide the caller with information about whether to update its completions
180 * or try to open a file. Since directories are cached by the directory mtime,
181 * a symlink which points to an invalid file (which will not be a directory),
182 * will not be reevaluated if that file is created, unless the containing
183 * directory is touched. I consider this case to be worth ignoring (josh).
185 struct _CompletionDirEntry
192 struct _CompletionUserDir
198 struct _PossibleCompletion
200 /* accessible fields, all are accessed externally by functions
204 gint is_a_completion;
205 gboolean is_directory;
212 struct _CompletionState
214 gint last_valid_char;
216 gint updated_text_len;
217 gint updated_text_alloc;
218 gboolean re_complete;
220 gchar *user_dir_name_buffer;
221 gint user_directories_len;
223 gchar *last_completion_text;
225 gint user_completion_index; /* if >= 0, currently completing ~user */
227 struct _CompletionDir *completion_dir; /* directory completing from */
228 struct _CompletionDir *active_completion_dir;
230 struct _PossibleCompletion the_completion;
232 struct _CompletionDir *reference_dir; /* initial directory */
234 GList* directory_storage;
235 GList* directory_sent_storage;
237 struct _CompletionUserDir *user_directories;
255 /* File completion functions which would be external, were they used
256 * outside of this file.
259 static CompletionState* cmpl_init_state (void);
260 static void cmpl_free_state (CompletionState *cmpl_state);
261 static gint cmpl_state_okay (CompletionState* cmpl_state);
262 static const gchar* cmpl_strerror (gint);
264 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
265 gchar **remaining_text,
266 CompletionState *cmpl_state);
268 /* Returns a name for consideration, possibly a completion, this name
269 * will be invalid after the next call to cmpl_next_completion.
271 static char* cmpl_this_completion (PossibleCompletion*);
273 /* True if this completion matches the given text. Otherwise, this
274 * output can be used to have a list of non-completions.
276 static gint cmpl_is_a_completion (PossibleCompletion*);
278 /* True if the completion is a directory
280 static gboolean cmpl_is_directory (PossibleCompletion*);
282 /* Obtains the next completion, or NULL
284 static PossibleCompletion* cmpl_next_completion (CompletionState*);
286 /* Updating completions: the return value of cmpl_updated_text() will
287 * be text_to_complete completed as much as possible after the most
288 * recent call to cmpl_completion_matches. For the present
289 * application, this is the suggested replacement for the user's input
290 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
293 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
295 /* After updating, to see if the completion was a directory, call
296 * this. If it was, you should consider re-calling completion_matches.
298 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
300 /* Current location: if using file completion, return the current
301 * directory, from which file completion begins. More specifically,
302 * the cwd concatenated with all exact completions up to the last
303 * directory delimiter('/').
305 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
308 /* This doesn't work currently and would require changes
309 * to fnmatch.c to get working.
311 /* backing up: if cmpl_completion_matches returns NULL, you may query
312 * the index of the last completable character into cmpl_updated_text.
314 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
317 /* When the user selects a non-directory, call cmpl_completion_fullname
318 * to get the full name of the selected file.
320 static gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
323 /* Directory operations. */
324 static CompletionDir* open_ref_dir (gchar* text_to_complete,
325 gchar** remaining_text,
326 CompletionState* cmpl_state);
327 #ifndef G_PLATFORM_WIN32
328 static gboolean check_dir (gchar *dir_name,
330 gboolean *stat_subdirs);
332 static CompletionDir* open_dir (gchar* dir_name,
333 CompletionState* cmpl_state);
335 static CompletionDir* open_user_dir (const gchar* text_to_complete,
336 CompletionState *cmpl_state);
338 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
339 CompletionState *cmpl_state);
340 static CompletionDirSent* open_new_dir (gchar* dir_name,
342 gboolean stat_subdirs);
343 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
344 static gint correct_parent (CompletionDir* cmpl_dir,
346 #ifndef G_PLATFORM_WIN32
347 static gchar* find_parent_dir_fullname (gchar* dirname);
349 static CompletionDir* attach_dir (CompletionDirSent* sent,
351 CompletionState *cmpl_state);
352 static void free_dir_sent (CompletionDirSent* sent);
353 static void free_dir (CompletionDir *dir);
354 static void prune_memory_usage(CompletionState *cmpl_state);
356 /* Completion operations */
358 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
359 CompletionState *cmpl_state);
361 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
362 static CompletionDir* find_completion_dir(gchar* text_to_complete,
363 gchar** remaining_text,
364 CompletionState* cmpl_state);
365 static PossibleCompletion* append_completion_text(gchar* text,
366 CompletionState* cmpl_state);
368 static gint get_pwdb(CompletionState* cmpl_state);
369 static gint compare_user_dir(const void* a, const void* b);
371 static gint first_diff_index(gchar* pat, gchar* text);
372 static gint compare_cmpl_dir(const void* a, const void* b);
373 static void update_cmpl(PossibleCompletion* poss,
374 CompletionState* cmpl_state);
376 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
377 static void gtk_file_selection_set_property (GObject *object,
381 static void gtk_file_selection_get_property (GObject *object,
385 static void gtk_file_selection_init (GtkFileSelection *filesel);
386 static void gtk_file_selection_finalize (GObject *object);
387 static void gtk_file_selection_destroy (GtkObject *object);
388 static void gtk_file_selection_map (GtkWidget *widget);
389 static gint gtk_file_selection_key_press (GtkWidget *widget,
392 static gint gtk_file_selection_insert_text (GtkWidget *widget,
393 const gchar *new_text,
394 gint new_text_length,
397 static void gtk_file_selection_update_fileops (GtkFileSelection *filesel);
399 static void gtk_file_selection_file_activate (GtkTreeView *tree_view,
401 GtkTreeViewColumn *column,
403 static void gtk_file_selection_file_changed (GtkTreeSelection *selection,
405 static void gtk_file_selection_dir_activate (GtkTreeView *tree_view,
407 GtkTreeViewColumn *column,
410 static void gtk_file_selection_populate (GtkFileSelection *fs,
412 gboolean try_complete,
413 gboolean reset_entry);
414 static void gtk_file_selection_abort (GtkFileSelection *fs);
416 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
419 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
420 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
421 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
423 static void free_selected_names (GPtrArray *names);
425 #ifndef G_PLATFORM_WIN32
427 #define compare_utf8_filenames(a, b) strcmp(a, b)
428 #define compare_sys_filenames(a, b) strcmp(a, b)
433 compare_utf8_filenames (const gchar *a,
436 gchar *a_folded, *b_folded;
439 a_folded = g_utf8_strdown (a, -1);
440 b_folded = g_utf8_strdown (b, -1);
442 retval = strcmp (a_folded, b_folded);
451 compare_sys_filenames (const gchar *a,
454 gchar *a_utf8, *b_utf8;
457 a_utf8 = g_filename_to_utf8 (a, -1, NULL, NULL, NULL);
458 b_utf8 = g_filename_to_utf8 (b, -1, NULL, NULL, NULL);
460 retval = compare_utf8_filenames (a_utf8, b_utf8);
470 static GtkWindowClass *parent_class = NULL;
472 /* Saves errno when something cmpl does fails. */
473 static gint cmpl_errno;
477 * Take the path currently in the file selection
478 * entry field and translate as necessary from
479 * a Win32 style to Cygwin style path. For
480 * instance translate:
481 * x:\somepath\file.jpg
483 * /cygdrive/x/somepath/file.jpg
485 * Replace the path in the selection text field.
486 * Return a boolean value concerning whether a
487 * translation had to be made.
490 translate_win32_path (GtkFileSelection *filesel)
494 gchar newPath[MAX_PATH];
497 * Retrieve the current path
499 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
501 cygwin_conv_to_posix_path (path, newPath);
502 updated = (strcmp (path, newPath) != 0);
505 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
512 gtk_file_selection_get_type (void)
514 static GType file_selection_type = 0;
516 if (!file_selection_type)
518 static const GTypeInfo filesel_info =
520 sizeof (GtkFileSelectionClass),
521 NULL, /* base_init */
522 NULL, /* base_finalize */
523 (GClassInitFunc) gtk_file_selection_class_init,
524 NULL, /* class_finalize */
525 NULL, /* class_data */
526 sizeof (GtkFileSelection),
528 (GInstanceInitFunc) gtk_file_selection_init,
531 file_selection_type =
532 g_type_register_static (GTK_TYPE_DIALOG, "GtkFileSelection",
536 return file_selection_type;
540 gtk_file_selection_class_init (GtkFileSelectionClass *class)
542 GObjectClass *gobject_class;
543 GtkObjectClass *object_class;
544 GtkWidgetClass *widget_class;
546 gobject_class = (GObjectClass*) class;
547 object_class = (GtkObjectClass*) class;
548 widget_class = (GtkWidgetClass*) class;
550 parent_class = g_type_class_peek_parent (class);
552 gobject_class->finalize = gtk_file_selection_finalize;
553 gobject_class->set_property = gtk_file_selection_set_property;
554 gobject_class->get_property = gtk_file_selection_get_property;
556 g_object_class_install_property (gobject_class,
558 g_param_spec_string ("filename",
560 _("The currently selected filename"),
562 G_PARAM_READABLE | G_PARAM_WRITABLE));
563 g_object_class_install_property (gobject_class,
565 g_param_spec_boolean ("show_fileops",
566 _("Show file operations"),
567 _("Whether buttons for creating/manipulating files should be displayed"),
571 g_object_class_install_property (gobject_class,
572 PROP_SELECT_MULTIPLE,
573 g_param_spec_boolean ("select_multiple",
574 _("Select multiple"),
575 _("Whether to allow multiple files to be selected"),
579 object_class->destroy = gtk_file_selection_destroy;
580 widget_class->map = gtk_file_selection_map;
583 static void gtk_file_selection_set_property (GObject *object,
588 GtkFileSelection *filesel;
590 filesel = GTK_FILE_SELECTION (object);
595 gtk_file_selection_set_filename (filesel,
596 g_value_get_string (value));
598 case PROP_SHOW_FILEOPS:
599 if (g_value_get_boolean (value))
600 gtk_file_selection_show_fileop_buttons (filesel);
602 gtk_file_selection_hide_fileop_buttons (filesel);
604 case PROP_SELECT_MULTIPLE:
605 gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 static void gtk_file_selection_get_property (GObject *object,
618 GtkFileSelection *filesel;
620 filesel = GTK_FILE_SELECTION (object);
625 g_value_set_string (value,
626 gtk_file_selection_get_filename(filesel));
629 case PROP_SHOW_FILEOPS:
630 /* This is a little bit hacky, but doing otherwise would require
631 * adding a field to the object.
633 g_value_set_boolean (value, (filesel->fileop_c_dir &&
634 filesel->fileop_del_file &&
635 filesel->fileop_ren_file));
637 case PROP_SELECT_MULTIPLE:
638 g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647 grab_default (GtkWidget *widget)
649 gtk_widget_grab_default (widget);
654 gtk_file_selection_init (GtkFileSelection *filesel)
656 GtkWidget *entry_vbox;
658 GtkWidget *list_hbox, *list_container;
659 GtkWidget *confirm_area;
660 GtkWidget *pulldown_hbox;
661 GtkWidget *scrolled_win;
667 GtkTreeViewColumn *column;
669 gtk_widget_push_composite_child ();
671 dialog = GTK_DIALOG (filesel);
673 filesel->cmpl_state = cmpl_init_state ();
675 /* The dialog-sized vertical box */
676 filesel->main_vbox = dialog->vbox;
677 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
679 /* The horizontal box containing create, rename etc. buttons */
680 filesel->button_area = gtk_hbutton_box_new ();
681 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
682 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
683 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
685 gtk_widget_show (filesel->button_area);
687 gtk_file_selection_show_fileop_buttons (filesel);
689 /* hbox for pulldown menu */
690 pulldown_hbox = gtk_hbox_new (TRUE, 5);
691 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
692 gtk_widget_show (pulldown_hbox);
695 filesel->history_pulldown = gtk_option_menu_new ();
696 gtk_widget_show (filesel->history_pulldown);
697 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
700 /* The horizontal box containing the directory and file listboxes */
702 spacer = gtk_hbox_new (FALSE, 0);
703 gtk_widget_set_size_request (spacer, -1, 5);
704 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
705 gtk_widget_show (spacer);
707 list_hbox = gtk_hbox_new (FALSE, 5);
708 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
709 gtk_widget_show (list_hbox);
711 list_container = g_object_new (GTK_TYPE_HPANED,
717 list_container = list_hbox;
719 spacer = gtk_hbox_new (FALSE, 0);
720 gtk_widget_set_size_request (spacer, -1, 5);
721 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
722 gtk_widget_show (spacer);
724 /* The directories list */
726 model = gtk_list_store_new (1, G_TYPE_STRING);
727 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
728 g_object_unref (model);
730 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
731 gtk_cell_renderer_text_new (),
734 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
735 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
736 gtk_widget_show (label);
737 gtk_tree_view_column_set_widget (column, label);
738 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
739 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
741 gtk_widget_set_size_request (filesel->dir_list,
742 DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
743 g_signal_connect (filesel->dir_list, "row_activated",
744 G_CALLBACK (gtk_file_selection_dir_activate), filesel);
746 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_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->dir_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), 0);
754 if (GTK_IS_PANED (list_container))
755 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
757 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
758 gtk_widget_show (filesel->dir_list);
759 gtk_widget_show (scrolled_win);
762 model = gtk_list_store_new (1, G_TYPE_STRING);
763 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
764 g_object_unref (model);
766 column = gtk_tree_view_column_new_with_attributes (_("Files"),
767 gtk_cell_renderer_text_new (),
770 label = gtk_label_new_with_mnemonic (_("_Files"));
771 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
772 gtk_widget_show (label);
773 gtk_tree_view_column_set_widget (column, label);
774 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
775 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
777 gtk_widget_set_size_request (filesel->file_list,
778 FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
779 g_signal_connect (filesel->file_list, "row_activated",
780 G_CALLBACK (gtk_file_selection_file_activate), filesel);
781 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
782 G_CALLBACK (gtk_file_selection_file_changed), filesel);
784 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
786 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
787 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
788 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
789 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
790 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
791 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
792 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
793 gtk_widget_show (filesel->file_list);
794 gtk_widget_show (scrolled_win);
796 /* action area for packing buttons into. */
797 filesel->action_area = gtk_hbox_new (TRUE, 0);
798 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
800 gtk_widget_show (filesel->action_area);
802 /* The OK/Cancel button area */
803 confirm_area = dialog->action_area;
805 /* The Cancel button */
806 filesel->cancel_button = gtk_dialog_add_button (dialog,
808 GTK_RESPONSE_CANCEL);
810 filesel->ok_button = gtk_dialog_add_button (dialog,
814 gtk_widget_grab_default (filesel->ok_button);
816 /* The selection entry widget */
817 entry_vbox = gtk_vbox_new (FALSE, 2);
818 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
819 gtk_widget_show (entry_vbox);
821 eventbox = gtk_event_box_new ();
822 filesel->selection_text = label = gtk_label_new ("");
823 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
824 gtk_container_add (GTK_CONTAINER (eventbox), label);
825 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
826 gtk_widget_show (label);
827 gtk_widget_show (eventbox);
829 filesel->selection_entry = gtk_entry_new ();
830 g_signal_connect (filesel->selection_entry, "key_press_event",
831 G_CALLBACK (gtk_file_selection_key_press), filesel);
832 g_signal_connect (filesel->selection_entry, "insert_text",
833 G_CALLBACK (gtk_file_selection_insert_text), NULL);
834 g_signal_connect_swapped (filesel->selection_entry, "changed",
835 G_CALLBACK (gtk_file_selection_update_fileops), filesel);
836 g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
837 G_CALLBACK (grab_default),
839 g_signal_connect_swapped (filesel->selection_entry, "activate",
840 G_CALLBACK (gtk_button_clicked),
843 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
844 gtk_widget_show (filesel->selection_entry);
846 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
847 filesel->selection_entry);
849 if (!cmpl_state_okay (filesel->cmpl_state))
853 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
855 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
859 gtk_file_selection_populate (filesel, "", FALSE, TRUE);
862 gtk_widget_grab_focus (filesel->selection_entry);
864 gtk_widget_pop_composite_child ();
868 uri_list_extract_first_uri (const gchar* uri_list)
872 g_return_val_if_fail (uri_list != NULL, NULL);
875 /* We don't actually try to validate the URI according to RFC
876 * 2396, or even check for allowed characters - we just ignore
877 * comments and trim whitespace off the ends. We also
878 * allow LF delimination as well as the specified CRLF.
880 * We do allow comments like specified in RFC 2483.
886 while (g_ascii_isspace(*p))
890 while (*q && (*q != '\n') && (*q != '\r'))
896 while (q > p && g_ascii_isspace (*q))
900 return g_strndup (p, q - p + 1);
903 p = strchr (p, '\n');
911 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
915 if (response_id == GTK_RESPONSE_YES)
917 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
919 gtk_file_selection_set_filename (fs, filename);
922 gtk_widget_destroy (dialog);
927 filenames_dropped (GtkWidget *widget,
928 GdkDragContext *context,
931 GtkSelectionData *selection_data,
936 char *filename = NULL;
938 char this_hostname[257];
940 GError *error = NULL;
942 if (!selection_data->data)
945 uri = uri_list_extract_first_uri ((char *)selection_data->data);
950 filename = g_filename_from_uri (uri, &hostname, &error);
955 g_warning ("Error getting dropped filename: %s\n",
957 g_error_free (error);
961 res = gethostname (this_hostname, 256);
962 this_hostname[256] = 0;
964 if ((hostname == NULL) ||
965 (res == 0 && strcmp (hostname, this_hostname) == 0) ||
966 (strcmp (hostname, "localhost") == 0))
967 gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
972 gchar *filename_utf8;
974 /* Conversion back to UTF-8 should always succeed for the result
975 * of g_filename_from_uri()
977 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
978 g_assert (filename_utf8);
980 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
981 GTK_DIALOG_DESTROY_WITH_PARENT,
982 GTK_MESSAGE_QUESTION,
984 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
985 "Are you sure that you want to select it?"), filename_utf8, hostname);
986 g_free (filename_utf8);
988 g_object_set_data_full (G_OBJECT (dialog), "gtk-fs-dnd-filename", g_strdup (filename), g_free);
990 g_signal_connect_data (dialog, "response",
991 (GCallback) dnd_really_drop,
994 gtk_widget_show (dialog);
1007 TARGET_COMPOUND_TEXT
1012 filenames_drag_get (GtkWidget *widget,
1013 GdkDragContext *context,
1014 GtkSelectionData *selection_data,
1017 GtkFileSelection *filesel)
1025 file = gtk_file_selection_get_filename (filesel);
1029 if (info == TARGET_URILIST)
1031 res = gethostname (hostname, 256);
1034 uri_list = g_filename_to_uri (file, (!res)?hostname:NULL, &error);
1037 g_warning ("Error getting filename: %s\n",
1039 g_error_free (error);
1043 gtk_selection_data_set (selection_data,
1044 selection_data->target, 8,
1045 (void *)uri_list, strlen((char *)uri_list));
1050 gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
1051 g_assert (filename_utf8);
1052 gtk_selection_data_set_text (selection_data, filename_utf8, -1);
1053 g_free (filename_utf8);
1059 file_selection_setup_dnd (GtkFileSelection *filesel)
1061 GtkWidget *eventbox;
1062 static const GtkTargetEntry drop_types[] = {
1063 { "text/uri-list", 0, TARGET_URILIST}
1065 static gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
1066 static const GtkTargetEntry drag_types[] = {
1067 { "text/uri-list", 0, TARGET_URILIST},
1068 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
1071 { "COMPOUND_TEXT", 0, 0 }
1073 static gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
1075 gtk_drag_dest_set (GTK_WIDGET (filesel),
1076 GTK_DEST_DEFAULT_ALL,
1077 drop_types, n_drop_types,
1080 g_signal_connect (filesel, "drag_data_received",
1081 G_CALLBACK (filenames_dropped), NULL);
1083 eventbox = gtk_widget_get_parent (filesel->selection_text);
1084 gtk_drag_source_set (eventbox,
1086 drag_types, n_drag_types,
1089 g_signal_connect (eventbox, "drag_data_get",
1090 G_CALLBACK (filenames_drag_get), filesel);
1094 gtk_file_selection_new (const gchar *title)
1096 GtkFileSelection *filesel;
1098 filesel = g_object_new (GTK_TYPE_FILE_SELECTION, NULL);
1099 gtk_window_set_title (GTK_WINDOW (filesel), title);
1100 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1102 file_selection_setup_dnd (filesel);
1104 return GTK_WIDGET (filesel);
1108 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1110 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1112 /* delete, create directory, and rename */
1113 if (!filesel->fileop_c_dir)
1115 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1116 g_signal_connect (filesel->fileop_c_dir, "clicked",
1117 G_CALLBACK (gtk_file_selection_create_dir),
1119 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1120 filesel->fileop_c_dir, TRUE, TRUE, 0);
1121 gtk_widget_show (filesel->fileop_c_dir);
1124 if (!filesel->fileop_del_file)
1126 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1127 g_signal_connect (filesel->fileop_del_file, "clicked",
1128 G_CALLBACK (gtk_file_selection_delete_file),
1130 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1131 filesel->fileop_del_file, TRUE, TRUE, 0);
1132 gtk_widget_show (filesel->fileop_del_file);
1135 if (!filesel->fileop_ren_file)
1137 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1138 g_signal_connect (filesel->fileop_ren_file, "clicked",
1139 G_CALLBACK (gtk_file_selection_rename_file),
1141 gtk_box_pack_start (GTK_BOX (filesel->button_area),
1142 filesel->fileop_ren_file, TRUE, TRUE, 0);
1143 gtk_widget_show (filesel->fileop_ren_file);
1146 gtk_file_selection_update_fileops (filesel);
1148 g_object_notify (G_OBJECT (filesel), "show_fileops");
1152 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1154 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1156 if (filesel->fileop_ren_file)
1158 gtk_widget_destroy (filesel->fileop_ren_file);
1159 filesel->fileop_ren_file = NULL;
1162 if (filesel->fileop_del_file)
1164 gtk_widget_destroy (filesel->fileop_del_file);
1165 filesel->fileop_del_file = NULL;
1168 if (filesel->fileop_c_dir)
1170 gtk_widget_destroy (filesel->fileop_c_dir);
1171 filesel->fileop_c_dir = NULL;
1173 g_object_notify (G_OBJECT (filesel), "show_fileops");
1179 * gtk_file_selection_set_filename:
1180 * @filesel: a #GtkFileSelection.
1181 * @filename: a string to set as the default file name.
1183 * Sets a default path for the file requestor. If @filename includes a
1184 * directory path, then the requestor will open with that path as its
1185 * current working directory.
1187 * This has the consequence that in order to open the requestor with a
1188 * working directory and an empty filename, @filename must have a trailing
1189 * directory separator.
1191 * The encoding of @filename is the on-disk encoding, which
1192 * may not be UTF-8. See g_filename_from_utf8().
1195 gtk_file_selection_set_filename (GtkFileSelection *filesel,
1196 const gchar *filename)
1199 const char *name, *last_slash;
1200 char *filename_utf8;
1202 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1203 g_return_if_fail (filename != NULL);
1205 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1206 g_return_if_fail (filename_utf8 != NULL);
1208 last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1212 buf = g_strdup ("");
1213 name = filename_utf8;
1217 buf = g_strdup (filename_utf8);
1218 buf[last_slash - filename_utf8 + 1] = 0;
1219 name = last_slash + 1;
1222 gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1224 if (filesel->selection_entry)
1225 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1227 g_object_notify (G_OBJECT (filesel), "filename");
1229 g_free (filename_utf8);
1233 * gtk_file_selection_get_filename:
1234 * @filesel: a #GtkFileSelection
1236 * This function returns the selected filename in the on-disk encoding
1237 * (see g_filename_from_utf8()), which may or may not be the same as that
1238 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1239 * The returned string points to a statically allocated buffer and
1240 * should be copied if you plan to keep it around.
1242 * If no file is selected then the selected directory path is returned.
1244 * Return value: currently-selected filename in the on-disk encoding.
1246 G_CONST_RETURN gchar*
1247 gtk_file_selection_get_filename (GtkFileSelection *filesel)
1249 static const gchar nothing[2] = "";
1250 static gchar something[MAXPATHLEN*2];
1254 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1256 #ifdef G_WITH_CYGWIN
1257 translate_win32_path (filesel);
1259 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1262 gchar *fullname = cmpl_completion_fullname (text, filesel->cmpl_state);
1263 sys_filename = g_filename_from_utf8 (fullname, -1, NULL, NULL, NULL);
1267 strncpy (something, sys_filename, sizeof (something));
1268 g_free (sys_filename);
1276 gtk_file_selection_complete (GtkFileSelection *filesel,
1277 const gchar *pattern)
1279 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1280 g_return_if_fail (pattern != NULL);
1282 if (filesel->selection_entry)
1283 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1284 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1288 gtk_file_selection_destroy (GtkObject *object)
1290 GtkFileSelection *filesel;
1292 HistoryCallbackArg *callback_arg;
1294 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1296 filesel = GTK_FILE_SELECTION (object);
1298 if (filesel->fileop_dialog)
1300 gtk_widget_destroy (filesel->fileop_dialog);
1301 filesel->fileop_dialog = NULL;
1304 if (filesel->history_list)
1306 list = filesel->history_list;
1309 callback_arg = list->data;
1310 g_free (callback_arg->directory);
1311 g_free (callback_arg);
1314 g_list_free (filesel->history_list);
1315 filesel->history_list = NULL;
1318 if (filesel->cmpl_state)
1320 cmpl_free_state (filesel->cmpl_state);
1321 filesel->cmpl_state = NULL;
1324 if (filesel->selected_names)
1326 free_selected_names (filesel->selected_names);
1327 filesel->selected_names = NULL;
1330 if (filesel->last_selected)
1332 g_free (filesel->last_selected);
1333 filesel->last_selected = NULL;
1336 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1340 gtk_file_selection_map (GtkWidget *widget)
1342 GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1344 /* Refresh the contents */
1345 gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1347 GTK_WIDGET_CLASS (parent_class)->map (widget);
1351 gtk_file_selection_finalize (GObject *object)
1353 GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1355 g_free (filesel->fileop_file);
1357 G_OBJECT_CLASS (parent_class)->finalize (object);
1360 /* Begin file operations callbacks */
1363 gtk_file_selection_fileop_error (GtkFileSelection *fs,
1364 gchar *error_message)
1368 g_return_if_fail (error_message != NULL);
1371 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1372 GTK_DIALOG_DESTROY_WITH_PARENT,
1375 "%s", error_message);
1377 /* yes, we free it */
1378 g_free (error_message);
1380 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1382 g_signal_connect_swapped (dialog, "response",
1383 G_CALLBACK (gtk_widget_destroy),
1386 gtk_widget_show (dialog);
1390 gtk_file_selection_fileop_destroy (GtkWidget *widget,
1393 GtkFileSelection *fs = data;
1395 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1397 fs->fileop_dialog = NULL;
1401 entry_is_empty (GtkEntry *entry)
1403 const gchar *text = gtk_entry_get_text (entry);
1405 return *text == '\0';
1409 gtk_file_selection_fileop_entry_changed (GtkEntry *entry,
1412 gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1416 gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1419 GtkFileSelection *fs = data;
1420 const gchar *dirname;
1423 gchar *sys_full_path;
1425 GError *error = NULL;
1426 CompletionState *cmpl_state;
1428 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1430 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1431 cmpl_state = (CompletionState*) fs->cmpl_state;
1432 path = cmpl_reference_position (cmpl_state);
1434 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1435 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1438 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1439 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1441 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname, error->message,
1442 _("You probably used symbols not allowed in filenames."));
1443 gtk_file_selection_fileop_error (fs, buf);
1444 g_error_free (error);
1448 if (mkdir (sys_full_path, 0777) < 0)
1450 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname,
1451 g_strerror (errno));
1452 gtk_file_selection_fileop_error (fs, buf);
1457 g_free (sys_full_path);
1459 gtk_widget_destroy (fs->fileop_dialog);
1460 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1464 gtk_file_selection_create_dir (GtkWidget *widget,
1467 GtkFileSelection *fs = data;
1473 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1475 if (fs->fileop_dialog)
1479 dialog = gtk_dialog_new ();
1480 fs->fileop_dialog = dialog;
1481 g_signal_connect (dialog, "destroy",
1482 G_CALLBACK (gtk_file_selection_fileop_destroy),
1484 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1485 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1486 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1488 /* If file dialog is grabbed, grab option dialog */
1489 /* When option dialog is closed, file dialog will be grabbed again */
1490 if (GTK_WINDOW (fs)->modal)
1491 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1493 vbox = gtk_vbox_new (FALSE, 0);
1494 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1495 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1497 gtk_widget_show( vbox);
1499 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1500 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1501 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1502 gtk_widget_show (label);
1504 /* The directory entry widget */
1505 fs->fileop_entry = gtk_entry_new ();
1506 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1507 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1509 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1510 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1511 gtk_widget_show (fs->fileop_entry);
1514 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1515 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1516 g_signal_connect_swapped (button, "clicked",
1517 G_CALLBACK (gtk_widget_destroy),
1520 gtk_widget_grab_focus (fs->fileop_entry);
1522 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1523 _("C_reate"), GTK_RESPONSE_OK);
1524 gtk_widget_set_sensitive (button, FALSE);
1525 g_signal_connect (button, "clicked",
1526 G_CALLBACK (gtk_file_selection_create_dir_confirmed),
1528 g_signal_connect (fs->fileop_entry, "changed",
1529 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1532 gtk_widget_grab_default (button);
1534 gtk_widget_show (dialog);
1538 gtk_file_selection_delete_file_response (GtkDialog *dialog,
1542 GtkFileSelection *fs = data;
1543 CompletionState *cmpl_state;
1546 gchar *sys_full_path;
1547 GError *error = NULL;
1550 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1552 if (response_id != GTK_RESPONSE_OK)
1554 gtk_widget_destroy (GTK_WIDGET (dialog));
1558 cmpl_state = (CompletionState*) fs->cmpl_state;
1559 path = cmpl_reference_position (cmpl_state);
1561 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1562 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1565 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1566 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1569 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1570 fs->fileop_file, error->message,
1571 _("It probably contains symbols not allowed in filenames."));
1573 gtk_file_selection_fileop_error (fs, buf);
1574 g_error_free (error);
1578 if (unlink (sys_full_path) < 0)
1580 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1581 fs->fileop_file, g_strerror (errno));
1582 gtk_file_selection_fileop_error (fs, buf);
1587 g_free (sys_full_path);
1589 gtk_widget_destroy (fs->fileop_dialog);
1590 gtk_file_selection_populate (fs, "", FALSE, TRUE);
1594 gtk_file_selection_delete_file (GtkWidget *widget,
1597 GtkFileSelection *fs = data;
1599 const gchar *filename;
1601 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1603 if (fs->fileop_dialog)
1606 #ifdef G_WITH_CYGWIN
1607 translate_win32_path (fs);
1610 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1611 if (strlen (filename) < 1)
1614 g_free (fs->fileop_file);
1615 fs->fileop_file = g_strdup (filename);
1618 fs->fileop_dialog = dialog =
1619 gtk_message_dialog_new (GTK_WINDOW (fs),
1620 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1621 GTK_MESSAGE_QUESTION,
1623 _("Really delete file \"%s\"?"), filename);
1625 g_signal_connect (dialog, "destroy",
1626 G_CALLBACK (gtk_file_selection_fileop_destroy),
1628 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1629 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1632 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1633 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1634 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1637 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1639 g_signal_connect (dialog, "response",
1640 G_CALLBACK (gtk_file_selection_delete_file_response),
1643 gtk_widget_show (dialog);
1647 gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1650 GtkFileSelection *fs = data;
1654 gchar *new_filename;
1655 gchar *old_filename;
1656 gchar *sys_new_filename;
1657 gchar *sys_old_filename;
1658 CompletionState *cmpl_state;
1659 GError *error = NULL;
1661 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1663 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1664 cmpl_state = (CompletionState*) fs->cmpl_state;
1665 path = cmpl_reference_position (cmpl_state);
1667 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1668 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1670 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1673 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1674 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1676 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1677 new_filename, error->message,
1678 _("You probably used symbols not allowed in filenames."));
1679 gtk_file_selection_fileop_error (fs, buf);
1680 g_error_free (error);
1684 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1687 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1688 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1690 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1691 old_filename, error->message,
1692 _("It probably contains symbols not allowed in filenames."));
1693 gtk_file_selection_fileop_error (fs, buf);
1694 g_error_free (error);
1698 if (rename (sys_old_filename, sys_new_filename) < 0)
1700 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1701 sys_old_filename, sys_new_filename,
1702 g_strerror (errno));
1703 gtk_file_selection_fileop_error (fs, buf);
1707 gtk_file_selection_populate (fs, "", FALSE, FALSE);
1708 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1711 g_free (sys_old_filename);
1714 g_free (new_filename);
1715 g_free (old_filename);
1716 g_free (sys_new_filename);
1718 gtk_widget_destroy (fs->fileop_dialog);
1722 gtk_file_selection_rename_file (GtkWidget *widget,
1725 GtkFileSelection *fs = data;
1732 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1734 if (fs->fileop_dialog)
1737 g_free (fs->fileop_file);
1738 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1739 if (strlen (fs->fileop_file) < 1)
1743 fs->fileop_dialog = dialog = gtk_dialog_new ();
1744 g_signal_connect (dialog, "destroy",
1745 G_CALLBACK (gtk_file_selection_fileop_destroy),
1747 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1748 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1749 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1751 /* If file dialog is grabbed, grab option dialog */
1752 /* When option dialog closed, file dialog will be grabbed again */
1753 if (GTK_WINDOW (fs)->modal)
1754 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1756 vbox = gtk_vbox_new (FALSE, 0);
1757 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1758 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1760 gtk_widget_show(vbox);
1762 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1763 label = gtk_label_new (buf);
1764 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1765 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1766 gtk_widget_show (label);
1769 /* New filename entry */
1770 fs->fileop_entry = gtk_entry_new ();
1771 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1773 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1774 gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1775 gtk_widget_show (fs->fileop_entry);
1777 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1778 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1779 0, strlen (fs->fileop_file));
1782 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1783 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1784 g_signal_connect_swapped (button, "clicked",
1785 G_CALLBACK (gtk_widget_destroy),
1788 gtk_widget_grab_focus (fs->fileop_entry);
1790 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1791 _("_Rename"), GTK_RESPONSE_OK);
1792 g_signal_connect (button, "clicked",
1793 G_CALLBACK (gtk_file_selection_rename_file_confirmed),
1795 g_signal_connect (fs->fileop_entry, "changed",
1796 G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1799 gtk_widget_grab_default (button);
1801 gtk_widget_show (dialog);
1805 gtk_file_selection_insert_text (GtkWidget *widget,
1806 const gchar *new_text,
1807 gint new_text_length,
1813 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1817 gdk_display_beep (gtk_widget_get_display (widget));
1818 g_signal_stop_emission_by_name (widget, "insert_text");
1828 gtk_file_selection_update_fileops (GtkFileSelection *fs)
1832 if (!fs->selection_entry)
1835 sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1837 if (fs->fileop_del_file)
1838 gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1840 if (fs->fileop_ren_file)
1841 gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1845 gtk_file_selection_key_press (GtkWidget *widget,
1849 GtkFileSelection *fs;
1852 g_return_val_if_fail (widget != NULL, FALSE);
1853 g_return_val_if_fail (event != NULL, FALSE);
1855 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1856 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1858 fs = GTK_FILE_SELECTION (user_data);
1859 #ifdef G_WITH_CYGWIN
1860 translate_win32_path (fs);
1862 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1864 gtk_file_selection_populate (fs, text, TRUE, TRUE);
1875 gtk_file_selection_history_callback (GtkWidget *widget,
1878 GtkFileSelection *fs = data;
1879 HistoryCallbackArg *callback_arg;
1882 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1884 list = fs->history_list;
1887 callback_arg = list->data;
1889 if (callback_arg->menu_item == widget)
1891 gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1900 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1901 gchar *current_directory)
1903 HistoryCallbackArg *callback_arg;
1904 GtkWidget *menu_item;
1910 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1911 g_return_if_fail (current_directory != NULL);
1913 list = fs->history_list;
1915 if (fs->history_menu)
1918 callback_arg = list->data;
1919 g_free (callback_arg->directory);
1920 g_free (callback_arg);
1923 g_list_free (fs->history_list);
1924 fs->history_list = NULL;
1926 gtk_widget_destroy (fs->history_menu);
1929 fs->history_menu = gtk_menu_new ();
1931 current_dir = g_strdup (current_directory);
1933 dir_len = strlen (current_dir);
1935 for (i = dir_len; i >= 0; i--)
1937 /* the i == dir_len is to catch the full path for the first
1939 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1941 /* another small hack to catch the full path */
1943 current_dir[i + 1] = '\0';
1944 #ifdef G_WITH_CYGWIN
1945 if (!strcmp (current_dir, "//"))
1948 menu_item = gtk_menu_item_new_with_label (current_dir);
1950 callback_arg = g_new (HistoryCallbackArg, 1);
1951 callback_arg->menu_item = menu_item;
1953 /* since the autocompletion gets confused if you don't
1954 * supply a trailing '/' on a dir entry, set the full
1955 * (current) path to "" which just refreshes the filesel */
1958 callback_arg->directory = g_strdup ("");
1962 callback_arg->directory = g_strdup (current_dir);
1965 fs->history_list = g_list_append (fs->history_list, callback_arg);
1967 g_signal_connect (menu_item, "activate",
1968 G_CALLBACK (gtk_file_selection_history_callback),
1970 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1971 gtk_widget_show (menu_item);
1975 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1977 g_free (current_dir);
1981 get_real_filename (gchar *filename,
1984 #ifdef G_WITH_CYGWIN
1985 /* Check to see if the selection was a drive selector */
1986 if (isalpha (filename[0]) && (filename[1] == ':'))
1988 gchar temp_filename[MAX_PATH];
1991 cygwin_conv_to_posix_path (filename, temp_filename);
1993 /* we need trailing '/'. */
1994 len = strlen (temp_filename);
1995 if (len > 0 && temp_filename[len-1] != '/')
1997 temp_filename[len] = '/';
1998 temp_filename[len+1] = '\0';
2004 return g_strdup (temp_filename);
2006 #endif /* G_WITH_CYGWIN */
2011 gtk_file_selection_file_activate (GtkTreeView *tree_view,
2013 GtkTreeViewColumn *column,
2016 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2017 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2021 gtk_tree_model_get_iter (model, &iter, path);
2022 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
2023 filename = get_real_filename (filename, TRUE);
2024 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2025 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
2031 gtk_file_selection_dir_activate (GtkTreeView *tree_view,
2033 GtkTreeViewColumn *column,
2036 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2037 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2041 gtk_tree_model_get_iter (model, &iter, path);
2042 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
2043 filename = get_real_filename (filename, TRUE);
2044 gtk_file_selection_populate (fs, filename, FALSE, FALSE);
2048 #ifdef G_PLATFORM_WIN32
2051 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
2055 char formatBuffer[128];
2058 /* Get the drives string */
2059 GetLogicalDriveStrings (sizeof (buffer), buffer);
2061 /* Add the drives as necessary */
2063 while (*textPtr != '\0')
2065 /* Ignore floppies (?) */
2066 if (GetDriveType (textPtr) != DRIVE_REMOVABLE)
2068 /* Build the actual displayable string */
2069 g_snprintf (formatBuffer, sizeof (formatBuffer), "%c:\\", toupper (textPtr[0]));
2071 /* Add to the list */
2072 gtk_list_store_append (model, &iter);
2073 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
2075 textPtr += (strlen (textPtr) + 1);
2081 escape_underscores (const gchar *str)
2083 GString *result = g_string_new (NULL);
2087 g_string_append_c (result, '_');
2089 g_string_append_c (result, *str);
2093 return g_string_free (result, FALSE);
2097 gtk_file_selection_populate (GtkFileSelection *fs,
2099 gboolean try_complete,
2100 gboolean reset_entry)
2102 CompletionState *cmpl_state;
2103 PossibleCompletion* poss;
2105 GtkListStore *dir_model;
2106 GtkListStore *file_model;
2108 gchar* rem_path = rel_path;
2110 gint did_recurse = FALSE;
2111 gint possible_count = 0;
2113 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
2115 cmpl_state = (CompletionState*) fs->cmpl_state;
2116 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2118 if (!cmpl_state_okay (cmpl_state))
2120 /* Something went wrong. */
2121 gtk_file_selection_abort (fs);
2125 g_assert (cmpl_state->reference_dir);
2127 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2128 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2130 gtk_list_store_clear (dir_model);
2131 gtk_list_store_clear (file_model);
2133 /* Set the dir list to include ./ and ../ */
2134 gtk_list_store_append (dir_model, &iter);
2135 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2136 gtk_list_store_append (dir_model, &iter);
2137 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2141 if (cmpl_is_a_completion (poss))
2143 possible_count += 1;
2145 filename = cmpl_this_completion (poss);
2147 if (cmpl_is_directory (poss))
2149 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2150 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2152 gtk_list_store_append (dir_model, &iter);
2153 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2158 gtk_list_store_append (file_model, &iter);
2159 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2163 poss = cmpl_next_completion (cmpl_state);
2166 #ifdef G_PLATFORM_WIN32
2167 /* For Windows, add drives as potential selections */
2168 win32_gtk_add_drives_to_dir_list (dir_model);
2171 /* File lists are set. */
2173 g_assert (cmpl_state->reference_dir);
2178 /* User is trying to complete filenames, so advance the user's input
2179 * string to the updated_text, which is the common leading substring
2180 * of all possible completions, and if its a directory attempt
2181 * attempt completions in it. */
2183 if (cmpl_updated_text (cmpl_state)[0])
2186 if (cmpl_updated_dir (cmpl_state))
2188 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2192 gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2198 if (fs->selection_entry)
2199 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2200 cmpl_updated_text (cmpl_state));
2205 if (fs->selection_entry)
2206 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2209 else if (reset_entry)
2211 if (fs->selection_entry)
2212 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2217 if (fs->selection_entry && try_complete)
2218 gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry), -1);
2220 if (fs->selection_entry)
2222 char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2223 sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2226 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2230 if (fs->history_pulldown)
2232 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2239 gtk_file_selection_abort (GtkFileSelection *fs)
2243 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2245 /* BEEP gdk_beep(); */
2247 if (fs->selection_entry)
2248 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2252 * gtk_file_selection_set_select_multiple:
2253 * @filesel: a #GtkFileSelection
2254 * @select_multiple: whether or not the user is allowed to select multiple
2255 * files in the file list.
2257 * Sets whether the user is allowed to select multiple files in the file list.
2258 * Use gtk_file_selection_get_selections () to get the list of selected files.
2261 gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2262 gboolean select_multiple)
2264 GtkTreeSelection *sel;
2265 GtkSelectionMode mode;
2267 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2269 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2271 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2273 if (mode != gtk_tree_selection_get_mode (sel))
2275 gtk_tree_selection_set_mode (sel, mode);
2277 g_object_notify (G_OBJECT (filesel), "select-multiple");
2282 * gtk_file_selection_get_select_multiple:
2283 * @filesel: a #GtkFileSelection
2285 * Determines whether or not the user is allowed to select multiple files in
2286 * the file list. See gtk_file_selection_set_select_multiple().
2288 * Return value: %TRUE if the user is allowed to select multiple files in the
2292 gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2294 GtkTreeSelection *sel;
2296 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2298 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2299 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2303 multiple_changed_foreach (GtkTreeModel *model,
2308 GPtrArray *names = data;
2311 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2313 g_ptr_array_add (names, filename);
2317 free_selected_names (GPtrArray *names)
2321 for (i = 0; i < names->len; i++)
2322 g_free (g_ptr_array_index (names, i));
2324 g_ptr_array_free (names, TRUE);
2328 gtk_file_selection_file_changed (GtkTreeSelection *selection,
2331 GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2332 GPtrArray *new_names;
2337 new_names = g_ptr_array_sized_new (8);
2339 gtk_tree_selection_selected_foreach (selection,
2340 multiple_changed_foreach,
2343 /* nothing selected */
2344 if (new_names->len == 0)
2346 g_ptr_array_free (new_names, TRUE);
2348 if (fs->selected_names != NULL)
2350 free_selected_names (fs->selected_names);
2351 fs->selected_names = NULL;
2354 goto maybe_clear_entry;
2357 if (new_names->len != 1)
2359 GPtrArray *old_names = fs->selected_names;
2361 if (old_names != NULL)
2363 /* A common case is selecting a range of files from top to bottom,
2364 * so quickly check for that to avoid looping over the entire list
2366 if (compare_utf8_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2367 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2368 index = new_names->len - 1;
2371 gint i = 0, j = 0, cmp;
2373 /* do a quick diff, stopping at the first file not in the
2376 while (i < old_names->len && j < new_names->len)
2378 cmp = compare_utf8_filenames (g_ptr_array_index (old_names, i),
2379 g_ptr_array_index (new_names, j));
2396 /* we ran off the end of the old list */
2397 if (index == -1 && i < new_names->len)
2403 /* A phantom anchor still exists at the point where the last item
2404 * was selected, which is used for subsequent range selections.
2405 * So search up from there.
2407 if (fs->last_selected &&
2408 compare_utf8_filenames (fs->last_selected,
2409 g_ptr_array_index (new_names, 0)) == 0)
2410 index = new_names->len - 1;
2418 if (fs->selected_names != NULL)
2419 free_selected_names (fs->selected_names);
2421 fs->selected_names = new_names;
2425 if (fs->last_selected != NULL)
2426 g_free (fs->last_selected);
2428 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2429 filename = get_real_filename (fs->last_selected, FALSE);
2431 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2433 if (filename != fs->last_selected)
2441 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2442 if ((entry != NULL) && (fs->last_selected != NULL) &&
2443 (compare_utf8_filenames (entry, fs->last_selected) == 0))
2444 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2448 * gtk_file_selection_get_selections:
2449 * @filesel: a #GtkFileSelection
2451 * Retrieves the list of file selections the user has made in the dialog box.
2452 * This function is intended for use when the user can select multiple files
2453 * in the file list. The first file in the list is equivalent to what
2454 * gtk_file_selection_get_filename() would return.
2456 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2457 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2458 * g_filename_to_utf8() on each string.
2460 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2461 * g_strfreev() to free it.
2464 gtk_file_selection_get_selections (GtkFileSelection *filesel)
2468 gchar *filename, *dirname;
2469 gchar *current, *buf;
2471 gboolean unselected_entry;
2473 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2475 filename = g_strdup (gtk_file_selection_get_filename (filesel));
2477 if (strlen (filename) == 0)
2483 names = filesel->selected_names;
2486 selections = g_new (gchar *, names->len + 2);
2488 selections = g_new (gchar *, 2);
2491 unselected_entry = TRUE;
2495 dirname = g_path_get_dirname (filename);
2497 for (i = 0; i < names->len; i++)
2499 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2501 current = g_build_filename (dirname, buf, NULL);
2504 selections[count++] = current;
2506 if (unselected_entry && compare_sys_filenames (current, filename) == 0)
2507 unselected_entry = FALSE;
2513 if (unselected_entry)
2514 selections[count++] = filename;
2518 selections[count] = NULL;
2523 /**********************************************************************/
2524 /* External Interface */
2525 /**********************************************************************/
2527 /* The four completion state selectors
2530 cmpl_updated_text (CompletionState *cmpl_state)
2532 return cmpl_state->updated_text;
2536 cmpl_updated_dir (CompletionState *cmpl_state)
2538 return cmpl_state->re_complete;
2542 cmpl_reference_position (CompletionState *cmpl_state)
2544 return cmpl_state->reference_dir->fullname;
2548 /* This doesn't work currently and would require changes
2549 * to fnmatch.c to get working.
2552 cmpl_last_valid_char (CompletionState *cmpl_state)
2554 return cmpl_state->last_valid_char;
2559 cmpl_completion_fullname (const gchar *text,
2560 CompletionState *cmpl_state)
2562 if (!cmpl_state_okay (cmpl_state))
2564 return g_strdup ("");
2566 else if (g_path_is_absolute (text))
2568 return g_strdup (text);
2571 else if (text[0] == '~')
2576 dir = open_user_dir (text, cmpl_state);
2580 slash = strchr (text, G_DIR_SEPARATOR);
2582 /* slash may be NULL, that works too */
2583 return g_strconcat (dir->fullname, slash, NULL);
2588 return g_build_filename (cmpl_state->reference_dir->fullname,
2593 /* The three completion selectors
2596 cmpl_this_completion (PossibleCompletion* pc)
2602 cmpl_is_directory (PossibleCompletion* pc)
2604 return pc->is_directory;
2608 cmpl_is_a_completion (PossibleCompletion* pc)
2610 return pc->is_a_completion;
2613 /**********************************************************************/
2614 /* Construction, deletion */
2615 /**********************************************************************/
2617 /* Get the nearest parent of the current directory for which
2618 * we can convert the filename into UTF-8. With paranoia.
2619 * Returns "." when all goes wrong.
2622 get_current_dir_utf8 (void)
2624 gchar *dir = g_get_current_dir ();
2625 gchar *dir_utf8 = NULL;
2631 dir_utf8 = g_filename_to_utf8 (dir, -1, NULL, NULL, NULL);
2635 last_slash = strrchr (dir, G_DIR_SEPARATOR);
2636 if (!last_slash) /* g_get_current_dir() wasn't absolute! */
2639 if (last_slash + 1 == g_path_skip_root (dir)) /* Parent directory is a root directory */
2641 if (last_slash[1] == '\0') /* Root misencoded! */
2644 last_slash[1] = '\0';
2647 last_slash[0] = '\0';
2649 g_assert (last_slash);
2654 return dir_utf8 ? dir_utf8 : g_strdup (".");
2657 static CompletionState*
2658 cmpl_init_state (void)
2661 CompletionState *new_state;
2663 new_state = g_new (CompletionState, 1);
2665 utf8_cwd = get_current_dir_utf8 ();
2669 new_state->reference_dir = NULL;
2670 new_state->completion_dir = NULL;
2671 new_state->active_completion_dir = NULL;
2672 new_state->directory_storage = NULL;
2673 new_state->directory_sent_storage = NULL;
2674 new_state->last_valid_char = 0;
2675 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2676 new_state->updated_text_alloc = MAXPATHLEN;
2677 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2678 new_state->the_completion.text_alloc = MAXPATHLEN;
2679 new_state->user_dir_name_buffer = NULL;
2680 new_state->user_directories = NULL;
2682 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2684 if (!new_state->reference_dir)
2686 /* Directories changing from underneath us, grumble */
2687 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2696 cmpl_free_dir_list (GList* dp0)
2702 free_dir (dp->data);
2710 cmpl_free_dir_sent_list (GList* dp0)
2716 free_dir_sent (dp->data);
2724 cmpl_free_state (CompletionState* cmpl_state)
2726 g_return_if_fail (cmpl_state != NULL);
2728 cmpl_free_dir_list (cmpl_state->directory_storage);
2729 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2731 if (cmpl_state->user_dir_name_buffer)
2732 g_free (cmpl_state->user_dir_name_buffer);
2733 if (cmpl_state->user_directories)
2734 g_free (cmpl_state->user_directories);
2735 if (cmpl_state->the_completion.text)
2736 g_free (cmpl_state->the_completion.text);
2737 if (cmpl_state->updated_text)
2738 g_free (cmpl_state->updated_text);
2740 g_free (cmpl_state);
2744 free_dir (CompletionDir* dir)
2746 g_free (dir->cmpl_text);
2747 g_free (dir->fullname);
2752 free_dir_sent (CompletionDirSent* sent)
2755 for (i = 0; i < sent->entry_count; i++)
2757 g_free (sent->entries[i].entry_name);
2758 g_free (sent->entries[i].sort_key);
2760 g_free (sent->entries);
2765 prune_memory_usage (CompletionState *cmpl_state)
2767 GList* cdsl = cmpl_state->directory_sent_storage;
2768 GList* cdl = cmpl_state->directory_storage;
2772 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2777 cmpl_free_dir_sent_list (cdsl->next);
2781 cmpl_state->directory_storage = NULL;
2784 if (cdl->data == cmpl_state->reference_dir)
2785 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2787 free_dir (cdl->data);
2794 /**********************************************************************/
2795 /* The main entrances. */
2796 /**********************************************************************/
2798 static PossibleCompletion*
2799 cmpl_completion_matches (gchar *text_to_complete,
2800 gchar **remaining_text,
2801 CompletionState *cmpl_state)
2806 PossibleCompletion *poss;
2808 prune_memory_usage (cmpl_state);
2810 g_assert (text_to_complete != NULL);
2812 cmpl_state->user_completion_index = -1;
2813 cmpl_state->last_completion_text = text_to_complete;
2814 cmpl_state->the_completion.text[0] = 0;
2815 cmpl_state->last_valid_char = 0;
2816 cmpl_state->updated_text_len = -1;
2817 cmpl_state->updated_text[0] = 0;
2818 cmpl_state->re_complete = FALSE;
2821 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2823 if (text_to_complete[0] == '~' && !first_slash)
2825 /* Text starts with ~ and there is no slash, show all the
2826 * home directory completions.
2828 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2830 update_cmpl (poss, cmpl_state);
2835 cmpl_state->reference_dir =
2836 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2838 if (!cmpl_state->reference_dir)
2841 cmpl_state->completion_dir =
2842 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2844 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2846 if (!cmpl_state->completion_dir)
2849 cmpl_state->completion_dir->cmpl_index = -1;
2850 cmpl_state->completion_dir->cmpl_parent = NULL;
2851 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2853 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2855 cmpl_state->reference_dir = cmpl_state->completion_dir;
2857 poss = attempt_file_completion (cmpl_state);
2859 update_cmpl (poss, cmpl_state);
2864 static PossibleCompletion*
2865 cmpl_next_completion (CompletionState* cmpl_state)
2867 PossibleCompletion* poss = NULL;
2869 cmpl_state->the_completion.text[0] = 0;
2872 if (cmpl_state->user_completion_index >= 0)
2873 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2875 poss = attempt_file_completion (cmpl_state);
2877 poss = attempt_file_completion (cmpl_state);
2880 update_cmpl (poss, cmpl_state);
2885 /**********************************************************************/
2886 /* Directory Operations */
2887 /**********************************************************************/
2889 /* Open the directory where completion will begin from, if possible. */
2890 static CompletionDir*
2891 open_ref_dir (gchar *text_to_complete,
2892 gchar **remaining_text,
2893 CompletionState *cmpl_state)
2896 CompletionDir *new_dir;
2898 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2900 #ifdef G_WITH_CYGWIN
2901 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2904 g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2906 new_dir = open_dir (root_dir, cmpl_state);
2909 *remaining_text = text_to_complete + 4;
2917 else if (text_to_complete[0] == '~')
2919 new_dir = open_user_dir (text_to_complete, cmpl_state);
2924 *remaining_text = first_slash + 1;
2926 *remaining_text = text_to_complete + strlen (text_to_complete);
2934 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2936 gchar *tmp = g_strdup (text_to_complete);
2940 while (*p && *p != '*' && *p != '?')
2944 p = strrchr (tmp, G_DIR_SEPARATOR);
2947 if (p + 1 == g_path_skip_root (tmp))
2951 new_dir = open_dir (tmp, cmpl_state);
2954 *remaining_text = text_to_complete +
2955 ((p == g_path_skip_root (tmp)) ? (p - tmp) : (p + 1 - tmp));
2959 /* If no possible candidates, use the cwd */
2960 gchar *utf8_curdir = get_current_dir_utf8 ();
2962 new_dir = open_dir (utf8_curdir, cmpl_state);
2965 *remaining_text = text_to_complete;
2967 g_free (utf8_curdir);
2974 *remaining_text = text_to_complete;
2976 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2981 new_dir->cmpl_index = -1;
2982 new_dir->cmpl_parent = NULL;
2990 /* open a directory by user name */
2991 static CompletionDir*
2992 open_user_dir (const gchar *text_to_complete,
2993 CompletionState *cmpl_state)
2995 CompletionDir *result;
2999 g_assert (text_to_complete && text_to_complete[0] == '~');
3001 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3004 cmp_len = first_slash - text_to_complete - 1;
3006 cmp_len = strlen (text_to_complete + 1);
3011 const gchar *homedir = g_get_home_dir ();
3012 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
3015 result = open_dir (utf8_homedir, cmpl_state);
3019 g_free (utf8_homedir);
3024 gchar* copy = g_new (char, cmp_len + 1);
3028 strncpy (copy, text_to_complete + 1, cmp_len);
3030 pwd = getpwnam (copy);
3037 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
3038 result = open_dir (utf8_dir, cmpl_state);
3046 /* open a directory relative the the current relative directory */
3047 static CompletionDir*
3048 open_relative_dir (gchar *dir_name,
3050 CompletionState *cmpl_state)
3052 CompletionDir *result;
3055 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
3056 g_string_assign (path, dir->fullname);
3058 if (dir->fullname_len > 1
3059 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
3060 g_string_append_c (path, G_DIR_SEPARATOR);
3061 g_string_append (path, dir_name);
3063 result = open_dir (path->str, cmpl_state);
3065 g_string_free (path, TRUE);
3070 /* after the cache lookup fails, really open a new directory */
3071 static CompletionDirSent*
3072 open_new_dir (gchar *dir_name,
3074 gboolean stat_subdirs)
3076 CompletionDirSent *sent;
3079 GError *error = NULL;
3080 gint entry_count = 0;
3083 struct stat ent_sbuf;
3085 gchar *sys_dir_name;
3087 sent = g_new (CompletionDirSent, 1);
3088 #ifndef G_PLATFORM_WIN32
3089 sent->mtime = sbuf->st_mtime;
3090 sent->inode = sbuf->st_ino;
3091 sent->device = sbuf->st_dev;
3093 path = g_string_sized_new (2*MAXPATHLEN + 10);
3095 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3098 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3102 directory = g_dir_open (sys_dir_name, 0, &error);
3105 cmpl_errno = error->code; /* ??? */
3106 g_free (sys_dir_name);
3110 while ((dirent = g_dir_read_name (directory)) != NULL)
3112 entry_count += 2; /* For ".",".." */
3114 sent->entries = g_new (CompletionDirEntry, entry_count);
3115 sent->entry_count = entry_count;
3117 g_dir_rewind (directory);
3119 for (i = 0; i < entry_count; i += 1)
3121 GError *error = NULL;
3129 dirent = g_dir_read_name (directory);
3130 if (!dirent) /* Directory changed */
3134 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3135 if (sent->entries[n_entries].entry_name == NULL
3136 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3138 gchar *escaped_str = g_strescape (dirent, NULL);
3139 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3140 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3142 error->message ? error->message : _("Invalid UTF-8"));
3143 g_free (escaped_str);
3144 g_clear_error (&error);
3147 g_clear_error (&error);
3149 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3151 g_string_assign (path, sys_dir_name);
3152 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3154 g_string_append_c (path, G_DIR_SEPARATOR);
3156 g_string_append (path, dirent);
3160 /* Here we know path->str is a "system charset" string */
3161 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3162 sent->entries[n_entries].is_dir = TRUE;
3164 /* stat may fail, and we don't mind, since it could be a
3165 * dangling symlink. */
3166 sent->entries[n_entries].is_dir = FALSE;
3169 sent->entries[n_entries].is_dir = 1;
3173 sent->entry_count = n_entries;
3175 g_free (sys_dir_name);
3176 g_string_free (path, TRUE);
3177 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3179 g_dir_close (directory);
3184 #ifndef G_PLATFORM_WIN32
3187 check_dir (gchar *dir_name,
3188 struct stat *result,
3189 gboolean *stat_subdirs)
3191 /* A list of directories that we know only contain other directories.
3192 * Trying to stat every file in these directories would be very
3199 struct stat statbuf;
3200 } no_stat_dirs[] = {
3201 { "/afs", FALSE, { 0 } },
3202 { "/net", FALSE, { 0 } }
3205 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3206 static gboolean initialized = FALSE;
3207 gchar *sys_dir_name;
3213 for (i = 0; i < n_no_stat_dirs; i++)
3215 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3216 no_stat_dirs[i].present = TRUE;
3220 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3223 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3227 if (stat (sys_dir_name, result) < 0)
3229 g_free (sys_dir_name);
3233 g_free (sys_dir_name);
3235 *stat_subdirs = TRUE;
3236 for (i = 0; i < n_no_stat_dirs; i++)
3238 if (no_stat_dirs[i].present &&
3239 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3240 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3242 *stat_subdirs = FALSE;
3252 /* open a directory by absolute pathname */
3253 static CompletionDir*
3254 open_dir (gchar *dir_name,
3255 CompletionState *cmpl_state)
3257 #ifndef G_PLATFORM_WIN32
3259 gboolean stat_subdirs;
3262 CompletionDirSent *sent;
3264 #ifndef G_PLATFORM_WIN32
3265 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3268 cdsl = cmpl_state->directory_sent_storage;
3274 if (sent->inode == sbuf.st_ino &&
3275 sent->mtime == sbuf.st_mtime &&
3276 sent->device == sbuf.st_dev)
3277 return attach_dir (sent, dir_name, cmpl_state);
3282 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3284 sent = open_new_dir (dir_name, NULL, TRUE);
3289 cmpl_state->directory_sent_storage =
3290 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3292 return attach_dir (sent, dir_name, cmpl_state);
3298 static CompletionDir*
3299 attach_dir (CompletionDirSent *sent,
3301 CompletionState *cmpl_state)
3303 CompletionDir* new_dir;
3305 new_dir = g_new (CompletionDir, 1);
3307 cmpl_state->directory_storage =
3308 g_list_prepend (cmpl_state->directory_storage, new_dir);
3310 new_dir->sent = sent;
3311 new_dir->fullname = g_strdup (dir_name);
3312 new_dir->fullname_len = strlen (dir_name);
3313 new_dir->cmpl_text = NULL;
3319 correct_dir_fullname (CompletionDir* cmpl_dir)
3321 gint length = strlen (cmpl_dir->fullname);
3322 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3323 gchar *sys_filename;
3326 /* Does it end with /. (\.) ? */
3328 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3330 /* Is it just the root directory (on a drive) ? */
3331 if (cmpl_dir->fullname + length - 2 == first_slash)
3333 cmpl_dir->fullname[length - 1] = 0;
3334 cmpl_dir->fullname_len = length - 1;
3339 cmpl_dir->fullname[length - 2] = 0;
3343 /* Ends with /./ (\.\)? */
3344 else if (length >= 3 &&
3345 strcmp (cmpl_dir->fullname + length - 3,
3346 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3347 cmpl_dir->fullname[length - 2] = 0;
3349 /* Ends with /.. (\..) ? */
3350 else if (length >= 3 &&
3351 strcmp (cmpl_dir->fullname + length - 3,
3352 G_DIR_SEPARATOR_S "..") == 0)
3354 /* Is it just /.. (X:\..)? */
3355 if (cmpl_dir->fullname + length - 3 == first_slash)
3357 cmpl_dir->fullname[length - 2] = 0;
3358 cmpl_dir->fullname_len = length - 2;
3362 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3365 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3369 if (stat (sys_filename, &sbuf) < 0)
3371 g_free (sys_filename);
3375 g_free (sys_filename);
3377 cmpl_dir->fullname[length - 3] = 0;
3379 if (!correct_parent (cmpl_dir, &sbuf))
3383 /* Ends with /../ (\..\)? */
3384 else if (length >= 4 &&
3385 strcmp (cmpl_dir->fullname + length - 4,
3386 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3388 /* Is it just /../ (X:\..\)? */
3389 if (cmpl_dir->fullname + length - 4 == first_slash)
3391 cmpl_dir->fullname[length - 3] = 0;
3392 cmpl_dir->fullname_len = length - 3;
3396 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3399 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3403 if (stat (sys_filename, &sbuf) < 0)
3405 g_free (sys_filename);
3409 g_free (sys_filename);
3411 cmpl_dir->fullname[length - 4] = 0;
3413 if (!correct_parent (cmpl_dir, &sbuf))
3417 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3423 correct_parent (CompletionDir *cmpl_dir,
3429 #ifndef G_PLATFORM_WIN32
3432 gchar *sys_filename;
3435 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3436 g_assert (last_slash);
3437 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3439 /* Clever (?) way to check for top-level directory that works also on
3440 * Win32, where there is a drive letter and colon prefixed...
3442 if (last_slash != first_slash)
3452 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3455 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3457 last_slash[0] = G_DIR_SEPARATOR;
3461 if (stat (sys_filename, &parbuf) < 0)
3463 g_free (sys_filename);
3466 last_slash[0] = G_DIR_SEPARATOR;
3469 g_free (sys_filename);
3471 #ifndef G_PLATFORM_WIN32 /* No inode numbers on Win32 */
3472 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3473 /* it wasn't a link */
3479 last_slash[0] = G_DIR_SEPARATOR;
3481 /* it was a link, have to figure it out the hard way */
3483 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3488 g_free (cmpl_dir->fullname);
3490 cmpl_dir->fullname = new_name;
3496 #ifndef G_PLATFORM_WIN32
3499 find_parent_dir_fullname (gchar* dirname)
3501 gchar *sys_orig_dir;
3506 sys_orig_dir = g_get_current_dir ();
3507 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3510 g_free (sys_orig_dir);
3511 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3515 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3518 chdir (sys_orig_dir);
3519 g_free (sys_dirname);
3520 g_free (sys_orig_dir);
3523 g_free (sys_dirname);
3525 sys_cwd = g_get_current_dir ();
3526 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3529 if (chdir (sys_orig_dir) != 0)
3532 g_free (sys_orig_dir);
3536 g_free (sys_orig_dir);
3542 /**********************************************************************/
3543 /* Completion Operations */
3544 /**********************************************************************/
3548 static PossibleCompletion*
3549 attempt_homedir_completion (gchar *text_to_complete,
3550 CompletionState *cmpl_state)
3554 if (!cmpl_state->user_dir_name_buffer &&
3555 !get_pwdb (cmpl_state))
3557 length = strlen (text_to_complete) - 1;
3559 cmpl_state->user_completion_index += 1;
3561 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3563 index = first_diff_index (text_to_complete + 1,
3564 cmpl_state->user_directories
3565 [cmpl_state->user_completion_index].login);
3572 if (cmpl_state->last_valid_char < (index + 1))
3573 cmpl_state->last_valid_char = index + 1;
3574 cmpl_state->user_completion_index += 1;
3578 cmpl_state->the_completion.is_a_completion = 1;
3579 cmpl_state->the_completion.is_directory = TRUE;
3581 append_completion_text ("~", cmpl_state);
3583 append_completion_text (cmpl_state->
3584 user_directories[cmpl_state->user_completion_index].login,
3587 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3590 if (text_to_complete[1]
3591 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3593 cmpl_state->user_completion_index = -1;
3598 cmpl_state->user_completion_index += 1;
3599 cmpl_state->the_completion.is_a_completion = 1;
3600 cmpl_state->the_completion.is_directory = TRUE;
3602 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3608 #ifdef G_PLATFORM_WIN32
3609 /* FIXME: determine whether we should casefold all Unicode letters
3610 * here, too (and in in first_diff_index() walk through the strings with
3611 * g_utf8_next_char()), or if this folding isn't actually needed at
3614 #define FOLD(c) (tolower(c))
3619 /* returns the index (>= 0) of the first differing character,
3620 * PATTERN_MATCH if the completion matches */
3622 first_diff_index (gchar *pat,
3627 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3637 return PATTERN_MATCH;
3640 static PossibleCompletion*
3641 append_completion_text (gchar *text,
3642 CompletionState *cmpl_state)
3646 if (!cmpl_state->the_completion.text)
3649 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3651 if (cmpl_state->the_completion.text_alloc > len)
3653 strcat (cmpl_state->the_completion.text, text);
3654 return &cmpl_state->the_completion;
3660 cmpl_state->the_completion.text_alloc = i;
3662 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3664 if (!cmpl_state->the_completion.text)
3668 strcat (cmpl_state->the_completion.text, text);
3669 return &cmpl_state->the_completion;
3673 static CompletionDir*
3674 find_completion_dir (gchar *text_to_complete,
3675 gchar **remaining_text,
3676 CompletionState *cmpl_state)
3678 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3679 CompletionDir* dir = cmpl_state->reference_dir;
3680 CompletionDir* next;
3681 *remaining_text = text_to_complete;
3685 gint len = first_slash - *remaining_text;
3687 gchar *found_name = NULL; /* Quiet gcc */
3689 gchar* pat_buf = g_new (gchar, len + 1);
3691 strncpy (pat_buf, *remaining_text, len);
3694 for (i = 0; i < dir->sent->entry_count; i += 1)
3696 if (dir->sent->entries[i].is_dir &&
3697 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name))
3707 found_name = dir->sent->entries[i].entry_name;
3714 /* Perhaps we are trying to open an automount directory */
3715 found_name = pat_buf;
3718 next = open_relative_dir (found_name, dir, cmpl_state);
3726 next->cmpl_parent = dir;
3730 if (!correct_dir_fullname (dir))
3736 *remaining_text = first_slash + 1;
3737 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3746 update_cmpl (PossibleCompletion *poss,
3747 CompletionState *cmpl_state)
3751 if (!poss || !cmpl_is_a_completion (poss))
3754 cmpl_len = strlen (cmpl_this_completion (poss));
3756 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3758 cmpl_state->updated_text_alloc = 2*cmpl_len;
3759 cmpl_state->updated_text =
3760 (gchar*)g_realloc (cmpl_state->updated_text,
3761 cmpl_state->updated_text_alloc);
3764 if (cmpl_state->updated_text_len < 0)
3766 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3767 cmpl_state->updated_text_len = cmpl_len;
3768 cmpl_state->re_complete = cmpl_is_directory (poss);
3770 else if (cmpl_state->updated_text_len == 0)
3772 cmpl_state->re_complete = FALSE;
3777 first_diff_index (cmpl_state->updated_text,
3778 cmpl_this_completion (poss));
3780 cmpl_state->re_complete = FALSE;
3782 if (first_diff == PATTERN_MATCH)
3785 if (first_diff > cmpl_state->updated_text_len)
3786 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3788 cmpl_state->updated_text_len = first_diff;
3789 cmpl_state->updated_text[first_diff] = 0;
3793 static PossibleCompletion*
3794 attempt_file_completion (CompletionState *cmpl_state)
3796 gchar *pat_buf, *first_slash;
3797 CompletionDir *dir = cmpl_state->active_completion_dir;
3799 dir->cmpl_index += 1;
3801 if (dir->cmpl_index == dir->sent->entry_count)
3803 if (dir->cmpl_parent == NULL)
3805 cmpl_state->active_completion_dir = NULL;
3811 cmpl_state->active_completion_dir = dir->cmpl_parent;
3813 return attempt_file_completion (cmpl_state);
3817 g_assert (dir->cmpl_text);
3819 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3823 gint len = first_slash - dir->cmpl_text;
3825 pat_buf = g_new (gchar, len + 1);
3826 strncpy (pat_buf, dir->cmpl_text, len);
3831 gint len = strlen (dir->cmpl_text);
3833 pat_buf = g_new (gchar, len + 2);
3834 strcpy (pat_buf, dir->cmpl_text);
3835 /* Don't append a * if the user entered one herself.
3836 * This way one can complete *.h and don't get matches
3837 * on any .help files, for instance.
3839 if (strchr (pat_buf, '*') == NULL)
3840 strcpy (pat_buf + len, "*");
3845 if (dir->sent->entries[dir->cmpl_index].is_dir)
3847 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name))
3849 CompletionDir* new_dir;
3851 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3860 new_dir->cmpl_parent = dir;
3862 new_dir->cmpl_index = -1;
3863 new_dir->cmpl_text = g_strdup (first_slash + 1);
3865 cmpl_state->active_completion_dir = new_dir;
3868 return attempt_file_completion (cmpl_state);
3873 return attempt_file_completion (cmpl_state);
3879 return attempt_file_completion (cmpl_state);
3884 if (dir->cmpl_parent != NULL)
3886 append_completion_text (dir->fullname +
3887 strlen (cmpl_state->completion_dir->fullname) + 1,
3889 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3892 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3894 cmpl_state->the_completion.is_a_completion =
3895 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name);
3897 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3898 if (dir->sent->entries[dir->cmpl_index].is_dir)
3899 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3902 return &cmpl_state->the_completion;
3909 get_pwdb (CompletionState* cmpl_state)
3911 struct passwd *pwd_ptr;
3914 gint len = 0, i, count = 0;
3916 if (cmpl_state->user_dir_name_buffer)
3920 while ((pwd_ptr = getpwent ()) != NULL)
3922 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3923 len += strlen (utf8);
3925 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3926 len += strlen (utf8);
3934 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3935 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3936 cmpl_state->user_directories_len = count;
3938 buf_ptr = cmpl_state->user_dir_name_buffer;
3940 for (i = 0; i < count; i += 1)
3942 pwd_ptr = getpwent ();
3949 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3950 strcpy (buf_ptr, utf8);
3953 cmpl_state->user_directories[i].login = buf_ptr;
3955 buf_ptr += strlen (buf_ptr);
3958 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3959 strcpy (buf_ptr, utf8);
3962 cmpl_state->user_directories[i].homedir = buf_ptr;
3964 buf_ptr += strlen (buf_ptr);
3968 qsort (cmpl_state->user_directories,
3969 cmpl_state->user_directories_len,
3970 sizeof (CompletionUserDir),
3979 if (cmpl_state->user_dir_name_buffer)
3980 g_free (cmpl_state->user_dir_name_buffer);
3981 if (cmpl_state->user_directories)
3982 g_free (cmpl_state->user_directories);
3984 cmpl_state->user_dir_name_buffer = NULL;
3985 cmpl_state->user_directories = NULL;
3991 compare_user_dir (const void *a,
3994 return strcmp ((((CompletionUserDir*)a))->login,
3995 (((CompletionUserDir*)b))->login);
4001 compare_cmpl_dir (const void *a,
4005 return strcmp (((CompletionDirEntry*)a)->sort_key,
4006 (((CompletionDirEntry*)b))->sort_key);
4010 cmpl_state_okay (CompletionState* cmpl_state)
4012 return cmpl_state && cmpl_state->reference_dir;
4016 cmpl_strerror (gint err)
4018 if (err == CMPL_ERRNO_TOO_LONG)
4019 return _("Name too long");
4020 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
4021 return _("Couldn't convert filename");
4023 return g_strerror (err);