Removed redundant network reply class.
This commit is contained in:
parent
faad5f9464
commit
6f324664af
5 changed files with 17 additions and 143 deletions
|
@ -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.
|
||||
|
|
|
@ -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<FollowRedirectReply*>(sender())) {
|
||||
Downloader *downloader = qobject_cast<Downloader*>(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) {
|
||||
|
|
|
@ -112,7 +112,7 @@ class AdBlockSubscription : public QObject {
|
|||
QString m_filePath;
|
||||
|
||||
private:
|
||||
FollowRedirectReply *m_reply;
|
||||
bool m_downloadingSubscription;
|
||||
QUrl m_url;
|
||||
bool m_updated;
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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
|
Loading…
Add table
Reference in a new issue