| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "dbwrappers/DatabaseQuery.h" |
| 12 |
#include "utils/SortUtils.h" |
| 13 |
#include "utils/XBMCTinyXML.h" |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <set> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
class CURL; |
| 21 |
class CVariant; |
| 22 |
|
| 23 |
class CSmartPlaylistRule : public CDatabaseQueryRule |
| 24 |
{ |
| 25 |
public: |
| 26 |
CSmartPlaylistRule(); |
| 27 |
~CSmartPlaylistRule() override = default; |
| 28 |
|
| 29 |
std::string GetLocalizedRule() const; |
| 30 |
|
| 31 |
static SortBy TranslateOrder(const char *order); |
| 32 |
static std::string TranslateOrder(SortBy order); |
| 33 |
static Field TranslateGroup(const char *group); |
| 34 |
static std::string TranslateGroup(Field group); |
| 35 |
|
| 36 |
static std::string GetLocalizedField(int field); |
| 37 |
static std::string GetLocalizedGroup(Field group); |
| 38 |
static bool CanGroupMix(Field group); |
| 39 |
|
| 40 |
static std::vector<Field> GetFields(const std::string &type); |
| 41 |
static std::vector<SortBy> GetOrders(const std::string &type); |
| 42 |
static std::vector<Field> GetGroups(const std::string &type); |
| 43 |
FIELD_TYPE GetFieldType(int field) const override; |
| 44 |
static bool IsFieldBrowseable(int field); |
| 45 |
|
| 46 |
static bool Validate(const std::string &input, void *data); |
| 47 |
static bool ValidateRating(const std::string &input, void *data); |
| 48 |
static bool ValidateMyRating(const std::string &input, void *data); |
| 49 |
|
| 50 |
protected: |
| 51 |
std::string GetField(int field, const std::string& type) const override; |
| 52 |
int TranslateField(const char *field) const override; |
| 53 |
std::string TranslateField(int field) const override; |
| 54 |
std::string FormatParameter(const std::string &negate, |
| 55 |
const std::string &oper, |
| 56 |
const CDatabase &db, |
| 57 |
const std::string &type) const override; |
| 58 |
std::string FormatWhereClause(const std::string &negate, |
| 59 |
const std::string& oper, |
| 60 |
const std::string ¶m, |
| 61 |
const CDatabase &db, |
| 62 |
const std::string &type) const override; |
| 63 |
SEARCH_OPERATOR GetOperator(const std::string &type) const override; |
| 64 |
std::string GetBooleanQuery(const std::string &negate, |
| 65 |
const std::string &strType) const override; |
| 66 |
|
| 67 |
private: |
| 68 |
std::string GetVideoResolutionQuery(const std::string ¶meter) const; |
| 69 |
static std::string FormatLinkQuery(const char *field, const char *table, const MediaType& mediaType, const std::string& mediaField, const std::string& parameter); |
| 70 |
std::string FormatYearQuery(const std::string& field, |
| 71 |
const std::string& param, |
| 72 |
const std::string& parameter) const; |
| 73 |
}; |
| 74 |
|
| 75 |
class CSmartPlaylistRuleCombination : public CDatabaseQueryRuleCombination |
| 76 |
{ |
| 77 |
public: |
| 78 |
CSmartPlaylistRuleCombination() = default; |
| 79 |
~CSmartPlaylistRuleCombination() override = default; |
| 80 |
|
| 81 |
std::string GetWhereClause(const CDatabase &db, |
| 82 |
const std::string& strType, |
| 83 |
std::set<std::string> &referencedPlaylists) const; |
| 84 |
void GetVirtualFolders(const std::string& strType, |
| 85 |
std::vector<std::string> &virtualFolders) const; |
| 86 |
|
| 87 |
void AddRule(const CSmartPlaylistRule &rule); |
| 88 |
}; |
| 89 |
|
| 90 |
class CSmartPlaylist : public IDatabaseQueryRuleFactory |
| 91 |
{ |
| 92 |
public: |
| 93 |
CSmartPlaylist(); |
| 94 |
~CSmartPlaylist() override = default; |
| 95 |
|
| 96 |
bool Load(const CURL& url); |
| 97 |
bool Load(const std::string &path); |
| 98 |
bool Load(const CVariant &obj); |
| 99 |
bool LoadFromXml(const std::string &xml); |
| 100 |
bool LoadFromJson(const std::string &json); |
| 101 |
bool Save(const std::string &path) const; |
| 102 |
bool Save(CVariant &obj, bool full = true) const; |
| 103 |
bool SaveAsJson(std::string &json, bool full = true) const; |
| 104 |
|
| 105 |
bool OpenAndReadName(const CURL &url); |
| 106 |
bool LoadFromXML(const TiXmlNode *root, const std::string &encoding = "UTF-8"); |
| 107 |
|
| 108 |
void Reset(); |
| 109 |
|
| 110 |
void SetName(const std::string &name); |
| 111 |
void SetType(const std::string &type); // music, video, mixed |
| 112 |
const std::string& GetName() const { return m_playlistName; } |
| 113 |
const std::string& GetType() const { return m_playlistType; } |
| 114 |
bool IsVideoType() const; |
| 115 |
bool IsMusicType() const; |
| 116 |
|
| 117 |
void SetMatchAllRules(bool matchAll) { m_ruleCombination.SetType(matchAll ? CSmartPlaylistRuleCombination::CombinationAnd : CSmartPlaylistRuleCombination::CombinationOr); } |
| 118 |
bool GetMatchAllRules() const { return m_ruleCombination.GetType() == CSmartPlaylistRuleCombination::CombinationAnd; } |
| 119 |
|
| 120 |
void SetLimit(unsigned int limit) { m_limit = limit; } |
| 121 |
unsigned int GetLimit() const { return m_limit; } |
| 122 |
|
| 123 |
void SetOrder(SortBy order) { m_orderField = order; } |
| 124 |
SortBy GetOrder() const { return m_orderField; } |
| 125 |
void SetOrderAscending(bool orderAscending) |
| 126 |
{ |
| 127 |
m_orderDirection = orderAscending ? SortOrderAscending : SortOrderDescending; |
| 128 |
} |
| 129 |
bool GetOrderAscending() const { return m_orderDirection != SortOrderDescending; } |
| 130 |
SortOrder GetOrderDirection() const { return m_orderDirection; } |
| 131 |
void SetOrderAttributes(SortAttribute attributes) { m_orderAttributes = attributes; } |
| 132 |
SortAttribute GetOrderAttributes() const { return m_orderAttributes; } |
| 133 |
|
| 134 |
void SetGroup(const std::string &group) { m_group = group; } |
| 135 |
const std::string& GetGroup() const { return m_group; } |
| 136 |
void SetGroupMixed(bool mixed) { m_groupMixed = mixed; } |
| 137 |
bool IsGroupMixed() const { return m_groupMixed; } |
| 138 |
|
| 139 |
/*! \brief get the where clause for a playlist |
| 140 |
We handle playlists inside playlists separately in order to ensure we don't introduce infinite loops |
| 141 |
by playlist A including playlist B which also (perhaps via other playlists) then includes playlistA. |
| 142 |
|
| 143 |
\param db the database to use to format up results |
| 144 |
\param referencedPlaylists a set of playlists to know when we reach a cycle |
| 145 |
\param needWhere whether we need to prepend the where clause with "WHERE " |
| 146 |
*/ |
| 147 |
std::string GetWhereClause(const CDatabase &db, std::set<std::string> &referencedPlaylists) const; |
| 148 |
void GetVirtualFolders(std::vector<std::string> &virtualFolders) const; |
| 149 |
|
| 150 |
std::string GetSaveLocation() const; |
| 151 |
|
| 152 |
static void GetAvailableFields(const std::string &type, std::vector<std::string> &fieldList); |
| 153 |
|
| 154 |
static bool IsVideoType(const std::string &type); |
| 155 |
static bool IsMusicType(const std::string &type); |
| 156 |
static bool CheckTypeCompatibility(const std::string &typeLeft, const std::string &typeRight); |
| 157 |
|
| 158 |
bool IsEmpty(bool ignoreSortAndLimit = true) const; |
| 159 |
|
| 160 |
// rule creation |
| 161 |
CDatabaseQueryRule *CreateRule() const override; |
| 162 |
CDatabaseQueryRuleCombination *CreateCombination() const override; |
| 163 |
private: |
| 164 |
friend class CGUIDialogSmartPlaylistEditor; |
| 165 |
friend class CGUIDialogMediaFilter; |
| 166 |
|
| 167 |
const TiXmlNode* readName(const TiXmlNode *root); |
| 168 |
const TiXmlNode* readNameFromPath(const CURL &url); |
| 169 |
const TiXmlNode* readNameFromXml(const std::string &xml); |
| 170 |
bool load(const TiXmlNode *root); |
| 171 |
|
| 172 |
CSmartPlaylistRuleCombination m_ruleCombination; |
| 173 |
std::string m_playlistName; |
| 174 |
std::string m_playlistType; |
| 175 |
|
| 176 |
// order information |
| 177 |
unsigned int m_limit; |
| 178 |
SortBy m_orderField; |
| 179 |
SortOrder m_orderDirection; |
| 180 |
SortAttribute m_orderAttributes; |
| 181 |
std::string m_group; |
| 182 |
bool m_groupMixed; |
| 183 |
|
| 184 |
CXBMCTinyXML m_xmlDoc; |
| 185 |
}; |
| 186 |
|