Solid

udevqtdevice.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Benjamin K. Stuhl <bks24@cornell.edu>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "udevqtdevice.h"
8#include "udevqt_p.h"
9
10#include <QByteArray>
11
12namespace UdevQt
13{
14DevicePrivate::DevicePrivate(struct udev_device *udev_, bool ref)
15 : udev(udev_)
16{
17 if (ref) {
18 udev_device_ref(udev);
19 }
20}
21
22DevicePrivate::~DevicePrivate()
23{
24 udev_device_unref(udev);
25}
26
27DevicePrivate &DevicePrivate::operator=(const DevicePrivate &other)
28{
29 udev_device_unref(udev);
30 udev = udev_device_ref(other.udev);
31 return *this;
32}
33
34QString DevicePrivate::decodePropertyValue(const QByteArray &encoded) const
35{
36 QByteArray decoded;
37 const int len = encoded.length();
38
39 for (int i = 0; i < len; i++) {
40 quint8 ch = encoded.at(i);
41
42 if (ch == '\\') {
43 if (i + 1 < len && encoded.at(i + 1) == '\\') {
44 decoded.append('\\');
45 i++;
46 continue;
47 } else if (i + 3 < len && encoded.at(i + 1) == 'x') {
48 QByteArray hex = encoded.mid(i + 2, 2);
49 bool ok;
50 int code = hex.toInt(&ok, 16);
51 if (ok) {
52 decoded.append(char(code));
53 }
54 i += 3;
55 continue;
56 }
57 } else {
58 decoded.append(ch);
59 }
60 }
61 return QString::fromUtf8(decoded);
62}
63
64Device::Device()
65 : d(nullptr)
66{
67}
68
69Device::Device(const Device &other)
70{
71 if (other.d) {
72 d = new DevicePrivate(other.d->udev);
73 } else {
74 d = nullptr;
75 }
76}
77
78Device::Device(DevicePrivate *devPrivate)
79 : d(devPrivate)
80{
81}
82
83Device::~Device()
84{
85 delete d;
86}
87
88Device &Device::operator=(const Device &other)
89{
90 if (this == &other) {
91 return *this;
92 }
93 if (!other.d) {
94 delete d;
95 d = nullptr;
96 return *this;
97 }
98 if (!d) {
99 d = new DevicePrivate(other.d->udev);
100 } else {
101 *d = *other.d;
102 }
103
104 return *this;
105}
106
107bool Device::isValid() const
108{
109 return (d != nullptr);
110}
111
112QString Device::subsystem() const
113{
114 if (!d) {
115 return QString();
116 }
117
118 return QString::fromLatin1(udev_device_get_subsystem(d->udev));
119}
120
121QString Device::devType() const
122{
123 if (!d) {
124 return QString();
125 }
126
127 return QString::fromLatin1(udev_device_get_devtype(d->udev));
128}
129
130QString Device::name() const
131{
132 if (!d) {
133 return QString();
134 }
135
136 return QString::fromLatin1(udev_device_get_sysname(d->udev));
137}
138
139QString Device::sysfsPath() const
140{
141 if (!d) {
142 return QString();
143 }
144
145 return QString::fromLatin1(udev_device_get_syspath(d->udev));
146}
147
148int Device::sysfsNumber() const
149{
150 if (!d) {
151 return -1;
152 }
153
154 QString value = QString::fromLatin1(udev_device_get_sysnum(d->udev));
155 bool success = false;
156 int number = value.toInt(&success);
157 if (success) {
158 return number;
159 }
160 return -1;
161}
162
163QString Device::driver() const
164{
165 if (!d) {
166 return QString();
167 }
168
169 return QString::fromLatin1(udev_device_get_driver(d->udev));
170}
171
172QString Device::primaryDeviceFile() const
173{
174 if (!d) {
175 return QString();
176 }
177
178 return QString::fromLatin1(udev_device_get_devnode(d->udev));
179}
180
181QStringList Device::alternateDeviceSymlinks() const
182{
183 if (!d) {
184 return QStringList();
185 }
186
187 return listFromListEntry(udev_device_get_devlinks_list_entry(d->udev));
188}
189
190QStringList Device::deviceProperties() const
191{
192 if (!d) {
193 return QStringList();
194 }
195
196 return listFromListEntry(udev_device_get_properties_list_entry(d->udev));
197}
198
199#ifdef UDEV_HAVE_GET_SYSATTR_LIST_ENTRY
200QStringList Device::sysfsProperties() const
201{
202 if (!d) {
203 return QStringList();
204 }
205
206 return listFromListEntry(udev_device_get_sysattr_list_entry(d->udev));
207}
208#endif
209
210Device Device::parent() const
211{
212 if (!d) {
213 return Device();
214 }
215
216 struct udev_device *p = udev_device_get_parent(d->udev);
217
218 if (!p) {
219 return Device();
220 }
221
222 return Device(new DevicePrivate(p));
223}
224
225QVariant Device::deviceProperty(const QString &name) const
226{
227 if (!d) {
228 return QVariant();
229 }
230
231 QByteArray propName = name.toLatin1();
232 QString propValue = QString::fromLatin1(udev_device_get_property_value(d->udev, propName.constData()));
233 if (!propValue.isEmpty()) {
234 return QVariant::fromValue(propValue);
235 }
236 return QVariant();
237}
238
239QString Device::decodedDeviceProperty(const QString &name) const
240{
241 if (!d) {
242 return QString();
243 }
244
245 QByteArray propName = name.toLatin1();
246 return d->decodePropertyValue(udev_device_get_property_value(d->udev, propName.constData()));
247}
248
249QVariant Device::sysfsProperty(const QString &name) const
250{
251 if (!d) {
252 return QVariant();
253 }
254
255 QByteArray propName = name.toLatin1();
256 QString propValue = QString::fromLatin1(udev_device_get_sysattr_value(d->udev, propName.constData()));
257 if (!propValue.isEmpty()) {
258 return QVariant::fromValue(propValue);
259 }
260 return QVariant();
261}
262
263Device Device::ancestorOfType(const QString &subsys, const QString &devtype) const
264{
265 if (!d) {
266 return Device();
267 }
268
269 struct udev_device *p = udev_device_get_parent_with_subsystem_devtype(d->udev, subsys.toLatin1().constData(), devtype.toLatin1().constData());
270
271 if (!p) {
272 return Device();
273 }
274
275 return Device(new DevicePrivate(p));
276}
277
278}
KIOCORE_EXPORT QString number(KIO::filesize_t size)
QString name(StandardAction id)
QByteArray & append(QByteArrayView data)
char at(qsizetype i) const const
const char * constData() const const
qsizetype length() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
int toInt(bool *ok, int base) const const
QByteArray toLatin1() const const
QTextStream & hex(QTextStream &stream)
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:07:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.