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