kodi
EpgContainer.h
1 /*
2  * Copyright (C) 2012-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 "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_epg.h"
12 #include "pvr/settings/PVRSettings.h"
13 #include "threads/CriticalSection.h"
14 #include "threads/Event.h"
15 #include "threads/Thread.h"
16 #include "utils/EventStream.h"
17 
18 #include <atomic>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 class CDateTime;
27 
28 namespace PVR
29 {
30  class CEpgUpdateRequest;
31  class CEpgTagStateChange;
32  class CPVREpg;
33  class CPVREpgChannelData;
34  class CPVREpgDatabase;
35  class CPVREpgInfoTag;
36  class CPVREpgSearchFilter;
37 
38  enum class PVREvent;
39 
40  struct PVREpgSearchData;
41 
42  class CPVREpgContainer : private CThread
43  {
44  friend class CPVREpgDatabase;
45 
46  public:
47  CPVREpgContainer() = delete;
48 
52  explicit CPVREpgContainer(CEventSource<PVREvent>& eventSource);
53 
57  ~CPVREpgContainer() override;
58 
63  std::shared_ptr<CPVREpgDatabase> GetEpgDatabase() const;
64 
68  void Start();
69 
73  void Stop();
74 
79  bool Load();
80 
84  void Unload();
85 
90  bool IsStarted() const;
91 
97  bool QueueDeleteEpgs(const std::vector<std::shared_ptr<CPVREpg>>& epgs);
98 
103  void Notify(const PVREvent& event);
104 
112  std::shared_ptr<CPVREpg> CreateChannelEpg(int iEpgId, const std::string& strScraperName, const std::shared_ptr<CPVREpgChannelData>& channelData);
113 
118  std::pair<CDateTime, CDateTime> GetFirstAndLastEPGDate() const;
119 
124  std::vector<std::shared_ptr<CPVREpg>> GetAllEpgs() const;
125 
131  std::shared_ptr<CPVREpg> GetById(int iEpgId) const;
132 
139  std::shared_ptr<CPVREpg> GetByChannelUid(int iClientId, int iChannelUid) const;
140 
147  std::shared_ptr<CPVREpgInfoTag> GetTagById(const std::shared_ptr<const CPVREpg>& epg,
148  unsigned int iBroadcastId) const;
149 
155  std::shared_ptr<CPVREpgInfoTag> GetTagByDatabaseId(int iDatabaseId) const;
156 
162  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetTags(const PVREpgSearchData& searchData) const;
163 
168  void SetHasPendingUpdates(bool bHasPendingUpdates = true);
169 
175  void UpdateRequest(int iClientID, int iUniqueChannelID);
176 
182  void UpdateFromClient(const std::shared_ptr<CPVREpgInfoTag>& tag, EPG_EVENT_STATE eNewState);
183 
188  int GetPastDaysToDisplay() const;
189 
194  int GetFutureDaysToDisplay() const;
195 
199  void OnPlaybackStarted();
200 
204  void OnPlaybackStopped();
205 
209  void OnSystemSleep();
210 
214  void OnSystemWake();
215 
220  int CleanupCachedImages();
221 
227  std::vector<std::shared_ptr<CPVREpgSearchFilter>> GetSavedSearches(bool bRadio);
228 
235  std::shared_ptr<CPVREpgSearchFilter> GetSavedSearchById(bool bRadio, int iId);
236 
243 
249  bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter& epgSearch);
250 
256  bool DeleteSavedSearch(const CPVREpgSearchFilter& search);
257 
258  private:
263  bool CheckPlayingEvents();
264 
269  int NextEpgId();
270 
274  void WaitForUpdateFinish();
275 
282  bool PersistAll(unsigned int iMaxTimeslice) const;
283 
288  bool RemoveOldEntries();
289 
295  bool UpdateEPG(bool bOnlyPending = false);
296 
301  bool InterruptUpdate() const;
302 
306  void Process() override;
307 
311  void LoadFromDatabase();
312 
317  void InsertFromDB(const std::shared_ptr<CPVREpg>& newEpg);
318 
325  bool QueueDeleteEpg(const std::shared_ptr<const CPVREpg>& epg,
326  const std::shared_ptr<CPVREpgDatabase>& database);
327 
328  std::shared_ptr<CPVREpgDatabase> m_database;
330  bool m_bIsUpdating = false;
331  std::atomic<bool> m_bIsInitialising = {
332  true};
333  bool m_bStarted = false;
334  bool m_bLoaded = false;
335  bool m_bPreventUpdates = false;
336  bool m_bPlaying = false;
337  int m_pendingUpdates = 0;
338  time_t m_iLastEpgCleanup = 0;
339  time_t m_iNextEpgUpdate = 0;
340  time_t m_iNextEpgActiveTagCheck = 0;
341  int m_iNextEpgId = 0;
343  std::map<int, std::shared_ptr<CPVREpg>> m_epgIdToEpgMap;
344  std::map<std::pair<int, int>, std::shared_ptr<CPVREpg>> m_channelUidToEpgMap;
346  mutable CCriticalSection m_critSection;
347  CEvent m_updateEvent;
349  std::list<CEpgUpdateRequest> m_updateRequests;
350  CCriticalSection m_updateRequestsLock;
352  std::list<CEpgTagStateChange> m_epgTagChanges;
353  CCriticalSection m_epgTagChangesLock;
355  bool m_bUpdateNotificationPending = false;
356  CPVRSettings m_settings;
357  CEventSource<PVREvent>& m_events;
358 
359  std::atomic<bool> m_bSuspended = {false};
360  };
361 }
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter &epgSearch)
Update time last executed for the given search.
Definition: EpgContainer.cpp:997
int GetFutureDaysToDisplay() const
Get the number of future days to show in the guide and to import from backends.
Definition: EpgContainer.cpp:909
bool IsStarted() const
Check whether the EpgContainer has fully started.
Definition: EpgContainer.cpp:116
Definition: Thread.h:44
Definition: EpgSearchFilter.h:23
Definition: EpgContainer.h:42
Definition: EpgDatabase.h:31
Definition: ContextMenuManager.h:24
void Unload()
unload all EPG data.
Definition: EpgContainer.cpp:158
void Notify(const PVREvent &event)
CEventStream callback for PVR events.
Definition: EpgContainer.cpp:197
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetTags(const PVREpgSearchData &searchData) const
Get all EPG tags matching the given search criteria.
Definition: EpgContainer.cpp:526
std::shared_ptr< CPVREpgSearchFilter > GetSavedSearchById(bool bRadio, int iId)
Get the saved search matching the given id.
Definition: EpgContainer.cpp:968
bool PersistSavedSearch(CPVREpgSearchFilter &search)
Persist a saved search in the database.
Definition: EpgContainer.cpp:980
void Start()
Start the EPG update thread.
Definition: EpgContainer.cpp:128
std::shared_ptr< CPVREpgDatabase > GetEpgDatabase() const
Get a pointer to the database instance.
Definition: EpgContainer.cpp:106
std::shared_ptr< CPVREpg > CreateChannelEpg(int iEpgId, const std::string &strScraperName, const std::shared_ptr< CPVREpgChannelData > &channelData)
Create the EPg for a given channel.
Definition: EpgContainer.cpp:561
void OnPlaybackStopped()
Inform the epg container that playback of an item was stopped due to user interaction.
Definition: EpgContainer.cpp:920
void SetHasPendingUpdates(bool bHasPendingUpdates=true)
Notify EPG container that there are pending manual EPG updates.
Definition: EpgContainer.cpp:883
void UpdateFromClient(const std::shared_ptr< CPVREpgInfoTag > &tag, EPG_EVENT_STATE eNewState)
A client announced an updated epg tag for a channel.
Definition: EpgContainer.cpp:898
bool QueueDeleteEpgs(const std::vector< std::shared_ptr< CPVREpg >> &epgs)
Queue the deletion of the given EPG tables from this container.
Definition: EpgContainer.cpp:619
void Stop()
Stop the EPG update thread.
Definition: EpgContainer.cpp:143
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
std::shared_ptr< CPVREpgInfoTag > GetTagByDatabaseId(int iDatabaseId) const
Get the EPG event with the given database id.
Definition: EpgContainer.cpp:505
void OnSystemSleep()
Inform the epg container that the system is going to sleep.
Definition: EpgContainer.cpp:926
void UpdateRequest(int iClientID, int iUniqueChannelID)
A client triggered an epg update request for a channel.
Definition: EpgContainer.cpp:892
std::shared_ptr< CPVREpgInfoTag > GetTagById(const std::shared_ptr< const CPVREpg > &epg, unsigned int iBroadcastId) const
Get the EPG event with the given event id.
Definition: EpgContainer.cpp:489
~CPVREpgContainer() override
Destroy this instance.
Definition: EpgContainer.cpp:100
void OnSystemWake()
Inform the epg container that the system gets awake from sleep.
Definition: EpgContainer.cpp:931
Definition: EpgSearchData.h:20
std::vector< std::shared_ptr< CPVREpgSearchFilter > > GetSavedSearches(bool bRadio)
Get all saved searches from the database.
Definition: EpgContainer.cpp:956
bool DeleteSavedSearch(const CPVREpgSearchFilter &search)
Delete a saved search from the database.
Definition: EpgContainer.cpp:1009
std::pair< CDateTime, CDateTime > GetFirstAndLastEPGDate() const
Get the start and end time across all EPGs.
Definition: EpgContainer.cpp:812
void OnPlaybackStarted()
Inform the epg container that playback of an item just started.
Definition: EpgContainer.cpp:914
std::shared_ptr< CPVREpg > GetById(int iEpgId) const
Get an EPG given its ID.
Definition: EpgContainer.cpp:459
std::shared_ptr< CPVREpg > GetByChannelUid(int iClientId, int iChannelUid) const
Get an EPG given its client id and channel uid.
Definition: EpgContainer.cpp:474
bool Load()
(re)load EPG data.
Definition: EpgContainer.cpp:153
EPG_EVENT_STATE
Definition: pvr_epg.h:601
int CleanupCachedImages()
Erase stale texture db entries and image files.
Definition: EpgContainer.cpp:936
Definition: PVRSettings.h:27
std::vector< std::shared_ptr< CPVREpg > > GetAllEpgs() const
Get all EPGs.
Definition: EpgContainer.cpp:448
int GetPastDaysToDisplay() const
Get the number of past days to show in the guide and to import from backends.
Definition: EpgContainer.cpp:904