1 /** Downloads managment
12 #ifdef HAVE_SYS_CYGWIN_H
13 #include <sys/cygwin.h>
15 #include <sys/types.h>
17 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
27 #include "bfu/dialog.h"
28 #include "cache/cache.h"
29 #include "config/options.h"
30 #include "dialogs/document.h"
31 #include "dialogs/download.h"
32 #include "dialogs/menu.h"
33 #include "intl/gettext/libintl.h"
34 #include "main/object.h"
35 #include "mime/mime.h"
36 #include "network/connection.h"
37 #include "network/progress.h"
38 #include "network/state.h"
39 #include "osdep/osdep.h"
40 #include "protocol/bittorrent/dialogs.h"
41 #include "protocol/date.h"
42 #include "protocol/protocol.h"
43 #include "protocol/uri.h"
44 #include "session/download.h"
45 #include "session/history.h"
46 #include "session/location.h"
47 #include "session/session.h"
48 #include "session/task.h"
49 #include "terminal/draw.h"
50 #include "terminal/screen.h"
51 #include "terminal/terminal.h"
52 #include "util/conv.h"
53 #include "util/error.h"
54 #include "util/file.h"
55 #include "util/lists.h"
56 #include "util/memlist.h"
57 #include "util/memory.h"
58 #include "util/string.h"
59 #include "util/time.h"
62 /* TODO: tp_*() should be in separate file, I guess? --pasky */
65 INIT_LIST_OF(struct file_download
, downloads
);
68 download_is_progressing(struct download
*download
)
71 && is_in_state(download
->state
, S_TRANS
)
72 && has_progress(download
->progress
);
76 are_there_downloads(void)
78 struct file_download
*file_download
;
80 foreach (file_download
, downloads
)
81 if (!file_download
->external_handler
)
88 static void download_data(struct download
*download
, struct file_download
*file_download
);
90 struct file_download
*
91 init_file_download(struct uri
*uri
, struct session
*ses
, unsigned char *file
, int fd
)
93 struct file_download
*file_download
;
95 file_download
= mem_calloc(1, sizeof(*file_download
));
96 if (!file_download
) return NULL
;
98 /* Actually we could allow fragments in the URI and just change all the
99 * places that compares and shows the URI, but for now it is much
100 * easier this way. */
101 file_download
->uri
= get_composed_uri(uri
, URI_BASE
);
102 if (!file_download
->uri
) {
103 mem_free(file_download
);
107 init_download_display(file_download
);
109 file_download
->file
= file
;
110 file_download
->handle
= fd
;
112 file_download
->download
.callback
= (download_callback_T
*) download_data
;
113 file_download
->download
.data
= file_download
;
114 file_download
->ses
= ses
;
115 /* The tab may be closed, but we will still want to ie. open the
116 * handler on that terminal. */
117 file_download
->term
= ses
->tab
->term
;
119 object_nolock(file_download
, "file_download"); /* Debugging purpose. */
120 add_to_list(downloads
, file_download
);
122 return file_download
;
127 abort_download(struct file_download
*file_download
)
130 /* When hacking to cleanup the download code, remove lots of duplicated
131 * code and implement stuff from bug 435 we should reintroduce this
132 * assertion. Currently it will trigger often and shows that the
133 * download dialog code potentially could access free()d memory. */
134 assert(!is_object_used(file_download
));
137 done_download_display(file_download
);
139 if (file_download
->ses
)
140 check_questions_queue(file_download
->ses
);
142 if (file_download
->dlg_data
)
143 cancel_dialog(file_download
->dlg_data
, NULL
);
144 cancel_download(&file_download
->download
, file_download
->stop
);
145 if (file_download
->uri
) done_uri(file_download
->uri
);
147 if (file_download
->handle
!= -1) {
148 prealloc_truncate(file_download
->handle
, file_download
->seek
);
149 close(file_download
->handle
);
152 mem_free_if(file_download
->external_handler
);
153 if (file_download
->file
) {
154 if (file_download
->delete) unlink(file_download
->file
);
155 mem_free(file_download
->file
);
157 del_from_list(file_download
);
158 mem_free(file_download
);
163 kill_downloads_to_file(unsigned char *file
)
165 struct file_download
*file_download
;
167 foreach (file_download
, downloads
) {
168 if (strcmp(file_download
->file
, file
))
171 file_download
= file_download
->prev
;
172 abort_download(file_download
->next
);
178 abort_all_downloads(void)
180 while (!list_empty(downloads
))
181 abort_download(downloads
.next
);
186 destroy_downloads(struct session
*ses
)
188 struct file_download
*file_download
, *next
;
191 /* We are supposed to blat all downloads to external handlers belonging
192 * to @ses, but we will refuse to do so if there is another session
193 * bound to this terminal. That looks like the reasonable thing to do,
194 * fulfilling the principle of least astonishment. */
195 foreach (s
, sessions
) {
196 if (s
== ses
|| s
->tab
->term
!= ses
->tab
->term
)
199 foreach (file_download
, downloads
) {
200 if (file_download
->ses
!= ses
)
203 file_download
->ses
= s
;
209 foreachsafe (file_download
, next
, downloads
) {
210 if (file_download
->ses
!= ses
)
213 if (!file_download
->external_handler
) {
214 file_download
->ses
= NULL
;
218 abort_download(file_download
);
223 detach_downloads_from_terminal(struct terminal
*term
)
225 struct file_download
*file_download
, *next
;
227 assert(term
!= NULL
);
228 if_assert_failed
return;
230 foreachsafe (file_download
, next
, downloads
) {
231 if (file_download
->term
!= term
)
234 if (!file_download
->external_handler
) {
235 file_download
->term
= NULL
;
236 if (file_download
->ses
237 && file_download
->ses
->tab
->term
== term
)
238 file_download
->ses
= NULL
;
242 abort_download(file_download
);
247 download_error_dialog(struct file_download
*file_download
, int saved_errno
)
249 unsigned char *emsg
= (unsigned char *) strerror(saved_errno
);
250 struct session
*ses
= file_download
->ses
;
251 struct terminal
*term
= file_download
->term
;
255 info_box(term
, MSGBOX_FREE_TEXT
,
256 N_("Download error"), ALIGN_CENTER
,
257 msg_text(term
, N_("Could not create file '%s':\n%s"),
258 file_download
->file
, emsg
));
262 write_cache_entry_to_file(struct cache_entry
*cached
, struct file_download
*file_download
)
264 struct fragment
*frag
;
266 if (file_download
->download
.progress
&& file_download
->download
.progress
->seek
) {
267 file_download
->seek
= file_download
->download
.progress
->seek
;
268 file_download
->download
.progress
->seek
= 0;
269 /* This is exclusive with the prealloc, thus we can perform
270 * this in front of that thing safely. */
271 if (lseek(file_download
->handle
, file_download
->seek
, SEEK_SET
) < 0) {
272 download_error_dialog(file_download
, errno
);
277 foreach (frag
, cached
->frag
) {
278 off_t remain
= file_download
->seek
- frag
->offset
;
279 int *h
= &file_download
->handle
;
282 if (remain
< 0 || frag
->length
<= remain
)
285 #ifdef USE_OPEN_PREALLOC
286 if (!file_download
->seek
287 && (!file_download
->download
.progress
288 || file_download
->download
.progress
->size
> 0)) {
290 *h
= open_prealloc(file_download
->file
,
291 O_CREAT
|O_WRONLY
|O_TRUNC
,
293 file_download
->download
.progress
294 ? file_download
->download
.progress
->size
297 download_error_dialog(file_download
, errno
);
304 w
= safe_write(*h
, frag
->data
+ remain
, frag
->length
- remain
);
306 download_error_dialog(file_download
, errno
);
310 file_download
->seek
+= w
;
317 abort_download_and_beep(struct file_download
*file_download
, struct terminal
*term
)
319 if (term
&& get_opt_int("document.download.notify_bell")
320 + file_download
->notify
>= 2) {
324 abort_download(file_download
);
328 download_data_store(struct download
*download
, struct file_download
*file_download
)
330 struct terminal
*term
= file_download
->term
;
332 assert_terminal_ptr_not_dangling(term
);
333 if_assert_failed term
= file_download
->term
= NULL
;
336 /* No term here, so no beep. --Zas */
337 abort_download(file_download
);
341 if (is_in_progress_state(download
->state
)) {
342 if (file_download
->dlg_data
)
343 redraw_dialog(file_download
->dlg_data
, 1);
347 if (!is_in_state(download
->state
, S_OK
)) {
348 unsigned char *url
= get_uri_string(file_download
->uri
, URI_PUBLIC
);
349 struct connection_state state
= download
->state
;
351 abort_download_and_beep(file_download
, term
);
355 info_box(term
, MSGBOX_FREE_TEXT
,
356 N_("Download error"), ALIGN_CENTER
,
357 msg_text(term
, N_("Error downloading %s:\n\n%s"),
358 url
, get_state_message(state
, term
)));
363 if (file_download
->external_handler
) {
364 prealloc_truncate(file_download
->handle
, file_download
->seek
);
365 close(file_download
->handle
);
366 file_download
->handle
= -1;
367 exec_on_terminal(term
, file_download
->external_handler
,
369 file_download
->block
? TERM_EXEC_FG
: TERM_EXEC_BG
);
370 file_download
->delete = 0;
371 abort_download_and_beep(file_download
, term
);
375 if (file_download
->notify
) {
376 unsigned char *url
= get_uri_string(file_download
->uri
, URI_PUBLIC
);
378 /* This is apparently a little racy. Deleting the box item will
379 * update the download browser _after_ the notification dialog
380 * has been drawn whereby it will be hidden. This should make
381 * the download browser update before launcing any
383 done_download_display(file_download
);
386 info_box(term
, MSGBOX_FREE_TEXT
,
387 N_("Download"), ALIGN_CENTER
,
388 msg_text(term
, N_("Download complete:\n%s"), url
));
393 if (file_download
->remotetime
394 && get_opt_bool("document.download.set_original_time")) {
397 foo
.actime
= foo
.modtime
= file_download
->remotetime
;
398 utime(file_download
->file
, &foo
);
401 abort_download_and_beep(file_download
, term
);
405 download_data(struct download
*download
, struct file_download
*file_download
)
407 struct cache_entry
*cached
= download
->cached
;
409 if (!cached
|| is_in_queued_state(download
->state
)) {
410 download_data_store(download
, file_download
);
414 if (cached
->last_modified
)
415 file_download
->remotetime
= parse_date(&cached
->last_modified
, NULL
, 0, 1);
417 if (cached
->redirect
&& file_download
->redirect_cnt
++ < MAX_REDIRECTS
) {
418 cancel_download(&file_download
->download
, 0);
420 assertm(compare_uri(cached
->uri
, file_download
->uri
, 0),
421 "Redirecting using bad base URI");
423 done_uri(file_download
->uri
);
425 file_download
->uri
= get_uri_reference(cached
->redirect
);
426 file_download
->download
.state
= connection_state(S_WAIT_REDIR
);
428 if (file_download
->dlg_data
)
429 redraw_dialog(file_download
->dlg_data
, 1);
431 load_uri(file_download
->uri
, cached
->uri
, &file_download
->download
,
432 PRI_DOWNLOAD
, CACHE_MODE_NORMAL
,
433 download
->progress
? download
->progress
->start
: 0);
438 if (!write_cache_entry_to_file(cached
, file_download
)) {
439 detach_connection(download
, file_download
->seek
);
440 abort_download(file_download
);
444 detach_connection(download
, file_download
->seek
);
445 download_data_store(download
, file_download
);
449 /* XXX: We assume that resume is everytime zero in lun's callbacks. */
451 struct terminal
*term
;
452 unsigned char *ofile
, *file
;
454 void (*callback
)(struct terminal
*, unsigned char *, void *, int);
459 COMMON_DOWNLOAD_DO
= 0,
464 int magic
; /* Must be first --witekfl */
466 unsigned char *real_file
;
470 int magic
; /* must be first --witekfl */
471 struct type_query
*type_query
;
472 unsigned char *real_file
;
477 unsigned char **real_file
;
480 void (*callback
)(struct terminal
*, int, void *, int);
485 lun_alternate(void *lun_hop_
)
487 struct lun_hop
*lun_hop
= lun_hop_
;
489 lun_hop
->callback(lun_hop
->term
, lun_hop
->file
, lun_hop
->data
, 0);
490 mem_free_if(lun_hop
->ofile
);
495 lun_cancel(void *lun_hop_
)
497 struct lun_hop
*lun_hop
= lun_hop_
;
499 lun_hop
->callback(lun_hop
->term
, NULL
, lun_hop
->data
, 0);
500 mem_free_if(lun_hop
->ofile
);
501 mem_free_if(lun_hop
->file
);
506 lun_overwrite(void *lun_hop_
)
508 struct lun_hop
*lun_hop
= lun_hop_
;
510 lun_hop
->callback(lun_hop
->term
, lun_hop
->ofile
, lun_hop
->data
, 0);
511 mem_free_if(lun_hop
->file
);
515 static void common_download_do(struct terminal
*term
, int fd
, void *data
, int resume
);
518 lun_resume(void *lun_hop_
)
520 struct lun_hop
*lun_hop
= lun_hop_
;
521 struct cdf_hop
*cdf_hop
= lun_hop
->data
;
523 int magic
= *(int *)cdf_hop
->data
;
525 if (magic
== CONTINUE_DOWNLOAD_DO
) {
526 struct cmdw_hop
*cmdw_hop
= mem_calloc(1, sizeof(*cmdw_hop
));
532 struct codw_hop
*codw_hop
= cdf_hop
->data
;
533 struct type_query
*type_query
= codw_hop
->type_query
;
535 cmdw_hop
->magic
= COMMON_DOWNLOAD_DO
;
536 cmdw_hop
->ses
= type_query
->ses
;
537 /* FIXME: Current ses->download_uri is overwritten here --witekfl */
538 cmdw_hop
->ses
->download_uri
= get_uri_reference(type_query
->uri
);
540 if (type_query
->external_handler
) mem_free_if(codw_hop
->file
);
541 tp_cancel(type_query
);
544 cdf_hop
->real_file
= &cmdw_hop
->real_file
;
545 cdf_hop
->data
= cmdw_hop
;
546 cdf_hop
->callback
= common_download_do
;
549 lun_hop
->callback(lun_hop
->term
, lun_hop
->ofile
, lun_hop
->data
, 1);
550 mem_free_if(lun_hop
->file
);
556 lookup_unique_name(struct terminal
*term
, unsigned char *ofile
, int resume
,
557 void (*callback
)(struct terminal
*, unsigned char *, void *, int),
560 /* [gettext_accelerator_context(.lookup_unique_name)] */
561 struct lun_hop
*lun_hop
;
565 ofile
= expand_tilde(ofile
);
567 /* Minor code duplication to prevent useless call to get_opt_int()
568 * if possible. --Zas */
570 callback(term
, ofile
, data
, resume
);
574 /* !overwrite means always silently overwrite, which may be admitelly
575 * indeed a little confusing ;-) */
576 overwrite
= get_opt_int("document.download.overwrite");
578 /* Nothing special to do... */
579 callback(term
, ofile
, data
, resume
);
583 /* Check if file is a directory, and use a default name if it's the
585 if (file_is_dir(ofile
)) {
586 info_box(term
, MSGBOX_FREE_TEXT
,
587 N_("Download error"), ALIGN_CENTER
,
588 msg_text(term
, N_("'%s' is a directory."),
591 callback(term
, NULL
, data
, 0);
595 /* Check if the file already exists (file != ofile). */
596 file
= get_unique_name(ofile
);
598 if (!file
|| overwrite
== 1 || file
== ofile
) {
599 /* Still nothing special to do... */
600 if (file
!= ofile
) mem_free(ofile
);
601 callback(term
, file
, data
, 0);
605 /* overwrite == 2 (ask) and file != ofile (=> original file already
608 lun_hop
= mem_calloc(1, sizeof(*lun_hop
));
610 if (file
!= ofile
) mem_free(file
);
612 callback(term
, NULL
, data
, 0);
615 lun_hop
->term
= term
;
616 lun_hop
->ofile
= ofile
;
617 lun_hop
->file
= (file
!= ofile
) ? file
: stracpy(ofile
);
618 lun_hop
->callback
= callback
;
619 lun_hop
->data
= data
;
621 msg_box(term
, NULL
, MSGBOX_FREE_TEXT
,
622 N_("File exists"), ALIGN_CENTER
,
623 msg_text(term
, N_("This file already exists:\n"
625 "The alternative filename is:\n"
627 empty_string_or_(lun_hop
->ofile
),
628 empty_string_or_(file
)),
630 MSG_BOX_BUTTON(N_("Sa~ve under the alternative name"), lun_alternate
, B_ENTER
),
631 MSG_BOX_BUTTON(N_("~Overwrite the original file"), lun_overwrite
, 0),
632 MSG_BOX_BUTTON(N_("~Resume download of the original file"), lun_resume
, 0),
633 MSG_BOX_BUTTON(N_("~Cancel"), lun_cancel
, B_ESC
));
639 create_download_file_do(struct terminal
*term
, unsigned char *file
, void *data
,
642 struct cdf_hop
*cdf_hop
= data
;
646 #ifdef NO_FILE_SECURITY
649 int sf
= cdf_hop
->safe
;
652 if (!file
) goto finish
;
657 /* Create parent directories if needed. */
660 /* O_APPEND means repositioning at the end of file before each write(),
661 * thus ignoring seek()s and that can hide mysterious bugs. IMHO.
663 h
= open(file
, O_CREAT
| O_WRONLY
| (resume
? 0 : O_TRUNC
)
664 | (sf
&& !resume
? O_EXCL
: 0),
666 saved_errno
= errno
; /* Saved in case of ... --Zas */
674 info_box(term
, MSGBOX_FREE_TEXT
,
675 N_("Download error"), ALIGN_CENTER
,
676 msg_text(term
, N_("Could not create file '%s':\n%s"),
677 file
, strerror(saved_errno
)));
685 if (!cdf_hop
->safe
) {
686 unsigned char *download_dir
= get_opt_str("document.download.directory");
689 safe_strncpy(download_dir
, file
, MAX_STR_LEN
);
691 /* Find the used directory so it's available in history */
692 for (i
= strlen(download_dir
); i
>= 0; i
--)
693 if (dir_sep(download_dir
[i
]))
695 download_dir
[i
+ 1] = 0;
699 if (cdf_hop
->real_file
)
700 *cdf_hop
->real_file
= file
;
705 cdf_hop
->callback(term
, h
, cdf_hop
->data
, resume
);
710 create_download_file(struct terminal
*term
, unsigned char *fi
,
711 unsigned char **real_file
, int safe
, int resume
,
712 void (*callback
)(struct terminal
*, int, void *, int),
715 struct cdf_hop
*cdf_hop
= mem_calloc(1, sizeof(*cdf_hop
));
719 callback(term
, -1, data
, 0);
723 cdf_hop
->real_file
= real_file
;
724 cdf_hop
->safe
= safe
;
725 cdf_hop
->callback
= callback
;
726 cdf_hop
->data
= data
;
728 /* FIXME: The wd bussiness is probably useless here? --pasky */
732 /* Also the tilde will be expanded here. */
733 lookup_unique_name(term
, fi
, resume
, create_download_file_do
, cdf_hop
);
742 static unsigned char *
743 get_temp_name(struct uri
*uri
)
746 unsigned char *extension
;
748 * We use tempnam() here, which is unsafe (race condition), for now.
749 * This should be changed at some time, but it needs an in-depth work
750 * of whole download code. --Zas */
751 unsigned char *nm
= tempnam(NULL
, ELINKS_TEMPNAME_PREFIX
);
753 if (!nm
) return NULL
;
755 if (!init_string(&name
)) {
760 add_to_string(&name
, nm
);
763 extension
= get_extension_from_uri(uri
);
765 add_shell_safe_to_string(&name
, extension
, strlen(extension
));
773 static unsigned char *
774 subst_file(unsigned char *prog
, unsigned char *file
)
777 /* When there is no %s in the mailcap entry, the handler program reads
778 * data from stdin instead of a file. */
781 if (!init_string(&name
)) return NULL
;
786 for (p
= 0; prog
[p
] && prog
[p
] != '%'; p
++);
788 add_bytes_to_string(&name
, prog
, p
);
793 #if defined(HAVE_CYGWIN_CONV_TO_FULL_WIN32_PATH)
795 unsigned char new_path
[MAX_PATH
];
797 unsigned char new_path
[1024];
800 cygwin_conv_to_full_win32_path(file
, new_path
);
801 add_to_string(&name
, new_path
);
803 add_shell_quoted_to_string(&name
, file
, strlen(file
));
812 if (init_string(&s
)) {
813 add_to_string(&s
, "/bin/cat ");
814 add_shell_quoted_to_string(&s
, file
, strlen(file
));
815 add_to_string(&s
, " | ");
816 add_string_to_string(&s
, &name
);
827 common_download_do(struct terminal
*term
, int fd
, void *data
, int resume
)
829 struct file_download
*file_download
;
830 struct cmdw_hop
*cmdw_hop
= data
;
831 unsigned char *file
= cmdw_hop
->real_file
;
832 struct session
*ses
= cmdw_hop
->ses
;
837 if (!file
|| fstat(fd
, &buf
)) return;
839 file_download
= init_file_download(ses
->download_uri
, ses
, file
, fd
);
840 if (!file_download
) return;
842 if (resume
) file_download
->seek
= buf
.st_size
;
844 display_download(ses
->tab
->term
, file_download
, ses
);
846 load_uri(file_download
->uri
, ses
->referrer
, &file_download
->download
,
847 PRI_DOWNLOAD
, CACHE_MODE_NORMAL
, file_download
->seek
);
851 common_download(struct session
*ses
, unsigned char *file
, int resume
)
853 struct cmdw_hop
*cmdw_hop
;
855 if (!ses
->download_uri
) return;
857 cmdw_hop
= mem_calloc(1, sizeof(*cmdw_hop
));
858 if (!cmdw_hop
) return;
860 cmdw_hop
->magic
= COMMON_DOWNLOAD_DO
;
862 kill_downloads_to_file(file
);
864 create_download_file(ses
->tab
->term
, file
, &cmdw_hop
->real_file
, 0,
865 resume
, common_download_do
, cmdw_hop
);
869 start_download(void *ses
, unsigned char *file
)
871 common_download(ses
, file
, 0);
875 resume_download(void *ses
, unsigned char *file
)
877 common_download(ses
, file
, 1);
883 continue_download_do(struct terminal
*term
, int fd
, void *data
, int resume
)
885 struct codw_hop
*codw_hop
= data
;
886 struct file_download
*file_download
= NULL
;
887 struct type_query
*type_query
;
890 assert(codw_hop
->type_query
);
891 assert(codw_hop
->type_query
->uri
);
892 assert(codw_hop
->type_query
->ses
);
894 type_query
= codw_hop
->type_query
;
895 if (!codw_hop
->real_file
) goto cancel
;
897 file_download
= init_file_download(type_query
->uri
, type_query
->ses
,
898 codw_hop
->real_file
, fd
);
899 if (!file_download
) goto cancel
;
901 if (type_query
->external_handler
) {
902 file_download
->external_handler
= subst_file(type_query
->external_handler
,
904 file_download
->delete = 1;
905 mem_free(codw_hop
->file
);
906 mem_free_set(&type_query
->external_handler
, NULL
);
909 file_download
->block
= !!type_query
->block
;
911 /* Done here and not in init_file_download() so that the external
912 * handler can become initialized. */
913 display_download(term
, file_download
, type_query
->ses
);
915 move_download(&type_query
->download
, &file_download
->download
, PRI_DOWNLOAD
);
916 done_type_query(type_query
);
922 if (type_query
->external_handler
) mem_free_if(codw_hop
->file
);
923 tp_cancel(type_query
);
928 continue_download(void *data
, unsigned char *file
)
930 struct type_query
*type_query
= data
;
931 struct codw_hop
*codw_hop
= mem_calloc(1, sizeof(*codw_hop
));
934 tp_cancel(type_query
);
938 if (type_query
->external_handler
) {
939 /* FIXME: get_temp_name() calls tempnam(). --Zas */
940 file
= get_temp_name(type_query
->uri
);
943 tp_cancel(type_query
);
948 codw_hop
->type_query
= type_query
;
949 codw_hop
->file
= file
;
950 codw_hop
->magic
= CONTINUE_DOWNLOAD_DO
;
952 kill_downloads_to_file(file
);
954 create_download_file(type_query
->ses
->tab
->term
, file
,
955 &codw_hop
->real_file
,
956 !!type_query
->external_handler
, 0,
957 continue_download_do
, codw_hop
);
961 static struct type_query
*
962 init_type_query(struct session
*ses
, struct download
*download
,
963 struct cache_entry
*cached
)
965 struct type_query
*type_query
;
967 /* There can be only one ... */
968 foreach (type_query
, ses
->type_queries
)
969 if (compare_uri(type_query
->uri
, ses
->loading_uri
, 0))
972 type_query
= mem_calloc(1, sizeof(*type_query
));
973 if (!type_query
) return NULL
;
975 type_query
->uri
= get_uri_reference(ses
->loading_uri
);
976 type_query
->ses
= ses
;
977 type_query
->target_frame
= null_or_stracpy(ses
->task
.target
.frame
);
979 type_query
->cached
= cached
;
980 type_query
->cgi
= cached
->cgi
;
981 object_lock(type_query
->cached
);
983 move_download(download
, &type_query
->download
, PRI_MAIN
);
984 download
->state
= connection_state(S_OK
);
986 add_to_list(ses
->type_queries
, type_query
);
992 done_type_query(struct type_query
*type_query
)
994 /* Unregister any active download */
995 cancel_download(&type_query
->download
, 0);
997 object_unlock(type_query
->cached
);
998 done_uri(type_query
->uri
);
999 mem_free_if(type_query
->external_handler
);
1000 mem_free_if(type_query
->target_frame
);
1001 del_from_list(type_query
);
1002 mem_free(type_query
);
1007 tp_cancel(void *data
)
1009 struct type_query
*type_query
= data
;
1011 /* XXX: Should we really abort? (1 vs 0 as the last param) --pasky */
1012 cancel_download(&type_query
->download
, 1);
1013 done_type_query(type_query
);
1018 tp_save(struct type_query
*type_query
)
1020 mem_free_set(&type_query
->external_handler
, NULL
);
1021 query_file(type_query
->ses
, type_query
->uri
, type_query
, continue_download
, tp_cancel
, 1);
1024 /** This button handler uses the add_dlg_button() interface so that pressing
1025 * 'Show header' will not close the type query dialog. */
1026 static widget_handler_status_T
1027 tp_show_header(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
1029 struct type_query
*type_query
= widget_data
->widget
->data
;
1031 cached_header_dialog(type_query
->ses
, type_query
->cached
);
1033 return EVENT_PROCESSED
;
1037 /** @bug FIXME: We need to modify this function to take frame data
1038 * instead, as we want to use this function for frames as well (now,
1039 * when frame has content type text/plain, it is ignored and displayed
1042 tp_display(struct type_query
*type_query
)
1044 struct view_state
*vs
;
1045 struct session
*ses
= type_query
->ses
;
1046 struct uri
*loading_uri
= ses
->loading_uri
;
1047 unsigned char *target_frame
= ses
->task
.target
.frame
;
1049 ses
->loading_uri
= type_query
->uri
;
1050 ses
->task
.target
.frame
= type_query
->target_frame
;
1051 vs
= ses_forward(ses
, /* type_query->frame */ 0);
1052 if (vs
) vs
->plain
= 1;
1053 ses
->loading_uri
= loading_uri
;
1054 ses
->task
.target
.frame
= target_frame
;
1056 if (/* !type_query->frame */ 1) {
1057 struct download
*old
= &type_query
->download
;
1058 struct download
*new = &cur_loc(ses
)->download
;
1060 new->callback
= (download_callback_T
*) doc_loading_callback
;
1063 move_download(old
, new, PRI_MAIN
);
1067 done_type_query(type_query
);
1071 tp_open(struct type_query
*type_query
)
1073 if (!type_query
->external_handler
|| !*type_query
->external_handler
) {
1074 tp_display(type_query
);
1078 if (type_query
->uri
->protocol
== PROTOCOL_FILE
&& !type_query
->cgi
) {
1079 unsigned char *file
= get_uri_string(type_query
->uri
, URI_PATH
);
1080 unsigned char *handler
= NULL
;
1084 handler
= subst_file(type_query
->external_handler
, file
);
1089 exec_on_terminal(type_query
->ses
->tab
->term
,
1091 type_query
->block
? TERM_EXEC_FG
: TERM_EXEC_BG
);
1095 done_type_query(type_query
);
1099 continue_download(type_query
, "");
1104 do_type_query(struct type_query
*type_query
, unsigned char *ct
, struct mime_handler
*handler
)
1106 /* [gettext_accelerator_context(.do_type_query)] */
1107 struct string filename
;
1108 unsigned char *description
;
1109 unsigned char *desc_sep
;
1110 unsigned char *format
, *text
, *title
;
1112 #define TYPE_QUERY_WIDGETS_COUNT 8
1113 int widgets
= TYPE_QUERY_WIDGETS_COUNT
;
1114 struct terminal
*term
= type_query
->ses
->tab
->term
;
1115 struct memory_list
*ml
;
1116 struct dialog_data
*dlg_data
;
1117 int selected_widget
;
1119 mem_free_set(&type_query
->external_handler
, NULL
);
1122 type_query
->block
= handler
->block
;
1123 if (!handler
->ask
) {
1124 type_query
->external_handler
= stracpy(handler
->program
);
1125 tp_open(type_query
);
1129 /* Start preparing for the type query dialog. */
1130 description
= handler
->description
;
1131 desc_sep
= *description
? "; " : "";
1132 title
= N_("What to do?");
1135 title
= N_("Unknown type");
1140 dlg
= calloc_dialog(TYPE_QUERY_WIDGETS_COUNT
, MAX_STR_LEN
* 2);
1143 if (init_string(&filename
)) {
1144 add_mime_filename_to_string(&filename
, type_query
->uri
);
1146 /* Let's make the filename pretty for display & save */
1147 /* TODO: The filename can be the empty string here. See bug 396. */
1150 decode_uri_string(&filename
);
1152 #endif /* CONFIG_UTF8 */
1153 decode_uri_string_for_display(&filename
);
1156 text
= get_dialog_offset(dlg
, TYPE_QUERY_WIDGETS_COUNT
);
1157 /* For "default directory index pages" with wrong content-type
1158 * the filename can be NULL, e.g. http://www.spamhaus.org in bug 396. */
1159 if (filename
.length
) {
1160 format
= _("What would you like to do with the file '%s' (type: %s%s%s)?", term
);
1161 snprintf(text
, MAX_STR_LEN
, format
, filename
.source
, ct
, desc_sep
, description
);
1163 format
= _("What would you like to do with the file (type: %s%s%s)?", term
);
1164 snprintf(text
, MAX_STR_LEN
, format
, ct
, desc_sep
, description
);
1167 done_string(&filename
);
1169 dlg
->title
= _(title
, term
);
1170 dlg
->layouter
= generic_dialog_layouter
;
1171 dlg
->layout
.padding_top
= 1;
1172 dlg
->layout
.fit_datalen
= 1;
1173 dlg
->udata2
= type_query
;
1175 add_dlg_text(dlg
, text
, ALIGN_LEFT
, 0);
1177 /* Add input field or text widget with info about the program handler. */
1178 if (!get_cmd_opt_bool("anonymous")) {
1179 unsigned char *field
= mem_calloc(1, MAX_STR_LEN
);
1186 if (handler
&& handler
->program
) {
1187 safe_strncpy(field
, handler
->program
, MAX_STR_LEN
);
1190 /* xgettext:no-c-format */
1191 add_dlg_field(dlg
, _("Program ('%' will be replaced by the filename)", term
),
1192 0, 0, NULL
, MAX_STR_LEN
, field
, NULL
);
1193 type_query
->external_handler
= field
;
1195 add_dlg_radio(dlg
, _("Block the terminal", term
), 0, 0, &type_query
->block
);
1196 selected_widget
= 3;
1198 } else if (handler
) {
1199 unsigned char *field
= text
+ MAX_STR_LEN
;
1201 format
= _("The file will be opened with the program '%s'.", term
);
1202 snprintf(field
, MAX_STR_LEN
, format
, handler
->program
);
1203 add_dlg_text(dlg
, field
, ALIGN_LEFT
, 0);
1205 type_query
->external_handler
= stracpy(handler
->program
);
1206 if (!type_query
->external_handler
) {
1212 selected_widget
= 2;
1216 selected_widget
= 1;
1219 /* Add buttons if they are both usable and allowed. */
1221 if (!get_cmd_opt_bool("anonymous") || handler
) {
1222 add_dlg_ok_button(dlg
, _("~Open", term
), B_ENTER
,
1223 (done_handler_T
*) tp_open
, type_query
);
1228 if (!get_cmd_opt_bool("anonymous")) {
1229 add_dlg_ok_button(dlg
, _("Sa~ve", term
), B_ENTER
,
1230 (done_handler_T
*) tp_save
, type_query
);
1235 add_dlg_ok_button(dlg
, _("~Display", term
), B_ENTER
,
1236 (done_handler_T
*) tp_display
, type_query
);
1238 if (type_query
->cached
&& type_query
->cached
->head
) {
1239 add_dlg_button(dlg
, _("Show ~header", term
), B_ENTER
,
1240 tp_show_header
, type_query
);
1245 add_dlg_ok_button(dlg
, _("~Cancel", term
), B_ESC
,
1246 (done_handler_T
*) tp_cancel
, type_query
);
1248 add_dlg_end(dlg
, widgets
);
1250 ml
= getml(dlg
, (void *) NULL
);
1252 /* XXX: Assume that the allocated @external_handler will be
1253 * freed when releasing the @type_query. */
1258 dlg_data
= do_dialog(term
, dlg
, ml
);
1259 /* Don't focus the text field; we want the user to be able
1260 * to select a button by typing the first letter of its label
1261 * without having to first leave the text field. */
1263 select_widget_by_id(dlg_data
, selected_widget
);
1268 unsigned char *type
;
1269 unsigned int plain
:1;
1270 } static const known_types
[] = {
1272 { "text/plain", 1 },
1273 { "application/xhtml+xml", 0 }, /* RFC 3236 */
1275 { "application/docbook+xml", 1 },
1276 { "application/rss+xml", 0 },
1277 { "application/xbel+xml", 1 },
1278 { "application/xbel", 1 },
1279 { "application/x-xbel", 1 },
1285 setup_download_handler(struct session
*ses
, struct download
*loading
,
1286 struct cache_entry
*cached
, int frame
)
1288 struct mime_handler
*handler
;
1289 struct view_state
*vs
;
1290 struct type_query
*type_query
;
1291 unsigned char *ctype
= get_content_type(cached
);
1296 if (!ctype
|| !*ctype
)
1297 goto plaintext_follow
;
1299 for (i
= 0; known_types
[i
].type
; i
++) {
1300 if (strcasecmp(ctype
, known_types
[i
].type
))
1303 plaintext
= known_types
[i
].plain
;
1304 goto plaintext_follow
;
1307 xwin
= ses
->tab
->term
->environment
& ENV_XWIN
;
1308 handler
= get_mime_type_handler(ctype
, xwin
);
1310 if (!handler
&& strlen(ctype
) >= 4 && !strncasecmp(ctype
, "text", 4))
1311 goto plaintext_follow
;
1313 type_query
= init_type_query(ses
, loading
, cached
);
1316 #ifdef CONFIG_BITTORRENT
1317 /* A terrible waste of a good MIME handler here, but we want
1318 * to use the type_query this is easier. */
1319 if ((!strcasecmp(ctype
, "application/x-bittorrent")
1320 || !strcasecmp(ctype
, "application/x-torrent"))
1321 && !get_cmd_opt_bool("anonymous"))
1322 query_bittorrent_dialog(type_query
);
1325 do_type_query(type_query
, ctype
, handler
);
1328 mem_free_if(handler
);
1333 vs
= ses_forward(ses
, frame
);
1334 if (vs
) vs
->plain
= plaintext
;