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

marble

  • sources
  • kde-4.12
  • 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  bool m_downloadEnabled;
40  QTimer m_requeueTimer;
46  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> > m_queueSets;
47  QMap<DownloadUsage, DownloadQueueSet *> m_defaultQueueSets;
48  StoragePolicy *const m_storagePolicy;
49  QNetworkAccessManager m_networkAccessManager;
50 
51 };
52 
53 HttpDownloadManager::Private::Private( StoragePolicy *policy )
54  : m_downloadEnabled( true ), //enabled for now
55  m_requeueTimer(),
56  m_storagePolicy( policy ),
57  m_networkAccessManager()
58 {
59  // setup default download policy and associated queue set
60  DownloadPolicy defaultBrowsePolicy;
61  defaultBrowsePolicy.setMaximumConnections( 20 );
62  m_defaultQueueSets[ DownloadBrowse ] = new DownloadQueueSet( defaultBrowsePolicy );
63  DownloadPolicy defaultBulkDownloadPolicy;
64  defaultBulkDownloadPolicy.setMaximumConnections( 2 );
65  m_defaultQueueSets[ DownloadBulk ] = new DownloadQueueSet( defaultBulkDownloadPolicy );
66 }
67 
68 HttpDownloadManager::Private::~Private()
69 {
70  QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = m_defaultQueueSets.begin();
71  QMap<DownloadUsage, DownloadQueueSet *>::iterator const end = m_defaultQueueSets.end();
72  for (; pos != end; ++pos )
73  delete pos.value();
74 }
75 
76 DownloadQueueSet *HttpDownloadManager::Private::findQueues( const QString& hostName,
77  const DownloadUsage usage )
78 {
79  DownloadQueueSet * result = 0;
80  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = m_queueSets.begin();
81  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator const end = m_queueSets.end();
82  for (; pos != end; ++pos ) {
83  if ( (*pos).first.matches( hostName, usage )) {
84  result = (*pos).second;
85  break;
86  }
87  }
88  if ( !result ) {
89  mDebug() << "No download policy found for" << hostName << usage
90  << ", using default policy.";
91  result = m_defaultQueueSets[ usage ];
92  }
93  return result;
94 }
95 
96 
97 HttpDownloadManager::HttpDownloadManager( StoragePolicy *policy )
98  : d( new Private( policy ) )
99 {
100  d->m_requeueTimer.setInterval( requeueTime );
101  connect( &d->m_requeueTimer, SIGNAL(timeout()), this, SLOT(requeue()) );
102  connectDefaultQueueSets();
103 }
104 
105 HttpDownloadManager::~HttpDownloadManager()
106 {
107  delete d;
108 }
109 
110 void HttpDownloadManager::setDownloadEnabled( const bool enable )
111 {
112  d->m_downloadEnabled = enable;
113  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
114  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator const end = d->m_queueSets.end();
115  for (; pos != end; ++pos ) {
116  pos->second->purgeJobs();
117  }
118 
119 }
120 
121 void HttpDownloadManager::addDownloadPolicy( const DownloadPolicy& policy )
122 {
123  if ( hasDownloadPolicy( policy ))
124  return;
125  DownloadQueueSet * const queueSet = new DownloadQueueSet( policy, this );
126  connectQueueSet( queueSet );
127  d->m_queueSets.append( QPair<DownloadPolicyKey, DownloadQueueSet *>
128  ( queueSet->downloadPolicy().key(), queueSet ));
129 }
130 
131 void HttpDownloadManager::addJob( const QUrl& sourceUrl, const QString& destFileName,
132  const QString &id, const DownloadUsage usage )
133 {
134  if ( !d->m_downloadEnabled )
135  return;
136 
137  DownloadQueueSet * const queueSet = d->findQueues( sourceUrl.host(), usage );
138  if ( queueSet->canAcceptJob( sourceUrl, destFileName )) {
139  HttpJob * const job = new HttpJob( sourceUrl, destFileName, id, &d->m_networkAccessManager );
140  job->setUserAgentPluginId( "QNamNetworkPlugin" );
141  job->setDownloadUsage( usage );
142  queueSet->addJob( job );
143  }
144 }
145 
146 void HttpDownloadManager::finishJob( const QByteArray& data, const QString& destinationFileName,
147  const QString& id )
148 {
149  mDebug() << "emitting downloadComplete( QByteArray, " << id << ")";
150  emit downloadComplete( data, id );
151  if ( d->m_storagePolicy ) {
152  const bool saved = d->m_storagePolicy->updateFile( destinationFileName, data );
153  if ( saved ) {
154  mDebug() << "emitting downloadComplete( " << destinationFileName << ", " << id << ")";
155  emit downloadComplete( destinationFileName, id );
156  } else {
157  qWarning() << "Could not save:" << destinationFileName;
158  }
159  }
160 }
161 
162 void HttpDownloadManager::requeue()
163 {
164  d->m_requeueTimer.stop();
165 
166  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
167  QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator const end = d->m_queueSets.end();
168  for (; pos != end; ++pos ) {
169  (*pos).second->retryJobs();
170  }
171 }
172 
173 void HttpDownloadManager::startRetryTimer()
174 {
175  if ( !d->m_requeueTimer.isActive() )
176  d->m_requeueTimer.start();
177 }
178 
179 void HttpDownloadManager::connectDefaultQueueSets()
180 {
181  QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = d->m_defaultQueueSets.begin();
182  QMap<DownloadUsage, DownloadQueueSet *>::iterator const end = d->m_defaultQueueSets.end();
183  for (; pos != end; ++pos )
184  connectQueueSet( pos.value() );
185 }
186 
187 void HttpDownloadManager::connectQueueSet( DownloadQueueSet * queueSet )
188 {
189  connect( queueSet, SIGNAL(jobFinished(QByteArray,QString,QString)),
190  SLOT(finishJob(QByteArray,QString,QString)));
191  connect( queueSet, SIGNAL(jobRetry()), SLOT(startRetryTimer()));
192  connect( queueSet, SIGNAL(jobRedirected(QUrl,QString,QString,DownloadUsage)),
193  SLOT(addJob(QUrl,QString,QString,DownloadUsage)));
194  // relay jobAdded/jobRemoved signals (interesting for progress bar)
195  connect( queueSet, SIGNAL(jobAdded()), SIGNAL(jobAdded()));
196  connect( queueSet, SIGNAL(jobRemoved()), SIGNAL(jobRemoved()));
197  connect( queueSet, SIGNAL(progressChanged(int,int)), SIGNAL(progressChanged(int,int)) );
198 }
199 
200 bool HttpDownloadManager::hasDownloadPolicy( const DownloadPolicy& policy ) const
201 {
202  bool found = false;
203  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = d->m_queueSets.begin();
204  QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator const end = d->m_queueSets.end();
205  for (; pos != end; ++pos ) {
206  if ( (*pos).second->downloadPolicy() == policy ) {
207  found = true;
208  break;
209  }
210  }
211  return found;
212 }
213 
214 #include "HttpDownloadManager.moc"
Marble::DownloadPolicy
Definition: DownloadPolicy.h:55
Marble::HttpDownloadManager::~HttpDownloadManager
virtual ~HttpDownloadManager()
Destroys the http download manager.
Definition: HttpDownloadManager.cpp:105
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:131
Marble::DownloadUsage
DownloadUsage
This enum is used to describe the type of download.
Definition: MarbleGlobal.h:160
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:110
HttpDownloadManager.h
Marble::DownloadBrowse
Browsing mode, normal operation of Marble, like a web browser.
Definition: MarbleGlobal.h:162
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:121
DownloadPolicy.h
StoragePolicy.h
DownloadQueueSet.h
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.
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
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::HttpJob
Definition: HttpJob.h:33
Marble::DownloadQueueSet::downloadPolicy
DownloadPolicy downloadPolicy() const
Definition: DownloadQueueSet.cpp:41
Marble::DownloadBulk
Bulk download, for example "File/Download region".
Definition: MarbleGlobal.h:161
usage
void usage(const QString &app)
Definition: merge_ts_po.cpp:16
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:50 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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