aboutsummaryrefslogtreecommitdiffstats
path: root/apps/windoze/CBuilder5/InstallMgr/StatusFrm.cpp
blob: 0abc9da950502898624fc0fceedfae5faf16a51c (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

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




struct FtpFile {
  char *filename;
  FILE *stream;
};

int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
	 return -1; /* failure, can't open file to write */
  }
  return fwrite(buffer, size, nmemb, out->stream);
}





//---------------------------------------------------------------------------
__fastcall TStatusForm::TStatusForm(TComponent* Owner)
		: TForm(Owner) {
	suffix = "";
	passive = false;
	curl_global_init(CURL_GLOBAL_DEFAULT);
}


__fastcall TStatusForm::~TStatusForm() {
	curl_global_cleanup();
}


//---------------------------------------------------------------------------
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()
{
	CURL *curl;
	CURLcode res;
	struct FtpFile ftpfile={
		"curl.tar.gz", /* name to store the file as if succesful */
		NULL
	};

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

	if (curl) {
    /* Get curl 7.9.2 from sunet.se's FTP site: */
    curl_easy_setopt(curl, CURLOPT_URL,
				 "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_FILE, &ftpfile);

    /* Switch on full protocol/debug output */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);

    if(CURLE_OK != res) {
	 /* we failed */
	 fprintf(stderr, "curl told us %d\n", res);
    }
  }

  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */

/*
	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
	}
	
	if (!Terminated) {
		if (dirTransfer) {
			StatusForm->ftpCon->ChangeDir(src.c_str());
			TStringList *dirText = new TStringList();
			StatusForm->ftpCon->List(dirText, "*", true);
			TIdFTPListItems *dirList = StatusForm->ftpCon->DirectoryListing;
			for (int i = 0; i < dirList->Count; i++) {
				if (dirList->Items[i]->ItemType != ditDirectory) {
					buffer = dest + "/" + (dirList->Items[i]->FileName.c_str());
//                           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->Count).c_str();
						buffer2 += "): ";
						buffer2 += (dirList->Items[i]->FileName.c_str());
		//				SWLog::systemlog->LogInformation("%s", buffer.c_str());
						TMainForm::createParent(buffer.c_str());	// make sure parent directory exists
						Synchronize((TThreadMethod)&PreDownload1);
						try {
							StatusForm->currentFileSize = dirList->Items[i]->Size;
							StatusForm->ftpCon->Get(dirList->Items[i]->FileName.c_str(), buffer.c_str(), true, false);
						}
						catch(...) {
							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
						}
						if (Terminated)
							break;
					}
				}
			}
		}
		else {
			Synchronize((TThreadMethod)&PreDownload2);
			try {
				StatusForm->ftpCon->Get(src.c_str(), dest.c_str(), true, false);
			}
			catch(...) {StatusForm->abort = true;}
		}
		try {
			StatusForm->ftpCon->Disconnect();
		}
		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();
}
*/
//---------------------------------------------------------------------------