Subversion Repositories svn1

Rev

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