Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7284 leency 1
DOWNLOADER downloader;
2
 
5493 leency 3
#ifdef LANG_RUS
7281 leency 4
	#define DL_WINDOW_HEADER "Менеджер загрузок"
5
	#define START_DOWNLOADING "Начать закачку"
6
	#define STOP_DOWNLOADING "Остановить"
7
	#define SHOW_IN_FOLDER "Показать в папке"
8
	#define OPEN_FILE_TEXT "Открыть файл"
9
	#define FILE_SAVED_AS "'Менеджер загрузок\nФайл сохранен как "
10
	#define KB_RECEIVED "Идет скачивание... %s получено"
5493 leency 11
#else
7281 leency 12
	#define DL_WINDOW_HEADER "Download Manager"
13
	#define START_DOWNLOADING "Start downloading"
14
	#define STOP_DOWNLOADING "Stop downloading"
15
	#define SHOW_IN_FOLDER "Show in folder"
16
	#define OPEN_FILE_TEXT "Open file"
17
	#define FILE_SAVED_AS "'Download manager\nFile saved as "
18
	#define KB_RECEIVED "Downloading... %s received"
5493 leency 19
#endif
6366 leency 20
char save_to[4096] = "/tmp0/1/Downloads";
6374 guillem 21
 
7521 leency 22
#define CONX 15
23
 
5493 leency 24
proc_info DL_Form;
6001 leency 25
char downloader_edit[10000];
5493 leency 26
char filepath[4096];
7746 leency 27
edit_box ed = {NULL,57,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19};
7521 leency 28
progress_bar pb = {0, CONX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
6969 leency 29
//progress_bar pb = {0, 180, 55, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
30
//progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
7521 leency 31
 
7281 leency 32
bool downloader_opened;
6366 leency 33
char downloader_stak[4096];
6374 guillem 34
 
7766 leency 35
bool download_and_exit = false;
6374 guillem 36
 
37
void Downloader()
5493 leency 38
{
7281 leency 39
	if (!dir_exists(#save_to)) CreateDir(#save_to);
40
	downloader_opened = true;
41
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
6969 leency 42
 
7806 leency 43
	sc.get();
44
	pb.frame_color = sc.work_dark;
6374 guillem 45
 
7281 leency 46
	filepath[0] = NULL;
47
 
48
	downloader.Stop();
49
	if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
50
	ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
6374 guillem 51
 
7281 leency 52
	loop() switch(WaitEvent())
53
	{
54
		case evMouse:
55
			edit_box_mouse stdcall (#ed);
56
			break;
6978 leency 57
 
7281 leency 58
		case evButton:
59
			Key_Scan(GetButtonID());
60
			break;
6978 leency 61
 
7281 leency 62
		case evKey:
63
			GetKeys();
64
			edit_box_key stdcall(#ed);
65
			if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
66
			break;
6978 leency 67
 
7281 leency 68
		case evReDraw:
7806 leency 69
			DefineAndDrawWindow(215, 100, 580, 130, 0x74, sc.work, DL_WINDOW_HEADER, 0);
7281 leency 70
			GetProcessInfo(#DL_Form, SelfInfo);
71
			if (DL_Form.status_window>2) break;
72
			if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
73
			if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
74
			DL_Draw_Window();
75
			break;
76
 
77
		default:
78
			if (!downloader.MonitorProgress()) break;
7525 leency 79
			pb.max = downloader.httpd.content_length / 100;
80
			EDI = downloader.httpd.content_received/100;
81
			if (pb.value != EDI)
7281 leency 82
			{
7525 leency 83
				pb.value = EDI;
7281 leency 84
				progressbar_draw stdcall(#pb);
85
				DrawDownloading();
86
			}
87
			if (downloader.state == STATE_COMPLETED)
88
			{
89
				SaveDownloadedFile();
7765 leency 90
				if (download_and_exit) ExitProcess();
7281 leency 91
				StopDownloading();
92
				DL_Draw_Window();
93
				break;
94
			}
95
	}
5493 leency 96
}
6374 guillem 97
 
6001 leency 98
void Key_Scan(int id)
99
{
7281 leency 100
	if (id==001) { downloader_opened=false; StopDownloading(); ExitProcess(); }
101
	if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
102
	if (id==302) StopDownloading();
103
	if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
104
	if (id==306) {
105
		SetCurDir(#save_to);
106
		RunProgram("/sys/@open", #filepath);
107
	}
6001 leency 108
}
6374 guillem 109
 
5493 leency 110
void DL_Draw_Window()
6374 guillem 111
{
7281 leency 112
	int but_x = 0;
113
	int but_y = 58;
7806 leency 114
	DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, sc.work);
7281 leency 115
	DeleteButton(301);
116
	DeleteButton(302);
117
	DeleteButton(305);
118
	DeleteButton(306);
119
	if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
120
	{
7521 leency 121
		but_x = CONX + DrawStandartCaptButton(CONX, but_y, 301, START_DOWNLOADING);
7281 leency 122
		if (filepath[0])
123
		{
124
			but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
125
			DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);
126
		}
127
	}
128
	if (downloader.state == STATE_IN_PROGRESS)
129
	{
130
		DrawStandartCaptButton(DL_Form.width - 190, but_y, 302, STOP_DOWNLOADING);
131
		DrawDownloading();
132
	}
7806 leency 133
	WriteText(CONX, ed.top + 4, 0x90, sc.work_text, "URL:");
7521 leency 134
    ed.width = DL_Form.cwidth - ed.left - CONX - 3;
7281 leency 135
	ed.offset=0;
136
	DrawEditBox(#ed);
5519 leency 137
}
6374 guillem 138
 
6001 leency 139
void StartDownloading()
140
{
8229 leency 141
	char http_url[URL_SIZE];
142
	char proxy_url[URL_SIZE];
7281 leency 143
	StopDownloading();
8229 leency 144
	if (!strncmp(#downloader_edit,"https://",7)) {
145
		notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
146
		sprintf(#http_url, "http://%s", #downloader_edit+8);
147
		if (!downloader.Start(#http_url)) {
148
			notify("'Download failed.' -E");
149
			StopDownloading();
150
		}
151
		//sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
152
		//if (!downloader.Start(#proxy_url)) {
153
		//	notify("'Download failed.' -E");
154
		//	StopDownloading();
155
		//}
156
		DL_Draw_Window();
7281 leency 157
		return;
158
	}
159
	if (!downloader.Start(#downloader_edit)) {
7765 leency 160
		if (download_and_exit) ExitProcess();
7281 leency 161
		notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
162
		StopDownloading();
163
		return;
164
	}
165
	ed.blur_border_color = 0xCACACA;
166
	ed.flags = 100000000000b;
167
	pb.value = 0;
168
	DL_Draw_Window();
6001 leency 169
}
7521 leency 170
 
7525 leency 171
/*
7521 leency 172
struct TIME
173
{
174
	dword old;
175
	dword cur;
176
	dword gone;
177
} time = {0,0,0};
178
 
179
dword netdata_received;
180
dword speed;
181
 
182
void CalculateSpeed()
183
{
184
	time.cur = GetStartTime();
185
 
186
	if (time.old) {
187
		time.gone = time.cur - time.old;
188
		if (time.gone > 200) {
189
			speed = downloader.httpd.content_received - netdata_received / time.gone * 100;
190
			debugval("speed", speed);
191
			debugln(ConvertSizeToKb(speed) );
192
			time.old = time.cur;
193
			netdata_received = downloader.httpd.content_received;
194
		}
195
	}
196
	else time.old = time.cur;
197
}
7525 leency 198
*/
6374 guillem 199
 
5519 leency 200
void DrawDownloading()
201
{
7281 leency 202
	char bytes_received[70];
7282 leency 203
	sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
7806 leency 204
	DrawBar(CONX, pb.top + 22, pb.width, 16, sc.work);
205
	WriteText(CONX, pb.top + 22, 0x90, sc.work_text, #bytes_received);
7521 leency 206
	//CalculateSpeed();
7281 leency 207
	progressbar_draw stdcall(#pb);
5493 leency 208
}
6374 guillem 209
 
5493 leency 210
void StopDownloading()
211
{
7281 leency 212
	downloader.Stop();
213
	ed.blur_border_color = 0xFFFfff;
214
	ed.flags = 10b;
215
	DL_Draw_Window();
216
}
217
 
218
void SaveDownloadedFile()
219
{
220
	int i;
221
	char aux[2048];
222
	char notify_message[4296];
223
 
224
	// Clean all slashes at the end
225
	strcpy(#aux,  #downloader_edit);
226
	while (aux[strlen(#aux)-1] == '/') {
227
		aux[strlen(#aux)-1] = 0;
228
	}
229
	sprintf(#filepath, "%s/%s", #save_to, #aux+strrchr(#aux, '/'));
230
 
231
	for (i=0; i
232
 
7765 leency 233
	if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
7281 leency 234
		sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
7765 leency 235
	} else {
7281 leency 236
		sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
7765 leency 237
	}
7281 leency 238
 
7765 leency 239
	if (!download_and_exit) notify(#notify_message);
6374 guillem 240
}