Solid

udevdevice.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Rafael Fernández López <ereslibre@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "udevdevice.h"
8
9#include "cpuinfo.h"
10#include "udevblock.h"
11#include "udevcamera.h"
12#include "udevgenericinterface.h"
13#include "udevportablemediaplayer.h"
14#include "udevprocessor.h"
15
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <linux/if_arp.h>
19
20using namespace Solid::Backends::UDev;
21
22UDevDevice::UDevDevice(const UdevQt::Device device)
23 : Solid::Ifaces::Device()
24 , m_device(device)
25{
26}
27
28UDevDevice::~UDevDevice()
29{
30}
31
32QString UDevDevice::udi() const
33{
34 return devicePath();
35}
36
37QString UDevDevice::parentUdi() const
38{
39 return QStringLiteral(UDEV_UDI_PREFIX);
40}
41
42QString UDevDevice::vendor() const
43{
44 QString vendor = m_device.sysfsProperty(QStringLiteral("manufacturer")).toString();
45 if (vendor.isEmpty()) {
46 if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
47 // sysfs doesn't have anything useful here
48 vendor = extractCpuVendor(deviceNumber());
49 }
50
51 if (vendor.isEmpty()) {
52 vendor = m_device.deviceProperty(QStringLiteral("ID_VENDOR")).toString().replace(QLatin1Char('_'), QLatin1Char(' '));
53 }
54 }
55 return vendor;
56}
57
58QString UDevDevice::product() const
59{
60 QString product = m_device.sysfsProperty(QStringLiteral("product")).toString();
61 if (product.isEmpty()) {
62 if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
63 // sysfs doesn't have anything useful here
64 product = extractCpuModel(deviceNumber());
65 }
66
67 if (product.isEmpty()) {
68 product = m_device.deviceProperty(QStringLiteral("ID_MODEL")).toString().replace(QLatin1Char('_'), QLatin1Char(' '));
69 }
70 }
71 return product;
72}
73
74QString UDevDevice::icon() const
75{
76 if (parentUdi().isEmpty()) {
77 return QLatin1String("computer");
78 }
79
80 if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
81 return QLatin1String("cpu");
82 } else if (queryDeviceInterface(Solid::DeviceInterface::PortableMediaPlayer)) {
83 // TODO: check out special cases like iPod
84 return QLatin1String("multimedia-player");
85 } else if (queryDeviceInterface(Solid::DeviceInterface::Camera)) {
86 return QLatin1String("camera-photo");
87 }
88
89 return QString();
90}
91
92QStringList UDevDevice::emblems() const
93{
94 return QStringList();
95}
96
97QString UDevDevice::description() const
98{
99 if (parentUdi().isEmpty()) {
100 return tr("Computer");
101 }
102
103 if (queryDeviceInterface(Solid::DeviceInterface::Processor)) {
104 return tr("Processor");
105 } else if (queryDeviceInterface(Solid::DeviceInterface::PortableMediaPlayer)) {
106 /*
107 * HACK: As Media player is very generic return the device product instead
108 * until we can return the Name.
109 */
110 const PortableMediaPlayer *player = new PortableMediaPlayer(const_cast<UDevDevice *>(this));
111 if (player->supportedProtocols().contains(QStringLiteral("mtp"))) {
112 return product();
113 } else {
114 // TODO: check out special cases like iPod
115 return tr("Portable Media Player");
116 }
117 } else if (queryDeviceInterface(Solid::DeviceInterface::Camera)) {
118 return tr("Camera");
119 }
120
121 return QString();
122}
123
124bool UDevDevice::queryDeviceInterface(const Solid::DeviceInterface::Type &type) const
125{
126 switch (type) {
127 case Solid::DeviceInterface::GenericInterface:
128 return true;
129
130 case Solid::DeviceInterface::Processor:
131 return m_device.subsystem() == QLatin1String("cpu");
132
133 case Solid::DeviceInterface::Camera:
134 return m_device.subsystem() == QLatin1String("usb") && property(QStringLiteral("ID_GPHOTO2")).isValid();
135
136 case Solid::DeviceInterface::PortableMediaPlayer:
137 return m_device.subsystem() == QLatin1String("usb") && property(QStringLiteral("ID_MEDIA_PLAYER")).isValid();
138
139 case Solid::DeviceInterface::Block:
140 return !property(QStringLiteral("MAJOR")).toString().isEmpty();
141
142 default:
143 return false;
144 }
145}
146
147QObject *UDevDevice::createDeviceInterface(const Solid::DeviceInterface::Type &type)
148{
149 if (!queryDeviceInterface(type)) {
150 return nullptr;
151 }
152
153 switch (type) {
154 case Solid::DeviceInterface::GenericInterface:
155 return new GenericInterface(this);
156
157 case Solid::DeviceInterface::Processor:
158 return new Processor(this);
159
160 case Solid::DeviceInterface::Camera:
161 return new Camera(this);
162
163 case Solid::DeviceInterface::PortableMediaPlayer:
164 return new PortableMediaPlayer(this);
165
166 case Solid::DeviceInterface::Block:
167 return new Block(this);
168
169 default:
170 qFatal("Shouldn't happen");
171 return nullptr;
172 }
173}
174
175QString UDevDevice::device() const
176{
177 return devicePath();
178}
179
180QVariant UDevDevice::property(const QString &key) const
181{
182 const QVariant res = m_device.deviceProperty(key);
183 if (res.isValid()) {
184 return res;
185 }
186 return m_device.sysfsProperty(key);
187}
188
189QMap<QString, QVariant> UDevDevice::allProperties() const
190{
192 const QStringList properties = m_device.deviceProperties();
193 for (const QString &prop : properties) {
194 res[prop] = property(prop);
195 }
196 return res;
197}
198
199bool UDevDevice::propertyExists(const QString &key) const
200{
201 return m_device.deviceProperties().contains(key);
202}
203
204QString UDevDevice::systemAttribute(const char *attribute) const
205{
206 return m_device.sysfsProperty(QString::fromLatin1(attribute)).toString();
207}
208
209QString UDevDevice::deviceName() const
210{
211 return m_device.sysfsPath();
212}
213
214int UDevDevice::deviceNumber() const
215{
216 return m_device.sysfsNumber();
217}
218
219QString UDevDevice::devicePath() const
220{
221 return QStringLiteral(UDEV_UDI_PREFIX) + deviceName();
222}
223
224UdevQt::Device UDevDevice::udevDevice()
225{
226 return m_device;
227}
228
229#include "moc_udevdevice.cpp"
Type
This enum type defines the type of device interface that a Device can have.
This class allows applications to deal with devices available in the underlying system.
KGuiItem properties()
QString tr(const char *sourceText, const char *disambiguation, int n)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
bool isValid() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:01:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.