diff --git a/src/libtomahawk/infosystem/infosystemcache.cpp b/src/libtomahawk/infosystem/infosystemcache.cpp index d35091607..01f4e7b45 100644 --- a/src/libtomahawk/infosystem/infosystemcache.cpp +++ b/src/libtomahawk/infosystem/infosystemcache.cpp @@ -32,7 +32,8 @@ namespace InfoSystem InfoSystemCache::InfoSystemCache( QObject* parent ) - : QObject(parent) + : QObject( parent ) + , m_syncTimer( this ) , m_cacheRemainingToLoad( 0 ) { qDebug() << Q_FUNC_INFO; @@ -49,6 +50,9 @@ InfoSystemCache::InfoSystemCache( QObject* parent ) QMetaObject::invokeMethod( this, "loadCache", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QString, cacheFile ) ); } } + m_syncTimer.setInterval( 60000 ); + m_syncTimer.setSingleShot( false ); + connect( &m_syncTimer, SIGNAL( timeout() ), SLOT( syncTimerFired() ) ); } @@ -69,6 +73,27 @@ InfoSystemCache::~InfoSystemCache() } +void +InfoSystemCache::syncTimerFired() +{ + qDebug() << Q_FUNC_INFO; + if ( m_cacheRemainingToLoad > 0 ) + return; + + qDebug() << "Syncing 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 + "/InfoSystemCache/" + QString::number( i ); + saveCache( type, cacheDir ); + } + } +} + + void InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteria, qint64 newMaxAge, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) { diff --git a/src/libtomahawk/infosystem/infosystemcache.h b/src/libtomahawk/infosystem/infosystemcache.h index da0111a84..1c8012c35 100644 --- a/src/libtomahawk/infosystem/infosystemcache.h +++ b/src/libtomahawk/infosystem/infosystemcache.h @@ -22,6 +22,7 @@ #include <QDateTime> #include <QObject> #include <QtDebug> +#include <QTimer> #include "infosystem.h" @@ -49,6 +50,7 @@ public slots: void updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output ); private slots: + void syncTimerFired(); void loadCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheFile ); void saveCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheDir ); @@ -57,6 +59,7 @@ private: QHash< InfoType, QHash< InfoCriteriaHash, QDateTime > > m_insertTimeCache; QHash< InfoType, QHash< InfoCriteriaHash, QDateTime > > m_maxTimeCache; QSet< InfoType > m_dirtySet; + QTimer m_syncTimer; int m_cacheRemainingToLoad; };