3 * Copyright (C) 2012-2013 Team XBMC
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)
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.
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/>.
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"
34 class CPVRChannelGroup;
35 class CPVRChannelGroupInternal;
36 class CPVRChannelsContainer;
38 class CPVRChannelGroups;
41 /** The PVR database */
43 class CPVRDatabase : public CDatabase
47 * @brief Create a new instance of the PVR database.
49 CPVRDatabase(void) {};
50 virtual ~CPVRDatabase(void) {};
53 * @brief Open the database.
54 * @return True if it was opened successfully, false otherwise.
59 * @brief Get the minimal database version that is required to operate correctly.
60 * @return The minimal database version.
62 virtual int GetSchemaVersion() const { return 22; };
65 * @brief Get the default sqlite database filename.
66 * @return The default filename.
68 const char *GetBaseDBName() const { return "TV"; };
70 /*! @name Channel methods */
74 * @brief Remove all channels from the database.
75 * @return True if all channels were removed, false otherwise.
77 bool DeleteChannels(void);
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.
84 bool DeleteClientChannels(const CPVRClient &client);
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.
92 bool Persist(CPVRChannel &channel, bool bQueueWrite = false);
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.
99 bool Delete(const CPVRChannel &channel);
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.
106 int Get(CPVRChannelGroupInternal &results);
110 /*! @name Channel settings methods */
114 * @brief Remove all channel settings from the database.
115 * @return True if all channels were removed successfully, false if not.
117 bool DeleteChannelSettings();
120 * @brief Remove channel settings from the database.
121 * @return True if channel were removed successfully, false if not.
123 bool DeleteChannelSettings(const CPVRChannel &channel);
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.
131 bool GetChannelSettings(const CPVRChannel &channel, CVideoSettings &settings);
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.
139 bool PersistChannelSettings(const CPVRChannel &channel, const CVideoSettings &settings);
143 /*! @name Channel group methods */
147 * @brief Remove all channel groups from the database
148 * @return True if all channel groups were removed.
150 bool DeleteChannelGroups(void);
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.
157 bool Delete(const CPVRChannelGroup &group);
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.
164 bool Get(CPVRChannelGroups &results);
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.
171 int Get(CPVRChannelGroup &group);
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.
178 bool Persist(CPVRChannelGroup &group);
181 * @brief Reset all epg ids to 0
182 * @return True when reset, false otherwise.
188 /*! @name Client methods */
191 * @brief Remove all client information from the database.
192 * @return True if all clients were removed successfully.
194 bool DeleteClients();
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.
202 int Persist(const ADDON::AddonPtr addon);
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.
209 bool Delete(const CPVRClient &client);
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.
216 int GetClientId(const CStdString &strClientUid);
221 * @brief Create the PVR database tables.
222 * @return True if the tables were created successfully, false otherwise.
225 void CreateAnalytics();
227 bool DeleteChannelsFromGroup(const CPVRChannelGroup &group);
228 bool DeleteChannelsFromGroup(const CPVRChannelGroup &group, const std::vector<int> &channelsToDelete);
230 bool GetCurrentGroupMembers(const CPVRChannelGroup &group, std::vector<int> &members);
231 int GetLastChannelId(void);
232 bool RemoveStaleChannelsFromGroup(const CPVRChannelGroup &group);
235 * @brief Update an old version of the database.
236 * @param version The version to update the database from.
238 void UpdateTables(int version);
239 virtual int GetMinSchemaVersion() const { return 11; }
241 bool PersistGroupMembers(CPVRChannelGroup &group);
243 bool PersistChannels(CPVRChannelGroup &group);
245 bool RemoveChannelsFromGroup(const CPVRChannelGroup &group);
249 * @brief Try to open the PVR database.
250 * @return The opened database or NULL if the database failed to open.
252 inline CPVRDatabase *GetPVRDatabase(void)
254 CPVRDatabase *database = g_PVRManager.GetTVDatabase();
255 if (!database || !database->IsOpen())
257 CLog::Log(LOGERROR, "PVR - failed to open the database");