18 #include <QNetworkAccessManager>
26 using namespace Marble;
31 class HttpDownloadManager::Private
39 bool m_downloadEnabled;
40 QTimer m_requeueTimer;
46 QList<QPair<DownloadPolicyKey, DownloadQueueSet *> > m_queueSets;
47 QMap<DownloadUsage, DownloadQueueSet *> m_defaultQueueSets;
49 QNetworkAccessManager m_networkAccessManager;
53 HttpDownloadManager::Private::Private(
StoragePolicy *policy )
54 : m_downloadEnabled( true ),
56 m_storagePolicy( policy ),
57 m_networkAccessManager()
68 HttpDownloadManager::Private::~Private()
70 QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = m_defaultQueueSets.begin();
71 QMap<DownloadUsage, DownloadQueueSet *>::iterator
const end = m_defaultQueueSets.end();
72 for (; pos != end; ++pos )
76 DownloadQueueSet *HttpDownloadManager::Private::findQueues(
const QString& hostName,
80 QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = m_queueSets.begin();
81 QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator
const end = m_queueSets.end();
82 for (; pos != end; ++pos ) {
83 if ( (*pos).first.matches( hostName, usage )) {
84 result = (*pos).second;
89 mDebug() <<
"No download policy found for" << hostName << usage
90 <<
", using default policy.";
91 result = m_defaultQueueSets[
usage ];
98 : d( new Private( policy ) )
101 connect( &d->m_requeueTimer, SIGNAL(timeout()),
this, SLOT(requeue()) );
102 connectDefaultQueueSets();
112 d->m_downloadEnabled = enable;
113 QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
114 QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator
const end = d->m_queueSets.end();
115 for (; pos != end; ++pos ) {
116 pos->second->purgeJobs();
123 if ( hasDownloadPolicy( policy ))
126 connectQueueSet( queueSet );
127 d->m_queueSets.append( QPair<DownloadPolicyKey, DownloadQueueSet *>
134 if ( !d->m_downloadEnabled )
138 if ( queueSet->
canAcceptJob( sourceUrl, destFileName )) {
139 HttpJob *
const job =
new HttpJob( sourceUrl, destFileName,
id, &d->m_networkAccessManager );
146 void HttpDownloadManager::finishJob(
const QByteArray& data,
const QString& destinationFileName,
149 mDebug() <<
"emitting downloadComplete( QByteArray, " <<
id <<
")";
151 if ( d->m_storagePolicy ) {
152 const bool saved = d->m_storagePolicy->updateFile( destinationFileName, data );
154 mDebug() <<
"emitting downloadComplete( " << destinationFileName <<
", " <<
id <<
")";
157 qWarning() <<
"Could not save:" << destinationFileName;
162 void HttpDownloadManager::requeue()
164 d->m_requeueTimer.stop();
166 QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator pos = d->m_queueSets.begin();
167 QList<QPair<DownloadPolicyKey, DownloadQueueSet *> >::iterator
const end = d->m_queueSets.end();
168 for (; pos != end; ++pos ) {
169 (*pos).second->retryJobs();
173 void HttpDownloadManager::startRetryTimer()
175 if ( !d->m_requeueTimer.isActive() )
176 d->m_requeueTimer.start();
179 void HttpDownloadManager::connectDefaultQueueSets()
181 QMap<DownloadUsage, DownloadQueueSet *>::iterator pos = d->m_defaultQueueSets.begin();
182 QMap<DownloadUsage, DownloadQueueSet *>::iterator
const end = d->m_defaultQueueSets.end();
183 for (; pos != end; ++pos )
184 connectQueueSet( pos.value() );
189 connect( queueSet, SIGNAL(jobFinished(QByteArray,QString,QString)),
190 SLOT(finishJob(QByteArray,QString,QString)));
191 connect( queueSet, SIGNAL(jobRetry()), SLOT(startRetryTimer()));
192 connect( queueSet, SIGNAL(jobRedirected(QUrl,QString,QString,
DownloadUsage)),
200 bool HttpDownloadManager::hasDownloadPolicy(
const DownloadPolicy& policy )
const
203 QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator pos = d->m_queueSets.begin();
204 QList<QPair<DownloadPolicyKey, DownloadQueueSet*> >::iterator
const end = d->m_queueSets.end();
205 for (; pos != end; ++pos ) {
206 if ( (*pos).second->downloadPolicy() == policy ) {
214 #include "HttpDownloadManager.moc"
virtual ~HttpDownloadManager()
Destroys the http download manager.
void setMaximumConnections(const int)
void addJob(const QUrl &sourceUrl, const QString &destFilename, const QString &id, const DownloadUsage usage)
Adds a new job with a sourceUrl, destination file name and given id.
DownloadUsage
This enum is used to describe the type of download.
void setDownloadEnabled(const bool enable)
Switches loading on/off, useful for offline mode.
Browsing mode, normal operation of Marble, like a web browser.
void addJob(HttpJob *const job)
void progressChanged(int active, int queued)
A job was queued, activated or removed (finished, failed)
void addDownloadPolicy(const DownloadPolicy &)
DownloadPolicyKey key() const
bool canAcceptJob(const QUrl &sourceUrl, const QString &destinationFileName) const
const quint32 requeueTime
void setDownloadUsage(const DownloadUsage)
void jobAdded()
Signal is emitted when a new job is added to the queue.
void downloadComplete(QString, QString)
void jobRemoved()
Signal is emitted when a job is removed from the queue.
void setUserAgentPluginId(const QString &pluginId) const
QDebug mDebug()
a function to replace qDebug() in Marble library code
DownloadPolicy downloadPolicy() const
Bulk download, for example "File/Download region".
void usage(const QString &app)