surf-dlconsole-20190919-d068a38.diff (7159B)
1 From 0ea5ecb238b932c533413b912b7981a737af56cf Mon Sep 17 00:00:00 2001 2 From: danoloan10 <danoloan10@tutanota.com> 3 Date: Thu, 19 Sep 2019 18:25:59 +0200 4 Subject: [PATCH] Basic integrated downloads via console display 5 6 --- 7 config.def.h | 16 ++++--- 8 surf.c | 118 +++++++++++++++++++++++++++++++++++++++------------ 9 2 files changed, 101 insertions(+), 33 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 34265f6..375be93 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -6,6 +6,8 @@ static char *styledir = "~/.surf/styles/"; 16 static char *certdir = "~/.surf/certificates/"; 17 static char *cachedir = "~/.surf/cache/"; 18 static char *cookiefile = "~/.surf/cookies.txt"; 19 +static char *dldir = "~/dl/"; 20 +static char *dlstatus = "~/.surf/dlstatus/"; 21 22 /* Webkit default features */ 23 /* Highest priority value will be used. 24 @@ -76,13 +78,12 @@ static WebKitFindOptions findopts = WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | 25 } \ 26 } 27 28 -/* DOWNLOAD(URI, referer) */ 29 -#define DOWNLOAD(u, r) { \ 30 +#define DLSTATUS { \ 31 .v = (const char *[]){ "st", "-e", "/bin/sh", "-c",\ 32 - "curl -g -L -J -O -A \"$1\" -b \"$2\" -c \"$2\"" \ 33 - " -e \"$3\" \"$4\"; read", \ 34 - "surf-download", useragent, cookiefile, r, u, NULL \ 35 - } \ 36 + "while true; do cat $1/* 2>/dev/null || echo \"no hay descargas\";"\ 37 + "A=; read A; "\ 38 + "if [ $A = \"clean\" ]; then rm $1/*; fi; clear; done",\ 39 + "surf-dlstatus", dlstatus, NULL } \ 40 } 41 42 /* PLUMB(URI) */ 43 @@ -180,6 +181,9 @@ static Key keys[] = { 44 { MODKEY|GDK_SHIFT_MASK, GDK_KEY_b, toggle, { .i = ScrollBars } }, 45 { MODKEY|GDK_SHIFT_MASK, GDK_KEY_t, toggle, { .i = StrictTLS } }, 46 { MODKEY|GDK_SHIFT_MASK, GDK_KEY_m, toggle, { .i = Style } }, 47 + 48 + /* download-console */ 49 + { MODKEY, GDK_KEY_d, spawndls, { 0 } }, 50 }; 51 52 /* button definitions */ 53 diff --git a/surf.c b/surf.c 54 index 2b54e3c..771858e 100644 55 --- a/surf.c 56 +++ b/surf.c 57 @@ -205,10 +205,6 @@ static void decidenewwindow(WebKitPolicyDecision *d, Client *c); 58 static void decideresource(WebKitPolicyDecision *d, Client *c); 59 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, 60 Client *c); 61 -static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d, 62 - Client *c); 63 -static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c); 64 -static void download(Client *c, WebKitURIResponse *r); 65 static void webprocessterminated(WebKitWebView *v, 66 WebKitWebProcessTerminationReason r, 67 Client *c); 68 @@ -237,6 +233,17 @@ static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h); 69 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h); 70 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h); 71 72 +/* download-console */ 73 +static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d, 74 + Client *c); 75 +static void downloadfailed(WebKitDownload *d, GParamSpec *ps, void *arg); 76 +static void downloadfinished(WebKitDownload *d, GParamSpec *ps, void *arg); 77 +static gboolean decidedestination(WebKitDownload *d, 78 + gchar *suggested_filename, void *arg); 79 +static void printprogress(WebKitDownload *d, GParamSpec *ps, void *arg); 80 +static void logdownload(WebKitDownload *d, gchar *tail); 81 +static void spawndls(Client *c, const Arg *a); 82 + 83 static char winid[64]; 84 static char togglestats[12]; 85 static char pagestats[2]; 86 @@ -340,6 +347,8 @@ setup(void) 87 scriptfile = buildfile(scriptfile); 88 cachedir = buildpath(cachedir); 89 certdir = buildpath(certdir); 90 + dlstatus = buildpath(dlstatus); 91 + dldir = buildpath(dldir); 92 93 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy)); 94 95 @@ -1079,6 +1088,8 @@ cleanup(void) 96 g_free(scriptfile); 97 g_free(stylefile); 98 g_free(cachedir); 99 + g_free(dldir); 100 + g_free(dlstatus); 101 XCloseDisplay(dpy); 102 } 103 104 @@ -1710,8 +1721,7 @@ decideresource(WebKitPolicyDecision *d, Client *c) 105 if (webkit_response_policy_decision_is_mime_type_supported(r)) { 106 webkit_policy_decision_use(d); 107 } else { 108 - webkit_policy_decision_ignore(d); 109 - download(c, res); 110 + webkit_policy_decision_download(d); 111 } 112 } 113 114 @@ -1721,27 +1731,6 @@ insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c) 115 c->insecure = 1; 116 } 117 118 -void 119 -downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c) 120 -{ 121 - g_signal_connect(G_OBJECT(d), "notify::response", 122 - G_CALLBACK(responsereceived), c); 123 -} 124 - 125 -void 126 -responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c) 127 -{ 128 - download(c, webkit_download_get_response(d)); 129 - webkit_download_cancel(d); 130 -} 131 - 132 -void 133 -download(Client *c, WebKitURIResponse *r) 134 -{ 135 - Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c)); 136 - spawn(c, &a); 137 -} 138 - 139 void 140 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r, 141 Client *c) 142 @@ -1971,6 +1960,81 @@ clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h) 143 spawn(c, &arg); 144 } 145 146 +/* download-console */ 147 + 148 +void 149 +downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c) 150 +{ 151 + webkit_download_set_allow_overwrite(d, TRUE); 152 + g_signal_connect(G_OBJECT(d), "decide-destination", 153 + G_CALLBACK(decidedestination), NULL); 154 + g_signal_connect(G_OBJECT(d), "notify::estimated-progress", 155 + G_CALLBACK(printprogress), NULL); 156 + g_signal_connect(G_OBJECT(d), "failed", 157 + G_CALLBACK(downloadfailed), NULL); 158 + g_signal_connect(G_OBJECT(d), "finished", 159 + G_CALLBACK(downloadfinished), NULL); 160 +} 161 + 162 +void 163 +downloadfailed(WebKitDownload *d, GParamSpec *ps, void *arg) 164 +{ 165 + logdownload(d, " -- FAILED"); 166 +} 167 + 168 +void 169 +downloadfinished(WebKitDownload *d, GParamSpec *ps, void *arg) 170 +{ 171 + logdownload(d, " -- COMPLETED"); 172 +} 173 + 174 +gboolean 175 +decidedestination(WebKitDownload *d, gchar *suggested_filename, void *arg) 176 +{ 177 + gchar *dest; 178 + dest = g_strdup_printf("file://%s/%s", dldir, suggested_filename); 179 + webkit_download_set_destination(d, dest); 180 + return TRUE; 181 +} 182 + 183 +void 184 +printprogress(WebKitDownload *d, GParamSpec *ps, void *arg) 185 +{ 186 + logdownload(d, ""); 187 +} 188 + 189 +void 190 +logdownload(WebKitDownload *d, gchar *tail) 191 +{ 192 + gchar *filename, *statfile; 193 + FILE *stat; 194 + 195 + filename = g_path_get_basename(webkit_download_get_destination(d)); 196 + statfile = g_strdup_printf("%s/%s", dlstatus, filename); 197 + 198 + if ((stat = fopen(statfile, "w")) == NULL) { 199 + perror("dlstatus"); 200 + } else { 201 + fprintf(stat, "%s: %d%% (%d.%ds)%s\n", 202 + filename, 203 + (int)(webkit_download_get_estimated_progress(d) * 100), 204 + (int) webkit_download_get_elapsed_time(d), 205 + (int)(webkit_download_get_elapsed_time(d) * 100), 206 + tail); 207 + fclose(stat); 208 + } 209 + 210 + g_free(statfile); 211 + g_free(filename); 212 +} 213 + 214 +void 215 +spawndls(Client *c, const Arg *a) 216 +{ 217 + Arg arg = (Arg)DLSTATUS; 218 + spawn(c, &arg); 219 +} 220 + 221 int 222 main(int argc, char *argv[]) 223 { 224 -- 225 2.22.1 226