Subversion Repositories svn1

Rev

Rev 293 | Rev 296 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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