2020-12-15 16:39:54 +01:00
|
|
|
#define MEMSIZE 1024 * 40
|
2020-12-03 03:56:54 +01:00
|
|
|
//Copyright 2020 by Leency
|
2020-11-29 20:08:52 +01:00
|
|
|
#include "../lib/gui.h"
|
|
|
|
#include "../lib/random.h"
|
|
|
|
#include "../lib/obj/box_lib.h"
|
|
|
|
#include "../lib/obj/http.h"
|
2020-11-30 01:55:04 +01:00
|
|
|
|
2020-11-29 20:08:52 +01:00
|
|
|
#include "const.h"
|
|
|
|
|
2020-12-03 03:56:54 +01:00
|
|
|
bool exit_param = false;
|
2020-12-15 16:39:54 +01:00
|
|
|
bool open_file = false;
|
2020-12-03 03:56:54 +01:00
|
|
|
|
|
|
|
_http http;
|
|
|
|
|
2020-12-01 18:37:47 +01:00
|
|
|
checkbox autoclose = { T_AUTOCLOSE, false };
|
2020-11-29 20:08:52 +01:00
|
|
|
|
2020-12-09 14:29:38 +01:00
|
|
|
char uEdit[URL_SIZE];
|
2020-12-03 03:56:54 +01:00
|
|
|
char filepath[URL_SIZE+96];
|
|
|
|
|
|
|
|
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,
|
2020-12-09 14:29:38 +01:00
|
|
|
0x10000000, sizeof(uEdit)-2,#uEdit,0,ed_focus,19,19};
|
2020-12-03 03:56:54 +01:00
|
|
|
|
2020-11-29 20:08:52 +01:00
|
|
|
|
|
|
|
void main()
|
2015-12-22 19:14:35 +01:00
|
|
|
{
|
2020-11-29 20:08:52 +01:00
|
|
|
dword shared_url;
|
|
|
|
load_dll(boxlib, #box_lib_init,0);
|
|
|
|
load_dll(libHTTP, #http_lib_init,1);
|
|
|
|
|
2020-12-04 12:58:20 +01:00
|
|
|
if (!dir_exists(#save_dir)) CreateDir(#save_dir);
|
|
|
|
SetCurDir(#save_dir);
|
2017-09-10 07:38:29 +02:00
|
|
|
|
2020-11-29 20:08:52 +01:00
|
|
|
if (param) {
|
2020-12-15 16:39:54 +01:00
|
|
|
if (streqrp(#param, "-e ")) {
|
2020-12-03 03:56:54 +01:00
|
|
|
exit_param = true;
|
2020-12-15 16:39:54 +01:00
|
|
|
param += 3;
|
|
|
|
}
|
|
|
|
if (streqrp(#param, "-eo ")) {
|
|
|
|
exit_param = true;
|
|
|
|
open_file = true;
|
|
|
|
param += 4;
|
2020-11-29 20:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(#param, "-mem", 5)) {
|
2020-11-30 01:55:04 +01:00
|
|
|
//shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
|
2020-12-09 14:29:38 +01:00
|
|
|
strcpy(#uEdit, shared_url);
|
2020-11-29 20:08:52 +01:00
|
|
|
} else {
|
2020-12-09 14:29:38 +01:00
|
|
|
strcpy(#uEdit, #param);
|
2020-11-29 20:08:52 +01:00
|
|
|
}
|
2020-12-03 03:56:54 +01:00
|
|
|
}
|
2020-12-09 14:29:38 +01:00
|
|
|
if (uEdit[0]) StartDownloading(); else {
|
2020-12-06 22:01:31 +01:00
|
|
|
edit_box_set_text stdcall (#ed, "http://");
|
|
|
|
}
|
2016-03-25 16:35:48 +01:00
|
|
|
|
2020-11-29 20:08:52 +01:00
|
|
|
@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
|
|
|
|
loop() switch(@WaitEvent())
|
2018-05-18 22:18:10 +02:00
|
|
|
{
|
2020-12-03 03:56:54 +01:00
|
|
|
case evMouse: edit_box_mouse stdcall (#ed); break;
|
2020-12-15 16:39:54 +01:00
|
|
|
case evButton: ProcessButtonClick(@GetButtonID()); break;
|
2020-12-03 03:56:54 +01:00
|
|
|
case evKey: ProcessKeyPress(); break;
|
|
|
|
case evReDraw: DrawWindow(); break;
|
|
|
|
default: MonitorProgress();
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
2015-12-22 19:14:35 +01:00
|
|
|
}
|
2016-03-25 16:35:48 +01:00
|
|
|
|
2020-12-15 16:39:54 +01:00
|
|
|
void ProcessButtonClick(int id)
|
2015-12-22 19:14:35 +01:00
|
|
|
{
|
2020-11-30 01:55:04 +01:00
|
|
|
autoclose.click(id);
|
2020-12-03 03:56:54 +01:00
|
|
|
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();
|
2015-12-22 19:14:35 +01:00
|
|
|
}
|
2016-03-25 16:35:48 +01:00
|
|
|
|
2020-11-29 20:08:52 +01:00
|
|
|
void DrawWindow()
|
2016-03-25 16:35:48 +01:00
|
|
|
{
|
2020-11-29 20:08:52 +01:00
|
|
|
sc.get();
|
|
|
|
pb.frame_color = sc.work_dark;
|
2020-12-03 03:56:54 +01:00
|
|
|
DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9,
|
|
|
|
WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
|
2020-11-29 20:08:52 +01:00
|
|
|
|
2020-12-03 03:56:54 +01:00
|
|
|
#define BUT_Y 58;
|
|
|
|
//autoclose.draw(WIN_W-135, BUT_Y+6);
|
|
|
|
if (!http.transfer)
|
2018-05-18 22:18:10 +02:00
|
|
|
{
|
2020-12-03 03:56:54 +01:00
|
|
|
DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD);
|
|
|
|
if (filepath)
|
2018-05-18 22:18:10 +02:00
|
|
|
{
|
2020-12-03 03:56:54 +01:00
|
|
|
DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR);
|
|
|
|
DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN);
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
2020-12-03 03:56:54 +01:00
|
|
|
} else {
|
|
|
|
DrawStandartCaptButton(WIN_W - 240, BUT_Y, BTN_STOP, T_CANCEL);
|
|
|
|
DrawDownloadingProgress();
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
2020-12-03 03:56:54 +01:00
|
|
|
//ed.offset=0; //DEL?
|
2018-05-18 22:18:10 +02:00
|
|
|
DrawEditBox(#ed);
|
2015-12-22 19:14:35 +01:00
|
|
|
}
|
2016-03-25 16:35:48 +01:00
|
|
|
|
2015-12-22 19:14:35 +01:00
|
|
|
void StartDownloading()
|
|
|
|
{
|
2020-12-09 14:29:38 +01:00
|
|
|
char get_url[URL_SIZE+33];
|
2020-12-03 03:56:54 +01:00
|
|
|
if (http.transfer > 0) return;
|
|
|
|
filepath = '\0';
|
2020-12-09 14:29:38 +01:00
|
|
|
if (!strncmp(#uEdit,"https:",6)) {
|
|
|
|
miniprintf(#get_url, "http://gate.aspero.pro/?site=%s", #uEdit);
|
2020-12-01 18:37:47 +01:00
|
|
|
//notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
|
2020-12-09 14:29:38 +01:00
|
|
|
//miniprintf(#http_url, "http://%s", #uEdit+8);
|
2020-12-01 18:37:47 +01:00
|
|
|
//if (!downloader.Start(#http_url)) {
|
2020-11-21 14:06:10 +01:00
|
|
|
// notify("'Download failed.' -E");
|
|
|
|
// StopDownloading();
|
|
|
|
//}
|
2020-12-09 14:29:38 +01:00
|
|
|
} else {
|
|
|
|
strcpy(#get_url, #uEdit);
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
2020-12-09 14:29:38 +01:00
|
|
|
if (http.get(#get_url)) {
|
2020-12-03 03:56:54 +01:00
|
|
|
ed.blur_border_color = 0xCACACA;
|
2020-12-04 12:58:20 +01:00
|
|
|
EditBox_UpdateText(#ed, ed_disabled);
|
2020-12-03 03:56:54 +01:00
|
|
|
pb.value = 0;
|
|
|
|
DrawWindow();
|
|
|
|
} else {
|
2020-11-29 20:08:52 +01:00
|
|
|
notify(T_ERROR_STARTING_DOWNLOAD);
|
2018-05-18 22:18:10 +02:00
|
|
|
StopDownloading();
|
2020-12-03 03:56:54 +01:00
|
|
|
if (exit_param) ExitProcess();
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
2015-12-22 19:14:35 +01:00
|
|
|
}
|
2018-11-01 22:48:33 +01:00
|
|
|
|
|
|
|
|
2020-12-03 03:56:54 +01:00
|
|
|
void DrawDownloadingProgress()
|
2015-12-22 19:14:35 +01:00
|
|
|
{
|
2018-05-18 22:18:10 +02:00
|
|
|
char bytes_received[70];
|
2020-12-03 03:56:54 +01:00
|
|
|
|
|
|
|
EDI = http.content_received / 100;
|
|
|
|
if (pb.value == EDI) return;
|
|
|
|
|
|
|
|
pb.value = EDI;
|
|
|
|
pb.max = http.content_length / 100;
|
|
|
|
progressbar_draw stdcall(#pb);
|
|
|
|
miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(http.content_received) );
|
2020-11-29 20:08:52 +01:00
|
|
|
WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
|
2018-11-01 22:48:33 +01:00
|
|
|
//CalculateSpeed();
|
2015-12-22 19:14:35 +01:00
|
|
|
}
|
2016-03-25 16:35:48 +01:00
|
|
|
|
2015-12-22 19:14:35 +01:00
|
|
|
void StopDownloading()
|
|
|
|
{
|
2020-12-04 12:58:20 +01:00
|
|
|
http.stop();
|
|
|
|
if (http.content_pointer) http.content_pointer = free(http.content_pointer);
|
2020-12-03 03:56:54 +01:00
|
|
|
http.content_received = http.content_length = 0;
|
|
|
|
|
2018-05-18 22:18:10 +02:00
|
|
|
ed.blur_border_color = 0xFFFfff;
|
2020-12-04 12:58:20 +01:00
|
|
|
EditBox_UpdateText(#ed, ed_focus);
|
2020-11-29 20:08:52 +01:00
|
|
|
DrawWindow();
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 03:56:54 +01:00
|
|
|
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?
|
|
|
|
|
2020-12-04 12:58:20 +01:00
|
|
|
if (http.receive_result) {
|
|
|
|
DrawDownloadingProgress();
|
|
|
|
} else {
|
|
|
|
if (http.status_code >= 300) && (http.status_code < 400) {
|
|
|
|
http.header_field("location", #redirect_url, URL_SIZE);
|
2020-12-09 14:29:38 +01:00
|
|
|
get_absolute_url(#redirect_url, #uEdit);
|
|
|
|
edit_box_set_text stdcall (#ed, #redirect_url);
|
2020-12-03 03:56:54 +01:00
|
|
|
StopDownloading();
|
|
|
|
StartDownloading();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SaveFile();
|
|
|
|
if (exit_param) ExitProcess();
|
|
|
|
StopDownloading();
|
|
|
|
DrawWindow();
|
2020-12-04 12:58:20 +01:00
|
|
|
}
|
2020-12-03 03:56:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveFile()
|
2018-05-18 22:18:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char notify_message[4296];
|
2020-12-04 12:58:20 +01:00
|
|
|
char file_name[URL_SIZE+96];
|
2018-05-18 22:18:10 +02:00
|
|
|
|
2020-12-04 12:58:20 +01:00
|
|
|
strcpy(#filepath, #save_dir);
|
|
|
|
chrcat(#filepath, '/');
|
2020-12-03 03:56:54 +01:00
|
|
|
|
2020-12-04 12:58:20 +01:00
|
|
|
//Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip"
|
|
|
|
if (http.header_field("content-disposition", #file_name, sizeof(file_name))) {
|
|
|
|
if (EDX = strstr(#file_name,"filename=\"")) {
|
|
|
|
strcat(#filepath, EDX+10);
|
|
|
|
ESBYTE[strchr(#filepath,'\"')] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Clean all slashes at the end
|
2020-12-09 14:29:38 +01:00
|
|
|
strcpy(#file_name, #uEdit);
|
2020-12-04 12:58:20 +01:00
|
|
|
while (file_name[strlen(#file_name)-1] == '/') {
|
|
|
|
file_name[strlen(#file_name)-1] = 0;
|
|
|
|
}
|
|
|
|
strcat(#filepath, #file_name+strrchr(#file_name, '/'));
|
2018-05-18 22:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
|
|
|
|
|
2020-12-04 12:58:20 +01:00
|
|
|
if (CreateFile(http.content_received, http.content_pointer, #filepath)==0) {
|
2020-11-29 20:08:52 +01:00
|
|
|
miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
|
|
|
|
} else {
|
|
|
|
miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
|
|
|
|
}
|
2018-05-18 22:18:10 +02:00
|
|
|
|
2020-12-03 03:56:54 +01:00
|
|
|
if (!exit_param) notify(#notify_message);
|
2020-12-15 16:39:54 +01:00
|
|
|
if (open_file) ProcessButtonClick(BTN_RUN);
|
2020-11-30 01:55:04 +01:00
|
|
|
if (autoclose.checked) ExitProcess();
|
2020-12-03 03:56:54 +01: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 = http.content_received - netdata_received / time.gone * 100;
|
|
|
|
debugval("speed", speed);
|
|
|
|
debugln(ConvertSizeToKb(speed) );
|
|
|
|
time.old = time.cur;
|
|
|
|
netdata_received = http.content_received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else time.old = time.cur;
|
|
|
|
}
|
|
|
|
*/
|