1 | /****************************************************************************
|
---|
2 | ** $Id: ftpmainwindow.ui.h 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | /****************************************************************************
|
---|
12 | **
|
---|
13 | ** ui.h extension file, included from the uic-generated form implementation.
|
---|
14 | **
|
---|
15 | ** The init() function is used in place of a constructor.
|
---|
16 | ** The destroy() function is used in place of a destructor.
|
---|
17 | ** The slots uploadFile(), downloadFile(), removeFile() and connectToHost() are
|
---|
18 | ** connected with the resp. actions of the GUI.
|
---|
19 | **
|
---|
20 | *****************************************************************************/
|
---|
21 |
|
---|
22 | #include <qftp.h>
|
---|
23 | #include <qlineedit.h>
|
---|
24 | #include <qspinbox.h>
|
---|
25 | #include <qstatusbar.h>
|
---|
26 | #include <qmessagebox.h>
|
---|
27 | #include <qfiledialog.h>
|
---|
28 | #include <qprogressdialog.h>
|
---|
29 | #include <qapplication.h>
|
---|
30 |
|
---|
31 | #include "connectdialog.h"
|
---|
32 | #include "ftpviewitem.h"
|
---|
33 |
|
---|
34 | void FtpMainWindow::init()
|
---|
35 | {
|
---|
36 | stateFtp = new QLabel( tr("Unconnected"), statusBar() );
|
---|
37 | statusBar()->addWidget( stateFtp, 0, TRUE );
|
---|
38 |
|
---|
39 | ftp = new QFtp( this );
|
---|
40 | connect( ftp, SIGNAL(commandStarted(int)),
|
---|
41 | SLOT(ftp_commandStarted()) );
|
---|
42 | connect( ftp, SIGNAL(commandFinished(int,bool)),
|
---|
43 | SLOT(ftp_commandFinished()) );
|
---|
44 | connect( ftp, SIGNAL(done(bool)),
|
---|
45 | SLOT(ftp_done(bool)) );
|
---|
46 | connect( ftp, SIGNAL(stateChanged(int)),
|
---|
47 | SLOT(ftp_stateChanged(int)) );
|
---|
48 | connect( ftp, SIGNAL(listInfo(const QUrlInfo &)),
|
---|
49 | SLOT(ftp_listInfo(const QUrlInfo &)) );
|
---|
50 | connect( ftp, SIGNAL(rawCommandReply(int, const QString &)),
|
---|
51 | SLOT(ftp_rawCommandReply(int, const QString &)) );
|
---|
52 | }
|
---|
53 |
|
---|
54 | void FtpMainWindow::destroy()
|
---|
55 | {
|
---|
56 | if ( ftp->state() != QFtp::Unconnected )
|
---|
57 | ftp->close();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void FtpMainWindow::uploadFile()
|
---|
61 | {
|
---|
62 | QString fileName = QFileDialog::getOpenFileName(
|
---|
63 | QString::null,
|
---|
64 | QString::null,
|
---|
65 | this,
|
---|
66 | "upload file dialog",
|
---|
67 | tr("Choose a file to upload") );
|
---|
68 | if ( fileName.isNull() )
|
---|
69 | return;
|
---|
70 |
|
---|
71 | QFile *file = new QFile( fileName );
|
---|
72 | if ( !file->open( IO_ReadOnly ) ) {
|
---|
73 | QMessageBox::critical( this, tr("Upload error"),
|
---|
74 | tr("Can't open file '%1' for reading.").arg(fileName) );
|
---|
75 | delete file;
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | QProgressDialog progress(
|
---|
80 | tr("Uploading file..."),
|
---|
81 | tr("Cancel"),
|
---|
82 | 0,
|
---|
83 | this,
|
---|
84 | "upload progress dialog",
|
---|
85 | TRUE );
|
---|
86 | connect( ftp, SIGNAL(dataTransferProgress(int,int)),
|
---|
87 | &progress, SLOT(setProgress(int,int)) );
|
---|
88 | connect( ftp, SIGNAL(commandFinished(int,bool)),
|
---|
89 | &progress, SLOT(reset()) );
|
---|
90 | connect( &progress, SIGNAL(cancelled()),
|
---|
91 | ftp, SLOT(abort()) );
|
---|
92 |
|
---|
93 | QFileInfo fi( fileName );
|
---|
94 | ftp->put( file, fi.fileName() );
|
---|
95 | progress.exec(); // ### takes a lot of time!!!
|
---|
96 |
|
---|
97 | ftp->list();
|
---|
98 | }
|
---|
99 |
|
---|
100 | void FtpMainWindow::downloadFile()
|
---|
101 | {
|
---|
102 | FtpViewItem *item = (FtpViewItem*)remoteView->selectedItem();
|
---|
103 | if ( !item || item->isDir() )
|
---|
104 | return;
|
---|
105 |
|
---|
106 | QString fileName = QFileDialog::getSaveFileName(
|
---|
107 | item->text(0),
|
---|
108 | QString::null,
|
---|
109 | this,
|
---|
110 | "download file dialog",
|
---|
111 | tr("Save downloaded file as") );
|
---|
112 | if ( fileName.isNull() )
|
---|
113 | return;
|
---|
114 |
|
---|
115 | // create file on the heap because it has to be valid throughout the whole
|
---|
116 | // asynchronous download operation
|
---|
117 | QFile *file = new QFile( fileName );
|
---|
118 | if ( !file->open( IO_WriteOnly ) ) {
|
---|
119 | QMessageBox::critical( this, tr("Download error"),
|
---|
120 | tr("Can't open file '%1' for writing.").arg(fileName) );
|
---|
121 | delete file;
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | QProgressDialog progress(
|
---|
126 | tr("Downloading file..."),
|
---|
127 | tr("Cancel"),
|
---|
128 | 0,
|
---|
129 | this,
|
---|
130 | "download progress dialog",
|
---|
131 | TRUE );
|
---|
132 | connect( ftp, SIGNAL(dataTransferProgress(int,int)),
|
---|
133 | &progress, SLOT(setProgress(int,int)) );
|
---|
134 | connect( ftp, SIGNAL(commandFinished(int,bool)),
|
---|
135 | &progress, SLOT(reset()) );
|
---|
136 | connect( &progress, SIGNAL(cancelled()),
|
---|
137 | ftp, SLOT(abort()) );
|
---|
138 |
|
---|
139 | ftp->get( item->text(0), file );
|
---|
140 | progress.exec(); // ### takes a lot of time!!!
|
---|
141 | }
|
---|
142 |
|
---|
143 | void FtpMainWindow::removeFile()
|
---|
144 | {
|
---|
145 | FtpViewItem *item = (FtpViewItem*)remoteView->selectedItem();
|
---|
146 | if ( !item || item->isDir() )
|
---|
147 | return;
|
---|
148 |
|
---|
149 | ftp->remove( item->text(0) );
|
---|
150 | ftp->list();
|
---|
151 | }
|
---|
152 |
|
---|
153 | void FtpMainWindow::connectToHost()
|
---|
154 | {
|
---|
155 | ConnectDialog connectDialog;
|
---|
156 | if ( connectDialog.exec() == QDialog::Rejected )
|
---|
157 | return;
|
---|
158 |
|
---|
159 | remotePath->clear();
|
---|
160 | remoteView->clear();
|
---|
161 |
|
---|
162 | if ( ftp->state() != QFtp::Unconnected )
|
---|
163 | ftp->close();
|
---|
164 |
|
---|
165 | ftp->connectToHost( connectDialog.host->text(), connectDialog.port->value() );
|
---|
166 | ftp->login( connectDialog.username->text(), connectDialog.password->text() );
|
---|
167 | ftp->rawCommand( "PWD" );
|
---|
168 | ftp->list();
|
---|
169 | }
|
---|
170 |
|
---|
171 | // This slot is connected to the QComboBox::activated() signal of the
|
---|
172 | // remotePath.
|
---|
173 | void FtpMainWindow::changePath( const QString &newPath )
|
---|
174 | {
|
---|
175 | ftp->cd( newPath );
|
---|
176 | ftp->rawCommand( "PWD" );
|
---|
177 | ftp->list();
|
---|
178 | }
|
---|
179 |
|
---|
180 | // This slot is connected to the QListView::doubleClicked() and
|
---|
181 | // QListView::returnPressed() signals of the remoteView.
|
---|
182 | void FtpMainWindow::changePathOrDownload( QListViewItem *item )
|
---|
183 | {
|
---|
184 | if ( ((FtpViewItem*)item)->isDir() )
|
---|
185 | changePath( item->text(0) );
|
---|
186 | else
|
---|
187 | downloadFile();
|
---|
188 | }
|
---|
189 |
|
---|
190 | /****************************************************************************
|
---|
191 | **
|
---|
192 | ** Slots connected to signals of the QFtp class
|
---|
193 | **
|
---|
194 | *****************************************************************************/
|
---|
195 |
|
---|
196 | void FtpMainWindow::ftp_commandStarted()
|
---|
197 | {
|
---|
198 | QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
|
---|
199 | if ( ftp->currentCommand() == QFtp::List ) {
|
---|
200 | remoteView->clear();
|
---|
201 | if ( currentFtpDir != "/" )
|
---|
202 | new FtpViewItem( remoteView, FtpViewItem::Directory, "..", "", "" );
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void FtpMainWindow::ftp_commandFinished()
|
---|
207 | {
|
---|
208 | QApplication::restoreOverrideCursor();
|
---|
209 | delete ftp->currentDevice();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void FtpMainWindow::ftp_done( bool error )
|
---|
213 | {
|
---|
214 | if ( error ) {
|
---|
215 | QMessageBox::critical( this, tr("FTP Error"), ftp->errorString() );
|
---|
216 |
|
---|
217 | // If we are connected, but not logged in, it is not meaningful to stay
|
---|
218 | // connected to the server since the error is a really fatal one (login
|
---|
219 | // failed).
|
---|
220 | if ( ftp->state() == QFtp::Connected )
|
---|
221 | ftp->close();
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | void FtpMainWindow::ftp_stateChanged( int state )
|
---|
226 | {
|
---|
227 | switch ( (QFtp::State)state ) {
|
---|
228 | case QFtp::Unconnected:
|
---|
229 | stateFtp->setText( tr("Unconnected") );
|
---|
230 | break;
|
---|
231 | case QFtp::HostLookup:
|
---|
232 | stateFtp->setText( tr("Host lookup") );
|
---|
233 | break;
|
---|
234 | case QFtp::Connecting:
|
---|
235 | stateFtp->setText( tr("Connecting") );
|
---|
236 | break;
|
---|
237 | case QFtp::Connected:
|
---|
238 | stateFtp->setText( tr("Connected") );
|
---|
239 | break;
|
---|
240 | case QFtp::LoggedIn:
|
---|
241 | stateFtp->setText( tr("Logged in") );
|
---|
242 | break;
|
---|
243 | case QFtp::Closing:
|
---|
244 | stateFtp->setText( tr("Closing") );
|
---|
245 | break;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | void FtpMainWindow::ftp_listInfo( const QUrlInfo &i )
|
---|
250 | {
|
---|
251 | FtpViewItem::Type type;
|
---|
252 | if ( i.isDir() )
|
---|
253 | type = FtpViewItem::Directory;
|
---|
254 | else
|
---|
255 | type = FtpViewItem::File;
|
---|
256 |
|
---|
257 | new FtpViewItem( remoteView, type,
|
---|
258 | i.name(), QString::number(i.size()), i.lastModified().toString() );
|
---|
259 | }
|
---|
260 |
|
---|
261 | void FtpMainWindow::ftp_rawCommandReply( int code, const QString &text )
|
---|
262 | {
|
---|
263 | if ( code == 257 ) {
|
---|
264 | currentFtpDir = text.section( '"', 1, 1 );
|
---|
265 |
|
---|
266 | for ( int i = 0; i<remotePath->count(); i++ ) {
|
---|
267 | // make sure that we don't insert duplicates
|
---|
268 | if ( remotePath->text( i ) == currentFtpDir )
|
---|
269 | remotePath->removeItem( i );
|
---|
270 | }
|
---|
271 | remotePath->insertItem( currentFtpDir, 0 );
|
---|
272 | remotePath->setCurrentItem( 0 );
|
---|
273 | }
|
---|
274 | }
|
---|