kodi
UDisks2Provider.h
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 "DBusUtil.h"
12 #include "storage/IStorageProvider.h"
13 
14 #include <string>
15 #include <vector>
16 
18 {
19  class Drive
20  {
21  public:
22  std::string m_object;
23  bool m_isRemovable = false;
24  std::vector<std::string> m_mediaCompatibility;
25 
26  explicit Drive(const char *object);
27  ~Drive() = default;
28 
32  bool IsOptical() const;
33 
37  std::string ToString() const;
38  };
39 
40  class Block
41  {
42  public:
43  Drive *m_drive = nullptr;
44  std::string m_object;
45  std::string m_driveobject;
46  std::string m_label;
47  std::string m_device;
48  bool m_isSystem = false;
49  u_int64_t m_size = 0;
50 
51  explicit Block(const char *object);
52  ~Block() = default;
53 
54  bool IsReady();
55 
59  std::string ToString() const;
60  };
61 
62  class Filesystem
63  {
64  public:
65  Block *m_block = nullptr;
66 
67  explicit Filesystem(const char *object);
68  ~Filesystem() = default;
69 
70  bool Mount();
71  bool Unmount();
72 
76  std::string GetDisplayName() const;
77 
81  bool IsApproved() const;
82 
86  bool IsMounted() const;
87 
91  bool IsReady() const;
92 
96  bool IsOptical() const;
97 
102  MEDIA_DETECT::STORAGE::Type GetStorageType() const;
103 
107  std::string GetMountPoint() const;
108 
111  void ResetMountPoint();
112 
116  void SetMountPoint(const std::string& mountPoint);
117 
121  std::string GetObject() const;
122 
126  std::string ToString() const;
127 
131  CMediaSource ToMediaShare() const;
132 
136  MEDIA_DETECT::STORAGE::StorageDevice ToStorageDevice() const;
137 
138  private:
139  bool m_isMounted = false;
140  std::string m_object;
141  std::string m_mountPoint;
142  };
143 
144  typedef std::map<std::string, Drive *> DriveMap;
145  typedef std::map<std::string, Block *> BlockMap;
146  typedef std::map<std::string, Filesystem *> FilesystemMap;
147 
148 public:
150  ~CUDisks2Provider() override;
151 
152  void Initialize() override;
153 
154  bool PumpDriveChangeEvents(IStorageEventsCallback *callback) override;
155 
156  static bool HasUDisks2();
157 
158  bool Eject(const std::string &mountpath) override;
159 
160  std::vector<std::string> GetDiskUsage() override;
161 
162  void GetLocalDrives(VECSOURCES &localDrives) override
163  { GetDisks(localDrives, false); }
164 
165  void GetRemovableDrives(VECSOURCES &removableDrives) override
166  { GetDisks(removableDrives, true); }
167 
168  void Stop() override
169  {}
170 
171 private:
172  CDBusConnection m_connection;
173 
174  DriveMap m_drives;
175  BlockMap m_blocks;
176  FilesystemMap m_filesystems;
177 
178  std::string m_daemonVersion;
179 
180  void GetDisks(VECSOURCES &devices, bool enumerateRemovable);
181 
182  void DriveAdded(Drive *drive);
183  bool DriveRemoved(const std::string& object);
184  void BlockAdded(Block *block, bool isNew = true);
185  bool BlockRemoved(const std::string& object);
186  void FilesystemAdded(Filesystem *fs, bool isNew = true);
187  bool FilesystemRemoved(const char *object, IStorageEventsCallback *callback);
188 
189  bool HandleInterfacesRemoved(DBusMessage *msg, IStorageEventsCallback *callback);
190  void HandleInterfacesAdded(DBusMessage *msg);
191  bool HandlePropertiesChanged(DBusMessage *msg, IStorageEventsCallback *callback);
192 
193  bool DrivePropertiesChanged(const char *object, DBusMessageIter *propsIter);
194  bool BlockPropertiesChanged(const char *object, DBusMessageIter *propsIter);
195  bool FilesystemPropertiesChanged(const char *object, DBusMessageIter *propsIter, IStorageEventsCallback *callback);
196 
197  bool RemoveInterface(const char *path, const char *iface, IStorageEventsCallback *callback);
198 
199  template<class Object, class Function>
200  void ParseProperties(Object *ref, DBusMessageIter *dictIter, Function f);
201  void ParseInterfaces(DBusMessageIter *dictIter);
202  void ParseDriveProperty(Drive *drive, const char *key, DBusMessageIter *varIter);
203  void ParseBlockProperty(Block *block, const char *key, DBusMessageIter *varIter);
204  void ParseFilesystemProperty(Filesystem *fs, const char *key, DBusMessageIter *varIter);
205  std::string ParseByteArray(DBusMessageIter *arrIter);
206  void HandleManagedObjects(DBusMessage *msg);
207  void ParseInterface(const char *object, const char *iface, DBusMessageIter *propsIter);
208 
209  static void AppendEmptyOptions(DBusMessageIter *argsIter);
210 };
Definition: DBusUtil.h:38
Definition: UDisks2Provider.h:17
Represents a share.
Definition: MediaSource.h:22
Abstracts a generic storage device.
Definition: IStorageProvider.h:32
Definition: IStorageProvider.h:65
Definition: Filesystem.h:18
Definition: IStorageProvider.h:44