1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "DownloadManager.h"
26 DownloadManager::DownloadManager(QObject *parent) :
27 QObject(parent) // Superclass construct
31 DownloadManager::DownloadManager(QUrl url, QString outfile, QObject *parent) :
32 QObject(parent), // Superclass construct
35 file_(QString(outfile))
37 file_.open(QIODevice::WriteOnly);
41 DownloadManager::~DownloadManager()
44 delete reply_; reply_ = NULL;
48 void DownloadManager::start()
50 reply_ = manager_.get(QNetworkRequest(url_));
52 connect(reply_, SIGNAL(readyRead()), this, SLOT(on_readyRead()));
53 connect(reply_, SIGNAL(downloadProgress (qint64 , qint64)), SLOT(on_downloadProgress(qint64, qint64)));
54 connect(reply_, SIGNAL(finished()), this, SLOT(on_replyFinished()));
58 void DownloadManager::on_readyRead()
60 //qDebug() << "on_readyRead()";
62 qWarning() << "DownloadManager::on_readyRead(): Writing to file: "
63 << filepath_ << " failed.";
67 void DownloadManager::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
69 static double last = 0;
71 double prog = static_cast<double>(bytesReceived) / static_cast<double>(bytesTotal);
72 if ((prog-last) >= 0.01) {
74 emit progress(static_cast<int>(100*prog));
75 } else if (bytesReceived == bytesTotal) {
81 void DownloadManager::on_replyFinished()
85 emit finished(filepath_);
89 bool DownloadManager::WriteToFile()
91 QByteArray readData = reply_->readAll();
93 if (readData.isEmpty()) {
94 qDebug() << "on_replyFinished(): No data available for reading";
96 // If writing failed, see error message.
97 if (file_.write(readData) == -1) {
98 qWarning() << file_.error();