5#include "DownloadQueueSet.h"
7#include "MarbleDebug.h"
14DownloadQueueSet::DownloadQueueSet(
QObject *
const parent )
19DownloadQueueSet::DownloadQueueSet( DownloadPolicy
const & policy,
QObject *
const parent )
21 m_downloadPolicy( policy )
25DownloadQueueSet::~DownloadQueueSet()
30DownloadPolicy DownloadQueueSet::downloadPolicy()
const
32 return m_downloadPolicy;
35void DownloadQueueSet::setDownloadPolicy( DownloadPolicy
const & policy )
37 m_downloadPolicy = policy;
40bool DownloadQueueSet::canAcceptJob(
const QUrl& sourceUrl,
41 const QString& destinationFileName )
const
43 if ( jobIsQueued( destinationFileName )) {
44 mDebug() <<
"Download rejected: It's in the queue already:"
45 << destinationFileName;
48 if ( jobIsWaitingForRetry( destinationFileName )) {
49 mDebug() <<
"Download rejected: Will try to download again in some time:"
50 << destinationFileName;
53 if ( jobIsActive( destinationFileName )) {
54 mDebug() <<
"Download rejected: It's being downloaded already:"
55 << destinationFileName;
58 if ( jobIsBlackListed( sourceUrl )) {
59 mDebug() <<
"Download rejected: Blacklisted.";
65void DownloadQueueSet::addJob( HttpJob *
const job )
68 mDebug() <<
"addJob: new job queue size:" << m_jobs.count();
70 emit progressChanged( m_activeJobs.size(), m_jobs.count() );
74void DownloadQueueSet::activateJobs()
76 while ( !m_jobs.isEmpty()
77 && m_activeJobs.count() < m_downloadPolicy.maximumConnections() )
79 HttpJob *
const job = m_jobs.pop();
84void DownloadQueueSet::retryJobs()
86 while ( !m_retryQueue.isEmpty() ) {
87 HttpJob *
const job = m_retryQueue.dequeue();
88 mDebug() <<
"Requeuing" << job->destinationFileName();
94void DownloadQueueSet::purgeJobs()
97 while( !m_jobs.isEmpty() ) {
98 HttpJob *
const job = m_jobs.pop();
103 qDeleteAll( m_retryQueue );
104 m_retryQueue.clear();
107 while( !m_activeJobs.isEmpty() ) {
108 deactivateJob( m_activeJobs.first() );
111 emit progressChanged( m_activeJobs.size(), m_jobs.count() );
114void DownloadQueueSet::finishJob( HttpJob * job,
const QByteArray& data )
116 mDebug() << job->sourceUrl() << job->destinationFileName();
118 deactivateJob( job );
120 emit jobFinished( data, job->destinationFileName(), job->initiatorId() );
125void DownloadQueueSet::redirectJob( HttpJob * job,
const QUrl& newSourceUrl )
127 mDebug() << job->sourceUrl() <<
" -> " << newSourceUrl;
129 deactivateJob( job );
131 emit jobRedirected( newSourceUrl, job->destinationFileName(), job->initiatorId(),
132 job->downloadUsage() );
136void DownloadQueueSet::retryOrBlacklistJob( HttpJob * job,
const int errorCode )
138 Q_ASSERT( errorCode != 0 );
139 Q_ASSERT( !m_retryQueue.contains( job ));
141 deactivateJob( job );
144 if ( job->tryAgain() ) {
145 mDebug() <<
QString(
"Download of %1 to %2 failed, but trying again soon" )
146 .
arg( job->sourceUrl().toString(), job->destinationFileName() );
147 m_retryQueue.enqueue( job );
151 mDebug() <<
"JOB-address: " << job
152 <<
"Blacklist-size:" << m_jobBlackList.size()
153 <<
"err:" << errorCode;
154 m_jobBlackList.insert( job->sourceUrl().toString() );
155 mDebug() <<
QString(
"Download of %1 Blacklisted. "
156 "Number of blacklist items: %2" )
157 .
arg( job->destinationFileName() )
158 .
arg( m_jobBlackList.size() );
165void DownloadQueueSet::activateJob( HttpJob *
const job )
167 m_activeJobs.push_back( job );
168 emit progressChanged( m_activeJobs.size(), m_jobs.count() );
170 connect( job, SIGNAL(jobDone(HttpJob*,
int)),
171 SLOT(retryOrBlacklistJob(HttpJob*,
int)));
173 SLOT(redirectJob(HttpJob*,
QUrl)));
187void DownloadQueueSet::deactivateJob( HttpJob *
const job )
189 const bool disconnected = job->disconnect();
190 Q_ASSERT( disconnected );
191 Q_UNUSED( disconnected );
192 const bool removed = m_activeJobs.removeOne( job );
195 emit progressChanged( m_activeJobs.size(), m_jobs.count() );
198bool DownloadQueueSet::jobIsActive(
QString const & destinationFileName )
const
202 for (; pos !=
end; ++pos) {
203 if ( (*pos)->destinationFileName() == destinationFileName ) {
210inline bool DownloadQueueSet::jobIsQueued(
QString const & destinationFileName )
const
212 return m_jobs.contains( destinationFileName );
215bool DownloadQueueSet::jobIsWaitingForRetry(
QString const & destinationFileName )
const
219 for (; pos !=
end; ++pos) {
220 if ( (*pos)->destinationFileName() == destinationFileName ) {
227bool DownloadQueueSet::jobIsBlackListed(
const QUrl& sourceUrl )
const
230 m_jobBlackList.constFind( sourceUrl.
toString() );
231 return pos != m_jobBlackList.constEnd();
235inline bool DownloadQueueSet::JobStack::contains(
const QString& destinationFileName )
const
237 return m_jobsContent.contains( destinationFileName );
240inline int DownloadQueueSet::JobStack::count()
const
242 return m_jobs.count();
245inline bool DownloadQueueSet::JobStack::isEmpty()
const
247 return m_jobs.isEmpty();
250inline HttpJob * DownloadQueueSet::JobStack::pop()
252 HttpJob *
const job = m_jobs.pop();
253 bool const removed = m_jobsContent.remove( job->destinationFileName() );
259inline void DownloadQueueSet::JobStack::push( HttpJob *
const job )
262 m_jobsContent.insert( job->destinationFileName() );
268#include "moc_DownloadQueueSet.cpp"
const QList< QKeySequence > & end()
Binds a QML item to a specific geodetic location in screen coordinates.
QString arg(Args &&... args) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString(FormattingOptions options) const const