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