Developer Documentation

downloader.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46
47 #include "optionsWidget.hh"
48 #include <iostream>
49 #include <OpenFlipper/common/GlobalOptions.hh>
50 #include <QMessageBox>
51
52 void OptionsWidget::startDownload( QString _url ) {
53 QUrl url(_url);
54
55 QFileInfo urlInfo(_url);
56
57 // Download the file to the Home Directory
58 QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
59 ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + urlInfo.fileName() );
60
61 QString fileName = fileInfo.filePath();
62
63 if (QFile::exists(fileName)) {
64 QFile::remove(fileName);
65 }
66
67 file = new QFile(fileName);
68 if (!file->open(QIODevice::WriteOnly)) {
69 std::cerr << "Unable to Open local file " + fileName.toStdString() + " for writing" << std::endl;
70 delete file;
71 file = 0;
72 checkUpdateButton->setEnabled(true);
73 } else {
74 QNetworkRequest req;
75 req.setUrl(url);
76
77 httpRequestAborted = false;
78 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
79 if (path.isEmpty())
80 path = "/";
81
82 statusLabel->setText(tr("Getting Versions file from Server"));
83
84 if ( ! progressDialog_ ) {
85 progressDialog_ = new QProgressDialog(this);
86 connect(progressDialog_, SIGNAL(canceled()), this, SLOT(cancelDownload()));
87 }
88 progressDialog_->setWindowTitle(tr("HTTP"));
89 progressDialog_->setLabelText(tr("Downloading %1.").arg(fileName));
90 progressDialog_->show();
91
92 downloadRep_ = networkMan_->get(req);
93
94 connect(downloadRep_, SIGNAL(error(QNetworkReply::NetworkError)),
95 this, SLOT(showError(QNetworkReply::NetworkError)));
96 connect(downloadRep_,SIGNAL(downloadProgress(qint64 , qint64 )),
97 this,SLOT(updateDataReadProgress(qint64 , qint64 )));
98
99 checkUpdateButton->setEnabled(false);
100 }
101
102 }
103
104 void OptionsWidget::authentication ( QNetworkReply* _reply, QAuthenticator* _authenticator )
105 {
106 if ( ! updateUser->text().isEmpty() )
107 _authenticator->setUser(updateUser->text());
108
109 if ( ! updatePass->text().isEmpty() )
110 _authenticator->setPassword(updatePass->text());
111 }
112
113 void OptionsWidget::httpRequestFinished(QNetworkReply* _qnr)
114 {
115 if (_qnr != downloadRep_)
116 return;
117
118 QNetworkReply::NetworkError error = _qnr->error();
119
120 if (httpRequestAborted) {
121 if (file) {
122 file->close();
123 file->remove();
124 delete file;
125 file = 0;
126 }
127
128 progressDialog_->hide();
129 checkUpdateButton->setEnabled(true);
130 return;
131 }
132
133 progressDialog_->hide();
134 file->close();
135
136 delete(progressDialog_);
137 progressDialog_ = 0;
138
139 if (error != QNetworkReply::NoError) {
140 file->remove();
141 } else {
142 QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
143 statusLabel->setText(tr("Downloaded %1").arg(file->fileName() ));
144 }
145
146 checkUpdateButton->setEnabled(true);
147 delete file;
148 file = 0;
149
150 if ( error == QNetworkReply::NoError ) {
151 if ( downloadType == VERSIONS_FILE )
152 compareVersions();
153 if ( downloadType == PLUGIN )
154 updateComponent();
155 }
156 }
157
158 void OptionsWidget::cancelDownload()
159 {
160 statusLabel->setText(tr("download canceled."));
161 httpRequestAborted = true;
162 if (downloadRep_)
163 downloadRep_->abort();
164 checkUpdateButton->setEnabled(true);
165 }
166
167 void OptionsWidget::updateDataReadProgress(qint64 _bytesReceived, qint64 _bytesTotal)
168 {
169 if (httpRequestAborted)
170 return;
171
172 if (progressDialog_) {
173 progressDialog_->setMaximum(_bytesTotal);
174 progressDialog_->setValue(_bytesReceived);
175 }
176 }
177
178 void OptionsWidget::showError(QNetworkReply::NetworkError _error)
179 {
180 if (_error == QNetworkReply::NoError)
181 return;
182 statusLabel->setText(tr("Download failed: %1.").arg(downloadRep_->errorString()));
183 QMessageBox::information(this, tr("HTTP Error"),
184 tr("Download failed: %1.")
185 .arg(downloadRep_->errorString()) + file->fileName() );
186 }
187
188
189
190
191
GlobalOptions.hh
OptionsWidget::showError
void showError(QNetworkReply::NetworkError _error)
error occured while downloading
Definition: downloader.cc:178
OptionsWidget::compareVersions
void compareVersions()
Compares the versions from the downloaded Versions file with the current versions.
Definition: optionsWidget.cc:944
OptionsWidget::startDownload
void startDownload(QString _url)
Starts the download of the given file.
Definition: downloader.cc:52
OptionsWidget::authentication
void authentication(QNetworkReply *_reply, QAuthenticator *_authenticator)
authentication
Definition: downloader.cc:104