From 6f324664afcd25ec4097473f4c1bea17b94514b2 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 19 Jun 2015 07:29:39 +0200 Subject: [PATCH] Removed redundant network reply class. --- CMakeLists.txt | 2 - .../adblock/adblocksubscription.cpp | 28 ++++---- src/network-web/adblock/adblocksubscription.h | 2 +- .../adblock/followredirectreply.cpp | 71 ------------------- src/network-web/adblock/followredirectreply.h | 57 --------------- 5 files changed, 17 insertions(+), 143 deletions(-) delete mode 100755 src/network-web/adblock/followredirectreply.cpp delete mode 100755 src/network-web/adblock/followredirectreply.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8eb0aa6e5..c9ec1399b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -429,7 +429,6 @@ set(APP_SOURCES src/network-web/adblock/adblocksearchtree.cpp src/network-web/adblock/adblocksubscription.cpp src/network-web/adblock/adblocktreewidget.cpp - src/network-web/adblock/followredirectreply.cpp # MAIN sources. src/main.cpp @@ -526,7 +525,6 @@ set(APP_HEADERS src/network-web/adblock/adblockmatcher.h src/network-web/adblock/adblocksubscription.h src/network-web/adblock/adblocktreewidget.h - src/network-web/adblock/followredirectreply.h ) # APP form files. diff --git a/src/network-web/adblock/adblocksubscription.cpp b/src/network-web/adblock/adblocksubscription.cpp index 93e046205..045d4a089 100755 --- a/src/network-web/adblock/adblocksubscription.cpp +++ b/src/network-web/adblock/adblocksubscription.cpp @@ -48,8 +48,7 @@ #include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblocksearchtree.h" -#include "network-web/adblock/followredirectreply.h" -#include "network-web/silentnetworkaccessmanager.h" +#include "network-web/downloader.h" #include "miscellaneous/iofactory.h" #include "miscellaneous/application.h" #include "exceptions/applicationexception.h" @@ -62,7 +61,7 @@ AdBlockSubscription::AdBlockSubscription(const QString &title, QObject *parent) - : QObject(parent), m_reply(NULL), m_title(title), m_updated(false) { + : QObject(parent), m_title(title), m_downloadingSubscription(false), m_updated(false) { } QString AdBlockSubscription::title() const { @@ -143,32 +142,35 @@ void AdBlockSubscription::saveSubscription() { } void AdBlockSubscription::updateSubscription() { - if (m_reply != NULL || !m_url.isValid()) { + if (m_downloadingSubscription || !m_url.isValid()) { return; } - // TODO: Refaktorovat. - m_reply = new FollowRedirectReply(m_url, SilentNetworkAccessManager::instance()); + m_downloadingSubscription = true; - connect(m_reply, SIGNAL(finished()), this, SLOT(subscriptionDownloaded())); + Downloader *downloader = new Downloader(); + + connect(downloader, SIGNAL(completed(QNetworkReply::NetworkError,QByteArray)), this, SLOT(subscriptionDownloaded())); + downloader->downloadFile(m_url.toString()); } void AdBlockSubscription::subscriptionDownloaded() { - if (m_reply != qobject_cast(sender())) { + Downloader *downloader = qobject_cast(sender()); + + if (downloader == NULL) { return; } bool error = false; - const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); + const QByteArray response = QString::fromUtf8(downloader->lastOutputData()).toUtf8(); - if (m_reply->error() != QNetworkReply::NoError || + if (downloader->lastOutputError() != QNetworkReply::NoError || !response.startsWith(QByteArray("[Adblock")) || !saveDownloadedData(response)) { error = true; } - m_reply->deleteLater(); - m_reply = 0; + downloader->deleteLater(); if (error) { emit subscriptionError(tr("Cannot load subscription!")); @@ -179,6 +181,8 @@ void AdBlockSubscription::subscriptionDownloaded() { emit subscriptionUpdated(); emit subscriptionChanged(); } + + m_downloadingSubscription = false; } bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) { diff --git a/src/network-web/adblock/adblocksubscription.h b/src/network-web/adblock/adblocksubscription.h index f861621e7..531f5926b 100755 --- a/src/network-web/adblock/adblocksubscription.h +++ b/src/network-web/adblock/adblocksubscription.h @@ -112,7 +112,7 @@ class AdBlockSubscription : public QObject { QString m_filePath; private: - FollowRedirectReply *m_reply; + bool m_downloadingSubscription; QUrl m_url; bool m_updated; }; diff --git a/src/network-web/adblock/followredirectreply.cpp b/src/network-web/adblock/followredirectreply.cpp deleted file mode 100755 index c46a8e352..000000000 --- a/src/network-web/adblock/followredirectreply.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// This file is part of RSS Guard. -// -// Copyright (C) 2014-2015 by Martin Rotter -// Copyright (C) 2010-2014 by David Rosca -// -// RSS Guard is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// RSS Guard is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with RSS Guard. If not, see . - -#include "network-web/adblock/followredirectreply.h" - -#include - - -FollowRedirectReply::FollowRedirectReply(const QUrl &url, QNetworkAccessManager *manager) - : QObject(), m_manager(manager), m_redirectCount(0) { - m_reply = m_manager->get(QNetworkRequest(url)); - connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished())); -} - -QNetworkReply *FollowRedirectReply::reply() const { - return m_reply; -} - -QUrl FollowRedirectReply::originalUrl() const { - return m_reply->request().url(); -} - -QUrl FollowRedirectReply::url() const { - return m_reply->url(); -} - -QNetworkReply::NetworkError FollowRedirectReply::error() const { - return m_reply->error(); -} - -QByteArray FollowRedirectReply::readAll() { - return m_reply->readAll(); -} - -void FollowRedirectReply::replyFinished() { - int reply_status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if ((reply_status != 301 && reply_status != 302) || m_redirectCount == 5) { - emit finished(); - return; - } - - m_redirectCount++; - - QUrl redirect_url = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); - m_reply->close(); - m_reply->deleteLater(); - - m_reply = m_manager->get(QNetworkRequest(redirect_url)); - connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished())); -} - -FollowRedirectReply::~FollowRedirectReply() { - m_reply->close(); - m_reply->deleteLater(); -} diff --git a/src/network-web/adblock/followredirectreply.h b/src/network-web/adblock/followredirectreply.h deleted file mode 100755 index f48c91799..000000000 --- a/src/network-web/adblock/followredirectreply.h +++ /dev/null @@ -1,57 +0,0 @@ -// This file is part of RSS Guard. -// -// Copyright (C) 2014-2015 by Martin Rotter -// Copyright (C) 2010-2014 by David Rosca -// -// RSS Guard is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// RSS Guard is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with RSS Guard. If not, see . - -#ifndef FOLLOWREDIRECTREPLY_H -#define FOLLOWREDIRECTREPLY_H - -#include - -#include - - -class QNetworkAccessManager; -class QNetworkReply; -class QUrl; - -class FollowRedirectReply : public QObject { - Q_OBJECT - - public: - explicit FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager); - virtual ~FollowRedirectReply(); - - QNetworkReply *reply() const; - QUrl originalUrl() const; - QUrl url() const; - - QNetworkReply::NetworkError error() const; - QByteArray readAll(); - - signals: - void finished(); - - private slots: - void replyFinished(); - - private: - QNetworkAccessManager *m_manager; - QNetworkReply *m_reply; - int m_redirectCount; -}; - -#endif // FOLLOWREDIRECTREPLY_H