9#include "filedownloader.h"
15#include "kstars_debug.h"
17#include <KLocalizedString>
20#include <QProgressDialog>
26 registerDataVerification([](
const QByteArray &) {
return true; });
27 registerFileVerification([](
const QString &) {
return true;});
30void FileDownloader::get(
const QUrl &fileUrl)
32 QNetworkRequest request(fileUrl);
33 m_DownloadedData.clear();
35 m_Reply = m_WebCtrl.get(request);
38 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SIGNAL(downloadProgress(qint64,qint64)));
39 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(setDownloadProgress(qint64,qint64)));
40 connect(m_Reply, SIGNAL(readyRead()),
this, SLOT(dataReady()));
42 setDownloadProgress(0, 0);
47 QNetworkRequest request(fileUrl);
49 m_DownloadedData.clear();
51 m_Reply = m_WebCtrl.post(request, data);
54 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SIGNAL(downloadProgress(qint64,qint64)));
55 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(setDownloadProgress(qint64,qint64)));
56 connect(m_Reply, SIGNAL(readyRead()),
this, SLOT(dataReady()));
58 setDownloadProgress(0, 0);
63 QNetworkRequest request(fileUrl);
65 m_DownloadedData.clear();
67 m_Reply = m_WebCtrl.post(request, parts);
70 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SIGNAL(downloadProgress(qint64,qint64)));
71 connect(m_Reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(setDownloadProgress(qint64,qint64)));
72 connect(m_Reply, SIGNAL(readyRead()),
this, SLOT(dataReady()));
74 setDownloadProgress(0, 0);
77void FileDownloader::dataReady()
79 if (m_downloadTemporaryFile.isOpen())
80 m_downloadTemporaryFile.write(m_Reply->readAll());
82 m_DownloadedData += m_Reply->readAll();
92 if (m_verifyData(m_DownloadedData) ==
false)
94 emit error(
i18n(
"Data verification failed"));
98 else if (m_downloadTemporaryFile.isOpen())
100 m_downloadTemporaryFile.flush();
101 m_downloadTemporaryFile.close();
103 if (m_verifyFile(m_downloadTemporaryFile.fileName()) ==
false)
105 emit error(
i18n(
"File verification failed"));
112 m_downloadTemporaryFile.copy(m_DownloadedFileURL.toLocalFile());
116 if (isCancelled ==
false)
122void FileDownloader::slotError()
124 m_Reply->deleteLater();
127 if (progressDialog !=
nullptr)
128 progressDialog->hide();
134 if (m_downloadTemporaryFile.isOpen())
136 m_downloadTemporaryFile.close();
137 m_downloadTemporaryFile.remove();
143 emit error(m_Reply->errorString());
147void FileDownloader::setProgressDialogEnabled(
bool ShowProgressDialog,
const QString &textTitle,
150 m_ShowProgressDialog = ShowProgressDialog;
153 title =
i18n(
"Downloading");
158 label =
i18n(
"Downloading Data...");
163QUrl FileDownloader::getDownloadedFileURL()
const
165 return m_DownloadedFileURL;
168bool FileDownloader::setDownloadedFileURL(
const QUrl &DownloadedFile)
170 m_DownloadedFileURL = DownloadedFile;
172 if (m_DownloadedFileURL.isEmpty() ==
false)
174 bool rc= m_downloadTemporaryFile.open();
177 qCWarning(KSTARS) << m_downloadTemporaryFile.errorString();
179 qCDebug(KSTARS) <<
"Opened" << m_downloadTemporaryFile.fileName() <<
"to download data into" << DownloadedFile.
toLocalFile();
187void FileDownloader::setDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
190 if (m_ShowProgressDialog)
192 if (progressDialog ==
nullptr)
196 progressDialog->setWindowTitle(title);
197 progressDialog->setLabelText(
i18n(
"Awaiting response from server..."));
198 connect(progressDialog, SIGNAL(canceled()),
this, SIGNAL(canceled()));
202 progressDialog->close();
204 progressDialog->setMinimum(0);
205 progressDialog->setMaximum(0);
206 progressDialog->show();
207 progressDialog->raise();
210 if (bytesReceived > 0)
212 progressDialog->setLabelText(label);
217 progressDialog->setMaximum(bytesTotal);
218 progressDialog->setValue(bytesReceived);
222 progressDialog->setMaximum(0);
226 Q_UNUSED(bytesReceived);
227 Q_UNUSED(bytesTotal);
231QByteArray FileDownloader::downloadedData()
const
233 return m_DownloadedData;
static KStars * Instance()
QString i18n(const char *text, const TYPE &arg...)
NetworkError error() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toLocalFile() const const