From c62c4fa1d936df79f0190624f924a75fa9eca47f Mon Sep 17 00:00:00 2001
From: Jeff Mitchell <tomahawk@jefferai.org>
Date: Tue, 11 Oct 2011 12:55:57 -0400
Subject: [PATCH] Fix cache misses going to incorrect target

---
 .../infoplugins/generic/chartsplugin.cpp       |  2 +-
 src/libtomahawk/infosystem/infosystemcache.cpp | 18 +++++++++++++-----
 src/libtomahawk/infosystem/infosystemcache.h   |  2 +-
 .../infosystem/infosystemworker.cpp            |  6 ------
 src/libtomahawk/infosystem/infosystemworker.h  |  3 +--
 5 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/libtomahawk/infosystem/infoplugins/generic/chartsplugin.cpp b/src/libtomahawk/infosystem/infoplugins/generic/chartsplugin.cpp
index d8af8a77e..a3da31e81 100644
--- a/src/libtomahawk/infosystem/infoplugins/generic/chartsplugin.cpp
+++ b/src/libtomahawk/infosystem/infoplugins/generic/chartsplugin.cpp
@@ -319,7 +319,7 @@ ChartsPlugin::notInCacheSlot( uint requestId, QHash<QString, QString> criteria,
 
         default:
         {
-            tLog() << "Couldn't figure out what to do with this type of request after cache miss";
+            tLog() << Q_FUNC_INFO << "Couldn't figure out what to do with this type of request after cache miss";
             emit info( requestId, requestData, QVariant() );
             return;
         }
diff --git a/src/libtomahawk/infosystem/infosystemcache.cpp b/src/libtomahawk/infosystem/infosystemcache.cpp
index 576751e04..300ad7b78 100644
--- a/src/libtomahawk/infosystem/infosystemcache.cpp
+++ b/src/libtomahawk/infosystem/infosystemcache.cpp
@@ -123,6 +123,7 @@ InfoSystemCache::pruneTimerFired()
 void
 InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCriteriaHash criteria, qint64 newMaxAge, Tomahawk::InfoSystem::InfoRequestData requestData )
 {
+    QObject* sendingObj = sender();
     const QString criteriaHashVal = criteriaMd5( criteria );
     const QString criteriaHashValWithType = criteriaMd5( criteria, requestData.type );
     QHash< QString, QString > fileLocationHash = m_fileLocationCache[ requestData.type ];
@@ -132,7 +133,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
         {
             //We already know of some values, so no need to re-read the directory again as it's already happened
             qDebug() << Q_FUNC_INFO << "notInCache -- filelocationhash empty";
-            emit notInCache( requestId, criteria, requestData );
+            notInCache( sendingObj, requestId, criteria, requestData );
             return;
         }
 
@@ -142,7 +143,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
         {
             //Dir doesn't exist so clearly not in cache
             qDebug() << Q_FUNC_INFO << "notInCache -- dir doesn't exist";
-            emit notInCache( requestId, criteria, requestData );
+            notInCache( sendingObj, requestId, criteria, requestData );
             return;
         }
 
@@ -159,7 +160,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
         {
             //Still didn't find it? It's really not in the cache then
             qDebug() << Q_FUNC_INFO << "notInCache -- filelocationhash doesn't contain criteria val";
-            emit notInCache( requestId, criteria, requestData );
+            notInCache( sendingObj, requestId, criteria, requestData );
             return;
         }
     }
@@ -179,7 +180,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
         m_dataCache.remove( criteriaHashValWithType );
 
         qDebug() << Q_FUNC_INFO << "notInCache -- file was stale";
-        emit notInCache( requestId, criteria, requestData );
+        notInCache( sendingObj, requestId, criteria, requestData );
         return;
     }
     else if ( newMaxAge > 0 )
@@ -189,7 +190,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
         if ( !QFile::rename( file.canonicalFilePath(), newFilePath ) )
         {
             qDebug() << Q_FUNC_INFO << "notInCache -- failed to move old cache file to new location";
-            emit notInCache( requestId, criteria, requestData );
+            notInCache( sendingObj, requestId, criteria, requestData );
             return;
         }
 
@@ -212,6 +213,13 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
 }
 
 
+void
+InfoSystemCache::notInCache( QObject *receiver, uint requestId, Tomahawk::InfoSystem::InfoCriteriaHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
+{
+    QMetaObject::invokeMethod( receiver, "notInCacheSlot", Q_ARG( uint, requestId ), Q_ARG( Tomahawk::InfoSystem::InfoCriteriaHash, criteria ), Q_ARG( Tomahawk::InfoSystem::InfoRequestData, requestData ) );
+}
+
+
 void
 InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output )
 {
diff --git a/src/libtomahawk/infosystem/infosystemcache.h b/src/libtomahawk/infosystem/infosystemcache.h
index 35d3d29bb..40e1d2c03 100644
--- a/src/libtomahawk/infosystem/infosystemcache.h
+++ b/src/libtomahawk/infosystem/infosystemcache.h
@@ -43,7 +43,6 @@ public:
     virtual ~InfoSystemCache();
 
 signals:
-    void notInCache( uint requestId, Tomahawk::InfoSystem::InfoCriteriaHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData );
     void info( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
 
 public slots:
@@ -54,6 +53,7 @@ private slots:
     void pruneTimerFired();
 
 private:
+    void notInCache( QObject *receiver, uint requestId, Tomahawk::InfoSystem::InfoCriteriaHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData );
     void doUpgrade( uint oldVersion, uint newVersion );
     const QString criteriaMd5( const Tomahawk::InfoSystem::InfoCriteriaHash &criteria, Tomahawk::InfoSystem::InfoType type = Tomahawk::InfoSystem::InfoNoInfo ) const;
     
diff --git a/src/libtomahawk/infosystem/infosystemworker.cpp b/src/libtomahawk/infosystem/infosystemworker.cpp
index 872d4f9b9..2743bbf74 100644
--- a/src/libtomahawk/infosystem/infosystemworker.cpp
+++ b/src/libtomahawk/infosystem/infosystemworker.cpp
@@ -126,12 +126,6 @@ InfoSystemWorker::init( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache> cac
                 cache.data(),
                 SLOT( getCachedInfoSlot( uint, Tomahawk::InfoSystem::InfoCriteriaHash, qint64, Tomahawk::InfoSystem::InfoRequestData ) )
             );
-        connect(
-                cache.data(),
-                SIGNAL( notInCache( uint, Tomahawk::InfoSystem::InfoCriteriaHash, Tomahawk::InfoSystem::InfoRequestData ) ),
-                plugin.data(),
-                SLOT( notInCacheSlot( uint, Tomahawk::InfoSystem::InfoCriteriaHash, Tomahawk::InfoSystem::InfoRequestData ) )
-            );
         connect(
                 plugin.data(),
                 SIGNAL( updateCache( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, Tomahawk::InfoSystem::InfoType, QVariant ) ),
diff --git a/src/libtomahawk/infosystem/infosystemworker.h b/src/libtomahawk/infosystem/infosystemworker.h
index 419c74b2e..347861dd9 100644
--- a/src/libtomahawk/infosystem/infosystemworker.h
+++ b/src/libtomahawk/infosystem/infosystemworker.h
@@ -70,14 +70,13 @@ private slots:
 private:
 
     void checkFinished( const QString &target );
+    QList< InfoPluginPtr > determineOrderedMatches( const InfoType type ) const;
     
     QHash< QString, QHash< InfoType, int > > m_dataTracker;
     QMultiMap< qint64, uint > m_timeRequestMapper;
     QHash< uint, bool > m_requestSatisfiedMap;
     QHash< uint, InfoRequestData* > m_savedRequestMap;
     
-    QList< InfoPluginPtr > determineOrderedMatches( const InfoType type ) const;
-    
     // For now, statically instantiate plugins; this is just somewhere to keep them
     QList< InfoPluginPtr > m_plugins;