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
,
457 file_selection_type
=
458 g_type_register_static (GTK_TYPE_DIALOG
, "GtkDirSelection",
462 return file_selection_type
;
466 gtk_dir_selection_class_init (GtkDirSelectionClass
*class)
468 GObjectClass
*gobject_class
;
469 GtkObjectClass
*object_class
;
470 GtkWidgetClass
*widget_class
;
472 gobject_class
= (GObjectClass
*) class;
473 object_class
= (GtkObjectClass
*) class;
474 widget_class
= (GtkWidgetClass
*) class;
476 parent_class
= g_type_class_peek_parent (class);
478 gobject_class
->finalize
= gtk_dir_selection_finalize
;
479 gobject_class
->set_property
= gtk_dir_selection_set_property
;
480 gobject_class
->get_property
= gtk_dir_selection_get_property
;
482 g_object_class_install_property (gobject_class
,
484 g_param_spec_string ("filename",
486 _("The currently selected filename"),
488 G_PARAM_READABLE
| G_PARAM_WRITABLE
));
489 g_object_class_install_property (gobject_class
,
491 g_param_spec_boolean ("show_fileops",
492 _("Show file operations"),
493 _("Whether buttons for creating/manipulating files should be displayed"),
497 g_object_class_install_property (gobject_class
,
498 PROP_SELECT_MULTIPLE
,
499 g_param_spec_boolean ("select_multiple",
500 _("Select multiple"),
501 _("Whether to allow multiple files to be selected"),
505 object_class
->destroy
= gtk_dir_selection_destroy
;
506 widget_class
->map
= gtk_dir_selection_map
;
509 static void gtk_dir_selection_set_property (GObject
*object
,
514 GtkDirSelection
*filesel
;
516 filesel
= GTK_DIR_SELECTION (object
);
521 gtk_dir_selection_set_filename (filesel
,
522 g_value_get_string (value
));
524 case PROP_SHOW_FILEOPS
:
525 if (g_value_get_boolean (value
))
526 gtk_dir_selection_show_fileop_buttons (filesel
);
528 gtk_dir_selection_hide_fileop_buttons (filesel
);
530 case PROP_SELECT_MULTIPLE
:
531 gtk_dir_selection_set_select_multiple (filesel
, g_value_get_boolean (value
));
534 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
539 static void gtk_dir_selection_get_property (GObject
*object
,
544 GtkDirSelection
*filesel
;
546 filesel
= GTK_DIR_SELECTION (object
);
551 g_value_set_string (value
,
552 gtk_dir_selection_get_filename(filesel
));
555 case PROP_SHOW_FILEOPS
:
556 /* This is a little bit hacky, but doing otherwise would require
557 * adding a field to the object.
559 g_value_set_boolean (value
, (filesel
->fileop_c_dir
&&
560 filesel
->fileop_del_file
&&
561 filesel
->fileop_ren_file
));
563 case PROP_SELECT_MULTIPLE
:
564 g_value_set_boolean (value
, gtk_dir_selection_get_select_multiple (filesel
));
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
573 grab_default (GtkWidget
*widget
)
575 gtk_widget_grab_default (widget
);
580 gtk_dir_selection_init (GtkDirSelection
*filesel
)
582 GtkWidget
*entry_vbox
;
584 GtkWidget
*list_hbox
, *list_container
;
585 GtkWidget
*confirm_area
;
586 GtkWidget
*pulldown_hbox
;
587 GtkWidget
*scrolled_win
;
593 GtkTreeViewColumn
*column
;
595 gtk_widget_push_composite_child ();
597 dialog
= GTK_DIALOG (filesel
);
599 filesel
->cmpl_state
= cmpl_init_state ();
601 /* The dialog-sized vertical box */
602 filesel
->main_vbox
= dialog
->vbox
;
603 gtk_container_set_border_width (GTK_CONTAINER (filesel
), 10);
605 /* The horizontal box containing create, rename etc. buttons */
606 filesel
->button_area
= gtk_hbutton_box_new ();
607 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel
->button_area
), GTK_BUTTONBOX_START
);
608 gtk_box_set_spacing (GTK_BOX (filesel
->button_area
), 0);
609 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->button_area
,
611 gtk_widget_show (filesel
->button_area
);
613 gtk_dir_selection_show_fileop_buttons (filesel
);
615 /* hbox for pulldown menu */
616 pulldown_hbox
= gtk_hbox_new (TRUE
, 5);
617 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), pulldown_hbox
, FALSE
, FALSE
, 0);
618 gtk_widget_show (pulldown_hbox
);
621 filesel
->history_pulldown
= gtk_option_menu_new ();
622 // gtk_widget_show (filesel->history_pulldown);
623 // gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
626 /* The horizontal box containing the directory and file listboxes */
628 spacer
= gtk_hbox_new (FALSE
, 0);
629 gtk_widget_set_size_request (spacer
, -1, 5);
630 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
631 gtk_widget_show (spacer
);
633 list_hbox
= gtk_hbox_new (FALSE
, 5);
634 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), list_hbox
, TRUE
, TRUE
, 0);
635 gtk_widget_show (list_hbox
);
637 list_container
= g_object_new (GTK_TYPE_HPANED
,
643 list_container
= list_hbox
;
645 spacer
= gtk_hbox_new (FALSE
, 0);
646 gtk_widget_set_size_request (spacer
, -1, 5);
647 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
648 gtk_widget_show (spacer
);
650 /* The directories list */
652 model
= gtk_list_store_new (1, G_TYPE_STRING
);
653 filesel
->dir_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
654 g_object_unref (model
);
656 column
= gtk_tree_view_column_new_with_attributes (_("Folders"),
657 gtk_cell_renderer_text_new (),
660 label
= gtk_label_new_with_mnemonic (_("Fol_ders"));
661 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->dir_list
);
662 gtk_widget_show (label
);
663 gtk_tree_view_column_set_widget (column
, label
);
664 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
665 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->dir_list
), column
);
667 gtk_widget_set_size_request (filesel
->dir_list
,
668 DIR_LIST_WIDTH
, DIR_LIST_HEIGHT
);
669 g_signal_connect (filesel
->dir_list
, "row_activated",
670 G_CALLBACK (gtk_dir_selection_dir_activate
), filesel
);
671 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->dir_list
)), "changed",
672 G_CALLBACK (gtk_dir_selection_dir_changed
), filesel
);
674 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
676 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
677 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
678 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->dir_list
);
679 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
680 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
681 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
682 if (GTK_IS_PANED (list_container
))
683 gtk_paned_pack1 (GTK_PANED (list_container
), scrolled_win
, TRUE
, TRUE
);
685 gtk_container_add (GTK_CONTAINER (list_container
), scrolled_win
);
686 gtk_widget_show (filesel
->dir_list
);
687 gtk_widget_show (scrolled_win
);
690 model
= gtk_list_store_new (1, G_TYPE_STRING
);
691 filesel
->file_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
692 g_object_unref (model
);
694 column
= gtk_tree_view_column_new_with_attributes (_("Files"),
695 gtk_cell_renderer_text_new (),
698 label
= gtk_label_new_with_mnemonic (_("_Files"));
699 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->file_list
);
700 gtk_widget_show (label
);
701 gtk_tree_view_column_set_widget (column
, label
);
702 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
703 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->file_list
), column
);
705 gtk_widget_set_size_request (filesel
->file_list
,
706 FILE_LIST_WIDTH
, FILE_LIST_HEIGHT
);
707 g_signal_connect (filesel
->file_list
, "row_activated",
708 G_CALLBACK (gtk_dir_selection_file_activate
), filesel
);
709 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
)), "changed",
710 G_CALLBACK (gtk_dir_selection_file_changed
), filesel
);
712 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
714 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
715 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
716 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->file_list
);
717 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
718 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
719 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
720 // gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
721 // gtk_widget_show (filesel->file_list);
722 // gtk_widget_show (scrolled_win);
724 /* action area for packing buttons into. */
725 filesel
->action_area
= gtk_hbox_new (TRUE
, 0);
726 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->action_area
,
728 gtk_widget_show (filesel
->action_area
);
730 /* The OK/Cancel button area */
731 confirm_area
= dialog
->action_area
;
733 /* The Cancel button */
734 filesel
->cancel_button
= gtk_dialog_add_button (dialog
,
736 GTK_RESPONSE_CANCEL
);
738 filesel
->ok_button
= gtk_dialog_add_button (dialog
,
742 gtk_widget_grab_default (filesel
->ok_button
);
744 /* The selection entry widget */
745 entry_vbox
= gtk_vbox_new (FALSE
, 2);
746 gtk_box_pack_end (GTK_BOX (filesel
->main_vbox
), entry_vbox
, FALSE
, FALSE
, 2);
747 gtk_widget_show (entry_vbox
);
749 eventbox
= gtk_event_box_new ();
750 filesel
->selection_text
= label
= gtk_label_new ("");
751 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
752 gtk_container_add (GTK_CONTAINER (eventbox
), label
);
753 gtk_box_pack_start (GTK_BOX (entry_vbox
), eventbox
, FALSE
, FALSE
, 0);
754 gtk_widget_show (label
);
755 gtk_widget_show (eventbox
);
757 filesel
->selection_entry
= gtk_entry_new ();
758 g_signal_connect (filesel
->selection_entry
, "key_press_event",
759 G_CALLBACK (gtk_dir_selection_key_press
), filesel
);
760 g_signal_connect (filesel
->selection_entry
, "insert_text",
761 G_CALLBACK (gtk_dir_selection_insert_text
), NULL
);
762 g_signal_connect_swapped (filesel
->selection_entry
, "changed",
763 G_CALLBACK (gtk_dir_selection_update_fileops
), filesel
);
764 g_signal_connect_swapped (filesel
->selection_entry
, "focus_in_event",
765 G_CALLBACK (grab_default
),
767 g_signal_connect_swapped (filesel
->selection_entry
, "activate",
768 G_CALLBACK (gtk_button_clicked
),
771 gtk_box_pack_start (GTK_BOX (entry_vbox
), filesel
->selection_entry
, TRUE
, TRUE
, 0);
772 gtk_widget_show (filesel
->selection_entry
);
774 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel
->selection_text
),
775 filesel
->selection_entry
);
777 if (!cmpl_state_okay (filesel
->cmpl_state
))
781 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
783 gtk_label_set_text (GTK_LABEL (filesel
->selection_text
), err_buf
);
787 gtk_dir_selection_populate (filesel
, "", FALSE
, TRUE
);
790 gtk_widget_grab_focus (filesel
->selection_entry
);
792 gtk_widget_pop_composite_child ();
796 uri_list_extract_first_uri (const gchar
* uri_list
)
800 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
803 /* We don't actually try to validate the URI according to RFC
804 * 2396, or even check for allowed characters - we just ignore
805 * comments and trim whitespace off the ends. We also
806 * allow LF delimination as well as the specified CRLF.
808 * We do allow comments like specified in RFC 2483.
814 while (g_ascii_isspace(*p
))
818 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
824 while (q
> p
&& g_ascii_isspace (*q
))
828 return g_strndup (p
, q
- p
+ 1);
831 p
= strchr (p
, '\n');
839 dnd_really_drop (GtkWidget
*dialog
, gint response_id
, GtkDirSelection
*fs
)
843 if (response_id
== GTK_RESPONSE_YES
)
845 filename
= g_object_get_data (G_OBJECT (dialog
), "gtk-fs-dnd-filename");
847 gtk_dir_selection_set_filename (fs
, filename
);
850 gtk_widget_destroy (dialog
);
855 filenames_dropped (GtkWidget
*widget
,
856 GdkDragContext
*context
,
859 GtkSelectionData
*selection_data
,
864 char *filename
= NULL
;
866 char this_hostname
[257];
868 GError
*error
= NULL
;
870 if (!selection_data
->data
)
873 uri
= uri_list_extract_first_uri ((char *)selection_data
->data
);
878 filename
= g_filename_from_uri (uri
, &hostname
, &error
);
883 g_warning ("Error getting dropped filename: %s\n",
885 g_error_free (error
);
889 res
= gethostname (this_hostname
, 256);
890 this_hostname
[256] = 0;
892 if ((hostname
== NULL
) ||
893 (res
== 0 && strcmp (hostname
, this_hostname
) == 0) ||
894 (strcmp (hostname
, "localhost") == 0))
895 gtk_dir_selection_set_filename (GTK_DIR_SELECTION (widget
),
900 gchar
*filename_utf8
;
902 /* Conversion back to UTF-8 should always succeed for the result
903 * of g_filename_from_uri()
905 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
906 g_assert (filename_utf8
);
908 dialog
= gtk_message_dialog_new (GTK_WINDOW (widget
),
909 GTK_DIALOG_DESTROY_WITH_PARENT
,
910 GTK_MESSAGE_QUESTION
,
912 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
913 "Are you sure that you want to select it?"), filename_utf8
, hostname
);
914 g_free (filename_utf8
);
916 g_object_set_data_full (G_OBJECT (dialog
), "gtk-fs-dnd-filename", g_strdup (filename
), g_free
);
918 g_signal_connect_data (dialog
, "response",
919 (GCallback
) dnd_really_drop
,
922 gtk_widget_show (dialog
);
940 filenames_drag_get (GtkWidget
*widget
,
941 GdkDragContext
*context
,
942 GtkSelectionData
*selection_data
,
945 GtkDirSelection
*filesel
)
953 file
= gtk_dir_selection_get_filename (filesel
);
957 if (info
== TARGET_URILIST
)
959 res
= gethostname (hostname
, 256);
962 uri_list
= g_filename_to_uri (file
, (!res
)?hostname
:NULL
, &error
);
965 g_warning ("Error getting filename: %s\n",
967 g_error_free (error
);
971 gtk_selection_data_set (selection_data
,
972 selection_data
->target
, 8,
973 (void *)uri_list
, strlen((char *)uri_list
));
978 gchar
*filename_utf8
= g_filename_to_utf8 (file
, -1, NULL
, NULL
, NULL
);
979 g_assert (filename_utf8
);
980 gtk_selection_data_set_text (selection_data
, filename_utf8
, -1);
981 g_free (filename_utf8
);
987 file_selection_setup_dnd (GtkDirSelection
*filesel
)
990 static const GtkTargetEntry drop_types
[] = {
991 { "text/uri-list", 0, TARGET_URILIST
}
993 static gint n_drop_types
= sizeof(drop_types
)/sizeof(drop_types
[0]);
994 static const GtkTargetEntry drag_types
[] = {
995 { "text/uri-list", 0, TARGET_URILIST
},
996 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
999 { "COMPOUND_TEXT", 0, 0 }
1001 static gint n_drag_types
= sizeof(drag_types
)/sizeof(drag_types
[0]);
1003 gtk_drag_dest_set (GTK_WIDGET (filesel
),
1004 GTK_DEST_DEFAULT_ALL
,
1005 drop_types
, n_drop_types
,
1008 g_signal_connect (filesel
, "drag_data_received",
1009 G_CALLBACK (filenames_dropped
), NULL
);
1011 eventbox
= gtk_widget_get_parent (filesel
->selection_text
);
1012 gtk_drag_source_set (eventbox
,
1014 drag_types
, n_drag_types
,
1017 g_signal_connect (eventbox
, "drag_data_get",
1018 G_CALLBACK (filenames_drag_get
), filesel
);
1022 gtk_dir_selection_new (const gchar
*title
)
1024 GtkDirSelection
*filesel
;
1026 filesel
= g_object_new (GTK_TYPE_DIR_SELECTION
, NULL
);
1027 gtk_window_set_title (GTK_WINDOW (filesel
), title
);
1028 gtk_dialog_set_has_separator (GTK_DIALOG (filesel
), FALSE
);
1030 file_selection_setup_dnd (filesel
);
1032 return GTK_WIDGET (filesel
);
1036 gtk_dir_selection_show_fileop_buttons (GtkDirSelection
*filesel
)
1038 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1040 /* delete, create directory, and rename */
1041 if (!filesel
->fileop_c_dir
)
1043 filesel
->fileop_c_dir
= gtk_button_new_with_mnemonic (_("_New Folder"));
1044 g_signal_connect (filesel
->fileop_c_dir
, "clicked",
1045 G_CALLBACK (gtk_dir_selection_create_dir
),
1047 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1048 // filesel->fileop_c_dir, TRUE, TRUE, 0);
1049 // gtk_widget_show (filesel->fileop_c_dir);
1052 if (!filesel
->fileop_del_file
)
1054 filesel
->fileop_del_file
= gtk_button_new_with_mnemonic (_("De_lete File"));
1055 g_signal_connect (filesel
->fileop_del_file
, "clicked",
1056 G_CALLBACK (gtk_dir_selection_delete_file
),
1058 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1059 // filesel->fileop_del_file, TRUE, TRUE, 0);
1060 // gtk_widget_show (filesel->fileop_del_file);
1063 if (!filesel
->fileop_ren_file
)
1065 filesel
->fileop_ren_file
= gtk_button_new_with_mnemonic (_("_Rename File"));
1066 g_signal_connect (filesel
->fileop_ren_file
, "clicked",
1067 G_CALLBACK (gtk_dir_selection_rename_file
),
1069 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1070 // filesel->fileop_ren_file, TRUE, TRUE, 0);
1071 // gtk_widget_show (filesel->fileop_ren_file);
1074 gtk_dir_selection_update_fileops (filesel
);
1076 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1080 gtk_dir_selection_hide_fileop_buttons (GtkDirSelection
*filesel
)
1082 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1084 if (filesel
->fileop_ren_file
)
1086 gtk_widget_destroy (filesel
->fileop_ren_file
);
1087 filesel
->fileop_ren_file
= NULL
;
1090 if (filesel
->fileop_del_file
)
1092 gtk_widget_destroy (filesel
->fileop_del_file
);
1093 filesel
->fileop_del_file
= NULL
;
1096 if (filesel
->fileop_c_dir
)
1098 gtk_widget_destroy (filesel
->fileop_c_dir
);
1099 filesel
->fileop_c_dir
= NULL
;
1101 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1107 * gtk_dir_selection_set_filename:
1108 * @filesel: a #GtkDirSelection.
1109 * @filename: a string to set as the default file name.
1111 * Sets a default path for the file requestor. If @filename includes a
1112 * directory path, then the requestor will open with that path as its
1113 * current working directory.
1115 * The encoding of @filename is the on-disk encoding, which
1116 * may not be UTF-8. See g_filename_from_utf8().
1119 gtk_dir_selection_set_filename (GtkDirSelection
*filesel
,
1120 const gchar
*filename
)
1123 const char *name
, *last_slash
;
1124 char *filename_utf8
;
1126 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1127 g_return_if_fail (filename
!= NULL
);
1129 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
1130 g_return_if_fail (filename_utf8
!= NULL
);
1132 last_slash
= strrchr (filename_utf8
, G_DIR_SEPARATOR
);
1136 buf
= g_strdup ("");
1137 name
= filename_utf8
;
1141 buf
= g_strdup (filename_utf8
);
1142 buf
[last_slash
- filename_utf8
+ 1] = 0;
1143 name
= last_slash
+ 1;
1146 gtk_dir_selection_populate (filesel
, buf
, FALSE
, TRUE
);
1148 if (filesel
->selection_entry
)
1149 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), name
);
1151 g_object_notify (G_OBJECT (filesel
), "filename");
1153 g_free (filename_utf8
);
1157 * gtk_dir_selection_get_filename:
1158 * @filesel: a #GtkDirSelection
1160 * This function returns the selected filename in the on-disk encoding
1161 * (see g_filename_from_utf8()), which may or may not be the same as that
1162 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1163 * The returned string points to a statically allocated buffer and
1164 * should be copied if you plan to keep it around.
1166 * If no file is selected then the selected directory path is returned.
1168 * Return value: currently-selected filename in the on-disk encoding.
1170 G_CONST_RETURN gchar
*
1171 gtk_dir_selection_get_filename (GtkDirSelection
*filesel
)
1173 static const gchar nothing
[2] = "";
1174 static gchar something
[MAXPATHLEN
*2];
1178 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), nothing
);
1180 #ifdef G_WITH_CYGWIN
1181 translate_win32_path (filesel
);
1183 text
= gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));
1186 sys_filename
= g_filename_from_utf8 (cmpl_completion_fullname (text
, filesel
->cmpl_state
), -1, NULL
, NULL
, NULL
);
1189 strncpy (something
, sys_filename
, sizeof (something
));
1190 g_free (sys_filename
);
1198 gtk_dir_selection_complete (GtkDirSelection
*filesel
,
1199 const gchar
*pattern
)
1201 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1202 g_return_if_fail (pattern
!= NULL
);
1204 if (filesel
->selection_entry
)
1205 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), pattern
);
1206 gtk_dir_selection_populate (filesel
, (gchar
*) pattern
, TRUE
, TRUE
);
1210 gtk_dir_selection_destroy (GtkObject
*object
)
1212 GtkDirSelection
*filesel
;
1214 HistoryCallbackArg
*callback_arg
;
1216 g_return_if_fail (GTK_IS_DIR_SELECTION (object
));
1218 filesel
= GTK_DIR_SELECTION (object
);
1220 if (filesel
->fileop_dialog
)
1222 gtk_widget_destroy (filesel
->fileop_dialog
);
1223 filesel
->fileop_dialog
= NULL
;
1226 if (filesel
->history_list
)
1228 list
= filesel
->history_list
;
1231 callback_arg
= list
->data
;
1232 g_free (callback_arg
->directory
);
1233 g_free (callback_arg
);
1236 g_list_free (filesel
->history_list
);
1237 filesel
->history_list
= NULL
;
1240 if (filesel
->cmpl_state
)
1242 cmpl_free_state (filesel
->cmpl_state
);
1243 filesel
->cmpl_state
= NULL
;
1246 if (filesel
->selected_names
)
1248 free_selected_names (filesel
->selected_names
);
1249 filesel
->selected_names
= NULL
;
1252 if (filesel
->last_selected
)
1254 g_free (filesel
->last_selected
);
1255 filesel
->last_selected
= NULL
;
1258 GTK_OBJECT_CLASS (parent_class
)->destroy (object
);
1262 gtk_dir_selection_map (GtkWidget
*widget
)
1264 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (widget
);
1266 /* Refresh the contents */
1267 gtk_dir_selection_populate (filesel
, "", FALSE
, FALSE
);
1269 GTK_WIDGET_CLASS (parent_class
)->map (widget
);
1273 gtk_dir_selection_finalize (GObject
*object
)
1275 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (object
);
1277 g_free (filesel
->fileop_file
);
1279 G_OBJECT_CLASS (parent_class
)->finalize (object
);
1282 /* Begin file operations callbacks */
1285 gtk_dir_selection_fileop_error (GtkDirSelection
*fs
,
1286 gchar
*error_message
)
1290 g_return_if_fail (error_message
!= NULL
);
1293 dialog
= gtk_message_dialog_new (GTK_WINDOW (fs
),
1294 GTK_DIALOG_DESTROY_WITH_PARENT
,
1297 "%s", error_message
);
1299 /* yes, we free it */
1300 g_free (error_message
);
1302 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1304 g_signal_connect_swapped (dialog
, "response",
1305 G_CALLBACK (gtk_widget_destroy
),
1308 gtk_widget_show (dialog
);
1312 gtk_dir_selection_fileop_destroy (GtkWidget
*widget
,
1315 GtkDirSelection
*fs
= data
;
1317 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1319 fs
->fileop_dialog
= NULL
;
1323 entry_is_empty (GtkEntry
*entry
)
1325 const gchar
*text
= gtk_entry_get_text (entry
);
1327 return *text
== '\0';
1331 gtk_dir_selection_fileop_entry_changed (GtkEntry
*entry
,
1334 gtk_widget_set_sensitive (button
, !entry_is_empty (entry
));
1338 gtk_dir_selection_create_dir_confirmed (GtkWidget
*widget
,
1341 GtkDirSelection
*fs
= data
;
1342 const gchar
*dirname
;
1345 gchar
*sys_full_path
;
1347 GError
*error
= NULL
;
1348 CompletionState
*cmpl_state
;
1350 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1352 dirname
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1353 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1354 path
= cmpl_reference_position (cmpl_state
);
1356 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, dirname
, NULL
);
1357 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1360 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1361 buf
= g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname
);
1363 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname
, error
->message
,
1364 _("You probably used symbols not allowed in filenames."));
1365 gtk_dir_selection_fileop_error (fs
, buf
);
1366 g_error_free (error
);
1370 if (mkdir (sys_full_path
, 0755) < 0)
1372 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname
,
1373 g_strerror (errno
));
1374 gtk_dir_selection_fileop_error (fs
, buf
);
1379 g_free (sys_full_path
);
1381 gtk_widget_destroy (fs
->fileop_dialog
);
1382 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1386 gtk_dir_selection_create_dir (GtkWidget
*widget
,
1389 GtkDirSelection
*fs
= data
;
1395 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1397 if (fs
->fileop_dialog
)
1401 dialog
= gtk_dialog_new ();
1402 fs
->fileop_dialog
= dialog
;
1403 g_signal_connect (dialog
, "destroy",
1404 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1406 gtk_window_set_title (GTK_WINDOW (dialog
), _("New Folder"));
1407 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1408 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1410 /* If file dialog is grabbed, grab option dialog */
1411 /* When option dialog is closed, file dialog will be grabbed again */
1412 if (GTK_WINDOW (fs
)->modal
)
1413 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1415 vbox
= gtk_vbox_new (FALSE
, 0);
1416 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1417 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1419 gtk_widget_show( vbox
);
1421 label
= gtk_label_new_with_mnemonic (_("_Folder name:"));
1422 gtk_misc_set_alignment(GTK_MISC (label
), 0.0, 0.0);
1423 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1424 gtk_widget_show (label
);
1426 /* The directory entry widget */
1427 fs
->fileop_entry
= gtk_entry_new ();
1428 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), fs
->fileop_entry
);
1429 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1431 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1432 gtk_widget_show (fs
->fileop_entry
);
1435 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1436 g_signal_connect_swapped (button
, "clicked",
1437 G_CALLBACK (gtk_widget_destroy
),
1439 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1440 button
, TRUE
, TRUE
, 0);
1441 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1442 gtk_widget_grab_default (button
);
1443 gtk_widget_show (button
);
1445 gtk_widget_grab_focus (fs
->fileop_entry
);
1447 button
= gtk_button_new_with_mnemonic (_("C_reate"));
1448 gtk_widget_set_sensitive (button
, FALSE
);
1449 g_signal_connect (button
, "clicked",
1450 G_CALLBACK (gtk_dir_selection_create_dir_confirmed
),
1452 g_signal_connect (fs
->fileop_entry
, "changed",
1453 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1456 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1457 button
, TRUE
, TRUE
, 0);
1458 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1459 gtk_widget_show (button
);
1461 gtk_widget_show (dialog
);
1465 gtk_dir_selection_delete_dir_response (GtkDialog
*dialog
,
1469 GtkDirSelection
*fs
= data
;
1470 CompletionState
*cmpl_state
;
1473 gchar
*sys_full_path
;
1474 GError
*error
= NULL
;
1477 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1479 if (response_id
!= GTK_RESPONSE_OK
)
1481 gtk_widget_destroy (GTK_WIDGET (dialog
));
1485 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1486 path
= cmpl_reference_position (cmpl_state
);
1488 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1489 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1492 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1493 buf
= g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1496 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1497 fs
->fileop_file
, error
->message
,
1498 _("It probably contains symbols not allowed in filenames."));
1500 gtk_dir_selection_fileop_error (fs
, buf
);
1501 g_error_free (error
);
1505 if (unlink (sys_full_path
) < 0)
1507 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s"),
1508 fs
->fileop_file
, g_strerror (errno
));
1509 gtk_dir_selection_fileop_error (fs
, buf
);
1514 g_free (sys_full_path
);
1516 gtk_widget_destroy (fs
->fileop_dialog
);
1517 gtk_dir_selection_populate (fs
, "", FALSE
, TRUE
);
1521 gtk_dir_selection_delete_file (GtkWidget
*widget
,
1524 GtkDirSelection
*fs
= data
;
1526 const gchar
*filename
;
1528 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1530 if (fs
->fileop_dialog
)
1533 #ifdef G_WITH_CYGWIN
1534 translate_win32_path (fs
);
1537 filename
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
1538 if (strlen (filename
) < 1)
1541 g_free (fs
->fileop_file
);
1542 fs
->fileop_file
= g_strdup (filename
);
1545 fs
->fileop_dialog
= dialog
=
1546 gtk_message_dialog_new (GTK_WINDOW (fs
),
1547 GTK_WINDOW (fs
)->modal
? GTK_DIALOG_MODAL
: 0,
1548 GTK_MESSAGE_QUESTION
,
1550 _("Really delete file \"%s\" ?"), filename
);
1552 g_signal_connect (dialog
, "destroy",
1553 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1555 gtk_window_set_title (GTK_WINDOW (dialog
), _("Delete File"));
1556 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1559 gtk_dialog_add_buttons (GTK_DIALOG (dialog
),
1560 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1561 GTK_STOCK_DELETE
, GTK_RESPONSE_OK
,
1564 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_CANCEL
);
1566 g_signal_connect (dialog
, "response",
1567 G_CALLBACK (gtk_dir_selection_delete_dir_response
),
1570 gtk_widget_show (dialog
);
1574 gtk_dir_selection_rename_dir_confirmed (GtkWidget
*widget
,
1577 GtkDirSelection
*fs
= data
;
1581 gchar
*new_filename
;
1582 gchar
*old_filename
;
1583 gchar
*sys_new_filename
;
1584 gchar
*sys_old_filename
;
1585 CompletionState
*cmpl_state
;
1586 GError
*error
= NULL
;
1588 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1590 file
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1591 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1592 path
= cmpl_reference_position (cmpl_state
);
1594 new_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, file
, NULL
);
1595 old_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1597 sys_new_filename
= g_filename_from_utf8 (new_filename
, -1, NULL
, NULL
, &error
);
1600 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1601 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename
);
1603 buf
= g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1604 new_filename
, error
->message
,
1605 _("You probably used symbols not allowed in filenames."));
1606 gtk_dir_selection_fileop_error (fs
, buf
);
1607 g_error_free (error
);
1611 sys_old_filename
= g_filename_from_utf8 (old_filename
, -1, NULL
, NULL
, &error
);
1614 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1615 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename
);
1617 buf
= g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1618 old_filename
, error
->message
,
1619 _("It probably contains symbols not allowed in filenames."));
1620 gtk_dir_selection_fileop_error (fs
, buf
);
1621 g_error_free (error
);
1625 if (rename (sys_old_filename
, sys_new_filename
) < 0)
1627 buf
= g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1628 sys_old_filename
, sys_new_filename
,
1629 g_strerror (errno
));
1630 gtk_dir_selection_fileop_error (fs
, buf
);
1634 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1635 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), file
);
1638 g_free (sys_old_filename
);
1641 g_free (new_filename
);
1642 g_free (old_filename
);
1643 g_free (sys_new_filename
);
1645 gtk_widget_destroy (fs
->fileop_dialog
);
1649 gtk_dir_selection_rename_file (GtkWidget
*widget
,
1652 GtkDirSelection
*fs
= data
;
1659 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1661 if (fs
->fileop_dialog
)
1664 g_free (fs
->fileop_file
);
1665 fs
->fileop_file
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1666 if (strlen (fs
->fileop_file
) < 1)
1670 fs
->fileop_dialog
= dialog
= gtk_dialog_new ();
1671 g_signal_connect (dialog
, "destroy",
1672 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1674 gtk_window_set_title (GTK_WINDOW (dialog
), _("Rename File"));
1675 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1676 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1678 /* If file dialog is grabbed, grab option dialog */
1679 /* When option dialog closed, file dialog will be grabbed again */
1680 if (GTK_WINDOW (fs
)->modal
)
1681 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1683 vbox
= gtk_vbox_new (FALSE
, 0);
1684 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1685 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1687 gtk_widget_show(vbox
);
1689 buf
= g_strdup_printf (_("Rename file \"%s\" to:"), fs
->fileop_file
);
1690 label
= gtk_label_new (buf
);
1691 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.0);
1692 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1693 gtk_widget_show (label
);
1696 /* New filename entry */
1697 fs
->fileop_entry
= gtk_entry_new ();
1698 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1700 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1701 gtk_widget_show (fs
->fileop_entry
);
1703 gtk_entry_set_text (GTK_ENTRY (fs
->fileop_entry
), fs
->fileop_file
);
1704 gtk_editable_select_region (GTK_EDITABLE (fs
->fileop_entry
),
1705 0, strlen (fs
->fileop_file
));
1708 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1709 g_signal_connect_swapped (button
, "clicked",
1710 G_CALLBACK (gtk_widget_destroy
),
1712 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1713 button
, TRUE
, TRUE
, 0);
1714 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1715 gtk_widget_grab_default (button
);
1716 gtk_widget_show (button
);
1718 gtk_widget_grab_focus (fs
->fileop_entry
);
1720 button
= gtk_button_new_with_mnemonic (_("_Rename"));
1721 g_signal_connect (button
, "clicked",
1722 G_CALLBACK (gtk_dir_selection_rename_dir_confirmed
),
1724 g_signal_connect (fs
->fileop_entry
, "changed",
1725 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1728 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1729 button
, TRUE
, TRUE
, 0);
1730 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1731 gtk_widget_show (button
);
1733 gtk_widget_show (dialog
);
1737 gtk_dir_selection_insert_text (GtkWidget
*widget
,
1738 const gchar
*new_text
,
1739 gint new_text_length
,
1745 filename
= g_filename_from_utf8 (new_text
, new_text_length
, NULL
, NULL
, NULL
);
1749 gdk_display_beep (gtk_widget_get_display (widget
));
1750 g_signal_stop_emission_by_name (widget
, "insert_text");
1760 gtk_dir_selection_update_fileops (GtkDirSelection
*fs
)
1764 if (!fs
->selection_entry
)
1767 sensitive
= !entry_is_empty (GTK_ENTRY (fs
->selection_entry
));
1769 if (fs
->fileop_del_file
)
1770 gtk_widget_set_sensitive (fs
->fileop_del_file
, sensitive
);
1772 if (fs
->fileop_ren_file
)
1773 gtk_widget_set_sensitive (fs
->fileop_ren_file
, sensitive
);
1777 gtk_dir_selection_key_press (GtkWidget
*widget
,
1781 GtkDirSelection
*fs
;
1784 g_return_val_if_fail (widget
!= NULL
, FALSE
);
1785 g_return_val_if_fail (event
!= NULL
, FALSE
);
1787 if ((event
->keyval
== GDK_Tab
|| event
->keyval
== GDK_KP_Tab
) &&
1788 (event
->state
& gtk_accelerator_get_default_mod_mask ()) == 0)
1790 fs
= GTK_DIR_SELECTION (user_data
);
1791 #ifdef G_WITH_CYGWIN
1792 translate_win32_path (fs
);
1794 text
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1796 gtk_dir_selection_populate (fs
, text
, TRUE
, TRUE
);
1807 gtk_dir_selection_history_callback (GtkWidget
*widget
,
1810 GtkDirSelection
*fs
= data
;
1811 HistoryCallbackArg
*callback_arg
;
1814 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1816 list
= fs
->history_list
;
1819 callback_arg
= list
->data
;
1821 if (callback_arg
->menu_item
== widget
)
1823 gtk_dir_selection_populate (fs
, callback_arg
->directory
, FALSE
, FALSE
);
1832 gtk_dir_selection_update_history_menu (GtkDirSelection
*fs
,
1833 gchar
*current_directory
)
1835 HistoryCallbackArg
*callback_arg
;
1836 GtkWidget
*menu_item
;
1842 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1843 g_return_if_fail (current_directory
!= NULL
);
1845 list
= fs
->history_list
;
1847 if (fs
->history_menu
)
1850 callback_arg
= list
->data
;
1851 g_free (callback_arg
->directory
);
1852 g_free (callback_arg
);
1855 g_list_free (fs
->history_list
);
1856 fs
->history_list
= NULL
;
1858 gtk_widget_destroy (fs
->history_menu
);
1861 fs
->history_menu
= gtk_menu_new ();
1863 current_dir
= g_strdup (current_directory
);
1865 dir_len
= strlen (current_dir
);
1867 for (i
= dir_len
; i
>= 0; i
--)
1869 /* the i == dir_len is to catch the full path for the first
1871 if ( (current_dir
[i
] == G_DIR_SEPARATOR
) || (i
== dir_len
))
1873 /* another small hack to catch the full path */
1875 current_dir
[i
+ 1] = '\0';
1876 #ifdef G_WITH_CYGWIN
1877 if (!strcmp (current_dir
, "//"))
1880 menu_item
= gtk_menu_item_new_with_label (current_dir
);
1882 callback_arg
= g_new (HistoryCallbackArg
, 1);
1883 callback_arg
->menu_item
= menu_item
;
1885 /* since the autocompletion gets confused if you don't
1886 * supply a trailing '/' on a dir entry, set the full
1887 * (current) path to "" which just refreshes the filesel */
1890 callback_arg
->directory
= g_strdup ("");
1894 callback_arg
->directory
= g_strdup (current_dir
);
1897 fs
->history_list
= g_list_append (fs
->history_list
, callback_arg
);
1899 g_signal_connect (menu_item
, "activate",
1900 G_CALLBACK (gtk_dir_selection_history_callback
),
1902 gtk_menu_shell_append (GTK_MENU_SHELL (fs
->history_menu
), menu_item
);
1903 gtk_widget_show (menu_item
);
1907 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs
->history_pulldown
),
1909 g_free (current_dir
);
1913 get_real_filename (gchar
*filename
,
1916 #ifdef G_WITH_CYGWIN
1917 /* Check to see if the selection was a drive selector */
1918 if (isalpha (filename
[0]) && (filename
[1] == ':'))
1920 gchar temp_filename
[PATH_MAX
];
1923 cygwin_conv_to_posix_path (filename
, temp_filename
);
1925 /* we need trailing '/'. */
1926 len
= strlen (temp_filename
);
1927 if (len
> 0 && temp_filename
[len
-1] != '/')
1929 temp_filename
[len
] = '/';
1930 temp_filename
[len
+1] = '\0';
1936 return g_strdup (temp_filename
);
1938 #endif /* G_WITH_CYGWIN */
1943 gtk_dir_selection_file_activate (GtkTreeView
*tree_view
,
1945 GtkTreeViewColumn
*column
,
1948 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1949 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1953 gtk_tree_model_get_iter (model
, &iter
, path
);
1954 gtk_tree_model_get (model
, &iter
, FILE_COLUMN
, &filename
, -1);
1955 filename
= get_real_filename (filename
, TRUE
);
1956 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
1957 gtk_button_clicked (GTK_BUTTON (fs
->ok_button
));
1963 gtk_dir_selection_dir_activate (GtkTreeView
*tree_view
,
1965 GtkTreeViewColumn
*column
,
1968 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1969 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1973 gtk_tree_model_get_iter (model
, &iter
, path
);
1974 gtk_tree_model_get (model
, &iter
, DIR_COLUMN
, &filename
, -1);
1975 filename
= get_real_filename (filename
, TRUE
);
1976 gtk_dir_selection_populate (fs
, filename
, FALSE
, FALSE
);
1980 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1983 win32_gtk_add_drives_to_dir_list (GtkListStore
*model
)
1987 char formatBuffer
[128];
1990 /* Get the drives string */
1991 GetLogicalDriveStrings (sizeof (buffer
), buffer
);
1993 /* Add the drives as necessary */
1995 while (*textPtr
!= '\0')
1997 /* Ignore floppies (?) */
1998 if ((tolower (textPtr
[0]) != 'a') && (tolower (textPtr
[0]) != 'b'))
2000 /* Build the actual displayable string */
2001 g_snprintf (formatBuffer
, sizeof (formatBuffer
), "%c:\\", toupper (textPtr
[0]));
2003 /* Add to the list */
2004 gtk_list_store_append (model
, &iter
);
2005 gtk_list_store_set (model
, &iter
, DIR_COLUMN
, formatBuffer
, -1);
2007 textPtr
+= (strlen (textPtr
) + 1);
2013 escape_underscores (const gchar
*str
)
2015 GString
*result
= g_string_new (NULL
);
2019 g_string_append_c (result
, '_');
2021 g_string_append_c (result
, *str
);
2025 return g_string_free (result
, FALSE
);
2029 gtk_dir_selection_populate (GtkDirSelection
*fs
,
2031 gboolean try_complete
,
2032 gboolean reset_entry
)
2034 CompletionState
*cmpl_state
;
2035 PossibleCompletion
* poss
;
2037 GtkListStore
*dir_model
;
2038 GtkListStore
*file_model
;
2040 gchar
* rem_path
= rel_path
;
2042 gint did_recurse
= FALSE
;
2043 gint possible_count
= 0;
2044 gint selection_index
= -1;
2046 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
2048 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
2049 poss
= cmpl_completion_matches (rel_path
, &rem_path
, cmpl_state
);
2051 if (!cmpl_state_okay (cmpl_state
))
2053 /* Something went wrong. */
2054 gtk_dir_selection_abort (fs
);
2058 g_assert (cmpl_state
->reference_dir
);
2060 dir_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->dir_list
)));
2061 file_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->file_list
)));
2063 gtk_list_store_clear (dir_model
);
2064 gtk_list_store_clear (file_model
);
2066 /* Set the dir list to include ./ and ../ */
2067 gtk_list_store_append (dir_model
, &iter
);
2068 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, "." G_DIR_SEPARATOR_S
, -1);
2069 gtk_list_store_append (dir_model
, &iter
);
2070 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, ".." G_DIR_SEPARATOR_S
, -1);
2074 if (cmpl_is_a_completion (poss
))
2076 possible_count
+= 1;
2078 filename
= cmpl_this_completion (poss
);
2080 if (cmpl_is_directory (poss
))
2082 if (strcmp (filename
, "." G_DIR_SEPARATOR_S
) != 0 &&
2083 strcmp (filename
, ".." G_DIR_SEPARATOR_S
) != 0)
2085 gtk_list_store_append (dir_model
, &iter
);
2086 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, filename
, -1);
2091 gtk_list_store_append (file_model
, &iter
);
2092 gtk_list_store_set (file_model
, &iter
, DIR_COLUMN
, filename
, -1);
2096 poss
= cmpl_next_completion (cmpl_state
);
2099 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2100 /* For Windows, add drives as potential selections */
2101 win32_gtk_add_drives_to_dir_list (dir_model
);
2104 /* File lists are set. */
2106 g_assert (cmpl_state
->reference_dir
);
2111 /* User is trying to complete filenames, so advance the user's input
2112 * string to the updated_text, which is the common leading substring
2113 * of all possible completions, and if its a directory attempt
2114 * attempt completions in it. */
2116 if (cmpl_updated_text (cmpl_state
)[0])
2119 if (cmpl_updated_dir (cmpl_state
))
2121 gchar
* dir_name
= g_strdup (cmpl_updated_text (cmpl_state
));
2125 gtk_dir_selection_populate (fs
, dir_name
, TRUE
, TRUE
);
2131 if (fs
->selection_entry
)
2132 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
),
2133 cmpl_updated_text (cmpl_state
));
2138 selection_index
= cmpl_last_valid_char (cmpl_state
) -
2139 (strlen (rel_path
) - strlen (rem_path
));
2140 if (fs
->selection_entry
)
2141 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), rem_path
);
2144 else if (reset_entry
)
2146 if (fs
->selection_entry
)
2147 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2152 if (fs
->selection_entry
)
2153 gtk_editable_set_position (GTK_EDITABLE (fs
->selection_entry
),
2156 if (fs
->selection_entry
)
2158 char *escaped
= escape_underscores (cmpl_reference_position (cmpl_state
));
2159 sel_text
= g_strconcat (_("_Selection: "), escaped
, NULL
);
2162 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs
->selection_text
), sel_text
);
2163 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), cmpl_reference_position (cmpl_state
));
2167 if (fs
->history_pulldown
)
2169 gtk_dir_selection_update_history_menu (fs
, cmpl_reference_position (cmpl_state
));
2176 gtk_dir_selection_abort (GtkDirSelection
*fs
)
2180 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
2182 /* BEEP gdk_beep(); */
2184 if (fs
->selection_entry
)
2185 gtk_label_set_text (GTK_LABEL (fs
->selection_text
), err_buf
);
2189 * gtk_dir_selection_set_select_multiple:
2190 * @filesel: a #GtkDirSelection
2191 * @select_multiple: whether or not the user is allowed to select multiple
2192 * files in the file list.
2194 * Sets whether the user is allowed to select multiple files in the file list.
2195 * Use gtk_dir_selection_get_selections () to get the list of selected files.
2198 gtk_dir_selection_set_select_multiple (GtkDirSelection
*filesel
,
2199 gboolean select_multiple
)
2201 GtkTreeSelection
*sel
;
2202 GtkSelectionMode mode
;
2204 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
2206 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2208 mode
= select_multiple
? GTK_SELECTION_MULTIPLE
: GTK_SELECTION_SINGLE
;
2210 if (mode
!= gtk_tree_selection_get_mode (sel
))
2212 gtk_tree_selection_set_mode (sel
, mode
);
2214 g_object_notify (G_OBJECT (filesel
), "select-multiple");
2219 * gtk_dir_selection_get_select_multiple:
2220 * @filesel: a #GtkDirSelection
2222 * Determines whether or not the user is allowed to select multiple files in
2223 * the file list. See gtk_dir_selection_set_select_multiple().
2225 * Return value: %TRUE if the user is allowed to select multiple files in the
2229 gtk_dir_selection_get_select_multiple (GtkDirSelection
*filesel
)
2231 GtkTreeSelection
*sel
;
2233 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), FALSE
);
2235 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2236 return (gtk_tree_selection_get_mode (sel
) == GTK_SELECTION_MULTIPLE
);
2240 multiple_changed_foreach (GtkTreeModel
*model
,
2245 GPtrArray
*names
= data
;
2248 gtk_tree_model_get (model
, iter
, FILE_COLUMN
, &filename
, -1);
2250 g_ptr_array_add (names
, filename
);
2254 free_selected_names (GPtrArray
*names
)
2258 for (i
= 0; i
< names
->len
; i
++)
2259 g_free (g_ptr_array_index (names
, i
));
2261 g_ptr_array_free (names
, TRUE
);
2265 gtk_dir_selection_file_changed (GtkTreeSelection
*selection
,
2268 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2269 GPtrArray
*new_names
;
2274 new_names
= g_ptr_array_sized_new (8);
2276 gtk_tree_selection_selected_foreach (selection
,
2277 multiple_changed_foreach
,
2280 /* nothing selected */
2281 if (new_names
->len
== 0)
2283 g_ptr_array_free (new_names
, TRUE
);
2285 if (fs
->selected_names
!= NULL
)
2287 free_selected_names (fs
->selected_names
);
2288 fs
->selected_names
= NULL
;
2291 goto maybe_clear_entry
;
2294 if (new_names
->len
!= 1)
2296 GPtrArray
*old_names
= fs
->selected_names
;
2298 if (old_names
!= NULL
)
2300 /* A common case is selecting a range of files from top to bottom,
2301 * so quickly check for that to avoid looping over the entire list
2303 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2304 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2305 index
= new_names
->len
- 1;
2308 gint i
= 0, j
= 0, cmp
;
2310 /* do a quick diff, stopping at the first file not in the
2313 while (i
< old_names
->len
&& j
< new_names
->len
)
2315 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2316 g_ptr_array_index (new_names
, j
));
2333 /* we ran off the end of the old list */
2334 if (index
== -1 && i
< new_names
->len
)
2340 /* A phantom anchor still exists at the point where the last item
2341 * was selected, which is used for subsequent range selections.
2342 * So search up from there.
2344 if (fs
->last_selected
&&
2345 compare_filenames (fs
->last_selected
,
2346 g_ptr_array_index (new_names
, 0)) == 0)
2347 index
= new_names
->len
- 1;
2355 if (fs
->selected_names
!= NULL
)
2356 free_selected_names (fs
->selected_names
);
2358 fs
->selected_names
= new_names
;
2362 if (fs
->last_selected
!= NULL
)
2363 g_free (fs
->last_selected
);
2365 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2366 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2368 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
2370 if (filename
!= fs
->last_selected
)
2378 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2379 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2380 (compare_filenames (entry
, fs
->last_selected
) == 0))
2381 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2385 gtk_dir_selection_dir_changed (GtkTreeSelection
*selection
,
2388 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2389 GPtrArray
*new_names
;
2394 new_names
= g_ptr_array_sized_new (8);
2396 gtk_tree_selection_selected_foreach (selection
,
2397 multiple_changed_foreach
,
2400 /* nothing selected */
2401 if (new_names
->len
== 0)
2403 g_ptr_array_free (new_names
, TRUE
);
2405 if (fs
->selected_names
!= NULL
)
2407 free_selected_names (fs
->selected_names
);
2408 fs
->selected_names
= NULL
;
2411 goto maybe_clear_entry
;
2414 if (new_names
->len
!= 1)
2416 GPtrArray
*old_names
= fs
->selected_names
;
2418 if (old_names
!= NULL
)
2420 /* A common case is selecting a range of files from top to bottom,
2421 * so quickly check for that to avoid looping over the entire list
2423 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2424 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2425 index
= new_names
->len
- 1;
2428 gint i
= 0, j
= 0, cmp
;
2430 /* do a quick diff, stopping at the first file not in the
2433 while (i
< old_names
->len
&& j
< new_names
->len
)
2435 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2436 g_ptr_array_index (new_names
, j
));
2453 /* we ran off the end of the old list */
2454 if (index
== -1 && i
< new_names
->len
)
2460 /* A phantom anchor still exists at the point where the last item
2461 * was selected, which is used for subsequent range selections.
2462 * So search up from there.
2464 if (fs
->last_selected
&&
2465 compare_filenames (fs
->last_selected
,
2466 g_ptr_array_index (new_names
, 0)) == 0)
2467 index
= new_names
->len
- 1;
2475 if (fs
->selected_names
!= NULL
)
2476 free_selected_names (fs
->selected_names
);
2478 fs
->selected_names
= new_names
;
2484 err
= gtk_label_get_text (GTK_LABEL (fs
->selection_text
));
2485 err
+= 11; //pass over "Selection: "
2486 sprintf(str
,"%s\0",err
);
2489 if (fs
->last_selected
!= NULL
)
2490 g_free (fs
->last_selected
);
2492 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2493 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2496 strcat(str
,filename
);
2497 str
[strlen(str
)-1] = '\0';
2499 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), str
);
2501 if (filename
!= fs
->last_selected
)
2509 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2510 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2511 (compare_filenames (entry
, fs
->last_selected
) == 0))
2512 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2516 * gtk_dir_selection_get_selections:
2517 * @filesel: a #GtkDirSelection
2519 * Retrieves the list of file selections the user has made in the dialog box.
2520 * This function is intended for use when the user can select multiple files
2521 * in the file list. The first file in the list is equivalent to what
2522 * gtk_dir_selection_get_filename() would return.
2524 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2525 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2526 * g_filename_to_utf8() on each string.
2528 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2529 * g_strfreev() to free it.
2532 gtk_dir_selection_get_selections (GtkDirSelection
*filesel
)
2536 gchar
*filename
, *dirname
;
2537 gchar
*current
, *buf
;
2539 gboolean unselected_entry
;
2541 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), NULL
);
2543 filename
= g_strdup (gtk_dir_selection_get_filename (filesel
));
2545 if (strlen (filename
) == 0)
2551 names
= filesel
->selected_names
;
2554 selections
= g_new (gchar
*, names
->len
+ 2);
2556 selections
= g_new (gchar
*, 2);
2559 unselected_entry
= TRUE
;
2563 dirname
= g_path_get_dirname (filename
);
2565 for (i
= 0; i
< names
->len
; i
++)
2567 buf
= g_filename_from_utf8 (g_ptr_array_index (names
, i
), -1,
2569 current
= g_build_filename (dirname
, buf
, NULL
);
2572 selections
[count
++] = current
;
2574 if (unselected_entry
&& compare_filenames (current
, filename
) == 0)
2575 unselected_entry
= FALSE
;
2581 if (unselected_entry
)
2582 selections
[count
++] = filename
;
2586 selections
[count
] = NULL
;
2591 /**********************************************************************/
2592 /* External Interface */
2593 /**********************************************************************/
2595 /* The four completion state selectors
2598 cmpl_updated_text (CompletionState
*cmpl_state
)
2600 return cmpl_state
->updated_text
;
2604 cmpl_updated_dir (CompletionState
*cmpl_state
)
2606 return cmpl_state
->re_complete
;
2610 cmpl_reference_position (CompletionState
*cmpl_state
)
2612 return cmpl_state
->reference_dir
->fullname
;
2616 cmpl_last_valid_char (CompletionState
*cmpl_state
)
2618 return cmpl_state
->last_valid_char
;
2622 cmpl_completion_fullname (const gchar
*text
,
2623 CompletionState
*cmpl_state
)
2625 static const char nothing
[2] = "";
2627 if (!cmpl_state_okay (cmpl_state
))
2631 else if (g_path_is_absolute (text
))
2633 strcpy (cmpl_state
->updated_text
, text
);
2636 else if (text
[0] == '~')
2641 dir
= open_user_dir (text
, cmpl_state
);
2645 /* spencer says just return ~something, so
2646 * for now just do it. */
2647 strcpy (cmpl_state
->updated_text
, text
);
2652 strcpy (cmpl_state
->updated_text
, dir
->fullname
);
2654 slash
= strchr (text
, G_DIR_SEPARATOR
);
2657 strcat (cmpl_state
->updated_text
, slash
);
2663 strcpy (cmpl_state
->updated_text
, cmpl_state
->reference_dir
->fullname
);
2664 if (cmpl_state
->updated_text
[strlen (cmpl_state
->updated_text
) - 1] != G_DIR_SEPARATOR
)
2665 strcat (cmpl_state
->updated_text
, G_DIR_SEPARATOR_S
);
2666 strcat (cmpl_state
->updated_text
, text
);
2669 return cmpl_state
->updated_text
;
2672 /* The three completion selectors
2675 cmpl_this_completion (PossibleCompletion
* pc
)
2681 cmpl_is_directory (PossibleCompletion
* pc
)
2683 return pc
->is_directory
;
2687 cmpl_is_a_completion (PossibleCompletion
* pc
)
2689 return pc
->is_a_completion
;
2692 /**********************************************************************/
2693 /* Construction, deletion */
2694 /**********************************************************************/
2696 static CompletionState
*
2697 cmpl_init_state (void)
2699 gchar
*sys_getcwd_buf
;
2701 CompletionState
*new_state
;
2703 new_state
= g_new (CompletionState
, 1);
2705 /* g_get_current_dir() returns a string in the "system" charset */
2706 sys_getcwd_buf
= g_get_current_dir ();
2707 utf8_cwd
= g_filename_to_utf8 (sys_getcwd_buf
, -1, NULL
, NULL
, NULL
);
2708 g_free (sys_getcwd_buf
);
2712 new_state
->reference_dir
= NULL
;
2713 new_state
->completion_dir
= NULL
;
2714 new_state
->active_completion_dir
= NULL
;
2715 new_state
->directory_storage
= NULL
;
2716 new_state
->directory_sent_storage
= NULL
;
2717 new_state
->last_valid_char
= 0;
2718 new_state
->updated_text
= g_new (gchar
, MAXPATHLEN
);
2719 new_state
->updated_text_alloc
= MAXPATHLEN
;
2720 new_state
->the_completion
.text
= g_new (gchar
, MAXPATHLEN
);
2721 new_state
->the_completion
.text_alloc
= MAXPATHLEN
;
2722 new_state
->user_dir_name_buffer
= NULL
;
2723 new_state
->user_directories
= NULL
;
2725 new_state
->reference_dir
= open_dir (utf8_cwd
, new_state
);
2727 if (!new_state
->reference_dir
)
2729 /* Directories changing from underneath us, grumble */
2730 strcpy (utf8_cwd
, G_DIR_SEPARATOR_S
);
2739 cmpl_free_dir_list (GList
* dp0
)
2745 free_dir (dp
->data
);
2753 cmpl_free_dir_sent_list (GList
* dp0
)
2759 free_dir_sent (dp
->data
);
2767 cmpl_free_state (CompletionState
* cmpl_state
)
2769 g_return_if_fail (cmpl_state
!= NULL
);
2771 cmpl_free_dir_list (cmpl_state
->directory_storage
);
2772 cmpl_free_dir_sent_list (cmpl_state
->directory_sent_storage
);
2774 if (cmpl_state
->user_dir_name_buffer
)
2775 g_free (cmpl_state
->user_dir_name_buffer
);
2776 if (cmpl_state
->user_directories
)
2777 g_free (cmpl_state
->user_directories
);
2778 if (cmpl_state
->the_completion
.text
)
2779 g_free (cmpl_state
->the_completion
.text
);
2780 if (cmpl_state
->updated_text
)
2781 g_free (cmpl_state
->updated_text
);
2783 g_free (cmpl_state
);
2787 free_dir (CompletionDir
* dir
)
2789 g_free (dir
->cmpl_text
);
2790 g_free (dir
->fullname
);
2795 free_dir_sent (CompletionDirSent
* sent
)
2798 for (i
= 0; i
< sent
->entry_count
; i
++)
2800 g_free (sent
->entries
[i
].entry_name
);
2801 g_free (sent
->entries
[i
].sort_key
);
2803 g_free (sent
->entries
);
2808 prune_memory_usage (CompletionState
*cmpl_state
)
2810 GList
* cdsl
= cmpl_state
->directory_sent_storage
;
2811 GList
* cdl
= cmpl_state
->directory_storage
;
2815 for (; cdsl
&& len
< CMPL_DIRECTORY_CACHE_SIZE
; len
+= 1)
2820 cmpl_free_dir_sent_list (cdsl
->next
);
2824 cmpl_state
->directory_storage
= NULL
;
2827 if (cdl
->data
== cmpl_state
->reference_dir
)
2828 cmpl_state
->directory_storage
= g_list_prepend (NULL
, cdl
->data
);
2830 free_dir (cdl
->data
);
2837 /**********************************************************************/
2838 /* The main entrances. */
2839 /**********************************************************************/
2841 static PossibleCompletion
*
2842 cmpl_completion_matches (gchar
*text_to_complete
,
2843 gchar
**remaining_text
,
2844 CompletionState
*cmpl_state
)
2847 PossibleCompletion
*poss
;
2849 prune_memory_usage (cmpl_state
);
2851 g_assert (text_to_complete
!= NULL
);
2853 cmpl_state
->user_completion_index
= -1;
2854 cmpl_state
->last_completion_text
= text_to_complete
;
2855 cmpl_state
->the_completion
.text
[0] = 0;
2856 cmpl_state
->last_valid_char
= 0;
2857 cmpl_state
->updated_text_len
= -1;
2858 cmpl_state
->updated_text
[0] = 0;
2859 cmpl_state
->re_complete
= FALSE
;
2862 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2864 if (text_to_complete
[0] == '~' && !first_slash
)
2866 /* Text starts with ~ and there is no slash, show all the
2867 * home directory completions.
2869 poss
= attempt_homedir_completion (text_to_complete
, cmpl_state
);
2871 update_cmpl (poss
, cmpl_state
);
2876 cmpl_state
->reference_dir
=
2877 open_ref_dir (text_to_complete
, remaining_text
, cmpl_state
);
2879 if (!cmpl_state
->reference_dir
)
2882 cmpl_state
->completion_dir
=
2883 find_completion_dir (*remaining_text
, remaining_text
, cmpl_state
);
2885 cmpl_state
->last_valid_char
= *remaining_text
- text_to_complete
;
2887 if (!cmpl_state
->completion_dir
)
2890 cmpl_state
->completion_dir
->cmpl_index
= -1;
2891 cmpl_state
->completion_dir
->cmpl_parent
= NULL
;
2892 cmpl_state
->completion_dir
->cmpl_text
= g_strdup (*remaining_text
);
2894 cmpl_state
->active_completion_dir
= cmpl_state
->completion_dir
;
2896 cmpl_state
->reference_dir
= cmpl_state
->completion_dir
;
2898 poss
= attempt_dir_completion (cmpl_state
);
2900 update_cmpl (poss
, cmpl_state
);
2905 static PossibleCompletion
*
2906 cmpl_next_completion (CompletionState
* cmpl_state
)
2908 PossibleCompletion
* poss
= NULL
;
2910 cmpl_state
->the_completion
.text
[0] = 0;
2913 if (cmpl_state
->user_completion_index
>= 0)
2914 poss
= attempt_homedir_completion (cmpl_state
->last_completion_text
, cmpl_state
);
2916 poss
= attempt_dir_completion (cmpl_state
);
2918 poss
= attempt_dir_completion (cmpl_state
);
2921 update_cmpl (poss
, cmpl_state
);
2926 /**********************************************************************/
2927 /* Directory Operations */
2928 /**********************************************************************/
2930 /* Open the directory where completion will begin from, if possible. */
2931 static CompletionDir
*
2932 open_ref_dir (gchar
*text_to_complete
,
2933 gchar
**remaining_text
,
2934 CompletionState
*cmpl_state
)
2937 CompletionDir
*new_dir
;
2939 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2941 #ifdef G_WITH_CYGWIN
2942 if (text_to_complete
[0] == '/' && text_to_complete
[1] == '/')
2945 g_snprintf (root_dir
, sizeof (root_dir
), "//%c", text_to_complete
[2]);
2947 new_dir
= open_dir (root_dir
, cmpl_state
);
2950 *remaining_text
= text_to_complete
+ 4;
2958 else if (text_to_complete
[0] == '~')
2960 new_dir
= open_user_dir (text_to_complete
, cmpl_state
);
2965 *remaining_text
= first_slash
+ 1;
2967 *remaining_text
= text_to_complete
+ strlen (text_to_complete
);
2975 else if (g_path_is_absolute (text_to_complete
) || !cmpl_state
->reference_dir
)
2977 gchar
*tmp
= g_strdup (text_to_complete
);
2981 while (*p
&& *p
!= '*' && *p
!= '?')
2985 p
= strrchr (tmp
, G_DIR_SEPARATOR
);
2993 new_dir
= open_dir (tmp
, cmpl_state
);
2996 *remaining_text
= text_to_complete
+
2997 ((p
== tmp
+ 1) ? (p
- tmp
) : (p
+ 1 - tmp
));
3001 /* If no possible candidates, use the cwd */
3002 gchar
*sys_curdir
= g_get_current_dir ();
3003 gchar
*utf8_curdir
= g_filename_to_utf8 (sys_curdir
, -1, NULL
, NULL
, NULL
);
3005 g_free (sys_curdir
);
3007 new_dir
= open_dir (utf8_curdir
, cmpl_state
);
3010 *remaining_text
= text_to_complete
;
3012 g_free (utf8_curdir
);
3019 *remaining_text
= text_to_complete
;
3021 new_dir
= open_dir (cmpl_state
->reference_dir
->fullname
, cmpl_state
);
3026 new_dir
->cmpl_index
= -1;
3027 new_dir
->cmpl_parent
= NULL
;
3035 /* open a directory by user name */
3036 static CompletionDir
*
3037 open_user_dir (const gchar
*text_to_complete
,
3038 CompletionState
*cmpl_state
)
3040 CompletionDir
*result
;
3044 g_assert (text_to_complete
&& text_to_complete
[0] == '~');
3046 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
3049 cmp_len
= first_slash
- text_to_complete
- 1;
3051 cmp_len
= strlen (text_to_complete
+ 1);
3056 const gchar
*homedir
= g_get_home_dir ();
3057 gchar
*utf8_homedir
= g_filename_to_utf8 (homedir
, -1, NULL
, NULL
, NULL
);
3060 result
= open_dir (utf8_homedir
, cmpl_state
);
3064 g_free (utf8_homedir
);
3069 gchar
* copy
= g_new (char, cmp_len
+ 1);
3073 strncpy (copy
, text_to_complete
+ 1, cmp_len
);
3075 pwd
= getpwnam (copy
);
3082 utf8_dir
= g_filename_to_utf8 (pwd
->pw_dir
, -1, NULL
, NULL
, NULL
);
3083 result
= open_dir (utf8_dir
, cmpl_state
);
3091 /* open a directory relative the the current relative directory */
3092 static CompletionDir
*
3093 open_relative_dir (gchar
*dir_name
,
3095 CompletionState
*cmpl_state
)
3097 CompletionDir
*result
;
3100 path
= g_string_sized_new (dir
->fullname_len
+ strlen (dir_name
) + 10);
3101 g_string_assign (path
, dir
->fullname
);
3103 if (dir
->fullname_len
> 1
3104 && path
->str
[dir
->fullname_len
- 1] != G_DIR_SEPARATOR
)
3105 g_string_append_c (path
, G_DIR_SEPARATOR
);
3106 g_string_append (path
, dir_name
);
3108 result
= open_dir (path
->str
, cmpl_state
);
3110 g_string_free (path
, TRUE
);
3115 /* after the cache lookup fails, really open a new directory */
3116 static CompletionDirSent
*
3117 open_new_dir (gchar
*dir_name
,
3119 gboolean stat_subdirs
)
3121 CompletionDirSent
*sent
;
3124 GError
*error
= NULL
;
3125 gint entry_count
= 0;
3128 struct stat ent_sbuf
;
3130 gchar
*sys_dir_name
;
3132 sent
= g_new (CompletionDirSent
, 1);
3133 sent
->mtime
= sbuf
->st_mtime
;
3134 sent
->inode
= sbuf
->st_ino
;
3135 sent
->device
= sbuf
->st_dev
;
3137 path
= g_string_sized_new (2*MAXPATHLEN
+ 10);
3139 sys_dir_name
= g_filename_from_utf8 (dir_name
, -1, NULL
, NULL
, NULL
);
3142 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3146 directory
= g_dir_open (sys_dir_name
, 0, &error
);
3149 cmpl_errno
= error
->code
; /* ??? */
3150 g_free (sys_dir_name
);
3154 while ((dirent
= g_dir_read_name (directory
)) != NULL
)
3157 entry_count
+= 2; /* For ".",".." */
3159 sent
->entries
= g_new (CompletionDirEntry
, entry_count
);
3160 sent
->entry_count
= entry_count
;
3162 g_dir_rewind (directory
);
3164 for (i
= 0; i
< entry_count
; i
+= 1)
3166 GError
*error
= NULL
;
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
))
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
, dir
->sent
->entries
[dir
->cmpl_index
].entry_name
))
3885 CompletionDir
* new_dir
;
3887 new_dir
= open_relative_dir (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
,
3896 new_dir
->cmpl_parent
= dir
;
3898 new_dir
->cmpl_index
= -1;
3899 new_dir
->cmpl_text
= g_strdup (first_slash
+ 1);
3901 cmpl_state
->active_completion_dir
= new_dir
;
3904 return attempt_dir_completion (cmpl_state
);
3909 return attempt_dir_completion (cmpl_state
);
3915 return attempt_dir_completion (cmpl_state
);
3920 if (dir
->cmpl_parent
!= NULL
)
3922 append_completion_text (dir
->fullname
+
3923 strlen (cmpl_state
->completion_dir
->fullname
) + 1,
3925 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3928 append_completion_text (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
, cmpl_state
);
3930 cmpl_state
->the_completion
.is_a_completion
=
3931 _gtk_fnmatch (pat_buf
, dir
->sent
->entries
[dir
->cmpl_index
].entry_name
);
3933 cmpl_state
->the_completion
.is_directory
= dir
->sent
->entries
[dir
->cmpl_index
].is_dir
;
3934 if (dir
->sent
->entries
[dir
->cmpl_index
].is_dir
)
3935 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3938 return &cmpl_state
->the_completion
;
3945 get_pwdb (CompletionState
* cmpl_state
)
3947 struct passwd
*pwd_ptr
;
3950 gint len
= 0, i
, count
= 0;
3952 if (cmpl_state
->user_dir_name_buffer
)
3956 while ((pwd_ptr
= getpwent ()) != NULL
)
3958 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
3959 len
+= strlen (utf8
);
3961 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
3962 len
+= strlen (utf8
);
3970 cmpl_state
->user_dir_name_buffer
= g_new (gchar
, len
);
3971 cmpl_state
->user_directories
= g_new (CompletionUserDir
, count
);
3972 cmpl_state
->user_directories_len
= count
;
3974 buf_ptr
= cmpl_state
->user_dir_name_buffer
;
3976 for (i
= 0; i
< count
; i
+= 1)
3978 pwd_ptr
= getpwent ();
3985 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
3986 strcpy (buf_ptr
, utf8
);
3989 cmpl_state
->user_directories
[i
].login
= buf_ptr
;
3991 buf_ptr
+= strlen (buf_ptr
);
3994 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
3995 strcpy (buf_ptr
, utf8
);
3998 cmpl_state
->user_directories
[i
].homedir
= buf_ptr
;
4000 buf_ptr
+= strlen (buf_ptr
);
4004 qsort (cmpl_state
->user_directories
,
4005 cmpl_state
->user_directories_len
,
4006 sizeof (CompletionUserDir
),
4015 if (cmpl_state
->user_dir_name_buffer
)
4016 g_free (cmpl_state
->user_dir_name_buffer
);
4017 if (cmpl_state
->user_directories
)
4018 g_free (cmpl_state
->user_directories
);
4020 cmpl_state
->user_dir_name_buffer
= NULL
;
4021 cmpl_state
->user_directories
= NULL
;
4027 compare_user_dir (const void *a
,
4030 return strcmp ((((CompletionUserDir
*)a
))->login
,
4031 (((CompletionUserDir
*)b
))->login
);
4037 compare_cmpl_dir (const void *a
,
4041 return strcmp (((CompletionDirEntry
*)a
)->sort_key
,
4042 (((CompletionDirEntry
*)b
))->sort_key
);
4046 cmpl_state_okay (CompletionState
* cmpl_state
)
4048 return cmpl_state
&& cmpl_state
->reference_dir
;
4052 cmpl_strerror (gint err
)
4054 if (err
== CMPL_ERRNO_TOO_LONG
)
4055 return _("Name too long");
4056 else if (err
== CMPL_ERRNO_DID_NOT_CONVERT
)
4057 return _("Couldn't convert filename");
4059 return g_strerror (err
);
4062 const gchar
* gtk_dir_selection_get_dir (GtkDirSelection
*filesel
)
4064 return gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));