Browse Source

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

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

85
src/core/downloadthread.cpp

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

15
src/core/downloadthread.h

@ -42,14 +42,14 @@ QT_BEGIN_NAMESPACE
class QNetworkAccessManager; class QNetworkAccessManager;
QT_END_NAMESPACE QT_END_NAMESPACE
class DownloadThread : public QObject { class DownloadThread : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
DownloadThread(QObject* parent = 0); DownloadThread(QObject *parent = 0);
QNetworkReply* downloadUrl(const QString &url, const QList<QNetworkCookie>& cookies = QList<QNetworkCookie>()); QNetworkReply *downloadUrl(const QString &url, const QList<QNetworkCookie> &cookies = QList<QNetworkCookie>());
void downloadTorrentUrl(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: signals:
void downloadFinished(const QString &url, const QString &file_path); void downloadFinished(const QString &url, const QString &file_path);
@ -57,10 +57,10 @@ signals:
void magnetRedirect(const QString &url_new, const QString &url_old); void magnetRedirect(const QString &url_new, const QString &url_old);
private slots: private slots:
void processDlFinished(QNetworkReply* reply); void processDlFinished(QNetworkReply *reply);
void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal); void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal);
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void ignoreSslErrors(QNetworkReply*,const QList<QSslError>&); void ignoreSslErrors(QNetworkReply *,const QList<QSslError> &);
#endif #endif
private: private:
@ -68,7 +68,6 @@ private:
QString errorCodeToString(QNetworkReply::NetworkError status); QString errorCodeToString(QNetworkReply::NetworkError status);
void applyProxySettings(); void applyProxySettings();
private:
QNetworkAccessManager m_networkManager; QNetworkAccessManager m_networkManager;
QHash<QString, QString> m_redirectMapping; QHash<QString, QString> m_redirectMapping;

Loading…
Cancel
Save