| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 | #include <QtNetwork>
|
|---|
| 44 |
|
|---|
| 45 | #include "ftpwindow.h"
|
|---|
| 46 |
|
|---|
| 47 | FtpWindow::FtpWindow(QWidget *parent)
|
|---|
| 48 | : QDialog(parent), ftp(0)
|
|---|
| 49 | {
|
|---|
| 50 | ftpServerLabel = new QLabel(tr("Ftp &server:"));
|
|---|
| 51 | ftpServerLineEdit = new QLineEdit("ftp.trolltech.com");
|
|---|
| 52 | ftpServerLabel->setBuddy(ftpServerLineEdit);
|
|---|
| 53 |
|
|---|
| 54 | statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
|
|---|
| 55 |
|
|---|
| 56 | fileList = new QTreeWidget;
|
|---|
| 57 | fileList->setEnabled(false);
|
|---|
| 58 | fileList->setRootIsDecorated(false);
|
|---|
| 59 | fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time"));
|
|---|
| 60 | fileList->header()->setStretchLastSection(false);
|
|---|
| 61 |
|
|---|
| 62 | connectButton = new QPushButton(tr("Connect"));
|
|---|
| 63 | connectButton->setDefault(true);
|
|---|
| 64 |
|
|---|
| 65 | cdToParentButton = new QPushButton;
|
|---|
| 66 | cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
|
|---|
| 67 | cdToParentButton->setEnabled(false);
|
|---|
| 68 |
|
|---|
| 69 | downloadButton = new QPushButton(tr("Download"));
|
|---|
| 70 | downloadButton->setEnabled(false);
|
|---|
| 71 |
|
|---|
| 72 | quitButton = new QPushButton(tr("Quit"));
|
|---|
| 73 |
|
|---|
| 74 | buttonBox = new QDialogButtonBox;
|
|---|
| 75 | buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
|
|---|
| 76 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|---|
| 77 |
|
|---|
| 78 | progressDialog = new QProgressDialog(this);
|
|---|
| 79 |
|
|---|
| 80 | connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
|
|---|
| 81 | this, SLOT(processItem(QTreeWidgetItem *, int)));
|
|---|
| 82 | connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
|
|---|
| 83 | this, SLOT(enableDownloadButton()));
|
|---|
| 84 | connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
|
|---|
| 85 | connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect()));
|
|---|
| 86 | connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
|
|---|
| 87 | connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
|
|---|
| 88 | connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
|---|
| 89 |
|
|---|
| 90 | QHBoxLayout *topLayout = new QHBoxLayout;
|
|---|
| 91 | topLayout->addWidget(ftpServerLabel);
|
|---|
| 92 | topLayout->addWidget(ftpServerLineEdit);
|
|---|
| 93 | topLayout->addWidget(cdToParentButton);
|
|---|
| 94 | topLayout->addWidget(connectButton);
|
|---|
| 95 |
|
|---|
| 96 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
|---|
| 97 | mainLayout->addLayout(topLayout);
|
|---|
| 98 | mainLayout->addWidget(fileList);
|
|---|
| 99 | mainLayout->addWidget(statusLabel);
|
|---|
| 100 | mainLayout->addWidget(buttonBox);
|
|---|
| 101 | setLayout(mainLayout);
|
|---|
| 102 |
|
|---|
| 103 | setWindowTitle(tr("FTP"));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | QSize FtpWindow::sizeHint() const
|
|---|
| 107 | {
|
|---|
| 108 | return QSize(500, 300);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | //![0]
|
|---|
| 112 | void FtpWindow::connectOrDisconnect()
|
|---|
| 113 | {
|
|---|
| 114 | if (ftp) {
|
|---|
| 115 | ftp->abort();
|
|---|
| 116 | ftp->deleteLater();
|
|---|
| 117 | ftp = 0;
|
|---|
| 118 | //![0]
|
|---|
| 119 | fileList->setEnabled(false);
|
|---|
| 120 | cdToParentButton->setEnabled(false);
|
|---|
| 121 | downloadButton->setEnabled(false);
|
|---|
| 122 | connectButton->setEnabled(true);
|
|---|
| 123 | connectButton->setText(tr("Connect"));
|
|---|
| 124 | #ifndef QT_NO_CURSOR
|
|---|
| 125 | setCursor(Qt::ArrowCursor);
|
|---|
| 126 | #endif
|
|---|
| 127 | return;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | #ifndef QT_NO_CURSOR
|
|---|
| 131 | setCursor(Qt::WaitCursor);
|
|---|
| 132 | #endif
|
|---|
| 133 |
|
|---|
| 134 | //![1]
|
|---|
| 135 | ftp = new QFtp(this);
|
|---|
| 136 | connect(ftp, SIGNAL(commandFinished(int, bool)),
|
|---|
| 137 | this, SLOT(ftpCommandFinished(int, bool)));
|
|---|
| 138 | connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
|
|---|
| 139 | this, SLOT(addToList(const QUrlInfo &)));
|
|---|
| 140 | connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
|
|---|
| 141 | this, SLOT(updateDataTransferProgress(qint64, qint64)));
|
|---|
| 142 |
|
|---|
| 143 | fileList->clear();
|
|---|
| 144 | currentPath.clear();
|
|---|
| 145 | isDirectory.clear();
|
|---|
| 146 | //![1]
|
|---|
| 147 |
|
|---|
| 148 | //![2]
|
|---|
| 149 | QUrl url(ftpServerLineEdit->text());
|
|---|
| 150 | if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
|
|---|
| 151 | ftp->connectToHost(ftpServerLineEdit->text(), 21);
|
|---|
| 152 | ftp->login();
|
|---|
| 153 | } else {
|
|---|
| 154 | ftp->connectToHost(url.host(), url.port(21));
|
|---|
| 155 |
|
|---|
| 156 | if (!url.userName().isEmpty())
|
|---|
| 157 | ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
|
|---|
| 158 | else
|
|---|
| 159 | ftp->login();
|
|---|
| 160 | if (!url.path().isEmpty())
|
|---|
| 161 | ftp->cd(url.path());
|
|---|
| 162 | }
|
|---|
| 163 | //![2]
|
|---|
| 164 |
|
|---|
| 165 | fileList->setEnabled(true);
|
|---|
| 166 | connectButton->setEnabled(false);
|
|---|
| 167 | connectButton->setText(tr("Disconnect"));
|
|---|
| 168 | statusLabel->setText(tr("Connecting to FTP server %1...")
|
|---|
| 169 | .arg(ftpServerLineEdit->text()));
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | //![3]
|
|---|
| 173 | void FtpWindow::downloadFile()
|
|---|
| 174 | {
|
|---|
| 175 | QString fileName = fileList->currentItem()->text(0);
|
|---|
| 176 | //![3]
|
|---|
| 177 | //
|
|---|
| 178 | if (QFile::exists(fileName)) {
|
|---|
| 179 | QMessageBox::information(this, tr("FTP"),
|
|---|
| 180 | tr("There already exists a file called %1 in "
|
|---|
| 181 | "the current directory.")
|
|---|
| 182 | .arg(fileName));
|
|---|
| 183 | return;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | //![4]
|
|---|
| 187 | file = new QFile(fileName);
|
|---|
| 188 | if (!file->open(QIODevice::WriteOnly)) {
|
|---|
| 189 | QMessageBox::information(this, tr("FTP"),
|
|---|
| 190 | tr("Unable to save the file %1: %2.")
|
|---|
| 191 | .arg(fileName).arg(file->errorString()));
|
|---|
| 192 | delete file;
|
|---|
| 193 | return;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | ftp->get(fileList->currentItem()->text(0), file);
|
|---|
| 197 |
|
|---|
| 198 | progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
|
|---|
| 199 | downloadButton->setEnabled(false);
|
|---|
| 200 | progressDialog->exec();
|
|---|
| 201 | }
|
|---|
| 202 | //![4]
|
|---|
| 203 |
|
|---|
| 204 | //![5]
|
|---|
| 205 | void FtpWindow::cancelDownload()
|
|---|
| 206 | {
|
|---|
| 207 | ftp->abort();
|
|---|
| 208 | }
|
|---|
| 209 | //![5]
|
|---|
| 210 |
|
|---|
| 211 | //![6]
|
|---|
| 212 | void FtpWindow::ftpCommandFinished(int, bool error)
|
|---|
| 213 | {
|
|---|
| 214 | #ifndef QT_NO_CURSOR
|
|---|
| 215 | setCursor(Qt::ArrowCursor);
|
|---|
| 216 | #endif
|
|---|
| 217 |
|
|---|
| 218 | if (ftp->currentCommand() == QFtp::ConnectToHost) {
|
|---|
| 219 | if (error) {
|
|---|
| 220 | QMessageBox::information(this, tr("FTP"),
|
|---|
| 221 | tr("Unable to connect to the FTP server "
|
|---|
| 222 | "at %1. Please check that the host "
|
|---|
| 223 | "name is correct.")
|
|---|
| 224 | .arg(ftpServerLineEdit->text()));
|
|---|
| 225 | connectOrDisconnect();
|
|---|
| 226 | return;
|
|---|
| 227 | }
|
|---|
| 228 | statusLabel->setText(tr("Logged onto %1.")
|
|---|
| 229 | .arg(ftpServerLineEdit->text()));
|
|---|
| 230 | fileList->setFocus();
|
|---|
| 231 | downloadButton->setDefault(true);
|
|---|
| 232 | connectButton->setEnabled(true);
|
|---|
| 233 | return;
|
|---|
| 234 | }
|
|---|
| 235 | //![6]
|
|---|
| 236 |
|
|---|
| 237 | //![7]
|
|---|
| 238 | if (ftp->currentCommand() == QFtp::Login)
|
|---|
| 239 | ftp->list();
|
|---|
| 240 | //![7]
|
|---|
| 241 |
|
|---|
| 242 | //![8]
|
|---|
| 243 | if (ftp->currentCommand() == QFtp::Get) {
|
|---|
| 244 | if (error) {
|
|---|
| 245 | statusLabel->setText(tr("Canceled download of %1.")
|
|---|
| 246 | .arg(file->fileName()));
|
|---|
| 247 | file->close();
|
|---|
| 248 | file->remove();
|
|---|
| 249 | } else {
|
|---|
| 250 | statusLabel->setText(tr("Downloaded %1 to current directory.")
|
|---|
| 251 | .arg(file->fileName()));
|
|---|
| 252 | file->close();
|
|---|
| 253 | }
|
|---|
| 254 | delete file;
|
|---|
| 255 | enableDownloadButton();
|
|---|
| 256 | progressDialog->hide();
|
|---|
| 257 | //![8]
|
|---|
| 258 | //![9]
|
|---|
| 259 | } else if (ftp->currentCommand() == QFtp::List) {
|
|---|
| 260 | if (isDirectory.isEmpty()) {
|
|---|
| 261 | fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
|
|---|
| 262 | fileList->setEnabled(false);
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 | //![9]
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | //![10]
|
|---|
| 269 | void FtpWindow::addToList(const QUrlInfo &urlInfo)
|
|---|
| 270 | {
|
|---|
| 271 | QTreeWidgetItem *item = new QTreeWidgetItem;
|
|---|
| 272 | item->setText(0, urlInfo.name());
|
|---|
| 273 | item->setText(1, QString::number(urlInfo.size()));
|
|---|
| 274 | item->setText(2, urlInfo.owner());
|
|---|
| 275 | item->setText(3, urlInfo.group());
|
|---|
| 276 | item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));
|
|---|
| 277 |
|
|---|
| 278 | QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
|
|---|
| 279 | item->setIcon(0, pixmap);
|
|---|
| 280 |
|
|---|
| 281 | isDirectory[urlInfo.name()] = urlInfo.isDir();
|
|---|
| 282 | fileList->addTopLevelItem(item);
|
|---|
| 283 | if (!fileList->currentItem()) {
|
|---|
| 284 | fileList->setCurrentItem(fileList->topLevelItem(0));
|
|---|
| 285 | fileList->setEnabled(true);
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 | //![10]
|
|---|
| 289 |
|
|---|
| 290 | //![11]
|
|---|
| 291 | void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/)
|
|---|
| 292 | {
|
|---|
| 293 | QString name = item->text(0);
|
|---|
| 294 | if (isDirectory.value(name)) {
|
|---|
| 295 | fileList->clear();
|
|---|
| 296 | isDirectory.clear();
|
|---|
| 297 | currentPath += "/" + name;
|
|---|
| 298 | ftp->cd(name);
|
|---|
| 299 | ftp->list();
|
|---|
| 300 | cdToParentButton->setEnabled(true);
|
|---|
| 301 | #ifndef QT_NO_CURSOR
|
|---|
| 302 | setCursor(Qt::WaitCursor);
|
|---|
| 303 | #endif
|
|---|
| 304 | return;
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 | //![11]
|
|---|
| 308 |
|
|---|
| 309 | //![12]
|
|---|
| 310 | void FtpWindow::cdToParent()
|
|---|
| 311 | {
|
|---|
| 312 | #ifndef QT_NO_CURSOR
|
|---|
| 313 | setCursor(Qt::WaitCursor);
|
|---|
| 314 | #endif
|
|---|
| 315 | fileList->clear();
|
|---|
| 316 | isDirectory.clear();
|
|---|
| 317 | currentPath = currentPath.left(currentPath.lastIndexOf('/'));
|
|---|
| 318 | if (currentPath.isEmpty()) {
|
|---|
| 319 | cdToParentButton->setEnabled(false);
|
|---|
| 320 | ftp->cd("/");
|
|---|
| 321 | } else {
|
|---|
| 322 | ftp->cd(currentPath);
|
|---|
| 323 | }
|
|---|
| 324 | ftp->list();
|
|---|
| 325 | }
|
|---|
| 326 | //![12]
|
|---|
| 327 |
|
|---|
| 328 | //![13]
|
|---|
| 329 | void FtpWindow::updateDataTransferProgress(qint64 readBytes,
|
|---|
| 330 | qint64 totalBytes)
|
|---|
| 331 | {
|
|---|
| 332 | progressDialog->setMaximum(totalBytes);
|
|---|
| 333 | progressDialog->setValue(readBytes);
|
|---|
| 334 | }
|
|---|
| 335 | //![13]
|
|---|
| 336 |
|
|---|
| 337 | //![14]
|
|---|
| 338 | void FtpWindow::enableDownloadButton()
|
|---|
| 339 | {
|
|---|
| 340 | QTreeWidgetItem *current = fileList->currentItem();
|
|---|
| 341 | if (current) {
|
|---|
| 342 | QString currentFile = current->text(0);
|
|---|
| 343 | downloadButton->setEnabled(!isDirectory.value(currentFile));
|
|---|
| 344 | } else {
|
|---|
| 345 | downloadButton->setEnabled(false);
|
|---|
| 346 | }
|
|---|
| 347 | }
|
|---|
| 348 | //![14]
|
|---|
| 349 |
|
|---|