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