Directory selector
[lttv.git] / ltt / branches / poly / lttv / modules / gui / mainWin / src / gtkdirsel.c
1
2 #include "config.h"
3
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #ifdef HAVE_SYS_PARAM_H
8 #include <sys/param.h>
9 #endif
10 #include <stdlib.h>
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #include <string.h>
15 #include <errno.h>
16 #ifdef HAVE_PWD_H
17 #include <pwd.h>
18 #endif
19
20 #include <glib.h> /* Include early to get G_OS_WIN32 and
21 * G_WITH_CYGWIN */
22
23 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
24 #include <ctype.h>
25 #define STRICT
26 #include <windows.h>
27 #undef STRICT
28 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
29 #ifdef G_OS_WIN32
30 #include <winsock.h> /* For gethostname */
31 #endif
32
33 #include "gdk/gdkkeysyms.h"
34 #include <gtk/gtk.h>
35 #include <lttv/gtkdirsel.h>
36
37 #define _(A) A
38 #define WANT_HPANED 1
39
40 #ifdef G_OS_WIN32
41 #include <direct.h>
42 #include <io.h>
43 #define mkdir(p,m) _mkdir(p)
44 #ifndef S_ISDIR
45 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
46 #endif
47 #endif /* G_OS_WIN32 */
48
49 #ifdef G_WITH_CYGWIN
50 #include <sys/cygwin.h> /* For cygwin_conv_to_posix_path */
51 #endif
52
53 #define DIR_LIST_WIDTH 180
54 #define DIR_LIST_HEIGHT 180
55 #define FILE_LIST_WIDTH 180
56 #define FILE_LIST_HEIGHT 180
57
58 /* The Hurd doesn't define either PATH_MAX or MAXPATHLEN, so we put this
59 * in here, since the rest of the code in the file does require some
60 * fixed maximum.
61 */
62 #ifndef MAXPATHLEN
63 # ifdef PATH_MAX
64 # define MAXPATHLEN PATH_MAX
65 # else
66 # define MAXPATHLEN 2048
67 # endif
68 #endif
69
70 /* I've put this here so it doesn't get confused with the
71 * file completion interface */
72 typedef struct _HistoryCallbackArg HistoryCallbackArg;
73
74 struct _HistoryCallbackArg
75 {
76 gchar *directory;
77 GtkWidget *menu_item;
78 };
79
80
81 typedef struct _CompletionState CompletionState;
82 typedef struct _CompletionDir CompletionDir;
83 typedef struct _CompletionDirSent CompletionDirSent;
84 typedef struct _CompletionDirEntry CompletionDirEntry;
85 typedef struct _CompletionUserDir CompletionUserDir;
86 typedef struct _PossibleCompletion PossibleCompletion;
87
88 /* Non-external file completion decls and structures */
89
90 /* A contant telling PRCS how many directories to cache. Its actually
91 * kept in a list, so the geometry isn't important. */
92 #define CMPL_DIRECTORY_CACHE_SIZE 10
93
94 /* A constant used to determine whether a substring was an exact
95 * match by first_diff_index()
96 */
97 #define PATTERN_MATCH -1
98 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
99 #define CMPL_ERRNO_DID_NOT_CONVERT ((1<<16)-2)
100
101 /* This structure contains all the useful information about a directory
102 * for the purposes of filename completion. These structures are cached
103 * in the CompletionState struct. CompletionDir's are reference counted.
104 */
105 struct _CompletionDirSent
106 {
107 ino_t inode;
108 time_t mtime;
109 dev_t device;
110
111 gint entry_count;
112 struct _CompletionDirEntry *entries;
113 };
114
115 struct _CompletionDir
116 {
117 CompletionDirSent *sent;
118
119 gchar *fullname;
120 gint fullname_len;
121
122 struct _CompletionDir *cmpl_parent;
123 gint cmpl_index;
124 gchar *cmpl_text;
125 };
126
127 /* This structure contains pairs of directory entry names with a flag saying
128 * whether or not they are a valid directory. NOTE: This information is used
129 * to provide the caller with information about whether to update its completions
130 * or try to open a file. Since directories are cached by the directory mtime,
131 * a symlink which points to an invalid file (which will not be a directory),
132 * will not be reevaluated if that file is created, unless the containing
133 * directory is touched. I consider this case to be worth ignoring (josh).
134 */
135 struct _CompletionDirEntry
136 {
137 gboolean is_dir;
138 gchar *entry_name;
139 gchar *sort_key;
140 };
141
142 struct _CompletionUserDir
143 {
144 gchar *login;
145 gchar *homedir;
146 };
147
148 struct _PossibleCompletion
149 {
150 /* accessible fields, all are accessed externally by functions
151 * declared above
152 */
153 gchar *text;
154 gint is_a_completion;
155 gboolean is_directory;
156
157 /* Private fields
158 */
159 gint text_alloc;
160 };
161
162 struct _CompletionState
163 {
164 gint last_valid_char;
165 gchar *updated_text;
166 gint updated_text_len;
167 gint updated_text_alloc;
168 gboolean re_complete;
169
170 gchar *user_dir_name_buffer;
171 gint user_directories_len;
172
173 gchar *last_completion_text;
174
175 gint user_completion_index; /* if >= 0, currently completing ~user */
176
177 struct _CompletionDir *completion_dir; /* directory completing from */
178 struct _CompletionDir *active_completion_dir;
179
180 struct _PossibleCompletion the_completion;
181
182 struct _CompletionDir *reference_dir; /* initial directory */
183
184 GList* directory_storage;
185 GList* directory_sent_storage;
186
187 struct _CompletionUserDir *user_directories;
188 };
189
190 enum {
191 PROP_0,
192 PROP_SHOW_FILEOPS,
193 PROP_FILENAME,
194 PROP_SELECT_MULTIPLE
195 };
196
197 enum {
198 DIR_COLUMN
199 };
200
201 enum {
202 FILE_COLUMN
203 };
204
205 /* File completion functions which would be external, were they used
206 * outside of this file.
207 */
208
209 static CompletionState* cmpl_init_state (void);
210 static void cmpl_free_state (CompletionState *cmpl_state);
211 static gint cmpl_state_okay (CompletionState* cmpl_state);
212 static const gchar* cmpl_strerror (gint);
213
214 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
215 gchar **remaining_text,
216 CompletionState *cmpl_state);
217
218 /* Returns a name for consideration, possibly a completion, this name
219 * will be invalid after the next call to cmpl_next_completion.
220 */
221 static char* cmpl_this_completion (PossibleCompletion*);
222
223 /* True if this completion matches the given text. Otherwise, this
224 * output can be used to have a list of non-completions.
225 */
226 static gint cmpl_is_a_completion (PossibleCompletion*);
227
228 /* True if the completion is a directory
229 */
230 static gboolean cmpl_is_directory (PossibleCompletion*);
231
232 /* Obtains the next completion, or NULL
233 */
234 static PossibleCompletion* cmpl_next_completion (CompletionState*);
235
236 /* Updating completions: the return value of cmpl_updated_text() will
237 * be text_to_complete completed as much as possible after the most
238 * recent call to cmpl_completion_matches. For the present
239 * application, this is the suggested replacement for the user's input
240 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
241 * been received.
242 */
243 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
244
245 /* After updating, to see if the completion was a directory, call
246 * this. If it was, you should consider re-calling completion_matches.
247 */
248 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
249
250 /* Current location: if using file completion, return the current
251 * directory, from which file completion begins. More specifically,
252 * the cwd concatenated with all exact completions up to the last
253 * directory delimiter('/').
254 */
255 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
256
257 /* backing up: if cmpl_completion_matches returns NULL, you may query
258 * the index of the last completable character into cmpl_updated_text.
259 */
260 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
261
262 /* When the user selects a non-directory, call cmpl_completion_fullname
263 * to get the full name of the selected file.
264 */
265 static const gchar* cmpl_completion_fullname (const gchar*, CompletionState* cmpl_state);
266
267
268 /* Directory operations. */
269 static CompletionDir* open_ref_dir (gchar* text_to_complete,
270 gchar** remaining_text,
271 CompletionState* cmpl_state);
272 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
273 static gboolean check_dir (gchar *dir_name,
274 struct stat *result,
275 gboolean *stat_subdirs);
276 #endif
277 static CompletionDir* open_dir (gchar* dir_name,
278 CompletionState* cmpl_state);
279 #ifdef HAVE_PWD_H
280 static CompletionDir* open_user_dir (const gchar* text_to_complete,
281 CompletionState *cmpl_state);
282 #endif
283 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
284 CompletionState *cmpl_state);
285 static CompletionDirSent* open_new_dir (gchar* dir_name,
286 struct stat* sbuf,
287 gboolean stat_subdirs);
288 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
289 static gint correct_parent (CompletionDir* cmpl_dir,
290 struct stat *sbuf);
291 #ifndef G_OS_WIN32
292 static gchar* find_parent_dir_fullname (gchar* dirname);
293 #endif
294 static CompletionDir* attach_dir (CompletionDirSent* sent,
295 gchar* dir_name,
296 CompletionState *cmpl_state);
297 static void free_dir_sent (CompletionDirSent* sent);
298 static void free_dir (CompletionDir *dir);
299 static void prune_memory_usage(CompletionState *cmpl_state);
300
301 /* Completion operations */
302 #ifdef HAVE_PWD_H
303 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
304 CompletionState *cmpl_state);
305 #endif
306 static PossibleCompletion* attempt_dir_completion(CompletionState *cmpl_state);
307 static CompletionDir* find_completion_dir(gchar* text_to_complete,
308 gchar** remaining_text,
309 CompletionState* cmpl_state);
310 static PossibleCompletion* append_completion_text(gchar* text,
311 CompletionState* cmpl_state);
312 #ifdef HAVE_PWD_H
313 static gint get_pwdb(CompletionState* cmpl_state);
314 static gint compare_user_dir(const void* a, const void* b);
315 #endif
316 static gint first_diff_index(gchar* pat, gchar* text);
317 static gint compare_cmpl_dir(const void* a, const void* b);
318 static void update_cmpl(PossibleCompletion* poss,
319 CompletionState* cmpl_state);
320
321 static void gtk_dir_selection_class_init (GtkDirSelectionClass *klass);
322 static void gtk_dir_selection_set_property (GObject *object,
323 guint prop_id,
324 const GValue *value,
325 GParamSpec *pspec);
326 static void gtk_dir_selection_get_property (GObject *object,
327 guint prop_id,
328 GValue *value,
329 GParamSpec *pspec);
330 static void gtk_dir_selection_init (GtkDirSelection *filesel);
331 static void gtk_dir_selection_finalize (GObject *object);
332 static void gtk_dir_selection_destroy (GtkObject *object);
333 static void gtk_dir_selection_map (GtkWidget *widget);
334 static gint gtk_dir_selection_key_press (GtkWidget *widget,
335 GdkEventKey *event,
336 gpointer user_data);
337 static gint gtk_dir_selection_insert_text (GtkWidget *widget,
338 const gchar *new_text,
339 gint new_text_length,
340 gint *position,
341 gpointer user_data);
342 static void gtk_dir_selection_update_fileops (GtkDirSelection *filesel);
343
344 static void gtk_dir_selection_file_activate (GtkTreeView *tree_view,
345 GtkTreePath *path,
346 GtkTreeViewColumn *column,
347 gpointer user_data);
348 static void gtk_dir_selection_file_changed (GtkTreeSelection *selection,
349 gpointer user_data);
350 static void gtk_dir_selection_dir_activate (GtkTreeView *tree_view,
351 GtkTreePath *path,
352 GtkTreeViewColumn *column,
353 gpointer user_data);
354 static void gtk_dir_selection_dir_changed (GtkTreeSelection *selection,
355 gpointer user_data);
356 static void gtk_dir_selection_populate (GtkDirSelection *fs,
357 gchar *rel_path,
358 gboolean try_complete,
359 gboolean reset_entry);
360 static void gtk_dir_selection_abort (GtkDirSelection *fs);
361
362 static void gtk_dir_selection_update_history_menu (GtkDirSelection *fs,
363 gchar *current_dir);
364
365 static void gtk_dir_selection_create_dir (GtkWidget *widget, gpointer data);
366 static void gtk_dir_selection_delete_file (GtkWidget *widget, gpointer data);
367 static void gtk_dir_selection_rename_file (GtkWidget *widget, gpointer data);
368
369 static void free_selected_names (GPtrArray *names);
370
371 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
372 #define compare_filenames(a, b) strcmp(a, b)
373 #else
374 #define compare_filenames(a, b) g_ascii_strcasecmp(a, b)
375 #endif
376
377
378 static GtkWindowClass *parent_class = NULL;
379
380 /* Saves errno when something cmpl does fails. */
381 static gint cmpl_errno;
382
383 #ifdef G_WITH_CYGWIN
384 /*
385 * Take the path currently in the file selection
386 * entry field and translate as necessary from
387 * a WIN32 style to CYGWIN32 style path. For
388 * instance translate:
389 * x:\somepath\file.jpg
390 * to:
391 * /cygdrive/x/somepath/file.jpg
392 *
393 * Replace the path in the selection text field.
394 * Return a boolean value concerning whether a
395 * translation had to be made.
396 */
397 static int
398 translate_win32_path (GtkDirSelection *filesel)
399 {
400 int updated = 0;
401 const gchar *path;
402 gchar newPath[MAX_PATH];
403
404 /*
405 * Retrieve the current path
406 */
407 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
408
409 cygwin_conv_to_posix_path (path, newPath);
410 updated = (strcmp (path, newPath) != 0);
411
412 if (updated)
413 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
414
415 return updated;
416 }
417 #endif
418
419 GType
420 gtk_dir_selection_get_type (void)
421 {
422 static GType file_selection_type = 0;
423
424 if (!file_selection_type)
425 {
426 static const GTypeInfo filesel_info =
427 {
428 sizeof (GtkDirSelectionClass),
429 NULL, /* base_init */
430 NULL, /* base_finalize */
431 (GClassInitFunc) gtk_dir_selection_class_init,
432 NULL, /* class_finalize */
433 NULL, /* class_data */
434 sizeof (GtkDirSelection),
435 0, /* n_preallocs */
436 (GInstanceInitFunc) gtk_dir_selection_init,
437 };
438
439 file_selection_type =
440 g_type_register_static (GTK_TYPE_DIALOG, "GtkDirSelection",
441 &filesel_info, 0);
442 }
443
444 return file_selection_type;
445 }
446
447 static void
448 gtk_dir_selection_class_init (GtkDirSelectionClass *class)
449 {
450 GObjectClass *gobject_class;
451 GtkObjectClass *object_class;
452 GtkWidgetClass *widget_class;
453
454 gobject_class = (GObjectClass*) class;
455 object_class = (GtkObjectClass*) class;
456 widget_class = (GtkWidgetClass*) class;
457
458 parent_class = g_type_class_peek_parent (class);
459
460 gobject_class->finalize = gtk_dir_selection_finalize;
461 gobject_class->set_property = gtk_dir_selection_set_property;
462 gobject_class->get_property = gtk_dir_selection_get_property;
463
464 g_object_class_install_property (gobject_class,
465 PROP_FILENAME,
466 g_param_spec_string ("filename",
467 _("Filename"),
468 _("The currently selected filename"),
469 NULL,
470 G_PARAM_READABLE | G_PARAM_WRITABLE));
471 g_object_class_install_property (gobject_class,
472 PROP_SHOW_FILEOPS,
473 g_param_spec_boolean ("show_fileops",
474 _("Show file operations"),
475 _("Whether buttons for creating/manipulating files should be displayed"),
476 FALSE,
477 G_PARAM_READABLE |
478 G_PARAM_WRITABLE));
479 g_object_class_install_property (gobject_class,
480 PROP_SELECT_MULTIPLE,
481 g_param_spec_boolean ("select_multiple",
482 _("Select multiple"),
483 _("Whether to allow multiple files to be selected"),
484 FALSE,
485 G_PARAM_READABLE |
486 G_PARAM_WRITABLE));
487 object_class->destroy = gtk_dir_selection_destroy;
488 widget_class->map = gtk_dir_selection_map;
489 }
490
491 static void gtk_dir_selection_set_property (GObject *object,
492 guint prop_id,
493 const GValue *value,
494 GParamSpec *pspec)
495 {
496 GtkDirSelection *filesel;
497
498 filesel = GTK_DIR_SELECTION (object);
499
500 switch (prop_id)
501 {
502 case PROP_FILENAME:
503 gtk_dir_selection_set_filename (filesel,
504 g_value_get_string (value));
505 break;
506 case PROP_SHOW_FILEOPS:
507 if (g_value_get_boolean (value))
508 gtk_dir_selection_show_fileop_buttons (filesel);
509 else
510 gtk_dir_selection_hide_fileop_buttons (filesel);
511 break;
512 case PROP_SELECT_MULTIPLE:
513 gtk_dir_selection_set_select_multiple (filesel, g_value_get_boolean (value));
514 break;
515 default:
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 break;
518 }
519 }
520
521 static void gtk_dir_selection_get_property (GObject *object,
522 guint prop_id,
523 GValue *value,
524 GParamSpec *pspec)
525 {
526 GtkDirSelection *filesel;
527
528 filesel = GTK_DIR_SELECTION (object);
529
530 switch (prop_id)
531 {
532 case PROP_FILENAME:
533 g_value_set_string (value,
534 gtk_dir_selection_get_filename(filesel));
535 break;
536
537 case PROP_SHOW_FILEOPS:
538 /* This is a little bit hacky, but doing otherwise would require
539 * adding a field to the object.
540 */
541 g_value_set_boolean (value, (filesel->fileop_c_dir &&
542 filesel->fileop_del_file &&
543 filesel->fileop_ren_file));
544 break;
545 case PROP_SELECT_MULTIPLE:
546 g_value_set_boolean (value, gtk_dir_selection_get_select_multiple (filesel));
547 break;
548 default:
549 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
550 break;
551 }
552 }
553
554 static gboolean
555 grab_default (GtkWidget *widget)
556 {
557 gtk_widget_grab_default (widget);
558 return FALSE;
559 }
560
561 static void
562 gtk_dir_selection_init (GtkDirSelection *filesel)
563 {
564 GtkWidget *entry_vbox;
565 GtkWidget *label;
566 GtkWidget *list_hbox, *list_container;
567 GtkWidget *confirm_area;
568 GtkWidget *pulldown_hbox;
569 GtkWidget *scrolled_win;
570 GtkWidget *eventbox;
571 GtkWidget *spacer;
572 GtkDialog *dialog;
573
574 GtkListStore *model;
575 GtkTreeViewColumn *column;
576
577 gtk_widget_push_composite_child ();
578
579 dialog = GTK_DIALOG (filesel);
580
581 filesel->cmpl_state = cmpl_init_state ();
582
583 /* The dialog-sized vertical box */
584 filesel->main_vbox = dialog->vbox;
585 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
586
587 /* The horizontal box containing create, rename etc. buttons */
588 filesel->button_area = gtk_hbutton_box_new ();
589 gtk_button_box_set_layout (GTK_BUTTON_BOX (filesel->button_area), GTK_BUTTONBOX_START);
590 gtk_box_set_spacing (GTK_BOX (filesel->button_area), 0);
591 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
592 FALSE, FALSE, 0);
593 gtk_widget_show (filesel->button_area);
594
595 gtk_dir_selection_show_fileop_buttons (filesel);
596
597 /* hbox for pulldown menu */
598 pulldown_hbox = gtk_hbox_new (TRUE, 5);
599 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
600 gtk_widget_show (pulldown_hbox);
601
602 /* Pulldown menu */
603 filesel->history_pulldown = gtk_option_menu_new ();
604 // gtk_widget_show (filesel->history_pulldown);
605 // gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
606 // FALSE, FALSE, 0);
607
608 /* The horizontal box containing the directory and file listboxes */
609
610 spacer = gtk_hbox_new (FALSE, 0);
611 gtk_widget_set_size_request (spacer, -1, 5);
612 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
613 gtk_widget_show (spacer);
614
615 list_hbox = gtk_hbox_new (FALSE, 5);
616 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
617 gtk_widget_show (list_hbox);
618 if (WANT_HPANED)
619 list_container = g_object_new (GTK_TYPE_HPANED,
620 "visible", TRUE,
621 "parent", list_hbox,
622 "border_width", 0,
623 NULL);
624 else
625 list_container = list_hbox;
626
627 spacer = gtk_hbox_new (FALSE, 0);
628 gtk_widget_set_size_request (spacer, -1, 5);
629 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), spacer, FALSE, FALSE, 0);
630 gtk_widget_show (spacer);
631
632 /* The directories list */
633
634 model = gtk_list_store_new (1, G_TYPE_STRING);
635 filesel->dir_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
636 g_object_unref (model);
637
638 column = gtk_tree_view_column_new_with_attributes (_("Folders"),
639 gtk_cell_renderer_text_new (),
640 "text", DIR_COLUMN,
641 NULL);
642 label = gtk_label_new_with_mnemonic (_("Fol_ders"));
643 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->dir_list);
644 gtk_widget_show (label);
645 gtk_tree_view_column_set_widget (column, label);
646 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
647 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->dir_list), column);
648
649 gtk_widget_set_size_request (filesel->dir_list,
650 DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
651 g_signal_connect (filesel->dir_list, "row_activated",
652 G_CALLBACK (gtk_dir_selection_dir_activate), filesel);
653 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->dir_list)), "changed",
654 G_CALLBACK (gtk_dir_selection_dir_changed), filesel);
655
656 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list)); */
657
658 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
659 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
660 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
661 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
662 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
663 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
664 if (GTK_IS_PANED (list_container))
665 gtk_paned_pack1 (GTK_PANED (list_container), scrolled_win, TRUE, TRUE);
666 else
667 gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
668 gtk_widget_show (filesel->dir_list);
669 gtk_widget_show (scrolled_win);
670
671 /* The files list */
672 model = gtk_list_store_new (1, G_TYPE_STRING);
673 filesel->file_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
674 g_object_unref (model);
675
676 column = gtk_tree_view_column_new_with_attributes (_("Files"),
677 gtk_cell_renderer_text_new (),
678 "text", FILE_COLUMN,
679 NULL);
680 label = gtk_label_new_with_mnemonic (_("_Files"));
681 gtk_label_set_mnemonic_widget (GTK_LABEL (label), filesel->file_list);
682 gtk_widget_show (label);
683 gtk_tree_view_column_set_widget (column, label);
684 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
685 gtk_tree_view_append_column (GTK_TREE_VIEW (filesel->file_list), column);
686
687 gtk_widget_set_size_request (filesel->file_list,
688 FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
689 g_signal_connect (filesel->file_list, "row_activated",
690 G_CALLBACK (gtk_dir_selection_file_activate), filesel);
691 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list)), "changed",
692 G_CALLBACK (gtk_dir_selection_file_changed), filesel);
693
694 /* gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list)); */
695
696 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
697 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
698 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
699 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
700 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
701 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 0);
702 // gtk_container_add (GTK_CONTAINER (list_container), scrolled_win);
703 // gtk_widget_show (filesel->file_list);
704 // gtk_widget_show (scrolled_win);
705
706 /* action area for packing buttons into. */
707 filesel->action_area = gtk_hbox_new (TRUE, 0);
708 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
709 FALSE, FALSE, 0);
710 gtk_widget_show (filesel->action_area);
711
712 /* The OK/Cancel button area */
713 confirm_area = dialog->action_area;
714
715 /* The Cancel button */
716 filesel->cancel_button = gtk_dialog_add_button (dialog,
717 GTK_STOCK_CANCEL,
718 GTK_RESPONSE_CANCEL);
719 /* The OK button */
720 filesel->ok_button = gtk_dialog_add_button (dialog,
721 GTK_STOCK_OK,
722 GTK_RESPONSE_OK);
723
724 gtk_widget_grab_default (filesel->ok_button);
725
726 /* The selection entry widget */
727 entry_vbox = gtk_vbox_new (FALSE, 2);
728 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 2);
729 gtk_widget_show (entry_vbox);
730
731 eventbox = gtk_event_box_new ();
732 filesel->selection_text = label = gtk_label_new ("");
733 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
734 gtk_container_add (GTK_CONTAINER (eventbox), label);
735 gtk_box_pack_start (GTK_BOX (entry_vbox), eventbox, FALSE, FALSE, 0);
736 gtk_widget_show (label);
737 gtk_widget_show (eventbox);
738
739 filesel->selection_entry = gtk_entry_new ();
740 g_signal_connect (filesel->selection_entry, "key_press_event",
741 G_CALLBACK (gtk_dir_selection_key_press), filesel);
742 g_signal_connect (filesel->selection_entry, "insert_text",
743 G_CALLBACK (gtk_dir_selection_insert_text), NULL);
744 g_signal_connect_swapped (filesel->selection_entry, "changed",
745 G_CALLBACK (gtk_dir_selection_update_fileops), filesel);
746 g_signal_connect_swapped (filesel->selection_entry, "focus_in_event",
747 G_CALLBACK (grab_default),
748 filesel->ok_button);
749 g_signal_connect_swapped (filesel->selection_entry, "activate",
750 G_CALLBACK (gtk_button_clicked),
751 filesel->ok_button);
752
753 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
754 gtk_widget_show (filesel->selection_entry);
755
756 gtk_label_set_mnemonic_widget (GTK_LABEL (filesel->selection_text),
757 filesel->selection_entry);
758
759 if (!cmpl_state_okay (filesel->cmpl_state))
760 {
761 gchar err_buf[256];
762
763 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
764
765 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
766 }
767 else
768 {
769 gtk_dir_selection_populate (filesel, "", FALSE, TRUE);
770 }
771
772 gtk_widget_grab_focus (filesel->selection_entry);
773
774 gtk_widget_pop_composite_child ();
775 }
776
777 static gchar *
778 uri_list_extract_first_uri (const gchar* uri_list)
779 {
780 const gchar *p, *q;
781
782 g_return_val_if_fail (uri_list != NULL, NULL);
783
784 p = uri_list;
785 /* We don't actually try to validate the URI according to RFC
786 * 2396, or even check for allowed characters - we just ignore
787 * comments and trim whitespace off the ends. We also
788 * allow LF delimination as well as the specified CRLF.
789 *
790 * We do allow comments like specified in RFC 2483.
791 */
792 while (p)
793 {
794 if (*p != '#')
795 {
796 while (g_ascii_isspace(*p))
797 p++;
798
799 q = p;
800 while (*q && (*q != '\n') && (*q != '\r'))
801 q++;
802
803 if (q > p)
804 {
805 q--;
806 while (q > p && g_ascii_isspace (*q))
807 q--;
808
809 if (q > p)
810 return g_strndup (p, q - p + 1);
811 }
812 }
813 p = strchr (p, '\n');
814 if (p)
815 p++;
816 }
817 return NULL;
818 }
819
820 static void
821 dnd_really_drop (GtkWidget *dialog, gint response_id, GtkDirSelection *fs)
822 {
823 gchar *filename;
824
825 if (response_id == GTK_RESPONSE_YES)
826 {
827 filename = g_object_get_data (G_OBJECT (dialog), "gtk-fs-dnd-filename");
828
829 gtk_dir_selection_set_filename (fs, filename);
830 }
831
832 gtk_widget_destroy (dialog);
833 }
834
835
836 static void
837 filenames_dropped (GtkWidget *widget,
838 GdkDragContext *context,
839 gint x,
840 gint y,
841 GtkSelectionData *selection_data,
842 guint info,
843 guint time)
844 {
845 char *uri = NULL;
846 char *filename = NULL;
847 char *hostname;
848 char this_hostname[257];
849 int res;
850 GError *error = NULL;
851
852 if (!selection_data->data)
853 return;
854
855 uri = uri_list_extract_first_uri ((char *)selection_data->data);
856
857 if (!uri)
858 return;
859
860 filename = g_filename_from_uri (uri, &hostname, &error);
861 g_free (uri);
862
863 if (!filename)
864 {
865 g_warning ("Error getting dropped filename: %s\n",
866 error->message);
867 g_error_free (error);
868 return;
869 }
870
871 res = gethostname (this_hostname, 256);
872 this_hostname[256] = 0;
873
874 if ((hostname == NULL) ||
875 (res == 0 && strcmp (hostname, this_hostname) == 0) ||
876 (strcmp (hostname, "localhost") == 0))
877 gtk_dir_selection_set_filename (GTK_DIR_SELECTION (widget),
878 filename);
879 else
880 {
881 GtkWidget *dialog;
882 gchar *filename_utf8;
883
884 /* Conversion back to UTF-8 should always succeed for the result
885 * of g_filename_from_uri()
886 */
887 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
888 g_assert (filename_utf8);
889
890 dialog = gtk_message_dialog_new (GTK_WINDOW (widget),
891 GTK_DIALOG_DESTROY_WITH_PARENT,
892 GTK_MESSAGE_QUESTION,
893 GTK_BUTTONS_YES_NO,
894 _("The file \"%s\" resides on another machine (called %s) and may not be available to this program.\n"
895 "Are you sure that you want to select it?"), filename_utf8, hostname);
896 g_free (filename_utf8);
897
898 g_object_set_data_full (G_OBJECT (dialog), "gtk-fs-dnd-filename", g_strdup (filename), g_free);
899
900 g_signal_connect_data (dialog, "response",
901 (GCallback) dnd_really_drop,
902 widget, NULL, 0);
903
904 gtk_widget_show (dialog);
905 }
906
907 g_free (hostname);
908 g_free (filename);
909 }
910
911 enum
912 {
913 TARGET_URILIST,
914 TARGET_UTF8_STRING,
915 TARGET_STRING,
916 TARGET_TEXT,
917 TARGET_COMPOUND_TEXT
918 };
919
920
921 static void
922 filenames_drag_get (GtkWidget *widget,
923 GdkDragContext *context,
924 GtkSelectionData *selection_data,
925 guint info,
926 guint time,
927 GtkDirSelection *filesel)
928 {
929 const gchar *file;
930 gchar *uri_list;
931 char hostname[256];
932 int res;
933 GError *error;
934
935 file = gtk_dir_selection_get_filename (filesel);
936
937 if (file)
938 {
939 if (info == TARGET_URILIST)
940 {
941 res = gethostname (hostname, 256);
942
943 error = NULL;
944 uri_list = g_filename_to_uri (file, (!res)?hostname:NULL, &error);
945 if (!uri_list)
946 {
947 g_warning ("Error getting filename: %s\n",
948 error->message);
949 g_error_free (error);
950 return;
951 }
952
953 gtk_selection_data_set (selection_data,
954 selection_data->target, 8,
955 (void *)uri_list, strlen((char *)uri_list));
956 g_free (uri_list);
957 }
958 else
959 {
960 gchar *filename_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
961 g_assert (filename_utf8);
962 gtk_selection_data_set_text (selection_data, filename_utf8, -1);
963 g_free (filename_utf8);
964 }
965 }
966 }
967
968 static void
969 file_selection_setup_dnd (GtkDirSelection *filesel)
970 {
971 GtkWidget *eventbox;
972 static const GtkTargetEntry drop_types[] = {
973 { "text/uri-list", 0, TARGET_URILIST}
974 };
975 static gint n_drop_types = sizeof(drop_types)/sizeof(drop_types[0]);
976 static const GtkTargetEntry drag_types[] = {
977 { "text/uri-list", 0, TARGET_URILIST},
978 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
979 { "STRING", 0, 0 },
980 { "TEXT", 0, 0 },
981 { "COMPOUND_TEXT", 0, 0 }
982 };
983 static gint n_drag_types = sizeof(drag_types)/sizeof(drag_types[0]);
984
985 gtk_drag_dest_set (GTK_WIDGET (filesel),
986 GTK_DEST_DEFAULT_ALL,
987 drop_types, n_drop_types,
988 GDK_ACTION_COPY);
989
990 g_signal_connect (filesel, "drag_data_received",
991 G_CALLBACK (filenames_dropped), NULL);
992
993 eventbox = gtk_widget_get_parent (filesel->selection_text);
994 gtk_drag_source_set (eventbox,
995 GDK_BUTTON1_MASK,
996 drag_types, n_drag_types,
997 GDK_ACTION_COPY);
998
999 g_signal_connect (eventbox, "drag_data_get",
1000 G_CALLBACK (filenames_drag_get), filesel);
1001 }
1002
1003 GtkWidget*
1004 gtk_dir_selection_new (const gchar *title)
1005 {
1006 GtkDirSelection *filesel;
1007
1008 filesel = g_object_new (GTK_TYPE_DIR_SELECTION, NULL);
1009 gtk_window_set_title (GTK_WINDOW (filesel), title);
1010 gtk_dialog_set_has_separator (GTK_DIALOG (filesel), FALSE);
1011
1012 file_selection_setup_dnd (filesel);
1013
1014 return GTK_WIDGET (filesel);
1015 }
1016
1017 void
1018 gtk_dir_selection_show_fileop_buttons (GtkDirSelection *filesel)
1019 {
1020 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel));
1021
1022 /* delete, create directory, and rename */
1023 if (!filesel->fileop_c_dir)
1024 {
1025 filesel->fileop_c_dir = gtk_button_new_with_mnemonic (_("_New Folder"));
1026 g_signal_connect (filesel->fileop_c_dir, "clicked",
1027 G_CALLBACK (gtk_dir_selection_create_dir),
1028 filesel);
1029 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1030 // filesel->fileop_c_dir, TRUE, TRUE, 0);
1031 // gtk_widget_show (filesel->fileop_c_dir);
1032 }
1033
1034 if (!filesel->fileop_del_file)
1035 {
1036 filesel->fileop_del_file = gtk_button_new_with_mnemonic (_("De_lete File"));
1037 g_signal_connect (filesel->fileop_del_file, "clicked",
1038 G_CALLBACK (gtk_dir_selection_delete_file),
1039 filesel);
1040 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1041 // filesel->fileop_del_file, TRUE, TRUE, 0);
1042 // gtk_widget_show (filesel->fileop_del_file);
1043 }
1044
1045 if (!filesel->fileop_ren_file)
1046 {
1047 filesel->fileop_ren_file = gtk_button_new_with_mnemonic (_("_Rename File"));
1048 g_signal_connect (filesel->fileop_ren_file, "clicked",
1049 G_CALLBACK (gtk_dir_selection_rename_file),
1050 filesel);
1051 // gtk_box_pack_start (GTK_BOX (filesel->button_area),
1052 // filesel->fileop_ren_file, TRUE, TRUE, 0);
1053 // gtk_widget_show (filesel->fileop_ren_file);
1054 }
1055
1056 gtk_dir_selection_update_fileops (filesel);
1057
1058 g_object_notify (G_OBJECT (filesel), "show_fileops");
1059 }
1060
1061 void
1062 gtk_dir_selection_hide_fileop_buttons (GtkDirSelection *filesel)
1063 {
1064 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel));
1065
1066 if (filesel->fileop_ren_file)
1067 {
1068 gtk_widget_destroy (filesel->fileop_ren_file);
1069 filesel->fileop_ren_file = NULL;
1070 }
1071
1072 if (filesel->fileop_del_file)
1073 {
1074 gtk_widget_destroy (filesel->fileop_del_file);
1075 filesel->fileop_del_file = NULL;
1076 }
1077
1078 if (filesel->fileop_c_dir)
1079 {
1080 gtk_widget_destroy (filesel->fileop_c_dir);
1081 filesel->fileop_c_dir = NULL;
1082 }
1083 g_object_notify (G_OBJECT (filesel), "show_fileops");
1084 }
1085
1086
1087
1088 /**
1089 * gtk_dir_selection_set_filename:
1090 * @filesel: a #GtkDirSelection.
1091 * @filename: a string to set as the default file name.
1092 *
1093 * Sets a default path for the file requestor. If @filename includes a
1094 * directory path, then the requestor will open with that path as its
1095 * current working directory.
1096 *
1097 * The encoding of @filename is the on-disk encoding, which
1098 * may not be UTF-8. See g_filename_from_utf8().
1099 **/
1100 void
1101 gtk_dir_selection_set_filename (GtkDirSelection *filesel,
1102 const gchar *filename)
1103 {
1104 gchar *buf;
1105 const char *name, *last_slash;
1106 char *filename_utf8;
1107
1108 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel));
1109 g_return_if_fail (filename != NULL);
1110
1111 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1112 g_return_if_fail (filename_utf8 != NULL);
1113
1114 last_slash = strrchr (filename_utf8, G_DIR_SEPARATOR);
1115
1116 if (!last_slash)
1117 {
1118 buf = g_strdup ("");
1119 name = filename_utf8;
1120 }
1121 else
1122 {
1123 buf = g_strdup (filename_utf8);
1124 buf[last_slash - filename_utf8 + 1] = 0;
1125 name = last_slash + 1;
1126 }
1127
1128 gtk_dir_selection_populate (filesel, buf, FALSE, TRUE);
1129
1130 if (filesel->selection_entry)
1131 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
1132 g_free (buf);
1133 g_object_notify (G_OBJECT (filesel), "filename");
1134
1135 g_free (filename_utf8);
1136 }
1137
1138 /**
1139 * gtk_dir_selection_get_filename:
1140 * @filesel: a #GtkDirSelection
1141 *
1142 * This function returns the selected filename in the on-disk encoding
1143 * (see g_filename_from_utf8()), which may or may not be the same as that
1144 * used by GTK+ (UTF-8). To convert to UTF-8, call g_filename_to_utf8().
1145 * The returned string points to a statically allocated buffer and
1146 * should be copied if you plan to keep it around.
1147 *
1148 * If no file is selected then the selected directory path is returned.
1149 *
1150 * Return value: currently-selected filename in the on-disk encoding.
1151 **/
1152 G_CONST_RETURN gchar*
1153 gtk_dir_selection_get_filename (GtkDirSelection *filesel)
1154 {
1155 static const gchar nothing[2] = "";
1156 static gchar something[MAXPATHLEN*2];
1157 char *sys_filename;
1158 const char *text;
1159
1160 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel), nothing);
1161
1162 #ifdef G_WITH_CYGWIN
1163 translate_win32_path (filesel);
1164 #endif
1165 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
1166 if (text)
1167 {
1168 sys_filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state), -1, NULL, NULL, NULL);
1169 if (!sys_filename)
1170 return nothing;
1171 strncpy (something, sys_filename, sizeof (something));
1172 g_free (sys_filename);
1173 return something;
1174 }
1175
1176 return nothing;
1177 }
1178
1179 void
1180 gtk_dir_selection_complete (GtkDirSelection *filesel,
1181 const gchar *pattern)
1182 {
1183 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel));
1184 g_return_if_fail (pattern != NULL);
1185
1186 if (filesel->selection_entry)
1187 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
1188 gtk_dir_selection_populate (filesel, (gchar*) pattern, TRUE, TRUE);
1189 }
1190
1191 static void
1192 gtk_dir_selection_destroy (GtkObject *object)
1193 {
1194 GtkDirSelection *filesel;
1195 GList *list;
1196 HistoryCallbackArg *callback_arg;
1197
1198 g_return_if_fail (GTK_IS_DIR_SELECTION (object));
1199
1200 filesel = GTK_DIR_SELECTION (object);
1201
1202 if (filesel->fileop_dialog)
1203 {
1204 gtk_widget_destroy (filesel->fileop_dialog);
1205 filesel->fileop_dialog = NULL;
1206 }
1207
1208 if (filesel->history_list)
1209 {
1210 list = filesel->history_list;
1211 while (list)
1212 {
1213 callback_arg = list->data;
1214 g_free (callback_arg->directory);
1215 g_free (callback_arg);
1216 list = list->next;
1217 }
1218 g_list_free (filesel->history_list);
1219 filesel->history_list = NULL;
1220 }
1221
1222 if (filesel->cmpl_state)
1223 {
1224 cmpl_free_state (filesel->cmpl_state);
1225 filesel->cmpl_state = NULL;
1226 }
1227
1228 if (filesel->selected_names)
1229 {
1230 free_selected_names (filesel->selected_names);
1231 filesel->selected_names = NULL;
1232 }
1233
1234 if (filesel->last_selected)
1235 {
1236 g_free (filesel->last_selected);
1237 filesel->last_selected = NULL;
1238 }
1239
1240 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1241 }
1242
1243 static void
1244 gtk_dir_selection_map (GtkWidget *widget)
1245 {
1246 GtkDirSelection *filesel = GTK_DIR_SELECTION (widget);
1247
1248 /* Refresh the contents */
1249 gtk_dir_selection_populate (filesel, "", FALSE, FALSE);
1250
1251 GTK_WIDGET_CLASS (parent_class)->map (widget);
1252 }
1253
1254 static void
1255 gtk_dir_selection_finalize (GObject *object)
1256 {
1257 GtkDirSelection *filesel = GTK_DIR_SELECTION (object);
1258
1259 g_free (filesel->fileop_file);
1260
1261 G_OBJECT_CLASS (parent_class)->finalize (object);
1262 }
1263
1264 /* Begin file operations callbacks */
1265
1266 static void
1267 gtk_dir_selection_fileop_error (GtkDirSelection *fs,
1268 gchar *error_message)
1269 {
1270 GtkWidget *dialog;
1271
1272 g_return_if_fail (error_message != NULL);
1273
1274 /* main dialog */
1275 dialog = gtk_message_dialog_new (GTK_WINDOW (fs),
1276 GTK_DIALOG_DESTROY_WITH_PARENT,
1277 GTK_MESSAGE_ERROR,
1278 GTK_BUTTONS_CLOSE,
1279 "%s", error_message);
1280
1281 /* yes, we free it */
1282 g_free (error_message);
1283
1284 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1285
1286 g_signal_connect_swapped (dialog, "response",
1287 G_CALLBACK (gtk_widget_destroy),
1288 dialog);
1289
1290 gtk_widget_show (dialog);
1291 }
1292
1293 static void
1294 gtk_dir_selection_fileop_destroy (GtkWidget *widget,
1295 gpointer data)
1296 {
1297 GtkDirSelection *fs = data;
1298
1299 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1300
1301 fs->fileop_dialog = NULL;
1302 }
1303
1304 static gboolean
1305 entry_is_empty (GtkEntry *entry)
1306 {
1307 const gchar *text = gtk_entry_get_text (entry);
1308
1309 return *text == '\0';
1310 }
1311
1312 static void
1313 gtk_dir_selection_fileop_entry_changed (GtkEntry *entry,
1314 GtkWidget *button)
1315 {
1316 gtk_widget_set_sensitive (button, !entry_is_empty (entry));
1317 }
1318
1319 static void
1320 gtk_dir_selection_create_dir_confirmed (GtkWidget *widget,
1321 gpointer data)
1322 {
1323 GtkDirSelection *fs = data;
1324 const gchar *dirname;
1325 gchar *path;
1326 gchar *full_path;
1327 gchar *sys_full_path;
1328 gchar *buf;
1329 GError *error = NULL;
1330 CompletionState *cmpl_state;
1331
1332 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1333
1334 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1335 cmpl_state = (CompletionState*) fs->cmpl_state;
1336 path = cmpl_reference_position (cmpl_state);
1337
1338 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
1339 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1340 if (error)
1341 {
1342 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1343 buf = g_strdup_printf (_("The folder name \"%s\" contains symbols that are not allowed in filenames"), dirname);
1344 else
1345 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n%s"), dirname, error->message,
1346 _("You probably used symbols not allowed in filenames."));
1347 gtk_dir_selection_fileop_error (fs, buf);
1348 g_error_free (error);
1349 goto out;
1350 }
1351
1352 if (mkdir (sys_full_path, 0755) < 0)
1353 {
1354 buf = g_strdup_printf (_("Error creating folder \"%s\": %s\n"), dirname,
1355 g_strerror (errno));
1356 gtk_dir_selection_fileop_error (fs, buf);
1357 }
1358
1359 out:
1360 g_free (full_path);
1361 g_free (sys_full_path);
1362
1363 gtk_widget_destroy (fs->fileop_dialog);
1364 gtk_dir_selection_populate (fs, "", FALSE, FALSE);
1365 }
1366
1367 static void
1368 gtk_dir_selection_create_dir (GtkWidget *widget,
1369 gpointer data)
1370 {
1371 GtkDirSelection *fs = data;
1372 GtkWidget *label;
1373 GtkWidget *dialog;
1374 GtkWidget *vbox;
1375 GtkWidget *button;
1376
1377 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1378
1379 if (fs->fileop_dialog)
1380 return;
1381
1382 /* main dialog */
1383 dialog = gtk_dialog_new ();
1384 fs->fileop_dialog = dialog;
1385 g_signal_connect (dialog, "destroy",
1386 G_CALLBACK (gtk_dir_selection_fileop_destroy),
1387 fs);
1388 gtk_window_set_title (GTK_WINDOW (dialog), _("New Folder"));
1389 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1390 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1391
1392 /* If file dialog is grabbed, grab option dialog */
1393 /* When option dialog is closed, file dialog will be grabbed again */
1394 if (GTK_WINDOW (fs)->modal)
1395 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1396
1397 vbox = gtk_vbox_new (FALSE, 0);
1398 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1399 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1400 FALSE, FALSE, 0);
1401 gtk_widget_show( vbox);
1402
1403 label = gtk_label_new_with_mnemonic (_("_Folder name:"));
1404 gtk_misc_set_alignment(GTK_MISC (label), 0.0, 0.0);
1405 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1406 gtk_widget_show (label);
1407
1408 /* The directory entry widget */
1409 fs->fileop_entry = gtk_entry_new ();
1410 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fs->fileop_entry);
1411 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1412 TRUE, TRUE, 5);
1413 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1414 gtk_widget_show (fs->fileop_entry);
1415
1416 /* buttons */
1417 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1418 g_signal_connect_swapped (button, "clicked",
1419 G_CALLBACK (gtk_widget_destroy),
1420 dialog);
1421 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1422 button, TRUE, TRUE, 0);
1423 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1424 gtk_widget_grab_default (button);
1425 gtk_widget_show (button);
1426
1427 gtk_widget_grab_focus (fs->fileop_entry);
1428
1429 button = gtk_button_new_with_mnemonic (_("C_reate"));
1430 gtk_widget_set_sensitive (button, FALSE);
1431 g_signal_connect (button, "clicked",
1432 G_CALLBACK (gtk_dir_selection_create_dir_confirmed),
1433 fs);
1434 g_signal_connect (fs->fileop_entry, "changed",
1435 G_CALLBACK (gtk_dir_selection_fileop_entry_changed),
1436 button);
1437
1438 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1439 button, TRUE, TRUE, 0);
1440 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1441 gtk_widget_show (button);
1442
1443 gtk_widget_show (dialog);
1444 }
1445
1446 static void
1447 gtk_dir_selection_delete_dir_response (GtkDialog *dialog,
1448 gint response_id,
1449 gpointer data)
1450 {
1451 GtkDirSelection *fs = data;
1452 CompletionState *cmpl_state;
1453 gchar *path;
1454 gchar *full_path;
1455 gchar *sys_full_path;
1456 GError *error = NULL;
1457 gchar *buf;
1458
1459 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1460
1461 if (response_id != GTK_RESPONSE_OK)
1462 {
1463 gtk_widget_destroy (GTK_WIDGET (dialog));
1464 return;
1465 }
1466
1467 cmpl_state = (CompletionState*) fs->cmpl_state;
1468 path = cmpl_reference_position (cmpl_state);
1469
1470 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1471 sys_full_path = g_filename_from_utf8 (full_path, -1, NULL, NULL, &error);
1472 if (error)
1473 {
1474 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1475 buf = g_strdup_printf (_("The filename \"%s\" contains symbols that are not allowed in filenames"),
1476 fs->fileop_file);
1477 else
1478 buf = g_strdup_printf (_("Error deleting file \"%s\": %s\n%s"),
1479 fs->fileop_file, error->message,
1480 _("It probably contains symbols not allowed in filenames."));
1481
1482 gtk_dir_selection_fileop_error (fs, buf);
1483 g_error_free (error);
1484 goto out;
1485 }
1486
1487 if (unlink (sys_full_path) < 0)
1488 {
1489 buf = g_strdup_printf (_("Error deleting file \"%s\": %s"),
1490 fs->fileop_file, g_strerror (errno));
1491 gtk_dir_selection_fileop_error (fs, buf);
1492 }
1493
1494 out:
1495 g_free (full_path);
1496 g_free (sys_full_path);
1497
1498 gtk_widget_destroy (fs->fileop_dialog);
1499 gtk_dir_selection_populate (fs, "", FALSE, TRUE);
1500 }
1501
1502 static void
1503 gtk_dir_selection_delete_file (GtkWidget *widget,
1504 gpointer data)
1505 {
1506 GtkDirSelection *fs = data;
1507 GtkWidget *dialog;
1508 const gchar *filename;
1509
1510 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1511
1512 if (fs->fileop_dialog)
1513 return;
1514
1515 #ifdef G_WITH_CYGWIN
1516 translate_win32_path (fs);
1517 #endif
1518
1519 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1520 if (strlen (filename) < 1)
1521 return;
1522
1523 g_free (fs->fileop_file);
1524 fs->fileop_file = g_strdup (filename);
1525
1526 /* main dialog */
1527 fs->fileop_dialog = dialog =
1528 gtk_message_dialog_new (GTK_WINDOW (fs),
1529 GTK_WINDOW (fs)->modal ? GTK_DIALOG_MODAL : 0,
1530 GTK_MESSAGE_QUESTION,
1531 GTK_BUTTONS_NONE,
1532 _("Really delete file \"%s\" ?"), filename);
1533
1534 g_signal_connect (dialog, "destroy",
1535 G_CALLBACK (gtk_dir_selection_fileop_destroy),
1536 fs);
1537 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1538 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1539
1540 /* buttons */
1541 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1542 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1543 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
1544 NULL);
1545
1546 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
1547
1548 g_signal_connect (dialog, "response",
1549 G_CALLBACK (gtk_dir_selection_delete_dir_response),
1550 fs);
1551
1552 gtk_widget_show (dialog);
1553 }
1554
1555 static void
1556 gtk_dir_selection_rename_dir_confirmed (GtkWidget *widget,
1557 gpointer data)
1558 {
1559 GtkDirSelection *fs = data;
1560 gchar *buf;
1561 const gchar *file;
1562 gchar *path;
1563 gchar *new_filename;
1564 gchar *old_filename;
1565 gchar *sys_new_filename;
1566 gchar *sys_old_filename;
1567 CompletionState *cmpl_state;
1568 GError *error = NULL;
1569
1570 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1571
1572 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1573 cmpl_state = (CompletionState*) fs->cmpl_state;
1574 path = cmpl_reference_position (cmpl_state);
1575
1576 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1577 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1578
1579 sys_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, &error);
1580 if (error)
1581 {
1582 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1583 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), new_filename);
1584 else
1585 buf = g_strdup_printf (_("Error renaming file to \"%s\": %s\n%s"),
1586 new_filename, error->message,
1587 _("You probably used symbols not allowed in filenames."));
1588 gtk_dir_selection_fileop_error (fs, buf);
1589 g_error_free (error);
1590 goto out1;
1591 }
1592
1593 sys_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, &error);
1594 if (error)
1595 {
1596 if (g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
1597 buf = g_strdup_printf (_("The file name \"%s\" contains symbols that are not allowed in filenames"), old_filename);
1598 else
1599 buf = g_strdup_printf (_("Error renaming file \"%s\": %s\n%s"),
1600 old_filename, error->message,
1601 _("It probably contains symbols not allowed in filenames."));
1602 gtk_dir_selection_fileop_error (fs, buf);
1603 g_error_free (error);
1604 goto out2;
1605 }
1606
1607 if (rename (sys_old_filename, sys_new_filename) < 0)
1608 {
1609 buf = g_strdup_printf (_("Error renaming file \"%s\" to \"%s\": %s"),
1610 sys_old_filename, sys_new_filename,
1611 g_strerror (errno));
1612 gtk_dir_selection_fileop_error (fs, buf);
1613 goto out2;
1614 }
1615
1616 gtk_dir_selection_populate (fs, "", FALSE, FALSE);
1617 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), file);
1618
1619 out2:
1620 g_free (sys_old_filename);
1621
1622 out1:
1623 g_free (new_filename);
1624 g_free (old_filename);
1625 g_free (sys_new_filename);
1626
1627 gtk_widget_destroy (fs->fileop_dialog);
1628 }
1629
1630 static void
1631 gtk_dir_selection_rename_file (GtkWidget *widget,
1632 gpointer data)
1633 {
1634 GtkDirSelection *fs = data;
1635 GtkWidget *label;
1636 GtkWidget *dialog;
1637 GtkWidget *vbox;
1638 GtkWidget *button;
1639 gchar *buf;
1640
1641 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1642
1643 if (fs->fileop_dialog)
1644 return;
1645
1646 g_free (fs->fileop_file);
1647 fs->fileop_file = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1648 if (strlen (fs->fileop_file) < 1)
1649 return;
1650
1651 /* main dialog */
1652 fs->fileop_dialog = dialog = gtk_dialog_new ();
1653 g_signal_connect (dialog, "destroy",
1654 G_CALLBACK (gtk_dir_selection_fileop_destroy),
1655 fs);
1656 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1657 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1658 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fs));
1659
1660 /* If file dialog is grabbed, grab option dialog */
1661 /* When option dialog closed, file dialog will be grabbed again */
1662 if (GTK_WINDOW (fs)->modal)
1663 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1664
1665 vbox = gtk_vbox_new (FALSE, 0);
1666 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1667 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox,
1668 FALSE, FALSE, 0);
1669 gtk_widget_show(vbox);
1670
1671 buf = g_strdup_printf (_("Rename file \"%s\" to:"), fs->fileop_file);
1672 label = gtk_label_new (buf);
1673 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1674 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 5);
1675 gtk_widget_show (label);
1676 g_free (buf);
1677
1678 /* New filename entry */
1679 fs->fileop_entry = gtk_entry_new ();
1680 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1681 TRUE, TRUE, 5);
1682 GTK_WIDGET_SET_FLAGS (fs->fileop_entry, GTK_CAN_DEFAULT);
1683 gtk_widget_show (fs->fileop_entry);
1684
1685 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1686 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1687 0, strlen (fs->fileop_file));
1688
1689 /* buttons */
1690 button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
1691 g_signal_connect_swapped (button, "clicked",
1692 G_CALLBACK (gtk_widget_destroy),
1693 dialog);
1694 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1695 button, TRUE, TRUE, 0);
1696 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1697 gtk_widget_grab_default (button);
1698 gtk_widget_show (button);
1699
1700 gtk_widget_grab_focus (fs->fileop_entry);
1701
1702 button = gtk_button_new_with_mnemonic (_("_Rename"));
1703 g_signal_connect (button, "clicked",
1704 G_CALLBACK (gtk_dir_selection_rename_dir_confirmed),
1705 fs);
1706 g_signal_connect (fs->fileop_entry, "changed",
1707 G_CALLBACK (gtk_dir_selection_fileop_entry_changed),
1708 button);
1709
1710 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
1711 button, TRUE, TRUE, 0);
1712 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1713 gtk_widget_show (button);
1714
1715 gtk_widget_show (dialog);
1716 }
1717
1718 static gint
1719 gtk_dir_selection_insert_text (GtkWidget *widget,
1720 const gchar *new_text,
1721 gint new_text_length,
1722 gint *position,
1723 gpointer user_data)
1724 {
1725 gchar *filename;
1726
1727 filename = g_filename_from_utf8 (new_text, new_text_length, NULL, NULL, NULL);
1728
1729 if (!filename)
1730 {
1731 gdk_display_beep (gtk_widget_get_display (widget));
1732 g_signal_stop_emission_by_name (widget, "insert_text");
1733 return FALSE;
1734 }
1735
1736 g_free (filename);
1737
1738 return TRUE;
1739 }
1740
1741 static void
1742 gtk_dir_selection_update_fileops (GtkDirSelection *fs)
1743 {
1744 gboolean sensitive;
1745
1746 if (!fs->selection_entry)
1747 return;
1748
1749 sensitive = !entry_is_empty (GTK_ENTRY (fs->selection_entry));
1750
1751 if (fs->fileop_del_file)
1752 gtk_widget_set_sensitive (fs->fileop_del_file, sensitive);
1753
1754 if (fs->fileop_ren_file)
1755 gtk_widget_set_sensitive (fs->fileop_ren_file, sensitive);
1756 }
1757
1758 static gint
1759 gtk_dir_selection_key_press (GtkWidget *widget,
1760 GdkEventKey *event,
1761 gpointer user_data)
1762 {
1763 GtkDirSelection *fs;
1764 char *text;
1765
1766 g_return_val_if_fail (widget != NULL, FALSE);
1767 g_return_val_if_fail (event != NULL, FALSE);
1768
1769 if ((event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab) &&
1770 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
1771 {
1772 fs = GTK_DIR_SELECTION (user_data);
1773 #ifdef G_WITH_CYGWIN
1774 translate_win32_path (fs);
1775 #endif
1776 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (fs->selection_entry)));
1777
1778 gtk_dir_selection_populate (fs, text, TRUE, TRUE);
1779
1780 g_free (text);
1781
1782 return TRUE;
1783 }
1784
1785 return FALSE;
1786 }
1787
1788 static void
1789 gtk_dir_selection_history_callback (GtkWidget *widget,
1790 gpointer data)
1791 {
1792 GtkDirSelection *fs = data;
1793 HistoryCallbackArg *callback_arg;
1794 GList *list;
1795
1796 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1797
1798 list = fs->history_list;
1799
1800 while (list) {
1801 callback_arg = list->data;
1802
1803 if (callback_arg->menu_item == widget)
1804 {
1805 gtk_dir_selection_populate (fs, callback_arg->directory, FALSE, FALSE);
1806 break;
1807 }
1808
1809 list = list->next;
1810 }
1811 }
1812
1813 static void
1814 gtk_dir_selection_update_history_menu (GtkDirSelection *fs,
1815 gchar *current_directory)
1816 {
1817 HistoryCallbackArg *callback_arg;
1818 GtkWidget *menu_item;
1819 GList *list;
1820 gchar *current_dir;
1821 gint dir_len;
1822 gint i;
1823
1824 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
1825 g_return_if_fail (current_directory != NULL);
1826
1827 list = fs->history_list;
1828
1829 if (fs->history_menu)
1830 {
1831 while (list) {
1832 callback_arg = list->data;
1833 g_free (callback_arg->directory);
1834 g_free (callback_arg);
1835 list = list->next;
1836 }
1837 g_list_free (fs->history_list);
1838 fs->history_list = NULL;
1839
1840 gtk_widget_destroy (fs->history_menu);
1841 }
1842
1843 fs->history_menu = gtk_menu_new ();
1844
1845 current_dir = g_strdup (current_directory);
1846
1847 dir_len = strlen (current_dir);
1848
1849 for (i = dir_len; i >= 0; i--)
1850 {
1851 /* the i == dir_len is to catch the full path for the first
1852 * entry. */
1853 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1854 {
1855 /* another small hack to catch the full path */
1856 if (i != dir_len)
1857 current_dir[i + 1] = '\0';
1858 #ifdef G_WITH_CYGWIN
1859 if (!strcmp (current_dir, "//"))
1860 continue;
1861 #endif
1862 menu_item = gtk_menu_item_new_with_label (current_dir);
1863
1864 callback_arg = g_new (HistoryCallbackArg, 1);
1865 callback_arg->menu_item = menu_item;
1866
1867 /* since the autocompletion gets confused if you don't
1868 * supply a trailing '/' on a dir entry, set the full
1869 * (current) path to "" which just refreshes the filesel */
1870 if (dir_len == i)
1871 {
1872 callback_arg->directory = g_strdup ("");
1873 }
1874 else
1875 {
1876 callback_arg->directory = g_strdup (current_dir);
1877 }
1878
1879 fs->history_list = g_list_append (fs->history_list, callback_arg);
1880
1881 g_signal_connect (menu_item, "activate",
1882 G_CALLBACK (gtk_dir_selection_history_callback),
1883 fs);
1884 gtk_menu_shell_append (GTK_MENU_SHELL (fs->history_menu), menu_item);
1885 gtk_widget_show (menu_item);
1886 }
1887 }
1888
1889 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1890 fs->history_menu);
1891 g_free (current_dir);
1892 }
1893
1894 static gchar *
1895 get_real_filename (gchar *filename,
1896 gboolean free_old)
1897 {
1898 #ifdef G_WITH_CYGWIN
1899 /* Check to see if the selection was a drive selector */
1900 if (isalpha (filename[0]) && (filename[1] == ':'))
1901 {
1902 gchar temp_filename[MAX_PATH];
1903 int len;
1904
1905 cygwin_conv_to_posix_path (filename, temp_filename);
1906
1907 /* we need trailing '/'. */
1908 len = strlen (temp_filename);
1909 if (len > 0 && temp_filename[len-1] != '/')
1910 {
1911 temp_filename[len] = '/';
1912 temp_filename[len+1] = '\0';
1913 }
1914
1915 if (free_old)
1916 g_free (filename);
1917
1918 return g_strdup (temp_filename);
1919 }
1920 #endif /* G_WITH_CYGWIN */
1921 return filename;
1922 }
1923
1924 static void
1925 gtk_dir_selection_file_activate (GtkTreeView *tree_view,
1926 GtkTreePath *path,
1927 GtkTreeViewColumn *column,
1928 gpointer user_data)
1929 {
1930 GtkDirSelection *fs = GTK_DIR_SELECTION (user_data);
1931 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1932 GtkTreeIter iter;
1933 gchar *filename;
1934
1935 gtk_tree_model_get_iter (model, &iter, path);
1936 gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
1937 filename = get_real_filename (filename, TRUE);
1938 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1939 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1940
1941 g_free (filename);
1942 }
1943
1944 static void
1945 gtk_dir_selection_dir_activate (GtkTreeView *tree_view,
1946 GtkTreePath *path,
1947 GtkTreeViewColumn *column,
1948 gpointer user_data)
1949 {
1950 GtkDirSelection *fs = GTK_DIR_SELECTION (user_data);
1951 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1952 GtkTreeIter iter;
1953 gchar *filename;
1954
1955 gtk_tree_model_get_iter (model, &iter, path);
1956 gtk_tree_model_get (model, &iter, DIR_COLUMN, &filename, -1);
1957 filename = get_real_filename (filename, TRUE);
1958 gtk_dir_selection_populate (fs, filename, FALSE, FALSE);
1959 g_free (filename);
1960 }
1961
1962 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1963
1964 static void
1965 win32_gtk_add_drives_to_dir_list (GtkListStore *model)
1966 {
1967 gchar *textPtr;
1968 gchar buffer[128];
1969 char formatBuffer[128];
1970 GtkTreeIter iter;
1971
1972 /* Get the drives string */
1973 GetLogicalDriveStrings (sizeof (buffer), buffer);
1974
1975 /* Add the drives as necessary */
1976 textPtr = buffer;
1977 while (*textPtr != '\0')
1978 {
1979 /* Ignore floppies (?) */
1980 if ((tolower (textPtr[0]) != 'a') && (tolower (textPtr[0]) != 'b'))
1981 {
1982 /* Build the actual displayable string */
1983 g_snprintf (formatBuffer, sizeof (formatBuffer), "%c:\\", toupper (textPtr[0]));
1984
1985 /* Add to the list */
1986 gtk_list_store_append (model, &iter);
1987 gtk_list_store_set (model, &iter, DIR_COLUMN, formatBuffer, -1);
1988 }
1989 textPtr += (strlen (textPtr) + 1);
1990 }
1991 }
1992 #endif
1993
1994 static gchar *
1995 escape_underscores (const gchar *str)
1996 {
1997 GString *result = g_string_new (NULL);
1998 while (*str)
1999 {
2000 if (*str == '_')
2001 g_string_append_c (result, '_');
2002
2003 g_string_append_c (result, *str);
2004 str++;
2005 }
2006
2007 return g_string_free (result, FALSE);
2008 }
2009
2010 static void
2011 gtk_dir_selection_populate (GtkDirSelection *fs,
2012 gchar *rel_path,
2013 gboolean try_complete,
2014 gboolean reset_entry)
2015 {
2016 CompletionState *cmpl_state;
2017 PossibleCompletion* poss;
2018 GtkTreeIter iter;
2019 GtkListStore *dir_model;
2020 GtkListStore *file_model;
2021 gchar* filename;
2022 gchar* rem_path = rel_path;
2023 gchar* sel_text;
2024 gint did_recurse = FALSE;
2025 gint possible_count = 0;
2026 gint selection_index = -1;
2027
2028 g_return_if_fail (GTK_IS_DIR_SELECTION (fs));
2029
2030 cmpl_state = (CompletionState*) fs->cmpl_state;
2031 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
2032
2033 if (!cmpl_state_okay (cmpl_state))
2034 {
2035 /* Something went wrong. */
2036 gtk_dir_selection_abort (fs);
2037 return;
2038 }
2039
2040 g_assert (cmpl_state->reference_dir);
2041
2042 dir_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->dir_list)));
2043 file_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list)));
2044
2045 gtk_list_store_clear (dir_model);
2046 gtk_list_store_clear (file_model);
2047
2048 /* Set the dir list to include ./ and ../ */
2049 gtk_list_store_append (dir_model, &iter);
2050 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, "." G_DIR_SEPARATOR_S, -1);
2051 gtk_list_store_append (dir_model, &iter);
2052 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, ".." G_DIR_SEPARATOR_S, -1);
2053
2054 while (poss)
2055 {
2056 if (cmpl_is_a_completion (poss))
2057 {
2058 possible_count += 1;
2059
2060 filename = cmpl_this_completion (poss);
2061
2062 if (cmpl_is_directory (poss))
2063 {
2064 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
2065 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
2066 {
2067 gtk_list_store_append (dir_model, &iter);
2068 gtk_list_store_set (dir_model, &iter, DIR_COLUMN, filename, -1);
2069 }
2070 }
2071 else
2072 {
2073 gtk_list_store_append (file_model, &iter);
2074 gtk_list_store_set (file_model, &iter, DIR_COLUMN, filename, -1);
2075 }
2076 }
2077
2078 poss = cmpl_next_completion (cmpl_state);
2079 }
2080
2081 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2082 /* For Windows, add drives as potential selections */
2083 win32_gtk_add_drives_to_dir_list (dir_model);
2084 #endif
2085
2086 /* File lists are set. */
2087
2088 g_assert (cmpl_state->reference_dir);
2089
2090 if (try_complete)
2091 {
2092
2093 /* User is trying to complete filenames, so advance the user's input
2094 * string to the updated_text, which is the common leading substring
2095 * of all possible completions, and if its a directory attempt
2096 * attempt completions in it. */
2097
2098 if (cmpl_updated_text (cmpl_state)[0])
2099 {
2100
2101 if (cmpl_updated_dir (cmpl_state))
2102 {
2103 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
2104
2105 did_recurse = TRUE;
2106
2107 gtk_dir_selection_populate (fs, dir_name, TRUE, TRUE);
2108
2109 g_free (dir_name);
2110 }
2111 else
2112 {
2113 if (fs->selection_entry)
2114 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
2115 cmpl_updated_text (cmpl_state));
2116 }
2117 }
2118 else
2119 {
2120 selection_index = cmpl_last_valid_char (cmpl_state) -
2121 (strlen (rel_path) - strlen (rem_path));
2122 if (fs->selection_entry)
2123 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
2124 }
2125 }
2126 else if (reset_entry)
2127 {
2128 if (fs->selection_entry)
2129 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2130 }
2131
2132 if (!did_recurse)
2133 {
2134 if (fs->selection_entry)
2135 gtk_editable_set_position (GTK_EDITABLE (fs->selection_entry),
2136 selection_index);
2137
2138 if (fs->selection_entry)
2139 {
2140 char *escaped = escape_underscores (cmpl_reference_position (cmpl_state));
2141 sel_text = g_strconcat (_("_Selection: "), escaped, NULL);
2142 g_free (escaped);
2143
2144 gtk_label_set_text_with_mnemonic (GTK_LABEL (fs->selection_text), sel_text);
2145 g_free (sel_text);
2146 }
2147
2148 if (fs->history_pulldown)
2149 {
2150 gtk_dir_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
2151 }
2152
2153 }
2154 }
2155
2156 static void
2157 gtk_dir_selection_abort (GtkDirSelection *fs)
2158 {
2159 gchar err_buf[256];
2160
2161 g_snprintf (err_buf, sizeof (err_buf), _("Folder unreadable: %s"), cmpl_strerror (cmpl_errno));
2162
2163 /* BEEP gdk_beep(); */
2164
2165 if (fs->selection_entry)
2166 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
2167 }
2168
2169 /**
2170 * gtk_dir_selection_set_select_multiple:
2171 * @filesel: a #GtkDirSelection
2172 * @select_multiple: whether or not the user is allowed to select multiple
2173 * files in the file list.
2174 *
2175 * Sets whether the user is allowed to select multiple files in the file list.
2176 * Use gtk_dir_selection_get_selections () to get the list of selected files.
2177 **/
2178 void
2179 gtk_dir_selection_set_select_multiple (GtkDirSelection *filesel,
2180 gboolean select_multiple)
2181 {
2182 GtkTreeSelection *sel;
2183 GtkSelectionMode mode;
2184
2185 g_return_if_fail (GTK_IS_DIR_SELECTION (filesel));
2186
2187 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2188
2189 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
2190
2191 if (mode != gtk_tree_selection_get_mode (sel))
2192 {
2193 gtk_tree_selection_set_mode (sel, mode);
2194
2195 g_object_notify (G_OBJECT (filesel), "select-multiple");
2196 }
2197 }
2198
2199 /**
2200 * gtk_dir_selection_get_select_multiple:
2201 * @filesel: a #GtkDirSelection
2202 *
2203 * Determines whether or not the user is allowed to select multiple files in
2204 * the file list. See gtk_dir_selection_set_select_multiple().
2205 *
2206 * Return value: %TRUE if the user is allowed to select multiple files in the
2207 * file list
2208 **/
2209 gboolean
2210 gtk_dir_selection_get_select_multiple (GtkDirSelection *filesel)
2211 {
2212 GtkTreeSelection *sel;
2213
2214 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel), FALSE);
2215
2216 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
2217 return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
2218 }
2219
2220 static void
2221 multiple_changed_foreach (GtkTreeModel *model,
2222 GtkTreePath *path,
2223 GtkTreeIter *iter,
2224 gpointer data)
2225 {
2226 GPtrArray *names = data;
2227 gchar *filename;
2228
2229 gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
2230
2231 g_ptr_array_add (names, filename);
2232 }
2233
2234 static void
2235 free_selected_names (GPtrArray *names)
2236 {
2237 gint i;
2238
2239 for (i = 0; i < names->len; i++)
2240 g_free (g_ptr_array_index (names, i));
2241
2242 g_ptr_array_free (names, TRUE);
2243 }
2244
2245 static void
2246 gtk_dir_selection_file_changed (GtkTreeSelection *selection,
2247 gpointer user_data)
2248 {
2249 GtkDirSelection *fs = GTK_DIR_SELECTION (user_data);
2250 GPtrArray *new_names;
2251 gchar *filename;
2252 const gchar *entry;
2253 gint index = -1;
2254
2255 new_names = g_ptr_array_sized_new (8);
2256
2257 gtk_tree_selection_selected_foreach (selection,
2258 multiple_changed_foreach,
2259 new_names);
2260
2261 /* nothing selected */
2262 if (new_names->len == 0)
2263 {
2264 g_ptr_array_free (new_names, TRUE);
2265
2266 if (fs->selected_names != NULL)
2267 {
2268 free_selected_names (fs->selected_names);
2269 fs->selected_names = NULL;
2270 }
2271
2272 goto maybe_clear_entry;
2273 }
2274
2275 if (new_names->len != 1)
2276 {
2277 GPtrArray *old_names = fs->selected_names;
2278
2279 if (old_names != NULL)
2280 {
2281 /* A common case is selecting a range of files from top to bottom,
2282 * so quickly check for that to avoid looping over the entire list
2283 */
2284 if (compare_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2285 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2286 index = new_names->len - 1;
2287 else
2288 {
2289 gint i = 0, j = 0, cmp;
2290
2291 /* do a quick diff, stopping at the first file not in the
2292 * old list
2293 */
2294 while (i < old_names->len && j < new_names->len)
2295 {
2296 cmp = compare_filenames (g_ptr_array_index (old_names, i),
2297 g_ptr_array_index (new_names, j));
2298 if (cmp < 0)
2299 {
2300 i++;
2301 }
2302 else if (cmp == 0)
2303 {
2304 i++;
2305 j++;
2306 }
2307 else if (cmp > 0)
2308 {
2309 index = j;
2310 break;
2311 }
2312 }
2313
2314 /* we ran off the end of the old list */
2315 if (index == -1 && i < new_names->len)
2316 index = j;
2317 }
2318 }
2319 else
2320 {
2321 /* A phantom anchor still exists at the point where the last item
2322 * was selected, which is used for subsequent range selections.
2323 * So search up from there.
2324 */
2325 if (fs->last_selected &&
2326 compare_filenames (fs->last_selected,
2327 g_ptr_array_index (new_names, 0)) == 0)
2328 index = new_names->len - 1;
2329 else
2330 index = 0;
2331 }
2332 }
2333 else
2334 index = 0;
2335
2336 if (fs->selected_names != NULL)
2337 free_selected_names (fs->selected_names);
2338
2339 fs->selected_names = new_names;
2340
2341 if (index != -1)
2342 {
2343 if (fs->last_selected != NULL)
2344 g_free (fs->last_selected);
2345
2346 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2347 filename = get_real_filename (fs->last_selected, FALSE);
2348
2349 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
2350
2351 if (filename != fs->last_selected)
2352 g_free (filename);
2353
2354 return;
2355 }
2356
2357 maybe_clear_entry:
2358
2359 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2360 if ((entry != NULL) && (fs->last_selected != NULL) &&
2361 (compare_filenames (entry, fs->last_selected) == 0))
2362 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2363 }
2364
2365 static void
2366 gtk_dir_selection_dir_changed (GtkTreeSelection *selection,
2367 gpointer user_data)
2368 {
2369 GtkDirSelection *fs = GTK_DIR_SELECTION (user_data);
2370 GPtrArray *new_names;
2371 gchar *filename;
2372 const gchar *entry;
2373 gint index = -1;
2374
2375 new_names = g_ptr_array_sized_new (8);
2376
2377 gtk_tree_selection_selected_foreach (selection,
2378 multiple_changed_foreach,
2379 new_names);
2380
2381 /* nothing selected */
2382 if (new_names->len == 0)
2383 {
2384 g_ptr_array_free (new_names, TRUE);
2385
2386 if (fs->selected_names != NULL)
2387 {
2388 free_selected_names (fs->selected_names);
2389 fs->selected_names = NULL;
2390 }
2391
2392 goto maybe_clear_entry;
2393 }
2394
2395 if (new_names->len != 1)
2396 {
2397 GPtrArray *old_names = fs->selected_names;
2398
2399 if (old_names != NULL)
2400 {
2401 /* A common case is selecting a range of files from top to bottom,
2402 * so quickly check for that to avoid looping over the entire list
2403 */
2404 if (compare_filenames (g_ptr_array_index (old_names, old_names->len - 1),
2405 g_ptr_array_index (new_names, new_names->len - 1)) != 0)
2406 index = new_names->len - 1;
2407 else
2408 {
2409 gint i = 0, j = 0, cmp;
2410
2411 /* do a quick diff, stopping at the first file not in the
2412 * old list
2413 */
2414 while (i < old_names->len && j < new_names->len)
2415 {
2416 cmp = compare_filenames (g_ptr_array_index (old_names, i),
2417 g_ptr_array_index (new_names, j));
2418 if (cmp < 0)
2419 {
2420 i++;
2421 }
2422 else if (cmp == 0)
2423 {
2424 i++;
2425 j++;
2426 }
2427 else if (cmp > 0)
2428 {
2429 index = j;
2430 break;
2431 }
2432 }
2433
2434 /* we ran off the end of the old list */
2435 if (index == -1 && i < new_names->len)
2436 index = j;
2437 }
2438 }
2439 else
2440 {
2441 /* A phantom anchor still exists at the point where the last item
2442 * was selected, which is used for subsequent range selections.
2443 * So search up from there.
2444 */
2445 if (fs->last_selected &&
2446 compare_filenames (fs->last_selected,
2447 g_ptr_array_index (new_names, 0)) == 0)
2448 index = new_names->len - 1;
2449 else
2450 index = 0;
2451 }
2452 }
2453 else
2454 index = 0;
2455
2456 if (fs->selected_names != NULL)
2457 free_selected_names (fs->selected_names);
2458
2459 fs->selected_names = new_names;
2460
2461 if (index != -1)
2462 {
2463 gchar * err, str[256];
2464 err = gtk_label_get_text (GTK_LABEL (fs->selection_text));
2465 err += 11; //pass over "Selection: "
2466 sprintf(str,"%s\0",err);
2467
2468
2469 if (fs->last_selected != NULL)
2470 g_free (fs->last_selected);
2471
2472 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2473 filename = get_real_filename (fs->last_selected, FALSE);
2474
2475 strcat(str,"/");
2476 strcat(str,filename);
2477 str[strlen(str)-1] = '\0';
2478
2479 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), str);
2480
2481 if (filename != fs->last_selected)
2482 g_free (filename);
2483
2484 return;
2485 }
2486
2487 maybe_clear_entry:
2488
2489 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2490 if ((entry != NULL) && (fs->last_selected != NULL) &&
2491 (compare_filenames (entry, fs->last_selected) == 0))
2492 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2493 }
2494
2495 /**
2496 * gtk_dir_selection_get_selections:
2497 * @filesel: a #GtkDirSelection
2498 *
2499 * Retrieves the list of file selections the user has made in the dialog box.
2500 * This function is intended for use when the user can select multiple files
2501 * in the file list. The first file in the list is equivalent to what
2502 * gtk_dir_selection_get_filename() would return.
2503 *
2504 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2505 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2506 * g_filename_to_utf8() on each string.
2507 *
2508 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2509 * g_strfreev() to free it.
2510 **/
2511 gchar **
2512 gtk_dir_selection_get_selections (GtkDirSelection *filesel)
2513 {
2514 GPtrArray *names;
2515 gchar **selections;
2516 gchar *filename, *dirname;
2517 gchar *current, *buf;
2518 gint i, count;
2519 gboolean unselected_entry;
2520
2521 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel), NULL);
2522
2523 filename = g_strdup (gtk_dir_selection_get_filename (filesel));
2524
2525 if (strlen (filename) == 0)
2526 {
2527 g_free (filename);
2528 return NULL;
2529 }
2530
2531 names = filesel->selected_names;
2532
2533 if (names != NULL)
2534 selections = g_new (gchar *, names->len + 2);
2535 else
2536 selections = g_new (gchar *, 2);
2537
2538 count = 0;
2539 unselected_entry = TRUE;
2540
2541 if (names != NULL)
2542 {
2543 dirname = g_path_get_dirname (filename);
2544
2545 for (i = 0; i < names->len; i++)
2546 {
2547 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2548 NULL, NULL, NULL);
2549 current = g_build_filename (dirname, buf, NULL);
2550 g_free (buf);
2551
2552 selections[count++] = current;
2553
2554 if (unselected_entry && compare_filenames (current, filename) == 0)
2555 unselected_entry = FALSE;
2556 }
2557
2558 g_free (dirname);
2559 }
2560
2561 if (unselected_entry)
2562 selections[count++] = filename;
2563 else
2564 g_free (filename);
2565
2566 selections[count] = NULL;
2567
2568 return selections;
2569 }
2570
2571 /**********************************************************************/
2572 /* External Interface */
2573 /**********************************************************************/
2574
2575 /* The four completion state selectors
2576 */
2577 static gchar*
2578 cmpl_updated_text (CompletionState *cmpl_state)
2579 {
2580 return cmpl_state->updated_text;
2581 }
2582
2583 static gboolean
2584 cmpl_updated_dir (CompletionState *cmpl_state)
2585 {
2586 return cmpl_state->re_complete;
2587 }
2588
2589 static gchar*
2590 cmpl_reference_position (CompletionState *cmpl_state)
2591 {
2592 return cmpl_state->reference_dir->fullname;
2593 }
2594
2595 static gint
2596 cmpl_last_valid_char (CompletionState *cmpl_state)
2597 {
2598 return cmpl_state->last_valid_char;
2599 }
2600
2601 static const gchar*
2602 cmpl_completion_fullname (const gchar *text,
2603 CompletionState *cmpl_state)
2604 {
2605 static const char nothing[2] = "";
2606
2607 if (!cmpl_state_okay (cmpl_state))
2608 {
2609 return nothing;
2610 }
2611 else if (g_path_is_absolute (text))
2612 {
2613 strcpy (cmpl_state->updated_text, text);
2614 }
2615 #ifdef HAVE_PWD_H
2616 else if (text[0] == '~')
2617 {
2618 CompletionDir* dir;
2619 char* slash;
2620
2621 dir = open_user_dir (text, cmpl_state);
2622
2623 if (!dir)
2624 {
2625 /* spencer says just return ~something, so
2626 * for now just do it. */
2627 strcpy (cmpl_state->updated_text, text);
2628 }
2629 else
2630 {
2631
2632 strcpy (cmpl_state->updated_text, dir->fullname);
2633
2634 slash = strchr (text, G_DIR_SEPARATOR);
2635
2636 if (slash)
2637 strcat (cmpl_state->updated_text, slash);
2638 }
2639 }
2640 #endif
2641 else
2642 {
2643 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
2644 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
2645 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
2646 strcat (cmpl_state->updated_text, text);
2647 }
2648
2649 return cmpl_state->updated_text;
2650 }
2651
2652 /* The three completion selectors
2653 */
2654 static gchar*
2655 cmpl_this_completion (PossibleCompletion* pc)
2656 {
2657 return pc->text;
2658 }
2659
2660 static gboolean
2661 cmpl_is_directory (PossibleCompletion* pc)
2662 {
2663 return pc->is_directory;
2664 }
2665
2666 static gint
2667 cmpl_is_a_completion (PossibleCompletion* pc)
2668 {
2669 return pc->is_a_completion;
2670 }
2671
2672 /**********************************************************************/
2673 /* Construction, deletion */
2674 /**********************************************************************/
2675
2676 static CompletionState*
2677 cmpl_init_state (void)
2678 {
2679 gchar *sys_getcwd_buf;
2680 gchar *utf8_cwd;
2681 CompletionState *new_state;
2682
2683 new_state = g_new (CompletionState, 1);
2684
2685 /* g_get_current_dir() returns a string in the "system" charset */
2686 sys_getcwd_buf = g_get_current_dir ();
2687 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
2688 g_free (sys_getcwd_buf);
2689
2690 tryagain:
2691
2692 new_state->reference_dir = NULL;
2693 new_state->completion_dir = NULL;
2694 new_state->active_completion_dir = NULL;
2695 new_state->directory_storage = NULL;
2696 new_state->directory_sent_storage = NULL;
2697 new_state->last_valid_char = 0;
2698 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2699 new_state->updated_text_alloc = MAXPATHLEN;
2700 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2701 new_state->the_completion.text_alloc = MAXPATHLEN;
2702 new_state->user_dir_name_buffer = NULL;
2703 new_state->user_directories = NULL;
2704
2705 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2706
2707 if (!new_state->reference_dir)
2708 {
2709 /* Directories changing from underneath us, grumble */
2710 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2711 goto tryagain;
2712 }
2713
2714 g_free (utf8_cwd);
2715 return new_state;
2716 }
2717
2718 static void
2719 cmpl_free_dir_list (GList* dp0)
2720 {
2721 GList *dp = dp0;
2722
2723 while (dp)
2724 {
2725 free_dir (dp->data);
2726 dp = dp->next;
2727 }
2728
2729 g_list_free (dp0);
2730 }
2731
2732 static void
2733 cmpl_free_dir_sent_list (GList* dp0)
2734 {
2735 GList *dp = dp0;
2736
2737 while (dp)
2738 {
2739 free_dir_sent (dp->data);
2740 dp = dp->next;
2741 }
2742
2743 g_list_free (dp0);
2744 }
2745
2746 static void
2747 cmpl_free_state (CompletionState* cmpl_state)
2748 {
2749 g_return_if_fail (cmpl_state != NULL);
2750
2751 cmpl_free_dir_list (cmpl_state->directory_storage);
2752 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2753
2754 if (cmpl_state->user_dir_name_buffer)
2755 g_free (cmpl_state->user_dir_name_buffer);
2756 if (cmpl_state->user_directories)
2757 g_free (cmpl_state->user_directories);
2758 if (cmpl_state->the_completion.text)
2759 g_free (cmpl_state->the_completion.text);
2760 if (cmpl_state->updated_text)
2761 g_free (cmpl_state->updated_text);
2762
2763 g_free (cmpl_state);
2764 }
2765
2766 static void
2767 free_dir (CompletionDir* dir)
2768 {
2769 g_free (dir->cmpl_text);
2770 g_free (dir->fullname);
2771 g_free (dir);
2772 }
2773
2774 static void
2775 free_dir_sent (CompletionDirSent* sent)
2776 {
2777 gint i;
2778 for (i = 0; i < sent->entry_count; i++)
2779 {
2780 g_free (sent->entries[i].entry_name);
2781 g_free (sent->entries[i].sort_key);
2782 }
2783 g_free (sent->entries);
2784 g_free (sent);
2785 }
2786
2787 static void
2788 prune_memory_usage (CompletionState *cmpl_state)
2789 {
2790 GList* cdsl = cmpl_state->directory_sent_storage;
2791 GList* cdl = cmpl_state->directory_storage;
2792 GList* cdl0 = cdl;
2793 gint len = 0;
2794
2795 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2796 cdsl = cdsl->next;
2797
2798 if (cdsl)
2799 {
2800 cmpl_free_dir_sent_list (cdsl->next);
2801 cdsl->next = NULL;
2802 }
2803
2804 cmpl_state->directory_storage = NULL;
2805 while (cdl)
2806 {
2807 if (cdl->data == cmpl_state->reference_dir)
2808 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2809 else
2810 free_dir (cdl->data);
2811 cdl = cdl->next;
2812 }
2813
2814 g_list_free (cdl0);
2815 }
2816
2817 /**********************************************************************/
2818 /* The main entrances. */
2819 /**********************************************************************/
2820
2821 static PossibleCompletion*
2822 cmpl_completion_matches (gchar *text_to_complete,
2823 gchar **remaining_text,
2824 CompletionState *cmpl_state)
2825 {
2826 gchar* first_slash;
2827 PossibleCompletion *poss;
2828
2829 prune_memory_usage (cmpl_state);
2830
2831 g_assert (text_to_complete != NULL);
2832
2833 cmpl_state->user_completion_index = -1;
2834 cmpl_state->last_completion_text = text_to_complete;
2835 cmpl_state->the_completion.text[0] = 0;
2836 cmpl_state->last_valid_char = 0;
2837 cmpl_state->updated_text_len = -1;
2838 cmpl_state->updated_text[0] = 0;
2839 cmpl_state->re_complete = FALSE;
2840
2841 #ifdef HAVE_PWD_H
2842 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2843
2844 if (text_to_complete[0] == '~' && !first_slash)
2845 {
2846 /* Text starts with ~ and there is no slash, show all the
2847 * home directory completions.
2848 */
2849 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2850
2851 update_cmpl (poss, cmpl_state);
2852
2853 return poss;
2854 }
2855 #endif
2856 cmpl_state->reference_dir =
2857 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2858
2859 if (!cmpl_state->reference_dir)
2860 return NULL;
2861
2862 cmpl_state->completion_dir =
2863 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2864
2865 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2866
2867 if (!cmpl_state->completion_dir)
2868 return NULL;
2869
2870 cmpl_state->completion_dir->cmpl_index = -1;
2871 cmpl_state->completion_dir->cmpl_parent = NULL;
2872 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2873
2874 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2875
2876 cmpl_state->reference_dir = cmpl_state->completion_dir;
2877
2878 poss = attempt_dir_completion (cmpl_state);
2879
2880 update_cmpl (poss, cmpl_state);
2881
2882 return poss;
2883 }
2884
2885 static PossibleCompletion*
2886 cmpl_next_completion (CompletionState* cmpl_state)
2887 {
2888 PossibleCompletion* poss = NULL;
2889
2890 cmpl_state->the_completion.text[0] = 0;
2891
2892 #ifdef HAVE_PWD_H
2893 if (cmpl_state->user_completion_index >= 0)
2894 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2895 else
2896 poss = attempt_dir_completion (cmpl_state);
2897 #else
2898 poss = attempt_dir_completion (cmpl_state);
2899 #endif
2900
2901 update_cmpl (poss, cmpl_state);
2902
2903 return poss;
2904 }
2905
2906 /**********************************************************************/
2907 /* Directory Operations */
2908 /**********************************************************************/
2909
2910 /* Open the directory where completion will begin from, if possible. */
2911 static CompletionDir*
2912 open_ref_dir (gchar *text_to_complete,
2913 gchar **remaining_text,
2914 CompletionState *cmpl_state)
2915 {
2916 gchar* first_slash;
2917 CompletionDir *new_dir;
2918
2919 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2920
2921 #ifdef G_WITH_CYGWIN
2922 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2923 {
2924 char root_dir[5];
2925 g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2926
2927 new_dir = open_dir (root_dir, cmpl_state);
2928
2929 if (new_dir) {
2930 *remaining_text = text_to_complete + 4;
2931 }
2932 }
2933 #else
2934 if (FALSE)
2935 ;
2936 #endif
2937 #ifdef HAVE_PWD_H
2938 else if (text_to_complete[0] == '~')
2939 {
2940 new_dir = open_user_dir (text_to_complete, cmpl_state);
2941
2942 if (new_dir)
2943 {
2944 if (first_slash)
2945 *remaining_text = first_slash + 1;
2946 else
2947 *remaining_text = text_to_complete + strlen (text_to_complete);
2948 }
2949 else
2950 {
2951 return NULL;
2952 }
2953 }
2954 #endif
2955 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2956 {
2957 gchar *tmp = g_strdup (text_to_complete);
2958 gchar *p;
2959
2960 p = tmp;
2961 while (*p && *p != '*' && *p != '?')
2962 p++;
2963
2964 *p = '\0';
2965 p = strrchr (tmp, G_DIR_SEPARATOR);
2966 if (p)
2967 {
2968 if (p == tmp)
2969 p++;
2970
2971 *p = '\0';
2972
2973 new_dir = open_dir (tmp, cmpl_state);
2974
2975 if (new_dir)
2976 *remaining_text = text_to_complete +
2977 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2978 }
2979 else
2980 {
2981 /* If no possible candidates, use the cwd */
2982 gchar *sys_curdir = g_get_current_dir ();
2983 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2984
2985 g_free (sys_curdir);
2986
2987 new_dir = open_dir (utf8_curdir, cmpl_state);
2988
2989 if (new_dir)
2990 *remaining_text = text_to_complete;
2991
2992 g_free (utf8_curdir);
2993 }
2994
2995 g_free (tmp);
2996 }
2997 else
2998 {
2999 *remaining_text = text_to_complete;
3000
3001 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
3002 }
3003
3004 if (new_dir)
3005 {
3006 new_dir->cmpl_index = -1;
3007 new_dir->cmpl_parent = NULL;
3008 }
3009
3010 return new_dir;
3011 }
3012
3013 #ifdef HAVE_PWD_H
3014
3015 /* open a directory by user name */
3016 static CompletionDir*
3017 open_user_dir (const gchar *text_to_complete,
3018 CompletionState *cmpl_state)
3019 {
3020 CompletionDir *result;
3021 gchar *first_slash;
3022 gint cmp_len;
3023
3024 g_assert (text_to_complete && text_to_complete[0] == '~');
3025
3026 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3027
3028 if (first_slash)
3029 cmp_len = first_slash - text_to_complete - 1;
3030 else
3031 cmp_len = strlen (text_to_complete + 1);
3032
3033 if (!cmp_len)
3034 {
3035 /* ~/ */
3036 const gchar *homedir = g_get_home_dir ();
3037 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
3038
3039 if (utf8_homedir)
3040 result = open_dir (utf8_homedir, cmpl_state);
3041 else
3042 result = NULL;
3043
3044 g_free (utf8_homedir);
3045 }
3046 else
3047 {
3048 /* ~user/ */
3049 gchar* copy = g_new (char, cmp_len + 1);
3050 gchar *utf8_dir;
3051 struct passwd *pwd;
3052
3053 strncpy (copy, text_to_complete + 1, cmp_len);
3054 copy[cmp_len] = 0;
3055 pwd = getpwnam (copy);
3056 g_free (copy);
3057 if (!pwd)
3058 {
3059 cmpl_errno = errno;
3060 return NULL;
3061 }
3062 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
3063 result = open_dir (utf8_dir, cmpl_state);
3064 g_free (utf8_dir);
3065 }
3066 return result;
3067 }
3068
3069 #endif
3070
3071 /* open a directory relative the the current relative directory */
3072 static CompletionDir*
3073 open_relative_dir (gchar *dir_name,
3074 CompletionDir *dir,
3075 CompletionState *cmpl_state)
3076 {
3077 CompletionDir *result;
3078 GString *path;
3079
3080 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
3081 g_string_assign (path, dir->fullname);
3082
3083 if (dir->fullname_len > 1
3084 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
3085 g_string_append_c (path, G_DIR_SEPARATOR);
3086 g_string_append (path, dir_name);
3087
3088 result = open_dir (path->str, cmpl_state);
3089
3090 g_string_free (path, TRUE);
3091
3092 return result;
3093 }
3094
3095 /* after the cache lookup fails, really open a new directory */
3096 static CompletionDirSent*
3097 open_new_dir (gchar *dir_name,
3098 struct stat *sbuf,
3099 gboolean stat_subdirs)
3100 {
3101 CompletionDirSent *sent;
3102 GDir *directory;
3103 const char *dirent;
3104 GError *error = NULL;
3105 gint entry_count = 0;
3106 gint n_entries = 0;
3107 gint i;
3108 struct stat ent_sbuf;
3109 GString *path;
3110 gchar *sys_dir_name;
3111
3112 sent = g_new (CompletionDirSent, 1);
3113 sent->mtime = sbuf->st_mtime;
3114 sent->inode = sbuf->st_ino;
3115 sent->device = sbuf->st_dev;
3116
3117 path = g_string_sized_new (2*MAXPATHLEN + 10);
3118
3119 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3120 if (!sys_dir_name)
3121 {
3122 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3123 return NULL;
3124 }
3125
3126 directory = g_dir_open (sys_dir_name, 0, &error);
3127 if (!directory)
3128 {
3129 cmpl_errno = error->code; /* ??? */
3130 g_free (sys_dir_name);
3131 return NULL;
3132 }
3133
3134 while ((dirent = g_dir_read_name (directory)) != NULL)
3135 entry_count++;
3136
3137 entry_count += 2; /* For ".",".." */
3138
3139 sent->entries = g_new (CompletionDirEntry, entry_count);
3140 sent->entry_count = entry_count;
3141
3142 g_dir_rewind (directory);
3143
3144 for (i = 0; i < entry_count; i += 1)
3145 {
3146 GError *error = NULL;
3147
3148 if (i == 0)
3149 dirent = ".";
3150 else if (i == 1)
3151 dirent = "..";
3152 else
3153 {
3154 dirent = g_dir_read_name (directory);
3155 if (!dirent) /* Directory changed */
3156 break;
3157 }
3158
3159 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3160 if (sent->entries[n_entries].entry_name == NULL
3161 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3162 {
3163 gchar *escaped_str = g_strescape (dirent, NULL);
3164 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3165 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3166 escaped_str,
3167 error->message ? error->message : _("Invalid Utf-8"));
3168 g_free (escaped_str);
3169 g_clear_error (&error);
3170 continue;
3171 }
3172 g_clear_error (&error);
3173
3174 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3175
3176 g_string_assign (path, sys_dir_name);
3177 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3178 {
3179 g_string_append_c (path, G_DIR_SEPARATOR);
3180 }
3181 g_string_append (path, dirent);
3182
3183 if (stat_subdirs)
3184 {
3185 /* Here we know path->str is a "system charset" string */
3186 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3187 sent->entries[n_entries].is_dir = TRUE;
3188 else
3189 /* stat may fail, and we don't mind, since it could be a
3190 * dangling symlink. */
3191 sent->entries[n_entries].is_dir = FALSE;
3192 }
3193 else
3194 sent->entries[n_entries].is_dir = 1;
3195
3196 n_entries++;
3197 }
3198 sent->entry_count = n_entries;
3199
3200 g_free (sys_dir_name);
3201 g_string_free (path, TRUE);
3202 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3203
3204 g_dir_close (directory);
3205
3206 return sent;
3207 }
3208
3209 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3210
3211 static gboolean
3212 check_dir (gchar *dir_name,
3213 struct stat *result,
3214 gboolean *stat_subdirs)
3215 {
3216 /* A list of directories that we know only contain other directories.
3217 * Trying to stat every file in these directories would be very
3218 * expensive.
3219 */
3220
3221 static struct {
3222 gchar *name;
3223 gboolean present;
3224 struct stat statbuf;
3225 } no_stat_dirs[] = {
3226 { "/afs", FALSE, { 0 } },
3227 { "/net", FALSE, { 0 } }
3228 };
3229
3230 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3231 static gboolean initialized = FALSE;
3232 gchar *sys_dir_name;
3233 gint i;
3234
3235 if (!initialized)
3236 {
3237 initialized = TRUE;
3238 for (i = 0; i < n_no_stat_dirs; i++)
3239 {
3240 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3241 no_stat_dirs[i].present = TRUE;
3242 }
3243 }
3244
3245 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3246 if (!sys_dir_name)
3247 {
3248 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3249 return FALSE;
3250 }
3251
3252 if (stat (sys_dir_name, result) < 0)
3253 {
3254 g_free (sys_dir_name);
3255 cmpl_errno = errno;
3256 return FALSE;
3257 }
3258 g_free (sys_dir_name);
3259
3260 *stat_subdirs = TRUE;
3261 for (i = 0; i < n_no_stat_dirs; i++)
3262 {
3263 if (no_stat_dirs[i].present &&
3264 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3265 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3266 {
3267 *stat_subdirs = FALSE;
3268 break;
3269 }
3270 }
3271
3272 return TRUE;
3273 }
3274
3275 #endif
3276
3277 /* open a directory by absolute pathname */
3278 static CompletionDir*
3279 open_dir (gchar *dir_name,
3280 CompletionState *cmpl_state)
3281 {
3282 struct stat sbuf;
3283 gboolean stat_subdirs;
3284 CompletionDirSent *sent;
3285 GList* cdsl;
3286
3287 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3288 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3289 return NULL;
3290
3291 cdsl = cmpl_state->directory_sent_storage;
3292
3293 while (cdsl)
3294 {
3295 sent = cdsl->data;
3296
3297 if (sent->inode == sbuf.st_ino &&
3298 sent->mtime == sbuf.st_mtime &&
3299 sent->device == sbuf.st_dev)
3300 return attach_dir (sent, dir_name, cmpl_state);
3301
3302 cdsl = cdsl->next;
3303 }
3304 #else
3305 stat_subdirs = TRUE;
3306 #endif
3307
3308 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3309
3310 if (sent)
3311 {
3312 cmpl_state->directory_sent_storage =
3313 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3314
3315 return attach_dir (sent, dir_name, cmpl_state);
3316 }
3317
3318 return NULL;
3319 }
3320
3321 static CompletionDir*
3322 attach_dir (CompletionDirSent *sent,
3323 gchar *dir_name,
3324 CompletionState *cmpl_state)
3325 {
3326 CompletionDir* new_dir;
3327
3328 new_dir = g_new (CompletionDir, 1);
3329
3330 cmpl_state->directory_storage =
3331 g_list_prepend (cmpl_state->directory_storage, new_dir);
3332
3333 new_dir->sent = sent;
3334 new_dir->fullname = g_strdup (dir_name);
3335 new_dir->fullname_len = strlen (dir_name);
3336 new_dir->cmpl_text = NULL;
3337
3338 return new_dir;
3339 }
3340
3341 static gint
3342 correct_dir_fullname (CompletionDir* cmpl_dir)
3343 {
3344 gint length = strlen (cmpl_dir->fullname);
3345 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3346 gchar *sys_filename;
3347 struct stat sbuf;
3348
3349 /* Does it end with /. (\.) ? */
3350 if (length >= 2 &&
3351 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3352 {
3353 /* Is it just the root directory (on a drive) ? */
3354 if (cmpl_dir->fullname + length - 2 == first_slash)
3355 {
3356 cmpl_dir->fullname[length - 1] = 0;
3357 cmpl_dir->fullname_len = length - 1;
3358 return TRUE;
3359 }
3360 else
3361 {
3362 cmpl_dir->fullname[length - 2] = 0;
3363 }
3364 }
3365
3366 /* Ends with /./ (\.\)? */
3367 else if (length >= 3 &&
3368 strcmp (cmpl_dir->fullname + length - 3,
3369 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3370 cmpl_dir->fullname[length - 2] = 0;
3371
3372 /* Ends with /.. (\..) ? */
3373 else if (length >= 3 &&
3374 strcmp (cmpl_dir->fullname + length - 3,
3375 G_DIR_SEPARATOR_S "..") == 0)
3376 {
3377 /* Is it just /.. (X:\..)? */
3378 if (cmpl_dir->fullname + length - 3 == first_slash)
3379 {
3380 cmpl_dir->fullname[length - 2] = 0;
3381 cmpl_dir->fullname_len = length - 2;
3382 return TRUE;
3383 }
3384
3385 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3386 if (!sys_filename)
3387 {
3388 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3389 return FALSE;
3390 }
3391
3392 if (stat (sys_filename, &sbuf) < 0)
3393 {
3394 g_free (sys_filename);
3395 cmpl_errno = errno;
3396 return FALSE;
3397 }
3398 g_free (sys_filename);
3399
3400 cmpl_dir->fullname[length - 3] = 0;
3401
3402 if (!correct_parent (cmpl_dir, &sbuf))
3403 return FALSE;
3404 }
3405
3406 /* Ends with /../ (\..\)? */
3407 else if (length >= 4 &&
3408 strcmp (cmpl_dir->fullname + length - 4,
3409 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3410 {
3411 /* Is it just /../ (X:\..\)? */
3412 if (cmpl_dir->fullname + length - 4 == first_slash)
3413 {
3414 cmpl_dir->fullname[length - 3] = 0;
3415 cmpl_dir->fullname_len = length - 3;
3416 return TRUE;
3417 }
3418
3419 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3420 if (!sys_filename)
3421 {
3422 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3423 return FALSE;
3424 }
3425
3426 if (stat (sys_filename, &sbuf) < 0)
3427 {
3428 g_free (sys_filename);
3429 cmpl_errno = errno;
3430 return FALSE;
3431 }
3432 g_free (sys_filename);
3433
3434 cmpl_dir->fullname[length - 4] = 0;
3435
3436 if (!correct_parent (cmpl_dir, &sbuf))
3437 return FALSE;
3438 }
3439
3440 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3441
3442 return TRUE;
3443 }
3444
3445 static gint
3446 correct_parent (CompletionDir *cmpl_dir,
3447 struct stat *sbuf)
3448 {
3449 struct stat parbuf;
3450 gchar *last_slash;
3451 gchar *first_slash;
3452 gchar *new_name;
3453 gchar *sys_filename;
3454 gchar c = 0;
3455
3456 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3457 g_assert (last_slash);
3458 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3459
3460 /* Clever (?) way to check for top-level directory that works also on
3461 * Win32, where there is a drive letter and colon prefixed...
3462 */
3463 if (last_slash != first_slash)
3464 {
3465 last_slash[0] = 0;
3466 }
3467 else
3468 {
3469 c = last_slash[1];
3470 last_slash[1] = 0;
3471 }
3472
3473 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3474 if (!sys_filename)
3475 {
3476 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3477 if (!c)
3478 last_slash[0] = G_DIR_SEPARATOR;
3479 return FALSE;
3480 }
3481
3482 if (stat (sys_filename, &parbuf) < 0)
3483 {
3484 g_free (sys_filename);
3485 cmpl_errno = errno;
3486 if (!c)
3487 last_slash[0] = G_DIR_SEPARATOR;
3488 return FALSE;
3489 }
3490 g_free (sys_filename);
3491
3492 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3493 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3494 /* it wasn't a link */
3495 return TRUE;
3496
3497 if (c)
3498 last_slash[1] = c;
3499 else
3500 last_slash[0] = G_DIR_SEPARATOR;
3501
3502 /* it was a link, have to figure it out the hard way */
3503
3504 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3505
3506 if (!new_name)
3507 return FALSE;
3508
3509 g_free (cmpl_dir->fullname);
3510
3511 cmpl_dir->fullname = new_name;
3512 #endif
3513
3514 return TRUE;
3515 }
3516
3517 #ifndef G_OS_WIN32
3518
3519 static gchar*
3520 find_parent_dir_fullname (gchar* dirname)
3521 {
3522 gchar *sys_orig_dir;
3523 gchar *result;
3524 gchar *sys_cwd;
3525 gchar *sys_dirname;
3526
3527 sys_orig_dir = g_get_current_dir ();
3528 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3529 if (!sys_dirname)
3530 {
3531 g_free (sys_orig_dir);
3532 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3533 return NULL;
3534 }
3535
3536 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3537 {
3538 cmpl_errno = errno;
3539 chdir (sys_orig_dir);
3540 g_free (sys_dirname);
3541 g_free (sys_orig_dir);
3542 return NULL;
3543 }
3544 g_free (sys_dirname);
3545
3546 sys_cwd = g_get_current_dir ();
3547 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3548 g_free (sys_cwd);
3549
3550 if (chdir (sys_orig_dir) != 0)
3551 {
3552 cmpl_errno = errno;
3553 g_free (sys_orig_dir);
3554 return NULL;
3555 }
3556
3557 g_free (sys_orig_dir);
3558 return result;
3559 }
3560
3561 #endif
3562
3563 /**********************************************************************/
3564 /* Completion Operations */
3565 /**********************************************************************/
3566
3567 #ifdef HAVE_PWD_H
3568
3569 static PossibleCompletion*
3570 attempt_homedir_completion (gchar *text_to_complete,
3571 CompletionState *cmpl_state)
3572 {
3573 gint index, length;
3574
3575 if (!cmpl_state->user_dir_name_buffer &&
3576 !get_pwdb (cmpl_state))
3577 return NULL;
3578 length = strlen (text_to_complete) - 1;
3579
3580 cmpl_state->user_completion_index += 1;
3581
3582 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3583 {
3584 index = first_diff_index (text_to_complete + 1,
3585 cmpl_state->user_directories
3586 [cmpl_state->user_completion_index].login);
3587
3588 switch (index)
3589 {
3590 case PATTERN_MATCH:
3591 break;
3592 default:
3593 if (cmpl_state->last_valid_char < (index + 1))
3594 cmpl_state->last_valid_char = index + 1;
3595 cmpl_state->user_completion_index += 1;
3596 continue;
3597 }
3598
3599 cmpl_state->the_completion.is_a_completion = 1;
3600 cmpl_state->the_completion.is_directory = TRUE;
3601
3602 append_completion_text ("~", cmpl_state);
3603
3604 append_completion_text (cmpl_state->
3605 user_directories[cmpl_state->user_completion_index].login,
3606 cmpl_state);
3607
3608 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3609 }
3610
3611 if (text_to_complete[1]
3612 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3613 {
3614 cmpl_state->user_completion_index = -1;
3615 return NULL;
3616 }
3617 else
3618 {
3619 cmpl_state->user_completion_index += 1;
3620 cmpl_state->the_completion.is_a_completion = 1;
3621 cmpl_state->the_completion.is_directory = TRUE;
3622
3623 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3624 }
3625 }
3626
3627 #endif
3628
3629 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3630 #define FOLD(c) (tolower(c))
3631 #else
3632 #define FOLD(c) (c)
3633 #endif
3634
3635 /* returns the index (>= 0) of the first differing character,
3636 * PATTERN_MATCH if the completion matches */
3637 static gint
3638 first_diff_index (gchar *pat,
3639 gchar *text)
3640 {
3641 gint diff = 0;
3642
3643 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3644 {
3645 pat += 1;
3646 text += 1;
3647 diff += 1;
3648 }
3649
3650 if (*pat)
3651 return diff;
3652
3653 return PATTERN_MATCH;
3654 }
3655
3656 static PossibleCompletion*
3657 append_completion_text (gchar *text,
3658 CompletionState *cmpl_state)
3659 {
3660 gint len, i = 1;
3661
3662 if (!cmpl_state->the_completion.text)
3663 return NULL;
3664
3665 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3666
3667 if (cmpl_state->the_completion.text_alloc > len)
3668 {
3669 strcat (cmpl_state->the_completion.text, text);
3670 return &cmpl_state->the_completion;
3671 }
3672
3673 while (i < len)
3674 i <<= 1;
3675
3676 cmpl_state->the_completion.text_alloc = i;
3677
3678 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3679
3680 if (!cmpl_state->the_completion.text)
3681 return NULL;
3682 else
3683 {
3684 strcat (cmpl_state->the_completion.text, text);
3685 return &cmpl_state->the_completion;
3686 }
3687 }
3688
3689 static CompletionDir*
3690 find_completion_dir (gchar *text_to_complete,
3691 gchar **remaining_text,
3692 CompletionState *cmpl_state)
3693 {
3694 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3695 CompletionDir* dir = cmpl_state->reference_dir;
3696 CompletionDir* next;
3697 *remaining_text = text_to_complete;
3698
3699 while (first_slash)
3700 {
3701 gint len = first_slash - *remaining_text;
3702 gint found = 0;
3703 gchar *found_name = NULL; /* Quiet gcc */
3704 gint i;
3705 gchar* pat_buf = g_new (gchar, len + 1);
3706
3707 strncpy (pat_buf, *remaining_text, len);
3708 pat_buf[len] = 0;
3709
3710 for (i = 0; i < dir->sent->entry_count; i += 1)
3711 {
3712 if (dir->sent->entries[i].is_dir &&
3713 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name))
3714 {
3715 if (found)
3716 {
3717 g_free (pat_buf);
3718 return dir;
3719 }
3720 else
3721 {
3722 found = 1;
3723 found_name = dir->sent->entries[i].entry_name;
3724 }
3725 }
3726 }
3727
3728 if (!found)
3729 {
3730 /* Perhaps we are trying to open an automount directory */
3731 found_name = pat_buf;
3732 }
3733
3734 next = open_relative_dir (found_name, dir, cmpl_state);
3735
3736 if (!next)
3737 {
3738 g_free (pat_buf);
3739 return NULL;
3740 }
3741
3742 next->cmpl_parent = dir;
3743
3744 dir = next;
3745
3746 if (!correct_dir_fullname (dir))
3747 {
3748 g_free (pat_buf);
3749 return NULL;
3750 }
3751
3752 *remaining_text = first_slash + 1;
3753 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3754
3755 g_free (pat_buf);
3756 }
3757
3758 return dir;
3759 }
3760
3761 static void
3762 update_cmpl (PossibleCompletion *poss,
3763 CompletionState *cmpl_state)
3764 {
3765 gint cmpl_len;
3766
3767 if (!poss || !cmpl_is_a_completion (poss))
3768 return;
3769
3770 cmpl_len = strlen (cmpl_this_completion (poss));
3771
3772 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3773 {
3774 cmpl_state->updated_text =
3775 (gchar*)g_realloc (cmpl_state->updated_text,
3776 cmpl_state->updated_text_alloc);
3777 cmpl_state->updated_text_alloc = 2*cmpl_len;
3778 }
3779
3780 if (cmpl_state->updated_text_len < 0)
3781 {
3782 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3783 cmpl_state->updated_text_len = cmpl_len;
3784 cmpl_state->re_complete = cmpl_is_directory (poss);
3785 }
3786 else if (cmpl_state->updated_text_len == 0)
3787 {
3788 cmpl_state->re_complete = FALSE;
3789 }
3790 else
3791 {
3792 gint first_diff =
3793 first_diff_index (cmpl_state->updated_text,
3794 cmpl_this_completion (poss));
3795
3796 cmpl_state->re_complete = FALSE;
3797
3798 if (first_diff == PATTERN_MATCH)
3799 return;
3800
3801 if (first_diff > cmpl_state->updated_text_len)
3802 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3803
3804 cmpl_state->updated_text_len = first_diff;
3805 cmpl_state->updated_text[first_diff] = 0;
3806 }
3807 }
3808
3809 static PossibleCompletion*
3810 attempt_dir_completion (CompletionState *cmpl_state)
3811 {
3812 gchar *pat_buf, *first_slash;
3813 CompletionDir *dir = cmpl_state->active_completion_dir;
3814
3815 dir->cmpl_index += 1;
3816
3817 if (dir->cmpl_index == dir->sent->entry_count)
3818 {
3819 if (dir->cmpl_parent == NULL)
3820 {
3821 cmpl_state->active_completion_dir = NULL;
3822
3823 return NULL;
3824 }
3825 else
3826 {
3827 cmpl_state->active_completion_dir = dir->cmpl_parent;
3828
3829 return attempt_dir_completion (cmpl_state);
3830 }
3831 }
3832
3833 g_assert (dir->cmpl_text);
3834
3835 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3836
3837 if (first_slash)
3838 {
3839 gint len = first_slash - dir->cmpl_text;
3840
3841 pat_buf = g_new (gchar, len + 1);
3842 strncpy (pat_buf, dir->cmpl_text, len);
3843 pat_buf[len] = 0;
3844 }
3845 else
3846 {
3847 gint len = strlen (dir->cmpl_text);
3848
3849 pat_buf = g_new (gchar, len + 2);
3850 strcpy (pat_buf, dir->cmpl_text);
3851 /* Don't append a * if the user entered one herself.
3852 * This way one can complete *.h and don't get matches
3853 * on any .help files, for instance.
3854 */
3855 if (strchr (pat_buf, '*') == NULL)
3856 strcpy (pat_buf + len, "*");
3857 }
3858
3859 if (first_slash)
3860 {
3861 if (dir->sent->entries[dir->cmpl_index].is_dir)
3862 {
3863 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name))
3864 {
3865 CompletionDir* new_dir;
3866
3867 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3868 dir, cmpl_state);
3869
3870 if (!new_dir)
3871 {
3872 g_free (pat_buf);
3873 return NULL;
3874 }
3875
3876 new_dir->cmpl_parent = dir;
3877
3878 new_dir->cmpl_index = -1;
3879 new_dir->cmpl_text = g_strdup (first_slash + 1);
3880
3881 cmpl_state->active_completion_dir = new_dir;
3882
3883 g_free (pat_buf);
3884 return attempt_dir_completion (cmpl_state);
3885 }
3886 else
3887 {
3888 g_free (pat_buf);
3889 return attempt_dir_completion (cmpl_state);
3890 }
3891 }
3892 else
3893 {
3894 g_free (pat_buf);
3895 return attempt_dir_completion (cmpl_state);
3896 }
3897 }
3898 else
3899 {
3900 if (dir->cmpl_parent != NULL)
3901 {
3902 append_completion_text (dir->fullname +
3903 strlen (cmpl_state->completion_dir->fullname) + 1,
3904 cmpl_state);
3905 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3906 }
3907
3908 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3909
3910 cmpl_state->the_completion.is_a_completion =
3911 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name);
3912
3913 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3914 if (dir->sent->entries[dir->cmpl_index].is_dir)
3915 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3916
3917 g_free (pat_buf);
3918 return &cmpl_state->the_completion;
3919 }
3920 }
3921
3922 #ifdef HAVE_PWD_H
3923
3924 static gint
3925 get_pwdb (CompletionState* cmpl_state)
3926 {
3927 struct passwd *pwd_ptr;
3928 gchar* buf_ptr;
3929 gchar *utf8;
3930 gint len = 0, i, count = 0;
3931
3932 if (cmpl_state->user_dir_name_buffer)
3933 return TRUE;
3934 setpwent ();
3935
3936 while ((pwd_ptr = getpwent ()) != NULL)
3937 {
3938 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3939 len += strlen (utf8);
3940 g_free (utf8);
3941 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3942 len += strlen (utf8);
3943 g_free (utf8);
3944 len += 2;
3945 count += 1;
3946 }
3947
3948 setpwent ();
3949
3950 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3951 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3952 cmpl_state->user_directories_len = count;
3953
3954 buf_ptr = cmpl_state->user_dir_name_buffer;
3955
3956 for (i = 0; i < count; i += 1)
3957 {
3958 pwd_ptr = getpwent ();
3959 if (!pwd_ptr)
3960 {
3961 cmpl_errno = errno;
3962 goto error;
3963 }
3964
3965 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3966 strcpy (buf_ptr, utf8);
3967 g_free (utf8);
3968
3969 cmpl_state->user_directories[i].login = buf_ptr;
3970
3971 buf_ptr += strlen (buf_ptr);
3972 buf_ptr += 1;
3973
3974 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3975 strcpy (buf_ptr, utf8);
3976 g_free (utf8);
3977
3978 cmpl_state->user_directories[i].homedir = buf_ptr;
3979
3980 buf_ptr += strlen (buf_ptr);
3981 buf_ptr += 1;
3982 }
3983
3984 qsort (cmpl_state->user_directories,
3985 cmpl_state->user_directories_len,
3986 sizeof (CompletionUserDir),
3987 compare_user_dir);
3988
3989 endpwent ();
3990
3991 return TRUE;
3992
3993 error:
3994
3995 if (cmpl_state->user_dir_name_buffer)
3996 g_free (cmpl_state->user_dir_name_buffer);
3997 if (cmpl_state->user_directories)
3998 g_free (cmpl_state->user_directories);
3999
4000 cmpl_state->user_dir_name_buffer = NULL;
4001 cmpl_state->user_directories = NULL;
4002
4003 return FALSE;
4004 }
4005
4006 static gint
4007 compare_user_dir (const void *a,
4008 const void *b)
4009 {
4010 return strcmp ((((CompletionUserDir*)a))->login,
4011 (((CompletionUserDir*)b))->login);
4012 }
4013
4014 #endif
4015
4016 static gint
4017 compare_cmpl_dir (const void *a,
4018 const void *b)
4019 {
4020
4021 return strcmp (((CompletionDirEntry*)a)->sort_key,
4022 (((CompletionDirEntry*)b))->sort_key);
4023 }
4024
4025 static gint
4026 cmpl_state_okay (CompletionState* cmpl_state)
4027 {
4028 return cmpl_state && cmpl_state->reference_dir;
4029 }
4030
4031 static const gchar*
4032 cmpl_strerror (gint err)
4033 {
4034 if (err == CMPL_ERRNO_TOO_LONG)
4035 return _("Name too long");
4036 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
4037 return _("Couldn't convert filename");
4038 else
4039 return g_strerror (err);
4040 }
4041
4042 gchar * gtk_dir_selection_get_dir (GtkDirSelection *filesel)
4043 {
4044 return gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
4045 }
This page took 0.118557 seconds and 4 git commands to generate.