Browse Source

Fix downloadthread.* coding style (Issue #2192).

adaptive-webui-19844
Vladimir Golovnev (Glassez) 10 years ago
parent
commit
f1bce0b8e0
  1. 73
      src/core/downloadthread.cpp
  2. 5
      src/core/downloadthread.h

73
src/core/downloadthread.cpp

@ -43,14 +43,17 @@ @@ -43,14 +43,17 @@
/** Download Thread **/
DownloadThread::DownloadThread(QObject* parent) : QObject(parent) {
DownloadThread::DownloadThread(QObject* parent)
: QObject(parent)
{
connect(&m_networkManager, SIGNAL(finished (QNetworkReply*)), this, SLOT(processDlFinished(QNetworkReply*)));
#ifndef QT_NO_OPENSSL
connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply*, QList<QSslError>)));
#endif
}
QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len) {
QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len)
{
if (len <= 4) {
qWarning("gUncompress: Input data is truncated");
return QByteArray();
@ -93,14 +96,16 @@ QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len) { @@ -93,14 +96,16 @@ QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len) {
}
result.append(out, CHUNK_SIZE - strm.avail_out);
} while (!strm.avail_out);
}
while (!strm.avail_out);
// clean up and return
inflateEnd(&strm);
return result;
}
void DownloadThread::processDlFinished(QNetworkReply* reply) {
void DownloadThread::processDlFinished(QNetworkReply *reply)
{
QString url = reply->url().toString();
qDebug("Download finished: %s", qPrintable(url));
// Check if the request was successful
@ -111,7 +116,8 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { @@ -111,7 +116,8 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) {
reply->deleteLater();
return;
}
// Check if the server ask us to redirect somewhere lese
// Check if the server ask us to redirect somewhere else
const QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (redirection.isValid()) {
// We should redirect
@ -121,6 +127,7 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { @@ -121,6 +127,7 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) {
newUrl = reply->url().resolved(newUrl);
const QString newUrlString = newUrl.toString();
qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(newUrlString));
// Redirect to magnet workaround
if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
qDebug("Magnet redirect detected.");
@ -129,16 +136,18 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { @@ -129,16 +136,18 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) {
reply->deleteLater();
return;
}
m_redirectMapping.insert(newUrlString, url);
// redirecting with first cookies
downloadUrl(newUrlString, m_networkManager.cookieJar()->cookiesForUrl(url));
reply->deleteLater();
return;
}
// Checking if it was redirected, restoring initial URL
if (m_redirectMapping.contains(url)) {
if (m_redirectMapping.contains(url))
url = m_redirectMapping.take(url);
}
// Success
QTemporaryFile *tmpfile = new QTemporaryFile;
if (tmpfile->open()) {
@ -158,16 +167,19 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) { @@ -158,16 +167,19 @@ void DownloadThread::processDlFinished(QNetworkReply* reply) {
delete tmpfile;
// Send finished signal
emit downloadFinished(url, filePath);
} else {
}
else {
delete tmpfile;
fsutils::forceRemove(filePath);
// Error when reading the request
emit downloadFailure(url, tr("I/O Error"));
}
} else {
}
else {
delete tmpfile;
emit downloadFailure(url, tr("I/O Error"));
}
// Clean up
reply->deleteLater();
}
@ -179,46 +191,56 @@ void DownloadThread::downloadTorrentUrl(const QString &url, const QList<QNetwork @@ -179,46 +191,56 @@ void DownloadThread::downloadTorrentUrl(const QString &url, const QList<QNetwork
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64)));
}
QNetworkReply* DownloadThread::downloadUrl(const QString &url, const QList<QNetworkCookie>& cookies) {
QNetworkReply *DownloadThread::downloadUrl(const QString &url, const QList<QNetworkCookie> &cookies)
{
// Update proxy settings
applyProxySettings();
// Set cookies
if (!cookies.empty()) {
qDebug("Setting %d cookies for url: %s", cookies.size(), qPrintable(url));
m_networkManager.cookieJar()->setCookiesFromUrl(cookies, url);
}
// Process download request
qDebug("url is %s", qPrintable(url));
const QUrl qurl = QUrl::fromEncoded(url.toUtf8());
QNetworkRequest request(qurl);
// Spoof Firefox 3.5 user agent to avoid
// Web server banning
request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5");
qDebug("Downloading %s...", request.url().toEncoded().data());
qDebug("%d cookies for this URL", m_networkManager.cookieJar()->cookiesForUrl(url).size());
for (int i = 0; i < m_networkManager.cookieJar()->cookiesForUrl(url).size(); ++i) {
qDebug("%s=%s", m_networkManager.cookieJar()->cookiesForUrl(url).at(i).name().data(), m_networkManager.cookieJar()->cookiesForUrl(url).at(i).value().data());
qDebug("Domain: %s, Path: %s", qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).domain()), qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).path()));
}
// accept gzip
request.setRawHeader("Accept-Encoding", "gzip");
return m_networkManager.get(request);
}
void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) {
void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal)
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) return;
if (bytesTotal > 0) {
// Total number of bytes is available
if (bytesTotal > 1048576*10) {
if (bytesTotal > 10485760) {
// More than 10MB, this is probably not a torrent file, aborting...
reply->abort();
reply->deleteLater();
} else {
}
else {
disconnect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64)));
}
} else {
if (bytesReceived > 1048576*10) {
}
else {
if (bytesReceived > 10485760) {
// More than 10MB, this is probably not a torrent file, aborting...
reply->abort();
reply->deleteLater();
@ -226,19 +248,22 @@ void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) @@ -226,19 +248,22 @@ void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal)
}
}
void DownloadThread::applyProxySettings() {
void DownloadThread::applyProxySettings()
{
QNetworkProxy proxy;
const Preferences* const pref = Preferences::instance();
if (pref->isProxyEnabled()) {
// Proxy enabled
proxy.setHostName(pref->getProxyIp());
proxy.setPort(pref->getProxyPort());
// Default proxy type is HTTP, we must change if it is SOCKS5
const int proxy_type = pref->getProxyType();
if (proxy_type == Proxy::SOCKS5 || proxy_type == Proxy::SOCKS5_PW) {
const int proxyType = pref->getProxyType();
if ((proxyType == Proxy::SOCKS5) || (proxyType == Proxy::SOCKS5_PW)) {
qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
proxy.setType(QNetworkProxy::Socks5Proxy);
} else {
}
else {
qDebug() << Q_FUNC_INFO << "using HTTP proxy";
proxy.setType(QNetworkProxy::HttpProxy);
}
@ -248,13 +273,16 @@ void DownloadThread::applyProxySettings() { @@ -248,13 +273,16 @@ void DownloadThread::applyProxySettings() {
proxy.setUser(pref->getProxyUsername());
proxy.setPassword(pref->getProxyPassword());
}
} else {
}
else {
proxy.setType(QNetworkProxy::NoProxy);
}
m_networkManager.setProxy(proxy);
}
QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status) {
QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status)
{
switch(status) {
case QNetworkReply::HostNotFoundError:
return tr("The remote host name was not found (invalid hostname)");
@ -304,7 +332,8 @@ QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status) { @@ -304,7 +332,8 @@ QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status) {
}
#ifndef QT_NO_OPENSSL
void DownloadThread::ignoreSslErrors(QNetworkReply* reply, const QList<QSslError> &errors) {
void DownloadThread::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
{
Q_UNUSED(errors)
// Ignore all SSL errors
reply->ignoreSslErrors();

5
src/core/downloadthread.h

@ -42,14 +42,14 @@ QT_BEGIN_NAMESPACE @@ -42,14 +42,14 @@ QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
QT_END_NAMESPACE
class DownloadThread : public QObject {
class DownloadThread : public QObject
{
Q_OBJECT
public:
DownloadThread(QObject *parent = 0);
QNetworkReply *downloadUrl(const QString &url, const QList<QNetworkCookie> &cookies = QList<QNetworkCookie>());
void downloadTorrentUrl(const QString &url, const QList<QNetworkCookie> &cookies = QList<QNetworkCookie>());
//void setProxy(QString IP, int port, QString username, QString password);
signals:
void downloadFinished(const QString &url, const QString &file_path);
@ -68,7 +68,6 @@ private: @@ -68,7 +68,6 @@ private:
QString errorCodeToString(QNetworkReply::NetworkError status);
void applyProxySettings();
private:
QNetworkAccessManager m_networkManager;
QHash<QString, QString> m_redirectMapping;

Loading…
Cancel
Save