diff --git a/include/tomahawk/infosystem.h b/include/tomahawk/infosystem.h index e0efec86b..1409dabf9 100644 --- a/include/tomahawk/infosystem.h +++ b/include/tomahawk/infosystem.h @@ -36,7 +36,7 @@ namespace InfoSystem { class InfoSystemCache; enum InfoType { - InfoTrackID, + InfoTrackID = 0, InfoTrackArtist, InfoTrackAlbum, InfoTrackGenre, diff --git a/src/infosystem/infosystemcache.cpp b/src/infosystem/infosystemcache.cpp index 8e4801540..3a86c5634 100644 --- a/src/infosystem/infosystemcache.cpp +++ b/src/infosystem/infosystemcache.cpp @@ -17,29 +17,128 @@ */ #include +#include +#include +#include #include "infosystemcache.h" -void -Tomahawk::InfoSystem::InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) + +namespace Tomahawk +{ + +namespace InfoSystem +{ + + +InfoSystemCache::InfoSystemCache( QObject* parent ) + : QObject(parent) { qDebug() << Q_FUNC_INFO; - if( !m_memCache.contains( type ) || !m_memCache[type].contains( criteria ) ) + QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation ); + for( int i = 0; i <= InfoNoInfo; i++ ) + { + InfoType type = (InfoType)(i); + if( m_dirtySet.contains( type ) && m_dataCache.contains( type ) ) + { + QString cacheDir = cacheBaseDir + QString::number( i ); + QDir dir( cacheDir ); + if( dir.exists() && QFile::exists( QString( cacheDir + '/' + QString::number( i ) ) ) ) + loadCache( type, QString( cacheDir + '/' + QString::number( i ) ) ); + } + } +} + + +InfoSystemCache::~InfoSystemCache() +{ + qDebug() << Q_FUNC_INFO; + qDebug() << "Saving infosystemcache to disk"; + QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation ); + for( int i = 0; i <= InfoNoInfo; i++ ) + { + InfoType type = (InfoType)(i); + if( m_dirtySet.contains( type ) && m_dataCache.contains( type ) ) + { + QString cacheDir = cacheBaseDir + QString::number( i ); + saveCache( type, cacheDir ); + } + } +} + + +void +InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) +{ + qDebug() << Q_FUNC_INFO; + if( !m_dataCache.contains( type ) || !m_dataCache[type].contains( criteria ) ) { emit notInCache( criteria, caller, type, input, customData ); return; } - emit info( caller, type, input, m_memCache[type][criteria], customData ); + emit info( caller, type, input, m_dataCache[type][criteria], customData ); } + void -Tomahawk::InfoSystem::InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ) +InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ) { qDebug() << Q_FUNC_INFO; - QHash< InfoCacheCriteria, QVariant > typecache; - if( m_memCache.contains( type ) ) - typecache = m_memCache[type]; - typecache[criteria] = output; - m_memCache[type] = typecache; + QHash< InfoCacheCriteria, QVariant > typedatacache; + QHash< InfoCacheCriteria, QDateTime > typetimecache; + typedatacache[criteria] = output; + typetimecache[criteria] = QDateTime::currentDateTimeUtc(); + m_dataCache[type] = typedatacache; + m_timeCache[type] = typetimecache; + m_dirtySet.insert( type ); } + + +void +InfoSystemCache::loadCache( InfoType type, const QString &cacheDir ) +{ + qDebug() << Q_FUNC_INFO; +} + + +void +InfoSystemCache::saveCache( InfoType type, const QString &cacheDir ) +{ + qDebug() << Q_FUNC_INFO; + QDir dir( cacheDir ); + if( !dir.exists( cacheDir ) ) + { + qDebug() << "Creating cache directory " << cacheDir; + if( !dir.mkpath( cacheDir ) ) + { + qDebug() << "Failed to create cache dir! Bailing..."; + return; + } + } + + QSettings cacheFile( QString( cacheDir + '/' + QString::number( (int)type ) ), QSettings::IniFormat ); + + foreach( InfoCacheCriteria criteria, m_dataCache[type].keys() ) + { + cacheFile.beginGroup( "type_" + QString::number( type ) ); + cacheFile.beginWriteArray( "criteria" ); + QStringList keys = criteria.keys(); + for( int i = 0; i < criteria.size(); i++ ) + { + cacheFile.setArrayIndex( i ); + cacheFile.setValue( keys.at( i ), criteria[keys.at( i )] ); + } + cacheFile.endArray(); + cacheFile.setValue( "data", m_dataCache[type][criteria] ); + cacheFile.setValue( "time", m_timeCache[type][criteria] ); + cacheFile.endGroup(); + } + + m_dirtySet.remove( type ); +} + + +} //namespace InfoSystem + +} //namespace Tomahawk \ No newline at end of file diff --git a/src/infosystem/infosystemcache.h b/src/infosystem/infosystemcache.h index ef31710c2..b74cf8a40 100644 --- a/src/infosystem/infosystemcache.h +++ b/src/infosystem/infosystemcache.h @@ -19,6 +19,7 @@ #ifndef TOMAHAWK_INFOSYSTEMCACHE_H #define TOMAHAWK_INFOSYSTEMCACHE_H +#include #include #include @@ -35,16 +36,9 @@ class InfoSystemCache : public QObject Q_OBJECT public: - InfoSystemCache( QObject *parent = 0 ) - : QObject( parent ) - { - qDebug() << Q_FUNC_INFO; - } + InfoSystemCache( QObject *parent = 0 ); - virtual ~InfoSystemCache() - { - qDebug() << Q_FUNC_INFO; - } + virtual ~InfoSystemCache(); signals: void notInCache( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); @@ -55,7 +49,12 @@ public slots: void updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ); private: - QHash< InfoType, QHash< InfoCacheCriteria, QVariant > > m_memCache; + void loadCache( InfoType type, const QString &cache ); + void saveCache( InfoType type, const QString &cache ); + + QHash< InfoType, QHash< InfoCacheCriteria, QVariant > > m_dataCache; + QHash< InfoType, QHash< InfoCacheCriteria, QDateTime > > m_timeCache; + QSet< InfoType > m_dirtySet; }; } //namespace InfoSystem diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 116b9c7a1..38d22a4b7 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -299,7 +299,7 @@ TomahawkApp::~TomahawkApp() delete m_mainwindow; delete m_audioEngine; #endif - + delete m_infoSystem; delete m_database; }