• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
HttpDownloadManager.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2006-2007 Torsten Rahn <tackat@kde.org>
9 // Copyright 2007 Inge Wallin <ingwa@kde.org>
10 // Copyright 2008,2009 Jens-Michael Hoffmann <jensmh@gmx.de>
11 //
12 
13 #include "HttpDownloadManager.h"
14 
15 #include <QList>
16 #include <QMap>
17 #include <QTimer>
18 #include <QNetworkAccessManager>
19 
20 #include "DownloadPolicy.h"
21 #include "DownloadQueueSet.h"
22 #include "HttpJob.h"
23 #include "MarbleDebug.h"
24 #include "StoragePolicy.h"
25 
26 using namespace Marble;
27 
28 // Time before a failed download job is requeued in ms
29 const quint32 requeueTime = 60000;
30 
31 class HttpDownloadManager::Private
32 {
33  public:
34  explicit Private( StoragePolicy *policy );
35  ~Private();
36 
37  DownloadQueueSet *findQueues( const QString& hostName, const DownloadUsage usage );
38 
39  QTimer m_requeueTimer;
45  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> > m_queueSets;
46  QMap<DownloadUsage, DownloadQueueSet *> m_defaultQueueSets;
47  StoragePolicy *const m_storagePolicy;
48  QNetworkAccessManager m_networkAccessManager;
49 
50 };
51 
52 HttpDownloadManager::Private::Private( StoragePolicy *policy )
53  : m_requeueTimer(),
54  m_storagePolicy( policy ),
55  m_networkAccessManager()
56 {
57  // setup default download policy and associated queue set
58  DownloadPolicy defaultBrowsePolicy;
59  defaultBrowsePolicy.setMaximumConnections( 20 );
60  m_defaultQueueSets[ DownloadBrowse ] = new DownloadQueueSet( defaultBrowsePolicy );
61  DownloadPolicy defaultBulkDownloadPolicy;
62  defaultBulkDownloadPolicy.setMaximumConnections( 2 );
63  m_defaultQueueSets[ DownloadBulk ] = new DownloadQueueSet( defaultBulkDownloadPolicy );
64 }
65 
66 HttpDownloadManager::Private::~Private()
67 {
68  QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = m_defaultQueueSets.begin();
69  QMap<DownloadUsage, DownloadQueueSet *>::iterator const end = m_defaultQueueSets.end();
70  for (; pos != end; ++pos )
71  delete pos.value();
72 }
73 
74 DownloadQueueSet *HttpDownloadManager::Private::findQueues( const QString& hostName,
75  const DownloadUsage usage )
76 {
77  DownloadQueueSet * result = 0;
78  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = m_queueSets.begin();
79  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator const end = m_queueSets.end();
80  for (; pos != end; ++pos ) {
81  if ( (*pos).first.matches( hostName, usage )) {
82  result = (*pos).second;
83  break;
84  }
85  }
86  if ( !result ) {
87  mDebug() << "No download policy found for" << hostName << usage
88  << ", using default policy.";
89  result = m_defaultQueueSets[ usage ];
90  }
91  return result;
92 }
93 
94 
95 HttpDownloadManager::HttpDownloadManager( StoragePolicy *policy )
96  : d( new Private( policy ) )
97 {
98  d->m_requeueTimer.setInterval( requeueTime );
99  connect( &d->m_requeueTimer, SIGNAL(timeout()), this, SLOT(requeue()) );
100  connectDefaultQueueSets();
101 }
102 
103 HttpDownloadManager::~HttpDownloadManager()
104 {
105  delete d;
106 }
107 
108 void HttpDownloadManager::setDownloadEnabled( const bool enable )
109 {
110  d->m_networkAccessManager.setNetworkAccessible( enable ? QNetworkAccessManager::Accessible : QNetworkAccessManager::NotAccessible );
111  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
112  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator const end = d->m_queueSets.end();
113  for (; pos != end; ++pos ) {
114  pos->second->purgeJobs();
115  }
116 
117 }
118 
119 void HttpDownloadManager::addDownloadPolicy( const DownloadPolicy& policy )
120 {
121  if ( hasDownloadPolicy( policy ))
122  return;
123  DownloadQueueSet * const queueSet = new DownloadQueueSet( policy, this );
124  connectQueueSet( queueSet );
125  d->m_queueSets.append( QPair<DownloadPolicyKey, DownloadQueueSet *>
126  ( queueSet->downloadPolicy().key(), queueSet ));
127 }
128 
129 void HttpDownloadManager::addJob( const QUrl& sourceUrl, const QString& destFileName,
130  const QString &id, const DownloadUsage usage )
131 {
132  if ( d->m_networkAccessManager.networkAccessible() == QNetworkAccessManager::NotAccessible )
133  return;
134 
135  DownloadQueueSet * const queueSet = d->findQueues( sourceUrl.host(), usage );
136  if ( queueSet->canAcceptJob( sourceUrl, destFileName )) {
137  HttpJob * const job = new HttpJob( sourceUrl, destFileName, id, &d->m_networkAccessManager );
138  job->setUserAgentPluginId( "QNamNetworkPlugin" );
139  job->setDownloadUsage( usage );
140  queueSet->addJob( job );
141  }
142 }
143 
144 void HttpDownloadManager::finishJob( const QByteArray& data, const QString& destinationFileName,
145  const QString& id )
146 {
147  mDebug() << "emitting downloadComplete( QByteArray, " << id << ")";
148  emit downloadComplete( data, id );
149  if ( d->m_storagePolicy ) {
150  const bool saved = d->m_storagePolicy->updateFile( destinationFileName, data );
151  if ( saved ) {
152  mDebug() << "emitting downloadComplete( " << destinationFileName << ", " << id << ")";
153  emit downloadComplete( destinationFileName, id );
154  } else {
155  qWarning() << "Could not save:" << destinationFileName;
156  }
157  }
158 }
159 
160 void HttpDownloadManager::requeue()
161 {
162  d->m_requeueTimer.stop();
163 
164  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
165  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator const end = d->m_queueSets.end();
166  for (; pos != end; ++pos ) {
167  (*pos).second->retryJobs();
168  }
169 }
170 
171 void HttpDownloadManager::startRetryTimer()
172 {
173  if ( !d->m_requeueTimer.isActive() )
174  d->m_requeueTimer.start();
175 }
176 
177 void HttpDownloadManager::connectDefaultQueueSets()
178 {
179  QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = d->m_defaultQueueSets.begin();
180  QMap<DownloadUsage, DownloadQueueSet *>::iterator const end = d->m_defaultQueueSets.end();
181  for (; pos != end; ++pos )
182  connectQueueSet( pos.value() );
183 }
184 
185 void HttpDownloadManager::connectQueueSet( DownloadQueueSet * queueSet )
186 {
187  connect( queueSet, SIGNAL(jobFinished(QByteArray,QString,QString)),
188  SLOT(finishJob(QByteArray,QString,QString)));
189  connect( queueSet, SIGNAL(jobRetry()), SLOT(startRetryTimer()));
190  connect( queueSet, SIGNAL(jobRedirected(QUrl,QString,QString,DownloadUsage)),
191  SLOT(addJob(QUrl,QString,QString,DownloadUsage)));
192  // relay jobAdded/jobRemoved signals (interesting for progress bar)
193  connect( queueSet, SIGNAL(jobAdded()), SIGNAL(jobAdded()));
194  connect( queueSet, SIGNAL(jobRemoved()), SIGNAL(jobRemoved()));
195  connect( queueSet, SIGNAL(progressChanged(int,int)), SIGNAL(progressChanged(int,int)) );
196 }
197 
198 bool HttpDownloadManager::hasDownloadPolicy( const DownloadPolicy& policy ) const
199 {
200  bool found = false;
201  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = d->m_queueSets.begin();
202  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator const end = d->m_queueSets.end();
203  for (; pos != end; ++pos ) {
204  if ( (*pos).second->downloadPolicy() == policy ) {
205  found = true;
206  break;
207  }
208  }
209  return found;
210 }
211 
212 #include "HttpDownloadManager.moc"
Marble::DownloadPolicy
Definition: DownloadPolicy.h:55
Marble::HttpDownloadManager::~HttpDownloadManager
virtual ~HttpDownloadManager()
Destroys the http download manager.
Definition: HttpDownloadManager.cpp:103
Marble::DownloadPolicy::setMaximumConnections
void setMaximumConnections(const int)
Definition: DownloadPolicy.cpp:84
Marble::HttpDownloadManager::addJob
void addJob(const QUrl &sourceUrl, const QString &destFilename, const QString &id, const DownloadUsage usage)
Adds a new job with a sourceUrl, destination file name and given id.
Definition: HttpDownloadManager.cpp:129
Marble::DownloadUsage
DownloadUsage
This enum is used to describe the type of download.
Definition: MarbleGlobal.h:164
QByteArray
Marble::StoragePolicy
Definition: StoragePolicy.h:25
Marble::HttpDownloadManager::setDownloadEnabled
void setDownloadEnabled(const bool enable)
Switches loading on/off, useful for offline mode.
Definition: HttpDownloadManager.cpp:108
QMap
QUrl::host
QString host() const
HttpDownloadManager.h
Marble::DownloadBrowse
Browsing mode, normal operation of Marble, like a web browser.
Definition: MarbleGlobal.h:166
Marble::DownloadQueueSet::addJob
void addJob(HttpJob *const job)
Definition: DownloadQueueSet.cpp:76
Marble::HttpDownloadManager::progressChanged
void progressChanged(int active, int queued)
A job was queued, activated or removed (finished, failed)
Marble::DownloadQueueSet
Life of a HttpJob
Definition: DownloadQueueSet.h:72
MarbleDebug.h
Marble::HttpDownloadManager::addDownloadPolicy
void addDownloadPolicy(const DownloadPolicy &)
Definition: HttpDownloadManager.cpp:119
DownloadPolicy.h
StoragePolicy.h
DownloadQueueSet.h
QTimer
Marble::DownloadPolicy::key
DownloadPolicyKey key() const
Definition: DownloadPolicy.cpp:89
Marble::DownloadQueueSet::canAcceptJob
bool canAcceptJob(const QUrl &sourceUrl, const QString &destinationFileName) const
Definition: DownloadQueueSet.cpp:51
requeueTime
const quint32 requeueTime
Definition: HttpDownloadManager.cpp:29
Marble::HttpJob::setDownloadUsage
void setDownloadUsage(const DownloadUsage)
Definition: HttpJob.cpp:112
Marble::HttpDownloadManager::jobAdded
void jobAdded()
Signal is emitted when a new job is added to the queue.
QString
QList
QPair
QUrl
QNetworkAccessManager
Marble::HttpDownloadManager::downloadComplete
void downloadComplete(QString, QString)
Marble::HttpDownloadManager::jobRemoved
void jobRemoved()
Signal is emitted when a job is removed from the queue.
Marble::HttpJob::setUserAgentPluginId
void setUserAgentPluginId(const QString &pluginId) const
Definition: HttpJob.cpp:117
HttpJob.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::HttpJob
Definition: HttpJob.h:33
QMap::iterator
Marble::DownloadQueueSet::downloadPolicy
DownloadPolicy downloadPolicy() const
Definition: DownloadQueueSet.cpp:41
Marble::DownloadBulk
Bulk download, for example "File/Download region".
Definition: MarbleGlobal.h:165
usage
void usage(const QString &app)
Definition: merge_ts_po.cpp:16
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal