Debian Codegraph @

xbmc/playlists/SmartPlayList.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "SmartPlayList.h"
10
11 #include "ServiceBroker.h"
12 #include "Util.h"
13 #include "dbwrappers/Database.h"
14 #include "filesystem/File.h"
15 #include "filesystem/SmartPlaylistDirectory.h"
16 #include "guilib/LocalizeStrings.h"
17 #include "settings/Settings.h"
18 #include "settings/SettingsComponent.h"
19 #include "utils/DatabaseUtils.h"
20 #include "utils/JSONVariantParser.h"
21 #include "utils/JSONVariantWriter.h"
22 #include "utils/StreamDetails.h"
23 #include "utils/StringUtils.h"
24 #include "utils/StringValidation.h"
25 #include "utils/URIUtils.h"
26 #include "utils/Variant.h"
27 #include "utils/XMLUtils.h"
28 #include "utils/log.h"
29
30 #include <cstdlib>
31 #include <memory>
32 #include <set>
33 #include <string>
34 #include <vector>
35
36 using namespace XFILE;
37
38 typedef struct
39 {
40 char string[17];
41 Field field;
42 CDatabaseQueryRule::FIELD_TYPE type;
43 StringValidation::Validator validator;
44 bool browseable;
45 int localizedString;
46 } translateField;
47
48 // clang-format off
49 static const translateField fields[] = {
50 { "none", FieldNone, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 231 },
51 { "filename", FieldFilename, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 561 },
52 { "path", FieldPath, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 573 },
53 { "album", FieldAlbum, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 558 },
54 { "albumartist", FieldAlbumArtist, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 566 },
55 { "artist", FieldArtist, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 557 },
56 { "tracknumber", FieldTrackNumber, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 554 },
57 { "role", FieldRole, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 38033 },
58 { "comment", FieldComment, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 569 },
59 { "review", FieldReview, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 183 },
60 { "themes", FieldThemes, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21895 },
61 { "moods", FieldMoods, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 175 },
62 { "styles", FieldStyles, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 176 },
63 { "type", FieldAlbumType, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 564 },
64 { "compilation", FieldCompilation, CDatabaseQueryRule::BOOLEAN_FIELD, NULL, false, 204 },
65 { "label", FieldMusicLabel, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21899 },
66 { "title", FieldTitle, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 556 },
67 { "sorttitle", FieldSortTitle, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 171 },
68 { "originaltitle", FieldOriginalTitle, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 20376 },
69 { "year", FieldYear, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, true, 562 },
70 { "time", FieldTime, CDatabaseQueryRule::SECONDS_FIELD, StringValidation::IsTime, false, 180 },
71 { "playcount", FieldPlaycount, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 567 },
72 { "lastplayed", FieldLastPlayed, CDatabaseQueryRule::DATE_FIELD, NULL, false, 568 },
73 { "inprogress", FieldInProgress, CDatabaseQueryRule::BOOLEAN_FIELD, NULL, false, 575 },
74 { "rating", FieldRating, CDatabaseQueryRule::REAL_FIELD, CSmartPlaylistRule::ValidateRating, false, 563 },
75 { "userrating", FieldUserRating, CDatabaseQueryRule::REAL_FIELD, CSmartPlaylistRule::ValidateMyRating, false, 38018 },
76 { "votes", FieldVotes, CDatabaseQueryRule::REAL_FIELD, StringValidation::IsPositiveInteger, false, 205 },
77 { "top250", FieldTop250, CDatabaseQueryRule::NUMERIC_FIELD, NULL, false, 13409 },
78 { "mpaarating", FieldMPAA, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 20074 },
79 { "dateadded", FieldDateAdded, CDatabaseQueryRule::DATE_FIELD, NULL, false, 570 },
80 { "datemodified", FieldDateModified, CDatabaseQueryRule::DATE_FIELD, NULL, false, 39119 },
81 { "datenew", FieldDateNew, CDatabaseQueryRule::DATE_FIELD, NULL, false, 21877 },
82 { "genre", FieldGenre, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 515 },
83 { "plot", FieldPlot, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 207 },
84 { "plotoutline", FieldPlotOutline, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 203 },
85 { "tagline", FieldTagline, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 202 },
86 { "set", FieldSet, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20457 },
87 { "director", FieldDirector, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20339 },
88 { "actor", FieldActor, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20337 },
89 { "writers", FieldWriter, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20417 },
90 { "airdate", FieldAirDate, CDatabaseQueryRule::DATE_FIELD, NULL, false, 20416 },
91 { "hastrailer", FieldTrailer, CDatabaseQueryRule::BOOLEAN_FIELD, NULL, false, 20423 },
92 { "studio", FieldStudio, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 572 },
93 { "country", FieldCountry, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 574 },
94 { "tvshow", FieldTvShowTitle, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20364 },
95 { "status", FieldTvShowStatus, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 126 },
96 { "season", FieldSeason, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 20373 },
97 { "episode", FieldEpisodeNumber, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 20359 },
98 { "numepisodes", FieldNumberOfEpisodes, CDatabaseQueryRule::REAL_FIELD, StringValidation::IsPositiveInteger, false, 20360 },
99 { "numwatched", FieldNumberOfWatchedEpisodes, CDatabaseQueryRule::REAL_FIELD, StringValidation::IsPositiveInteger, false, 21457 },
100 { "videoresolution", FieldVideoResolution, CDatabaseQueryRule::REAL_FIELD, NULL, false, 21443 },
101 { "videocodec", FieldVideoCodec, CDatabaseQueryRule::TEXTIN_FIELD, NULL, false, 21445 },
102 { "videoaspect", FieldVideoAspectRatio, CDatabaseQueryRule::REAL_FIELD, NULL, false, 21374 },
103 { "audiochannels", FieldAudioChannels, CDatabaseQueryRule::REAL_FIELD, NULL, false, 21444 },
104 { "audiocodec", FieldAudioCodec, CDatabaseQueryRule::TEXTIN_FIELD, NULL, false, 21446 },
105 { "audiolanguage", FieldAudioLanguage, CDatabaseQueryRule::TEXTIN_FIELD, NULL, false, 21447 },
106 { "audiocount", FieldAudioCount, CDatabaseQueryRule::REAL_FIELD, StringValidation::IsPositiveInteger, false, 21481 },
107 { "subtitlecount", FieldSubtitleCount, CDatabaseQueryRule::REAL_FIELD, StringValidation::IsPositiveInteger, false, 21482 },
108 { "subtitlelanguage", FieldSubtitleLanguage, CDatabaseQueryRule::TEXTIN_FIELD, NULL, false, 21448 },
109 { "random", FieldRandom, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 590 },
110 { "playlist", FieldPlaylist, CDatabaseQueryRule::PLAYLIST_FIELD, NULL, true, 559 },
111 { "virtualfolder", FieldVirtualFolder, CDatabaseQueryRule::PLAYLIST_FIELD, NULL, true, 614 },
112 { "tag", FieldTag, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 20459 },
113 { "instruments", FieldInstruments, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21892 },
114 { "biography", FieldBiography, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21887 },
115 { "born", FieldBorn, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21893 },
116 { "bandformed", FieldBandFormed, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21894 },
117 { "disbanded", FieldDisbanded, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21896 },
118 { "died", FieldDied, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 21897 },
119 { "artisttype", FieldArtistType, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 564 },
120 { "gender", FieldGender, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 39025 },
121 { "disambiguation", FieldDisambiguation, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 39026 },
122 { "source", FieldSource, CDatabaseQueryRule::TEXT_FIELD, NULL, true, 39030 },
123 { "disctitle", FieldDiscTitle, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 38076 },
124 { "isboxset", FieldIsBoxset, CDatabaseQueryRule::BOOLEAN_FIELD, NULL, false, 38074 },
125 { "totaldiscs", FieldTotalDiscs, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 38077 },
126 { "originalyear", FieldOrigYear, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, true, 38078 },
127 { "bpm", FieldBPM, CDatabaseQueryRule::NUMERIC_FIELD, NULL, false, 38080 },
128 { "samplerate", FieldSampleRate, CDatabaseQueryRule::NUMERIC_FIELD, NULL, false, 613 },
129 { "bitrate", FieldMusicBitRate, CDatabaseQueryRule::NUMERIC_FIELD, NULL, false, 623 },
130 { "channels", FieldNoOfChannels, CDatabaseQueryRule::NUMERIC_FIELD, StringValidation::IsPositiveInteger, false, 253 },
131 { "albumstatus", FieldAlbumStatus, CDatabaseQueryRule::TEXT_FIELD, NULL, false, 38081 },
132 { "albumduration", FieldAlbumDuration, CDatabaseQueryRule::SECONDS_FIELD, StringValidation::IsTime, false, 180 },
133 { "hdrtype", FieldHdrType, CDatabaseQueryRule::TEXTIN_FIELD, NULL, false, 20474 },
134 };
135 // clang-format on
136
137 typedef struct
138 {
139 std::string name;
140 Field field;
141 bool canMix;
142 int localizedString;
143 } group;
144
145 // clang-format off
146 static const group groups[] = { { "", FieldUnknown, false, 571 },
147 { "none", FieldNone, false, 231 },
148 { "sets", FieldSet, true, 20434 },
149 { "genres", FieldGenre, false, 135 },
150 { "years", FieldYear, false, 652 },
151 { "actors", FieldActor, false, 344 },
152 { "directors", FieldDirector, false, 20348 },
153 { "writers", FieldWriter, false, 20418 },
154 { "studios", FieldStudio, false, 20388 },
155 { "countries", FieldCountry, false, 20451 },
156 { "artists", FieldArtist, false, 133 },
157 { "albums", FieldAlbum, false, 132 },
158 { "tags", FieldTag, false, 20459 },
159 { "originalyears", FieldOrigYear, false, 38078 },
160 };
161 // clang-format on
162
163 #define RULE_VALUE_SEPARATOR " / "
164
165 CSmartPlaylistRule::CSmartPlaylistRule() = default;
166
167 int CSmartPlaylistRule::TranslateField(const char *field) const
168 {
169 for (const translateField& f : fields)
170 if (StringUtils::EqualsNoCase(field, f.string)) return f.field;
171 return FieldNone;
172 }
173
174 std::string CSmartPlaylistRule::TranslateField(int field) const
175 {
176 for (const translateField& f : fields)
177 if (field == f.field) return f.string;
178 return "none";
179 }
180
181 SortBy CSmartPlaylistRule::TranslateOrder(const char *order)
182 {
183 return SortUtils::SortMethodFromString(order);
184 }
185
186 std::string CSmartPlaylistRule::TranslateOrder(SortBy order)
187 {
188 std::string sortOrder = SortUtils::SortMethodToString(order);
189 if (sortOrder.empty())
190 return "none";
191
192 return sortOrder;
193 }
194
195 Field CSmartPlaylistRule::TranslateGroup(const char *group)
196 {
197 for (const auto & i : groups)
198 {
199 if (StringUtils::EqualsNoCase(group, i.name))
200 return i.field;
201 }
202
203 return FieldUnknown;
204 }
205
206 std::string CSmartPlaylistRule::TranslateGroup(Field group)
207 {
208 for (const auto & i : groups)
209 {
210 if (group == i.field)
211 return i.name;
212 }
213
214 return "";
215 }
216
217 std::string CSmartPlaylistRule::GetLocalizedField(int field)
218 {
219 for (const translateField& f : fields)
220 if (field == f.field) return g_localizeStrings.Get(f.localizedString);
221 return g_localizeStrings.Get(16018);
222 }
223
224 CDatabaseQueryRule::FIELD_TYPE CSmartPlaylistRule::GetFieldType(int field) const
225 {
226 for (const translateField& f : fields)
227 if (field == f.field) return f.type;
228 return TEXT_FIELD;
229 }
230
231 bool CSmartPlaylistRule::IsFieldBrowseable(int field)
232 {
233 for (const translateField& f : fields)
234 if (field == f.field) return f.browseable;
235
236 return false;
237 }
238
239 bool CSmartPlaylistRule::Validate(const std::string &input, void *data)
240 {
241 if (data == NULL)
242 return true;
243
244 CSmartPlaylistRule *rule = static_cast<CSmartPlaylistRule*>(data);
245
246 // check if there's a validator for this rule
247 StringValidation::Validator validator = NULL;
248 for (const translateField& field : fields)
249 {
250 if (rule->m_field == field.field)
251 {
252 validator = field.validator;
253 break;
254 }
255 }
256 if (validator == NULL)
257 return true;
258
259 // split the input into multiple values and validate every value separately
260 std::vector<std::string> values = StringUtils::Split(input, RULE_VALUE_SEPARATOR);
261 for (std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
262 {
263 if (!validator(*it, data))
264 return false;
265 }
266
267 return true;
268 }
269
270 bool CSmartPlaylistRule::ValidateRating(const std::string &input, void *data)
271 {
272 char *end = NULL;
273 std::string strRating = input;
274 StringUtils::Trim(strRating);
275
276 double rating = std::strtod(strRating.c_str(), &end);
277 return (end == NULL || *end == '\0') &&
278 rating >= 0.0 && rating <= 10.0;
279 }
280
281 bool CSmartPlaylistRule::ValidateMyRating(const std::string &input, void *data)
282 {
283 std::string strRating = input;
284 StringUtils::Trim(strRating);
285
286 int rating = atoi(strRating.c_str());
287 return StringValidation::IsPositiveInteger(input, data) && rating <= 10;
288 }
289
290 std::vector<Field> CSmartPlaylistRule::GetFields(const std::string &type)
291 {
292 std::vector<Field> fields;
293 bool isVideo = false;
294 if (type == "mixed")
295 {
296 fields.push_back(FieldGenre);
297 fields.push_back(FieldAlbum);
298 fields.push_back(FieldArtist);
299 fields.push_back(FieldAlbumArtist);
300 fields.push_back(FieldTitle);
301 fields.push_back(FieldOriginalTitle);
302 fields.push_back(FieldYear);
303 fields.push_back(FieldTime);
304 fields.push_back(FieldTrackNumber);
305 fields.push_back(FieldFilename);
306 fields.push_back(FieldPath);
307 fields.push_back(FieldPlaycount);
308 fields.push_back(FieldLastPlayed);
309 }
310 else if (type == "songs")
311 {
312 fields.push_back(FieldGenre);
313 fields.push_back(FieldSource);
314 fields.push_back(FieldAlbum);
315 fields.push_back(FieldDiscTitle);
316 fields.push_back(FieldArtist);
317 fields.push_back(FieldAlbumArtist);
318 fields.push_back(FieldTitle);
319 fields.push_back(FieldYear);
320 if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
321 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
322 fields.push_back(FieldOrigYear);
323 fields.push_back(FieldTime);
324 fields.push_back(FieldTrackNumber);
325 fields.push_back(FieldFilename);
326 fields.push_back(FieldPath);
327 fields.push_back(FieldPlaycount);
328 fields.push_back(FieldLastPlayed);
329 fields.push_back(FieldRating);
330 fields.push_back(FieldUserRating);
331 fields.push_back(FieldComment);
332 fields.push_back(FieldMoods);
333 fields.push_back(FieldBPM);
334 fields.push_back(FieldSampleRate);
335 fields.push_back(FieldMusicBitRate);
336 fields.push_back(FieldNoOfChannels);
337 fields.push_back(FieldDateAdded);
338 fields.push_back(FieldDateModified);
339 fields.push_back(FieldDateNew);
340 }
341 else if (type == "albums")
342 {
343 fields.push_back(FieldGenre);
344 fields.push_back(FieldSource);
345 fields.push_back(FieldAlbum);
346 fields.push_back(FieldDiscTitle);
347 fields.push_back(FieldTotalDiscs);
348 fields.push_back(FieldIsBoxset);
349 fields.push_back(FieldArtist); // any artist
350 fields.push_back(FieldAlbumArtist); // album artist
351 fields.push_back(FieldYear);
352 if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
353 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
354 fields.push_back(FieldOrigYear);
355 fields.push_back(FieldAlbumDuration);
356 fields.push_back(FieldReview);
357 fields.push_back(FieldThemes);
358 fields.push_back(FieldMoods);
359 fields.push_back(FieldStyles);
360 fields.push_back(FieldCompilation);
361 fields.push_back(FieldAlbumType);
362 fields.push_back(FieldMusicLabel);
363 fields.push_back(FieldRating);
364 fields.push_back(FieldUserRating);
365 fields.push_back(FieldPlaycount);
366 fields.push_back(FieldLastPlayed);
367 fields.push_back(FieldPath);
368 fields.push_back(FieldAlbumStatus);
369 fields.push_back(FieldDateAdded);
370 fields.push_back(FieldDateModified);
371 fields.push_back(FieldDateNew);
372 }
373 else if (type == "artists")
374 {
375 fields.push_back(FieldArtist);
376 fields.push_back(FieldSource);
377 fields.push_back(FieldGenre);
378 fields.push_back(FieldMoods);
379 fields.push_back(FieldStyles);
380 fields.push_back(FieldInstruments);
381 fields.push_back(FieldBiography);
382 fields.push_back(FieldArtistType);
383 fields.push_back(FieldGender);
384 fields.push_back(FieldDisambiguation);
385 fields.push_back(FieldBorn);
386 fields.push_back(FieldBandFormed);
387 fields.push_back(FieldDisbanded);
388 fields.push_back(FieldDied);
389 fields.push_back(FieldRole);
390 fields.push_back(FieldPath);
391 fields.push_back(FieldDateAdded);
392 fields.push_back(FieldDateModified);
393 fields.push_back(FieldDateNew);
394 }
395 else if (type == "tvshows")
396 {
397 fields.push_back(FieldTitle);
398 fields.push_back(FieldOriginalTitle);
399 fields.push_back(FieldPlot);
400 fields.push_back(FieldTvShowStatus);
401 fields.push_back(FieldVotes);
402 fields.push_back(FieldRating);
403 fields.push_back(FieldUserRating);
404 fields.push_back(FieldYear);
405 fields.push_back(FieldGenre);
406 fields.push_back(FieldDirector);
407 fields.push_back(FieldActor);
408 fields.push_back(FieldNumberOfEpisodes);
409 fields.push_back(FieldNumberOfWatchedEpisodes);
410 fields.push_back(FieldPlaycount);
411 fields.push_back(FieldPath);
412 fields.push_back(FieldStudio);
413 fields.push_back(FieldMPAA);
414 fields.push_back(FieldDateAdded);
415 fields.push_back(FieldLastPlayed);
416 fields.push_back(FieldInProgress);
417 fields.push_back(FieldTag);
418 }
419 else if (type == "episodes")
420 {
421 fields.push_back(FieldTitle);
422 fields.push_back(FieldTvShowTitle);
423 fields.push_back(FieldOriginalTitle);
424 fields.push_back(FieldPlot);
425 fields.push_back(FieldVotes);
426 fields.push_back(FieldRating);
427 fields.push_back(FieldUserRating);
428 fields.push_back(FieldTime);
429 fields.push_back(FieldWriter);
430 fields.push_back(FieldAirDate);
431 fields.push_back(FieldPlaycount);
432 fields.push_back(FieldLastPlayed);
433 fields.push_back(FieldInProgress);
434 fields.push_back(FieldGenre);
435 fields.push_back(FieldYear); // premiered
436 fields.push_back(FieldDirector);
437 fields.push_back(FieldActor);
438 fields.push_back(FieldEpisodeNumber);
439 fields.push_back(FieldSeason);
440 fields.push_back(FieldFilename);
441 fields.push_back(FieldPath);
442 fields.push_back(FieldStudio);
443 fields.push_back(FieldMPAA);
444 fields.push_back(FieldDateAdded);
445 fields.push_back(FieldTag);
446 isVideo = true;
447 }
448 else if (type == "movies")
449 {
450 fields.push_back(FieldTitle);
451 fields.push_back(FieldOriginalTitle);
452 fields.push_back(FieldPlot);
453 fields.push_back(FieldPlotOutline);
454 fields.push_back(FieldTagline);
455 fields.push_back(FieldVotes);
456 fields.push_back(FieldRating);
457 fields.push_back(FieldUserRating);
458 fields.push_back(FieldTime);
459 fields.push_back(FieldWriter);
460 fields.push_back(FieldPlaycount);
461 fields.push_back(FieldLastPlayed);
462 fields.push_back(FieldInProgress);
463 fields.push_back(FieldGenre);
464 fields.push_back(FieldCountry);
465 fields.push_back(FieldYear); // premiered
466 fields.push_back(FieldDirector);
467 fields.push_back(FieldActor);
468 fields.push_back(FieldMPAA);
469 fields.push_back(FieldTop250);
470 fields.push_back(FieldStudio);
471 fields.push_back(FieldTrailer);
472 fields.push_back(FieldFilename);
473 fields.push_back(FieldPath);
474 fields.push_back(FieldSet);
475 fields.push_back(FieldTag);
476 fields.push_back(FieldDateAdded);
477 isVideo = true;
478 }
479 else if (type == "musicvideos")
480 {
481 fields.push_back(FieldTitle);
482 fields.push_back(FieldGenre);
483 fields.push_back(FieldAlbum);
484 fields.push_back(FieldYear);
485 fields.push_back(FieldArtist);
486 fields.push_back(FieldFilename);
487 fields.push_back(FieldPath);
488 fields.push_back(FieldPlaycount);
489 fields.push_back(FieldLastPlayed);
490 fields.push_back(FieldRating);
491 fields.push_back(FieldUserRating);
492 fields.push_back(FieldTime);
493 fields.push_back(FieldDirector);
494 fields.push_back(FieldStudio);
495 fields.push_back(FieldPlot);
496 fields.push_back(FieldTag);
497 fields.push_back(FieldDateAdded);
498 isVideo = true;
499 }
500 if (isVideo)
501 {
502 fields.push_back(FieldVideoResolution);
503 fields.push_back(FieldAudioChannels);
504 fields.push_back(FieldAudioCount);
505 fields.push_back(FieldSubtitleCount);
506 fields.push_back(FieldVideoCodec);
507 fields.push_back(FieldAudioCodec);
508 fields.push_back(FieldAudioLanguage);
509 fields.push_back(FieldSubtitleLanguage);
510 fields.push_back(FieldVideoAspectRatio);
511 fields.push_back(FieldHdrType);
512 }
513 fields.push_back(FieldPlaylist);
514 fields.push_back(FieldVirtualFolder);
515
516 return fields;
517 }
518
519 std::vector<SortBy> CSmartPlaylistRule::GetOrders(const std::string &type)
520 {
521 std::vector<SortBy> orders;
522 orders.push_back(SortByNone);
523 if (type == "mixed")
524 {
525 orders.push_back(SortByGenre);
526 orders.push_back(SortByAlbum);
527 orders.push_back(SortByArtist);
528 orders.push_back(SortByTitle);
529 orders.push_back(SortByYear);
530 orders.push_back(SortByTime);
531 orders.push_back(SortByTrackNumber);
532 orders.push_back(SortByFile);
533 orders.push_back(SortByPath);
534 orders.push_back(SortByPlaycount);
535 orders.push_back(SortByLastPlayed);
536 }
537 else if (type == "songs")
538 {
539 orders.push_back(SortByGenre);
540 orders.push_back(SortByAlbum);
541 orders.push_back(SortByArtist);
542 orders.push_back(SortByTitle);
543 orders.push_back(SortByYear);
544 if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
545 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
546 orders.push_back(SortByOrigDate);
547 orders.push_back(SortByTime);
548 orders.push_back(SortByTrackNumber);
549 orders.push_back(SortByFile);
550 orders.push_back(SortByPath);
551 orders.push_back(SortByPlaycount);
552 orders.push_back(SortByLastPlayed);
553 orders.push_back(SortByDateAdded);
554 orders.push_back(SortByRating);
555 orders.push_back(SortByUserRating);
556 orders.push_back(SortByBPM);
557 }
558 else if (type == "albums")
559 {
560 orders.push_back(SortByGenre);
561 orders.push_back(SortByAlbum);
562 orders.push_back(SortByTotalDiscs);
563 orders.push_back(SortByArtist); // any artist
564 orders.push_back(SortByYear);
565 if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
566 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
567 orders.push_back(SortByOrigDate);
568 //orders.push_back(SortByThemes);
569 //orders.push_back(SortByMoods);
570 //orders.push_back(SortByStyles);
571 orders.push_back(SortByAlbumType);
572 //orders.push_back(SortByMusicLabel);
573 orders.push_back(SortByRating);
574 orders.push_back(SortByUserRating);
575 orders.push_back(SortByPlaycount);
576 orders.push_back(SortByLastPlayed);
577 orders.push_back(SortByDateAdded);
578 }
579 else if (type == "artists")
580 {
581 orders.push_back(SortByArtist);
582 }
583 else if (type == "tvshows")
584 {
585 orders.push_back(SortBySortTitle);
586 orders.push_back(SortByOriginalTitle);
587 orders.push_back(SortByTvShowStatus);
588 orders.push_back(SortByVotes);
589 orders.push_back(SortByRating);
590 orders.push_back(SortByUserRating);
591 orders.push_back(SortByYear);
592 orders.push_back(SortByGenre);
593 orders.push_back(SortByNumberOfEpisodes);
594 orders.push_back(SortByNumberOfWatchedEpisodes);
595 //orders.push_back(SortByPlaycount);
596 orders.push_back(SortByPath);
597 orders.push_back(SortByStudio);
598 orders.push_back(SortByMPAA);
599 orders.push_back(SortByDateAdded);
600 orders.push_back(SortByLastPlayed);
601 }
602 else if (type == "episodes")
603 {
604 orders.push_back(SortByTitle);
605 orders.push_back(SortByOriginalTitle);
606 orders.push_back(SortByTvShowTitle);
607 orders.push_back(SortByVotes);
608 orders.push_back(SortByRating);
609 orders.push_back(SortByUserRating);
610 orders.push_back(SortByTime);
611 orders.push_back(SortByPlaycount);
612 orders.push_back(SortByLastPlayed);
613 orders.push_back(SortByYear); // premiered/dateaired
614 orders.push_back(SortByEpisodeNumber);
615 orders.push_back(SortBySeason);
616 orders.push_back(SortByFile);
617 orders.push_back(SortByPath);
618 orders.push_back(SortByStudio);
619 orders.push_back(SortByMPAA);
620 orders.push_back(SortByDateAdded);
621 }
622 else if (type == "movies")
623 {
624 orders.push_back(SortBySortTitle);
625 orders.push_back(SortByOriginalTitle);
626 orders.push_back(SortByVotes);
627 orders.push_back(SortByRating);
628 orders.push_back(SortByUserRating);
629 orders.push_back(SortByTime);
630 orders.push_back(SortByPlaycount);
631 orders.push_back(SortByLastPlayed);
632 orders.push_back(SortByGenre);
633 orders.push_back(SortByCountry);
634 orders.push_back(SortByYear); // premiered
635 orders.push_back(SortByMPAA);
636 orders.push_back(SortByTop250);
637 orders.push_back(SortByStudio);
638 orders.push_back(SortByFile);
639 orders.push_back(SortByPath);
640 orders.push_back(SortByDateAdded);
641 }
642 else if (type == "musicvideos")
643 {
644 orders.push_back(SortByTitle);
645 orders.push_back(SortByGenre);
646 orders.push_back(SortByAlbum);
647 orders.push_back(SortByYear);
648 orders.push_back(SortByArtist);
649 orders.push_back(SortByFile);
650 orders.push_back(SortByPath);
651 orders.push_back(SortByPlaycount);
652 orders.push_back(SortByLastPlayed);
653 orders.push_back(SortByTime);
654 orders.push_back(SortByRating);
655 orders.push_back(SortByUserRating);
656 orders.push_back(SortByStudio);
657 orders.push_back(SortByDateAdded);
658 }
659 orders.push_back(SortByRandom);
660
661 return orders;
662 }
663
664 std::vector<Field> CSmartPlaylistRule::GetGroups(const std::string &type)
665 {
666 std::vector<Field> groups;
667 groups.push_back(FieldUnknown);
668
669 if (type == "artists")
670 groups.push_back(FieldGenre);
671 else if (type == "albums")
672 {
673 groups.push_back(FieldYear);
674 if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
675 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
676 groups.push_back(FieldOrigYear);
677 }
678 if (type == "movies")
679 {
680 groups.push_back(FieldNone);
681 groups.push_back(FieldSet);
682 groups.push_back(FieldGenre);
683 groups.push_back(FieldYear);
684 groups.push_back(FieldActor);
685 groups.push_back(FieldDirector);
686 groups.push_back(FieldWriter);
687 groups.push_back(FieldStudio);
688 groups.push_back(FieldCountry);
689 groups.push_back(FieldTag);
690 }
691 else if (type == "tvshows")
692 {
693 groups.push_back(FieldGenre);
694 groups.push_back(FieldYear);
695 groups.push_back(FieldActor);
696 groups.push_back(FieldDirector);
697 groups.push_back(FieldStudio);
698 groups.push_back(FieldTag);
699 }
700 else if (type == "musicvideos")
701 {
702 groups.push_back(FieldArtist);
703 groups.push_back(FieldAlbum);
704 groups.push_back(FieldGenre);
705 groups.push_back(FieldYear);
706 groups.push_back(FieldDirector);
707 groups.push_back(FieldStudio);
708 groups.push_back(FieldTag);
709 }
710
711 return groups;
712 }
713
714 std::string CSmartPlaylistRule::GetLocalizedGroup(Field group)
715 {
716 for (const auto & i : groups)
717 {
718 if (group == i.field)
719 return g_localizeStrings.Get(i.localizedString);
720 }
721
722 return g_localizeStrings.Get(groups[0].localizedString);
723 }
724
725 bool CSmartPlaylistRule::CanGroupMix(Field group)
726 {
727 for (const auto & i : groups)
728 {
729 if (group == i.field)
730 return i.canMix;
731 }
732
733 return false;
734 }
735
736 std::string CSmartPlaylistRule::GetLocalizedRule() const
737 {
738 return StringUtils::Format("{} {} {}", GetLocalizedField(m_field),
739 GetLocalizedOperator(m_operator), GetParameter());
740 }
741
742 std::string CSmartPlaylistRule::GetVideoResolutionQuery(const std::string &parameter) const
743 {
744 std::string retVal(" IN (SELECT DISTINCT idFile FROM streamdetails WHERE iVideoWidth ");
745 int iRes = (int)std::strtol(parameter.c_str(), NULL, 10);
746
747 int min, max;
748 if (iRes >= 2160)
749 {
750 min = 1921;
751 max = INT_MAX;
752 }
753 else if (iRes >= 1080) { min = 1281; max = 1920; }
754 else if (iRes >= 720) { min = 961; max = 1280; }
755 else if (iRes >= 540) { min = 721; max = 960; }
756 else { min = 0; max = 720; }
757
758 switch (m_operator)
759 {
760 case OPERATOR_EQUALS:
761 retVal += StringUtils::Format(">= {} AND iVideoWidth <= {}", min, max);
762 break;
763 case OPERATOR_DOES_NOT_EQUAL:
764 retVal += StringUtils::Format("< {} OR iVideoWidth > {}", min, max);
765 break;
766 case OPERATOR_LESS_THAN:
767 retVal += StringUtils::Format("< {}", min);
768 break;
769 case OPERATOR_GREATER_THAN:
770 retVal += StringUtils::Format("> {}", max);
771 break;
772 default:
773 break;
774 }
775
776 retVal += ")";
777 return retVal;
778 }
779
780 std::string CSmartPlaylistRule::GetBooleanQuery(const std::string &negate, const std::string &strType) const
781 {
782 if (strType == "movies")
783 {
784 if (m_field == FieldInProgress)
785 return "movie_view.idFile " + negate + " IN (SELECT DISTINCT idFile FROM bookmark WHERE type = 1)";
786 else if (m_field == FieldTrailer)
787 return negate + GetField(m_field, strType) + "!= ''";
788 }
789 else if (strType == "episodes")
790 {
791 if (m_field == FieldInProgress)
792 return "episode_view.idFile " + negate + " IN (SELECT DISTINCT idFile FROM bookmark WHERE type = 1)";
793 }
794 else if (strType == "tvshows")
795 {
796 if (m_field == FieldInProgress)
797 return negate + " ("
798 "(tvshow_view.watchedcount > 0 AND tvshow_view.watchedcount < tvshow_view.totalCount) OR "
799 "(tvshow_view.watchedcount = 0 AND EXISTS "
800 "(SELECT 1 FROM episode_view WHERE episode_view.idShow = " + GetField(FieldId, strType) + " AND episode_view.resumeTimeInSeconds > 0)"
801 ")"
802 ")";
803 }
804 if (strType == "albums")
805 {
806 if (m_field == FieldCompilation)
807 return negate + GetField(m_field, strType);
808 if (m_field == FieldIsBoxset)
809 return negate + "albumview.bBoxedSet = 1";
810 }
811 return "";
812 }
813
814 CDatabaseQueryRule::SEARCH_OPERATOR CSmartPlaylistRule::GetOperator(const std::string &strType) const
815 {
816 SEARCH_OPERATOR op = CDatabaseQueryRule::GetOperator(strType);
817 if ((strType == "tvshows" || strType == "episodes") && m_field == FieldYear)
818 { // special case for premiered which is a date rather than a year
819 //! @todo SMARTPLAYLISTS do we really need this, or should we just make this field the premiered date and request a date?
820 if (op == OPERATOR_EQUALS)
821 op = OPERATOR_CONTAINS;
822 else if (op == OPERATOR_DOES_NOT_EQUAL)
823 op = OPERATOR_DOES_NOT_CONTAIN;
824 }
825 return op;
826 }
827
828 std::string CSmartPlaylistRule::FormatParameter(const std::string &operatorString, const std::string &param, const CDatabase &db, const std::string &strType) const
829 {
830 // special-casing
831 if (m_field == FieldTime || m_field == FieldAlbumDuration)
832 { // translate time to seconds
833 std::string seconds = std::to_string(StringUtils::TimeStringToSeconds(param));
834 return db.PrepareSQL(operatorString, seconds.c_str());
835 }
836 return CDatabaseQueryRule::FormatParameter(operatorString, param, db, strType);
837 }
838
839 std::string CSmartPlaylistRule::FormatLinkQuery(const char *field, const char *table, const MediaType& mediaType, const std::string& mediaField, const std::string& parameter)
840 {
841 // NOTE: no need for a PrepareSQL here, as the parameter has already been formatted
842 return StringUtils::Format(
843 " EXISTS (SELECT 1 FROM {}_link"
844 " JOIN {} ON {}.{}_id={}_link.{}_id"
845 " WHERE {}_link.media_id={} AND {}.name {} AND {}_link.media_type = '{}')",
846 field, table, table, table, field, table, field, mediaField, table, parameter, field,
847 mediaType);
848 }
849
850 std::string CSmartPlaylistRule::FormatYearQuery(const std::string& field,
851 const std::string& param,
852 const std::string& parameter) const
853 {
854 std::string query;
855 if (m_operator == OPERATOR_EQUALS && param == "0")
856 query = "(TRIM(" + field + ") = '' OR " + field + " IS NULL)";
857 else if (m_operator == OPERATOR_DOES_NOT_EQUAL && param == "0")
858 query = "(TRIM(" + field + ") <> '' AND " + field + " IS NOT NULL)";
859 else
860 { // Get year from ISO8601 date string, cast as INTEGER
861 query = "CAST(" + field + " as INTEGER)" + parameter;
862 if (m_operator == OPERATOR_LESS_THAN)
863 query = "(TRIM(" + field + ") = '' OR " + field + " IS NULL OR " + query + ")";
864 }
865 return query;
866 }
867
868 std::string CSmartPlaylistRule::FormatWhereClause(const std::string &negate, const std::string &oper, const std::string &param,
869 const CDatabase &db, const std::string &strType) const
870 {
871 std::string parameter = FormatParameter(oper, param, db, strType);
872
873 std::string query;
874 std::string table;
875 if (strType == "songs")
876 {
877 table = "songview";
878
879 if (m_field == FieldGenre)
880 query = negate + " EXISTS (SELECT 1 FROM song_genre, genre WHERE song_genre.idSong = " + GetField(FieldId, strType) + " AND song_genre.idGenre = genre.idGenre AND genre.strGenre" + parameter + ")";
881 else if (m_field == FieldArtist)
882 query = negate + " EXISTS (SELECT 1 FROM song_artist, artist WHERE song_artist.idSong = " + GetField(FieldId, strType) + " AND song_artist.idArtist = artist.idArtist AND artist.strArtist" + parameter + ")";
883 else if (m_field == FieldAlbumArtist)
884 query = negate + " EXISTS (SELECT 1 FROM album_artist, artist WHERE album_artist.idAlbum = " + table + ".idAlbum AND album_artist.idArtist = artist.idArtist AND artist.strArtist" + parameter + ")";
885 else if (m_field == FieldLastPlayed && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
886 query = GetField(m_field, strType) + " is NULL or " + GetField(m_field, strType) + parameter;
887 else if (m_field == FieldSource)
888 query = negate + " EXISTS (SELECT 1 FROM album_source, source WHERE album_source.idAlbum = " + table + ".idAlbum AND album_source.idSource = source.idSource AND source.strName" + parameter + ")";
889 else if (m_field == FieldYear || m_field == FieldOrigYear)
890 {
891 std::string field;
892 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
893 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
894 field = GetField(FieldOrigYear, strType);
895 else
896 field = GetField(m_field, strType);
897 query = FormatYearQuery(field, param, parameter);
898 }
899 }
900 else if (strType == "albums")
901 {
902 table = "albumview";
903
904 if (m_field == FieldGenre)
905 query = negate + " EXISTS (SELECT 1 FROM song, song_genre, genre WHERE song.idAlbum = " + GetField(FieldId, strType) + " AND song.idSong = song_genre.idSong AND song_genre.idGenre = genre.idGenre AND genre.strGenre" + parameter + ")";
906 else if (m_field == FieldArtist)
907 query = negate + " EXISTS (SELECT 1 FROM song, song_artist, artist WHERE song.idAlbum = " + GetField(FieldId, strType) + " AND song.idSong = song_artist.idSong AND song_artist.idArtist = artist.idArtist AND artist.strArtist" + parameter + ")";
908 else if (m_field == FieldAlbumArtist)
909 query = negate + " EXISTS (SELECT 1 FROM album_artist, artist WHERE album_artist.idAlbum = " + GetField(FieldId, strType) + " AND album_artist.idArtist = artist.idArtist AND artist.strArtist" + parameter + ")";
910 else if (m_field == FieldPath)
911 query = negate + " EXISTS (SELECT 1 FROM song JOIN path on song.idpath = path.idpath WHERE song.idAlbum = " + GetField(FieldId, strType) + " AND path.strPath" + parameter + ")";
912 else if (m_field == FieldLastPlayed && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
913 query = GetField(m_field, strType) + " is NULL or " + GetField(m_field, strType) + parameter;
914 else if (m_field == FieldSource)
915 query = negate + " EXISTS (SELECT 1 FROM album_source, source WHERE album_source.idAlbum = " + GetField(FieldId, strType) + " AND album_source.idSource = source.idSource AND source.strName" + parameter + ")";
916 else if (m_field == FieldDiscTitle)
917 query = negate +
918 " EXISTS (SELECT 1 FROM song WHERE song.idAlbum = " + GetField(FieldId, strType) +
919 " AND song.strDiscSubtitle" + parameter + ")";
920 else if (m_field == FieldYear || m_field == FieldOrigYear)
921 {
922 std::string field;
923 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
924 CSettings::SETTING_MUSICLIBRARY_USEORIGINALDATE))
925 field = GetField(FieldOrigYear, strType);
926 else
927 field = GetField(m_field, strType);
928 query = FormatYearQuery(field, param, parameter);
929 }
930 }
931 else if (strType == "artists")
932 {
933 table = "artistview";
934
935 if (m_field == FieldGenre)
936 {
937 query = negate + " (EXISTS (SELECT DISTINCT song_artist.idArtist FROM song_artist, song_genre, genre WHERE song_artist.idArtist = " + GetField(FieldId, strType) + " AND song_artist.idSong = song_genre.idSong AND song_genre.idGenre = genre.idGenre AND genre.strGenre" + parameter + ")";
938 query += " OR ";
939 query += "EXISTS (SELECT DISTINCT album_artist.idArtist FROM album_artist, song, song_genre, genre WHERE album_artist.idArtist = " + GetField(FieldId, strType) + " AND song.idAlbum = album_artist.idAlbum AND song.idSong = song_genre.idSong AND song_genre.idGenre = genre.idGenre AND genre.strGenre" + parameter + "))";
940 }
941 else if (m_field == FieldRole)
942 {
943 query = negate + " (EXISTS (SELECT DISTINCT song_artist.idArtist FROM song_artist, role WHERE song_artist.idArtist = " + GetField(FieldId, strType) + " AND song_artist.idRole = role.idRole AND role.strRole" + parameter + "))";
944 }
945 else if (m_field == FieldPath)
946 {
947 query = negate + " (EXISTS (SELECT DISTINCT song_artist.idArtist FROM song_artist JOIN song ON song.idSong = song_artist.idSong JOIN path ON song.idpath = path.idpath ";
948 query += "WHERE song_artist.idArtist = " + GetField(FieldId, strType) + " AND path.strPath" + parameter + "))";
949 }
950 else if (m_field == FieldSource)
951 {
952 query = negate + " (EXISTS(SELECT 1 FROM song_artist, song, album_source, source WHERE song_artist.idArtist = " + GetField(FieldId, strType) + " AND song.idSong = song_artist.idSong AND song_artist.idRole = 1 AND album_source.idAlbum = song.idAlbum AND album_source.idSource = source.idSource AND source.strName" + parameter + ")";
953 query += " OR ";
954 query += " EXISTS (SELECT 1 FROM album_artist, album_source, source WHERE album_artist.idArtist = " + GetField(FieldId, strType) + " AND album_source.idAlbum = album_artist.idAlbum AND album_source.idSource = source.idSource AND source.strName" + parameter + "))";
955 }
956 }
957 else if (strType == "movies")
958 {
959 table = "movie_view";
960
961 if (m_field == FieldGenre)
962 query = negate + FormatLinkQuery("genre", "genre", MediaTypeMovie, GetField(FieldId, strType), parameter);
963 else if (m_field == FieldDirector)
964 query = negate + FormatLinkQuery("director", "actor", MediaTypeMovie, GetField(FieldId, strType), parameter);
965 else if (m_field == FieldActor)
966 query = negate + FormatLinkQuery("actor", "actor", MediaTypeMovie, GetField(FieldId, strType), parameter);
967 else if (m_field == FieldWriter)
968 query = negate + FormatLinkQuery("writer", "actor", MediaTypeMovie, GetField(FieldId, strType), parameter);
969 else if (m_field == FieldStudio)
970 query = negate + FormatLinkQuery("studio", "studio", MediaTypeMovie, GetField(FieldId, strType), parameter);
971 else if (m_field == FieldCountry)
972 query = negate + FormatLinkQuery("country", "country", MediaTypeMovie, GetField(FieldId, strType), parameter);
973 else if ((m_field == FieldLastPlayed || m_field == FieldDateAdded) && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
974 query = GetField(m_field, strType) + " IS NULL OR " + GetField(m_field, strType) + parameter;
975 else if (m_field == FieldTag)
976 query = negate + FormatLinkQuery("tag", "tag", MediaTypeMovie, GetField(FieldId, strType), parameter);
977 }
978 else if (strType == "musicvideos")
979 {
980 table = "musicvideo_view";
981
982 if (m_field == FieldGenre)
983 query = negate + FormatLinkQuery("genre", "genre", MediaTypeMusicVideo, GetField(FieldId, strType), parameter);
984 else if (m_field == FieldArtist || m_field == FieldAlbumArtist)
985 query = negate + FormatLinkQuery("actor", "actor", MediaTypeMusicVideo, GetField(FieldId, strType), parameter);
986 else if (m_field == FieldStudio)
987 query = negate + FormatLinkQuery("studio", "studio", MediaTypeMusicVideo, GetField(FieldId, strType), parameter);
988 else if (m_field == FieldDirector)
989 query = negate + FormatLinkQuery("director", "actor", MediaTypeMusicVideo, GetField(FieldId, strType), parameter);
990 else if ((m_field == FieldLastPlayed || m_field == FieldDateAdded) && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
991 query = GetField(m_field, strType) + " IS NULL OR " + GetField(m_field, strType) + parameter;
992 else if (m_field == FieldTag)
993 query = negate + FormatLinkQuery("tag", "tag", MediaTypeMusicVideo, GetField(FieldId, strType), parameter);
994 }
995 else if (strType == "tvshows")
996 {
997 table = "tvshow_view";
998
999 if (m_field == FieldGenre)
1000 query = negate + FormatLinkQuery("genre", "genre", MediaTypeTvShow, GetField(FieldId, strType), parameter);
1001 else if (m_field == FieldDirector)
1002 query = negate + FormatLinkQuery("director", "actor", MediaTypeTvShow, GetField(FieldId, strType), parameter);
1003 else if (m_field == FieldActor)
1004 query = negate + FormatLinkQuery("actor", "actor", MediaTypeTvShow, GetField(FieldId, strType), parameter);
1005 else if (m_field == FieldStudio)
1006 query = negate + FormatLinkQuery("studio", "studio", MediaTypeTvShow, GetField(FieldId, strType), parameter);
1007 else if (m_field == FieldMPAA)
1008 query = negate + " (" + GetField(m_field, strType) + parameter + ")";
1009 else if ((m_field == FieldLastPlayed || m_field == FieldDateAdded) && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
1010 query = GetField(m_field, strType) + " IS NULL OR " + GetField(m_field, strType) + parameter;
1011 else if (m_field == FieldPlaycount)
1012 query = "CASE WHEN COALESCE(" + GetField(FieldNumberOfEpisodes, strType) + " - " + GetField(FieldNumberOfWatchedEpisodes, strType) + ", 0) > 0 THEN 0 ELSE 1 END " + parameter;
1013 else if (m_field == FieldTag)
1014 query = negate + FormatLinkQuery("tag", "tag", MediaTypeTvShow, GetField(FieldId, strType), parameter);
1015 }
1016 else if (strType == "episodes")
1017 {
1018 table = "episode_view";
1019
1020 if (m_field == FieldGenre)
1021 query = negate + FormatLinkQuery("genre", "genre", MediaTypeTvShow, (table + ".idShow").c_str(), parameter);
1022 else if (m_field == FieldTag)
1023 query = negate + FormatLinkQuery("tag", "tag", MediaTypeTvShow, (table + ".idShow").c_str(), parameter);
1024 else if (m_field == FieldDirector)
1025 query = negate + FormatLinkQuery("director", "actor", MediaTypeEpisode, GetField(FieldId, strType), parameter);
1026 else if (m_field == FieldActor)
1027 query = negate + FormatLinkQuery("actor", "actor", MediaTypeEpisode, GetField(FieldId, strType), parameter);
1028 else if (m_field == FieldWriter)
1029 query = negate + FormatLinkQuery("writer", "actor", MediaTypeEpisode, GetField(FieldId, strType), parameter);
1030 else if ((m_field == FieldLastPlayed || m_field == FieldDateAdded) && (m_operator == OPERATOR_LESS_THAN || m_operator == OPERATOR_BEFORE || m_operator == OPERATOR_NOT_IN_THE_LAST))
1031 query = GetField(m_field, strType) + " IS NULL OR " + GetField(m_field, strType) + parameter;
1032 else if (m_field == FieldStudio)
1033 query = negate + FormatLinkQuery("studio", "studio", MediaTypeTvShow, (table + ".idShow").c_str(), parameter);
1034 else if (m_field == FieldMPAA)
1035 query = negate + " (" + GetField(m_field, strType) + parameter + ")";
1036 }
1037 if (m_field == FieldVideoResolution)
1038 query = table + ".idFile" + negate + GetVideoResolutionQuery(param);
1039 else if (m_field == FieldAudioChannels)
1040 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND iAudioChannels " + parameter + ")";
1041 else if (m_field == FieldVideoCodec)
1042 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND strVideoCodec " + parameter + ")";
1043 else if (m_field == FieldAudioCodec)
1044 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND strAudioCodec " + parameter + ")";
1045 else if (m_field == FieldAudioLanguage)
1046 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND strAudioLanguage " + parameter + ")";
1047 else if (m_field == FieldSubtitleLanguage)
1048 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND strSubtitleLanguage " + parameter + ")";
1049 else if (m_field == FieldVideoAspectRatio)
1050 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND fVideoAspect " + parameter + ")";
1051 else if (m_field == FieldAudioCount)
1052 query = db.PrepareSQL(negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND streamdetails.iStreamtype = %i GROUP BY streamdetails.idFile HAVING COUNT(streamdetails.iStreamType) " + parameter + ")",CStreamDetail::AUDIO);
1053 else if (m_field == FieldSubtitleCount)
1054 query = db.PrepareSQL(negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND streamdetails.iStreamType = %i GROUP BY streamdetails.idFile HAVING COUNT(streamdetails.iStreamType) " + parameter + ")",CStreamDetail::SUBTITLE);
1055 else if (m_field == FieldHdrType)
1056 query = negate + " EXISTS (SELECT 1 FROM streamdetails WHERE streamdetails.idFile = " + table + ".idFile AND strHdrType " + parameter + ")";
1057 if (m_field == FieldPlaycount && strType != "songs" && strType != "albums" && strType != "tvshows")
1058 { // playcount IS stored as NULL OR number IN video db
1059 if ((m_operator == OPERATOR_EQUALS && param == "0") ||
1060 (m_operator == OPERATOR_DOES_NOT_EQUAL && param != "0") ||
1061 (m_operator == OPERATOR_LESS_THAN))
1062 {
1063 std::string field = GetField(FieldPlaycount, strType);
1064 query = field + " IS NULL OR " + field + parameter;
1065 }
1066 }
1067 if (query.empty())
1068 query = CDatabaseQueryRule::FormatWhereClause(negate, oper, param, db, strType);
1069 return query;
1070 }
1071
1072 std::string CSmartPlaylistRule::GetField(int field, const std::string &type) const
1073 {
1074 if (field >= FieldUnknown && field < FieldMax)
1075 return DatabaseUtils::GetField((Field)field, CMediaTypes::FromString(type), DatabaseQueryPartWhere);
1076 return "";
1077 }
1078
1079 std::string CSmartPlaylistRuleCombination::GetWhereClause(const CDatabase &db, const std::string& strType, std::set<std::string> &referencedPlaylists) const
1080 {
1081 std::string rule;
1082
1083 // translate the combinations into SQL
1084 for (CDatabaseQueryRuleCombinations::const_iterator it = m_combinations.begin(); it != m_combinations.end(); ++it)
1085 {
1086 if (it != m_combinations.begin())
1087 rule += m_type == CombinationAnd ? " AND " : " OR ";
1088 std::shared_ptr<CSmartPlaylistRuleCombination> combo = std::static_pointer_cast<CSmartPlaylistRuleCombination>(*it);
1089 if (combo)
1090 rule += "(" + combo->GetWhereClause(db, strType, referencedPlaylists) + ")";
1091 }
1092
1093 // translate the rules into SQL
1094 for (CDatabaseQueryRules::const_iterator it = m_rules.begin(); it != m_rules.end(); ++it)
1095 {
1096 // don't include playlists that are meant to be displayed
1097 // as a virtual folders in the SQL WHERE clause
1098 if ((*it)->m_field == FieldVirtualFolder)
1099 continue;
1100
1101 if (!rule.empty())
1102 rule += m_type == CombinationAnd ? " AND " : " OR ";
1103 rule += "(";
1104 std::string currentRule;
1105 if ((*it)->m_field == FieldPlaylist)
1106 {
1107 std::string playlistFile = CSmartPlaylistDirectory::GetPlaylistByName((*it)->m_parameter.at(0), strType);
1108 if (!playlistFile.empty() && referencedPlaylists.find(playlistFile) == referencedPlaylists.end())
1109 {
1110 referencedPlaylists.insert(playlistFile);
1111 CSmartPlaylist playlist;
1112 if (playlist.Load(playlistFile))
1113 {
1114 std::string playlistQuery;
1115 // only playlists of same type will be part of the query
1116 if (playlist.GetType() == strType || (playlist.GetType() == "mixed" && (strType == "songs" || strType == "musicvideos")) || playlist.GetType().empty())
1117 {
1118 playlist.SetType(strType);
1119 playlistQuery = playlist.GetWhereClause(db, referencedPlaylists);
1120 }
1121 if (playlist.GetType() == strType)
1122 {
1123 if ((*it)->m_operator == CDatabaseQueryRule::OPERATOR_DOES_NOT_EQUAL)
1124 currentRule = StringUtils::Format("NOT ({})", playlistQuery);
1125 else
1126 currentRule = playlistQuery;
1127 }
1128 }
1129 }
1130 }
1131 else
1132 currentRule = (*it)->GetWhereClause(db, strType);
1133 // if we don't get a rule, we add '1' or '0' so the query is still valid and doesn't fail
1134 if (currentRule.empty())
1135 currentRule = m_type == CombinationAnd ? "'1'" : "'0'";
1136 rule += currentRule;
1137 rule += ")";
1138 }
1139
1140 return rule;
1141 }
1142
1143 void CSmartPlaylistRuleCombination::GetVirtualFolders(const std::string& strType, std::vector<std::string> &virtualFolders) const
1144 {
1145 for (CDatabaseQueryRuleCombinations::const_iterator it = m_combinations.begin(); it != m_combinations.end(); ++it)
1146 {
1147 std::shared_ptr<CSmartPlaylistRuleCombination> combo = std::static_pointer_cast<CSmartPlaylistRuleCombination>(*it);
1148 if (combo)
1149 combo->GetVirtualFolders(strType, virtualFolders);
1150 }
1151
1152 for (CDatabaseQueryRules::const_iterator it = m_rules.begin(); it != m_rules.end(); ++it)
1153 {
1154 if (((*it)->m_field != FieldVirtualFolder && (*it)->m_field != FieldPlaylist) || (*it)->m_operator != CDatabaseQueryRule::OPERATOR_EQUALS)
1155 continue;
1156
1157 std::string playlistFile = CSmartPlaylistDirectory::GetPlaylistByName((*it)->m_parameter.at(0), strType);
1158 if (playlistFile.empty())
1159 continue;
1160
1161 if ((*it)->m_field == FieldVirtualFolder)
1162 virtualFolders.push_back(playlistFile);
1163 else
1164 {
1165 // look for any virtual folders in the expanded playlists
1166 CSmartPlaylist playlist;
1167 if (!playlist.Load(playlistFile))
1168 continue;
1169
1170 if (CSmartPlaylist::CheckTypeCompatibility(playlist.GetType(), strType))
1171 playlist.GetVirtualFolders(virtualFolders);
1172 }
1173 }
1174 }
1175
1176 void CSmartPlaylistRuleCombination::AddRule(const CSmartPlaylistRule &rule)
1177 {
1178 std::shared_ptr<CSmartPlaylistRule> ptr(new CSmartPlaylistRule(rule));
1179 m_rules.push_back(ptr);
1180 }
1181
1182 CSmartPlaylist::CSmartPlaylist()
1183 {
1184 Reset();
1185 }
1186
1187 bool CSmartPlaylist::OpenAndReadName(const CURL &url)
1188 {
1189 if (readNameFromPath(url) == NULL)
1190 return false;
1191
1192 return !m_playlistName.empty();
1193 }
1194
1195 const TiXmlNode* CSmartPlaylist::readName(const TiXmlNode *root)
1196 {
1197 if (root == NULL)
1198 return NULL;
1199
1200 const TiXmlElement *rootElem = root->ToElement();
1201 if (rootElem == NULL)
1202 return NULL;
1203
1204 if (!StringUtils::EqualsNoCase(root->Value(), "smartplaylist"))
1205 {
1206 CLog::Log(LOGERROR, "Error loading Smart playlist");
1207 return NULL;
1208 }
1209
1210 // load the playlist type
1211 const char* type = rootElem->Attribute("type");
1212 if (type)
1213 m_playlistType = type;
1214 // backward compatibility:
1215 if (m_playlistType == "music")
1216 m_playlistType = "songs";
1217 if (m_playlistType == "video")
1218 m_playlistType = "musicvideos";
1219
1220 // load the playlist name
1221 XMLUtils::GetString(root, "name", m_playlistName);
1222
1223 return root;
1224 }
1225
1226 const TiXmlNode* CSmartPlaylist::readNameFromPath(const CURL &url)
1227 {
1228 CFileStream file;
1229 if (!file.Open(url))
1230 {
1231 CLog::Log(LOGERROR, "Error loading Smart playlist {} (failed to read file)", url.GetRedacted());
1232 return NULL;
1233 }
1234
1235 m_xmlDoc.Clear();
1236 file >> m_xmlDoc;
1237
1238 const TiXmlNode *root = readName(m_xmlDoc.RootElement());
1239 if (m_playlistName.empty())
1240 {
1241 m_playlistName = CUtil::GetTitleFromPath(url.Get());
1242 if (URIUtils::HasExtension(m_playlistName, ".xsp"))
1243 URIUtils::RemoveExtension(m_playlistName);
1244 }
1245
1246 return root;
1247 }
1248
1249 const TiXmlNode* CSmartPlaylist::readNameFromXml(const std::string &xml)
1250 {
1251 if (xml.empty())
1252 {
1253 CLog::Log(LOGERROR, "Error loading empty Smart playlist");
1254 return NULL;
1255 }
1256
1257 m_xmlDoc.Clear();
1258 if (!m_xmlDoc.Parse(xml))
1259 {
1260 CLog::Log(LOGERROR, "Error loading Smart playlist (failed to parse xml: {})",
1261 m_xmlDoc.ErrorDesc());
1262 return NULL;
1263 }
1264
1265 const TiXmlNode *root = readName(m_xmlDoc.RootElement());
1266
1267 return root;
1268 }
1269
1270 bool CSmartPlaylist::load(const TiXmlNode *root)
1271 {
1272 if (root == NULL)
1273 return false;
1274
1275 return LoadFromXML(root);
1276 }
1277
1278 bool CSmartPlaylist::Load(const CURL &url)
1279 {
1280 return load(readNameFromPath(url));
1281 }
1282
1283 bool CSmartPlaylist::Load(const std::string &path)
1284 {
1285 const CURL pathToUrl(path);
1286 return load(readNameFromPath(pathToUrl));
1287 }
1288
1289 bool CSmartPlaylist::Load(const CVariant &obj)
1290 {
1291 if (!obj.isObject())
1292 return false;
1293
1294 // load the playlist type
1295 if (obj.isMember("type") && obj["type"].isString())
1296 m_playlistType = obj["type"].asString();
1297
1298 // backward compatibility
1299 if (m_playlistType == "music")
1300 m_playlistType = "songs";
1301 if (m_playlistType == "video")
1302 m_playlistType = "musicvideos";
1303
1304 // load the playlist name
1305 if (obj.isMember("name") && obj["name"].isString())
1306 m_playlistName = obj["name"].asString();
1307
1308 if (obj.isMember("rules"))
1309 m_ruleCombination.Load(obj["rules"], this);
1310
1311 if (obj.isMember("group") && obj["group"].isMember("type") && obj["group"]["type"].isString())
1312 {
1313 m_group = obj["group"]["type"].asString();
1314 if (obj["group"].isMember("mixed") && obj["group"]["mixed"].isBoolean())
1315 m_groupMixed = obj["group"]["mixed"].asBoolean();
1316 }
1317
1318 // now any limits
1319 if (obj.isMember("limit") && (obj["limit"].isInteger() || obj["limit"].isUnsignedInteger()) && obj["limit"].asUnsignedInteger() > 0)
1320 m_limit = (unsigned int)obj["limit"].asUnsignedInteger();
1321
1322 // and order
1323 if (obj.isMember("order") && obj["order"].isMember("method") && obj["order"]["method"].isString())
1324 {
1325 const CVariant &order = obj["order"];
1326 if (order.isMember("direction") && order["direction"].isString())
1327 m_orderDirection = StringUtils::EqualsNoCase(order["direction"].asString(), "ascending") ? SortOrderAscending : SortOrderDescending;
1328
1329 if (order.isMember("ignorefolders") && obj["ignorefolders"].isBoolean())
1330 m_orderAttributes = obj["ignorefolders"].asBoolean() ? SortAttributeIgnoreFolders : SortAttributeNone;
1331
1332 m_orderField = CSmartPlaylistRule::TranslateOrder(obj["order"]["method"].asString().c_str());
1333 }
1334
1335 return true;
1336 }
1337
1338 bool CSmartPlaylist::LoadFromXml(const std::string &xml)
1339 {
1340 return load(readNameFromXml(xml));
1341 }
1342
1343 bool CSmartPlaylist::LoadFromXML(const TiXmlNode *root, const std::string &encoding)
1344 {
1345 if (!root)
1346 return false;
1347
1348 std::string tmp;
1349 if (XMLUtils::GetString(root, "match", tmp))
1350 m_ruleCombination.SetType(StringUtils::EqualsNoCase(tmp, "all") ? CSmartPlaylistRuleCombination::CombinationAnd : CSmartPlaylistRuleCombination::CombinationOr);
1351
1352 // now the rules
1353 const TiXmlNode *ruleNode = root->FirstChild("rule");
1354 while (ruleNode)
1355 {
1356 CSmartPlaylistRule rule;
1357 if (rule.Load(ruleNode, encoding))
1358 m_ruleCombination.AddRule(rule);
1359
1360 ruleNode = ruleNode->NextSibling("rule");
1361 }
1362
1363 const TiXmlElement *groupElement = root->FirstChildElement("group");
1364 if (groupElement != NULL && groupElement->FirstChild() != NULL)
1365 {
1366 m_group = groupElement->FirstChild()->ValueStr();
1367 const char* mixed = groupElement->Attribute("mixed");
1368 m_groupMixed = mixed != NULL && StringUtils::EqualsNoCase(mixed, "true");
1369 }
1370
1371 // now any limits
1372 // format is <limit>25</limit>
1373 XMLUtils::GetUInt(root, "limit", m_limit);
1374
1375 // and order
1376 // format is <order direction="ascending">field</order>
1377 const TiXmlElement *order = root->FirstChildElement("order");
1378 if (order && order->FirstChild())
1379 {
1380 const char *direction = order->Attribute("direction");
1381 if (direction)
1382 m_orderDirection = StringUtils::EqualsNoCase(direction, "ascending") ? SortOrderAscending : SortOrderDescending;
1383
1384 const char *ignorefolders = order->Attribute("ignorefolders");
1385 if (ignorefolders != NULL)
1386 m_orderAttributes = StringUtils::EqualsNoCase(ignorefolders, "true") ? SortAttributeIgnoreFolders : SortAttributeNone;
1387
1388 m_orderField = CSmartPlaylistRule::TranslateOrder(order->FirstChild()->Value());
1389 }
1390 return true;
1391 }
1392
1393 bool CSmartPlaylist::LoadFromJson(const std::string &json)
1394 {
1395 if (json.empty())
1396 return false;
1397
1398 CVariant obj;
1399 if (!CJSONVariantParser::Parse(json, obj))
1400 return false;
1401
1402 return Load(obj);
1403 }
1404
1405 bool CSmartPlaylist::Save(const std::string &path) const
1406 {
1407 CXBMCTinyXML doc;
1408 TiXmlDeclaration decl("1.0", "UTF-8", "yes");
1409 doc.InsertEndChild(decl);
1410
1411 TiXmlElement xmlRootElement("smartplaylist");
1412 xmlRootElement.SetAttribute("type",m_playlistType.c_str());
1413 TiXmlNode *pRoot = doc.InsertEndChild(xmlRootElement);
1414 if (!pRoot)
1415 return false;
1416
1417 // add the <name> tag
1418 XMLUtils::SetString(pRoot, "name", m_playlistName);
1419
1420 // add the <match> tag
1421 XMLUtils::SetString(pRoot, "match", m_ruleCombination.GetType() == CSmartPlaylistRuleCombination::CombinationAnd ? "all" : "one");
1422
1423 // add <rule> tags
1424 m_ruleCombination.Save(pRoot);
1425
1426 // add <group> tag if necessary
1427 if (!m_group.empty())
1428 {
1429 TiXmlElement nodeGroup("group");
1430 if (m_groupMixed)
1431 nodeGroup.SetAttribute("mixed", "true");
1432 TiXmlText group(m_group.c_str());
1433 nodeGroup.InsertEndChild(group);
1434 pRoot->InsertEndChild(nodeGroup);
1435 }
1436
1437 // add <limit> tag
1438 if (m_limit)
1439 XMLUtils::SetInt(pRoot, "limit", m_limit);
1440
1441 // add <order> tag
1442 if (m_orderField != SortByNone)
1443 {
1444 TiXmlText order(CSmartPlaylistRule::TranslateOrder(m_orderField).c_str());
1445 TiXmlElement nodeOrder("order");
1446 nodeOrder.SetAttribute("direction", m_orderDirection == SortOrderDescending ? "descending" : "ascending");
1447 if (m_orderAttributes & SortAttributeIgnoreFolders)
1448 nodeOrder.SetAttribute("ignorefolders", "true");
1449 nodeOrder.InsertEndChild(order);
1450 pRoot->InsertEndChild(nodeOrder);
1451 }
1452 return doc.SaveFile(path);
1453 }
1454
1455 bool CSmartPlaylist::Save(CVariant &obj, bool full /* = true */) const
1456 {
1457 if (obj.type() == CVariant::VariantTypeConstNull)
1458 return false;
1459
1460 obj.clear();
1461 // add "type"
1462 obj["type"] = m_playlistType;
1463
1464 // add "rules"
1465 CVariant rulesObj = CVariant(CVariant::VariantTypeObject);
1466 if (m_ruleCombination.Save(rulesObj))
1467 obj["rules"] = rulesObj;
1468
1469 // add "group"
1470 if (!m_group.empty())
1471 {
1472 obj["group"]["type"] = m_group;
1473 obj["group"]["mixed"] = m_groupMixed;
1474 }
1475
1476 // add "limit"
1477 if (full && m_limit)
1478 obj["limit"] = m_limit;
1479
1480 // add "order"
1481 if (full && m_orderField != SortByNone)
1482 {
1483 obj["order"] = CVariant(CVariant::VariantTypeObject);
1484 obj["order"]["method"] = CSmartPlaylistRule::TranslateOrder(m_orderField);
1485 obj["order"]["direction"] = m_orderDirection == SortOrderDescending ? "descending" : "ascending";
1486 obj["order"]["ignorefolders"] = (m_orderAttributes & SortAttributeIgnoreFolders);
1487 }
1488
1489 return true;
1490 }
1491
1492 bool CSmartPlaylist::SaveAsJson(std::string &json, bool full /* = true */) const
1493 {
1494 CVariant xsp(CVariant::VariantTypeObject);
1495 if (!Save(xsp, full))
1496 return false;
1497
1498 return CJSONVariantWriter::Write(xsp, json, true) && !json.empty();
1499 }
1500
1501 void CSmartPlaylist::Reset()
1502 {
1503 m_ruleCombination.clear();
1504 m_limit = 0;
1505 m_orderField = SortByNone;
1506 m_orderDirection = SortOrderNone;
1507 m_orderAttributes = SortAttributeNone;
1508 m_playlistType = "songs"; // sane default
1509 m_group.clear();
1510 m_groupMixed = false;
1511 }
1512
1513 void CSmartPlaylist::SetName(const std::string &name)
1514 {
1515 m_playlistName = name;
1516 }
1517
1518 void CSmartPlaylist::SetType(const std::string &type)
1519 {
1520 m_playlistType = type;
1521 }
1522
1523 bool CSmartPlaylist::IsVideoType() const
1524 {
1525 return IsVideoType(m_playlistType);
1526 }
1527
1528 bool CSmartPlaylist::IsMusicType() const
1529 {
1530 return IsMusicType(m_playlistType);
1531 }
1532
1533 bool CSmartPlaylist::IsVideoType(const std::string &type)
1534 {
1535 return type == "movies" || type == "tvshows" || type == "episodes" ||
1536 type == "musicvideos" || type == "mixed";
1537 }
1538
1539 bool CSmartPlaylist::IsMusicType(const std::string &type)
1540 {
1541 return type == "artists" || type == "albums" ||
1542 type == "songs" || type == "mixed";
1543 }
1544
1545 std::string CSmartPlaylist::GetWhereClause(const CDatabase &db, std::set<std::string> &referencedPlaylists) const
1546 {
1547 return m_ruleCombination.GetWhereClause(db, GetType(), referencedPlaylists);
1548 }
1549
1550 void CSmartPlaylist::GetVirtualFolders(std::vector<std::string> &virtualFolders) const
1551 {
1552 m_ruleCombination.GetVirtualFolders(GetType(), virtualFolders);
1553 }
1554
1555 std::string CSmartPlaylist::GetSaveLocation() const
1556 {
1557 if (m_playlistType == "mixed")
1558 return "mixed";
1559 if (IsMusicType())
1560 return "music";
1561 // all others are video
1562 return "video";
1563 }
1564
1565 void CSmartPlaylist::GetAvailableFields(const std::string &type, std::vector<std::string> &fieldList)
1566 {
1567 std::vector<Field> typeFields = CSmartPlaylistRule::GetFields(type);
1568 for (std::vector<Field>::const_iterator field = typeFields.begin(); field != typeFields.end(); ++field)
1569 {
1570 for (const translateField& i : fields)
1571 {
1572 if (*field == i.field)
1573 fieldList.emplace_back(i.string);
1574 }
1575 }
1576 }
1577
1578 bool CSmartPlaylist::IsEmpty(bool ignoreSortAndLimit /* = true */) const
1579 {
1580 bool empty = m_ruleCombination.empty();
1581 if (empty && !ignoreSortAndLimit)
1582 empty = m_limit <= 0 && m_orderField == SortByNone && m_orderDirection == SortOrderNone;
1583
1584 return empty;
1585 }
1586
1587 bool CSmartPlaylist::CheckTypeCompatibility(const std::string &typeLeft, const std::string &typeRight)
1588 {
1589 if (typeLeft == typeRight)
1590 return true;
1591
1592 if (typeLeft == "mixed" &&
1593 (typeRight == "songs" || typeRight == "musicvideos"))
1594 return true;
1595
1596 if (typeRight == "mixed" &&
1597 (typeLeft == "songs" || typeLeft == "musicvideos"))
1598 return true;
1599
1600 return false;
1601 }
1602
1603 CDatabaseQueryRule *CSmartPlaylist::CreateRule() const
1604 {
1605 return new CSmartPlaylistRule();
1606 }
1607 CDatabaseQueryRuleCombination *CSmartPlaylist::CreateCombination() const
1608 {
1609 return new CSmartPlaylistRuleCombination();
1610 }