Sync from quickmods

This commit is contained in:
Petr Mrázek
2014-09-06 18:16:56 +02:00
parent 36efcf8d3c
commit 20cb97a35a
57 changed files with 569 additions and 326 deletions

View File

@ -53,16 +53,42 @@ void ByteArrayDownload::downloadError(QNetworkReply::NetworkError error)
QLOG_ERROR() << "Error getting URL:" << m_url.toString().toLocal8Bit()
<< "Network error: " << error;
m_status = Job_Failed;
m_errorString = m_reply->errorString();
}
void ByteArrayDownload::downloadFinished()
{
if (m_followRedirects)
{
QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader);
QString redirectURL;
if(redirect.isValid())
{
redirectURL = redirect.toString();
}
// FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061
else if(m_reply->hasRawHeader("Location"))
{
auto data = m_reply->rawHeader("Location");
if(data.size() > 2 && data[0] == '/' && data[1] == '/')
redirectURL = m_reply->url().scheme() + ":" + data;
}
if (!redirectURL.isEmpty())
{
m_url = QUrl(redirect.toString());
QLOG_INFO() << "Following redirect to " << m_url.toString();
start();
return;
}
}
// if the download succeeded
if (m_status != Job_Failed)
{
// nothing went wrong...
m_status = Job_Finished;
m_data = m_reply->readAll();
m_content_type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();
m_reply.reset();
emit succeeded(m_index_within_job);
return;

View File

@ -31,6 +31,10 @@ public:
/// if not saving to file, downloaded data is placed here
QByteArray m_data;
QString m_errorString;
bool m_followRedirects = false;
public
slots:
virtual void start();

View File

@ -101,6 +101,30 @@ void CacheDownload::downloadError(QNetworkReply::NetworkError error)
}
void CacheDownload::downloadFinished()
{
if (m_followRedirects)
{
QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader);
QString redirectURL;
if(redirect.isValid())
{
redirectURL = redirect.toString();
}
// FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061
else if(m_reply->hasRawHeader("Location"))
{
auto data = m_reply->rawHeader("Location");
if(data.size() > 2 && data[0] == '/' && data[1] == '/')
redirectURL = m_reply->url().scheme() + ":" + data;
}
if (!redirectURL.isEmpty())
{
m_url = QUrl(redirect.toString());
QLOG_INFO() << "Following redirect to " << m_url.toString();
start();
return;
}
}
// if the download succeeded
if (m_status == Job_Failed)
{

View File

@ -36,6 +36,8 @@ private:
bool wroteAnyData = false;
public:
bool m_followRedirects = false;
explicit CacheDownload(QUrl url, MetaEntryPtr entry);
static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry)
{

View File

@ -55,6 +55,9 @@ public:
/// the network reply
std::shared_ptr<QNetworkReply> m_reply;
/// the content of the content-type header
QString m_content_type;
/// source URL
QUrl m_url;

View File

@ -30,8 +30,8 @@ class NetJob : public ProgressProvider
{
Q_OBJECT
public:
explicit NetJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {};
virtual ~NetJob() {};
explicit NetJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {}
virtual ~NetJob() {}
template <typename T> bool addNetAction(T action)
{
NetActionPtr base = std::static_pointer_cast<NetAction>(action);
@ -62,7 +62,10 @@ public:
{
return downloads[index];
}
;
const NetActionPtr at(const int index)
{
return downloads.at(index);
}
NetActionPtr first()
{
if (downloads.size())
@ -73,21 +76,10 @@ public:
{
return downloads.size();
}
virtual void getProgress(qint64 &current, qint64 &total)
{
current = current_progress;
total = total_progress;
}
;
virtual QString getStatus() const
{
return m_job_name;
}
virtual bool isRunning() const
{
return m_running;
}
;
QStringList getFailedFiles();
signals:
void started();