• 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
DownloadQueueSet.cpp
Go to the documentation of this file.
1 // Copyright 2009 Jens-Michael Hoffmann <jmho@c-xx.com>
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include "DownloadQueueSet.h"
17 
18 #include "MarbleDebug.h"
19 
20 #include "HttpJob.h"
21 
22 namespace Marble
23 {
24 
25 DownloadQueueSet::DownloadQueueSet( QObject * const parent )
26  : QObject( parent )
27 {
28 }
29 
30 DownloadQueueSet::DownloadQueueSet( DownloadPolicy const & policy, QObject * const parent )
31  : QObject( parent ),
32  m_downloadPolicy( policy )
33 {
34 }
35 
36 DownloadQueueSet::~DownloadQueueSet()
37 {
38  // todo: delete HttpJobs
39 }
40 
41 DownloadPolicy DownloadQueueSet::downloadPolicy() const
42 {
43  return m_downloadPolicy;
44 }
45 
46 void DownloadQueueSet::setDownloadPolicy( DownloadPolicy const & policy )
47 {
48  m_downloadPolicy = policy;
49 }
50 
51 bool DownloadQueueSet::canAcceptJob( const QUrl& sourceUrl,
52  const QString& destinationFileName ) const
53 {
54  if ( jobIsQueued( destinationFileName )) {
55  mDebug() << "Download rejected: It's in the queue already:"
56  << destinationFileName;
57  return false;
58  }
59  if ( jobIsWaitingForRetry( destinationFileName )) {
60  mDebug() << "Download rejected: Will try to download again in some time:"
61  << destinationFileName;
62  return false;
63  }
64  if ( jobIsActive( destinationFileName )) {
65  mDebug() << "Download rejected: It's being downloaded already:"
66  << destinationFileName;
67  return false;
68  }
69  if ( jobIsBlackListed( sourceUrl )) {
70  mDebug() << "Download rejected: Blacklisted.";
71  return false;
72  }
73  return true;
74 }
75 
76 void DownloadQueueSet::addJob( HttpJob * const job )
77 {
78  m_jobs.push( job );
79  mDebug() << "addJob: new job queue size:" << m_jobs.count();
80  emit jobAdded();
81  emit progressChanged( m_activeJobs.size(), m_jobs.count() );
82  activateJobs();
83 }
84 
85 void DownloadQueueSet::activateJobs()
86 {
87  while ( !m_jobs.isEmpty()
88  && m_activeJobs.count() < m_downloadPolicy.maximumConnections() )
89  {
90  HttpJob * const job = m_jobs.pop();
91  activateJob( job );
92  }
93 }
94 
95 void DownloadQueueSet::retryJobs()
96 {
97  while ( !m_retryQueue.isEmpty() ) {
98  HttpJob * const job = m_retryQueue.dequeue();
99  mDebug() << "Requeuing" << job->destinationFileName();
100  // FIXME: addJob calls activateJobs every time
101  addJob( job );
102  }
103 }
104 
105 void DownloadQueueSet::purgeJobs()
106 {
107  // purge all waiting jobs
108  while( !m_jobs.isEmpty() ) {
109  HttpJob * const job = m_jobs.pop();
110  job->deleteLater();
111  }
112 
113  // purge all retry jobs
114  qDeleteAll( m_retryQueue );
115  m_retryQueue.clear();
116 
117  // cancel all current jobs
118  while( !m_activeJobs.isEmpty() ) {
119  deactivateJob( m_activeJobs.first() );
120  }
121 
122  emit progressChanged( m_activeJobs.size(), m_jobs.count() );
123 }
124 
125 void DownloadQueueSet::finishJob( HttpJob * job, const QByteArray& data )
126 {
127  mDebug() << "finishJob: " << job->sourceUrl() << job->destinationFileName();
128 
129  deactivateJob( job );
130  emit jobRemoved();
131  emit jobFinished( data, job->destinationFileName(), job->initiatorId() );
132  job->deleteLater();
133  activateJobs();
134 }
135 
136 void DownloadQueueSet::redirectJob( HttpJob * job, const QUrl& newSourceUrl )
137 {
138  mDebug() << "jobRedirected:" << job->sourceUrl() << " -> " << newSourceUrl;
139 
140  deactivateJob( job );
141  emit jobRemoved();
142  emit jobRedirected( newSourceUrl, job->destinationFileName(), job->initiatorId(),
143  job->downloadUsage() );
144  job->deleteLater();
145 }
146 
147 void DownloadQueueSet::retryOrBlacklistJob( HttpJob * job, const int errorCode )
148 {
149  Q_ASSERT( errorCode != 0 );
150  Q_ASSERT( !m_retryQueue.contains( job ));
151 
152  deactivateJob( job );
153  emit jobRemoved();
154 
155  if ( job->tryAgain() ) {
156  mDebug() << QString( "Download of %1 to %2 failed, but trying again soon" )
157  .arg( job->sourceUrl().toString() ).arg( job->destinationFileName() );
158  m_retryQueue.enqueue( job );
159  emit jobRetry();
160  }
161  else {
162  mDebug() << "JOB-address: " << job
163  << "Blacklist-size:" << m_jobBlackList.size()
164  << "err:" << errorCode;
165  m_jobBlackList.insert( job->sourceUrl().toString() );
166  mDebug() << QString( "Download of %1 Blacklisted. "
167  "Number of blacklist items: %2" )
168  .arg( job->destinationFileName() )
169  .arg( m_jobBlackList.size() );
170 
171  job->deleteLater();
172  }
173  activateJobs();
174 }
175 
176 void DownloadQueueSet::activateJob( HttpJob * const job )
177 {
178  m_activeJobs.push_back( job );
179  emit progressChanged( m_activeJobs.size(), m_jobs.count() );
180 
181  connect( job, SIGNAL(jobDone(HttpJob*,int)),
182  SLOT(retryOrBlacklistJob(HttpJob*,int)));
183  connect( job, SIGNAL(redirected(HttpJob*,QUrl)),
184  SLOT(redirectJob(HttpJob*,QUrl)));
185  connect( job, SIGNAL(dataReceived(HttpJob*,QByteArray)),
186  SLOT(finishJob(HttpJob*,QByteArray)));
187 
188  job->execute();
189 }
190 
198 void DownloadQueueSet::deactivateJob( HttpJob * const job )
199 {
200  const bool disconnected = job->disconnect();
201  Q_ASSERT( disconnected );
202  Q_UNUSED( disconnected ); // for Q_ASSERT in release mode
203  const bool removed = m_activeJobs.removeOne( job );
204  Q_ASSERT( removed );
205  Q_UNUSED( removed ); // for Q_ASSERT in release mode
206  emit progressChanged( m_activeJobs.size(), m_jobs.count() );
207 }
208 
209 bool DownloadQueueSet::jobIsActive( QString const & destinationFileName ) const
210 {
211  QList<HttpJob*>::const_iterator pos = m_activeJobs.constBegin();
212  QList<HttpJob*>::const_iterator const end = m_activeJobs.constEnd();
213  for (; pos != end; ++pos) {
214  if ( (*pos)->destinationFileName() == destinationFileName ) {
215  return true;
216  }
217  }
218  return false;
219 }
220 
221 inline bool DownloadQueueSet::jobIsQueued( QString const & destinationFileName ) const
222 {
223  return m_jobs.contains( destinationFileName );
224 }
225 
226 bool DownloadQueueSet::jobIsWaitingForRetry( QString const & destinationFileName ) const
227 {
228  QList<HttpJob*>::const_iterator pos = m_retryQueue.constBegin();
229  QList<HttpJob*>::const_iterator const end = m_retryQueue.constEnd();
230  for (; pos != end; ++pos) {
231  if ( (*pos)->destinationFileName() == destinationFileName ) {
232  return true;
233  }
234  }
235  return false;
236 }
237 
238 bool DownloadQueueSet::jobIsBlackListed( const QUrl& sourceUrl ) const
239 {
240  QSet<QString>::const_iterator const pos =
241  m_jobBlackList.constFind( sourceUrl.toString() );
242  return pos != m_jobBlackList.constEnd();
243 }
244 
245 
246 inline bool DownloadQueueSet::JobStack::contains( const QString& destinationFileName ) const
247 {
248  return m_jobsContent.contains( destinationFileName );
249 }
250 
251 inline int DownloadQueueSet::JobStack::count() const
252 {
253  return m_jobs.count();
254 }
255 
256 inline bool DownloadQueueSet::JobStack::isEmpty() const
257 {
258  return m_jobs.isEmpty();
259 }
260 
261 inline HttpJob * DownloadQueueSet::JobStack::pop()
262 {
263  HttpJob * const job = m_jobs.pop();
264  bool const removed = m_jobsContent.remove( job->destinationFileName() );
265  Q_UNUSED( removed ); // for Q_ASSERT in release mode
266  Q_ASSERT( removed );
267  return job;
268 }
269 
270 inline void DownloadQueueSet::JobStack::push( HttpJob * const job )
271 {
272  m_jobs.push( job );
273  m_jobsContent.insert( job->destinationFileName() );
274 }
275 
276 
277 }
278 
279 #include "DownloadQueueSet.moc"
Marble::DownloadQueueSet::jobRetry
void jobRetry()
Marble::DownloadPolicy
Definition: DownloadPolicy.h:55
Marble::DownloadQueueSet::jobAdded
void jobAdded()
Marble::DownloadQueueSet::jobFinished
void jobFinished(const QByteArray &data, const QString &destinationFileName, const QString &id)
QByteArray
QSet::size
int size() const
Marble::DownloadQueueSet::activateJobs
void activateJobs()
Definition: DownloadQueueSet.cpp:85
Marble::DownloadQueueSet::addJob
void addJob(HttpJob *const job)
Definition: DownloadQueueSet.cpp:76
QSet::insert
const_iterator insert(const T &value)
Marble::DownloadQueueSet::setDownloadPolicy
void setDownloadPolicy(const DownloadPolicy &)
Definition: DownloadQueueSet.cpp:46
MarbleDebug.h
QUrl::toString
QString toString(QFlags< QUrl::FormattingOption > options) const
QList::const_iterator
Marble::DownloadQueueSet::jobRemoved
void jobRemoved()
Marble::HttpJob::sourceUrl
QUrl sourceUrl() const
Definition: HttpJob.cpp:66
Marble::DownloadQueueSet::jobRedirected
void jobRedirected(const QUrl &newSourceUrl, const QString &destinationFileName, const QString &id, DownloadUsage)
DownloadQueueSet.h
QObject
Marble::DownloadQueueSet::canAcceptJob
bool canAcceptJob(const QUrl &sourceUrl, const QString &destinationFileName) const
Definition: DownloadQueueSet.cpp:51
Marble::DownloadPolicy::maximumConnections
int maximumConnections() const
Definition: DownloadPolicy.cpp:79
QSet::constEnd
const_iterator constEnd() const
Marble::DownloadQueueSet::progressChanged
void progressChanged(int active, int queued)
QSet
QObject::deleteLater
void deleteLater()
QString
QUrl
QSet::constFind
const_iterator constFind(const T &value) const
Marble::HttpJob::initiatorId
QString initiatorId() const
Definition: HttpJob.cpp:76
Marble::HttpJob::destinationFileName
QString destinationFileName() const
Definition: HttpJob.cpp:86
Marble::DownloadQueueSet::~DownloadQueueSet
~DownloadQueueSet()
Definition: DownloadQueueSet.cpp:36
HttpJob.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::DownloadQueueSet::purgeJobs
void purgeJobs()
Definition: DownloadQueueSet.cpp:105
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Marble::DownloadQueueSet::DownloadQueueSet
DownloadQueueSet(QObject *const parent=0)
Definition: DownloadQueueSet.cpp:25
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::HttpJob
Definition: HttpJob.h:33
Marble::DownloadQueueSet::retryJobs
void retryJobs()
Definition: DownloadQueueSet.cpp:95
Marble::DownloadQueueSet::downloadPolicy
DownloadPolicy downloadPolicy() const
Definition: DownloadQueueSet.cpp:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:38 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