Subversion Repositories svn1-original

Rev

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

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