[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / playlists / SmartPlayList.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://www.xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <set>
23 #include <vector>
24
25 #include "utils/SortUtils.h"
26 #include "utils/StdString.h"
27 #include "utils/XBMCTinyXML.h"
28
29 class CDatabase;
30 class CVariant;
31
32 class ISmartPlaylistRule
33 {
34 public:
35   virtual ~ISmartPlaylistRule() { }
36
37   virtual bool Load(const TiXmlNode *node, const std::string &encoding = "UTF-8") = 0;
38   virtual bool Load(const CVariant &obj) = 0;
39   virtual bool Save(TiXmlNode *parent) const = 0;
40   virtual bool Save(CVariant &obj) const = 0;
41 };
42
43 class CSmartPlaylistRule : public ISmartPlaylistRule
44 {
45 public:
46   CSmartPlaylistRule();
47   virtual ~CSmartPlaylistRule() { }
48
49   enum SEARCH_OPERATOR { OPERATOR_START = 0,
50                          OPERATOR_CONTAINS,
51                          OPERATOR_DOES_NOT_CONTAIN,
52                          OPERATOR_EQUALS,
53                          OPERATOR_DOES_NOT_EQUAL,
54                          OPERATOR_STARTS_WITH,
55                          OPERATOR_ENDS_WITH,
56                          OPERATOR_GREATER_THAN,
57                          OPERATOR_LESS_THAN,
58                          OPERATOR_AFTER,
59                          OPERATOR_BEFORE,
60                          OPERATOR_IN_THE_LAST,
61                          OPERATOR_NOT_IN_THE_LAST,
62                          OPERATOR_TRUE,
63                          OPERATOR_FALSE,
64                          OPERATOR_BETWEEN,
65                          OPERATOR_END
66                        };
67
68   enum FIELD_TYPE { TEXT_FIELD = 0,
69                     NUMERIC_FIELD,
70                     DATE_FIELD,
71                     PLAYLIST_FIELD,
72                     SECONDS_FIELD,
73                     BOOLEAN_FIELD,
74                     TEXTIN_FIELD
75                   };
76
77   virtual bool Load(const TiXmlNode *node, const std::string &encoding = "UTF-8");
78   virtual bool Load(const CVariant &obj);
79   virtual bool Save(TiXmlNode *parent) const;
80   virtual bool Save(CVariant &obj) const;
81
82   CStdString                  GetWhereClause(const CDatabase &db, const CStdString& strType) const;
83   static Field                TranslateField(const char *field);
84   static CStdString           TranslateField(Field field);
85   static SortBy               TranslateOrder(const char *order);
86   static CStdString           TranslateOrder(SortBy order);
87   static CStdString           GetField(Field field, const CStdString& strType);
88   static CStdString           TranslateOperator(SEARCH_OPERATOR oper);
89
90   static CStdString           GetLocalizedField(Field field);
91   static CStdString           GetLocalizedOperator(SEARCH_OPERATOR oper);
92   static std::vector<Field>   GetFields(const CStdString &type);
93   static std::vector<SortBy>  GetOrders(const CStdString &type);
94   static FIELD_TYPE           GetFieldType(Field field);
95   static bool                 IsFieldBrowseable(Field field);
96
97   CStdString                  GetLocalizedRule() const;
98   CStdString                  GetParameter() const;
99   void                        SetParameter(const CStdString &value);
100   void                        SetParameter(const std::vector<CStdString> &values);
101
102   Field                       m_field;
103   SEARCH_OPERATOR             m_operator;
104   std::vector<CStdString>     m_parameter;
105 private:
106   static SEARCH_OPERATOR TranslateOperator(const char *oper);
107
108   CStdString GetVideoResolutionQuery(const CStdString &parameter) const;
109 };
110
111 class CSmartPlaylistRuleCombination;
112
113 typedef std::vector<CSmartPlaylistRule> CSmartPlaylistRules;
114 typedef std::vector<CSmartPlaylistRuleCombination> CSmartPlaylistRuleCombinations;
115
116 class CSmartPlaylistRuleCombination : public ISmartPlaylistRule
117 {
118 public:
119   CSmartPlaylistRuleCombination();
120   virtual ~CSmartPlaylistRuleCombination() { }
121
122   typedef enum {
123     CombinationOr = 0,
124     CombinationAnd
125   } Combination;
126
127   virtual bool Load(const TiXmlNode *node, const std::string &encoding = "UTF-8") { return false; }
128   virtual bool Load(const CVariant &obj);
129   virtual bool Save(TiXmlNode *parent) const { return false; }
130   virtual bool Save(CVariant &obj) const;
131
132   CStdString GetWhereClause(const CDatabase &db, const CStdString& strType, std::set<CStdString> &referencedPlaylists) const;
133   std::string TranslateCombinationType() const;
134
135   Combination GetType() const { return m_type; }
136   void SetType(Combination combination) { m_type = combination; }
137
138   void AddRule(const CSmartPlaylistRule &rule);
139   void AddCombination(const CSmartPlaylistRuleCombination &rule);
140
141 private:
142   friend class CSmartPlaylist;
143   friend class CGUIDialogSmartPlaylistEditor;
144   friend class CGUIDialogMediaFilter;
145
146   Combination m_type;
147   CSmartPlaylistRuleCombinations m_combinations;
148   CSmartPlaylistRules m_rules;
149 };
150
151 class CSmartPlaylist
152 {
153 public:
154   CSmartPlaylist();
155   virtual ~CSmartPlaylist() { }
156
157   bool Load(const CStdString &path);
158   bool Load(const CVariant &obj);
159   bool LoadFromXml(const CStdString &xml);
160   bool LoadFromJson(const CStdString &json);
161   bool Save(const CStdString &path) const;
162   bool Save(CVariant &obj, bool full = true) const;
163   bool SaveAsJson(CStdString &json, bool full = true) const;
164
165   bool OpenAndReadName(const CStdString &path);
166   bool LoadFromXML(const TiXmlNode *root, const CStdString &encoding = "UTF-8");
167
168   void Reset();
169
170   void SetName(const CStdString &name);
171   void SetType(const CStdString &type); // music, video, mixed
172   const CStdString& GetName() const { return m_playlistName; };
173   const CStdString& GetType() const { return m_playlistType; };
174
175   void SetMatchAllRules(bool matchAll) { m_ruleCombination.SetType(matchAll ? CSmartPlaylistRuleCombination::CombinationAnd : CSmartPlaylistRuleCombination::CombinationOr); }
176   bool GetMatchAllRules() const { return m_ruleCombination.GetType() == CSmartPlaylistRuleCombination::CombinationAnd; }
177
178   void SetLimit(unsigned int limit) { m_limit = limit; };
179   unsigned int GetLimit() const { return m_limit; };
180
181   void SetOrder(SortBy order) { m_orderField = order; };
182   SortBy GetOrder() const { return m_orderField; };
183
184   void SetOrderAscending(bool orderAscending) { m_orderDirection = orderAscending ? SortOrderAscending : SortOrderDescending; };
185   bool GetOrderAscending() const { return m_orderDirection != SortOrderDescending; };
186   SortOrder GetOrderDirection() const { return m_orderDirection; }
187
188   /*! \brief get the where clause for a playlist
189    We handle playlists inside playlists separately in order to ensure we don't introduce infinite loops
190    by playlist A including playlist B which also (perhaps via other playlists) then includes playlistA.
191    
192    \param db the database to use to format up results
193    \param referencedPlaylists a set of playlists to know when we reach a cycle
194    \param needWhere whether we need to prepend the where clause with "WHERE "
195    */
196   CStdString GetWhereClause(const CDatabase &db, std::set<CStdString> &referencedPlaylists) const;
197
198   CStdString GetSaveLocation() const;
199
200   static void GetAvailableFields(const std::string &type, std::vector<std::string> &fieldList);
201   static void GetAvailableOperators(std::vector<std::string> &operatorList);
202
203   bool IsEmpty(bool ignoreSortAndLimit = true) const;
204 private:
205   friend class CGUIDialogSmartPlaylistEditor;
206   friend class CGUIDialogMediaFilter;
207
208   const TiXmlNode* readName(const TiXmlNode *root);
209   const TiXmlNode* readNameFromPath(const CStdString &path);
210   const TiXmlNode* readNameFromXml(const CStdString &xml);
211   bool load(const TiXmlNode *root);
212
213   CSmartPlaylistRuleCombination m_ruleCombination;
214   CStdString m_playlistName;
215   CStdString m_playlistType;
216
217   // order information
218   unsigned int m_limit;
219   SortBy m_orderField;
220   SortOrder m_orderDirection;
221
222   CXBMCTinyXML m_xmlDoc;
223 };
224