1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "SmartPlaylistDirectory.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "filesystem/Directory.h"
#include "filesystem/File.h"
#include "filesystem/FileDirectoryFactory.h"
#include "music/MusicDatabase.h"
#include "music/MusicDbUrl.h"
#include "playlists/PlayListTypes.h"
#include "playlists/SmartPlayList.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "utils/SortUtils.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "video/VideoDatabase.h"
#include "video/VideoDbUrl.h"
#include <math.h>
#define PROPERTY_PATH_DB "path.db"
#define PROPERTY_SORT_ORDER "sort.order"
#define PROPERTY_SORT_ASCENDING "sort.ascending"
#define PROPERTY_GROUP_BY "group.by"
#define PROPERTY_GROUP_MIXED "group.mixed"
namespace XFILE
{
CSmartPlaylistDirectory::CSmartPlaylistDirectory() = default;
CSmartPlaylistDirectory::~CSmartPlaylistDirectory() = default;
bool CSmartPlaylistDirectory::GetDirectory(const CURL& url, CFileItemList& items)
{
// Load in the SmartPlaylist and get the WHERE query
CSmartPlaylist playlist;
if (!playlist.Load(url))
return false;
bool result = GetDirectory(playlist, items);
if (result)
items.SetProperty("library.smartplaylist", true);
return result;
}
bool CSmartPlaylistDirectory::GetDirectory(const CSmartPlaylist &playlist, CFileItemList& items, const std::string &strBaseDir /* = "" */, bool filter /* = false */)
{
bool success = false, success2 = false;
std::vector<std::string> virtualFolders;
SortDescription sorting;
if (playlist.GetLimit() > 0)
sorting.limitEnd = playlist.GetLimit();
sorting.sortBy = playlist.GetOrder();
sorting.sortOrder = playlist.GetOrderAscending() ? SortOrderAscending : SortOrderDescending;
sorting.sortAttributes = playlist.GetOrderAttributes();
if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_IGNORETHEWHENSORTING))
sorting.sortAttributes = (SortAttribute)(sorting.sortAttributes | SortAttributeIgnoreArticle);
if (playlist.IsMusicType() && CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
CSettings::SETTING_MUSICLIBRARY_USEARTISTSORTNAME))
sorting.sortAttributes =
static_cast<SortAttribute>(sorting.sortAttributes | SortAttributeUseArtistSortName);
items.SetSortIgnoreFolders((sorting.sortAttributes & SortAttributeIgnoreFolders) ==
SortAttributeIgnoreFolders);
std::string option = !filter ? "xsp" : "filter";
std::string group = playlist.GetGroup();
bool isGrouped = !group.empty() && !StringUtils::EqualsNoCase(group, "none") && !playlist.IsGroupMixed();
// Hint for playlist files like STRM
PLAYLIST::Id playlistTypeHint = PLAYLIST::TYPE_NONE;
// get all virtual folders and add them to the item list
playlist.GetVirtualFolders(virtualFolders);
for (const std::string& virtualFolder : virtualFolders)
{
CFileItemPtr pItem = CFileItemPtr(new CFileItem(virtualFolder, true));
IFileDirectory *dir = CFileDirectoryFactory::Create(pItem->GetURL(), pItem.get());
if (dir != NULL)
{
pItem->SetSpecialSort(SortSpecialOnTop);
items.Add(pItem);
delete dir;
}
}
if (playlist.GetType() == "movies" ||
playlist.GetType() == "tvshows" ||
playlist.GetType() == "episodes")
{
playlistTypeHint = PLAYLIST::TYPE_VIDEO;
CVideoDatabase db;
if (db.Open())
{
MediaType mediaType = CMediaTypes::FromString(playlist.GetType());
std::string baseDir = strBaseDir;
if (strBaseDir.empty())
{
if (mediaType == MediaTypeTvShow || mediaType == MediaTypeEpisode)
baseDir = "videodb://tvshows/";
else if (mediaType == MediaTypeMovie)
baseDir = "videodb://movies/";
else
return false;
if (!isGrouped)
baseDir += "titles";
else
baseDir += group;
URIUtils::AddSlashAtEnd(baseDir);
if (mediaType == MediaTypeEpisode)
baseDir += "-1/-1/";
}
CVideoDbUrl videoUrl;
if (!videoUrl.FromString(baseDir))
return false;
// store the smartplaylist as JSON in the URL as well
std::string xsp;
if (!playlist.IsEmpty(filter))
{
if (!playlist.SaveAsJson(xsp, !filter))
return false;
}
if (!xsp.empty())
videoUrl.AddOption(option, xsp);
else
videoUrl.RemoveOption(option);
CDatabase::Filter dbfilter;
success = db.GetItems(videoUrl.ToString(), items, dbfilter, sorting);
db.Close();
// if we retrieve a list of episodes and we didn't receive
// a pre-defined base path, we need to fix it
if (strBaseDir.empty() && mediaType == MediaTypeEpisode && !isGrouped)
videoUrl.AppendPath("-1/-1/");
items.SetProperty(PROPERTY_PATH_DB, videoUrl.ToString());
}
}
else if (playlist.IsMusicType() || playlist.GetType().empty())
{
playlistTypeHint = PLAYLIST::TYPE_MUSIC;
CMusicDatabase db;
if (db.Open())
{
CSmartPlaylist plist(playlist);
if (playlist.GetType() == "mixed" || playlist.GetType().empty())
plist.SetType("songs");
MediaType mediaType = CMediaTypes::FromString(plist.GetType());
std::string baseDir = strBaseDir;
if (strBaseDir.empty())
{
baseDir = "musicdb://";
if (!isGrouped)
{
if (mediaType == MediaTypeArtist)
baseDir += "artists";
else if (mediaType == MediaTypeAlbum)
baseDir += "albums";
else if (mediaType == MediaTypeSong)
baseDir += "songs";
else
return false;
}
else
baseDir += group;
URIUtils::AddSlashAtEnd(baseDir);
}
CMusicDbUrl musicUrl;
if (!musicUrl.FromString(baseDir))
return false;
// store the smartplaylist as JSON in the URL as well
std::string xsp;
if (!plist.IsEmpty(filter))
{
if (!plist.SaveAsJson(xsp, !filter))
return false;
}
if (!xsp.empty())
musicUrl.AddOption(option, xsp);
else
musicUrl.RemoveOption(option);
CDatabase::Filter dbfilter;
success = db.GetItems(musicUrl.ToString(), items, dbfilter, sorting);
db.Close();
items.SetProperty(PROPERTY_PATH_DB, musicUrl.ToString());
}
}
if (playlist.GetType() == "musicvideos" || playlist.GetType() == "mixed")
{
playlistTypeHint = PLAYLIST::TYPE_VIDEO;
CVideoDatabase db;
if (db.Open())
{
CSmartPlaylist mvidPlaylist(playlist);
if (playlist.GetType() == "mixed")
mvidPlaylist.SetType("musicvideos");
std::string baseDir = strBaseDir;
if (baseDir.empty())
{
baseDir = "videodb://musicvideos/";
if (!isGrouped)
baseDir += "titles";
else
baseDir += group;
URIUtils::AddSlashAtEnd(baseDir);
}
CVideoDbUrl videoUrl;
if (!videoUrl.FromString(baseDir))
return false;
// adjust the group in case we're retrieving a grouped playlist
// based on artists. This is needed because the video library
// is using the actorslink table for artists.
if (isGrouped && group == "artists")
{
group = "actors";
mvidPlaylist.SetGroup(group);
}
// store the smartplaylist as JSON in the URL as well
std::string xsp;
if (!mvidPlaylist.IsEmpty(filter))
{
if (!mvidPlaylist.SaveAsJson(xsp, !filter))
return false;
}
if (!xsp.empty())
videoUrl.AddOption(option, xsp);
else
videoUrl.RemoveOption(option);
CFileItemList items2;
CDatabase::Filter dbfilter;
success2 = db.GetItems(videoUrl.ToString(), items2, dbfilter, sorting);
db.Close();
if (items.Size() <= 0)
items.SetPath(videoUrl.ToString());
items.Append(items2);
if (items2.Size())
{
if (items.Size() > items2.Size())
items.SetContent("mixed");
else
items.SetContent("musicvideos");
}
items.SetProperty(PROPERTY_PATH_DB, videoUrl.ToString());
}
}
items.SetLabel(playlist.GetName());
if (isGrouped)
items.SetContent(group);
else
items.SetContent(playlist.GetType());
items.SetProperty(PROPERTY_SORT_ORDER, (int)playlist.GetOrder());
items.SetProperty(PROPERTY_SORT_ASCENDING, playlist.GetOrderDirection() == SortOrderAscending);
if (!group.empty())
{
items.SetProperty(PROPERTY_GROUP_BY, group);
items.SetProperty(PROPERTY_GROUP_MIXED, playlist.IsGroupMixed());
}
// sort grouped list by label
if (items.Size() > 1 && !group.empty())
items.Sort(SortByLabel, SortOrderAscending, CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_IGNORETHEWHENSORTING) ? SortAttributeIgnoreArticle : SortAttributeNone);
// go through and set the playlist order
for (int i = 0; i < items.Size(); i++)
{
CFileItemPtr item = items[i];
item->m_iprogramCount = i; // hack for playlist order
item->SetProperty("playlist_type_hint", playlistTypeHint);
}
if (playlist.GetType() == "mixed")
return success || success2;
else if (playlist.GetType() == "musicvideos")
return success2;
else
return success;
}
bool CSmartPlaylistDirectory::ContainsFiles(const CURL& url)
{
// smart playlists always have files??
return true;
}
std::string CSmartPlaylistDirectory::GetPlaylistByName(const std::string& name, const std::string& playlistType)
{
CFileItemList list;
bool filesExist = false;
if (CSmartPlaylist::IsMusicType(playlistType))
filesExist = CDirectory::GetDirectory("special://musicplaylists/", list, ".xsp", DIR_FLAG_DEFAULTS);
else // all others are video
filesExist = CDirectory::GetDirectory("special://videoplaylists/", list, ".xsp", DIR_FLAG_DEFAULTS);
if (filesExist)
{
for (int i = 0; i < list.Size(); i++)
{
CFileItemPtr item = list[i];
CSmartPlaylist playlist;
if (playlist.OpenAndReadName(item->GetURL()))
{
if (StringUtils::EqualsNoCase(playlist.GetName(), name))
return item->GetPath();
}
}
for (int i = 0; i < list.Size(); i++)
{ // check based on filename
CFileItemPtr item = list[i];
if (URIUtils::GetFileName(item->GetPath()) == name)
{ // found :)
return item->GetPath();
}
}
}
return "";
}
bool CSmartPlaylistDirectory::Remove(const CURL& url)
{
return XFILE::CFile::Delete(url);
}
}
|