Subversion Repositories Kolibri OS

Rev

Rev 7766 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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