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