1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 XangXiu Yang
5 * Jean-Hugues Deschenes
6 * 02-02-2005 Patch removing non standard _fn_match calls
8 * This program is free software; you can redistribute it and/or modify
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License Version 2 as
11 * published by the Free Software Foundation;
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 #include <sys/types.h>
29 #include <limits.h> // For PATH_MAX
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
43 #include <glib.h> /* Include early to get G_OS_WIN32 and
46 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
51 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
53 #include <winsock.h> /* For gethostname */
56 #include "gdk/gdkkeysyms.h"
58 #include <lttvwindow/gtkdirsel.h>
66 #define mkdir(p,m) _mkdir(p)
68 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
70 #endif /* G_OS_WIN32 */
73 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
76 #define DIR_LIST_WIDTH 180
77 #define DIR_LIST_HEIGHT 180
78 #define FILE_LIST_WIDTH 180
79 #define FILE_LIST_HEIGHT 180
81 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
82 * in here, since the rest of the code in the file does require some
87 # define MAXPATHLEN PATH_MAX
89 # define MAXPATHLEN 2048
93 /* I've put this here so it doesn't get confused with the
94 * file completion interface */
95 typedef struct _HistoryCallbackArg HistoryCallbackArg
;
97 struct _HistoryCallbackArg
100 GtkWidget
*menu_item
;
104 typedef struct _CompletionState CompletionState
;
105 typedef struct _CompletionDir CompletionDir
;
106 typedef struct _CompletionDirSent CompletionDirSent
;
107 typedef struct _CompletionDirEntry CompletionDirEntry
;
108 typedef struct _CompletionUserDir CompletionUserDir
;
109 typedef struct _PossibleCompletion PossibleCompletion
;
111 /* Non-external file completion decls and structures */
113 /* A contant telling PRCS how many directories to cache. Its actually
114 * kept in a list, so the geometry isn't important. */
115 #define CMPL_DIRECTORY_CACHE_SIZE 10
117 /* A constant used to determine whether a substring was an exact
118 * match by first_diff_index()
120 #define PATTERN_MATCH -1
121 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
122 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
124 /* This structure contains all the useful information about a directory
125 * for the purposes of filename completion. These structures are cached
126 * in the CompletionState struct. CompletionDir's are reference counted.
128 struct _CompletionDirSent
135 struct _CompletionDirEntry
*entries
;
138 struct _CompletionDir
140 CompletionDirSent
*sent
;
145 struct _CompletionDir
*cmpl_parent
;
150 /* This structure contains pairs of directory entry names with a flag saying
151 * whether or not they are a valid directory. NOTE: This information is used
152 * to provide the caller with information about whether to update its completions
153 * or try to open a file. Since directories are cached by the directory mtime,
154 * a symlink which points to an invalid file (which will not be a directory),
155 * will not be reevaluated if that file is created, unless the containing
156 * directory is touched. I consider this case to be worth ignoring (josh).
158 struct _CompletionDirEntry
165 struct _CompletionUserDir
171 struct _PossibleCompletion
173 /* accessible fields, all are accessed externally by functions
177 gint is_a_completion
;
178 gboolean is_directory
;
185 struct _CompletionState
187 gint last_valid_char
;
189 gint updated_text_len
;
190 gint updated_text_alloc
;
191 gboolean re_complete
;
193 gchar
*user_dir_name_buffer
;
194 gint user_directories_len
;
196 gchar
*last_completion_text
;
198 gint user_completion_index
; /* if >= 0, currently completing ~user */
200 struct _CompletionDir
*completion_dir
; /* directory completing from */
201 struct _CompletionDir
*active_completion_dir
;
203 struct _PossibleCompletion the_completion
;
205 struct _CompletionDir
*reference_dir
; /* initial directory */
207 GList
* directory_storage
;
208 GList
* directory_sent_storage
;
210 struct _CompletionUserDir
*user_directories
;
228 /* File completion functions which would be external, were they used
229 * outside of this file.
232 static CompletionState
* cmpl_init_state (void);
233 static void cmpl_free_state (CompletionState
*cmpl_state
);
234 static gint
cmpl_state_okay (CompletionState
* cmpl_state
);
235 static const gchar
* cmpl_strerror (gint
);
237 static PossibleCompletion
* cmpl_completion_matches(gchar
*text_to_complete
,
238 gchar
**remaining_text
,
239 CompletionState
*cmpl_state
);
241 /* Returns a name for consideration, possibly a completion, this name
242 * will be invalid after the next call to cmpl_next_completion.
244 static char* cmpl_this_completion (PossibleCompletion
*);
246 /* True if this completion matches the given text. Otherwise, this
247 * output can be used to have a list of non-completions.
249 static gint
cmpl_is_a_completion (PossibleCompletion
*);
251 /* True if the completion is a directory
253 static gboolean
cmpl_is_directory (PossibleCompletion
*);
255 /* Obtains the next completion, or NULL
257 static PossibleCompletion
* cmpl_next_completion (CompletionState
*);
259 /* Updating completions: the return value of cmpl_updated_text() will
260 * be text_to_complete completed as much as possible after the most
261 * recent call to cmpl_completion_matches. For the present
262 * application, this is the suggested replacement for the user's input
263 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
266 static gchar
* cmpl_updated_text (CompletionState
* cmpl_state
);
268 /* After updating, to see if the completion was a directory, call
269 * this. If it was, you should consider re-calling completion_matches.
271 static gboolean
cmpl_updated_dir (CompletionState
* cmpl_state
);
273 /* Current location: if using file completion, return the current
274 * directory, from which file completion begins. More specifically,
275 * the cwd concatenated with all exact completions up to the last
276 * directory delimiter('/').
278 static gchar
* cmpl_reference_position (CompletionState
* cmpl_state
);
280 /* backing up: if cmpl_completion_matches returns NULL, you may query
281 * the index of the last completable character into cmpl_updated_text.
283 static gint
cmpl_last_valid_char (CompletionState
* cmpl_state
);
285 /* When the user selects a non-directory, call cmpl_completion_fullname
286 * to get the full name of the selected file.
288 static const gchar
* cmpl_completion_fullname (const gchar
*, CompletionState
* cmpl_state
);
291 /* Directory operations. */
292 static CompletionDir
* open_ref_dir (gchar
* text_to_complete
,
293 gchar
** remaining_text
,
294 CompletionState
* cmpl_state
);
295 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
296 static gboolean
check_dir (gchar
*dir_name
,
298 gboolean
*stat_subdirs
);
300 static CompletionDir
* open_dir (gchar
* dir_name
,
301 CompletionState
* cmpl_state
);
303 static CompletionDir
* open_user_dir (const gchar
* text_to_complete
,
304 CompletionState
*cmpl_state
);
306 static CompletionDir
* open_relative_dir (gchar
* dir_name
, CompletionDir
* dir
,
307 CompletionState
*cmpl_state
);
308 static CompletionDirSent
* open_new_dir (gchar
* dir_name
,
310 gboolean stat_subdirs
);
311 static gint
correct_dir_fullname (CompletionDir
* cmpl_dir
);
312 static gint
correct_parent (CompletionDir
* cmpl_dir
,
315 static gchar
* find_parent_dir_fullname (gchar
* dirname
);
317 static CompletionDir
* attach_dir (CompletionDirSent
* sent
,
319 CompletionState
*cmpl_state
);
320 static void free_dir_sent (CompletionDirSent
* sent
);
321 static void free_dir (CompletionDir
*dir
);
322 static void prune_memory_usage(CompletionState
*cmpl_state
);
324 /* Completion operations */
326 static PossibleCompletion
* attempt_homedir_completion(gchar
* text_to_complete
,
327 CompletionState
*cmpl_state
);
329 static PossibleCompletion
* attempt_dir_completion(CompletionState
*cmpl_state
);
330 static CompletionDir
* find_completion_dir(gchar
* text_to_complete
,
331 gchar
** remaining_text
,
332 CompletionState
* cmpl_state
);
333 static PossibleCompletion
* append_completion_text(gchar
* text
,
334 CompletionState
* cmpl_state
);
336 static gint
get_pwdb(CompletionState
* cmpl_state
);
337 static gint
compare_user_dir(const void* a
, const void* b
);
339 static gint
first_diff_index(gchar
* pat
, gchar
* text
);
340 static gint
compare_cmpl_dir(const void* a
, const void* b
);
341 static void update_cmpl(PossibleCompletion
* poss
,
342 CompletionState
* cmpl_state
);
344 static void gtk_dir_selection_class_init (GtkDirSelectionClass
*klass
);
345 static void gtk_dir_selection_set_property (GObject
*object
,
349 static void gtk_dir_selection_get_property (GObject
*object
,
353 static void gtk_dir_selection_init (GtkDirSelection
*filesel
);
354 static void gtk_dir_selection_finalize (GObject
*object
);
355 static void gtk_dir_selection_destroy (GtkObject
*object
);
356 static void gtk_dir_selection_map (GtkWidget
*widget
);
357 static gint
gtk_dir_selection_key_press (GtkWidget
*widget
,
360 static gint
gtk_dir_selection_insert_text (GtkWidget
*widget
,
361 const gchar
*new_text
,
362 gint new_text_length
,
365 static void gtk_dir_selection_update_fileops (GtkDirSelection
*filesel
);
367 static void gtk_dir_selection_file_activate (GtkTreeView
*tree_view
,
369 GtkTreeViewColumn
*column
,
371 static void gtk_dir_selection_file_changed (GtkTreeSelection
*selection
,
373 static void gtk_dir_selection_dir_activate (GtkTreeView
*tree_view
,
375 GtkTreeViewColumn
*column
,
377 static void gtk_dir_selection_dir_changed (GtkTreeSelection
*selection
,
379 static void gtk_dir_selection_populate (GtkDirSelection
*fs
,
381 gboolean try_complete
,
382 gboolean reset_entry
);
383 static void gtk_dir_selection_abort (GtkDirSelection
*fs
);
385 static void gtk_dir_selection_update_history_menu (GtkDirSelection
*fs
,
388 static void gtk_dir_selection_create_dir (GtkWidget
*widget
, gpointer data
);
389 static void gtk_dir_selection_delete_file (GtkWidget
*widget
, gpointer data
);
390 static void gtk_dir_selection_rename_file (GtkWidget
*widget
, gpointer data
);
392 static void free_selected_names (GPtrArray
*names
);
394 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
395 #define compare_filenames(a, b) strcmp(a, b)
397 #define compare_filenames(a, b) g_ascii_strcasecmp(a, b)
401 static GtkWindowClass
*parent_class
= NULL
;
403 /* Saves errno when something cmpl does fails. */
404 static gint cmpl_errno
;
408 * Take the path currently in the file selection
409 * entry field and translate as necessary from
410 * a WIN32 style to CYGWIN32 style path. For
411 * instance translate:
412 * x:\somepath\file.jpg
414 * /cygdrive/x/somepath/file.jpg
416 * Replace the path in the selection text field.
417 * Return a boolean value concerning whether a
418 * translation had to be made.
421 translate_win32_path (GtkDirSelection
*filesel
)
425 gchar newPath
[PATH_MAX
];
428 * Retrieve the current path
430 path
= gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));
432 cygwin_conv_to_posix_path (path
, newPath
);
433 updated
= (strcmp (path
, newPath
) != 0);
436 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), newPath
);
443 gtk_dir_selection_get_type (void)
445 static GType file_selection_type
= 0;
447 if (!file_selection_type
)
449 static const GTypeInfo filesel_info
=
451 sizeof (GtkDirSelectionClass
),
452 NULL
, /* base_init */
453 NULL
, /* base_finalize */
454 (GClassInitFunc
) gtk_dir_selection_class_init
,
455 NULL
, /* class_finalize */
456 NULL
, /* class_data */
457 sizeof (GtkDirSelection
),
459 (GInstanceInitFunc
) gtk_dir_selection_init
,
463 file_selection_type
=
464 g_type_register_static (GTK_TYPE_DIALOG
, "GtkDirSelection",
468 return file_selection_type
;
472 gtk_dir_selection_class_init (GtkDirSelectionClass
*class)
474 GObjectClass
*gobject_class
;
475 GtkObjectClass
*object_class
;
476 GtkWidgetClass
*widget_class
;
478 gobject_class
= (GObjectClass
*) class;
479 object_class
= (GtkObjectClass
*) class;
480 widget_class
= (GtkWidgetClass
*) class;
482 parent_class
= g_type_class_peek_parent (class);
484 gobject_class
->finalize
= gtk_dir_selection_finalize
;
485 gobject_class
->set_property
= gtk_dir_selection_set_property
;
486 gobject_class
->get_property
= gtk_dir_selection_get_property
;
488 g_object_class_install_property (gobject_class
,
490 g_param_spec_string ("filename",
492 _("The currently selected filename"),
494 G_PARAM_READABLE
| G_PARAM_WRITABLE
));
495 g_object_class_install_property (gobject_class
,
497 g_param_spec_boolean ("show_fileops",
498 _("Show file operations"),
499 _("Whether buttons for creating/manipulating files should be displayed"),
503 g_object_class_install_property (gobject_class
,
504 PROP_SELECT_MULTIPLE
,
505 g_param_spec_boolean ("select_multiple",
506 _("Select multiple"),
507 _("Whether to allow multiple files to be selected"),
511 object_class
->destroy
= gtk_dir_selection_destroy
;
512 widget_class
->map
= gtk_dir_selection_map
;
515 static void gtk_dir_selection_set_property (GObject
*object
,
520 GtkDirSelection
*filesel
;
522 filesel
= GTK_DIR_SELECTION (object
);
527 gtk_dir_selection_set_filename (filesel
,
528 g_value_get_string (value
));
530 case PROP_SHOW_FILEOPS
:
531 if (g_value_get_boolean (value
))
532 gtk_dir_selection_show_fileop_buttons (filesel
);
534 gtk_dir_selection_hide_fileop_buttons (filesel
);
536 case PROP_SELECT_MULTIPLE
:
537 gtk_dir_selection_set_select_multiple (filesel
, g_value_get_boolean (value
));
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
545 static void gtk_dir_selection_get_property (GObject
*object
,
550 GtkDirSelection
*filesel
;
552 filesel
= GTK_DIR_SELECTION (object
);
557 g_value_set_string (value
,
558 gtk_dir_selection_get_filename(filesel
));
561 case PROP_SHOW_FILEOPS
:
562 /* This is a little bit hacky, but doing otherwise would require
563 * adding a field to the object.
565 g_value_set_boolean (value
, (filesel
->fileop_c_dir
&&
566 filesel
->fileop_del_file
&&
567 filesel
->fileop_ren_file
));
569 case PROP_SELECT_MULTIPLE
:
570 g_value_set_boolean (value
, gtk_dir_selection_get_select_multiple (filesel
));
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
579 grab_default (GtkWidget
*widget
)
581 gtk_widget_grab_default (widget
);
586 gtk_dir_selection_init (GtkDirSelection
*filesel
)
588 GtkWidget
*entry_vbox
;
590 GtkWidget
*list_hbox
, *list_container
;
591 GtkWidget
*confirm_area
;
592 GtkWidget
*pulldown_hbox
;
593 GtkWidget
*scrolled_win
;
599 GtkTreeViewColumn
*column
;
601 gtk_widget_push_composite_child ();
603 dialog
= GTK_DIALOG (filesel
);
605 filesel
->cmpl_state
= cmpl_init_state ();
607 /* The dialog-sized vertical box */
608 filesel
->main_vbox
= dialog
->vbox
;
609 gtk_container_set_border_width (GTK_CONTAINER (filesel
), 10);
611 /* The horizontal box containing create, rename etc. buttons */
612 filesel
->button_area
= gtk_hbutton_box_new ();
613 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel
->button_area
), GTK_BUTTONBOX_START
);
614 gtk_box_set_spacing (GTK_BOX (filesel
->button_area
), 0);
615 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->button_area
,
617 gtk_widget_show (filesel
->button_area
);
619 gtk_dir_selection_show_fileop_buttons (filesel
);
621 /* hbox for pulldown menu */
622 pulldown_hbox
= gtk_hbox_new (TRUE
, 5);
623 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), pulldown_hbox
, FALSE
, FALSE
, 0);
624 gtk_widget_show (pulldown_hbox
);
627 filesel
->history_pulldown
= gtk_option_menu_new ();
628 // gtk_widget_show (filesel->history_pulldown);
629 // gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
632 /* The horizontal box containing the directory and file listboxes */
634 spacer
= gtk_hbox_new (FALSE
, 0);
635 gtk_widget_set_size_request (spacer
, -1, 5);
636 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
637 gtk_widget_show (spacer
);
639 list_hbox
= gtk_hbox_new (FALSE
, 5);
640 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), list_hbox
, TRUE
, TRUE
, 0);
641 gtk_widget_show (list_hbox
);
643 list_container
= g_object_new (GTK_TYPE_HPANED
,
649 list_container
= list_hbox
;
651 spacer
= gtk_hbox_new (FALSE
, 0);
652 gtk_widget_set_size_request (spacer
, -1, 5);
653 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), spacer
, FALSE
, FALSE
, 0);
654 gtk_widget_show (spacer
);
656 /* The directories list */
658 model
= gtk_list_store_new (1, G_TYPE_STRING
);
659 filesel
->dir_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
660 g_object_unref (model
);
662 column
= gtk_tree_view_column_new_with_attributes (_("Folders"),
663 gtk_cell_renderer_text_new (),
666 label
= gtk_label_new_with_mnemonic (_("Fol_ders"));
667 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->dir_list
);
668 gtk_widget_show (label
);
669 gtk_tree_view_column_set_widget (column
, label
);
670 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
671 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->dir_list
), column
);
673 gtk_widget_set_size_request (filesel
->dir_list
,
674 DIR_LIST_WIDTH
, DIR_LIST_HEIGHT
);
675 g_signal_connect (filesel
->dir_list
, "row_activated",
676 G_CALLBACK (gtk_dir_selection_dir_activate
), filesel
);
677 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->dir_list
)), "changed",
678 G_CALLBACK (gtk_dir_selection_dir_changed
), filesel
);
680 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
682 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
683 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
684 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->dir_list
);
685 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
686 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
687 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
688 if (GTK_IS_PANED (list_container
))
689 gtk_paned_pack1 (GTK_PANED (list_container
), scrolled_win
, TRUE
, TRUE
);
691 gtk_container_add (GTK_CONTAINER (list_container
), scrolled_win
);
692 gtk_widget_show (filesel
->dir_list
);
693 gtk_widget_show (scrolled_win
);
696 model
= gtk_list_store_new (1, G_TYPE_STRING
);
697 filesel
->file_list
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (model
));
698 g_object_unref (model
);
700 column
= gtk_tree_view_column_new_with_attributes (_("Files"),
701 gtk_cell_renderer_text_new (),
704 label
= gtk_label_new_with_mnemonic (_("_Files"));
705 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), filesel
->file_list
);
706 gtk_widget_show (label
);
707 gtk_tree_view_column_set_widget (column
, label
);
708 gtk_tree_view_column_set_sizing (column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
709 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel
->file_list
), column
);
711 gtk_widget_set_size_request (filesel
->file_list
,
712 FILE_LIST_WIDTH
, FILE_LIST_HEIGHT
);
713 g_signal_connect (filesel
->file_list
, "row_activated",
714 G_CALLBACK (gtk_dir_selection_file_activate
), filesel
);
715 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
)), "changed",
716 G_CALLBACK (gtk_dir_selection_file_changed
), filesel
);
718 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
720 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
721 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win
), GTK_SHADOW_IN
);
722 gtk_container_add (GTK_CONTAINER (scrolled_win
), filesel
->file_list
);
723 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
724 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
725 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win
), 0);
726 // gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
727 // gtk_widget_show (filesel->file_list);
728 // gtk_widget_show (scrolled_win);
730 /* action area for packing buttons into. */
731 filesel
->action_area
= gtk_hbox_new (TRUE
, 0);
732 gtk_box_pack_start (GTK_BOX (filesel
->main_vbox
), filesel
->action_area
,
734 gtk_widget_show (filesel
->action_area
);
736 /* The OK/Cancel button area */
737 confirm_area
= dialog
->action_area
;
739 /* The Cancel button */
740 filesel
->cancel_button
= gtk_dialog_add_button (dialog
,
742 GTK_RESPONSE_CANCEL
);
744 filesel
->ok_button
= gtk_dialog_add_button (dialog
,
748 gtk_widget_grab_default (filesel
->ok_button
);
750 /* The selection entry widget */
751 entry_vbox
= gtk_vbox_new (FALSE
, 2);
752 gtk_box_pack_end (GTK_BOX (filesel
->main_vbox
), entry_vbox
, FALSE
, FALSE
, 2);
753 gtk_widget_show (entry_vbox
);
755 eventbox
= gtk_event_box_new ();
756 filesel
->selection_text
= label
= gtk_label_new ("");
757 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
758 gtk_container_add (GTK_CONTAINER (eventbox
), label
);
759 gtk_box_pack_start (GTK_BOX (entry_vbox
), eventbox
, FALSE
, FALSE
, 0);
760 gtk_widget_show (label
);
761 gtk_widget_show (eventbox
);
763 filesel
->selection_entry
= gtk_entry_new ();
764 g_signal_connect (filesel
->selection_entry
, "key_press_event",
765 G_CALLBACK (gtk_dir_selection_key_press
), filesel
);
766 g_signal_connect (filesel
->selection_entry
, "insert_text",
767 G_CALLBACK (gtk_dir_selection_insert_text
), NULL
);
768 g_signal_connect_swapped (filesel
->selection_entry
, "changed",
769 G_CALLBACK (gtk_dir_selection_update_fileops
), filesel
);
770 g_signal_connect_swapped (filesel
->selection_entry
, "focus_in_event",
771 G_CALLBACK (grab_default
),
773 g_signal_connect_swapped (filesel
->selection_entry
, "activate",
774 G_CALLBACK (gtk_button_clicked
),
777 gtk_box_pack_start (GTK_BOX (entry_vbox
), filesel
->selection_entry
, TRUE
, TRUE
, 0);
778 gtk_widget_show (filesel
->selection_entry
);
780 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel
->selection_text
),
781 filesel
->selection_entry
);
783 if (!cmpl_state_okay (filesel
->cmpl_state
))
787 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
789 gtk_label_set_text (GTK_LABEL (filesel
->selection_text
), err_buf
);
793 gtk_dir_selection_populate (filesel
, "", FALSE
, TRUE
);
796 gtk_widget_grab_focus (filesel
->selection_entry
);
798 gtk_widget_pop_composite_child ();
802 uri_list_extract_first_uri (const gchar
* uri_list
)
806 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
809 /* We don't actually try to validate the URI according to RFC
810 * 2396, or even check for allowed characters - we just ignore
811 * comments and trim whitespace off the ends. We also
812 * allow LF delimination as well as the specified CRLF.
814 * We do allow comments like specified in RFC 2483.
820 while (g_ascii_isspace(*p
))
824 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
830 while (q
> p
&& g_ascii_isspace (*q
))
834 return g_strndup (p
, q
- p
+ 1);
837 p
= strchr (p
, '\n');
845 dnd_really_drop (GtkWidget
*dialog
, gint response_id
, GtkDirSelection
*fs
)
849 if (response_id
== GTK_RESPONSE_YES
)
851 filename
= g_object_get_data (G_OBJECT (dialog
), "gtk-fs-dnd-filename");
853 gtk_dir_selection_set_filename (fs
, filename
);
856 gtk_widget_destroy (dialog
);
861 filenames_dropped (GtkWidget
*widget
,
862 GdkDragContext
*context
,
865 GtkSelectionData
*selection_data
,
870 char *filename
= NULL
;
872 char this_hostname
[257];
874 GError
*error
= NULL
;
876 if (!selection_data
->data
)
879 uri
= uri_list_extract_first_uri ((char *)selection_data
->data
);
884 filename
= g_filename_from_uri (uri
, &hostname
, &error
);
889 g_warning ("Error getting dropped filename: %s\n",
891 g_error_free (error
);
895 res
= gethostname (this_hostname
, 256);
896 this_hostname
[256] = 0;
898 if ((hostname
== NULL
) ||
899 (res
== 0 && strcmp (hostname
, this_hostname
) == 0) ||
900 (strcmp (hostname
, "localhost") == 0))
901 gtk_dir_selection_set_filename (GTK_DIR_SELECTION (widget
),
906 gchar
*filename_utf8
;
908 /* Conversion back to UTF-8 should always succeed for the result
909 * of g_filename_from_uri()
911 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
912 g_assert (filename_utf8
);
914 dialog
= gtk_message_dialog_new (GTK_WINDOW (widget
),
915 GTK_DIALOG_DESTROY_WITH_PARENT
,
916 GTK_MESSAGE_QUESTION
,
918 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
919 "Are you sure that you want to select it?"), filename_utf8
, hostname
);
920 g_free (filename_utf8
);
922 g_object_set_data_full (G_OBJECT (dialog
), "gtk-fs-dnd-filename", g_strdup (filename
), g_free
);
924 g_signal_connect_data (dialog
, "response",
925 (GCallback
) dnd_really_drop
,
928 gtk_widget_show (dialog
);
946 filenames_drag_get (GtkWidget
*widget
,
947 GdkDragContext
*context
,
948 GtkSelectionData
*selection_data
,
951 GtkDirSelection
*filesel
)
959 file
= gtk_dir_selection_get_filename (filesel
);
963 if (info
== TARGET_URILIST
)
965 res
= gethostname (hostname
, 256);
968 uri_list
= g_filename_to_uri (file
, (!res
)?hostname
:NULL
, &error
);
971 g_warning ("Error getting filename: %s\n",
973 g_error_free (error
);
977 gtk_selection_data_set (selection_data
,
978 selection_data
->target
, 8,
979 (void *)uri_list
, strlen((char *)uri_list
));
984 gchar
*filename_utf8
= g_filename_to_utf8 (file
, -1, NULL
, NULL
, NULL
);
985 g_assert (filename_utf8
);
986 gtk_selection_data_set_text (selection_data
, filename_utf8
, -1);
987 g_free (filename_utf8
);
993 file_selection_setup_dnd (GtkDirSelection
*filesel
)
996 static const GtkTargetEntry drop_types
[] = {
997 { "text/uri-list", 0, TARGET_URILIST
}
999 static gint n_drop_types
= sizeof(drop_types
)/sizeof(drop_types
[0]);
1000 static const GtkTargetEntry drag_types
[] = {
1001 { "text/uri-list", 0, TARGET_URILIST
},
1002 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
1005 { "COMPOUND_TEXT", 0, 0 }
1007 static gint n_drag_types
= sizeof(drag_types
)/sizeof(drag_types
[0]);
1009 gtk_drag_dest_set (GTK_WIDGET (filesel
),
1010 GTK_DEST_DEFAULT_ALL
,
1011 drop_types
, n_drop_types
,
1014 g_signal_connect (filesel
, "drag_data_received",
1015 G_CALLBACK (filenames_dropped
), NULL
);
1017 eventbox
= gtk_widget_get_parent (filesel
->selection_text
);
1018 gtk_drag_source_set (eventbox
,
1020 drag_types
, n_drag_types
,
1023 g_signal_connect (eventbox
, "drag_data_get",
1024 G_CALLBACK (filenames_drag_get
), filesel
);
1028 gtk_dir_selection_new (const gchar
*title
)
1030 GtkDirSelection
*filesel
;
1032 filesel
= g_object_new (GTK_TYPE_DIR_SELECTION
, NULL
);
1033 gtk_window_set_title (GTK_WINDOW (filesel
), title
);
1034 gtk_dialog_set_has_separator (GTK_DIALOG (filesel
), FALSE
);
1036 file_selection_setup_dnd (filesel
);
1038 return GTK_WIDGET (filesel
);
1042 gtk_dir_selection_show_fileop_buttons (GtkDirSelection
*filesel
)
1044 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1046 /* delete, create directory, and rename */
1047 if (!filesel
->fileop_c_dir
)
1049 filesel
->fileop_c_dir
= gtk_button_new_with_mnemonic (_("_New Folder"));
1050 g_signal_connect (filesel
->fileop_c_dir
, "clicked",
1051 G_CALLBACK (gtk_dir_selection_create_dir
),
1053 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1054 // filesel->fileop_c_dir, TRUE, TRUE, 0);
1055 // gtk_widget_show (filesel->fileop_c_dir);
1058 if (!filesel
->fileop_del_file
)
1060 filesel
->fileop_del_file
= gtk_button_new_with_mnemonic (_("De_lete File"));
1061 g_signal_connect (filesel
->fileop_del_file
, "clicked",
1062 G_CALLBACK (gtk_dir_selection_delete_file
),
1064 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1065 // filesel->fileop_del_file, TRUE, TRUE, 0);
1066 // gtk_widget_show (filesel->fileop_del_file);
1069 if (!filesel
->fileop_ren_file
)
1071 filesel
->fileop_ren_file
= gtk_button_new_with_mnemonic (_("_Rename File"));
1072 g_signal_connect (filesel
->fileop_ren_file
, "clicked",
1073 G_CALLBACK (gtk_dir_selection_rename_file
),
1075 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1076 // filesel->fileop_ren_file, TRUE, TRUE, 0);
1077 // gtk_widget_show (filesel->fileop_ren_file);
1080 gtk_dir_selection_update_fileops (filesel
);
1082 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1086 gtk_dir_selection_hide_fileop_buttons (GtkDirSelection
*filesel
)
1088 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1090 if (filesel
->fileop_ren_file
)
1092 gtk_widget_destroy (filesel
->fileop_ren_file
);
1093 filesel
->fileop_ren_file
= NULL
;
1096 if (filesel
->fileop_del_file
)
1098 gtk_widget_destroy (filesel
->fileop_del_file
);
1099 filesel
->fileop_del_file
= NULL
;
1102 if (filesel
->fileop_c_dir
)
1104 gtk_widget_destroy (filesel
->fileop_c_dir
);
1105 filesel
->fileop_c_dir
= NULL
;
1107 g_object_notify (G_OBJECT (filesel
), "show_fileops");
1113 * gtk_dir_selection_set_filename:
1114 * @filesel: a #GtkDirSelection.
1115 * @filename: a string to set as the default file name.
1117 * Sets a default path for the file requestor. If @filename includes a
1118 * directory path, then the requestor will open with that path as its
1119 * current working directory.
1121 * The encoding of @filename is the on-disk encoding, which
1122 * may not be UTF-8. See g_filename_from_utf8().
1125 gtk_dir_selection_set_filename (GtkDirSelection
*filesel
,
1126 const gchar
*filename
)
1129 const char *name
, *last_slash
;
1130 char *filename_utf8
;
1132 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1133 g_return_if_fail (filename
!= NULL
);
1135 filename_utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
1136 g_return_if_fail (filename_utf8
!= NULL
);
1138 last_slash
= strrchr (filename_utf8
, G_DIR_SEPARATOR
);
1142 buf
= g_strdup ("");
1143 name
= filename_utf8
;
1147 buf
= g_strdup (filename_utf8
);
1148 buf
[last_slash
- filename_utf8
+ 1] = 0;
1149 name
= last_slash
+ 1;
1152 gtk_dir_selection_populate (filesel
, buf
, FALSE
, TRUE
);
1154 if (filesel
->selection_entry
)
1155 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), name
);
1157 g_object_notify (G_OBJECT (filesel
), "filename");
1159 g_free (filename_utf8
);
1163 * gtk_dir_selection_get_filename:
1164 * @filesel: a #GtkDirSelection
1166 * This function returns the selected filename in the on-disk encoding
1167 * (see g_filename_from_utf8()), which may or may not be the same as that
1168 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1169 * The returned string points to a statically allocated buffer and
1170 * should be copied if you plan to keep it around.
1172 * If no file is selected then the selected directory path is returned.
1174 * Return value: currently-selected filename in the on-disk encoding.
1176 G_CONST_RETURN gchar
*
1177 gtk_dir_selection_get_filename (GtkDirSelection
*filesel
)
1179 static const gchar nothing
[2] = "";
1180 static gchar something
[MAXPATHLEN
*2];
1184 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), nothing
);
1186 #ifdef G_WITH_CYGWIN
1187 translate_win32_path (filesel
);
1189 text
= gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));
1192 sys_filename
= g_filename_from_utf8 (cmpl_completion_fullname (text
, filesel
->cmpl_state
), -1, NULL
, NULL
, NULL
);
1195 strncpy (something
, sys_filename
, sizeof (something
));
1196 g_free (sys_filename
);
1204 gtk_dir_selection_complete (GtkDirSelection
*filesel
,
1205 const gchar
*pattern
)
1207 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
1208 g_return_if_fail (pattern
!= NULL
);
1210 if (filesel
->selection_entry
)
1211 gtk_entry_set_text (GTK_ENTRY (filesel
->selection_entry
), pattern
);
1212 gtk_dir_selection_populate (filesel
, (gchar
*) pattern
, TRUE
, TRUE
);
1216 gtk_dir_selection_destroy (GtkObject
*object
)
1218 GtkDirSelection
*filesel
;
1220 HistoryCallbackArg
*callback_arg
;
1222 g_return_if_fail (GTK_IS_DIR_SELECTION (object
));
1224 filesel
= GTK_DIR_SELECTION (object
);
1226 if (filesel
->fileop_dialog
)
1228 gtk_widget_destroy (filesel
->fileop_dialog
);
1229 filesel
->fileop_dialog
= NULL
;
1232 if (filesel
->history_list
)
1234 list
= filesel
->history_list
;
1237 callback_arg
= list
->data
;
1238 g_free (callback_arg
->directory
);
1239 g_free (callback_arg
);
1242 g_list_free (filesel
->history_list
);
1243 filesel
->history_list
= NULL
;
1246 if (filesel
->cmpl_state
)
1248 cmpl_free_state (filesel
->cmpl_state
);
1249 filesel
->cmpl_state
= NULL
;
1252 if (filesel
->selected_names
)
1254 free_selected_names (filesel
->selected_names
);
1255 filesel
->selected_names
= NULL
;
1258 if (filesel
->last_selected
)
1260 g_free (filesel
->last_selected
);
1261 filesel
->last_selected
= NULL
;
1264 GTK_OBJECT_CLASS (parent_class
)->destroy (object
);
1268 gtk_dir_selection_map (GtkWidget
*widget
)
1270 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (widget
);
1272 /* Refresh the contents */
1273 gtk_dir_selection_populate (filesel
, "", FALSE
, FALSE
);
1275 GTK_WIDGET_CLASS (parent_class
)->map (widget
);
1279 gtk_dir_selection_finalize (GObject
*object
)
1281 GtkDirSelection
*filesel
= GTK_DIR_SELECTION (object
);
1283 g_free (filesel
->fileop_file
);
1285 G_OBJECT_CLASS (parent_class
)->finalize (object
);
1288 /* Begin file operations callbacks */
1291 gtk_dir_selection_fileop_error (GtkDirSelection
*fs
,
1292 gchar
*error_message
)
1296 g_return_if_fail (error_message
!= NULL
);
1299 dialog
= gtk_message_dialog_new (GTK_WINDOW (fs
),
1300 GTK_DIALOG_DESTROY_WITH_PARENT
,
1303 "%s", error_message
);
1305 /* yes, we free it */
1306 g_free (error_message
);
1308 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1310 g_signal_connect_swapped (dialog
, "response",
1311 G_CALLBACK (gtk_widget_destroy
),
1314 gtk_widget_show (dialog
);
1318 gtk_dir_selection_fileop_destroy (GtkWidget
*widget
,
1321 GtkDirSelection
*fs
= data
;
1323 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1325 fs
->fileop_dialog
= NULL
;
1329 entry_is_empty (GtkEntry
*entry
)
1331 const gchar
*text
= gtk_entry_get_text (entry
);
1333 return *text
== '\0';
1337 gtk_dir_selection_fileop_entry_changed (GtkEntry
*entry
,
1340 gtk_widget_set_sensitive (button
, !entry_is_empty (entry
));
1344 gtk_dir_selection_create_dir_confirmed (GtkWidget
*widget
,
1347 GtkDirSelection
*fs
= data
;
1348 const gchar
*dirname
;
1351 gchar
*sys_full_path
;
1353 GError
*error
= NULL
;
1354 CompletionState
*cmpl_state
;
1356 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1358 dirname
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1359 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1360 path
= cmpl_reference_position (cmpl_state
);
1362 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, dirname
, NULL
);
1363 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1366 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1367 buf
= g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname
);
1369 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname
, error
->message
,
1370 _("You probably used symbols not allowed in filenames."));
1371 gtk_dir_selection_fileop_error (fs
, buf
);
1372 g_error_free (error
);
1376 if (mkdir (sys_full_path
, 0755) < 0)
1378 buf
= g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname
,
1379 g_strerror (errno
));
1380 gtk_dir_selection_fileop_error (fs
, buf
);
1385 g_free (sys_full_path
);
1387 gtk_widget_destroy (fs
->fileop_dialog
);
1388 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1392 gtk_dir_selection_create_dir (GtkWidget
*widget
,
1395 GtkDirSelection
*fs
= data
;
1401 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1403 if (fs
->fileop_dialog
)
1407 dialog
= gtk_dialog_new ();
1408 fs
->fileop_dialog
= dialog
;
1409 g_signal_connect (dialog
, "destroy",
1410 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1412 gtk_window_set_title (GTK_WINDOW (dialog
), _("New Folder"));
1413 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1414 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1416 /* If file dialog is grabbed, grab option dialog */
1417 /* When option dialog is closed, file dialog will be grabbed again */
1418 if (GTK_WINDOW (fs
)->modal
)
1419 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1421 vbox
= gtk_vbox_new (FALSE
, 0);
1422 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1423 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1425 gtk_widget_show( vbox
);
1427 label
= gtk_label_new_with_mnemonic (_("_Folder name:"));
1428 gtk_misc_set_alignment(GTK_MISC (label
), 0.0, 0.0);
1429 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1430 gtk_widget_show (label
);
1432 /* The directory entry widget */
1433 fs
->fileop_entry
= gtk_entry_new ();
1434 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), fs
->fileop_entry
);
1435 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1437 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1438 gtk_widget_show (fs
->fileop_entry
);
1441 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1442 g_signal_connect_swapped (button
, "clicked",
1443 G_CALLBACK (gtk_widget_destroy
),
1445 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1446 button
, TRUE
, TRUE
, 0);
1447 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1448 gtk_widget_grab_default (button
);
1449 gtk_widget_show (button
);
1451 gtk_widget_grab_focus (fs
->fileop_entry
);
1453 button
= gtk_button_new_with_mnemonic (_("C_reate"));
1454 gtk_widget_set_sensitive (button
, FALSE
);
1455 g_signal_connect (button
, "clicked",
1456 G_CALLBACK (gtk_dir_selection_create_dir_confirmed
),
1458 g_signal_connect (fs
->fileop_entry
, "changed",
1459 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1462 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1463 button
, TRUE
, TRUE
, 0);
1464 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1465 gtk_widget_show (button
);
1467 gtk_widget_show (dialog
);
1471 gtk_dir_selection_delete_dir_response (GtkDialog
*dialog
,
1475 GtkDirSelection
*fs
= data
;
1476 CompletionState
*cmpl_state
;
1479 gchar
*sys_full_path
;
1480 GError
*error
= NULL
;
1483 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1485 if (response_id
!= GTK_RESPONSE_OK
)
1487 gtk_widget_destroy (GTK_WIDGET (dialog
));
1491 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1492 path
= cmpl_reference_position (cmpl_state
);
1494 full_path
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1495 sys_full_path
= g_filename_from_utf8 (full_path
, -1, NULL
, NULL
, &error
);
1498 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1499 buf
= g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1502 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1503 fs
->fileop_file
, error
->message
,
1504 _("It probably contains symbols not allowed in filenames."));
1506 gtk_dir_selection_fileop_error (fs
, buf
);
1507 g_error_free (error
);
1511 if (unlink (sys_full_path
) < 0)
1513 buf
= g_strdup_printf (_("Error deleting file \"%s\": %s"),
1514 fs
->fileop_file
, g_strerror (errno
));
1515 gtk_dir_selection_fileop_error (fs
, buf
);
1520 g_free (sys_full_path
);
1522 gtk_widget_destroy (fs
->fileop_dialog
);
1523 gtk_dir_selection_populate (fs
, "", FALSE
, TRUE
);
1527 gtk_dir_selection_delete_file (GtkWidget
*widget
,
1530 GtkDirSelection
*fs
= data
;
1532 const gchar
*filename
;
1534 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1536 if (fs
->fileop_dialog
)
1539 #ifdef G_WITH_CYGWIN
1540 translate_win32_path (fs
);
1543 filename
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
1544 if (strlen (filename
) < 1)
1547 g_free (fs
->fileop_file
);
1548 fs
->fileop_file
= g_strdup (filename
);
1551 fs
->fileop_dialog
= dialog
=
1552 gtk_message_dialog_new (GTK_WINDOW (fs
),
1553 GTK_WINDOW (fs
)->modal
? GTK_DIALOG_MODAL
: 0,
1554 GTK_MESSAGE_QUESTION
,
1556 _("Really delete file \"%s\" ?"), filename
);
1558 g_signal_connect (dialog
, "destroy",
1559 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1561 gtk_window_set_title (GTK_WINDOW (dialog
), _("Delete File"));
1562 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1565 gtk_dialog_add_buttons (GTK_DIALOG (dialog
),
1566 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1567 GTK_STOCK_DELETE
, GTK_RESPONSE_OK
,
1570 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_CANCEL
);
1572 g_signal_connect (dialog
, "response",
1573 G_CALLBACK (gtk_dir_selection_delete_dir_response
),
1576 gtk_widget_show (dialog
);
1580 gtk_dir_selection_rename_dir_confirmed (GtkWidget
*widget
,
1583 GtkDirSelection
*fs
= data
;
1587 gchar
*new_filename
;
1588 gchar
*old_filename
;
1589 gchar
*sys_new_filename
;
1590 gchar
*sys_old_filename
;
1591 CompletionState
*cmpl_state
;
1592 GError
*error
= NULL
;
1594 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1596 file
= gtk_entry_get_text (GTK_ENTRY (fs
->fileop_entry
));
1597 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
1598 path
= cmpl_reference_position (cmpl_state
);
1600 new_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, file
, NULL
);
1601 old_filename
= g_strconcat (path
, G_DIR_SEPARATOR_S
, fs
->fileop_file
, NULL
);
1603 sys_new_filename
= g_filename_from_utf8 (new_filename
, -1, NULL
, NULL
, &error
);
1606 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1607 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename
);
1609 buf
= g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1610 new_filename
, error
->message
,
1611 _("You probably used symbols not allowed in filenames."));
1612 gtk_dir_selection_fileop_error (fs
, buf
);
1613 g_error_free (error
);
1617 sys_old_filename
= g_filename_from_utf8 (old_filename
, -1, NULL
, NULL
, &error
);
1620 if (g_error_matches (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
1621 buf
= g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename
);
1623 buf
= g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1624 old_filename
, error
->message
,
1625 _("It probably contains symbols not allowed in filenames."));
1626 gtk_dir_selection_fileop_error (fs
, buf
);
1627 g_error_free (error
);
1631 if (rename (sys_old_filename
, sys_new_filename
) < 0)
1633 buf
= g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1634 sys_old_filename
, sys_new_filename
,
1635 g_strerror (errno
));
1636 gtk_dir_selection_fileop_error (fs
, buf
);
1640 gtk_dir_selection_populate (fs
, "", FALSE
, FALSE
);
1641 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), file
);
1644 g_free (sys_old_filename
);
1647 g_free (new_filename
);
1648 g_free (old_filename
);
1649 g_free (sys_new_filename
);
1651 gtk_widget_destroy (fs
->fileop_dialog
);
1655 gtk_dir_selection_rename_file (GtkWidget
*widget
,
1658 GtkDirSelection
*fs
= data
;
1665 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1667 if (fs
->fileop_dialog
)
1670 g_free (fs
->fileop_file
);
1671 fs
->fileop_file
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1672 if (strlen (fs
->fileop_file
) < 1)
1676 fs
->fileop_dialog
= dialog
= gtk_dialog_new ();
1677 g_signal_connect (dialog
, "destroy",
1678 G_CALLBACK (gtk_dir_selection_fileop_destroy
),
1680 gtk_window_set_title (GTK_WINDOW (dialog
), _("Rename File"));
1681 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_MOUSE
);
1682 gtk_window_set_transient_for (GTK_WINDOW (dialog
), GTK_WINDOW (fs
));
1684 /* If file dialog is grabbed, grab option dialog */
1685 /* When option dialog closed, file dialog will be grabbed again */
1686 if (GTK_WINDOW (fs
)->modal
)
1687 gtk_window_set_modal (GTK_WINDOW (dialog
), TRUE
);
1689 vbox
= gtk_vbox_new (FALSE
, 0);
1690 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 8);
1691 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), vbox
,
1693 gtk_widget_show(vbox
);
1695 buf
= g_strdup_printf (_("Rename file \"%s\" to:"), fs
->fileop_file
);
1696 label
= gtk_label_new (buf
);
1697 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.0);
1698 gtk_box_pack_start (GTK_BOX (vbox
), label
, FALSE
, FALSE
, 5);
1699 gtk_widget_show (label
);
1702 /* New filename entry */
1703 fs
->fileop_entry
= gtk_entry_new ();
1704 gtk_box_pack_start (GTK_BOX (vbox
), fs
->fileop_entry
,
1706 GTK_WIDGET_SET_FLAGS (fs
->fileop_entry
, GTK_CAN_DEFAULT
);
1707 gtk_widget_show (fs
->fileop_entry
);
1709 gtk_entry_set_text (GTK_ENTRY (fs
->fileop_entry
), fs
->fileop_file
);
1710 gtk_editable_select_region (GTK_EDITABLE (fs
->fileop_entry
),
1711 0, strlen (fs
->fileop_file
));
1714 button
= gtk_button_new_from_stock (GTK_STOCK_CANCEL
);
1715 g_signal_connect_swapped (button
, "clicked",
1716 G_CALLBACK (gtk_widget_destroy
),
1718 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1719 button
, TRUE
, TRUE
, 0);
1720 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1721 gtk_widget_grab_default (button
);
1722 gtk_widget_show (button
);
1724 gtk_widget_grab_focus (fs
->fileop_entry
);
1726 button
= gtk_button_new_with_mnemonic (_("_Rename"));
1727 g_signal_connect (button
, "clicked",
1728 G_CALLBACK (gtk_dir_selection_rename_dir_confirmed
),
1730 g_signal_connect (fs
->fileop_entry
, "changed",
1731 G_CALLBACK (gtk_dir_selection_fileop_entry_changed
),
1734 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->action_area
),
1735 button
, TRUE
, TRUE
, 0);
1736 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1737 gtk_widget_show (button
);
1739 gtk_widget_show (dialog
);
1743 gtk_dir_selection_insert_text (GtkWidget
*widget
,
1744 const gchar
*new_text
,
1745 gint new_text_length
,
1751 filename
= g_filename_from_utf8 (new_text
, new_text_length
, NULL
, NULL
, NULL
);
1755 gdk_display_beep (gtk_widget_get_display (widget
));
1756 g_signal_stop_emission_by_name (widget
, "insert_text");
1766 gtk_dir_selection_update_fileops (GtkDirSelection
*fs
)
1770 if (!fs
->selection_entry
)
1773 sensitive
= !entry_is_empty (GTK_ENTRY (fs
->selection_entry
));
1775 if (fs
->fileop_del_file
)
1776 gtk_widget_set_sensitive (fs
->fileop_del_file
, sensitive
);
1778 if (fs
->fileop_ren_file
)
1779 gtk_widget_set_sensitive (fs
->fileop_ren_file
, sensitive
);
1783 gtk_dir_selection_key_press (GtkWidget
*widget
,
1787 GtkDirSelection
*fs
;
1790 g_return_val_if_fail (widget
!= NULL
, FALSE
);
1791 g_return_val_if_fail (event
!= NULL
, FALSE
);
1793 if ((event
->keyval
== GDK_Tab
|| event
->keyval
== GDK_KP_Tab
) &&
1794 (event
->state
& gtk_accelerator_get_default_mod_mask ()) == 0)
1796 fs
= GTK_DIR_SELECTION (user_data
);
1797 #ifdef G_WITH_CYGWIN
1798 translate_win32_path (fs
);
1800 text
= g_strdup (gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
)));
1802 gtk_dir_selection_populate (fs
, text
, TRUE
, TRUE
);
1813 gtk_dir_selection_history_callback (GtkWidget
*widget
,
1816 GtkDirSelection
*fs
= data
;
1817 HistoryCallbackArg
*callback_arg
;
1820 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1822 list
= fs
->history_list
;
1825 callback_arg
= list
->data
;
1827 if (callback_arg
->menu_item
== widget
)
1829 gtk_dir_selection_populate (fs
, callback_arg
->directory
, FALSE
, FALSE
);
1838 gtk_dir_selection_update_history_menu (GtkDirSelection
*fs
,
1839 gchar
*current_directory
)
1841 HistoryCallbackArg
*callback_arg
;
1842 GtkWidget
*menu_item
;
1848 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
1849 g_return_if_fail (current_directory
!= NULL
);
1851 list
= fs
->history_list
;
1853 if (fs
->history_menu
)
1856 callback_arg
= list
->data
;
1857 g_free (callback_arg
->directory
);
1858 g_free (callback_arg
);
1861 g_list_free (fs
->history_list
);
1862 fs
->history_list
= NULL
;
1864 gtk_widget_destroy (fs
->history_menu
);
1867 fs
->history_menu
= gtk_menu_new ();
1869 current_dir
= g_strdup (current_directory
);
1871 dir_len
= strlen (current_dir
);
1873 for (i
= dir_len
; i
>= 0; i
--)
1875 /* the i == dir_len is to catch the full path for the first
1877 if ( (current_dir
[i
] == G_DIR_SEPARATOR
) || (i
== dir_len
))
1879 /* another small hack to catch the full path */
1881 current_dir
[i
+ 1] = '\0';
1882 #ifdef G_WITH_CYGWIN
1883 if (!strcmp (current_dir
, "//"))
1886 menu_item
= gtk_menu_item_new_with_label (current_dir
);
1888 callback_arg
= g_new (HistoryCallbackArg
, 1);
1889 callback_arg
->menu_item
= menu_item
;
1891 /* since the autocompletion gets confused if you don't
1892 * supply a trailing '/' on a dir entry, set the full
1893 * (current) path to "" which just refreshes the filesel */
1896 callback_arg
->directory
= g_strdup ("");
1900 callback_arg
->directory
= g_strdup (current_dir
);
1903 fs
->history_list
= g_list_append (fs
->history_list
, callback_arg
);
1905 g_signal_connect (menu_item
, "activate",
1906 G_CALLBACK (gtk_dir_selection_history_callback
),
1908 gtk_menu_shell_append (GTK_MENU_SHELL (fs
->history_menu
), menu_item
);
1909 gtk_widget_show (menu_item
);
1913 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs
->history_pulldown
),
1915 g_free (current_dir
);
1919 get_real_filename (gchar
*filename
,
1922 #ifdef G_WITH_CYGWIN
1923 /* Check to see if the selection was a drive selector */
1924 if (isalpha (filename
[0]) && (filename
[1] == ':'))
1926 gchar temp_filename
[PATH_MAX
];
1929 cygwin_conv_to_posix_path (filename
, temp_filename
);
1931 /* we need trailing '/'. */
1932 len
= strlen (temp_filename
);
1933 if (len
> 0 && temp_filename
[len
-1] != '/')
1935 temp_filename
[len
] = '/';
1936 temp_filename
[len
+1] = '\0';
1942 return g_strdup (temp_filename
);
1944 #endif /* G_WITH_CYGWIN */
1949 gtk_dir_selection_file_activate (GtkTreeView
*tree_view
,
1951 GtkTreeViewColumn
*column
,
1954 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1955 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1959 gtk_tree_model_get_iter (model
, &iter
, path
);
1960 gtk_tree_model_get (model
, &iter
, FILE_COLUMN
, &filename
, -1);
1961 filename
= get_real_filename (filename
, TRUE
);
1962 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
1963 gtk_button_clicked (GTK_BUTTON (fs
->ok_button
));
1969 gtk_dir_selection_dir_activate (GtkTreeView
*tree_view
,
1971 GtkTreeViewColumn
*column
,
1974 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
1975 GtkTreeModel
*model
= gtk_tree_view_get_model (tree_view
);
1979 gtk_tree_model_get_iter (model
, &iter
, path
);
1980 gtk_tree_model_get (model
, &iter
, DIR_COLUMN
, &filename
, -1);
1981 filename
= get_real_filename (filename
, TRUE
);
1982 gtk_dir_selection_populate (fs
, filename
, FALSE
, FALSE
);
1986 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1989 win32_gtk_add_drives_to_dir_list (GtkListStore
*model
)
1993 char formatBuffer
[128];
1996 /* Get the drives string */
1997 GetLogicalDriveStrings (sizeof (buffer
), buffer
);
1999 /* Add the drives as necessary */
2001 while (*textPtr
!= '\0')
2003 /* Ignore floppies (?) */
2004 if ((tolower (textPtr
[0]) != 'a') && (tolower (textPtr
[0]) != 'b'))
2006 /* Build the actual displayable string */
2007 g_snprintf (formatBuffer
, sizeof (formatBuffer
), "%c:\\", toupper (textPtr
[0]));
2009 /* Add to the list */
2010 gtk_list_store_append (model
, &iter
);
2011 gtk_list_store_set (model
, &iter
, DIR_COLUMN
, formatBuffer
, -1);
2013 textPtr
+= (strlen (textPtr
) + 1);
2019 escape_underscores (const gchar
*str
)
2021 GString
*result
= g_string_new (NULL
);
2025 g_string_append_c (result
, '_');
2027 g_string_append_c (result
, *str
);
2031 return g_string_free (result
, FALSE
);
2035 gtk_dir_selection_populate (GtkDirSelection
*fs
,
2037 gboolean try_complete
,
2038 gboolean reset_entry
)
2040 CompletionState
*cmpl_state
;
2041 PossibleCompletion
* poss
;
2043 GtkListStore
*dir_model
;
2044 GtkListStore
*file_model
;
2046 gchar
* rem_path
= rel_path
;
2048 gint did_recurse
= FALSE
;
2049 gint possible_count
= 0;
2050 gint selection_index
= -1;
2052 g_return_if_fail (GTK_IS_DIR_SELECTION (fs
));
2054 cmpl_state
= (CompletionState
*) fs
->cmpl_state
;
2055 poss
= cmpl_completion_matches (rel_path
, &rem_path
, cmpl_state
);
2057 if (!cmpl_state_okay (cmpl_state
))
2059 /* Something went wrong. */
2060 gtk_dir_selection_abort (fs
);
2064 g_assert (cmpl_state
->reference_dir
);
2066 dir_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->dir_list
)));
2067 file_model
= GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs
->file_list
)));
2069 gtk_list_store_clear (dir_model
);
2070 gtk_list_store_clear (file_model
);
2072 /* Set the dir list to include ./ and ../ */
2073 gtk_list_store_append (dir_model
, &iter
);
2074 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, "." G_DIR_SEPARATOR_S
, -1);
2075 gtk_list_store_append (dir_model
, &iter
);
2076 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, ".." G_DIR_SEPARATOR_S
, -1);
2080 if (cmpl_is_a_completion (poss
))
2082 possible_count
+= 1;
2084 filename
= cmpl_this_completion (poss
);
2086 if (cmpl_is_directory (poss
))
2088 if (strcmp (filename
, "." G_DIR_SEPARATOR_S
) != 0 &&
2089 strcmp (filename
, ".." G_DIR_SEPARATOR_S
) != 0)
2091 gtk_list_store_append (dir_model
, &iter
);
2092 gtk_list_store_set (dir_model
, &iter
, DIR_COLUMN
, filename
, -1);
2097 gtk_list_store_append (file_model
, &iter
);
2098 gtk_list_store_set (file_model
, &iter
, DIR_COLUMN
, filename
, -1);
2102 poss
= cmpl_next_completion (cmpl_state
);
2105 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2106 /* For Windows, add drives as potential selections */
2107 win32_gtk_add_drives_to_dir_list (dir_model
);
2110 /* File lists are set. */
2112 g_assert (cmpl_state
->reference_dir
);
2117 /* User is trying to complete filenames, so advance the user's input
2118 * string to the updated_text, which is the common leading substring
2119 * of all possible completions, and if its a directory attempt
2120 * attempt completions in it. */
2122 if (cmpl_updated_text (cmpl_state
)[0])
2125 if (cmpl_updated_dir (cmpl_state
))
2127 gchar
* dir_name
= g_strdup (cmpl_updated_text (cmpl_state
));
2131 gtk_dir_selection_populate (fs
, dir_name
, TRUE
, TRUE
);
2137 if (fs
->selection_entry
)
2138 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
),
2139 cmpl_updated_text (cmpl_state
));
2144 selection_index
= cmpl_last_valid_char (cmpl_state
) -
2145 (strlen (rel_path
) - strlen (rem_path
));
2146 if (fs
->selection_entry
)
2147 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), rem_path
);
2150 else if (reset_entry
)
2152 if (fs
->selection_entry
)
2153 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2158 if (fs
->selection_entry
)
2159 gtk_editable_set_position (GTK_EDITABLE (fs
->selection_entry
),
2162 if (fs
->selection_entry
)
2164 char *escaped
= escape_underscores (cmpl_reference_position (cmpl_state
));
2165 sel_text
= g_strconcat (_("_Selection: "), escaped
, NULL
);
2168 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs
->selection_text
), sel_text
);
2169 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), cmpl_reference_position (cmpl_state
));
2173 if (fs
->history_pulldown
)
2175 gtk_dir_selection_update_history_menu (fs
, cmpl_reference_position (cmpl_state
));
2182 gtk_dir_selection_abort (GtkDirSelection
*fs
)
2186 g_snprintf (err_buf
, sizeof (err_buf
), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno
));
2188 /* BEEP gdk_beep(); */
2190 if (fs
->selection_entry
)
2191 gtk_label_set_text (GTK_LABEL (fs
->selection_text
), err_buf
);
2195 * gtk_dir_selection_set_select_multiple:
2196 * @filesel: a #GtkDirSelection
2197 * @select_multiple: whether or not the user is allowed to select multiple
2198 * files in the file list.
2200 * Sets whether the user is allowed to select multiple files in the file list.
2201 * Use gtk_dir_selection_get_selections () to get the list of selected files.
2204 gtk_dir_selection_set_select_multiple (GtkDirSelection
*filesel
,
2205 gboolean select_multiple
)
2207 GtkTreeSelection
*sel
;
2208 GtkSelectionMode mode
;
2210 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel
));
2212 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2214 mode
= select_multiple
? GTK_SELECTION_MULTIPLE
: GTK_SELECTION_SINGLE
;
2216 if (mode
!= gtk_tree_selection_get_mode (sel
))
2218 gtk_tree_selection_set_mode (sel
, mode
);
2220 g_object_notify (G_OBJECT (filesel
), "select-multiple");
2225 * gtk_dir_selection_get_select_multiple:
2226 * @filesel: a #GtkDirSelection
2228 * Determines whether or not the user is allowed to select multiple files in
2229 * the file list. See gtk_dir_selection_set_select_multiple().
2231 * Return value: %TRUE if the user is allowed to select multiple files in the
2235 gtk_dir_selection_get_select_multiple (GtkDirSelection
*filesel
)
2237 GtkTreeSelection
*sel
;
2239 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), FALSE
);
2241 sel
= gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel
->file_list
));
2242 return (gtk_tree_selection_get_mode (sel
) == GTK_SELECTION_MULTIPLE
);
2246 multiple_changed_foreach (GtkTreeModel
*model
,
2251 GPtrArray
*names
= data
;
2254 gtk_tree_model_get (model
, iter
, FILE_COLUMN
, &filename
, -1);
2256 g_ptr_array_add (names
, filename
);
2260 free_selected_names (GPtrArray
*names
)
2264 for (i
= 0; i
< names
->len
; i
++)
2265 g_free (g_ptr_array_index (names
, i
));
2267 g_ptr_array_free (names
, TRUE
);
2271 gtk_dir_selection_file_changed (GtkTreeSelection
*selection
,
2274 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2275 GPtrArray
*new_names
;
2280 new_names
= g_ptr_array_sized_new (8);
2282 gtk_tree_selection_selected_foreach (selection
,
2283 multiple_changed_foreach
,
2286 /* nothing selected */
2287 if (new_names
->len
== 0)
2289 g_ptr_array_free (new_names
, TRUE
);
2291 if (fs
->selected_names
!= NULL
)
2293 free_selected_names (fs
->selected_names
);
2294 fs
->selected_names
= NULL
;
2297 goto maybe_clear_entry
;
2300 if (new_names
->len
!= 1)
2302 GPtrArray
*old_names
= fs
->selected_names
;
2304 if (old_names
!= NULL
)
2306 /* A common case is selecting a range of files from top to bottom,
2307 * so quickly check for that to avoid looping over the entire list
2309 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2310 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2311 index
= new_names
->len
- 1;
2317 /* do a quick diff, stopping at the first file not in the
2320 while (i
< old_names
->len
&& j
< new_names
->len
)
2322 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2323 g_ptr_array_index (new_names
, j
));
2340 /* we ran off the end of the old list */
2341 if (index
== -1 && i
< new_names
->len
)
2347 /* A phantom anchor still exists at the point where the last item
2348 * was selected, which is used for subsequent range selections.
2349 * So search up from there.
2351 if (fs
->last_selected
&&
2352 compare_filenames (fs
->last_selected
,
2353 g_ptr_array_index (new_names
, 0)) == 0)
2354 index
= new_names
->len
- 1;
2362 if (fs
->selected_names
!= NULL
)
2363 free_selected_names (fs
->selected_names
);
2365 fs
->selected_names
= new_names
;
2369 if (fs
->last_selected
!= NULL
)
2370 g_free (fs
->last_selected
);
2372 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2373 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2375 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), filename
);
2377 if (filename
!= fs
->last_selected
)
2385 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2386 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2387 (compare_filenames (entry
, fs
->last_selected
) == 0))
2388 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2392 gtk_dir_selection_dir_changed (GtkTreeSelection
*selection
,
2395 GtkDirSelection
*fs
= GTK_DIR_SELECTION (user_data
);
2396 GPtrArray
*new_names
;
2401 new_names
= g_ptr_array_sized_new (8);
2403 gtk_tree_selection_selected_foreach (selection
,
2404 multiple_changed_foreach
,
2407 /* nothing selected */
2408 if (new_names
->len
== 0)
2410 g_ptr_array_free (new_names
, TRUE
);
2412 if (fs
->selected_names
!= NULL
)
2414 free_selected_names (fs
->selected_names
);
2415 fs
->selected_names
= NULL
;
2418 goto maybe_clear_entry
;
2421 if (new_names
->len
!= 1)
2423 GPtrArray
*old_names
= fs
->selected_names
;
2425 if (old_names
!= NULL
)
2427 /* A common case is selecting a range of files from top to bottom,
2428 * so quickly check for that to avoid looping over the entire list
2430 if (compare_filenames (g_ptr_array_index (old_names
, old_names
->len
- 1),
2431 g_ptr_array_index (new_names
, new_names
->len
- 1)) != 0)
2432 index
= new_names
->len
- 1;
2438 /* do a quick diff, stopping at the first file not in the
2441 while (i
< old_names
->len
&& j
< new_names
->len
)
2443 cmp
= compare_filenames (g_ptr_array_index (old_names
, i
),
2444 g_ptr_array_index (new_names
, j
));
2461 /* we ran off the end of the old list */
2462 if (index
== -1 && i
< new_names
->len
)
2468 /* A phantom anchor still exists at the point where the last item
2469 * was selected, which is used for subsequent range selections.
2470 * So search up from there.
2472 if (fs
->last_selected
&&
2473 compare_filenames (fs
->last_selected
,
2474 g_ptr_array_index (new_names
, 0)) == 0)
2475 index
= new_names
->len
- 1;
2483 if (fs
->selected_names
!= NULL
)
2484 free_selected_names (fs
->selected_names
);
2486 fs
->selected_names
= new_names
;
2492 err
= gtk_label_get_text (GTK_LABEL (fs
->selection_text
));
2493 err
+= 11; //pass over "Selection: "
2494 sprintf(str
,"%s",err
);
2497 if (fs
->last_selected
!= NULL
)
2498 g_free (fs
->last_selected
);
2500 fs
->last_selected
= g_strdup (g_ptr_array_index (new_names
, index
));
2501 filename
= get_real_filename (fs
->last_selected
, FALSE
);
2504 strcat(str
,filename
);
2505 str
[strlen(str
)-1] = '\0';
2507 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), str
);
2509 if (filename
!= fs
->last_selected
)
2517 entry
= gtk_entry_get_text (GTK_ENTRY (fs
->selection_entry
));
2518 if ((entry
!= NULL
) && (fs
->last_selected
!= NULL
) &&
2519 (compare_filenames (entry
, fs
->last_selected
) == 0))
2520 gtk_entry_set_text (GTK_ENTRY (fs
->selection_entry
), "");
2524 * gtk_dir_selection_get_selections:
2525 * @filesel: a #GtkDirSelection
2527 * Retrieves the list of file selections the user has made in the dialog box.
2528 * This function is intended for use when the user can select multiple files
2529 * in the file list. The first file in the list is equivalent to what
2530 * gtk_dir_selection_get_filename() would return.
2532 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2533 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2534 * g_filename_to_utf8() on each string.
2536 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2537 * g_strfreev() to free it.
2540 gtk_dir_selection_get_selections (GtkDirSelection
*filesel
)
2544 gchar
*filename
, *dirname
;
2545 gchar
*current
, *buf
;
2547 gboolean unselected_entry
;
2549 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel
), NULL
);
2551 filename
= g_strdup (gtk_dir_selection_get_filename (filesel
));
2553 if (strlen (filename
) == 0)
2559 names
= filesel
->selected_names
;
2562 selections
= g_new (gchar
*, names
->len
+ 2);
2564 selections
= g_new (gchar
*, 2);
2567 unselected_entry
= TRUE
;
2571 dirname
= g_path_get_dirname (filename
);
2573 for (i
= 0; i
< names
->len
; i
++)
2575 buf
= g_filename_from_utf8 (g_ptr_array_index (names
, i
), -1,
2577 current
= g_build_filename (dirname
, buf
, NULL
);
2580 selections
[count
++] = current
;
2582 if (unselected_entry
&& compare_filenames (current
, filename
) == 0)
2583 unselected_entry
= FALSE
;
2589 if (unselected_entry
)
2590 selections
[count
++] = filename
;
2594 selections
[count
] = NULL
;
2599 /**********************************************************************/
2600 /* External Interface */
2601 /**********************************************************************/
2603 /* The four completion state selectors
2606 cmpl_updated_text (CompletionState
*cmpl_state
)
2608 return cmpl_state
->updated_text
;
2612 cmpl_updated_dir (CompletionState
*cmpl_state
)
2614 return cmpl_state
->re_complete
;
2618 cmpl_reference_position (CompletionState
*cmpl_state
)
2620 return cmpl_state
->reference_dir
->fullname
;
2624 cmpl_last_valid_char (CompletionState
*cmpl_state
)
2626 return cmpl_state
->last_valid_char
;
2630 cmpl_completion_fullname (const gchar
*text
,
2631 CompletionState
*cmpl_state
)
2633 static const char nothing
[2] = "";
2635 if (!cmpl_state_okay (cmpl_state
))
2639 else if (g_path_is_absolute (text
))
2641 strcpy (cmpl_state
->updated_text
, text
);
2644 else if (text
[0] == '~')
2649 dir
= open_user_dir (text
, cmpl_state
);
2653 /* spencer says just return ~something, so
2654 * for now just do it. */
2655 strcpy (cmpl_state
->updated_text
, text
);
2660 strcpy (cmpl_state
->updated_text
, dir
->fullname
);
2662 slash
= strchr (text
, G_DIR_SEPARATOR
);
2665 strcat (cmpl_state
->updated_text
, slash
);
2671 strcpy (cmpl_state
->updated_text
, cmpl_state
->reference_dir
->fullname
);
2672 if (cmpl_state
->updated_text
[strlen (cmpl_state
->updated_text
) - 1] != G_DIR_SEPARATOR
)
2673 strcat (cmpl_state
->updated_text
, G_DIR_SEPARATOR_S
);
2674 strcat (cmpl_state
->updated_text
, text
);
2677 return cmpl_state
->updated_text
;
2680 /* The three completion selectors
2683 cmpl_this_completion (PossibleCompletion
* pc
)
2689 cmpl_is_directory (PossibleCompletion
* pc
)
2691 return pc
->is_directory
;
2695 cmpl_is_a_completion (PossibleCompletion
* pc
)
2697 return pc
->is_a_completion
;
2700 /**********************************************************************/
2701 /* Construction, deletion */
2702 /**********************************************************************/
2704 static CompletionState
*
2705 cmpl_init_state (void)
2707 gchar
*sys_getcwd_buf
;
2709 CompletionState
*new_state
;
2711 new_state
= g_new (CompletionState
, 1);
2713 /* g_get_current_dir() returns a string in the "system" charset */
2714 sys_getcwd_buf
= g_get_current_dir ();
2715 utf8_cwd
= g_filename_to_utf8 (sys_getcwd_buf
, -1, NULL
, NULL
, NULL
);
2716 g_free (sys_getcwd_buf
);
2720 new_state
->reference_dir
= NULL
;
2721 new_state
->completion_dir
= NULL
;
2722 new_state
->active_completion_dir
= NULL
;
2723 new_state
->directory_storage
= NULL
;
2724 new_state
->directory_sent_storage
= NULL
;
2725 new_state
->last_valid_char
= 0;
2726 new_state
->updated_text
= g_new (gchar
, MAXPATHLEN
);
2727 new_state
->updated_text_alloc
= MAXPATHLEN
;
2728 new_state
->the_completion
.text
= g_new (gchar
, MAXPATHLEN
);
2729 new_state
->the_completion
.text_alloc
= MAXPATHLEN
;
2730 new_state
->user_dir_name_buffer
= NULL
;
2731 new_state
->user_directories
= NULL
;
2733 new_state
->reference_dir
= open_dir (utf8_cwd
, new_state
);
2735 if (!new_state
->reference_dir
)
2737 /* Directories changing from underneath us, grumble */
2738 strcpy (utf8_cwd
, G_DIR_SEPARATOR_S
);
2747 cmpl_free_dir_list (GList
* dp0
)
2753 free_dir (dp
->data
);
2761 cmpl_free_dir_sent_list (GList
* dp0
)
2767 free_dir_sent (dp
->data
);
2775 cmpl_free_state (CompletionState
* cmpl_state
)
2777 g_return_if_fail (cmpl_state
!= NULL
);
2779 cmpl_free_dir_list (cmpl_state
->directory_storage
);
2780 cmpl_free_dir_sent_list (cmpl_state
->directory_sent_storage
);
2782 if (cmpl_state
->user_dir_name_buffer
)
2783 g_free (cmpl_state
->user_dir_name_buffer
);
2784 if (cmpl_state
->user_directories
)
2785 g_free (cmpl_state
->user_directories
);
2786 if (cmpl_state
->the_completion
.text
)
2787 g_free (cmpl_state
->the_completion
.text
);
2788 if (cmpl_state
->updated_text
)
2789 g_free (cmpl_state
->updated_text
);
2791 g_free (cmpl_state
);
2795 free_dir (CompletionDir
* dir
)
2797 g_free (dir
->cmpl_text
);
2798 g_free (dir
->fullname
);
2803 free_dir_sent (CompletionDirSent
* sent
)
2806 for (i
= 0; i
< sent
->entry_count
; i
++)
2808 g_free (sent
->entries
[i
].entry_name
);
2809 g_free (sent
->entries
[i
].sort_key
);
2811 g_free (sent
->entries
);
2816 prune_memory_usage (CompletionState
*cmpl_state
)
2818 GList
* cdsl
= cmpl_state
->directory_sent_storage
;
2819 GList
* cdl
= cmpl_state
->directory_storage
;
2823 for (; cdsl
&& len
< CMPL_DIRECTORY_CACHE_SIZE
; len
+= 1)
2828 cmpl_free_dir_sent_list (cdsl
->next
);
2832 cmpl_state
->directory_storage
= NULL
;
2835 if (cdl
->data
== cmpl_state
->reference_dir
)
2836 cmpl_state
->directory_storage
= g_list_prepend (NULL
, cdl
->data
);
2838 free_dir (cdl
->data
);
2845 /**********************************************************************/
2846 /* The main entrances. */
2847 /**********************************************************************/
2849 static PossibleCompletion
*
2850 cmpl_completion_matches (gchar
*text_to_complete
,
2851 gchar
**remaining_text
,
2852 CompletionState
*cmpl_state
)
2854 PossibleCompletion
*poss
;
2856 prune_memory_usage (cmpl_state
);
2858 g_assert (text_to_complete
!= NULL
);
2860 cmpl_state
->user_completion_index
= -1;
2861 cmpl_state
->last_completion_text
= text_to_complete
;
2862 cmpl_state
->the_completion
.text
[0] = 0;
2863 cmpl_state
->last_valid_char
= 0;
2864 cmpl_state
->updated_text_len
= -1;
2865 cmpl_state
->updated_text
[0] = 0;
2866 cmpl_state
->re_complete
= FALSE
;
2869 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2871 if (text_to_complete
[0] == '~' && !first_slash
)
2873 /* Text starts with ~ and there is no slash, show all the
2874 * home directory completions.
2876 poss
= attempt_homedir_completion (text_to_complete
, cmpl_state
);
2878 update_cmpl (poss
, cmpl_state
);
2883 cmpl_state
->reference_dir
=
2884 open_ref_dir (text_to_complete
, remaining_text
, cmpl_state
);
2886 if (!cmpl_state
->reference_dir
)
2889 cmpl_state
->completion_dir
=
2890 find_completion_dir (*remaining_text
, remaining_text
, cmpl_state
);
2892 cmpl_state
->last_valid_char
= *remaining_text
- text_to_complete
;
2894 if (!cmpl_state
->completion_dir
)
2897 cmpl_state
->completion_dir
->cmpl_index
= -1;
2898 cmpl_state
->completion_dir
->cmpl_parent
= NULL
;
2899 cmpl_state
->completion_dir
->cmpl_text
= g_strdup (*remaining_text
);
2901 cmpl_state
->active_completion_dir
= cmpl_state
->completion_dir
;
2903 cmpl_state
->reference_dir
= cmpl_state
->completion_dir
;
2905 poss
= attempt_dir_completion (cmpl_state
);
2907 update_cmpl (poss
, cmpl_state
);
2912 static PossibleCompletion
*
2913 cmpl_next_completion (CompletionState
* cmpl_state
)
2915 PossibleCompletion
* poss
= NULL
;
2917 cmpl_state
->the_completion
.text
[0] = 0;
2920 if (cmpl_state
->user_completion_index
>= 0)
2921 poss
= attempt_homedir_completion (cmpl_state
->last_completion_text
, cmpl_state
);
2923 poss
= attempt_dir_completion (cmpl_state
);
2925 poss
= attempt_dir_completion (cmpl_state
);
2928 update_cmpl (poss
, cmpl_state
);
2933 /**********************************************************************/
2934 /* Directory Operations */
2935 /**********************************************************************/
2937 /* Open the directory where completion will begin from, if possible. */
2938 static CompletionDir
*
2939 open_ref_dir (gchar
*text_to_complete
,
2940 gchar
**remaining_text
,
2941 CompletionState
*cmpl_state
)
2944 CompletionDir
*new_dir
;
2946 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
2948 #ifdef G_WITH_CYGWIN
2949 if (text_to_complete
[0] == '/' && text_to_complete
[1] == '/')
2952 g_snprintf (root_dir
, sizeof (root_dir
), "//%c", text_to_complete
[2]);
2954 new_dir
= open_dir (root_dir
, cmpl_state
);
2957 *remaining_text
= text_to_complete
+ 4;
2965 else if (text_to_complete
[0] == '~')
2967 new_dir
= open_user_dir (text_to_complete
, cmpl_state
);
2972 *remaining_text
= first_slash
+ 1;
2974 *remaining_text
= text_to_complete
+ strlen (text_to_complete
);
2982 else if (g_path_is_absolute (text_to_complete
) || !cmpl_state
->reference_dir
)
2984 gchar
*tmp
= g_strdup (text_to_complete
);
2988 while (*p
&& *p
!= '*' && *p
!= '?')
2992 p
= strrchr (tmp
, G_DIR_SEPARATOR
);
3000 new_dir
= open_dir (tmp
, cmpl_state
);
3003 *remaining_text
= text_to_complete
+
3004 ((p
== tmp
+ 1) ? (p
- tmp
) : (p
+ 1 - tmp
));
3008 /* If no possible candidates, use the cwd */
3009 gchar
*sys_curdir
= g_get_current_dir ();
3010 gchar
*utf8_curdir
= g_filename_to_utf8 (sys_curdir
, -1, NULL
, NULL
, NULL
);
3012 g_free (sys_curdir
);
3014 new_dir
= open_dir (utf8_curdir
, cmpl_state
);
3017 *remaining_text
= text_to_complete
;
3019 g_free (utf8_curdir
);
3026 *remaining_text
= text_to_complete
;
3028 new_dir
= open_dir (cmpl_state
->reference_dir
->fullname
, cmpl_state
);
3033 new_dir
->cmpl_index
= -1;
3034 new_dir
->cmpl_parent
= NULL
;
3042 /* open a directory by user name */
3043 static CompletionDir
*
3044 open_user_dir (const gchar
*text_to_complete
,
3045 CompletionState
*cmpl_state
)
3047 CompletionDir
*result
;
3051 g_assert (text_to_complete
&& text_to_complete
[0] == '~');
3053 first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
3056 cmp_len
= first_slash
- text_to_complete
- 1;
3058 cmp_len
= strlen (text_to_complete
+ 1);
3063 const gchar
*homedir
= g_get_home_dir ();
3064 gchar
*utf8_homedir
= g_filename_to_utf8 (homedir
, -1, NULL
, NULL
, NULL
);
3067 result
= open_dir (utf8_homedir
, cmpl_state
);
3071 g_free (utf8_homedir
);
3076 gchar
* copy
= g_new (char, cmp_len
+ 1);
3080 strncpy (copy
, text_to_complete
+ 1, cmp_len
);
3082 pwd
= getpwnam (copy
);
3089 utf8_dir
= g_filename_to_utf8 (pwd
->pw_dir
, -1, NULL
, NULL
, NULL
);
3090 result
= open_dir (utf8_dir
, cmpl_state
);
3098 /* open a directory relative the the current relative directory */
3099 static CompletionDir
*
3100 open_relative_dir (gchar
*dir_name
,
3102 CompletionState
*cmpl_state
)
3104 CompletionDir
*result
;
3107 path
= g_string_sized_new (dir
->fullname_len
+ strlen (dir_name
) + 10);
3108 g_string_assign (path
, dir
->fullname
);
3110 if (dir
->fullname_len
> 1
3111 && path
->str
[dir
->fullname_len
- 1] != G_DIR_SEPARATOR
)
3112 g_string_append_c (path
, G_DIR_SEPARATOR
);
3113 g_string_append (path
, dir_name
);
3115 result
= open_dir (path
->str
, cmpl_state
);
3117 g_string_free (path
, TRUE
);
3122 /* after the cache lookup fails, really open a new directory */
3123 static CompletionDirSent
*
3124 open_new_dir (gchar
*dir_name
,
3126 gboolean stat_subdirs
)
3128 CompletionDirSent
*sent
;
3131 GError
*error
= NULL
;
3132 gint entry_count
= 0;
3135 struct stat ent_sbuf
;
3137 gchar
*sys_dir_name
;
3139 sent
= g_new (CompletionDirSent
, 1);
3140 sent
->mtime
= sbuf
->st_mtime
;
3141 sent
->inode
= sbuf
->st_ino
;
3142 sent
->device
= sbuf
->st_dev
;
3144 path
= g_string_sized_new (2*MAXPATHLEN
+ 10);
3146 sys_dir_name
= g_filename_from_utf8 (dir_name
, -1, NULL
, NULL
, NULL
);
3149 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3153 directory
= g_dir_open (sys_dir_name
, 0, &error
);
3156 cmpl_errno
= error
->code
; /* ??? */
3157 g_free (sys_dir_name
);
3161 while ((dirent
= g_dir_read_name (directory
)) != NULL
)
3164 entry_count
+= 2; /* For ".",".." */
3166 sent
->entries
= g_new (CompletionDirEntry
, entry_count
);
3167 sent
->entry_count
= entry_count
;
3169 g_dir_rewind (directory
);
3171 for (i
= 0; i
< entry_count
; i
+= 1)
3179 dirent
= g_dir_read_name (directory
);
3180 if (!dirent
) /* Directory changed */
3184 sent
->entries
[n_entries
].entry_name
= g_filename_to_utf8 (dirent
, -1, NULL
, NULL
, &error
);
3185 if (sent
->entries
[n_entries
].entry_name
== NULL
3186 || !g_utf8_validate (sent
->entries
[n_entries
].entry_name
, -1, NULL
))
3188 gchar
*escaped_str
= g_strescape (dirent
, NULL
);
3189 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3190 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3192 error
->message
? error
->message
: _("Invalid Utf-8"));
3193 g_free (escaped_str
);
3194 g_clear_error (&error
);
3197 g_clear_error (&error
);
3199 sent
->entries
[n_entries
].sort_key
= g_utf8_collate_key (sent
->entries
[n_entries
].entry_name
, -1);
3201 g_string_assign (path
, sys_dir_name
);
3202 if (path
->str
[path
->len
-1] != G_DIR_SEPARATOR
)
3204 g_string_append_c (path
, G_DIR_SEPARATOR
);
3206 g_string_append (path
, dirent
);
3210 /* Here we know path->str is a "system charset" string */
3211 if (stat (path
->str
, &ent_sbuf
) >= 0 && S_ISDIR (ent_sbuf
.st_mode
))
3212 sent
->entries
[n_entries
].is_dir
= TRUE
;
3214 /* stat may fail, and we don't mind, since it could be a
3215 * dangling symlink. */
3216 sent
->entries
[n_entries
].is_dir
= FALSE
;
3219 sent
->entries
[n_entries
].is_dir
= 1;
3223 sent
->entry_count
= n_entries
;
3225 g_free (sys_dir_name
);
3226 g_string_free (path
, TRUE
);
3227 qsort (sent
->entries
, sent
->entry_count
, sizeof (CompletionDirEntry
), compare_cmpl_dir
);
3229 g_dir_close (directory
);
3234 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3237 check_dir (gchar
*dir_name
,
3238 struct stat
*result
,
3239 gboolean
*stat_subdirs
)
3241 /* A list of directories that we know only contain other directories.
3242 * Trying to stat every file in these directories would be very
3249 struct stat statbuf
;
3250 } no_stat_dirs
[] = {
3251 { "/afs", FALSE
, { 0 } },
3252 { "/net", FALSE
, { 0 } }
3255 static const gint n_no_stat_dirs
= G_N_ELEMENTS (no_stat_dirs
);
3256 static gboolean initialized
= FALSE
;
3257 gchar
*sys_dir_name
;
3263 for (i
= 0; i
< n_no_stat_dirs
; i
++)
3265 if (stat (no_stat_dirs
[i
].name
, &no_stat_dirs
[i
].statbuf
) == 0)
3266 no_stat_dirs
[i
].present
= TRUE
;
3270 sys_dir_name
= g_filename_from_utf8 (dir_name
, -1, NULL
, NULL
, NULL
);
3273 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3277 if (stat (sys_dir_name
, result
) < 0)
3279 g_free (sys_dir_name
);
3283 g_free (sys_dir_name
);
3285 *stat_subdirs
= TRUE
;
3286 for (i
= 0; i
< n_no_stat_dirs
; i
++)
3288 if (no_stat_dirs
[i
].present
&&
3289 (no_stat_dirs
[i
].statbuf
.st_dev
== result
->st_dev
) &&
3290 (no_stat_dirs
[i
].statbuf
.st_ino
== result
->st_ino
))
3292 *stat_subdirs
= FALSE
;
3302 /* open a directory by absolute pathname */
3303 static CompletionDir
*
3304 open_dir (gchar
*dir_name
,
3305 CompletionState
*cmpl_state
)
3308 gboolean stat_subdirs
;
3309 CompletionDirSent
*sent
;
3312 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3313 if (!check_dir (dir_name
, &sbuf
, &stat_subdirs
))
3316 cdsl
= cmpl_state
->directory_sent_storage
;
3322 if (sent
->inode
== sbuf
.st_ino
&&
3323 sent
->mtime
== sbuf
.st_mtime
&&
3324 sent
->device
== sbuf
.st_dev
)
3325 return attach_dir (sent
, dir_name
, cmpl_state
);
3330 stat_subdirs
= TRUE
;
3333 sent
= open_new_dir (dir_name
, &sbuf
, stat_subdirs
);
3337 cmpl_state
->directory_sent_storage
=
3338 g_list_prepend (cmpl_state
->directory_sent_storage
, sent
);
3340 return attach_dir (sent
, dir_name
, cmpl_state
);
3346 static CompletionDir
*
3347 attach_dir (CompletionDirSent
*sent
,
3349 CompletionState
*cmpl_state
)
3351 CompletionDir
* new_dir
;
3353 new_dir
= g_new (CompletionDir
, 1);
3355 cmpl_state
->directory_storage
=
3356 g_list_prepend (cmpl_state
->directory_storage
, new_dir
);
3358 new_dir
->sent
= sent
;
3359 new_dir
->fullname
= g_strdup (dir_name
);
3360 new_dir
->fullname_len
= strlen (dir_name
);
3361 new_dir
->cmpl_text
= NULL
;
3367 correct_dir_fullname (CompletionDir
* cmpl_dir
)
3369 gint length
= strlen (cmpl_dir
->fullname
);
3370 gchar
*first_slash
= strchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3371 gchar
*sys_filename
;
3374 /* Does it end with /. (\.) ? */
3376 strcmp (cmpl_dir
->fullname
+ length
- 2, G_DIR_SEPARATOR_S
".") == 0)
3378 /* Is it just the root directory (on a drive) ? */
3379 if (cmpl_dir
->fullname
+ length
- 2 == first_slash
)
3381 cmpl_dir
->fullname
[length
- 1] = 0;
3382 cmpl_dir
->fullname_len
= length
- 1;
3387 cmpl_dir
->fullname
[length
- 2] = 0;
3391 /* Ends with /./ (\.\)? */
3392 else if (length
>= 3 &&
3393 strcmp (cmpl_dir
->fullname
+ length
- 3,
3394 G_DIR_SEPARATOR_S
"." G_DIR_SEPARATOR_S
) == 0)
3395 cmpl_dir
->fullname
[length
- 2] = 0;
3397 /* Ends with /.. (\..) ? */
3398 else if (length
>= 3 &&
3399 strcmp (cmpl_dir
->fullname
+ length
- 3,
3400 G_DIR_SEPARATOR_S
"..") == 0)
3402 /* Is it just /.. (X:\..)? */
3403 if (cmpl_dir
->fullname
+ length
- 3 == first_slash
)
3405 cmpl_dir
->fullname
[length
- 2] = 0;
3406 cmpl_dir
->fullname_len
= length
- 2;
3410 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3413 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3417 if (stat (sys_filename
, &sbuf
) < 0)
3419 g_free (sys_filename
);
3423 g_free (sys_filename
);
3425 cmpl_dir
->fullname
[length
- 3] = 0;
3427 if (!correct_parent (cmpl_dir
, &sbuf
))
3431 /* Ends with /../ (\..\)? */
3432 else if (length
>= 4 &&
3433 strcmp (cmpl_dir
->fullname
+ length
- 4,
3434 G_DIR_SEPARATOR_S
".." G_DIR_SEPARATOR_S
) == 0)
3436 /* Is it just /../ (X:\..\)? */
3437 if (cmpl_dir
->fullname
+ length
- 4 == first_slash
)
3439 cmpl_dir
->fullname
[length
- 3] = 0;
3440 cmpl_dir
->fullname_len
= length
- 3;
3444 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3447 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3451 if (stat (sys_filename
, &sbuf
) < 0)
3453 g_free (sys_filename
);
3457 g_free (sys_filename
);
3459 cmpl_dir
->fullname
[length
- 4] = 0;
3461 if (!correct_parent (cmpl_dir
, &sbuf
))
3465 cmpl_dir
->fullname_len
= strlen (cmpl_dir
->fullname
);
3471 correct_parent (CompletionDir
*cmpl_dir
,
3478 gchar
*sys_filename
;
3481 last_slash
= strrchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3482 g_assert (last_slash
);
3483 first_slash
= strchr (cmpl_dir
->fullname
, G_DIR_SEPARATOR
);
3485 /* Clever (?) way to check for top-level directory that works also on
3486 * Win32, where there is a drive letter and colon prefixed...
3488 if (last_slash
!= first_slash
)
3498 sys_filename
= g_filename_from_utf8 (cmpl_dir
->fullname
, -1, NULL
, NULL
, NULL
);
3501 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3503 last_slash
[0] = G_DIR_SEPARATOR
;
3507 if (stat (sys_filename
, &parbuf
) < 0)
3509 g_free (sys_filename
);
3512 last_slash
[0] = G_DIR_SEPARATOR
;
3515 g_free (sys_filename
);
3517 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3518 if (parbuf
.st_ino
== sbuf
->st_ino
&& parbuf
.st_dev
== sbuf
->st_dev
)
3519 /* it wasn't a link */
3525 last_slash
[0] = G_DIR_SEPARATOR
;
3527 /* it was a link, have to figure it out the hard way */
3529 new_name
= find_parent_dir_fullname (cmpl_dir
->fullname
);
3534 g_free (cmpl_dir
->fullname
);
3536 cmpl_dir
->fullname
= new_name
;
3545 find_parent_dir_fullname (gchar
* dirname
)
3547 gchar
*sys_orig_dir
;
3552 sys_orig_dir
= g_get_current_dir ();
3553 sys_dirname
= g_filename_from_utf8 (dirname
, -1, NULL
, NULL
, NULL
);
3556 g_free (sys_orig_dir
);
3557 cmpl_errno
= CMPL_ERRNO_DID_NOT_CONVERT
;
3561 if (chdir (sys_dirname
) != 0 || chdir ("..") != 0)
3564 chdir (sys_orig_dir
);
3565 g_free (sys_dirname
);
3566 g_free (sys_orig_dir
);
3569 g_free (sys_dirname
);
3571 sys_cwd
= g_get_current_dir ();
3572 result
= g_filename_to_utf8 (sys_cwd
, -1, NULL
, NULL
, NULL
);
3575 if (chdir (sys_orig_dir
) != 0)
3578 g_free (sys_orig_dir
);
3582 g_free (sys_orig_dir
);
3588 /**********************************************************************/
3589 /* Completion Operations */
3590 /**********************************************************************/
3594 static PossibleCompletion
*
3595 attempt_homedir_completion (gchar
*text_to_complete
,
3596 CompletionState
*cmpl_state
)
3600 if (!cmpl_state
->user_dir_name_buffer
&&
3601 !get_pwdb (cmpl_state
))
3603 length
= strlen (text_to_complete
) - 1;
3605 cmpl_state
->user_completion_index
+= 1;
3607 while (cmpl_state
->user_completion_index
< cmpl_state
->user_directories_len
)
3609 index
= first_diff_index (text_to_complete
+ 1,
3610 cmpl_state
->user_directories
3611 [cmpl_state
->user_completion_index
].login
);
3618 if (cmpl_state
->last_valid_char
< (index
+ 1))
3619 cmpl_state
->last_valid_char
= index
+ 1;
3620 cmpl_state
->user_completion_index
+= 1;
3624 cmpl_state
->the_completion
.is_a_completion
= 1;
3625 cmpl_state
->the_completion
.is_directory
= TRUE
;
3627 append_completion_text ("~", cmpl_state
);
3629 append_completion_text (cmpl_state
->
3630 user_directories
[cmpl_state
->user_completion_index
].login
,
3633 return append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3636 if (text_to_complete
[1]
3637 || cmpl_state
->user_completion_index
> cmpl_state
->user_directories_len
)
3639 cmpl_state
->user_completion_index
= -1;
3644 cmpl_state
->user_completion_index
+= 1;
3645 cmpl_state
->the_completion
.is_a_completion
= 1;
3646 cmpl_state
->the_completion
.is_directory
= TRUE
;
3648 return append_completion_text ("~" G_DIR_SEPARATOR_S
, cmpl_state
);
3654 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3655 #define FOLD(c) (tolower(c))
3660 /* returns the index (>= 0) of the first differing character,
3661 * PATTERN_MATCH if the completion matches */
3663 first_diff_index (gchar
*pat
,
3668 while (*pat
&& *text
&& FOLD (*text
) == FOLD (*pat
))
3678 return PATTERN_MATCH
;
3681 static PossibleCompletion
*
3682 append_completion_text (gchar
*text
,
3683 CompletionState
*cmpl_state
)
3687 if (!cmpl_state
->the_completion
.text
)
3690 len
= strlen (text
) + strlen (cmpl_state
->the_completion
.text
) + 1;
3692 if (cmpl_state
->the_completion
.text_alloc
> len
)
3694 strcat (cmpl_state
->the_completion
.text
, text
);
3695 return &cmpl_state
->the_completion
;
3701 cmpl_state
->the_completion
.text_alloc
= i
;
3703 cmpl_state
->the_completion
.text
= (gchar
*) g_realloc (cmpl_state
->the_completion
.text
, i
);
3705 if (!cmpl_state
->the_completion
.text
)
3709 strcat (cmpl_state
->the_completion
.text
, text
);
3710 return &cmpl_state
->the_completion
;
3714 static CompletionDir
*
3715 find_completion_dir (gchar
*text_to_complete
,
3716 gchar
**remaining_text
,
3717 CompletionState
*cmpl_state
)
3719 gchar
* first_slash
= strchr (text_to_complete
, G_DIR_SEPARATOR
);
3720 CompletionDir
* dir
= cmpl_state
->reference_dir
;
3721 CompletionDir
* next
;
3722 *remaining_text
= text_to_complete
;
3726 gint len
= first_slash
- *remaining_text
;
3728 gchar
*found_name
= NULL
; /* Quiet gcc */
3730 gchar
* pat_buf
= g_new (gchar
, len
+ 1);
3731 GtkFileFilter
*filter
= gtk_file_filter_new ();
3732 GtkFileFilterInfo filter_info
= {GTK_FILE_FILTER_FILENAME
,};
3734 strncpy (pat_buf
, *remaining_text
, len
);
3737 gtk_file_filter_add_pattern (filter
, pat_buf
);
3739 for (i
= 0; i
< dir
->sent
->entry_count
; i
+= 1)
3741 filter_info
.filename
= dir
->sent
->entries
[i
].entry_name
;
3742 if (dir
->sent
->entries
[i
].is_dir
&&
3743 gtk_file_filter_filter(filter
, &filter_info
))
3753 found_name
= dir
->sent
->entries
[i
].entry_name
;
3760 /* Perhaps we are trying to open an automount directory */
3761 found_name
= pat_buf
;
3764 next
= open_relative_dir (found_name
, dir
, cmpl_state
);
3772 next
->cmpl_parent
= dir
;
3776 if (!correct_dir_fullname (dir
))
3782 *remaining_text
= first_slash
+ 1;
3783 first_slash
= strchr (*remaining_text
, G_DIR_SEPARATOR
);
3792 update_cmpl (PossibleCompletion
*poss
,
3793 CompletionState
*cmpl_state
)
3797 if (!poss
|| !cmpl_is_a_completion (poss
))
3800 cmpl_len
= strlen (cmpl_this_completion (poss
));
3802 if (cmpl_state
->updated_text_alloc
< cmpl_len
+ 1)
3804 cmpl_state
->updated_text
=
3805 (gchar
*)g_realloc (cmpl_state
->updated_text
,
3806 cmpl_state
->updated_text_alloc
);
3807 cmpl_state
->updated_text_alloc
= 2*cmpl_len
;
3810 if (cmpl_state
->updated_text_len
< 0)
3812 strcpy (cmpl_state
->updated_text
, cmpl_this_completion (poss
));
3813 cmpl_state
->updated_text_len
= cmpl_len
;
3814 cmpl_state
->re_complete
= cmpl_is_directory (poss
);
3816 else if (cmpl_state
->updated_text_len
== 0)
3818 cmpl_state
->re_complete
= FALSE
;
3823 first_diff_index (cmpl_state
->updated_text
,
3824 cmpl_this_completion (poss
));
3826 cmpl_state
->re_complete
= FALSE
;
3828 if (first_diff
== PATTERN_MATCH
)
3831 if (first_diff
> cmpl_state
->updated_text_len
)
3832 strcpy (cmpl_state
->updated_text
, cmpl_this_completion (poss
));
3834 cmpl_state
->updated_text_len
= first_diff
;
3835 cmpl_state
->updated_text
[first_diff
] = 0;
3839 static PossibleCompletion
*
3840 attempt_dir_completion (CompletionState
*cmpl_state
)
3842 gchar
*pat_buf
, *first_slash
;
3843 CompletionDir
*dir
= cmpl_state
->active_completion_dir
;
3844 GtkFileFilter
*filter
= gtk_file_filter_new ();
3845 GtkFileFilterInfo filter_info
= {GTK_FILE_FILTER_FILENAME
,};
3847 dir
->cmpl_index
+= 1;
3849 if (dir
->cmpl_index
== dir
->sent
->entry_count
)
3851 if (dir
->cmpl_parent
== NULL
)
3853 cmpl_state
->active_completion_dir
= NULL
;
3859 cmpl_state
->active_completion_dir
= dir
->cmpl_parent
;
3861 return attempt_dir_completion (cmpl_state
);
3865 g_assert (dir
->cmpl_text
);
3867 first_slash
= strchr (dir
->cmpl_text
, G_DIR_SEPARATOR
);
3871 gint len
= first_slash
- dir
->cmpl_text
;
3873 pat_buf
= g_new (gchar
, len
+ 1);
3874 strncpy (pat_buf
, dir
->cmpl_text
, len
);
3879 gint len
= strlen (dir
->cmpl_text
);
3881 pat_buf
= g_new (gchar
, len
+ 2);
3882 strcpy (pat_buf
, dir
->cmpl_text
);
3883 /* Don't append a * if the user entered one herself.
3884 * This way one can complete *.h and don't get matches
3885 * on any .help files, for instance.
3887 if (strchr (pat_buf
, '*') == NULL
)
3888 strcpy (pat_buf
+ len
, "*");
3891 gtk_file_filter_add_pattern (filter
, pat_buf
);
3895 if (dir
->sent
->entries
[dir
->cmpl_index
].is_dir
)
3897 filter_info
.filename
= dir
->sent
->entries
[dir
->cmpl_index
].entry_name
;
3898 if(gtk_file_filter_filter(filter
, &filter_info
))
3900 CompletionDir
* new_dir
;
3902 new_dir
= open_relative_dir (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
,
3911 new_dir
->cmpl_parent
= dir
;
3913 new_dir
->cmpl_index
= -1;
3914 new_dir
->cmpl_text
= g_strdup (first_slash
+ 1);
3916 cmpl_state
->active_completion_dir
= new_dir
;
3919 return attempt_dir_completion (cmpl_state
);
3924 return attempt_dir_completion (cmpl_state
);
3930 return attempt_dir_completion (cmpl_state
);
3935 if (dir
->cmpl_parent
!= NULL
)
3937 append_completion_text (dir
->fullname
+
3938 strlen (cmpl_state
->completion_dir
->fullname
) + 1,
3940 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3943 append_completion_text (dir
->sent
->entries
[dir
->cmpl_index
].entry_name
, cmpl_state
);
3945 filter_info
.filename
= dir
->sent
->entries
[dir
->cmpl_index
].entry_name
;
3946 cmpl_state
->the_completion
.is_a_completion
=
3947 gtk_file_filter_filter(filter
, &filter_info
);
3949 cmpl_state
->the_completion
.is_directory
= dir
->sent
->entries
[dir
->cmpl_index
].is_dir
;
3950 if (dir
->sent
->entries
[dir
->cmpl_index
].is_dir
)
3951 append_completion_text (G_DIR_SEPARATOR_S
, cmpl_state
);
3954 return &cmpl_state
->the_completion
;
3961 get_pwdb (CompletionState
* cmpl_state
)
3963 struct passwd
*pwd_ptr
;
3966 gint len
= 0, i
, count
= 0;
3968 if (cmpl_state
->user_dir_name_buffer
)
3972 while ((pwd_ptr
= getpwent ()) != NULL
)
3974 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
3975 len
+= strlen (utf8
);
3977 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
3978 len
+= strlen (utf8
);
3986 cmpl_state
->user_dir_name_buffer
= g_new (gchar
, len
);
3987 cmpl_state
->user_directories
= g_new (CompletionUserDir
, count
);
3988 cmpl_state
->user_directories_len
= count
;
3990 buf_ptr
= cmpl_state
->user_dir_name_buffer
;
3992 for (i
= 0; i
< count
; i
+= 1)
3994 pwd_ptr
= getpwent ();
4001 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_name
, -1, NULL
, NULL
, NULL
);
4002 strcpy (buf_ptr
, utf8
);
4005 cmpl_state
->user_directories
[i
].login
= buf_ptr
;
4007 buf_ptr
+= strlen (buf_ptr
);
4010 utf8
= g_filename_to_utf8 (pwd_ptr
->pw_dir
, -1, NULL
, NULL
, NULL
);
4011 strcpy (buf_ptr
, utf8
);
4014 cmpl_state
->user_directories
[i
].homedir
= buf_ptr
;
4016 buf_ptr
+= strlen (buf_ptr
);
4020 qsort (cmpl_state
->user_directories
,
4021 cmpl_state
->user_directories_len
,
4022 sizeof (CompletionUserDir
),
4031 if (cmpl_state
->user_dir_name_buffer
)
4032 g_free (cmpl_state
->user_dir_name_buffer
);
4033 if (cmpl_state
->user_directories
)
4034 g_free (cmpl_state
->user_directories
);
4036 cmpl_state
->user_dir_name_buffer
= NULL
;
4037 cmpl_state
->user_directories
= NULL
;
4043 compare_user_dir (const void *a
,
4046 return strcmp ((((CompletionUserDir
*)a
))->login
,
4047 (((CompletionUserDir
*)b
))->login
);
4053 compare_cmpl_dir (const void *a
,
4057 return strcmp (((const CompletionDirEntry
*)a
)->sort_key
,
4058 (((const CompletionDirEntry
*)b
))->sort_key
);
4062 cmpl_state_okay (CompletionState
* cmpl_state
)
4064 return cmpl_state
&& cmpl_state
->reference_dir
;
4068 cmpl_strerror (gint err
)
4070 if (err
== CMPL_ERRNO_TOO_LONG
)
4071 return _("Name too long");
4072 else if (err
== CMPL_ERRNO_DID_NOT_CONVERT
)
4073 return _("Couldn't convert filename");
4075 return g_strerror (err
);
4078 const gchar
* gtk_dir_selection_get_dir (GtkDirSelection
*filesel
)
4080 return gtk_entry_get_text (GTK_ENTRY (filesel
->selection_entry
));