Subversion Repositories Kolibri OS

Rev

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

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