MythTV  master
mediamonitor-windows.cpp
Go to the documentation of this file.
1 
6 #undef UNICODE
7 #define _WIN32_WINNT 0x0500
8 #include "compat.h"
9 
10 #include "mediamonitor-windows.h"
11 #include "mythcdrom.h"
12 #include "mythhdd.h"
13 #include "mythlogging.h"
14 
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 
85  AddDevice(media);
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  QList<MythMediaDevice*>::const_iterator itr = m_Devices.begin();
109  for (; itr != m_Devices.end(); ++itr)
110  {
111  if ((*itr)->getDevicePath() == path)
112  {
113  LOG(VB_MEDIA, LOG_INFO,
114  "MediamonitorWindows::AddDevice() -- " +
115  QString("Not adding '%1', it appears to be a duplicate.")
116  .arg(path));
117 
118  return false;
119  }
120  }
121 
122  // TODO - either look up the model, or leave blank
123  pDevice->setDeviceModel(path.toLocal8Bit().constData());
124 
125  QMutexLocker locker(&m_DevicesLock);
126  connect(pDevice, SIGNAL(statusChanged(MythMediaStatus, MythMediaDevice*)),
128  m_Devices.push_back(pDevice);
129  m_UseCount[pDevice] = 0;
130 
131  return true;
132 }
133 
135 {
136  QStringList list;
137 
138  char strDrives[128];
139  if (::GetLogicalDriveStrings(sizeof(strDrives), strDrives))
140  {
141  for (char* driveName = strDrives; *driveName;
142  driveName += strlen(driveName) + 1)
143  {
144  if (::GetDriveType(driveName) == DRIVE_CDROM)
145  list.append(driveName);
146  }
147  }
148 
149  return list;
150 }
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
const QString & getDevicePath() const
Definition: mythmedia.h:61
QMap< MythMediaDevice *, int > m_UseCount
void setDeviceModel(const char *model)
Definition: mythmedia.h:68
MediaMonitorWindows(QObject *par, unsigned long interval, bool allowEject)
QStringList GetCDROMBlockDevices(void)
void setVolumeID(const char *vol)
Definition: mythmedia.h:73
QList< MythMediaDevice * > m_Devices
void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia)
Slot which is called when the device status changes and posts a media event to the mainwindow.
static MythCDROM * get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Definition: mythcdrom.cpp:35
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
MythMediaStatus
Definition: mythmedia.h:12
bool AddDevice(MythMediaDevice *pDevice) override
const QString listDevices(void)
A string summarising the current devices, for debugging.