MythTV  master
programinfocache.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // vim:set sw=4 ts=4 expandtab:
3 #ifndef PROGRAM_INFO_CACHE_H
4 #define PROGRAM_INFO_CACHE_H
5 
6 // C++ headers
7 #include <cstdint>
8 #include <vector>
9 
10 // Qt headers
11 #include <QWaitCondition>
12 #include <QDateTime>
13 #include <QMutex>
14 #include <QHash>
15 
16 class ProgramInfoLoader;
17 class ProgramInfo;
18 class QObject;
19 
21 {
22  friend class ProgramInfoLoader;
23 
24  public:
25  enum UpdateState {
26  PIC_NONE = 0x00,
29  PIC_NO_ACTION = 0x80,
30  };
31  Q_DECLARE_FLAGS(UpdateStates, UpdateState);
32 
33  explicit ProgramInfoCache(QObject *o)
34  : m_listener(o) {}
36 
37  void ScheduleLoad(bool updateUI = true);
38  bool IsLoadInProgress(void) const;
39  void WaitForLoadToComplete(void) const;
40 
41  // All the following public methods must only be called from the UI Thread.
42  void Refresh(void);
43  void Add(const ProgramInfo &pginfo);
44  bool Remove(uint recordingID);
45  ProgramInfoCache::UpdateStates Update(const ProgramInfo &pginfo);
46  void UpdateFileSize(uint recordingID, uint64_t filesize, UpdateStates flags);
47  void GetOrdered(std::vector<ProgramInfo*> &list, bool newest_first = false);
49  bool empty(void) const { return m_cache.empty(); }
50  ProgramInfo *GetRecordingInfo(uint recordingID) const;
51 
52  private:
53  void Load(bool updateUI = true);
54  void Clear(void);
55 
56  private:
57  // NOTE: Hash would be faster for lookups and updates, but we need a sorted
58  // list for to rebuild the full list. Question is, which is done more?
59  // We could store a hash, but sort the vector in GetOrdered which might
60  // be a suitable compromise, fractionally slower initial load but faster
61  // scrolling and updates
62  using Cache = QHash<uint,ProgramInfo*>;
63 
64  mutable QMutex m_lock;
66  std::vector<ProgramInfo*> *m_nextCache {nullptr};
67  QObject *m_listener {nullptr};
68  bool m_loadIsQueued {false};
70  mutable QWaitCondition m_loadWait;
71 };
72 
73 Q_DECLARE_OPERATORS_FOR_FLAGS(ProgramInfoCache::UpdateStates)
74 
75 #endif // PROGRAM_INFO_CACHE_H
ProgramInfoCache::Add
void Add(const ProgramInfo &pginfo)
Adds a ProgramInfo to the cache.
Definition: programinfocache.cpp:272
ProgramInfoCache::m_cache
Cache m_cache
Definition: programinfocache.h:65
ProgramInfoCache::m_loadIsQueued
bool m_loadIsQueued
Definition: programinfocache.h:68
ProgramInfoCache::ProgramInfoCache
ProgramInfoCache(QObject *o)
Definition: programinfocache.h:33
ProgramInfoCache
Definition: programinfocache.h:20
ProgramInfoCache::GetOrdered
void GetOrdered(std::vector< ProgramInfo * > &list, bool newest_first=false)
Definition: programinfocache.cpp:313
ProgramInfoCache::PIC_RECGROUP_CHANGED
@ PIC_RECGROUP_CHANGED
Definition: programinfocache.h:28
ProgramInfoCache::empty
bool empty(void) const
Definition: programinfocache.h:49
ProgramInfoCache::m_listener
QObject * m_listener
Definition: programinfocache.h:67
ProgramInfoCache::m_loadsInProgress
uint m_loadsInProgress
Definition: programinfocache.h:69
ProgramInfoCache::UpdateState
UpdateState
Definition: programinfocache.h:25
ProgramInfoCache::Q_DECLARE_FLAGS
Q_DECLARE_FLAGS(UpdateStates, UpdateState)
ProgramInfoCache::ScheduleLoad
void ScheduleLoad(bool updateUI=true)
Definition: programinfocache.cpp:64
ProgramInfoCache::Update
ProgramInfoCache::UpdateStates Update(const ProgramInfo &pginfo)
Updates a ProgramInfo in the cache.
Definition: programinfocache.cpp:194
ProgramInfoCache::GetRecordingInfo
ProgramInfo * GetRecordingInfo(uint recordingID) const
Definition: programinfocache.cpp:324
ProgramInfoCache::Refresh
void Refresh(void)
Refreshed the cache.
Definition: programinfocache.cpp:158
uint
unsigned int uint
Definition: compat.h:81
ProgramInfoCache::WaitForLoadToComplete
void WaitForLoadToComplete(void) const
Definition: programinfocache.cpp:141
ProgramInfoCache::PIC_NO_ACTION
@ PIC_NO_ACTION
Definition: programinfocache.h:29
ProgramInfoCache::IsLoadInProgress
bool IsLoadInProgress(void) const
Definition: programinfocache.cpp:135
ProgramInfoCache::Load
void Load(bool updateUI=true)
Definition: programinfocache.cpp:76
ProgramInfoCache::m_lock
QMutex m_lock
Definition: programinfocache.h:64
ProgramInfoLoader
Definition: programinfocache.cpp:38
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
ProgramInfoCache::m_loadWait
QWaitCondition m_loadWait
Definition: programinfocache.h:70
ProgramInfoCache::PIC_MARK_CHANGED
@ PIC_MARK_CHANGED
Definition: programinfocache.h:27
ProgramInfoCache::UpdateFileSize
void UpdateFileSize(uint recordingID, uint64_t filesize, UpdateStates flags)
Updates a ProgramInfo in the cache.
Definition: programinfocache.cpp:234
ProgramInfoCache::~ProgramInfoCache
~ProgramInfoCache()
Definition: programinfocache.cpp:53
ProgramInfoCache::Remove
bool Remove(uint recordingID)
Marks a ProgramInfo in the cache for deletion on the next call to Refresh().
Definition: programinfocache.cpp:285
ProgramInfoCache::Cache
QHash< uint, ProgramInfo * > Cache
Definition: programinfocache.h:62
ProgramInfoCache::Clear
void Clear(void)
Clears the cache, m_lock must be held when this is called.
Definition: programinfocache.cpp:335
ProgramInfoCache::PIC_NONE
@ PIC_NONE
Definition: programinfocache.h:26
ProgramInfoCache::m_nextCache
std::vector< ProgramInfo * > * m_nextCache
Definition: programinfocache.h:66