warning fix
[lttv.git] / ltt / branches / poly / lttv / modules / gui / main / 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 const gchar * err;
2464 gchar str[256];
2465 err = gtk_label_get_text (GTK_LABEL (fs->selection_text));
2466 err += 11; //pass over "Selection: "
2467 sprintf(str,"%s\0",err);
2468
2469
2470 if (fs->last_selected != NULL)
2471 g_free (fs->last_selected);
2472
2473 fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
2474 filename = get_real_filename (fs->last_selected, FALSE);
2475
2476 strcat(str,"/");
2477 strcat(str,filename);
2478 str[strlen(str)-1] = '\0';
2479
2480 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), str);
2481
2482 if (filename != fs->last_selected)
2483 g_free (filename);
2484
2485 return;
2486 }
2487
2488 maybe_clear_entry:
2489
2490 entry = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
2491 if ((entry != NULL) && (fs->last_selected != NULL) &&
2492 (compare_filenames (entry, fs->last_selected) == 0))
2493 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
2494 }
2495
2496 /**
2497 * gtk_dir_selection_get_selections:
2498 * @filesel: a #GtkDirSelection
2499 *
2500 * Retrieves the list of file selections the user has made in the dialog box.
2501 * This function is intended for use when the user can select multiple files
2502 * in the file list. The first file in the list is equivalent to what
2503 * gtk_dir_selection_get_filename() would return.
2504 *
2505 * The filenames are in the encoding of g_filename_from_utf8(), which may or
2506 * may not be the same as that used by GTK+ (UTF-8). To convert to UTF-8, call
2507 * g_filename_to_utf8() on each string.
2508 *
2509 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2510 * g_strfreev() to free it.
2511 **/
2512 gchar **
2513 gtk_dir_selection_get_selections (GtkDirSelection *filesel)
2514 {
2515 GPtrArray *names;
2516 gchar **selections;
2517 gchar *filename, *dirname;
2518 gchar *current, *buf;
2519 gint i, count;
2520 gboolean unselected_entry;
2521
2522 g_return_val_if_fail (GTK_IS_DIR_SELECTION (filesel), NULL);
2523
2524 filename = g_strdup (gtk_dir_selection_get_filename (filesel));
2525
2526 if (strlen (filename) == 0)
2527 {
2528 g_free (filename);
2529 return NULL;
2530 }
2531
2532 names = filesel->selected_names;
2533
2534 if (names != NULL)
2535 selections = g_new (gchar *, names->len + 2);
2536 else
2537 selections = g_new (gchar *, 2);
2538
2539 count = 0;
2540 unselected_entry = TRUE;
2541
2542 if (names != NULL)
2543 {
2544 dirname = g_path_get_dirname (filename);
2545
2546 for (i = 0; i < names->len; i++)
2547 {
2548 buf = g_filename_from_utf8 (g_ptr_array_index (names, i), -1,
2549 NULL, NULL, NULL);
2550 current = g_build_filename (dirname, buf, NULL);
2551 g_free (buf);
2552
2553 selections[count++] = current;
2554
2555 if (unselected_entry && compare_filenames (current, filename) == 0)
2556 unselected_entry = FALSE;
2557 }
2558
2559 g_free (dirname);
2560 }
2561
2562 if (unselected_entry)
2563 selections[count++] = filename;
2564 else
2565 g_free (filename);
2566
2567 selections[count] = NULL;
2568
2569 return selections;
2570 }
2571
2572 /**********************************************************************/
2573 /* External Interface */
2574 /**********************************************************************/
2575
2576 /* The four completion state selectors
2577 */
2578 static gchar*
2579 cmpl_updated_text (CompletionState *cmpl_state)
2580 {
2581 return cmpl_state->updated_text;
2582 }
2583
2584 static gboolean
2585 cmpl_updated_dir (CompletionState *cmpl_state)
2586 {
2587 return cmpl_state->re_complete;
2588 }
2589
2590 static gchar*
2591 cmpl_reference_position (CompletionState *cmpl_state)
2592 {
2593 return cmpl_state->reference_dir->fullname;
2594 }
2595
2596 static gint
2597 cmpl_last_valid_char (CompletionState *cmpl_state)
2598 {
2599 return cmpl_state->last_valid_char;
2600 }
2601
2602 static const gchar*
2603 cmpl_completion_fullname (const gchar *text,
2604 CompletionState *cmpl_state)
2605 {
2606 static const char nothing[2] = "";
2607
2608 if (!cmpl_state_okay (cmpl_state))
2609 {
2610 return nothing;
2611 }
2612 else if (g_path_is_absolute (text))
2613 {
2614 strcpy (cmpl_state->updated_text, text);
2615 }
2616 #ifdef HAVE_PWD_H
2617 else if (text[0] == '~')
2618 {
2619 CompletionDir* dir;
2620 char* slash;
2621
2622 dir = open_user_dir (text, cmpl_state);
2623
2624 if (!dir)
2625 {
2626 /* spencer says just return ~something, so
2627 * for now just do it. */
2628 strcpy (cmpl_state->updated_text, text);
2629 }
2630 else
2631 {
2632
2633 strcpy (cmpl_state->updated_text, dir->fullname);
2634
2635 slash = strchr (text, G_DIR_SEPARATOR);
2636
2637 if (slash)
2638 strcat (cmpl_state->updated_text, slash);
2639 }
2640 }
2641 #endif
2642 else
2643 {
2644 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
2645 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
2646 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
2647 strcat (cmpl_state->updated_text, text);
2648 }
2649
2650 return cmpl_state->updated_text;
2651 }
2652
2653 /* The three completion selectors
2654 */
2655 static gchar*
2656 cmpl_this_completion (PossibleCompletion* pc)
2657 {
2658 return pc->text;
2659 }
2660
2661 static gboolean
2662 cmpl_is_directory (PossibleCompletion* pc)
2663 {
2664 return pc->is_directory;
2665 }
2666
2667 static gint
2668 cmpl_is_a_completion (PossibleCompletion* pc)
2669 {
2670 return pc->is_a_completion;
2671 }
2672
2673 /**********************************************************************/
2674 /* Construction, deletion */
2675 /**********************************************************************/
2676
2677 static CompletionState*
2678 cmpl_init_state (void)
2679 {
2680 gchar *sys_getcwd_buf;
2681 gchar *utf8_cwd;
2682 CompletionState *new_state;
2683
2684 new_state = g_new (CompletionState, 1);
2685
2686 /* g_get_current_dir() returns a string in the "system" charset */
2687 sys_getcwd_buf = g_get_current_dir ();
2688 utf8_cwd = g_filename_to_utf8 (sys_getcwd_buf, -1, NULL, NULL, NULL);
2689 g_free (sys_getcwd_buf);
2690
2691 tryagain:
2692
2693 new_state->reference_dir = NULL;
2694 new_state->completion_dir = NULL;
2695 new_state->active_completion_dir = NULL;
2696 new_state->directory_storage = NULL;
2697 new_state->directory_sent_storage = NULL;
2698 new_state->last_valid_char = 0;
2699 new_state->updated_text = g_new (gchar, MAXPATHLEN);
2700 new_state->updated_text_alloc = MAXPATHLEN;
2701 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
2702 new_state->the_completion.text_alloc = MAXPATHLEN;
2703 new_state->user_dir_name_buffer = NULL;
2704 new_state->user_directories = NULL;
2705
2706 new_state->reference_dir = open_dir (utf8_cwd, new_state);
2707
2708 if (!new_state->reference_dir)
2709 {
2710 /* Directories changing from underneath us, grumble */
2711 strcpy (utf8_cwd, G_DIR_SEPARATOR_S);
2712 goto tryagain;
2713 }
2714
2715 g_free (utf8_cwd);
2716 return new_state;
2717 }
2718
2719 static void
2720 cmpl_free_dir_list (GList* dp0)
2721 {
2722 GList *dp = dp0;
2723
2724 while (dp)
2725 {
2726 free_dir (dp->data);
2727 dp = dp->next;
2728 }
2729
2730 g_list_free (dp0);
2731 }
2732
2733 static void
2734 cmpl_free_dir_sent_list (GList* dp0)
2735 {
2736 GList *dp = dp0;
2737
2738 while (dp)
2739 {
2740 free_dir_sent (dp->data);
2741 dp = dp->next;
2742 }
2743
2744 g_list_free (dp0);
2745 }
2746
2747 static void
2748 cmpl_free_state (CompletionState* cmpl_state)
2749 {
2750 g_return_if_fail (cmpl_state != NULL);
2751
2752 cmpl_free_dir_list (cmpl_state->directory_storage);
2753 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
2754
2755 if (cmpl_state->user_dir_name_buffer)
2756 g_free (cmpl_state->user_dir_name_buffer);
2757 if (cmpl_state->user_directories)
2758 g_free (cmpl_state->user_directories);
2759 if (cmpl_state->the_completion.text)
2760 g_free (cmpl_state->the_completion.text);
2761 if (cmpl_state->updated_text)
2762 g_free (cmpl_state->updated_text);
2763
2764 g_free (cmpl_state);
2765 }
2766
2767 static void
2768 free_dir (CompletionDir* dir)
2769 {
2770 g_free (dir->cmpl_text);
2771 g_free (dir->fullname);
2772 g_free (dir);
2773 }
2774
2775 static void
2776 free_dir_sent (CompletionDirSent* sent)
2777 {
2778 gint i;
2779 for (i = 0; i < sent->entry_count; i++)
2780 {
2781 g_free (sent->entries[i].entry_name);
2782 g_free (sent->entries[i].sort_key);
2783 }
2784 g_free (sent->entries);
2785 g_free (sent);
2786 }
2787
2788 static void
2789 prune_memory_usage (CompletionState *cmpl_state)
2790 {
2791 GList* cdsl = cmpl_state->directory_sent_storage;
2792 GList* cdl = cmpl_state->directory_storage;
2793 GList* cdl0 = cdl;
2794 gint len = 0;
2795
2796 for (; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
2797 cdsl = cdsl->next;
2798
2799 if (cdsl)
2800 {
2801 cmpl_free_dir_sent_list (cdsl->next);
2802 cdsl->next = NULL;
2803 }
2804
2805 cmpl_state->directory_storage = NULL;
2806 while (cdl)
2807 {
2808 if (cdl->data == cmpl_state->reference_dir)
2809 cmpl_state->directory_storage = g_list_prepend (NULL, cdl->data);
2810 else
2811 free_dir (cdl->data);
2812 cdl = cdl->next;
2813 }
2814
2815 g_list_free (cdl0);
2816 }
2817
2818 /**********************************************************************/
2819 /* The main entrances. */
2820 /**********************************************************************/
2821
2822 static PossibleCompletion*
2823 cmpl_completion_matches (gchar *text_to_complete,
2824 gchar **remaining_text,
2825 CompletionState *cmpl_state)
2826 {
2827 gchar* first_slash;
2828 PossibleCompletion *poss;
2829
2830 prune_memory_usage (cmpl_state);
2831
2832 g_assert (text_to_complete != NULL);
2833
2834 cmpl_state->user_completion_index = -1;
2835 cmpl_state->last_completion_text = text_to_complete;
2836 cmpl_state->the_completion.text[0] = 0;
2837 cmpl_state->last_valid_char = 0;
2838 cmpl_state->updated_text_len = -1;
2839 cmpl_state->updated_text[0] = 0;
2840 cmpl_state->re_complete = FALSE;
2841
2842 #ifdef HAVE_PWD_H
2843 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2844
2845 if (text_to_complete[0] == '~' && !first_slash)
2846 {
2847 /* Text starts with ~ and there is no slash, show all the
2848 * home directory completions.
2849 */
2850 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
2851
2852 update_cmpl (poss, cmpl_state);
2853
2854 return poss;
2855 }
2856 #endif
2857 cmpl_state->reference_dir =
2858 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
2859
2860 if (!cmpl_state->reference_dir)
2861 return NULL;
2862
2863 cmpl_state->completion_dir =
2864 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
2865
2866 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
2867
2868 if (!cmpl_state->completion_dir)
2869 return NULL;
2870
2871 cmpl_state->completion_dir->cmpl_index = -1;
2872 cmpl_state->completion_dir->cmpl_parent = NULL;
2873 cmpl_state->completion_dir->cmpl_text = g_strdup (*remaining_text);
2874
2875 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
2876
2877 cmpl_state->reference_dir = cmpl_state->completion_dir;
2878
2879 poss = attempt_dir_completion (cmpl_state);
2880
2881 update_cmpl (poss, cmpl_state);
2882
2883 return poss;
2884 }
2885
2886 static PossibleCompletion*
2887 cmpl_next_completion (CompletionState* cmpl_state)
2888 {
2889 PossibleCompletion* poss = NULL;
2890
2891 cmpl_state->the_completion.text[0] = 0;
2892
2893 #ifdef HAVE_PWD_H
2894 if (cmpl_state->user_completion_index >= 0)
2895 poss = attempt_homedir_completion (cmpl_state->last_completion_text, cmpl_state);
2896 else
2897 poss = attempt_dir_completion (cmpl_state);
2898 #else
2899 poss = attempt_dir_completion (cmpl_state);
2900 #endif
2901
2902 update_cmpl (poss, cmpl_state);
2903
2904 return poss;
2905 }
2906
2907 /**********************************************************************/
2908 /* Directory Operations */
2909 /**********************************************************************/
2910
2911 /* Open the directory where completion will begin from, if possible. */
2912 static CompletionDir*
2913 open_ref_dir (gchar *text_to_complete,
2914 gchar **remaining_text,
2915 CompletionState *cmpl_state)
2916 {
2917 gchar* first_slash;
2918 CompletionDir *new_dir;
2919
2920 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
2921
2922 #ifdef G_WITH_CYGWIN
2923 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
2924 {
2925 char root_dir[5];
2926 g_snprintf (root_dir, sizeof (root_dir), "//%c", text_to_complete[2]);
2927
2928 new_dir = open_dir (root_dir, cmpl_state);
2929
2930 if (new_dir) {
2931 *remaining_text = text_to_complete + 4;
2932 }
2933 }
2934 #else
2935 if (FALSE)
2936 ;
2937 #endif
2938 #ifdef HAVE_PWD_H
2939 else if (text_to_complete[0] == '~')
2940 {
2941 new_dir = open_user_dir (text_to_complete, cmpl_state);
2942
2943 if (new_dir)
2944 {
2945 if (first_slash)
2946 *remaining_text = first_slash + 1;
2947 else
2948 *remaining_text = text_to_complete + strlen (text_to_complete);
2949 }
2950 else
2951 {
2952 return NULL;
2953 }
2954 }
2955 #endif
2956 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2957 {
2958 gchar *tmp = g_strdup (text_to_complete);
2959 gchar *p;
2960
2961 p = tmp;
2962 while (*p && *p != '*' && *p != '?')
2963 p++;
2964
2965 *p = '\0';
2966 p = strrchr (tmp, G_DIR_SEPARATOR);
2967 if (p)
2968 {
2969 if (p == tmp)
2970 p++;
2971
2972 *p = '\0';
2973
2974 new_dir = open_dir (tmp, cmpl_state);
2975
2976 if (new_dir)
2977 *remaining_text = text_to_complete +
2978 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2979 }
2980 else
2981 {
2982 /* If no possible candidates, use the cwd */
2983 gchar *sys_curdir = g_get_current_dir ();
2984 gchar *utf8_curdir = g_filename_to_utf8 (sys_curdir, -1, NULL, NULL, NULL);
2985
2986 g_free (sys_curdir);
2987
2988 new_dir = open_dir (utf8_curdir, cmpl_state);
2989
2990 if (new_dir)
2991 *remaining_text = text_to_complete;
2992
2993 g_free (utf8_curdir);
2994 }
2995
2996 g_free (tmp);
2997 }
2998 else
2999 {
3000 *remaining_text = text_to_complete;
3001
3002 new_dir = open_dir (cmpl_state->reference_dir->fullname, cmpl_state);
3003 }
3004
3005 if (new_dir)
3006 {
3007 new_dir->cmpl_index = -1;
3008 new_dir->cmpl_parent = NULL;
3009 }
3010
3011 return new_dir;
3012 }
3013
3014 #ifdef HAVE_PWD_H
3015
3016 /* open a directory by user name */
3017 static CompletionDir*
3018 open_user_dir (const gchar *text_to_complete,
3019 CompletionState *cmpl_state)
3020 {
3021 CompletionDir *result;
3022 gchar *first_slash;
3023 gint cmp_len;
3024
3025 g_assert (text_to_complete && text_to_complete[0] == '~');
3026
3027 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3028
3029 if (first_slash)
3030 cmp_len = first_slash - text_to_complete - 1;
3031 else
3032 cmp_len = strlen (text_to_complete + 1);
3033
3034 if (!cmp_len)
3035 {
3036 /* ~/ */
3037 const gchar *homedir = g_get_home_dir ();
3038 gchar *utf8_homedir = g_filename_to_utf8 (homedir, -1, NULL, NULL, NULL);
3039
3040 if (utf8_homedir)
3041 result = open_dir (utf8_homedir, cmpl_state);
3042 else
3043 result = NULL;
3044
3045 g_free (utf8_homedir);
3046 }
3047 else
3048 {
3049 /* ~user/ */
3050 gchar* copy = g_new (char, cmp_len + 1);
3051 gchar *utf8_dir;
3052 struct passwd *pwd;
3053
3054 strncpy (copy, text_to_complete + 1, cmp_len);
3055 copy[cmp_len] = 0;
3056 pwd = getpwnam (copy);
3057 g_free (copy);
3058 if (!pwd)
3059 {
3060 cmpl_errno = errno;
3061 return NULL;
3062 }
3063 utf8_dir = g_filename_to_utf8 (pwd->pw_dir, -1, NULL, NULL, NULL);
3064 result = open_dir (utf8_dir, cmpl_state);
3065 g_free (utf8_dir);
3066 }
3067 return result;
3068 }
3069
3070 #endif
3071
3072 /* open a directory relative the the current relative directory */
3073 static CompletionDir*
3074 open_relative_dir (gchar *dir_name,
3075 CompletionDir *dir,
3076 CompletionState *cmpl_state)
3077 {
3078 CompletionDir *result;
3079 GString *path;
3080
3081 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
3082 g_string_assign (path, dir->fullname);
3083
3084 if (dir->fullname_len > 1
3085 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
3086 g_string_append_c (path, G_DIR_SEPARATOR);
3087 g_string_append (path, dir_name);
3088
3089 result = open_dir (path->str, cmpl_state);
3090
3091 g_string_free (path, TRUE);
3092
3093 return result;
3094 }
3095
3096 /* after the cache lookup fails, really open a new directory */
3097 static CompletionDirSent*
3098 open_new_dir (gchar *dir_name,
3099 struct stat *sbuf,
3100 gboolean stat_subdirs)
3101 {
3102 CompletionDirSent *sent;
3103 GDir *directory;
3104 const char *dirent;
3105 GError *error = NULL;
3106 gint entry_count = 0;
3107 gint n_entries = 0;
3108 gint i;
3109 struct stat ent_sbuf;
3110 GString *path;
3111 gchar *sys_dir_name;
3112
3113 sent = g_new (CompletionDirSent, 1);
3114 sent->mtime = sbuf->st_mtime;
3115 sent->inode = sbuf->st_ino;
3116 sent->device = sbuf->st_dev;
3117
3118 path = g_string_sized_new (2*MAXPATHLEN + 10);
3119
3120 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3121 if (!sys_dir_name)
3122 {
3123 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3124 return NULL;
3125 }
3126
3127 directory = g_dir_open (sys_dir_name, 0, &error);
3128 if (!directory)
3129 {
3130 cmpl_errno = error->code; /* ??? */
3131 g_free (sys_dir_name);
3132 return NULL;
3133 }
3134
3135 while ((dirent = g_dir_read_name (directory)) != NULL)
3136 entry_count++;
3137
3138 entry_count += 2; /* For ".",".." */
3139
3140 sent->entries = g_new (CompletionDirEntry, entry_count);
3141 sent->entry_count = entry_count;
3142
3143 g_dir_rewind (directory);
3144
3145 for (i = 0; i < entry_count; i += 1)
3146 {
3147 GError *error = NULL;
3148
3149 if (i == 0)
3150 dirent = ".";
3151 else if (i == 1)
3152 dirent = "..";
3153 else
3154 {
3155 dirent = g_dir_read_name (directory);
3156 if (!dirent) /* Directory changed */
3157 break;
3158 }
3159
3160 sent->entries[n_entries].entry_name = g_filename_to_utf8 (dirent, -1, NULL, NULL, &error);
3161 if (sent->entries[n_entries].entry_name == NULL
3162 || !g_utf8_validate (sent->entries[n_entries].entry_name, -1, NULL))
3163 {
3164 gchar *escaped_str = g_strescape (dirent, NULL);
3165 g_message (_("The filename \"%s\" couldn't be converted to UTF-8 "
3166 "(try setting the environment variable G_BROKEN_FILENAMES): %s"),
3167 escaped_str,
3168 error->message ? error->message : _("Invalid Utf-8"));
3169 g_free (escaped_str);
3170 g_clear_error (&error);
3171 continue;
3172 }
3173 g_clear_error (&error);
3174
3175 sent->entries[n_entries].sort_key = g_utf8_collate_key (sent->entries[n_entries].entry_name, -1);
3176
3177 g_string_assign (path, sys_dir_name);
3178 if (path->str[path->len-1] != G_DIR_SEPARATOR)
3179 {
3180 g_string_append_c (path, G_DIR_SEPARATOR);
3181 }
3182 g_string_append (path, dirent);
3183
3184 if (stat_subdirs)
3185 {
3186 /* Here we know path->str is a "system charset" string */
3187 if (stat (path->str, &ent_sbuf) >= 0 && S_ISDIR (ent_sbuf.st_mode))
3188 sent->entries[n_entries].is_dir = TRUE;
3189 else
3190 /* stat may fail, and we don't mind, since it could be a
3191 * dangling symlink. */
3192 sent->entries[n_entries].is_dir = FALSE;
3193 }
3194 else
3195 sent->entries[n_entries].is_dir = 1;
3196
3197 n_entries++;
3198 }
3199 sent->entry_count = n_entries;
3200
3201 g_free (sys_dir_name);
3202 g_string_free (path, TRUE);
3203 qsort (sent->entries, sent->entry_count, sizeof (CompletionDirEntry), compare_cmpl_dir);
3204
3205 g_dir_close (directory);
3206
3207 return sent;
3208 }
3209
3210 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3211
3212 static gboolean
3213 check_dir (gchar *dir_name,
3214 struct stat *result,
3215 gboolean *stat_subdirs)
3216 {
3217 /* A list of directories that we know only contain other directories.
3218 * Trying to stat every file in these directories would be very
3219 * expensive.
3220 */
3221
3222 static struct {
3223 gchar *name;
3224 gboolean present;
3225 struct stat statbuf;
3226 } no_stat_dirs[] = {
3227 { "/afs", FALSE, { 0 } },
3228 { "/net", FALSE, { 0 } }
3229 };
3230
3231 static const gint n_no_stat_dirs = G_N_ELEMENTS (no_stat_dirs);
3232 static gboolean initialized = FALSE;
3233 gchar *sys_dir_name;
3234 gint i;
3235
3236 if (!initialized)
3237 {
3238 initialized = TRUE;
3239 for (i = 0; i < n_no_stat_dirs; i++)
3240 {
3241 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
3242 no_stat_dirs[i].present = TRUE;
3243 }
3244 }
3245
3246 sys_dir_name = g_filename_from_utf8 (dir_name, -1, NULL, NULL, NULL);
3247 if (!sys_dir_name)
3248 {
3249 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3250 return FALSE;
3251 }
3252
3253 if (stat (sys_dir_name, result) < 0)
3254 {
3255 g_free (sys_dir_name);
3256 cmpl_errno = errno;
3257 return FALSE;
3258 }
3259 g_free (sys_dir_name);
3260
3261 *stat_subdirs = TRUE;
3262 for (i = 0; i < n_no_stat_dirs; i++)
3263 {
3264 if (no_stat_dirs[i].present &&
3265 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
3266 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
3267 {
3268 *stat_subdirs = FALSE;
3269 break;
3270 }
3271 }
3272
3273 return TRUE;
3274 }
3275
3276 #endif
3277
3278 /* open a directory by absolute pathname */
3279 static CompletionDir*
3280 open_dir (gchar *dir_name,
3281 CompletionState *cmpl_state)
3282 {
3283 struct stat sbuf;
3284 gboolean stat_subdirs;
3285 CompletionDirSent *sent;
3286 GList* cdsl;
3287
3288 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
3289 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
3290 return NULL;
3291
3292 cdsl = cmpl_state->directory_sent_storage;
3293
3294 while (cdsl)
3295 {
3296 sent = cdsl->data;
3297
3298 if (sent->inode == sbuf.st_ino &&
3299 sent->mtime == sbuf.st_mtime &&
3300 sent->device == sbuf.st_dev)
3301 return attach_dir (sent, dir_name, cmpl_state);
3302
3303 cdsl = cdsl->next;
3304 }
3305 #else
3306 stat_subdirs = TRUE;
3307 #endif
3308
3309 sent = open_new_dir (dir_name, &sbuf, stat_subdirs);
3310
3311 if (sent)
3312 {
3313 cmpl_state->directory_sent_storage =
3314 g_list_prepend (cmpl_state->directory_sent_storage, sent);
3315
3316 return attach_dir (sent, dir_name, cmpl_state);
3317 }
3318
3319 return NULL;
3320 }
3321
3322 static CompletionDir*
3323 attach_dir (CompletionDirSent *sent,
3324 gchar *dir_name,
3325 CompletionState *cmpl_state)
3326 {
3327 CompletionDir* new_dir;
3328
3329 new_dir = g_new (CompletionDir, 1);
3330
3331 cmpl_state->directory_storage =
3332 g_list_prepend (cmpl_state->directory_storage, new_dir);
3333
3334 new_dir->sent = sent;
3335 new_dir->fullname = g_strdup (dir_name);
3336 new_dir->fullname_len = strlen (dir_name);
3337 new_dir->cmpl_text = NULL;
3338
3339 return new_dir;
3340 }
3341
3342 static gint
3343 correct_dir_fullname (CompletionDir* cmpl_dir)
3344 {
3345 gint length = strlen (cmpl_dir->fullname);
3346 gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3347 gchar *sys_filename;
3348 struct stat sbuf;
3349
3350 /* Does it end with /. (\.) ? */
3351 if (length >= 2 &&
3352 strcmp (cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
3353 {
3354 /* Is it just the root directory (on a drive) ? */
3355 if (cmpl_dir->fullname + length - 2 == first_slash)
3356 {
3357 cmpl_dir->fullname[length - 1] = 0;
3358 cmpl_dir->fullname_len = length - 1;
3359 return TRUE;
3360 }
3361 else
3362 {
3363 cmpl_dir->fullname[length - 2] = 0;
3364 }
3365 }
3366
3367 /* Ends with /./ (\.\)? */
3368 else if (length >= 3 &&
3369 strcmp (cmpl_dir->fullname + length - 3,
3370 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
3371 cmpl_dir->fullname[length - 2] = 0;
3372
3373 /* Ends with /.. (\..) ? */
3374 else if (length >= 3 &&
3375 strcmp (cmpl_dir->fullname + length - 3,
3376 G_DIR_SEPARATOR_S "..") == 0)
3377 {
3378 /* Is it just /.. (X:\..)? */
3379 if (cmpl_dir->fullname + length - 3 == first_slash)
3380 {
3381 cmpl_dir->fullname[length - 2] = 0;
3382 cmpl_dir->fullname_len = length - 2;
3383 return TRUE;
3384 }
3385
3386 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3387 if (!sys_filename)
3388 {
3389 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3390 return FALSE;
3391 }
3392
3393 if (stat (sys_filename, &sbuf) < 0)
3394 {
3395 g_free (sys_filename);
3396 cmpl_errno = errno;
3397 return FALSE;
3398 }
3399 g_free (sys_filename);
3400
3401 cmpl_dir->fullname[length - 3] = 0;
3402
3403 if (!correct_parent (cmpl_dir, &sbuf))
3404 return FALSE;
3405 }
3406
3407 /* Ends with /../ (\..\)? */
3408 else if (length >= 4 &&
3409 strcmp (cmpl_dir->fullname + length - 4,
3410 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
3411 {
3412 /* Is it just /../ (X:\..\)? */
3413 if (cmpl_dir->fullname + length - 4 == first_slash)
3414 {
3415 cmpl_dir->fullname[length - 3] = 0;
3416 cmpl_dir->fullname_len = length - 3;
3417 return TRUE;
3418 }
3419
3420 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3421 if (!sys_filename)
3422 {
3423 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3424 return FALSE;
3425 }
3426
3427 if (stat (sys_filename, &sbuf) < 0)
3428 {
3429 g_free (sys_filename);
3430 cmpl_errno = errno;
3431 return FALSE;
3432 }
3433 g_free (sys_filename);
3434
3435 cmpl_dir->fullname[length - 4] = 0;
3436
3437 if (!correct_parent (cmpl_dir, &sbuf))
3438 return FALSE;
3439 }
3440
3441 cmpl_dir->fullname_len = strlen (cmpl_dir->fullname);
3442
3443 return TRUE;
3444 }
3445
3446 static gint
3447 correct_parent (CompletionDir *cmpl_dir,
3448 struct stat *sbuf)
3449 {
3450 struct stat parbuf;
3451 gchar *last_slash;
3452 gchar *first_slash;
3453 gchar *new_name;
3454 gchar *sys_filename;
3455 gchar c = 0;
3456
3457 last_slash = strrchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3458 g_assert (last_slash);
3459 first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
3460
3461 /* Clever (?) way to check for top-level directory that works also on
3462 * Win32, where there is a drive letter and colon prefixed...
3463 */
3464 if (last_slash != first_slash)
3465 {
3466 last_slash[0] = 0;
3467 }
3468 else
3469 {
3470 c = last_slash[1];
3471 last_slash[1] = 0;
3472 }
3473
3474 sys_filename = g_filename_from_utf8 (cmpl_dir->fullname, -1, NULL, NULL, NULL);
3475 if (!sys_filename)
3476 {
3477 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3478 if (!c)
3479 last_slash[0] = G_DIR_SEPARATOR;
3480 return FALSE;
3481 }
3482
3483 if (stat (sys_filename, &parbuf) < 0)
3484 {
3485 g_free (sys_filename);
3486 cmpl_errno = errno;
3487 if (!c)
3488 last_slash[0] = G_DIR_SEPARATOR;
3489 return FALSE;
3490 }
3491 g_free (sys_filename);
3492
3493 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
3494 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
3495 /* it wasn't a link */
3496 return TRUE;
3497
3498 if (c)
3499 last_slash[1] = c;
3500 else
3501 last_slash[0] = G_DIR_SEPARATOR;
3502
3503 /* it was a link, have to figure it out the hard way */
3504
3505 new_name = find_parent_dir_fullname (cmpl_dir->fullname);
3506
3507 if (!new_name)
3508 return FALSE;
3509
3510 g_free (cmpl_dir->fullname);
3511
3512 cmpl_dir->fullname = new_name;
3513 #endif
3514
3515 return TRUE;
3516 }
3517
3518 #ifndef G_OS_WIN32
3519
3520 static gchar*
3521 find_parent_dir_fullname (gchar* dirname)
3522 {
3523 gchar *sys_orig_dir;
3524 gchar *result;
3525 gchar *sys_cwd;
3526 gchar *sys_dirname;
3527
3528 sys_orig_dir = g_get_current_dir ();
3529 sys_dirname = g_filename_from_utf8 (dirname, -1, NULL, NULL, NULL);
3530 if (!sys_dirname)
3531 {
3532 g_free (sys_orig_dir);
3533 cmpl_errno = CMPL_ERRNO_DID_NOT_CONVERT;
3534 return NULL;
3535 }
3536
3537 if (chdir (sys_dirname) != 0 || chdir ("..") != 0)
3538 {
3539 cmpl_errno = errno;
3540 chdir (sys_orig_dir);
3541 g_free (sys_dirname);
3542 g_free (sys_orig_dir);
3543 return NULL;
3544 }
3545 g_free (sys_dirname);
3546
3547 sys_cwd = g_get_current_dir ();
3548 result = g_filename_to_utf8 (sys_cwd, -1, NULL, NULL, NULL);
3549 g_free (sys_cwd);
3550
3551 if (chdir (sys_orig_dir) != 0)
3552 {
3553 cmpl_errno = errno;
3554 g_free (sys_orig_dir);
3555 return NULL;
3556 }
3557
3558 g_free (sys_orig_dir);
3559 return result;
3560 }
3561
3562 #endif
3563
3564 /**********************************************************************/
3565 /* Completion Operations */
3566 /**********************************************************************/
3567
3568 #ifdef HAVE_PWD_H
3569
3570 static PossibleCompletion*
3571 attempt_homedir_completion (gchar *text_to_complete,
3572 CompletionState *cmpl_state)
3573 {
3574 gint index, length;
3575
3576 if (!cmpl_state->user_dir_name_buffer &&
3577 !get_pwdb (cmpl_state))
3578 return NULL;
3579 length = strlen (text_to_complete) - 1;
3580
3581 cmpl_state->user_completion_index += 1;
3582
3583 while (cmpl_state->user_completion_index < cmpl_state->user_directories_len)
3584 {
3585 index = first_diff_index (text_to_complete + 1,
3586 cmpl_state->user_directories
3587 [cmpl_state->user_completion_index].login);
3588
3589 switch (index)
3590 {
3591 case PATTERN_MATCH:
3592 break;
3593 default:
3594 if (cmpl_state->last_valid_char < (index + 1))
3595 cmpl_state->last_valid_char = index + 1;
3596 cmpl_state->user_completion_index += 1;
3597 continue;
3598 }
3599
3600 cmpl_state->the_completion.is_a_completion = 1;
3601 cmpl_state->the_completion.is_directory = TRUE;
3602
3603 append_completion_text ("~", cmpl_state);
3604
3605 append_completion_text (cmpl_state->
3606 user_directories[cmpl_state->user_completion_index].login,
3607 cmpl_state);
3608
3609 return append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3610 }
3611
3612 if (text_to_complete[1]
3613 || cmpl_state->user_completion_index > cmpl_state->user_directories_len)
3614 {
3615 cmpl_state->user_completion_index = -1;
3616 return NULL;
3617 }
3618 else
3619 {
3620 cmpl_state->user_completion_index += 1;
3621 cmpl_state->the_completion.is_a_completion = 1;
3622 cmpl_state->the_completion.is_directory = TRUE;
3623
3624 return append_completion_text ("~" G_DIR_SEPARATOR_S, cmpl_state);
3625 }
3626 }
3627
3628 #endif
3629
3630 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
3631 #define FOLD(c) (tolower(c))
3632 #else
3633 #define FOLD(c) (c)
3634 #endif
3635
3636 /* returns the index (>= 0) of the first differing character,
3637 * PATTERN_MATCH if the completion matches */
3638 static gint
3639 first_diff_index (gchar *pat,
3640 gchar *text)
3641 {
3642 gint diff = 0;
3643
3644 while (*pat && *text && FOLD (*text) == FOLD (*pat))
3645 {
3646 pat += 1;
3647 text += 1;
3648 diff += 1;
3649 }
3650
3651 if (*pat)
3652 return diff;
3653
3654 return PATTERN_MATCH;
3655 }
3656
3657 static PossibleCompletion*
3658 append_completion_text (gchar *text,
3659 CompletionState *cmpl_state)
3660 {
3661 gint len, i = 1;
3662
3663 if (!cmpl_state->the_completion.text)
3664 return NULL;
3665
3666 len = strlen (text) + strlen (cmpl_state->the_completion.text) + 1;
3667
3668 if (cmpl_state->the_completion.text_alloc > len)
3669 {
3670 strcat (cmpl_state->the_completion.text, text);
3671 return &cmpl_state->the_completion;
3672 }
3673
3674 while (i < len)
3675 i <<= 1;
3676
3677 cmpl_state->the_completion.text_alloc = i;
3678
3679 cmpl_state->the_completion.text = (gchar*) g_realloc (cmpl_state->the_completion.text, i);
3680
3681 if (!cmpl_state->the_completion.text)
3682 return NULL;
3683 else
3684 {
3685 strcat (cmpl_state->the_completion.text, text);
3686 return &cmpl_state->the_completion;
3687 }
3688 }
3689
3690 static CompletionDir*
3691 find_completion_dir (gchar *text_to_complete,
3692 gchar **remaining_text,
3693 CompletionState *cmpl_state)
3694 {
3695 gchar* first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
3696 CompletionDir* dir = cmpl_state->reference_dir;
3697 CompletionDir* next;
3698 *remaining_text = text_to_complete;
3699
3700 while (first_slash)
3701 {
3702 gint len = first_slash - *remaining_text;
3703 gint found = 0;
3704 gchar *found_name = NULL; /* Quiet gcc */
3705 gint i;
3706 gchar* pat_buf = g_new (gchar, len + 1);
3707
3708 strncpy (pat_buf, *remaining_text, len);
3709 pat_buf[len] = 0;
3710
3711 for (i = 0; i < dir->sent->entry_count; i += 1)
3712 {
3713 if (dir->sent->entries[i].is_dir &&
3714 _gtk_fnmatch (pat_buf, dir->sent->entries[i].entry_name))
3715 {
3716 if (found)
3717 {
3718 g_free (pat_buf);
3719 return dir;
3720 }
3721 else
3722 {
3723 found = 1;
3724 found_name = dir->sent->entries[i].entry_name;
3725 }
3726 }
3727 }
3728
3729 if (!found)
3730 {
3731 /* Perhaps we are trying to open an automount directory */
3732 found_name = pat_buf;
3733 }
3734
3735 next = open_relative_dir (found_name, dir, cmpl_state);
3736
3737 if (!next)
3738 {
3739 g_free (pat_buf);
3740 return NULL;
3741 }
3742
3743 next->cmpl_parent = dir;
3744
3745 dir = next;
3746
3747 if (!correct_dir_fullname (dir))
3748 {
3749 g_free (pat_buf);
3750 return NULL;
3751 }
3752
3753 *remaining_text = first_slash + 1;
3754 first_slash = strchr (*remaining_text, G_DIR_SEPARATOR);
3755
3756 g_free (pat_buf);
3757 }
3758
3759 return dir;
3760 }
3761
3762 static void
3763 update_cmpl (PossibleCompletion *poss,
3764 CompletionState *cmpl_state)
3765 {
3766 gint cmpl_len;
3767
3768 if (!poss || !cmpl_is_a_completion (poss))
3769 return;
3770
3771 cmpl_len = strlen (cmpl_this_completion (poss));
3772
3773 if (cmpl_state->updated_text_alloc < cmpl_len + 1)
3774 {
3775 cmpl_state->updated_text =
3776 (gchar*)g_realloc (cmpl_state->updated_text,
3777 cmpl_state->updated_text_alloc);
3778 cmpl_state->updated_text_alloc = 2*cmpl_len;
3779 }
3780
3781 if (cmpl_state->updated_text_len < 0)
3782 {
3783 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3784 cmpl_state->updated_text_len = cmpl_len;
3785 cmpl_state->re_complete = cmpl_is_directory (poss);
3786 }
3787 else if (cmpl_state->updated_text_len == 0)
3788 {
3789 cmpl_state->re_complete = FALSE;
3790 }
3791 else
3792 {
3793 gint first_diff =
3794 first_diff_index (cmpl_state->updated_text,
3795 cmpl_this_completion (poss));
3796
3797 cmpl_state->re_complete = FALSE;
3798
3799 if (first_diff == PATTERN_MATCH)
3800 return;
3801
3802 if (first_diff > cmpl_state->updated_text_len)
3803 strcpy (cmpl_state->updated_text, cmpl_this_completion (poss));
3804
3805 cmpl_state->updated_text_len = first_diff;
3806 cmpl_state->updated_text[first_diff] = 0;
3807 }
3808 }
3809
3810 static PossibleCompletion*
3811 attempt_dir_completion (CompletionState *cmpl_state)
3812 {
3813 gchar *pat_buf, *first_slash;
3814 CompletionDir *dir = cmpl_state->active_completion_dir;
3815
3816 dir->cmpl_index += 1;
3817
3818 if (dir->cmpl_index == dir->sent->entry_count)
3819 {
3820 if (dir->cmpl_parent == NULL)
3821 {
3822 cmpl_state->active_completion_dir = NULL;
3823
3824 return NULL;
3825 }
3826 else
3827 {
3828 cmpl_state->active_completion_dir = dir->cmpl_parent;
3829
3830 return attempt_dir_completion (cmpl_state);
3831 }
3832 }
3833
3834 g_assert (dir->cmpl_text);
3835
3836 first_slash = strchr (dir->cmpl_text, G_DIR_SEPARATOR);
3837
3838 if (first_slash)
3839 {
3840 gint len = first_slash - dir->cmpl_text;
3841
3842 pat_buf = g_new (gchar, len + 1);
3843 strncpy (pat_buf, dir->cmpl_text, len);
3844 pat_buf[len] = 0;
3845 }
3846 else
3847 {
3848 gint len = strlen (dir->cmpl_text);
3849
3850 pat_buf = g_new (gchar, len + 2);
3851 strcpy (pat_buf, dir->cmpl_text);
3852 /* Don't append a * if the user entered one herself.
3853 * This way one can complete *.h and don't get matches
3854 * on any .help files, for instance.
3855 */
3856 if (strchr (pat_buf, '*') == NULL)
3857 strcpy (pat_buf + len, "*");
3858 }
3859
3860 if (first_slash)
3861 {
3862 if (dir->sent->entries[dir->cmpl_index].is_dir)
3863 {
3864 if (_gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name))
3865 {
3866 CompletionDir* new_dir;
3867
3868 new_dir = open_relative_dir (dir->sent->entries[dir->cmpl_index].entry_name,
3869 dir, cmpl_state);
3870
3871 if (!new_dir)
3872 {
3873 g_free (pat_buf);
3874 return NULL;
3875 }
3876
3877 new_dir->cmpl_parent = dir;
3878
3879 new_dir->cmpl_index = -1;
3880 new_dir->cmpl_text = g_strdup (first_slash + 1);
3881
3882 cmpl_state->active_completion_dir = new_dir;
3883
3884 g_free (pat_buf);
3885 return attempt_dir_completion (cmpl_state);
3886 }
3887 else
3888 {
3889 g_free (pat_buf);
3890 return attempt_dir_completion (cmpl_state);
3891 }
3892 }
3893 else
3894 {
3895 g_free (pat_buf);
3896 return attempt_dir_completion (cmpl_state);
3897 }
3898 }
3899 else
3900 {
3901 if (dir->cmpl_parent != NULL)
3902 {
3903 append_completion_text (dir->fullname +
3904 strlen (cmpl_state->completion_dir->fullname) + 1,
3905 cmpl_state);
3906 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3907 }
3908
3909 append_completion_text (dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
3910
3911 cmpl_state->the_completion.is_a_completion =
3912 _gtk_fnmatch (pat_buf, dir->sent->entries[dir->cmpl_index].entry_name);
3913
3914 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
3915 if (dir->sent->entries[dir->cmpl_index].is_dir)
3916 append_completion_text (G_DIR_SEPARATOR_S, cmpl_state);
3917
3918 g_free (pat_buf);
3919 return &cmpl_state->the_completion;
3920 }
3921 }
3922
3923 #ifdef HAVE_PWD_H
3924
3925 static gint
3926 get_pwdb (CompletionState* cmpl_state)
3927 {
3928 struct passwd *pwd_ptr;
3929 gchar* buf_ptr;
3930 gchar *utf8;
3931 gint len = 0, i, count = 0;
3932
3933 if (cmpl_state->user_dir_name_buffer)
3934 return TRUE;
3935 setpwent ();
3936
3937 while ((pwd_ptr = getpwent ()) != NULL)
3938 {
3939 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3940 len += strlen (utf8);
3941 g_free (utf8);
3942 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3943 len += strlen (utf8);
3944 g_free (utf8);
3945 len += 2;
3946 count += 1;
3947 }
3948
3949 setpwent ();
3950
3951 cmpl_state->user_dir_name_buffer = g_new (gchar, len);
3952 cmpl_state->user_directories = g_new (CompletionUserDir, count);
3953 cmpl_state->user_directories_len = count;
3954
3955 buf_ptr = cmpl_state->user_dir_name_buffer;
3956
3957 for (i = 0; i < count; i += 1)
3958 {
3959 pwd_ptr = getpwent ();
3960 if (!pwd_ptr)
3961 {
3962 cmpl_errno = errno;
3963 goto error;
3964 }
3965
3966 utf8 = g_filename_to_utf8 (pwd_ptr->pw_name, -1, NULL, NULL, NULL);
3967 strcpy (buf_ptr, utf8);
3968 g_free (utf8);
3969
3970 cmpl_state->user_directories[i].login = buf_ptr;
3971
3972 buf_ptr += strlen (buf_ptr);
3973 buf_ptr += 1;
3974
3975 utf8 = g_filename_to_utf8 (pwd_ptr->pw_dir, -1, NULL, NULL, NULL);
3976 strcpy (buf_ptr, utf8);
3977 g_free (utf8);
3978
3979 cmpl_state->user_directories[i].homedir = buf_ptr;
3980
3981 buf_ptr += strlen (buf_ptr);
3982 buf_ptr += 1;
3983 }
3984
3985 qsort (cmpl_state->user_directories,
3986 cmpl_state->user_directories_len,
3987 sizeof (CompletionUserDir),
3988 compare_user_dir);
3989
3990 endpwent ();
3991
3992 return TRUE;
3993
3994 error:
3995
3996 if (cmpl_state->user_dir_name_buffer)
3997 g_free (cmpl_state->user_dir_name_buffer);
3998 if (cmpl_state->user_directories)
3999 g_free (cmpl_state->user_directories);
4000
4001 cmpl_state->user_dir_name_buffer = NULL;
4002 cmpl_state->user_directories = NULL;
4003
4004 return FALSE;
4005 }
4006
4007 static gint
4008 compare_user_dir (const void *a,
4009 const void *b)
4010 {
4011 return strcmp ((((CompletionUserDir*)a))->login,
4012 (((CompletionUserDir*)b))->login);
4013 }
4014
4015 #endif
4016
4017 static gint
4018 compare_cmpl_dir (const void *a,
4019 const void *b)
4020 {
4021
4022 return strcmp (((CompletionDirEntry*)a)->sort_key,
4023 (((CompletionDirEntry*)b))->sort_key);
4024 }
4025
4026 static gint
4027 cmpl_state_okay (CompletionState* cmpl_state)
4028 {
4029 return cmpl_state && cmpl_state->reference_dir;
4030 }
4031
4032 static const gchar*
4033 cmpl_strerror (gint err)
4034 {
4035 if (err == CMPL_ERRNO_TOO_LONG)
4036 return _("Name too long");
4037 else if (err == CMPL_ERRNO_DID_NOT_CONVERT)
4038 return _("Couldn't convert filename");
4039 else
4040 return g_strerror (err);
4041 }
4042
4043 const gchar * gtk_dir_selection_get_dir (GtkDirSelection *filesel)
4044 {
4045 return gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
4046 }
This page took 0.109277 seconds and 5 git commands to generate.