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()";
64 void DownloadManager::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
66 static double last = 0;
68 double prog = static_cast<double>(bytesReceived) / static_cast<double>(bytesTotal);
69 if ((prog-last) >= 0.01) {
71 emit progress(static_cast<int>(100*prog));
72 } else if (bytesReceived == bytesTotal) {
78 void DownloadManager::on_replyFinished()
82 emit finished(filepath_);
86 void DownloadManager::WriteToFile()
88 QByteArray readData = reply_->readAll();
90 if (readData.isEmpty()) {
91 qDebug() << "on_replyFinished(): No data available for reading";
93 file_.write(readData);
94 /// @todo check file_.error()