2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "PictureThumbLoader.h"
23 #include "filesystem/File.h"
25 #include "TextureCache.h"
26 #include "filesystem/Directory.h"
27 #include "filesystem/MultiPathDirectory.h"
28 #include "guilib/GUIWindowManager.h"
29 #include "GUIUserMessages.h"
30 #include "settings/GUISettings.h"
31 #include "utils/URIUtils.h"
32 #include "settings/Settings.h"
33 #include "settings/AdvancedSettings.h"
34 #include "video/VideoThumbLoader.h"
36 using namespace XFILE;
39 CPictureThumbLoader::CPictureThumbLoader() : CThumbLoader(1), CJobQueue(true)
41 m_regenerateThumbs = false;
44 CPictureThumbLoader::~CPictureThumbLoader()
49 bool CPictureThumbLoader::LoadItem(CFileItem* pItem)
51 if (pItem->m_bIsShareOrDrive) return true;
52 if (pItem->IsParentFolder()) return true;
54 if (pItem->HasArt("thumb") && m_regenerateThumbs)
56 CTextureCache::Get().ClearCachedImage(pItem->GetArt("thumb"));
59 db.ClearTextureForPath(pItem->GetPath(), "thumb");
60 pItem->SetArt("thumb", "");
64 if (pItem->IsPicture() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList())
65 { // load the thumb from the image file
66 thumb = pItem->HasArt("thumb") ? pItem->GetArt("thumb") : CTextureCache::GetWrappedThumbURL(pItem->GetPath());
68 else if (pItem->IsVideo() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList())
70 if (!CVideoThumbLoader::FillThumb(*pItem))
72 CStdString thumbURL = CVideoThumbLoader::GetEmbeddedThumbURL(*pItem);
73 if (CTextureCache::Get().HasCachedImage(thumbURL))
77 else if (g_guiSettings.GetBool("myvideos.extractthumb") && g_guiSettings.GetBool("myvideos.extractflags"))
79 CFileItem item(*pItem);
80 CThumbExtractor* extract = new CThumbExtractor(item, pItem->GetPath(), true, thumbURL);
86 else if (!pItem->HasArt("thumb"))
87 { // folder, zip, cbz, rar, cbr, playlist may have a previously cached image
88 thumb = GetCachedImage(*pItem, "thumb");
92 CTextureCache::Get().BackgroundCacheImage(thumb);
93 pItem->SetArt("thumb", thumb);
95 pItem->FillInDefaultIcon();
99 void CPictureThumbLoader::OnJobComplete(unsigned int jobID, bool success, CJob* job)
103 CThumbExtractor* loader = (CThumbExtractor*)job;
104 loader->m_item.SetPath(loader->m_listpath);
105 CFileItemPtr pItem(new CFileItem(loader->m_item));
106 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM, 0, pItem);
107 g_windowManager.SendThreadMessage(msg);
109 CJobQueue::OnJobComplete(jobID, success, job);
113 void CPictureThumbLoader::OnLoaderFinish()
115 m_regenerateThumbs = false;
118 void CPictureThumbLoader::ProcessFoldersAndArchives(CFileItem *pItem)
120 if (pItem->HasArt("thumb"))
125 if (pItem->IsCBR() || pItem->IsCBZ())
127 CStdString strTBN(URIUtils::ReplaceExtension(pItem->GetPath(),".tbn"));
128 if (CFile::Exists(strTBN))
130 db.SetTextureForPath(pItem->GetPath(), "thumb", strTBN);
131 CTextureCache::Get().BackgroundCacheImage(strTBN);
132 pItem->SetArt("thumb", strTBN);
136 if ((pItem->m_bIsFolder || pItem->IsCBR() || pItem->IsCBZ()) && !pItem->m_bIsShareOrDrive && !pItem->IsParentFolder())
138 // first check for a folder.jpg
139 CStdString thumb = "folder.jpg";
140 CStdString strPath = pItem->GetPath();
143 URIUtils::CreateArchivePath(strPath,"rar",pItem->GetPath(),"");
148 URIUtils::CreateArchivePath(strPath,"zip",pItem->GetPath(),"");
151 if (pItem->IsMultiPath())
152 strPath = CMultiPathDirectory::GetFirstPath(pItem->GetPath());
153 thumb = URIUtils::AddFileToFolder(strPath, thumb);
154 if (CFile::Exists(thumb))
156 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
157 CTextureCache::Get().BackgroundCacheImage(thumb);
158 pItem->SetArt("thumb", thumb);
161 if (!pItem->IsPlugin())
163 // we load the directory, grab 4 random thumb files (if available) and then generate
168 CDirectory::GetDirectory(strPath, items, g_settings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
170 // create the folder thumb by choosing 4 random thumbs within the folder and putting
171 // them into one thumb.
172 // count the number of images
173 for (int i=0; i < items.Size();)
175 if (!items[i]->IsPicture() || items[i]->IsZIP() || items[i]->IsRAR() || items[i]->IsPlayList())
185 if (pItem->IsCBZ() || pItem->IsCBR())
187 CDirectory::GetDirectory(strPath, items, g_settings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
188 for (int i=0;i<items.Size();++i)
190 CFileItemPtr item = items[i];
191 if (item->m_bIsFolder)
193 ProcessFoldersAndArchives(item.get());
194 pItem->SetArt("thumb", items[i]->GetArt("thumb"));
195 pItem->SetIconImage(items[i]->GetIconImage());
200 return; // no images in this folder
206 if (items.Size() < 4 || pItem->IsCBR() || pItem->IsCBZ())
207 { // less than 4 items, so just grab the first thumb
208 items.Sort(SORT_METHOD_LABEL, SortOrderAscending);
209 CStdString thumb = CTextureCache::GetWrappedThumbURL(items[0]->GetPath());
210 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
211 CTextureCache::Get().BackgroundCacheImage(thumb);
212 pItem->SetArt("thumb", thumb);
216 // ok, now we've got the files to get the thumbs from, lets create it...
217 // we basically load the 4 images and combine them
218 vector<string> files;
219 for (int thumb = 0; thumb < 4; thumb++)
220 files.push_back(items[thumb]->GetPath());
221 CStdString thumb = CTextureCache::GetWrappedImageURL(pItem->GetPath(), "picturefolder");
222 CStdString relativeCacheFile = CTextureCache::GetCacheFile(thumb) + ".png";
223 if (CPicture::CreateTiledThumb(files, CTextureCache::GetCachedPath(relativeCacheFile)))
225 CTextureDetails details;
226 details.file = relativeCacheFile;
227 details.width = g_advancedSettings.GetThumbSize();
228 details.height = g_advancedSettings.GetThumbSize();
229 CTextureCache::Get().AddCachedTexture(thumb, details);
230 db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
231 pItem->SetArt("thumb", CTextureCache::GetCachedPath(relativeCacheFile));
235 // refill in the icon to get it to update
236 pItem->FillInDefaultIcon();