Subversion Repositories Kolibri OS

Rev

Rev 7521 | 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];
7506 leency 27
edit_box ed = {NULL,57,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,sizeof(downloader_edit)-2,#downloader_edit,0,2,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
 
6374 guillem 33
 
7281 leency 34
bool downloader_opened;
6366 leency 35
char downloader_stak[4096];
6374 guillem 36
 
37
 
38
 
39
void Downloader()
5493 leency 40
{
7281 leency 41
	if (!dir_exists(#save_to)) CreateDir(#save_to);
42
	downloader_opened = true;
43
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
6969 leency 44
 
7281 leency 45
	system.color.get();
46
	pb.frame_color = system.color.work_dark;
6374 guillem 47
 
7281 leency 48
	filepath[0] = NULL;
49
 
50
	downloader.Stop();
51
	if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
52
	ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
6374 guillem 53
 
7281 leency 54
	loop() switch(WaitEvent())
55
	{
56
		case evMouse:
57
			edit_box_mouse stdcall (#ed);
58
			break;
6978 leency 59
 
7281 leency 60
		case evButton:
61
			Key_Scan(GetButtonID());
62
			break;
6978 leency 63
 
7281 leency 64
		case evKey:
65
			GetKeys();
66
			edit_box_key stdcall(#ed);
67
			if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
68
			break;
6978 leency 69
 
7281 leency 70
		case evReDraw:
71
			DefineAndDrawWindow(215, 100, 580, 130, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
72
			GetProcessInfo(#DL_Form, SelfInfo);
73
			if (DL_Form.status_window>2) break;
74
			if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
75
			if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
76
			DL_Draw_Window();
77
			break;
78
 
79
		default:
80
			if (!downloader.MonitorProgress()) break;
7525 leency 81
			pb.max = downloader.httpd.content_length / 100;
82
			EDI = downloader.httpd.content_received/100;
83
			if (pb.value != EDI)
7281 leency 84
			{
7525 leency 85
				pb.value = EDI;
7281 leency 86
				progressbar_draw stdcall(#pb);
87
				DrawDownloading();
88
			}
89
			if (downloader.state == STATE_COMPLETED)
90
			{
91
				SaveDownloadedFile();
92
				StopDownloading();
93
				DL_Draw_Window();
94
				break;
95
			}
96
	}
5493 leency 97
}
6374 guillem 98
 
6001 leency 99
void Key_Scan(int id)
100
{
7281 leency 101
	if (id==001) { downloader_opened=false; StopDownloading(); ExitProcess(); }
102
	if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
103
	if (id==302) StopDownloading();
104
	if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
105
	if (id==306) {
106
		SetCurDir(#save_to);
107
		RunProgram("/sys/@open", #filepath);
108
	}
6001 leency 109
}
6374 guillem 110
 
5493 leency 111
void DL_Draw_Window()
6374 guillem 112
{
7281 leency 113
	int but_x = 0;
114
	int but_y = 58;
115
	DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
116
	DeleteButton(301);
117
	DeleteButton(302);
118
	DeleteButton(305);
119
	DeleteButton(306);
120
	if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
121
	{
7521 leency 122
		but_x = CONX + DrawStandartCaptButton(CONX, but_y, 301, START_DOWNLOADING);
7281 leency 123
		if (filepath[0])
124
		{
125
			but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
126
			DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);
127
		}
128
	}
129
	if (downloader.state == STATE_IN_PROGRESS)
130
	{
131
		DrawStandartCaptButton(DL_Form.width - 190, but_y, 302, STOP_DOWNLOADING);
132
		DrawDownloading();
133
	}
7521 leency 134
	WriteText(CONX, ed.top + 4, 0x90, system.color.work_text, "URL:");
135
    ed.width = DL_Form.cwidth - ed.left - CONX - 3;
7281 leency 136
	ed.offset=0;
137
	DrawEditBox(#ed);
5519 leency 138
}
6374 guillem 139
 
6001 leency 140
void StartDownloading()
141
{
7281 leency 142
	StopDownloading();
143
	if (strncmp(#downloader_edit,"http://",7)!=0) {
144
		notify("'File address should start from http://' -E");
145
		return;
146
	}
147
	if (!downloader.Start(#downloader_edit)) {
148
		notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
149
		StopDownloading();
150
		return;
151
	}
152
	ed.blur_border_color = 0xCACACA;
153
	ed.flags = 100000000000b;
154
	pb.value = 0;
155
	DL_Draw_Window();
6001 leency 156
}
7521 leency 157
 
7525 leency 158
/*
7521 leency 159
struct TIME
160
{
161
	dword old;
162
	dword cur;
163
	dword gone;
164
} time = {0,0,0};
165
 
166
dword netdata_received;
167
dword speed;
168
 
169
void CalculateSpeed()
170
{
171
	time.cur = GetStartTime();
172
 
173
	if (time.old) {
174
		time.gone = time.cur - time.old;
175
		if (time.gone > 200) {
176
			speed = downloader.httpd.content_received - netdata_received / time.gone * 100;
177
			debugval("speed", speed);
178
			debugln(ConvertSizeToKb(speed) );
179
			time.old = time.cur;
180
			netdata_received = downloader.httpd.content_received;
181
		}
182
	}
183
	else time.old = time.cur;
184
}
7525 leency 185
*/
6374 guillem 186
 
5519 leency 187
void DrawDownloading()
188
{
7281 leency 189
	char bytes_received[70];
7282 leency 190
	sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
7521 leency 191
	DrawBar(CONX, pb.top + 22, pb.width, 16, system.color.work);
192
	WriteText(CONX, pb.top + 22, 0x90, system.color.work_text, #bytes_received);
193
	//CalculateSpeed();
7281 leency 194
	progressbar_draw stdcall(#pb);
5493 leency 195
}
6374 guillem 196
 
5493 leency 197
void StopDownloading()
198
{
7281 leency 199
	downloader.Stop();
200
	ed.blur_border_color = 0xFFFfff;
201
	ed.flags = 10b;
202
	DL_Draw_Window();
203
}
204
 
205
void SaveDownloadedFile()
206
{
207
	int i;
208
	char aux[2048];
209
	char notify_message[4296];
210
 
211
	// Clean all slashes at the end
212
	strcpy(#aux,  #downloader_edit);
213
	while (aux[strlen(#aux)-1] == '/') {
214
		aux[strlen(#aux)-1] = 0;
215
	}
216
	sprintf(#filepath, "%s/%s", #save_to, #aux+strrchr(#aux, '/'));
217
 
218
	for (i=0; i
219
 
7282 leency 220
	if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0)
7281 leency 221
		sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
222
	else
223
		sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
224
 
225
	notify(#notify_message);
6374 guillem 226
}