aboutsummaryrefslogtreecommitdiffstats
path: root/apps/windoze/CBuilder5/InstallMgr/StatusFrm.cpp
blob: d3e5c34bf0855ebfeb5549749288404aefb0f42d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "StatusFrm.h"
#include "MainFrm.h"
#include <installmgr.h>
#include <vector>

using namespace std;

//---------------------------------------------------------------------------
#pragma package(smart_init)
//#pragma link "IdBaseComponent"
//#pragma link "IdComponent"
//#pragma link "IdFTP"
//#pragma link "IdTCPClient"
//#pragma link "IdTCPConnection"
#pragma resource "*.dfm"
TStatusForm *StatusForm;


void status_callback(double dltotal, double dlnow) {
	StatusForm->statusBar->Caption = ::IntToStr((long)dlnow) + " bytes out of " + ::IntToStr((long)dltotal) + " transferred (" + IntToStr((int)((float)(dlnow + 1) / (float)(dltotal) * 100)) + "%)";
	StatusForm->statusBar->Repaint();
}


//---------------------------------------------------------------------------
__fastcall TStatusForm::TStatusForm(TComponent* Owner)
		: TForm(Owner) {
	suffix = "";
	passive = false;
	ftpCon = new TFTPCon();
}


__fastcall TStatusForm::~TStatusForm() {
	delete ftpCon;
}


//---------------------------------------------------------------------------
void __fastcall TStatusForm::FormShow(TObject *Sender)
{
	abort = false;
	ftpThread = new TFTPThread(ist, src.c_str(), dest.c_str(), dirTransfer, passive, true, suffix.c_str());
	ftpThread->OnTerminate = Cleanup;
	ftpThread->Resume();
}
//---------------------------------------------------------------------------
__fastcall TStatusForm::TFTPThread::TFTPThread(InstallSourceTab *iist, const char *isrc, const char *idest, bool idirTransfer, bool CreateSuspended, bool ipassive, const char *isuffix)
	: TThread(CreateSuspended)
{
	ist = iist;
	src = isrc;
	dest = idest;
	suffix = isuffix;
	dirTransfer = idirTransfer;
	Priority = tpNormal;
	passive = ipassive;
	FreeOnTerminate = true;
	Synchronize((TThreadMethod)&CreateFTPObject);
}

__fastcall TStatusForm::TFTPThread::~TFTPThread()
{
}


void __fastcall TStatusForm::TFTPThread::Execute()
{
	void *session = FTPOpenSession();

	Synchronize((TThreadMethod)&PreConnect);
	StatusForm->ftpCon->Host = ist->Source.c_str();
	StatusForm->ftpCon->Username = "ftp";
	StatusForm->ftpCon->Password = "installmgr@user.com";
	StatusForm->ftpCon->Passive = passive;

	
/*
	try {
		StatusForm->ftpCon->Connect(true, -1);
		StatusForm->ftpCon->TransferType = ftBinary;
		StatusForm->ftpCon->ChangeDir(ist->Directory.c_str());
	}
	catch(...) {
		MessageBox(0, "Can't connect.  Please check your configuration.", "Connection Error", MB_OK);
		StatusForm->Button1Click(0);	// abort thread
	}
*/
	string url = "ftp://" + ist->Source + ist->Directory.c_str() + "/"; //dont forget the final slash
	if (FTPURLGetFile(session, "dirlist", url.c_str())) {
		MessageBox(0, "Can't connect.  Please check your configuration.", "Connection Error", MB_OK);
		StatusForm->Button1Click(0);	// abort thread
	}
	if (!Terminated) {
		if (dirTransfer) {
//			StatusForm->ftpCon->ChangeDir(src.c_str());


	string url = "ftp://" + ist->Source + ist->Directory.c_str() + "/" + src + "/"; //dont forget the final slash
	vector<struct ftpparse> dirList = FTPURLGetDir(session, url.c_str());

	if (!dirList.size()) {
		MessageBox(0, "Can't connect.  Please check your configuration.", "Connection Error", MB_OK);
		StatusForm->Button1Click(0);	// abort thread
	}
					
//			StatusForm->ftpCon->List(dirText, "*", true);
//			TIdFTPListItems *dirList = StatusForm->ftpCon->DirectoryListing;
			for (int i = 0; i < dirList.size(); i++) {
				if (dirList[i].flagtrycwd != 1) {
					buffer = dest + "/" + (dirList[i].name);
//                           files->Strings[i].c_str();
					if (!strcmp(&buffer.c_str()[buffer.length()-suffix.length()], suffix.c_str())) {
						buffer2 = "Downloading (";
						buffer2 += IntToStr(i+1).c_str();
						buffer2 += " of ";
						buffer2 += IntToStr(dirList.size()).c_str();
						buffer2 += "): ";
						buffer2 += (dirList[i].name);
		//				SWLog::systemlog->LogInformation("%s", buffer.c_str());
						TMainForm::createParent(buffer.c_str());	// make sure parent directory exists
						Synchronize((TThreadMethod)&PreDownload1);
						try {
							StatusForm->currentFileSize = dirList[i].size;
//							StatusForm->ftpCon->Get(dirList->Items[i]->FileName.c_str(), buffer.c_str(), true, false);

							string url = "ftp://" + ist->Source + ist->Directory.c_str() + "/" + src + "/" + dirList[i].name; //dont forget the final slash

							if (FTPURLGetFile(session, buffer.c_str(), url.c_str(), status_callback)) {
								MessageBox(0, "Can't download file.  If you have not done so recently, you might try pressing the Refresh from Remote Source button.", "Download Error", MB_OK);
								StatusForm->Button1Click(0);	// abort thread
							}
						}
						catch (...) {}
						if (Terminated)
							break;
					}
				}
			}
		}
		else {
			Synchronize((TThreadMethod)&PreDownload2);
			try {
//				StatusForm->ftpCon->Get(src.c_str(), dest.c_str(), true, false);
				string url = "ftp://" + ist->Source + ist->Directory.c_str() + "/" + src.c_str(); //dont forget the final slash
				if (FTPURLGetFile(session, dest.c_str(), url.c_str(), status_callback)) {
					MessageBox(0, "Can't connect.  Please check your configuration.", "Connection Error", MB_OK);
					StatusForm->Button1Click(0);	// abort thread
				}
			}
			catch(...) {StatusForm->abort = true;}
		}
		try {
//			StatusForm->ftpCon->Disconnect();
			FTPCloseSession(session);
		}
		catch(...){}
	}
}


void __fastcall TStatusForm::TFTPThread::FTPLinkPacketRecvd(TObject *Sender)
{
//	Synchronize((TThreadMethod)&UpdateBytes);
}


//void __fastcall TStatusForm::TFTPThread::UpdateBytes(void) {
void __fastcall TStatusForm::UpdateBytes(void) {
	StatusForm->statusBar->Caption = IntToStr(StatusForm->currentByteCount) + " bytes out of " + IntToStr(StatusForm->currentFileSize) + " transferred (" + IntToStr((int)((float)(StatusForm->currentByteCount + 1) / (float)(StatusForm->currentFileSize + 1) * 100)) + "%)";
	StatusForm->statusBar->Repaint();
}


void __fastcall TStatusForm::TFTPThread::CreateFTPObject(void)
{
//	FTPLink = new TNMFTP(0);
}


void __fastcall TStatusForm::TFTPThread::PreConnect(void)
{
	buffer = "Connecting to server at ";
	buffer += ist->Source.c_str();
	buffer += "...";
	StatusForm->actionBar->Caption = buffer.c_str();
	StatusForm->statusBar->Caption = "";
	StatusForm->Repaint();
}


void __fastcall TStatusForm::TFTPThread::PreDownload1(void)
{
//	SWLog::systemlog->LogInformation("Creating parent dir: %s", buffer.c_str());
//	SWLog::systemlog->LogInformation("Return: %d", ret);
	StatusForm->actionBar->Caption = buffer2.c_str();
	StatusForm->statusBar->Caption = "";
	StatusForm->Repaint();
}


void __fastcall TStatusForm::TFTPThread::PreDownload2(void)
{
	buffer = "Downloading: ";
	buffer += src.c_str();
	StatusForm->actionBar->Caption = buffer.c_str();
	StatusForm->statusBar->Caption = "";
	StatusForm->Repaint();
	MainForm->createParent(dest.c_str());	// make sure parent directory exists
}


void __fastcall TStatusForm::Cleanup(TObject *Sender)
//void __fastcall TStatusForm::TFTPThread::Cleanup()
{
	if (abort)
		StatusForm->ModalResult = mrCancel;
	else	StatusForm->ModalResult = mrOk;
//	StatusForm->Close();
}

void __fastcall TStatusForm::FormClose(TObject *Sender, TCloseAction &Action)
{
	suffix = "";
	MainForm->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TStatusForm::Button1Click(TObject *Sender)
{
	abort = true;
//	ftpCon->Abort();
	ftpThread->Terminate();
}
//---------------------------------------------------------------------------

/*
void __fastcall TStatusForm::ftpConWork(TObject *Sender,
	 TWorkMode AWorkMode, const int AWorkCount)
{
	currentByteCount = AWorkCount;
//	Synchronize((TThreadMethod)&UpdateBytes);
	UpdateBytes();
}
*/
//---------------------------------------------------------------------------