32 m_downloadPolicy( policy )
43 return m_downloadPolicy;
48 m_downloadPolicy = policy;
52 const QString& destinationFileName )
const
54 if ( jobIsQueued( destinationFileName )) {
55 mDebug() <<
"Download rejected: It's in the queue already:"
56 << destinationFileName;
59 if ( jobIsWaitingForRetry( destinationFileName )) {
60 mDebug() <<
"Download rejected: Will try to download again in some time:"
61 << destinationFileName;
64 if ( jobIsActive( destinationFileName )) {
65 mDebug() <<
"Download rejected: It's being downloaded already:"
66 << destinationFileName;
69 if ( jobIsBlackListed( sourceUrl )) {
70 mDebug() <<
"Download rejected: Blacklisted.";
79 mDebug() <<
"addJob: new job queue size:" << m_jobs.count();
87 while ( !m_jobs.isEmpty()
90 HttpJob *
const job = m_jobs.pop();
97 while ( !m_retryQueue.isEmpty() ) {
98 HttpJob *
const job = m_retryQueue.dequeue();
108 while( !m_jobs.isEmpty() ) {
109 HttpJob *
const job = m_jobs.pop();
114 qDeleteAll( m_retryQueue );
115 m_retryQueue.clear();
118 while( !m_activeJobs.isEmpty() ) {
119 deactivateJob( m_activeJobs.first() );
129 deactivateJob( job );
136 void DownloadQueueSet::redirectJob( HttpJob * job,
const QUrl& newSourceUrl )
138 mDebug() <<
"jobRedirected:" << job->sourceUrl() <<
" -> " << newSourceUrl;
140 deactivateJob( job );
142 emit
jobRedirected( newSourceUrl, job->destinationFileName(), job->initiatorId(),
143 job->downloadUsage() );
147 void DownloadQueueSet::retryOrBlacklistJob( HttpJob * job,
const int errorCode )
149 Q_ASSERT( errorCode != 0 );
150 Q_ASSERT( !m_retryQueue.contains( job ));
152 deactivateJob( job );
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 );
162 mDebug() <<
"JOB-address: " << job
163 <<
"Blacklist-size:" << m_jobBlackList.
size()
164 <<
"err:" << errorCode;
165 m_jobBlackList.
insert( job->sourceUrl().toString() );
167 "Number of blacklist items: %2" )
168 .
arg( job->destinationFileName() )
169 .arg( m_jobBlackList.
size() );
176 void DownloadQueueSet::activateJob( HttpJob *
const job )
178 m_activeJobs.push_back( job );
181 connect( job, SIGNAL(jobDone(HttpJob*,
int)),
182 SLOT(retryOrBlacklistJob(HttpJob*,
int)));
184 SLOT(redirectJob(HttpJob*,
QUrl)));
198 void DownloadQueueSet::deactivateJob( HttpJob *
const job )
200 const bool disconnected = job->disconnect();
201 Q_ASSERT( disconnected );
202 Q_UNUSED( disconnected );
203 const bool removed = m_activeJobs.removeOne( job );
209 bool DownloadQueueSet::jobIsActive(
QString const & destinationFileName )
const
213 for (; pos != end; ++pos) {
214 if ( (*pos)->destinationFileName() == destinationFileName ) {
221 inline bool DownloadQueueSet::jobIsQueued(
QString const & destinationFileName )
const
223 return m_jobs.contains( destinationFileName );
226 bool DownloadQueueSet::jobIsWaitingForRetry(
QString const & destinationFileName )
const
230 for (; pos != end; ++pos) {
231 if ( (*pos)->destinationFileName() == destinationFileName ) {
238 bool DownloadQueueSet::jobIsBlackListed(
const QUrl& sourceUrl )
const
242 return pos != m_jobBlackList.
constEnd();
246 inline bool DownloadQueueSet::JobStack::contains(
const QString& destinationFileName )
const
248 return m_jobsContent.contains( destinationFileName );
251 inline int DownloadQueueSet::JobStack::count()
const
253 return m_jobs.count();
256 inline bool DownloadQueueSet::JobStack::isEmpty()
const
258 return m_jobs.isEmpty();
261 inline HttpJob * DownloadQueueSet::JobStack::pop()
263 HttpJob *
const job = m_jobs.pop();
264 bool const removed = m_jobsContent.remove( job->destinationFileName() );
270 inline void DownloadQueueSet::JobStack::push( HttpJob *
const job )
273 m_jobsContent.insert( job->destinationFileName() );
279 #include "DownloadQueueSet.moc"
void jobFinished(const QByteArray &data, const QString &destinationFileName, const QString &id)
void addJob(HttpJob *const job)
const_iterator insert(const T &value)
void setDownloadPolicy(const DownloadPolicy &)
QString toString(QFlags< QUrl::FormattingOption > options) const
void jobRedirected(const QUrl &newSourceUrl, const QString &destinationFileName, const QString &id, DownloadUsage)
bool canAcceptJob(const QUrl &sourceUrl, const QString &destinationFileName) const
int maximumConnections() const
const_iterator constEnd() const
void progressChanged(int active, int queued)
const_iterator constFind(const T &value) const
QString initiatorId() const
QString destinationFileName() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
DownloadQueueSet(QObject *const parent=0)
QDebug mDebug()
a function to replace qDebug() in Marble library code
DownloadPolicy downloadPolicy() const