File indexing completed on 2024-05-05 12:26:24
0001 /* 0002 SPDX-FileCopyrightText: 2013 Ivan Cukic <ivan.cukic(at)kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #ifndef SOLID_DECALARATIVE_DEVICES_H 0008 #define SOLID_DECALARATIVE_DEVICES_H 0009 0010 #include <QObject> 0011 #include <solid/deviceinterface.h> 0012 #include <solid/predicate.h> 0013 0014 #include <QSharedPointer> 0015 0016 namespace Solid 0017 { 0018 class DeviceNotifier; 0019 class DevicesQueryPrivate; 0020 0021 /** 0022 * A class that watches the devices known to the solid system. 0023 * 0024 * It behaves similarly to Solid::DeviceNotifier, but 0025 * adds some convenience methods which allow it to 0026 * watch only the devices matching a specified query 0027 * (formatted for Solid::Predicate). 0028 * 0029 * It is intended to be used from QML like this: 0030 * 0031 * @code 0032 * Solid.Devices { 0033 * id: allDevices 0034 * } 0035 * 0036 * Solid.Devices { 0037 * id: networkShares 0038 * query: "IS NetworkShare" 0039 * } 0040 * 0041 * Solid.Devices { 0042 * id: mice 0043 * query: "PointingDevice.type == 'Mouse'" 0044 * } 0045 * 0046 * Text { 0047 * text: "Total number of devices: " + allDevices.count 0048 * } 0049 * 0050 * Text { 0051 * text: "NFS url: " + networkShares.device( 0052 * networkShares.devices[0], "NetworkShare" 0053 * ).url 0054 * } 0055 * @endcode 0056 */ 0057 class Devices : public QObject 0058 { 0059 Q_OBJECT 0060 0061 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged) 0062 Q_PROPERTY(int count READ count NOTIFY countChanged) 0063 Q_PROPERTY(bool empty READ isEmpty NOTIFY emptyChanged) 0064 Q_PROPERTY(QStringList devices READ devices NOTIFY devicesChanged) 0065 0066 public: 0067 explicit Devices(QObject *parent = nullptr); 0068 ~Devices() override; 0069 0070 Q_SIGNALS: 0071 /** 0072 * Emitted when a new device matching the specified 0073 * query arrives 0074 * @param udi UDI of the new device 0075 */ 0076 void deviceAdded(const QString &udi) const; 0077 0078 /** 0079 * Emitted when a device matching the specified 0080 * query disappears 0081 * @param udi UDI of the device 0082 */ 0083 void deviceRemoved(const QString &udi) const; 0084 0085 /** 0086 * Emitted when the number of devices that 0087 * match the specified query has changed 0088 * @param count new device count 0089 */ 0090 void countChanged(int count) const; 0091 0092 /** 0093 * Emitted when the list of devices that 0094 * match the specified query has changed 0095 * @param devices list of UDIs 0096 */ 0097 void devicesChanged(const QStringList &devices) const; 0098 0099 /** 0100 * Emitted when the query has changed 0101 * @param query new query 0102 */ 0103 void queryChanged(const QString &query) const; 0104 0105 /** 0106 * Emitted when the empty property changes 0107 * @param empty is the device list empty 0108 */ 0109 void emptyChanged(bool empty) const; 0110 0111 public: 0112 /** 0113 * Retrieves the number of the devices that 0114 * match the specified query 0115 * @return device count 0116 */ 0117 int count() const; 0118 0119 /** 0120 * Retrieves whether there are devices matching 0121 * the specified query 0122 * @return true if there are no matching devices 0123 */ 0124 bool isEmpty() const; 0125 0126 /** 0127 * Retrieves the list of UDIs of the devices that 0128 * match the specified query 0129 */ 0130 QStringList devices() const; 0131 0132 /** 0133 * Query to check the devices against. It needs 0134 * to be formatted for Solid::Predicate. 0135 * @see Solid::Predicate 0136 */ 0137 QString query() const; 0138 0139 /** 0140 * Sets the query to filter the devices. 0141 * @param query new query 0142 */ 0143 void setQuery(const QString &query); 0144 0145 public Q_SLOTS: 0146 /** 0147 * Retrieves an interface object to the specified device 0148 * @param udi udi of the desired device 0149 * @param type how to interpret the device 0150 * @see Solid::Device::asDeviceInterface 0151 */ 0152 QObject *device(const QString &udi, const QString &type); 0153 0154 private Q_SLOTS: 0155 void addDevice(const QString &udi); 0156 void removeDevice(const QString &udi); 0157 0158 /** 0159 * Initializes the backend object 0160 */ 0161 void initialize() const; 0162 0163 /** 0164 * Frees up the backend and sends the appropriate events 0165 */ 0166 void reset(); 0167 0168 private: 0169 QString m_query; 0170 0171 mutable QSharedPointer<DevicesQueryPrivate> m_backend; 0172 }; 0173 0174 } // namespace Solid 0175 0176 #endif