[dev] [surf] downloads

From: Peter John Hartman <peterjohnhartman_AT_gmail.com>
Date: Thu, 3 Nov 2011 16:30:30 -0400

Hi,

One thing that *rumor* has it surf can't handle are fancy-schmancy
downloads, for instance, I'm told RapidShare fails[1]. On #suckless,
it was decided that we might want to slip a modified version of njw's
patch[2] into surf tip. (The modified version is below.)

The deal is that in the unmodified or modified version, this patch changes
the behaviour. Whereas before surf assumed the user would have some handy
shell script that called wget available to handle downloads, now it doesn't.
Strong opinions on the matter? Granted that the modified/unmodified patch
actually works with RapidShare and the extant behaviour doesn't, I'm
inclined to change the behaviour.

So, to the modified patch (below), and some comments:

First of all, as opposed to njw's patch, this patch doesn't spam up the
desktop with a new window and it doesn't generate an HTML file on the fly
with some javascript. That's probably a good thing.

Second of all, and instead, it just prints to stdout (a) the fact that the
Download has started, together with the filename, and (b) the fact that it
has finished/cancelled/errored, together with the filename.

Third of all, I've slipped in my little "spawn" call to spawn a shell script
to do post-processing, e.g., run-mailcap "/home/peterjh/dls/$*". This is,
of course, hugely optional. I like it.

Best, Peter

diff -r 46a72ccc435f config.def.h
--- a/config.def.h Sat Oct 29 10:59:34 2011 +0200
+++ b/config.def.h Thu Nov 03 16:29:32 2011 -0400
_AT_@ -5,6 +5,7 @@ static char *progress_trust = "#00FF00";
 static char *stylefile = ".surf/style.css";
 static char *scriptfile = ".surf/script.js";
 static char *cookiefile = ".surf/cookies.txt";
+static char *downdir = "/tmp";
 static time_t sessiontime = 3600;
 #define HIDE_BACKGROUND FALSE
 
diff -r 46a72ccc435f surf.c
--- a/surf.c Sat Oct 29 10:59:34 2011 +0200
+++ b/surf.c Thu Nov 03 16:29:32 2011 -0400
_AT_@ -73,6 +73,7 @@ static gboolean decidewindow(WebKitWebVi
 static void destroyclient(Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 static void die(char *str);
+static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
 static void drawindicator(Client *c);
 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
 static void find(Client *c, const Arg *arg);
_AT_@ -239,6 +240,28 @@ die(char *str) {
 }
 
 void
+download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
+ WebKitDownloadStatus status;
+ const gchar *filename;
+
+ filename = webkit_download_get_suggested_filename(o);
+
+ status = webkit_download_get_status(o);
+ if(status == WEBKIT_DOWNLOAD_STATUS_FINISHED) {
+ fprintf(stdout, "Download Finished: %s\n", filename);
+ /* pjh: hugely optional */
+ Arg arg;
+ updatewinid(c);
+ arg = (Arg)DOWNLOAD((char *)filename); /* webkit_download_get_uri(o));*/
+ spawn(c, &arg);
+ /* pjh: end hugely optional */
+ }
+ else if (status == WEBKIT_DOWNLOAD_STATUS_CANCELLED)
+ fprintf(stdout, "Download Cancelled: %s\n", filename);
+ else if (status == WEBKIT_DOWNLOAD_STATUS_ERROR)
+ fprintf(stdout, "Download Error: %s\n", filename);
+
+}
+
+void
 drawindicator(Client *c) {
         gint width;
         const char *uri;
_AT_@ -328,12 +351,19 @@ gotheaders(SoupMessage *msg, gpointer v)
 
 gboolean
 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
- Arg arg;
+ gchar *uri, *path;
+ const gchar *filename;
 
- updatewinid(c);
- arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
- spawn(c, &arg);
- return FALSE;
+ filename = webkit_download_get_suggested_filename(o);
+ path = g_build_filename(downdir, filename, NULL);
+ uri = g_filename_to_uri(path, NULL, NULL);
+ webkit_download_set_destination_uri(o, uri);
+ g_free(path);
+ g_free(uri);
+ fprintf(stdout, "Download Started: %s\n", filename);
+ g_signal_connect(o, "notify::status", G_CALLBACK(download), c);
+ webkit_download_start(o);
+ return TRUE;
 }
 

[1] http://surf.suckless.org/failing_sites
[2] http://surf.suckless.org/patches/download

-- 
sic dicit magister P
University of Toronto / Fordham University
Collins Hall B06; Office Hours TF10-12
http://individual.utoronto.ca/peterjh
gpg --keyserver pgp.mit.edu --recv-keys E0DBD3D6 
Received on Thu Nov 03 2011 - 21:30:30 CET

This archive was generated by hypermail 2.3.0 : Thu Nov 03 2011 - 21:36:03 CET