Remove LiveTV menu.
[vuplus_xbmc] / xbmc / pvr / PVRDatabase.h
1 #pragma once
2 /*
3  *      Copyright (C) 2012-2013 Team XBMC
4  *      http://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 "addons/Addon.h"
23 #include "addons/AddonDll.h"
24 #include "addons/DllPVRClient.h"
25 #include "PVRManager.h"
26 #include "dbwrappers/Database.h"
27 #include "XBDateTime.h"
28 #include "utils/log.h"
29
30 class CVideoSettings;
31
32 namespace PVR
33 {
34   class CPVRChannelGroup;
35   class CPVRChannelGroupInternal;
36   class CPVRChannelsContainer;
37   class CPVRChannel;
38   class CPVRChannelGroups;
39   class CPVRClient;
40
41   /** The PVR database */
42
43   class CPVRDatabase : public CDatabase
44   {
45   public:
46     /*!
47      * @brief Create a new instance of the PVR database.
48      */
49     CPVRDatabase(void) {};
50     virtual ~CPVRDatabase(void) {};
51
52     /*!
53      * @brief Open the database.
54      * @return True if it was opened successfully, false otherwise.
55      */
56     virtual bool Open();
57
58     /*!
59      * @brief Get the minimal database version that is required to operate correctly.
60      * @return The minimal database version.
61      */
62     virtual int GetSchemaVersion() const { return 22; };
63
64     /*!
65      * @brief Get the default sqlite database filename.
66      * @return The default filename.
67      */
68     const char *GetBaseDBName() const { return "TV"; };
69
70     /*! @name Channel methods */
71     //@{
72
73     /*!
74      * @brief Remove all channels from the database.
75      * @return True if all channels were removed, false otherwise.
76      */
77     bool DeleteChannels(void);
78
79     /*!
80      * @brief Remove all channels from a client from the database.
81      * @param client The client to delete the channels for.
82      * @return True if the channels were deleted, false otherwise.
83      */
84     bool DeleteClientChannels(const CPVRClient &client);
85
86     /*!
87      * @brief Add or update a channel entry in the database
88      * @param channel The channel to persist.
89      * @param bQueueWrite If true, don't write immediately
90      * @return True when persisted or queued, false otherwise.
91      */
92     bool Persist(CPVRChannel &channel, bool bQueueWrite = false);
93
94     /*!
95      * @brief Remove a channel entry from the database
96      * @param channel The channel to remove.
97      * @return True if the channel was removed, false otherwise.
98      */
99     bool Delete(const CPVRChannel &channel);
100
101     /*!
102      * @brief Get the list of channels from the database
103      * @param results The channel group to store the results in.
104      * @return The amount of channels that were added.
105      */
106     int Get(CPVRChannelGroupInternal &results);
107
108     //@}
109
110     /*! @name Channel settings methods */
111     //@{
112
113     /*!
114      * @brief Remove all channel settings from the database.
115      * @return True if all channels were removed successfully, false if not.
116      */
117     bool DeleteChannelSettings();
118
119     /*!
120      * @brief Remove channel settings from the database.
121      * @return True if channel were removed successfully, false if not.
122      */
123     bool DeleteChannelSettings(const CPVRChannel &channel);
124
125     /*!
126      * @brief Get the channel settings from the database.
127      * @param channel The channel to get the settings for.
128      * @param settings Store the settings in here.
129      * @return True if the settings were fetched successfully, false if not.
130      */
131     bool GetChannelSettings(const CPVRChannel &channel, CVideoSettings &settings);
132
133     /*!
134      * @brief Store channel settings in the database.
135      * @param channel The channel to store the settings for.
136      * @param settings The settings to store.
137      * @return True if the settings were stored successfully, false if not.
138      */
139     bool PersistChannelSettings(const CPVRChannel &channel, const CVideoSettings &settings);
140
141     //@}
142
143     /*! @name Channel group methods */
144     //@{
145
146     /*!
147      * @brief Remove all channel groups from the database
148      * @return True if all channel groups were removed.
149      */
150     bool DeleteChannelGroups(void);
151
152     /*!
153      * @brief Delete a channel group from the database.
154      * @param group The group to delete.
155      * @return True if the group was deleted successfully, false otherwise.
156      */
157     bool Delete(const CPVRChannelGroup &group);
158
159     /*!
160      * @brief Get the channel groups.
161      * @param results The container to store the results in.
162      * @return True if the list was fetched successfully, false otherwise.
163      */
164     bool Get(CPVRChannelGroups &results);
165
166     /*!
167      * @brief Add the group members to a group.
168      * @param group The group to get the channels for.
169      * @return The amount of channels that were added.
170      */
171     int Get(CPVRChannelGroup &group);
172
173     /*!
174      * @brief Add or update a channel group entry in the database.
175      * @param group The group to persist.
176      * @return True if the group was persisted successfully, false otherwise.
177      */
178     bool Persist(CPVRChannelGroup &group);
179
180     /*!
181      * @brief Reset all epg ids to 0
182      * @return True when reset, false otherwise.
183      */
184     bool ResetEPG(void);
185
186     //@}
187
188     /*! @name Client methods */
189     //@{
190     /*!
191      * @brief Remove all client information from the database.
192      * @return True if all clients were removed successfully.
193      */
194     bool DeleteClients();
195
196     /*!
197      * @brief Add a client to the database if it's not already in there.
198      * @param strClientName The name of the client.
199      * @param strGuid The unique ID of the client.
200      * @return The database ID of the client.
201      */
202     int Persist(const ADDON::AddonPtr addon);
203
204     /*!
205      * @brief Remove a client from the database
206      * @param strGuid The unique ID of the client.
207      * @return True if the client was removed successfully, false otherwise.
208      */
209     bool Delete(const CPVRClient &client);
210
211     /*!
212      * @brief Get the database ID of a client.
213      * @param strClientUid The unique ID of the client.
214      * @return The database ID of the client or -1 if it wasn't found.
215      */
216     int GetClientId(const CStdString &strClientUid);
217     //@}
218
219   private:
220     /*!
221      * @brief Create the PVR database tables.
222      * @return True if the tables were created successfully, false otherwise.
223      */
224     void CreateTables();
225     void CreateAnalytics();
226
227     bool DeleteChannelsFromGroup(const CPVRChannelGroup &group);
228     bool DeleteChannelsFromGroup(const CPVRChannelGroup &group, const std::vector<int> &channelsToDelete);
229
230     bool GetCurrentGroupMembers(const CPVRChannelGroup &group, std::vector<int> &members);
231     int GetLastChannelId(void);
232     bool RemoveStaleChannelsFromGroup(const CPVRChannelGroup &group);
233
234     /*!
235      * @brief Update an old version of the database.
236      * @param version The version to update the database from.
237      */
238     void UpdateTables(int version);
239     virtual int GetMinSchemaVersion() const { return 11; }
240
241     bool PersistGroupMembers(CPVRChannelGroup &group);
242
243     bool PersistChannels(CPVRChannelGroup &group);
244
245     bool RemoveChannelsFromGroup(const CPVRChannelGroup &group);
246   };
247
248   /*!
249    * @brief Try to open the PVR database.
250    * @return The opened database or NULL if the database failed to open.
251    */
252   inline CPVRDatabase *GetPVRDatabase(void)
253   {
254     CPVRDatabase *database = g_PVRManager.GetTVDatabase();
255     if (!database || !database->IsOpen())
256     {
257       CLog::Log(LOGERROR, "PVR - failed to open the database");
258       database = NULL;
259     }
260
261     return database;
262   }
263 }