1,27 → 1,25 |
#define MEMSIZE 1024 * 100 |
//Copyright 2020 by Leency |
|
#define MEMSIZE 1024 * 100 |
#include "../lib/gui.h" |
#include "../lib/random.h" |
|
#include "../lib/obj/box_lib.h" |
#include "../lib/obj/http.h" |
|
#include "../lib/patterns/http_downloader.h" |
|
#include "const.h" |
|
DOWNLOADER downloader; |
dword bufpointer, bufsize; |
bool exit_param = false; |
|
_http http; |
|
checkbox autoclose = { T_AUTOCLOSE, false }; |
|
char downloader_edit[4000]; |
char filepath[4096]; |
edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000, |
sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19}; |
progress_bar pb = {0, GAPX, 58, 315, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; |
//progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color; |
char main_url[URL_SIZE]; |
char filepath[URL_SIZE+96]; |
|
bool exit_when_done = false; |
progress_bar pb = {0, GAPX, 58, 315, 17, 0, NULL, NULL, 0xFFFfff, 0x74DA00, NULL}; |
edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff, |
0x10000000, sizeof(main_url)-2,#main_url,0,ed_focus,19,19}; |
|
|
void main() |
31,199 → 29,189 |
load_dll(libHTTP, #http_lib_init,1); |
|
if (!dir_exists(#save_to)) CreateDir(#save_to); |
SetCurDir(#save_to); |
|
if (param) { |
if (!strncmp(#param, "-exit ", 6)) { |
exit_when_done = true; |
exit_param = true; |
param += 6; |
} |
|
if (!strncmp(#param, "-mem", 5)) { |
//shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE); |
strcpy(#downloader_edit, shared_url); |
strcpy(#main_url, shared_url); |
} else { |
strcpy(#downloader_edit, #param); |
strcpy(#main_url, #param); |
} |
} |
if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); |
ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); |
if (main_url[0]) StartDownloading(); else strcpy(#main_url, "http://"); |
EditBox_UpdateText(#ed, #main_url); |
|
@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); |
@SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP); |
loop() switch(@WaitEvent()) |
{ |
case evMouse: |
edit_box_mouse stdcall (#ed); |
break; |
|
case evButton: |
ProcessEvent(GetButtonID()); |
break; |
|
case evKey: |
GetKeys(); |
edit_box_key stdcall(#ed); |
if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301); |
break; |
|
case evReDraw: |
DrawWindow(); |
break; |
|
default: |
if (!downloader.MonitorProgress()) break; |
pb.max = downloader.content_length / 100; |
EDI = downloader.content_received/100; |
if (pb.value != EDI) |
{ |
pb.value = EDI; |
progressbar_draw stdcall(#pb); |
DrawDownloading(); |
case evMouse: edit_box_mouse stdcall (#ed); break; |
case evButton: ProcessButtonClick(); break; |
case evKey: ProcessKeyPress(); break; |
case evReDraw: DrawWindow(); break; |
default: MonitorProgress(); |
} |
if (downloader.state == STATE_COMPLETED) |
{ |
SaveDownloadedFile(); |
if (exit_when_done) ExitProcess(); |
StopDownloading(); |
DrawWindow(); |
break; |
} |
} |
} |
|
void ProcessEvent(int id) |
void ProcessButtonClick() |
{ |
int id = @GetButtonID(); |
autoclose.click(id); |
if (id==001) { StopDownloading(); ExitProcess(); } |
if (id==301) && (downloader.transfer <= 0) StartDownloading(); |
if (id==302) StopDownloading(); |
if (id==305) RunProgram("/sys/File managers/Eolite", #filepath); |
if (id==306) { |
SetCurDir(#save_to); |
RunProgram("/sys/@open", #filepath); |
if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); } |
if (id==BTN_START) StartDownloading(); |
if (id==BTN_STOP) StopDownloading(); |
if (id==BTN_DIR) RunProgram("/sys/File managers/Eolite", #filepath); |
if (id==BTN_RUN) RunProgram("/sys/@open", #filepath); |
} |
|
void ProcessKeyPress() |
{ |
@GetKey(); |
edit_box_key stdcall(#ed); |
EAX >>= 16; |
if (AL == SCAN_CODE_ENTER) StartDownloading(); |
if (AL == SCAN_CODE_ESC) StopDownloading(); |
} |
|
void DrawWindow() |
{ |
int but_x = 0; |
#define BUT_Y 58; |
|
sc.get(); |
pb.frame_color = sc.work_dark; |
DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); |
DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, |
WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0); |
|
autoclose.draw(WIN_W-135, BUT_Y+6); |
|
if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) |
#define BUT_Y 58; |
//autoclose.draw(WIN_W-135, BUT_Y+6); |
if (!http.transfer) |
{ |
but_x = GAPX + DrawStandartCaptButton(GAPX, BUT_Y, 301, START_DOWNLOADING); |
if (filepath[0]) |
DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD); |
if (filepath) |
{ |
but_x += DrawStandartCaptButton(but_x, BUT_Y, 305, SHOW_IN_FOLDER); |
DrawStandartCaptButton(but_x, BUT_Y, 306, OPEN_FILE_TEXT); |
DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR); |
DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN); |
} |
} else { |
DrawStandartCaptButton(WIN_W - 240, BUT_Y, BTN_STOP, T_CANCEL); |
DrawDownloadingProgress(); |
} |
if (downloader.state == STATE_IN_PROGRESS) |
{ |
DrawStandartCaptButton(WIN_W - 240, BUT_Y, 302, STOP_DOWNLOADING); |
DrawDownloading(); |
} |
ed.offset=0; |
//ed.offset=0; //DEL? |
DrawEditBox(#ed); |
} |
|
void StartDownloading() |
{ |
char http_url[URL_SIZE]; |
char proxy_url[URL_SIZE]; |
StopDownloading(); |
if (!strncmp(#downloader_edit,"https://",7)) { |
if (http.transfer > 0) return; |
filepath = '\0'; |
if (!strncmp(#main_url,"https:",6)) { |
miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #main_url); |
strcpy(#main_url, #proxy_url); |
EditBox_UpdateText(#ed, #main_url); |
//notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); |
//miniprintf(#http_url, "http://%s", #downloader_edit+8); |
//miniprintf(#http_url, "http://%s", #main_url+8); |
//if (!downloader.Start(#http_url)) { |
// notify("'Download failed.' -E"); |
// StopDownloading(); |
//} |
miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit); |
if (!downloader.Start(#proxy_url)) { |
notify("'Download failed.' -E"); |
StopDownloading(); |
} |
DrawWindow(); |
return; |
} |
if (!downloader.Start(#downloader_edit)) { |
if (exit_when_done) ExitProcess(); |
notify(T_ERROR_STARTING_DOWNLOAD); |
StopDownloading(); |
return; |
} |
if (http.get(#main_url)) { |
ed.blur_border_color = 0xCACACA; |
ed.flags = 100000000000b; |
pb.value = 0; |
DrawWindow(); |
} else { |
notify(T_ERROR_STARTING_DOWNLOAD); |
StopDownloading(); |
if (exit_param) ExitProcess(); |
} |
} |
|
/* |
struct TIME |
{ |
dword old; |
dword cur; |
dword gone; |
} time = {0,0,0}; |
|
dword netdata_received; |
dword speed; |
|
void CalculateSpeed() |
void DrawDownloadingProgress() |
{ |
time.cur = GetStartTime(); |
char bytes_received[70]; |
|
if (time.old) { |
time.gone = time.cur - time.old; |
if (time.gone > 200) { |
speed = downloader.content_received - netdata_received / time.gone * 100; |
debugval("speed", speed); |
debugln(ConvertSizeToKb(speed) ); |
time.old = time.cur; |
netdata_received = downloader.content_received; |
} |
} |
else time.old = time.cur; |
} |
*/ |
EDI = http.content_received / 100; |
if (pb.value == EDI) return; |
|
void DrawDownloading() |
{ |
char bytes_received[70]; |
miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.content_received) ); |
pb.value = EDI; |
pb.max = http.content_length / 100; |
progressbar_draw stdcall(#pb); |
miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(http.content_received) ); |
WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); |
//CalculateSpeed(); |
progressbar_draw stdcall(#pb); |
} |
|
void StopDownloading() |
{ |
downloader.Stop(); |
if (http.stop()) { |
bufsize = 0; |
bufpointer = free(bufpointer); |
} |
http.content_received = http.content_length = 0; |
|
ed.blur_border_color = 0xFFFfff; |
ed.flags = 10b; |
DrawWindow(); |
} |
|
void SaveDownloadedFile() |
void MonitorProgress() |
{ |
char redirect_url[URL_SIZE]; |
if (http.transfer <= 0) return; |
http.receive(); |
if (!http.content_length) http.content_length = http.content_received * 20; //MOVE? |
|
if (http.receive_result == 0) { |
if (http.status_code >= 300) && (http.status_code < 400) |
{ |
http.header_field("location\0", #redirect_url, URL_SIZE); |
get_absolute_url(#redirect_url, #main_url); |
strcpy(#main_url, #redirect_url); |
EditBox_UpdateText(#ed, #main_url); |
StopDownloading(); |
StartDownloading(); |
return; |
} |
bufpointer = http.content_pointer; |
bufsize = http.content_received; |
http.hfree(); |
} |
|
DrawDownloadingProgress(); |
|
if (!http.receive_result) |
{ |
SaveFile(); |
if (exit_param) ExitProcess(); |
StopDownloading(); |
DrawWindow(); |
return; |
} |
} |
|
void SaveFile() |
{ |
int i; |
char aux[2048]; |
char notify_message[4296]; |
|
//char file_name[URL_SIZE]; |
//Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip" |
//header_field("Content-Disposition\0", #redirect_url, URL_SIZE); |
|
// Clean all slashes at the end |
strcpy(#aux, #downloader_edit); |
strcpy(#aux, #main_url); |
while (aux[strlen(#aux)-1] == '/') { |
aux[strlen(#aux)-1] = 0; |
} |
|
//miniprintf(#filepath, "%s/", #save_to); |
strcpy(#filepath, #save_to); |
chrcat(#filepath, '/'); |
strcat(#filepath, #aux+strrchr(#aux, '/')); |
230,20 → 218,42 |
|
for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-'; |
|
if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) { |
if (CreateFile(http.content_received, bufpointer, #filepath)==0) { |
miniprintf(#notify_message, FILE_SAVED_AS, #filepath); |
} else { |
miniprintf(#notify_message, FILE_NOT_SAVED, #filepath); |
} |
|
if (!exit_param) notify(#notify_message); |
if (autoclose.checked) ExitProcess(); |
} |
|
|
/* |
if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) { |
strcpy(#notify_message, "'Download complete' -Dt"); |
} else { |
strcpy(#notify_message, "'Error saving downloaded file!' -Et"); |
struct TIME |
{ |
dword old; |
dword cur; |
dword gone; |
} time = {0,0,0}; |
|
dword netdata_received; |
dword speed; |
|
void CalculateSpeed() |
{ |
time.cur = GetStartTime(); |
|
if (time.old) { |
time.gone = time.cur - time.old; |
if (time.gone > 200) { |
speed = http.content_received - netdata_received / time.gone * 100; |
debugval("speed", speed); |
debugln(ConvertSizeToKb(speed) ); |
time.old = time.cur; |
netdata_received = http.content_received; |
} |
*/ |
|
if (!exit_when_done) notify(#notify_message); |
if (autoclose.checked) ExitProcess(); |
} |
} |
else time.old = time.cur; |
} |
*/ |