| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "PictureThumbLoader.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "Picture.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "TextureCache.h" |
| 15 |
#include "URL.h" |
| 16 |
#include "filesystem/Directory.h" |
| 17 |
#include "filesystem/MultiPathDirectory.h" |
| 18 |
#include "guilib/GUIComponent.h" |
| 19 |
#include "guilib/GUIWindowManager.h" |
| 20 |
#include "settings/AdvancedSettings.h" |
| 21 |
#include "settings/Settings.h" |
| 22 |
#include "settings/SettingsComponent.h" |
| 23 |
#include "utils/FileExtensionProvider.h" |
| 24 |
#include "utils/FileUtils.h" |
| 25 |
#include "utils/URIUtils.h" |
| 26 |
#include "video/VideoThumbLoader.h" |
| 27 |
|
| 28 |
using namespace XFILE; |
| 29 |
|
| 30 |
CPictureThumbLoader::CPictureThumbLoader() : CThumbLoader() |
| 31 |
{ |
| 32 |
m_regenerateThumbs = false; |
| 33 |
} |
| 34 |
|
| 35 |
CPictureThumbLoader::~CPictureThumbLoader() |
| 36 |
{ |
| 37 |
StopThread(); |
| 38 |
} |
| 39 |
|
| 40 |
void CPictureThumbLoader::OnLoaderFinish() |
| 41 |
{ |
| 42 |
if (m_regenerateThumbs) |
| 43 |
{ |
| 44 |
CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_REFRESH_THUMBS); |
| 45 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg); |
| 46 |
} |
| 47 |
m_regenerateThumbs = false; |
| 48 |
CThumbLoader::OnLoaderFinish(); |
| 49 |
} |
| 50 |
|
| 51 |
bool CPictureThumbLoader::LoadItem(CFileItem* pItem) |
| 52 |
{ |
| 53 |
bool result = LoadItemCached(pItem); |
| 54 |
result |= LoadItemLookup(pItem); |
| 55 |
|
| 56 |
return result; |
| 57 |
} |
| 58 |
|
| 59 |
bool CPictureThumbLoader::LoadItemCached(CFileItem* pItem) |
| 60 |
{ |
| 61 |
if (pItem->m_bIsShareOrDrive |
| 62 |
|| pItem->IsParentFolder()) |
| 63 |
return false; |
| 64 |
|
| 65 |
if (pItem->HasArt("thumb") && m_regenerateThumbs) |
| 66 |
{ |
| 67 |
CServiceBroker::GetTextureCache()->ClearCachedImage(pItem->GetArt("thumb")); |
| 68 |
if (m_textureDatabase->Open()) |
| 69 |
{ |
| 70 |
m_textureDatabase->ClearTextureForPath(pItem->GetPath(), "thumb"); |
| 71 |
m_textureDatabase->Close(); |
| 72 |
} |
| 73 |
pItem->SetArt("thumb", ""); |
| 74 |
} |
| 75 |
|
| 76 |
std::string thumb; |
| 77 |
if (pItem->IsPicture() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList()) |
| 78 |
{ // load the thumb from the image file |
| 79 |
thumb = pItem->HasArt("thumb") ? pItem->GetArt("thumb") : CTextureUtils::GetWrappedThumbURL(pItem->GetPath()); |
| 80 |
} |
| 81 |
else if (pItem->IsVideo() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList()) |
| 82 |
{ // video |
| 83 |
CVideoThumbLoader loader; |
| 84 |
loader.LoadItem(pItem); |
| 85 |
} |
| 86 |
else if (!pItem->HasArt("thumb")) |
| 87 |
{ // folder, zip, cbz, rar, cbr, playlist may have a previously cached image |
| 88 |
thumb = GetCachedImage(*pItem, "thumb"); |
| 89 |
} |
| 90 |
if (!thumb.empty()) |
| 91 |
{ |
| 92 |
CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb); |
| 93 |
pItem->SetArt("thumb", thumb); |
| 94 |
} |
| 95 |
pItem->FillInDefaultIcon(); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
bool CPictureThumbLoader::LoadItemLookup(CFileItem* pItem) |
| 100 |
{ |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
void CPictureThumbLoader::ProcessFoldersAndArchives(CFileItem *pItem) |
| 105 |
{ |
| 106 |
if (pItem->HasArt("thumb")) |
| 107 |
return; |
| 108 |
|
| 109 |
CTextureDatabase db; |
| 110 |
db.Open(); |
| 111 |
if (pItem->IsCBR() || pItem->IsCBZ()) |
| 112 |
{ |
| 113 |
std::string strTBN(URIUtils::ReplaceExtension(pItem->GetPath(),".tbn")); |
| 114 |
if (CFileUtils::Exists(strTBN)) |
| 115 |
{ |
| 116 |
db.SetTextureForPath(pItem->GetPath(), "thumb", strTBN); |
| 117 |
CServiceBroker::GetTextureCache()->BackgroundCacheImage(strTBN); |
| 118 |
pItem->SetArt("thumb", strTBN); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
if ((pItem->m_bIsFolder || pItem->IsCBR() || pItem->IsCBZ()) && !pItem->m_bIsShareOrDrive |
| 123 |
&& !pItem->IsParentFolder() && !pItem->IsPath("add")) |
| 124 |
{ |
| 125 |
// first check for a folder.jpg |
| 126 |
std::string thumb = "folder.jpg"; |
| 127 |
CURL pathToUrl = pItem->GetURL(); |
| 128 |
if (pItem->IsCBR()) |
| 129 |
{ |
| 130 |
pathToUrl = URIUtils::CreateArchivePath("rar",pItem->GetURL(),""); |
| 131 |
thumb = "cover.jpg"; |
| 132 |
} |
| 133 |
if (pItem->IsCBZ()) |
| 134 |
{ |
| 135 |
pathToUrl = URIUtils::CreateArchivePath("zip",pItem->GetURL(),""); |
| 136 |
thumb = "cover.jpg"; |
| 137 |
} |
| 138 |
if (pItem->IsMultiPath()) |
| 139 |
pathToUrl = CURL(CMultiPathDirectory::GetFirstPath(pItem->GetPath())); |
| 140 |
thumb = URIUtils::AddFileToFolder(pathToUrl.Get(), thumb); |
| 141 |
if (CFileUtils::Exists(thumb)) |
| 142 |
{ |
| 143 |
db.SetTextureForPath(pItem->GetPath(), "thumb", thumb); |
| 144 |
CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb); |
| 145 |
pItem->SetArt("thumb", thumb); |
| 146 |
return; |
| 147 |
} |
| 148 |
if (!pItem->IsPlugin()) |
| 149 |
{ |
| 150 |
// we load the directory, grab 4 random thumb files (if available) and then generate |
| 151 |
// the thumb. |
| 152 |
|
| 153 |
CFileItemList items; |
| 154 |
|
| 155 |
CDirectory::GetDirectory(pathToUrl, items, CServiceBroker::GetFileExtensionProvider().GetPictureExtensions(), DIR_FLAG_NO_FILE_DIRS); |
| 156 |
|
| 157 |
// create the folder thumb by choosing 4 random thumbs within the folder and putting |
| 158 |
// them into one thumb. |
| 159 |
// count the number of images |
| 160 |
for (int i=0; i < items.Size();) |
| 161 |
{ |
| 162 |
if (!items[i]->IsPicture() || items[i]->IsZIP() || items[i]->IsRAR() || items[i]->IsPlayList()) |
| 163 |
{ |
| 164 |
items.Remove(i); |
| 165 |
} |
| 166 |
else |
| 167 |
i++; |
| 168 |
} |
| 169 |
|
| 170 |
if (items.IsEmpty()) |
| 171 |
{ |
| 172 |
if (pItem->IsCBZ() || pItem->IsCBR()) |
| 173 |
{ |
| 174 |
CDirectory::GetDirectory(pathToUrl, items, CServiceBroker::GetFileExtensionProvider().GetPictureExtensions(), DIR_FLAG_NO_FILE_DIRS); |
| 175 |
for (int i=0;i<items.Size();++i) |
| 176 |
{ |
| 177 |
CFileItemPtr item = items[i]; |
| 178 |
if (item->m_bIsFolder) |
| 179 |
{ |
| 180 |
ProcessFoldersAndArchives(item.get()); |
| 181 |
pItem->SetArt("thumb", items[i]->GetArt("thumb")); |
| 182 |
pItem->SetArt("icon", items[i]->GetArt("icon")); |
| 183 |
return; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return; // no images in this folder |
| 188 |
} |
| 189 |
|
| 190 |
// randomize them |
| 191 |
items.Randomize(); |
| 192 |
|
| 193 |
if (items.Size() < 4 || pItem->IsCBR() || pItem->IsCBZ()) |
| 194 |
{ // less than 4 items, so just grab the first thumb |
| 195 |
items.Sort(SortByLabel, SortOrderAscending); |
| 196 |
std::string thumb = CTextureUtils::GetWrappedThumbURL(items[0]->GetPath()); |
| 197 |
db.SetTextureForPath(pItem->GetPath(), "thumb", thumb); |
| 198 |
CServiceBroker::GetTextureCache()->BackgroundCacheImage(thumb); |
| 199 |
pItem->SetArt("thumb", thumb); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
std::string thumb = CTextureUtils::GetWrappedImageURL(pItem->GetPath(), "picturefolder"); |
| 204 |
db.SetTextureForPath(pItem->GetPath(), "thumb", thumb); |
| 205 |
pItem->SetArt("thumb", thumb); |
| 206 |
} |
| 207 |
} |
| 208 |
// refill in the icon to get it to update |
| 209 |
pItem->FillInDefaultIcon(); |
| 210 |
} |
| 211 |
} |