MythTV  master
mythmediamonitor.cpp
Go to the documentation of this file.
1 
2 // Standard C headers
3 #include <cstdio>
4 
5 // C++ headers
6 #include <iostream>
7 #include <typeinfo>
8 using namespace std;
9 
10 // Qt headers
11 #include <QCoreApplication>
12 #include <QFile>
13 #include <QList>
14 #include <QDir>
15 
16 // MythTV headers
17 #include "mythmediamonitor.h"
18 #include "mythcdrom.h"
19 #include "mythcorecontext.h"
20 #include "mythconfig.h"
21 #include "mythdialogbox.h"
22 #include "mythdate.h"
23 #include "mythlogging.h"
24 #include "mythmainwindow.h"
25 #include "mythsystemlegacy.h"
26 #include "mythmiscutil.h"
27 
28 #ifdef USING_DARWIN_DA
29 #include "mediamonitor-darwin.h"
30 #elif CONFIG_CYGWIN || defined(_WIN32)
31 #include "mediamonitor-windows.h"
32 #else
33 #include "mediamonitor-unix.h"
34 #endif
35 
36 static const QString _Location = QObject::tr("Media Monitor");
37 
39 
40 // MonitorThread
41 MonitorThread::MonitorThread(MediaMonitor* pMon, unsigned long interval) :
42  MThread("Monitor")
43 {
44  m_Monitor = pMon;
45  m_Interval = interval;
46  m_lastCheckTime = QDateTime::currentDateTimeUtc();
47 }
48 
49 // Nice and simple, as long as our monitor is valid and active,
50 // loop and check it's devices.
52 {
53  RunProlog();
54  QMutex mtx;
55  mtx.lock();
56  while (m_Monitor && m_Monitor->IsActive())
57  {
58  m_Monitor->CheckDevices();
59  m_Monitor->m_wait.wait(&mtx, m_Interval);
60  QDateTime now(QDateTime::currentDateTimeUtc());
61  // if 10 seconds have elapsed instead of 5 seconds
62  // assume the system was suspended and reconnect
63  // sockets
64  if (m_lastCheckTime.secsTo(now) > 120)
65  {
67  if (HasMythMainWindow())
68  {
69  LOG(VB_GENERAL, LOG_INFO, "Restarting LIRC handler");
71  }
72  }
73  m_lastCheckTime = now;
74  }
75  mtx.unlock();
76  RunEpilog();
77 }
78 
80 // MediaMonitor
81 
82 #define MONITOR_INTERVAL 5000
83 
85 {
86  if (s_monitor)
87  return s_monitor;
88 
89 #ifdef USING_DARWIN_DA
90  s_monitor = new MediaMonitorDarwin(nullptr, MONITOR_INTERVAL, true);
91 #else
92  #if CONFIG_CYGWIN || defined(_WIN32)
93  s_monitor = new MediaMonitorWindows(nullptr, MONITOR_INTERVAL, true);
94  #else
95  s_monitor = new MediaMonitorUnix(nullptr, MONITOR_INTERVAL, true);
96  #endif
97 #endif
98 
99  return s_monitor;
100 }
101 
102 void MediaMonitor::SetCDSpeed(const char *device, int speed)
103 {
104  MediaMonitor *mon = GetMediaMonitor();
105  if (mon)
106  {
107  MythMediaDevice *pMedia = mon->GetMedia(device);
108  if (pMedia && mon->ValidateAndLock(pMedia))
109  {
110  pMedia->setSpeed(speed);
111  mon->Unlock(pMedia);
112  return;
113  }
114  }
115 
116  MythCDROM *cd = MythCDROM::get(nullptr, device, false, false);
117  if (cd)
118  {
119  cd->setDeviceSpeed(device, speed);
120  delete cd;
121  return;
122  }
123 
124  LOG(VB_MEDIA, LOG_INFO,
125  QString("MediaMonitor::setSpeed(%1) - Cannot find/create CDROM?")
126  .arg(device));
127 }
128 
129 // When ejecting one of multiple devices, present a nice name to the user
130 static QString DevName(MythMediaDevice *d)
131 {
132  QString str = d->getVolumeID(); // First choice, the name of the media
133 
134  if (str.isEmpty())
135  {
136  str = d->getDeviceModel(); // otherwise, the drive manufacturer/model
137 
138  if (!str.isEmpty()) // and/or the device node
139  str += " (" + d->getDevicePath() + ')';
140  else
141  str = d->getDevicePath();
142  }
143  // We could add even more information here, but volume names
144  // are usually descriptively unique (i.e. usually good enough)
145  //else
146  // str += " (" + d->getDeviceModel() + ", " + d->getDevicePath() + ')';
147 
148  return str;
149 }
150 
156 QList<MythMediaDevice*> MediaMonitor::GetRemovable(bool showMounted,
157  bool showUsable)
158 {
159  QList <MythMediaDevice *> drives;
160  QList <MythMediaDevice *>::iterator it;
161  QMutexLocker locker(&m_DevicesLock);
162 
163  for (it = m_Devices.begin(); it != m_Devices.end(); ++it)
164  {
165  // By default, we only list CD/DVD devices.
166  // Caller can also request mounted drives to be listed (e.g. USB flash)
167 
168  if (showUsable && !(*it)->isUsable())
169  continue;
170 
171  if (QString(typeid(**it).name()).contains("MythCDROM") ||
172  (showMounted && (*it)->isMounted(false)))
173  drives.append(*it);
174  }
175 
176  return drives;
177 }
178 
185  bool showMounted,
186  bool showUsable)
187 {
188  QList <MythMediaDevice *> drives = GetRemovable(showMounted,
189  showUsable);
190 
191  if (drives.count() == 0)
192  {
193  QString msg = "MediaMonitor::selectDrivePopup() - no removable devices";
194 
195  LOG(VB_MEDIA, LOG_INFO, msg);
196  return nullptr;
197  }
198 
199  if (drives.count() == 1)
200  {
201  LOG(VB_MEDIA, LOG_INFO,
202  "MediaMonitor::selectDrivePopup(" + label +
203  ") - One suitable device");
204  return drives.front();
205  }
206 
208  if (!win)
209  return nullptr;
210 
211  MythScreenStack *stack = win->GetMainStack();
212  if (!stack)
213  return nullptr;
214 
215  // Ignore MENU dialog actions
216  int btnIndex = -2;
217  while (btnIndex < -1)
218  {
219  auto dlg = new MythDialogBox(label, stack, "select drive");
220  if (!dlg->Create())
221  {
222  delete dlg;
223  return nullptr;
224  }
225 
226  // Add button for each drive
227  for (auto drive : drives)
228  dlg->AddButton(DevName(drive));
229 
230  dlg->AddButton(tr("Cancel"));
231 
232  stack->AddScreen(dlg);
233 
234  // Wait in local event loop so events are processed
235  QEventLoop block;
236  connect(dlg, &MythDialogBox::Closed,
237  &block, [&](const QString& /*resultId*/, int result) { block.exit(result); });
238 
239  // Block until dialog closes
240  btnIndex = block.exec();
241  }
242 
243  // If the user cancelled, return a special value
244  return btnIndex < 0 || btnIndex >= drives.size() ? (MythMediaDevice *)-1
245  : drives.at(btnIndex);
246 }
247 
248 
258 {
259  MythMediaDevice *selected =
260  selectDrivePopup(tr("Select removable media to eject or insert"), true);
261 
262  // If the user cancelled, no need to display or do anything more
263  if (selected == (MythMediaDevice *) -1)
264  return;
265 
266  if (!selected)
267  {
268  ShowNotification(tr("No devices to eject"), _Location);
269  return;
270  }
271 
272  AttemptEject(selected);
273 }
274 
275 
276 void MediaMonitor::EjectMedia(const QString &path)
277 {
278  MythMediaDevice *device = GetMedia(path);
279  if (device)
280  AttemptEject(device);
281 }
282 
283 
285 {
286  QString dev = DevName(device);
287 
288  if (device->getStatus() == MEDIASTAT_OPEN)
289  {
290  LOG(VB_MEDIA, LOG_INFO,
291  QString("Disk %1's tray is OPEN. Closing tray").arg(dev));
292 
293  if (device->eject(false) != MEDIAERR_OK)
294  {
295  QString msg =
296  QObject::tr("Unable to open or close the empty drive %1");
297  QString extra =
298  QObject::tr("You may have to use the eject button under its tray");
299  ShowNotificationError(msg.arg(dev), _Location, extra);
300  }
301  return;
302  }
303 
304  if (device->isMounted())
305  {
306  LOG(VB_MEDIA, LOG_INFO,
307  QString("Disk %1 is mounted? Unmounting").arg(dev));
308  device->unmount();
309 
310 #if !CONFIG_DARWIN
311  if (device->isMounted())
312  {
313  ShowNotificationError(tr("Failed to unmount %1").arg(dev),
314  _Location);
315  return;
316  }
317 #endif
318  }
319 
320  LOG(VB_MEDIA, LOG_INFO,
321  QString("Unlocking disk %1, then ejecting").arg(dev));
322  device->unlock();
323 
324  MythMediaError err = device->eject();
325 
326  if (err == MEDIAERR_UNSUPPORTED)
327  {
328  // Physical ejection isn't possible (there is no tray or slot),
329  // but logically the device is now ejected (ignored by the OS).
330  ShowNotification(tr("You may safely remove %1").arg(dev), _Location);
331  }
332  else if (err == MEDIAERR_FAILED)
333  {
334  ShowNotificationError(tr("Failed to eject %1").arg(dev), _Location);
335  }
336 }
337 
344 MediaMonitor::MediaMonitor(QObject* par, unsigned long interval,
345  bool allowEject)
346  : QObject(par), m_DevicesLock(QMutex::Recursive),
347  m_MonitorPollingInterval(interval), m_AllowEject(allowEject)
348 {
349  // User can specify that some devices are not monitored
350  QString ignore = gCoreContext->GetSetting("IgnoreDevices", "");
351 
352  if (ignore.length())
353  m_IgnoreList = ignore.split(',', QString::SkipEmptyParts);
354  else
355  m_IgnoreList = QStringList(); // Force empty list
356 
357  LOG(VB_MEDIA, LOG_NOTICE, "Creating MediaMonitor");
358  LOG(VB_MEDIA, LOG_INFO, "IgnoreDevices=" + ignore);
359 
360  // If any of IgnoreDevices are symlinks, also add the real device
361  QStringList::Iterator dev;
362  for (dev = m_IgnoreList.begin(); dev != m_IgnoreList.end(); ++dev)
363  {
364  auto *fi = new QFileInfo(*dev);
365 
366  if (fi && fi->isSymLink())
367  {
368  QString target = getSymlinkTarget(*dev);
369 
370  if (m_IgnoreList.filter(target).isEmpty())
371  {
372  LOG(VB_MEDIA, LOG_INFO,
373  "Also ignoring " + target + " (symlinked from " +
374  *dev + ").");
375  m_IgnoreList += target;
376  }
377  }
378  delete fi;
379  }
380 }
381 
383 {
384  if (m_Thread)
385  {
386  StopMonitoring();
387  delete m_Thread;
388  m_Thread = nullptr;
389  }
390  QObject::deleteLater();
391 }
392 
401 bool MediaMonitor::RemoveDevice(const QString &dev)
402 {
403  QMutexLocker locker(&m_DevicesLock);
404 
405  QList<MythMediaDevice*>::iterator it;
406  for (it = m_Devices.begin(); it != m_Devices.end(); ++it)
407  {
408  if ((*it)->getDevicePath() == dev)
409  {
410  // Ensure device gets an unmount
411  (*it)->checkMedia();
412 
413  if (m_UseCount[*it] == 0)
414  {
415  m_UseCount.remove(*it);
416  (*it)->deleteLater();
417  m_Devices.erase(it);
418  }
419  else
420  {
421  // Other threads are still using this device
422  // postpone actual delete until they finish.
423  disconnect(*it);
424  m_RemovedDevices.append(*it);
425  m_Devices.erase(it);
426  }
427 
428  return true;
429  }
430  }
431  return false;
432 }
433 
438 {
439  /* check if new devices have been plugged in */
441 
442  QMutexLocker locker(&m_DevicesLock);
443 
444  QList<MythMediaDevice*>::iterator itr = m_Devices.begin();
445  while (itr != m_Devices.end())
446  {
447  MythMediaDevice* pDev = *itr;
448  if (pDev)
449  pDev->checkMedia();
450  ++itr;
451  }
452 }
453 
458 {
459  // Sanity check
460  if (m_Active)
461  return;
462  if (!gCoreContext->GetBoolSetting("MonitorDrives", false)) {
463  LOG(VB_MEDIA, LOG_NOTICE, "MediaMonitor diasabled by user setting.");
464  return;
465  }
466 
467  if (!m_Thread)
469 
470  qRegisterMetaType<MythMediaStatus>("MythMediaStatus");
471 
472  LOG(VB_MEDIA, LOG_NOTICE, "Starting MediaMonitor");
473  m_Active = true;
474  m_Thread->start();
475 }
476 
481 {
482  // Sanity check
483  if (!m_Active)
484  return;
485 
486  LOG(VB_MEDIA, LOG_NOTICE, "Stopping MediaMonitor");
487  m_Active = false;
488  m_wait.wakeAll();
489  m_Thread->wait();
490  LOG(VB_MEDIA, LOG_NOTICE, "Stopped MediaMonitor");
491 }
492 
504 {
505  QMutexLocker locker(&m_DevicesLock);
506 
507  if (!m_Devices.contains(pMedia))
508  return false;
509 
510  m_UseCount[pMedia]++;
511 
512  return true;
513 }
514 
521 {
522  QMutexLocker locker(&m_DevicesLock);
523 
524  if (!m_UseCount.contains(pMedia))
525  return;
526 
527  m_UseCount[pMedia]--;
528 
529  if (m_UseCount[pMedia] == 0 && m_RemovedDevices.contains(pMedia))
530  {
531  m_RemovedDevices.removeAll(pMedia);
532  m_UseCount.remove(pMedia);
533  pMedia->deleteLater();
534  }
535 }
536 
544 {
545  QMutexLocker locker(&m_DevicesLock);
546 
547  QList<MythMediaDevice*>::iterator it = m_Devices.begin();
548  for (;it != m_Devices.end(); ++it)
549  {
550  if ((*it)->isSameDevice(path) &&
551  (((*it)->getStatus() == MEDIASTAT_USEABLE) ||
552  ((*it)->getStatus() == MEDIASTAT_MOUNTED) ||
553  ((*it)->getStatus() == MEDIASTAT_NOTMOUNTED)))
554  {
555  return(*it);
556  }
557  }
558 
559  return nullptr;
560 }
561 
568 QString MediaMonitor::GetMountPath(const QString& devPath)
569 {
570  QString mountPath;
571 
572  if (s_monitor)
573  {
574  MythMediaDevice *pMedia = s_monitor->GetMedia(devPath);
575  if (pMedia && s_monitor->ValidateAndLock(pMedia))
576  {
577  mountPath = pMedia->getMountPath();
578  s_monitor->Unlock(pMedia);
579  }
580  // The media monitor could be inactive.
581  // Create a fake media device just to lookup mount map:
582  else
583  {
584  pMedia = MythCDROM::get(nullptr, devPath.toLatin1(), true, false);
585  if (pMedia && pMedia->findMountPath())
586  mountPath = pMedia->getMountPath();
587  else
588  LOG(VB_MEDIA, LOG_INFO,
589  "MediaMonitor::GetMountPath() - failed");
590  // need some way to delete the media device.
591  }
592  }
593 
594  return mountPath;
595 }
596 
615 QList<MythMediaDevice*> MediaMonitor::GetMedias(unsigned mediatypes)
616 {
617  QMutexLocker locker(&m_DevicesLock);
618 
619  QList<MythMediaDevice*> medias;
620 
621  QList<MythMediaDevice*>::iterator it = m_Devices.begin();
622  for (;it != m_Devices.end(); ++it)
623  {
624  if (((*it)->getMediaType() & mediatypes) &&
625  (((*it)->getStatus() == MEDIASTAT_USEABLE) ||
626  ((*it)->getStatus() == MEDIASTAT_MOUNTED) ||
627  ((*it)->getStatus() == MEDIASTAT_NOTMOUNTED)))
628  {
629  medias.push_back(*it);
630  }
631  }
632 
633  return medias;
634 }
635 
655 void MediaMonitor::RegisterMediaHandler(const QString &destination,
656  const QString &description,
657  void (*callback)
658  (MythMediaDevice*),
659  int mediaType,
660  const QString &extensions)
661 {
662  if (m_handlerMap.count(destination) == 0)
663  {
664  MHData mhd = { callback, mediaType, destination, description };
665  QString msg = MythMediaDevice::MediaTypeString((MythMediaType)mediaType);
666 
667  if (extensions.length())
668  msg += QString(", ext(%1)").arg(extensions, 0, 16);
669 
670  LOG(VB_MEDIA, LOG_INFO,
671  "Registering '" + destination + "' as a media handler for " +
672  msg);
673 
674  m_handlerMap[destination] = mhd;
675 
676  if (extensions.length())
677  MythMediaDevice::RegisterMediaExtensions(mediaType, extensions);
678  }
679  else
680  {
681  LOG(VB_GENERAL, LOG_INFO,
682  destination + " is already registered as a media handler.");
683  }
684 }
685 
694 {
695  QList<MHData> handlers;
696  QMap<QString, MHData>::Iterator itr = m_handlerMap.begin();
697 
698  while (itr != m_handlerMap.end())
699  {
700  if (((*itr).MythMediaType & (int)pMedia->getMediaType()))
701  {
702  LOG(VB_GENERAL, LOG_NOTICE,
703  QString("Found a handler for %1 - '%2'")
704  .arg(pMedia->MediaTypeString()) .arg(itr.key()));
705  handlers.append(*itr);
706  }
707  itr++;
708  }
709 
710  if (handlers.empty())
711  {
712  LOG(VB_MEDIA, LOG_INFO, "No media handler found for event type");
713  return;
714  }
715 
716 
717  // TODO - Generate a dialog, add buttons for each description,
718  // if user didn't cancel, selected = handlers.at(choice);
719  int selected = 0;
720 
721  handlers.at(selected).callback(pMedia);
722 }
723 
729  MythMediaDevice* pMedia)
730 {
731  // If we're not active then ignore signal.
732  if (!m_Active)
733  return;
734 
735  MythMediaStatus stat = pMedia->getStatus();
736  QString msg = QString(" (%1, %2 -> %3)")
737  .arg(pMedia->MediaTypeString())
738  .arg(MythMediaDevice::MediaStatusStrings[oldStatus])
740 
741  // This gets called from outside the main thread so we need
742  // to post an event back to the main thread.
743  // We now send events for all non-error statuses, so plugins get ejects
744  if (stat != MEDIASTAT_ERROR && stat != MEDIASTAT_UNKNOWN &&
745  // Don't send an event for a new device that's not mounted
746  !(oldStatus == MEDIASTAT_UNPLUGGED && stat == MEDIASTAT_NOTMOUNTED))
747  {
748  // Should we ValidateAndLock() first?
749  QEvent *e = new MythMediaEvent(stat, pMedia);
750 
751  LOG(VB_MEDIA, LOG_INFO, "Posting MediaEvent" + msg);
752 
753  // sendEvent() is needed here - it waits for the event to be used.
754  // postEvent() would result in pDevice's media type changing
755  // ... before the plugin's event chain would process it.
756  // Another way would be to send an exact copy of pDevice instead.
757  QCoreApplication::sendEvent((QObject*)GetMythMainWindow(), e);
758  delete e;
759  }
760  else
761  LOG(VB_MEDIA, LOG_INFO,
762  "Media status changed, but not sending event" + msg);
763 
764 
765  if (stat == MEDIASTAT_OPEN || stat == MEDIASTAT_NODISK
766  || stat == MEDIASTAT_UNPLUGGED)
767  {
768  pMedia->clearData();
769  }
770 }
771 
776 {
777  if (m_IgnoreList.contains(device->getMountPath()) ||
778  m_IgnoreList.contains(device->getRealDevice())||
779  m_IgnoreList.contains(device->getDevicePath()) )
780  {
781  LOG(VB_MEDIA, LOG_INFO,
782  "Ignoring device: " + device->getDevicePath());
783  return true;
784  }
785 #if 0
786  else
787  {
788  LOG(VB_MEDIA, LOG_DEBUG,
789  "Not ignoring: " + device->getDevicePath() + " / " +
790  device->getMountPath());
791  LOG(VB_MEDIA, LOG_DEBUG,
792  "Paths not in: " + m_IgnoreList.join(", "));
793  }
794 #endif
795 
796  return false;
797 }
798 
803 bool MediaMonitor::eventFilter(QObject *obj, QEvent *event)
804 {
805  if (event->type() == MythMediaEvent::kEventType)
806  {
807  auto *me = dynamic_cast<MythMediaEvent *>(event);
808  if (me == nullptr)
809  {
810  LOG(VB_GENERAL, LOG_ALERT,
811  "MediaMonitor::eventFilter() couldn't cast event");
812  return true;
813  }
814 
815  MythMediaDevice *pDev = me->getDevice();
816  if (!pDev)
817  {
818  LOG(VB_GENERAL, LOG_ALERT,
819  "MediaMonitor::eventFilter() got a bad media event?");
820  return true;
821  }
822 
823  if (pDev->isUsable())
824  JumpToMediaHandler(pDev);
825  else
826  {
827  // We don't want to jump around in the menus, but should
828  // call each plugin's callback so it can track this change.
829 
830  QMap<QString, MHData>::Iterator itr = m_handlerMap.begin();
831  while (itr != m_handlerMap.end())
832  {
833  if ((*itr).MythMediaType & (int)pDev->getMediaType() ||
834  pDev->getStatus() == MEDIASTAT_OPEN)
835  (*itr).callback(pDev);
836  itr++;
837  }
838  }
839 
840  return false; // Don't eat the event
841  }
842 
843  // standard event processing
844  return QObject::eventFilter(obj, event);
845 }
846 
847 /*
848  * These methods return the user's preferred devices for playing and burning
849  * CDs and DVDs. Traditionally we had a database setting to remember this,
850  * but that is a bit wasteful when most users only have one drive.
851  *
852  * To make it a bit more beginner friendly, if no database default exists,
853  * or if it contains "default", the code tries to find a monitored drive.
854  * If, still, nothing is suitable, a caller hard-coded default is used.
855  *
856  * Ideally, these would return a MythMediaDevice * instead of a QString
857  */
858 
859 QString MediaMonitor::defaultDevice(const QString &dbSetting,
860  const QString &label,
861  const char *hardCodedDefault)
862 {
863  QString device = gCoreContext->GetSetting(dbSetting);
864 
865  LOG(VB_MEDIA, LOG_DEBUG,
866  QString("MediaMonitor::defaultDevice(%1,..,%2) dbSetting='%3'")
867  .arg(dbSetting).arg(hardCodedDefault).arg(device));
868 
869  // No settings database defaults? Try to choose one:
870  if (device.isEmpty() || device == "default")
871  {
872  device = hardCodedDefault;
873 
874  if (!s_monitor)
876 
877  if (s_monitor)
878  {
879  MythMediaDevice *d = s_monitor->selectDrivePopup(label, false, true);
880 
881  if (d == (MythMediaDevice *) -1) // User cancelled
882  {
883  device.clear(); // If user has explicitly cancelled return empty string
884  d = nullptr;
885  }
886 
887  if (d && s_monitor->ValidateAndLock(d))
888  {
889  device = d->getDevicePath();
890  s_monitor->Unlock(d);
891  }
892  }
893  }
894 
895  LOG(VB_MEDIA, LOG_DEBUG,
896  "MediaMonitor::defaultDevice() returning " + device);
897  return device;
898 }
899 
904 {
905  return defaultDevice("CDDevice", tr("Select a CD drive"), DEFAULT_CD);
906 }
907 
912 {
913  return defaultDevice("VCDDeviceLocation",
914  tr("Select a VCD drive"), DEFAULT_CD);
915 }
916 
921 {
922  return defaultDevice("DVDDeviceLocation",
923  tr("Select a DVD drive"), DEFAULT_DVD);
924 }
925 
930 {
931  return defaultDevice("CDWriterDeviceLocation",
932  tr("Select a CD writer"), DEFAULT_CD);
933 }
934 
942 {
943  QString device = defaultDevice("MythArchiveDVDLocation",
944  tr("Select a DVD writer"), DEFAULT_DVD);
945 
946  return device;
947 }
948 
949 
954 {
955  QList<MythMediaDevice*>::const_iterator dev;
956  QStringList list;
957 
958  for (dev = m_Devices.begin(); dev != m_Devices.end(); ++dev)
959  {
960  QString devStr;
961  QString model = (*dev)->getDeviceModel();
962  QString path = (*dev)->getDevicePath();
963  QString real = (*dev)->getRealDevice();
964 
965  if (path != real)
966  devStr += path + "->";
967  devStr += real;
968 
969  if (!model.length())
970  model = "unknown";
971  devStr += " (" + model + ")";
972 
973  list += devStr;
974  }
975 
976  return list.join(", ");
977 }
978 
986 {
988  if (mon)
989  mon->ChooseAndEjectMedia();
990  else
991  {
992  LOG(VB_MEDIA, LOG_INFO, "CD/DVD Monitor isn't enabled.");
993 #ifdef __linux__
994  LOG(VB_MEDIA, LOG_INFO, "Trying Linux 'eject -T' command");
995  myth_system("eject -T");
996 #elif CONFIG_DARWIN
997  QString def = DEFAULT_CD;
998  LOG(VB_MEDIA, LOG_INFO, "Trying 'diskutil eject " + def);
999  myth_system("diskutil eject " + def);
1000 #endif
1001  }
1002 }
1003 
1004 /*
1005  * vim:ts=4:sw=4:ai:et:si:sts=4
1006  */
MythMediaType getMediaType() const
Definition: mythmedia.h:91
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
const QString & getDevicePath() const
Definition: mythmedia.h:61
CD/DVD tray open (meaningless for non-CDs?)
Definition: mythmedia.h:16
QMap< MythMediaDevice *, int > m_UseCount
QMap< QString, MHData > m_handlerMap
Registered Media Handlers.
MythMediaType
Definition: mythmedia.h:24
QPointer< MediaMonitor > m_Monitor
MonitorThread * m_Thread
static QString GetMountPath(const QString &devPath)
If the device is being monitored, return its mountpoint.
unsigned long m_MonitorPollingInterval
void EjectMedia(const QString &path)
bool shouldIgnore(const MythMediaDevice *device)
Check user preferences to see if this device should be monitored.
I am assuming, for now, that everything on Windows uses drive letters (e.g.
Unable to mount, but could be usable.
Definition: mythmedia.h:13
QList< MythMediaDevice * > m_RemovedDevices
static void ejectOpticalDisc(void)
Eject a disk, unmount a drive, open a tray.
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
Basic menu dialog, message and a list of options.
static QString defaultCDWriter()
CDWriterDeviceLocation, user-selected drive, or /dev/cdrom.
MythMediaDevice * GetMedia(const QString &path)
Get media device by pathname.
virtual void StartMonitoring(void)
Start the monitoring thread if needed.
static void RegisterMediaExtensions(uint mediatype, const QString &extensions)
Used to register media types with extensions.
Definition: mythmedia.cpp:297
QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries, unsigned maxLinks)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
static QString defaultDevice(const QString &setting, const QString &label, const char *hardCodedDefault)
Stores details of media handlers.
MythScreenStack * GetMainStack()
static QString defaultDVDdevice()
DVDDeviceLocation, user-selected drive, or /dev/dvd.
unsigned long m_Interval
friend class MonitorThread
virtual void deleteLater(void)
static const char * MediaStatusStrings[]
Definition: mythmedia.h:117
void StopMonitoring(void)
Stop the monitoring thread if needed.
#define MONITOR_INTERVAL
static void SetCDSpeed(const char *device, int speed)
QList< MythMediaDevice * > m_Devices
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
QWaitCondition m_wait
virtual MythMediaError unlock()
Definition: mythmedia.cpp:348
void ShowNotification(const QString &msg, const QString &from, const QString &detail, const VNMask visibility, const MythNotification::Priority priority)
void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia)
Slot which is called when the device status changes and posts a media event to the mainwindow.
static const uint16_t * d
QString GetSetting(const QString &key, const QString &defaultval="")
void CheckDevices(void)
Poll the devices in our list.
MythMediaError
Definition: mythmedia.h:39
static MediaMonitor * GetMediaMonitor(void)
static QString defaultVCDdevice()
VCDDeviceLocation, user-selected drive, or /dev/cdrom.
void ShowNotificationError(const QString &msg, const QString &from, const QString &detail, const VNMask visibility, const MythNotification::Priority priority)
convenience utility to display error message as notification
MonitorThread(MediaMonitor *pMon, unsigned long interval)
static QString defaultCDdevice()
CDDevice, user-selected drive, or /dev/cdrom.
QList< MythMediaDevice * > GetMedias(unsigned mediatypes)
Ask for available media.
static void AttemptEject(MythMediaDevice *device)
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:23
void Unlock(MythMediaDevice *pMedia)
decrements the MythMediaDevices reference count
uint myth_system(const QString &command, uint flags, uint timeout)
const QString & getRealDevice() const
Definition: mythmedia.h:63
bool eventFilter(QObject *obj, QEvent *event) override
Installed into the main window's event chain, so that the main thread can safely jump to plugin code.
void setDeviceSpeed(const char *devicePath, int speed) override
Definition: mythcdrom.cpp:132
virtual void setSpeed(int speed)
Definition: mythmedia.cpp:330
bool HasMythMainWindow(void)
QList< MythMediaDevice * > GetRemovable(bool showMounted=false, bool showUsable=false)
Generate a list of removable drives.
virtual MythMediaError eject(bool open_close=true)
Definition: mythmedia.cpp:305
MythMainWindow * GetMythMainWindow(void)
static MythCDROM * get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Definition: mythcdrom.cpp:35
static Type kEventType
Definition: mythmedia.h:192
bool RemoveDevice(const QString &dev)
Remove a device from the media monitor.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool ValidateAndLock(MythMediaDevice *pMedia)
Validates the MythMediaDevice and increments its reference count.
bool findMountPath()
Try to find a mount of m_DevicePath in the mounts file.
Definition: mythmedia.cpp:364
bool GetBoolSetting(const QString &key, bool defaultval=false)
MediaMonitor(QObject *par, unsigned long interval, bool allowEject)
Lookup some settings, and do OS-specific stuff in sub-classes.
This currently depends on Apple's DiskArbitration framework.
const QString & getMountPath() const
Definition: mythmedia.h:58
void RegisterMediaHandler(const QString &destination, const QString &description, void(*callback)(MythMediaDevice *), int mediaType, const QString &extensions)
Register a handler for media related events.
static QString defaultDVDWriter()
MythArchiveDVDLocation, user-selected drive, or /dev/dvd.
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
static QString DevName(MythMediaDevice *d)
QDateTime m_lastCheckTime
QString listDevices(void)
A string summarising the current devices, for debugging.
#define DEFAULT_CD
bool isUsable() const
Is this device "ready", for a plugin to access?
Definition: mythmedia.h:84
virtual MythMediaStatus checkMedia()=0
QString MediaTypeString()
Definition: mythmedia.cpp:514
void JumpToMediaHandler(MythMediaDevice *pMedia)
Find a relevant jump point for this type of media.
bool isMounted(bool bVerify=true)
Tells us if m_DevicePath is a mounted device.
Definition: mythmedia.cpp:356
CD/DVD tray closed but empty, device unusable.
Definition: mythmedia.h:17
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.
bool volatile m_Active
Was MonitorThread started?
static MediaMonitor * s_monitor
static const QString _Location
void StartLIRC(void)
virtual void CheckDeviceNotifications(void)
void Closed(QString, int)
MythMediaDevice * selectDrivePopup(const QString &label, bool showMounted=false, bool showUsable=false)
List removable drives, let the user select one.
bool unmount()
Definition: mythmedia.h:108
MythMediaStatus getStatus() const
Definition: mythmedia.h:70
QStringList m_IgnoreList
void ChooseAndEjectMedia(void)
Unmounts and ejects removable media devices.
#define DEFAULT_DVD