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.
9 #include "PVRCachedImages.h"
11 #include "ServiceBroker.h"
12 #include "TextureCache.h"
13 #include "TextureDatabase.h"
15 #include "imagefiles/ImageFileURL.h"
16 #include "utils/StringUtils.h"
17 #include "utils/Variant.h"
18 #include "utils/log.h"
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
;
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
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");
90 return iCleanedImages
;