Debian Codegraph @

xbmc/storage/MediaManager.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "MediaManager.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "URL.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/LocalizeStrings.h"
16 #include "utils/URIUtils.h"
17
18 #include <mutex>
19 #ifdef TARGET_WINDOWS
20 #include "platform/win32/WIN32Util.h"
21 #include "utils/CharsetConverter.h"
22 #endif
23 #include "guilib/GUIWindowManager.h"
24 #ifdef HAS_OPTICAL_DRIVE
25 #ifndef TARGET_WINDOWS
26 //! @todo switch all ports to use auto sources
27 #include <map>
28 #include <utility>
29 #include "DetectDVDType.h"
30 #endif
31 #endif
32 #include "Autorun.h"
33 #include "AutorunMediaJob.h"
34 #include "GUIUserMessages.h"
35 #include "addons/VFSEntry.h"
36 #include "dialogs/GUIDialogKaiToast.h"
37 #include "dialogs/GUIDialogPlayEject.h"
38 #include "messaging/helpers/DialogOKHelper.h"
39 #include "settings/AdvancedSettings.h"
40 #include "settings/MediaSourceSettings.h"
41 #include "settings/Settings.h"
42 #include "settings/SettingsComponent.h"
43 #include "utils/FileUtils.h"
44 #include "utils/JobManager.h"
45 #include "utils/StringUtils.h"
46 #include "utils/XBMCTinyXML.h"
47 #include "utils/XMLUtils.h"
48 #include "utils/log.h"
49
50 #include <string>
51 #include <vector>
52
53 #ifdef HAS_OPTICAL_DRIVE
54 using namespace MEDIA_DETECT;
55 #endif
56
57 const char MEDIA_SOURCES_XML[] = { "special://profile/mediasources.xml" };
58
59 CMediaManager::CMediaManager()
60 {
61 m_bhasoptical = false;
62 }
63
64 void CMediaManager::Stop()
65 {
66 if (m_platformStorage)
67 m_platformStorage->Stop();
68
69 m_platformStorage.reset();
70 }
71
72 void CMediaManager::Initialize()
73 {
74 if (!m_platformStorage)
75 {
76 m_platformStorage = IStorageProvider::CreateInstance();
77 }
78 #ifdef HAS_OPTICAL_DRIVE
79 m_platformDiscDriveHander = IDiscDriveHandler::CreateInstance();
80 m_strFirstAvailDrive = m_platformStorage->GetFirstOpticalDeviceFileName();
81 #endif
82 m_platformStorage->Initialize();
83 }
84
85 bool CMediaManager::LoadSources()
86 {
87 // clear our location list
88 m_locations.clear();
89
90 // load xml file...
91 CXBMCTinyXML xmlDoc;
92 if ( !xmlDoc.LoadFile( MEDIA_SOURCES_XML ) )
93 return false;
94
95 TiXmlElement* pRootElement = xmlDoc.RootElement();
96 if (!pRootElement || StringUtils::CompareNoCase(pRootElement->Value(), "mediasources") != 0)
97 {
98 CLog::Log(LOGERROR, "Error loading {}, Line {} ({})", MEDIA_SOURCES_XML, xmlDoc.ErrorRow(),
99 xmlDoc.ErrorDesc());
100 return false;
101 }
102
103 // load the <network> block
104 TiXmlNode *pNetwork = pRootElement->FirstChild("network");
105 if (pNetwork)
106 {
107 TiXmlElement *pLocation = pNetwork->FirstChildElement("location");
108 while (pLocation)
109 {
110 CNetworkLocation location;
111 pLocation->Attribute("id", &location.id);
112 if (pLocation->FirstChild())
113 {
114 location.path = pLocation->FirstChild()->Value();
115 m_locations.push_back(location);
116 }
117 pLocation = pLocation->NextSiblingElement("location");
118 }
119 }
120 LoadAddonSources();
121 return true;
122 }
123
124 bool CMediaManager::SaveSources()
125 {
126 CXBMCTinyXML xmlDoc;
127 TiXmlElement xmlRootElement("mediasources");
128 TiXmlNode *pRoot = xmlDoc.InsertEndChild(xmlRootElement);
129 if (!pRoot) return false;
130
131 TiXmlElement networkNode("network");
132 TiXmlNode *pNetworkNode = pRoot->InsertEndChild(networkNode);
133 if (pNetworkNode)
134 {
135 for (std::vector<CNetworkLocation>::iterator it = m_locations.begin(); it != m_locations.end(); ++it)
136 {
137 TiXmlElement locationNode("location");
138 locationNode.SetAttribute("id", (*it).id);
139 TiXmlText value((*it).path);
140 locationNode.InsertEndChild(value);
141 pNetworkNode->InsertEndChild(locationNode);
142 }
143 }
144 return xmlDoc.SaveFile(MEDIA_SOURCES_XML);
145 }
146
147 void CMediaManager::GetLocalDrives(VECSOURCES &localDrives, bool includeQ)
148 {
149 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
150 m_platformStorage->GetLocalDrives(localDrives);
151 }
152
153 void CMediaManager::GetRemovableDrives(VECSOURCES &removableDrives)
154 {
155 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
156 if (m_platformStorage)
157 m_platformStorage->GetRemovableDrives(removableDrives);
158 }
159
160 void CMediaManager::GetNetworkLocations(VECSOURCES &locations, bool autolocations)
161 {
162 for (unsigned int i = 0; i < m_locations.size(); i++)
163 {
164 CMediaSource share;
165 share.strPath = m_locations[i].path;
166 CURL url(share.strPath);
167 share.strName = url.GetWithoutUserDetails();
168 locations.push_back(share);
169 }
170 if (autolocations)
171 {
172 CMediaSource share;
173 share.m_ignore = true;
174 #ifdef HAS_FILESYSTEM_SMB
175 share.strPath = "smb://";
176 share.strName = g_localizeStrings.Get(20171);
177 locations.push_back(share);
178 #endif
179
180 #ifdef HAS_FILESYSTEM_NFS
181 share.strPath = "nfs://";
182 share.strName = g_localizeStrings.Get(20259);
183 locations.push_back(share);
184 #endif// HAS_FILESYSTEM_NFS
185
186 #ifdef HAS_UPNP
187 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_SERVICES_UPNP))
188 {
189 const std::string& strDevices = g_localizeStrings.Get(33040); //"% Devices"
190 share.strPath = "upnp://";
191 share.strName = StringUtils::Format(strDevices, "UPnP"); //"UPnP Devices"
192 locations.push_back(share);
193 }
194 #endif
195
196 #ifdef HAS_ZEROCONF
197 share.strPath = "zeroconf://";
198 share.strName = g_localizeStrings.Get(20262);
199 locations.push_back(share);
200 #endif
201
202 if (CServiceBroker::IsAddonInterfaceUp())
203 {
204 for (const auto& addon : CServiceBroker::GetVFSAddonCache().GetAddonInstances())
205 {
206 const auto& info = addon->GetProtocolInfo();
207 if (!info.type.empty() && info.supportBrowsing)
208 {
209 share.strPath = info.type + "://";
210 share.strName = g_localizeStrings.GetAddonString(addon->ID(), info.label);
211 if (share.strName.empty())
212 share.strName = g_localizeStrings.Get(info.label);
213 locations.push_back(share);
214 }
215 }
216 }
217 }
218 }
219
220 bool CMediaManager::AddNetworkLocation(const std::string &path)
221 {
222 CNetworkLocation location;
223 location.path = path;
224 location.id = (int)m_locations.size();
225 m_locations.push_back(location);
226 return SaveSources();
227 }
228
229 bool CMediaManager::HasLocation(const std::string& path) const
230 {
231 for (unsigned int i=0;i<m_locations.size();++i)
232 {
233 if (URIUtils::CompareWithoutSlashAtEnd(m_locations[i].path, path))
234 return true;
235 }
236
237 return false;
238 }
239
240
241 bool CMediaManager::RemoveLocation(const std::string& path)
242 {
243 for (unsigned int i=0;i<m_locations.size();++i)
244 {
245 if (URIUtils::CompareWithoutSlashAtEnd(m_locations[i].path, path))
246 {
247 // prompt for sources, remove, cancel,
248 m_locations.erase(m_locations.begin()+i);
249 return SaveSources();
250 }
251 }
252
253 return false;
254 }
255
256 bool CMediaManager::SetLocationPath(const std::string& oldPath, const std::string& newPath)
257 {
258 for (unsigned int i=0;i<m_locations.size();++i)
259 {
260 if (URIUtils::CompareWithoutSlashAtEnd(m_locations[i].path, oldPath))
261 {
262 m_locations[i].path = newPath;
263 return SaveSources();
264 }
265 }
266
267 return false;
268 }
269
270 void CMediaManager::LoadAddonSources() const
271 {
272 if (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bVirtualShares)
273 {
274 CMediaSourceSettings::GetInstance().AddShare("video", GetRootAddonTypeSource("video"));
275 CMediaSourceSettings::GetInstance().AddShare("programs", GetRootAddonTypeSource("programs"));
276 CMediaSourceSettings::GetInstance().AddShare("pictures", GetRootAddonTypeSource("pictures"));
277 CMediaSourceSettings::GetInstance().AddShare("music", GetRootAddonTypeSource("music"));
278 CMediaSourceSettings::GetInstance().AddShare("games", GetRootAddonTypeSource("games"));
279 }
280 }
281
282 CMediaSource CMediaManager::GetRootAddonTypeSource(const std::string& type) const
283 {
284 if (type == "programs" || type == "myprograms")
285 {
286 return ComputeRootAddonTypeSource("executable", g_localizeStrings.Get(1043),
287 "DefaultAddonProgram.png");
288 }
289 else if (type == "video" || type == "videos")
290 {
291 return ComputeRootAddonTypeSource("video", g_localizeStrings.Get(1037),
292 "DefaultAddonVideo.png");
293 }
294 else if (type == "music")
295 {
296 return ComputeRootAddonTypeSource("audio", g_localizeStrings.Get(1038),
297 "DefaultAddonMusic.png");
298 }
299 else if (type == "pictures")
300 {
301 return ComputeRootAddonTypeSource("image", g_localizeStrings.Get(1039),
302 "DefaultAddonPicture.png");
303 }
304 else if (type == "games")
305 {
306 return ComputeRootAddonTypeSource("game", g_localizeStrings.Get(35049), "DefaultAddonGame.png");
307 }
308 else
309 {
310 CLog::LogF(LOGERROR, "Invalid type {} provided", type);
311 return {};
312 }
313 }
314
315 CMediaSource CMediaManager::ComputeRootAddonTypeSource(const std::string& type,
316 const std::string& label,
317 const std::string& thumb) const
318 {
319 CMediaSource source;
320 source.strPath = "addons://sources/" + type + "/";
321 source.strName = label;
322 source.m_strThumbnailImage = thumb;
323 source.m_iDriveType = CMediaSource::SOURCE_TYPE_VPATH;
324 source.m_ignore = true;
325 return source;
326 }
327
328 void CMediaManager::AddAutoSource(const CMediaSource &share, bool bAutorun)
329 {
330 CMediaSourceSettings::GetInstance().AddShare("files", share);
331 CMediaSourceSettings::GetInstance().AddShare("video", share);
332 CMediaSourceSettings::GetInstance().AddShare("pictures", share);
333 CMediaSourceSettings::GetInstance().AddShare("music", share);
334 CMediaSourceSettings::GetInstance().AddShare("programs", share);
335 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_SOURCES);
336 CGUIComponent *gui = CServiceBroker::GetGUI();
337 if (gui)
338 gui->GetWindowManager().SendThreadMessage( msg );
339
340 #ifdef HAS_OPTICAL_DRIVE
341 if(bAutorun)
342 MEDIA_DETECT::CAutorun::ExecuteAutorun(share.strPath);
343 #endif
344 }
345
346 void CMediaManager::RemoveAutoSource(const CMediaSource &share)
347 {
348 CMediaSourceSettings::GetInstance().DeleteSource("files", share.strName, share.strPath, true);
349 CMediaSourceSettings::GetInstance().DeleteSource("video", share.strName, share.strPath, true);
350 CMediaSourceSettings::GetInstance().DeleteSource("pictures", share.strName, share.strPath, true);
351 CMediaSourceSettings::GetInstance().DeleteSource("music", share.strName, share.strPath, true);
352 CMediaSourceSettings::GetInstance().DeleteSource("programs", share.strName, share.strPath, true);
353 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_SOURCES);
354 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage( msg );
355
356 #ifdef HAS_OPTICAL_DRIVE
357 // delete cached CdInfo if any
358 RemoveCdInfo(TranslateDevicePath(share.strPath, true));
359 RemoveDiscInfo(TranslateDevicePath(share.strPath, true));
360 #endif
361 }
362
363 /////////////////////////////////////////////////////////////
364 // AutoSource status functions:
365 //! @todo translate cdda://<device>/
366
367 std::string CMediaManager::TranslateDevicePath(const std::string& devicePath, bool bReturnAsDevice)
368 {
369 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
370 std::string strDevice = devicePath;
371 // fallback for cdda://local/ and empty devicePath
372 #ifdef HAS_OPTICAL_DRIVE
373 if(devicePath.empty() || StringUtils::StartsWith(devicePath, "cdda://local"))
374 strDevice = m_strFirstAvailDrive;
375 #endif
376
377 #ifdef TARGET_WINDOWS
378 if(!m_bhasoptical)
379 return "";
380
381 if(bReturnAsDevice == false)
382 StringUtils::Replace(strDevice, "\\\\.\\","");
383 else if(!strDevice.empty() && strDevice[1]==':')
384 strDevice = StringUtils::Format("\\\\.\\{}:", strDevice[0]);
385
386 URIUtils::RemoveSlashAtEnd(strDevice);
387 #endif
388 return strDevice;
389 }
390
391 bool CMediaManager::IsDiscInDrive(const std::string& devicePath)
392 {
393 #ifdef HAS_OPTICAL_DRIVE
394 #ifdef TARGET_WINDOWS
395 if(!m_bhasoptical)
396 return false;
397
398 std::string strDevice = TranslateDevicePath(devicePath, false);
399 std::map<std::string,CCdInfo*>::iterator it;
400 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
401 it = m_mapCdInfo.find(strDevice);
402 if(it != m_mapCdInfo.end())
403 return true;
404 else
405 return false;
406 #else
407 if(URIUtils::IsDVD(devicePath) || devicePath.empty())
408 return MEDIA_DETECT::CDetectDVDMedia::IsDiscInDrive(); //! @todo switch all ports to use auto sources
409 else
410 return true; // Assume other paths to be mounted already
411 #endif
412 #else
413 return false;
414 #endif
415 }
416
417 bool CMediaManager::IsAudio(const std::string& devicePath)
418 {
419 #ifdef HAS_OPTICAL_DRIVE
420 #ifdef TARGET_WINDOWS
421 if(!m_bhasoptical)
422 return false;
423
424 CCdInfo* pCdInfo = GetCdInfo(devicePath);
425 if(pCdInfo != NULL && pCdInfo->IsAudio(1))
426 return true;
427
428 return false;
429 #else
430 //! @todo switch all ports to use auto sources
431 MEDIA_DETECT::CCdInfo* pInfo = MEDIA_DETECT::CDetectDVDMedia::GetCdInfo();
432 if (pInfo != NULL && pInfo->IsAudio(1))
433 return true;
434 #endif
435 #endif
436 return false;
437 }
438
439 bool CMediaManager::HasOpticalDrive()
440 {
441 #ifdef HAS_OPTICAL_DRIVE
442 if (!m_strFirstAvailDrive.empty())
443 return true;
444 #endif
445 return false;
446 }
447
448 DriveState CMediaManager::GetDriveStatus(const std::string& devicePath)
449 {
450 #ifdef HAS_OPTICAL_DRIVE
451 #ifdef TARGET_WINDOWS
452 if (!m_bhasoptical || !m_platformDiscDriveHander)
453 return DriveState::NOT_READY;
454
455 std::string translatedDevicePath = TranslateDevicePath(devicePath, true);
456 return m_platformDiscDriveHander->GetDriveState(translatedDevicePath);
457 #else
458 return MEDIA_DETECT::CDetectDVDMedia::GetDriveState();
459 #endif
460 #else
461 return DriveState::NOT_READY;
462 #endif
463 }
464
465 #ifdef HAS_OPTICAL_DRIVE
466 CCdInfo* CMediaManager::GetCdInfo(const std::string& devicePath)
467 {
468 #ifdef TARGET_WINDOWS
469 if(!m_bhasoptical)
470 return NULL;
471
472 std::string strDevice = TranslateDevicePath(devicePath, false);
473 std::map<std::string,CCdInfo*>::iterator it;
474 {
475 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
476 it = m_mapCdInfo.find(strDevice);
477 if(it != m_mapCdInfo.end())
478 return it->second;
479 }
480
481 CCdInfo* pCdInfo=NULL;
482 CCdIoSupport cdio;
483 pCdInfo = cdio.GetCdInfo((char*)strDevice.c_str());
484 if(pCdInfo!=NULL)
485 {
486 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
487 m_mapCdInfo.insert(std::pair<std::string,CCdInfo*>(strDevice,pCdInfo));
488 }
489
490 return pCdInfo;
491 #else
492 return MEDIA_DETECT::CDetectDVDMedia::GetCdInfo();
493 #endif
494 }
495
496 bool CMediaManager::RemoveCdInfo(const std::string& devicePath)
497 {
498 if(!m_bhasoptical)
499 return false;
500
501 std::string strDevice = TranslateDevicePath(devicePath, false);
502
503 std::map<std::string,CCdInfo*>::iterator it;
504 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
505 it = m_mapCdInfo.find(strDevice);
506 if(it != m_mapCdInfo.end())
507 {
508 if(it->second != NULL)
509 delete it->second;
510
511 m_mapCdInfo.erase(it);
512 return true;
513 }
514 return false;
515 }
516
517 std::string CMediaManager::GetDiskLabel(const std::string& devicePath)
518 {
519 #ifdef TARGET_WINDOWS_STORE
520 return ""; // GetVolumeInformationW nut support in UWP app
521 #elif defined(TARGET_WINDOWS)
522 if(!m_bhasoptical)
523 return "";
524
525 std::string mediaPath = CServiceBroker::GetMediaManager().TranslateDevicePath(devicePath);
526
527 auto cached = m_mapDiscInfo.find(mediaPath);
528 if (cached != m_mapDiscInfo.end())
529 return cached->second.name;
530
531 // try to minimize the chance of a "device not ready" dialog
532 std::string drivePath = CServiceBroker::GetMediaManager().TranslateDevicePath(devicePath, true);
533 if (CServiceBroker::GetMediaManager().GetDriveStatus(drivePath) !=
534 DriveState::CLOSED_MEDIA_PRESENT)
535 return "";
536
537 UTILS::DISCS::DiscInfo info;
538 info = GetDiscInfo(mediaPath);
539 if (!info.name.empty())
540 {
541 m_mapDiscInfo[mediaPath] = info;
542 return info.name;
543 }
544
545 std::string strDevice = TranslateDevicePath(devicePath);
546 WCHAR cVolumenName[128];
547 WCHAR cFSName[128];
548 URIUtils::AddSlashAtEnd(strDevice);
549 std::wstring strDeviceW;
550 g_charsetConverter.utf8ToW(strDevice, strDeviceW);
551 if(GetVolumeInformationW(strDeviceW.c_str(), cVolumenName, 127, NULL, NULL, NULL, cFSName, 127)==0)
552 return "";
553 g_charsetConverter.wToUTF8(cVolumenName, strDevice);
554 info.name = StringUtils::TrimRight(strDevice, " ");
555 if (!info.name.empty())
556 m_mapDiscInfo[mediaPath] = info;
557
558 return info.name;
559 #else
560 return MEDIA_DETECT::CDetectDVDMedia::GetDVDLabel();
561 #endif
562 }
563
564 std::string CMediaManager::GetDiskUniqueId(const std::string& devicePath)
565 {
566 std::string mediaPath;
567
568 CCdInfo* pInfo = CServiceBroker::GetMediaManager().GetCdInfo(devicePath);
569 if (pInfo == NULL)
570 return "";
571
572 if (pInfo->IsAudio(1))
573 mediaPath = "cdda://local/";
574
575 if (mediaPath.empty() && (pInfo->IsISOUDF(1) || pInfo->IsISOHFS(1) || pInfo->IsIso9660(1) || pInfo->IsIso9660Interactive(1)))
576 mediaPath = "iso9660://";
577
578 if (mediaPath.empty())
579 mediaPath = devicePath;
580
581 #ifdef TARGET_WINDOWS
582 if (mediaPath.empty() || mediaPath == "iso9660://")
583 {
584 mediaPath = CServiceBroker::GetMediaManager().TranslateDevicePath(devicePath);
585 }
586 #endif
587
588 UTILS::DISCS::DiscInfo info = GetDiscInfo(mediaPath);
589 if (info.empty())
590 {
591 CLog::Log(LOGDEBUG, "GetDiskUniqueId: Retrieving ID for path {} failed, ID is empty.",
592 CURL::GetRedacted(mediaPath));
593 return "";
594 }
595
596 std::string strID = StringUtils::Format("removable://{}_{}", info.name, info.serial);
597 CLog::Log(LOGDEBUG, "GetDiskUniqueId: Got ID {} for disc with path {}", strID,
598 CURL::GetRedacted(mediaPath));
599
600 return strID;
601 }
602
603 std::string CMediaManager::GetDiscPath()
604 {
605 #ifdef TARGET_WINDOWS
606 return CServiceBroker::GetMediaManager().TranslateDevicePath("");
607 #else
608
609 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
610 VECSOURCES drives;
611 m_platformStorage->GetRemovableDrives(drives);
612 for(unsigned i = 0; i < drives.size(); ++i)
613 {
614 if(drives[i].m_iDriveType == CMediaSource::SOURCE_TYPE_DVD && !drives[i].strPath.empty())
615 return drives[i].strPath;
616 }
617
618 // iso9660://, cdda://local/ or D:\ depending on disc type
619 return MEDIA_DETECT::CDetectDVDMedia::GetDVDPath();
620 #endif
621 }
622
623 std::shared_ptr<IDiscDriveHandler> CMediaManager::GetDiscDriveHandler()
624 {
625 return m_platformDiscDriveHander;
626 }
627 #endif
628
629 void CMediaManager::SetHasOpticalDrive(bool bstatus)
630 {
631 std::unique_lock<CCriticalSection> waitLock(m_muAutoSource);
632 m_bhasoptical = bstatus;
633 }
634
635 bool CMediaManager::Eject(const std::string& mountpath)
636 {
637 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
638 return m_platformStorage->Eject(mountpath);
639 }
640
641 void CMediaManager::EjectTray( const bool bEject, const char cDriveLetter )
642 {
643 #ifdef HAS_OPTICAL_DRIVE
644 if (m_platformDiscDriveHander)
645 {
646 m_platformDiscDriveHander->EjectDriveTray(TranslateDevicePath(""));
647 }
648 #endif
649 }
650
651 void CMediaManager::CloseTray(const char cDriveLetter)
652 {
653 #ifdef HAS_OPTICAL_DRIVE
654 if (m_platformDiscDriveHander)
655 {
656 m_platformDiscDriveHander->ToggleDriveTray(TranslateDevicePath(""));
657 }
658 #endif
659 }
660
661 void CMediaManager::ToggleTray(const char cDriveLetter)
662 {
663 #ifdef HAS_OPTICAL_DRIVE
664 if (m_platformDiscDriveHander)
665 {
666 m_platformDiscDriveHander->ToggleDriveTray(TranslateDevicePath(""));
667 }
668 #endif
669 }
670
671 void CMediaManager::ProcessEvents()
672 {
673 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
674 if (m_platformStorage->PumpDriveChangeEvents(this))
675 {
676 #if defined(HAS_OPTICAL_DRIVE) && defined(TARGET_DARWIN_OSX)
677 // darwins GetFirstOpticalDeviceFileName only gives us something
678 // when a disc is inserted
679 // so we have to refresh m_strFirstAvailDrive when this happens after Initialize
680 // was called (e.x. the disc was inserted after the start of xbmc)
681 // else TranslateDevicePath wouldn't give the correct device
682 m_strFirstAvailDrive = m_platformStorage->GetFirstOpticalDeviceFileName();
683 #endif
684
685 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_UPDATE_SOURCES);
686 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
687 }
688 }
689
690 std::vector<std::string> CMediaManager::GetDiskUsage()
691 {
692 std::unique_lock<CCriticalSection> lock(m_CritSecStorageProvider);
693 return m_platformStorage->GetDiskUsage();
694 }
695
696 void CMediaManager::OnStorageAdded(const MEDIA_DETECT::STORAGE::StorageDevice& device)
697 {
698 #ifdef HAS_OPTICAL_DRIVE
699 const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
700 if (settings->GetInt(CSettings::SETTING_AUDIOCDS_AUTOACTION) != AUTOCD_NONE || settings->GetBool(CSettings::SETTING_DVDS_AUTORUN))
701 {
702 if (settings->GetInt(CSettings::SETTING_AUDIOCDS_AUTOACTION) == AUTOCD_RIP)
703 {
704 CServiceBroker::GetJobManager()->AddJob(new CAutorunMediaJob(device.label, device.path), this,
705 CJob::PRIORITY_LOW);
706 }
707 else
708 {
709 if (device.type == MEDIA_DETECT::STORAGE::Type::OPTICAL)
710 {
711 if (MEDIA_DETECT::CAutorun::ExecuteAutorun(device.path))
712 {
713 return;
714 }
715 CLog::Log(LOGDEBUG, "{}: Could not execute autorun for optical disc with path {}",
716 __FUNCTION__, device.path);
717 }
718 CServiceBroker::GetJobManager()->AddJob(new CAutorunMediaJob(device.label, device.path), this,
719 CJob::PRIORITY_HIGH);
720 }
721 }
722 else
723 {
724 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(13021),
725 device.label, TOAST_DISPLAY_TIME, false);
726 }
727 #endif
728 }
729
730 void CMediaManager::OnStorageSafelyRemoved(const MEDIA_DETECT::STORAGE::StorageDevice& device)
731 {
732 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(13023),
733 device.label, TOAST_DISPLAY_TIME, false);
734 }
735
736 void CMediaManager::OnStorageUnsafelyRemoved(const MEDIA_DETECT::STORAGE::StorageDevice& device)
737 {
738 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(13022),
739 device.label);
740 }
741
742 UTILS::DISCS::DiscInfo CMediaManager::GetDiscInfo(const std::string& mediaPath)
743 {
744 UTILS::DISCS::DiscInfo info;
745
746 if (mediaPath.empty())
747 return info;
748
749 // Try finding VIDEO_TS/VIDEO_TS.IFO - this indicates a DVD disc is inserted
750 std::string pathVideoTS = URIUtils::AddFileToFolder(mediaPath, "VIDEO_TS", "VIDEO_TS.IFO");
751 // correct the filename if needed
752 if (StringUtils::StartsWith(mediaPath, "dvd://") ||
753 StringUtils::StartsWith(mediaPath, "iso9660://"))
754 {
755 pathVideoTS = TranslateDevicePath("");
756 }
757
758 // check for DVD discs
759 if (CFileUtils::Exists(pathVideoTS))
760 {
761 info = UTILS::DISCS::ProbeDVDDiscInfo(pathVideoTS);
762 if (!info.empty())
763 return info;
764 }
765 // check for Blu-ray discs
766 if (CFileUtils::Exists(URIUtils::AddFileToFolder(mediaPath, "BDMV", "index.bdmv")))
767 {
768 info = UTILS::DISCS::ProbeBlurayDiscInfo(mediaPath);
769 }
770
771 return info;
772 }
773
774 void CMediaManager::RemoveDiscInfo(const std::string& devicePath)
775 {
776 std::string strDevice = TranslateDevicePath(devicePath, false);
777
778 auto it = m_mapDiscInfo.find(strDevice);
779 if (it != m_mapDiscInfo.end())
780 m_mapDiscInfo.erase(it);
781 }
782
783 bool CMediaManager::playStubFile(const CFileItem& item)
784 {
785 // Figure out Lines 1 and 2 of the dialog
786 std::string strLine1, strLine2;
787
788 // use generic message by default
789 strLine1 = g_localizeStrings.Get(435).c_str();
790 strLine2 = g_localizeStrings.Get(436).c_str();
791
792 CXBMCTinyXML discStubXML;
793 if (discStubXML.LoadFile(item.GetPath()))
794 {
795 TiXmlElement* pRootElement = discStubXML.RootElement();
796 if (!pRootElement || StringUtils::CompareNoCase(pRootElement->Value(), "discstub") != 0)
797 CLog::Log(LOGINFO, "No <discstub> node found for {}. Using default info dialog message",
798 item.GetPath());
799 else
800 {
801 XMLUtils::GetString(pRootElement, "title", strLine1);
802 XMLUtils::GetString(pRootElement, "message", strLine2);
803 // no title? use the label of the CFileItem as line 1
804 if (strLine1.empty())
805 strLine1 = item.GetLabel();
806 }
807 }
808
809 if (HasOpticalDrive())
810 {
811 #ifdef HAS_OPTICAL_DRIVE
812 if (CGUIDialogPlayEject::ShowAndGetInput(strLine1, strLine2))
813 return MEDIA_DETECT::CAutorun::PlayDiscAskResume();
814 #endif
815 }
816 else
817 {
818 KODI::MESSAGING::HELPERS::ShowOKDialogText(strLine1, strLine2);
819 }
820 return true;
821 }