summaryrefslogtreecommitdiff
path: root/browser.c
diff options
context:
space:
mode:
authorPeter Hofmann <scm@uninformativ.de>2014-06-17 19:23:27 +0200
committerPeter Hofmann <scm@uninformativ.de>2014-06-17 21:08:56 +0200
commiteaff2ba4e8682c29837fdebc428a6b2285c633a6 (patch)
tree348332c14c03cff3d026af4804efa0b519ea031a /browser.c
parent7ac557053f5d18840f03d6e393341f981dc724fc (diff)
downloadlariza-eaff2ba4e8682c29837fdebc428a6b2285c633a6.tar.gz
Built-in download feature
Downloading via an external tool poses a problem: You have to pass the current "web context" from the browser to your tool. This context comprises cookies, the referrer, the user agent and information about HTTP basic auth. With some effort, you can pass most of this to your tool -- except for HTTP basic auth. tl;dr: Downloading via wget is pretty complicated. With this commit, WebKit handles the downloads. What's missing, are some GUI elements to monitor and cancel downloads.
Diffstat (limited to 'browser.c')
-rw-r--r--browser.c83
1 files changed, 43 insertions, 40 deletions
diff --git a/browser.c b/browser.c
index c8b9353..199ea22 100644
--- a/browser.c
+++ b/browser.c
@@ -25,10 +25,10 @@ static void changed_load_status(GObject *obj, GParamSpec *pspec,
gpointer data);
static void changed_title(GObject *, GParamSpec *, gpointer);
static void changed_uri(GObject *, GParamSpec *, gpointer);
+static gboolean download_handle(WebKitWebView *, WebKitDownload *, gpointer);
static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
WebKitNetworkRequest *, gchar *,
WebKitWebPolicyDecision *, gpointer);
-static gboolean download_wget(WebKitWebView *, WebKitDownload *, gpointer);
static gchar *ensure_url_scheme(const gchar *);
static void grab_environment_configuration(void);
static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
@@ -232,7 +232,7 @@ client_new(const gchar *uri)
"mime-type-policy-decision-requested",
G_CALLBACK(download_request), NULL);
g_signal_connect(G_OBJECT(c->web_view), "download-requested",
- G_CALLBACK(download_wget), NULL);
+ G_CALLBACK(download_handle), NULL);
g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
G_CALLBACK(key_web_view), c);
g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
@@ -375,57 +375,60 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
}
gboolean
-download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
- WebKitNetworkRequest *request, gchar *mime_type,
- WebKitWebPolicyDecision *policy_decision, gpointer data)
+download_handle(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
{
- (void)frame;
- (void)request;
+ gchar *path, *path2 = NULL, *uri;
+ gboolean ret;
+ int suffix = 1;
+
+ (void)web_view;
(void)data;
- if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
+ path = g_build_filename(download_dir,
+ webkit_download_get_suggested_filename(download),
+ NULL);
+ path2 = g_strdup(path);
+ while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
{
- webkit_web_policy_decision_download(policy_decision);
- return TRUE;
+ g_free(path2);
+
+ path2 = g_strdup_printf("%s.%d", path, suffix);
+ suffix++;
}
- return FALSE;
+
+ if (suffix == 1000)
+ {
+ fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
+ ret = FALSE;
+ }
+ else
+ {
+ uri = g_filename_to_uri(path2, NULL, NULL);
+ webkit_download_set_destination_uri(download, uri);
+ ret = TRUE;
+ g_free(uri);
+ }
+
+ g_free(path);
+ g_free(path2);
+
+ return ret;
}
gboolean
-download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
+download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
+ WebKitNetworkRequest *request, gchar *mime_type,
+ WebKitWebPolicyDecision *policy_decision, gpointer data)
{
- const gchar *uri;
- char id[16] = "";
- gint ret;
-
- (void)web_view;
+ (void)frame;
+ (void)request;
(void)data;
- uri = webkit_download_get_uri(download);
- if (fork() == 0)
+ if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
{
- chdir(download_dir);
- if (embed == 0)
- ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
- else
- {
- if (snprintf(id, 16, "%ld", embed) >= 16)
- {
- fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
- exit(EXIT_FAILURE);
- }
- ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
- uri, NULL);
- }
-
- if (ret == -1)
- {
- fprintf(stderr, __NAME__": exec'ing xterm for download");
- perror(" failed");
- exit(EXIT_FAILURE);
- }
+ webkit_web_policy_decision_download(policy_decision);
+ return TRUE;
}
-
return FALSE;
}