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