01e2cb48e9547db3d47e688ccc29cccce9b7906b
[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          WriteToFile();
62 }
63
64 void DownloadManager::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
65 {
66         static double last = 0;
67         
68         double prog = static_cast<double>(bytesReceived) / static_cast<double>(bytesTotal);
69         if ((prog-last) >= 0.01) {
70                 last = prog;
71                 emit progress(static_cast<int>(100*prog));
72         } else if (bytesReceived == bytesTotal) {
73                 emit progress(100);
74         }
75         
76 }
77
78 void DownloadManager::on_replyFinished()
79 {
80         WriteToFile();
81         file_.close();
82         emit finished(filepath_);
83 }
84
85
86 void DownloadManager::WriteToFile() 
87 {
88         QByteArray readData = reply_->readAll();
89         
90         if (readData.isEmpty()) {
91                 qDebug() << "on_replyFinished(): No data available for reading";
92         } else {
93                 file_.write(readData);
94                 /// @todo check file_.error()
95         }
96 }