From 1e7c20add6dee471433507e69d5509c7ca5b7cbe Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 14 Nov 2012 05:45:57 +0100 Subject: [PATCH] * Added NetworkReply, a simple wrapper for QNetworkReply that handles HTTP redirects. --- src/libtomahawk/CMakeLists.txt | 1 + src/libtomahawk/utils/NetworkReply.cpp | 88 ++++++++++++++++++++++++++ src/libtomahawk/utils/NetworkReply.h | 56 ++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 src/libtomahawk/utils/NetworkReply.cpp create mode 100644 src/libtomahawk/utils/NetworkReply.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index fc5b8e9ab..be12e1df8 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -116,6 +116,7 @@ set( libGuiSources utils/BinaryExtractWorker.cpp utils/SharedTimeLine.cpp utils/WebResultHintChecker.cpp + utils/NetworkReply.cpp widgets/AnimatedCounterLabel.cpp widgets/BasicHeader.cpp diff --git a/src/libtomahawk/utils/NetworkReply.cpp b/src/libtomahawk/utils/NetworkReply.cpp new file mode 100644 index 000000000..4496a272d --- /dev/null +++ b/src/libtomahawk/utils/NetworkReply.cpp @@ -0,0 +1,88 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * Tomahawk 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. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "NetworkReply.h" + +#include "utils/TomahawkUtils.h" +#include "utils/Logger.h" + +using namespace Tomahawk; + + +NetworkReply::NetworkReply( QNetworkReply* parent ) + : QObject() + , m_reply( parent ) +{ + m_url = m_reply->url(); + + connect( m_reply, SIGNAL( finished() ), SLOT( networkLoadFinished() ) ); + connect( m_reply, SIGNAL( error( QNetworkReply::NetworkError ) ), SIGNAL( error( QNetworkReply::NetworkError ) ) ); + connect( m_reply, SIGNAL( destroyed( QObject* ) ), SLOT( deletedByParent() ) ); +} + + +NetworkReply::~NetworkReply() +{ + if ( m_reply ) + m_reply->deleteLater(); +} + + +void +NetworkReply::deletedByParent() +{ + if ( sender() == m_reply ) + { + m_reply = 0; + deleteLater(); + } +} + + +void +NetworkReply::load( const QUrl& url ) +{ + m_url = url; + QNetworkRequest request( url ); + + Q_ASSERT( TomahawkUtils::nam() != 0 ); + m_reply = TomahawkUtils::nam()->get( request ); + + connect( m_reply, SIGNAL( finished() ), SLOT( networkLoadFinished() ) ); + connect( m_reply, SIGNAL( error( QNetworkReply::NetworkError ) ), SIGNAL( error( QNetworkReply::NetworkError ) ) ); + connect( m_reply, SIGNAL( destroyed( QObject* ) ), SLOT( deletedByParent() ) ); +} + + +void +NetworkReply::networkLoadFinished() +{ + if ( m_reply->error() != QNetworkReply::NoError ) + return; + + QVariant redir = m_reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); + if ( redir.isValid() && !redir.toUrl().isEmpty() ) + { + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Redirected HTTP request to" << redir; + m_reply->deleteLater(); + load( redir.toUrl() ); + emit redirected(); + } + else + emit finished(); +} diff --git a/src/libtomahawk/utils/NetworkReply.h b/src/libtomahawk/utils/NetworkReply.h new file mode 100644 index 000000000..5e0a6226b --- /dev/null +++ b/src/libtomahawk/utils/NetworkReply.h @@ -0,0 +1,56 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * Tomahawk 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. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef NETWORKREPLY_H +#define NETWORKREPLY_H + +#include +#include + +#include "Typedefs.h" + +#include "DllMacro.h" + +class DLLEXPORT NetworkReply : public QObject +{ +Q_OBJECT + +public: + explicit NetworkReply( QNetworkReply* parent = 0 ); + virtual ~NetworkReply(); + + QNetworkReply* reply() const { return m_reply; } + +signals: + void redirected(); + + void finished(); + void error( QNetworkReply::NetworkError error ); + +private slots: + void deletedByParent(); + void networkLoadFinished(); + +private: + void load( const QUrl& url ); + + QNetworkReply* m_reply; + QUrl m_url; +}; + +#endif // NETWORKREPLY_H