Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 #include "optionsWidget.hh"
54 #include <iostream>
56 
57 void OptionsWidget::startDownload( QString _url ) {
58  QUrl url(_url);
59 
60  QFileInfo urlInfo(_url);
61 
62  // Download the file to the Home Directory
63  QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
64  ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + urlInfo.fileName() );
65 
66  QString fileName = fileInfo.filePath();
67 
68  if (QFile::exists(fileName)) {
69  QFile::remove(fileName);
70  }
71 
72  file = new QFile(fileName);
73  if (!file->open(QIODevice::WriteOnly)) {
74  std::cerr << "Unable to Open local file " + fileName.toStdString() + " for writing" << std::endl;
75  delete file;
76  file = 0;
77  checkUpdateButton->setEnabled(true);
78  } else {
79  QNetworkRequest req;
80  req.setUrl(url);
81 
82  httpRequestAborted = false;
83  QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
84  if (path.isEmpty())
85  path = "/";
86 
87  statusLabel->setText(tr("Getting Versions file from Server"));
88 
89  if ( ! progressDialog_ ) {
90  progressDialog_ = new QProgressDialog(this);
91  connect(progressDialog_, SIGNAL(canceled()), this, SLOT(cancelDownload()));
92  }
93  progressDialog_->setWindowTitle(tr("HTTP"));
94  progressDialog_->setLabelText(tr("Downloading %1.").arg(fileName));
95  progressDialog_->show();
96 
97  downloadRep_ = networkMan_->get(req);
98 
99  connect(downloadRep_, SIGNAL(error(QNetworkReply::NetworkError)),
100  this, SLOT(showError(QNetworkReply::NetworkError)));
101  connect(downloadRep_,SIGNAL(downloadProgress(qint64 , qint64 )),
102  this,SLOT(updateDataReadProgress(qint64 , qint64 )));
103 
104  checkUpdateButton->setEnabled(false);
105  }
106 
107 }
108 
109 void OptionsWidget::authentication ( QNetworkReply* _reply, QAuthenticator* _authenticator )
110 {
111  if ( ! updateUser->text().isEmpty() )
112  _authenticator->setUser(updateUser->text());
113 
114  if ( ! updatePass->text().isEmpty() )
115  _authenticator->setPassword(updatePass->text());
116 }
117 
118 void OptionsWidget::httpRequestFinished(QNetworkReply* _qnr)
119 {
120  if (_qnr != downloadRep_)
121  return;
122 
123  QNetworkReply::NetworkError error = _qnr->error();
124 
125  if (httpRequestAborted) {
126  if (file) {
127  file->close();
128  file->remove();
129  delete file;
130  file = 0;
131  }
132 
133  progressDialog_->hide();
134  checkUpdateButton->setEnabled(true);
135  return;
136  }
137 
138  progressDialog_->hide();
139  file->close();
140 
141  delete(progressDialog_);
142  progressDialog_ = 0;
143 
144  if (error != QNetworkReply::NoError) {
145  file->remove();
146  } else {
147  QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
148  statusLabel->setText(tr("Downloaded %1").arg(file->fileName() ));
149  }
150 
151  checkUpdateButton->setEnabled(true);
152  delete file;
153  file = 0;
154 
155  if ( error == QNetworkReply::NoError ) {
156  if ( downloadType == VERSIONS_FILE )
157  compareVersions();
158  if ( downloadType == PLUGIN )
159  updateComponent();
160  }
161 }
162 
163 void OptionsWidget::cancelDownload()
164 {
165  statusLabel->setText(tr("download canceled."));
166  httpRequestAborted = true;
167  if (downloadRep_)
168  downloadRep_->abort();
169  checkUpdateButton->setEnabled(true);
170 }
171 
172 void OptionsWidget::updateDataReadProgress(qint64 _bytesReceived, qint64 _bytesTotal)
173 {
174  if (httpRequestAborted)
175  return;
176 
177  if (progressDialog_) {
178  progressDialog_->setMaximum(_bytesTotal);
179  progressDialog_->setValue(_bytesReceived);
180  }
181 }
182 
183 void OptionsWidget::showError(QNetworkReply::NetworkError _error)
184 {
185  if (_error == QNetworkReply::NoError)
186  return;
187  statusLabel->setText(tr("Download failed: %1.").arg(downloadRep_->errorString()));
188  QMessageBox::information(this, tr("HTTP Error"),
189  tr("Download failed: %1.")
190  .arg(downloadRep_->errorString()) + file->fileName() );
191 }
192 
193 
194 
195 
196 
void showError(QNetworkReply::NetworkError _error)
error occured while downloading
Definition: downloader.cc:183
void startDownload(QString _url)
Starts the download of the given file.
Definition: downloader.cc:57
void authentication(QNetworkReply *_reply, QAuthenticator *_authenticator)
authentication
Definition: downloader.cc:109
void compareVersions()
Compares the versions from the downloaded Versions file with the current versions.