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
|
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
/****************************************************************************
**
** ui.h extension file, included from the uic-generated form implementation.
**
** The init() function is used in place of a constructor.
** The destroy() function is used in place of a destructor.
** The slots uploadFile(), downloadFile(), removeFile() and connectToHost() are
** connected with the resp. actions of the GUI.
**
*****************************************************************************/
#include <ntqftp.h>
#include <ntqlineedit.h>
#include <ntqspinbox.h>
#include <tqstatusbar.h>
#include <ntqmessagebox.h>
#include <ntqfiledialog.h>
#include <ntqprogressdialog.h>
#include <ntqapplication.h>
#include "connectdialog.h"
#include "ftpviewitem.h"
void FtpMainWindow::init()
{
stateFtp = new TQLabel( tr("Unconnected"), statusBar() );
statusBar()->addWidget( stateFtp, 0, TRUE );
ftp = new TQFtp( this );
connect( ftp, TQ_SIGNAL(commandStarted(int)),
TQ_SLOT(ftp_commandStarted()) );
connect( ftp, TQ_SIGNAL(commandFinished(int,bool)),
TQ_SLOT(ftp_commandFinished()) );
connect( ftp, TQ_SIGNAL(done(bool)),
TQ_SLOT(ftp_done(bool)) );
connect( ftp, TQ_SIGNAL(stateChanged(int)),
TQ_SLOT(ftp_stateChanged(int)) );
connect( ftp, TQ_SIGNAL(listInfo(const TQUrlInfo &)),
TQ_SLOT(ftp_listInfo(const TQUrlInfo &)) );
connect( ftp, TQ_SIGNAL(rawCommandReply(int, const TQString &)),
TQ_SLOT(ftp_rawCommandReply(int, const TQString &)) );
}
void FtpMainWindow::destroy()
{
if ( ftp->state() != TQFtp::Unconnected )
ftp->close();
}
void FtpMainWindow::uploadFile()
{
TQString fileName = TQFileDialog::getOpenFileName(
TQString::null,
TQString::null,
this,
"upload file dialog",
tr("Choose a file to upload") );
if ( fileName.isNull() )
return;
TQFile *file = new TQFile( fileName );
if ( !file->open( IO_ReadOnly ) ) {
TQMessageBox::critical( this, tr("Upload error"),
tr("Can't open file '%1' for reading.").arg(fileName) );
delete file;
return;
}
TQProgressDialog progress(
tr("Uploading file..."),
tr("Cancel"),
0,
this,
"upload progress dialog",
TRUE );
connect( ftp, TQ_SIGNAL(dataTransferProgress(int,int)),
&progress, TQ_SLOT(setProgress(int,int)) );
connect( ftp, TQ_SIGNAL(commandFinished(int,bool)),
&progress, TQ_SLOT(reset()) );
connect( &progress, TQ_SIGNAL(cancelled()),
ftp, TQ_SLOT(abort()) );
TQFileInfo fi( fileName );
ftp->put( file, fi.fileName() );
progress.exec(); // ### takes a lot of time!!!
ftp->list();
}
void FtpMainWindow::downloadFile()
{
FtpViewItem *item = (FtpViewItem*)remoteView->selectedItem();
if ( !item || item->isDir() )
return;
TQString fileName = TQFileDialog::getSaveFileName(
item->text(0),
TQString::null,
this,
"download file dialog",
tr("Save downloaded file as") );
if ( fileName.isNull() )
return;
// create file on the heap because it has to be valid throughout the whole
// asynchronous download operation
TQFile *file = new TQFile( fileName );
if ( !file->open( IO_WriteOnly ) ) {
TQMessageBox::critical( this, tr("Download error"),
tr("Can't open file '%1' for writing.").arg(fileName) );
delete file;
return;
}
TQProgressDialog progress(
tr("Downloading file..."),
tr("Cancel"),
0,
this,
"download progress dialog",
TRUE );
connect( ftp, TQ_SIGNAL(dataTransferProgress(int,int)),
&progress, TQ_SLOT(setProgress(int,int)) );
connect( ftp, TQ_SIGNAL(commandFinished(int,bool)),
&progress, TQ_SLOT(reset()) );
connect( &progress, TQ_SIGNAL(cancelled()),
ftp, TQ_SLOT(abort()) );
ftp->get( item->text(0), file );
progress.exec(); // ### takes a lot of time!!!
}
void FtpMainWindow::removeFile()
{
FtpViewItem *item = (FtpViewItem*)remoteView->selectedItem();
if ( !item || item->isDir() )
return;
ftp->remove( item->text(0) );
ftp->list();
}
void FtpMainWindow::connectToHost()
{
ConnectDialog connectDialog;
if ( connectDialog.exec() == TQDialog::Rejected )
return;
remotePath->clear();
remoteView->clear();
if ( ftp->state() != TQFtp::Unconnected )
ftp->close();
ftp->connectToHost( connectDialog.host->text(), connectDialog.port->value() );
ftp->login( connectDialog.username->text(), connectDialog.password->text() );
ftp->rawCommand( "PWD" );
ftp->list();
}
// This slot is connected to the TQComboBox::activated() signal of the
// remotePath.
void FtpMainWindow::changePath( const TQString &newPath )
{
ftp->cd( newPath );
ftp->rawCommand( "PWD" );
ftp->list();
}
// This slot is connected to the TQListView::doubleClicked() and
// TQListView::returnPressed() signals of the remoteView.
void FtpMainWindow::changePathOrDownload( TQListViewItem *item )
{
if ( ((FtpViewItem*)item)->isDir() )
changePath( item->text(0) );
else
downloadFile();
}
/****************************************************************************
**
** Slots connected to signals of the TQFtp class
**
*****************************************************************************/
void FtpMainWindow::ftp_commandStarted()
{
TQApplication::setOverrideCursor( TQCursor(TQt::WaitCursor) );
if ( ftp->currentCommand() == TQFtp::List ) {
remoteView->clear();
if ( currentFtpDir != "/" )
new FtpViewItem( remoteView, FtpViewItem::Directory, "..", "", "" );
}
}
void FtpMainWindow::ftp_commandFinished()
{
TQApplication::restoreOverrideCursor();
delete ftp->currentDevice();
}
void FtpMainWindow::ftp_done( bool error )
{
if ( error ) {
TQMessageBox::critical( this, tr("FTP Error"), ftp->errorString() );
// If we are connected, but not logged in, it is not meaningful to stay
// connected to the server since the error is a really fatal one (login
// failed).
if ( ftp->state() == TQFtp::Connected )
ftp->close();
}
}
void FtpMainWindow::ftp_stateChanged( int state )
{
switch ( (TQFtp::State)state ) {
case TQFtp::Unconnected:
stateFtp->setText( tr("Unconnected") );
break;
case TQFtp::HostLookup:
stateFtp->setText( tr("Host lookup") );
break;
case TQFtp::Connecting:
stateFtp->setText( tr("Connecting") );
break;
case TQFtp::Connected:
stateFtp->setText( tr("Connected") );
break;
case TQFtp::LoggedIn:
stateFtp->setText( tr("Logged in") );
break;
case TQFtp::Closing:
stateFtp->setText( tr("Closing") );
break;
}
}
void FtpMainWindow::ftp_listInfo( const TQUrlInfo &i )
{
FtpViewItem::Type type;
if ( i.isDir() )
type = FtpViewItem::Directory;
else
type = FtpViewItem::File;
new FtpViewItem( remoteView, type,
i.name(), TQString::number(i.size()), i.lastModified().toString() );
}
void FtpMainWindow::ftp_rawCommandReply( int code, const TQString &text )
{
if ( code == 257 ) {
currentFtpDir = text.section( '"', 1, 1 );
for ( int i = 0; i<remotePath->count(); i++ ) {
// make sure that we don't insert duplicates
if ( remotePath->text( i ) == currentFtpDir )
remotePath->removeItem( i );
}
remotePath->insertItem( currentFtpDir, 0 );
remotePath->setCurrentItem( 0 );
}
}
|