]> git.armaanb.net Git - chorizo.git/commitdiff
Built-in download feature
authorPeter Hofmann <scm@uninformativ.de>
Tue, 17 Jun 2014 17:23:27 +0000 (19:23 +0200)
committerPeter Hofmann <scm@uninformativ.de>
Tue, 17 Jun 2014 19:08:56 +0000 (21:08 +0200)
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.

README
browser.c

diff --git a/README b/README
index add1d4068fb1c9ca9cac83b4d33cd2ffcfad25f0..c32871663e7a4c0b7cba8e00addc18a36338ae72 100644 (file)
--- a/README
+++ b/README
@@ -12,7 +12,7 @@ Features:
        - An input box to change the current URL
        - Global content zoom
        - Pluggability into suckless' tabbed
-       - Downloading files using wget
+       - Built-in downloads
        - vi-like scrolling (modified by CTRL)
        - Searching the current page for a word
        - Adblock
@@ -21,6 +21,7 @@ Features:
 
 Planned features:
 
+       - Monitoring and canceling downloads
        - Keyword based searching (opening "wi foo" will search wikipedia)
 
 
index c8b93533f672bd99e62f223297e548fbdd77797a..199ea2238fe5c0ed498a3c4aab74f648bed3d563 100644 (file)
--- 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;
 }