surf-0.5-download.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       surf-0.5-download.diff (6076B)
       ---
            1 diff --git a/config.def.h b/config.def.h
            2 index 1cba4d7..3065c73 100644
            3 --- a/config.def.h
            4 +++ b/config.def.h
            5 @@ -11,6 +11,7 @@ static char *progress_proxy_untrust = "#FF6600";
            6  static char *stylefile      = "~/.surf/style.css";
            7  static char *scriptfile     = "~/.surf/script.js";
            8  static char *cookiefile     = "~/.surf/cookies.txt";
            9 +static char *downdir        = "/tmp";
           10  static time_t sessiontime   = 3600;
           11  static char *cafile         = "/etc/ssl/certs/ca-certificates.crt";
           12  static char *strictssl      = FALSE; /* Refuse untrusted SSL connections */
           13 diff --git a/surf.c b/surf.c
           14 index c9fa08d..4d589aa 100644
           15 --- a/surf.c
           16 +++ b/surf.c
           17 @@ -114,6 +114,7 @@ static void destroyclient(Client *c);
           18  static void destroywin(GtkWidget* w, Client *c);
           19  static void die(const char *errstr, ...);
           20  static void drawindicator(Client *c);
           21 +static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
           22  static void eval(Client *c, const Arg *arg);
           23  static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
           24  static void find(Client *c, const Arg *arg);
           25 @@ -289,6 +290,50 @@ cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
           26          flock(COOKIEJAR(self)->lock, LOCK_UN);
           27  }
           28  
           29 +struct client_size_tuple {
           30 +        Client* c;
           31 +        gint s;
           32 +};
           33 +
           34 +static void
           35 +late_download_update(WebKitWebView* view, GParamSpec *pspec, struct client_size_tuple* t){
           36 +        char script[1024]; char* s= script;
           37 +        snprintf(script, 1024, "c(%d, %d)", t->s, t->s);
           38 +        const Arg a= {.v = (void*) &s};
           39 +        eval(t->c, &a);
           40 +        free(t);
           41 +}
           42 +
           43 +static void
           44 +download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
           45 +        WebKitDownloadStatus status;
           46 +        char script[2048]; char* s= script;
           47 +
           48 +        status = webkit_download_get_status(o);
           49 +        if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
           50 +                snprintf(script, 2048, "u(%d, %d, %d)",
           51 +                        (gint)webkit_download_get_current_size(o),
           52 +                        (gint)webkit_download_get_total_size(o),
           53 +                        (gint)(webkit_download_get_progress(o) * 100));
           54 +                const Arg a= {.v = (void*) &s};
           55 +                eval(c, &a);
           56 +        }
           57 +        else if (status == WEBKIT_DOWNLOAD_STATUS_FINISHED){
           58 +                if( webkit_web_view_get_load_status(c->view) == WEBKIT_LOAD_FINISHED ){
           59 +                        snprintf(script, 2048, "c(%d, %d)",
           60 +                                (gint)webkit_download_get_current_size(o),
           61 +                                (gint)webkit_download_get_total_size(o));
           62 +                        const Arg a= {.v = (void*) &s};
           63 +                        eval(c, &a);
           64 +                }
           65 +                else {
           66 +                        struct client_size_tuple* t= calloc(1, sizeof(struct client_size_tuple));
           67 +                        t->c= c; t->s= (gint)webkit_download_get_current_size(o);
           68 +                        g_signal_connect(c->view, "document-load-finished", G_CALLBACK(late_download_update), t);
           69 +                }
           70 +        }
           71 +}
           72 +
           73  static void
           74  evalscript(JSContextRef js, char *script, char* scriptname) {
           75          JSStringRef jsscript, jsscriptname;
           76 @@ -496,12 +541,105 @@ geturi(Client *c) {
           77  
           78  static gboolean
           79  initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
           80 -        Arg arg;
           81 +        gchar *uri, *path;
           82 +        const gchar *filename;
           83 +        Client *n;
           84 +        const char template[] =
           85 +"<html>" \
           86 +"<head>" \
           87 +"<title>Download - %s</title>" \
           88 +"<script>" \
           89 +"function formText(x){" \
           90 +"  if(x >= 1073741824)  { return (Math.floor(x/10737418.24)/100) + \"G\"; }" \
           91 +"  else if(x >= 1048576){ return (Math.floor(x/10485.76)/100) + \"M\"; }" \
           92 +"  else if(x >= 1024)   { return (Math.floor(x/10.24)/100) + \"k\"; }" \
           93 +"  else                 { return x+\"b\"; }" \
           94 +"}" \
           95 +"function updateText(c,t){" \
           96 +"  txt= formText(c) + \"/\" + formText(t);" \
           97 +"  DLTEXT.textContent= txt;" \
           98 +"  /* center text in bar */" \
           99 +"  DLTEXT.setAttribute('x', 102-4.4*txt.length)" \
          100 +"}" \
          101 +"function c(c, t){" \
          102 +"  DLGRAD.setAttribute('x2', 230);" \
          103 +"  DLGRAD.setAttribute('x1', 205);" \
          104 +"  updateText(c,t);" \
          105 +"  document.getElementById('stop1').setAttribute('style', \"stop-color:#2020ff;\");" \
          106 +"}" \
          107 +"function u(c,t,p){" \
          108 +"  DLGRAD.setAttribute('x2', Math.floor(p*205/100) + 25);" \
          109 +"  DLGRAD.setAttribute('x1', Math.floor(p*205/100));" \
          110 +"  updateText(c,t);" \
          111 +"}" \
          112 +"</script>" \
          113 +"</head>" \
          114 +"<body>" \
          115 +"<center>" \
          116 +"<h2>Downloading</h2>" \
          117 +"<h3>%s</h3>" \
          118 +"to %s<br/>" \
          119 +"<svg" \
          120 +"   xmlns:cc=\"http://creativecommons.org/ns#\"" \
          121 +"   xmlns:svg=\"http://www.w3.org/2000/svg\"" \
          122 +"   xmlns=\"http://www.w3.org/2000/svg\"" \
          123 +"   xmlns:xlink=\"http://www.w3.org/1999/xlink\"" \
          124 +"   width=\"210\"" \
          125 +"   height=\"60\"" \
          126 +"   id=\"download\">" \
          127 +"  <defs>" \
          128 +"    <linearGradient" \
          129 +"       id=\"dlgradient\"" \
          130 +"       x1=\"0\"" \
          131 +"       y1=\"0\"" \
          132 +"       x2=\"25\"" \
          133 +"       y2=\"0\"" \
          134 +"       gradientUnits=\"userSpaceOnUse\">" \
          135 +"      <stop style=\"stop-color:#00ff00;\" offset=\"0\" id=\"stop1\" />" \
          136 +"      <stop style=\"stop-color:#00ff00;stop-opacity:0;\" offset=\"1\" id=\"stop2\" />" \
          137 +"    </linearGradient>" \
          138 +"  </defs>" \
          139 +"    <rect" \
          140 +"       style=\"fill:url(#dlgradient);stroke:#000000;stroke-width:3\"" \
          141 +"       id=\"rect2985\"" \
          142 +"       width=\"200\"" \
          143 +"       height=\"50\"" \
          144 +"       x=\"5\"" \
          145 +"       y=\"5\"" \
          146 +"       ry=\"25\" />" \
          147 +"    <text id=\"dltext\" x=\"92\" y=\"35\">0/0</text>" \
          148 +"</svg>" \
          149 +"</center>" \
          150 +"<script>" \
          151 +"DLGRAD= document.getElementById('dlgradient');" \
          152 +"DLTEXT= document.getElementById('dltext');" \
          153 +"</script>" \
          154 +"</body>" \
          155 +"</html>";
          156 +        char html[sizeof(template)+2048];
          157 +        filename = webkit_download_get_suggested_filename(o);
          158 +
          159 +        path = g_build_filename(downdir, filename, NULL);
          160 +        uri = g_filename_to_uri(path, NULL, NULL);
          161 +
          162 +        webkit_download_set_destination_uri(o, uri);
          163 +        webkit_download_start(o);
          164 +
          165 +        n = newclient();
          166 +        snprintf(html, sizeof(template)+2048, template, filename, filename, path);
          167 +        webkit_web_view_load_string(n->view, html, NULL, NULL, NULL);
          168 +
          169 +        g_signal_connect(o, "notify::progress", G_CALLBACK(download), n);
          170 +        g_signal_connect(o, "notify::status", G_CALLBACK(download), n);
          171 +
          172 +        n->title = g_strdup_printf("Downloading %s", filename);
          173 +        n->progress = 0;
          174 +        update(n);
          175 +
          176 +        g_free(path);
          177 +        g_free(uri);
          178  
          179 -        updatewinid(c);
          180 -        arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
          181 -        spawn(c, &arg);
          182 -        return FALSE;
          183 +        return TRUE;
          184  }
          185  
          186  static void