change and add some comments, remove some warning
[mdictionary] / src / plugins / xdxf / HttpDownloader.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary 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 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary 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 mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21 /*!
22     \file HttpDownloader.h
23     \author Bartosz Szatkowski <bulislaw@linux.com>
24  */
25
26 #include "HttpDownloader.h"
27
28 HttpDownloader::HttpDownloader(QObject *parent): QObject(parent) {
29     http = new QHttp();
30     //connect(http, SIGNAL(requestFinished(int, bool)), this,
31      //       SLOT(getPageFinished()));
32
33     manager = new QNetworkAccessManager;
34     connect(manager, SIGNAL(finished(QNetworkReply*)),
35             SLOT(downloadFinished(QNetworkReply*)));
36
37     //connect(http, SIGNAL())
38 }
39
40
41 void HttpDownloader::download(QUrl url, QString file) {
42     destFile = file;
43     QNetworkRequest request(url);
44     // Following line is crucial becouse sourceforge wont redirect correctly
45     //    if no user-agent is supplied
46     request.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)");
47     currentReply = manager->get(request);
48     connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)),
49             this, SIGNAL(progress(qint64,qint64)));
50 }
51
52
53 void HttpDownloader::downloadFinished(QNetworkReply *reply) {
54     disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)),
55                this, SIGNAL(progress(qint64,qint64)));
56     if(reply->error() != QNetworkReply::NoError) {
57         Q_EMIT error(reply->errorString());
58         return;
59     }
60
61     QUrl r = reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
62             .toUrl();
63
64     if(r.isValid()) {
65        // Following redirect
66        QNetworkRequest req;
67        req.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)");
68        req.setUrl(r);
69        currentReply = manager->get(req);
70        connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)),
71                this, SIGNAL(progress(qint64,qint64)));
72     }
73     else {
74         QFile resultFile(destFile);
75         resultFile.open(QFile::WriteOnly);
76         resultFile.write(reply->readAll());
77         resultFile.close();
78         Q_EMIT finished();
79     }
80 }
81
82
83 void HttpDownloader::kill() {
84     http->abort();
85 }