Merge pull request #25614 from garbear/show-version
[xbmc.git] / xbmc / pvr / PVRCachedImages.cpp
blob28969f9ccd9fbd8647825ccdc3c825abfd6faccd
1 /*
2 * Copyright (C) 2005-2021 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "PVRCachedImages.h"
11 #include "ServiceBroker.h"
12 #include "TextureCache.h"
13 #include "TextureDatabase.h"
14 #include "URL.h"
15 #include "imagefiles/ImageFileURL.h"
16 #include "utils/StringUtils.h"
17 #include "utils/Variant.h"
18 #include "utils/log.h"
20 #include <algorithm>
22 using namespace PVR;
24 int CPVRCachedImages::Cleanup(const std::vector<PVRImagePattern>& urlPatterns,
25 const std::vector<std::string>& urlsToCheck,
26 bool clearTextureForPath /* = false */)
28 int iCleanedImages = 0;
30 if (urlPatterns.empty())
32 CLog::LogFC(LOGERROR, LOGPVR, "No URL patterns given");
33 return iCleanedImages;
36 CTextureDatabase db;
37 if (!db.Open())
39 CLog::LogFC(LOGERROR, LOGPVR, "Failed to open texture database");
40 return iCleanedImages;
43 CDatabase::Filter filter;
45 for (const auto& pattern : urlPatterns)
47 const std::string encodedPattern =
48 StringUtils::Format("{}@{}", pattern.owner, CURL::Encode(pattern.path));
50 std::string escapedPattern;
51 for (size_t i = 0; i < encodedPattern.size(); ++i)
53 if (encodedPattern[i] == '%' || encodedPattern[i] == '^')
54 escapedPattern += '^';
56 escapedPattern += encodedPattern[i];
59 const std::string where =
60 StringUtils::Format("url LIKE 'image://{}%' ESCAPE '^'", escapedPattern);
61 filter.AppendWhere(where, false); // logical OR
64 CVariant items;
65 if (!db.GetTextures(items, filter))
67 CLog::LogFC(LOGERROR, LOGPVR, "Failed to get items from texture database");
68 return iCleanedImages;
71 for (unsigned int i = 0; i < items.size(); ++i)
73 // Unwrap the image:// URL returned from texture db.
74 const std::string textureURL =
75 IMAGE_FILES::CImageFileURL(items[i]["url"].asString()).GetTargetFile();
77 if (std::none_of(urlsToCheck.cbegin(), urlsToCheck.cend(),
78 [&textureURL](const std::string& url) { return url == textureURL; }))
80 CLog::LogFC(LOGDEBUG, LOGPVR, "Removing stale cached image: '{}'", textureURL);
81 CServiceBroker::GetTextureCache()->ClearCachedImage(items[i]["textureid"].asInteger());
83 if (clearTextureForPath)
84 db.ClearTextureForPath(textureURL, "thumb");
86 iCleanedImages++;
90 return iCleanedImages;