Solid

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

KDE's Doxygen guidelines are available online.