Rev 8229 | Rev 8281 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8229 | Rev 8278 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | //Copyright 2020 by Leency |
|
- | 2 | ||
- | 3 | #define MEMSIZE 1024 * 100 |
|
- | 4 | #include "../lib/gui.h" |
|
1 | DOWNLOADER downloader; |
5 | #include "../lib/random.h" |
- | 6 | #include "../lib/obj/box_lib.h" |
|
- | 7 | #include "../lib/obj/http.h" |
|
- | 8 | #include "../lib/patterns/http_downloader.h" |
|
Line 2... | Line 9... | ||
2 | 9 | ||
3 | #ifdef LANG_RUS |
- | |
4 | #define DL_WINDOW_HEADER "¥¥¤¦¥à § £à㧮ª" |
- | |
5 | #define START_DOWNLOADING " ç âì § ª çªã" |
- | |
6 | #define STOP_DOWNLOADING "áâ ®¢¨âì" |
- | |
7 | #define SHOW_IN_FOLDER "®ª § âì ¢ ¯ ¯ª¥" |
- | |
8 | #define OPEN_FILE_TEXT "âªàëâì ä ©«" |
- | |
9 | #define FILE_SAVED_AS "'¥¥¤¦¥à § £à㧮ª\n ©« á®åà ¥ ª ª " |
- | |
10 | #define KB_RECEIVED "¤¥â ᪠稢 ¨¥... %s ¯®«ã祮" |
- | |
11 | #else |
- | |
12 | #define DL_WINDOW_HEADER "Download Manager" |
- | |
13 | #define START_DOWNLOADING "Start downloading" |
- | |
14 | #define STOP_DOWNLOADING "Stop downloading" |
- | |
15 | #define SHOW_IN_FOLDER "Show in folder" |
- | |
16 | #define OPEN_FILE_TEXT "Open file" |
- | |
17 | #define FILE_SAVED_AS "'Download manager\nFile saved as " |
- | |
18 | #define KB_RECEIVED "Downloading... %s received" |
- | |
19 | #endif |
- | |
Line 20... | Line 10... | ||
20 | char save_to[4096] = "/tmp0/1/Downloads"; |
10 | #include "const.h" |
Line 21... | Line -... | ||
21 | - | ||
22 | #define CONX 15 |
11 | |
23 | 12 | DOWNLOADER downloader; |
|
24 | proc_info DL_Form; |
13 | |
25 | char downloader_edit[10000]; |
14 | char downloader_edit[4000]; |
26 | char filepath[4096]; |
15 | char filepath[4096]; |
27 | edit_box ed = {NULL,57,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19}; |
16 | edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000, |
Line 28... | Line 17... | ||
28 | progress_bar pb = {0, CONX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; |
17 | sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19}; |
29 | //progress_bar pb = {0, 180, 55, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; |
- | |
Line 30... | Line -... | ||
30 | //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color; |
- | |
Line 31... | Line 18... | ||
31 | 18 | progress_bar pb = {0, GAPX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; |
|
32 | bool downloader_opened; |
19 | //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color; |
33 | char downloader_stak[4096]; |
20 | |
34 | 21 | bool exit_when_done = false; |
|
35 | bool download_and_exit = false; |
22 | |
Line 36... | Line -... | ||
36 | - | ||
37 | void Downloader() |
23 | |
Line -... | Line 24... | ||
- | 24 | void main() |
|
- | 25 | { |
|
38 | { |
26 | dword shared_url; |
- | 27 | load_dll(boxlib, #box_lib_init,0); |
|
- | 28 | load_dll(libHTTP, #http_lib_init,1); |
|
Line -... | Line 29... | ||
- | 29 | ||
- | 30 | if (!dir_exists(#save_to)) CreateDir(#save_to); |
|
- | 31 | ||
- | 32 | if (param) { |
|
39 | if (!dir_exists(#save_to)) CreateDir(#save_to); |
33 | if (!strncmp(#param, "-exit ", 6)) { |
- | 34 | exit_when_done = true; |
|
- | 35 | param += 6; |
|
40 | downloader_opened = true; |
36 | } |
41 | SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); |
37 | |
Line -... | Line 38... | ||
- | 38 | if (!strncmp(#param, "-mem", 5)) { |
|
- | 39 | shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE); |
|
42 | 40 | strcpy(#downloader_edit, shared_url); |
|
43 | sc.get(); |
41 | } else { |
44 | pb.frame_color = sc.work_dark; |
42 | strcpy(#downloader_edit, #param); |
45 | 43 | } |
|
46 | filepath[0] = NULL; |
44 | } |
Line 47... | Line 45... | ||
47 | 45 | if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); |
|
48 | downloader.Stop(); |
46 | ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); |
49 | if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); |
47 | |
Line 50... | Line 48... | ||
50 | ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); |
48 | @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); |
51 | 49 | @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP); |
|
52 | loop() switch(WaitEvent()) |
50 | loop() switch(@WaitEvent()) |
53 | { |
51 | { |
54 | case evMouse: |
52 | case evMouse: |
Line 55... | Line 53... | ||
55 | edit_box_mouse stdcall (#ed); |
53 | edit_box_mouse stdcall (#ed); |
56 | break; |
- | |
57 | - | ||
58 | case evButton: |
- | |
59 | Key_Scan(GetButtonID()); |
- | |
60 | break; |
- | |
61 | 54 | break; |
|
62 | case evKey: |
55 | |
Line 63... | Line 56... | ||
63 | GetKeys(); |
56 | case evButton: |
64 | edit_box_key stdcall(#ed); |
57 | ProcessEvent(GetButtonID()); |
65 | if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301); |
58 | break; |
Line 85... | Line 78... | ||
85 | DrawDownloading(); |
78 | DrawDownloading(); |
86 | } |
79 | } |
87 | if (downloader.state == STATE_COMPLETED) |
80 | if (downloader.state == STATE_COMPLETED) |
88 | { |
81 | { |
89 | SaveDownloadedFile(); |
82 | SaveDownloadedFile(); |
90 | if (download_and_exit) ExitProcess(); |
83 | if (exit_when_done) ExitProcess(); |
91 | StopDownloading(); |
84 | StopDownloading(); |
92 | DL_Draw_Window(); |
85 | DrawWindow(); |
93 | break; |
86 | break; |
94 | } |
87 | } |
95 | } |
88 | } |
96 | } |
89 | } |
Line 97... | Line 90... | ||
97 | 90 | ||
98 | void Key_Scan(int id) |
91 | void ProcessEvent(int id) |
99 | { |
92 | { |
100 | if (id==001) { downloader_opened=false; StopDownloading(); ExitProcess(); } |
93 | if (id==001) { StopDownloading(); ExitProcess(); } |
101 | if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading(); |
94 | if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading(); |
102 | if (id==302) StopDownloading(); |
95 | if (id==302) StopDownloading(); |
103 | if (id==305) RunProgram("/sys/File managers/Eolite", #save_to); |
96 | if (id==305) RunProgram("/sys/File managers/Eolite", #filepath); |
104 | if (id==306) { |
97 | if (id==306) { |
105 | SetCurDir(#save_to); |
98 | SetCurDir(#save_to); |
106 | RunProgram("/sys/@open", #filepath); |
99 | RunProgram("/sys/@open", #filepath); |
107 | } |
100 | } |
Line 108... | Line 101... | ||
108 | } |
101 | } |
109 | 102 | ||
110 | void DL_Draw_Window() |
103 | void DrawWindow() |
111 | { |
104 | { |
112 | int but_x = 0; |
- | |
- | 105 | int but_x = 0; |
|
113 | int but_y = 58; |
106 | int but_y = 58; |
114 | DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, sc.work); |
107 | |
115 | DeleteButton(301); |
108 | sc.get(); |
116 | DeleteButton(302); |
- | |
- | 109 | pb.frame_color = sc.work_dark; |
|
117 | DeleteButton(305); |
110 | DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); |
118 | DeleteButton(306); |
111 | |
119 | if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) |
112 | if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) |
120 | { |
113 | { |
121 | but_x = CONX + DrawStandartCaptButton(CONX, but_y, 301, START_DOWNLOADING); |
114 | but_x = GAPX + DrawStandartCaptButton(GAPX, but_y, 301, START_DOWNLOADING); |
122 | if (filepath[0]) |
115 | if (filepath[0]) |
123 | { |
116 | { |
124 | but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER); |
117 | but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER); |
125 | DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT); |
118 | DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT); |
126 | } |
119 | } |
127 | } |
120 | } |
128 | if (downloader.state == STATE_IN_PROGRESS) |
121 | if (downloader.state == STATE_IN_PROGRESS) |
129 | { |
122 | { |
130 | DrawStandartCaptButton(DL_Form.width - 190, but_y, 302, STOP_DOWNLOADING); |
123 | DrawStandartCaptButton(WIN_W - 190, but_y, 302, STOP_DOWNLOADING); |
131 | DrawDownloading(); |
- | |
132 | } |
- | |
133 | WriteText(CONX, ed.top + 4, 0x90, sc.work_text, "URL:"); |
124 | DrawDownloading(); |
134 | ed.width = DL_Form.cwidth - ed.left - CONX - 3; |
125 | } |
135 | ed.offset=0; |
126 | ed.offset=0; |
Line 136... | Line 127... | ||
136 | DrawEditBox(#ed); |
127 | DrawEditBox(#ed); |
Line 141... | Line 132... | ||
141 | char http_url[URL_SIZE]; |
132 | char http_url[URL_SIZE]; |
142 | char proxy_url[URL_SIZE]; |
133 | char proxy_url[URL_SIZE]; |
143 | StopDownloading(); |
134 | StopDownloading(); |
144 | if (!strncmp(#downloader_edit,"https://",7)) { |
135 | if (!strncmp(#downloader_edit,"https://",7)) { |
145 | notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); |
136 | notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); |
146 | sprintf(#http_url, "http://%s", #downloader_edit+8); |
137 | miniprintf(#http_url, "http://%s", #downloader_edit+8); |
147 | if (!downloader.Start(#http_url)) { |
138 | if (!downloader.Start(#http_url)) { |
148 | notify("'Download failed.' -E"); |
139 | notify("'Download failed.' -E"); |
149 | StopDownloading(); |
140 | StopDownloading(); |
150 | } |
141 | } |
151 | //sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit); |
142 | //sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit); |
152 | //if (!downloader.Start(#proxy_url)) { |
143 | //if (!downloader.Start(#proxy_url)) { |
153 | // notify("'Download failed.' -E"); |
144 | // notify("'Download failed.' -E"); |
154 | // StopDownloading(); |
145 | // StopDownloading(); |
155 | //} |
146 | //} |
156 | DL_Draw_Window(); |
147 | DrawWindow(); |
157 | return; |
148 | return; |
158 | } |
149 | } |
159 | if (!downloader.Start(#downloader_edit)) { |
150 | if (!downloader.Start(#downloader_edit)) { |
160 | if (download_and_exit) ExitProcess(); |
151 | if (exit_when_done) ExitProcess(); |
161 | notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E"); |
152 | notify(T_ERROR_STARTING_DOWNLOAD); |
162 | StopDownloading(); |
153 | StopDownloading(); |
163 | return; |
154 | return; |
164 | } |
155 | } |
165 | ed.blur_border_color = 0xCACACA; |
156 | ed.blur_border_color = 0xCACACA; |
166 | ed.flags = 100000000000b; |
157 | ed.flags = 100000000000b; |
167 | pb.value = 0; |
158 | pb.value = 0; |
168 | DL_Draw_Window(); |
159 | DrawWindow(); |
169 | } |
160 | } |
Line 170... | Line 161... | ||
170 | 161 | ||
171 | /* |
162 | /* |
172 | struct TIME |
163 | struct TIME |
Line 198... | Line 189... | ||
198 | */ |
189 | */ |
Line 199... | Line 190... | ||
199 | 190 | ||
200 | void DrawDownloading() |
191 | void DrawDownloading() |
201 | { |
192 | { |
202 | char bytes_received[70]; |
193 | char bytes_received[70]; |
203 | sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) ); |
- | |
204 | DrawBar(CONX, pb.top + 22, pb.width, 16, sc.work); |
194 | miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) ); |
205 | WriteText(CONX, pb.top + 22, 0x90, sc.work_text, #bytes_received); |
195 | WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); |
206 | //CalculateSpeed(); |
196 | //CalculateSpeed(); |
207 | progressbar_draw stdcall(#pb); |
197 | progressbar_draw stdcall(#pb); |
Line 208... | Line 198... | ||
208 | } |
198 | } |
209 | 199 | ||
210 | void StopDownloading() |
200 | void StopDownloading() |
211 | { |
201 | { |
212 | downloader.Stop(); |
202 | downloader.Stop(); |
213 | ed.blur_border_color = 0xFFFfff; |
203 | ed.blur_border_color = 0xFFFfff; |
214 | ed.flags = 10b; |
204 | ed.flags = 10b; |
Line 215... | Line 205... | ||
215 | DL_Draw_Window(); |
205 | DrawWindow(); |
216 | } |
206 | } |
217 | 207 | ||
Line 224... | Line 214... | ||
224 | // Clean all slashes at the end |
214 | // Clean all slashes at the end |
225 | strcpy(#aux, #downloader_edit); |
215 | strcpy(#aux, #downloader_edit); |
226 | while (aux[strlen(#aux)-1] == '/') { |
216 | while (aux[strlen(#aux)-1] == '/') { |
227 | aux[strlen(#aux)-1] = 0; |
217 | aux[strlen(#aux)-1] = 0; |
228 | } |
218 | } |
- | 219 | ||
- | 220 | //miniprintf(#filepath, "%s/", #save_to); |
|
- | 221 | strcpy(#filepath, #save_to); |
|
- | 222 | chrcat(#filepath, '/'); |
|
229 | sprintf(#filepath, "%s/%s", #save_to, #aux+strrchr(#aux, '/')); |
223 | strcat(#filepath, #aux+strrchr(#aux, '/')); |
Line 230... | Line 224... | ||
230 | 224 | ||
Line 231... | Line 225... | ||
231 | for (i=0; i |
225 | for (i=0; i |
232 | 226 | ||
233 | if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { |
227 | if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { |
234 | sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt"); |
228 | miniprintf(#notify_message, FILE_SAVED_AS, #filepath); |
235 | } else { |
229 | } else { |
Line -... | Line 230... | ||
- | 230 | miniprintf(#notify_message, FILE_NOT_SAVED, #filepath); |
|
- | 231 | } |
|
- | 232 | ||
- | 233 | /* |
|
- | 234 | if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) { |
|
- | 235 | strcpy(#notify_message, "'Download complete' -Dt"); |
|
- | 236 | } else { |
|
- | 237 | strcpy(#notify_message, "'Error saving downloaded file!' -Et"); |
|
236 | sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et"); |
238 | } |
237 | } |
239 | */ |
238 | 240 |