diff options
author | Daniel Carl <danielcarl@gmx.de> | 2019-05-19 00:29:23 +0200 |
---|---|---|
committer | Daniel Carl <danielcarl@gmx.de> | 2019-05-19 00:41:00 +0200 |
commit | 53a91b6db6cb5d2e052d4e0a3be621778d42ac82 (patch) | |
tree | b75ced0e19635d278aba9565b747295a57842a3f /src | |
parent | 79c69ba194db0e4ddfb4ff985bc51c3b14ac8dd3 (diff) |
Add external download command #543 #348.
Added 'download-command' setting to configure a command/script that
handles the download of an uri. With the new setting flag
'download-use-external' can be decided if the external download command
is used to download an uri or the built in downloader.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 63 | ||||
-rw-r--r-- | src/setting.c | 4 |
2 files changed, 59 insertions, 8 deletions
@@ -63,6 +63,9 @@ static void on_webctx_download_started(WebKitWebContext *webctx, static void on_webctx_init_web_extension(WebKitWebContext *webctx, gpointer data); static gboolean on_webdownload_decide_destination(WebKitDownload *download, gchar *suggested_filename, Client *c); +static void on_webdownload_response_received(WebKitDownload *download, + GParamSpec *ps, Client *c); +static void spawn_download_command(Client *c, WebKitURIResponse *response); static void on_webdownload_failed(WebKitDownload *download, GError *error, Client *c); static void on_webdownload_finished(WebKitDownload *download, Client *c); @@ -1070,15 +1073,19 @@ static void on_webctx_download_started(WebKitWebContext *webctx, autocmd_run(c, AU_DOWNLOAD_STARTED, uri, NULL); #endif - g_signal_connect(download, "decide-destination", G_CALLBACK(on_webdownload_decide_destination), c); - g_signal_connect(download, "failed", G_CALLBACK(on_webdownload_failed), c); - g_signal_connect(download, "finished", G_CALLBACK(on_webdownload_finished), c); - g_signal_connect(download, "received-data", G_CALLBACK(on_webdownload_received_data), c); + if (GET_BOOL(c, "download-use-external")) { + g_signal_connect(download, "notify::response", G_CALLBACK(on_webdownload_response_received), c); + } else { + g_signal_connect(download, "decide-destination", G_CALLBACK(on_webdownload_decide_destination), c); + g_signal_connect(download, "failed", G_CALLBACK(on_webdownload_failed), c); + g_signal_connect(download, "finished", G_CALLBACK(on_webdownload_finished), c); + g_signal_connect(download, "received-data", G_CALLBACK(on_webdownload_received_data), c); - c->state.downloads = g_list_append(c->state.downloads, download); + c->state.downloads = g_list_append(c->state.downloads, download); - /* to reflect the correct download count */ - vb_statusbar_update(c); + /* to reflect the correct download count */ + vb_statusbar_update(c); + } } /** @@ -1120,6 +1127,48 @@ static gboolean on_webdownload_decide_destination(WebKitDownload *download, return vb_download_set_destination(c, download, suggested_filename, NULL); } +static void on_webdownload_response_received(WebKitDownload *download, + GParamSpec *ps, Client *c) +{ + spawn_download_command(c, webkit_download_get_response(download)); + webkit_download_cancel(download); +} + +static void spawn_download_command(Client *c, WebKitURIResponse *response) +{ + char *cmd; + char **argv, **envp; + int argc; + GError *error = NULL; + + cmd = g_strdup_printf(GET_CHAR(c, "download-command"), + webkit_uri_response_get_uri(response)); + + if (!g_shell_parse_argv(cmd, &argc, &argv, &error)) { + g_warning("Could not parse download-command '%s': %s", + cmd, + error->message); + g_error_free(error); + g_free(cmd); + return; + } + + envp = g_get_environ(); + envp = g_environ_setenv(envp, "VIMB_DOWNLOAD_PATH", + GET_CHAR(c, "download-path"), TRUE); + + if (g_spawn_async(NULL, argv, envp, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error)) { + vb_echo(c, MSG_NORMAL, FALSE, "Download started"); + } else { + vb_echo(c, MSG_ERROR, TRUE, "Could not start download"); + g_warning("%s", error->message); + g_clear_error(&error); + } + g_free(cmd); + g_strfreev(envp); + g_strfreev(argv); +} + /** * Callback for the webkit download failed signal. * This signal is emitted when an error occurs during the download operation. diff --git a/src/setting.c b/src/setting.c index 51883ee..07218ba 100644 --- a/src/setting.c +++ b/src/setting.c @@ -150,6 +150,8 @@ void setting_init(Client *c) i = 100; setting_add(c, "default-zoom", TYPE_INTEGER, &i, default_zoom, 0, NULL); setting_add(c, "download-path", TYPE_CHAR, &"~/", NULL, 0, NULL); + setting_add(c, "download-command", TYPE_CHAR, &"/bin/sh -c \"curl -sLJOC - -e '$VIMB_URI' %s\"", NULL, 0, NULL); + setting_add(c, "download-use-external", TYPE_BOOLEAN, &off, NULL, 0, NULL); setting_add(c, "incsearch", TYPE_BOOLEAN, &off, internal, 0, &c->config.incsearch); i = 10; /* TODO should be global and not overwritten by a new client */ @@ -158,7 +160,7 @@ void setting_init(Client *c) setting_add(c, "spell-checking", TYPE_BOOLEAN, &off, webkit_spell_checking, 0, NULL); setting_add(c, "spell-checking-languages", TYPE_CHAR, &"en_US", webkit_spell_checking_language, FLAG_LIST|FLAG_NODUP, NULL); - /* gui style settings vimb3 */ + /* gui style settings vimb */ setting_add(c, "completion-css", TYPE_CHAR, &"color:#fff;background-color:#656565;font:" SETTING_GUI_FONT_NORMAL, gui_style, 0, NULL); setting_add(c, "completion-hover-css", TYPE_CHAR, &"background-color:#777;", gui_style, 0, NULL); setting_add(c, "completion-selected-css", TYPE_CHAR, &"color:#f6f3e8;background-color:#888;", gui_style, 0, NULL); |