File indexing completed on 2024-05-12 04:01:50
0001 /* 0002 SPDX-FileCopyrightText: 2005-2007 Kevin Ottens <ervin@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef SOLID_DEVICE_H 0008 #define SOLID_DEVICE_H 0009 0010 #include <QList> 0011 #include <QSharedData> 0012 0013 #include <solid/solid_export.h> 0014 0015 #include <solid/deviceinterface.h> 0016 0017 namespace Solid 0018 { 0019 class DevicePrivate; 0020 0021 /** 0022 * @class Solid::Device device.h <Solid/Device> 0023 * 0024 * This class allows applications to deal with devices available in the 0025 * underlying system. 0026 * 0027 * Device stores a reference to device data provided by the backend. 0028 * Device objects are designed to be used by value. Copying these objects 0029 * is quite cheap, so using pointers to the me is generally not needed. 0030 * 0031 * @author Kevin Ottens <ervin@kde.org> 0032 */ 0033 class SOLID_EXPORT Device 0034 { 0035 public: 0036 /** 0037 * Retrieves all the devices available in the underlying system. 0038 * 0039 * @return the list of the devices available 0040 */ 0041 static QList<Device> allDevices(); 0042 0043 /** 0044 * Retrieves a list of devices of the system given matching the given 0045 * constraints (parent and device interface type) 0046 * 0047 * @param type device interface type available on the devices we're looking for, or DeviceInterface::Unknown 0048 * if there's no constraint on the device interfaces 0049 * @param parentUdi UDI of the parent of the devices we're searching for, or QString() 0050 * if there's no constraint on the parent 0051 * @return the list of devices corresponding to the given constraints 0052 * @see Solid::Predicate 0053 */ 0054 static QList<Device> listFromType(const DeviceInterface::Type &type, const QString &parentUdi = QString()); 0055 0056 /** 0057 * Retrieves a list of devices of the system given matching the given 0058 * constraints (parent and predicate) 0059 * 0060 * @param predicate Predicate that the devices we're searching for must verify 0061 * @param parentUdi UDI of the parent of the devices we're searching for, or QString() 0062 * if there's no constraint on the parent 0063 * @return the list of devices corresponding to the given constraints 0064 * @see Solid::Predicate 0065 */ 0066 static QList<Device> listFromQuery(const Predicate &predicate, const QString &parentUdi = QString()); 0067 0068 /** 0069 * Convenience function see above. 0070 * 0071 * @param predicate 0072 * @param parentUdi 0073 * @return the list of devices 0074 */ 0075 static QList<Device> listFromQuery(const QString &predicate, const QString &parentUdi = QString()); 0076 0077 /** 0078 * Returns storage volume for given canonical path to file stored on that device 0079 * 0080 * @param canonical path to an existing file 0081 * @return StorageVolume Device containing file to which path was given or an empty Device, if no device could be found 0082 * @since 5.73 0083 */ 0084 static Device storageAccessFromPath(const QString &path); 0085 0086 /** 0087 * Constructs a device for a given Universal Device Identifier (UDI). 0088 * 0089 * @param udi the udi of the device to create 0090 */ 0091 explicit Device(const QString &udi = QString()); 0092 0093 /** 0094 * Constructs a copy of a device. 0095 * 0096 * @param device the device to copy 0097 */ 0098 Device(const Device &device); 0099 0100 /** 0101 * Destroys the device. 0102 */ 0103 ~Device(); 0104 0105 /** 0106 * Assigns a device to this device and returns a reference to it. 0107 * 0108 * @param device the device to assign 0109 * @return a reference to the device 0110 */ 0111 Device &operator=(const Device &device); 0112 0113 /** 0114 * Indicates if this device is valid. 0115 * A device is considered valid if it's available in the system. 0116 * 0117 * @return true if this device is available, false otherwise 0118 */ 0119 bool isValid() const; 0120 0121 /** 0122 * Retrieves the Universal Device Identifier (UDI). 0123 * 0124 * \warning Don't use the UDI for anything except communication with Solid. Also don't store 0125 * UDIs as there's no guarantee that the UDI stays the same when the hardware setup changed. 0126 * The UDI is a unique identifier that is local to the computer in question and for the 0127 * current boot session. The UDIs may change after a reboot. 0128 * Similar hardware in other computers may have different values; different 0129 * hardware could have the same UDI. 0130 * 0131 * @return the udi of the device 0132 */ 0133 QString udi() const; 0134 0135 /** 0136 * Retrieves the Universal Device Identifier (UDI) 0137 * of the Device's parent. 0138 * 0139 * @return the udi of the device's parent 0140 */ 0141 QString parentUdi() const; 0142 0143 /** 0144 * Retrieves the parent of the Device. 0145 * 0146 * @return the device's parent 0147 * @see parentUdi() 0148 */ 0149 Device parent() const; 0150 0151 /** 0152 * Retrieves the name of the device vendor. 0153 * 0154 * @return the vendor name 0155 */ 0156 QString vendor() const; 0157 0158 /** 0159 * Retrieves the name of the product corresponding to this device. 0160 * 0161 * @return the product name 0162 */ 0163 QString product() const; 0164 0165 /** 0166 * Retrieves the name of the icon representing this device. 0167 * The naming follows the freedesktop.org specification. 0168 * 0169 * @return the icon name 0170 */ 0171 QString icon() const; 0172 0173 /** 0174 * Retrieves the names of the emblems representing the state of this device. 0175 * The naming follows the freedesktop.org specification. 0176 * 0177 * @return the emblem names 0178 * @since 4.4 0179 */ 0180 QStringList emblems() const; 0181 0182 /** 0183 * Retrieves the display Name to use for this device. 0184 * Same as description when not defined. 0185 * 0186 * @return the display Name 0187 * @since 5.71 0188 */ 0189 QString displayName() const; 0190 0191 /** 0192 * Retrieves the description of device. 0193 * 0194 * @return the description 0195 * @since 4.4 0196 */ 0197 QString description() const; 0198 0199 /** 0200 * Tests if a device interface is available from the device. 0201 * 0202 * @param type the device interface type to query 0203 * @return true if the device interface is available, false otherwise 0204 */ 0205 bool isDeviceInterface(const DeviceInterface::Type &type) const; 0206 0207 /** 0208 * Retrieves a specialized interface to interact with the device corresponding to 0209 * a particular device interface. 0210 * 0211 * @param type the device interface type 0212 * @returns a pointer to the device interface interface if it exists, 0 otherwise 0213 */ 0214 DeviceInterface *asDeviceInterface(const DeviceInterface::Type &type); 0215 0216 /** 0217 * Retrieves a specialized interface to interact with the device corresponding to 0218 * a particular device interface. 0219 * 0220 * @param type the device interface type 0221 * @returns a pointer to the device interface interface if it exists, 0 otherwise 0222 */ 0223 const DeviceInterface *asDeviceInterface(const DeviceInterface::Type &type) const; 0224 0225 /** 0226 * Retrieves a specialized interface to interact with the device corresponding 0227 * to a given device interface. 0228 * 0229 * @returns a pointer to the device interface if it exists, 0 otherwise 0230 */ 0231 template<class DevIface> 0232 DevIface *as() 0233 { 0234 DeviceInterface::Type type = DevIface::deviceInterfaceType(); 0235 DeviceInterface *iface = asDeviceInterface(type); 0236 return qobject_cast<DevIface *>(iface); 0237 } 0238 0239 /** 0240 * Retrieves a specialized interface to interact with the device corresponding 0241 * to a given device interface. 0242 * 0243 * @returns a pointer to the device interface if it exists, 0 otherwise 0244 */ 0245 template<class DevIface> 0246 const DevIface *as() const 0247 { 0248 DeviceInterface::Type type = DevIface::deviceInterfaceType(); 0249 const DeviceInterface *iface = asDeviceInterface(type); 0250 return qobject_cast<const DevIface *>(iface); 0251 } 0252 0253 /** 0254 * Tests if a device provides a given device interface. 0255 * 0256 * @returns true if the interface is available, false otherwise 0257 */ 0258 template<class DevIface> 0259 bool is() const 0260 { 0261 return isDeviceInterface(DevIface::deviceInterfaceType()); 0262 } 0263 0264 private: 0265 QExplicitlySharedDataPointer<DevicePrivate> d; 0266 friend class DeviceManagerPrivate; 0267 }; 0268 } 0269 0270 Q_DECLARE_TYPEINFO(Solid::Device, Q_RELOCATABLE_TYPE); 0271 0272 #endif