From 1cd969af7f51d43d5243efc55c1dad0b9d58dcd8 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sun, 27 Mar 2011 22:37:36 -0400 Subject: [PATCH] Initial baby steps towards the info system cache --- include/tomahawk/infosystem.h | 75 +++++++++++++++--------------- src/CMakeLists.txt | 3 ++ src/infosystem/infosystem.cpp | 56 +++++++++++++++++++++- src/infosystem/infosystemcache.cpp | 0 src/infosystem/infosystemcache.h | 53 +++++++++++++++++++++ 5 files changed, 149 insertions(+), 38 deletions(-) create mode 100644 src/infosystem/infosystemcache.cpp create mode 100644 src/infosystem/infosystemcache.h diff --git a/include/tomahawk/infosystem.h b/include/tomahawk/infosystem.h index 7bc098a97..877345409 100644 --- a/include/tomahawk/infosystem.h +++ b/include/tomahawk/infosystem.h @@ -21,17 +21,19 @@ #include #include -#include -#include -#include -#include -#include - +#include +#include +#include +#include +#include +#include namespace Tomahawk { namespace InfoSystem { +class InfoSystemCache; + enum InfoType { InfoTrackID, InfoTrackArtist, @@ -91,30 +93,30 @@ enum InfoType { typedef QMap< InfoType, QVariant > InfoMap; typedef QMap< QString, QMap< QString, QString > > InfoGenericMap; -typedef QHash InfoCustomDataHash; -typedef QHash MusixMatchHash; +typedef QHash< QString, QVariant > InfoCustomDataHash; +typedef QHash< QString, QString > MusixMatchHash; class InfoPlugin : public QObject { Q_OBJECT public: - InfoPlugin(QObject *parent) - :QObject(parent) - { - qDebug() << Q_FUNC_INFO; - } + InfoPlugin( QObject *parent ); - ~InfoPlugin() + virtual ~InfoPlugin() { qDebug() << Q_FUNC_INFO; } - virtual void getInfo(const QString &caller, const InfoType type, const QVariant &data, Tomahawk::InfoSystem::InfoCustomDataHash customData) = 0; + virtual void getInfo( const QString &caller, const InfoType type, const QVariant &data, Tomahawk::InfoSystem::InfoCustomDataHash customData ) = 0; signals: - void info(QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData); - void finished(QString, Tomahawk::InfoSystem::InfoType); + void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData ); + void getCachedInfo( QHash< QString, QString > criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData ); + void finished( QString, Tomahawk::InfoSystem::InfoType ); + +//public slots: + //void notInCacheSlot( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData ) = 0; protected: InfoType m_type; @@ -127,46 +129,45 @@ class InfoSystem : public QObject Q_OBJECT public: + + InfoSystem( QObject *parent ); + ~InfoSystem(); + void registerInfoTypes( const InfoPluginPtr &plugin, const QSet< InfoType > &types ); - InfoSystem(QObject *parent); - ~InfoSystem() - { - qDebug() << Q_FUNC_INFO; - } + void getInfo( const QString &caller, const InfoType type, const QVariant &data, InfoCustomDataHash customData ); + void getInfo( const QString &caller, const InfoMap &input, InfoCustomDataHash customData ); - void registerInfoTypes(const InfoPluginPtr &plugin, const QSet< InfoType > &types); - - void getInfo(const QString &caller, const InfoType type, const QVariant &data, InfoCustomDataHash customData); - void getInfo(const QString &caller, const InfoMap &input, InfoCustomDataHash customData); + InfoSystemCache* getCache() { return m_cache; } signals: - void info(QString caller, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData); - void finished(QString target); + void info( QString caller, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData ); + void finished( QString target ); public slots: - void infoSlot(QString target, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData); - void finishedSlot(QString target,Tomahawk::InfoSystem::InfoType type); + void infoSlot( QString target, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData ); + void finishedSlot( QString target,Tomahawk::InfoSystem::InfoType type); private: - - QLinkedList< InfoPluginPtr > determineOrderedMatches(const InfoType type) const; + QLinkedList< InfoPluginPtr > determineOrderedMatches( const InfoType type ) const; - QMap< InfoType, QLinkedList > m_infoMap; + QMap< InfoType, QLinkedList< InfoPluginPtr > > m_infoMap; // For now, statically instantiate plugins; this is just somewhere to keep them - QLinkedList m_plugins; + QLinkedList< InfoPluginPtr > m_plugins; QHash< QString, QHash< Tomahawk::InfoSystem::InfoType, int > > m_dataTracker; + InfoSystemCache* m_cache; + QThread* m_infoSystemCacheThreadController; }; } } -Q_DECLARE_METATYPE(Tomahawk::InfoSystem::InfoGenericMap) -Q_DECLARE_METATYPE(Tomahawk::InfoSystem::InfoCustomDataHash); -Q_DECLARE_METATYPE(Tomahawk::InfoSystem::MusixMatchHash) +Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoGenericMap ) +Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCustomDataHash ); +Q_DECLARE_METATYPE( Tomahawk::InfoSystem::MusixMatchHash ) #endif // TOMAHAWK_INFOSYSTEM_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 271c84211..259756b05 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,7 @@ ENDIF() SET( tomahawkSources ${tomahawkSources} sip/SipHandler.cpp + infosystem/infosystemcache.cpp infosystem/infosystem.cpp infosystem/infoplugins/echonestplugin.cpp infosystem/infoplugins/lastfmplugin.cpp @@ -77,6 +78,7 @@ SET( tomahawkHeaders ${tomahawkHeaders} sip/SipHandler.h + infosystem/infosystemcache.h infosystem/infoplugins/echonestplugin.h infosystem/infoplugins/lastfmplugin.h infosystem/infoplugins/musixmatchplugin.h @@ -136,6 +138,7 @@ INCLUDE_DIRECTORIES( utils libtomahawk libtomahawk/utils + infosystem mac ${THIRDPARTY_DIR}/alsa-playback diff --git a/src/infosystem/infosystem.cpp b/src/infosystem/infosystem.cpp index 893c78162..dd8862582 100644 --- a/src/infosystem/infosystem.cpp +++ b/src/infosystem/infosystem.cpp @@ -16,20 +16,44 @@ * along with Tomahawk. If not, see . */ +#include + #include "tomahawk/infosystem.h" +#include "tomahawkutils.h" +#include "infosystemcache.h" #include "infoplugins/echonestplugin.h" #include "infoplugins/musixmatchplugin.h" #include "infoplugins/lastfmplugin.h" using namespace Tomahawk::InfoSystem; +InfoPlugin::InfoPlugin(QObject *parent) + :QObject( parent ) + { + qDebug() << Q_FUNC_INFO; + InfoSystem *system = qobject_cast< InfoSystem* >( parent ); + if( system ) + QObject::connect( system->getCache(), + SIGNAL( notInCache( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomDataHash ) ), + this, + SLOT( notInCacheSlot( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomDataHash ) ) + ); + } + + InfoSystem::InfoSystem(QObject *parent) - : QObject( parent ) + : QObject(parent) { qDebug() << Q_FUNC_INFO; qRegisterMetaType > >("Tomahawk::InfoSystem::InfoGenericMap"); qRegisterMetaType >("Tomahawk::InfoSystem::InfoCustomDataHash"); qRegisterMetaType >("Tomahawk::InfoSystem::MusixMatchHash"); + + m_infoSystemCacheThreadController = new QThread( this ); + m_cache = new Tomahawk::InfoSystem::InfoSystemCache(); + m_cache->moveToThread( m_infoSystemCacheThreadController ); + m_infoSystemCacheThreadController->start( QThread::IdlePriority ); + InfoPluginPtr enptr(new EchoNestPlugin(this)); m_plugins.append(enptr); InfoPluginPtr mmptr(new MusixMatchPlugin(this)); @@ -38,6 +62,36 @@ InfoSystem::InfoSystem(QObject *parent) m_plugins.append(lfmptr); } +InfoSystem::~InfoSystem() +{ + qDebug() << Q_FUNC_INFO; + Q_FOREACH( InfoPluginPtr plugin, m_plugins ) + { + if( plugin ) + delete plugin.data(); + } + + if( m_infoSystemCacheThreadController ) + { + m_infoSystemCacheThreadController->quit(); + + while( !m_infoSystemCacheThreadController->isFinished() ) + { + QCoreApplication::processEvents( QEventLoop::AllEvents, 200 ); + TomahawkUtils::Sleep::msleep( 100 ); + } + + if( m_cache ) + { + delete m_cache; + m_cache = 0; + } + + delete m_infoSystemCacheThreadController; + m_infoSystemCacheThreadController = 0; + } +} + void InfoSystem::registerInfoTypes(const InfoPluginPtr &plugin, const QSet< InfoType >& types) { qDebug() << Q_FUNC_INFO; diff --git a/src/infosystem/infosystemcache.cpp b/src/infosystem/infosystemcache.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/src/infosystem/infosystemcache.h b/src/infosystem/infosystemcache.h new file mode 100644 index 000000000..97990b7a1 --- /dev/null +++ b/src/infosystem/infosystemcache.h @@ -0,0 +1,53 @@ +/* === 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 TOMAHAWK_INFOSYSTEMCACHE_H +#define TOMAHAWK_INFOSYSTEMCACHE_H + +#include +#include + +namespace Tomahawk +{ + +namespace InfoSystem +{ + +class InfoSystemCache : public QObject +{ +Q_OBJECT + +public: + InfoSystemCache( QObject *parent = 0 ) + : QObject( parent ) + { + qDebug() << Q_FUNC_INFO; + } + + virtual ~InfoSystemCache() + { + qDebug() << Q_FUNC_INFO; + } + +}; + +} //namespace InfoSystem + +} //namespace Tomahawk + +#endif //TOMAHAWK_INFOSYSTEMCACHE_H \ No newline at end of file