MythTV  master
mythmediamonitor.h
Go to the documentation of this file.
1 #ifndef MYTH_MEDIA_MONITOR_H
2 #define MYTH_MEDIA_MONITOR_H
3 
4 #include <QStringList>
5 #include <QPointer>
6 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
7 #include <QMutex>
8 #else
9 #include <QRecursiveMutex>
10 #endif
11 #include <QWaitCondition>
12 #include <QList>
13 #include <QDateTime>
14 
15 #include "mthread.h"
16 #include "mythexp.h"
17 #include "mythmedia.h"
18 
20 
21 // Adding member initializers caused compilation to fail with an error
22 // that it cannot convert a brace-enclosed initializer list to MHData.
23 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
24 struct MHData
25 {
26  void (*callback)(MythMediaDevice *mediadevice);
28  QString destination;
29  QString description;
30 };
31 
32 class MediaMonitor;
33 class MonitorThread : public MThread
34 {
35  public:
36  MonitorThread(MediaMonitor* pMon, unsigned long interval);
37  ~MonitorThread() override { wait(); m_monitor = nullptr; }
38  void setMonitor(MediaMonitor* pMon) { m_monitor = pMon; }
39  void run(void) override; // MThread
40 
41  protected:
42  QPointer<MediaMonitor> m_monitor;
43  unsigned long m_interval;
44  QDateTime m_lastCheckTime;
45 };
46 
47 class MPUBLIC MediaMonitor : public QObject
48 {
49  Q_OBJECT
50  friend class MonitorThread;
51  friend class MonitorThreadDarwin;
52 
53  public:
54  virtual void deleteLater(void);
55  bool IsActive(void) const { return m_active; }
56 
57  virtual void StartMonitoring(void);
58  void StopMonitoring(void);
59  void ChooseAndEjectMedia(void);
60  void EjectMedia(const QString &path);
61 
62  static MediaMonitor *GetMediaMonitor(void);
63  static QString GetMountPath(const QString& devPath);
64  static void SetCDSpeed(const char *device, int speed);
65 
66  bool ValidateAndLock(MythMediaDevice *pMedia);
67  void Unlock(MythMediaDevice *pMedia);
68 
69  // To safely dereference the pointers returned by this function
70  // first validate the pointer with ValidateAndLock(), if true is returned
71  // it is safe to dereference the pointer. When finished call Unlock()
72  QList<MythMediaDevice*> GetRemovable(bool showMounted = false,
73  bool showUsable = false);
74  QList<MythMediaDevice*> GetMedias(unsigned mediatypes);
75  MythMediaDevice* GetMedia(const QString &path);
76 
77  void RegisterMediaHandler(const QString &destination,
78  const QString &description,
79  void (*callback) (MythMediaDevice*),
80  int mediaType,
81  const QString &extensions);
82  void JumpToMediaHandler(MythMediaDevice* pMedia);
83 
84  // Plugins should use these if they need to access optical disks:
85  static QString defaultCDdevice();
86  static QString defaultVCDdevice();
87  static QString defaultDVDdevice();
88  static QString defaultCDWriter();
89  static QString defaultDVDWriter();
90 
91  static void ejectOpticalDisc(void);
92 
93  virtual QStringList GetCDROMBlockDevices(void) = 0;
94 
95  public slots:
96  void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice* pMedia) const;
97 
98  protected:
99  MediaMonitor(QObject *par, unsigned long interval, bool allowEject);
100  ~MediaMonitor() override = default;
101 
102  static void AttemptEject(MythMediaDevice *device);
103  void CheckDevices(void);
104  virtual void CheckDeviceNotifications(void) {};
105  virtual bool AddDevice(MythMediaDevice* pDevice) = 0;
106  bool RemoveDevice(const QString &dev);
107  bool shouldIgnore(const MythMediaDevice *device);
108  bool eventFilter(QObject *obj, QEvent *event) override; // QObject
109 
110  QString listDevices(void);
111 
112  static QString defaultDevice(const QString &setting,
113  const QString &label,
114  const char *hardCodedDefault);
115  MythMediaDevice *selectDrivePopup(const QString &label,
116  bool showMounted = false,
117  bool showUsable = false);
118 
119  protected:
120 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
121  QMutex m_devicesLock {QMutex::Recursive};
122 #else
123  QRecursiveMutex m_devicesLock;
124 #endif
125  QList<MythMediaDevice*> m_devices;
126  QList<MythMediaDevice*> m_removedDevices;
127  QMap<MythMediaDevice*, int> m_useCount;
128 
129  // List of devices/mountpoints that the user doesn't want to monitor:
130  QStringList m_ignoreList;
131 
132  bool volatile m_active {false};
133  QWaitCondition m_wait;
137 
138  QMap<QString, MHData> m_handlerMap;
139 
141 };
142 
143 #define REG_MEDIA_HANDLER(a, b, c, d, e) \
144  MediaMonitor::GetMediaMonitor()->RegisterMediaHandler(a, b, c, d, e)
145 
146 #endif // MYTH_MEDIA_MONITOR_H
MediaMonitor::m_devices
QList< MythMediaDevice * > m_devices
Definition: mythmediamonitor.h:125
MonitorThread::MonitorThread
MonitorThread(MediaMonitor *pMon, unsigned long interval)
Definition: mythmediamonitor.cpp:40
MPUBLIC
#define MPUBLIC
Definition: mythexp.h:10
MediaMonitor::m_wait
QWaitCondition m_wait
Definition: mythmediamonitor.h:133
MHData::callback
void(* callback)(MythMediaDevice *mediadevice)
Definition: mythmediamonitor.h:26
MediaMonitor::m_removedDevices
QList< MythMediaDevice * > m_removedDevices
Definition: mythmediamonitor.h:126
MediaMonitor::m_allowEject
bool m_allowEject
Definition: mythmediamonitor.h:136
MonitorThread
Definition: mythmediamonitor.h:33
MonitorThread::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: mythmediamonitor.cpp:50
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:303
mythexp.h
MHData::destination
QString destination
Definition: mythmediamonitor.h:28
MonitorThread::~MonitorThread
~MonitorThread() override
Definition: mythmediamonitor.h:37
MHData::MythMediaType
int MythMediaType
Definition: mythmediamonitor.h:27
MediaMonitor::m_monitorPollingInterval
unsigned long m_monitorPollingInterval
Definition: mythmediamonitor.h:135
MediaMonitor::s_monitor
static MediaMonitor * s_monitor
Definition: mythmediamonitor.h:140
MonitorThread::m_lastCheckTime
QDateTime m_lastCheckTime
Definition: mythmediamonitor.h:44
mythmedia.h
MediaMonitor::CheckDeviceNotifications
virtual void CheckDeviceNotifications(void)
Definition: mythmediamonitor.h:104
MonitorThread::m_monitor
QPointer< MediaMonitor > m_monitor
Definition: mythmediamonitor.h:42
MediaMonitor::m_ignoreList
QStringList m_ignoreList
Definition: mythmediamonitor.h:130
MediaMonitor
Definition: mythmediamonitor.h:47
MediaMonitor::m_handlerMap
QMap< QString, MHData > m_handlerMap
Registered Media Handlers.
Definition: mythmediamonitor.h:138
MediaMonitor::m_useCount
QMap< MythMediaDevice *, int > m_useCount
Definition: mythmediamonitor.h:127
MonitorThreadDarwin
Definition: mediamonitor-darwin.h:7
MediaMonitor::IsActive
bool IsActive(void) const
Definition: mythmediamonitor.h:55
MonitorThread::setMonitor
void setMonitor(MediaMonitor *pMon)
Definition: mythmediamonitor.h:38
MonitorThread::m_interval
unsigned long m_interval
Definition: mythmediamonitor.h:43
MHData
Stores details of media handlers.
Definition: mythmediamonitor.h:24
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
MThread::m_thread
MThreadInternal * m_thread
Definition: mthread.h:135
MythMediaDevice
Definition: mythmedia.h:48
MythMediaStatus
MythMediaStatus
Definition: mythmedia.h:12
MediaMonitor::m_devicesLock
QRecursiveMutex m_devicesLock
Definition: mythmediamonitor.h:123
MHData::description
QString description
Definition: mythmediamonitor.h:29