Kstars

urlfiledownload.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2017 Pavel Mraz
3 
4  SPDX-FileCopyrightText: 2017 Jasem Mutlaq
5 
6  SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #include "urlfiledownload.h"
10 #include <QDebug>
11 
12 UrlFileDownload::UrlFileDownload(QObject *parent, QNetworkDiskCache *cache) : QObject(parent)
13 {
14  connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
15 
16  m_manager.setCache(cache);
17 }
18 
19 void UrlFileDownload::begin(const QUrl &url, const pixCacheKey_t &key)
20 {
21  QNetworkRequest request(url);
23 
24  QNetworkReply *reply = m_manager.get(request);
25 
26  connect(this, SIGNAL(sigAbort()), reply, SLOT(abort()));
27 
28  QVariant val;
29  val.setValue(key);
30  reply->setProperty("user_data0", val);
31 }
32 
33 void UrlFileDownload::abortAll()
34 {
35  emit sigAbort();
36 }
37 
38 void UrlFileDownload::downloadFinished(QNetworkReply *reply)
39 {
40  pixCacheKey_t key = reply->property("user_data0").value<pixCacheKey_t>();
41 
42  //QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
43 
44  if (reply->error() == QNetworkReply::NoError)
45  {
46  QByteArray data = reply->readAll();
47 
48  emit sigDownloadDone(reply->error(), data, key);
49  }
50  else
51  {
52  QByteArray empty;
53  emit sigDownloadDone(reply->error(), empty, key);
54  }
55 
56  reply->deleteLater();
57 }
T value() const const
QNetworkReply::NetworkError error() const const
void setValue(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void deleteLater()
bool setProperty(const char *name, const QVariant &value)
QByteArray readAll()
QVariant property(const char *name) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:14:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.