diff -N -up surf-0.4.1/config.def.h surf-0.4.1-download//config.def.h --- surf-0.4.1/config.def.h 2010-06-08 04:06:41.000000000 -0300 +++ surf-0.4.1-download//config.def.h 2010-07-09 17:44:56.000000000 -0300 @@ -6,6 +6,8 @@ static char *stylefile = ".surf/sty static char *scriptfile = ".surf/script.js"; static char *cookiefile = ".surf/cookies.txt"; static time_t sessiontime = 3600; +static char *downdir = "/tmp"; +static char *downhtml = ".surf/download.html"; #define NOBACKGROUND 0 #define SETPROP(p, q) { .v = (char *[]){ "/bin/sh", "-c", \ diff -N -up surf-0.4.1/surf.c surf-0.4.1-download//surf.c --- surf-0.4.1/surf.c 2010-06-08 04:06:42.000000000 -0300 +++ surf-0.4.1-download//surf.c 2010-07-09 17:43:35.000000000 -0300 @@ -74,7 +74,9 @@ 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 void evalscript(Client *c, char *script); static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c); static void find(Client *c, const Arg *arg); static const char *getatom(Client *c, int a); @@ -239,6 +241,26 @@ die(char *str) { } void +download(WebKitDownload *o, GParamSpec *pspec, Client *c) { + WebKitDownloadStatus status; + gchar *script; + + status = webkit_download_get_status(o); + if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) { + c->progress = (gint) (webkit_download_get_progress(o) * 100); + script = g_strdup_printf("fileinfo('%s', '%s', %d, %d, %d)", + webkit_download_get_suggested_filename(o), + webkit_download_get_destination_uri(o), + (gint)webkit_download_get_total_size(o), + (gint)webkit_download_get_current_size(o), + (gint)c->progress); + evalscript(c, script); + g_free(script); + update(c); + } +} + +void drawindicator(Client *c) { gint width; const char *uri; @@ -261,6 +283,16 @@ drawindicator(Client *c) { g_object_unref(gc); } +void +evalscript(Client *c, char *script) { + JSValueRef exception = NULL; + WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view); + JSContextRef js = webkit_web_frame_get_global_context(frame); + JSStringRef jsscript = JSStringCreateWithUTF8CString(script); + + JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception); +} + gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) { drawindicator(c); @@ -328,12 +360,33 @@ gotheaders(SoupMessage *msg, gpointer v) gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) { - Arg arg; + gchar *uri, *path; + const gchar *filename; + Client *n; - updatewinid(c); - arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o)); - spawn(c, &arg); - return FALSE; + n = newclient(); + 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); + + path = buildpath(downhtml); + uri = g_filename_to_uri(path, NULL, NULL); + webkit_web_view_load_uri(n->view, uri); + g_free(path); + g_free(uri); + + g_signal_connect(o, "notify::progress", G_CALLBACK(download), n); + g_signal_connect(o, "notify::status", G_CALLBACK(download), n); + webkit_download_start(o); + n->title = g_strdup_printf("Downloading %s", filename); + n->progress = 0; + update(n); + + return TRUE; } gboolean