~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/network/ftp/ftpwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
#include <QtNetwork>
 
31
 
 
32
#include "ftpwindow.h"
 
33
 
 
34
FtpWindow::FtpWindow(QWidget *parent)
 
35
    : QDialog(parent)
 
36
{
 
37
    ftpServerLabel = new QLabel(tr("Ftp &server:"));
 
38
    ftpServerLineEdit = new QLineEdit("ftp.trolltech.com");
 
39
    ftpServerLabel->setBuddy(ftpServerLineEdit);
 
40
 
 
41
    statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
 
42
 
 
43
    fileList = new QListWidget;
 
44
 
 
45
    connectButton = new QPushButton(tr("Connect"));
 
46
    connectButton->setDefault(true);
 
47
 
 
48
    downloadButton = new QPushButton(tr("Download"));
 
49
    downloadButton->setEnabled(false);
 
50
 
 
51
    cdToParentButton = new QPushButton;
 
52
    cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
 
53
    cdToParentButton->setEnabled(false);
 
54
 
 
55
    quitButton = new QPushButton(tr("Quit"));
 
56
 
 
57
    ftp = new QFtp(this);
 
58
 
 
59
    progressDialog = new QProgressDialog(this);
 
60
 
 
61
    connect(ftpServerLineEdit, SIGNAL(textChanged(const QString &)),
 
62
            this, SLOT(enableConnectButton()));
 
63
    connect(fileList, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
 
64
            this, SLOT(processItem(QListWidgetItem *)));
 
65
    connect(fileList, SIGNAL(itemEntered(QListWidgetItem *)),
 
66
            this, SLOT(processItem(QListWidgetItem *)));
 
67
    connect(fileList, SIGNAL(itemSelectionChanged()),
 
68
            this, SLOT(enableDownloadButton()));
 
69
    connect(ftp, SIGNAL(commandFinished(int, bool)),
 
70
            this, SLOT(ftpCommandFinished(int, bool)));
 
71
    connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
 
72
            this, SLOT(addToList(const QUrlInfo &)));
 
73
    connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
 
74
            this, SLOT(updateDataTransferProgress(qint64, qint64)));
 
75
    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
 
76
    connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToFtpServer()));
 
77
    connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
 
78
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
 
79
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
 
80
 
 
81
    QHBoxLayout *topLayout = new QHBoxLayout;
 
82
    topLayout->addWidget(ftpServerLabel);
 
83
    topLayout->addWidget(ftpServerLineEdit);
 
84
    topLayout->addWidget(cdToParentButton);
 
85
 
 
86
    QHBoxLayout *buttonLayout = new QHBoxLayout;
 
87
    buttonLayout->addStretch(1);
 
88
    buttonLayout->addWidget(downloadButton);
 
89
    buttonLayout->addWidget(connectButton);
 
90
    buttonLayout->addWidget(quitButton);
 
91
 
 
92
    QVBoxLayout *mainLayout = new QVBoxLayout;
 
93
    mainLayout->addLayout(topLayout);
 
94
    mainLayout->addWidget(fileList);
 
95
    mainLayout->addWidget(statusLabel);
 
96
    mainLayout->addLayout(buttonLayout);
 
97
    setLayout(mainLayout);
 
98
 
 
99
    setWindowTitle(tr("FTP"));
 
100
}
 
101
 
 
102
void FtpWindow::connectToFtpServer()
 
103
{
 
104
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
105
    ftp->connectToHost(ftpServerLineEdit->text());
 
106
    ftp->login();
 
107
    ftp->list();
 
108
    statusLabel->setText(tr("Connecting to FTP server %1...")
 
109
                         .arg(ftpServerLineEdit->text()));
 
110
}
 
111
 
 
112
void FtpWindow::downloadFile()
 
113
{
 
114
    QString fileName = fileList->currentItem()->text();
 
115
 
 
116
    if (QFile::exists(fileName)) {
 
117
        QMessageBox::information(this, tr("FTP"),
 
118
                                 tr("There already exists a file called %1 in "
 
119
                                    "the current directory.")
 
120
                                 .arg(fileName));
 
121
        return;
 
122
    }
 
123
 
 
124
    file = new QFile(fileName);
 
125
    if (!file->open(QIODevice::WriteOnly)) {
 
126
        QMessageBox::information(this, tr("FTP"),
 
127
                                 tr("Unable to save the file %1: %2.")
 
128
                                 .arg(fileName).arg(file->errorString()));
 
129
        delete file;
 
130
        return;
 
131
    }
 
132
 
 
133
    ftp->get(fileList->currentItem()->text(), file);
 
134
 
 
135
    progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
 
136
    progressDialog->show();
 
137
    downloadButton->setEnabled(false);
 
138
}
 
139
 
 
140
void FtpWindow::cancelDownload()
 
141
{
 
142
    ftp->abort();
 
143
}
 
144
 
 
145
void FtpWindow::ftpCommandFinished(int, bool error)
 
146
{
 
147
    if (ftp->currentCommand() == QFtp::ConnectToHost) {
 
148
        if (error) {
 
149
            QApplication::restoreOverrideCursor();
 
150
            QMessageBox::information(this, tr("FTP"),
 
151
                                     tr("Unable to connect to the FTP server "
 
152
                                        "at %1. Please check that the host "
 
153
                                        "name is correct.")
 
154
                                     .arg(ftpServerLineEdit->text()));
 
155
            return;
 
156
        }
 
157
 
 
158
        statusLabel->setText(tr("Logged onto %1.")
 
159
                             .arg(ftpServerLineEdit->text()));
 
160
        fileList->setFocus();
 
161
        connectButton->setEnabled(false);
 
162
        downloadButton->setDefault(true);
 
163
        return;
 
164
    }
 
165
 
 
166
    if (ftp->currentCommand() == QFtp::Get) {
 
167
        QApplication::restoreOverrideCursor();
 
168
        if (error) {
 
169
            statusLabel->setText(tr("Canceled download of %1.")
 
170
                                 .arg(file->fileName()));
 
171
            file->close();
 
172
            file->remove();
 
173
        } else {
 
174
            statusLabel->setText(tr("Downloaded %1 to current directory.")
 
175
                                 .arg(file->fileName()));
 
176
            file->close();
 
177
        }
 
178
        delete file;
 
179
        enableDownloadButton();
 
180
    } else if (ftp->currentCommand() == QFtp::List) {
 
181
        QApplication::restoreOverrideCursor();
 
182
        if (isDirectory.isEmpty()) {
 
183
            fileList->addItem(tr("<empty>"));
 
184
            fileList->setEnabled(false);
 
185
        }
 
186
    }
 
187
}
 
188
 
 
189
void FtpWindow::addToList(const QUrlInfo &urlInfo)
 
190
{
 
191
    QListWidgetItem *item = new QListWidgetItem;
 
192
    item->setText(urlInfo.name());
 
193
    QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
 
194
    item->setIcon(pixmap);
 
195
 
 
196
    isDirectory[urlInfo.name()] = urlInfo.isDir();
 
197
    fileList->addItem(item);
 
198
    if (!fileList->currentItem()) {
 
199
        fileList->setCurrentItem(fileList->item(0));
 
200
        fileList->setEnabled(true);
 
201
    }
 
202
}
 
203
 
 
204
void FtpWindow::processItem(QListWidgetItem *item)
 
205
{
 
206
    QString name = item->text();
 
207
    if (isDirectory.value(name)) {
 
208
        fileList->clear();
 
209
        isDirectory.clear();
 
210
        currentPath += "/" + name;
 
211
        ftp->cd(name);
 
212
        ftp->list();
 
213
        cdToParentButton->setEnabled(true);
 
214
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
215
        return;
 
216
    }
 
217
}
 
218
 
 
219
void FtpWindow::cdToParent()
 
220
{
 
221
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
222
    fileList->clear();
 
223
    isDirectory.clear();
 
224
    currentPath = currentPath.left(currentPath.lastIndexOf('/'));
 
225
    if (currentPath.isEmpty()) {
 
226
        cdToParentButton->setEnabled(false);
 
227
        ftp->cd("/");
 
228
    } else {
 
229
        ftp->cd(currentPath);
 
230
    }
 
231
    ftp->list();
 
232
}
 
233
 
 
234
void FtpWindow::updateDataTransferProgress(qint64 readBytes,
 
235
                                           qint64 totalBytes)
 
236
{
 
237
    progressDialog->setMaximum(totalBytes);
 
238
    progressDialog->setValue(readBytes);
 
239
}
 
240
 
 
241
void FtpWindow::enableConnectButton()
 
242
{
 
243
    connectButton->setEnabled(!ftpServerLineEdit->text().isEmpty());
 
244
}
 
245
 
 
246
void FtpWindow::enableDownloadButton()
 
247
{
 
248
    QListWidgetItem *current = fileList->currentItem();
 
249
    if (current) {
 
250
        QString currentFile = current->text();
 
251
        downloadButton->setEnabled(!isDirectory.value(currentFile));
 
252
    } else {
 
253
        downloadButton->setEnabled(false);
 
254
    }
 
255
}