1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 XangXiu Yang
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 #include <sys/types.h>
24 #include <limits.h> // For PATH_MAX
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
38 #include <glib.h> /* Include early to get G_OS_WIN32 and
41 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
46 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
48 #include <winsock.h> /* For gethostname */
51 #include "gdk/gdkkeysyms.h"
53 #include <lttvwindow/gtkdirsel.h>
61 #define mkdir(p,m) _mkdir(p)
63 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
65 #endif /* G_OS_WIN32 */
68 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
71 #define DIR_LIST_WIDTH 180
72 #define DIR_LIST_HEIGHT 180
73 #define FILE_LIST_WIDTH 180
74 #define FILE_LIST_HEIGHT 180
76 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
77 * in here, since the rest of the code in the file does require some
82 # define MAXPATHLEN PATH_MAX
84 # define MAXPATHLEN 2048
88 /* I've put this here so it doesn't get confused with the
89 * file completion interface */
90 typedef struct _HistoryCallbackArg HistoryCallbackArg
;
92 struct _HistoryCallbackArg
99 typedef struct _CompletionState CompletionState
;
100 typedef struct _CompletionDir CompletionDir
;
101 typedef struct _CompletionDirSent CompletionDirSent
;
102 typedef struct _CompletionDirEntry CompletionDirEntry
;
103 typedef struct _CompletionUserDir CompletionUserDir
;
104 typedef struct _PossibleCompletion PossibleCompletion
;
106 /* Non-external file completion decls and structures */
108 /* A contant telling PRCS how many directories to cache. Its actually
109 * kept in a list, so the geometry isn't important. */
110 #define CMPL_DIRECTORY_CACHE_SIZE 10
112 /* A constant used to determine whether a substring was an exact
113 * match by first_diff_index()
115 #define PATTERN_MATCH -1
116 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
117 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
119 /* This structure contains all the useful information about a directory
120 * for the purposes of filename completion. These structures are cached
121 * in the CompletionState struct. CompletionDir's are reference counted.
123 struct _CompletionDirSent
130 struct _CompletionDirEntry
*entries
;
133 struct _CompletionDir
135 CompletionDirSent
*sent
;
140 struct _CompletionDir
*cmpl_parent
;
145 /* This structure contains pairs of directory entry names with a flag saying
146 * whether or not they are a valid directory. NOTE: This information is used
147 * to provide the caller with information about whether to update its completions
148 * or try to open a file. Since directories are cached by the directory mtime,
149 * a symlink which points to an invalid file (which will not be a directory),
150 * will not be reevaluated if that file is created, unless the containing
151 * directory is touched. I consider this case to be worth ignoring (josh).
153 struct _CompletionDirEntry
160 struct _CompletionUserDir
166 struct _PossibleCompletion
168 /* accessible fields, all are accessed externally by functions
172 gint is_a_completion
;
173 gboolean is_directory
;
180 struct _CompletionState
182 gint last_valid_char
;
184 gint updated_text_len
;
185 gint updated_text_alloc
;
186 gboolean re_complete
;
188 gchar
*user_dir_name_buffer
;
189 gint user_directories_len
;
191 gchar
*last_completion_text
;
193 gint user_completion_index
; /* if >= 0, currently completing ~user */
195 struct _CompletionDir
*completion_dir
; /* directory completing from */
196 struct _CompletionDir
*active_completion_dir
;
198 struct _PossibleCompletion the_completion
;
200 struct _CompletionDir
*reference_dir
; /* initial directory */
202 GList
* directory_storage
;
203 GList
* directory_sent_storage
;
205 struct _CompletionUserDir
*user_directories
;
223 /* File completion functions which would be external, were they used
224 * outside of this file.
227 static CompletionState
* cmpl_init_state (void);
228 static void cmpl_free_state (CompletionState
*cmpl_state
);
229 static gint
cmpl_state_okay (CompletionState
* cmpl_state
);
230 static const gchar
* cmpl_strerror (gint
);
232 static PossibleCompletion
* cmpl_completion_matches(gchar
*text_to_complete
,
233 gchar
**remaining_text
,
234 CompletionState
*cmpl_state
);
236 /* Returns a name for consideration, possibly a completion, this name
237 * will be invalid after the next call to cmpl_next_completion.
239 static char* cmpl_this_completion (PossibleCompletion
*);
241 /* True if this completion matches the given text. Otherwise, this
242 * output can be used to have a list of non-completions.
244 static gint
cmpl_is_a_completion (PossibleCompletion
*);
246 /* True if the completion is a directory
248 static gboolean
cmpl_is_directory (PossibleCompletion
*);
250 /* Obtains the next completion, or NULL
252 static PossibleCompletion
* cmpl_next_completion (CompletionState
*);
254 /* Updating completions: the return value of cmpl_updated_text() will
255 * be text_to_complete completed as much as possible after the most
256 * recent call to cmpl_completion_matches. For the present
257 * application, this is the suggested replacement for the user's input
258 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
261 static gchar
* cmpl_updated_text (CompletionState
* cmpl_state
);
263 /* After updating, to see if the completion was a directory, call
264 * this. If it was, you should consider re-calling completion_matches.
266 static gboolean
cmpl_updated_dir (CompletionState
* cmpl_state
);
268 /* Current location: if using file completion, return the current
269 * directory, from which file completion begins. More specifically,
270 * the cwd concatenated with all exact completions up to the last
271 * directory delimiter('/').
273 static gchar
* cmpl_reference_position (CompletionState
* cmpl_state
);
275 /* backing up: if cmpl_completion_matches returns NULL, you may query
276 * the index of the last completable character into cmpl_updated_text.
278 static gint
cmpl_last_valid_char (CompletionState
* cmpl_state
);
280 /* When the user selects a non-directory, call cmpl_completion_fullname
281 * to get the full name of the selected file.
283 static const gchar
* cmpl_completion_fullname (const gchar
*, CompletionState
* cmpl_state
);
286 /* Directory operations. */
287 static CompletionDir
* open_ref_dir (gchar
* text_to_complete
,
288 gchar
** remaining_text
,
289 CompletionState
* cmpl_state
);
290 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
291 static gboolean
check_dir (gchar
*dir_name
,
293 gboolean
*stat_subdirs
);
295 static CompletionDir
* open_dir (gchar
* dir_name
,
296 CompletionState
* cmpl_state
);
298 static CompletionDir
* open_user_dir (const gchar
* text_to_complete
,
299 CompletionState
*cmpl_state
);
301 static CompletionDir
* open_relative_dir (gchar
* dir_name
, CompletionDir
* dir
,
302 CompletionState
*cmpl_state
);
303 static CompletionDirSent
* open_new_dir (gchar
* dir_name
,
305 gboolean stat_subdirs
);
306 static gint
correct_dir_fullname (CompletionDir
* cmpl_dir
);
307 static gint
correct_parent (CompletionDir
* cmpl_dir
,
310 static gchar
* find_parent_dir_fullname (gchar
* dirname
);
312 static CompletionDir
* attach_dir (CompletionDirSent
* sent
,
314 CompletionState
*cmpl_state
);
315 static void free_dir_sent (CompletionDirSent
* sent
);
316 static void free_dir (CompletionDir
*dir
);
317 static void prune_memory_usage(CompletionState
*cmpl_state
);
319 /* Completion operations */
321 static PossibleCompletion
* attempt_homedir_completion(gchar
* text_to_complete
,
322 CompletionState
*cmpl_state
);
324 static PossibleCompletion
* attempt_dir_completion(CompletionState
*cmpl_state
);
325 static CompletionDir
* find_completion_dir(gchar
* text_to_complete
,
326 gchar
** remaining_text
,
327 CompletionState
* cmpl_state
);
328 static PossibleCompletion
* append_completion_text(gchar
* text
,
329 CompletionState
* cmpl_state
);
331 static gint
get_pwdb(CompletionState
* cmpl_state
);
332 static gint
compare_user_dir(const void* a
, const void* b
);
334 static gint
first_diff_index(gchar
* pat
, gchar
* text
);
335 static gint
compare_cmpl_dir(const void* a
, const void* b
);
336 static void update_cmpl(PossibleCompletion
* poss
,
337 CompletionState
* cmpl_state
);
339 static void gtk_dir_selection_class_init (GtkDirSelectionClass
*klass
);
340 static void gtk_dir_selection_set_property (GObject
*object
,
344 static void gtk_dir_selection_get_property (GObject
*object
,
348 static void gtk_dir_selection_init (GtkDirSelection
*filesel
);
349 static void gtk_dir_selection_finalize (GObject
*object
);
350 static void gtk_dir_selection_destroy (GtkObject
*object
);
351 static void gtk_dir_selection_map (GtkWidget
*widget
);
352 static gint
gtk_dir_selection_key_press (GtkWidget
*widget
,
355 static gint
gtk_dir_selection_insert_text (GtkWidget
*widget
,
356 const gchar
*new_text
,
357 gint new_text_length
,
360 static void gtk_dir_selection_update_fileops (GtkDirSelection
*filesel
);
362 static void gtk_dir_selection_file_activate (GtkTreeView
*tree_view
,
364 GtkTreeViewColumn
*column
,
366 static void gtk_dir_selection_file_changed (GtkTreeSelection
*selection
,
368 static void gtk_dir_selection_dir_activate (GtkTreeView
*tree_view
,
370 GtkTreeViewColumn
*column
,
372 static void gtk_dir_selection_dir_changed (GtkTreeSelection
*selection
,
374 static void gtk_dir_selection_populate (GtkDirSelection
*fs
,
376 gboolean try_complete
,
377 gboolean reset_entry
);
378 static void gtk_dir_selection_abort (GtkDirSelection
*fs
);
380 static void gtk_dir_selection_update_history_menu (GtkDirSelection
*fs
,
383 static void gtk_dir_selection_create_dir (GtkWidget
*widget
, gpointer data
);
384 static void gtk_dir_selection_delete_file (GtkWidget
*widget
, gpointer data
);
385 static void gtk_dir_selection_rename_file (GtkWidget
*widget
, gpointer data
);
387 static void free_selected_names (GPtrArray
*names
);
389 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
390 #define compare_filenames(a, b) strcmp(a, b)
392 #define compare_filenames(a, b) g_ascii_strcasecmp(a, b)
396 static GtkWindowClass
*parent_class
= NULL
;
398 /* Saves errno when something cmpl does fails. */
399 static gint cmpl_errno
;
403 * Take the path currently in the file selection
404 * entry field and translate as necessary from
405 * a WIN32 style to CYGWIN32 style path. For
406 * instance translate:
407 * x:\somepath\file.jpg
409 * /cygdrive/x/somepath/file.jpg
411 * Replace the path in the selection text field.
412 * Return a boolean value concerning whether a
413 * translation had to be made.
416 translate_win32_path (GtkDirSelection
*filesel
)
420 gchar newPath
[PATH_MAX
];
423 * Retrieve the current path
425 path
= gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));
427 cygwin_conv_to_posix_path (path
, newPath
);
428 updated
= (strcmp (path
, newPath
) != 0);
431 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), newPath
);
438 gtk_dir_selection_get_type (void)
440 static GType file_selection_type
= 0;
442 if (!file_selection_type
)
444 static const GTypeInfo filesel_info
=
446 sizeof (GtkDirSelectionClass
),
447 NULL
, /* base_init */
448 NULL
, /* base_finalize */
449 (GClassInitFunc
) gtk_dir_selection_class_init
,
450 NULL
, /* class_finalize */
451 NULL
, /* class_data */
452 sizeof (GtkDirSelection
),
454 (GInstanceInitFunc
) gtk_dir_selection_init
,
458 file_selection_type
=
459 g_type_register_static (GTK_TYPE_DIALOG
, "GtkDirSelection",
463 return file_selection_type
;
467 gtk_dir_selection_class_init (GtkDirSelectionClass
*class)
469 GObjectClass
*gobject_class
;
470 GtkObjectClass
*object_class
;
471 GtkWidgetClass
*widget_class
;
473 gobject_class
= (GObjectClass
*) class;
474 object_class
= (GtkObjectClass
*) class;
475 widget_class
= (GtkWidgetClass
*) class;
477 parent_class
= g_type_class_peek_parent (class);
479 gobject_class
->finalize
= gtk_dir_selection_finalize
;
480 gobject_class
->set_property
= gtk_dir_selection_set_property
;
481 gobject_class
->get_property
= gtk_dir_selection_get_property
;
483 g_object_class_install_property (gobject_class
,
485 g_param_spec_string ("filename",
487 _("The currently selected filename"),
489 G_PARAM_READABLE
| G_PARAM_WRITABLE
));
490 g_object_class_install_property (gobject_class
,
492 g_param_spec_boolean ("show_fileops",
493 _("Show file operations"),
494 _("Whether buttons for creating/manipulating files should be displayed"),
498 g_object_class_install_property (gobject_class
,
499 PROP_SELECT_MULTIPLE
,
500 g_param_spec_boolean ("select_multiple",
501 _("Select multiple"),
502 _("Whether to allow multiple files to be selected"),
506 object_class
->destroy
= gtk_dir_selection_destroy
;
507 widget_class
->map
= gtk_dir_selection_map
;
510 static void gtk_dir_selection_set_property (GObject
*object
,
515 GtkDirSelection
*filesel
;
517 filesel
= GTK_DIR_SELECTION (object
);
522 gtk_dir_selection_set_filename (filesel
,
523 g_value_get_string (value
));
525 case PROP_SHOW_FILEOPS
:
526 if (g_value_get_boolean (value
))
527 gtk_dir_selection_show_fileop_buttons (filesel
);
529 gtk_dir_selection_hide_fileop_buttons (filesel
);
531 case PROP_SELECT_MULTIPLE
:
532 gtk_dir_selection_set_select_multiple (filesel
, g_value_get_boolean (value
));
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
540 static void gtk_dir_selection_get_property (GObject
*object
,
545 GtkDirSelection
*filesel
;
547 filesel
= GTK_DIR_SELECTION (object
);
552 g_value_set_string (value
,
553 gtk_dir_selection_get_filename(filesel
));
556 case PROP_SHOW_FILEOPS
:
557 /* This is a little bit hacky, but doing otherwise would require
558 * adding a field to the object.
560 g_value_set_boolean (value
, (filesel
->fileop_c_dir
&&
561 filesel
->fileop_del_file
&&
562 filesel
->fileop_ren_file
));
564 case PROP_SELECT_MULTIPLE
:
565 g_value_set_boolean (value
, gtk_dir_selection_get_select_multiple (filesel
));
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
574 grab_default (GtkWidget
*widget
)
576 gtk_widget_grab_default (widget
);
581 gtk_dir_selection_init (GtkDirSelection
*filesel
)
583 GtkWidget
*entry_vbox
;
585 GtkWidget
*list_hbox
, *list_container
;
586 GtkWidget
*confirm_area
;
587 GtkWidget
*pulldown_hbox
;
588 GtkWidget
*scrolled_win
;
594 GtkTreeViewColumn
*column
;
596 gtk_widget_push_composite_child ();
598 dialog
= GTK_DIALOG (filesel
);
600 filesel
->cmpl_state
= cmpl_init_state ();
602 /* The dialog-sized vertical box */
603 filesel
->main_vbox
= dialog
->vbox
;
604 gtk_container_set_border_width (GTK_CONTAINER (filesel
), 10);
606 /* The horizontal box containing create, rename etc. buttons */
607 filesel
->button_area
= gtk_hbutton_box_new ();
608 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel
->button_area
), GTK_BUTTONBOX_START
);
609 gtk_box_set_spacing (GTK_BOX (filesel
->button_area
), 0);
610 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->button_area
,
612 gtk_widget_show (filesel
->button_area
);
614 gtk_dir_selection_show_fileop_buttons (filesel
);
616 /* hbox for pulldown menu */
617 pulldown_hbox
= gtk_hbox_new (TRUE
, 5);
618 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), pulldown_hbox
, FALSE
, FALSE
, 0);
619 gtk_widget_show (pulldown_hbox
);
622 filesel
->history_pulldown
= gtk_option_menu_new ();
623 // gtk_widget_show (filesel->history_pulldown);
624 // gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
627 /* The horizontal box containing the directory and file listboxes */
629 spacer
= gtk_hbox_new (FALSE
, 0);
630 gtk_widget_set_size_request (spacer
, -1, 5);
631 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
632 gtk_widget_show (spacer
);
634 list_hbox
= gtk_hbox_new (FALSE
, 5);
635 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), list_hbox
, TRUE
, TRUE
, 0);
636 gtk_widget_show (list_hbox
);
638 list_container
= g_object_new (GTK_TYPE_HPANED
,
644 list_container
= list_hbox
;
646 spacer
= gtk_hbox_new (FALSE
, 0);
647 gtk_widget_set_size_request (spacer
, -1, 5);
648 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
649 gtk_widget_show (spacer
);
651 /* The directories list */
653 model
= gtk_list_store_new (1, G_TYPE_STRING
);
654 filesel
->dir_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
655 g_object_unref (model
);
657 column
= gtk_tree_view_column_new_with_attributes (_("Folders"),
658 gtk_cell_renderer_text_new (),
661 label
= gtk_label_new_with_mnemonic (_("Fol_ders"));
662 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->dir_list
);
663 gtk_widget_show (label
);
664 gtk_tree_view_column_set_widget (column
, label
);
665 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
666 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->dir_list
), column
);
668 gtk_widget_set_size_request (filesel
->dir_list
,
669 DIR_LIST_WIDTH
, DIR_LIST_HEIGHT
);
670 g_signal_connect (filesel
->dir_list
, "row_activated",
671 G_CALLBACK (gtk_dir_selection_dir_activate
), filesel
);
672 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->dir_list
)), "changed",
673 G_CALLBACK (gtk_dir_selection_dir_changed
), filesel
);
675 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
677 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
678 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
679 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->dir_list
);
680 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
681 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
682 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
683 if (GTK_IS_PANED (list_container
))
684 gtk_paned_pack1 (GTK_PANED (list_container
), scrolled_win
, TRUE
, TRUE
);
686 gtk_container_add (GTK_CONTAINER (list_container
), scrolled_win
);
687 gtk_widget_show (filesel
->dir_list
);
688 gtk_widget_show (scrolled_win
);
691 model
= gtk_list_store_new (1, G_TYPE_STRING
);
692 filesel
->file_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
693 g_object_unref (model
);
695 column
= gtk_tree_view_column_new_with_attributes (_("Files"),
696 gtk_cell_renderer_text_new (),
699 label
= gtk_label_new_with_mnemonic (_("_Files"));
700 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->file_list
);
701 gtk_widget_show (label
);
702 gtk_tree_view_column_set_widget (column
, label
);
703 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
704 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->file_list
), column
);
706 gtk_widget_set_size_request (filesel
->file_list
,
707 FILE_LIST_WIDTH
, FILE_LIST_HEIGHT
);
708 g_signal_connect (filesel
->file_list
, "row_activated",
709 G_CALLBACK (gtk_dir_selection_file_activate
), filesel
);
710 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
)), "changed",
711 G_CALLBACK (gtk_dir_selection_file_changed
), filesel
);
713 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
715 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
716 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
717 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->file_list
);
718 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
719 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
720 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
721 // gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
722 // gtk_widget_show (filesel->file_list);
723 // gtk_widget_show (scrolled_win);
725 /* action area for packing buttons into. */
726 filesel
->action_area
= gtk_hbox_new (TRUE
, 0);
727 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->action_area
,
729 gtk_widget_show (filesel
->action_area
);
731 /* The OK/Cancel button area */
732 confirm_area
= dialog
->action_area
;
734 /* The Cancel button */
735 filesel
->cancel_button
= gtk_dialog_add_button (dialog
,
737 GTK_RESPONSE_CANCEL
);
739 filesel
->ok_button
= gtk_dialog_add_button (dialog
,
743 gtk_widget_grab_default (filesel
->ok_button
);
745 /* The selection entry widget */
746 entry_vbox
= gtk_vbox_new (FALSE
, 2);
747 gtk_box_pack_end (GTK_BOX (filesel
->main_vbox
), entry_vbox
, FALSE
, FALSE
, 2);
748 gtk_widget_show (entry_vbox
);
750 eventbox
= gtk_event_box_new ();
751 filesel
->selection_text
= label
= gtk_label_new ("");
752 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
753 gtk_container_add (GTK_CONTAINER (eventbox
), label
);
754 gtk_box_pack_start (GTK_BOX (entry_vbox
), eventbox
, FALSE
, FALSE
, 0);
755 gtk_widget_show (label
);
756 gtk_widget_show (eventbox
);
758 filesel
->selection_entry
= gtk_entry_new ();
759 g_signal_connect (filesel
->selection_entry
, "key_press_event",
760 G_CALLBACK (gtk_dir_selection_key_press
), filesel
);
761 g_signal_connect (filesel
->selection_entry
, "insert_text",
762 G_CALLBACK (gtk_dir_selection_insert_text
), NULL
);
763 g_signal_connect_swapped (filesel
->selection_entry
, "changed",
764 G_CALLBACK (gtk_dir_selection_update_fileops
), filesel
);
765 g_signal_connect_swapped (filesel
->selection_entry
, "focus_in_event",
766 G_CALLBACK (grab_default
),
768 g_signal_connect_swapped (filesel
->selection_entry
, "activate",
769 G_CALLBACK (gtk_button_clicked
),
772 gtk_box_pack_start (GTK_BOX (entry_vbox
), filesel
->selection_entry
, TRUE
, TRUE
, 0);
773 gtk_widget_show (filesel
->selection_entry
);
775 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel
->selection_text
),
776 filesel
->selection_entry
);
778 if (!cmpl_state_okay (filesel
->cmpl_state
))
782 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
784 gtk_label_set_text (GTK_LABEL (filesel
->selection_text
), err_buf
);
788 gtk_dir_selection_populate (filesel
, "", FALSE
, TRUE
);
791 gtk_widget_grab_focus (filesel
->selection_entry
);
793 gtk_widget_pop_composite_child ();
797 uri_list_extract_first_uri (const gchar
* uri_list
)
801 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
804 /* We don't actually try to validate the URI according to RFC
805 * 2396, or even check for allowed characters - we just ignore
806 * comments and trim whitespace off the ends. We also
807 * allow LF delimination as well as the specified CRLF.
809 * We do allow comments like specified in RFC 2483.
815 while (g_ascii_isspace(*p
))
819 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
825 while (q
> p
&& g_ascii_isspace (*q
))
829 return g_strndup (p
, q
- p
+ 1);
832 p
= strchr (p
, '\n');
840 dnd_really_drop (GtkWidget
*dialog
, gint response_id
, GtkDirSelection
*fs
)
844 if (response_id
== GTK_RESPONSE_YES
)
846 filename
= g_object_get_data (G_OBJECT (dialog
), "gtk-fs-dnd-filename");
848 gtk_dir_selection_set_filename (fs
, filename
);
851 gtk_widget_destroy (dialog
);
856 filenames_dropped (GtkWidget
*widget
,
857 GdkDragContext
*context
,
860 GtkSelectionData
*selection_data
,
865 char *filename
= NULL
;
867 char this_hostname
[257];
869 GError
*error
= NULL
;
871 if (!selection_data
->data
)
874 uri
= uri_list_extract_first_uri ((char *)selection_data
->data
);
879 filename
= g_filename_from_uri (uri
, &hostname
, &error
);
884 g_warning ("Error getting dropped filename: %s\n",
886 g_error_free (error
);
890 res
= gethostname (this_hostname
, 256);
891 this_hostname
[256] = 0;
893 if ((hostname
== NULL
) ||
894 (res
== 0 && strcmp (hostname
, this_hostname
) == 0) ||
895 (strcmp (hostname
, "localhost") == 0))
896 gtk_dir_selection_set_filename (GTK_DIR_SELECTION (widget
),
901 gchar
*filename_utf8
;
903 /* Conversion back to UTF-8 should always succeed for the result
904 * of g_filename_from_uri()
906 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
907 g_assert (filename_utf8
);
909 dialog
= gtk_message_dialog_new (GTK_WINDOW (widget
),
910 GTK_DIALOG_DESTROY_WITH_PARENT
,
911 GTK_MESSAGE_QUESTION
,
913 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
914 "Are you sure that you want to select it?"), filename_utf8
, hostname
);
915 g_free (filename_utf8
);
917 g_object_set_data_full (G_OBJECT (dialog
), "gtk-fs-dnd-filename", g_strdup (filename
), g_free
);
919 g_signal_connect_data (dialog
, "response",
920 (GCallback
) dnd_really_drop
,
923 gtk_widget_show (dialog
);
941 filenames_drag_get (GtkWidget
*widget
,
942 GdkDragContext
*context
,
943 GtkSelectionData
*selection_data
,
946 GtkDirSelection
*filesel
)
954 file
= gtk_dir_selection_get_filename (filesel
);
958 if (info
== TARGET_URILIST
)
960 res
= gethostname (hostname
, 256);
963 uri_list
= g_filename_to_uri (file
, (!res
)?hostname
:NULL
, &error
);
966 g_warning ("Error getting filename: %s\n",
968 g_error_free (error
);
972 gtk_selection_data_set (selection_data
,
973 selection_data
->target
, 8,
974 (void *)uri_list
, strlen((char *)uri_list
));
979 gchar
*filename_utf8
= g_filename_to_utf8 (file
, -1, NULL
, NULL
, NULL
);
980 g_assert (filename_utf8
);
981 gtk_selection_data_set_text (selection_data
, filename_utf8
, -1);
982 g_free (filename_utf8
);
988 file_selection_setup_dnd (GtkDirSelection
*filesel
)
991 static const GtkTargetEntry drop_types
[] = {
992 { "text/uri-list", 0, TARGET_URILIST
}
994 static gint n_drop_types
= sizeof(drop_types
)/sizeof(drop_types
[0]);
995 static const GtkTargetEntry drag_types
[] = {
996 { "text/uri-list", 0, TARGET_URILIST
},
997 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
1000 { "COMPOUND_TEXT", 0, 0 }
1002 static gint n_drag_types
= sizeof(drag_types
)/sizeof(drag_types
[0]);
1004 gtk_drag_dest_set (GTK_WIDGET (filesel
),
1005 GTK_DEST_DEFAULT_ALL
,
1006 drop_types
, n_drop_types
,
1009 g_signal_connect (filesel
, "drag_data_received",
1010 G_CALLBACK (filenames_dropped
), NULL
);
1012 eventbox
= gtk_widget_get_parent (filesel
->selection_text
);
1013 gtk_drag_source_set (eventbox
,
1015 drag_types
, n_drag_types
,
1018 g_signal_connect (eventbox
, "drag_data_get",
1019 G_CALLBACK (filenames_drag_get
), filesel
);
1023 gtk_dir_selection_new (const gchar
*title
)
1025 GtkDirSelection
*filesel
;
1027 filesel
= g_object_new (GTK_TYPE_DIR_SELECTION
, NULL
);
1028 gtk_window_set_title (GTK_WINDOW (filesel
), title
);
1029 gtk_dialog_set_has_separator (GTK_DIALOG (filesel
), FALSE
);
1031 file_selection_setup_dnd (filesel
);
1033 return GTK_WIDGET (filesel
);
1037 gtk_dir_selection_show_fileop_buttons (GtkDirSelection
*filesel
)
1039 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1041 /* delete, create directory, and rename */
1042 if (!filesel
->fileop_c_dir
)
1044 filesel
->fileop_c_dir
= gtk_button_new_with_mnemonic (_("_New Folder"));
1045 g_signal_connect (filesel
->fileop_c_dir
, "clicked",
1046 G_CALLBACK (gtk_dir_selection_create_dir
),
1048 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1049 // filesel->fileop_c_dir, TRUE, TRUE, 0);
1050 // gtk_widget_show (filesel->fileop_c_dir);
1053 if (!filesel
->fileop_del_file
)
1055 filesel
->fileop_del_file
= gtk_button_new_with_mnemonic (_("De_lete File"));
1056 g_signal_connect (filesel
->fileop_del_file
, "clicked",
1057 G_CALLBACK (gtk_dir_selection_delete_file
),
1059 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1060 // filesel->fileop_del_file, TRUE, TRUE, 0);
1061 // gtk_widget_show (filesel->fileop_del_file);
1064 if (!filesel
->fileop_ren_file
)
1066 filesel
->fileop_ren_file
= gtk_button_new_with_mnemonic (_("_Rename File"));
1067 g_signal_connect (filesel
->fileop_ren_file
, "clicked",
1068 G_CALLBACK (gtk_dir_selection_rename_file
),
1070 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1071 // filesel->fileop_ren_file, TRUE, TRUE, 0);
1072 // gtk_widget_show (filesel->fileop_ren_file);
1075 gtk_dir_selection_update_fileops (filesel
);
1077 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1081 gtk_dir_selection_hide_fileop_buttons (GtkDirSelection
*filesel
)
1083 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1085 if (filesel
->fileop_ren_file
)
1087 gtk_widget_destroy (filesel
->fileop_ren_file
);
1088 filesel
->fileop_ren_file
= NULL
;
1091 if (filesel
->fileop_del_file
)
1093 gtk_widget_destroy (filesel
->fileop_del_file
);
1094 filesel
->fileop_del_file
= NULL
;
1097 if (filesel
->fileop_c_dir
)
1099 gtk_widget_destroy (filesel
->fileop_c_dir
);
1100 filesel
->fileop_c_dir
= NULL
;
1102 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1108 * gtk_dir_selection_set_filename:
1109 * @filesel: a #GtkDirSelection.
1110 * @filename: a string to set as the default file name.
1112 * Sets a default path for the file requestor. If @filename includes a
1113 * directory path, then the requestor will open with that path as its
1114 * current working directory.
1116 * The encoding of @filename is the on-disk encoding, which
1117 * may not be UTF-8. See g_filename_from_utf8().
1120 gtk_dir_selection_set_filename (GtkDirSelection
*filesel
,
1121 const gchar
*filename
)
1124 const char *name
, *last_slash
;
1125 char *filename_utf8
;
1127 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1128 g_return_if_fail (filename
!= NULL
);
1130 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
1131 g_return_if_fail (filename_utf8
!= NULL
);
1133 last_slash
= strrchr (filename_utf8
, G_DIR_SEPARATOR
);
1137 buf
= g_strdup ("");
1138 name
= filename_utf8
;
1142 buf
= g_strdup (filename_utf8
);
1143 buf
[last_slash
- filename_utf8
+ 1] = 0;
1144 name
= last_slash
+ 1;
1147 gtk_dir_selection_populate (filesel
, buf
, FALSE
, TRUE
);
1149 if (filesel
->selection_entry
)
1150 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), name
);
1152 g_object_notify (G_OBJECT (filesel
), "filename");
1154 g_free (filename_utf8
);
1158 * gtk_dir_selection_get_filename:
1159 * @filesel: a #GtkDirSelection
1161 * This function returns the selected filename in the on-disk encoding
1162 * (see g_filename_from_utf8()), which may or may not be the same as that
1163 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1164 * The returned string points to a statically allocated buffer and
1165 * should be copied if you plan to keep it around.
1167 * If no file is selected then the selected directory path is returned.
1169 * Return value: currently-selected filename in the on-disk encoding.
1171 G_CONST_RETURN gchar
*
1172 gtk_dir_selection_get_filename (GtkDirSelection
*filesel
)
1174 static const gchar nothing
[2] = "";
1175 static gchar something
[MAXPATHLEN
*2];
1179 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), nothing
);
1181 #ifdef G_WITH_CYGWIN
1182 translate_win32_path (filesel
);
1184 text
= gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));
1187 sys_filename
= g_filename_from_utf8 (cmpl_completion_fullname (text
, filesel
->cmpl_state
), -1, NULL
, NULL
, NULL
);
1190 strncpy (something
, sys_filename
, sizeof (something
));
1191 g_free (sys_filename
);
1199 gtk_dir_selection_complete (GtkDirSelection
*filesel
,
1200 const gchar
*pattern
)
1202 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1203 g_return_if_fail (pattern
!= NULL
);
1205 if (filesel
->selection_entry
)
1206 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), pattern
);
1207 gtk_dir_selection_populate (filesel
, (gchar
*) pattern
, TRUE
, TRUE
);
1211 gtk_dir_selection_destroy (GtkObject
*object
)
1213 GtkDirSelection
*filesel
;
1215 HistoryCallbackArg
*callback_arg
;
1217 g_return_if_fail (GTK_IS_DIR_SELECTION (object
));
1219 filesel
= GTK_DIR_SELECTION (object
);
1221 if (filesel
->fileop_dialog
)
1223 gtk_widget_destroy (filesel
->fileop_dialog
);
1224 filesel
->fileop_dialog
= NULL
;
1227 if (filesel
->history_list
)
1229 list
= filesel
->history_list
;
1232 callback_arg
= list
->data
;
1233 g_free (callback_arg
->directory
);
1234 g_free (callback_arg
);
1237 g_list_free (filesel
->history_list
);
1238 filesel
->history_list
= NULL
;
1241 if (filesel
->cmpl_state
)
1243 cmpl_free_state (filesel
->cmpl_state
);
1244 filesel
->cmpl_state
= NULL
;
1247 if (filesel
->selected_names
)
1249 free_selected_names (filesel
->selected_names
);
1250 filesel
->selected_names
= NULL
;
1253 if (filesel
->last_selected
)
1255 g_free (filesel
->last_selected
);
1256 filesel
->last_selected
= NULL
;
1259 GTK_OBJECT_CLASS (parent_class
)->destroy (object
);
1263 gtk_dir_selection_map (GtkWidget
*widget
)
1265 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (widget
);
1267 /* Refresh the contents */
1268 gtk_dir_selection_populate (filesel
, "", FALSE
, FALSE
);
1270 GTK_WIDGET_CLASS (parent_class
)->map (widget
);
1274 gtk_dir_selection_finalize (GObject
*object
)
1276 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (object
);
1278 g_free (filesel
->fileop_file
);
1280 G_OBJECT_CLASS (parent_class
)->finalize (object
);
1283 /* Begin file operations callbacks */
1286 gtk_dir_selection_fileop_error (GtkDirSelection
*fs
,
1287 gchar
*error_message
)
1291 g_return_if_fail (error_message
!= NULL
);
1294 dialog
= gtk_message_dialog_new (GTK_WINDOW (fs
),
1295 GTK_DIALOG_DESTROY_WITH_PARENT
,
1298 "%s", error_message
);
1300 /* yes, we free it */
1301 g_free (error_message
);
1303 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1305 g_signal_connect_swapped (dialog
, "response",
1306 G_CALLBACK (gtk_widget_destroy
),
1309 gtk_widget_show (dialog
);
1313 gtk_dir_selection_fileop_destroy (GtkWidget
*widget
,
1316 GtkDirSelection
*fs
= data
;
1318 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1320 fs
->fileop_dialog
= NULL
;
1324 entry_is_empty (GtkEntry
*entry
)
1326 const gchar
*text
= gtk_entry_get_text (entry
);
1328 return *text
== '\0';
1332 gtk_dir_selection_fileop_entry_changed (GtkEntry
*entry
,
1335 gtk_widget_set_sensitive (button
, !entry_is_empty (entry
));
1339 gtk_dir_selection_create_dir_confirmed (GtkWidget
*widget
,
1342 GtkDirSelection
*fs
= data
;
1343 const gchar
*dirname
;
1346 gchar
*sys_full_path
;
1348 GError
*error
= NULL
;
1349 CompletionState
*cmpl_state
;
1351 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1353 dirname
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1354 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1355 path
= cmpl_reference_position (cmpl_state
);
1357 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, dirname
, NULL
);
1358 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1361 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1362 buf
= g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname
);
1364 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname
, error
->message
,
1365 _("You probably used symbols not allowed in filenames."));
1366 gtk_dir_selection_fileop_error (fs
, buf
);
1367 g_error_free (error
);
1371 if (mkdir (sys_full_path
, 0755) < 0)
1373 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname
,
1374 g_strerror (errno
));
1375 gtk_dir_selection_fileop_error (fs
, buf
);
1380 g_free (sys_full_path
);
1382 gtk_widget_destroy (fs
->fileop_dialog
);
1383 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1387 gtk_dir_selection_create_dir (GtkWidget
*widget
,
1390 GtkDirSelection
*fs
= data
;
1396 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1398 if (fs
->fileop_dialog
)
1402 dialog
= gtk_dialog_new ();
1403 fs
->fileop_dialog
= dialog
;
1404 g_signal_connect (dialog
, "destroy",
1405 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1407 gtk_window_set_title (GTK_WINDOW (dialog
), _("New Folder"));
1408 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1409 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1411 /* If file dialog is grabbed, grab option dialog */
1412 /* When option dialog is closed, file dialog will be grabbed again */
1413 if (GTK_WINDOW (fs
)->modal
)
1414 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1416 vbox
= gtk_vbox_new (FALSE
, 0);
1417 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1418 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1420 gtk_widget_show( vbox
);
1422 label
= gtk_label_new_with_mnemonic (_("_Folder name:"));
1423 gtk_misc_set_alignment(GTK_MISC (label
), 0.0, 0.0);
1424 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1425 gtk_widget_show (label
);
1427 /* The directory entry widget */
1428 fs
->fileop_entry
= gtk_entry_new ();
1429 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), fs
->fileop_entry
);
1430 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1432 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1433 gtk_widget_show (fs
->fileop_entry
);
1436 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1437 g_signal_connect_swapped (button
, "clicked",
1438 G_CALLBACK (gtk_widget_destroy
),
1440 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1441 button
, TRUE
, TRUE
, 0);
1442 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1443 gtk_widget_grab_default (button
);
1444 gtk_widget_show (button
);
1446 gtk_widget_grab_focus (fs
->fileop_entry
);
1448 button
= gtk_button_new_with_mnemonic (_("C_reate"));
1449 gtk_widget_set_sensitive (button
, FALSE
);
1450 g_signal_connect (button
, "clicked",
1451 G_CALLBACK (gtk_dir_selection_create_dir_confirmed
),
1453 g_signal_connect (fs
->fileop_entry
, "changed",
1454 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1457 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1458 button
, TRUE
, TRUE
, 0);
1459 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1460 gtk_widget_show (button
);
1462 gtk_widget_show (dialog
);
1466 gtk_dir_selection_delete_dir_response (GtkDialog
*dialog
,
1470 GtkDirSelection
*fs
= data
;
1471 CompletionState
*cmpl_state
;
1474 gchar
*sys_full_path
;
1475 GError
*error
= NULL
;
1478 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1480 if (response_id
!= GTK_RESPONSE_OK
)
1482 gtk_widget_destroy (GTK_WIDGET (dialog
));
1486 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1487 path
= cmpl_reference_position (cmpl_state
);
1489 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1490 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1493 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1494 buf
= g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1497 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1498 fs
->fileop_file
, error
->message
,
1499 _("It probably contains symbols not allowed in filenames."));
1501 gtk_dir_selection_fileop_error (fs
, buf
);
1502 g_error_free (error
);
1506 if (unlink (sys_full_path
) < 0)
1508 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s"),
1509 fs
->fileop_file
, g_strerror (errno
));
1510 gtk_dir_selection_fileop_error (fs
, buf
);
1515 g_free (sys_full_path
);
1517 gtk_widget_destroy (fs
->fileop_dialog
);
1518 gtk_dir_selection_populate (fs
, "", FALSE
, TRUE
);
1522 gtk_dir_selection_delete_file (GtkWidget
*widget
,
1525 GtkDirSelection
*fs
= data
;
1527 const gchar
*filename
;
1529 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1531 if (fs
->fileop_dialog
)
1534 #ifdef G_WITH_CYGWIN
1535 translate_win32_path (fs
);
1538 filename
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
1539 if (strlen (filename
) < 1)
1542 g_free (fs
->fileop_file
);
1543 fs
->fileop_file
= g_strdup (filename
);
1546 fs
->fileop_dialog
= dialog
=
1547 gtk_message_dialog_new (GTK_WINDOW (fs
),
1548 GTK_WINDOW (fs
)->modal
? GTK_DIALOG_MODAL
: 0,
1549 GTK_MESSAGE_QUESTION
,
1551 _("Really delete file \"%s\" ?"), filename
);
1553 g_signal_connect (dialog
, "destroy",
1554 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1556 gtk_window_set_title (GTK_WINDOW (dialog
), _("Delete File"));
1557 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1560 gtk_dialog_add_buttons (GTK_DIALOG (dialog
),
1561 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1562 GTK_STOCK_DELETE
, GTK_RESPONSE_OK
,
1565 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_CANCEL
);
1567 g_signal_connect (dialog
, "response",
1568 G_CALLBACK (gtk_dir_selection_delete_dir_response
),
1571 gtk_widget_show (dialog
);
1575 gtk_dir_selection_rename_dir_confirmed (GtkWidget
*widget
,
1578 GtkDirSelection
*fs
= data
;
1582 gchar
*new_filename
;
1583 gchar
*old_filename
;
1584 gchar
*sys_new_filename
;
1585 gchar
*sys_old_filename
;
1586 CompletionState
*cmpl_state
;
1587 GError
*error
= NULL
;
1589 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1591 file
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1592 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1593 path
= cmpl_reference_position (cmpl_state
);
1595 new_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, file
, NULL
);
1596 old_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1598 sys_new_filename
= g_filename_from_utf8 (new_filename
, -1, NULL
, NULL
, &error
);
1601 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1602 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename
);
1604 buf
= g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1605 new_filename
, error
->message
,
1606 _("You probably used symbols not allowed in filenames."));
1607 gtk_dir_selection_fileop_error (fs
, buf
);
1608 g_error_free (error
);
1612 sys_old_filename
= g_filename_from_utf8 (old_filename
, -1, NULL
, NULL
, &error
);
1615 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1616 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename
);
1618 buf
= g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1619 old_filename
, error
->message
,
1620 _("It probably contains symbols not allowed in filenames."));
1621 gtk_dir_selection_fileop_error (fs
, buf
);
1622 g_error_free (error
);
1626 if (rename (sys_old_filename
, sys_new_filename
) < 0)
1628 buf
= g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1629 sys_old_filename
, sys_new_filename
,
1630 g_strerror (errno
));
1631 gtk_dir_selection_fileop_error (fs
, buf
);
1635 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1636 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), file
);
1639 g_free (sys_old_filename
);
1642 g_free (new_filename
);
1643 g_free (old_filename
);
1644 g_free (sys_new_filename
);
1646 gtk_widget_destroy (fs
->fileop_dialog
);
1650 gtk_dir_selection_rename_file (GtkWidget
*widget
,
1653 GtkDirSelection
*fs
= data
;
1660 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1662 if (fs
->fileop_dialog
)
1665 g_free (fs
->fileop_file
);
1666 fs
->fileop_file
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1667 if (strlen (fs
->fileop_file
) < 1)
1671 fs
->fileop_dialog
= dialog
= gtk_dialog_new ();
1672 g_signal_connect (dialog
, "destroy",
1673 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1675 gtk_window_set_title (GTK_WINDOW (dialog
), _("Rename File"));
1676 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1677 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1679 /* If file dialog is grabbed, grab option dialog */
1680 /* When option dialog closed, file dialog will be grabbed again */
1681 if (GTK_WINDOW (fs
)->modal
)
1682 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1684 vbox
= gtk_vbox_new (FALSE
, 0);
1685 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1686 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1688 gtk_widget_show(vbox
);
1690 buf
= g_strdup_printf (_("Rename file \"%s\" to:"), fs
->fileop_file
);
1691 label
= gtk_label_new (buf
);
1692 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.0);
1693 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1694 gtk_widget_show (label
);
1697 /* New filename entry */
1698 fs
->fileop_entry
= gtk_entry_new ();
1699 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1701 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1702 gtk_widget_show (fs
->fileop_entry
);
1704 gtk_entry_set_text (GTK_ENTRY (fs
->fileop_entry
), fs
->fileop_file
);
1705 gtk_editable_select_region (GTK_EDITABLE (fs
->fileop_entry
),
1706 0, strlen (fs
->fileop_file
));
1709 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1710 g_signal_connect_swapped (button
, "clicked",
1711 G_CALLBACK (gtk_widget_destroy
),
1713 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1714 button
, TRUE
, TRUE
, 0);
1715 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1716 gtk_widget_grab_default (button
);
1717 gtk_widget_show (button
);
1719 gtk_widget_grab_focus (fs
->fileop_entry
);
1721 button
= gtk_button_new_with_mnemonic (_("_Rename"));
1722 g_signal_connect (button
, "clicked",
1723 G_CALLBACK (gtk_dir_selection_rename_dir_confirmed
),
1725 g_signal_connect (fs
->fileop_entry
, "changed",
1726 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1729 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1730 button
, TRUE
, TRUE
, 0);
1731 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1732 gtk_widget_show (button
);
1734 gtk_widget_show (dialog
);
1738 gtk_dir_selection_insert_text (GtkWidget
*widget
,
1739 const gchar
*new_text
,
1740 gint new_text_length
,
1746 filename
= g_filename_from_utf8 (new_text
, new_text_length
, NULL
, NULL
, NULL
);
1750 gdk_display_beep (gtk_widget_get_display (widget
));
1751 g_signal_stop_emission_by_name (widget
, "insert_text");
1761 gtk_dir_selection_update_fileops (GtkDirSelection
*fs
)
1765 if (!fs
->selection_entry
)
1768 sensitive
= !entry_is_empty (GTK_ENTRY (fs
->selection_entry
));
1770 if (fs
->fileop_del_file
)
1771 gtk_widget_set_sensitive (fs
->fileop_del_file
, sensitive
);
1773 if (fs
->fileop_ren_file
)
1774 gtk_widget_set_sensitive (fs
->fileop_ren_file
, sensitive
);
1778 gtk_dir_selection_key_press (GtkWidget
*widget
,
1782 GtkDirSelection
*fs
;
1785 g_return_val_if_fail (widget
!= NULL
, FALSE
);
1786 g_return_val_if_fail (event
!= NULL
, FALSE
);
1788 if ((event
->keyval
== GDK_Tab
|| event
->keyval
== GDK_KP_Tab
) &&
1789 (event
->state
& gtk_accelerator_get_default_mod_mask ()) == 0)
1791 fs
= GTK_DIR_SELECTION (user_data
);
1792 #ifdef G_WITH_CYGWIN
1793 translate_win32_path (fs
);
1795 text
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1797 gtk_dir_selection_populate (fs
, text
, TRUE
, TRUE
);
1808 gtk_dir_selection_history_callback (GtkWidget
*widget
,
1811 GtkDirSelection
*fs
= data
;
1812 HistoryCallbackArg
*callback_arg
;
1815 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1817 list
= fs
->history_list
;
1820 callback_arg
= list
->data
;
1822 if (callback_arg
->menu_item
== widget
)
1824 gtk_dir_selection_populate (fs
, callback_arg
->directory
, FALSE
, FALSE
);
1833 gtk_dir_selection_update_history_menu (GtkDirSelection
*fs
,
1834 gchar
*current_directory
)
1836 HistoryCallbackArg
*callback_arg
;
1837 GtkWidget
*menu_item
;
1843 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1844 g_return_if_fail (current_directory
!= NULL
);
1846 list
= fs
->history_list
;
1848 if (fs
->history_menu
)
1851 callback_arg
= list
->data
;
1852 g_free (callback_arg
->directory
);
1853 g_free (callback_arg
);
1856 g_list_free (fs
->history_list
);
1857 fs
->history_list
= NULL
;
1859 gtk_widget_destroy (fs
->history_menu
);
1862 fs
->history_menu
= gtk_menu_new ();
1864 current_dir
= g_strdup (current_directory
);
1866 dir_len
= strlen (current_dir
);
1868 for (i
= dir_len
; i
>= 0; i
--)
1870 /* the i == dir_len is to catch the full path for the first
1872 if ( (current_dir
[i
] == G_DIR_SEPARATOR
) || (i
== dir_len
))
1874 /* another small hack to catch the full path */
1876 current_dir
[i
+ 1] = '\0';
1877 #ifdef G_WITH_CYGWIN
1878 if (!strcmp (current_dir
, "//"))
1881 menu_item
= gtk_menu_item_new_with_label (current_dir
);
1883 callback_arg
= g_new (HistoryCallbackArg
, 1);
1884 callback_arg
->menu_item
= menu_item
;
1886 /* since the autocompletion gets confused if you don't
1887 * supply a trailing '/' on a dir entry, set the full
1888 * (current) path to "" which just refreshes the filesel */
1891 callback_arg
->directory
= g_strdup ("");
1895 callback_arg
->directory
= g_strdup (current_dir
);
1898 fs
->history_list
= g_list_append (fs
->history_list
, callback_arg
);
1900 g_signal_connect (menu_item
, "activate",
1901 G_CALLBACK (gtk_dir_selection_history_callback
),
1903 gtk_menu_shell_append (GTK_MENU_SHELL (fs
->history_menu
), menu_item
);
1904 gtk_widget_show (menu_item
);
1908 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs
->history_pulldown
),
1910 g_free (current_dir
);
1914 get_real_filename (gchar
*filename
,
1917 #ifdef G_WITH_CYGWIN
1918 /* Check to see if the selection was a drive selector */
1919 if (isalpha (filename
[0]) && (filename
[1] == ':'))
1921 gchar temp_filename
[PATH_MAX
];
1924 cygwin_conv_to_posix_path (filename
, temp_filename
);
1926 /* we need trailing '/'. */
1927 len
= strlen (temp_filename
);
1928 if (len
> 0 && temp_filename
[len
-1] != '/')
1930 temp_filename
[len
] = '/';
1931 temp_filename
[len
+1] = '\0';
1937 return g_strdup (temp_filename
);
1939 #endif /* G_WITH_CYGWIN */
1944 gtk_dir_selection_file_activate (GtkTreeView
*tree_view
,
1946 GtkTreeViewColumn
*column
,
1949 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1950 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1954 gtk_tree_model_get_iter (model
, &iter
, path
);
1955 gtk_tree_model_get (model
, &iter
, FILE_COLUMN
, &filename
, -1);
1956 filename
= get_real_filename (filename
, TRUE
);
1957 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
1958 gtk_button_clicked (GTK_BUTTON (fs
->ok_button
));
1964 gtk_dir_selection_dir_activate (GtkTreeView
*tree_view
,
1966 GtkTreeViewColumn
*column
,
1969 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1970 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1974 gtk_tree_model_get_iter (model
, &iter
, path
);
1975 gtk_tree_model_get (model
, &iter
, DIR_COLUMN
, &filename
, -1);
1976 filename
= get_real_filename (filename
, TRUE
);
1977 gtk_dir_selection_populate (fs
, filename
, FALSE
, FALSE
);
1981 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1984 win32_gtk_add_drives_to_dir_list (GtkListStore
*model
)
1988 char formatBuffer
[128];
1991 /* Get the drives string */
1992 GetLogicalDriveStrings (sizeof (buffer
), buffer
);
1994 /* Add the drives as necessary */
1996 while (*textPtr
!= '\0')
1998 /* Ignore floppies (?) */
1999 if ((tolower (textPtr
[0]) != 'a') && (tolower (textPtr
[0]) != 'b'))
2001 /* Build the actual displayable string */
2002 g_snprintf (formatBuffer
, sizeof (formatBuffer
), "%c:\\", toupper (textPtr
[0]));
2004 /* Add to the list */
2005 gtk_list_store_append (model
, &iter
);
2006 gtk_list_store_set (model
, &iter
, DIR_COLUMN
, formatBuffer
, -1);
2008 textPtr
+= (strlen (textPtr
) + 1);
2014 escape_underscores (const gchar
*str
)
2016 GString
*result
= g_string_new (NULL
);
2020 g_string_append_c (result
, '_');
2022 g_string_append_c (result
, *str
);
2026 return g_string_free (result
, FALSE
);
2030 gtk_dir_selection_populate (GtkDirSelection
*fs
,
2032 gboolean try_complete
,
2033 gboolean reset_entry
)
2035 CompletionState
*cmpl_state
;
2036 PossibleCompletion
* poss
;
2038 GtkListStore
*dir_model
;
2039 GtkListStore
*file_model
;
2041 gchar
* rem_path
= rel_path
;
2043 gint did_recurse
= FALSE
;
2044 gint possible_count
= 0;
2045 gint selection_index
= -1;
2047 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
2049 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
2050 poss
= cmpl_completion_matches (rel_path
, &rem_path
, cmpl_state
);
2052 if (!cmpl_state_okay (cmpl_state
))
2054 /* Something went wrong. */
2055 gtk_dir_selection_abort (fs
);
2059 g_assert (cmpl_state
->reference_dir
);
2061 dir_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->dir_list
)));
2062 file_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->file_list
)));
2064 gtk_list_store_clear (dir_model
);
2065 gtk_list_store_clear (file_model
);
2067 /* Set the dir list to include ./ and ../ */
2068 gtk_list_store_append (dir_model
, &iter
);
2069 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, "." G_DIR_SEPARATOR_S
, -1);
2070 gtk_list_store_append (dir_model
, &iter
);
2071 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, ".." G_DIR_SEPARATOR_S
, -1);
2075 if (cmpl_is_a_completion (poss
))
2077 possible_count
+= 1;
2079 filename
= cmpl_this_completion (poss
);
2081 if (cmpl_is_directory (poss
))
2083 if (strcmp (filename
, "." G_DIR_SEPARATOR_S
) != 0 &&
2084 strcmp (filename
, ".." G_DIR_SEPARATOR_S
) != 0)
2086 gtk_list_store_append (dir_model
, &iter
);
2087 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, filename
, -1);
2092 gtk_list_store_append (file_model
, &iter
);
2093 gtk_list_store_set (file_model
, &iter
, DIR_COLUMN
, filename
, -1);
2097 poss
= cmpl_next_completion (cmpl_state
);
2100 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2101 /* For Windows, add drives as potential selections */
2102 win32_gtk_add_drives_to_dir_list (dir_model
);
2105 /* File lists are set. */
2107 g_assert (cmpl_state
->reference_dir
);
2112 /* User is trying to complete filenames, so advance the user's input
2113 * string to the updated_text, which is the common leading substring
2114 * of all possible completions, and if its a directory attempt
2115 * attempt completions in it. */
2117 if (cmpl_updated_text (cmpl_state
)[0])
2120 if (cmpl_updated_dir (cmpl_state
))
2122 gchar
* dir_name
= g_strdup (cmpl_updated_text (cmpl_state
));
2126 gtk_dir_selection_populate (fs
, dir_name
, TRUE
, TRUE
);
2132 if (fs
->selection_entry
)
2133 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
),
2134 cmpl_updated_text (cmpl_state
));
2139 selection_index
= cmpl_last_valid_char (cmpl_state
) -
2140 (strlen (rel_path
) - strlen (rem_path
));
2141 if (fs
->selection_entry
)
2142 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), rem_path
);
2145 else if (reset_entry
)
2147 if (fs
->selection_entry
)
2148 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2153 if (fs
->selection_entry
)
2154 gtk_editable_set_position (GTK_EDITABLE (fs
->selection_entry
),
2157 if (fs
->selection_entry
)
2159 char *escaped
= escape_underscores (cmpl_reference_position (cmpl_state
));
2160 sel_text
= g_strconcat (_("_Selection: "), escaped
, NULL
);
2163 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs
->selection_text
), sel_text
);
2164 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), cmpl_reference_position (cmpl_state
));
2168 if (fs
->history_pulldown
)
2170 gtk_dir_selection_update_history_menu (fs
, cmpl_reference_position (cmpl_state
));
2177 gtk_dir_selection_abort (GtkDirSelection
*fs
)
2181 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
2183 /* BEEP gdk_beep(); */
2185 if (fs
->selection_entry
)
2186 gtk_label_set_text (GTK_LABEL (fs
->selection_text
), err_buf
);
2190 * gtk_dir_selection_set_select_multiple:
2191 * @filesel: a #GtkDirSelection
2192 * @select_multiple: whether or not the user is allowed to select multiple
2193 * files in the file list.
2195 * Sets whether the user is allowed to select multiple files in the file list.
2196 * Use gtk_dir_selection_get_selections () to get the list of selected files.
2199 gtk_dir_selection_set_select_multiple (GtkDirSelection
*filesel
,
2200 gboolean select_multiple
)
2202 GtkTreeSelection
*sel
;
2203 GtkSelectionMode mode
;
2205 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
2207 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2209 mode
= select_multiple
? GTK_SELECTION_MULTIPLE
: GTK_SELECTION_SINGLE
;
2211 if (mode
!= gtk_tree_selection_get_mode (sel
))
2213 gtk_tree_selection_set_mode (sel
, mode
);
2215 g_object_notify (G_OBJECT (filesel
), "select-multiple");
2220 * gtk_dir_selection_get_select_multiple:
2221 * @filesel: a #GtkDirSelection
2223 * Determines whether or not the user is allowed to select multiple files in
2224 * the file list. See gtk_dir_selection_set_select_multiple().
2226 * Return value: %TRUE if the user is allowed to select multiple files in the
2230 gtk_dir_selection_get_select_multiple (GtkDirSelection
*filesel
)
2232 GtkTreeSelection
*sel
;
2234 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), FALSE
);
2236 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2237 return (gtk_tree_selection_get_mode (sel
) == GTK_SELECTION_MULTIPLE
);
2241 multiple_changed_foreach (GtkTreeModel
*model
,
2246 GPtrArray
*names
= data
;
2249 gtk_tree_model_get (model
, iter
, FILE_COLUMN
, &filename
, -1);
2251 g_ptr_array_add (names
, filename
);
2255 free_selected_names (GPtrArray
*names
)
2259 for (i
= 0; i
< names
->len
; i
++)
2260 g_free (g_ptr_array_index (names
, i
));
2262 g_ptr_array_free (names
, TRUE
);
2266 gtk_dir_selection_file_changed (GtkTreeSelection
*selection
,
2269 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2270 GPtrArray
*new_names
;
2275 new_names
= g_ptr_array_sized_new (8);
2277 gtk_tree_selection_selected_foreach (selection
,
2278 multiple_changed_foreach
,
2281 /* nothing selected */
2282 if (new_names
->len
== 0)
2284 g_ptr_array_free (new_names
, TRUE
);
2286 if (fs
->selected_names
!= NULL
)
2288 free_selected_names (fs
->selected_names
);
2289 fs
->selected_names
= NULL
;
2292 goto maybe_clear_entry
;
2295 if (new_names
->len
!= 1)
2297 GPtrArray
*old_names
= fs
->selected_names
;
2299 if (old_names
!= NULL
)
2301 /* A common case is selecting a range of files from top to bottom,
2302 * so quickly check for that to avoid looping over the entire list
2304 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2305 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2306 index
= new_names
->len
- 1;
2312 /* do a quick diff, stopping at the first file not in the
2315 while (i
< old_names
->len
&& j
< new_names
->len
)
2317 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2318 g_ptr_array_index (new_names
, j
));
2335 /* we ran off the end of the old list */
2336 if (index
== -1 && i
< new_names
->len
)
2342 /* A phantom anchor still exists at the point where the last item
2343 * was selected, which is used for subsequent range selections.
2344 * So search up from there.
2346 if (fs
->last_selected
&&
2347 compare_filenames (fs
->last_selected
,
2348 g_ptr_array_index (new_names
, 0)) == 0)
2349 index
= new_names
->len
- 1;
2357 if (fs
->selected_names
!= NULL
)
2358 free_selected_names (fs
->selected_names
);
2360 fs
->selected_names
= new_names
;
2364 if (fs
->last_selected
!= NULL
)
2365 g_free (fs
->last_selected
);
2367 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2368 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2370 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
2372 if (filename
!= fs
->last_selected
)
2380 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2381 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2382 (compare_filenames (entry
, fs
->last_selected
) == 0))
2383 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2387 gtk_dir_selection_dir_changed (GtkTreeSelection
*selection
,
2390 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2391 GPtrArray
*new_names
;
2396 new_names
= g_ptr_array_sized_new (8);
2398 gtk_tree_selection_selected_foreach (selection
,
2399 multiple_changed_foreach
,
2402 /* nothing selected */
2403 if (new_names
->len
== 0)
2405 g_ptr_array_free (new_names
, TRUE
);
2407 if (fs
->selected_names
!= NULL
)
2409 free_selected_names (fs
->selected_names
);
2410 fs
->selected_names
= NULL
;
2413 goto maybe_clear_entry
;
2416 if (new_names
->len
!= 1)
2418 GPtrArray
*old_names
= fs
->selected_names
;
2420 if (old_names
!= NULL
)
2422 /* A common case is selecting a range of files from top to bottom,
2423 * so quickly check for that to avoid looping over the entire list
2425 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2426 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2427 index
= new_names
->len
- 1;
2433 /* do a quick diff, stopping at the first file not in the
2436 while (i
< old_names
->len
&& j
< new_names
->len
)
2438 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2439 g_ptr_array_index (new_names
, j
));
2456 /* we ran off the end of the old list */
2457 if (index
== -1 && i
< new_names
->len
)
2463 /* A phantom anchor still exists at the point where the last item
2464 * was selected, which is used for subsequent range selections.
2465 * So search up from there.
2467 if (fs
->last_selected
&&
2468 compare_filenames (fs
->last_selected
,
2469 g_ptr_array_index (new_names
, 0)) == 0)
2470 index
= new_names
->len
- 1;
2478 if (fs
->selected_names
!= NULL
)
2479 free_selected_names (fs
->selected_names
);
2481 fs
->selected_names
= new_names
;
2487 err
= gtk_label_get_text (GTK_LABEL (fs
->selection_text
));
2488 err
+= 11; //pass over "Selection: "
2489 sprintf(str
,"%s",err
);
2492 if (fs
->last_selected
!= NULL
)
2493 g_free (fs
->last_selected
);
2495 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2496 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2499 strcat(str
,filename
);
2500 str
[strlen(str
)-1] = '\0';
2502 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), str
);
2504 if (filename
!= fs
->last_selected
)
2512 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2513 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2514 (compare_filenames (entry
, fs
->last_selected
) == 0))
2515 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2519 * gtk_dir_selection_get_selections:
2520 * @filesel: a #GtkDirSelection
2522 * Retrieves the list of file selections the user has made in the dialog box.
2523 * This function is intended for use when the user can select multiple files
2524 * in the file list. The first file in the list is equivalent to what
2525 * gtk_dir_selection_get_filename() would return.
2527 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2528 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2529 * g_filename_to_utf8() on each string.
2531 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2532 * g_strfreev() to free it.
2535 gtk_dir_selection_get_selections (GtkDirSelection
*filesel
)
2539 gchar
*filename
, *dirname
;
2540 gchar
*current
, *buf
;
2542 gboolean unselected_entry
;
2544 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), NULL
);
2546 filename
= g_strdup (gtk_dir_selection_get_filename (filesel
));
2548 if (strlen (filename
) == 0)
2554 names
= filesel
->selected_names
;
2557 selections
= g_new (gchar
*, names
->len
+ 2);
2559 selections
= g_new (gchar
*, 2);
2562 unselected_entry
= TRUE
;
2566 dirname
= g_path_get_dirname (filename
);
2568 for (i
= 0; i
< names
->len
; i
++)
2570 buf
= g_filename_from_utf8 (g_ptr_array_index (names
, i
), -1,
2572 current
= g_build_filename (dirname
, buf
, NULL
);
2575 selections
[count
++] = current
;
2577 if (unselected_entry
&& compare_filenames (current
, filename
) == 0)
2578 unselected_entry
= FALSE
;
2584 if (unselected_entry
)
2585 selections
[count
++] = filename
;
2589 selections
[count
] = NULL
;
2594 /**********************************************************************/
2595 /* External Interface */
2596 /**********************************************************************/
2598 /* The four completion state selectors
2601 cmpl_updated_text (CompletionState
*cmpl_state
)
2603 return cmpl_state
->updated_text
;
2607 cmpl_updated_dir (CompletionState
*cmpl_state
)
2609 return cmpl_state
->re_complete
;
2613 cmpl_reference_position (CompletionState
*cmpl_state
)
2615 return cmpl_state
->reference_dir
->fullname
;
2619 cmpl_last_valid_char (CompletionState
*cmpl_state
)
2621 return cmpl_state
->last_valid_char
;
2625 cmpl_completion_fullname (const gchar
*text
,
2626 CompletionState
*cmpl_state
)
2628 static const char nothing
[2] = "";
2630 if (!cmpl_state_okay (cmpl_state
))
2634 else if (g_path_is_absolute (text
))
2636 strcpy (cmpl_state
->updated_text
, text
);
2639 else if (text
[0] == '~')
2644 dir
= open_user_dir (text
, cmpl_state
);
2648 /* spencer says just return ~something, so
2649 * for now just do it. */
2650 strcpy (cmpl_state
->updated_text
, text
);
2655 strcpy (cmpl_state
->updated_text
, dir
->fullname
);
2657 slash
= strchr (text
, G_DIR_SEPARATOR
);
2660 strcat (cmpl_state
->updated_text
, slash
);
2666 strcpy (cmpl_state
->updated_text
, cmpl_state
->reference_dir
->fullname
);
2667 if (cmpl_state
->updated_text
[strlen (cmpl_state
->updated_text
) - 1] != G_DIR_SEPARATOR
)
2668 strcat (cmpl_state
->updated_text
, G_DIR_SEPARATOR_S
);
2669 strcat (cmpl_state
->updated_text
, text
);
2672 return cmpl_state
->updated_text
;
2675 /* The three completion selectors
2678 cmpl_this_completion (PossibleCompletion
* pc
)
2684 cmpl_is_directory (PossibleCompletion
* pc
)
2686 return pc
->is_directory
;
2690 cmpl_is_a_completion (PossibleCompletion
* pc
)
2692 return pc
->is_a_completion
;
2695 /**********************************************************************/
2696 /* Construction, deletion */
2697 /**********************************************************************/
2699 static CompletionState
*
2700 cmpl_init_state (void)
2702 gchar
*sys_getcwd_buf
;
2704 CompletionState
*new_state
;
2706 new_state
= g_new (CompletionState
, 1);
2708 /* g_get_current_dir() returns a string in the "system" charset */
2709 sys_getcwd_buf
= g_get_current_dir ();
2710 utf8_cwd
= g_filename_to_utf8 (sys_getcwd_buf
, -1, NULL
, NULL
, NULL
);
2711 g_free (sys_getcwd_buf
);
2715 new_state
->reference_dir
= NULL
;
2716 new_state
->completion_dir
= NULL
;
2717 new_state
->active_completion_dir
= NULL
;
2718 new_state
->directory_storage
= NULL
;
2719 new_state
->directory_sent_storage
= NULL
;
2720 new_state
->last_valid_char
= 0;
2721 new_state
->updated_text
= g_new (gchar
, MAXPATHLEN
);
2722 new_state
->updated_text_alloc
= MAXPATHLEN
;
2723 new_state
->the_completion
.text
= g_new (gchar
, MAXPATHLEN
);
2724 new_state
->the_completion
.text_alloc
= MAXPATHLEN
;
2725 new_state
->user_dir_name_buffer
= NULL
;
2726 new_state
->user_directories
= NULL
;
2728 new_state
->reference_dir
= open_dir (utf8_cwd
, new_state
);
2730 if (!new_state
->reference_dir
)
2732 /* Directories changing from underneath us, grumble */
2733 strcpy (utf8_cwd
, G_DIR_SEPARATOR_S
);
2742 cmpl_free_dir_list (GList
* dp0
)
2748 free_dir (dp
->data
);
2756 cmpl_free_dir_sent_list (GList
* dp0
)
2762 free_dir_sent (dp
->data
);
2770 cmpl_free_state (CompletionState
* cmpl_state
)
2772 g_return_if_fail (cmpl_state
!= NULL
);
2774 cmpl_free_dir_list (cmpl_state
->directory_storage
);
2775 cmpl_free_dir_sent_list (cmpl_state
->directory_sent_storage
);
2777 if (cmpl_state
->user_dir_name_buffer
)
2778 g_free (cmpl_state
->user_dir_name_buffer
);
2779 if (cmpl_state
->user_directories
)
2780 g_free (cmpl_state
->user_directories
);
2781 if (cmpl_state
->the_completion
.text
)
2782 g_free (cmpl_state
->the_completion
.text
);
2783 if (cmpl_state
->updated_text
)
2784 g_free (cmpl_state
->updated_text
);
2786 g_free (cmpl_state
);
2790 free_dir (CompletionDir
* dir
)
2792 g_free (dir
->cmpl_text
);
2793 g_free (dir
->fullname
);
2798 free_dir_sent (CompletionDirSent
* sent
)
2801 for (i
= 0; i
< sent
->entry_count
; i
++)
2803 g_free (sent
->entries
[i
].entry_name
);
2804 g_free (sent
->entries
[i
].sort_key
);
2806 g_free (sent
->entries
);
2811 prune_memory_usage (CompletionState
*cmpl_state
)
2813 GList
* cdsl
= cmpl_state
->directory_sent_storage
;
2814 GList
* cdl
= cmpl_state
->directory_storage
;
2818 for (; cdsl
&& len
< CMPL_DIRECTORY_CACHE_SIZE
; len
+= 1)
2823 cmpl_free_dir_sent_list (cdsl
->next
);
2827 cmpl_state
->directory_storage
= NULL
;
2830 if (cdl
->data
== cmpl_state
->reference_dir
)
2831 cmpl_state
->directory_storage
= g_list_prepend (NULL
, cdl
->data
);
2833 free_dir (cdl
->data
);
2840 /**********************************************************************/
2841 /* The main entrances. */
2842 /**********************************************************************/
2844 static PossibleCompletion
*
2845 cmpl_completion_matches (gchar
*text_to_complete
,
2846 gchar
**remaining_text
,
2847 CompletionState
*cmpl_state
)
2849 PossibleCompletion
*poss
;
2851 prune_memory_usage (cmpl_state
);
2853 g_assert (text_to_complete
!= NULL
);
2855 cmpl_state
->user_completion_index
= -1;
2856 cmpl_state
->last_completion_text
= text_to_complete
;
2857 cmpl_state
->the_completion
.text
[0] = 0;
2858 cmpl_state
->last_valid_char
= 0;
2859 cmpl_state
->updated_text_len
= -1;
2860 cmpl_state
->updated_text
[0] = 0;
2861 cmpl_state
->re_complete
= FALSE
;
2864 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2866 if (text_to_complete
[0] == '~' && !first_slash
)
2868 /* Text starts with ~ and there is no slash, show all the
2869 * home directory completions.
2871 poss
= attempt_homedir_completion (text_to_complete
, cmpl_state
);
2873 update_cmpl (poss
, cmpl_state
);
2878 cmpl_state
->reference_dir
=
2879 open_ref_dir (text_to_complete
, remaining_text
, cmpl_state
);
2881 if (!cmpl_state
->reference_dir
)
2884 cmpl_state
->completion_dir
=
2885 find_completion_dir (*remaining_text
, remaining_text
, cmpl_state
);
2887 cmpl_state
->last_valid_char
= *remaining_text
- text_to_complete
;
2889 if (!cmpl_state
->completion_dir
)
2892 cmpl_state
->completion_dir
->cmpl_index
= -1;
2893 cmpl_state
->completion_dir
->cmpl_parent
= NULL
;
2894 cmpl_state
->completion_dir
->cmpl_text
= g_strdup (*remaining_text
);
2896 cmpl_state
->active_completion_dir
= cmpl_state
->completion_dir
;
2898 cmpl_state
->reference_dir
= cmpl_state
->completion_dir
;
2900 poss
= attempt_dir_completion (cmpl_state
);
2902 update_cmpl (poss
, cmpl_state
);
2907 static PossibleCompletion
*
2908 cmpl_next_completion (CompletionState
* cmpl_state
)
2910 PossibleCompletion
* poss
= NULL
;
2912 cmpl_state
->the_completion
.text
[0] = 0;
2915 if (cmpl_state
->user_completion_index
>= 0)
2916 poss
= attempt_homedir_completion (cmpl_state
->last_completion_text
, cmpl_state
);
2918 poss
= attempt_dir_completion (cmpl_state
);
2920 poss
= attempt_dir_completion (cmpl_state
);
2923 update_cmpl (poss
, cmpl_state
);
2928 /**********************************************************************/
2929 /* Directory Operations */
2930 /**********************************************************************/
2932 /* Open the directory where completion will begin from, if possible. */
2933 static CompletionDir
*
2934 open_ref_dir (gchar
*text_to_complete
,
2935 gchar
**remaining_text
,
2936 CompletionState
*cmpl_state
)
2939 CompletionDir
*new_dir
;
2941 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2943 #ifdef G_WITH_CYGWIN
2944 if (text_to_complete
[0] == '/' && text_to_complete
[1] == '/')
2947 g_snprintf (root_dir
, sizeof (root_dir
), "//%c", text_to_complete
[2]);
2949 new_dir
= open_dir (root_dir
, cmpl_state
);
2952 *remaining_text
= text_to_complete
+ 4;
2960 else if (text_to_complete
[0] == '~')
2962 new_dir
= open_user_dir (text_to_complete
, cmpl_state
);
2967 *remaining_text
= first_slash
+ 1;
2969 *remaining_text
= text_to_complete
+ strlen (text_to_complete
);
2977 else if (g_path_is_absolute (text_to_complete
) || !cmpl_state
->reference_dir
)
2979 gchar
*tmp
= g_strdup (text_to_complete
);
2983 while (*p
&& *p
!= '*' && *p
!= '?')
2987 p
= strrchr (tmp
, G_DIR_SEPARATOR
);
2995 new_dir
= open_dir (tmp
, cmpl_state
);
2998 *remaining_text
= text_to_complete
+
2999 ((p
== tmp
+ 1) ? (p
- tmp
) : (p
+ 1 - tmp
));
3003 /* If no possible candidates, use the cwd */
3004 gchar
*sys_curdir
= g_get_current_dir ();
3005 gchar
*utf8_curdir
= g_filename_to_utf8 (sys_curdir
, -1, NULL
, NULL
, NULL
);
3007 g_free (sys_curdir
);
3009 new_dir
= open_dir (utf8_curdir
, cmpl_state
);
3012 *remaining_text
= text_to_complete
;
3014 g_free (utf8_curdir
);
3021 *remaining_text
= text_to_complete
;
3023 new_dir
= open_dir (cmpl_state
->reference_dir
->fullname
, cmpl_state
);
3028 new_dir
->cmpl_index
= -1;
3029 new_dir
->cmpl_parent
= NULL
;
3037 /* open a directory by user name */
3038 static CompletionDir
*
3039 open_user_dir (const gchar
*text_to_complete
,
3040 CompletionState
*cmpl_state
)
3042 CompletionDir
*result
;
3046 g_assert (text_to_complete
&& text_to_complete
[0] == '~');
3048 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
3051 cmp_len
= first_slash
- text_to_complete
- 1;
3053 cmp_len
= strlen (text_to_complete
+ 1);
3058 const gchar
*homedir
= g_get_home_dir ();
3059 gchar
*utf8_homedir
= g_filename_to_utf8 (homedir
, -1, NULL
, NULL
, NULL
);
3062 result
= open_dir (utf8_homedir
, cmpl_state
);
3066 g_free (utf8_homedir
);
3071 gchar
* copy
= g_new (char, cmp_len
+ 1);
3075 strncpy (copy
, text_to_complete
+ 1, cmp_len
);
3077 pwd
= getpwnam (copy
);
3084 utf8_dir
= g_filename_to_utf8 (pwd
->pw_dir
, -1, NULL
, NULL
, NULL
);
3085 result
= open_dir (utf8_dir
, cmpl_state
);
3093 /* open a directory relative the the current relative directory */
3094 static CompletionDir
*
3095 open_relative_dir (gchar
*dir_name
,
3097 CompletionState
*cmpl_state
)
3099 CompletionDir
*result
;
3102 path
= g_string_sized_new (dir
->fullname_len
+ strlen (dir_name
) + 10);
3103 g_string_assign (path
, dir
->fullname
);
3105 if (dir
->fullname_len
> 1
3106 && path
->str
[dir
->fullname_len
- 1] != G_DIR_SEPARATOR
)
3107 g_string_append_c (path
, G_DIR_SEPARATOR
);
3108 g_string_append (path
, dir_name
);
3110 result
= open_dir (path
->str
, cmpl_state
);
3112 g_string_free (path
, TRUE
);
3117 /* after the cache lookup fails, really open a new directory */
3118 static CompletionDirSent
*
3119 open_new_dir (gchar
*dir_name
,
3121 gboolean stat_subdirs
)
3123 CompletionDirSent
*sent
;
3126 GError
*error
= NULL
;
3127 gint entry_count
= 0;
3130 struct stat ent_sbuf
;
3132 gchar
*sys_dir_name
;
3134 sent
= g_new (CompletionDirSent
, 1);
3135 sent
->mtime
= sbuf
->st_mtime
;
3136 sent
->inode
= sbuf
->st_ino
;
3137 sent
->device
= sbuf
->st_dev
;
3139 path
= g_string_sized_new (2*MAXPATHLEN
+ 10);
3141 sys_dir_name
= g_filename_from_utf8 (dir_name
, -1, NULL
, NULL
, NULL
);
3144 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3148 directory
= g_dir_open (sys_dir_name
, 0, &error
);
3151 cmpl_errno
= error
->code
; /* ??? */
3152 g_free (sys_dir_name
);
3156 while ((dirent
= g_dir_read_name (directory
)) != NULL
)
3159 entry_count
+= 2; /* For ".",".." */
3161 sent
->entries
= g_new (CompletionDirEntry
, entry_count
);
3162 sent
->entry_count
= entry_count
;
3164 g_dir_rewind (directory
);
3166 for (i
= 0; i
< entry_count
; i
+= 1)
3174 dirent
= g_dir_read_name (directory
);
3175 if (!dirent
) /* Directory changed */
3179 sent
->entries
[n_entries
].entry_name
= g_filename_to_utf8 (dirent
, -1, NULL
, NULL
, &error
);
3180 if (sent
->entries
[n_entries
].entry_name
== NULL
3181 || !g_utf8_validate (sent
->entries
[n_entries
].entry_name
, -1, NULL
))
3183 gchar
*escaped_str
= g_strescape (dirent
, NULL
);
3184 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3185 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3187 error
->message
? error
->message
: _("Invalid Utf-8"));
3188 g_free (escaped_str
);
3189 g_clear_error (&error
);
3192 g_clear_error (&error
);
3194 sent
->entries
[n_entries
].sort_key
= g_utf8_collate_key (sent
->entries
[n_entries
].entry_name
, -1);
3196 g_string_assign (path
, sys_dir_name
);
3197 if (path
->str
[path
->len
-1] != G_DIR_SEPARATOR
)
3199 g_string_append_c (path
, G_DIR_SEPARATOR
);
3201 g_string_append (path
, dirent
);
3205 /* Here we know path->str is a "system charset" string */
3206 if (stat (path
->str
, &ent_sbuf
) >= 0 && S_ISDIR (ent_sbuf
.st_mode
))
3207 sent
->entries
[n_entries
].is_dir
= TRUE
;
3209 /* stat may fail, and we don't mind, since it could be a
3210 * dangling symlink. */
3211 sent
->entries
[n_entries
].is_dir
= FALSE
;
3214 sent
->entries
[n_entries
].is_dir
= 1;
3218 sent
->entry_count
= n_entries
;
3220 g_free (sys_dir_name
);
3221 g_string_free (path
, TRUE
);
3222 qsort (sent
->entries
, sent
->entry_count
, sizeof (CompletionDirEntry
), compare_cmpl_dir
);
3224 g_dir_close (directory
);
3229 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3232 check_dir (gchar
*dir_name
,
3233 struct stat
*result
,
3234 gboolean
*stat_subdirs
)
3236 /* A list of directories that we know only contain other directories.
3237 * Trying to stat every file in these directories would be very
3244 struct stat statbuf
;
3245 } no_stat_dirs
[] = {
3246 { "/afs", FALSE
, { 0 } },
3247 { "/net", FALSE
, { 0 } }
3250 static const gint n_no_stat_dirs
= G_N_ELEMENTS (no_stat_dirs
);
3251 static gboolean initialized
= FALSE
;
3252 gchar
*sys_dir_name
;
3258 for (i
= 0; i
< n_no_stat_dirs
; i
++)
3260 if (stat (no_stat_dirs
[i
].name
, &no_stat_dirs
[i
].statbuf
) == 0)
3261 no_stat_dirs
[i
].present
= TRUE
;
3265 sys_dir_name
= g_filename_from_utf8 (dir_name
, -1, NULL
, NULL
, NULL
);
3268 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3272 if (stat (sys_dir_name
, result
) < 0)
3274 g_free (sys_dir_name
);
3278 g_free (sys_dir_name
);
3280 *stat_subdirs
= TRUE
;
3281 for (i
= 0; i
< n_no_stat_dirs
; i
++)
3283 if (no_stat_dirs
[i
].present
&&
3284 (no_stat_dirs
[i
].statbuf
.st_dev
== result
->st_dev
) &&
3285 (no_stat_dirs
[i
].statbuf
.st_ino
== result
->st_ino
))
3287 *stat_subdirs
= FALSE
;
3297 /* open a directory by absolute pathname */
3298 static CompletionDir
*
3299 open_dir (gchar
*dir_name
,
3300 CompletionState
*cmpl_state
)
3303 gboolean stat_subdirs
;
3304 CompletionDirSent
*sent
;
3307 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3308 if (!check_dir (dir_name
, &sbuf
, &stat_subdirs
))
3311 cdsl
= cmpl_state
->directory_sent_storage
;
3317 if (sent
->inode
== sbuf
.st_ino
&&
3318 sent
->mtime
== sbuf
.st_mtime
&&
3319 sent
->device
== sbuf
.st_dev
)
3320 return attach_dir (sent
, dir_name
, cmpl_state
);
3325 stat_subdirs
= TRUE
;
3328 sent
= open_new_dir (dir_name
, &sbuf
, stat_subdirs
);
3332 cmpl_state
->directory_sent_storage
=
3333 g_list_prepend (cmpl_state
->directory_sent_storage
, sent
);
3335 return attach_dir (sent
, dir_name
, cmpl_state
);
3341 static CompletionDir
*
3342 attach_dir (CompletionDirSent
*sent
,
3344 CompletionState
*cmpl_state
)
3346 CompletionDir
* new_dir
;
3348 new_dir
= g_new (CompletionDir
, 1);
3350 cmpl_state
->directory_storage
=
3351 g_list_prepend (cmpl_state
->directory_storage
, new_dir
);
3353 new_dir
->sent
= sent
;
3354 new_dir
->fullname
= g_strdup (dir_name
);
3355 new_dir
->fullname_len
= strlen (dir_name
);
3356 new_dir
->cmpl_text
= NULL
;
3362 correct_dir_fullname (CompletionDir
* cmpl_dir
)
3364 gint length
= strlen (cmpl_dir
->fullname
);
3365 gchar
*first_slash
= strchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3366 gchar
*sys_filename
;
3369 /* Does it end with /. (\.) ? */
3371 strcmp (cmpl_dir
->fullname
+ length
- 2, G_DIR_SEPARATOR_S
".") == 0)
3373 /* Is it just the root directory (on a drive) ? */
3374 if (cmpl_dir
->fullname
+ length
- 2 == first_slash
)
3376 cmpl_dir
->fullname
[length
- 1] = 0;
3377 cmpl_dir
->fullname_len
= length
- 1;
3382 cmpl_dir
->fullname
[length
- 2] = 0;
3386 /* Ends with /./ (\.\)? */
3387 else if (length
>= 3 &&
3388 strcmp (cmpl_dir
->fullname
+ length
- 3,
3389 G_DIR_SEPARATOR_S
"." G_DIR_SEPARATOR_S
) == 0)
3390 cmpl_dir
->fullname
[length
- 2] = 0;
3392 /* Ends with /.. (\..) ? */
3393 else if (length
>= 3 &&
3394 strcmp (cmpl_dir
->fullname
+ length
- 3,
3395 G_DIR_SEPARATOR_S
"..") == 0)
3397 /* Is it just /.. (X:\..)? */
3398 if (cmpl_dir
->fullname
+ length
- 3 == first_slash
)
3400 cmpl_dir
->fullname
[length
- 2] = 0;
3401 cmpl_dir
->fullname_len
= length
- 2;
3405 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3408 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3412 if (stat (sys_filename
, &sbuf
) < 0)
3414 g_free (sys_filename
);
3418 g_free (sys_filename
);
3420 cmpl_dir
->fullname
[length
- 3] = 0;
3422 if (!correct_parent (cmpl_dir
, &sbuf
))
3426 /* Ends with /../ (\..\)? */
3427 else if (length
>= 4 &&
3428 strcmp (cmpl_dir
->fullname
+ length
- 4,
3429 G_DIR_SEPARATOR_S
".." G_DIR_SEPARATOR_S
) == 0)
3431 /* Is it just /../ (X:\..\)? */
3432 if (cmpl_dir
->fullname
+ length
- 4 == first_slash
)
3434 cmpl_dir
->fullname
[length
- 3] = 0;
3435 cmpl_dir
->fullname_len
= length
- 3;
3439 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3442 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3446 if (stat (sys_filename
, &sbuf
) < 0)
3448 g_free (sys_filename
);
3452 g_free (sys_filename
);
3454 cmpl_dir
->fullname
[length
- 4] = 0;
3456 if (!correct_parent (cmpl_dir
, &sbuf
))
3460 cmpl_dir
->fullname_len
= strlen (cmpl_dir
->fullname
);
3466 correct_parent (CompletionDir
*cmpl_dir
,
3473 gchar
*sys_filename
;
3476 last_slash
= strrchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3477 g_assert (last_slash
);
3478 first_slash
= strchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3480 /* Clever (?) way to check for top-level directory that works also on
3481 * Win32, where there is a drive letter and colon prefixed...
3483 if (last_slash
!= first_slash
)
3493 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3496 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3498 last_slash
[0] = G_DIR_SEPARATOR
;
3502 if (stat (sys_filename
, &parbuf
) < 0)
3504 g_free (sys_filename
);
3507 last_slash
[0] = G_DIR_SEPARATOR
;
3510 g_free (sys_filename
);
3512 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3513 if (parbuf
.st_ino
== sbuf
->st_ino
&& parbuf
.st_dev
== sbuf
->st_dev
)
3514 /* it wasn't a link */
3520 last_slash
[0] = G_DIR_SEPARATOR
;
3522 /* it was a link, have to figure it out the hard way */
3524 new_name
= find_parent_dir_fullname (cmpl_dir
->fullname
);
3529 g_free (cmpl_dir
->fullname
);
3531 cmpl_dir
->fullname
= new_name
;
3540 find_parent_dir_fullname (gchar
* dirname
)
3542 gchar
*sys_orig_dir
;
3547 sys_orig_dir
= g_get_current_dir ();
3548 sys_dirname
= g_filename_from_utf8 (dirname
, -1, NULL
, NULL
, NULL
);
3551 g_free (sys_orig_dir
);
3552 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3556 if (chdir (sys_dirname
) != 0 || chdir ("..") != 0)
3559 chdir (sys_orig_dir
);
3560 g_free (sys_dirname
);
3561 g_free (sys_orig_dir
);
3564 g_free (sys_dirname
);
3566 sys_cwd
= g_get_current_dir ();
3567 result
= g_filename_to_utf8 (sys_cwd
, -1, NULL
, NULL
, NULL
);
3570 if (chdir (sys_orig_dir
) != 0)
3573 g_free (sys_orig_dir
);
3577 g_free (sys_orig_dir
);
3583 /**********************************************************************/
3584 /* Completion Operations */
3585 /**********************************************************************/
3589 static PossibleCompletion
*
3590 attempt_homedir_completion (gchar
*text_to_complete
,
3591 CompletionState
*cmpl_state
)
3595 if (!cmpl_state
->user_dir_name_buffer
&&
3596 !get_pwdb (cmpl_state
))
3598 length
= strlen (text_to_complete
) - 1;
3600 cmpl_state
->user_completion_index
+= 1;
3602 while (cmpl_state
->user_completion_index
< cmpl_state
->user_directories_len
)
3604 index
= first_diff_index (text_to_complete
+ 1,
3605 cmpl_state
->user_directories
3606 [cmpl_state
->user_completion_index
].login
);
3613 if (cmpl_state
->last_valid_char
< (index
+ 1))
3614 cmpl_state
->last_valid_char
= index
+ 1;
3615 cmpl_state
->user_completion_index
+= 1;
3619 cmpl_state
->the_completion
.is_a_completion
= 1;
3620 cmpl_state
->the_completion
.is_directory
= TRUE
;
3622 append_completion_text ("~", cmpl_state
);
3624 append_completion_text (cmpl_state
->
3625 user_directories
[cmpl_state
->user_completion_index
].login
,
3628 return append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3631 if (text_to_complete
[1]
3632 || cmpl_state
->user_completion_index
> cmpl_state
->user_directories_len
)
3634 cmpl_state
->user_completion_index
= -1;
3639 cmpl_state
->user_completion_index
+= 1;
3640 cmpl_state
->the_completion
.is_a_completion
= 1;
3641 cmpl_state
->the_completion
.is_directory
= TRUE
;
3643 return append_completion_text ("~" G_DIR_SEPARATOR_S
, cmpl_state
);
3649 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3650 #define FOLD(c) (tolower(c))
3655 /* returns the index (>= 0) of the first differing character,
3656 * PATTERN_MATCH if the completion matches */
3658 first_diff_index (gchar
*pat
,
3663 while (*pat
&& *text
&& FOLD (*text
) == FOLD (*pat
))
3673 return PATTERN_MATCH
;
3676 static PossibleCompletion
*
3677 append_completion_text (gchar
*text
,
3678 CompletionState
*cmpl_state
)
3682 if (!cmpl_state
->the_completion
.text
)
3685 len
= strlen (text
) + strlen (cmpl_state
->the_completion
.text
) + 1;
3687 if (cmpl_state
->the_completion
.text_alloc
> len
)
3689 strcat (cmpl_state
->the_completion
.text
, text
);
3690 return &cmpl_state
->the_completion
;
3696 cmpl_state
->the_completion
.text_alloc
= i
;
3698 cmpl_state
->the_completion
.text
= (gchar
*) g_realloc (cmpl_state
->the_completion
.text
, i
);
3700 if (!cmpl_state
->the_completion
.text
)
3704 strcat (cmpl_state
->the_completion
.text
, text
);
3705 return &cmpl_state
->the_completion
;
3709 static CompletionDir
*
3710 find_completion_dir (gchar
*text_to_complete
,
3711 gchar
**remaining_text
,
3712 CompletionState
*cmpl_state
)
3714 gchar
* first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
3715 CompletionDir
* dir
= cmpl_state
->reference_dir
;
3716 CompletionDir
* next
;
3717 *remaining_text
= text_to_complete
;
3721 gint len
= first_slash
- *remaining_text
;
3723 gchar
*found_name
= NULL
; /* Quiet gcc */
3725 gchar
* pat_buf
= g_new (gchar
, len
+ 1);
3727 strncpy (pat_buf
, *remaining_text
, len
);
3730 for (i
= 0; i
< dir
->sent
->entry_count
; i
+= 1)
3732 if (dir
->sent
->entries
[i
].is_dir
&&
3733 _gtk_fnmatch (pat_buf
, dir
->sent
->entries
[i
].entry_name
,1))
3743 found_name
= dir
->sent
->entries
[i
].entry_name
;
3750 /* Perhaps we are trying to open an automount directory */
3751 found_name
= pat_buf
;
3754 next
= open_relative_dir (found_name
, dir
, cmpl_state
);
3762 next
->cmpl_parent
= dir
;
3766 if (!correct_dir_fullname (dir
))
3772 *remaining_text
= first_slash
+ 1;
3773 first_slash
= strchr (*remaining_text
, G_DIR_SEPARATOR
);
3782 update_cmpl (PossibleCompletion
*poss
,
3783 CompletionState
*cmpl_state
)
3787 if (!poss
|| !cmpl_is_a_completion (poss
))
3790 cmpl_len
= strlen (cmpl_this_completion (poss
));
3792 if (cmpl_state
->updated_text_alloc
< cmpl_len
+ 1)
3794 cmpl_state
->updated_text
=
3795 (gchar
*)g_realloc (cmpl_state
->updated_text
,
3796 cmpl_state
->updated_text_alloc
);
3797 cmpl_state
->updated_text_alloc
= 2*cmpl_len
;
3800 if (cmpl_state
->updated_text_len
< 0)
3802 strcpy (cmpl_state
->updated_text
, cmpl_this_completion (poss
));
3803 cmpl_state
->updated_text_len
= cmpl_len
;
3804 cmpl_state
->re_complete
= cmpl_is_directory (poss
);
3806 else if (cmpl_state
->updated_text_len
== 0)
3808 cmpl_state
->re_complete
= FALSE
;
3813 first_diff_index (cmpl_state
->updated_text
,
3814 cmpl_this_completion (poss
));
3816 cmpl_state
->re_complete
= FALSE
;
3818 if (first_diff
== PATTERN_MATCH
)
3821 if (first_diff
> cmpl_state
->updated_text_len
)
3822 strcpy (cmpl_state
->updated_text
, cmpl_this_completion (poss
));
3824 cmpl_state
->updated_text_len
= first_diff
;
3825 cmpl_state
->updated_text
[first_diff
] = 0;
3829 static PossibleCompletion
*
3830 attempt_dir_completion (CompletionState
*cmpl_state
)
3832 gchar
*pat_buf
, *first_slash
;
3833 CompletionDir
*dir
= cmpl_state
->active_completion_dir
;
3835 dir
->cmpl_index
+= 1;
3837 if (dir
->cmpl_index
== dir
->sent
->entry_count
)
3839 if (dir
->cmpl_parent
== NULL
)
3841 cmpl_state
->active_completion_dir
= NULL
;
3847 cmpl_state
->active_completion_dir
= dir
->cmpl_parent
;
3849 return attempt_dir_completion (cmpl_state
);
3853 g_assert (dir
->cmpl_text
);
3855 first_slash
= strchr (dir
->cmpl_text
, G_DIR_SEPARATOR
);
3859 gint len
= first_slash
- dir
->cmpl_text
;
3861 pat_buf
= g_new (gchar
, len
+ 1);
3862 strncpy (pat_buf
, dir
->cmpl_text
, len
);
3867 gint len
= strlen (dir
->cmpl_text
);
3869 pat_buf
= g_new (gchar
, len
+ 2);
3870 strcpy (pat_buf
, dir
->cmpl_text
);
3871 /* Don't append a * if the user entered one herself.
3872 * This way one can complete *.h and don't get matches
3873 * on any .help files, for instance.
3875 if (strchr (pat_buf
, '*') == NULL
)
3876 strcpy (pat_buf
+ len
, "*");
3881 if (dir
->sent
->entries
[dir
->cmpl_index
].is_dir
)
3883 if (_gtk_fnmatch (pat_buf
,
3884 dir
->sent
->entries
[dir
->cmpl_index
].entry_name
,1))
3886 CompletionDir
* new_dir
;
3888 new_dir
= open_relative_dir (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
,
3897 new_dir
->cmpl_parent
= dir
;
3899 new_dir
->cmpl_index
= -1;
3900 new_dir
->cmpl_text
= g_strdup (first_slash
+ 1);
3902 cmpl_state
->active_completion_dir
= new_dir
;
3905 return attempt_dir_completion (cmpl_state
);
3910 return attempt_dir_completion (cmpl_state
);
3916 return attempt_dir_completion (cmpl_state
);
3921 if (dir
->cmpl_parent
!= NULL
)
3923 append_completion_text (dir
->fullname
+
3924 strlen (cmpl_state
->completion_dir
->fullname
) + 1,
3926 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3929 append_completion_text (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
, cmpl_state
);
3931 cmpl_state
->the_completion
.is_a_completion
=
3932 _gtk_fnmatch (pat_buf
, dir
->sent
->entries
[dir
->cmpl_index
].entry_name
,1);
3934 cmpl_state
->the_completion
.is_directory
= dir
->sent
->entries
[dir
->cmpl_index
].is_dir
;
3935 if (dir
->sent
->entries
[dir
->cmpl_index
].is_dir
)
3936 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3939 return &cmpl_state
->the_completion
;
3946 get_pwdb (CompletionState
* cmpl_state
)
3948 struct passwd
*pwd_ptr
;
3951 gint len
= 0, i
, count
= 0;
3953 if (cmpl_state
->user_dir_name_buffer
)
3957 while ((pwd_ptr
= getpwent ()) != NULL
)
3959 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
3960 len
+= strlen (utf8
);
3962 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
3963 len
+= strlen (utf8
);
3971 cmpl_state
->user_dir_name_buffer
= g_new (gchar
, len
);
3972 cmpl_state
->user_directories
= g_new (CompletionUserDir
, count
);
3973 cmpl_state
->user_directories_len
= count
;
3975 buf_ptr
= cmpl_state
->user_dir_name_buffer
;
3977 for (i
= 0; i
< count
; i
+= 1)
3979 pwd_ptr
= getpwent ();
3986 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
3987 strcpy (buf_ptr
, utf8
);
3990 cmpl_state
->user_directories
[i
].login
= buf_ptr
;
3992 buf_ptr
+= strlen (buf_ptr
);
3995 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
3996 strcpy (buf_ptr
, utf8
);
3999 cmpl_state
->user_directories
[i
].homedir
= buf_ptr
;
4001 buf_ptr
+= strlen (buf_ptr
);
4005 qsort (cmpl_state
->user_directories
,
4006 cmpl_state
->user_directories_len
,
4007 sizeof (CompletionUserDir
),
4016 if (cmpl_state
->user_dir_name_buffer
)
4017 g_free (cmpl_state
->user_dir_name_buffer
);
4018 if (cmpl_state
->user_directories
)
4019 g_free (cmpl_state
->user_directories
);
4021 cmpl_state
->user_dir_name_buffer
= NULL
;
4022 cmpl_state
->user_directories
= NULL
;
4028 compare_user_dir (const void *a
,
4031 return strcmp ((((CompletionUserDir
*)a
))->login
,
4032 (((CompletionUserDir
*)b
))->login
);
4038 compare_cmpl_dir (const void *a
,
4042 return strcmp (((const CompletionDirEntry
*)a
)->sort_key
,
4043 (((const CompletionDirEntry
*)b
))->sort_key
);
4047 cmpl_state_okay (CompletionState
* cmpl_state
)
4049 return cmpl_state
&& cmpl_state
->reference_dir
;
4053 cmpl_strerror (gint err
)
4055 if (err
== CMPL_ERRNO_TOO_LONG
)
4056 return _("Name too long");
4057 else if (err
== CMPL_ERRNO_DID_NOT_CONVERT
)
4058 return _("Couldn't convert filename");
4060 return g_strerror (err
);
4063 const gchar
* gtk_dir_selection_get_dir (GtkDirSelection
*filesel
)
4065 return gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));