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
12UrlFileDownload::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
19void 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
33void UrlFileDownload::abortAll()
34{
35 emit sigAbort();
36}
37
38void 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}
QByteArray readAll()
QNetworkReply * get(const QNetworkRequest &request)
NetworkError error() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QVariant property(const char *name) const const
bool setProperty(const char *name, QVariant &&value)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setValue(QVariant &&value)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.