Solid

frontend/device.cpp
1/*
2 SPDX-FileCopyrightText: 2005-2007 Kevin Ottens <ervin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "device.h"
8#include "device_p.h"
9#include "devicemanager_p.h"
10#include "devicenotifier.h"
11
12#include "deviceinterface_p.h"
13#include "soliddefs_p.h"
14
15#include <solid/devices/ifaces/device.h>
16
17#include <solid/battery.h>
18#include <solid/block.h>
19#include <solid/camera.h>
20#include <solid/devices/ifaces/battery.h>
21#include <solid/devices/ifaces/block.h>
22#include <solid/devices/ifaces/camera.h>
23#include <solid/devices/ifaces/genericinterface.h>
24#include <solid/devices/ifaces/networkshare.h>
25#include <solid/devices/ifaces/opticaldisc.h>
26#include <solid/devices/ifaces/opticaldrive.h>
27#include <solid/devices/ifaces/portablemediaplayer.h>
28#include <solid/devices/ifaces/processor.h>
29#include <solid/devices/ifaces/storageaccess.h>
30#include <solid/devices/ifaces/storagedrive.h>
31#include <solid/devices/ifaces/storagevolume.h>
32#include <solid/genericinterface.h>
33#include <solid/networkshare.h>
34#include <solid/opticaldisc.h>
35#include <solid/opticaldrive.h>
36#include <solid/portablemediaplayer.h>
37#include <solid/processor.h>
38#include <solid/storageaccess.h>
39#include <solid/storagedrive.h>
40#include <solid/storagevolume.h>
41
43{
44 DeviceManagerPrivate *manager = static_cast<DeviceManagerPrivate *>(Solid::DeviceNotifier::instance());
45 d = manager->findRegisteredDevice(udi);
46}
47
49 : d(device.d)
50{
51}
52
56
58{
59 d = device.d;
60 return *this;
61}
62
64{
65 return d->backendObject() != nullptr;
66}
67
69{
70 return d->udi();
71}
72
74{
75 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), parentUdi());
76}
77
79{
80 QString udi = parentUdi();
81
82 if (udi.isEmpty()) {
83 return Device();
84 } else {
85 return Device(udi);
86 }
87}
88
90{
91 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), vendor());
92}
93
95{
96 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), product());
97}
98
100{
101 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), icon());
102}
103
105{
106 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QStringList(), emblems());
107}
108
110{
111 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), displayName());
112}
113
115{
116 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), description());
117}
118
120{
121 return_SOLID_CALL(Ifaces::Device *, d->backendObject(), false, queryDeviceInterface(type));
122}
123
124#define deviceinterface_cast(IfaceType, DevType, backendObject) (qobject_cast<IfaceType *>(backendObject) ? new DevType(backendObject) : nullptr)
125
127{
128 const Solid::DeviceInterface *interface = const_cast<const Device *>(this)->asDeviceInterface(type);
129 return const_cast<Solid::DeviceInterface *>(interface);
130}
131
133{
134 Ifaces::Device *device = qobject_cast<Ifaces::Device *>(d->backendObject());
135
136 if (device != nullptr) {
137 DeviceInterface *iface = d->interface(type);
138
139 if (iface != nullptr) {
140 return iface;
141 }
142
143 QObject *dev_iface = device->createDeviceInterface(type);
144
145 if (dev_iface != nullptr) {
146 switch (type) {
147 case DeviceInterface::GenericInterface:
148 iface = deviceinterface_cast(Ifaces::GenericInterface, GenericInterface, dev_iface);
149 break;
150 case DeviceInterface::Processor:
151 iface = deviceinterface_cast(Ifaces::Processor, Processor, dev_iface);
152 break;
153 case DeviceInterface::Block:
154 iface = deviceinterface_cast(Ifaces::Block, Block, dev_iface);
155 break;
156 case DeviceInterface::StorageAccess:
157 iface = deviceinterface_cast(Ifaces::StorageAccess, StorageAccess, dev_iface);
158 break;
159 case DeviceInterface::StorageDrive:
160 iface = deviceinterface_cast(Ifaces::StorageDrive, StorageDrive, dev_iface);
161 break;
162 case DeviceInterface::OpticalDrive:
163 iface = deviceinterface_cast(Ifaces::OpticalDrive, OpticalDrive, dev_iface);
164 break;
165 case DeviceInterface::StorageVolume:
166 iface = deviceinterface_cast(Ifaces::StorageVolume, StorageVolume, dev_iface);
167 break;
168 case DeviceInterface::OpticalDisc:
169 iface = deviceinterface_cast(Ifaces::OpticalDisc, OpticalDisc, dev_iface);
170 break;
171 case DeviceInterface::Camera:
172 iface = deviceinterface_cast(Ifaces::Camera, Camera, dev_iface);
173 break;
174 case DeviceInterface::PortableMediaPlayer:
175 iface = deviceinterface_cast(Ifaces::PortableMediaPlayer, PortableMediaPlayer, dev_iface);
176 break;
177 case DeviceInterface::Battery:
178 iface = deviceinterface_cast(Ifaces::Battery, Battery, dev_iface);
179 break;
180 case DeviceInterface::NetworkShare:
181 iface = deviceinterface_cast(Ifaces::NetworkShare, NetworkShare, dev_iface);
182 break;
183 case DeviceInterface::Unknown:
184 case DeviceInterface::Last:
185 break;
186 }
187 }
188
189 if (iface != nullptr) {
190 // Lie on the constness since we're simply doing caching here
191 const_cast<Device *>(this)->d->setInterface(type, iface);
192 iface->d_ptr->setDevicePrivate(d.data());
193 }
194
195 return iface;
196 } else {
197 return nullptr;
198 }
199}
200
201//////////////////////////////////////////////////////////////////////
202
203Solid::DevicePrivate::DevicePrivate(const QString &udi)
204 : QObject()
205 , QSharedData()
206 , m_udi(udi)
207{
208}
209
210Solid::DevicePrivate::~DevicePrivate()
211{
212 setBackendObject(nullptr);
213}
214
215void Solid::DevicePrivate::_k_destroyed(QObject *object)
216{
217 Q_UNUSED(object);
218 setBackendObject(nullptr);
219}
220
221void Solid::DevicePrivate::setBackendObject(Ifaces::Device *object)
222{
223 if (m_backendObject) {
224 m_backendObject.data()->disconnect(this);
225 }
226
227 delete m_backendObject.data();
228 m_backendObject = object;
229
230 if (object) {
231 connect(object, SIGNAL(destroyed(QObject *)), this, SLOT(_k_destroyed(QObject *)));
232 }
233
234 if (!m_ifaces.isEmpty()) {
235 qDeleteAll(m_ifaces);
236
237 m_ifaces.clear();
238 if (!ref.deref()) {
239 deleteLater();
240 }
241 }
242}
243
244Solid::DeviceInterface *Solid::DevicePrivate::interface(const DeviceInterface::Type &type) const
245{
246 return m_ifaces[type];
247}
248
249void Solid::DevicePrivate::setInterface(const DeviceInterface::Type &type, DeviceInterface *interface)
250{
251 if (m_ifaces.isEmpty()) {
252 ref.ref();
253 }
254 m_ifaces[type] = interface;
255}
256
257#include "moc_device_p.cpp"
This device interface is available on batteries.
This device interface is available on block devices.
This device interface is available on digital camera devices.
Base class of all the device interfaces.
Type
This enum type defines the type of device interface that a Device can have.
This class allows applications to deal with devices available in the underlying system.
QString description() const
Retrieves the description of device.
QStringList emblems() const
Retrieves the names of the emblems representing the state of this device.
QString udi() const
Retrieves the Universal Device Identifier (UDI).
Device & operator=(const Device &device)
Assigns a device to this device and returns a reference to it.
QString displayName() const
Retrieves the display Name to use for this device.
QString parentUdi() const
Retrieves the Universal Device Identifier (UDI) of the Device's parent.
QString icon() const
Retrieves the name of the icon representing this device.
~Device()
Destroys the device.
bool isDeviceInterface(const DeviceInterface::Type &type) const
Tests if a device interface is available from the device.
Device parent() const
Retrieves the parent of the Device.
DeviceInterface * asDeviceInterface(const DeviceInterface::Type &type)
Retrieves a specialized interface to interact with the device corresponding to a particular device in...
Device(const QString &udi=QString())
Constructs a device for a given Universal Device Identifier (UDI).
QString vendor() const
Retrieves the name of the device vendor.
bool isValid() const
Indicates if this device is valid.
QString product() const
Retrieves the name of the product corresponding to this device.
Generic interface to deal with a device.
This device interface is available on batteries.
This device interface is available on block devices.
This device interface is available on digital camera devices.
This class specifies the interface a device will have to comply to in order to be used in the system.
virtual QObject * createDeviceInterface(const Solid::DeviceInterface::Type &type)=0
Create a specialized interface to interact with the device corresponding to a particular device inter...
Generic interface to deal with a device.
NetworkShare interface.
This device interface is available on optical discs.
This device interface is available on CD-ROM drives.
This class implements Portable Media Player device interface and represents a portable media player a...
This device interface is available on processors.
This device interface is available on volume devices.
This device interface is available on storage devices.
This device interface is available on volume devices.
NetworkShare interface.
This device interface is available on optical discs.
This device interface is available on CD-R*,DVD*,Blu-Ray,HD-DVD drives.
This class implements Portable Media Player device interface and represents a portable media player a...
This device interface is available on processors.
This device interface is available on volume devices to access them (i.e.
This device interface is available on storage devices.
This device interface is available on volume devices.
Type type(const QSqlDatabase &db)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.