2020-11-29 22:08:52 +03:00
|
|
|
//Copyright 2020 by Leency
|
2018-05-20 17:03:53 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
#define MEMSIZE 1024 * 100
|
|
|
|
#include "../lib/gui.h"
|
|
|
|
#include "../lib/random.h"
|
2020-11-30 03:55:04 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
#include "../lib/obj/box_lib.h"
|
|
|
|
#include "../lib/obj/http.h"
|
2020-11-30 03:55:04 +03:00
|
|
|
#include "../lib/obj/libini.h"
|
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
#include "../lib/patterns/http_downloader.h"
|
2018-11-02 00:48:33 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
#include "const.h"
|
|
|
|
|
|
|
|
DOWNLOADER downloader;
|
2020-11-30 03:55:04 +03:00
|
|
|
checkbox autoclose = { T_AUTOCLOSE, true };
|
2020-11-29 22:08:52 +03:00
|
|
|
|
|
|
|
char downloader_edit[4000];
|
2015-12-22 21:14:35 +03:00
|
|
|
char filepath[4096];
|
2020-11-29 22:08:52 +03:00
|
|
|
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};
|
2020-11-30 03:55:04 +03:00
|
|
|
progress_bar pb = {0, GAPX, 58, 315, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
|
2017-09-10 08:38:29 +03:00
|
|
|
//progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2020-11-30 03:55:04 +03:00
|
|
|
bool exit_when_done = false;
|
2020-11-29 22:08:52 +03:00
|
|
|
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
void main()
|
2015-12-22 21:14:35 +03:00
|
|
|
{
|
2020-11-29 22:08:52 +03:00
|
|
|
dword shared_url;
|
|
|
|
load_dll(boxlib, #box_lib_init,0);
|
|
|
|
load_dll(libHTTP, #http_lib_init,1);
|
2020-11-30 03:55:04 +03:00
|
|
|
load_dll(libini, #lib_init,1);
|
2020-11-29 22:08:52 +03:00
|
|
|
|
2018-05-18 23:18:10 +03:00
|
|
|
if (!dir_exists(#save_to)) CreateDir(#save_to);
|
2017-09-10 08:38:29 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
if (param) {
|
|
|
|
if (!strncmp(#param, "-exit ", 6)) {
|
|
|
|
exit_when_done = true;
|
|
|
|
param += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(#param, "-mem", 5)) {
|
2020-11-30 03:55:04 +03:00
|
|
|
//shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
|
2020-11-29 22:08:52 +03:00
|
|
|
strcpy(#downloader_edit, shared_url);
|
|
|
|
} else {
|
|
|
|
strcpy(#downloader_edit, #param);
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 23:18:10 +03:00
|
|
|
if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
|
|
|
|
ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
|
|
|
|
@SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
|
|
|
|
loop() switch(@WaitEvent())
|
2018-05-18 23:18:10 +03:00
|
|
|
{
|
|
|
|
case evMouse:
|
|
|
|
edit_box_mouse stdcall (#ed);
|
|
|
|
break;
|
2017-09-17 04:32:09 +03:00
|
|
|
|
2018-05-18 23:18:10 +03:00
|
|
|
case evButton:
|
2020-11-29 22:08:52 +03:00
|
|
|
ProcessEvent(GetButtonID());
|
2018-05-18 23:18:10 +03:00
|
|
|
break;
|
2017-09-17 04:32:09 +03:00
|
|
|
|
2018-05-18 23:18:10 +03:00
|
|
|
case evKey:
|
|
|
|
GetKeys();
|
|
|
|
edit_box_key stdcall(#ed);
|
2020-11-29 22:08:52 +03:00
|
|
|
if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301);
|
2018-05-18 23:18:10 +03:00
|
|
|
break;
|
2017-09-17 04:32:09 +03:00
|
|
|
|
2018-05-18 23:18:10 +03:00
|
|
|
case evReDraw:
|
2020-11-29 22:08:52 +03:00
|
|
|
DrawWindow();
|
2018-05-18 23:18:10 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!downloader.MonitorProgress()) break;
|
2018-11-04 03:20:00 +03:00
|
|
|
pb.max = downloader.httpd.content_length / 100;
|
|
|
|
EDI = downloader.httpd.content_received/100;
|
|
|
|
if (pb.value != EDI)
|
2018-05-18 23:18:10 +03:00
|
|
|
{
|
2018-11-04 03:20:00 +03:00
|
|
|
pb.value = EDI;
|
2018-05-18 23:18:10 +03:00
|
|
|
progressbar_draw stdcall(#pb);
|
|
|
|
DrawDownloading();
|
|
|
|
}
|
|
|
|
if (downloader.state == STATE_COMPLETED)
|
|
|
|
{
|
|
|
|
SaveDownloadedFile();
|
2020-11-29 22:08:52 +03:00
|
|
|
if (exit_when_done) ExitProcess();
|
2018-05-18 23:18:10 +03:00
|
|
|
StopDownloading();
|
2020-11-29 22:08:52 +03:00
|
|
|
DrawWindow();
|
2018-05-18 23:18:10 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 21:14:35 +03:00
|
|
|
}
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
void ProcessEvent(int id)
|
2015-12-22 21:14:35 +03:00
|
|
|
{
|
2020-11-30 03:55:04 +03:00
|
|
|
autoclose.click(id);
|
2020-11-29 22:08:52 +03:00
|
|
|
if (id==001) { StopDownloading(); ExitProcess(); }
|
2018-05-18 23:18:10 +03:00
|
|
|
if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
|
|
|
|
if (id==302) StopDownloading();
|
2020-11-29 22:08:52 +03:00
|
|
|
if (id==305) RunProgram("/sys/File managers/Eolite", #filepath);
|
2018-05-18 23:18:10 +03:00
|
|
|
if (id==306) {
|
|
|
|
SetCurDir(#save_to);
|
|
|
|
RunProgram("/sys/@open", #filepath);
|
|
|
|
}
|
2015-12-22 21:14:35 +03:00
|
|
|
}
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
void DrawWindow()
|
2016-03-25 18:35:48 +03:00
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
int but_x = 0;
|
2020-11-30 03:55:04 +03:00
|
|
|
#define BUT_Y 58;
|
2020-11-29 22:08:52 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-11-30 03:55:04 +03:00
|
|
|
autoclose.draw(WIN_W-135, BUT_Y+6);
|
|
|
|
|
2018-05-18 23:18:10 +03:00
|
|
|
if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
|
|
|
|
{
|
2020-11-30 03:55:04 +03:00
|
|
|
but_x = GAPX + DrawStandartCaptButton(GAPX, BUT_Y, 301, START_DOWNLOADING);
|
2018-05-18 23:18:10 +03:00
|
|
|
if (filepath[0])
|
|
|
|
{
|
2020-11-30 03:55:04 +03:00
|
|
|
but_x += DrawStandartCaptButton(but_x, BUT_Y, 305, SHOW_IN_FOLDER);
|
|
|
|
DrawStandartCaptButton(but_x, BUT_Y, 306, OPEN_FILE_TEXT);
|
2018-05-18 23:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (downloader.state == STATE_IN_PROGRESS)
|
|
|
|
{
|
2020-11-30 03:55:04 +03:00
|
|
|
DrawStandartCaptButton(WIN_W - 240, BUT_Y, 302, STOP_DOWNLOADING);
|
2018-05-18 23:18:10 +03:00
|
|
|
DrawDownloading();
|
|
|
|
}
|
|
|
|
ed.offset=0;
|
|
|
|
DrawEditBox(#ed);
|
2015-12-22 21:14:35 +03:00
|
|
|
}
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2015-12-22 21:14:35 +03:00
|
|
|
void StartDownloading()
|
|
|
|
{
|
2020-11-21 16:06:10 +03:00
|
|
|
char http_url[URL_SIZE];
|
|
|
|
char proxy_url[URL_SIZE];
|
2018-05-18 23:18:10 +03:00
|
|
|
StopDownloading();
|
2020-11-21 16:06:10 +03:00
|
|
|
if (!strncmp(#downloader_edit,"https://",7)) {
|
|
|
|
notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
|
2020-11-29 22:08:52 +03:00
|
|
|
miniprintf(#http_url, "http://%s", #downloader_edit+8);
|
2020-11-21 16:06:10 +03:00
|
|
|
if (!downloader.Start(#http_url)) {
|
|
|
|
notify("'Download failed.' -E");
|
|
|
|
StopDownloading();
|
|
|
|
}
|
|
|
|
//sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
|
|
|
|
//if (!downloader.Start(#proxy_url)) {
|
|
|
|
// notify("'Download failed.' -E");
|
|
|
|
// StopDownloading();
|
|
|
|
//}
|
2020-11-29 22:08:52 +03:00
|
|
|
DrawWindow();
|
2018-05-18 23:18:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!downloader.Start(#downloader_edit)) {
|
2020-11-29 22:08:52 +03:00
|
|
|
if (exit_when_done) ExitProcess();
|
|
|
|
notify(T_ERROR_STARTING_DOWNLOAD);
|
2018-05-18 23:18:10 +03:00
|
|
|
StopDownloading();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ed.blur_border_color = 0xCACACA;
|
|
|
|
ed.flags = 100000000000b;
|
|
|
|
pb.value = 0;
|
2020-11-29 22:08:52 +03:00
|
|
|
DrawWindow();
|
2015-12-22 21:14:35 +03:00
|
|
|
}
|
2018-11-02 00:48:33 +03:00
|
|
|
|
2018-11-04 03:20:00 +03:00
|
|
|
/*
|
2018-11-02 00:48:33 +03:00
|
|
|
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 = downloader.httpd.content_received - netdata_received / time.gone * 100;
|
|
|
|
debugval("speed", speed);
|
|
|
|
debugln(ConvertSizeToKb(speed) );
|
|
|
|
time.old = time.cur;
|
|
|
|
netdata_received = downloader.httpd.content_received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else time.old = time.cur;
|
|
|
|
}
|
2018-11-04 03:20:00 +03:00
|
|
|
*/
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2015-12-22 21:14:35 +03:00
|
|
|
void DrawDownloading()
|
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
char bytes_received[70];
|
2020-11-29 22:08:52 +03:00
|
|
|
miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
|
|
|
|
WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
|
2018-11-02 00:48:33 +03:00
|
|
|
//CalculateSpeed();
|
2018-05-18 23:18:10 +03:00
|
|
|
progressbar_draw stdcall(#pb);
|
2015-12-22 21:14:35 +03:00
|
|
|
}
|
2016-03-25 18:35:48 +03:00
|
|
|
|
2015-12-22 21:14:35 +03:00
|
|
|
void StopDownloading()
|
|
|
|
{
|
2018-05-18 23:18:10 +03:00
|
|
|
downloader.Stop();
|
|
|
|
ed.blur_border_color = 0xFFFfff;
|
|
|
|
ed.flags = 10b;
|
2020-11-29 22:08:52 +03:00
|
|
|
DrawWindow();
|
2018-05-18 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveDownloadedFile()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char aux[2048];
|
|
|
|
char notify_message[4296];
|
|
|
|
|
|
|
|
// Clean all slashes at the end
|
|
|
|
strcpy(#aux, #downloader_edit);
|
|
|
|
while (aux[strlen(#aux)-1] == '/') {
|
|
|
|
aux[strlen(#aux)-1] = 0;
|
|
|
|
}
|
2020-11-29 22:08:52 +03:00
|
|
|
|
|
|
|
//miniprintf(#filepath, "%s/", #save_to);
|
|
|
|
strcpy(#filepath, #save_to);
|
|
|
|
chrcat(#filepath, '/');
|
|
|
|
strcat(#filepath, #aux+strrchr(#aux, '/'));
|
2018-05-18 23:18:10 +03:00
|
|
|
|
|
|
|
for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
|
|
|
|
|
2020-04-05 21:28:02 +03:00
|
|
|
if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
|
2020-11-29 22:08:52 +03:00
|
|
|
miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
|
|
|
|
} else {
|
|
|
|
miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
|
|
|
|
strcpy(#notify_message, "'Download complete' -Dt");
|
2020-04-05 21:28:02 +03:00
|
|
|
} else {
|
2020-11-29 22:08:52 +03:00
|
|
|
strcpy(#notify_message, "'Error saving downloaded file!' -Et");
|
2020-04-05 21:28:02 +03:00
|
|
|
}
|
2020-11-29 22:08:52 +03:00
|
|
|
*/
|
2018-05-18 23:18:10 +03:00
|
|
|
|
2020-11-29 22:08:52 +03:00
|
|
|
if (!exit_when_done) notify(#notify_message);
|
2020-11-30 03:55:04 +03:00
|
|
|
if (autoclose.checked) ExitProcess();
|
2016-03-25 18:35:48 +03:00
|
|
|
}
|