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 QT_DBUS_LIB
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["interfaces"].toString().simplified().split(',');
39 d->interfaceList << "GenericInterface";
40 d->locked = false;
41 d->broken = false;
42
43#ifdef QT_DBUS_LIB
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 QT_DBUS_LIB
72#endif
73}
74
75QString FakeDevice::udi() const
76{
77 return d->udi;
78}
79
80QString FakeDevice::parentUdi() const
81{
82 return d->propertyMap["parent"].toString();
83}
84
85QString FakeDevice::vendor() const
86{
87 return d->propertyMap["vendor"].toString();
88}
89
90QString FakeDevice::product() const
91{
92 return d->propertyMap["name"].toString();
93}
94
95QString FakeDevice::icon() const
96{
97 if (parentUdi().isEmpty()) {
98 return "system";
99 } else if (queryDeviceInterface(Solid::DeviceInterface::OpticalDrive)) {
100 return "cdrom-unmount";
101 } else if (queryDeviceInterface(Solid::DeviceInterface::PortableMediaPlayer)) {
102 return "ipod-unmount";
103 } else if (queryDeviceInterface(Solid::DeviceInterface::Camera)) {
104 return "camera-unmount";
105 } else if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
106 return "cpu";
107 } else if (queryDeviceInterface(Solid::DeviceInterface::StorageDrive)) {
108 return "hdd-unmount";
109 } else if (queryDeviceInterface(Solid::DeviceInterface::Block)) {
110 return "blockdevice";
111 } else {
112 return "hwinfo";
113 }
114}
115
116QStringList FakeDevice::emblems() const
117{
118 QStringList res;
119
120 if (queryDeviceInterface(Solid::DeviceInterface::StorageAccess)) {
121 if (property("isMounted").toBool()) {
122 res << "emblem-mounted";
123 } else {
124 res << "emblem-unmounted";
125 }
126 }
127
128 return res;
129}
130
131QString FakeDevice::description() const
132{
133 return product();
134}
135
136QVariant FakeDevice::property(const QString &key) const
137{
138 return d->propertyMap[key];
139}
140
141QMap<QString, QVariant> FakeDevice::allProperties() const
142{
143 return d->propertyMap;
144}
145
146bool FakeDevice::propertyExists(const QString &key) const
147{
148 return d->propertyMap.contains(key);
149}
150
151bool FakeDevice::setProperty(const QString &key, const QVariant &value)
152{
153 if (d->broken) {
154 return false;
155 }
156
157 Solid::GenericInterface::PropertyChange change_type = Solid::GenericInterface::PropertyModified;
158
159 if (!d->propertyMap.contains(key)) {
160 change_type = Solid::GenericInterface::PropertyAdded;
161 }
162
163 d->propertyMap[key] = value;
164
165 QMap<QString, int> change;
166 change[key] = change_type;
167
168 Q_EMIT d->propertyChanged(change);
169
170 return true;
171}
172
173bool FakeDevice::removeProperty(const QString &key)
174{
175 if (d->broken || !d->propertyMap.contains(key)) {
176 return false;
177 }
178
179 d->propertyMap.remove(key);
180
181 QMap<QString, int> change;
182 change[key] = Solid::GenericInterface::PropertyRemoved;
183
184 Q_EMIT d->propertyChanged(change);
185
186 return true;
187}
188
189void FakeDevice::setBroken(bool broken)
190{
191 d->broken = broken;
192}
193
194bool FakeDevice::isBroken()
195{
196 return d->broken;
197}
198
199bool FakeDevice::lock(const QString &reason)
200{
201 if (d->broken || d->locked) {
202 return false;
203 }
204
205 d->locked = true;
206 d->lockReason = reason;
207
208 return true;
209}
210
211bool FakeDevice::unlock()
212{
213 if (d->broken || !d->locked) {
214 return false;
215 }
216
217 d->locked = false;
218 d->lockReason.clear();
219
220 return true;
221}
222
223bool FakeDevice::isLocked() const
224{
225 return d->locked;
226}
227
228QString FakeDevice::lockReason() const
229{
230 return d->lockReason;
231}
232
233void FakeDevice::raiseCondition(const QString &condition, const QString &reason)
234{
235 Q_EMIT d->conditionRaised(condition, reason);
236}
237
238bool FakeDevice::queryDeviceInterface(const Solid::DeviceInterface::Type &type) const
239{
240 return d->interfaceList.contains(Solid::DeviceInterface::typeToString(type));
241}
242
243QObject *FakeDevice::createDeviceInterface(const Solid::DeviceInterface::Type &type)
244{
245 // Do not try to cast with a unsupported device interface.
246 if (!queryDeviceInterface(type)) {
247 return nullptr;
248 }
249
250 FakeDeviceInterface *iface = nullptr;
251
252 switch (type) {
253 case Solid::DeviceInterface::GenericInterface:
254 iface = new FakeGenericInterface(this);
255 break;
256 case Solid::DeviceInterface::Processor:
257 iface = new FakeProcessor(this);
258 break;
259 case Solid::DeviceInterface::Block:
260 iface = new FakeBlock(this);
261 break;
262 case Solid::DeviceInterface::StorageDrive:
263 iface = new FakeStorage(this);
264 break;
265 case Solid::DeviceInterface::OpticalDrive:
266 iface = new FakeCdrom(this);
267 break;
268 case Solid::DeviceInterface::StorageVolume:
269 iface = new FakeVolume(this);
270 break;
271 case Solid::DeviceInterface::OpticalDisc:
272 iface = new FakeOpticalDisc(this);
273 break;
274 case Solid::DeviceInterface::StorageAccess:
275 iface = new FakeStorageAccess(this);
276 break;
277 case Solid::DeviceInterface::Camera:
278 iface = new FakeCamera(this);
279 break;
280 case Solid::DeviceInterface::PortableMediaPlayer:
281 iface = new FakePortableMediaPlayer(this);
282 break;
283 case Solid::DeviceInterface::Battery:
284 iface = new FakeBattery(this);
285 break;
286 case Solid::DeviceInterface::NetworkShare:
287 iface = new FakeNetworkShare(this);
288 break;
289 case Solid::DeviceInterface::Unknown:
290 break;
291 case Solid::DeviceInterface::Last:
292 break;
293 }
294
295#ifdef QT_DBUS_LIB
296 if (iface) {
298 iface,
300 }
301#endif
302
303 return iface;
304}
305
306#include "moc_fakedevice.cpp"
307#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)
The single responsibility of this class is to create arguments valid for logind Inhibit call.
Definition fakebattery.h:16
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 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.