4f247004d90af8532df16a2f5973ee560ebef79c
[vuplus_xbmc] / xbmc / pictures / PictureThumbLoader.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "PictureThumbLoader.h"
22 #include "Picture.h"
23 #include "filesystem/File.h"
24 #include "FileItem.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 "utils/URIUtils.h"
31 #include "settings/AdvancedSettings.h"
32 #include "settings/Settings.h"
33 #include "video/VideoThumbLoader.h"
34
35 using namespace XFILE;
36 using namespace std;
37
38 CPictureThumbLoader::CPictureThumbLoader() : CThumbLoader(), CJobQueue(true, 1, CJob::PRIORITY_LOW_PAUSABLE)
39 {
40   m_regenerateThumbs = false;
41 }
42
43 CPictureThumbLoader::~CPictureThumbLoader()
44 {
45   StopThread();
46 }
47
48 void CPictureThumbLoader::OnLoaderFinish()
49 {
50   m_regenerateThumbs = false;
51   CThumbLoader::OnLoaderFinish();
52 }
53
54 bool CPictureThumbLoader::LoadItem(CFileItem* pItem)
55 {
56   bool result  = LoadItemCached(pItem);
57        result |= LoadItemLookup(pItem);
58
59   return result;
60 }
61
62 bool CPictureThumbLoader::LoadItemCached(CFileItem* pItem)
63 {
64   if (pItem->m_bIsShareOrDrive
65   ||  pItem->IsParentFolder())
66     return false;
67
68   if (pItem->HasArt("thumb") && m_regenerateThumbs)
69   {
70     CTextureCache::Get().ClearCachedImage(pItem->GetArt("thumb"));
71     if (m_textureDatabase->Open())
72     {
73       m_textureDatabase->ClearTextureForPath(pItem->GetPath(), "thumb");
74       m_textureDatabase->Close();
75     }
76     pItem->SetArt("thumb", "");
77   }
78
79   CStdString thumb;
80   if (pItem->IsPicture() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList())
81   { // load the thumb from the image file
82     thumb = pItem->HasArt("thumb") ? pItem->GetArt("thumb") : CTextureUtils::GetWrappedThumbURL(pItem->GetPath());
83   }
84   else if (pItem->IsVideo() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList())
85   { // video
86     CVideoThumbLoader loader;
87     if (!loader.FillThumb(*pItem))
88     {
89       CStdString thumbURL = CVideoThumbLoader::GetEmbeddedThumbURL(*pItem);
90       if (CTextureCache::Get().HasCachedImage(thumbURL))
91       {
92         thumb = thumbURL;
93       }
94       else if (CSettings::Get().GetBool("myvideos.extractthumb") && CSettings::Get().GetBool("myvideos.extractflags"))
95       {
96         CFileItem item(*pItem);
97         CThumbExtractor* extract = new CThumbExtractor(item, pItem->GetPath(), true, thumbURL);
98         AddJob(extract);
99         thumb.clear();
100       }
101     }
102   }
103   else if (!pItem->HasArt("thumb"))
104   { // folder, zip, cbz, rar, cbr, playlist may have a previously cached image
105     thumb = GetCachedImage(*pItem, "thumb");
106   }
107   if (!thumb.IsEmpty())
108   {
109     CTextureCache::Get().BackgroundCacheImage(thumb);
110     pItem->SetArt("thumb", thumb);
111   }
112   pItem->FillInDefaultIcon();
113   return true;
114 }
115
116 bool CPictureThumbLoader::LoadItemLookup(CFileItem* pItem)
117 {
118   return false;
119 }
120
121 void CPictureThumbLoader::OnJobComplete(unsigned int jobID, bool success, CJob* job)
122 {
123   if (success)
124   {
125     CThumbExtractor* loader = (CThumbExtractor*)job;
126     loader->m_item.SetPath(loader->m_listpath);
127     CFileItemPtr pItem(new CFileItem(loader->m_item));
128     CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM, 0, pItem);
129     g_windowManager.SendThreadMessage(msg);
130   }
131   CJobQueue::OnJobComplete(jobID, success, job);
132 }
133
134 void CPictureThumbLoader::ProcessFoldersAndArchives(CFileItem *pItem)
135 {
136   if (pItem->HasArt("thumb"))
137     return;
138
139   CTextureDatabase db;
140   db.Open();
141   if (pItem->IsCBR() || pItem->IsCBZ())
142   {
143     CStdString strTBN(URIUtils::ReplaceExtension(pItem->GetPath(),".tbn"));
144     if (CFile::Exists(strTBN))
145     {
146       db.SetTextureForPath(pItem->GetPath(), "thumb", strTBN);
147       CTextureCache::Get().BackgroundCacheImage(strTBN);
148       pItem->SetArt("thumb", strTBN);
149       return;
150     }
151   }
152   if ((pItem->m_bIsFolder || pItem->IsCBR() || pItem->IsCBZ()) && !pItem->m_bIsShareOrDrive
153       && !pItem->IsParentFolder() && !pItem->GetPath().Equals("add"))
154   {
155     // first check for a folder.jpg
156     CStdString thumb = "folder.jpg";
157     CStdString strPath = pItem->GetPath();
158     if (pItem->IsCBR())
159     {
160       URIUtils::CreateArchivePath(strPath,"rar",pItem->GetPath(),"");
161       thumb = "cover.jpg";
162     }
163     if (pItem->IsCBZ())
164     {
165       URIUtils::CreateArchivePath(strPath,"zip",pItem->GetPath(),"");
166       thumb = "cover.jpg";
167     }
168     if (pItem->IsMultiPath())
169       strPath = CMultiPathDirectory::GetFirstPath(pItem->GetPath());
170     thumb = URIUtils::AddFileToFolder(strPath, thumb);
171     if (CFile::Exists(thumb))
172     {
173       db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
174       CTextureCache::Get().BackgroundCacheImage(thumb);
175       pItem->SetArt("thumb", thumb);
176       return;
177     }
178     if (!pItem->IsPlugin())
179     {
180       // we load the directory, grab 4 random thumb files (if available) and then generate
181       // the thumb.
182
183       CFileItemList items;
184
185       CDirectory::GetDirectory(strPath, items, g_advancedSettings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
186       
187       // create the folder thumb by choosing 4 random thumbs within the folder and putting
188       // them into one thumb.
189       // count the number of images
190       for (int i=0; i < items.Size();)
191       {
192         if (!items[i]->IsPicture() || items[i]->IsZIP() || items[i]->IsRAR() || items[i]->IsPlayList())
193         {
194           items.Remove(i);
195         }
196         else
197           i++;
198       }
199
200       if (items.IsEmpty())
201       {
202         if (pItem->IsCBZ() || pItem->IsCBR())
203         {
204           CDirectory::GetDirectory(strPath, items, g_advancedSettings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
205           for (int i=0;i<items.Size();++i)
206           {
207             CFileItemPtr item = items[i];
208             if (item->m_bIsFolder)
209             {
210               ProcessFoldersAndArchives(item.get());
211               pItem->SetArt("thumb", items[i]->GetArt("thumb"));
212               pItem->SetIconImage(items[i]->GetIconImage());
213               return;
214             }
215           }
216         }
217         return; // no images in this folder
218       }
219
220       // randomize them
221       items.Randomize();
222
223       if (items.Size() < 4 || pItem->IsCBR() || pItem->IsCBZ())
224       { // less than 4 items, so just grab the first thumb
225         items.Sort(SortByLabel, SortOrderAscending);
226         CStdString thumb = CTextureUtils::GetWrappedThumbURL(items[0]->GetPath());
227         db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
228         CTextureCache::Get().BackgroundCacheImage(thumb);
229         pItem->SetArt("thumb", thumb);
230       }
231       else
232       {
233         // ok, now we've got the files to get the thumbs from, lets create it...
234         // we basically load the 4 images and combine them
235         vector<string> files;
236         for (int thumb = 0; thumb < 4; thumb++)
237           files.push_back(items[thumb]->GetPath());
238         CStdString thumb = CTextureUtils::GetWrappedImageURL(pItem->GetPath(), "picturefolder");
239         CStdString relativeCacheFile = CTextureCache::GetCacheFile(thumb) + ".png";
240         if (CPicture::CreateTiledThumb(files, CTextureCache::GetCachedPath(relativeCacheFile)))
241         {
242           CTextureDetails details;
243           details.file = relativeCacheFile;
244           details.width = g_advancedSettings.GetThumbSize();
245           details.height = g_advancedSettings.GetThumbSize();
246           CTextureCache::Get().AddCachedTexture(thumb, details);
247           db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
248           pItem->SetArt("thumb", CTextureCache::GetCachedPath(relativeCacheFile));
249         }
250       }
251     }
252     // refill in the icon to get it to update
253     pItem->FillInDefaultIcon();
254   }
255 }