Removed redundant network reply class.

This commit is contained in:
Martin Rotter 2015-06-19 07:29:39 +02:00
parent faad5f9464
commit 6f324664af
5 changed files with 17 additions and 143 deletions

View file

@ -429,7 +429,6 @@ set(APP_SOURCES
src/network-web/adblock/adblocksearchtree.cpp src/network-web/adblock/adblocksearchtree.cpp
src/network-web/adblock/adblocksubscription.cpp src/network-web/adblock/adblocksubscription.cpp
src/network-web/adblock/adblocktreewidget.cpp src/network-web/adblock/adblocktreewidget.cpp
src/network-web/adblock/followredirectreply.cpp
# MAIN sources. # MAIN sources.
src/main.cpp src/main.cpp
@ -526,7 +525,6 @@ set(APP_HEADERS
src/network-web/adblock/adblockmatcher.h src/network-web/adblock/adblockmatcher.h
src/network-web/adblock/adblocksubscription.h src/network-web/adblock/adblocksubscription.h
src/network-web/adblock/adblocktreewidget.h src/network-web/adblock/adblocktreewidget.h
src/network-web/adblock/followredirectreply.h
) )
# APP form files. # APP form files.

View file

@ -48,8 +48,7 @@
#include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblocksearchtree.h" #include "network-web/adblock/adblocksearchtree.h"
#include "network-web/adblock/followredirectreply.h" #include "network-web/downloader.h"
#include "network-web/silentnetworkaccessmanager.h"
#include "miscellaneous/iofactory.h" #include "miscellaneous/iofactory.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include "exceptions/applicationexception.h" #include "exceptions/applicationexception.h"
@ -62,7 +61,7 @@
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject *parent) 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 { QString AdBlockSubscription::title() const {
@ -143,32 +142,35 @@ void AdBlockSubscription::saveSubscription() {
} }
void AdBlockSubscription::updateSubscription() { void AdBlockSubscription::updateSubscription() {
if (m_reply != NULL || !m_url.isValid()) { if (m_downloadingSubscription || !m_url.isValid()) {
return; return;
} }
// TODO: Refaktorovat. m_downloadingSubscription = true;
m_reply = new FollowRedirectReply(m_url, SilentNetworkAccessManager::instance());
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() { void AdBlockSubscription::subscriptionDownloaded() {
if (m_reply != qobject_cast<FollowRedirectReply*>(sender())) { Downloader *downloader = qobject_cast<Downloader*>(sender());
if (downloader == NULL) {
return; return;
} }
bool error = false; 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")) || !response.startsWith(QByteArray("[Adblock")) ||
!saveDownloadedData(response)) { !saveDownloadedData(response)) {
error = true; error = true;
} }
m_reply->deleteLater(); downloader->deleteLater();
m_reply = 0;
if (error) { if (error) {
emit subscriptionError(tr("Cannot load subscription!")); emit subscriptionError(tr("Cannot load subscription!"));
@ -179,6 +181,8 @@ void AdBlockSubscription::subscriptionDownloaded() {
emit subscriptionUpdated(); emit subscriptionUpdated();
emit subscriptionChanged(); emit subscriptionChanged();
} }
m_downloadingSubscription = false;
} }
bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) { bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) {

View file

@ -112,7 +112,7 @@ class AdBlockSubscription : public QObject {
QString m_filePath; QString m_filePath;
private: private:
FollowRedirectReply *m_reply; bool m_downloadingSubscription;
QUrl m_url; QUrl m_url;
bool m_updated; bool m_updated;
}; };

View file

@ -1,71 +0,0 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
#include "network-web/adblock/followredirectreply.h"
#include <QNetworkAccessManager>
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();
}

View file

@ -1,57 +0,0 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
#ifndef FOLLOWREDIRECTREPLY_H
#define FOLLOWREDIRECTREPLY_H
#include <QObject>
#include <QNetworkReply>
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