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 #pragma once
10 
11 #include "IStorageProvider.h"
12 #include "MediaSource.h" // for VECSOURCES
13 #include "threads/CriticalSection.h"
14 #include "utils/Job.h"
15 
16 #include <map>
17 #include <vector>
18 
19 #include "PlatformDefs.h"
20 
21 #define TRAY_OPEN     16
22 #define TRAY_CLOSED_NO_MEDIA  64
23 #define TRAY_CLOSED_MEDIA_PRESENT 96
24 
25 #define DRIVE_OPEN      0 // Open...
26 #define DRIVE_NOT_READY     1 // Opening.. Closing...
27 #define DRIVE_READY      2
28 #define DRIVE_CLOSED_NO_MEDIA   3 // CLOSED...but no media in drive
29 #define DRIVE_CLOSED_MEDIA_PRESENT  4 // Will be send once when the drive just have closed
30 #define DRIVE_NONE  5 // system doesn't have an optical drive
31 
32 class CFileItem;
33 
34 class CNetworkLocation
35 {
36 public:
CNetworkLocation()37   CNetworkLocation() { id = 0; };
38   int id;
39   std::string path;
40 };
41 
42 class CMediaManager : public IStorageEventsCallback, public IJobCallback
43 {
44 public:
45   CMediaManager();
46 
47   void Initialize();
48   void Stop();
49 
50   bool LoadSources();
51   bool SaveSources();
52 
53   void GetLocalDrives(VECSOURCES &localDrives, bool includeQ = true);
54   void GetRemovableDrives(VECSOURCES &removableDrives);
55   void GetNetworkLocations(VECSOURCES &locations, bool autolocations = true);
56 
57   bool AddNetworkLocation(const std::string &path);
58   bool HasLocation(const std::string& path) const;
59   bool RemoveLocation(const std::string& path);
60   bool SetLocationPath(const std::string& oldPath, const std::string& newPath);
61 
62   void AddAutoSource(const CMediaSource &share, bool bAutorun=false);
63   void RemoveAutoSource(const CMediaSource &share);
64   bool IsDiscInDrive(const std::string& devicePath="");
65   bool IsAudio(const std::string& devicePath="");
66   bool HasOpticalDrive();
67   std::string TranslateDevicePath(const std::string& devicePath, bool bReturnAsDevice=false);
68   DWORD GetDriveStatus(const std::string& devicePath="");
69 #ifdef HAS_DVD_DRIVE
70   MEDIA_DETECT::CCdInfo* GetCdInfo(const std::string& devicePath="");
71   bool RemoveCdInfo(const std::string& devicePath="");
72   std::string GetDiskLabel(const std::string& devicePath="");
73   std::string GetDiskUniqueId(const std::string& devicePath="");
74 #endif
75   std::string GetDiscPath();
76   void SetHasOpticalDrive(bool bstatus);
77 
78   bool Eject(const std::string& mountpath);
79   void EjectTray( const bool bEject=true, const char cDriveLetter='\0' );
80   void CloseTray(const char cDriveLetter='\0');
81   void ToggleTray(const char cDriveLetter='\0');
82 
83   void ProcessEvents();
84 
85   std::vector<std::string> GetDiskUsage();
86 
87   void OnStorageAdded(const std::string &label, const std::string &path) override;
88   void OnStorageSafelyRemoved(const std::string &label) override;
89   void OnStorageUnsafelyRemoved(const std::string &label) override;
90 
OnJobComplete(unsigned int jobID,bool success,CJob * job)91   void OnJobComplete(unsigned int jobID, bool success, CJob *job) override { }
92 
93   bool playStubFile(const CFileItem& item);
94 
95 protected:
96   std::vector<CNetworkLocation> m_locations;
97 
98   CCriticalSection m_muAutoSource, m_CritSecStorageProvider;
99 #ifdef HAS_DVD_DRIVE
100   std::map<std::string,MEDIA_DETECT::CCdInfo*> m_mapCdInfo;
101 #endif
102   bool m_bhasoptical;
103   std::string m_strFirstAvailDrive;
104 
105 private:
106   IStorageProvider *m_platformStorage;
107 
108   struct DiscInfo
109   {
110     std::string name;
111     std::string serial;
112     std::string type;
113 
emptyDiscInfo114     bool empty()
115     {
116       return (name.empty() && serial.empty());
117     }
118   };
119 
120   DiscInfo GetDiscInfo(const std::string& mediaPath);
121   void RemoveDiscInfo(const std::string& devicePath);
122   std::map<std::string, DiscInfo> m_mapDiscInfo;
123 };
124