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,
const QString &destinationFileName)
const
42 if (jobIsQueued(destinationFileName)) {
43 mDebug() <<
"Download rejected: It's in the queue already:" << destinationFileName;
46 if (jobIsWaitingForRetry(destinationFileName)) {
47 mDebug() <<
"Download rejected: Will try to download again in some time:" << destinationFileName;
50 if (jobIsActive(destinationFileName)) {
51 mDebug() <<
"Download rejected: It's being downloaded already:" << destinationFileName;
54 if (jobIsBlackListed(sourceUrl)) {
55 mDebug() <<
"Download rejected: Blacklisted.";
61void DownloadQueueSet::addJob(HttpJob *
const job)
64 mDebug() <<
"addJob: new job queue size:" << m_jobs.count();
66 Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
70void DownloadQueueSet::activateJobs()
72 while (!m_jobs.isEmpty() && m_activeJobs.count() < m_downloadPolicy.maximumConnections()) {
73 HttpJob *
const job = m_jobs.pop();
78void DownloadQueueSet::retryJobs()
80 while (!m_retryQueue.isEmpty()) {
81 HttpJob *
const job = m_retryQueue.dequeue();
82 mDebug() <<
"Requeuing" << job->destinationFileName();
88void DownloadQueueSet::purgeJobs()
91 while (!m_jobs.isEmpty()) {
92 HttpJob *
const job = m_jobs.pop();
97 qDeleteAll(m_retryQueue);
101 while (!m_activeJobs.isEmpty()) {
102 deactivateJob(m_activeJobs.first());
105 Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
108void DownloadQueueSet::finishJob(HttpJob *job,
const QByteArray &data)
110 mDebug() << job->sourceUrl() << job->destinationFileName();
114 Q_EMIT jobFinished(data, job->destinationFileName(), job->initiatorId());
119void DownloadQueueSet::redirectJob(HttpJob *job,
const QUrl &newSourceUrl)
121 mDebug() << job->sourceUrl() <<
" -> " << newSourceUrl;
125 Q_EMIT jobRedirected(newSourceUrl, job->destinationFileName(), job->initiatorId(), job->downloadUsage());
129void DownloadQueueSet::retryOrBlacklistJob(HttpJob *job,
const int errorCode)
131 Q_ASSERT(errorCode != 0);
132 Q_ASSERT(!m_retryQueue.contains(job));
137 if (job->tryAgain()) {
138 mDebug() << QStringLiteral(
"Download of %1 to %2 failed, but trying again soon").arg(job->sourceUrl().toString(), job->destinationFileName());
139 m_retryQueue.enqueue(job);
142 mDebug() <<
"JOB-address: " << job <<
"Blacklist-size:" << m_jobBlackList.size() <<
"err:" << errorCode;
143 m_jobBlackList.insert(job->sourceUrl().toString());
144 mDebug() << QStringLiteral(
145 "Download of %1 Blacklisted. "
146 "Number of blacklist items: %2")
147 .arg(job->destinationFileName())
148 .arg(m_jobBlackList.size());
155void DownloadQueueSet::activateJob(HttpJob *
const job)
157 m_activeJobs.push_back(job);
158 Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
160 connect(job, SIGNAL(jobDone(HttpJob *,
int)), SLOT(retryOrBlacklistJob(HttpJob *,
int)));
161 connect(job, SIGNAL(redirected(HttpJob *,
QUrl)), SLOT(redirectJob(HttpJob *,
QUrl)));
174void DownloadQueueSet::deactivateJob(HttpJob *
const job)
176 const bool disconnected = job->disconnect();
177 Q_ASSERT(disconnected);
178 Q_UNUSED(disconnected);
179 const bool removed = m_activeJobs.removeOne(job);
182 Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
185bool DownloadQueueSet::jobIsActive(
QString const &destinationFileName)
const
189 for (; pos !=
end; ++pos) {
190 if ((*pos)->destinationFileName() == destinationFileName) {
197inline bool DownloadQueueSet::jobIsQueued(
QString const &destinationFileName)
const
199 return m_jobs.contains(destinationFileName);
202bool DownloadQueueSet::jobIsWaitingForRetry(
QString const &destinationFileName)
const
206 for (; pos !=
end; ++pos) {
207 if ((*pos)->destinationFileName() == destinationFileName) {
214bool DownloadQueueSet::jobIsBlackListed(
const QUrl &sourceUrl)
const
217 return pos != m_jobBlackList.constEnd();
220inline bool DownloadQueueSet::JobStack::contains(
const QString &destinationFileName)
const
222 return m_jobsContent.contains(destinationFileName);
225inline int DownloadQueueSet::JobStack::count()
const
227 return m_jobs.count();
230inline bool DownloadQueueSet::JobStack::isEmpty()
const
232 return m_jobs.isEmpty();
235inline HttpJob *DownloadQueueSet::JobStack::pop()
237 HttpJob *
const job = m_jobs.pop();
238 bool const removed = m_jobsContent.remove(job->destinationFileName());
244inline void DownloadQueueSet::JobStack::push(HttpJob *
const job)
247 m_jobsContent.insert(job->destinationFileName());
252#include "moc_DownloadQueueSet.cpp"
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
Binds a QML item to a specific geodetic location in screen coordinates.
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString(FormattingOptions options) const const