Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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