Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
8278 leency 1
//Copyright 2020 by Leency
2
 
3
#define MEMSIZE 1024 * 100
4
#include "../lib/gui.h"
5
#include "../lib/random.h"
6
#include "../lib/obj/box_lib.h"
7
#include "../lib/obj/http.h"
8
#include "../lib/patterns/http_downloader.h"
9
 
10
#include "const.h"
11
 
7284 leency 12
DOWNLOADER downloader;
13
 
8278 leency 14
char downloader_edit[4000];
5493 leency 15
char filepath[4096];
8278 leency 16
edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,
17
	sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19};
18
progress_bar pb = {0, GAPX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
6969 leency 19
//progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
8278 leency 20
 
21
bool exit_when_done = false;
7521 leency 22
 
6374 guillem 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
 
7281 leency 30
	if (!dir_exists(#save_to)) CreateDir(#save_to);
6969 leency 31
 
8278 leency 32
	if (param) {
33
		if (!strncmp(#param, "-exit ", 6)) {
34
			exit_when_done = true;
35
			param += 6;
36
		}
37
 
38
		if (!strncmp(#param, "-mem", 5)) {
39
			shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
40
			strcpy(#downloader_edit, shared_url);
41
		} else {
42
			strcpy(#downloader_edit, #param);
43
		}
44
	}
7281 leency 45
	if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
46
	ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
6374 guillem 47
 
8278 leency 48
	@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
49
	@SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
50
	loop() switch(@WaitEvent())
7281 leency 51
	{
52
		case evMouse:
53
			edit_box_mouse stdcall (#ed);
54
			break;
6978 leency 55
 
7281 leency 56
		case evButton:
8278 leency 57
			ProcessEvent(GetButtonID());
7281 leency 58
			break;
6978 leency 59
 
7281 leency 60
		case evKey:
61
			GetKeys();
62
			edit_box_key stdcall(#ed);
8278 leency 63
			if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301);
7281 leency 64
			break;
6978 leency 65
 
7281 leency 66
		case evReDraw:
8278 leency 67
			DrawWindow();
7281 leency 68
			break;
69
 
70
		default:
71
			if (!downloader.MonitorProgress()) break;
7525 leency 72
			pb.max = downloader.httpd.content_length / 100;
73
			EDI = downloader.httpd.content_received/100;
74
			if (pb.value != EDI)
7281 leency 75
			{
7525 leency 76
				pb.value = EDI;
7281 leency 77
				progressbar_draw stdcall(#pb);
78
				DrawDownloading();
79
			}
80
			if (downloader.state == STATE_COMPLETED)
81
			{
82
				SaveDownloadedFile();
8278 leency 83
				if (exit_when_done) ExitProcess();
7281 leency 84
				StopDownloading();
8278 leency 85
				DrawWindow();
7281 leency 86
				break;
87
			}
88
	}
5493 leency 89
}
6374 guillem 90
 
8278 leency 91
void ProcessEvent(int id)
6001 leency 92
{
8278 leency 93
	if (id==001) { StopDownloading(); ExitProcess(); }
7281 leency 94
	if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
95
	if (id==302) StopDownloading();
8278 leency 96
	if (id==305) RunProgram("/sys/File managers/Eolite", #filepath);
7281 leency 97
	if (id==306) {
98
		SetCurDir(#save_to);
99
		RunProgram("/sys/@open", #filepath);
100
	}
6001 leency 101
}
6374 guillem 102
 
8278 leency 103
void DrawWindow()
6374 guillem 104
{
7281 leency 105
	int but_x = 0;
106
	int but_y = 58;
8278 leency 107
 
108
	sc.get();
109
	pb.frame_color = sc.work_dark;
110
	DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
111
 
7281 leency 112
	if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
113
	{
8278 leency 114
		but_x = GAPX + DrawStandartCaptButton(GAPX, but_y, 301, START_DOWNLOADING);
7281 leency 115
		if (filepath[0])
116
		{
117
			but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
118
			DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);
119
		}
120
	}
121
	if (downloader.state == STATE_IN_PROGRESS)
122
	{
8278 leency 123
		DrawStandartCaptButton(WIN_W - 190, but_y, 302, STOP_DOWNLOADING);
7281 leency 124
		DrawDownloading();
125
	}
126
	ed.offset=0;
127
	DrawEditBox(#ed);
5519 leency 128
}
6374 guillem 129
 
6001 leency 130
void StartDownloading()
131
{
8229 leency 132
	char http_url[URL_SIZE];
133
	char proxy_url[URL_SIZE];
7281 leency 134
	StopDownloading();
8229 leency 135
	if (!strncmp(#downloader_edit,"https://",7)) {
136
		notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
8278 leency 137
		miniprintf(#http_url, "http://%s", #downloader_edit+8);
8229 leency 138
		if (!downloader.Start(#http_url)) {
139
			notify("'Download failed.' -E");
140
			StopDownloading();
141
		}
142
		//sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
143
		//if (!downloader.Start(#proxy_url)) {
144
		//	notify("'Download failed.' -E");
145
		//	StopDownloading();
146
		//}
8278 leency 147
		DrawWindow();
7281 leency 148
		return;
149
	}
150
	if (!downloader.Start(#downloader_edit)) {
8278 leency 151
		if (exit_when_done) ExitProcess();
152
		notify(T_ERROR_STARTING_DOWNLOAD);
7281 leency 153
		StopDownloading();
154
		return;
155
	}
156
	ed.blur_border_color = 0xCACACA;
157
	ed.flags = 100000000000b;
158
	pb.value = 0;
8278 leency 159
	DrawWindow();
6001 leency 160
}
7521 leency 161
 
7525 leency 162
/*
7521 leency 163
struct TIME
164
{
165
	dword old;
166
	dword cur;
167
	dword gone;
168
} time = {0,0,0};
169
 
170
dword netdata_received;
171
dword speed;
172
 
173
void CalculateSpeed()
174
{
175
	time.cur = GetStartTime();
176
 
177
	if (time.old) {
178
		time.gone = time.cur - time.old;
179
		if (time.gone > 200) {
180
			speed = downloader.httpd.content_received - netdata_received / time.gone * 100;
181
			debugval("speed", speed);
182
			debugln(ConvertSizeToKb(speed) );
183
			time.old = time.cur;
184
			netdata_received = downloader.httpd.content_received;
185
		}
186
	}
187
	else time.old = time.cur;
188
}
7525 leency 189
*/
6374 guillem 190
 
5519 leency 191
void DrawDownloading()
192
{
7281 leency 193
	char bytes_received[70];
8278 leency 194
	miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
195
	WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
7521 leency 196
	//CalculateSpeed();
7281 leency 197
	progressbar_draw stdcall(#pb);
5493 leency 198
}
6374 guillem 199
 
5493 leency 200
void StopDownloading()
201
{
7281 leency 202
	downloader.Stop();
203
	ed.blur_border_color = 0xFFFfff;
204
	ed.flags = 10b;
8278 leency 205
	DrawWindow();
7281 leency 206
}
207
 
208
void SaveDownloadedFile()
209
{
210
	int i;
211
	char aux[2048];
212
	char notify_message[4296];
213
 
214
	// Clean all slashes at the end
215
	strcpy(#aux,  #downloader_edit);
216
	while (aux[strlen(#aux)-1] == '/') {
217
		aux[strlen(#aux)-1] = 0;
218
	}
8278 leency 219
 
220
	//miniprintf(#filepath, "%s/", #save_to);
221
	strcpy(#filepath, #save_to);
222
	chrcat(#filepath, '/');
223
	strcat(#filepath, #aux+strrchr(#aux, '/'));
7281 leency 224
 
225
	for (i=0; i
226
 
7765 leency 227
	if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
8278 leency 228
		miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
7765 leency 229
	} else {
8278 leency 230
		miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
7765 leency 231
	}
8278 leency 232
 
233
	/*
234
	if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
235
		strcpy(#notify_message, "'Download complete' -Dt");
236
	} else {
237
		strcpy(#notify_message, "'Error saving downloaded file!' -Et");
238
	}
239
	*/
7281 leency 240
 
8278 leency 241
	if (!exit_when_done) notify(#notify_message);
6374 guillem 242
}