• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Solid

  • solid
  • solid
devicemanager.cpp
Go to the documentation of this file.
1/*
2 Copyright 2005-2007 Kevin Ottens <ervin@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "devicenotifier.h"
22#include "devicemanager_p.h" //krazy:exclude=includes (devicenotifier.h is the header file for this class)
23
24#include "device.h"
25#include "device_p.h"
26#include "predicate.h"
27
28#include "ifaces/devicemanager.h"
29#include "ifaces/device.h"
30
31#include "soliddefs_p.h"
32
33SOLID_GLOBAL_STATIC(Solid::DeviceManagerStorage, globalDeviceStorage)
34
35Solid::DeviceManagerPrivate::DeviceManagerPrivate()
36 : m_nullDevice(new DevicePrivate(QString()))
37{
38 loadBackends();
39
40 QList<QObject*> backends = managerBackends();
41 foreach (QObject *backend, backends) {
42 connect(backend, SIGNAL(deviceAdded(QString)),
43 this, SLOT(_k_deviceAdded(QString)));
44 connect(backend, SIGNAL(deviceRemoved(QString)),
45 this, SLOT(_k_deviceRemoved(QString)));
46 }
47}
48
49Solid::DeviceManagerPrivate::~DeviceManagerPrivate()
50{
51 QList<QObject*> backends = managerBackends();
52 foreach (QObject *backend, backends) {
53 disconnect(backend, 0, this, 0);
54 }
55
56 foreach (QWeakPointer<DevicePrivate> dev, m_devicesMap) {
57 if (!dev.data()->ref.deref()) {
58 delete dev.data();
59 }
60 }
61
62 m_devicesMap.clear();
63}
64
65QList<Solid::Device> Solid::Device::allDevices()
66{
67 QList<Device> list;
68 QList<QObject*> backends = globalDeviceStorage->managerBackends();
69
70 foreach (QObject *backendObj, backends) {
71 Ifaces::DeviceManager *backend = qobject_cast<Ifaces::DeviceManager *>(backendObj);
72
73 if (backend == 0) continue;
74
75 QStringList udis = backend->allDevices();
76
77 foreach (const QString &udi, udis) {
78 list.append(Device(udi));
79 }
80 }
81
82 return list;
83}
84
85QList<Solid::Device> Solid::Device::listFromQuery(const QString &predicate,
86 const QString &parentUdi)
87{
88 Predicate p = Predicate::fromString(predicate);
89
90 if (p.isValid())
91 {
92 return listFromQuery(p, parentUdi);
93 }
94 else
95 {
96 return QList<Device>();
97 }
98}
99
100QList<Solid::Device> Solid::Device::listFromType(const DeviceInterface::Type &type,
101 const QString &parentUdi)
102{
103 QList<Device> list;
104 QList<QObject*> backends = globalDeviceStorage->managerBackends();
105
106 foreach (QObject *backendObj, backends) {
107 Ifaces::DeviceManager *backend = qobject_cast<Ifaces::DeviceManager *>(backendObj);
108
109 if (backend == 0) continue;
110 if (!backend->supportedInterfaces().contains(type)) continue;
111
112 QStringList udis = backend->devicesFromQuery(parentUdi, type);
113
114 foreach (const QString &udi, udis) {
115 list.append(Device(udi));
116 }
117 }
118
119 return list;
120}
121
122QList<Solid::Device> Solid::Device::listFromQuery(const Predicate &predicate,
123 const QString &parentUdi)
124{
125 QList<Device> list;
126 QList<QObject*> backends = globalDeviceStorage->managerBackends();
127 QSet<DeviceInterface::Type> usedTypes = predicate.usedTypes();
128
129 foreach (QObject *backendObj, backends) {
130 Ifaces::DeviceManager *backend = qobject_cast<Ifaces::DeviceManager *>(backendObj);
131
132 if (backend == 0) continue;
133
134 QSet<QString> udis;
135 if (predicate.isValid()) {
136 QSet<DeviceInterface::Type> supportedTypes = backend->supportedInterfaces();
137 if (supportedTypes.intersect(usedTypes).isEmpty()) {
138 continue;
139 }
140
141 foreach (DeviceInterface::Type type, supportedTypes) {
142 udis+= QSet<QString>::fromList(backend->devicesFromQuery(parentUdi, type));
143 }
144 } else {
145 udis+= QSet<QString>::fromList(backend->allDevices());
146 }
147
148 foreach (const QString &udi, udis)
149 {
150 Device dev(udi);
151
152 bool matches = false;
153
154 if(!predicate.isValid()) {
155 matches = true;
156 } else {
157 matches = predicate.matches(dev);
158 }
159
160 if (matches)
161 {
162 list.append(dev);
163 }
164 }
165 }
166
167 return list;
168}
169
170Solid::DeviceNotifier *Solid::DeviceNotifier::instance()
171{
172 return globalDeviceStorage->notifier();
173}
174
175void Solid::DeviceManagerPrivate::_k_deviceAdded(const QString &udi)
176{
177 if (m_devicesMap.contains(udi)) {
178 DevicePrivate *dev = m_devicesMap[udi].data();
179
180 // Ok, this one was requested somewhere was invalid
181 // and now becomes magically valid!
182
183 if (dev && dev->backendObject() == 0) {
184 dev->setBackendObject(createBackendObject(udi));
185 Q_ASSERT(dev->backendObject()!=0);
186 }
187 }
188
189 emit deviceAdded(udi);
190}
191
192void Solid::DeviceManagerPrivate::_k_deviceRemoved(const QString &udi)
193{
194 if (m_devicesMap.contains(udi)) {
195 DevicePrivate *dev = m_devicesMap[udi].data();
196
197 // Ok, this one was requested somewhere was valid
198 // and now becomes magically invalid!
199
200 if (dev) {
201 Q_ASSERT(dev->backendObject()!=0);
202 dev->setBackendObject(0);
203 Q_ASSERT(dev->backendObject()==0);
204 }
205 }
206
207 emit deviceRemoved(udi);
208}
209
210void Solid::DeviceManagerPrivate::_k_destroyed(QObject *object)
211{
212 QString udi = m_reverseMap.take(object);
213
214 if (!udi.isEmpty()) {
215 m_devicesMap.remove(udi);
216 }
217}
218
219Solid::DevicePrivate *Solid::DeviceManagerPrivate::findRegisteredDevice(const QString &udi)
220{
221 if (udi.isEmpty()) {
222 return m_nullDevice.data();
223 } else if (m_devicesMap.contains(udi)) {
224 return m_devicesMap[udi].data();
225 } else {
226 Ifaces::Device *iface = createBackendObject(udi);
227
228 DevicePrivate *devData = new DevicePrivate(udi);
229 devData->setBackendObject(iface);
230
231 QWeakPointer<DevicePrivate> ptr(devData);
232 m_devicesMap[udi] = ptr;
233 m_reverseMap[devData] = udi;
234
235 connect(devData, SIGNAL(destroyed(QObject*)),
236 this, SLOT(_k_destroyed(QObject*)));
237
238 return devData;
239 }
240}
241
242Solid::Ifaces::Device *Solid::DeviceManagerPrivate::createBackendObject(const QString &udi)
243{
244 QList<QObject*> backends = globalDeviceStorage->managerBackends();
245
246 foreach (QObject *backendObj, backends) {
247 Ifaces::DeviceManager *backend = qobject_cast<Ifaces::DeviceManager *>(backendObj);
248
249 if (backend == 0) continue;
250 if (!udi.startsWith(backend->udiPrefix())) continue;
251
252 Ifaces::Device *iface = 0;
253
254 QObject *object = backend->createDevice(udi);
255 iface = qobject_cast<Ifaces::Device *>(object);
256
257 if (iface==0) {
258 delete object;
259 }
260
261 return iface;
262 }
263
264 return 0;
265}
266
267Solid::DeviceManagerStorage::DeviceManagerStorage()
268{
269
270}
271
272QList<QObject*> Solid::DeviceManagerStorage::managerBackends()
273{
274 ensureManagerCreated();
275 return m_storage.localData()->managerBackends();
276}
277
278Solid::DeviceNotifier *Solid::DeviceManagerStorage::notifier()
279{
280 ensureManagerCreated();
281 return m_storage.localData();
282}
283
284void Solid::DeviceManagerStorage::ensureManagerCreated()
285{
286 if (!m_storage.hasLocalData()) {
287 m_storage.setLocalData(new DeviceManagerPrivate());
288 }
289}
290
291#include "devicenotifier.moc"
292#include "devicemanager_p.moc"
293
QObject
Solid::DeviceInterface::Type
Type
This enum type defines the type of device interface that a Device can have.
Definition: deviceinterface.h:67
Solid::DeviceManagerPrivate
Definition: devicemanager_p.h:43
Solid::DeviceManagerPrivate::~DeviceManagerPrivate
~DeviceManagerPrivate()
Definition: devicemanager.cpp:49
Solid::DeviceManagerPrivate::findRegisteredDevice
DevicePrivate * findRegisteredDevice(const QString &udi)
Definition: devicemanager.cpp:219
Solid::DeviceManagerStorage
Definition: devicemanager_p.h:65
Solid::DeviceManagerStorage::managerBackends
QList< QObject * > managerBackends()
Definition: devicemanager.cpp:272
Solid::DeviceManagerStorage::notifier
DeviceNotifier * notifier()
Definition: devicemanager.cpp:278
Solid::DeviceManagerStorage::DeviceManagerStorage
DeviceManagerStorage()
Definition: devicemanager.cpp:267
Solid::DeviceNotifier
This class allow to query the underlying system to obtain information about the hardware available.
Definition: devicenotifier.h:43
Solid::DeviceNotifier::instance
static DeviceNotifier * instance()
Definition: devicemanager.cpp:170
Solid::DevicePrivate
Definition: device_p.h:36
Solid::DevicePrivate::backendObject
Ifaces::Device * backendObject() const
Definition: device_p.h:44
Solid::DevicePrivate::setBackendObject
void setBackendObject(Ifaces::Device *object)
Definition: device.cpp:290
Solid::Device
This class allows applications to deal with devices available in the underlying system.
Definition: device.h:49
Solid::Device::listFromType
static QList< Device > listFromType(const DeviceInterface::Type &type, const QString &parentUdi=QString())
Retrieves a list of devices of the system given matching the given constraints (parent and device int...
Definition: devicemanager.cpp:100
Solid::Device::allDevices
static QList< Device > allDevices()
Retrieves all the devices available in the underlying system.
Definition: devicemanager.cpp:65
Solid::Device::listFromQuery
static QList< Device > listFromQuery(const QString &predicate, const QString &parentUdi=QString())
Convenience function see above.
Definition: devicemanager.cpp:85
Solid::Device::listFromQuery
static QList< Device > listFromQuery(const Predicate &predicate, const QString &parentUdi=QString())
Retrieves a list of devices of the system given matching the given constraints (parent and predicate)
Definition: devicemanager.cpp:122
Solid::ManagerBasePrivate::managerBackends
QList< QObject * > managerBackends() const
Definition: managerbase.cpp:114
Solid::Predicate
This class implements predicates for devices.
Definition: predicate.h:46
Solid::Predicate::matches
bool matches(const Device &device) const
Checks if a device matches the predicate.
Definition: predicate.cpp:189
Solid::Predicate::isValid
bool isValid() const
Indicates if the predicate is valid.
Definition: predicate.cpp:184
Solid::Predicate::usedTypes
QSet< DeviceInterface::Type > usedTypes() const
Retrieves the device interface types used in this predicate.
Definition: predicate.cpp:242
Solid::Predicate::fromString
static Predicate fromString(const QString &predicate)
Converts a string to a predicate.
Definition: predicateparse.cpp:56
device.h
device_p.h
devicemanager_p.h
devicenotifier.h
Solid
Definition: acadapter.h:29
predicate.h
soliddefs_p.h
SOLID_GLOBAL_STATIC
#define SOLID_GLOBAL_STATIC(TYPE, NAME)
Definition: soliddefs_p.h:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Solid

Skip menu "Solid"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal