1
/* GTK - The GIMP Toolkit
2
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
21
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22
* file for a list of people on the GTK+ Team. See the ChangeLog
23
* files for a list of changes. These files are distributed with
24
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30
#include <sys/types.h>
32
#ifdef HAVE_SYS_PARAM_H
33
#include <sys/param.h>
45
#include <glib.h> /* Include early to get G_OS_WIN32 etc */
46
#include <glib/gstdio.h>
48
#if defined(G_PLATFORM_WIN32)
53
#endif /* G_PLATFORM_WIN32 */
55
#include "gdk/gdkkeysyms.h"
56
#include "gtkbutton.h"
57
#include "gtkcellrenderertext.h"
59
#include "gtkfilesel.h"
64
#include "gtkliststore.h"
66
#include "gtkprivate.h"
67
#include "gtkscrolledwindow.h"
69
#include "gtktreeselection.h"
70
#include "gtktreeview.h"
73
#include "gtkmenuitem.h"
74
#include "gtkdialog.h"
75
#include "gtkmessagedialog.h"
77
#include "gtkeventbox.h"
79
#undef GTK_DISABLE_DEPRECATED
80
#include "gtkoptionmenu.h"
83
#include "gtkhpaned.h"
91
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
93
#endif /* G_OS_WIN32 */
96
#include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
99
#define DIR_LIST_WIDTH 180
100
#define DIR_LIST_HEIGHT 180
101
#define FILE_LIST_WIDTH 180
102
#define FILE_LIST_HEIGHT 180
104
/* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
105
* in here, since the rest of the code in the file does require some
110
# define MAXPATHLEN PATH_MAX
112
# define MAXPATHLEN 2048
116
/* I've put this here so it doesn't get confused with the
117
* file completion interface */
118
typedef struct _HistoryCallbackArg HistoryCallbackArg;
120
struct _HistoryCallbackArg
123
GtkWidget *menu_item;
127
typedef struct _CompletionState CompletionState;
128
typedef struct _CompletionDir CompletionDir;
129
typedef struct _CompletionDirSent CompletionDirSent;
130
typedef struct _CompletionDirEntry CompletionDirEntry;
131
typedef struct _CompletionUserDir CompletionUserDir;
132
typedef struct _PossibleCompletion PossibleCompletion;
134
/* Non-external file completion decls and structures */
136
/* A contant telling PRCS how many directories to cache. Its actually
137
* kept in a list, so the geometry isn't important. */
138
#define CMPL_DIRECTORY_CACHE_SIZE 10
140
/* A constant used to determine whether a substring was an exact
141
* match by first_diff_index()
143
#define PATTERN_MATCH -1
144
#define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
145
#define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
147
/* This structure contains all the useful information about a directory
148
* for the purposes of filename completion. These structures are cached
149
* in the CompletionState struct. CompletionDir's are reference counted.
151
struct _CompletionDirSent
153
#ifndef G_PLATFORM_WIN32
160
struct _CompletionDirEntry *entries;
163
struct _CompletionDir
165
CompletionDirSent *sent;
170
struct _CompletionDir *cmpl_parent;
175
/* This structure contains pairs of directory entry names with a flag saying
176
* whether or not they are a valid directory. NOTE: This information is used
177
* to provide the caller with information about whether to update its completions
178
* or try to open a file. Since directories are cached by the directory mtime,
179
* a symlink which points to an invalid file (which will not be a directory),
180
* will not be reevaluated if that file is created, unless the containing
181
* directory is touched. I consider this case to be worth ignoring (josh).
183
struct _CompletionDirEntry
190
struct _CompletionUserDir
196
struct _PossibleCompletion
198
/* accessible fields, all are accessed externally by functions
202
gint is_a_completion;
203
gboolean is_directory;
210
struct _CompletionState
212
gint last_valid_char;
214
gint updated_text_len;
215
gint updated_text_alloc;
216
gboolean re_complete;
218
gchar *user_dir_name_buffer;
219
gint user_directories_len;
221
gchar *last_completion_text;
223
gint user_completion_index; /* if >= 0, currently completing ~user */
225
struct _CompletionDir *completion_dir; /* directory completing from */
226
struct _CompletionDir *active_completion_dir;
228
struct _PossibleCompletion the_completion;
230
struct _CompletionDir *reference_dir; /* initial directory */
232
GList* directory_storage;
233
GList* directory_sent_storage;
235
struct _CompletionUserDir *user_directories;
253
/* File completion functions which would be external, were they used
254
* outside of this file.
257
static CompletionState* cmpl_init_state (void);
258
static void cmpl_free_state (CompletionState *cmpl_state);
259
static gint cmpl_state_okay (CompletionState* cmpl_state);
260
static const gchar* cmpl_strerror (gint);
262
static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
263
gchar **remaining_text,
264
CompletionState *cmpl_state);
266
/* Returns a name for consideration, possibly a completion, this name
267
* will be invalid after the next call to cmpl_next_completion.
269
static char* cmpl_this_completion (PossibleCompletion*);
271
/* True if this completion matches the given text. Otherwise, this
272
* output can be used to have a list of non-completions.
274
static gint cmpl_is_a_completion (PossibleCompletion*);
276
/* True if the completion is a directory
278
static gboolean cmpl_is_directory (PossibleCompletion*);
280
/* Obtains the next completion, or NULL
282
static PossibleCompletion* cmpl_next_completion (CompletionState*);
284
/* Updating completions: the return value of cmpl_updated_text() will
285
* be text_to_complete completed as much as possible after the most
286
* recent call to cmpl_completion_matches. For the present
287
* application, this is the suggested replacement for the user's input
288
* string. You must CALL THIS AFTER ALL cmpl_text_completions have
291
static gchar* cmpl_updated_text (CompletionState* cmpl_state);
293
/* After updating, to see if the completion was a directory, call
294
* this. If it was, you should consider re-calling completion_matches.
296
static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
298
/* Current location: if using file completion, return the current
299
* directory, from which file completion begins. More specifically,
300
* the cwd concatenated with all exact completions up to the last
301
* directory delimiter('/').
303
static gchar* cmpl_reference_position (CompletionState* cmpl_state);
306
/* This doesn't work currently and would require changes
307
* to fnmatch.c to get working.
309
/* backing up: if cmpl_completion_matches returns NULL, you may query
310
* the index of the last completable character into cmpl_updated_text.
312
static gint cmpl_last_valid_char (CompletionState* cmpl_state);
315
/* When the user selects a non-directory, call cmpl_completion_fullname
316
* to get the full name of the selected file.
318
static gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
321
/* Directory operations. */
322
static CompletionDir* open_ref_dir (gchar* text_to_complete,
323
gchar** remaining_text,
324
CompletionState* cmpl_state);
325
#ifndef G_PLATFORM_WIN32
326
static gboolean check_dir (gchar *dir_name,
328
gboolean *stat_subdirs);
330
static CompletionDir* open_dir (gchar* dir_name,
331
CompletionState* cmpl_state);
333
static CompletionDir* open_user_dir (const gchar* text_to_complete,
334
CompletionState *cmpl_state);
336
static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
337
CompletionState *cmpl_state);
338
static CompletionDirSent* open_new_dir (gchar* dir_name,
340
gboolean stat_subdirs);
341
static gint correct_dir_fullname (CompletionDir* cmpl_dir);
342
static gint correct_parent (CompletionDir* cmpl_dir,
344
#ifndef G_PLATFORM_WIN32
345
static gchar* find_parent_dir_fullname (gchar* dirname);
347
static CompletionDir* attach_dir (CompletionDirSent* sent,
349
CompletionState *cmpl_state);
350
static void free_dir_sent (CompletionDirSent* sent);
351
static void free_dir (CompletionDir *dir);
352
static void prune_memory_usage(CompletionState *cmpl_state);
354
/* Completion operations */
356
static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
357
CompletionState *cmpl_state);
359
static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
360
static CompletionDir* find_completion_dir(gchar* text_to_complete,
361
gchar** remaining_text,
362
CompletionState* cmpl_state);
363
static PossibleCompletion* append_completion_text(gchar* text,
364
CompletionState* cmpl_state);
366
static gint get_pwdb(CompletionState* cmpl_state);
367
static gint compare_user_dir(const void* a, const void* b);
369
static gint first_diff_index(gchar* pat, gchar* text);
370
static gint compare_cmpl_dir(const void* a, const void* b);
371
static void update_cmpl(PossibleCompletion* poss,
372
CompletionState* cmpl_state);
374
static void gtk_file_selection_set_property (GObject *object,
378
static void gtk_file_selection_get_property (GObject *object,
382
static void gtk_file_selection_finalize (GObject *object);
383
static void gtk_file_selection_destroy (GtkObject *object);
384
static void gtk_file_selection_map (GtkWidget *widget);
385
static gint gtk_file_selection_key_press (GtkWidget *widget,
388
static gint gtk_file_selection_insert_text (GtkWidget *widget,
389
const gchar *new_text,
390
gint new_text_length,
393
static void gtk_file_selection_update_fileops (GtkFileSelection *filesel);
395
static void gtk_file_selection_file_activate (GtkTreeView *tree_view,
397
GtkTreeViewColumn *column,
399
static void gtk_file_selection_file_changed (GtkTreeSelection *selection,
401
static void gtk_file_selection_dir_activate (GtkTreeView *tree_view,
403
GtkTreeViewColumn *column,
406
static void gtk_file_selection_populate (GtkFileSelection *fs,
408
gboolean try_complete,
409
gboolean reset_entry);
410
static void gtk_file_selection_abort (GtkFileSelection *fs);
412
static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
415
static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
416
static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
417
static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
419
static void free_selected_names (GPtrArray *names);
421
#ifndef G_PLATFORM_WIN32
423
#define compare_utf8_filenames(a, b) strcmp(a, b)
424
#define compare_sys_filenames(a, b) strcmp(a, b)
429
compare_utf8_filenames (const gchar *a,
432
gchar *a_folded, *b_folded;
435
a_folded = g_utf8_strdown (a, -1);
436
b_folded = g_utf8_strdown (b, -1);
438
retval = strcmp (a_folded, b_folded);
447
compare_sys_filenames (const gchar *a,
450
gchar *a_utf8, *b_utf8;
453
a_utf8 = g_filename_to_utf8 (a, -1, NULL, NULL, NULL);
454
b_utf8 = g_filename_to_utf8 (b, -1, NULL, NULL, NULL);
456
retval = compare_utf8_filenames (a_utf8, b_utf8);
466
/* Saves errno when something cmpl does fails. */
467
static gint cmpl_errno;
471
* Take the path currently in the file selection
472
* entry field and translate as necessary from
473
* a Win32 style to Cygwin style path. For
474
* instance translate:
475
* x:\somepath\file.jpg
477
* /cygdrive/x/somepath/file.jpg
479
* Replace the path in the selection text field.
480
* Return a boolean value concerning whether a
481
* translation had to be made.
484
translate_win32_path (GtkFileSelection *filesel)
488
gchar newPath[MAX_PATH];
491
* Retrieve the current path
493
path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
495
cygwin_conv_to_posix_path (path, newPath);
496
updated = (strcmp (path, newPath) != 0);
499
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
505
G_DEFINE_TYPE (GtkFileSelection, gtk_file_selection, GTK_TYPE_DIALOG)
508
gtk_file_selection_class_init (GtkFileSelectionClass *class)
510
GObjectClass *gobject_class;
511
GtkObjectClass *object_class;
512
GtkWidgetClass *widget_class;
514
gobject_class = (GObjectClass*) class;
515
object_class = (GtkObjectClass*) class;
516
widget_class = (GtkWidgetClass*) class;
518
gobject_class->finalize = gtk_file_selection_finalize;
519
gobject_class->set_property = gtk_file_selection_set_property;
520
gobject_class->get_property = gtk_file_selection_get_property;
522
g_object_class_install_property (gobject_class,
524
g_param_spec_string ("filename",
526
P_("The currently selected filename"),
528
GTK_PARAM_READWRITE));
529
g_object_class_install_property (gobject_class,
531
g_param_spec_boolean ("show-fileops",
532
P_("Show file operations"),
533
P_("Whether buttons for creating/manipulating files should be displayed"),
535
GTK_PARAM_READWRITE));
536
g_object_class_install_property (gobject_class,
537
PROP_SELECT_MULTIPLE,
538
g_param_spec_boolean ("select-multiple",
539
P_("Select Multiple"),
540
P_("Whether to allow multiple files to be selected"),
542
GTK_PARAM_READWRITE));
543
object_class->destroy = gtk_file_selection_destroy;
544
widget_class->map = gtk_file_selection_map;
547
static void gtk_file_selection_set_property (GObject *object,
552
GtkFileSelection *filesel;
554
filesel = GTK_FILE_SELECTION (object);
559
gtk_file_selection_set_filename (filesel,
560
g_value_get_string (value));
562
case PROP_SHOW_FILEOPS:
563
if (g_value_get_boolean (value))
564
gtk_file_selection_show_fileop_buttons (filesel);
566
gtk_file_selection_hide_fileop_buttons (filesel);
568
case PROP_SELECT_MULTIPLE:
569
gtk_file_selection_set_select_multiple (filesel, g_value_get_boolean (value));
572
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577
static void gtk_file_selection_get_property (GObject *object,
582
GtkFileSelection *filesel;
584
filesel = GTK_FILE_SELECTION (object);
589
g_value_set_string (value,
590
gtk_file_selection_get_filename(filesel));
593
case PROP_SHOW_FILEOPS:
594
/* This is a little bit hacky, but doing otherwise would require
595
* adding a field to the object.
597
g_value_set_boolean (value, (filesel->fileop_c_dir &&
598
filesel->fileop_del_file &&
599
filesel->fileop_ren_file));
601
case PROP_SELECT_MULTIPLE:
602
g_value_set_boolean (value, gtk_file_selection_get_select_multiple (filesel));
605
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
611
grab_default (GtkWidget *widget)
613
gtk_widget_grab_default (widget);
618
gtk_file_selection_init (GtkFileSelection *filesel)
620
GtkWidget *entry_vbox;
622
GtkWidget *list_hbox, *list_container;
623
GtkWidget *pulldown_hbox;
624
GtkWidget *scrolled_win;
630
GtkTreeViewColumn *column;
632
gtk_widget_push_composite_child ();
634
dialog = GTK_DIALOG (filesel);
636
filesel->cmpl_state = cmpl_init_state ();
638
/* The dialog-sized vertical box */
639
filesel->main_vbox = dialog->vbox;
640
gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
642
/* The horizontal box containing create, rename etc. buttons */
643
filesel->button_area = gtk_hbutton_box_new ();
644
gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
645
gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
646
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
648
gtk_widget_show (filesel->button_area);
650
gtk_file_selection_show_fileop_buttons (filesel);
652
/* hbox for pulldown menu */
653
pulldown_hbox = gtk_hbox_new (TRUE, 5);
654
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
655
gtk_widget_show (pulldown_hbox);
658
filesel->history_pulldown = gtk_option_menu_new ();
659
gtk_widget_show (filesel->history_pulldown);
660
gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
663
/* The horizontal box containing the directory and file listboxes */
665
spacer = gtk_hbox_new (FALSE, 0);
666
gtk_widget_set_size_request (spacer, -1, 5);
667
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
668
gtk_widget_show (spacer);
670
list_hbox = gtk_hbox_new (FALSE, 5);
671
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
672
gtk_widget_show (list_hbox);
674
list_container = g_object_new (GTK_TYPE_HPANED,
680
list_container = list_hbox;
682
spacer = gtk_hbox_new (FALSE, 0);
683
gtk_widget_set_size_request (spacer, -1, 5);
684
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
685
gtk_widget_show (spacer);
687
/* The directories list */
689
model = gtk_list_store_new (1, G_TYPE_STRING);
690
filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
691
g_object_unref (model);
693
column = gtk_tree_view_column_new_with_attributes (_("Folders"),
694
gtk_cell_renderer_text_new (),
697
label = gtk_label_new_with_mnemonic (_("Fol_ders"));
698
gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
699
gtk_widget_show (label);
700
gtk_tree_view_column_set_widget (column, label);
701
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
702
gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
704
gtk_widget_set_size_request (filesel->dir_list,
705
DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
706
g_signal_connect (filesel->dir_list, "row_activated",
707
G_CALLBACK (gtk_file_selection_dir_activate), filesel);
709
/* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
711
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
712
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
713
gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
714
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
715
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
716
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
717
if (GTK_IS_PANED (list_container))
718
gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
720
gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
721
gtk_widget_show (filesel->dir_list);
722
gtk_widget_show (scrolled_win);
725
model = gtk_list_store_new (1, G_TYPE_STRING);
726
filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
727
g_object_unref (model);
729
column = gtk_tree_view_column_new_with_attributes (_("Files"),
730
gtk_cell_renderer_text_new (),
733
label = gtk_label_new_with_mnemonic (_("_Files"));
734
gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
735
gtk_widget_show (label);
736
gtk_tree_view_column_set_widget (column, label);
737
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
738
gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
740
gtk_widget_set_size_request (filesel->file_list,
741
FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
742
g_signal_connect (filesel->file_list, "row_activated",
743
G_CALLBACK (gtk_file_selection_file_activate), filesel);
744
g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
745
G_CALLBACK (gtk_file_selection_file_changed), filesel);
747
/* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
749
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
750
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
751
gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
752
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
753
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
754
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
755
gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
756
gtk_widget_show (filesel->file_list);
757
gtk_widget_show (scrolled_win);
759
/* action area for packing buttons into. */
760
filesel->action_area = gtk_hbox_new (TRUE, 0);
761
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
763
gtk_widget_show (filesel->action_area);
765
/* The OK/Cancel button area */
767
/* The Cancel button */
768
filesel->cancel_button = gtk_dialog_add_button (dialog,
770
GTK_RESPONSE_CANCEL);
772
filesel->ok_button = gtk_dialog_add_button (dialog,
776
gtk_dialog_set_alternative_button_order (dialog,
781
gtk_widget_grab_default (filesel->ok_button);
783
/* The selection entry widget */
784
entry_vbox = gtk_vbox_new (FALSE, 2);
785
gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
786
gtk_widget_show (entry_vbox);
788
eventbox = gtk_event_box_new ();
789
filesel->selection_text = label = gtk_label_new ("");
790
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
791
gtk_container_add (GTK_CONTAINER (eventbox), label);
792
gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
793
gtk_widget_show (label);
794
gtk_widget_show (eventbox);
796
filesel->selection_entry = gtk_entry_new ();
797
g_signal_connect (filesel->selection_entry, "key_press_event",
798
G_CALLBACK (gtk_file_selection_key_press), filesel);
799
g_signal_connect (filesel->selection_entry, "insert_text",
800
G_CALLBACK (gtk_file_selection_insert_text), NULL);
801
g_signal_connect_swapped (filesel->selection_entry, "changed",
802
G_CALLBACK (gtk_file_selection_update_fileops), filesel);
803
g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
804
G_CALLBACK (grab_default),
806
g_signal_connect_swapped (filesel->selection_entry, "activate",
807
G_CALLBACK (gtk_button_clicked),
810
gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
811
gtk_widget_show (filesel->selection_entry);
813
gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
814
filesel->selection_entry);
816
if (!cmpl_state_okay (filesel->cmpl_state))
820
g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
822
gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
826
gtk_file_selection_populate (filesel, "", FALSE, TRUE);
829
gtk_widget_grab_focus (filesel->selection_entry);
831
gtk_widget_pop_composite_child ();
835
uri_list_extract_first_uri (const gchar* uri_list)
839
g_return_val_if_fail (uri_list != NULL, NULL);
842
/* We don't actually try to validate the URI according to RFC
843
* 2396, or even check for allowed characters - we just ignore
844
* comments and trim whitespace off the ends. We also
845
* allow LF delimination as well as the specified CRLF.
847
* We do allow comments like specified in RFC 2483.
853
while (g_ascii_isspace(*p))
857
while (*q && (*q != '\n') && (*q != '\r'))
863
while (q > p && g_ascii_isspace (*q))
867
return g_strndup (p, q - p + 1);
870
p = strchr (p, '\n');
878
dnd_really_drop (GtkWidget *dialog, gint response_id, GtkFileSelection *fs)
882
if (response_id == GTK_RESPONSE_YES)
884
filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
886
gtk_file_selection_set_filename (fs, filename);
889
gtk_widget_destroy (dialog);
894
filenames_dropped (GtkWidget *widget,
895
GdkDragContext *context,
898
GtkSelectionData *selection_data,
903
char *filename = NULL;
905
const char *this_hostname;
906
GError *error = NULL;
908
if (!selection_data->data)
911
uri = uri_list_extract_first_uri ((char *)selection_data->data);
916
filename = g_filename_from_uri (uri, &hostname, &error);
921
g_warning ("Error getting dropped filename: %s\n",
923
g_error_free (error);
927
this_hostname = g_get_host_name ();
929
if ((hostname == NULL) ||
930
(strcmp (hostname, this_hostname) == 0) ||
931
(strcmp (hostname, "localhost") == 0))
932
gtk_file_selection_set_filename (GTK_FILE_SELECTION (widget),
937
gchar *filename_utf8;
939
/* Conversion back to UTF-8 should always succeed for the result
940
* of g_filename_from_uri()
942
filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
943
g_assert (filename_utf8);
945
dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
946
GTK_DIALOG_DESTROY_WITH_PARENT,
947
GTK_MESSAGE_QUESTION,
949
_("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
950
"Are you sure that you want to select it?"), filename_utf8, hostname);
951
g_free (filename_utf8);
953
g_object_set_data_full (G_OBJECT (dialog), I_("gtk-fs-dnd-filename"), g_strdup (filename), g_free);
955
g_signal_connect_data (dialog, "response",
956
(GCallback) dnd_really_drop,
959
gtk_widget_show (dialog);
977
filenames_drag_get (GtkWidget *widget,
978
GdkDragContext *context,
979
GtkSelectionData *selection_data,
982
GtkFileSelection *filesel)
986
const char *hostname;
989
file = gtk_file_selection_get_filename (filesel);
993
if (info == TARGET_URILIST)
995
hostname = g_get_host_name ();
998
uri_list = g_filename_to_uri (file, hostname, &error);
1001
g_warning ("Error getting filename: %s\n",
1003
g_error_free (error);
1007
gtk_selection_data_set (selection_data,
1008
selection_data->target, 8,
1009
(void *)uri_list, strlen((char *)uri_list));
1014
gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
1015
g_assert (filename_utf8);
1016
gtk_selection_data_set_text (selection_data, filename_utf8, -1);
1017
g_free (filename_utf8);
1023
file_selection_setup_dnd (GtkFileSelection *filesel)
1025
GtkWidget *eventbox;
1026
static const GtkTargetEntry drop_types[] = {
1027
{ "text/uri-list", 0, TARGET_URILIST}
1029
static const gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
1030
static const GtkTargetEntry drag_types[] = {
1031
{ "text/uri-list", 0, TARGET_URILIST},
1032
{ "UTF8_STRING", 0, TARGET_UTF8_STRING },
1035
{ "COMPOUND_TEXT", 0, 0 }
1037
static const gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
1039
gtk_drag_dest_set (GTK_WIDGET (filesel),
1040
GTK_DEST_DEFAULT_ALL,
1041
drop_types, n_drop_types,
1044
g_signal_connect (filesel, "drag_data_received",
1045
G_CALLBACK (filenames_dropped), NULL);
1047
eventbox = gtk_widget_get_parent (filesel->selection_text);
1048
gtk_drag_source_set (eventbox,
1050
drag_types, n_drag_types,
1053
g_signal_connect (eventbox, "drag_data_get",
1054
G_CALLBACK (filenames_drag_get), filesel);
1058
gtk_file_selection_new (const gchar *title)
1060
GtkFileSelection *filesel;
1062
filesel = g_object_new (GTK_TYPE_FILE_SELECTION, NULL);
1063
gtk_window_set_title (GTK_WINDOW (filesel), title);
1064
gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1066
file_selection_setup_dnd (filesel);
1068
return GTK_WIDGET (filesel);
1072
gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
1074
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1076
/* delete, create directory, and rename */
1077
if (!filesel->fileop_c_dir)
1079
filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1080
g_signal_connect (filesel->fileop_c_dir, "clicked",
1081
G_CALLBACK (gtk_file_selection_create_dir),
1083
gtk_box_pack_start (GTK_BOX (filesel->button_area),
1084
filesel->fileop_c_dir, TRUE, TRUE, 0);
1085
gtk_widget_show (filesel->fileop_c_dir);
1088
if (!filesel->fileop_del_file)
1090
filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1091
g_signal_connect (filesel->fileop_del_file, "clicked",
1092
G_CALLBACK (gtk_file_selection_delete_file),
1094
gtk_box_pack_start (GTK_BOX (filesel->button_area),
1095
filesel->fileop_del_file, TRUE, TRUE, 0);
1096
gtk_widget_show (filesel->fileop_del_file);
1099
if (!filesel->fileop_ren_file)
1101
filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1102
g_signal_connect (filesel->fileop_ren_file, "clicked",
1103
G_CALLBACK (gtk_file_selection_rename_file),
1105
gtk_box_pack_start (GTK_BOX (filesel->button_area),
1106
filesel->fileop_ren_file, TRUE, TRUE, 0);
1107
gtk_widget_show (filesel->fileop_ren_file);
1110
gtk_file_selection_update_fileops (filesel);
1112
g_object_notify (G_OBJECT (filesel), "show-fileops");
1116
gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
1118
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1120
if (filesel->fileop_ren_file)
1122
gtk_widget_destroy (filesel->fileop_ren_file);
1123
filesel->fileop_ren_file = NULL;
1126
if (filesel->fileop_del_file)
1128
gtk_widget_destroy (filesel->fileop_del_file);
1129
filesel->fileop_del_file = NULL;
1132
if (filesel->fileop_c_dir)
1134
gtk_widget_destroy (filesel->fileop_c_dir);
1135
filesel->fileop_c_dir = NULL;
1137
g_object_notify (G_OBJECT (filesel), "show-fileops");
1143
* gtk_file_selection_set_filename:
1144
* @filesel: a #GtkFileSelection.
1145
* @filename: a string to set as the default file name.
1147
* Sets a default path for the file requestor. If @filename includes a
1148
* directory path, then the requestor will open with that path as its
1149
* current working directory.
1151
* This has the consequence that in order to open the requestor with a
1152
* working directory and an empty filename, @filename must have a trailing
1153
* directory separator.
1155
* The encoding of @filename is preferred GLib file name encoding, which
1156
* may not be UTF-8. See g_filename_from_utf8().
1159
gtk_file_selection_set_filename (GtkFileSelection *filesel,
1160
const gchar *filename)
1163
const char *name, *last_slash;
1164
char *filename_utf8;
1166
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1167
g_return_if_fail (filename != NULL);
1169
filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1170
g_return_if_fail (filename_utf8 != NULL);
1172
last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1176
buf = g_strdup ("");
1177
name = filename_utf8;
1181
buf = g_strdup (filename_utf8);
1182
buf[last_slash - filename_utf8 + 1] = 0;
1183
name = last_slash + 1;
1186
gtk_file_selection_populate (filesel, buf, FALSE, TRUE);
1188
if (filesel->selection_entry)
1189
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1191
g_object_notify (G_OBJECT (filesel), "filename");
1193
g_free (filename_utf8);
1197
* gtk_file_selection_get_filename:
1198
* @filesel: a #GtkFileSelection
1200
* This function returns the selected filename in the GLib file name
1201
* encoding. To convert to UTF-8, call g_filename_to_utf8(). The
1202
* returned string points to a statically allocated buffer and should
1203
* be copied if you plan to keep it around.
1205
* If no file is selected then the selected directory path is returned.
1207
* Return value: currently-selected filename in the on-disk encoding.
1209
G_CONST_RETURN gchar*
1210
gtk_file_selection_get_filename (GtkFileSelection *filesel)
1212
static const gchar nothing[2] = "";
1213
static GString *something;
1217
g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
1219
#ifdef G_WITH_CYGWIN
1220
translate_win32_path (filesel);
1222
text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1225
gchar *fullname = cmpl_completion_fullname (text, filesel->cmpl_state);
1226
sys_filename = g_filename_from_utf8 (fullname, -1, NULL, NULL, NULL);
1231
something = g_string_new (sys_filename);
1233
g_string_assign (something, sys_filename);
1234
g_free (sys_filename);
1236
return something->str;
1243
gtk_file_selection_complete (GtkFileSelection *filesel,
1244
const gchar *pattern)
1246
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
1247
g_return_if_fail (pattern != NULL);
1249
if (filesel->selection_entry)
1250
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1251
gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1255
gtk_file_selection_destroy (GtkObject *object)
1257
GtkFileSelection *filesel;
1259
HistoryCallbackArg *callback_arg;
1261
g_return_if_fail (GTK_IS_FILE_SELECTION (object));
1263
filesel = GTK_FILE_SELECTION (object);
1265
if (filesel->fileop_dialog)
1267
gtk_widget_destroy (filesel->fileop_dialog);
1268
filesel->fileop_dialog = NULL;
1271
if (filesel->history_list)
1273
list = filesel->history_list;
1276
callback_arg = list->data;
1277
g_free (callback_arg->directory);
1278
g_free (callback_arg);
1281
g_list_free (filesel->history_list);
1282
filesel->history_list = NULL;
1285
if (filesel->cmpl_state)
1287
cmpl_free_state (filesel->cmpl_state);
1288
filesel->cmpl_state = NULL;
1291
if (filesel->selected_names)
1293
free_selected_names (filesel->selected_names);
1294
filesel->selected_names = NULL;
1297
if (filesel->last_selected)
1299
g_free (filesel->last_selected);
1300
filesel->last_selected = NULL;
1303
GTK_OBJECT_CLASS (gtk_file_selection_parent_class)->destroy (object);
1307
gtk_file_selection_map (GtkWidget *widget)
1309
GtkFileSelection *filesel = GTK_FILE_SELECTION (widget);
1311
/* Refresh the contents */
1312
gtk_file_selection_populate (filesel, "", FALSE, FALSE);
1314
GTK_WIDGET_CLASS (gtk_file_selection_parent_class)->map (widget);
1318
gtk_file_selection_finalize (GObject *object)
1320
GtkFileSelection *filesel = GTK_FILE_SELECTION (object);
1322
g_free (filesel->fileop_file);
1324
G_OBJECT_CLASS (gtk_file_selection_parent_class)->finalize (object);
1327
/* Begin file operations callbacks */
1330
gtk_file_selection_fileop_error (GtkFileSelection *fs,
1331
gchar *error_message)
1335
g_return_if_fail (error_message != NULL);
1338
dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1339
GTK_DIALOG_DESTROY_WITH_PARENT,
1342
"%s", error_message);
1344
/* yes, we free it */
1345
g_free (error_message);
1347
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1349
g_signal_connect_swapped (dialog, "response",
1350
G_CALLBACK (gtk_widget_destroy),
1353
gtk_widget_show (dialog);
1357
gtk_file_selection_fileop_destroy (GtkWidget *widget,
1360
GtkFileSelection *fs = data;
1362
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1364
fs->fileop_dialog = NULL;
1368
entry_is_empty (GtkEntry *entry)
1370
const gchar *text = gtk_entry_get_text (entry);
1372
return *text == '\0';
1376
gtk_file_selection_fileop_entry_changed (GtkEntry *entry,
1379
gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1383
gtk_file_selection_create_dir_confirmed (GtkWidget *widget,
1386
GtkFileSelection *fs = data;
1387
const gchar *dirname;
1390
gchar *sys_full_path;
1392
GError *error = NULL;
1393
CompletionState *cmpl_state;
1395
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1397
dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1398
cmpl_state = (CompletionState*) fs->cmpl_state;
1399
path = cmpl_reference_position (cmpl_state);
1401
full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1402
sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1405
if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1406
buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1408
buf = g_strdup_printf (_("Error creating directory '%s': %s"),
1409
dirname, error->message);
1410
gtk_file_selection_fileop_error (fs, buf);
1411
g_error_free (error);
1415
if (g_mkdir (sys_full_path, 0777) < 0)
1417
buf = g_strdup_printf (_("Error creating directory '%s': %s"),
1418
dirname, g_strerror (errno));
1419
gtk_file_selection_fileop_error (fs, buf);
1424
g_free (sys_full_path);
1426
gtk_widget_destroy (fs->fileop_dialog);
1427
gtk_file_selection_populate (fs, "", FALSE, FALSE);
1431
gtk_file_selection_create_dir (GtkWidget *widget,
1434
GtkFileSelection *fs = data;
1440
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1442
if (fs->fileop_dialog)
1446
dialog = gtk_dialog_new ();
1447
fs->fileop_dialog = dialog;
1448
g_signal_connect (dialog, "destroy",
1449
G_CALLBACK (gtk_file_selection_fileop_destroy),
1451
gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1452
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1453
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1455
/* If file dialog is grabbed, grab option dialog */
1456
/* When option dialog is closed, file dialog will be grabbed again */
1457
if (GTK_WINDOW (fs)->modal)
1458
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1460
vbox = gtk_vbox_new (FALSE, 0);
1461
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1462
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1464
gtk_widget_show( vbox);
1466
label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1467
gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1468
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1469
gtk_widget_show (label);
1471
/* The directory entry widget */
1472
fs->fileop_entry = gtk_entry_new ();
1473
gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1474
gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1476
GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1477
gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1478
gtk_widget_show (fs->fileop_entry);
1481
button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1482
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1483
g_signal_connect_swapped (button, "clicked",
1484
G_CALLBACK (gtk_widget_destroy),
1487
gtk_widget_grab_focus (fs->fileop_entry);
1489
button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1490
_("C_reate"), GTK_RESPONSE_OK);
1491
gtk_widget_set_sensitive (button, FALSE);
1492
g_signal_connect (button, "clicked",
1493
G_CALLBACK (gtk_file_selection_create_dir_confirmed),
1495
g_signal_connect (fs->fileop_entry, "changed",
1496
G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1499
gtk_widget_grab_default (button);
1501
gtk_widget_show (dialog);
1505
gtk_file_selection_delete_file_response (GtkDialog *dialog,
1509
GtkFileSelection *fs = data;
1510
CompletionState *cmpl_state;
1513
gchar *sys_full_path;
1514
GError *error = NULL;
1517
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1519
if (response_id != GTK_RESPONSE_OK)
1521
gtk_widget_destroy (GTK_WIDGET (dialog));
1525
cmpl_state = (CompletionState*) fs->cmpl_state;
1526
path = cmpl_reference_position (cmpl_state);
1528
full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1529
sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1532
if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1533
buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1536
buf = g_strdup_printf (_("Error deleting file '%s': %s"),
1537
fs->fileop_file, error->message);
1539
gtk_file_selection_fileop_error (fs, buf);
1540
g_error_free (error);
1544
if (g_unlink (sys_full_path) < 0)
1546
buf = g_strdup_printf (_("Error deleting file '%s': %s"),
1547
fs->fileop_file, g_strerror (errno));
1548
gtk_file_selection_fileop_error (fs, buf);
1553
g_free (sys_full_path);
1555
gtk_widget_destroy (fs->fileop_dialog);
1556
gtk_file_selection_populate (fs, "", FALSE, TRUE);
1560
gtk_file_selection_delete_file (GtkWidget *widget,
1563
GtkFileSelection *fs = data;
1565
const gchar *filename;
1567
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1569
if (fs->fileop_dialog)
1572
#ifdef G_WITH_CYGWIN
1573
translate_win32_path (fs);
1576
filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1577
if (strlen (filename) < 1)
1580
g_free (fs->fileop_file);
1581
fs->fileop_file = g_strdup (filename);
1584
fs->fileop_dialog = dialog =
1585
gtk_message_dialog_new (GTK_WINDOW (fs),
1586
GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1587
GTK_MESSAGE_QUESTION,
1589
_("Really delete file \"%s\"?"), filename);
1591
g_signal_connect (dialog, "destroy",
1592
G_CALLBACK (gtk_file_selection_fileop_destroy),
1594
gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1595
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1598
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1599
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1600
GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1603
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1605
g_signal_connect (dialog, "response",
1606
G_CALLBACK (gtk_file_selection_delete_file_response),
1609
gtk_widget_show (dialog);
1613
gtk_file_selection_rename_file_confirmed (GtkWidget *widget,
1616
GtkFileSelection *fs = data;
1620
gchar *new_filename;
1621
gchar *old_filename;
1622
gchar *sys_new_filename;
1623
gchar *sys_old_filename;
1624
CompletionState *cmpl_state;
1625
GError *error = NULL;
1627
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1629
file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1630
cmpl_state = (CompletionState*) fs->cmpl_state;
1631
path = cmpl_reference_position (cmpl_state);
1633
new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1634
old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1636
sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1639
if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1640
buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1642
buf = g_strdup_printf (_("Error renaming file to \"%s\": %s"),
1643
new_filename, error->message);
1644
gtk_file_selection_fileop_error (fs, buf);
1645
g_error_free (error);
1649
sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1652
if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1653
buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1655
buf = g_strdup_printf (_("Error renaming file \"%s\": %s"),
1656
old_filename, error->message);
1657
gtk_file_selection_fileop_error (fs, buf);
1658
g_error_free (error);
1662
if (g_rename (sys_old_filename, sys_new_filename) < 0)
1664
buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1665
sys_old_filename, sys_new_filename,
1666
g_strerror (errno));
1667
gtk_file_selection_fileop_error (fs, buf);
1671
gtk_file_selection_populate (fs, "", FALSE, FALSE);
1672
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1675
g_free (sys_old_filename);
1678
g_free (new_filename);
1679
g_free (old_filename);
1680
g_free (sys_new_filename);
1682
gtk_widget_destroy (fs->fileop_dialog);
1686
gtk_file_selection_rename_file (GtkWidget *widget,
1689
GtkFileSelection *fs = data;
1696
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1698
if (fs->fileop_dialog)
1701
g_free (fs->fileop_file);
1702
fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1703
if (strlen (fs->fileop_file) < 1)
1707
fs->fileop_dialog = dialog = gtk_dialog_new ();
1708
g_signal_connect (dialog, "destroy",
1709
G_CALLBACK (gtk_file_selection_fileop_destroy),
1711
gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1712
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1713
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1715
/* If file dialog is grabbed, grab option dialog */
1716
/* When option dialog closed, file dialog will be grabbed again */
1717
if (GTK_WINDOW (fs)->modal)
1718
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1720
vbox = gtk_vbox_new (FALSE, 0);
1721
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1722
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1724
gtk_widget_show(vbox);
1726
buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1727
label = gtk_label_new (buf);
1728
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1729
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1730
gtk_widget_show (label);
1733
/* New filename entry */
1734
fs->fileop_entry = gtk_entry_new ();
1735
gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1737
GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1738
gtk_entry_set_activates_default (GTK_ENTRY (fs->fileop_entry), TRUE);
1739
gtk_widget_show (fs->fileop_entry);
1741
gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1742
gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1743
0, strlen (fs->fileop_file));
1746
button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1747
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1748
g_signal_connect_swapped (button, "clicked",
1749
G_CALLBACK (gtk_widget_destroy),
1752
gtk_widget_grab_focus (fs->fileop_entry);
1754
button = gtk_dialog_add_button (GTK_DIALOG (dialog),
1755
_("_Rename"), GTK_RESPONSE_OK);
1756
g_signal_connect (button, "clicked",
1757
G_CALLBACK (gtk_file_selection_rename_file_confirmed),
1759
g_signal_connect (fs->fileop_entry, "changed",
1760
G_CALLBACK (gtk_file_selection_fileop_entry_changed),
1763
gtk_widget_grab_default (button);
1765
gtk_widget_show (dialog);
1769
gtk_file_selection_insert_text (GtkWidget *widget,
1770
const gchar *new_text,
1771
gint new_text_length,
1777
filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1781
gdk_display_beep (gtk_widget_get_display (widget));
1782
g_signal_stop_emission_by_name (widget, "insert_text");
1792
gtk_file_selection_update_fileops (GtkFileSelection *fs)
1796
if (!fs->selection_entry)
1799
sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1801
if (fs->fileop_del_file)
1802
gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1804
if (fs->fileop_ren_file)
1805
gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1809
gtk_file_selection_key_press (GtkWidget *widget,
1813
GtkFileSelection *fs;
1816
g_return_val_if_fail (widget != NULL, FALSE);
1817
g_return_val_if_fail (event != NULL, FALSE);
1819
if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1820
(event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1822
fs = GTK_FILE_SELECTION (user_data);
1823
#ifdef G_WITH_CYGWIN
1824
translate_win32_path (fs);
1826
text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1828
gtk_file_selection_populate (fs, text, TRUE, TRUE);
1839
gtk_file_selection_history_callback (GtkWidget *widget,
1842
GtkFileSelection *fs = data;
1843
HistoryCallbackArg *callback_arg;
1846
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1848
list = fs->history_list;
1851
callback_arg = list->data;
1853
if (callback_arg->menu_item == widget)
1855
gtk_file_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1864
gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1865
gchar *current_directory)
1867
HistoryCallbackArg *callback_arg;
1868
GtkWidget *menu_item;
1874
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1875
g_return_if_fail (current_directory != NULL);
1877
list = fs->history_list;
1879
if (fs->history_menu)
1882
callback_arg = list->data;
1883
g_free (callback_arg->directory);
1884
g_free (callback_arg);
1887
g_list_free (fs->history_list);
1888
fs->history_list = NULL;
1890
gtk_widget_destroy (fs->history_menu);
1893
fs->history_menu = gtk_menu_new ();
1895
current_dir = g_strdup (current_directory);
1897
dir_len = strlen (current_dir);
1899
for (i = dir_len; i >= 0; i--)
1901
/* the i == dir_len is to catch the full path for the first
1903
if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1905
/* another small hack to catch the full path */
1907
current_dir[i + 1] = '\0';
1908
#ifdef G_WITH_CYGWIN
1909
if (!strcmp (current_dir, "//"))
1912
menu_item = gtk_menu_item_new_with_label (current_dir);
1914
callback_arg = g_new (HistoryCallbackArg, 1);
1915
callback_arg->menu_item = menu_item;
1917
/* since the autocompletion gets confused if you don't
1918
* supply a trailing '/' on a dir entry, set the full
1919
* (current) path to "" which just refreshes the filesel */
1922
callback_arg->directory = g_strdup ("");
1926
callback_arg->directory = g_strdup (current_dir);
1929
fs->history_list = g_list_append (fs->history_list, callback_arg);
1931
g_signal_connect (menu_item, "activate",
1932
G_CALLBACK (gtk_file_selection_history_callback),
1934
gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1935
gtk_widget_show (menu_item);
1939
gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1941
g_free (current_dir);
1945
get_real_filename (gchar *filename,
1948
#ifdef G_WITH_CYGWIN
1949
/* Check to see if the selection was a drive selector */
1950
if (isalpha (filename[0]) && (filename[1] == ':'))
1952
gchar temp_filename[MAX_PATH];
1955
cygwin_conv_to_posix_path (filename, temp_filename);
1957
/* we need trailing '/'. */
1958
len = strlen (temp_filename);
1959
if (len > 0 && temp_filename[len-1] != '/')
1961
temp_filename[len] = '/';
1962
temp_filename[len+1] = '\0';
1968
return g_strdup (temp_filename);
1970
#endif /* G_WITH_CYGWIN */
1975
gtk_file_selection_file_activate (GtkTreeView *tree_view,
1977
GtkTreeViewColumn *column,
1980
GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
1981
GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1985
gtk_tree_model_get_iter (model, &iter, path);
1986
gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
1987
filename = get_real_filename (filename, TRUE);
1988
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1989
gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1995
gtk_file_selection_dir_activate (GtkTreeView *tree_view,
1997
GtkTreeViewColumn *column,
2000
GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2001
GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
2005
gtk_tree_model_get_iter (model, &iter, path);
2006
gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
2007
filename = get_real_filename (filename, TRUE);
2008
gtk_file_selection_populate (fs, filename, FALSE, FALSE);
2012
#ifdef G_PLATFORM_WIN32
2015
win32_gtk_add_drives_to_dir_list (GtkListStore *model)
2019
char formatBuffer[128];
2022
/* Get the drives string */
2023
GetLogicalDriveStrings (sizeof (buffer), buffer);
2025
/* Add the drives as necessary */
2027
while (*textPtr != '\0')
2029
/* Ignore floppies (?) */
2030
if (GetDriveType (textPtr) != DRIVE_REMOVABLE)
2032
/* Build the actual displayable string */
2033
g_snprintf (formatBuffer, sizeof (formatBuffer), "%c:\\", toupper (textPtr[0]));
2035
/* Add to the list */
2036
gtk_list_store_append (model, &iter);
2037
gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
2039
textPtr += (strlen (textPtr) + 1);
2045
escape_underscores (const gchar *str)
2047
GString *result = g_string_new (NULL);
2051
g_string_append_c (result, '_');
2053
g_string_append_c (result, *str);
2057
return g_string_free (result, FALSE);
2061
gtk_file_selection_populate (GtkFileSelection *fs,
2063
gboolean try_complete,
2064
gboolean reset_entry)
2066
CompletionState *cmpl_state;
2067
PossibleCompletion* poss;
2069
GtkListStore *dir_model;
2070
GtkListStore *file_model;
2072
gchar* rem_path = rel_path;
2074
gint did_recurse = FALSE;
2075
gint possible_count = 0;
2077
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
2079
cmpl_state = (CompletionState*) fs->cmpl_state;
2080
poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2082
if (!cmpl_state_okay (cmpl_state))
2084
/* Something went wrong. */
2085
gtk_file_selection_abort (fs);
2089
g_assert (cmpl_state->reference_dir);
2091
dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2092
file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2094
gtk_list_store_clear (dir_model);
2095
gtk_list_store_clear (file_model);
2097
/* Set the dir list to include ./ and ../ */
2098
gtk_list_store_append (dir_model, &iter);
2099
gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2100
gtk_list_store_append (dir_model, &iter);
2101
gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2105
if (cmpl_is_a_completion (poss))
2107
possible_count += 1;
2109
filename = cmpl_this_completion (poss);
2111
if (cmpl_is_directory (poss))
2113
if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2114
strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2116
gtk_list_store_append (dir_model, &iter);
2117
gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2122
gtk_list_store_append (file_model, &iter);
2123
gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2127
poss = cmpl_next_completion (cmpl_state);
2130
#ifdef G_PLATFORM_WIN32
2131
/* For Windows, add drives as potential selections */
2132
win32_gtk_add_drives_to_dir_list (dir_model);
2135
/* File lists are set. */
2137
g_assert (cmpl_state->reference_dir);
2142
/* User is trying to complete filenames, so advance the user's input
2143
* string to the updated_text, which is the common leading substring
2144
* of all possible completions, and if its a directory attempt
2145
* attempt completions in it. */
2147
if (cmpl_updated_text (cmpl_state)[0])
2150
if (cmpl_updated_dir (cmpl_state))
2152
gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2156
gtk_file_selection_populate (fs, dir_name, TRUE, TRUE);
2162
if (fs->selection_entry)
2163
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2164
cmpl_updated_text (cmpl_state));
2169
if (fs->selection_entry)
2170
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2173
else if (reset_entry)
2175
if (fs->selection_entry)
2176
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2181
if (fs->selection_entry && try_complete)
2182
gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry), -1);
2184
if (fs->selection_entry)
2186
char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2187
sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2190
gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2194
if (fs->history_pulldown)
2196
gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2203
gtk_file_selection_abort (GtkFileSelection *fs)
2207
g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2209
/* BEEP gdk_beep(); */
2211
if (fs->selection_entry)
2212
gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2216
* gtk_file_selection_set_select_multiple:
2217
* @filesel: a #GtkFileSelection
2218
* @select_multiple: whether or not the user is allowed to select multiple
2219
* files in the file list.
2221
* Sets whether the user is allowed to select multiple files in the file list.
2222
* Use gtk_file_selection_get_selections () to get the list of selected files.
2225
gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
2226
gboolean select_multiple)
2228
GtkTreeSelection *sel;
2229
GtkSelectionMode mode;
2231
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
2233
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2235
mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2237
if (mode != gtk_tree_selection_get_mode (sel))
2239
gtk_tree_selection_set_mode (sel, mode);
2241
g_object_notify (G_OBJECT (filesel), "select-multiple");
2246
* gtk_file_selection_get_select_multiple:
2247
* @filesel: a #GtkFileSelection
2249
* Determines whether or not the user is allowed to select multiple files in
2250
* the file list. See gtk_file_selection_set_select_multiple().
2252
* Return value: %TRUE if the user is allowed to select multiple files in the
2256
gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
2258
GtkTreeSelection *sel;
2260
g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
2262
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2263
return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2267
multiple_changed_foreach (GtkTreeModel *model,
2272
GPtrArray *names = data;
2275
gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2277
g_ptr_array_add (names, filename);
2281
free_selected_names (GPtrArray *names)
2285
for (i = 0; i < names->len; i++)
2286
g_free (g_ptr_array_index (names, i));
2288
g_ptr_array_free (names, TRUE);
2292
gtk_file_selection_file_changed (GtkTreeSelection *selection,
2295
GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
2296
GPtrArray *new_names;
2301
new_names = g_ptr_array_sized_new (8);
2303
gtk_tree_selection_selected_foreach (selection,
2304
multiple_changed_foreach,
2307
/* nothing selected */
2308
if (new_names->len == 0)
2310
g_ptr_array_free (new_names, TRUE);
2312
if (fs->selected_names != NULL)
2314
free_selected_names (fs->selected_names);
2315
fs->selected_names = NULL;
2318
goto maybe_clear_entry;
2321
if (new_names->len != 1)
2323
GPtrArray *old_names = fs->selected_names;
2325
if (old_names != NULL)
2327
/* A common case is selecting a range of files from top to bottom,
2328
* so quickly check for that to avoid looping over the entire list
2330
if (compare_utf8_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2331
g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2332
index = new_names->len - 1;
2335
gint i = 0, j = 0, cmp;
2337
/* do a quick diff, stopping at the first file not in the
2340
while (i < old_names->len && j < new_names->len)
2342
cmp = compare_utf8_filenames (g_ptr_array_index (old_names, i),
2343
g_ptr_array_index (new_names, j));
2360
/* we ran off the end of the old list */
2361
if (index == -1 && i < new_names->len)
2367
/* A phantom anchor still exists at the point where the last item
2368
* was selected, which is used for subsequent range selections.
2369
* So search up from there.
2371
if (fs->last_selected &&
2372
compare_utf8_filenames (fs->last_selected,
2373
g_ptr_array_index (new_names, 0)) == 0)
2374
index = new_names->len - 1;
2382
if (fs->selected_names != NULL)
2383
free_selected_names (fs->selected_names);
2385
fs->selected_names = new_names;
2389
if (fs->last_selected != NULL)
2390
g_free (fs->last_selected);
2392
fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2393
filename = get_real_filename (fs->last_selected, FALSE);
2395
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2397
if (filename != fs->last_selected)
2405
entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2406
if ((entry != NULL) && (fs->last_selected != NULL) &&
2407
(compare_utf8_filenames (entry, fs->last_selected) == 0))
2408
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2412
* gtk_file_selection_get_selections:
2413
* @filesel: a #GtkFileSelection
2415
* Retrieves the list of file selections the user has made in the dialog box.
2416
* This function is intended for use when the user can select multiple files
2419
* The filenames are in the GLib file name encoding. To convert to
2420
* UTF-8, call g_filename_to_utf8() on each string.
2422
* Return value: a newly-allocated %NULL-terminated array of strings. Use
2423
* g_strfreev() to free it.
2426
gtk_file_selection_get_selections (GtkFileSelection *filesel)
2430
gchar *filename, *dirname;
2431
gchar *current, *buf;
2433
gboolean unselected_entry;
2435
g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
2437
filename = g_strdup (gtk_file_selection_get_filename (filesel));
2439
if (strlen (filename) == 0)
2445
names = filesel->selected_names;
2448
selections = g_new (gchar *, names->len + 2);
2450
selections = g_new (gchar *, 2);
2453
unselected_entry = TRUE;
2457
dirname = g_path_get_dirname (filename);
2459
if ((names->len >= 1) &&
2460
(strcmp (gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry)), "") == 0))
2461
{ /* multiple files are selected and last selection was removed via ctrl click */
2463
dirname = g_strdup (filename); /* as gtk_file_selection_get_filename returns dir
2464
if no file is selected */
2465
unselected_entry = FALSE;
2468
for (i = 0; i < names->len; i++)
2470
buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2472
current = g_build_filename (dirname, buf, NULL);
2475
selections[count++] = current;
2477
if (unselected_entry && compare_sys_filenames (current, filename) == 0)
2478
unselected_entry = FALSE;
2484
if (unselected_entry)
2485
selections[count++] = filename;
2489
selections[count] = NULL;
2494
/**********************************************************************/
2495
/* External Interface */
2496
/**********************************************************************/
2498
/* The four completion state selectors
2501
cmpl_updated_text (CompletionState *cmpl_state)
2503
return cmpl_state->updated_text;
2507
cmpl_updated_dir (CompletionState *cmpl_state)
2509
return cmpl_state->re_complete;
2513
cmpl_reference_position (CompletionState *cmpl_state)
2515
return cmpl_state->reference_dir->fullname;
2519
/* This doesn't work currently and would require changes
2520
* to fnmatch.c to get working.
2523
cmpl_last_valid_char (CompletionState *cmpl_state)
2525
return cmpl_state->last_valid_char;
2530
cmpl_completion_fullname (const gchar *text,
2531
CompletionState *cmpl_state)
2533
if (!cmpl_state_okay (cmpl_state))
2535
return g_strdup ("");
2537
else if (g_path_is_absolute (text))
2539
return g_strdup (text);
2542
else if (text[0] == '~')
2547
dir = open_user_dir (text, cmpl_state);
2551
slash = strchr (text, G_DIR_SEPARATOR);
2553
/* slash may be NULL, that works too */
2554
return g_strconcat (dir->fullname, slash, NULL);
2559
return g_build_filename (cmpl_state->reference_dir->fullname,
2564
/* The three completion selectors
2567
cmpl_this_completion (PossibleCompletion* pc)
2573
cmpl_is_directory (PossibleCompletion* pc)
2575
return pc->is_directory;
2579
cmpl_is_a_completion (PossibleCompletion* pc)
2581
return pc->is_a_completion;
2584
/**********************************************************************/
2585
/* Construction, deletion */
2586
/**********************************************************************/
2588
/* Get the nearest parent of the current directory for which
2589
* we can convert the filename into UTF-8. With paranoia.
2590
* Returns "." when all goes wrong.
2593
get_current_dir_utf8 (void)
2595
gchar *dir = g_get_current_dir ();
2596
gchar *dir_utf8 = NULL;
2602
dir_utf8 = g_filename_to_utf8 (dir, -1, NULL, NULL, NULL);
2606
last_slash = strrchr (dir, G_DIR_SEPARATOR);
2607
if (!last_slash) /* g_get_current_dir() wasn't absolute! */
2610
if (last_slash + 1 == g_path_skip_root (dir)) /* Parent directory is a root directory */
2612
if (last_slash[1] == '\0') /* Root misencoded! */
2615
last_slash[1] = '\0';
2618
last_slash[0] = '\0';
2620
g_assert (last_slash);
2625
return dir_utf8 ? dir_utf8 : g_strdup (".");
2628
static CompletionState*
2629
cmpl_init_state (void)
2632
CompletionState *new_state;
2635
new_state = g_new (CompletionState, 1);
2637
utf8_cwd = get_current_dir_utf8 ();
2641
new_state->reference_dir = NULL;
2642
new_state->completion_dir = NULL;
2643
new_state->active_completion_dir = NULL;
2644
new_state->directory_storage = NULL;
2645
new_state->directory_sent_storage = NULL;
2646
new_state->last_valid_char = 0;
2647
new_state->updated_text = g_new (gchar, MAXPATHLEN);
2648
new_state->updated_text_alloc = MAXPATHLEN;
2649
new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2650
new_state->the_completion.text_alloc = MAXPATHLEN;
2651
new_state->user_dir_name_buffer = NULL;
2652
new_state->user_directories = NULL;
2654
new_state->reference_dir = open_dir (utf8_cwd, new_state);
2656
if (!new_state->reference_dir)
2658
/* Directories changing from underneath us, grumble */
2659
strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2669
cmpl_free_dir_list (GList* dp0)
2675
free_dir (dp->data);
2683
cmpl_free_dir_sent_list (GList* dp0)
2689
free_dir_sent (dp->data);
2697
cmpl_free_state (CompletionState* cmpl_state)
2699
g_return_if_fail (cmpl_state != NULL);
2701
cmpl_free_dir_list (cmpl_state->directory_storage);
2702
cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2704
if (cmpl_state->user_dir_name_buffer)
2705
g_free (cmpl_state->user_dir_name_buffer);
2706
if (cmpl_state->user_directories)
2707
g_free (cmpl_state->user_directories);
2708
if (cmpl_state->the_completion.text)
2709
g_free (cmpl_state->the_completion.text);
2710
if (cmpl_state->updated_text)
2711
g_free (cmpl_state->updated_text);
2713
g_free (cmpl_state);
2717
free_dir (CompletionDir* dir)
2719
g_free (dir->cmpl_text);
2720
g_free (dir->fullname);
2725
free_dir_sent (CompletionDirSent* sent)
2728
for (i = 0; i < sent->entry_count; i++)
2730
g_free (sent->entries[i].entry_name);
2731
g_free (sent->entries[i].sort_key);
2733
g_free (sent->entries);
2738
prune_memory_usage (CompletionState *cmpl_state)
2740
GList* cdsl = cmpl_state->directory_sent_storage;
2741
GList* cdl = cmpl_state->directory_storage;
2745
for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2750
cmpl_free_dir_sent_list (cdsl->next);
2754
cmpl_state->directory_storage = NULL;
2757
if (cdl->data == cmpl_state->reference_dir)
2758
cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2760
free_dir (cdl->data);
2767
/**********************************************************************/
2768
/* The main entrances. */
2769
/**********************************************************************/
2771
static PossibleCompletion*
2772
cmpl_completion_matches (gchar *text_to_complete,
2773
gchar **remaining_text,
2774
CompletionState *cmpl_state)
2779
PossibleCompletion *poss;
2781
prune_memory_usage (cmpl_state);
2783
g_assert (text_to_complete != NULL);
2785
cmpl_state->user_completion_index = -1;
2786
cmpl_state->last_completion_text = text_to_complete;
2787
cmpl_state->the_completion.text[0] = 0;
2788
cmpl_state->last_valid_char = 0;
2789
cmpl_state->updated_text_len = -1;
2790
cmpl_state->updated_text[0] = 0;
2791
cmpl_state->re_complete = FALSE;
2794
first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2796
if (text_to_complete[0] == '~' && !first_slash)
2798
/* Text starts with ~ and there is no slash, show all the
2799
* home directory completions.
2801
poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2803
update_cmpl (poss, cmpl_state);
2808
cmpl_state->reference_dir =
2809
open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2811
if (!cmpl_state->reference_dir)
2814
cmpl_state->completion_dir =
2815
find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2817
cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2819
if (!cmpl_state->completion_dir)
2822
cmpl_state->completion_dir->cmpl_index = -1;
2823
cmpl_state->completion_dir->cmpl_parent = NULL;
2824
cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2826
cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2828
cmpl_state->reference_dir = cmpl_state->completion_dir;
2830
poss = attempt_file_completion (cmpl_state);
2832
update_cmpl (poss, cmpl_state);
2837
static PossibleCompletion*
2838
cmpl_next_completion (CompletionState* cmpl_state)
2840
PossibleCompletion* poss = NULL;
2842
cmpl_state->the_completion.text[0] = 0;
2845
if (cmpl_state->user_completion_index >= 0)
2846
poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2848
poss = attempt_file_completion (cmpl_state);
2850
poss = attempt_file_completion (cmpl_state);
2853
update_cmpl (poss, cmpl_state);
2858
/**********************************************************************/
2859
/* Directory Operations */
2860
/**********************************************************************/
2862
/* Open the directory where completion will begin from, if possible. */
2863
static CompletionDir*
2864
open_ref_dir (gchar *text_to_complete,
2865
gchar **remaining_text,
2866
CompletionState *cmpl_state)
2869
CompletionDir *new_dir;
2871
first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2873
#ifdef G_WITH_CYGWIN
2874
if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2877
g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2879
new_dir = open_dir (root_dir, cmpl_state);
2882
*remaining_text = text_to_complete + 4;
2890
else if (text_to_complete[0] == '~')
2892
new_dir = open_user_dir (text_to_complete, cmpl_state);
2897
*remaining_text = first_slash + 1;
2899
*remaining_text = text_to_complete + strlen (text_to_complete);
2907
else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2909
gchar *tmp = g_strdup (text_to_complete);
2913
while (*p && *p != '*' && *p != '?')
2917
p = strrchr (tmp, G_DIR_SEPARATOR);
2920
if (p + 1 == g_path_skip_root (tmp))
2924
new_dir = open_dir (tmp, cmpl_state);
2927
*remaining_text = text_to_complete +
2928
((p == g_path_skip_root (tmp)) ? (p - tmp) : (p + 1 - tmp));
2932
/* If no possible candidates, use the cwd */
2933
gchar *utf8_curdir = get_current_dir_utf8 ();
2935
new_dir = open_dir (utf8_curdir, cmpl_state);
2938
*remaining_text = text_to_complete;
2940
g_free (utf8_curdir);
2947
*remaining_text = text_to_complete;
2949
new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
2954
new_dir->cmpl_index = -1;
2955
new_dir->cmpl_parent = NULL;
2963
/* open a directory by user name */
2964
static CompletionDir*
2965
open_user_dir (const gchar *text_to_complete,
2966
CompletionState *cmpl_state)
2968
CompletionDir *result;
2972
g_assert (text_to_complete && text_to_complete[0] == '~');
2974
first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2977
cmp_len = first_slash - text_to_complete - 1;
2979
cmp_len = strlen (text_to_complete + 1);
2984
const gchar *homedir = g_get_home_dir ();
2985
gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
2988
result = open_dir (utf8_homedir, cmpl_state);
2992
g_free (utf8_homedir);
2997
gchar* copy = g_new (char, cmp_len + 1);
3001
strncpy (copy, text_to_complete + 1, cmp_len);
3003
pwd = getpwnam (copy);
3010
utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
3011
result = open_dir (utf8_dir, cmpl_state);
3019
/* open a directory relative to the current relative directory */
3020
static CompletionDir*
3021
open_relative_dir (gchar *dir_name,
3023
CompletionState *cmpl_state)
3025
CompletionDir *result;
3028
path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
3029
g_string_assign (path, dir->fullname);
3031
if (dir->fullname_len > 1
3032
&& path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
3033
g_string_append_c (path, G_DIR_SEPARATOR);
3034
g_string_append (path, dir_name);
3036
result = open_dir (path->str, cmpl_state);
3038
g_string_free (path, TRUE);
3043
/* after the cache lookup fails, really open a new directory */
3044
static CompletionDirSent*
3045
open_new_dir (gchar *dir_name,
3047
gboolean stat_subdirs)
3049
CompletionDirSent *sent;
3052
GError *error = NULL;
3053
gint entry_count = 0;
3056
struct stat ent_sbuf;
3058
gchar *sys_dir_name;
3060
sent = g_new (CompletionDirSent, 1);
3061
#ifndef G_PLATFORM_WIN32
3062
sent->mtime = sbuf->st_mtime;
3063
sent->inode = sbuf->st_ino;
3064
sent->device = sbuf->st_dev;
3066
path = g_string_sized_new (2*MAXPATHLEN + 10);
3068
sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3071
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3075
directory = g_dir_open (sys_dir_name, 0, &error);
3078
cmpl_errno = error->code; /* ??? */
3079
g_free (sys_dir_name);
3083
while ((dirent = g_dir_read_name (directory)) != NULL)
3085
entry_count += 2; /* For ".",".." */
3087
sent->entries = g_new (CompletionDirEntry, entry_count);
3088
sent->entry_count = entry_count;
3090
g_dir_rewind (directory);
3092
for (i = 0; i < entry_count; i += 1)
3094
GError *error = NULL;
3102
dirent = g_dir_read_name (directory);
3103
if (!dirent) /* Directory changed */
3107
sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3108
if (sent->entries[n_entries].entry_name == NULL
3109
|| !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3111
gchar *escaped_str = g_strescape (dirent, NULL);
3112
g_message (_("The filename \"%s\" couldn't be converted to UTF-8. "
3113
"(try setting the environment variable G_FILENAME_ENCODING): %s"),
3115
error->message ? error->message : _("Invalid UTF-8"));
3116
g_free (escaped_str);
3117
g_clear_error (&error);
3120
g_clear_error (&error);
3122
sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3124
g_string_assign (path, sys_dir_name);
3125
if (path->str[path->len-1] != G_DIR_SEPARATOR)
3127
g_string_append_c (path, G_DIR_SEPARATOR);
3129
g_string_append (path, dirent);
3133
/* Here we know path->str is a "system charset" string */
3134
if (g_stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3135
sent->entries[n_entries].is_dir = TRUE;
3137
/* stat may fail, and we don't mind, since it could be a
3138
* dangling symlink. */
3139
sent->entries[n_entries].is_dir = FALSE;
3142
sent->entries[n_entries].is_dir = 1;
3146
sent->entry_count = n_entries;
3148
g_free (sys_dir_name);
3149
g_string_free (path, TRUE);
3150
qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3152
g_dir_close (directory);
3157
#ifndef G_PLATFORM_WIN32
3160
check_dir (gchar *dir_name,
3161
struct stat *result,
3162
gboolean *stat_subdirs)
3164
/* A list of directories that we know only contain other directories.
3165
* Trying to stat every file in these directories would be very
3170
const gchar name[5];
3172
struct stat statbuf;
3173
} no_stat_dirs[] = {
3174
{ "/afs", FALSE, { 0 } },
3175
{ "/net", FALSE, { 0 } }
3178
static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3179
static gboolean initialized = FALSE;
3180
gchar *sys_dir_name;
3186
for (i = 0; i < n_no_stat_dirs; i++)
3188
if (g_stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3189
no_stat_dirs[i].present = TRUE;
3193
sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3196
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3200
if (g_stat (sys_dir_name, result) < 0)
3202
g_free (sys_dir_name);
3206
g_free (sys_dir_name);
3208
*stat_subdirs = TRUE;
3209
for (i = 0; i < n_no_stat_dirs; i++)
3211
if (no_stat_dirs[i].present &&
3212
(no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3213
(no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3215
*stat_subdirs = FALSE;
3225
/* open a directory by absolute pathname */
3226
static CompletionDir*
3227
open_dir (gchar *dir_name,
3228
CompletionState *cmpl_state)
3230
#ifndef G_PLATFORM_WIN32
3232
gboolean stat_subdirs;
3235
CompletionDirSent *sent;
3237
#ifndef G_PLATFORM_WIN32
3238
if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3241
cdsl = cmpl_state->directory_sent_storage;
3247
if (sent->inode == sbuf.st_ino &&
3248
sent->mtime == sbuf.st_mtime &&
3249
sent->device == sbuf.st_dev)
3250
return attach_dir (sent, dir_name, cmpl_state);
3255
sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3257
sent = open_new_dir (dir_name, NULL, TRUE);
3262
cmpl_state->directory_sent_storage =
3263
g_list_prepend (cmpl_state->directory_sent_storage, sent);
3265
return attach_dir (sent, dir_name, cmpl_state);
3271
static CompletionDir*
3272
attach_dir (CompletionDirSent *sent,
3274
CompletionState *cmpl_state)
3276
CompletionDir* new_dir;
3278
new_dir = g_new (CompletionDir, 1);
3280
cmpl_state->directory_storage =
3281
g_list_prepend (cmpl_state->directory_storage, new_dir);
3283
new_dir->sent = sent;
3284
new_dir->fullname = g_strdup (dir_name);
3285
new_dir->fullname_len = strlen (dir_name);
3286
new_dir->cmpl_text = NULL;
3292
correct_dir_fullname (CompletionDir* cmpl_dir)
3294
gint length = strlen (cmpl_dir->fullname);
3295
gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3296
gchar *sys_filename;
3299
/* Does it end with /. (\.) ? */
3301
strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3303
/* Is it just the root directory (on a drive) ? */
3304
if (cmpl_dir->fullname + length - 2 == first_slash)
3306
cmpl_dir->fullname[length - 1] = 0;
3307
cmpl_dir->fullname_len = length - 1;
3312
cmpl_dir->fullname[length - 2] = 0;
3316
/* Ends with /./ (\.\)? */
3317
else if (length >= 3 &&
3318
strcmp (cmpl_dir->fullname + length - 3,
3319
G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3320
cmpl_dir->fullname[length - 2] = 0;
3322
/* Ends with /.. (\..) ? */
3323
else if (length >= 3 &&
3324
strcmp (cmpl_dir->fullname + length - 3,
3325
G_DIR_SEPARATOR_S "..") == 0)
3327
/* Is it just /.. (X:\..)? */
3328
if (cmpl_dir->fullname + length - 3 == first_slash)
3330
cmpl_dir->fullname[length - 2] = 0;
3331
cmpl_dir->fullname_len = length - 2;
3335
sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3338
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3342
if (g_stat (sys_filename, &sbuf) < 0)
3344
g_free (sys_filename);
3348
g_free (sys_filename);
3350
cmpl_dir->fullname[length - 3] = 0;
3352
if (!correct_parent (cmpl_dir, &sbuf))
3356
/* Ends with /../ (\..\)? */
3357
else if (length >= 4 &&
3358
strcmp (cmpl_dir->fullname + length - 4,
3359
G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3361
/* Is it just /../ (X:\..\)? */
3362
if (cmpl_dir->fullname + length - 4 == first_slash)
3364
cmpl_dir->fullname[length - 3] = 0;
3365
cmpl_dir->fullname_len = length - 3;
3369
sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3372
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3376
if (g_stat (sys_filename, &sbuf) < 0)
3378
g_free (sys_filename);
3382
g_free (sys_filename);
3384
cmpl_dir->fullname[length - 4] = 0;
3386
if (!correct_parent (cmpl_dir, &sbuf))
3390
cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3396
correct_parent (CompletionDir *cmpl_dir,
3402
#ifndef G_PLATFORM_WIN32
3405
gchar *sys_filename;
3408
last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3409
g_assert (last_slash);
3410
first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3412
/* Clever (?) way to check for top-level directory that works also on
3413
* Win32, where there is a drive letter and colon prefixed...
3415
if (last_slash != first_slash)
3425
sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3428
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3430
last_slash[0] = G_DIR_SEPARATOR;
3434
if (g_stat (sys_filename, &parbuf) < 0)
3436
g_free (sys_filename);
3439
last_slash[0] = G_DIR_SEPARATOR;
3442
g_free (sys_filename);
3444
#ifndef G_PLATFORM_WIN32 /* No inode numbers on Win32 */
3445
if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3446
/* it wasn't a link */
3452
last_slash[0] = G_DIR_SEPARATOR;
3454
/* it was a link, have to figure it out the hard way */
3456
new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3461
g_free (cmpl_dir->fullname);
3463
cmpl_dir->fullname = new_name;
3469
#ifndef G_PLATFORM_WIN32
3472
find_parent_dir_fullname (gchar* dirname)
3474
gchar *sys_orig_dir;
3479
sys_orig_dir = g_get_current_dir ();
3480
sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3483
g_free (sys_orig_dir);
3484
cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3488
if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3491
chdir (sys_orig_dir);
3492
g_free (sys_dirname);
3493
g_free (sys_orig_dir);
3496
g_free (sys_dirname);
3498
sys_cwd = g_get_current_dir ();
3499
result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3502
if (chdir (sys_orig_dir) != 0)
3505
g_free (sys_orig_dir);
3509
g_free (sys_orig_dir);
3515
/**********************************************************************/
3516
/* Completion Operations */
3517
/**********************************************************************/
3521
static PossibleCompletion*
3522
attempt_homedir_completion (gchar *text_to_complete,
3523
CompletionState *cmpl_state)
3527
if (!cmpl_state->user_dir_name_buffer &&
3528
!get_pwdb (cmpl_state))
3531
cmpl_state->user_completion_index += 1;
3533
while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3535
index = first_diff_index (text_to_complete + 1,
3536
cmpl_state->user_directories
3537
[cmpl_state->user_completion_index].login);
3544
if (cmpl_state->last_valid_char < (index + 1))
3545
cmpl_state->last_valid_char = index + 1;
3546
cmpl_state->user_completion_index += 1;
3550
cmpl_state->the_completion.is_a_completion = 1;
3551
cmpl_state->the_completion.is_directory = TRUE;
3553
append_completion_text ("~", cmpl_state);
3555
append_completion_text (cmpl_state->
3556
user_directories[cmpl_state->user_completion_index].login,
3559
return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3562
if (text_to_complete[1]
3563
|| cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3565
cmpl_state->user_completion_index = -1;
3570
cmpl_state->user_completion_index += 1;
3571
cmpl_state->the_completion.is_a_completion = 1;
3572
cmpl_state->the_completion.is_directory = TRUE;
3574
return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3580
#ifdef G_PLATFORM_WIN32
3581
/* FIXME: determine whether we should casefold all Unicode letters
3582
* here, too (and in in first_diff_index() walk through the strings with
3583
* g_utf8_next_char()), or if this folding isn't actually needed at
3586
#define FOLD(c) (tolower(c))
3591
/* returns the index (>= 0) of the first differing character,
3592
* PATTERN_MATCH if the completion matches */
3594
first_diff_index (gchar *pat,
3599
while (*pat && *text && FOLD (*text) == FOLD (*pat))
3609
return PATTERN_MATCH;
3612
static PossibleCompletion*
3613
append_completion_text (gchar *text,
3614
CompletionState *cmpl_state)
3618
if (!cmpl_state->the_completion.text)
3621
len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3623
if (cmpl_state->the_completion.text_alloc > len)
3625
strcat (cmpl_state->the_completion.text, text);
3626
return &cmpl_state->the_completion;
3632
cmpl_state->the_completion.text_alloc = i;
3634
cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3636
if (!cmpl_state->the_completion.text)
3640
strcat (cmpl_state->the_completion.text, text);
3641
return &cmpl_state->the_completion;
3645
static CompletionDir*
3646
find_completion_dir (gchar *text_to_complete,
3647
gchar **remaining_text,
3648
CompletionState *cmpl_state)
3650
gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3651
CompletionDir* dir = cmpl_state->reference_dir;
3652
CompletionDir* next;
3653
*remaining_text = text_to_complete;
3657
gint len = first_slash - *remaining_text;
3659
gchar *found_name = NULL; /* Quiet gcc */
3661
gchar* pat_buf = g_new (gchar, len + 1);
3663
strncpy (pat_buf, *remaining_text, len);
3666
for (i = 0; i < dir->sent->entry_count; i += 1)
3668
if (dir->sent->entries[i].is_dir &&
3669
_gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name, TRUE))
3679
found_name = dir->sent->entries[i].entry_name;
3686
/* Perhaps we are trying to open an automount directory */
3687
found_name = pat_buf;
3690
next = open_relative_dir (found_name, dir, cmpl_state);
3698
next->cmpl_parent = dir;
3702
if (!correct_dir_fullname (dir))
3708
*remaining_text = first_slash + 1;
3709
first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3718
update_cmpl (PossibleCompletion *poss,
3719
CompletionState *cmpl_state)
3723
if (!poss || !cmpl_is_a_completion (poss))
3726
cmpl_len = strlen (cmpl_this_completion (poss));
3728
if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3730
cmpl_state->updated_text_alloc = 2*cmpl_len;
3731
cmpl_state->updated_text =
3732
(gchar*)g_realloc (cmpl_state->updated_text,
3733
cmpl_state->updated_text_alloc);
3736
if (cmpl_state->updated_text_len < 0)
3738
strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3739
cmpl_state->updated_text_len = cmpl_len;
3740
cmpl_state->re_complete = cmpl_is_directory (poss);
3742
else if (cmpl_state->updated_text_len == 0)
3744
cmpl_state->re_complete = FALSE;
3749
first_diff_index (cmpl_state->updated_text,
3750
cmpl_this_completion (poss));
3752
cmpl_state->re_complete = FALSE;
3754
if (first_diff == PATTERN_MATCH)
3757
if (first_diff > cmpl_state->updated_text_len)
3758
strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3760
cmpl_state->updated_text_len = first_diff;
3761
cmpl_state->updated_text[first_diff] = 0;
3765
static PossibleCompletion*
3766
attempt_file_completion (CompletionState *cmpl_state)
3768
gchar *pat_buf, *first_slash;
3769
CompletionDir *dir = cmpl_state->active_completion_dir;
3771
dir->cmpl_index += 1;
3773
if (dir->cmpl_index == dir->sent->entry_count)
3775
if (dir->cmpl_parent == NULL)
3777
cmpl_state->active_completion_dir = NULL;
3783
cmpl_state->active_completion_dir = dir->cmpl_parent;
3785
return attempt_file_completion (cmpl_state);
3789
g_assert (dir->cmpl_text);
3791
first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3795
gint len = first_slash - dir->cmpl_text;
3797
pat_buf = g_new (gchar, len + 1);
3798
strncpy (pat_buf, dir->cmpl_text, len);
3803
gint len = strlen (dir->cmpl_text);
3805
pat_buf = g_new (gchar, len + 2);
3806
strcpy (pat_buf, dir->cmpl_text);
3807
/* Don't append a * if the user entered one herself.
3808
* This way one can complete *.h and don't get matches
3809
* on any .help files, for instance.
3811
if (strchr (pat_buf, '*') == NULL)
3812
strcpy (pat_buf + len, "*");
3817
if (dir->sent->entries[dir->cmpl_index].is_dir)
3819
if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name, TRUE))
3821
CompletionDir* new_dir;
3823
new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3832
new_dir->cmpl_parent = dir;
3834
new_dir->cmpl_index = -1;
3835
new_dir->cmpl_text = g_strdup (first_slash + 1);
3837
cmpl_state->active_completion_dir = new_dir;
3840
return attempt_file_completion (cmpl_state);
3845
return attempt_file_completion (cmpl_state);
3851
return attempt_file_completion (cmpl_state);
3856
if (dir->cmpl_parent != NULL)
3858
append_completion_text (dir->fullname +
3859
strlen (cmpl_state->completion_dir->fullname) + 1,
3861
append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3864
append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3866
cmpl_state->the_completion.is_a_completion =
3867
_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name, TRUE);
3869
cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3870
if (dir->sent->entries[dir->cmpl_index].is_dir)
3871
append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3874
return &cmpl_state->the_completion;
3881
get_pwdb (CompletionState* cmpl_state)
3883
struct passwd *pwd_ptr;
3886
gint len = 0, i, count = 0;
3888
if (cmpl_state->user_dir_name_buffer)
3892
while ((pwd_ptr = getpwent ()) != NULL)
3894
utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3895
len += strlen (utf8);
3897
utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3898
len += strlen (utf8);
3906
cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3907
cmpl_state->user_directories = g_new (CompletionUserDir, count);
3908
cmpl_state->user_directories_len = count;
3910
buf_ptr = cmpl_state->user_dir_name_buffer;
3912
for (i = 0; i < count; i += 1)
3914
pwd_ptr = getpwent ();
3921
utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3922
strcpy (buf_ptr, utf8);
3925
cmpl_state->user_directories[i].login = buf_ptr;
3927
buf_ptr += strlen (buf_ptr);
3930
utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3931
strcpy (buf_ptr, utf8);
3934
cmpl_state->user_directories[i].homedir = buf_ptr;
3936
buf_ptr += strlen (buf_ptr);
3940
qsort (cmpl_state->user_directories,
3941
cmpl_state->user_directories_len,
3942
sizeof (CompletionUserDir),
3951
if (cmpl_state->user_dir_name_buffer)
3952
g_free (cmpl_state->user_dir_name_buffer);
3953
if (cmpl_state->user_directories)
3954
g_free (cmpl_state->user_directories);
3956
cmpl_state->user_dir_name_buffer = NULL;
3957
cmpl_state->user_directories = NULL;
3963
compare_user_dir (const void *a,
3966
return strcmp ((((CompletionUserDir*)a))->login,
3967
(((CompletionUserDir*)b))->login);
3973
compare_cmpl_dir (const void *a,
3977
return strcmp (((CompletionDirEntry*)a)->sort_key,
3978
(((CompletionDirEntry*)b))->sort_key);
3982
cmpl_state_okay (CompletionState* cmpl_state)
3984
return cmpl_state && cmpl_state->reference_dir;
3988
cmpl_strerror (gint err)
3990
if (err == CMPL_ERRNO_TOO_LONG)
3991
return _("Name too long");
3992
else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
3993
return _("Couldn't convert filename");
3995
return g_strerror (err);
4000
/* DLL ABI stability backward compatibility versions */
4002
#undef gtk_file_selection_get_filename
4004
G_CONST_RETURN gchar*
4005
gtk_file_selection_get_filename (GtkFileSelection *filesel)
4007
static gchar retval[MAXPATHLEN*2+1];
4010
tem = g_locale_from_utf8 (gtk_file_selection_get_filename_utf8 (filesel),
4011
-1, NULL, NULL, NULL);
4013
strncpy (retval, tem, sizeof (retval) - 1);
4014
retval[sizeof (retval) - 1] = '\0';
4020
#undef gtk_file_selection_set_filename
4023
gtk_file_selection_set_filename (GtkFileSelection *filesel,
4024
const gchar *filename)
4026
gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4027
gtk_file_selection_set_filename_utf8 (filesel, utf8_filename);
4028
g_free (utf8_filename);
4031
#undef gtk_file_selection_get_selections
4034
gtk_file_selection_get_selections (GtkFileSelection *filesel)
4037
gchar **selections = gtk_file_selection_get_selections_utf8 (filesel);
4039
if (selections != NULL)
4040
while (selections[i] != NULL)
4042
gchar *tem = selections[i];
4043
selections[i] = g_locale_from_utf8 (selections[i],
4044
-1, NULL, NULL, NULL);
4052
#endif /* G_OS_WIN32 */
4054
#define __GTK_FILESEL_C__
4055
#include "gtkaliasdef.c"