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