Subversion Repositories Kolibri OS

Rev

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

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