[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / pictures / PictureThumbLoader.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.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 "settings/GUISettings.h"
31 #include "utils/URIUtils.h"
32 #include "settings/Settings.h"
33 #include "settings/AdvancedSettings.h"
34 #include "video/VideoThumbLoader.h"
35
36 using namespace XFILE;
37 using namespace std;
38
39 CPictureThumbLoader::CPictureThumbLoader() : CThumbLoader(1), CJobQueue(true)
40 {
41   m_regenerateThumbs = false;
42 }
43
44 CPictureThumbLoader::~CPictureThumbLoader()
45 {
46   StopThread();
47 }
48
49 bool CPictureThumbLoader::LoadItem(CFileItem* pItem)
50 {
51   if (pItem->m_bIsShareOrDrive) return true;
52   if (pItem->IsParentFolder()) return true;
53
54   if (pItem->HasArt("thumb") && m_regenerateThumbs)
55   {
56     CTextureCache::Get().ClearCachedImage(pItem->GetArt("thumb"));
57     CTextureDatabase db;
58     if (db.Open())
59       db.ClearTextureForPath(pItem->GetPath(), "thumb");
60     pItem->SetArt("thumb", "");
61   }
62
63   CStdString 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());
67   }
68   else if (pItem->IsVideo() && !pItem->IsZIP() && !pItem->IsRAR() && !pItem->IsCBZ() && !pItem->IsCBR() && !pItem->IsPlayList())
69   { // video
70     if (!CVideoThumbLoader::FillThumb(*pItem))
71     {
72       CStdString thumbURL = CVideoThumbLoader::GetEmbeddedThumbURL(*pItem);
73       if (CTextureCache::Get().HasCachedImage(thumbURL))
74       {
75         thumb = thumbURL;
76       }
77       else if (g_guiSettings.GetBool("myvideos.extractthumb") && g_guiSettings.GetBool("myvideos.extractflags"))
78       {
79         CFileItem item(*pItem);
80         CThumbExtractor* extract = new CThumbExtractor(item, pItem->GetPath(), true, thumbURL);
81         AddJob(extract);
82         thumb.clear();
83       }
84     }
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.IsEmpty())
91   {
92     CTextureCache::Get().BackgroundCacheImage(thumb);
93     pItem->SetArt("thumb", thumb);
94   }
95   pItem->FillInDefaultIcon();
96   return true;
97 }
98
99 void CPictureThumbLoader::OnJobComplete(unsigned int jobID, bool success, CJob* job)
100 {
101   if (success)
102   {
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);
108   }
109   CJobQueue::OnJobComplete(jobID, success, job);
110 }
111
112
113 void CPictureThumbLoader::OnLoaderFinish()
114 {
115   m_regenerateThumbs = false;
116 }
117
118 void CPictureThumbLoader::ProcessFoldersAndArchives(CFileItem *pItem)
119 {
120   if (pItem->HasArt("thumb"))
121     return;
122
123   CTextureDatabase db;
124   db.Open();
125   if (pItem->IsCBR() || pItem->IsCBZ())
126   {
127     CStdString strTBN(URIUtils::ReplaceExtension(pItem->GetPath(),".tbn"));
128     if (CFile::Exists(strTBN))
129     {
130       db.SetTextureForPath(pItem->GetPath(), "thumb", strTBN);
131       CTextureCache::Get().BackgroundCacheImage(strTBN);
132       pItem->SetArt("thumb", strTBN);
133       return;
134     }
135   }
136   if ((pItem->m_bIsFolder || pItem->IsCBR() || pItem->IsCBZ()) && !pItem->m_bIsShareOrDrive && !pItem->IsParentFolder())
137   {
138     // first check for a folder.jpg
139     CStdString thumb = "folder.jpg";
140     CStdString strPath = pItem->GetPath();
141     if (pItem->IsCBR())
142     {
143       URIUtils::CreateArchivePath(strPath,"rar",pItem->GetPath(),"");
144       thumb = "cover.jpg";
145     }
146     if (pItem->IsCBZ())
147     {
148       URIUtils::CreateArchivePath(strPath,"zip",pItem->GetPath(),"");
149       thumb = "cover.jpg";
150     }
151     if (pItem->IsMultiPath())
152       strPath = CMultiPathDirectory::GetFirstPath(pItem->GetPath());
153     thumb = URIUtils::AddFileToFolder(strPath, thumb);
154     if (CFile::Exists(thumb))
155     {
156       db.SetTextureForPath(pItem->GetPath(), "thumb", thumb);
157       CTextureCache::Get().BackgroundCacheImage(thumb);
158       pItem->SetArt("thumb", thumb);
159       return;
160     }
161     if (!pItem->IsPlugin())
162     {
163       // we load the directory, grab 4 random thumb files (if available) and then generate
164       // the thumb.
165
166       CFileItemList items;
167
168       CDirectory::GetDirectory(strPath, items, g_settings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
169       
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();)
174       {
175         if (!items[i]->IsPicture() || items[i]->IsZIP() || items[i]->IsRAR() || items[i]->IsPlayList())
176         {
177           items.Remove(i);
178         }
179         else
180           i++;
181       }
182
183       if (items.IsEmpty())
184       {
185         if (pItem->IsCBZ() || pItem->IsCBR())
186         {
187           CDirectory::GetDirectory(strPath, items, g_settings.m_pictureExtensions, DIR_FLAG_NO_FILE_DIRS);
188           for (int i=0;i<items.Size();++i)
189           {
190             CFileItemPtr item = items[i];
191             if (item->m_bIsFolder)
192             {
193               ProcessFoldersAndArchives(item.get());
194               pItem->SetArt("thumb", items[i]->GetArt("thumb"));
195               pItem->SetIconImage(items[i]->GetIconImage());
196               return;
197             }
198           }
199         }
200         return; // no images in this folder
201       }
202
203       // randomize them
204       items.Randomize();
205
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);
213       }
214       else
215       {
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)))
224         {
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));
232         }
233       }
234     }
235     // refill in the icon to get it to update
236     pItem->FillInDefaultIcon();
237   }
238 }