Marble

HttpJob.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
5// SPDX-FileCopyrightText: 2008, 2009 Jens-Michael Hoffmann <jensmh@gmx.de>
6// SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
7//
8
9#include "HttpJob.h"
10
11#include "HttpDownloadManager.h"
12#include "MarbleDebug.h"
13
14#include <QNetworkAccessManager>
15
16using namespace Marble;
17
18class Marble::HttpJobPrivate
19{
20public:
21 HttpJobPrivate(const QUrl &sourceUrl, const QString &destFileName, const QString &id, QNetworkAccessManager *networkAccessManager);
22
23 QUrl m_sourceUrl;
24 QString m_destinationFileName;
25 QString m_initiatorId;
26 int m_trialsLeft;
27 DownloadUsage m_downloadUsage;
28 QString m_userAgent;
29 QNetworkAccessManager *const m_networkAccessManager;
30 QNetworkReply *m_networkReply;
31};
32
33HttpJobPrivate::HttpJobPrivate(const QUrl &sourceUrl, const QString &destFileName, const QString &id, QNetworkAccessManager *networkAccessManager)
34 : m_sourceUrl(sourceUrl)
35 , m_destinationFileName(destFileName)
36 , m_initiatorId(id)
37 , m_trialsLeft(3)
38 , m_downloadUsage(DownloadBrowse)
39 ,
40 // FIXME: remove initialization depending on if empty pluginId
41 // results in valid user agent string
42 m_userAgent(QStringLiteral("unknown"))
43 , m_networkAccessManager(networkAccessManager)
44 , m_networkReply(nullptr)
45{
46}
47
48HttpJob::HttpJob(const QUrl &sourceUrl, const QString &destFileName, const QString &id, QNetworkAccessManager *networkAccessManager)
49 : d(new HttpJobPrivate(sourceUrl, destFileName, id, networkAccessManager))
50{
51}
52
53HttpJob::~HttpJob()
54{
55 delete d;
56}
57
58QUrl HttpJob::sourceUrl() const
59{
60 return d->m_sourceUrl;
61}
62
63void HttpJob::setSourceUrl(const QUrl &url)
64{
65 d->m_sourceUrl = url;
66}
67
68QString HttpJob::initiatorId() const
69{
70 return d->m_initiatorId;
71}
72
73void HttpJob::setInitiatorId(const QString &id)
74{
75 d->m_initiatorId = id;
76}
77
78QString HttpJob::destinationFileName() const
79{
80 return d->m_destinationFileName;
81}
82
83void HttpJob::setDestinationFileName(const QString &fileName)
84{
85 d->m_destinationFileName = fileName;
86}
87
88bool HttpJob::tryAgain()
89{
90 if (d->m_trialsLeft > 0) {
91 d->m_trialsLeft--;
92 return true;
93 } else {
94 return false;
95 }
96}
97
98DownloadUsage HttpJob::downloadUsage() const
99{
100 return d->m_downloadUsage;
101}
102
103void HttpJob::setDownloadUsage(const DownloadUsage usage)
104{
105 d->m_downloadUsage = usage;
106}
107
108void HttpJob::setUserAgentPluginId(const QString &pluginId) const
109{
110 d->m_userAgent = pluginId;
111}
112
113QByteArray HttpJob::userAgent() const
114{
115 switch (d->m_downloadUsage) {
116 case DownloadBrowse:
117 return HttpDownloadManager::userAgent(QStringLiteral("Browser"), d->m_userAgent);
118 case DownloadBulk:
119 return HttpDownloadManager::userAgent(QStringLiteral("BulkDownloader"), d->m_userAgent);
120 default:
121 qCritical() << "Unknown download usage value:" << d->m_downloadUsage;
122 return HttpDownloadManager::userAgent(QStringLiteral("unknown"), d->m_userAgent);
123 }
124}
125
126void HttpJob::execute()
127{
128 QNetworkRequest request(d->m_sourceUrl);
129 request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
130 request.setRawHeader("User-Agent", userAgent());
131 d->m_networkReply = d->m_networkAccessManager->get(request);
132
133 connect(d->m_networkReply, &QNetworkReply::downloadProgress, this, &HttpJob::downloadProgress);
134 connect(d->m_networkReply, &QNetworkReply::errorOccurred, this, &HttpJob::error);
135 connect(d->m_networkReply, &QNetworkReply::finished, this, &HttpJob::finished);
136}
137void HttpJob::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
138{
139 Q_UNUSED(bytesReceived);
140 Q_UNUSED(bytesTotal);
141 // mDebug() << "downloadProgress" << destinationFileName()
142 // << bytesReceived << '/' << bytesTotal;
143}
144
145void HttpJob::error(QNetworkReply::NetworkError code)
146{
147 mDebug() << "error" << destinationFileName() << code;
148}
149
150void HttpJob::finished()
151{
152 QNetworkReply::NetworkError const error = d->m_networkReply->error();
153 // mDebug() << "finished" << destinationFileName()
154 // << "error" << error;
155
156 const QVariant httpPipeliningWasUsed = d->m_networkReply->attribute(QNetworkRequest::HttpPipeliningWasUsedAttribute);
157 if (!httpPipeliningWasUsed.isNull())
158 mDebug() << "http pipelining used:" << httpPipeliningWasUsed.toBool();
159
160 switch (error) {
162 // check if we are redirected
163 const QVariant redirectionAttribute = d->m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
164 if (!redirectionAttribute.isNull()) {
165 Q_EMIT redirected(this, redirectionAttribute.toUrl());
166 } else {
167 // no redirection occurred
168 const QByteArray data = d->m_networkReply->readAll();
169 Q_EMIT dataReceived(this, data);
170 }
171 } break;
172
173 default:
174 Q_EMIT jobDone(this, 1);
175 }
176
177 d->m_networkReply->disconnect(this);
178 // No delete. This method is called by a signal QNetworkReply::finished.
179 d->m_networkReply->deleteLater();
180 d->m_networkReply = nullptr;
181}
182
183#include "moc_HttpJob.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
DownloadUsage
This enum is used to describe the type of download.
@ DownloadBrowse
Browsing mode, normal operation of Marble, like a web browser.
@ DownloadBulk
Bulk download, for example "File/Download region".
QByteArray readAll()
QNetworkReply * get(const QNetworkRequest &request)
QVariant attribute(QNetworkRequest::Attribute code) const const
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
NetworkError error() const const
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool disconnect(const QMetaObject::Connection &connection)
bool isNull() const const
bool toBool() const const
QUrl toUrl() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.