1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-31 09:32:03 +02:00

Move IOFactories out of Servent

This commit is contained in:
Uwe L. Korn
2014-03-13 02:15:13 +00:00
parent f773653c3f
commit 596794da57
11 changed files with 253 additions and 106 deletions

View File

@@ -32,6 +32,7 @@
#include "Pipeline.h"
#include "Result.h"
#include "Source.h"
#include "UrlHandler.h"
#include <boost/bind.hpp>
@@ -243,7 +244,7 @@ Api_v1::sid( QxtWebRequestEvent* event, QString unused )
boost::function< void ( QSharedPointer< QIODevice >& ) > callback =
boost::bind( &Api_v1::processSid, this, event, rp, _1 );
Servent::instance()->getIODeviceForUrl( rp, callback );
Tomahawk::UrlHandler::getIODeviceForUrl( rp, callback );
}

View File

@@ -209,6 +209,7 @@ list(APPEND libSources
TrackData.cpp
SourcePlaylistInterface.cpp
PlaylistInterface.cpp
UrlHandler.cpp
EchonestCatalogSynchronizer.cpp

View File

@@ -0,0 +1,130 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013-2014, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "UrlHandler_p.h"
#include "utils/NetworkAccessManager.h"
#include "Result.h"
#include <QFile>
#include <boost/bind.hpp>
Q_DECLARE_METATYPE( IODeviceCallback )
namespace Tomahawk {
namespace UrlHandler {
QMap< QString, IODeviceFactoryFunc > iofactories;
void
initialiseDefaultIOFactories()
{
{
// _1 = result, _2 = callback function for IODevice
IODeviceFactoryFunc fac = boost::bind( localFileIODeviceFactory, _1, _2 );
iofactories.insert( "file", fac );
}
{
IODeviceFactoryFunc fac = boost::bind( httpIODeviceFactory, _1, _2 );
iofactories.insert( "http", fac );
iofactories.insert( "https", fac );
}
}
void
registerIODeviceFactory( const QString &proto, IODeviceFactoryFunc fac )
{
if ( iofactories.isEmpty() )
{
initialiseDefaultIOFactories();
}
iofactories.insert( proto, fac );
}
void
getIODeviceForUrl( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
if ( iofactories.isEmpty() )
{
initialiseDefaultIOFactories();
}
QSharedPointer<QIODevice> sp;
QRegExp rx( "^([a-zA-Z0-9]+)://(.+)$" );
if ( rx.indexIn( result->url() ) == -1 )
{
callback( sp );
return;
}
const QString proto = rx.cap( 1 );
if ( !iofactories.contains( proto ) )
{
callback( sp );
return;
}
// JSResolverHelper::customIODeviceFactory is async!
iofactories.value( proto )( result, callback );
}
void
localFileIODeviceFactory( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
// ignore "file://" at front of url
QFile* io = new QFile( result->url().mid( QString( "file://" ).length() ) );
if ( io )
io->open( QIODevice::ReadOnly );
//boost::functions cannot accept temporaries as parameters
QSharedPointer< QIODevice > sp = QSharedPointer<QIODevice>( io );
callback( sp );
}
void
httpIODeviceFactory( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
QNetworkRequest req( result->url() );
// Follow HTTP Redirects
NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( req ) );
qRegisterMetaType<NetworkReply*>("NetworkReply*");
qRegisterMetaType<IODeviceCallback>("IODeviceCallback");
HttpIODeviceReadyHandler* handler = new HttpIODeviceReadyHandler( reply, callback );
reply->connect( reply, SIGNAL( finalUrlReached() ),
handler, SLOT( called() ));
}
} // namespace UrlHandler
} // namespace Tomahawk

View File

@@ -0,0 +1,50 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013-2014, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef TOMAHAWK_URLHANDLER_H
#define TOMAHAWK_URLHANDLER_H
#include "DllMacro.h"
#include "Typedefs.h"
#include <boost/function.hpp>
typedef boost::function< void( const Tomahawk::result_ptr&,
boost::function< void( QSharedPointer< QIODevice >& ) > )> IODeviceFactoryFunc;
typedef boost::function< void ( QSharedPointer< QIODevice >& ) > IODeviceCallback;
namespace Tomahawk
{
namespace UrlHandler
{
DLLEXPORT void getIODeviceForUrl( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
DLLEXPORT void registerIODeviceFactory( const QString &proto, IODeviceFactoryFunc fac );
DLLEXPORT void localFileIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
DLLEXPORT void httpIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
} // namespace UrlHandler
} // namespace Tomahawk
#endif // TOMAHAWK_URLHANDLER_H

View File

@@ -0,0 +1,56 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef URLHANDLER_P_H
#define URLHANDLER_P_H
#include "UrlHandler.h"
#include "utils/NetworkReply.h"
class HttpIODeviceReadyHandler : public QObject
{
Q_OBJECT
public:
NetworkReply* reply;
IODeviceCallback callback;
QWeakPointer<HttpIODeviceReadyHandler> ref;
HttpIODeviceReadyHandler( NetworkReply* _reply, IODeviceCallback _callback )
: reply( _reply )
, callback( _callback )
{
// Do Nothing
}
public slots:
void called()
{
QSharedPointer< QIODevice > sp = QSharedPointer< QIODevice >( reply->reply(), &QObject::deleteLater );
callback( sp );
// Call once, then self-destruct
deleteLater();
}
};
#endif // URLHANDLER_P_H

View File

@@ -37,6 +37,7 @@
#include "Pipeline.h"
#include "PlaylistEntry.h"
#include "TomahawkSettings.h"
#include "UrlHandler.h"
#include <QDir>
@@ -632,7 +633,7 @@ AudioEngine::loadTrack( const Tomahawk::result_ptr& result )
{
boost::function< void ( QSharedPointer< QIODevice >& ) > callback =
boost::bind( &AudioEngine::performLoadTrack, this, result, _1 );
Servent::instance()->getIODeviceForUrl( d->currentTrack, callback );
Tomahawk::UrlHandler::getIODeviceForUrl( d->currentTrack, callback );
}
else
{

View File

@@ -3,7 +3,7 @@
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
* Copyright 2013-2014, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -44,6 +44,7 @@
#include "Source.h"
#include "SourceList.h"
#include "StreamConnection.h"
#include "UrlHandler.h"
#include <QCoreApplication>
#include <QMutexLocker>
@@ -63,7 +64,7 @@ Q_DECLARE_METATYPE( QList< SipInfo > )
Q_DECLARE_METATYPE( Connection* )
Q_DECLARE_METATYPE( QTcpSocketExtra* )
Q_DECLARE_METATYPE( Tomahawk::peerinfo_ptr )
Q_DECLARE_METATYPE( IODeviceCallback )
using namespace Tomahawk;
@@ -87,22 +88,8 @@ Servent::Servent( QObject* parent )
setProxy( QNetworkProxy::NoProxy );
{
// _1 = result, _2 = callback function for IODevice
IODeviceFactoryFunc fac = boost::bind( &Servent::localFileIODeviceFactory, this, _1, _2 );
this->registerIODeviceFactory( "file", fac );
}
{
IODeviceFactoryFunc fac = boost::bind( &Servent::remoteIODeviceFactory, this, _1, _2 );
this->registerIODeviceFactory( "servent", fac );
}
{
IODeviceFactoryFunc fac = boost::bind( &Servent::httpIODeviceFactory, this, _1, _2 );
this->registerIODeviceFactory( "http", fac );
this->registerIODeviceFactory( "https", fac );
}
IODeviceFactoryFunc fac = boost::bind( &Servent::remoteIODeviceFactory, this, _1, _2 );
Tomahawk::UrlHandler::registerIODeviceFactory( "servent", fac );
}
@@ -1381,78 +1368,6 @@ Servent::triggerDBSync()
}
void
Servent::registerIODeviceFactory( const QString &proto,
IODeviceFactoryFunc fac )
{
d_func()->iofactories.insert( proto, fac );
}
void
Servent::getIODeviceForUrl( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
QSharedPointer<QIODevice> sp;
QRegExp rx( "^([a-zA-Z0-9]+)://(.+)$" );
if ( rx.indexIn( result->url() ) == -1 )
{
callback( sp );
return;
}
const QString proto = rx.cap( 1 );
if ( !d_func()->iofactories.contains( proto ) )
{
callback( sp );
return;
}
//JSResolverHelper::customIODeviceFactory is async!
d_func()->iofactories.value( proto )( result, callback );
}
void
Servent::localFileIODeviceFactory( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
// ignore "file://" at front of url
QFile* io = new QFile( result->url().mid( QString( "file://" ).length() ) );
if ( io )
io->open( QIODevice::ReadOnly );
//boost::functions cannot accept temporaries as parameters
QSharedPointer< QIODevice > sp = QSharedPointer<QIODevice>( io );
callback( sp );
}
void
Servent::httpIODeviceFactory( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback )
{
QNetworkRequest req( result->url() );
// Follow HTTP Redirects
NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( req ) );
qRegisterMetaType<NetworkReply*>("NetworkReply*");
qRegisterMetaType<IODeviceCallback>("IODeviceCallback");
NewClosure( reply, SIGNAL( finalUrlReached() ),
this, SLOT( httpIODeviceReady( NetworkReply*, IODeviceCallback ) ),
reply, callback )->setAutoDelete( true );
}
void
Servent::httpIODeviceReady( NetworkReply* reply, IODeviceCallback callback )
{
//boost::functions cannot accept temporaries as parameters
QSharedPointer< QIODevice > sp = QSharedPointer< QIODevice >( reply->reply(), &QObject::deleteLater );
callback( sp );
}
bool
Servent::isReady() const
{

View File

@@ -50,10 +50,6 @@ namespace boost
template <class T> class function;
} // boost
typedef boost::function< void( const Tomahawk::result_ptr&,
boost::function< void( QSharedPointer< QIODevice >& ) > )> IODeviceFactoryFunc;
typedef boost::function< void ( QSharedPointer< QIODevice >& ) > IODeviceCallback;
class ServentPrivate;
class DLLEXPORT Servent : public QTcpServer
@@ -81,6 +77,9 @@ public:
ControlConnection* lookupControlConnection( const SipInfo& sipInfo );
ControlConnection* lookupControlConnection( const QString& nodeid );
void remoteIODeviceFactory( const Tomahawk::result_ptr& result,
boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
// you may call this method as often as you like for the same peerInfo, dupe checking is done inside
void registerPeer( const Tomahawk::peerinfo_ptr& peerInfo );
void handleSipInfo( const Tomahawk::peerinfo_ptr& peerInfo );
@@ -124,12 +123,6 @@ public:
QList< StreamConnection* > streams() const;
void getIODeviceForUrl( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
void registerIODeviceFactory( const QString &proto, IODeviceFactoryFunc fac );
void remoteIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
void localFileIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
void httpIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback );
bool isReady() const;
QList<SipInfo> getLocalSipInfos(const QString& nodeid, const QString &key);
@@ -168,7 +161,6 @@ public slots:
void triggerDBSync();
void onSipInfoChanged();
void httpIODeviceReady( NetworkReply* reply, IODeviceCallback callback );
private slots:
void deleteLazyOffer( const QString& key );

View File

@@ -48,7 +48,6 @@ public:
Q_DECLARE_PUBLIC ( Servent )
private:
QMap< QString, IODeviceFactoryFunc > iofactories;
QMap< QString, QPointer< Connection > > offers;
QMap< QString, QPair< Tomahawk::peerinfo_ptr, QString > > lazyoffers;
QStringList connectedNodes;

View File

@@ -31,6 +31,7 @@
#include "MsgProcessor.h"
#include "Result.h"
#include "SourceList.h"
#include "UrlHandler.h"
#include <boost/bind.hpp>
#include <boost/function.hpp>
@@ -189,7 +190,7 @@ StreamConnection::startSending( const Tomahawk::result_ptr& result )
boost::function< void ( QSharedPointer< QIODevice >& ) > callback =
boost::bind( &StreamConnection::reallyStartSending, this, result, _1 );
Servent::instance()->getIODeviceForUrl( m_result, callback );
Tomahawk::UrlHandler::getIODeviceForUrl( m_result, callback );
}

View File

@@ -36,6 +36,7 @@
#include "Pipeline.h"
#include "Result.h"
#include "SourceList.h"
#include "UrlHandler.h"
#include <boost/bind.hpp>
#include <QFile>
@@ -455,7 +456,7 @@ JSResolverHelper::addCustomUrlHandler( const QString& protocol,
boost::function< void( const Tomahawk::result_ptr&,
boost::function< void( QSharedPointer< QIODevice >& ) > )> fac =
boost::bind( &JSResolverHelper::customIODeviceFactory, this, _1, _2 );
Servent::instance()->registerIODeviceFactory( protocol, fac );
Tomahawk::UrlHandler::registerIODeviceFactory( protocol, fac );
m_urlCallback = callbackFuncName;
}