sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

surf-0.4.1-download.diff (3481B)


      1 diff -r 71388899ac09 config.def.h
      2 --- a/config.def.h	Tue Jun 08 09:06:10 2010 +0200
      3 +++ b/config.def.h	Wed Sep 28 14:15:32 2011 +0100
      4 @@ -5,6 +5,7 @@
      5  static char *stylefile      = ".surf/style.css";
      6  static char *scriptfile     = ".surf/script.js";
      7  static char *cookiefile     = ".surf/cookies.txt";
      8 +static char *downdir        = "/tmp";
      9  static time_t sessiontime   = 3600;
     10  #define NOBACKGROUND 0
     11  
     12 diff -r 71388899ac09 surf.c
     13 --- a/surf.c	Tue Jun 08 09:06:10 2010 +0200
     14 +++ b/surf.c	Wed Sep 28 14:15:32 2011 +0100
     15 @@ -74,7 +74,9 @@
     16  static void destroyclient(Client *c);
     17  static void destroywin(GtkWidget* w, Client *c);
     18  static void die(char *str);
     19 +static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
     20  static void drawindicator(Client *c);
     21 +static void evalscript(Client *c, char *script);
     22  static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
     23  static void find(Client *c, const Arg *arg);
     24  static const char *getatom(Client *c, int a);
     25 @@ -239,6 +241,21 @@
     26  }
     27  
     28  void
     29 +download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
     30 +	WebKitDownloadStatus status;
     31 +	char script[2048];
     32 +
     33 +	status = webkit_download_get_status(o);
     34 +	if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
     35 +		snprintf(script, 2048, "u(%d, %d, %d)",
     36 +		         (gint)webkit_download_get_current_size(o),
     37 +		         (gint)webkit_download_get_total_size(o),
     38 +		         (gint)(webkit_download_get_progress(o) * 100));
     39 +		evalscript(c, script);
     40 +	}
     41 +}
     42 +
     43 +void
     44  drawindicator(Client *c) {
     45  	gint width;
     46  	const char *uri;
     47 @@ -261,6 +278,17 @@
     48  	g_object_unref(gc);
     49  }
     50  
     51 +void
     52 +evalscript(Client *c, char *script) {
     53 +	JSValueRef exception = NULL;
     54 +	WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
     55 +	JSContextRef js = webkit_web_frame_get_global_context(frame);
     56 +	JSStringRef jsscript = JSStringCreateWithUTF8CString(script);
     57 +
     58 +	JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception)
     59 +;
     60 +}
     61 +
     62  gboolean
     63  exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
     64  	drawindicator(c);
     65 @@ -328,12 +356,40 @@
     66  
     67  gboolean
     68  initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
     69 -	Arg arg;
     70 +	gchar *uri, *path;
     71 +	const gchar *filename;
     72 +	Client *n;
     73 +	char html[1024];
     74  
     75 -	updatewinid(c);
     76 -	arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
     77 -	spawn(c, &arg);
     78 -	return FALSE;
     79 +	n = newclient();
     80 +	filename = webkit_download_get_suggested_filename(o);
     81 +
     82 +	path = g_build_filename(downdir, filename, NULL);
     83 +	uri = g_filename_to_uri(path, NULL, NULL);
     84 +	webkit_download_set_destination_uri(o, uri);
     85 +	g_free(path);
     86 +	g_free(uri);
     87 + 
     88 +	snprintf(html, 1024,
     89 +	         "<html><head><script>" \
     90 +	         "function u(c, t, p) {" \
     91 +	         "  document.getElementById('c').innerHTML = c;" \
     92 +	         "  document.getElementById('t').innerHTML = t;" \
     93 +	         "  document.getElementById('p').innerHTML = p;}" \
     94 +	         "</script></head><body><p>Downloading %s</p>" \
     95 +	         "<p><span id='c'>0</span> / <span id='t'>0</span> " \
     96 +	         "(<span id='p'>0</span>%%)</p></body></html>",
     97 +	         filename);
     98 +	webkit_web_view_load_string(n->view, html, NULL, NULL, NULL);
     99 +
    100 +	g_signal_connect(o, "notify::progress", G_CALLBACK(download), n);
    101 +	g_signal_connect(o, "notify::status", G_CALLBACK(download), n);
    102 +	webkit_download_start(o);
    103 +	n->title = g_strdup_printf("Downloading %s", filename);
    104 +	n->progress = 0;
    105 +	update(n);
    106 +
    107 +	return TRUE;
    108  }
    109  
    110  gboolean