Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5493 leency 1
#ifdef LANG_RUS
2
	#define DL_WINDOW_HEADER "Download Manager"
3
	#define START_DOWNLOADING "Start downloading"
4
	#define STOP_DOWNLOADING "Stop downloading"
5
	#define FILE_SAVED_AS "'Download manager\nFile saved as "
6
	#define DOWNLOADING_COMPLETE "Downloading complete."
7
#else
8
	#define DL_WINDOW_HEADER "Download Manager"
9
	#define START_DOWNLOADING "Start downloading"
10
	#define STOP_DOWNLOADING "Stop downloading"
11
	#define FILE_SAVED_AS "'Download manager\nFile saved as "
12
	#define DOWNLOADING_COMPLETE "Downloading complete."
13
#endif
14
 
15
proc_info DL_Form;
16
system_colors sc;
17
char DL_URL[10000];
18
dword DL_bufpointer;
19
dword DL_bufsize;
20
dword DL_http_transfer;
21
dword DL_http_buffer;
22
char filepath[4096];
23
int downloaded_size, full_size;
24
int	mouse_twbi;
25
edit_box DL_address_box = {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(DL_URL),#DL_URL,#mouse_twbi,2,19,19};
26
 
27
char save_to[4096] = "/tmp0/1/Downloads/";
28
byte cleft = 10;
29
 
30
 
31
void Downloader()
32
{
33
	int key, btn;
34
	mouse m;
35
	char notify_message[4296];
36
 
37
	if (DL_URL[0]) {
38
		StartDownloading();
39
	}
40
	else strcpy(#DL_URL, "http://");
41
	DL_address_box.size = DL_address_box.pos = DL_address_box.shift = DL_address_box.shift_old = strlen(#DL_URL);
42
 
43
	downloaded_size = full_size = 0;
44
 
45
	SetEventMask(0x27);
46
	loop()
47
	{
48
		WaitEventTimeout(40);
49
		switch(EAX & 0xFF)
50
		{
51
			CASE evMouse:
52
				if (!CheckActiveProcess(DL_Form.ID)) break;
53
				if (DL_http_transfer <= 0) edit_box_mouse stdcall (#DL_address_box);
54
				break;
55
 
56
			case evButton:
57
				btn=GetButtonID();
58
				if (btn==1)	ExitProcess();
59
				DL_Scan(btn);
60
				break;
61
 
62
			case evKey:
63
				key = GetKey();
64
				if (DL_address_box.flags & 0b10)
65
				{
66
					EAX=key<<8;
67
					edit_box_key stdcall(#DL_address_box);
68
				}
69
				if (key==13) DL_Scan(301);
70
				break;
71
 
72
			case evReDraw:
73
				sc.get();
74
				DefineAndDrawWindow(215, 100, 420, 150, 0x74, sc.work, DL_WINDOW_HEADER, 0);
75
				GetProcessInfo(#DL_Form, SelfInfo);
76
				if (DL_Form.status_window>2) break;
77
				if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
78
				if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
79
				DL_Draw_Window();
80
				break;
81
 
82
			default:
83
				if (DL_Form.width==0) break;
84
				if (DL_http_transfer <= 0) break;
85
				http_process stdcall (DL_http_transfer);
86
				if (EAX == 0) {
87
					ESI = DL_http_transfer;
88
					DL_bufpointer = ESI.http_msg.content_ptr;
89
					DL_bufsize = ESI.http_msg.content_received;
90
					http_free stdcall (DL_http_transfer);
91
					DL_http_transfer=0;
92
					strcpy(#filepath, #save_to);
93
					strcat(#filepath, #DL_URL+strrchr(#DL_URL, '/'));
94
					if (WriteFile(DL_bufsize, DL_bufpointer, #filepath)==0)
95
					{
96
						strcpy(#notify_message, FILE_SAVED_AS);
97
						strcat(#notify_message, #filepath);
98
						strcat(#notify_message, "' -Dt");
99
					}
100
					else
101
					{
102
						strcpy(#notify_message, "'Download manager\nError! Can\96t save file as ");
103
						strcat(#notify_message, #filepath);
104
						strcat(#notify_message, "' -Et");
105
					}
106
					notify(#notify_message);
107
					DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xFFFfff;
108
					DL_Draw_Window();
109
				}
110
				ESI = DL_http_transfer;
111
				downloaded_size = ESI.http_msg.content_received;
112
				full_size = ESI.http_msg.content_length;
113
				DrawDownloading();
114
		}
115
	}
116
}
117
 
118
void DrawDownloading()
119
{
120
	dword tmp;
121
	char bytes_received[70], percent[30];
122
 
123
	percent[0]=0;
124
 
125
	if (DL_http_transfer > 0)
126
	{
127
		strcpy(#bytes_received, "Downloading... ");
128
		tmp = ConvertSizeToKb(downloaded_size);
129
		strcat(#bytes_received, tmp);
130
		strcat(#bytes_received, " received.");
131
		if (full_size>0)
132
		{
133
			tmp = itoa(downloaded_size * 100 / full_size);
134
			strcpy(#percent, tmp);
135
			strcat(#percent, " \x25");
136
		}
137
	}
138
	else
139
	{
140
		strcpy(#bytes_received, DOWNLOADING_COMPLETE);
141
	}
142
	DrawBar(cleft, 90, DL_Form.cwidth - cleft, 9, sc.work);
143
	WriteText(cleft, 90, 0x80, sc.work_text, #bytes_received);
144
	WriteText(DL_Form.cwidth - 50, 90, 0x80, sc.work_text, #percent);
145
}
146
 
147
void DL_Draw_Window()
148
{
149
	DrawBar(0,0,DL_Form.cwidth,DL_Form.cheight,sc.work); //bg
150
	if (DL_http_transfer <= 0)
151
	{
152
		DrawCaptButton(cleft, 50, 120, 20, 301, sc.work_button, sc.work_button_text, START_DOWNLOADING);
153
	}
154
	else
155
	{
156
		DrawCaptButton(cleft, 50, 120, 20, 302, sc.work_button, sc.work_button_text, STOP_DOWNLOADING);
157
	}
158
	if (DL_http_transfer <= 0)
159
	{
160
		DrawCaptButton(cleft+130, 50, 120, 20, 305, sc.work_button, sc.work_button_text, "Show in folder");
161
		DrawCaptButton(cleft+260, 50, 120, 20, 306, sc.work_button, sc.work_button_text, "Open file");
162
	}
163
	WriteText(cleft, DL_address_box.top + 4, 0x80, sc.work_text, "URL:");
164
	DL_address_box.left = strlen("URL:")*6 + 10 + cleft;
165
	DL_address_box.width = DL_Form.cwidth - DL_address_box.left - cleft - 3;
166
	DL_address_box.offset=0;
167
	edit_box_draw stdcall(#DL_address_box);
168
	DrawRectangle(DL_address_box.left-1, DL_address_box.top-1, DL_address_box.width+2, 16,DL_address_box.color);
169
	DrawRectangle(DL_address_box.left-2, DL_address_box.top-2, DL_address_box.width+4, 18,sc.work_graph);
170
 
171
	DrawDownloading();
172
}
173
 
174
void DL_Scan(int id)
175
{
176
	if (id==301) && (DL_http_transfer <= 0) StartDownloading();
177
	if (id==302) StopDownloading();
178
	if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
179
	if (id==306) RunProgram("@open", #filepath);
180
}
181
 
182
 
183
void StopDownloading()
184
{
185
	if (DL_http_transfer<>0)
186
	{
187
		EAX = DL_http_transfer;
188
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
189
		$push	EAX							// save it on the stack
190
		http_free stdcall (DL_http_transfer);	// abort connection
191
		$pop	EAX
192
		mem_Free(EAX);						// free data
193
		DL_http_transfer=0;
194
		DL_bufsize = 0;
195
		DL_bufpointer = mem_Free(DL_bufpointer);
196
		downloaded_size = full_size = 0;
197
	}
198
	DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xFFFfff;
199
	DL_Draw_Window();
200
}
201
 
202
void StartDownloading()
203
{
204
	StopDownloading();
205
	if (strncmp(#DL_URL,"http:",5)==0)
206
	{
207
		DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xededed;
208
		http_get stdcall (#DL_URL, #accept_language);
209
		DL_http_transfer = EAX;
210
		DL_Draw_Window();
211
		if (DL_http_transfer == 0)
212
		{
213
			StopDownloading();
214
			DL_bufsize = 0;
215
			DL_bufpointer = mem_Free(DL_bufpointer);
216
			return;
217
		}
218
	}
219
	else
220
	{
221
		notify("File adress should starts from http://");
222
	}
223
}