Subversion Repositories Kolibri OS

Rev

Rev 8319 | Rev 8339 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8305 leency 1
#define MEMSIZE 1024 * 100
8278 leency 2
//Copyright 2020 by Leency
3
#include "../lib/gui.h"
4
#include "../lib/random.h"
5
#include "../lib/obj/box_lib.h"
6
#include "../lib/obj/http.h"
8281 leency 7
 
8278 leency 8
#include "const.h"
9
 
8305 leency 10
bool exit_param = false;
11
 
12
_http http;
13
 
8291 leency 14
checkbox autoclose = { T_AUTOCLOSE, false };
7284 leency 15
 
8305 leency 16
char main_url[URL_SIZE];
17
char filepath[URL_SIZE+96];
7521 leency 18
 
8305 leency 19
progress_bar pb = {0, GAPX, 58, 315, 17, 0, NULL, NULL, 0xFFFfff, 0x74DA00, NULL};
20
edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,
21
	0x10000000, sizeof(main_url)-2,#main_url,0,ed_focus,19,19};
22
 
23
 
8278 leency 24
void main()
5493 leency 25
{
8278 leency 26
	dword shared_url;
27
	load_dll(boxlib,  #box_lib_init,0);
28
	load_dll(libHTTP, #http_lib_init,1);
29
 
8319 leency 30
	if (!dir_exists(#save_dir)) CreateDir(#save_dir);
31
	SetCurDir(#save_dir);
6969 leency 32
 
8278 leency 33
	if (param) {
34
		if (!strncmp(#param, "-exit ", 6)) {
8305 leency 35
			exit_param = true;
8278 leency 36
			param += 6;
37
		}
38
 
39
		if (!strncmp(#param, "-mem", 5)) {
8281 leency 40
			//shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
8305 leency 41
			strcpy(#main_url, shared_url);
8278 leency 42
		} else {
8305 leency 43
			strcpy(#main_url, #param);
8278 leency 44
		}
8305 leency 45
	}
8334 leency 46
	if (main_url[0]) StartDownloading(); else {
47
		edit_box_set_text stdcall (#ed, "http://");
48
	}
6374 guillem 49
 
8278 leency 50
	@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
51
	@SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
52
	loop() switch(@WaitEvent())
7281 leency 53
	{
8305 leency 54
		case evMouse:  edit_box_mouse stdcall (#ed); break;
55
		case evButton: ProcessButtonClick(); break;
56
		case evKey:    ProcessKeyPress(); break;
57
		case evReDraw: DrawWindow(); break;
58
		default:       MonitorProgress();
7281 leency 59
	}
5493 leency 60
}
6374 guillem 61
 
8305 leency 62
void ProcessButtonClick()
6001 leency 63
{
8305 leency 64
	int id = @GetButtonID();
8281 leency 65
	autoclose.click(id);
8305 leency 66
	if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); }
67
	if (id==BTN_START) StartDownloading();
68
	if (id==BTN_STOP) StopDownloading();
69
	if (id==BTN_DIR) RunProgram("/sys/File managers/Eolite", #filepath);
70
	if (id==BTN_RUN) RunProgram("/sys/@open", #filepath);
6001 leency 71
}
8305 leency 72
 
73
void ProcessKeyPress()
74
{
75
	@GetKey();
76
	edit_box_key stdcall(#ed);
77
	EAX >>= 16;
78
	if (AL == SCAN_CODE_ENTER) StartDownloading();
79
	if (AL == SCAN_CODE_ESC) StopDownloading();
80
}
6374 guillem 81
 
8278 leency 82
void DrawWindow()
6374 guillem 83
{
8278 leency 84
	sc.get();
85
	pb.frame_color = sc.work_dark;
8305 leency 86
	DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9,
87
		WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
8278 leency 88
 
8305 leency 89
	#define BUT_Y 58;
90
	//autoclose.draw(WIN_W-135, BUT_Y+6);
91
	if (!http.transfer)
7281 leency 92
	{
8305 leency 93
		DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD);
94
		if (filepath)
7281 leency 95
		{
8305 leency 96
			DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR);
97
			DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN);
7281 leency 98
		}
8305 leency 99
	} else {
100
		DrawStandartCaptButton(WIN_W - 240, BUT_Y, BTN_STOP, T_CANCEL);
101
		DrawDownloadingProgress();
7281 leency 102
	}
8305 leency 103
	//ed.offset=0; //DEL?
7281 leency 104
	DrawEditBox(#ed);
5519 leency 105
}
6374 guillem 106
 
6001 leency 107
void StartDownloading()
108
{
8229 leency 109
	char proxy_url[URL_SIZE];
8305 leency 110
	if (http.transfer > 0) return;
111
	filepath = '\0';
112
	if (!strncmp(#main_url,"https:",6)) {
113
		miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #main_url);
114
		strcpy(#main_url, #proxy_url);
8319 leency 115
		EditBox_UpdateText(#ed, ed.flags);
8291 leency 116
		//notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
8305 leency 117
		//miniprintf(#http_url, "http://%s", #main_url+8);
8291 leency 118
		//if (!downloader.Start(#http_url)) {
119
		//	notify("'Download failed.' -E");
120
		//	StopDownloading();
121
		//}
8305 leency 122
	}
123
	if (http.get(#main_url)) {
124
		ed.blur_border_color = 0xCACACA;
8319 leency 125
		EditBox_UpdateText(#ed, ed_disabled);
8305 leency 126
		pb.value = 0;
8278 leency 127
		DrawWindow();
8305 leency 128
	} else {
8278 leency 129
		notify(T_ERROR_STARTING_DOWNLOAD);
7281 leency 130
		StopDownloading();
8305 leency 131
		if (exit_param) ExitProcess();
7281 leency 132
	}
6001 leency 133
}
7521 leency 134
 
8305 leency 135
 
136
void DrawDownloadingProgress()
7521 leency 137
{
8305 leency 138
	char bytes_received[70];
7521 leency 139
 
8305 leency 140
	EDI = http.content_received / 100;
141
	if (pb.value == EDI) return;
7521 leency 142
 
8305 leency 143
	pb.value = EDI;
144
	pb.max = http.content_length / 100;
145
	progressbar_draw stdcall(#pb);
146
	miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(http.content_received) );
8278 leency 147
	WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
7521 leency 148
	//CalculateSpeed();
5493 leency 149
}
6374 guillem 150
 
5493 leency 151
void StopDownloading()
152
{
8319 leency 153
	http.stop();
154
	if (http.content_pointer) http.content_pointer = free(http.content_pointer);
8305 leency 155
	http.content_received = http.content_length = 0;
156
 
7281 leency 157
	ed.blur_border_color = 0xFFFfff;
8319 leency 158
	EditBox_UpdateText(#ed, ed_focus);
8278 leency 159
	DrawWindow();
7281 leency 160
}
161
 
8305 leency 162
void MonitorProgress()
7281 leency 163
{
8305 leency 164
	char redirect_url[URL_SIZE];
165
	if (http.transfer <= 0) return;
166
	http.receive();
167
	if (!http.content_length) http.content_length = http.content_received * 20; //MOVE?
168
 
8319 leency 169
	if (http.receive_result) {
170
		DrawDownloadingProgress();
171
	} else {
172
		if (http.status_code >= 300) && (http.status_code < 400) {
173
			http.header_field("location", #redirect_url, URL_SIZE);
8305 leency 174
			get_absolute_url(#redirect_url, #main_url);
175
			strcpy(#main_url, #redirect_url);
176
			StopDownloading();
177
			StartDownloading();
178
			return;
179
		}
180
		SaveFile();
181
		if (exit_param) ExitProcess();
182
		StopDownloading();
183
		DrawWindow();
8319 leency 184
 
185
		http.hfree();
186
	}
8305 leency 187
}
188
 
189
void SaveFile()
190
{
7281 leency 191
	int i;
192
	char notify_message[4296];
8319 leency 193
	char file_name[URL_SIZE+96];
7281 leency 194
 
8319 leency 195
	strcpy(#filepath, #save_dir);
196
	chrcat(#filepath, '/');
197
 
8305 leency 198
	//Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip"
8319 leency 199
	if (http.header_field("content-disposition", #file_name, sizeof(file_name))) {
200
		if (EDX = strstr(#file_name,"filename=\"")) {
201
			strcat(#filepath, EDX+10);
202
			ESBYTE[strchr(#filepath,'\"')] = '\0';
203
		}
204
	} else {
205
		// Clean all slashes at the end
206
		strcpy(#file_name, #main_url);
207
		while (file_name[strlen(#file_name)-1] == '/') {
208
			file_name[strlen(#file_name)-1] = 0;
209
		}
210
		strcat(#filepath, #file_name+strrchr(#file_name, '/'));
7281 leency 211
	}
212
 
213
	for (i=0; i
214
 
8319 leency 215
	if (CreateFile(http.content_received, http.content_pointer, #filepath)==0) {
8278 leency 216
		miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
7765 leency 217
	} else {
8278 leency 218
		miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
7765 leency 219
	}
8305 leency 220
 
221
	if (!exit_param) notify(#notify_message);
222
	if (autoclose.checked) ExitProcess();
223
}
8278 leency 224
 
8305 leency 225
 
226
/*
227
struct TIME
228
{
229
	dword old;
230
	dword cur;
231
	dword gone;
232
} time = {0,0,0};
233
 
234
dword netdata_received;
235
dword speed;
236
 
237
void CalculateSpeed()
238
{
239
	time.cur = GetStartTime();
240
 
241
	if (time.old) {
242
		time.gone = time.cur - time.old;
243
		if (time.gone > 200) {
244
			speed = http.content_received - netdata_received / time.gone * 100;
245
			debugval("speed", speed);
246
			debugln(ConvertSizeToKb(speed) );
247
			time.old = time.cur;
248
			netdata_received = http.content_received;
249
		}
8278 leency 250
	}
8305 leency 251
	else time.old = time.cur;
252
}
253
*/