From bdfb2fb35c4d6fa407361dcc99aefb00ff185e6d Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Tue, 12 Apr 2011 22:35:42 -0600 Subject: allow a second argument to the 'download' command that specifies a destination path --- examples/data/scripts/download.sh | 14 +++++++++++--- src/callbacks.c | 8 ++++++++ src/uzbl-core.c | 18 ++++++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/data/scripts/download.sh b/examples/data/scripts/download.sh index fe566ed..dbc9caf 100755 --- a/examples/data/scripts/download.sh +++ b/examples/data/scripts/download.sh @@ -1,9 +1,17 @@ #!/bin/sh # uzbl's example configuration sets this script up as its download_handler. -# when uzbl starts a download it runs this script. +# this script is run when uzbl encounters a URL that it can't display, and when +# a download is requested using the 'download' command. +# # if the script prints a file path to stdout, uzbl will save the download to -# that path. -# if nothing is printed to stdout, the download will be cancelled. +# that path using it's internal downloader. +# +# if nothing is printed to stdout, the internal download will be cancelled. +# you could do your own download handling in your script that way. + +# if $5 is set, it is the path that was passed to uzbl's "download" command. +# we want to use that if it's available. +[ -n "$5" ] && echo "$5" && exit . "$UZBL_UTIL_DIR/uzbl-dir.sh" diff --git a/src/callbacks.c b/src/callbacks.c index 360b0c4..703107b 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -837,6 +837,11 @@ download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_dat /* get the URI being downloaded */ const gchar *uri = webkit_download_get_uri(download); + /* get the destination path, if specified. + * this is only intended to be set when this function is trigger by an + * explicit download using uzbl's 'download' action. */ + const gchar *destination = user_data; + if (uzbl.state.verbose) printf("Download requested -> %s\n", uri); @@ -883,6 +888,9 @@ download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_dat gchar *total_size_s = g_strdup_printf("%d", total_size); g_array_append_val(a, total_size_s); + if(destination) + g_array_append_val(a, destination); + GString *result = g_string_new (""); run_parsed_command(c, a, result); diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 3a96482..204c89c 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -745,15 +745,21 @@ void download(WebKitWebView *web_view, GArray *argv, GString *result) { (void) result; - const gchar *uri = NULL; + const gchar *uri = argv_idx(argv, 0); + const gchar *destination = NULL; + if(argv->len > 1) + destination = argv_idx(argv, 1); - if(argv->len > 0) - uri = argv_idx(argv, 0); + WebKitNetworkRequest *req = webkit_network_request_new(uri); + WebKitDownload *download = webkit_download_new(req); + + download_cb(web_view, download, destination); + + if(webkit_download_get_destination_uri(download)) + webkit_download_start(download); else - uri = uzbl.state.uri; + g_object_unref(download); - WebKitNetworkRequest *req = webkit_network_request_new(uri); - webkit_web_view_request_download(web_view, req); g_object_unref(req); } -- cgit v1.2.3