Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / TextureCache.cpp
index 251ff17..8b45d39 100644 (file)
@@ -27,6 +27,7 @@
 #include "settings/AdvancedSettings.h"
 #include "utils/log.h"
 #include "utils/URIUtils.h"
+#include "utils/StringUtils.h"
 #include "URL.h"
 #include "utils/StringUtils.h"
 
@@ -38,7 +39,7 @@ CTextureCache &CTextureCache::Get()
   return s_cache;
 }
 
-CTextureCache::CTextureCache()
+CTextureCache::CTextureCache() : CJobQueue(false, 1, CJob::PRIORITY_LOW_PAUSABLE)
 {
 }
 
@@ -65,6 +66,7 @@ bool CTextureCache::IsCachedImage(const CStdString &url) const
   if (url != "-" && !CURL::IsFullPath(url))
     return true;
   if (URIUtils::IsInPath(url, "special://skin/") ||
+      URIUtils::IsInPath(url, "special://temp/") ||
       URIUtils::IsInPath(url, "androidapp://")   ||
       URIUtils::IsInPath(url, CProfilesManager::Get().GetThumbnailsFolder()))
     return true;
@@ -75,12 +77,12 @@ bool CTextureCache::HasCachedImage(const CStdString &url)
 {
   CTextureDetails details;
   CStdString cachedImage(GetCachedImage(url, details));
-  return (!cachedImage.IsEmpty() && cachedImage != url);
+  return (!cachedImage.empty() && cachedImage != url);
 }
 
 CStdString CTextureCache::GetCachedImage(const CStdString &image, CTextureDetails &details, bool trackUsage)
 {
-  CStdString url = UnwrapImageURL(image);
+  CStdString url = CTextureUtils::UnwrapImageURL(image);
 
   if (IsCachedImage(url))
     return url;
@@ -95,39 +97,6 @@ CStdString CTextureCache::GetCachedImage(const CStdString &image, CTextureDetail
   return "";
 }
 
-CStdString CTextureCache::GetWrappedImageURL(const CStdString &image, const CStdString &type, const CStdString &options)
-{
-  if (StringUtils::StartsWith(image, "image://"))
-    return image; // already wrapped
-
-  CURL url;
-  url.SetProtocol("image");
-  url.SetUserName(type);
-  url.SetHostName(image);
-  if (!options.IsEmpty())
-  {
-    url.SetFileName("transform");
-    url.SetOptions("?" + options);
-  }
-  return url.Get();
-}
-
-CStdString CTextureCache::GetWrappedThumbURL(const CStdString &image)
-{
-  return GetWrappedImageURL(image, "", "size=thumb");
-}
-
-CStdString CTextureCache::UnwrapImageURL(const CStdString &image)
-{
-  if (StringUtils::StartsWith(image, "image://"))
-  {
-    CURL url(image);
-    if (url.GetUserName().IsEmpty() && url.GetOptions().IsEmpty())
-      return url.GetHostName();
-  }
-  return image;
-}
-
 bool CTextureCache::CanCacheImageURL(const CURL &url)
 {
   return (url.GetUserName().empty() || url.GetUserName() == "music");
@@ -138,7 +107,7 @@ CStdString CTextureCache::CheckCachedImage(const CStdString &url, bool returnDDS
   CTextureDetails details;
   CStdString path(GetCachedImage(url, details, true));
   needsRecaching = !details.hash.empty();
-  if (!path.IsEmpty())
+  if (!path.empty())
   {
     if (!needsRecaching && returnDDS && !URIUtils::IsInPath(url, "special://skin/")) // TODO: should skin images be .dds'd (currently they're not necessarily writeable)
     { // check for dds version
@@ -157,11 +126,11 @@ void CTextureCache::BackgroundCacheImage(const CStdString &url)
 {
   CTextureDetails details;
   CStdString path(GetCachedImage(url, details));
-  if (!path.IsEmpty() && details.hash.empty())
+  if (!path.empty() && details.hash.empty())
     return; // image is already cached and doesn't need to be checked further
 
   // needs (re)caching
-  AddJob(new CTextureCacheJob(UnwrapImageURL(url), details.hash));
+  AddJob(new CTextureCacheJob(CTextureUtils::UnwrapImageURL(url), details.hash));
 }
 
 bool CTextureCache::CacheImage(const CStdString &image, CTextureDetails &details)
@@ -175,11 +144,11 @@ bool CTextureCache::CacheImage(const CStdString &image, CTextureDetails &details
 
 CStdString CTextureCache::CacheImage(const CStdString &image, CBaseTexture **texture, CTextureDetails *details)
 {
-  CStdString url = UnwrapImageURL(image);
+  CStdString url = CTextureUtils::UnwrapImageURL(image);
   CSingleLock lock(m_processingSection);
-  if (m_processing.find(url) == m_processing.end())
+  if (m_processinglist.find(url) == m_processinglist.end())
   {
-    m_processing.insert(url);
+    m_processinglist.insert(url);
     lock.Leave();
     // cache the texture directly
     CTextureCacheJob job(url);
@@ -197,7 +166,7 @@ CStdString CTextureCache::CacheImage(const CStdString &image, CBaseTexture **tex
     m_completeEvent.WaitMSec(1000);
     {
       CSingleLock lock(m_processingSection);
-      if (m_processing.find(url) == m_processing.end())
+      if (m_processinglist.find(url) == m_processinglist.end())
         break;
     }
   }
@@ -221,6 +190,22 @@ void CTextureCache::ClearCachedImage(const CStdString &url, bool deleteSource /*
     CFile::Delete(path);
 }
 
+bool CTextureCache::ClearCachedImage(int id)
+{
+  CStdString cachedFile;
+  if (ClearCachedTexture(id, cachedFile))
+  {
+    cachedFile = GetCachedPath(cachedFile);
+    if (CFile::Exists(cachedFile))
+      CFile::Delete(cachedFile);
+    cachedFile = URIUtils::ReplaceExtension(cachedFile, ".dds");
+    if (CFile::Exists(cachedFile))
+      CFile::Delete(cachedFile);
+    return true;
+  }
+  return false;
+}
+
 bool CTextureCache::GetCachedTexture(const CStdString &url, CTextureDetails &details)
 {
   CSingleLock lock(m_databaseSection);
@@ -258,14 +243,18 @@ bool CTextureCache::ClearCachedTexture(const CStdString &url, CStdString &cached
   return m_database.ClearCachedTexture(url, cachedURL);
 }
 
+bool CTextureCache::ClearCachedTexture(int id, CStdString &cachedURL)
+{
+  CSingleLock lock(m_databaseSection);
+  return m_database.ClearCachedTexture(id, cachedURL);
+}
+
 CStdString CTextureCache::GetCacheFile(const CStdString &url)
 {
   Crc32 crc;
   crc.ComputeFromLowerCase(url);
-  CStdString hex;
-  hex.Format("%08x", (unsigned int)crc);
-  CStdString hash;
-  hash.Format("%c/%s", hex[0], hex.c_str());
+  CStdString hex = StringUtils::Format("%08x", (unsigned int)crc);
+  CStdString hash = StringUtils::Format("%c/%s", hex[0], hex.c_str());
   return hash;
 }
 
@@ -286,9 +275,9 @@ void CTextureCache::OnCachingComplete(bool success, CTextureCacheJob *job)
 
   { // remove from our processing list
     CSingleLock lock(m_processingSection);
-    std::set<CStdString>::iterator i = m_processing.find(job->m_url);
-    if (i != m_processing.end())
-      m_processing.erase(i);
+    std::set<CStdString>::iterator i = m_processinglist.find(job->m_url);
+    if (i != m_processinglist.end())
+      m_processinglist.erase(i);
   }
 
   m_completeEvent.Set();
@@ -312,10 +301,10 @@ void CTextureCache::OnJobProgress(unsigned int jobID, unsigned int progress, uns
     {
       CSingleLock lock(m_processingSection);
       const CTextureCacheJob *cacheJob = (CTextureCacheJob *)job;
-      std::set<CStdString>::iterator i = m_processing.find(cacheJob->m_url);
-      if (i == m_processing.end())
+      std::set<CStdString>::iterator i = m_processinglist.find(cacheJob->m_url);
+      if (i == m_processinglist.end())
       {
-        m_processing.insert(cacheJob->m_url);
+        m_processinglist.insert(cacheJob->m_url);
         return;
       }
     }
@@ -329,7 +318,7 @@ bool CTextureCache::Export(const CStdString &image, const CStdString &destinatio
 {
   CTextureDetails details;
   CStdString cachedImage(GetCachedImage(image, details));
-  if (!cachedImage.IsEmpty())
+  if (!cachedImage.empty())
   {
     CStdString dest = destination + URIUtils::GetExtension(cachedImage);
     if (overwrite || !CFile::Exists(dest))
@@ -346,7 +335,7 @@ bool CTextureCache::Export(const CStdString &image, const CStdString &destinatio
 {
   CTextureDetails details;
   CStdString cachedImage(GetCachedImage(image, details));
-  if (!cachedImage.IsEmpty())
+  if (!cachedImage.empty())
   {
     if (CFile::Cache(cachedImage, destination))
       return true;