Solid

fakedevice.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Michaƫl Larouche <michael.larouche@kdemail.net>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6#include "fakedevice.h"
7#include "fakedevice_p.h"
8
9#include "fakebattery.h"
10#include "fakeblock.h"
11#include "fakecamera.h"
12#include "fakecdrom.h"
13#include "fakedeviceinterface.h"
14#include "fakegenericinterface.h"
15#include "fakenetworkshare.h"
16#include "fakeopticaldisc.h"
17#include "fakeportablemediaplayer.h"
18#include "fakeprocessor.h"
19#include "fakestorage.h"
20#include "fakestorageaccess.h"
21#include "fakevolume.h"
22
23#include <QStringList>
24#ifdef HAVE_DBUS
25#include <QDBusConnection>
26#endif
27
28#include <solid/genericinterface.h>
29
30using namespace Solid::Backends::Fake;
31
32FakeDevice::FakeDevice(const QString &udi, const QMap<QString, QVariant> &propertyMap)
33 : Solid::Ifaces::Device()
34 , d(new Private)
35{
36 d->udi = udi;
37 d->propertyMap = propertyMap;
38 d->interfaceList = d->propertyMap[QStringLiteral("interfaces")].toString().simplified().split(QLatin1Char(','));
39 d->interfaceList << QStringLiteral("GenericInterface");
40 d->locked = false;
41 d->broken = false;
42
43#ifdef HAVE_DBUS
45#endif
46
47 // Force instantiation of all the device interfaces
48 // this way they'll get exported on the bus
49 // that means they'll be created twice, but that won't be
50 // a problem for unit testing.
51 for (const QString &interface : std::as_const(d->interfaceList)) {
53 createDeviceInterface(type);
54 }
55
56 connect(d.data(), SIGNAL(propertyChanged(QMap<QString, int>)), this, SIGNAL(propertyChanged(QMap<QString, int>)));
57 connect(d.data(), SIGNAL(conditionRaised(QString, QString)), this, SIGNAL(conditionRaised(QString, QString)));
58}
59
60FakeDevice::FakeDevice(const FakeDevice &dev)
61 : Solid::Ifaces::Device()
62 , d(dev.d)
63{
64 connect(d.data(), SIGNAL(propertyChanged(QMap<QString, int>)), this, SIGNAL(propertyChanged(QMap<QString, int>)));
65 connect(d.data(), SIGNAL(conditionRaised(QString, QString)), this, SIGNAL(conditionRaised(QString, QString)));
66}
67
68FakeDevice::~FakeDevice()
69{
70#ifdef HAVE_DBUS
72#endif
73}
74
75QString FakeDevice::udi() const
76{
77 return d->udi;
78}
79
80QString FakeDevice::parentUdi() const
81{
82 return d->propertyMap[QStringLiteral("parent")].toString();
83}
84
85QString FakeDevice::vendor() const
86{
87 return d->propertyMap[QStringLiteral("vendor")].toString();
88}
89
90QString FakeDevice::product() const
91{
92 return d->propertyMap[QStringLiteral("name")].toString();
93}
94
95QString FakeDevice::icon() const
96{
97 if (parentUdi().isEmpty()) {
98 return QStringLiteral("system");
99 } else if (queryDeviceInterface(Solid::DeviceInterface::OpticalDrive)) {
100 return QStringLiteral("cdrom-unmount");
101 } else if (queryDeviceInterface(Solid::DeviceInterface::PortableMediaPlayer)) {
102 return QStringLiteral("ipod-unmount");
103 } else if (queryDeviceInterface(Solid::DeviceInterface::Camera)) {
104 return QStringLiteral("camera-unmount");
105 } else if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
106 return QStringLiteral("cpu");
107 } else if (queryDeviceInterface(Solid::DeviceInterface::StorageDrive)) {
108 return QStringLiteral("hdd-unmount");
109 } else if (queryDeviceInterface(Solid::DeviceInterface::Block)) {
110 return QStringLiteral("blockdevice");
111 } else {
112 return QStringLiteral("hwinfo");
113 }
114}
115
116QStringList FakeDevice::emblems() const
117{
118 if (queryDeviceInterface(Solid::DeviceInterface::StorageAccess)) {
119 if (property(QStringLiteral("isMounted")).toBool()) {
120 return {QStringLiteral("emblem-mounted")};
121 } else {
122 return {QStringLiteral("emblem-unmounted")};
123 }
124 }
125
126 return {};
127}
128
129QString FakeDevice::description() const
130{
131 return product();
132}
133
134QVariant FakeDevice::property(const QString &key) const
135{
136 return d->propertyMap[key];
137}
138
139QMap<QString, QVariant> FakeDevice::allProperties() const
140{
141 return d->propertyMap;
142}
143
144bool FakeDevice::propertyExists(const QString &key) const
145{
146 return d->propertyMap.contains(key);
147}
148
149bool FakeDevice::setProperty(const QString &key, const QVariant &value)
150{
151 if (d->broken) {
152 return false;
153 }
154
155 Solid::GenericInterface::PropertyChange change_type = Solid::GenericInterface::PropertyModified;
156
157 if (!d->propertyMap.contains(key)) {
158 change_type = Solid::GenericInterface::PropertyAdded;
159 }
160
161 d->propertyMap[key] = value;
162
163 QMap<QString, int> change;
164 change[key] = change_type;
165
166 Q_EMIT d->propertyChanged(change);
167
168 return true;
169}
170
171bool FakeDevice::removeProperty(const QString &key)
172{
173 if (d->broken || !d->propertyMap.contains(key)) {
174 return false;
175 }
176
177 d->propertyMap.remove(key);
178
179 QMap<QString, int> change;
180 change[key] = Solid::GenericInterface::PropertyRemoved;
181
182 Q_EMIT d->propertyChanged(change);
183
184 return true;
185}
186
187void FakeDevice::setBroken(bool broken)
188{
189 d->broken = broken;
190}
191
192bool FakeDevice::isBroken()
193{
194 return d->broken;
195}
196
197bool FakeDevice::lock(const QString &reason)
198{
199 if (d->broken || d->locked) {
200 return false;
201 }
202
203 d->locked = true;
204 d->lockReason = reason;
205
206 return true;
207}
208
209bool FakeDevice::unlock()
210{
211 if (d->broken || !d->locked) {
212 return false;
213 }
214
215 d->locked = false;
216 d->lockReason.clear();
217
218 return true;
219}
220
221bool FakeDevice::isLocked() const
222{
223 return d->locked;
224}
225
226QString FakeDevice::lockReason() const
227{
228 return d->lockReason;
229}
230
231void FakeDevice::raiseCondition(const QString &condition, const QString &reason)
232{
233 Q_EMIT d->conditionRaised(condition, reason);
234}
235
236bool FakeDevice::queryDeviceInterface(const Solid::DeviceInterface::Type &type) const
237{
238 return d->interfaceList.contains(Solid::DeviceInterface::typeToString(type));
239}
240
241QObject *FakeDevice::createDeviceInterface(const Solid::DeviceInterface::Type &type)
242{
243 // Do not try to cast with a unsupported device interface.
244 if (!queryDeviceInterface(type)) {
245 return nullptr;
246 }
247
248 FakeDeviceInterface *iface = nullptr;
249
250 switch (type) {
251 case Solid::DeviceInterface::GenericInterface:
252 iface = new FakeGenericInterface(this);
253 break;
254 case Solid::DeviceInterface::Processor:
255 iface = new FakeProcessor(this);
256 break;
257 case Solid::DeviceInterface::Block:
258 iface = new FakeBlock(this);
259 break;
260 case Solid::DeviceInterface::StorageDrive:
261 iface = new FakeStorage(this);
262 break;
263 case Solid::DeviceInterface::OpticalDrive:
264 iface = new FakeCdrom(this);
265 break;
266 case Solid::DeviceInterface::StorageVolume:
267 iface = new FakeVolume(this);
268 break;
269 case Solid::DeviceInterface::OpticalDisc:
270 iface = new FakeOpticalDisc(this);
271 break;
272 case Solid::DeviceInterface::StorageAccess:
273 iface = new FakeStorageAccess(this);
274 break;
275 case Solid::DeviceInterface::Camera:
276 iface = new FakeCamera(this);
277 break;
278 case Solid::DeviceInterface::PortableMediaPlayer:
279 iface = new FakePortableMediaPlayer(this);
280 break;
281 case Solid::DeviceInterface::Battery:
282 iface = new FakeBattery(this);
283 break;
284 case Solid::DeviceInterface::NetworkShare:
285 iface = new FakeNetworkShare(this);
286 break;
287 case Solid::DeviceInterface::Unknown:
288 break;
289 case Solid::DeviceInterface::Last:
290 break;
291 }
292
293#ifdef HAVE_DBUS
294 if (iface) {
296 iface,
298 }
299#endif
300
301 return iface;
302}
303
304#include "moc_fakedevice.cpp"
305#include "moc_fakedevice_p.cpp"
static QString typeToString(Type type)
Type
This enum type defines the type of device interface that a Device can have.
static Type stringToType(const QString &type)
This class allows applications to deal with devices available in the underlying system.
QString udi() const
Retrieves the Universal Device Identifier (UDI).
PropertyChange
This enum type defines the type of change that can occur to a GenericInterface property.
Type type(const QSqlDatabase &db)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
void unregisterObject(const QString &path, UnregisterMode mode)
Q_EMITQ_EMIT
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 Sat Dec 21 2024 17:03:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.