summaryrefslogtreecommitdiffstats
path: root/src/fileDownloader.cpp
diff options
context:
space:
mode:
authorJonathan Bauer2017-11-29 15:27:55 +0100
committerJonathan Bauer2017-11-29 15:27:55 +0100
commitfa01f545f158319bd81c7a8ab8aeac4efcc471c6 (patch)
treebbdb9d21516ca318c2e151aa73a6b034a3f40388 /src/fileDownloader.cpp
parentFIX EVERYWHERE (diff)
downloadvmchooser2-fa01f545f158319bd81c7a8ab8aeac4efcc471c6.tar.gz
vmchooser2-fa01f545f158319bd81c7a8ab8aeac4efcc471c6.tar.xz
vmchooser2-fa01f545f158319bd81c7a8ab8aeac4efcc471c6.zip
FileDownloader.* -> fileDownloader.*
Diffstat (limited to 'src/fileDownloader.cpp')
-rw-r--r--src/fileDownloader.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/fileDownloader.cpp b/src/fileDownloader.cpp
new file mode 100644
index 0000000..dc53356
--- /dev/null
+++ b/src/fileDownloader.cpp
@@ -0,0 +1,73 @@
+/*
+ * fileDownloader.cpp
+ *
+ * Created on: Mar 7, 2014
+ * Author: nils
+ */
+
+#include <QFileInfo>
+
+#include "fileDownloader.h"
+
+// Maximum size of download
+#define MAXSIZE (200000)
+
+static QNetworkAccessManager m_WebCtrl;
+
+FileDownloader::FileDownloader(const QUrl& fileUrl, QObject *parent) :
+ QObject(parent), started(false), url(fileUrl) {
+}
+
+FileDownloader::~FileDownloader() {
+
+}
+
+bool FileDownloader::downloadFile() {
+ if (this->started)
+ return true;
+ QNetworkReply *reply = m_WebCtrl.get(QNetworkRequest(this->url));
+ if (reply == NULL)
+ return false;
+ this->started = true;
+ connect(reply, SIGNAL(finished()), SLOT(fileDownloaded()));
+ connect(reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
+ return true;
+}
+
+/*
+ * Slots from networkreply
+ */
+
+void FileDownloader::downloadFailed(QNetworkReply::NetworkError) {
+ QNetworkReply *reply = (QNetworkReply*)this->sender();
+ killReply(reply);
+ emit downloaded(this->url, QByteArray());
+}
+
+void FileDownloader::fileDownloaded() {
+ QNetworkReply *reply = (QNetworkReply*)this->sender();
+ if (reply == NULL)
+ return;
+ QByteArray downloadedData(reply->readAll());
+ killReply(reply);
+ //emit a signal
+ emit downloaded(this->url, downloadedData);
+}
+
+void FileDownloader::downloadProgress(qint64 received, qint64 totalSize) {
+ QNetworkReply *reply = (QNetworkReply*)this->sender();
+ if (reply == NULL)
+ return;
+ if (received > MAXSIZE || totalSize > MAXSIZE) {
+ killReply(reply);
+ emit downloaded(this->url, QByteArray());
+ }
+}
+
+void FileDownloader::killReply(QNetworkReply *reply) {
+ if (reply == NULL)
+ return;
+ reply->blockSignals(true);
+ reply->abort();
+ reply->deleteLater();
+}