From c910cac76c39f40c2f5fe07fc468152f2b7cac04 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Tue, 9 Sep 2014 16:07:39 +0200 Subject: [PATCH 1/4] 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(); From 10e944b7a72b4245239a007472d670e62f73c24e Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Wed, 10 Sep 2014 11:01:22 +0200 Subject: [PATCH 2/4] use existing TomahawkUtils function to remove directory --- .../infosystem/InfoSystemCache.cpp | 6 +--- src/libtomahawk/utils/TomahawkCache.cpp | 29 +------------------ src/libtomahawk/utils/TomahawkCache.h | 9 ------ 3 files changed, 2 insertions(+), 42 deletions(-) diff --git a/src/libtomahawk/infosystem/InfoSystemCache.cpp b/src/libtomahawk/infosystem/InfoSystemCache.cpp index 3bcbf3984..106b84946 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.cpp +++ b/src/libtomahawk/infosystem/InfoSystemCache.cpp @@ -25,10 +25,6 @@ #include "utils/Logger.h" #include "Source.h" -#if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) - #include "utils/TomahawkCache.h" -#endif - #include #include #include @@ -49,7 +45,7 @@ InfoSystemCache::InfoSystemCache( QObject* parent ) if ( TomahawkSettings::instance()->infoSystemCacheVersion() < INFOSYSTEM_CACHE_VERSION ) { #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) - TomahawkUtils::Cache::instance()->removeDirectoryRecursively( m_cacheBaseDir ); + TomahawkUtils::removeDirectory( m_cacheBaseDir ); #else QDir(m_cacheBaseDir)::removeRecursively(); #endif diff --git a/src/libtomahawk/utils/TomahawkCache.cpp b/src/libtomahawk/utils/TomahawkCache.cpp index fa404f156..b35f9df33 100644 --- a/src/libtomahawk/utils/TomahawkCache.cpp +++ b/src/libtomahawk/utils/TomahawkCache.cpp @@ -48,7 +48,7 @@ Cache::Cache() if ( TomahawkSettings::instance()->genericCacheVersion() < CACHE_VERSION ) { #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) - removeDirectoryRecursively( m_cacheBaseDir ); + TomahawkUtils::removeDirectory( m_cacheBaseDir ); #else QDir( m_cacheBaseDir )::removeRecursively(); #endif @@ -126,33 +126,6 @@ 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 ccb56759d..682318aed 100644 --- a/src/libtomahawk/utils/TomahawkCache.h +++ b/src/libtomahawk/utils/TomahawkCache.h @@ -81,15 +81,6 @@ 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(); From 2797c06a8cc21d5081668a7da517a670f478ae17 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Wed, 10 Sep 2014 11:17:38 +0200 Subject: [PATCH 3/4] typo in Qt5 syntax in Infosystem/TomahawkCache --- src/libtomahawk/infosystem/InfoSystemCache.cpp | 2 +- src/libtomahawk/utils/TomahawkCache.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libtomahawk/infosystem/InfoSystemCache.cpp b/src/libtomahawk/infosystem/InfoSystemCache.cpp index 106b84946..80873bb48 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.cpp +++ b/src/libtomahawk/infosystem/InfoSystemCache.cpp @@ -47,7 +47,7 @@ InfoSystemCache::InfoSystemCache( QObject* parent ) #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) TomahawkUtils::removeDirectory( m_cacheBaseDir ); #else - QDir(m_cacheBaseDir)::removeRecursively(); + QDir(m_cacheBaseDir).removeRecursively(); #endif TomahawkSettings::instance()->setInfoSystemCacheVersion( INFOSYSTEM_CACHE_VERSION ); } diff --git a/src/libtomahawk/utils/TomahawkCache.cpp b/src/libtomahawk/utils/TomahawkCache.cpp index b35f9df33..de252708f 100644 --- a/src/libtomahawk/utils/TomahawkCache.cpp +++ b/src/libtomahawk/utils/TomahawkCache.cpp @@ -50,7 +50,7 @@ Cache::Cache() #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) TomahawkUtils::removeDirectory( m_cacheBaseDir ); #else - QDir( m_cacheBaseDir )::removeRecursively(); + QDir( m_cacheBaseDir ).removeRecursively(); #endif TomahawkSettings::instance()->setGenericCacheVersion( CACHE_VERSION ); } From b4a290c8ec85f33cb8df867e0c2e6d81e56be11b Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Thu, 11 Sep 2014 10:48:53 +0200 Subject: [PATCH 4/4] change DEFINES to c++ constants, remove qt5 ifdefs inside Generic/InfosystemCache and let TomahawkUtils handle the version check --- src/libtomahawk/infosystem/InfoSystemCache.cpp | 11 ++++------- src/libtomahawk/infosystem/InfoSystemCache.h | 9 +++++++-- src/libtomahawk/utils/TomahawkCache.cpp | 11 ++++------- src/libtomahawk/utils/TomahawkCache.h | 9 +++++++-- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/libtomahawk/infosystem/InfoSystemCache.cpp b/src/libtomahawk/infosystem/InfoSystemCache.cpp index 80873bb48..159602d5d 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.cpp +++ b/src/libtomahawk/infosystem/InfoSystemCache.cpp @@ -35,6 +35,7 @@ namespace Tomahawk namespace InfoSystem { +const int InfoSystemCache::s_infosystemCacheVersion = 4; InfoSystemCache::InfoSystemCache( QObject* parent ) : QObject( parent ) @@ -42,14 +43,10 @@ InfoSystemCache::InfoSystemCache( QObject* parent ) { tDebug() << Q_FUNC_INFO; - if ( TomahawkSettings::instance()->infoSystemCacheVersion() < INFOSYSTEM_CACHE_VERSION ) + if ( TomahawkSettings::instance()->infoSystemCacheVersion() < s_infosystemCacheVersion ) { - #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) - TomahawkUtils::removeDirectory( m_cacheBaseDir ); - #else - QDir(m_cacheBaseDir).removeRecursively(); - #endif - TomahawkSettings::instance()->setInfoSystemCacheVersion( INFOSYSTEM_CACHE_VERSION ); + TomahawkUtils::removeDirectory( m_cacheBaseDir ); + TomahawkSettings::instance()->setInfoSystemCacheVersion( s_infosystemCacheVersion ); } m_pruneTimer.setInterval( 300000 ); diff --git a/src/libtomahawk/infosystem/InfoSystemCache.h b/src/libtomahawk/infosystem/InfoSystemCache.h index 7c27b1bc5..bc3689e99 100644 --- a/src/libtomahawk/infosystem/InfoSystemCache.h +++ b/src/libtomahawk/infosystem/InfoSystemCache.h @@ -34,8 +34,6 @@ namespace Tomahawk namespace InfoSystem { -#define INFOSYSTEM_CACHE_VERSION 4 - class DLLEXPORT InfoSystemCache : public QObject { Q_OBJECT @@ -56,6 +54,13 @@ private slots: void pruneTimerFired(); private: + /** + * Version number of the infosystem cache. + * If you change existing cached data, + * increase this number. + */ + static const int s_infosystemCacheVersion; + void notInCache( QObject *receiver, Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ); const QString criteriaMd5( const Tomahawk::InfoSystem::InfoStringHash &criteria, Tomahawk::InfoSystem::InfoType type = Tomahawk::InfoSystem::InfoNoInfo ) const; diff --git a/src/libtomahawk/utils/TomahawkCache.cpp b/src/libtomahawk/utils/TomahawkCache.cpp index de252708f..31bd581df 100644 --- a/src/libtomahawk/utils/TomahawkCache.cpp +++ b/src/libtomahawk/utils/TomahawkCache.cpp @@ -29,6 +29,7 @@ using namespace TomahawkUtils; Cache*Cache::s_instance = 0; +const int Cache::s_cacheVersion = 1; Cache* Cache::instance() @@ -45,14 +46,10 @@ Cache::Cache() , m_cacheBaseDir( TomahawkSettings::instance()->storageCacheLocation() + "/GenericCache/" ) , m_cacheManifest( m_cacheBaseDir + "cachemanifest.ini", QSettings::IniFormat ) { - if ( TomahawkSettings::instance()->genericCacheVersion() < CACHE_VERSION ) + if ( TomahawkSettings::instance()->genericCacheVersion() < s_cacheVersion ) { - #if QT_VERSION <= QT_VERSION_CHECK( 5, 0, 0 ) - TomahawkUtils::removeDirectory( m_cacheBaseDir ); - #else - QDir( m_cacheBaseDir ).removeRecursively(); - #endif - TomahawkSettings::instance()->setGenericCacheVersion( CACHE_VERSION ); + TomahawkUtils::removeDirectory( m_cacheBaseDir ); + TomahawkSettings::instance()->setGenericCacheVersion( s_cacheVersion ); } m_pruneTimer.setInterval( 300000 ); diff --git a/src/libtomahawk/utils/TomahawkCache.h b/src/libtomahawk/utils/TomahawkCache.h index 682318aed..b7c3245f5 100644 --- a/src/libtomahawk/utils/TomahawkCache.h +++ b/src/libtomahawk/utils/TomahawkCache.h @@ -31,8 +31,6 @@ namespace TomahawkUtils { -#define CACHE_VERSION 1 - /** * Internal data structure. Don't use. */ @@ -89,6 +87,13 @@ private: Cache(); static Cache* s_instance; + /** + * Version number of the cache. + * If you change existing cached data, + * increase this number. + */ + static const int s_cacheVersion; + /** * Adds a client to the manifest. * Does not lock the mutex.