MythTV  master
mediamonitor-windows.cpp
Go to the documentation of this file.
1 
6 #undef UNICODE
7 #define _WIN32_WINNT 0x0500
8 
9 #include "libmythbase/compat.h"
10 #include "libmythbase/mythcdrom.h"
11 #include "libmythbase/mythhdd.h"
13 
14 #include "mediamonitor-windows.h"
15 
23  unsigned long interval,
24  bool allowEject)
25  : MediaMonitor(par, interval, allowEject)
26 {
27  char strDrives[128];
28  if (!::GetLogicalDriveStrings(sizeof(strDrives), strDrives))
29  {
30  LOG(VB_GENERAL, LOG_ERR,
31  "Error. MediaMonitorWindows failed at GetLogicalDriveStrings.");
32  return;
33  }
34 
35  for (char *driveName = strDrives; *driveName;
36  driveName += strlen(driveName) + 1)
37  {
38  MythMediaDevice *media = nullptr;
39  UINT type = ::GetDriveType(driveName);
40  switch (type)
41  {
42  case DRIVE_CDROM:
43  LOG(VB_MEDIA, LOG_DEBUG,
44  QString("MediaMonitorWindows found cdrom '%1'").arg(driveName));
45  media = MythCDROM::get(this, driveName, false, allowEject);
46  break;
47  case DRIVE_REMOVABLE:
48  LOG(VB_MEDIA, LOG_DEBUG,
49  QString("MediaMonitorWindows found removeable '%1'")
50  .arg(driveName));
51  media = MythHDD::Get(this, driveName, false, allowEject);
52  break;
53  case DRIVE_UNKNOWN:
54  LOG(VB_MEDIA, LOG_DEBUG,
55  QString("MediaMonitorWindows found unknown '%1'")
56  .arg(driveName));
57  media = MythCDROM::get(this, driveName, false, allowEject);
58  break;
59  case DRIVE_NO_ROOT_DIR:
60  LOG(VB_MEDIA, LOG_DEBUG,
61  QString("MediaMonitorWindows found '%1' with no root dir")
62  .arg(driveName));
63  media = MythCDROM::get(this, driveName, false, allowEject);
64  break;
65  default:
66  LOG(VB_MEDIA, LOG_INFO, QString("MediaMonitorWindows found '%1' type %2")
67  .arg(driveName).arg(type));
68  case DRIVE_FIXED:
69  case DRIVE_REMOTE:
70  case DRIVE_RAMDISK:
71  continue;
72  }
73 
74  if (media)
75  {
76  // We store the volume name to improve
77  // user activities like ChooseAndEjectMedia().
78  char volumeName[MAX_PATH];
79  if (GetVolumeInformation(driveName, volumeName, MAX_PATH,
80  nullptr, nullptr, nullptr, nullptr, 0))
81  {
82  media->setVolumeID(volumeName);
83  }
84 
86  }
87  else
88  LOG(VB_GENERAL, LOG_ALERT,
89  "Error. Couldn't create MythMediaDevice.");
90  }
91 
92  LOG(VB_MEDIA, LOG_INFO, "Initial device list: " + listDevices());
93 }
94 
96 {
97  if (!pDevice)
98  {
99  LOG(VB_GENERAL, LOG_ERR, "MediaMonitorWindows::AddDevice(null)");
100  return false;
101  }
102 
103  QString path = pDevice->getDevicePath();
104 
105  //
106  // Check if this is a duplicate of a device we have already added
107  //
108  for (const auto *device : std::as_const(m_devices))
109  {
110  if (device->getDevicePath() == path)
111  {
112  LOG(VB_MEDIA, LOG_INFO,
113  "MediamonitorWindows::AddDevice() -- " +
114  QString("Not adding '%1', it appears to be a duplicate.")
115  .arg(path));
116 
117  return false;
118  }
119  }
120 
121  // TODO - either look up the model, or leave blank
122  pDevice->setDeviceModel(path.toLocal8Bit().constData());
123 
124  QMutexLocker locker(&m_devicesLock);
125  connect(pDevice, &MythMediaDevice::statusChanged,
127  m_devices.push_back(pDevice);
128  m_useCount[pDevice] = 0;
129 
130  return true;
131 }
132 
134 {
135  QStringList list;
136 
137  char strDrives[128];
138  if (::GetLogicalDriveStrings(sizeof(strDrives), strDrives))
139  {
140  for (char* driveName = strDrives; *driveName;
141  driveName += strlen(driveName) + 1)
142  {
143  if (::GetDriveType(driveName) == DRIVE_CDROM)
144  list.append(driveName);
145  }
146  }
147 
148  return list;
149 }
MediaMonitor::m_devices
QList< MythMediaDevice * > m_devices
Definition: mythmediamonitor.h:119
mediamonitor-windows.h
MythMediaDevice::setVolumeID
void setVolumeID(const char *vol)
Definition: mythmedia.h:73
mythcdrom.h
MythMediaDevice::getDevicePath
const QString & getDevicePath() const
Definition: mythmedia.h:61
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MediaMonitor::mediaStatusChanged
void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia) const
Slot which is called when the device status changes and posts a media event to the mainwindow.
Definition: mythmediamonitor.cpp:711
MediaMonitorWindows::MediaMonitorWindows
MediaMonitorWindows(QObject *par, unsigned long interval, bool allowEject)
Definition: mediamonitor-windows.cpp:22
MythMediaDevice::statusChanged
void statusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia)
mythlogging.h
MythMediaDevice::setDeviceModel
void setDeviceModel(const char *model)
Definition: mythmedia.h:68
compat.h
MythHDD::Get
static MythHDD * Get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Helper function used to create a new instance of a hard disk device.
Definition: mythhdd.cpp:15
MediaMonitor
Definition: mythmediamonitor.h:45
MediaMonitor::listDevices
QString listDevices(void)
A string summarising the current devices, for debugging.
Definition: mythmediamonitor.cpp:936
MediaMonitorWindows::AddDevice
bool AddDevice(MythMediaDevice *pDevice) override
Definition: mediamonitor-windows.cpp:95
MediaMonitorWindows::GetCDROMBlockDevices
QStringList GetCDROMBlockDevices(void) override
Definition: mediamonitor-windows.cpp:133
MythCDROM::get
static MythCDROM * get(QObject *par, const QString &devicePath, bool SuperMount, bool AllowEject)
Definition: mythcdrom.cpp:43
MediaMonitor::m_useCount
QMap< MythMediaDevice *, int > m_useCount
Definition: mythmediamonitor.h:121
MythMediaDevice
Definition: mythmedia.h:48
MediaMonitor::m_devicesLock
QRecursiveMutex m_devicesLock
Definition: mythmediamonitor.h:118
mythhdd.h