Merge pull request #2755 from popcornmix/hang_eos_seek
[vuplus_xbmc] / xbmc / filesystem / VideoDatabaseDirectory.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 "VideoDatabaseDirectory.h"
22 #include "utils/URIUtils.h"
23 #include "VideoDatabaseDirectory/QueryParams.h"
24 #include "video/VideoDatabase.h"
25 #include "guilib/TextureManager.h"
26 #include "File.h"
27 #include "FileItem.h"
28 #include "settings/Settings.h"
29 #include "utils/Crc32.h"
30 #include "guilib/LocalizeStrings.h"
31 #include "utils/LegacyPathTranslation.h"
32 #include "utils/log.h"
33
34 using namespace std;
35 using namespace XFILE;
36 using namespace VIDEODATABASEDIRECTORY;
37
38 CVideoDatabaseDirectory::CVideoDatabaseDirectory(void)
39 {
40 }
41
42 CVideoDatabaseDirectory::~CVideoDatabaseDirectory(void)
43 {
44 }
45
46 bool CVideoDatabaseDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
47 {
48   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
49   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
50
51   if (!pNode.get())
52     return false;
53
54   bool bResult = pNode->GetChilds(items);
55   for (int i=0;i<items.Size();++i)
56   {
57     CFileItemPtr item = items[i];
58     if (item->m_bIsFolder && !item->HasIcon() && !item->HasArt("thumb"))
59     {
60       CStdString strImage = GetIcon(item->GetPath());
61       if (!strImage.IsEmpty() && g_TextureManager.HasTexture(strImage))
62         item->SetIconImage(strImage);
63     }
64   }
65   items.SetLabel(pNode->GetLocalizedName());
66
67   return bResult;
68 }
69
70 NODE_TYPE CVideoDatabaseDirectory::GetDirectoryChildType(const CStdString& strPath)
71 {
72   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
73   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
74
75   if (!pNode.get())
76     return NODE_TYPE_NONE;
77
78   return pNode->GetChildType();
79 }
80
81 NODE_TYPE CVideoDatabaseDirectory::GetDirectoryType(const CStdString& strPath)
82 {
83   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
84   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
85
86   if (!pNode.get())
87     return NODE_TYPE_NONE;
88
89   return pNode->GetType();
90 }
91
92 NODE_TYPE CVideoDatabaseDirectory::GetDirectoryParentType(const CStdString& strPath)
93 {
94   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
95   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
96
97   if (!pNode.get())
98     return NODE_TYPE_NONE;
99
100   CDirectoryNode* pParentNode=pNode->GetParent();
101
102   if (!pParentNode)
103     return NODE_TYPE_NONE;
104
105   return pParentNode->GetChildType();
106 }
107
108 bool CVideoDatabaseDirectory::GetQueryParams(const CStdString& strPath, CQueryParams& params)
109 {
110   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
111   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
112
113   if (!pNode.get())
114     return false;
115
116   CDirectoryNode::GetDatabaseInfo(strPath,params);
117   return true;
118 }
119
120 void CVideoDatabaseDirectory::ClearDirectoryCache(const CStdString& strDirectory)
121 {
122   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strDirectory);
123   URIUtils::RemoveSlashAtEnd(path);
124
125   Crc32 crc;
126   crc.ComputeFromLowerCase(path);
127
128   CStdString strFileName;
129   strFileName.Format("special://temp/%08x.fi", (unsigned __int32) crc);
130   CFile::Delete(strFileName);
131 }
132
133 bool CVideoDatabaseDirectory::IsAllItem(const CStdString& strDirectory)
134 {
135   if (strDirectory.Right(4).Equals("/-1/"))
136     return true;
137   return false;
138 }
139
140 bool CVideoDatabaseDirectory::GetLabel(const CStdString& strDirectory, CStdString& strLabel)
141 {
142   strLabel = "";
143
144   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strDirectory);
145   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
146   if (!pNode.get() || path.IsEmpty())
147     return false;
148
149   // first see if there's any filter criteria
150   CQueryParams params;
151   CDirectoryNode::GetDatabaseInfo(path, params);
152
153   CVideoDatabase videodatabase;
154   if (!videodatabase.Open())
155     return false;
156
157   // get genre
158   if (params.GetGenreId() != -1)
159     strLabel += videodatabase.GetGenreById(params.GetGenreId());
160
161   // get country
162   if (params.GetCountryId() != -1)
163     strLabel += videodatabase.GetCountryById(params.GetCountryId());
164
165   // get set
166   if (params.GetSetId() != -1)
167     strLabel += videodatabase.GetSetById(params.GetSetId());
168
169   // get tag
170   if (params.GetTagId() != -1)
171     strLabel += videodatabase.GetTagById(params.GetTagId());
172
173   // get year
174   if (params.GetYear() != -1)
175   {
176     CStdString strTemp;
177     strTemp.Format("%i",params.GetYear());
178     if (!strLabel.IsEmpty())
179       strLabel += " / ";
180     strLabel += strTemp;
181   }
182
183   if (strLabel.IsEmpty())
184   {
185     switch (pNode->GetChildType())
186     {
187     case NODE_TYPE_TITLE_MOVIES:
188     case NODE_TYPE_TITLE_TVSHOWS:
189     case NODE_TYPE_TITLE_MUSICVIDEOS:
190       strLabel = g_localizeStrings.Get(369); break;
191     case NODE_TYPE_ACTOR: // Actor
192       strLabel = g_localizeStrings.Get(344); break;
193     case NODE_TYPE_GENRE: // Genres
194       strLabel = g_localizeStrings.Get(135); break;
195     case NODE_TYPE_COUNTRY: // Countries
196       strLabel = g_localizeStrings.Get(20451); break;
197     case NODE_TYPE_YEAR: // Year
198       strLabel = g_localizeStrings.Get(562); break;
199     case NODE_TYPE_DIRECTOR: // Director
200       strLabel = g_localizeStrings.Get(20348); break;
201     case NODE_TYPE_SETS: // Sets
202       strLabel = g_localizeStrings.Get(20434); break;
203     case NODE_TYPE_TAGS: // Tags
204       strLabel = g_localizeStrings.Get(20459); break;
205     case NODE_TYPE_MOVIES_OVERVIEW: // Movies
206       strLabel = g_localizeStrings.Get(342); break;
207     case NODE_TYPE_TVSHOWS_OVERVIEW: // TV Shows
208       strLabel = g_localizeStrings.Get(20343); break;
209     case NODE_TYPE_RECENTLY_ADDED_MOVIES: // Recently Added Movies
210       strLabel = g_localizeStrings.Get(20386); break;
211     case NODE_TYPE_RECENTLY_ADDED_EPISODES: // Recently Added Episodes
212       strLabel = g_localizeStrings.Get(20387); break;
213     case NODE_TYPE_STUDIO: // Studios
214       strLabel = g_localizeStrings.Get(20388); break;
215     case NODE_TYPE_MUSICVIDEOS_OVERVIEW: // Music Videos
216       strLabel = g_localizeStrings.Get(20389); break;
217     case NODE_TYPE_RECENTLY_ADDED_MUSICVIDEOS: // Recently Added Music Videos
218       strLabel = g_localizeStrings.Get(20390); break;
219     case NODE_TYPE_SEASONS: // Seasons
220       strLabel = g_localizeStrings.Get(33054); break;
221     case NODE_TYPE_EPISODES: // Episodes
222       strLabel = g_localizeStrings.Get(20360); break;
223     default:
224       CLog::Log(LOGWARNING, "%s - Unknown nodetype requested %d", __FUNCTION__, pNode->GetChildType());
225       return false;
226     }
227   }
228
229   return true;
230 }
231
232 CStdString CVideoDatabaseDirectory::GetIcon(const CStdString &strDirectory)
233 {
234   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strDirectory);
235   switch (GetDirectoryChildType(path))
236   {
237   case NODE_TYPE_TITLE_MOVIES:
238     if (path.Equals("videodb://movies/titles/"))
239     {
240       if (CSettings::Get().GetBool("myvideos.flatten"))
241         return "DefaultMovies.png";
242       return "DefaultMovieTitle.png";
243     }
244     return "";
245   case NODE_TYPE_TITLE_TVSHOWS:
246     if (path.Equals("videodb://tvshows/titles/"))
247     {
248       if (CSettings::Get().GetBool("myvideos.flatten"))
249         return "DefaultTVShows.png";
250       return "DefaultTVShowTitle.png";
251     }
252     return "";
253   case NODE_TYPE_TITLE_MUSICVIDEOS:
254     if (path.Equals("videodb://musicvideos/titles/"))
255     {
256       if (CSettings::Get().GetBool("myvideos.flatten"))
257         return "DefaultMusicVideos.png";
258       return "DefaultMusicVideoTitle.png";
259     }
260     return "";
261   case NODE_TYPE_ACTOR: // Actor
262     return "DefaultActor.png";
263   case NODE_TYPE_GENRE: // Genres
264     return "DefaultGenre.png";
265   case NODE_TYPE_COUNTRY: // Countries
266     return "DefaultCountry.png";
267   case NODE_TYPE_SETS: // Sets
268     return "DefaultSets.png";
269   case NODE_TYPE_TAGS: // Tags
270     return "DefaultTags.png";
271   case NODE_TYPE_YEAR: // Year
272     return "DefaultYear.png";
273   case NODE_TYPE_DIRECTOR: // Director
274     return "DefaultDirector.png";
275   case NODE_TYPE_MOVIES_OVERVIEW: // Movies
276     return "DefaultMovies.png";
277   case NODE_TYPE_TVSHOWS_OVERVIEW: // TV Shows
278     return "DefaultTVShows.png";
279   case NODE_TYPE_RECENTLY_ADDED_MOVIES: // Recently Added Movies
280     return "DefaultRecentlyAddedMovies.png";
281   case NODE_TYPE_RECENTLY_ADDED_EPISODES: // Recently Added Episodes
282     return "DefaultRecentlyAddedEpisodes.png";
283   case NODE_TYPE_RECENTLY_ADDED_MUSICVIDEOS: // Recently Added Episodes
284     return "DefaultRecentlyAddedMusicVideos.png";
285   case NODE_TYPE_STUDIO: // Studios
286     return "DefaultStudios.png";
287   case NODE_TYPE_MUSICVIDEOS_OVERVIEW: // Music Videos
288     return "DefaultMusicVideos.png";
289   case NODE_TYPE_MUSICVIDEOS_ALBUM: // Music Videos - Albums
290     return "DefaultMusicAlbums.png";
291   default:
292     CLog::Log(LOGWARNING, "%s - Unknown nodetype requested %s", __FUNCTION__, strDirectory.c_str());
293     break;
294   }
295
296   return "";
297 }
298
299 bool CVideoDatabaseDirectory::ContainsMovies(const CStdString &path)
300 {
301   VIDEODATABASEDIRECTORY::NODE_TYPE type = GetDirectoryChildType(path);
302   if (type == VIDEODATABASEDIRECTORY::NODE_TYPE_TITLE_MOVIES || type == VIDEODATABASEDIRECTORY::NODE_TYPE_EPISODES || type == VIDEODATABASEDIRECTORY::NODE_TYPE_TITLE_MUSICVIDEOS) return true;
303   return false;
304 }
305
306 bool CVideoDatabaseDirectory::Exists(const char* strPath)
307 {
308   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
309   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
310
311   if (!pNode.get())
312     return false;
313
314   if (pNode->GetChildType() == VIDEODATABASEDIRECTORY::NODE_TYPE_NONE)
315     return false;
316
317   return true;
318 }
319
320 bool CVideoDatabaseDirectory::CanCache(const CStdString& strPath)
321 {
322   CStdString path = CLegacyPathTranslation::TranslateVideoDbPath(strPath);
323   auto_ptr<CDirectoryNode> pNode(CDirectoryNode::ParseURL(path));
324   if (!pNode.get())
325     return false;
326   return pNode->CanCache();
327 }