summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasem Mutlaq <mutlaqja@ikarustech.com>2016-11-03 08:50:17 (GMT)
committerJasem Mutlaq <mutlaqja@ikarustech.com>2016-11-03 08:50:17 (GMT)
commit65d0badca750abcdb352a6703d89a4935c59b7fd (patch)
tree66a391cb7c71d13db2feebe6ed6227d16f3a4c52
parent05b8f16b749b9f1637e31a10ed7c0a1aab80112f (diff)
Improve file downloading to emit cancelled signal and show optionally a progress dialog. Also can directly save to file and now saves progressivly as not to consume a lot of RAM
-rw-r--r--kstars/auxiliary/filedownloader.cpp119
-rw-r--r--kstars/auxiliary/filedownloader.h30
2 files changed, 129 insertions, 20 deletions
diff --git a/kstars/auxiliary/filedownloader.cpp b/kstars/auxiliary/filedownloader.cpp
index 01deb73..d47c89a 100644
--- a/kstars/auxiliary/filedownloader.cpp
+++ b/kstars/auxiliary/filedownloader.cpp
@@ -11,12 +11,16 @@
*/
#include <QFile>
+#include <QProgressDialog>
+
+#include <KLocalizedString>
#include "filedownloader.h"
+#include "kstars.h"
FileDownloader::FileDownloader(QObject *parent) : QObject(parent)
{
- connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)), this, SLOT (fileDownloaded(QNetworkReply*)));
+ connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)), this, SLOT (dataFinished(QNetworkReply*)));
}
FileDownloader::~FileDownloader()
@@ -26,41 +30,122 @@ FileDownloader::~FileDownloader()
void FileDownloader::get(const QUrl & fileUrl)
{
QNetworkRequest request(fileUrl);
- QNetworkReply *reply = m_WebCtrl.get(request);
-
- connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ m_DownloadedData.clear();
+ isCancelled = false;
+ m_Reply = m_WebCtrl.get(request);
+
+ connect(m_Reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(setDownloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(readyRead()), this, SLOT(dataReady()));
}
void FileDownloader::post(const QUrl &fileUrl, QByteArray & data)
{
QNetworkRequest request(fileUrl);
- QNetworkReply *reply = m_WebCtrl.post(request, data);
-
- connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ m_DownloadedData.clear();
+ isCancelled = false;
+ m_Reply = m_WebCtrl.post(request, data);
+
+ connect(m_Reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(setDownloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(readyRead()), this, SLOT(dataReady()));
}
void FileDownloader::post(const QUrl & fileUrl, QHttpMultiPart *parts)
{
QNetworkRequest request(fileUrl);
- QNetworkReply *reply = m_WebCtrl.post(request, parts);
-
- connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ m_DownloadedData.clear();
+ isCancelled = false;
+ m_Reply = m_WebCtrl.post(request, parts);
+
+ connect(m_Reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError()));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(setDownloadProgress(qint64,qint64)));
+ connect(m_Reply, SIGNAL(readyRead()), this, SLOT(dataReady()));
}
+void FileDownloader::dataReady()
+{
+ if (m_DownloadedFile.isOpen())
+ m_DownloadedFile.write(m_Reply->readAll());
+ else
+ m_DownloadedData += m_Reply->readAll();
+}
-void FileDownloader::fileDownloaded(QNetworkReply* pReply)
+void FileDownloader::dataFinished(QNetworkReply* pReply)
{
- m_DownloadedData = pReply->readAll();
+ if (isCancelled == false)
+ emit downloaded();
+
pReply->deleteLater();
- //emit a signal
- emit downloaded();
}
void FileDownloader::slotError()
{
- QNetworkReply *pReply = static_cast<QNetworkReply *>(sender());
- pReply->deleteLater();
- emit error(pReply->errorString());
+ m_Reply->deleteLater();
+ emit error(m_Reply->errorString());
+}
+
+void FileDownloader::setProgressDialogEnabled(bool ShowProgressDialog, const QString& textTitle, const QString &textLabel)
+{
+ m_ShowProgressDialog = ShowProgressDialog;
+
+ if (title.isEmpty())
+ title = i18n("Downloading");
+ else
+ title = textTitle;
+
+ if (textLabel.isEmpty())
+ label = i18n("Downloading Data...");
+ else
+ label = textLabel;
+}
+
+QUrl FileDownloader::getDownloadedFileURL() const
+{
+ return m_DownloadedFileURL;
+}
+
+bool FileDownloader::setDownloadedFileURL(const QUrl &DownloadedFile)
+{
+ m_DownloadedFileURL = DownloadedFile;
+
+ if (m_DownloadedFileURL.isEmpty() == false)
+ {
+ m_DownloadedFile.setFileName(m_DownloadedFileURL.toLocalFile());
+ bool rc = m_DownloadedFile.open( QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text );
+
+ if (rc == false)
+ qWarning() << m_DownloadedFile.errorString();
+
+ return rc;
+ }
+
+ m_DownloadedFile.close();
+ return true;
+}
+
+void FileDownloader::setDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ if (m_ShowProgressDialog)
+ {
+ if (progressDialog == NULL)
+ {
+ isCancelled = false;
+ progressDialog = new QProgressDialog(KStars::Instance());
+ progressDialog->setWindowTitle(title);
+ progressDialog->setLabelText(label);
+ connect(progressDialog, SIGNAL(canceled()), this, SIGNAL(canceled()));
+ connect(progressDialog, &QProgressDialog::canceled, this, [&]() { isCancelled = true; m_Reply->abort(); });
+ progressDialog->setMinimum(0);
+ progressDialog->show();
+ }
+
+ progressDialog->setMaximum(bytesTotal);
+ progressDialog->setValue(bytesReceived);
+ }
}
QByteArray FileDownloader::downloadedData() const
diff --git a/kstars/auxiliary/filedownloader.h b/kstars/auxiliary/filedownloader.h
index 0f30cf9..0898624 100644
--- a/kstars/auxiliary/filedownloader.h
+++ b/kstars/auxiliary/filedownloader.h
@@ -15,10 +15,13 @@
#include <QObject>
#include <QByteArray>
+#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
+class QProgressDialog;
+
class FileDownloader : public QObject
{
Q_OBJECT
@@ -32,18 +35,39 @@ public:
QByteArray downloadedData() const;
+ QUrl getDownloadedFileURL() const;
+ bool setDownloadedFileURL(const QUrl &DownloadedFile);
+
+ void setProgressDialogEnabled(bool ShowProgressDialog, const QString &textTitle = QString(), const QString &textLabel = QString());
+
signals:
void downloaded();
+ void canceled();
void error(const QString &errorString);
+ void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
-
-private slots:
- void fileDownloaded(QNetworkReply* pReply);
+private slots:
+ void dataFinished(QNetworkReply* pReply);
+ void dataReady();
void slotError();
+ void setDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
private:
QNetworkAccessManager m_WebCtrl;
QByteArray m_DownloadedData;
+
+ // Downloaded file
+ QUrl m_DownloadedFileURL;
+ QFile m_DownloadedFile;
+
+ // Network reply
+ QNetworkReply *m_Reply = NULL;
+
+ // Optional Progress dialog
+ bool m_ShowProgressDialog = false;
+ QProgressDialog* progressDialog = NULL;
+ bool isCancelled = false;
+ QString label, title;
};
#endif // FILEDOWNLOADER_H