author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Thu, 02 Sep 2010 21:20:32 +0300 | |
changeset 34 | a33bf25e6f72 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QtNetwork> |
|
43 |
||
44 |
#include "ftpwindow.h" |
|
45 |
||
46 |
FtpWindow::FtpWindow(QWidget *parent) |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
47 |
: QDialog(parent), ftp(0), networkSession(0) |
0 | 48 |
{ |
49 |
ftpServerLabel = new QLabel(tr("Ftp &server:")); |
|
50 |
ftpServerLineEdit = new QLineEdit("ftp.qt.nokia.com"); |
|
51 |
ftpServerLabel->setBuddy(ftpServerLineEdit); |
|
52 |
||
53 |
statusLabel = new QLabel(tr("Please enter the name of an FTP server.")); |
|
54 |
#ifdef Q_OS_SYMBIAN |
|
55 |
// Use word wrapping to fit the text on screen |
|
56 |
statusLabel->setWordWrap( true ); |
|
57 |
#endif |
|
58 |
||
59 |
fileList = new QTreeWidget; |
|
60 |
fileList->setEnabled(false); |
|
61 |
fileList->setRootIsDecorated(false); |
|
62 |
fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time")); |
|
63 |
fileList->header()->setStretchLastSection(false); |
|
64 |
||
65 |
connectButton = new QPushButton(tr("Connect")); |
|
66 |
connectButton->setDefault(true); |
|
67 |
||
68 |
cdToParentButton = new QPushButton; |
|
69 |
cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png")); |
|
70 |
cdToParentButton->setEnabled(false); |
|
71 |
||
72 |
downloadButton = new QPushButton(tr("Download")); |
|
73 |
downloadButton->setEnabled(false); |
|
74 |
||
75 |
quitButton = new QPushButton(tr("Quit")); |
|
76 |
||
77 |
buttonBox = new QDialogButtonBox; |
|
78 |
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole); |
|
79 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
|
80 |
||
81 |
progressDialog = new QProgressDialog(this); |
|
82 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
this, SLOT(processItem(QTreeWidgetItem*,int))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), |
0 | 86 |
this, SLOT(enableDownloadButton())); |
87 |
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); |
|
88 |
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect())); |
|
89 |
connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent())); |
|
90 |
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); |
|
91 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
|
92 |
||
93 |
QHBoxLayout *topLayout = new QHBoxLayout; |
|
94 |
topLayout->addWidget(ftpServerLabel); |
|
95 |
topLayout->addWidget(ftpServerLineEdit); |
|
96 |
#ifndef Q_OS_SYMBIAN |
|
97 |
topLayout->addWidget(cdToParentButton); |
|
98 |
topLayout->addWidget(connectButton); |
|
99 |
#else |
|
100 |
// Make app better lookin on small screen |
|
101 |
QHBoxLayout *topLayout2 = new QHBoxLayout; |
|
102 |
topLayout2->addWidget(cdToParentButton); |
|
103 |
topLayout2->addWidget(connectButton); |
|
104 |
#endif |
|
105 |
||
106 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
107 |
mainLayout->addLayout(topLayout); |
|
108 |
#ifdef Q_OS_SYMBIAN |
|
109 |
// Make app better lookin on small screen |
|
110 |
mainLayout->addLayout(topLayout2); |
|
111 |
#endif |
|
112 |
mainLayout->addWidget(fileList); |
|
113 |
mainLayout->addWidget(statusLabel); |
|
114 |
mainLayout->addWidget(buttonBox); |
|
115 |
setLayout(mainLayout); |
|
116 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
117 |
QNetworkConfigurationManager manager; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
118 |
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
119 |
// Get saved network configuration |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
120 |
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
121 |
settings.beginGroup(QLatin1String("QtNetwork")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
122 |
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
123 |
settings.endGroup(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
124 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
125 |
// If the saved network configuration is not currently discovered use the system default |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
126 |
QNetworkConfiguration config = manager.configurationFromIdentifier(id); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
127 |
if ((config.state() & QNetworkConfiguration::Discovered) != |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
128 |
QNetworkConfiguration::Discovered) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
129 |
config = manager.defaultConfiguration(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
130 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
131 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
132 |
networkSession = new QNetworkSession(config, this); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
133 |
connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnectButton())); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
134 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
135 |
connectButton->setEnabled(false); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
136 |
statusLabel->setText(tr("Opening network session.")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
137 |
networkSession->open(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
138 |
} |
0 | 139 |
|
140 |
setWindowTitle(tr("FTP")); |
|
141 |
} |
|
142 |
||
143 |
QSize FtpWindow::sizeHint() const |
|
144 |
{ |
|
145 |
return QSize(500, 300); |
|
146 |
} |
|
147 |
||
148 |
//![0] |
|
149 |
void FtpWindow::connectOrDisconnect() |
|
150 |
{ |
|
151 |
if (ftp) { |
|
152 |
ftp->abort(); |
|
153 |
ftp->deleteLater(); |
|
154 |
ftp = 0; |
|
155 |
//![0] |
|
156 |
fileList->setEnabled(false); |
|
157 |
cdToParentButton->setEnabled(false); |
|
158 |
downloadButton->setEnabled(false); |
|
159 |
connectButton->setEnabled(true); |
|
160 |
connectButton->setText(tr("Connect")); |
|
161 |
#ifndef QT_NO_CURSOR |
|
162 |
setCursor(Qt::ArrowCursor); |
|
163 |
#endif |
|
164 |
statusLabel->setText(tr("Please enter the name of an FTP server.")); |
|
165 |
return; |
|
166 |
} |
|
167 |
||
168 |
#ifndef QT_NO_CURSOR |
|
169 |
setCursor(Qt::WaitCursor); |
|
170 |
#endif |
|
171 |
||
172 |
//![1] |
|
173 |
ftp = new QFtp(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
connect(ftp, SIGNAL(commandFinished(int,bool)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
this, SLOT(ftpCommandFinished(int,bool))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
connect(ftp, SIGNAL(listInfo(QUrlInfo)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
this, SLOT(addToList(QUrlInfo))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
connect(ftp, SIGNAL(dataTransferProgress(qint64,qint64)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
this, SLOT(updateDataTransferProgress(qint64,qint64))); |
0 | 180 |
|
181 |
fileList->clear(); |
|
182 |
currentPath.clear(); |
|
183 |
isDirectory.clear(); |
|
184 |
//![1] |
|
185 |
||
186 |
//![2] |
|
187 |
QUrl url(ftpServerLineEdit->text()); |
|
188 |
if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) { |
|
189 |
ftp->connectToHost(ftpServerLineEdit->text(), 21); |
|
190 |
ftp->login(); |
|
191 |
} else { |
|
192 |
ftp->connectToHost(url.host(), url.port(21)); |
|
193 |
||
194 |
if (!url.userName().isEmpty()) |
|
195 |
ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password()); |
|
196 |
else |
|
197 |
ftp->login(); |
|
198 |
if (!url.path().isEmpty()) |
|
199 |
ftp->cd(url.path()); |
|
200 |
} |
|
201 |
//![2] |
|
202 |
||
203 |
fileList->setEnabled(true); |
|
204 |
connectButton->setEnabled(false); |
|
205 |
connectButton->setText(tr("Disconnect")); |
|
206 |
statusLabel->setText(tr("Connecting to FTP server %1...") |
|
207 |
.arg(ftpServerLineEdit->text())); |
|
208 |
} |
|
209 |
||
210 |
//![3] |
|
211 |
void FtpWindow::downloadFile() |
|
212 |
{ |
|
213 |
QString fileName = fileList->currentItem()->text(0); |
|
214 |
//![3] |
|
215 |
// |
|
216 |
if (QFile::exists(fileName)) { |
|
217 |
QMessageBox::information(this, tr("FTP"), |
|
218 |
tr("There already exists a file called %1 in " |
|
219 |
"the current directory.") |
|
220 |
.arg(fileName)); |
|
221 |
return; |
|
222 |
} |
|
223 |
||
224 |
//![4] |
|
225 |
file = new QFile(fileName); |
|
226 |
if (!file->open(QIODevice::WriteOnly)) { |
|
227 |
QMessageBox::information(this, tr("FTP"), |
|
228 |
tr("Unable to save the file %1: %2.") |
|
229 |
.arg(fileName).arg(file->errorString())); |
|
230 |
delete file; |
|
231 |
return; |
|
232 |
} |
|
233 |
||
234 |
ftp->get(fileList->currentItem()->text(0), file); |
|
235 |
||
236 |
progressDialog->setLabelText(tr("Downloading %1...").arg(fileName)); |
|
237 |
downloadButton->setEnabled(false); |
|
238 |
progressDialog->exec(); |
|
239 |
} |
|
240 |
//![4] |
|
241 |
||
242 |
//![5] |
|
243 |
void FtpWindow::cancelDownload() |
|
244 |
{ |
|
245 |
ftp->abort(); |
|
246 |
} |
|
247 |
//![5] |
|
248 |
||
249 |
//![6] |
|
250 |
void FtpWindow::ftpCommandFinished(int, bool error) |
|
251 |
{ |
|
252 |
#ifndef QT_NO_CURSOR |
|
253 |
setCursor(Qt::ArrowCursor); |
|
254 |
#endif |
|
255 |
||
256 |
if (ftp->currentCommand() == QFtp::ConnectToHost) { |
|
257 |
if (error) { |
|
258 |
QMessageBox::information(this, tr("FTP"), |
|
259 |
tr("Unable to connect to the FTP server " |
|
260 |
"at %1. Please check that the host " |
|
261 |
"name is correct.") |
|
262 |
.arg(ftpServerLineEdit->text())); |
|
263 |
connectOrDisconnect(); |
|
264 |
return; |
|
265 |
} |
|
266 |
statusLabel->setText(tr("Logged onto %1.") |
|
267 |
.arg(ftpServerLineEdit->text())); |
|
268 |
fileList->setFocus(); |
|
269 |
downloadButton->setDefault(true); |
|
270 |
connectButton->setEnabled(true); |
|
271 |
return; |
|
272 |
} |
|
273 |
//![6] |
|
274 |
||
275 |
//![7] |
|
276 |
if (ftp->currentCommand() == QFtp::Login) |
|
277 |
ftp->list(); |
|
278 |
//![7] |
|
279 |
||
280 |
//![8] |
|
281 |
if (ftp->currentCommand() == QFtp::Get) { |
|
282 |
if (error) { |
|
283 |
statusLabel->setText(tr("Canceled download of %1.") |
|
284 |
.arg(file->fileName())); |
|
285 |
file->close(); |
|
286 |
file->remove(); |
|
287 |
} else { |
|
288 |
statusLabel->setText(tr("Downloaded %1 to current directory.") |
|
289 |
.arg(file->fileName())); |
|
290 |
file->close(); |
|
291 |
} |
|
292 |
delete file; |
|
293 |
enableDownloadButton(); |
|
294 |
progressDialog->hide(); |
|
295 |
//![8] |
|
296 |
//![9] |
|
297 |
} else if (ftp->currentCommand() == QFtp::List) { |
|
298 |
if (isDirectory.isEmpty()) { |
|
299 |
fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>"))); |
|
300 |
fileList->setEnabled(false); |
|
301 |
} |
|
302 |
} |
|
303 |
//![9] |
|
304 |
} |
|
305 |
||
306 |
//![10] |
|
307 |
void FtpWindow::addToList(const QUrlInfo &urlInfo) |
|
308 |
{ |
|
309 |
QTreeWidgetItem *item = new QTreeWidgetItem; |
|
310 |
item->setText(0, urlInfo.name()); |
|
311 |
item->setText(1, QString::number(urlInfo.size())); |
|
312 |
item->setText(2, urlInfo.owner()); |
|
313 |
item->setText(3, urlInfo.group()); |
|
314 |
item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy")); |
|
315 |
||
316 |
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png"); |
|
317 |
item->setIcon(0, pixmap); |
|
318 |
||
319 |
isDirectory[urlInfo.name()] = urlInfo.isDir(); |
|
320 |
fileList->addTopLevelItem(item); |
|
321 |
if (!fileList->currentItem()) { |
|
322 |
fileList->setCurrentItem(fileList->topLevelItem(0)); |
|
323 |
fileList->setEnabled(true); |
|
324 |
} |
|
325 |
} |
|
326 |
//![10] |
|
327 |
||
328 |
//![11] |
|
329 |
void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/) |
|
330 |
{ |
|
331 |
QString name = item->text(0); |
|
332 |
if (isDirectory.value(name)) { |
|
333 |
fileList->clear(); |
|
334 |
isDirectory.clear(); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
335 |
currentPath += '/'; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
336 |
currentPath += name; |
0 | 337 |
ftp->cd(name); |
338 |
ftp->list(); |
|
339 |
cdToParentButton->setEnabled(true); |
|
340 |
#ifndef QT_NO_CURSOR |
|
341 |
setCursor(Qt::WaitCursor); |
|
342 |
#endif |
|
343 |
return; |
|
344 |
} |
|
345 |
} |
|
346 |
//![11] |
|
347 |
||
348 |
//![12] |
|
349 |
void FtpWindow::cdToParent() |
|
350 |
{ |
|
351 |
#ifndef QT_NO_CURSOR |
|
352 |
setCursor(Qt::WaitCursor); |
|
353 |
#endif |
|
354 |
fileList->clear(); |
|
355 |
isDirectory.clear(); |
|
356 |
currentPath = currentPath.left(currentPath.lastIndexOf('/')); |
|
357 |
if (currentPath.isEmpty()) { |
|
358 |
cdToParentButton->setEnabled(false); |
|
359 |
ftp->cd("/"); |
|
360 |
} else { |
|
361 |
ftp->cd(currentPath); |
|
362 |
} |
|
363 |
ftp->list(); |
|
364 |
} |
|
365 |
//![12] |
|
366 |
||
367 |
//![13] |
|
368 |
void FtpWindow::updateDataTransferProgress(qint64 readBytes, |
|
369 |
qint64 totalBytes) |
|
370 |
{ |
|
371 |
progressDialog->setMaximum(totalBytes); |
|
372 |
progressDialog->setValue(readBytes); |
|
373 |
} |
|
374 |
//![13] |
|
375 |
||
376 |
//![14] |
|
377 |
void FtpWindow::enableDownloadButton() |
|
378 |
{ |
|
379 |
QTreeWidgetItem *current = fileList->currentItem(); |
|
380 |
if (current) { |
|
381 |
QString currentFile = current->text(0); |
|
382 |
downloadButton->setEnabled(!isDirectory.value(currentFile)); |
|
383 |
} else { |
|
384 |
downloadButton->setEnabled(false); |
|
385 |
} |
|
386 |
} |
|
387 |
//![14] |
|
388 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
389 |
void FtpWindow::enableConnectButton() |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
390 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
391 |
// Save the used configuration |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
392 |
QNetworkConfiguration config = networkSession->configuration(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
393 |
QString id; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
394 |
if (config.type() == QNetworkConfiguration::UserChoice) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
395 |
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
396 |
else |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
397 |
id = config.identifier(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
398 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
399 |
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
400 |
settings.beginGroup(QLatin1String("QtNetwork")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
401 |
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
402 |
settings.endGroup(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
403 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
404 |
connectButton->setEnabled(networkSession->isOpen()); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
405 |
statusLabel->setText(tr("Please enter the name of an FTP server.")); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
406 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
407 |