Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / xbmc / pictures / PictureThumbLoader.cpp
blobbdbc020347b92314ce373ef312a0c47d30cc9f36
1 /*
2 * Copyright (C) 2005-2018 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 "PictureThumbLoader.h"
11 #include "FileItem.h"
12 #include "FileItemList.h"
13 #include "Picture.h"
14 #include "ServiceBroker.h"
15 #include "TextureCache.h"
16 #include "URL.h"
17 #include "filesystem/Directory.h"
18 #include "filesystem/MultiPathDirectory.h"
19 #include "guilib/GUIComponent.h"
20 #include "guilib/GUIWindowManager.h"
21 #include "imagefiles/ImageFileURL.h"
22 #include "playlists/PlayListFileItemClassify.h"
23 #include "settings/AdvancedSettings.h"
24 #include "settings/Settings.h"
25 #include "settings/SettingsComponent.h"
26 #include "utils/ArtUtils.h"
27 #include "utils/FileExtensionProvider.h"
28 #include "utils/FileUtils.h"
29 #include "utils/URIUtils.h"
30 #include "video/VideoFileItemClassify.h"
31 #include "video/VideoThumbLoader.h"
33 using namespace KODI;
34 using namespace XFILE;
36 CPictureThumbLoader::CPictureThumbLoader() : CThumbLoader()
38 m_regenerateThumbs = false;
41 CPictureThumbLoader::~CPictureThumbLoader()
43 StopThread();
46 void CPictureThumbLoader::OnLoaderFinish()
48 if (m_regenerateThumbs)
50 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_REFRESH_THUMBS);
51 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
53 m_regenerateThumbs = false;
54 CThumbLoader::OnLoaderFinish();
57 bool CPictureThumbLoader::LoadItem(CFileItem* pItem)
59 bool result = LoadItemCached(pItem);
60 result |= LoadItemLookup(pItem);
62 return result;
65 bool CPictureThumbLoader::LoadItemCached(CFileItem* pItem)
67 if (pItem->m_bIsShareOrDrive
68 || pItem->IsParentFolder())
69 return false;
71 if (pItem->HasArt("thumb") && m_regenerateThumbs)
73 CServiceBroker::GetTextureCache()->ClearCachedImage(pItem->GetArt("thumb"));
74 if (m_textureDatabase->Open())
76 m_textureDatabase->ClearTextureForPath(pItem->GetPath(), "thumb");
77 m_textureDatabase->Close();
79 pItem->SetArt("thumb", "");
82 std::string thumb;
83 if (pItem->IsPicture() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() &&
84 !pItem->IsCBR() && !PLAYLIST::IsPlayList(*pItem))
85 { // load the thumb from the image file
86 thumb = pItem->HasArt("thumb") ? pItem->GetArt("thumb")
87 : IMAGE_FILES::URLFromFile(pItem->GetPath());
89 else if (VIDEO::IsVideo(*pItem) && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() &&
90 !pItem->IsCBR() && !PLAYLIST::IsPlayList(*pItem))
91 { // video
92 CVideoThumbLoader loader;
93 loader.LoadItem(pItem);
95 else if (!pItem->HasArt("thumb"))
96 { // folder, zip, cbz, rar, cbr, playlist may have a previously cached image
97 thumb = GetCachedImage(*pItem, "thumb");
99 if (!thumb.empty())
101 CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb);
102 pItem->SetArt("thumb", thumb);
104 ART::FillInDefaultIcon(*pItem);
105 return true;
108 bool CPictureThumbLoader::LoadItemLookup(CFileItem* pItem)
110 return false;
113 void CPictureThumbLoader::ProcessFoldersAndArchives(CFileItem *pItem)
115 if (pItem->HasArt("thumb"))
116 return;
118 CTextureDatabase db;
119 db.Open();
120 if (pItem->IsCBR() || pItem->IsCBZ())
122 std::string strTBN(URIUtils::ReplaceExtension(pItem->GetPath(),".tbn"));
123 if (CFileUtils::Exists(strTBN))
125 db.SetTextureForPath(pItem->GetPath(), "thumb", strTBN);
126 CServiceBroker::GetTextureCache()->BackgroundCacheImage(strTBN);
127 pItem->SetArt("thumb", strTBN);
128 return;
131 if ((pItem->m_bIsFolder || pItem->IsCBR() || pItem->IsCBZ()) && !pItem->m_bIsShareOrDrive
132 && !pItem->IsParentFolder() && !pItem->IsPath("add"))
134 // first check for a folder.jpg
135 std::string thumb = "folder.jpg";
136 CURL pathToUrl = pItem->GetURL();
137 if (pItem->IsCBR())
139 pathToUrl = URIUtils::CreateArchivePath("rar",pItem->GetURL(),"");
140 thumb = "cover.jpg";
142 if (pItem->IsCBZ())
144 pathToUrl = URIUtils::CreateArchivePath("zip",pItem->GetURL(),"");
145 thumb = "cover.jpg";
147 if (pItem->IsMultiPath())
148 pathToUrl = CURL(CMultiPathDirectory::GetFirstPath(pItem->GetPath()));
149 thumb = URIUtils::AddFileToFolder(pathToUrl.Get(), thumb);
150 if (CFileUtils::Exists(thumb))
152 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
153 CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb);
154 pItem->SetArt("thumb", thumb);
155 return;
157 if (!pItem->IsPlugin())
159 // we load the directory, grab 4 random thumb files (if available) and then generate
160 // the thumb.
162 CFileItemList items;
164 CDirectory::GetDirectory(pathToUrl, items, CServiceBroker::GetFileExtensionProvider().GetPictureExtensions(), DIR_FLAG_NO_FILE_DIRS);
166 // create the folder thumb by choosing 4 random thumbs within the folder and putting
167 // them into one thumb.
168 // count the number of images
169 for (int i=0; i < items.Size();)
171 if (!items[i]->IsPicture() || items[i]->IsZIP() || items[i]->IsRAR() ||
172 PLAYLIST::IsPlayList(*items[i]))
174 items.Remove(i);
176 else
177 i++;
180 if (items.IsEmpty())
182 if (pItem->IsCBZ() || pItem->IsCBR())
184 CDirectory::GetDirectory(pathToUrl, items, CServiceBroker::GetFileExtensionProvider().GetPictureExtensions(), DIR_FLAG_NO_FILE_DIRS);
185 for (int i=0;i<items.Size();++i)
187 CFileItemPtr item = items[i];
188 if (item->m_bIsFolder)
190 ProcessFoldersAndArchives(item.get());
191 pItem->SetArt("thumb", items[i]->GetArt("thumb"));
192 pItem->SetArt("icon", items[i]->GetArt("icon"));
193 return;
197 return; // no images in this folder
200 // randomize them
201 items.Randomize();
203 if (items.Size() < 4 || pItem->IsCBR() || pItem->IsCBZ())
204 { // less than 4 items, so just grab the first thumb
205 items.Sort(SortByLabel, SortOrderAscending);
206 std::string thumb = IMAGE_FILES::URLFromFile(items[0]->GetPath());
207 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
208 CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb);
209 pItem->SetArt("thumb", thumb);
211 else
213 std::string thumb = IMAGE_FILES::URLFromFile(pItem->GetPath(), "picturefolder");
214 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
215 pItem->SetArt("thumb", thumb);
218 // refill in the icon to get it to update
219 ART::FillInDefaultIcon(*pItem);