Subversion Repositories svn1-original

Rev

Rev 299 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
297 david 1
#include <QtGui>
2
#include <QLineEdit>
3
#include <QLabel>
4
#include "qmhttppath.h"
5
 
6
#include <QtGui>
7
#include <QtNetwork>
8
 
9
#include "Qmhttppath.h"
10
#include "ui_authenticationdialog.h"
11
 
12
Qmhttppath::Qmhttppath(QWidget *parent)
13
    : QDialog(parent)
14
{
15
    this->resize(500,40);
16
    urlLineEdit = new QLineEdit("http://www.mccays.com.au/theevent/Essential Content/current_entries.htm");
17
 
18
    urlLabel = new QLabel(tr("&URL:"));
19
    urlLabel->setBuddy(urlLineEdit);
20
    statusLabel = new QLabel(tr("Please enter the URL of a file you want to download."));
21
 
22
    downloadButton = new QPushButton(tr("Download"));
23
    downloadButton->setDefault(true);
24
    quitButton = new QPushButton(tr("Quit"));
25
    quitButton->setAutoDefault(false);
26
 
27
    buttonBox = new QDialogButtonBox;
28
    buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
29
    buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
30
 
31
    progressDialog = new QProgressDialog(this);
32
 
33
    connect(urlLineEdit, SIGNAL(textChanged(QString)),
34
            this, SLOT(enableDownloadButton()));
35
 
36
    connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
37
            this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
38
#ifndef QT_NO_OPENSSL
39
    connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
40
            this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
41
#endif
42
    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
43
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
44
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
45
 
46
    QHBoxLayout *topLayout = new QHBoxLayout;
47
    topLayout->addWidget(urlLabel);
48
    topLayout->addWidget(urlLineEdit);
49
 
50
    QVBoxLayout *mainLayout = new QVBoxLayout;
51
    mainLayout->addLayout(topLayout);
52
    mainLayout->addWidget(statusLabel);
53
    mainLayout->addWidget(buttonBox);
54
    setLayout(mainLayout);
55
 
56
    setWindowTitle(tr("Download web entries via HTTP"));
57
    urlLineEdit->setFocus();
58
}
59
 
60
void Qmhttppath::startRequest(QUrl url)
61
{
62
    reply = qnam.get(QNetworkRequest(url));
63
    connect(reply, SIGNAL(finished()),
64
            this, SLOT(httpFinished()));
65
    connect(reply, SIGNAL(readyRead()),
66
            this, SLOT(httpReadyRead()));
67
    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
68
            this, SLOT(updateDataReadProgress(qint64,qint64)));
69
}
70
 
71
void Qmhttppath::downloadFile()
72
{
73
    url = urlLineEdit->text();
74
 
75
    QFileInfo fileInfo(url.path());
76
    QString fileName = fileInfo.fileName();
77
    if (fileName.isEmpty())
78
        fileName = "index.html";
79
 
80
    //    if (QFile::exists(fileName)) {
81
    //        if (QMessageBox::question(this, tr("HTTP"),
82
    //                                  tr("There already exists a file called %1 in "
83
    //                                     "the current directory. Overwrite?").arg(fileName),
84
    //                                  QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
85
    //            == QMessageBox::No)
86
    //            return;
87
    //        QFile::remove(fileName);
88
    //    }
89
 
90
    //    file = new QFile(fileName);
91
    //    if (!file->open(QIODevice::WriteOnly)) {
92
    //        QMessageBox::information(this, tr("HTTP"),
93
    //                                 tr("Unable to save the file %1: %2.")
94
    //                                 .arg(fileName).arg(file->errorString()));
95
    //        delete file;
96
    //        file = 0;
97
    //        return;
98
    //    }
99
 
100
 
101
    progressDialog->setWindowTitle(tr("HTTP"));
102
    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
103
    downloadButton->setEnabled(false);
104
 
105
    // schedule the request
106
    httpRequestAborted = false;
107
    startRequest(url);
108
}
109
 
110
void Qmhttppath::cancelDownload()
111
{
112
    statusLabel->setText(tr("Download canceled."));
113
    httpRequestAborted = true;
114
    reply->abort();
115
    downloadButton->setEnabled(true);
116
}
117
 
118
void Qmhttppath::httpFinished()
119
{
120
    if (httpRequestAborted) {
121
        //        if (file) {
122
        //            file->close();
123
        //            file->remove();
124
        //            delete file;
125
        //            file = 0;
126
        //        }
127
        reply->deleteLater();
128
        progressDialog->hide();
129
        return;
130
    }
131
 
132
    progressDialog->hide();
133
    //    file->flush();
134
    //    file->close();
135
 
136
    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
137
    if (reply->error()) {
138
        //file->remove();
139
        QMessageBox::information(this, tr("HTTP"),
140
                                 tr("Download failed: %1.")
141
                                 .arg(reply->errorString()));
142
        downloadButton->setEnabled(true);
143
    } else if (!redirectionTarget.isNull()) {
144
        QUrl newUrl = url.resolved(redirectionTarget.toUrl());
145
        if (QMessageBox::question(this, tr("HTTP"),
146
                                  tr("Redirect to %1 ?").arg(newUrl.toString()),
147
                                  QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
148
            url = newUrl;
149
            reply->deleteLater();
150
            //file->open(QIODevice::WriteOnly);
151
            //file->resize(0);
152
            startRequest(url);
153
            return;
154
        }
155
    } else {
156
        QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
157
        statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
158
        downloadButton->setEnabled(true);
159
 
160
        // Save all the data that we have
161
        allData = reply->readAll();
162
    }
163
 
164
    reply->deleteLater();
165
    reply = 0;
166
    //delete file;
167
    //file = 0;
168
    accept();
169
}
170
 
171
void Qmhttppath::httpReadyRead()
172
{
173
    // this slot gets called every time the QNetworkReply has new data.
174
    // We read all of its new data and write it into the file.
175
    // That way we use less RAM than when reading it at the finished()
176
    // signal of the QNetworkReply
177
 
178
}
179
 
180
void Qmhttppath::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
181
{
182
    if (httpRequestAborted)
183
        return;
184
 
185
    progressDialog->setMaximum(totalBytes);
186
    progressDialog->setValue(bytesRead);
187
}
188
 
189
void Qmhttppath::enableDownloadButton()
190
{
191
    downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
192
}
193
 
194
void Qmhttppath::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator)
195
{
196
    QDialog dlg;
197
    Ui::Dialog ui;
198
    ui.setupUi(&dlg);
199
    dlg.adjustSize();
200
    ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host()));
201
 
202
    // Did the URL have information? Fill the UI
203
    // This is only relevant if the URL-supplied credentials were wrong
204
    ui.userEdit->setText(url.userName());
205
    ui.passwordEdit->setText(url.password());
206
 
207
    if (dlg.exec() == QDialog::Accepted) {
208
        authenticator->setUser(ui.userEdit->text());
209
        authenticator->setPassword(ui.passwordEdit->text());
210
    }
211
}
212
 
213
#ifndef QT_NO_OPENSSL
214
void Qmhttppath::sslErrors(QNetworkReply*,const QList<QSslError> &errors)
215
{
216
    QString errorString;
217
    foreach (const QSslError &error, errors) {
218
        if (!errorString.isEmpty())
219
            errorString += ", ";
220
        errorString += error.errorString();
221
    }
222
 
223
    if (QMessageBox::warning(this, tr("HTTP"),
224
                             tr("One or more SSL errors has occurred: %1").arg(errorString),
225
                             QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
226
        reply->ignoreSslErrors();
227
    }
228
}
229
#endif