Added download mechanism
authorBartosz Szatkowski <bulislaw@linux.com>
Mon, 4 Oct 2010 11:09:59 +0000 (13:09 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Mon, 4 Oct 2010 11:09:59 +0000 (13:09 +0200)
src/plugins/xdxf/HttpDownloader.cpp [new file with mode: 0644]
src/plugins/xdxf/HttpDownloader.h [new file with mode: 0644]

diff --git a/src/plugins/xdxf/HttpDownloader.cpp b/src/plugins/xdxf/HttpDownloader.cpp
new file mode 100644 (file)
index 0000000..652425f
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+/*!
+  \file HttpDownloader.h
+  \author Bartosz Szatkowski <bulislaw@linux.com>
+ */
+
+
+#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 (file)
index 0000000..3a8c678
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+/*!
+  \file HttpDownloader.h
+  \author Bartosz Szatkowski <bulislaw@linux.com>
+ */
+
+#ifndef HTTPDOWNLOADER_H
+#define HTTPDOWNLOADER_H
+
+#include <QObject>
+#include <QHttp>
+#include <QNetworkAccessManager>
+#include <QUrl>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QFile>
+#include <QDebug>
+
+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