2007-07-26 20:07:39 -04:00
|
|
|
/** Downloads managment
|
|
|
|
* @file */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_SYS_CYGWIN_H
|
|
|
|
#include <sys/cygwin.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <utime.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "bfu/dialog.h"
|
|
|
|
#include "cache/cache.h"
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "dialogs/document.h"
|
|
|
|
#include "dialogs/download.h"
|
|
|
|
#include "dialogs/menu.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/object.h"
|
2010-07-24 11:07:18 -04:00
|
|
|
#include "main/select.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "mime/mime.h"
|
|
|
|
#include "network/connection.h"
|
|
|
|
#include "network/progress.h"
|
|
|
|
#include "network/state.h"
|
|
|
|
#include "osdep/osdep.h"
|
|
|
|
#include "protocol/bittorrent/dialogs.h"
|
|
|
|
#include "protocol/date.h"
|
|
|
|
#include "protocol/protocol.h"
|
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "session/download.h"
|
|
|
|
#include "session/history.h"
|
|
|
|
#include "session/location.h"
|
|
|
|
#include "session/session.h"
|
|
|
|
#include "session/task.h"
|
|
|
|
#include "terminal/draw.h"
|
|
|
|
#include "terminal/screen.h"
|
|
|
|
#include "terminal/terminal.h"
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/file.h"
|
|
|
|
#include "util/lists.h"
|
|
|
|
#include "util/memlist.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "util/time.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: tp_*() should be in separate file, I guess? --pasky */
|
|
|
|
|
|
|
|
|
2007-07-26 15:39:08 -04:00
|
|
|
INIT_LIST_OF(struct file_download, downloads);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
download_is_progressing(struct download *download)
|
|
|
|
{
|
|
|
|
return download
|
2008-08-03 08:24:26 -04:00
|
|
|
&& is_in_state(download->state, S_TRANS)
|
2005-09-15 09:58:31 -04:00
|
|
|
&& has_progress(download->progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
are_there_downloads(void)
|
|
|
|
{
|
|
|
|
struct file_download *file_download;
|
|
|
|
|
|
|
|
foreach (file_download, downloads)
|
|
|
|
if (!file_download->external_handler)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void download_data(struct download *download, struct file_download *file_download);
|
|
|
|
|
2009-07-18 20:57:10 -04:00
|
|
|
/*! @note If this fails, the caller is responsible of freeing @a file
|
|
|
|
* and closing @a fd. */
|
2005-09-15 09:58:31 -04:00
|
|
|
struct file_download *
|
2021-01-02 10:20:27 -05:00
|
|
|
init_file_download(struct uri *uri, struct session *ses, char *file, int fd)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct file_download *file_download;
|
|
|
|
|
2022-01-16 15:08:50 -05:00
|
|
|
file_download = (struct file_download *)mem_calloc(1, sizeof(*file_download));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!file_download) return NULL;
|
|
|
|
|
|
|
|
/* Actually we could allow fragments in the URI and just change all the
|
|
|
|
* places that compares and shows the URI, but for now it is much
|
|
|
|
* easier this way. */
|
|
|
|
file_download->uri = get_composed_uri(uri, URI_BASE);
|
|
|
|
if (!file_download->uri) {
|
|
|
|
mem_free(file_download);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_download_display(file_download);
|
|
|
|
|
|
|
|
file_download->file = file;
|
|
|
|
file_download->handle = fd;
|
|
|
|
|
|
|
|
file_download->download.callback = (download_callback_T *) download_data;
|
|
|
|
file_download->download.data = file_download;
|
|
|
|
file_download->ses = ses;
|
|
|
|
/* The tab may be closed, but we will still want to ie. open the
|
|
|
|
* handler on that terminal. */
|
|
|
|
file_download->term = ses->tab->term;
|
|
|
|
|
|
|
|
object_nolock(file_download, "file_download"); /* Debugging purpose. */
|
|
|
|
add_to_list(downloads, file_download);
|
|
|
|
|
|
|
|
return file_download;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
abort_download(struct file_download *file_download)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* When hacking to cleanup the download code, remove lots of duplicated
|
|
|
|
* code and implement stuff from bug 435 we should reintroduce this
|
|
|
|
* assertion. Currently it will trigger often and shows that the
|
|
|
|
* download dialog code potentially could access free()d memory. */
|
|
|
|
assert(!is_object_used(file_download));
|
|
|
|
#endif
|
|
|
|
done_download_display(file_download);
|
|
|
|
|
|
|
|
if (file_download->ses)
|
|
|
|
check_questions_queue(file_download->ses);
|
|
|
|
|
|
|
|
if (file_download->dlg_data)
|
|
|
|
cancel_dialog(file_download->dlg_data, NULL);
|
2006-02-12 10:41:21 -05:00
|
|
|
cancel_download(&file_download->download, file_download->stop);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (file_download->uri) done_uri(file_download->uri);
|
|
|
|
|
|
|
|
if (file_download->handle != -1) {
|
|
|
|
prealloc_truncate(file_download->handle, file_download->seek);
|
|
|
|
close(file_download->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_free_if(file_download->external_handler);
|
|
|
|
if (file_download->file) {
|
2016-04-20 12:57:32 -04:00
|
|
|
if (file_download->delete_) unlink(file_download->file);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(file_download->file);
|
|
|
|
}
|
2022-05-21 12:17:04 -04:00
|
|
|
mem_free_if(file_download->inpext);
|
|
|
|
mem_free_if(file_download->outext);
|
2005-09-15 09:58:31 -04:00
|
|
|
del_from_list(file_download);
|
|
|
|
mem_free(file_download);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
kill_downloads_to_file(char *file)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct file_download *file_download;
|
|
|
|
|
|
|
|
foreach (file_download, downloads) {
|
|
|
|
if (strcmp(file_download->file, file))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file_download = file_download->prev;
|
|
|
|
abort_download(file_download->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
abort_all_downloads(void)
|
|
|
|
{
|
|
|
|
while (!list_empty(downloads))
|
2022-01-26 11:51:52 -05:00
|
|
|
abort_download((struct file_download *)downloads.next);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
destroy_downloads(struct session *ses)
|
|
|
|
{
|
|
|
|
struct file_download *file_download, *next;
|
|
|
|
struct session *s;
|
|
|
|
|
|
|
|
/* We are supposed to blat all downloads to external handlers belonging
|
|
|
|
* to @ses, but we will refuse to do so if there is another session
|
|
|
|
* bound to this terminal. That looks like the reasonable thing to do,
|
|
|
|
* fulfilling the principle of least astonishment. */
|
|
|
|
foreach (s, sessions) {
|
|
|
|
if (s == ses || s->tab->term != ses->tab->term)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foreach (file_download, downloads) {
|
|
|
|
if (file_download->ses != ses)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file_download->ses = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreachsafe (file_download, next, downloads) {
|
|
|
|
if (file_download->ses != ses)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!file_download->external_handler) {
|
|
|
|
file_download->ses = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_download(file_download);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 03:06:20 -04:00
|
|
|
void
|
|
|
|
detach_downloads_from_terminal(struct terminal *term)
|
|
|
|
{
|
|
|
|
struct file_download *file_download, *next;
|
|
|
|
|
|
|
|
assert(term != NULL);
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
foreachsafe (file_download, next, downloads) {
|
|
|
|
if (file_download->term != term)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!file_download->external_handler) {
|
|
|
|
file_download->term = NULL;
|
|
|
|
if (file_download->ses
|
|
|
|
&& file_download->ses->tab->term == term)
|
|
|
|
file_download->ses = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_download(file_download);
|
|
|
|
}
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
download_error_dialog(struct file_download *file_download, int saved_errno)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *emsg = (char *) strerror(saved_errno);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = file_download->ses;
|
|
|
|
struct terminal *term = file_download->term;
|
|
|
|
|
|
|
|
if (!ses) return;
|
|
|
|
|
|
|
|
info_box(term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Download error"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("Could not create file '%s':\n%s"),
|
|
|
|
file_download->file, emsg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_cache_entry_to_file(struct cache_entry *cached, struct file_download *file_download)
|
|
|
|
{
|
|
|
|
struct fragment *frag;
|
|
|
|
|
|
|
|
if (file_download->download.progress && file_download->download.progress->seek) {
|
|
|
|
file_download->seek = file_download->download.progress->seek;
|
|
|
|
file_download->download.progress->seek = 0;
|
|
|
|
/* This is exclusive with the prealloc, thus we can perform
|
|
|
|
* this in front of that thing safely. */
|
|
|
|
if (lseek(file_download->handle, file_download->seek, SEEK_SET) < 0) {
|
|
|
|
download_error_dialog(file_download, errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (frag, cached->frag) {
|
|
|
|
off_t remain = file_download->seek - frag->offset;
|
|
|
|
int *h = &file_download->handle;
|
|
|
|
ssize_t w;
|
|
|
|
|
|
|
|
if (remain < 0 || frag->length <= remain)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef USE_OPEN_PREALLOC
|
|
|
|
if (!file_download->seek
|
|
|
|
&& (!file_download->download.progress
|
|
|
|
|| file_download->download.progress->size > 0)) {
|
|
|
|
close(*h);
|
|
|
|
*h = open_prealloc(file_download->file,
|
|
|
|
O_CREAT|O_WRONLY|O_TRUNC,
|
|
|
|
0666,
|
|
|
|
file_download->download.progress
|
|
|
|
? file_download->download.progress->size
|
|
|
|
: cached->length);
|
|
|
|
if (*h == -1) {
|
|
|
|
download_error_dialog(file_download, errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_bin(*h);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
w = safe_write(*h, frag->data + remain, frag->length - remain);
|
|
|
|
if (w == -1) {
|
|
|
|
download_error_dialog(file_download, errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_download->seek += w;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
abort_download_and_beep(struct file_download *file_download, struct terminal *term)
|
|
|
|
{
|
2007-08-30 17:11:51 -04:00
|
|
|
if (term && get_opt_int("document.download.notify_bell",
|
|
|
|
file_download->ses)
|
2005-09-15 09:58:31 -04:00
|
|
|
+ file_download->notify >= 2) {
|
|
|
|
beep_terminal(term);
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_download(file_download);
|
|
|
|
}
|
|
|
|
|
2010-07-24 11:07:18 -04:00
|
|
|
struct exec_mailcap {
|
|
|
|
struct session *ses;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *command;
|
|
|
|
char *file;
|
2010-07-24 11:07:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_follow_url_mailcap(struct session *ses, struct uri *uri)
|
|
|
|
{
|
|
|
|
if (!uri) {
|
|
|
|
print_error_dialog(ses, connection_state(S_BAD_URL), uri, PRI_CANCEL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ses->reloadlevel = CACHE_MODE_NORMAL;
|
|
|
|
|
|
|
|
if (ses->task.type == TASK_FORWARD) {
|
|
|
|
if (compare_uri(ses->loading_uri, uri, 0)) {
|
|
|
|
/* We're already loading the URL. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_loading(ses, 0);
|
|
|
|
|
|
|
|
ses_goto(ses, uri, NULL, NULL, CACHE_MODE_NORMAL, TASK_FORWARD, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exec_mailcap_command(void *data)
|
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct exec_mailcap *exec_mailcap = (struct exec_mailcap *)data;
|
2010-07-24 11:07:18 -04:00
|
|
|
|
|
|
|
if (exec_mailcap) {
|
|
|
|
if (exec_mailcap->command) {
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string string;
|
2010-07-24 11:07:18 -04:00
|
|
|
|
2010-07-25 08:40:19 -04:00
|
|
|
if (init_string(&string)) {
|
2022-01-15 13:08:39 -05:00
|
|
|
static char mailcap_elmailcap[] = "mailcap:elmailcap";
|
|
|
|
struct uri *ref = get_uri(mailcap_elmailcap, URI_NONE);
|
2010-07-24 11:07:18 -04:00
|
|
|
struct uri *uri;
|
|
|
|
struct session *ses = exec_mailcap->ses;
|
|
|
|
|
2010-07-25 08:40:19 -04:00
|
|
|
add_to_string(&string, "mailcap:");
|
|
|
|
add_to_string(&string, exec_mailcap->command);
|
|
|
|
if (exec_mailcap->file) {
|
2010-07-31 15:15:12 -04:00
|
|
|
add_to_string(&string, " && /bin/rm -f ");
|
2010-07-25 08:40:19 -04:00
|
|
|
add_to_string(&string, exec_mailcap->file);
|
|
|
|
}
|
2010-07-24 11:07:18 -04:00
|
|
|
|
2022-01-14 14:52:17 -05:00
|
|
|
uri = get_uri(string.source, URI_NONE);
|
2010-07-25 08:40:19 -04:00
|
|
|
done_string(&string);
|
2010-07-24 11:07:18 -04:00
|
|
|
set_session_referrer(ses, ref);
|
|
|
|
if (ref) done_uri(ref);
|
|
|
|
|
|
|
|
do_follow_url_mailcap(ses, uri);
|
|
|
|
if (uri) done_uri(uri);
|
|
|
|
}
|
|
|
|
mem_free(exec_mailcap->command);
|
|
|
|
}
|
|
|
|
mem_free_if(exec_mailcap->file);
|
|
|
|
mem_free(exec_mailcap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
exec_later(struct session *ses, char *handler, char *file)
|
2010-07-24 11:07:18 -04:00
|
|
|
{
|
2022-01-16 15:08:50 -05:00
|
|
|
struct exec_mailcap *exec_mailcap = (struct exec_mailcap *)mem_calloc(1, sizeof(*exec_mailcap));
|
2010-07-24 11:07:18 -04:00
|
|
|
|
|
|
|
if (exec_mailcap) {
|
|
|
|
exec_mailcap->ses = ses;
|
|
|
|
exec_mailcap->command = null_or_stracpy(handler);
|
|
|
|
exec_mailcap->file = null_or_stracpy(file);
|
|
|
|
register_bottom_half(exec_mailcap_command, exec_mailcap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 12:17:04 -04:00
|
|
|
static void
|
|
|
|
exec_dgi_command(void *data)
|
|
|
|
{
|
|
|
|
struct exec_dgi *exec_dgi = (struct exec_dgi *)data;
|
|
|
|
|
|
|
|
if (exec_dgi) {
|
|
|
|
if (exec_dgi->command) {
|
|
|
|
struct string string;
|
|
|
|
|
|
|
|
if (init_string(&string)) {
|
|
|
|
static char dgi_dgi[] = "dgi://";
|
|
|
|
struct uri *ref = get_uri(dgi_dgi, URI_NONE);
|
|
|
|
struct uri *uri;
|
|
|
|
struct session *ses = exec_dgi->ses;
|
|
|
|
|
|
|
|
add_to_string(&string, "dgi:///dgi?command=");
|
|
|
|
add_to_string(&string, exec_dgi->command);
|
|
|
|
add_to_string(&string, "&filename=");
|
|
|
|
if (exec_dgi->file) {
|
|
|
|
add_to_string(&string, exec_dgi->file);
|
|
|
|
}
|
|
|
|
add_to_string(&string, "&inpext=");
|
|
|
|
if (exec_dgi->inpext) {
|
|
|
|
add_to_string(&string, exec_dgi->inpext);
|
|
|
|
}
|
|
|
|
add_to_string(&string, "&outext=");
|
|
|
|
if (exec_dgi->outext) {
|
|
|
|
add_to_string(&string, exec_dgi->outext);
|
|
|
|
}
|
|
|
|
if (exec_dgi->del) {
|
|
|
|
add_to_string(&string, "&delete=1");
|
|
|
|
}
|
|
|
|
uri = get_uri(string.source, URI_BASE_FRAGMENT);
|
|
|
|
done_string(&string);
|
|
|
|
set_session_referrer(ses, ref);
|
|
|
|
if (ref) done_uri(ref);
|
|
|
|
|
|
|
|
do_follow_url_mailcap(ses, uri);
|
|
|
|
if (uri) done_uri(uri);
|
|
|
|
}
|
|
|
|
mem_free(exec_dgi->command);
|
|
|
|
}
|
|
|
|
mem_free_if(exec_dgi->file);
|
|
|
|
mem_free_if(exec_dgi->inpext);
|
|
|
|
mem_free_if(exec_dgi->outext);
|
|
|
|
mem_free(exec_dgi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exec_later_dgi(struct session *ses, char *handler, char *file, char *inpext, char *outext, int del)
|
|
|
|
{
|
|
|
|
struct exec_dgi *exec_dgi = (struct exec_dgi *)mem_calloc(1, sizeof(*exec_dgi));
|
|
|
|
|
|
|
|
if (exec_dgi) {
|
|
|
|
exec_dgi->ses = ses;
|
|
|
|
exec_dgi->command = null_or_stracpy(handler);
|
|
|
|
exec_dgi->file = null_or_stracpy(file);
|
|
|
|
exec_dgi->inpext = null_or_stracpy(inpext);
|
|
|
|
exec_dgi->outext = null_or_stracpy(outext);
|
|
|
|
exec_dgi->del = del;
|
|
|
|
register_bottom_half(exec_dgi_command, exec_dgi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
|
|
|
download_data_store(struct download *download, struct file_download *file_download)
|
|
|
|
{
|
|
|
|
struct terminal *term = file_download->term;
|
|
|
|
|
2008-09-30 03:06:20 -04:00
|
|
|
assert_terminal_ptr_not_dangling(term);
|
|
|
|
if_assert_failed term = file_download->term = NULL;
|
|
|
|
|
2007-11-07 08:01:10 -05:00
|
|
|
if (is_in_progress_state(download->state)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
if (file_download->dlg_data)
|
|
|
|
redraw_dialog(file_download->dlg_data, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
/* If the original terminal of the download has been closed,
|
|
|
|
* display any messages in the default terminal instead. */
|
|
|
|
if (term == NULL)
|
|
|
|
term = get_default_terminal(); /* may be NULL too */
|
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
if (!is_in_state(download->state, S_OK)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *url = get_uri_string(file_download->uri, URI_PUBLIC);
|
2008-08-03 08:24:26 -04:00
|
|
|
struct connection_state state = download->state;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
/* abort_download_and_beep allows term==NULL. */
|
2005-09-15 09:58:31 -04:00
|
|
|
abort_download_and_beep(file_download, term);
|
|
|
|
|
|
|
|
if (!url) return;
|
|
|
|
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
if (term) {
|
|
|
|
info_box(term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Download error"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("Error downloading %s:\n\n%s"),
|
|
|
|
url, get_state_message(state, term)));
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_download->external_handler) {
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
if (term == NULL) {
|
|
|
|
/* There is no terminal in which to run the handler.
|
2016-04-20 12:57:32 -04:00
|
|
|
* Abort the download. file_download->delete_ should
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
* be 1 here so that the following call also deletes
|
|
|
|
* the temporary file. */
|
|
|
|
abort_download(file_download);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
prealloc_truncate(file_download->handle, file_download->seek);
|
|
|
|
close(file_download->handle);
|
|
|
|
file_download->handle = -1;
|
2010-07-24 11:07:18 -04:00
|
|
|
if (file_download->copiousoutput) {
|
|
|
|
exec_later(file_download->ses,
|
2010-07-25 08:40:19 -04:00
|
|
|
file_download->external_handler, file_download->file);
|
2010-07-27 04:07:52 -04:00
|
|
|
/* Temporary file is deleted by the mailcap_protocol_handler */
|
2016-04-20 12:57:32 -04:00
|
|
|
file_download->delete_ = 0;
|
2022-05-21 12:17:04 -04:00
|
|
|
} else if (file_download->dgi) {
|
|
|
|
exec_later_dgi(file_download->ses,
|
|
|
|
file_download->external_handler, file_download->file,
|
|
|
|
file_download->inpext, file_download->outext, 1);
|
|
|
|
/* Temporary file is deleted by the dgi_protocol_handler */
|
|
|
|
file_download->delete_ = 0;
|
2010-07-24 11:07:18 -04:00
|
|
|
} else {
|
|
|
|
exec_on_terminal(term, file_download->external_handler,
|
|
|
|
file_download->file,
|
|
|
|
file_download->block ? TERM_EXEC_FG :
|
|
|
|
TERM_EXEC_BG);
|
|
|
|
}
|
2016-04-20 12:57:32 -04:00
|
|
|
file_download->delete_ = 0;
|
2007-11-07 06:08:36 -05:00
|
|
|
abort_download_and_beep(file_download, term);
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
if (file_download->notify && term) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *url = get_uri_string(file_download->uri, URI_PUBLIC);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* This is apparently a little racy. Deleting the box item will
|
|
|
|
* update the download browser _after_ the notification dialog
|
|
|
|
* has been drawn whereby it will be hidden. This should make
|
|
|
|
* the download browser update before launcing any
|
|
|
|
* notification. */
|
|
|
|
done_download_display(file_download);
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
info_box(term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Download"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("Download complete:\n%s"), url));
|
|
|
|
mem_free(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_download->remotetime
|
2007-08-30 17:11:51 -04:00
|
|
|
&& get_opt_bool("document.download.set_original_time",
|
|
|
|
file_download->ses)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
struct utimbuf foo;
|
|
|
|
|
|
|
|
foo.actime = foo.modtime = file_download->remotetime;
|
|
|
|
utime(file_download->file, &foo);
|
|
|
|
}
|
|
|
|
|
bug 1054: Don't abort downloads when closing a terminal.
Except if they have external handlers.
When ELinks receives an event from a terminal, move that terminal to
the beginning of the global "terminals" list, so that the terminals
are always sorted according to the time of the most recent use. Note,
this affects the numbering of bookmark folders in session snapshots.
Add get_default_terminal(), which returns the most recently used
terminal that is still open. Use that in various places that
previously used terminals.prev or terminals.next. Four functions
fetch the size of the terminal for User-Agent headers, and
get_default_terminal() is not really right, but neither was the
original code; add TODO comments in those functions.
When the user chooses "Background and Notify", associate the download
with the terminal where the dialog box is. So any later messages will
then appear in that terminal, if it is still open. However, don't
change the terminal if the download has an external handler.
When a download gets some data, don't immediately check the associated
terminal. Instead, wait for the download to end. Then, if the
terminal of the download has been closed, use get_default_terminal()
instead. If there is no default terminal either, just skip any
message boxes.
2008-10-15 04:05:43 -04:00
|
|
|
/* abort_download_and_beep allows term==NULL. */
|
2005-09-15 09:58:31 -04:00
|
|
|
abort_download_and_beep(file_download, term);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
download_data(struct download *download, struct file_download *file_download)
|
|
|
|
{
|
|
|
|
struct cache_entry *cached = download->cached;
|
|
|
|
|
|
|
|
if (!cached || is_in_queued_state(download->state)) {
|
|
|
|
download_data_store(download, file_download);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cached->last_modified)
|
|
|
|
file_download->remotetime = parse_date(&cached->last_modified, NULL, 0, 1);
|
|
|
|
|
|
|
|
if (cached->redirect && file_download->redirect_cnt++ < MAX_REDIRECTS) {
|
2006-02-12 10:41:21 -05:00
|
|
|
cancel_download(&file_download->download, 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
assertm(compare_uri(cached->uri, file_download->uri, 0),
|
|
|
|
"Redirecting using bad base URI");
|
|
|
|
|
|
|
|
done_uri(file_download->uri);
|
|
|
|
|
|
|
|
file_download->uri = get_uri_reference(cached->redirect);
|
2008-08-03 08:24:26 -04:00
|
|
|
file_download->download.state = connection_state(S_WAIT_REDIR);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (file_download->dlg_data)
|
|
|
|
redraw_dialog(file_download->dlg_data, 1);
|
|
|
|
|
|
|
|
load_uri(file_download->uri, cached->uri, &file_download->download,
|
|
|
|
PRI_DOWNLOAD, CACHE_MODE_NORMAL,
|
|
|
|
download->progress ? download->progress->start : 0);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write_cache_entry_to_file(cached, file_download)) {
|
|
|
|
detach_connection(download, file_download->seek);
|
|
|
|
abort_download(file_download);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
detach_connection(download, file_download->seek);
|
|
|
|
download_data_store(download, file_download);
|
|
|
|
}
|
|
|
|
|
2009-07-17 16:16:38 -04:00
|
|
|
/** Type of the callback function that will be called when the user
|
|
|
|
* answers the question posed by lookup_unique_name().
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* The terminal on which the callback should display any windows.
|
|
|
|
* Comes directly from the @a term argument of lookup_unique_name().
|
|
|
|
*
|
|
|
|
* @param file
|
|
|
|
* The name of the local file to which the data should be downloaded,
|
|
|
|
* or NULL if the download should not begin. The callback is
|
|
|
|
* responsible of doing mem_free(@a file).
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* A pointer to any data that the callback cares about.
|
|
|
|
* Comes directly from the @a data argument of lookup_unique_name().
|
|
|
|
*
|
2009-07-24 09:57:33 -04:00
|
|
|
* @param flags
|
2009-07-24 11:09:59 -04:00
|
|
|
* The same as the @a flags argument of create_download_file(),
|
|
|
|
* except the ::DOWNLOAD_RESUME_SELECTED bit will be changed to match
|
|
|
|
* what the user chose.
|
2009-07-17 16:16:38 -04:00
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2021-01-02 10:20:27 -05:00
|
|
|
typedef void lun_callback_T(struct terminal *term, char *file,
|
2022-01-28 11:26:15 -05:00
|
|
|
void *data, download_flags_T flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The user is being asked what to do when the local file for
|
|
|
|
* the download already exists. This structure is allocated by
|
|
|
|
* lookup_unique_name() and freed by each lun_* function:
|
|
|
|
* lun_alternate(), lun_cancel(), lun_overwrite(), and lun_resume(). */
|
2005-09-15 09:58:31 -04:00
|
|
|
struct lun_hop {
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The terminal in which ELinks is asking the question.
|
2009-07-17 16:11:47 -04:00
|
|
|
* This gets passed to #callback. */
|
2005-09-15 09:58:31 -04:00
|
|
|
struct terminal *term;
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The name of the local file into which the data was
|
|
|
|
* originally going to be downloaded, but which already
|
|
|
|
* exists. In this string, "~" has already been expanded
|
|
|
|
* to the home directory. The string must be freed with
|
|
|
|
* mem_free(). */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *ofile;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
|
|
|
/** An alternative file name that the user may choose instead
|
|
|
|
* of #ofile. The string must be freed with mem_free(). */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
2009-07-17 16:16:38 -04:00
|
|
|
/** This function will be called when the user answers. */
|
|
|
|
lun_callback_T *callback;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
2009-07-20 20:26:59 -04:00
|
|
|
/** A pointer to be passed to #callback. */
|
2005-09-15 09:58:31 -04:00
|
|
|
void *data;
|
|
|
|
|
2009-07-24 11:09:59 -04:00
|
|
|
/** Saved flags to be passed to #callback.
|
|
|
|
* If the user chooses to resume, then lun_resume() sets
|
|
|
|
* ::DOWNLOAD_RESUME_SELECTED when it calls #callback.
|
|
|
|
*
|
|
|
|
* @invariant The ::DOWNLOAD_RESUME_SELECTED bit should be
|
|
|
|
* clear here because otherwise there would have been no
|
|
|
|
* reason to ask the user and initialize this structure. */
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags;
|
2006-07-16 08:46:46 -04:00
|
|
|
};
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Data saved by common_download() for the common_download_do()
|
|
|
|
* callback. */
|
2006-07-16 08:46:46 -04:00
|
|
|
struct cmdw_hop {
|
|
|
|
struct session *ses;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
2009-07-18 20:24:55 -04:00
|
|
|
/** The URI from which the data will be downloaded. */
|
|
|
|
struct uri *download_uri;
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The name of the local file to which the data will be
|
|
|
|
* downloaded. This is initially NULL, but its address is
|
|
|
|
* given to create_download_file(), which arranges for the
|
|
|
|
* pointer to be set before common_download_do() is called.
|
|
|
|
* The string must be freed with mem_free(). */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *real_file;
|
2006-07-16 08:46:46 -04:00
|
|
|
};
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Data saved by continue_download() for the continue_download_do()
|
|
|
|
* callback. */
|
2006-07-16 08:46:46 -04:00
|
|
|
struct codw_hop {
|
|
|
|
struct type_query *type_query;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
|
|
|
/** The name of the local file to which the data will be
|
|
|
|
* downloaded. This is initially NULL, but its address is
|
|
|
|
* given to create_download_file(), which arranges for the
|
|
|
|
* pointer to be set before continue_download_do() is called.
|
|
|
|
* The string must be freed with mem_free(). */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *real_file;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file;
|
2006-07-16 08:46:46 -04:00
|
|
|
};
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Data saved by create_download_file() for the create_download_file_do()
|
|
|
|
* callback. */
|
2006-07-16 08:46:46 -04:00
|
|
|
struct cdf_hop {
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Where to save the name of the file that was actually
|
|
|
|
* opened. One of the arguments of #callback is a file
|
|
|
|
* descriptor for this file. @c real_file can be NULL if
|
|
|
|
* #callback does not care about the name. */
|
2021-01-02 10:20:27 -05:00
|
|
|
char **real_file;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
|
|
|
/** This function will be called when the file has been opened,
|
2009-07-17 16:16:38 -04:00
|
|
|
* or when it is known that the file will not be opened. */
|
|
|
|
cdf_callback_T *callback;
|
2009-07-17 07:10:36 -04:00
|
|
|
|
2009-07-18 14:57:40 -04:00
|
|
|
/** A pointer to be passed to #callback. */
|
2006-07-16 08:46:46 -04:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The use chose "Save under the alternative name" when asked where
|
|
|
|
* to download a file.
|
|
|
|
*
|
|
|
|
* lookup_unique_name() passes this function as a ::done_handler_T to
|
|
|
|
* msg_box().
|
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2007-03-10 16:50:56 -05:00
|
|
|
lun_alternate(void *lun_hop_)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct lun_hop *lun_hop = (struct lun_hop *)lun_hop_;
|
2007-03-10 16:50:56 -05:00
|
|
|
|
2009-07-14 03:27:09 -04:00
|
|
|
lun_hop->callback(lun_hop->term, lun_hop->file, lun_hop->data,
|
2009-07-24 09:57:33 -04:00
|
|
|
lun_hop->flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(lun_hop->ofile);
|
|
|
|
mem_free(lun_hop);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The use chose "Cancel" when asked where to download a file.
|
|
|
|
*
|
|
|
|
* lookup_unique_name() passes this function as a ::done_handler_T to
|
|
|
|
* msg_box().
|
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2007-03-10 16:50:56 -05:00
|
|
|
lun_cancel(void *lun_hop_)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct lun_hop *lun_hop = (struct lun_hop *)lun_hop_;
|
2007-03-10 16:50:56 -05:00
|
|
|
|
2009-07-14 03:27:09 -04:00
|
|
|
lun_hop->callback(lun_hop->term, NULL, lun_hop->data,
|
2009-07-24 09:57:33 -04:00
|
|
|
lun_hop->flags);
|
2006-07-16 08:46:46 -04:00
|
|
|
mem_free_if(lun_hop->ofile);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(lun_hop->file);
|
|
|
|
mem_free(lun_hop);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The use chose "Overwrite the original file" when asked where to
|
|
|
|
* download a file.
|
|
|
|
*
|
|
|
|
* lookup_unique_name() passes this function as a ::done_handler_T to
|
|
|
|
* msg_box().
|
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2007-03-10 16:50:56 -05:00
|
|
|
lun_overwrite(void *lun_hop_)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct lun_hop *lun_hop = (struct lun_hop *)lun_hop_;
|
2007-03-10 16:50:56 -05:00
|
|
|
|
2009-07-14 03:27:09 -04:00
|
|
|
lun_hop->callback(lun_hop->term, lun_hop->ofile, lun_hop->data,
|
2009-07-24 09:57:33 -04:00
|
|
|
lun_hop->flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(lun_hop->file);
|
|
|
|
mem_free(lun_hop);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The user chose "Resume download of the original file" when asked
|
|
|
|
* where to download a file.
|
|
|
|
*
|
|
|
|
* lookup_unique_name() passes this function as a ::done_handler_T to
|
|
|
|
* msg_box().
|
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2007-03-10 16:50:56 -05:00
|
|
|
lun_resume(void *lun_hop_)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct lun_hop *lun_hop = (struct lun_hop *)lun_hop_;
|
2006-07-16 08:46:46 -04:00
|
|
|
|
2009-07-14 03:27:09 -04:00
|
|
|
lun_hop->callback(lun_hop->term, lun_hop->ofile, lun_hop->data,
|
2009-07-24 09:57:33 -04:00
|
|
|
lun_hop->flags | DOWNLOAD_RESUME_SELECTED);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(lun_hop->file);
|
|
|
|
mem_free(lun_hop);
|
|
|
|
}
|
|
|
|
|
2006-07-16 08:46:46 -04:00
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** If attempting to download to an existing file, perhaps ask
|
|
|
|
* the user whether to resume, overwrite, or save elsewhere.
|
|
|
|
* This function constructs a struct lun_hop, which will be freed
|
|
|
|
* when the user answers the question.
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* The terminal in which this function should show its UI.
|
|
|
|
*
|
|
|
|
* @param[in] ofile
|
|
|
|
* A proposed name for the local file to which the data would be
|
|
|
|
* downloaded. "~" here refers to the home directory.
|
|
|
|
* lookup_unique_name() treats this original string as read-only.
|
|
|
|
*
|
2009-07-24 09:57:33 -04:00
|
|
|
* @param[in] flags
|
2009-07-24 11:09:59 -04:00
|
|
|
* Flags controlling how to download the file.
|
|
|
|
* ::DOWNLOAD_RESUME_ALLOWED adds a "Resume" button to the dialog.
|
|
|
|
* ::DOWNLOAD_RESUME_SELECTED means the user already chose to resume
|
|
|
|
* downloading (with ::ACT_MAIN_LINK_DOWNLOAD_RESUME), before ELinks
|
|
|
|
* even asked for the file name; thus don't ask whether to overwrite.
|
|
|
|
* Other flags, such as ::DOWNLOAD_EXTERNAL, have no effect at this
|
|
|
|
* level but they get passed to @a callback.
|
2009-07-17 07:10:36 -04:00
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* Will be called when the user answers, or right away if the question
|
2009-07-17 16:16:38 -04:00
|
|
|
* need not or cannot be asked.
|
2009-07-17 07:10:36 -04:00
|
|
|
*
|
|
|
|
* @param data
|
2009-07-20 20:26:59 -04:00
|
|
|
* A pointer to be passed to @a callback.
|
2009-07-17 10:07:12 -04:00
|
|
|
*
|
|
|
|
* @relates lun_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
lookup_unique_name(struct terminal *term, char *ofile,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags,
|
2009-07-17 16:16:38 -04:00
|
|
|
lun_callback_T *callback, void *data)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
Here is a framework that detects cases where a PO file assigns
the same accelerator key to multiple buttons in a dialog box or
to multiple items in a menu. ELinks already has some support for
this but it requires the translator to run ELinks and manually
scan through all menus and dialogs. The attached changes make it
possible to quickly detect and list any conflicts, including ones
that can only occur on operating systems or configurations that
the translator is not currently using.
The changes have no immediate effect on the elinks executable or
the MO files. PO files become larger, however.
The scheme works like this:
- Like before, accelerator keys in translatable strings are
tagged with the tilde (~) character.
- Whenever a C source file defines an accelerator key, it must
assign one or more named "contexts" to it. The translations in
the PO files inherit these contexts. If multiple strings use
the same accelerator (case insensitive) in the same context,
that's a conflict and can be detected automatically.
- The contexts are defined with "gettext_accelerator_context"
comments in source files. These comments delimit regions where
all translatable strings containing tildes are given the same
contexts. There must be one special comment at the top of the
region; it lists the contexts assigned to that region. The
region automatically ends at the end of the function (found
with regexp /^\}/), but it can also be closed explicitly with
another special comment. The comments are formatted like this:
/* [gettext_accelerator_context(foo, bar, baz)]
begins a region that uses the contexts "foo", "bar", and "baz".
The comma is the delimiter; whitespace is optional.
[gettext_accelerator_context()]
ends the region. */
The scripts don't currently check whether this syntax occurs
inside or outside comments.
- The names of contexts consist of C identifiers delimited with
periods. I typically used the name of a function that sets
up a dialog, or the name of an array where the items of a
menu are listed. There is a special feature for static
functions: if the name begins with a period, then the period
will be replaced with the name of the source file and a colon.
- If a menu is programmatically generated from multiple parts,
of which some are never used together, so that it is safe to
use the same accelerators in them, then it is necessary to
define multiple contexts for the same menu. link_menu() in
src/viewer/text/link.c is the most complex example of this.
- During make update-po:
- A Perl script (po/gather-accelerator-contexts.pl) reads
po/elinks.pot, scans the source files listed in it for
"gettext_accelerator_context" comments, and rewrites
po/elinks.pot with "accelerator_context" comments that
indicate the contexts of each msgid: the union of all
contexts of all of its uses in the source files. It also
removes any "gettext_accelerator_context" comments that
xgettext --add-comments has copied to elinks.pot.
- If po/gather-accelerator-contexts.pl does not find any
contexts for some use of an msgid that seems to contain an
accelerator (because it contains a tilde), it warns. If the
tilde refers to e.g. "~/.elinks" and does not actually mark
an accelerator, the warning can be silenced by specifying the
special context "IGNORE", which the script otherwise ignores.
- msgmerge copies the "accelerator_context" comments from
po/elinks.pot to po/*.po. Translators do not edit those
comments.
- During make check-po:
- Another Perl script (po/check-accelerator-contexts.pl) reads
po/*.po and keeps track of which accelerators have been bound
in each context. It warns about any conflicts it finds.
This script does not access the C source files; thus it does
not matter if the line numbers in "#:" lines are out of date.
This implementation is not perfect and I am not proposing to
add it to the main source tree at this time. Specifically:
- It introduces compile-time dependencies on Perl and Locale::PO.
There should be a configure-time or compile-time check so that
the new features are skipped if the prerequisites are missing.
- When the scripts include msgstr strings in warnings, they
should transcode them from the charset of the PO file to the
one specified by the user's locale.
- It is not adequately documented (well, except perhaps here).
- po/check-accelerator-contexts.pl reports the same conflict
multiple times if it occurs in multiple contexts.
- The warning messages should include line numbers, so that users
of Emacs could conveniently edit the conflicting part of the PO
file. This is not feasible with the current version of
Locale::PO.
- Locale::PO does not understand #~ lines and spews warnings
about them. There is an ugly hack to hide these warnings.
- Jonas Fonseca suggested the script could propose accelerators
that are still available. This has not been implemented.
There are three files attached:
- po/gather-accelerator-contexts.pl: Augments elinks.pot with
context information.
- po/check-accelerator-contexts.pl: Checks conflicts.
- accelerator-contexts.diff: Makes po/Makefile run the scripts,
and adds special comments to source files.
2005-12-04 18:38:29 -05:00
|
|
|
/* [gettext_accelerator_context(.lookup_unique_name)] */
|
2009-07-20 18:06:36 -04:00
|
|
|
struct lun_hop *lun_hop = NULL;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file = NULL;
|
2009-07-20 18:06:36 -04:00
|
|
|
struct dialog_data *dialog_data;
|
2005-09-15 09:58:31 -04:00
|
|
|
int overwrite;
|
|
|
|
|
|
|
|
ofile = expand_tilde(ofile);
|
2009-07-20 17:57:17 -04:00
|
|
|
if (!ofile) goto error;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Minor code duplication to prevent useless call to get_opt_int()
|
|
|
|
* if possible. --Zas */
|
2009-07-24 09:57:33 -04:00
|
|
|
if (flags & DOWNLOAD_RESUME_SELECTED) {
|
2007-11-07 06:57:13 -05:00
|
|
|
callback(term, ofile, data, flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* !overwrite means always silently overwrite, which may be admitelly
|
|
|
|
* indeed a little confusing ;-) */
|
2007-08-28 12:41:18 -04:00
|
|
|
overwrite = get_opt_int("document.download.overwrite", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!overwrite) {
|
|
|
|
/* Nothing special to do... */
|
2007-11-07 06:57:13 -05:00
|
|
|
callback(term, ofile, data, flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if file is a directory, and use a default name if it's the
|
|
|
|
* case. */
|
|
|
|
if (file_is_dir(ofile)) {
|
|
|
|
info_box(term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Download error"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("'%s' is a directory."),
|
|
|
|
ofile));
|
2009-07-20 17:57:17 -04:00
|
|
|
goto error;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the file already exists (file != ofile). */
|
2021-03-31 13:24:26 -04:00
|
|
|
file = (flags & DOWNLOAD_OVERWRITE) ? ofile : get_unique_name(ofile);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!file || overwrite == 1 || file == ofile) {
|
|
|
|
/* Still nothing special to do... */
|
|
|
|
if (file != ofile) mem_free(ofile);
|
2009-07-24 09:57:33 -04:00
|
|
|
callback(term, file, data, flags & ~DOWNLOAD_RESUME_SELECTED);
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overwrite == 2 (ask) and file != ofile (=> original file already
|
|
|
|
* exists) */
|
|
|
|
|
2022-01-16 15:08:50 -05:00
|
|
|
lun_hop = (struct lun_hop *)mem_calloc(1, sizeof(*lun_hop));
|
2009-07-20 17:57:17 -04:00
|
|
|
if (!lun_hop) goto error;
|
2005-09-15 09:58:31 -04:00
|
|
|
lun_hop->term = term;
|
|
|
|
lun_hop->ofile = ofile;
|
2009-07-20 18:09:37 -04:00
|
|
|
lun_hop->file = file; /* file != ofile verified above */
|
2005-09-15 09:58:31 -04:00
|
|
|
lun_hop->callback = callback;
|
|
|
|
lun_hop->data = data;
|
2009-07-24 09:57:33 -04:00
|
|
|
lun_hop->flags = flags;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-20 18:06:36 -04:00
|
|
|
dialog_data = msg_box(
|
|
|
|
term, NULL, MSGBOX_FREE_TEXT,
|
2005-09-15 09:58:31 -04:00
|
|
|
N_("File exists"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("This file already exists:\n"
|
|
|
|
"%s\n\n"
|
|
|
|
"The alternative filename is:\n"
|
|
|
|
"%s"),
|
|
|
|
empty_string_or_(lun_hop->ofile),
|
|
|
|
empty_string_or_(file)),
|
|
|
|
lun_hop, 4,
|
2007-03-10 16:50:56 -05:00
|
|
|
MSG_BOX_BUTTON(N_("Sa~ve under the alternative name"), lun_alternate, B_ENTER),
|
|
|
|
MSG_BOX_BUTTON(N_("~Overwrite the original file"), lun_overwrite, 0),
|
2009-07-24 09:57:33 -04:00
|
|
|
MSG_BOX_BUTTON((flags & DOWNLOAD_RESUME_ALLOWED
|
2009-07-14 03:27:09 -04:00
|
|
|
? N_("~Resume download of the original file")
|
|
|
|
: NULL),
|
|
|
|
lun_resume, 0),
|
2007-03-10 16:50:56 -05:00
|
|
|
MSG_BOX_BUTTON(N_("~Cancel"), lun_cancel, B_ESC));
|
2009-07-20 18:06:36 -04:00
|
|
|
if (!dialog_data) goto error;
|
2009-07-20 17:57:17 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2009-07-20 18:06:36 -04:00
|
|
|
mem_free_if(lun_hop);
|
2009-07-20 17:57:17 -04:00
|
|
|
if (file != ofile) mem_free_if(file);
|
|
|
|
mem_free_if(ofile);
|
2009-07-24 09:57:33 -04:00
|
|
|
callback(term, NULL, data, flags & ~DOWNLOAD_RESUME_SELECTED);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Now that the final name of the download file has been chosen,
|
2009-07-17 16:16:38 -04:00
|
|
|
* open the file and call the ::cdf_callback_T that was originally
|
|
|
|
* given to create_download_file().
|
2009-07-17 07:10:36 -04:00
|
|
|
*
|
2009-07-17 16:16:38 -04:00
|
|
|
* create_download_file() passes this function as a ::lun_callback_T
|
|
|
|
* to lookup_unique_name().
|
2009-07-17 10:07:12 -04:00
|
|
|
*
|
|
|
|
* @relates cdf_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
create_download_file_do(struct terminal *term, char *file,
|
2022-01-28 11:26:15 -05:00
|
|
|
void *data, download_flags_T flags)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct cdf_hop *cdf_hop = (struct cdf_hop *)data;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *wd;
|
2005-09-15 09:58:31 -04:00
|
|
|
int h = -1;
|
|
|
|
int saved_errno;
|
|
|
|
#ifdef NO_FILE_SECURITY
|
|
|
|
int sf = 0;
|
|
|
|
#else
|
2009-07-24 11:09:59 -04:00
|
|
|
int sf = !!(flags & DOWNLOAD_EXTERNAL);
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!file) goto finish;
|
|
|
|
|
|
|
|
wd = get_cwd();
|
|
|
|
set_cwd(term->cwd);
|
|
|
|
|
2006-01-04 15:52:15 -05:00
|
|
|
/* Create parent directories if needed. */
|
|
|
|
mkalldirs(file);
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* O_APPEND means repositioning at the end of file before each write(),
|
|
|
|
* thus ignoring seek()s and that can hide mysterious bugs. IMHO.
|
|
|
|
* --pasky */
|
2009-07-14 03:27:09 -04:00
|
|
|
h = open(file, O_CREAT | O_WRONLY
|
2009-07-24 09:57:33 -04:00
|
|
|
| (flags & DOWNLOAD_RESUME_SELECTED ? 0 : O_TRUNC)
|
2021-03-31 13:24:26 -04:00
|
|
|
| (sf &&
|
|
|
|
!(flags & DOWNLOAD_RESUME_SELECTED) &&
|
|
|
|
!(flags & DOWNLOAD_OVERWRITE) ? O_EXCL : 0),
|
2005-09-15 09:58:31 -04:00
|
|
|
sf ? 0600 : 0666);
|
|
|
|
saved_errno = errno; /* Saved in case of ... --Zas */
|
|
|
|
|
|
|
|
if (wd) {
|
|
|
|
set_cwd(wd);
|
|
|
|
mem_free(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h == -1) {
|
|
|
|
info_box(term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Download error"), ALIGN_CENTER,
|
|
|
|
msg_text(term, N_("Could not create file '%s':\n%s"),
|
|
|
|
file, strerror(saved_errno)));
|
|
|
|
|
|
|
|
mem_free(file);
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
set_bin(h);
|
|
|
|
|
2007-11-07 07:15:37 -05:00
|
|
|
if (!(flags & DOWNLOAD_EXTERNAL)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *download_dir = get_opt_str("document.download.directory", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
safe_strncpy(download_dir, file, MAX_STR_LEN);
|
|
|
|
|
|
|
|
/* Find the used directory so it's available in history */
|
|
|
|
for (i = strlen(download_dir); i >= 0; i--)
|
|
|
|
if (dir_sep(download_dir[i]))
|
|
|
|
break;
|
|
|
|
download_dir[i + 1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cdf_hop->real_file)
|
|
|
|
*cdf_hop->real_file = file;
|
|
|
|
else
|
|
|
|
mem_free(file);
|
|
|
|
|
|
|
|
finish:
|
2007-11-07 06:57:13 -05:00
|
|
|
cdf_hop->callback(term, h, cdf_hop->data, flags);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(cdf_hop);
|
|
|
|
}
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** Create a file to which data can be downloaded.
|
|
|
|
* This function constructs a struct cdf_hop that will be freed
|
|
|
|
* when @a callback returns.
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* If any dialog boxes are needed, show them in this terminal.
|
|
|
|
*
|
|
|
|
* @param fi
|
|
|
|
* A proposed name for the local file to which the data would be
|
|
|
|
* downloaded. "~" here refers to the home directory.
|
|
|
|
* create_download_file() treats this original string as read-only.
|
|
|
|
*
|
|
|
|
* @param real_file
|
|
|
|
* If non-NULL, prepare to save in *@a real_file the name of the local
|
2009-07-17 16:11:47 -04:00
|
|
|
* file that was eventually opened. @a callback must then arrange for
|
|
|
|
* this string to be freed with mem_free().
|
2009-07-17 07:10:36 -04:00
|
|
|
*
|
2009-07-24 09:57:33 -04:00
|
|
|
* @param flags
|
2009-07-24 11:09:59 -04:00
|
|
|
* Flags controlling how to download the file.
|
|
|
|
* ::DOWNLOAD_RESUME_ALLOWED adds a "Resume" button to the dialog.
|
|
|
|
* ::DOWNLOAD_RESUME_SELECTED skips the dialog entirely.
|
|
|
|
* ::DOWNLOAD_EXTERNAL causes the file to be created with settings
|
|
|
|
* suitable for a temporary file: give only the user herself access to
|
|
|
|
* the file (even if the umask is looser), and create the file with
|
|
|
|
* @c O_EXCL unless resuming.
|
2009-07-17 07:10:36 -04:00
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* This function will be called when the file has been opened,
|
|
|
|
* or when it is known that the file will not be opened.
|
|
|
|
*
|
|
|
|
* @param data
|
2009-07-18 14:57:40 -04:00
|
|
|
* A pointer to be passed to @a callback.
|
2009-07-17 10:07:12 -04:00
|
|
|
*
|
|
|
|
* @relates cdf_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
create_download_file(struct terminal *term, char *fi,
|
|
|
|
char **real_file,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags,
|
2009-07-17 16:16:38 -04:00
|
|
|
cdf_callback_T *callback, void *data)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-16 15:08:50 -05:00
|
|
|
struct cdf_hop *cdf_hop = (struct cdf_hop *)mem_calloc(1, sizeof(*cdf_hop));
|
2021-01-02 10:20:27 -05:00
|
|
|
char *wd;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!cdf_hop) {
|
2009-07-24 09:57:33 -04:00
|
|
|
callback(term, -1, data, flags & ~DOWNLOAD_RESUME_SELECTED);
|
2005-09-15 09:58:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdf_hop->real_file = real_file;
|
|
|
|
cdf_hop->callback = callback;
|
|
|
|
cdf_hop->data = data;
|
|
|
|
|
|
|
|
/* FIXME: The wd bussiness is probably useless here? --pasky */
|
|
|
|
wd = get_cwd();
|
|
|
|
set_cwd(term->cwd);
|
|
|
|
|
|
|
|
/* Also the tilde will be expanded here. */
|
2007-11-07 06:57:13 -05:00
|
|
|
lookup_unique_name(term, fi, flags, create_download_file_do, cdf_hop);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (wd) {
|
|
|
|
set_cwd(wd);
|
|
|
|
mem_free(wd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
2005-09-15 09:58:31 -04:00
|
|
|
get_temp_name(struct uri *uri)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *extension;
|
2021-03-31 13:24:26 -04:00
|
|
|
char *nm;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
extension = get_extension_from_uri(uri);
|
2021-03-31 13:24:26 -04:00
|
|
|
if (!extension)
|
|
|
|
extension = stracpy("");
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2021-03-31 13:24:26 -04:00
|
|
|
nm = tempname(NULL, ELINKS_TEMPNAME_PREFIX, extension);
|
|
|
|
mem_free(extension);
|
|
|
|
|
|
|
|
return nm;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
|
|
|
subst_file(char *prog, char *file, char *uri)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string name;
|
2007-02-21 07:43:16 -05:00
|
|
|
/* When there is no %s in the mailcap entry, the handler program reads
|
|
|
|
* data from stdin instead of a file. */
|
|
|
|
int input = 1;
|
2022-02-21 12:41:01 -05:00
|
|
|
char *replace;
|
2022-03-04 14:59:46 -05:00
|
|
|
char *original = (char *)("% ");
|
2020-09-26 14:12:44 -04:00
|
|
|
int truncate;
|
2020-09-27 13:31:14 -04:00
|
|
|
int tlen = 40;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!init_string(&name)) return NULL;
|
|
|
|
|
|
|
|
while (*prog) {
|
|
|
|
int p;
|
|
|
|
|
|
|
|
for (p = 0; prog[p] && prog[p] != '%'; p++);
|
|
|
|
|
|
|
|
add_bytes_to_string(&name, prog, p);
|
|
|
|
prog += p;
|
|
|
|
|
|
|
|
if (*prog == '%') {
|
2020-07-25 07:28:56 -04:00
|
|
|
prog++;
|
2020-09-26 14:12:44 -04:00
|
|
|
truncate = 0;
|
2020-07-25 07:28:56 -04:00
|
|
|
if (*prog == 'f' || *prog == ' ' || *prog == '\0')
|
|
|
|
replace = file;
|
2020-09-26 14:12:44 -04:00
|
|
|
else if (*prog == 'u') {
|
2020-07-25 07:28:56 -04:00
|
|
|
replace = uri;
|
2020-09-26 18:40:07 -04:00
|
|
|
if (!memcmp(uri, "data:", sizeof("data:") - 1))
|
2020-09-26 14:12:44 -04:00
|
|
|
truncate = 1;
|
|
|
|
}
|
2020-07-25 07:28:56 -04:00
|
|
|
else if (*prog == '%')
|
2022-03-04 14:59:46 -05:00
|
|
|
replace = (char *)("%");
|
2020-07-25 07:28:56 -04:00
|
|
|
else {
|
|
|
|
original[1] = *prog;
|
|
|
|
replace = original;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*prog == ' ' || *prog == '\0')
|
|
|
|
prog--;
|
|
|
|
|
2007-02-21 07:43:16 -05:00
|
|
|
input = 0;
|
2005-09-15 09:58:31 -04:00
|
|
|
#if defined(HAVE_CYGWIN_CONV_TO_FULL_WIN32_PATH)
|
|
|
|
#ifdef MAX_PATH
|
2021-01-02 10:20:27 -05:00
|
|
|
char new_path[MAX_PATH];
|
2005-09-15 09:58:31 -04:00
|
|
|
#else
|
2021-01-02 10:20:27 -05:00
|
|
|
char new_path[1024];
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
|
|
|
|
2020-07-25 07:28:56 -04:00
|
|
|
cygwin_conv_to_full_win32_path(replace, new_path);
|
2005-09-15 09:58:31 -04:00
|
|
|
add_to_string(&name, new_path);
|
|
|
|
#else
|
2020-09-27 13:31:14 -04:00
|
|
|
if (! truncate || strlen(replace) <= tlen)
|
|
|
|
add_shell_quoted_to_string(&name,
|
|
|
|
replace, strlen(replace));
|
2020-09-26 14:12:44 -04:00
|
|
|
else {
|
2020-09-27 13:31:14 -04:00
|
|
|
add_shell_quoted_to_string(&name,
|
|
|
|
replace, tlen);
|
2020-09-26 18:40:07 -04:00
|
|
|
add_shell_quoted_to_string(&name,
|
|
|
|
"...", sizeof("...") - 1);
|
2020-09-26 14:12:44 -04:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
|
|
|
prog++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-21 07:43:16 -05:00
|
|
|
if (input) {
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string s;
|
2007-02-21 07:43:16 -05:00
|
|
|
|
|
|
|
if (init_string(&s)) {
|
|
|
|
add_to_string(&s, "/bin/cat ");
|
2007-02-26 13:51:22 -05:00
|
|
|
add_shell_quoted_to_string(&s, file, strlen(file));
|
|
|
|
add_to_string(&s, " | ");
|
2007-02-21 07:43:16 -05:00
|
|
|
add_string_to_string(&s, &name);
|
|
|
|
done_string(&name);
|
|
|
|
return s.source;
|
|
|
|
}
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
return name.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-17 16:16:38 -04:00
|
|
|
/*! common_download() passes this function as a ::cdf_callback_T to
|
|
|
|
* create_download_file().
|
|
|
|
*
|
|
|
|
* @relates cmdw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2009-07-14 03:27:09 -04:00
|
|
|
common_download_do(struct terminal *term, int fd, void *data,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct file_download *file_download;
|
2022-01-26 11:51:52 -05:00
|
|
|
struct cmdw_hop *cmdw_hop = (struct cmdw_hop *)data;
|
2009-07-18 20:24:55 -04:00
|
|
|
struct uri *download_uri = cmdw_hop->download_uri;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file = cmdw_hop->real_file;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = cmdw_hop->ses;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
mem_free(cmdw_hop);
|
|
|
|
|
2009-07-18 20:18:11 -04:00
|
|
|
if (!file || fstat(fd, &buf)) goto finish;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-18 20:24:55 -04:00
|
|
|
file_download = init_file_download(download_uri, ses, file, fd);
|
2009-07-18 20:18:11 -04:00
|
|
|
if (!file_download) goto finish;
|
2009-07-18 20:57:10 -04:00
|
|
|
/* If init_file_download succeeds, it takes ownership of file
|
|
|
|
* and fd. */
|
|
|
|
file = NULL;
|
|
|
|
fd = -1;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-24 09:57:33 -04:00
|
|
|
if (flags & DOWNLOAD_RESUME_SELECTED)
|
2009-07-14 03:27:09 -04:00
|
|
|
file_download->seek = buf.st_size;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
display_download(ses->tab->term, file_download, ses);
|
|
|
|
|
|
|
|
load_uri(file_download->uri, ses->referrer, &file_download->download,
|
|
|
|
PRI_DOWNLOAD, CACHE_MODE_NORMAL, file_download->seek);
|
2009-07-18 20:18:11 -04:00
|
|
|
|
|
|
|
finish:
|
|
|
|
mem_free_if(file);
|
2009-07-18 20:57:10 -04:00
|
|
|
if (fd != -1) close(fd);
|
2009-07-18 20:24:55 -04:00
|
|
|
done_uri(download_uri);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2009-07-18 19:08:50 -04:00
|
|
|
/** Begin or resume downloading from session.download_uri to the
|
|
|
|
* @a file specified by the user.
|
|
|
|
*
|
|
|
|
* This function contains the code shared between start_download() and
|
|
|
|
* resume_download().
|
|
|
|
*
|
|
|
|
* @relates cmdw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
common_download(struct session *ses, char *file,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct cmdw_hop *cmdw_hop;
|
|
|
|
|
|
|
|
if (!ses->download_uri) return;
|
|
|
|
|
2022-01-16 15:08:50 -05:00
|
|
|
cmdw_hop = (struct cmdw_hop *)mem_calloc(1, sizeof(*cmdw_hop));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!cmdw_hop) return;
|
|
|
|
cmdw_hop->ses = ses;
|
2009-07-18 20:24:55 -04:00
|
|
|
cmdw_hop->download_uri = ses->download_uri;
|
|
|
|
ses->download_uri = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
kill_downloads_to_file(file);
|
|
|
|
|
2009-07-24 11:09:59 -04:00
|
|
|
create_download_file(ses->tab->term, file, &cmdw_hop->real_file,
|
2009-07-24 09:57:33 -04:00
|
|
|
flags, common_download_do, cmdw_hop);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2009-07-18 19:08:50 -04:00
|
|
|
/** Begin downloading from session.download_uri to the @a file
|
|
|
|
* specified by the user.
|
|
|
|
*
|
|
|
|
* The ::ACT_MAIN_SAVE_AS, ::ACT_MAIN_SAVE_URL_AS,
|
|
|
|
* ::ACT_MAIN_LINK_DOWNLOAD, and ::ACT_MAIN_LINK_DOWNLOAD_IMAGE
|
|
|
|
* actions pass this function as the @c std callback to query_file().
|
|
|
|
*
|
|
|
|
* @relates cmdw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
start_download(void *ses, char *file)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
common_download((struct session *)ses, file,
|
2009-07-14 03:27:09 -04:00
|
|
|
DOWNLOAD_RESUME_ALLOWED);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2009-07-17 10:07:12 -04:00
|
|
|
|
2009-07-18 19:08:50 -04:00
|
|
|
/** Resume downloading from session.download_uri to the @a file
|
|
|
|
* specified by the user.
|
|
|
|
*
|
|
|
|
* The ::ACT_MAIN_LINK_DOWNLOAD_RESUME action passes this function as
|
|
|
|
* the @c std callback to query_file().
|
|
|
|
*
|
|
|
|
* @relates cmdw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
resume_download(void *ses, char *file)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
common_download((struct session *)ses, file,
|
2009-07-14 03:27:09 -04:00
|
|
|
DOWNLOAD_RESUME_ALLOWED | DOWNLOAD_RESUME_SELECTED);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2009-07-18 14:57:40 -04:00
|
|
|
/** Resume downloading a file, based on information in struct
|
|
|
|
* codw_hop. This function actually starts a new download from the
|
|
|
|
* current end of the file, even though a download from the beginning
|
|
|
|
* is already in progress at codw_hop->type_query->download. The
|
|
|
|
* caller will cancel the preexisting download after this function
|
|
|
|
* returns.
|
|
|
|
*
|
|
|
|
* @relates codw_hop */
|
|
|
|
static void
|
|
|
|
transform_codw_to_cmdw(struct terminal *term, int fd,
|
|
|
|
struct codw_hop *codw_hop,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags)
|
2009-07-18 14:57:40 -04:00
|
|
|
{
|
|
|
|
struct type_query *type_query = codw_hop->type_query;
|
2022-01-16 15:08:50 -05:00
|
|
|
struct cmdw_hop *cmdw_hop = (struct cmdw_hop *)mem_calloc(1, sizeof(*cmdw_hop));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-19 06:41:44 -04:00
|
|
|
if (!cmdw_hop) {
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2009-07-18 14:57:40 -04:00
|
|
|
|
|
|
|
cmdw_hop->ses = type_query->ses;
|
2009-07-18 20:24:55 -04:00
|
|
|
cmdw_hop->download_uri = get_uri_reference(type_query->uri);
|
2009-07-18 14:57:40 -04:00
|
|
|
cmdw_hop->real_file = codw_hop->real_file;
|
|
|
|
codw_hop->real_file = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-24 09:57:33 -04:00
|
|
|
common_download_do(term, fd, cmdw_hop, flags);
|
2009-07-18 14:57:40 -04:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2009-07-17 16:16:38 -04:00
|
|
|
/*! continue_download() passes this function as a ::cdf_callback_T to
|
|
|
|
* create_download_file().
|
|
|
|
*
|
|
|
|
* @relates codw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2009-07-14 03:27:09 -04:00
|
|
|
continue_download_do(struct terminal *term, int fd, void *data,
|
2022-01-28 11:26:15 -05:00
|
|
|
download_flags_T flags)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct codw_hop *codw_hop = (struct codw_hop *)data;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct file_download *file_download = NULL;
|
|
|
|
struct type_query *type_query;
|
|
|
|
|
|
|
|
assert(codw_hop);
|
|
|
|
assert(codw_hop->type_query);
|
|
|
|
assert(codw_hop->type_query->uri);
|
|
|
|
assert(codw_hop->type_query->ses);
|
|
|
|
|
|
|
|
type_query = codw_hop->type_query;
|
|
|
|
if (!codw_hop->real_file) goto cancel;
|
|
|
|
|
2009-07-24 09:57:33 -04:00
|
|
|
if (flags & DOWNLOAD_RESUME_SELECTED) {
|
|
|
|
transform_codw_to_cmdw(term, fd, codw_hop, flags);
|
2009-07-19 06:41:44 -04:00
|
|
|
fd = -1; /* ownership transfer */
|
2009-07-18 14:57:40 -04:00
|
|
|
goto cancel;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
file_download = init_file_download(type_query->uri, type_query->ses,
|
|
|
|
codw_hop->real_file, fd);
|
|
|
|
if (!file_download) goto cancel;
|
2009-07-18 20:57:10 -04:00
|
|
|
/* If init_file_download succeeds, it takes ownership of
|
|
|
|
* codw_hop->real_file and fd. */
|
|
|
|
codw_hop->real_file = NULL;
|
|
|
|
fd = -1;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-05-21 12:17:04 -04:00
|
|
|
if (type_query->dgi && type_query->external_handler) {
|
|
|
|
file_download->external_handler = type_query->external_handler;
|
|
|
|
file_download->file = codw_hop->file;
|
|
|
|
file_download->inpext = null_or_stracpy(type_query->inpext);
|
|
|
|
file_download->outext = null_or_stracpy(type_query->outext);
|
|
|
|
file_download->dgi = type_query->dgi;
|
|
|
|
file_download->delete_ = 1;
|
|
|
|
/* change owners a few lines above */
|
|
|
|
codw_hop->file = NULL;
|
|
|
|
type_query->external_handler = NULL;
|
|
|
|
} else if (type_query->external_handler) {
|
2005-09-15 09:58:31 -04:00
|
|
|
file_download->external_handler = subst_file(type_query->external_handler,
|
2020-07-25 07:28:56 -04:00
|
|
|
codw_hop->file,
|
|
|
|
type_query->uri->string);
|
2016-04-20 12:57:32 -04:00
|
|
|
file_download->delete_ = 1;
|
2010-07-24 11:07:18 -04:00
|
|
|
file_download->copiousoutput = type_query->copiousoutput;
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(codw_hop->file);
|
|
|
|
mem_free_set(&type_query->external_handler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_download->block = !!type_query->block;
|
|
|
|
|
|
|
|
/* Done here and not in init_file_download() so that the external
|
|
|
|
* handler can become initialized. */
|
|
|
|
display_download(term, file_download, type_query->ses);
|
|
|
|
|
2006-02-03 04:44:28 -05:00
|
|
|
move_download(&type_query->download, &file_download->download, PRI_DOWNLOAD);
|
2005-09-15 09:58:31 -04:00
|
|
|
done_type_query(type_query);
|
|
|
|
|
|
|
|
mem_free(codw_hop);
|
|
|
|
return;
|
|
|
|
|
|
|
|
cancel:
|
2009-07-18 20:18:11 -04:00
|
|
|
mem_free_if(codw_hop->real_file);
|
2009-07-18 20:57:10 -04:00
|
|
|
if (fd != -1) close(fd);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (type_query->external_handler) mem_free_if(codw_hop->file);
|
|
|
|
tp_cancel(type_query);
|
|
|
|
mem_free(codw_hop);
|
|
|
|
}
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** When asked what to do with a file, the user chose to download it
|
|
|
|
* to a local file named @a file.
|
|
|
|
* Or an external handler was selected, in which case
|
|
|
|
* type_query.external_handler is non-NULL and @a file does not
|
2009-07-17 10:07:12 -04:00
|
|
|
* matter because this function will generate a name.
|
|
|
|
*
|
2009-07-17 19:00:44 -04:00
|
|
|
* tp_save() passes this function as the @c std callback to query_file().
|
|
|
|
*
|
2009-07-17 10:07:12 -04:00
|
|
|
* @relates codw_hop */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
continue_download(void *data, char *file)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct type_query *type_query = (struct type_query *)data;
|
2022-01-16 15:08:50 -05:00
|
|
|
struct codw_hop *codw_hop = (struct codw_hop *)mem_calloc(1, sizeof(*codw_hop));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!codw_hop) {
|
|
|
|
tp_cancel(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type_query->external_handler) {
|
|
|
|
file = get_temp_name(type_query->uri);
|
|
|
|
if (!file) {
|
|
|
|
mem_free(codw_hop);
|
|
|
|
tp_cancel(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codw_hop->type_query = type_query;
|
|
|
|
codw_hop->file = file;
|
|
|
|
|
|
|
|
kill_downloads_to_file(file);
|
|
|
|
|
|
|
|
create_download_file(type_query->ses->tab->term, file,
|
|
|
|
&codw_hop->real_file,
|
2007-11-07 07:15:37 -05:00
|
|
|
type_query->external_handler
|
2021-03-31 13:24:26 -04:00
|
|
|
? DOWNLOAD_RESUME_ALLOWED |
|
|
|
|
DOWNLOAD_EXTERNAL |
|
|
|
|
DOWNLOAD_OVERWRITE
|
2009-07-24 11:09:59 -04:00
|
|
|
: DOWNLOAD_RESUME_ALLOWED,
|
2005-09-15 09:58:31 -04:00
|
|
|
continue_download_do, codw_hop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-24 11:32:25 -04:00
|
|
|
/*! @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
static struct type_query *
|
2008-05-11 11:42:21 -04:00
|
|
|
find_type_query(struct session *ses)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct type_query *type_query;
|
|
|
|
|
|
|
|
foreach (type_query, ses->type_queries)
|
|
|
|
if (compare_uri(type_query->uri, ses->loading_uri, 0))
|
2008-05-11 11:42:21 -04:00
|
|
|
return type_query;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** Prepare to ask the user what to do with a file, but don't display
|
|
|
|
* the window yet. To display it, do_type_query() must be called
|
|
|
|
* separately. setup_download_handler() takes care of that.
|
|
|
|
*
|
|
|
|
* @relates type_query */
|
2008-05-11 11:42:21 -04:00
|
|
|
static struct type_query *
|
|
|
|
init_type_query(struct session *ses, struct download *download,
|
|
|
|
struct cache_entry *cached)
|
|
|
|
{
|
|
|
|
struct type_query *type_query;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-01-16 15:08:50 -05:00
|
|
|
type_query = (struct type_query *)mem_calloc(1, sizeof(*type_query));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!type_query) return NULL;
|
|
|
|
|
|
|
|
type_query->uri = get_uri_reference(ses->loading_uri);
|
|
|
|
type_query->ses = ses;
|
|
|
|
type_query->target_frame = null_or_stracpy(ses->task.target.frame);
|
|
|
|
|
|
|
|
type_query->cached = cached;
|
2008-03-13 17:53:31 -04:00
|
|
|
type_query->cgi = cached->cgi;
|
2005-09-15 09:58:31 -04:00
|
|
|
object_lock(type_query->cached);
|
|
|
|
|
2006-02-03 04:44:28 -05:00
|
|
|
move_download(download, &type_query->download, PRI_MAIN);
|
2008-08-03 08:24:26 -04:00
|
|
|
download->state = connection_state(S_OK);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
add_to_list(ses->type_queries, type_query);
|
|
|
|
|
|
|
|
return type_query;
|
|
|
|
}
|
|
|
|
|
2009-07-18 11:09:36 -04:00
|
|
|
/** Cancel any download started for @a type_query, remove the structure
|
2009-07-17 19:00:44 -04:00
|
|
|
* from the session.type_queries list, and free it.
|
|
|
|
*
|
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
done_type_query(struct type_query *type_query)
|
|
|
|
{
|
|
|
|
/* Unregister any active download */
|
2006-02-12 10:41:21 -05:00
|
|
|
cancel_download(&type_query->download, 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
object_unlock(type_query->cached);
|
|
|
|
done_uri(type_query->uri);
|
2022-05-21 12:17:04 -04:00
|
|
|
mem_free_if(type_query->inpext);
|
|
|
|
mem_free_if(type_query->outext);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(type_query->external_handler);
|
|
|
|
mem_free_if(type_query->target_frame);
|
|
|
|
del_from_list(type_query);
|
|
|
|
mem_free(type_query);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The user chose "Cancel" when asked what to do with a file,
|
|
|
|
* or the type query was cancelled for some other reason.
|
|
|
|
*
|
|
|
|
* do_type_query() and bittorrent_query_callback() pass this function
|
|
|
|
* as a ::done_handler_T to add_dlg_ok_button(), and tp_save() passes
|
|
|
|
* this function as a @c cancel callback to query_file().
|
|
|
|
*
|
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
tp_cancel(void *data)
|
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct type_query *type_query = (struct type_query *)data;
|
2006-02-03 04:44:28 -05:00
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* XXX: Should we really abort? (1 vs 0 as the last param) --pasky */
|
2006-02-03 04:44:28 -05:00
|
|
|
cancel_download(&type_query->download, 1);
|
2005-09-15 09:58:31 -04:00
|
|
|
done_type_query(type_query);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The user chose "Save" when asked what to do with a file.
|
2009-07-17 10:07:12 -04:00
|
|
|
* Now ask her where to save the file.
|
|
|
|
*
|
2009-07-17 19:00:44 -04:00
|
|
|
* do_type_query() and bittorrent_query_callback() pass this function
|
|
|
|
* as a ::done_handler_T to add_dlg_ok_button().
|
|
|
|
*
|
2009-07-17 10:07:12 -04:00
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
tp_save(struct type_query *type_query)
|
|
|
|
{
|
|
|
|
mem_free_set(&type_query->external_handler, NULL);
|
|
|
|
query_file(type_query->ses, type_query->uri, type_query, continue_download, tp_cancel, 1);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The user chose "Show header" when asked what to do with a file.
|
|
|
|
*
|
|
|
|
* do_type_query() passes this function as a ::widget_handler_T to
|
|
|
|
* add_dlg_button(). Unlike with add_dlg_ok_button(), pressing this
|
|
|
|
* button does not close the dialog box. This way, the user can
|
|
|
|
* first examine the header and then choose what to do.
|
2009-07-17 10:07:12 -04:00
|
|
|
*
|
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
static widget_handler_status_T
|
|
|
|
tp_show_header(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
2022-01-26 11:51:52 -05:00
|
|
|
struct type_query *type_query = (struct type_query *)widget_data->widget->data;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
cached_header_dialog(type_query->ses, type_query->cached);
|
|
|
|
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/** The user chose "Display" when asked what to do with a file,
|
|
|
|
* or she chose "Open" and there is no external handler.
|
|
|
|
*
|
|
|
|
* do_type_query() and bittorrent_query_callback() pass this function
|
|
|
|
* as a ::done_handler_T to add_dlg_ok_button().
|
|
|
|
*
|
|
|
|
* @bug FIXME: We need to modify this function to take frame data
|
2007-07-26 20:07:39 -04:00
|
|
|
* instead, as we want to use this function for frames as well (now,
|
|
|
|
* when frame has content type text/plain, it is ignored and displayed
|
2009-07-17 10:07:12 -04:00
|
|
|
* as HTML).
|
|
|
|
*
|
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
tp_display(struct type_query *type_query)
|
|
|
|
{
|
|
|
|
struct view_state *vs;
|
|
|
|
struct session *ses = type_query->ses;
|
|
|
|
struct uri *loading_uri = ses->loading_uri;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *target_frame = null_or_stracpy(ses->task.target.frame);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
ses->loading_uri = type_query->uri;
|
2009-01-01 16:06:59 -05:00
|
|
|
mem_free_set(&ses->task.target.frame, null_or_stracpy(type_query->target_frame));
|
2005-09-15 09:58:31 -04:00
|
|
|
vs = ses_forward(ses, /* type_query->frame */ 0);
|
|
|
|
if (vs) vs->plain = 1;
|
|
|
|
ses->loading_uri = loading_uri;
|
2009-01-01 16:06:59 -05:00
|
|
|
mem_free_set(&ses->task.target.frame, target_frame);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (/* !type_query->frame */ 1) {
|
|
|
|
struct download *old = &type_query->download;
|
2016-04-20 12:42:22 -04:00
|
|
|
struct download *new_ = &cur_loc(ses)->download;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2016-04-20 12:42:22 -04:00
|
|
|
new_->callback = (download_callback_T *) doc_loading_callback;
|
|
|
|
new_->data = ses;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2016-04-20 12:42:22 -04:00
|
|
|
move_download(old, new_, PRI_MAIN);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
display_timer(ses);
|
|
|
|
done_type_query(type_query);
|
|
|
|
}
|
|
|
|
|
2009-07-17 07:10:36 -04:00
|
|
|
/** The user chose "Open" when asked what to do with a file.
|
|
|
|
* Or an external handler was found and it has been configured
|
2009-07-17 10:07:12 -04:00
|
|
|
* to run without asking.
|
|
|
|
*
|
2009-07-17 19:00:44 -04:00
|
|
|
* do_type_query() passes this function as a ::done_handler_T to
|
|
|
|
* add_dlg_ok_button().
|
|
|
|
*
|
2009-07-17 10:07:12 -04:00
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
|
|
|
tp_open(struct type_query *type_query)
|
|
|
|
{
|
|
|
|
if (!type_query->external_handler || !*type_query->external_handler) {
|
|
|
|
tp_display(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-09 07:50:09 -04:00
|
|
|
if (type_query->uri->protocol == PROTOCOL_FILE && !type_query->cgi) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file = get_uri_string(type_query->uri, URI_PATH);
|
|
|
|
char *handler = NULL;
|
2008-03-09 07:50:09 -04:00
|
|
|
|
2022-05-21 12:17:04 -04:00
|
|
|
if (type_query->dgi) {
|
|
|
|
if (file) {
|
|
|
|
decode_uri(file);
|
|
|
|
}
|
|
|
|
exec_later_dgi(type_query->ses, type_query->external_handler, file,
|
|
|
|
type_query->inpext, type_query->outext, 0);
|
|
|
|
mem_free_if(file);
|
|
|
|
done_type_query(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-09 07:50:09 -04:00
|
|
|
if (file) {
|
|
|
|
decode_uri(file);
|
2020-07-25 07:28:56 -04:00
|
|
|
handler = subst_file(type_query->external_handler,
|
|
|
|
file, file);
|
2008-03-09 07:50:09 -04:00
|
|
|
mem_free(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler) {
|
2010-07-24 11:07:18 -04:00
|
|
|
if (type_query->copiousoutput) {
|
|
|
|
exec_later(type_query->ses, handler, NULL);
|
|
|
|
} else {
|
|
|
|
exec_on_terminal(type_query->ses->tab->term,
|
|
|
|
handler, "", type_query->block ?
|
|
|
|
TERM_EXEC_FG : TERM_EXEC_BG);
|
|
|
|
}
|
2008-03-09 07:50:09 -04:00
|
|
|
mem_free(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_type_query(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:51:10 -05:00
|
|
|
continue_download(type_query, (char *)(""));
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/*! Ask the user what to do with a file.
|
|
|
|
*
|
|
|
|
* This function does not support BitTorrent downloads.
|
|
|
|
* For those, query_bittorrent_dialog() must be called instead.
|
|
|
|
* setup_download_handler() takes care of this.
|
|
|
|
*
|
|
|
|
* @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
do_type_query(struct type_query *type_query, char *ct, struct mime_handler *handler)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
Here is a framework that detects cases where a PO file assigns
the same accelerator key to multiple buttons in a dialog box or
to multiple items in a menu. ELinks already has some support for
this but it requires the translator to run ELinks and manually
scan through all menus and dialogs. The attached changes make it
possible to quickly detect and list any conflicts, including ones
that can only occur on operating systems or configurations that
the translator is not currently using.
The changes have no immediate effect on the elinks executable or
the MO files. PO files become larger, however.
The scheme works like this:
- Like before, accelerator keys in translatable strings are
tagged with the tilde (~) character.
- Whenever a C source file defines an accelerator key, it must
assign one or more named "contexts" to it. The translations in
the PO files inherit these contexts. If multiple strings use
the same accelerator (case insensitive) in the same context,
that's a conflict and can be detected automatically.
- The contexts are defined with "gettext_accelerator_context"
comments in source files. These comments delimit regions where
all translatable strings containing tildes are given the same
contexts. There must be one special comment at the top of the
region; it lists the contexts assigned to that region. The
region automatically ends at the end of the function (found
with regexp /^\}/), but it can also be closed explicitly with
another special comment. The comments are formatted like this:
/* [gettext_accelerator_context(foo, bar, baz)]
begins a region that uses the contexts "foo", "bar", and "baz".
The comma is the delimiter; whitespace is optional.
[gettext_accelerator_context()]
ends the region. */
The scripts don't currently check whether this syntax occurs
inside or outside comments.
- The names of contexts consist of C identifiers delimited with
periods. I typically used the name of a function that sets
up a dialog, or the name of an array where the items of a
menu are listed. There is a special feature for static
functions: if the name begins with a period, then the period
will be replaced with the name of the source file and a colon.
- If a menu is programmatically generated from multiple parts,
of which some are never used together, so that it is safe to
use the same accelerators in them, then it is necessary to
define multiple contexts for the same menu. link_menu() in
src/viewer/text/link.c is the most complex example of this.
- During make update-po:
- A Perl script (po/gather-accelerator-contexts.pl) reads
po/elinks.pot, scans the source files listed in it for
"gettext_accelerator_context" comments, and rewrites
po/elinks.pot with "accelerator_context" comments that
indicate the contexts of each msgid: the union of all
contexts of all of its uses in the source files. It also
removes any "gettext_accelerator_context" comments that
xgettext --add-comments has copied to elinks.pot.
- If po/gather-accelerator-contexts.pl does not find any
contexts for some use of an msgid that seems to contain an
accelerator (because it contains a tilde), it warns. If the
tilde refers to e.g. "~/.elinks" and does not actually mark
an accelerator, the warning can be silenced by specifying the
special context "IGNORE", which the script otherwise ignores.
- msgmerge copies the "accelerator_context" comments from
po/elinks.pot to po/*.po. Translators do not edit those
comments.
- During make check-po:
- Another Perl script (po/check-accelerator-contexts.pl) reads
po/*.po and keeps track of which accelerators have been bound
in each context. It warns about any conflicts it finds.
This script does not access the C source files; thus it does
not matter if the line numbers in "#:" lines are out of date.
This implementation is not perfect and I am not proposing to
add it to the main source tree at this time. Specifically:
- It introduces compile-time dependencies on Perl and Locale::PO.
There should be a configure-time or compile-time check so that
the new features are skipped if the prerequisites are missing.
- When the scripts include msgstr strings in warnings, they
should transcode them from the charset of the PO file to the
one specified by the user's locale.
- It is not adequately documented (well, except perhaps here).
- po/check-accelerator-contexts.pl reports the same conflict
multiple times if it occurs in multiple contexts.
- The warning messages should include line numbers, so that users
of Emacs could conveniently edit the conflicting part of the PO
file. This is not feasible with the current version of
Locale::PO.
- Locale::PO does not understand #~ lines and spews warnings
about them. There is an ugly hack to hide these warnings.
- Jonas Fonseca suggested the script could propose accelerators
that are still available. This has not been implemented.
There are three files attached:
- po/gather-accelerator-contexts.pl: Augments elinks.pot with
context information.
- po/check-accelerator-contexts.pl: Checks conflicts.
- accelerator-contexts.diff: Makes po/Makefile run the scripts,
and adds special comments to source files.
2005-12-04 18:38:29 -05:00
|
|
|
/* [gettext_accelerator_context(.do_type_query)] */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string filename;
|
2022-02-21 12:54:55 -05:00
|
|
|
const char *description;
|
|
|
|
const char *desc_sep;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *format, *text, *title;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct dialog *dlg;
|
|
|
|
#define TYPE_QUERY_WIDGETS_COUNT 8
|
|
|
|
int widgets = TYPE_QUERY_WIDGETS_COUNT;
|
|
|
|
struct terminal *term = type_query->ses->tab->term;
|
|
|
|
struct memory_list *ml;
|
|
|
|
struct dialog_data *dlg_data;
|
|
|
|
int selected_widget;
|
|
|
|
|
|
|
|
mem_free_set(&type_query->external_handler, NULL);
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
type_query->block = handler->block;
|
2010-07-24 11:07:18 -04:00
|
|
|
type_query->copiousoutput = handler->copiousoutput;
|
2022-05-21 12:17:04 -04:00
|
|
|
type_query->dgi = handler->dgi;
|
|
|
|
type_query->inpext = null_or_stracpy(handler->inpext);
|
|
|
|
type_query->outext = null_or_stracpy(handler->outext);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!handler->ask) {
|
|
|
|
type_query->external_handler = stracpy(handler->program);
|
|
|
|
tp_open(type_query);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start preparing for the type query dialog. */
|
|
|
|
description = handler->description;
|
|
|
|
desc_sep = *description ? "; " : "";
|
|
|
|
title = N_("What to do?");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
title = N_("Unknown type");
|
|
|
|
description = "";
|
|
|
|
desc_sep = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
dlg = calloc_dialog(TYPE_QUERY_WIDGETS_COUNT, MAX_STR_LEN * 2);
|
|
|
|
if (!dlg) return;
|
|
|
|
|
|
|
|
if (init_string(&filename)) {
|
|
|
|
add_mime_filename_to_string(&filename, type_query->uri);
|
|
|
|
|
|
|
|
/* Let's make the filename pretty for display & save */
|
|
|
|
/* TODO: The filename can be the empty string here. See bug 396. */
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp)
|
2006-03-12 19:54:34 -05:00
|
|
|
decode_uri_string(&filename);
|
|
|
|
else
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-03-12 19:54:34 -05:00
|
|
|
decode_uri_string_for_display(&filename);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
text = get_dialog_offset(dlg, TYPE_QUERY_WIDGETS_COUNT);
|
2006-11-05 22:55:26 -05:00
|
|
|
/* For "default directory index pages" with wrong content-type
|
|
|
|
* the filename can be NULL, e.g. http://www.spamhaus.org in bug 396. */
|
|
|
|
if (filename.length) {
|
|
|
|
format = _("What would you like to do with the file '%s' (type: %s%s%s)?", term);
|
|
|
|
snprintf(text, MAX_STR_LEN, format, filename.source, ct, desc_sep, description);
|
|
|
|
} else {
|
|
|
|
format = _("What would you like to do with the file (type: %s%s%s)?", term);
|
|
|
|
snprintf(text, MAX_STR_LEN, format, ct, desc_sep, description);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
done_string(&filename);
|
|
|
|
|
|
|
|
dlg->title = _(title, term);
|
|
|
|
dlg->layouter = generic_dialog_layouter;
|
|
|
|
dlg->layout.padding_top = 1;
|
|
|
|
dlg->layout.fit_datalen = 1;
|
|
|
|
dlg->udata2 = type_query;
|
|
|
|
|
|
|
|
add_dlg_text(dlg, text, ALIGN_LEFT, 0);
|
|
|
|
|
|
|
|
/* Add input field or text widget with info about the program handler. */
|
|
|
|
if (!get_cmd_opt_bool("anonymous")) {
|
2022-01-16 15:08:50 -05:00
|
|
|
char *field = (char *)mem_calloc(1, MAX_STR_LEN);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!field) {
|
|
|
|
mem_free(dlg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler && handler->program) {
|
2007-07-24 06:47:35 -04:00
|
|
|
safe_strncpy(field, handler->program, MAX_STR_LEN);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* xgettext:no-c-format */
|
2020-07-26 03:27:14 -04:00
|
|
|
add_dlg_field(dlg,
|
|
|
|
_("Program ('%f' will be replaced by the filename, "
|
|
|
|
"'%u' by the uri)", term),
|
2005-09-15 09:58:31 -04:00
|
|
|
0, 0, NULL, MAX_STR_LEN, field, NULL);
|
|
|
|
type_query->external_handler = field;
|
|
|
|
|
2009-07-23 14:25:48 -04:00
|
|
|
add_dlg_checkbox(dlg, _("Block the terminal", term), &type_query->block);
|
2005-09-15 09:58:31 -04:00
|
|
|
selected_widget = 3;
|
|
|
|
|
|
|
|
} else if (handler) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *field = text + MAX_STR_LEN;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
format = _("The file will be opened with the program '%s'.", term);
|
|
|
|
snprintf(field, MAX_STR_LEN, format, handler->program);
|
|
|
|
add_dlg_text(dlg, field, ALIGN_LEFT, 0);
|
|
|
|
|
|
|
|
type_query->external_handler = stracpy(handler->program);
|
|
|
|
if (!type_query->external_handler) {
|
|
|
|
mem_free(dlg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widgets--;
|
|
|
|
selected_widget = 2;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
widgets -= 2;
|
|
|
|
selected_widget = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add buttons if they are both usable and allowed. */
|
|
|
|
|
|
|
|
if (!get_cmd_opt_bool("anonymous") || handler) {
|
|
|
|
add_dlg_ok_button(dlg, _("~Open", term), B_ENTER,
|
|
|
|
(done_handler_T *) tp_open, type_query);
|
|
|
|
} else {
|
|
|
|
widgets--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!get_cmd_opt_bool("anonymous")) {
|
|
|
|
add_dlg_ok_button(dlg, _("Sa~ve", term), B_ENTER,
|
|
|
|
(done_handler_T *) tp_save, type_query);
|
|
|
|
} else {
|
|
|
|
widgets--;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_dlg_ok_button(dlg, _("~Display", term), B_ENTER,
|
|
|
|
(done_handler_T *) tp_display, type_query);
|
|
|
|
|
|
|
|
if (type_query->cached && type_query->cached->head) {
|
|
|
|
add_dlg_button(dlg, _("Show ~header", term), B_ENTER,
|
|
|
|
tp_show_header, type_query);
|
|
|
|
} else {
|
|
|
|
widgets--;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_dlg_ok_button(dlg, _("~Cancel", term), B_ESC,
|
|
|
|
(done_handler_T *) tp_cancel, type_query);
|
|
|
|
|
|
|
|
add_dlg_end(dlg, widgets);
|
|
|
|
|
2007-03-11 06:41:17 -04:00
|
|
|
ml = getml(dlg, (void *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!ml) {
|
|
|
|
/* XXX: Assume that the allocated @external_handler will be
|
|
|
|
* freed when releasing the @type_query. */
|
|
|
|
mem_free(dlg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlg_data = do_dialog(term, dlg, ml);
|
|
|
|
/* Don't focus the text field; we want the user to be able
|
|
|
|
* to select a button by typing the first letter of its label
|
|
|
|
* without having to first leave the text field. */
|
|
|
|
if (dlg_data) {
|
|
|
|
select_widget_by_id(dlg_data, selected_widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
2022-01-31 11:49:13 -05:00
|
|
|
const char *type;
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned int plain:1;
|
2007-02-04 08:32:35 -05:00
|
|
|
} static const known_types[] = {
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "text/html", 0 },
|
2006-01-01 17:22:10 -05:00
|
|
|
{ "text/plain", 1 },
|
2021-07-01 14:18:29 -04:00
|
|
|
{ "text/gemini", 0 },
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "application/xhtml+xml", 0 }, /* RFC 3236 */
|
2021-12-08 11:17:50 -05:00
|
|
|
#ifdef CONFIG_DOM
|
2006-01-01 17:22:10 -05:00
|
|
|
{ "application/docbook+xml", 1 },
|
2006-01-07 21:44:23 -05:00
|
|
|
{ "application/rss+xml", 0 },
|
2005-12-30 16:19:32 -05:00
|
|
|
{ "application/xbel+xml", 1 },
|
|
|
|
{ "application/xbel", 1 },
|
|
|
|
{ "application/x-xbel", 1 },
|
2005-12-19 21:13:08 -05:00
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
{ NULL, 1 },
|
|
|
|
};
|
|
|
|
|
2009-07-17 19:00:44 -04:00
|
|
|
/*! @relates type_query */
|
2005-09-15 09:58:31 -04:00
|
|
|
int
|
|
|
|
setup_download_handler(struct session *ses, struct download *loading,
|
|
|
|
struct cache_entry *cached, int frame)
|
|
|
|
{
|
|
|
|
struct mime_handler *handler;
|
|
|
|
struct view_state *vs;
|
|
|
|
struct type_query *type_query;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *ctype = get_content_type(cached);
|
2005-09-15 09:58:31 -04:00
|
|
|
int plaintext = 1;
|
|
|
|
int ret = 0;
|
|
|
|
int xwin, i;
|
|
|
|
|
|
|
|
if (!ctype || !*ctype)
|
|
|
|
goto plaintext_follow;
|
|
|
|
|
|
|
|
for (i = 0; known_types[i].type; i++) {
|
2008-10-18 21:25:00 -04:00
|
|
|
if (c_strcasecmp(ctype, known_types[i].type))
|
2005-09-15 09:58:31 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
plaintext = known_types[i].plain;
|
|
|
|
goto plaintext_follow;
|
|
|
|
}
|
|
|
|
|
|
|
|
xwin = ses->tab->term->environment & ENV_XWIN;
|
|
|
|
handler = get_mime_type_handler(ctype, xwin);
|
|
|
|
|
2008-10-18 21:25:00 -04:00
|
|
|
if (!handler && strlen(ctype) >= 4 && !c_strncasecmp(ctype, "text", 4))
|
2005-09-15 09:58:31 -04:00
|
|
|
goto plaintext_follow;
|
|
|
|
|
2008-05-11 11:42:21 -04:00
|
|
|
type_query = find_type_query(ses);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (type_query) {
|
|
|
|
ret = 1;
|
2008-05-11 11:42:21 -04:00
|
|
|
} else {
|
|
|
|
type_query = init_type_query(ses, loading, cached);
|
|
|
|
if (type_query) {
|
|
|
|
ret = 1;
|
2005-09-15 09:58:31 -04:00
|
|
|
#ifdef CONFIG_BITTORRENT
|
2008-05-11 11:42:21 -04:00
|
|
|
/* A terrible waste of a good MIME handler here, but we want
|
|
|
|
* to use the type_query this is easier. */
|
2008-11-01 16:39:17 -04:00
|
|
|
if ((!c_strcasecmp(ctype, "application/x-bittorrent")
|
|
|
|
|| !c_strcasecmp(ctype, "application/x-torrent"))
|
2008-05-11 11:42:21 -04:00
|
|
|
&& !get_cmd_opt_bool("anonymous"))
|
|
|
|
query_bittorrent_dialog(type_query);
|
|
|
|
else
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
2008-05-11 11:42:21 -04:00
|
|
|
do_type_query(type_query, ctype, handler);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mem_free_if(handler);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
plaintext_follow:
|
|
|
|
vs = ses_forward(ses, frame);
|
|
|
|
if (vs) vs->plain = plaintext;
|
|
|
|
return 0;
|
|
|
|
}
|