1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 \file HttpDownloader.h
23 \author Bartosz Szatkowski <bulislaw@linux.com>
26 #include "HttpDownloader.h"
28 HttpDownloader::HttpDownloader(QObject *parent) {
30 //connect(http, SIGNAL(requestFinished(int, bool)), this,
31 // SLOT(getPageFinished()));
33 manager = new QNetworkAccessManager;
34 connect(manager, SIGNAL(finished(QNetworkReply*)),
35 SLOT(downloadFinished(QNetworkReply*)));
37 //connect(http, SIGNAL())
41 void HttpDownloader::download(QUrl url, QString 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)));
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());
61 QUrl r = reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
67 req.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)");
69 currentReply = manager->get(req);
70 connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)),
71 this, SIGNAL(progress(qint64,qint64)));
74 QFile resultFile(destFile);
75 resultFile.open(QFile::WriteOnly);
76 resultFile.write(reply->readAll());
83 void HttpDownloader::kill() {