- License texts modified to GPLv2
[qtrapids] / src / plugins / searchplugin / DownloadManager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
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.               *
7  *                                                                         *
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.                          *
12  *                                                                         *
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  ***************************************************************************/
18
19 #include <QDebug>
20
21 #include "DownloadManager.h"
22
23
24 DownloadManager::DownloadManager(QObject *parent) :
25         QObject(parent) // Superclass construct
26 {
27 }
28
29 DownloadManager::DownloadManager(QUrl url, QString outfile, QObject *parent) :
30         QObject(parent), // Superclass construct
31         url_(url),
32         filepath_(outfile),
33         file_(QString(outfile))
34 {
35         file_.open(QIODevice::WriteOnly);
36 }
37
38
39 DownloadManager::~DownloadManager()
40 {
41         file_.close();
42         delete reply_; reply_ = NULL;
43 }
44
45
46 void DownloadManager::start()
47 {
48         reply_ = manager_.get(QNetworkRequest(url_));
49         
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()));
53 }
54
55
56 void DownloadManager::on_readyRead()
57 {
58         //qDebug() << "on_readyRead()";
59         if(!WriteToFile()) {
60                 qWarning() << "DownloadManager::on_readyRead(): Writing to file: " 
61                         << filepath_ << " failed.";
62         }
63 }
64
65 void DownloadManager::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
66 {
67         static double last = 0;
68         
69         double prog = static_cast<double>(bytesReceived) / static_cast<double>(bytesTotal);
70         if ((prog-last) >= 0.01) {
71                 last = prog;
72                 emit progress(static_cast<int>(100*prog));
73         } else if (bytesReceived == bytesTotal) {
74                 emit progress(100);
75         }
76         
77 }
78
79 void DownloadManager::on_replyFinished()
80 {
81         WriteToFile();
82         file_.close();
83         emit finished(filepath_);
84 }
85
86
87 bool DownloadManager::WriteToFile() 
88 {
89         QByteArray readData = reply_->readAll();
90         
91         if (readData.isEmpty()) {
92                 qDebug() << "on_replyFinished(): No data available for reading";
93         } else {
94                 // If writing failed, see error message.
95                 if (file_.write(readData) == -1) {
96                         qWarning() << file_.error();
97                         return false;
98                 }
99         }
100         return true;
101 }