Rev 8278 | Rev 8291 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8278 | leency | 1 | //Copyright 2020 by Leency |
2 | |||
3 | #define MEMSIZE 1024 * 100 |
||
4 | #include "../lib/gui.h" |
||
5 | #include "../lib/random.h" |
||
8281 | leency | 6 | |
8278 | leency | 7 | #include "../lib/obj/box_lib.h" |
8 | #include "../lib/obj/http.h" |
||
8281 | leency | 9 | #include "../lib/obj/libini.h" |
10 | |||
8278 | leency | 11 | #include "../lib/patterns/http_downloader.h" |
12 | |||
13 | #include "const.h" |
||
14 | |||
7284 | leency | 15 | DOWNLOADER downloader; |
8281 | leency | 16 | checkbox autoclose = { T_AUTOCLOSE, true }; |
7284 | leency | 17 | |
8278 | leency | 18 | char downloader_edit[4000]; |
5493 | leency | 19 | char filepath[4096]; |
8278 | leency | 20 | edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000, |
21 | sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19}; |
||
8281 | leency | 22 | progress_bar pb = {0, GAPX, 58, 315, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; |
6969 | leency | 23 | //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color; |
8278 | leency | 24 | |
8281 | leency | 25 | bool exit_when_done = false; |
7521 | leency | 26 | |
6374 | guillem | 27 | |
8278 | leency | 28 | void main() |
5493 | leency | 29 | { |
8278 | leency | 30 | dword shared_url; |
31 | load_dll(boxlib, #box_lib_init,0); |
||
32 | load_dll(libHTTP, #http_lib_init,1); |
||
8281 | leency | 33 | load_dll(libini, #lib_init,1); |
8278 | leency | 34 | |
7281 | leency | 35 | if (!dir_exists(#save_to)) CreateDir(#save_to); |
6969 | leency | 36 | |
8278 | leency | 37 | if (param) { |
38 | if (!strncmp(#param, "-exit ", 6)) { |
||
39 | exit_when_done = true; |
||
40 | param += 6; |
||
41 | } |
||
42 | |||
43 | if (!strncmp(#param, "-mem", 5)) { |
||
8281 | leency | 44 | //shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE); |
8278 | leency | 45 | strcpy(#downloader_edit, shared_url); |
46 | } else { |
||
47 | strcpy(#downloader_edit, #param); |
||
48 | } |
||
49 | } |
||
7281 | leency | 50 | if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); |
51 | ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); |
||
6374 | guillem | 52 | |
8278 | leency | 53 | @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); |
54 | @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP); |
||
55 | loop() switch(@WaitEvent()) |
||
7281 | leency | 56 | { |
57 | case evMouse: |
||
58 | edit_box_mouse stdcall (#ed); |
||
59 | break; |
||
6978 | leency | 60 | |
7281 | leency | 61 | case evButton: |
8278 | leency | 62 | ProcessEvent(GetButtonID()); |
7281 | leency | 63 | break; |
6978 | leency | 64 | |
7281 | leency | 65 | case evKey: |
66 | GetKeys(); |
||
67 | edit_box_key stdcall(#ed); |
||
8278 | leency | 68 | if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301); |
7281 | leency | 69 | break; |
6978 | leency | 70 | |
7281 | leency | 71 | case evReDraw: |
8278 | leency | 72 | DrawWindow(); |
7281 | leency | 73 | break; |
74 | |||
75 | default: |
||
76 | if (!downloader.MonitorProgress()) break; |
||
7525 | leency | 77 | pb.max = downloader.httpd.content_length / 100; |
78 | EDI = downloader.httpd.content_received/100; |
||
79 | if (pb.value != EDI) |
||
7281 | leency | 80 | { |
7525 | leency | 81 | pb.value = EDI; |
7281 | leency | 82 | progressbar_draw stdcall(#pb); |
83 | DrawDownloading(); |
||
84 | } |
||
85 | if (downloader.state == STATE_COMPLETED) |
||
86 | { |
||
87 | SaveDownloadedFile(); |
||
8278 | leency | 88 | if (exit_when_done) ExitProcess(); |
7281 | leency | 89 | StopDownloading(); |
8278 | leency | 90 | DrawWindow(); |
7281 | leency | 91 | break; |
92 | } |
||
93 | } |
||
5493 | leency | 94 | } |
6374 | guillem | 95 | |
8278 | leency | 96 | void ProcessEvent(int id) |
6001 | leency | 97 | { |
8281 | leency | 98 | autoclose.click(id); |
8278 | leency | 99 | if (id==001) { StopDownloading(); ExitProcess(); } |
7281 | leency | 100 | if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading(); |
101 | if (id==302) StopDownloading(); |
||
8278 | leency | 102 | if (id==305) RunProgram("/sys/File managers/Eolite", #filepath); |
7281 | leency | 103 | if (id==306) { |
104 | SetCurDir(#save_to); |
||
105 | RunProgram("/sys/@open", #filepath); |
||
106 | } |
||
6001 | leency | 107 | } |
6374 | guillem | 108 | |
8278 | leency | 109 | void DrawWindow() |
6374 | guillem | 110 | { |
7281 | leency | 111 | int but_x = 0; |
8281 | leency | 112 | #define BUT_Y 58; |
8278 | leency | 113 | |
114 | sc.get(); |
||
115 | pb.frame_color = sc.work_dark; |
||
116 | DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); |
||
117 | |||
8281 | leency | 118 | autoclose.draw(WIN_W-135, BUT_Y+6); |
119 | |||
7281 | leency | 120 | if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) |
121 | { |
||
8281 | leency | 122 | but_x = GAPX + DrawStandartCaptButton(GAPX, BUT_Y, 301, START_DOWNLOADING); |
7281 | leency | 123 | if (filepath[0]) |
124 | { |
||
8281 | leency | 125 | but_x += DrawStandartCaptButton(but_x, BUT_Y, 305, SHOW_IN_FOLDER); |
126 | DrawStandartCaptButton(but_x, BUT_Y, 306, OPEN_FILE_TEXT); |
||
7281 | leency | 127 | } |
128 | } |
||
129 | if (downloader.state == STATE_IN_PROGRESS) |
||
130 | { |
||
8281 | leency | 131 | DrawStandartCaptButton(WIN_W - 240, BUT_Y, 302, STOP_DOWNLOADING); |
7281 | leency | 132 | DrawDownloading(); |
133 | } |
||
134 | ed.offset=0; |
||
135 | DrawEditBox(#ed); |
||
5519 | leency | 136 | } |
6374 | guillem | 137 | |
6001 | leency | 138 | void StartDownloading() |
139 | { |
||
8229 | leency | 140 | char http_url[URL_SIZE]; |
141 | char proxy_url[URL_SIZE]; |
||
7281 | leency | 142 | StopDownloading(); |
8229 | leency | 143 | if (!strncmp(#downloader_edit,"https://",7)) { |
144 | notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); |
||
8278 | leency | 145 | miniprintf(#http_url, "http://%s", #downloader_edit+8); |
8229 | leency | 146 | if (!downloader.Start(#http_url)) { |
147 | notify("'Download failed.' -E"); |
||
148 | StopDownloading(); |
||
149 | } |
||
150 | //sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit); |
||
151 | //if (!downloader.Start(#proxy_url)) { |
||
152 | // notify("'Download failed.' -E"); |
||
153 | // StopDownloading(); |
||
154 | //} |
||
8278 | leency | 155 | DrawWindow(); |
7281 | leency | 156 | return; |
157 | } |
||
158 | if (!downloader.Start(#downloader_edit)) { |
||
8278 | leency | 159 | if (exit_when_done) ExitProcess(); |
160 | notify(T_ERROR_STARTING_DOWNLOAD); |
||
7281 | leency | 161 | StopDownloading(); |
162 | return; |
||
163 | } |
||
164 | ed.blur_border_color = 0xCACACA; |
||
165 | ed.flags = 100000000000b; |
||
166 | pb.value = 0; |
||
8278 | leency | 167 | DrawWindow(); |
6001 | leency | 168 | } |
7521 | leency | 169 | |
7525 | leency | 170 | /* |
7521 | leency | 171 | struct TIME |
172 | { |
||
173 | dword old; |
||
174 | dword cur; |
||
175 | dword gone; |
||
176 | } time = {0,0,0}; |
||
177 | |||
178 | dword netdata_received; |
||
179 | dword speed; |
||
180 | |||
181 | void CalculateSpeed() |
||
182 | { |
||
183 | time.cur = GetStartTime(); |
||
184 | |||
185 | if (time.old) { |
||
186 | time.gone = time.cur - time.old; |
||
187 | if (time.gone > 200) { |
||
188 | speed = downloader.httpd.content_received - netdata_received / time.gone * 100; |
||
189 | debugval("speed", speed); |
||
190 | debugln(ConvertSizeToKb(speed) ); |
||
191 | time.old = time.cur; |
||
192 | netdata_received = downloader.httpd.content_received; |
||
193 | } |
||
194 | } |
||
195 | else time.old = time.cur; |
||
196 | } |
||
7525 | leency | 197 | */ |
6374 | guillem | 198 | |
5519 | leency | 199 | void DrawDownloading() |
200 | { |
||
7281 | leency | 201 | char bytes_received[70]; |
8278 | leency | 202 | miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) ); |
203 | WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); |
||
7521 | leency | 204 | //CalculateSpeed(); |
7281 | leency | 205 | progressbar_draw stdcall(#pb); |
5493 | leency | 206 | } |
6374 | guillem | 207 | |
5493 | leency | 208 | void StopDownloading() |
209 | { |
||
7281 | leency | 210 | downloader.Stop(); |
211 | ed.blur_border_color = 0xFFFfff; |
||
212 | ed.flags = 10b; |
||
8278 | leency | 213 | DrawWindow(); |
7281 | leency | 214 | } |
215 | |||
216 | void SaveDownloadedFile() |
||
217 | { |
||
218 | int i; |
||
219 | char aux[2048]; |
||
220 | char notify_message[4296]; |
||
221 | |||
222 | // Clean all slashes at the end |
||
223 | strcpy(#aux, #downloader_edit); |
||
224 | while (aux[strlen(#aux)-1] == '/') { |
||
225 | aux[strlen(#aux)-1] = 0; |
||
226 | } |
||
8278 | leency | 227 | |
228 | //miniprintf(#filepath, "%s/", #save_to); |
||
229 | strcpy(#filepath, #save_to); |
||
230 | chrcat(#filepath, '/'); |
||
231 | strcat(#filepath, #aux+strrchr(#aux, '/')); |
||
7281 | leency | 232 | |
233 | for (i=0; i |
||
234 | |||
7765 | leency | 235 | if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { |
8278 | leency | 236 | miniprintf(#notify_message, FILE_SAVED_AS, #filepath); |
7765 | leency | 237 | } else { |
8278 | leency | 238 | miniprintf(#notify_message, FILE_NOT_SAVED, #filepath); |
7765 | leency | 239 | } |
8278 | leency | 240 | |
241 | /* |
||
242 | if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { |
||
243 | strcpy(#notify_message, "'Download complete' -Dt"); |
||
244 | } else { |
||
245 | strcpy(#notify_message, "'Error saving downloaded file!' -Et"); |
||
246 | } |
||
247 | */ |
||
7281 | leency | 248 | |
8278 | leency | 249 | if (!exit_when_done) notify(#notify_message); |
8281 | leency | 250 | if (autoclose.checked) ExitProcess(); |
6374 | guillem | 251 | }=> |