5 #include "DownloadQueueSet.h"
7 #include "MarbleDebug.h"
14 DownloadQueueSet::DownloadQueueSet(
QObject *
const parent )
19 DownloadQueueSet::DownloadQueueSet( DownloadPolicy
const & policy,
QObject *
const parent )
21 m_downloadPolicy( policy )
25 DownloadQueueSet::~DownloadQueueSet()
30 DownloadPolicy DownloadQueueSet::downloadPolicy()
const
32 return m_downloadPolicy;
35 void DownloadQueueSet::setDownloadPolicy( DownloadPolicy
const & policy )
37 m_downloadPolicy = policy;
40 bool 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.";
65 void DownloadQueueSet::addJob( HttpJob *
const job )
68 mDebug() <<
"addJob: new job queue size:" << m_jobs.count();
70 emit progressChanged( m_activeJobs.
size(), m_jobs.count() );
74 void DownloadQueueSet::activateJobs()
76 while ( !m_jobs.isEmpty()
77 && m_activeJobs.
count() < m_downloadPolicy.maximumConnections() )
79 HttpJob *
const job = m_jobs.pop();
84 void DownloadQueueSet::retryJobs()
86 while ( !m_retryQueue.isEmpty() ) {
87 HttpJob *
const job = m_retryQueue.
dequeue();
88 mDebug() <<
"Requeuing" << job->destinationFileName();
94 void 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() );
114 void DownloadQueueSet::finishJob( HttpJob * job,
const QByteArray& data )
116 mDebug() <<
"finishJob: " << job->sourceUrl() << job->destinationFileName();
118 deactivateJob( job );
120 emit jobFinished( data, job->destinationFileName(), job->initiatorId() );
125 void DownloadQueueSet::redirectJob( HttpJob * job,
const QUrl& newSourceUrl )
127 mDebug() <<
"jobRedirected:" << job->sourceUrl() <<
" -> " << newSourceUrl;
129 deactivateJob( job );
131 emit jobRedirected( newSourceUrl, job->destinationFileName(), job->initiatorId(),
132 job->downloadUsage() );
136 void 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() );
151 mDebug() <<
"JOB-address: " << job
152 <<
"Blacklist-size:" << m_jobBlackList.
size()
153 <<
"err:" << errorCode;
154 m_jobBlackList.
insert( job->sourceUrl().toString() );
156 "Number of blacklist items: %2" )
157 .
arg( job->destinationFileName() )
165 void DownloadQueueSet::activateJob( HttpJob *
const 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)));
187 void 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() );
198 bool DownloadQueueSet::jobIsActive(
QString const & destinationFileName )
const
202 for (; pos !=
end; ++pos) {
203 if ( (*pos)->destinationFileName() == destinationFileName ) {
210 inline bool DownloadQueueSet::jobIsQueued(
QString const & destinationFileName )
const
212 return m_jobs.contains( destinationFileName );
215 bool DownloadQueueSet::jobIsWaitingForRetry(
QString const & destinationFileName )
const
219 for (; pos !=
end; ++pos) {
220 if ( (*pos)->destinationFileName() == destinationFileName ) {
227 bool DownloadQueueSet::jobIsBlackListed(
const QUrl& sourceUrl )
const
231 return pos != m_jobBlackList.
constEnd();
235 inline bool DownloadQueueSet::JobStack::contains(
const QString& destinationFileName )
const
237 return m_jobsContent.contains( destinationFileName );
240 inline int DownloadQueueSet::JobStack::count()
const
242 return m_jobs.count();
245 inline bool DownloadQueueSet::JobStack::isEmpty()
const
247 return m_jobs.isEmpty();
250 inline HttpJob * DownloadQueueSet::JobStack::pop()
252 HttpJob *
const job = m_jobs.pop();
253 bool const removed = m_jobsContent.remove( job->destinationFileName() );
259 inline void DownloadQueueSet::JobStack::push( HttpJob *
const job )
262 m_jobsContent.insert( job->destinationFileName() );
268 #include "moc_DownloadQueueSet.cpp"