From c910cac76c39f40c2f5fe07fc468152f2b7cac04 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Tue, 9 Sep 2014 16:07:39 +0200 Subject: [PATCH] add genericcache version setting to TomahawkSettings, check for the cache versions in TomahawkCache/InfossystemCache and delete cache if it is old --- src/libtomahawk/TomahawkSettings.cpp | 13 ++++++- src/libtomahawk/TomahawkSettings.h | 3 ++ .../infosystem/InfoSystemCache.cpp | 14 +++++++ src/libtomahawk/infosystem/InfoSystemCache.h | 2 + src/libtomahawk/utils/TomahawkCache.cpp | 37 +++++++++++++++++++ src/libtomahawk/utils/TomahawkCache.h | 12 ++++++ 6 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/libtomahawk/TomahawkSettings.cpp b/src/libtomahawk/TomahawkSettings.cpp index 28867067e..05f848c84 100644 --- a/src/libtomahawk/TomahawkSettings.cpp +++ b/src/libtomahawk/TomahawkSettings.cpp @@ -701,9 +701,20 @@ TomahawkSettings::setInfoSystemCacheVersion( uint version ) uint TomahawkSettings::infoSystemCacheVersion() const { - return value( "infosystemcacheversion", 3 ).toUInt(); + return value( "infosystemcacheversion", 0 ).toUInt(); } +void +TomahawkSettings::setGenericCacheVersion( uint version ) +{ + setValue( "genericcacheversion", version ); +} + +uint +TomahawkSettings::genericCacheVersion() const +{ + return value( "genericcacheversion", 0 ).toUInt(); +} QString TomahawkSettings::storageCacheLocation() const diff --git a/src/libtomahawk/TomahawkSettings.h b/src/libtomahawk/TomahawkSettings.h index d4e52d59b..491455557 100644 --- a/src/libtomahawk/TomahawkSettings.h +++ b/src/libtomahawk/TomahawkSettings.h @@ -60,6 +60,9 @@ public: uint infoSystemCacheVersion() const; void setInfoSystemCacheVersion( uint version ); + uint genericCacheVersion() const; + void setGenericCacheVersion( uint version ); + bool watchForChanges() const; void setWatchForChanges( bool watch ); diff --git a/src/libtomahawk/infosystem/InfoSystemCache.cpp b/src/libtomahawk/infosystem/InfoSystemCache.cpp index 3749ae7ec..3bcbf3984 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.cpp +++ b/src/libtomahawk/infosystem/InfoSystemCache.cpp @@ -25,6 +25,10 @@ #include "utils/Logger.h" #include "Source.h" +#if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) + #include "utils/TomahawkCache.h" +#endif + #include #include #include @@ -42,6 +46,16 @@ InfoSystemCache::InfoSystemCache( QObject* parent ) { tDebug() << Q_FUNC_INFO; + if ( TomahawkSettings::instance()->infoSystemCacheVersion() < INFOSYSTEM_CACHE_VERSION ) + { + #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) + TomahawkUtils::Cache::instance()->removeDirectoryRecursively( m_cacheBaseDir ); + #else + QDir(m_cacheBaseDir)::removeRecursively(); + #endif + TomahawkSettings::instance()->setInfoSystemCacheVersion( INFOSYSTEM_CACHE_VERSION ); + } + m_pruneTimer.setInterval( 300000 ); m_pruneTimer.setSingleShot( false ); connect( &m_pruneTimer, SIGNAL( timeout() ), SLOT( pruneTimerFired() ) ); diff --git a/src/libtomahawk/infosystem/InfoSystemCache.h b/src/libtomahawk/infosystem/InfoSystemCache.h index d21e32865..7c27b1bc5 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.h +++ b/src/libtomahawk/infosystem/InfoSystemCache.h @@ -34,6 +34,8 @@ namespace Tomahawk namespace InfoSystem { +#define INFOSYSTEM_CACHE_VERSION 4 + class DLLEXPORT InfoSystemCache : public QObject { Q_OBJECT diff --git a/src/libtomahawk/utils/TomahawkCache.cpp b/src/libtomahawk/utils/TomahawkCache.cpp index d0c679121..fa404f156 100644 --- a/src/libtomahawk/utils/TomahawkCache.cpp +++ b/src/libtomahawk/utils/TomahawkCache.cpp @@ -45,6 +45,16 @@ Cache::Cache() , m_cacheBaseDir( TomahawkSettings::instance()->storageCacheLocation() + "/GenericCache/" ) , m_cacheManifest( m_cacheBaseDir + "cachemanifest.ini", QSettings::IniFormat ) { + if ( TomahawkSettings::instance()->genericCacheVersion() < CACHE_VERSION ) + { + #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) + removeDirectoryRecursively( m_cacheBaseDir ); + #else + QDir( m_cacheBaseDir )::removeRecursively(); + #endif + TomahawkSettings::instance()->setGenericCacheVersion( CACHE_VERSION ); + } + m_pruneTimer.setInterval( 300000 ); m_pruneTimer.setSingleShot( false ); connect( &m_pruneTimer, SIGNAL( timeout() ), SLOT( pruneTimerFired() ) ); @@ -116,6 +126,33 @@ Cache::getData( const QString& identifier, const QString& key ) } +#if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) +bool +Cache::removeDirectoryRecursively(const QString& dirName) +{ + bool result = true; + + QDir dir(dirName); + if (dir.exists(dirName)) { + foreach( QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst) ) { + if (info.isDir()) { + result = removeDirectoryRecursively(info.absoluteFilePath()); + } + else { + result = QFile::remove(info.absoluteFilePath()); + } + + if (!result) { + return result; + } + } + result = dir.rmdir(dirName); + } + return result; +} +#endif + + void Cache::putData( const QString& identifier, qint64 maxAge, const QString& key, const QVariant& value ) { diff --git a/src/libtomahawk/utils/TomahawkCache.h b/src/libtomahawk/utils/TomahawkCache.h index 5cc3ece17..ccb56759d 100644 --- a/src/libtomahawk/utils/TomahawkCache.h +++ b/src/libtomahawk/utils/TomahawkCache.h @@ -26,10 +26,13 @@ #include #include #include +#include namespace TomahawkUtils { +#define CACHE_VERSION 1 + /** * Internal data structure. Don't use. */ @@ -78,6 +81,15 @@ public: * @return the data, if found, if not found an invalid QVariant is returned. */ QVariant getData( const QString& identifier, const QString& key ); +#if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) + /** + * Recursively deletes all files & directories in a folder + * Only needed in Qt4 + * @param dirName the directory which should be deleted + * @return if the removal was successfull or not + */ + bool removeDirectoryRecursively( const QString& dirName ); +#endif private slots: void pruneTimerFired();