1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
21 #include "DownloadManager.h"
24 DownloadManager::DownloadManager(QObject *parent) :
25 QObject(parent) // Superclass construct
29 DownloadManager::DownloadManager(QUrl url, QString outfile, QObject *parent) :
30 QObject(parent), // Superclass construct
33 file_(QString(outfile))
35 file_.open(QIODevice::WriteOnly);
39 DownloadManager::~DownloadManager()
42 delete reply_; reply_ = NULL;
46 void DownloadManager::start()
48 reply_ = manager_.get(QNetworkRequest(url_));
50 connect(reply_, SIGNAL(readyRead()), this, SLOT(on_readyRead()));
51 connect(reply_, SIGNAL(downloadProgress (qint64 , qint64)), SLOT(on_downloadProgress(qint64, qint64)));
52 connect(reply_, SIGNAL(finished()), this, SLOT(on_replyFinished()));
56 void DownloadManager::on_readyRead()
58 //qDebug() << "on_readyRead()";
60 qWarning() << "DownloadManager::on_readyRead(): Writing to file: "
61 << filepath_ << " failed.";
65 void DownloadManager::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
67 static double last = 0;
69 double prog = static_cast<double>(bytesReceived) / static_cast<double>(bytesTotal);
70 if ((prog-last) >= 0.01) {
72 emit progress(static_cast<int>(100*prog));
73 } else if (bytesReceived == bytesTotal) {
79 void DownloadManager::on_replyFinished()
83 emit finished(filepath_);
87 bool DownloadManager::WriteToFile()
89 QByteArray readData = reply_->readAll();
91 if (readData.isEmpty()) {
92 qDebug() << "on_replyFinished(): No data available for reading";
94 // If writing failed, see error message.
95 if (file_.write(readData) == -1) {
96 qWarning() << file_.error();