From: Bartosz Szatkowski Date: Mon, 4 Oct 2010 11:09:59 +0000 (+0200) Subject: Added download mechanism X-Git-Url: http://vcs.maemo.org/git/?a=commitdiff_plain;h=f7bd5def8f0ea655e29729bf4688438939a57902;p=mdictionary Added download mechanism --- diff --git a/src/plugins/xdxf/HttpDownloader.cpp b/src/plugins/xdxf/HttpDownloader.cpp new file mode 100644 index 0000000..652425f --- /dev/null +++ b/src/plugins/xdxf/HttpDownloader.cpp @@ -0,0 +1,69 @@ +/******************************************************************************* + + This file is part of mDictionary. + + mDictionary is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + mDictionary is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with mDictionary. If not, see . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +/*! + \file HttpDownloader.h + \author Bartosz Szatkowski + */ + + +#include "HttpDownloader.h" + +HttpDownloader::HttpDownloader(QObject *parent) { + http = new QHttp(); + //connect(http, SIGNAL(requestFinished(int, bool)), this, + // SLOT(getPageFinished())); + + manager = new QNetworkAccessManager; + connect(manager, SIGNAL(finished(QNetworkReply*)), + SLOT(downloadFinished(QNetworkReply*))); +} + + +void HttpDownloader::download(QUrl url, QString file) { + destFile = file; + QNetworkRequest request(url); + // Following line is crucial becouse sourceforge wont redirect correctly + // if no user-agent is supplied + request.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)"); + manager->get(request); + +} + +void HttpDownloader::downloadFinished(QNetworkReply *reply) { + QUrl r = reply->attribute(QNetworkRequest::RedirectionTargetAttribute) + .toUrl(); + + if(r.isValid()) { + // Following redirect + QNetworkRequest req; + req.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)"); + req.setUrl(r); + manager->get(req); + } + else { + QFile resultFile(destFile); + resultFile.open(QFile::WriteOnly); + resultFile.write(reply->readAll()); + resultFile.close(); + Q_EMIT finished(); + } +} diff --git a/src/plugins/xdxf/HttpDownloader.h b/src/plugins/xdxf/HttpDownloader.h new file mode 100644 index 0000000..3a8c678 --- /dev/null +++ b/src/plugins/xdxf/HttpDownloader.h @@ -0,0 +1,59 @@ +/******************************************************************************* + + This file is part of mDictionary. + + mDictionary is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + mDictionary is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with mDictionary. If not, see . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +/*! + \file HttpDownloader.h + \author Bartosz Szatkowski + */ + +#ifndef HTTPDOWNLOADER_H +#define HTTPDOWNLOADER_H + +#include +#include +#include +#include +#include +#include +#include +#include + +class HttpDownloader : public QObject { + Q_OBJECT +public: + HttpDownloader(QObject *parent = 0); + void download(const QUrl, const QString); + +public Q_SLOTS: + void downloadFinished(QNetworkReply *); + +Q_SIGNALS: + void finished(); + +private: + QHttp *http; + QNetworkAccessManager *manager; + QString destFile; + + +}; + +#endif // HTTPDOWNLOADER_H