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