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