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 "MarbleDebug.h"
12#include "HttpDownloadManager.h"
13
14#include <QNetworkAccessManager>
15
16using namespace Marble;
17
18class Marble::HttpJobPrivate
19{
20 public:
21 HttpJobPrivate( const QUrl & sourceUrl, const QString & destFileName,
22 const QString &id, QNetworkAccessManager *networkAccessManager );
23
24 QUrl m_sourceUrl;
25 QString m_destinationFileName;
26 QString m_initiatorId;
27 int m_trialsLeft;
28 DownloadUsage m_downloadUsage;
29 QString m_userAgent;
30 QNetworkAccessManager *const m_networkAccessManager;
31 QNetworkReply *m_networkReply;
32};
33
34HttpJobPrivate::HttpJobPrivate( const QUrl & sourceUrl, const QString & destFileName,
35 const QString &id, QNetworkAccessManager *networkAccessManager )
36 : m_sourceUrl( sourceUrl ),
37 m_destinationFileName( destFileName ),
38 m_initiatorId( id ),
39 m_trialsLeft( 3 ),
40 m_downloadUsage( DownloadBrowse ),
41 // FIXME: remove initialization depending on if empty pluginId
42 // results in valid user agent string
43 m_userAgent( "unknown" ),
44 m_networkAccessManager( networkAccessManager ),
45 m_networkReply( nullptr )
46{
47}
48
49
50HttpJob::HttpJob( const QUrl & sourceUrl, const QString & destFileName, const QString &id, QNetworkAccessManager *networkAccessManager )
51 : d( new HttpJobPrivate( sourceUrl, destFileName, id, networkAccessManager ) )
52{
53}
54
55HttpJob::~HttpJob()
56{
57 delete d;
58}
59
60QUrl HttpJob::sourceUrl() const
61{
62 return d->m_sourceUrl;
63}
64
65void HttpJob::setSourceUrl( const QUrl &url )
66{
67 d->m_sourceUrl = url;
68}
69
70QString HttpJob::initiatorId() const
71{
72 return d->m_initiatorId;
73}
74
75void HttpJob::setInitiatorId( const QString &id )
76{
77 d->m_initiatorId = id;
78}
79
80QString HttpJob::destinationFileName() const
81{
82 return d->m_destinationFileName;
83}
84
85void HttpJob::setDestinationFileName( const QString &fileName )
86{
87 d->m_destinationFileName = fileName;
88}
89
90bool HttpJob::tryAgain()
91{
92 if( d->m_trialsLeft > 0 ) {
93 d->m_trialsLeft--;
94 return true;
95 }
96 else {
97 return false;
98 }
99}
100
101DownloadUsage HttpJob::downloadUsage() const
102{
103 return d->m_downloadUsage;
104}
105
106void HttpJob::setDownloadUsage( const DownloadUsage usage )
107{
108 d->m_downloadUsage = usage;
109}
110
111void HttpJob::setUserAgentPluginId( const QString & pluginId ) const
112{
113 d->m_userAgent = pluginId;
114}
115
116QByteArray HttpJob::userAgent() const
117{
118 switch ( d->m_downloadUsage ) {
119 case DownloadBrowse:
120 return HttpDownloadManager::userAgent("Browser", d->m_userAgent);
121 case DownloadBulk:
122 return HttpDownloadManager::userAgent("BulkDownloader", d->m_userAgent);
123 default:
124 qCritical() << "Unknown download usage value:" << d->m_downloadUsage;
125 return HttpDownloadManager::userAgent("unknown", d->m_userAgent);
126 }
127}
128
129void HttpJob::execute()
130{
131 QNetworkRequest request( d->m_sourceUrl );
132 request.setAttribute( QNetworkRequest::HttpPipeliningAllowedAttribute, true );
133 request.setRawHeader( "User-Agent", userAgent() );
134 d->m_networkReply = d->m_networkAccessManager->get( request );
135
136 connect( d->m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
137 SLOT(downloadProgress(qint64,qint64)));
138 connect( d->m_networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
140 connect( d->m_networkReply, SIGNAL(finished()),
141 SLOT(finished()));
142}
143void HttpJob::downloadProgress( qint64 bytesReceived, qint64 bytesTotal )
144{
146 Q_UNUSED(bytesTotal);
147// mDebug() << "downloadProgress" << destinationFileName()
148// << bytesReceived << '/' << bytesTotal;
149}
150
151void HttpJob::error( QNetworkReply::NetworkError code )
152{
153 mDebug() << "error" << destinationFileName() << code;
154}
155
156void HttpJob::finished()
157{
158 QNetworkReply::NetworkError const error = d->m_networkReply->error();
159// mDebug() << "finished" << destinationFileName()
160// << "error" << error;
161
164 if ( !httpPipeliningWasUsed.isNull() )
165 mDebug() << "http pipelining used:" << httpPipeliningWasUsed.toBool();
166
167 switch ( error ) {
169 // check if we are redirected
172 if ( !redirectionAttribute.isNull() ) {
173 emit redirected( this, redirectionAttribute.toUrl() );
174 }
175 else {
176 // no redirection occurred
177 const QByteArray data = d->m_networkReply->readAll();
178 emit dataReceived( this, data );
179 }
180 }
181 break;
182
183 default:
184 emit jobDone( this, 1 );
185 }
186
187 d->m_networkReply->disconnect( this );
188 // No delete. This method is called by a signal QNetworkReply::finished.
189 d->m_networkReply->deleteLater();
190 d->m_networkReply = nullptr;
191}
192
193#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
NetworkError error() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool disconnect(const QMetaObject::Connection &connection)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.