Merge branch 'container_foldername'
[vuplus_xbmc] / xbmc / filesystem / SmartPlaylistDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2008 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, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "SmartPlaylistDirectory.h"
23 #include "utils/log.h"
24 #include "playlists/SmartPlayList.h"
25 #include "music/MusicDatabase.h"
26 #include "video/VideoDatabase.h"
27 #include "Directory.h"
28 #include "File.h"
29 #include "FileItem.h"
30
31 namespace XFILE
32 {
33   CSmartPlaylistDirectory::CSmartPlaylistDirectory()
34   {
35   }
36
37   CSmartPlaylistDirectory::~CSmartPlaylistDirectory()
38   {
39   }
40
41   bool CSmartPlaylistDirectory::GetDirectory(const CStdString& strPath, CFileItemList& items)
42   {
43     // Load in the SmartPlaylist and get the WHERE query
44     CSmartPlaylist playlist;
45     if (!playlist.Load(strPath))
46       return false;
47     bool success = false, success2 = false;
48     if (playlist.GetType().Equals("tvshows"))
49     {
50       CVideoDatabase db;
51       db.Open();
52       CStdString whereOrder = playlist.GetWhereClause(db) + " " + playlist.GetOrderClause(db);
53       success = db.GetTvShowsByWhere("videodb://2/2/", whereOrder, items);
54       items.SetContent("tvshows");
55       db.Close();
56     }
57     else if (playlist.GetType().Equals("episodes"))
58     {
59       CVideoDatabase db;
60       db.Open();
61       CStdString whereOrder = playlist.GetWhereClause(db) + " " + playlist.GetOrderClause(db);
62       success = db.GetEpisodesByWhere("videodb://2/2/", whereOrder, items);
63       items.SetContent("episodes");
64       db.Close();
65     }
66     else if (playlist.GetType().Equals("movies"))
67     {
68       CVideoDatabase db;
69       db.Open();
70       success = db.GetMoviesByWhere("videodb://1/2/", playlist.GetWhereClause(db), playlist.GetOrderClause(db), items, true);
71       items.SetContent("movies");
72       db.Close();
73     }
74     else if (playlist.GetType().Equals("albums"))
75     {
76       CMusicDatabase db;
77       db.Open();
78       success = db.GetAlbumsByWhere("musicdb://3/", playlist.GetWhereClause(db), playlist.GetOrderClause(db), items);
79       items.SetContent("albums");
80       db.Close();
81     }
82     if (playlist.GetType().Equals("songs") || playlist.GetType().Equals("mixed") || playlist.GetType().IsEmpty())
83     {
84       CMusicDatabase db;
85       db.Open();
86       CStdString type=playlist.GetType();
87       if (type.IsEmpty())
88         type = "songs";
89       if (playlist.GetType().Equals("mixed"))
90         playlist.SetType("songs");
91
92       CStdString whereOrder = playlist.GetWhereClause(db) + " " + playlist.GetOrderClause(db);
93       success = db.GetSongsByWhere("", whereOrder, items);
94       items.SetContent("songs");
95       db.Close();
96       playlist.SetType(type);
97     }
98     if (playlist.GetType().Equals("musicvideos") || playlist.GetType().Equals("mixed"))
99     {
100       CVideoDatabase db;
101       db.Open();
102       CStdString type=playlist.GetType();
103       if (playlist.GetType().Equals("mixed"))
104         playlist.SetType("musicvideos");
105       CStdString whereOrder = playlist.GetWhereClause(db) + " " + playlist.GetOrderClause(db);
106       CFileItemList items2;
107       success2 = db.GetMusicVideosByWhere("videodb://3/2/", whereOrder, items2, false); // TODO: SMARTPLAYLISTS Don't check locks???
108       db.Close();
109       items.Append(items2);
110       if (items2.Size())
111         items.SetContent("musicvideos");
112       playlist.SetType(type);
113     }
114     items.SetLabel(playlist.GetName());
115     // go through and set the playlist order
116     for (int i = 0; i < items.Size(); i++)
117     {
118       CFileItemPtr item = items[i];
119       item->m_iprogramCount = i;  // hack for playlist order
120     }
121     if (playlist.GetType().Equals("mixed"))
122       return success || success2;
123     else if (playlist.GetType().Equals("musicvideos"))
124       return success2;
125     else
126       return success;
127   }
128
129   bool CSmartPlaylistDirectory::ContainsFiles(const CStdString& strPath)
130   {
131     // smart playlists always have files??
132     return true;
133   }
134
135   CStdString CSmartPlaylistDirectory::GetPlaylistByName(const CStdString& name, const CStdString& playlistType)
136   {
137     CFileItemList list;
138     bool filesExist = false;
139     if (playlistType == "songs" || playlistType == "albums")
140       filesExist = CDirectory::GetDirectory("special://musicplaylists/", list, "*.xsp");
141     else // all others are video
142       filesExist = CDirectory::GetDirectory("special://videoplaylists/", list, "*.xsp");
143     if (filesExist)
144     {
145       for (int i = 0; i < list.Size(); i++)
146       {
147         CFileItemPtr item = list[i];
148         if (item->GetLabel().CompareNoCase(name) == 0)
149         { // found :)
150           return item->m_strPath;
151         }
152       }
153     }
154     return "";
155   }
156
157   bool CSmartPlaylistDirectory::Remove(const char *strPath)
158   {
159     return XFILE::CFile::Delete(strPath);
160   }
161 }
162
163
164