forked from opdenkamp/xbmc
/
ThumbnailCache.cpp
276 lines (232 loc) · 7.89 KB
/
ThumbnailCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "ThumbnailCache.h"
#include "filesystem/File.h"
#include "threads/SingleLock.h"
#include "FileItem.h"
#include "video/VideoInfoTag.h"
#include "video/VideoDatabase.h"
#include "music/tags/MusicInfoTag.h"
#include "music/Album.h"
#include "music/Artist.h"
#include "settings/Settings.h"
#include "utils/URIUtils.h"
#include "utils/Crc32.h"
#include "filesystem/StackDirectory.h"
using namespace std;
using namespace XFILE;
using namespace MUSIC_INFO;
CThumbnailCache* CThumbnailCache::m_pCacheInstance = NULL;
CCriticalSection CThumbnailCache::m_cs;
CThumbnailCache::~CThumbnailCache()
{}
CThumbnailCache::CThumbnailCache()
{
}
CThumbnailCache* CThumbnailCache::GetThumbnailCache()
{
CSingleLock lock (m_cs);
if (m_pCacheInstance == NULL)
m_pCacheInstance = new CThumbnailCache;
return m_pCacheInstance;
}
bool CThumbnailCache::ThumbExists(const CStdString& strFileName, bool bAddCache /*=false*/)
{
CSingleLock lock (m_cs);
if (strFileName.size() == 0) return false;
map<CStdString, bool>::iterator it;
it = m_Cache.find(strFileName);
if (it != m_Cache.end())
return it->second;
bool bExists = CFile::Exists(strFileName);
if (bAddCache)
Add(strFileName, bExists);
return bExists;
}
bool CThumbnailCache::IsCached(const CStdString& strFileName)
{
CSingleLock lock (m_cs);
map<CStdString, bool>::iterator it;
it = m_Cache.find(strFileName);
return it != m_Cache.end();
}
void CThumbnailCache::Clear()
{
CSingleLock lock (m_cs);
if (m_pCacheInstance != NULL)
{
m_Cache.erase(m_Cache.begin(), m_Cache.end());
delete m_pCacheInstance;
}
m_pCacheInstance = NULL;
}
void CThumbnailCache::Add(const CStdString& strFileName, bool bExists)
{
CSingleLock lock (m_cs);
map<CStdString, bool>::iterator it;
it = m_Cache.find(strFileName);
if (it != m_Cache.end())
it->second = bExists;
else
m_Cache.insert(pair<CStdString, bool>(strFileName, bExists));
}
CStdString CThumbnailCache::GetAlbumThumb(const CFileItem &item)
{
return GetAlbumThumb(item.GetMusicInfoTag());
}
CStdString CThumbnailCache::GetAlbumThumb(const CMusicInfoTag *musicInfo)
{
if (!musicInfo)
return CStdString();
return GetAlbumThumb(musicInfo->GetAlbum(), !musicInfo->GetAlbumArtist().empty() ? musicInfo->GetAlbumArtist() : musicInfo->GetArtist());
}
CStdString CThumbnailCache::GetAlbumThumb(const CAlbum &album)
{
return GetAlbumThumb(album.strAlbum, album.strArtist);
}
CStdString CThumbnailCache::GetAlbumThumb(const CStdString& album, const CStdString& artist)
{
if (album.IsEmpty())
return GetMusicThumb("unknown" + artist);
if (artist.IsEmpty())
return GetMusicThumb(album + "unknown");
return GetMusicThumb(album + artist);
}
CStdString CThumbnailCache::GetArtistThumb(const CFileItem &item)
{
return GetArtistThumb(item.GetLabel());
}
CStdString CThumbnailCache::GetArtistThumb(const CArtist &artist)
{
return GetArtistThumb(artist.strArtist);
}
CStdString CThumbnailCache::GetArtistThumb(const CStdString &label)
{
return GetThumb("artist" + label, g_settings.GetMusicArtistThumbFolder());
}
CStdString CThumbnailCache::GetActorThumb(const CFileItem &item)
{
return GetActorThumb(item.GetLabel());
}
CStdString CThumbnailCache::GetActorThumb(const CStdString &label)
{
return GetThumb("actor" + label, g_settings.GetVideoThumbFolder(), true);
}
CStdString CThumbnailCache::GetSeasonThumb(const CFileItem &item)
{
return GetSeasonThumb(item.GetLabel(), item.GetVideoInfoTag());
}
CStdString CThumbnailCache::GetSeasonThumb(const CStdString &label, const CVideoInfoTag *videoInfo /* = NULL */)
{
CStdString seasonPath;
if (videoInfo)
seasonPath = videoInfo->m_strPath;
return GetThumb("season" + seasonPath + label, g_settings.GetVideoThumbFolder(), true);
}
CStdString CThumbnailCache::GetEpisodeThumb(const CFileItem &item)
{
if (!item.HasVideoInfoTag())
return CStdString();
return GetEpisodeThumb(item.GetVideoInfoTag());
}
CStdString CThumbnailCache::GetEpisodeThumb(const CVideoInfoTag* videoInfo)
{
// get the locally cached thumb
CStdString strCRC;
strCRC.Format("%sepisode%i", videoInfo->m_strFileNameAndPath.c_str(), videoInfo->m_iEpisode);
return GetThumb(strCRC, g_settings.GetVideoThumbFolder(), true);
}
CStdString CThumbnailCache::GetVideoThumb(const CFileItem &item)
{
if (item.IsStack())
return GetThumb(CStackDirectory::GetFirstStackedFile(item.GetPath()), g_settings.GetVideoThumbFolder(), true);
else if (item.IsVideoDb() && item.HasVideoInfoTag())
{
if (item.m_bIsFolder && !item.GetVideoInfoTag()->m_strPath.IsEmpty())
return GetThumb(item.GetVideoInfoTag()->m_strPath, g_settings.GetVideoThumbFolder(), true);
else if (!item.GetVideoInfoTag()->m_strFileNameAndPath.IsEmpty())
return GetThumb(item.GetVideoInfoTag()->m_strFileNameAndPath, g_settings.GetVideoThumbFolder(), true);
}
return GetThumb(item.GetPath(), g_settings.GetVideoThumbFolder(), true);
}
CStdString CThumbnailCache::GetFanart(const CFileItem &item)
{
// get the locally cached thumb
if (item.IsVideoDb() || (item.HasVideoInfoTag() && !item.GetVideoInfoTag()->IsEmpty()))
{
if (!item.HasVideoInfoTag())
return "";
if (!item.GetVideoInfoTag()->m_strArtist.IsEmpty())
return GetThumb(item.GetVideoInfoTag()->m_strArtist,g_settings.GetMusicFanartFolder());
if (!item.m_bIsFolder && !item.GetVideoInfoTag()->m_strShowTitle.IsEmpty())
{
CStdString showPath;
if (!item.GetVideoInfoTag()->m_strShowPath.IsEmpty())
showPath = item.GetVideoInfoTag()->m_strShowPath;
else
{
CVideoDatabase database;
database.Open();
int iShowId = item.GetVideoInfoTag()->m_iIdShow;
if (iShowId <= 0)
iShowId = database.GetTvShowId(item.GetVideoInfoTag()->m_strPath);
CStdString showPath;
database.GetFilePathById(iShowId,showPath,VIDEODB_CONTENT_TVSHOWS);
}
return GetThumb(showPath,g_settings.GetVideoFanartFolder());
}
CStdString path = item.GetVideoInfoTag()->GetPath();
if (path.empty())
return "";
return GetThumb(path,g_settings.GetVideoFanartFolder());
}
if (item.HasMusicInfoTag())
return GetThumb(item.GetMusicInfoTag()->GetArtist(),g_settings.GetMusicFanartFolder());
return GetThumb(item.GetPath(),g_settings.GetVideoFanartFolder());
}
CStdString CThumbnailCache::GetThumb(const CStdString &path, const CStdString &path2, bool split /* = false */)
{
// get the locally cached thumb
Crc32 crc;
crc.ComputeFromLowerCase(path);
CStdString thumb;
if (split)
{
CStdString hex;
hex.Format("%08x", (__int32)crc);
thumb.Format("%c\\%08x.tbn", hex[0], (unsigned __int32)crc);
}
else
thumb.Format("%08x.tbn", (unsigned __int32)crc);
return URIUtils::AddFileToFolder(path2, thumb);
}
CStdString CThumbnailCache::GetMusicThumb(const CStdString& path)
{
Crc32 crc;
CStdString noSlashPath(path);
URIUtils::RemoveSlashAtEnd(noSlashPath);
crc.ComputeFromLowerCase(noSlashPath);
CStdString hex;
hex.Format("%08x", (unsigned __int32) crc);
CStdString thumb;
thumb.Format("%c/%s.tbn", hex[0], hex.c_str());
return URIUtils::AddFileToFolder(g_settings.GetMusicThumbFolder(), thumb);
}