Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
4662 leency 1
#define MEMSIZE 0x100000
2
//libraries
3
#include "..\lib\kolibri.h"
4
#include "..\lib\strings.h"
5499 leency 5
#include "..\lib\gui.h"
4666 leency 6
#include "..\lib\draw_buf.h"
4662 leency 7
#include "..\lib\file_system.h"
8
#include "..\lib\mem.h"
9
#include "..\lib\dll.h"
10
#include "..\lib\list_box.h"
11
//*.obj libraries
5499 leency 12
#include "..\lib\obj\box_lib.h"
13
#include "..\lib\obj\libio_lib.h"
14
#include "..\lib\obj\http.h"
4662 leency 15
 
4667 leency 16
char header[]="New Downloader v0.6";
4662 leency 17
 
18
#ifdef LANG_RUS
19
	char accept_language[]= "Accept-Language: ru\n";
20
#else
21
	char accept_language[]= "Accept-Language: en\n";
22
#endif
23
 
24
proc_info Form;
25
#define WIN_W 400
4666 leency 26
#define WIN_H 220
5674 pavelyakov 27
 
4662 leency 28
#define URL param
29
 
30
int	mouse_twb;
31
edit_box address_box= {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#URL,#mouse_twb,2,19,19};
32
 
33
dword speed[2000];
34
dword speed_position;
35
dword bufpointer;
36
dword bufsize;
37
 
38
dword http_transfer = 0;
39
dword http_buffer;
40
 
4666 leency 41
DrawBufer diagram;
4662 leency 42
 
43
void main()
44
{
45
	int key, btn;
5640 pavelyakov 46
 
4662 leency 47
	char filepath[4096];
48
	char notify_message[4296];
49
 
5626 leency 50
	load_dll(boxlib, #box_lib_init,0);
51
	load_dll(libio, #libio_init,1);
52
	load_dll(libHTTP, #http_lib_init,1);
4662 leency 53
	if (!URL) strcpy(#URL, "http://builds.kolibrios.org/eng/latest-iso.7z");
54
	address_box.size = address_box.pos = strlen(#URL);
55
 
4666 leency 56
	SetEventMask(0x27); //a7
4662 leency 57
	loop()
58
	{
59
		WaitEventTimeout(40);
60
		switch(EAX & 0xFF)
61
		{
62
			CASE evMouse:
63
				if (!CheckActiveProcess(Form.ID)) break;
64
				if (http_transfer <= 0) edit_box_mouse stdcall (#address_box);
65
				break;
66
 
67
			case evButton:
68
				btn=GetButtonID();
69
				if (btn==1)	ExitProcess();
70
				Scan(btn);
71
				break;
72
 
73
			case evKey:
74
				key = GetKey();
75
				if (address_box.flags & 0b10)
76
				{
77
					EAX=key<<8;
78
					edit_box_key stdcall(#address_box);
79
				}
80
				if (key==13) Scan(301);
81
				break;
82
 
83
			case evReDraw:
5674 pavelyakov 84
				system.color.get();
85
				DefineAndDrawWindow(215,100,WIN_W,WIN_H,0x73,system.color.work,#header,0);
4662 leency 86
				GetProcessInfo(#Form, SelfInfo);
5493 leency 87
				if (Form.status_window>2) break;
4662 leency 88
				if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
89
				if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
4666 leency 90
				diagram.Init(20, 87, Form.cwidth - 40, Form.cheight - 87 - 28);
4662 leency 91
				Draw_Window();
92
				break;
93
 
94
			default:
95
				if (Form.width==0) break;
96
				if (http_transfer <= 0) break;
5534 hidnplayr 97
				http_receive stdcall (http_transfer);
4662 leency 98
				if (EAX == 0) {
99
					ESI = http_transfer;
100
					bufpointer = ESI.http_msg.content_ptr;
101
					bufsize = ESI.http_msg.content_received;
102
					http_free stdcall (http_transfer);
103
					http_transfer=0;
104
					strcpy(#filepath, "/tmp0/1/");
105
					strcat(#filepath, #URL+strrchr(#URL, '/'));
5493 leency 106
					if (!WriteFile(bufsize, bufpointer, #filepath))
4662 leency 107
					{
108
						strcpy(#notify_message, "File saved as ");
109
					}
110
					else
111
					{
112
						strcpy(#notify_message, "Error! Can't save file as ");
113
					}
114
					strcat(#notify_message, #filepath);
115
					notify(#notify_message);
116
					address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
117
					Draw_Window();
118
				}
119
				ESI = http_transfer;
120
				speed[speed_position] = ESI.http_msg.content_received;
121
				DrawSpeed();
122
				speed_position++;
123
		}
124
	}
125
}
126
 
127
void DrawSpeed()
128
{
129
	int i;
4666 leency 130
	int speed_in_position, speed_string;
4662 leency 131
	int max_speed, start_from;
132
	char bytes_received[70];
133
 
4666 leency 134
	diagram.Fill(0xFCF8F7);
4662 leency 135
	max_speed = speed[speed_position];
4666 leency 136
	if (speed_position < diagram.bufw) start_from = 0; else start_from = speed_position - diagram.bufw + 1;
4667 leency 137
	if (speed_position>0) for (i = 0; i <= speed_position-start_from; i++)
4662 leency 138
	{
139
		if (max_speed>0)
140
		{
4666 leency 141
			speed_in_position = diagram.bufh - 1 * speed[i+start_from] / max_speed;
142
			diagram.DrawBar(i, diagram.bufh - speed_in_position, 1, speed_in_position, 0x00A3CB);
4662 leency 143
		}
144
	}
4666 leency 145
	diagram.Show();
4662 leency 146
	if (speed_position==0) return;
147
	if (http_transfer > 0)
148
	{
149
		strcpy(#bytes_received, "Downloading... ");
150
	}
151
	else
152
	{
153
		strcpy(#bytes_received, "Downloading competle. ");
154
	}
4666 leency 155
	speed_string = ConvertSize(speed[speed_position-1]);
156
	strcat(#bytes_received, speed_string);
157
	strcat(#bytes_received, " received.");
5674 pavelyakov 158
	DrawBar(diagram.bufx, diagram.bufy + diagram.bufh + 10, diagram.bufw, 9, system.color.work);
159
	WriteText(diagram.bufx, diagram.bufy + diagram.bufh + 10, 0x80, system.color.work_text, #bytes_received);
4662 leency 160
}
161
 
162
void Draw_Window()
163
{
5674 pavelyakov 164
	DrawBar(0,0,Form.cwidth,Form.cheight,system.color.work); //bg
4666 leency 165
	if (http_transfer <= 0)
166
	{
5674 pavelyakov 167
		DrawCaptButton(diagram.bufx, 50, 120, 20, 301, system.color.work_button, system.color.work_button_text, "Start downloading");
4666 leency 168
	}
169
	else
170
	{
5674 pavelyakov 171
		DrawCaptButton(diagram.bufx, 50, 120, 20, 302, system.color.work_button, system.color.work_button_text, "Stop downloading");
4666 leency 172
	}
4662 leency 173
	if (http_transfer <= 0) && (speed_position>0)
174
	{
5674 pavelyakov 175
		DrawCaptButton(diagram.bufx+130, 50, 120, 20, 305, system.color.work_button, system.color.work_button_text, "Show in folder");
4662 leency 176
	}
5674 pavelyakov 177
	WriteText(diagram.bufx, address_box.top + 4, 0x80, system.color.work_text, "URL:");
4666 leency 178
	address_box.left = strlen("URL:")*6 + 10 + diagram.bufx;
179
	address_box.width = Form.cwidth - address_box.left - diagram.bufx - 3;
4662 leency 180
	address_box.offset=0;
181
	edit_box_draw stdcall(#address_box);
182
	DrawRectangle(address_box.left-1, address_box.top-1, address_box.width+2, 16,address_box.color);
5674 pavelyakov 183
	DrawRectangle(address_box.left-2, address_box.top-2, address_box.width+4, 18,system.color.work_graph);
4662 leency 184
 
5674 pavelyakov 185
	DrawRectangle(diagram.bufx-2, diagram.bufy-2, diagram.bufw+2, diagram.bufh+2, system.color.work_graph);
4662 leency 186
	DrawSpeed();
187
}
188
 
189
void Scan(int id)
190
{
4667 leency 191
	if (id==301) && (http_transfer <= 0) StartDownloading();
4662 leency 192
	if (id==302) StopDownloading();
193
	if (id==305) RunProgram("/sys/File managers/Eolite", "/tmp0/1/");
194
}
195
 
196
 
197
void StopDownloading()
198
{
199
	if (http_transfer<>0)
200
	{
201
		EAX = http_transfer;
202
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
203
		$push	EAX							// save it on the stack
204
		http_free stdcall (http_transfer);	// abort connection
205
		$pop	EAX
206
		mem_Free(EAX);						// free data
207
		http_transfer=0;
208
		bufsize = 0;
209
		bufpointer = mem_Free(bufpointer);
210
	}
211
	address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
4667 leency 212
	speed_position = 0;
4662 leency 213
	Draw_Window();
214
}
215
 
216
void StartDownloading()
217
{
218
	StopDownloading();
219
	speed_position = 0;
220
	if (strncmp(#URL,"http:",5)==0)
221
	{
222
		address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xededed;
5534 hidnplayr 223
		http_get stdcall (#URL, 0, 0, #accept_language);
4662 leency 224
		http_transfer = EAX;
4667 leency 225
		Draw_Window();
4662 leency 226
		if (http_transfer == 0)
227
		{
228
			StopDownloading();
229
			bufsize = 0;
230
			bufpointer = mem_Free(bufpointer);
231
			return;
232
		}
233
	}
234
	else
235
	{
5493 leency 236
		notify("File adress should starts from http://");
4662 leency 237
	}
238
}
239
 
240
 
241
 
242
stop: