Adds a small class to download files from the Qt frontend
This commit is contained in:
54
JGE/include/qt/filedownloader.h
Normal file
54
JGE/include/qt/filedownloader.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#ifndef FILEDOWNLOADER_H
|
||||||
|
#define FILEDOWNLOADER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include <qdeclarative.h>
|
||||||
|
#include <QTemporaryFile>
|
||||||
|
|
||||||
|
|
||||||
|
class FileDownloader : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(qint64 received READ received NOTIFY receivedChanged)
|
||||||
|
public:
|
||||||
|
explicit FileDownloader(QUrl url, QString localPath, QObject *parent = 0);
|
||||||
|
virtual ~FileDownloader();
|
||||||
|
qint64 received() const {return m_received;};
|
||||||
|
bool isDone() {return m_done;};
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void downloaded();
|
||||||
|
void receivedChanged();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void fileDownloaded(QNetworkReply* pReply){
|
||||||
|
if(m_tmp.write(pReply->readAll()) == -1) return;
|
||||||
|
|
||||||
|
if(!m_tmp.rename(m_localPath)) return;
|
||||||
|
|
||||||
|
m_tmp.setAutoRemove(false);
|
||||||
|
|
||||||
|
//emit a signal
|
||||||
|
emit downloaded();
|
||||||
|
};
|
||||||
|
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal){
|
||||||
|
m_received = bytesReceived*100/bytesTotal;
|
||||||
|
emit receivedChanged();
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
QNetworkAccessManager m_WebCtrl;
|
||||||
|
qint64 m_received;
|
||||||
|
QTemporaryFile m_tmp;
|
||||||
|
QString m_localPath;
|
||||||
|
bool m_OK;
|
||||||
|
bool m_done;
|
||||||
|
};
|
||||||
|
QML_DECLARE_TYPEINFO(FileDownloader, QML_HAS_ATTACHED_PROPERTIES)
|
||||||
|
|
||||||
|
#endif // FILEDOWNLOADER_H
|
||||||
27
JGE/src/qt/filedownloader.cpp
Normal file
27
JGE/src/qt/filedownloader.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "filedownloader.h"
|
||||||
|
|
||||||
|
FileDownloader::FileDownloader(QUrl url, QString localPath, QObject *parent) :
|
||||||
|
QObject(parent), m_received(0), m_localPath(localPath), m_OK(false), m_done(false)
|
||||||
|
|
||||||
|
{
|
||||||
|
QFile local(m_localPath);
|
||||||
|
if(local.exists()) {
|
||||||
|
m_done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
|
||||||
|
SLOT(fileDownloaded(QNetworkReply*)));
|
||||||
|
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
QNetworkReply* reply = m_WebCtrl.get(request);
|
||||||
|
|
||||||
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
||||||
|
SLOT(downloadProgress(qint64, qint64)));
|
||||||
|
|
||||||
|
m_OK = m_tmp.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileDownloader::~FileDownloader()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user