| 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 | |
| 20 | using namespace Solid::Backends::UDev; |
| 21 | |
| 22 | UDevDevice::UDevDevice(const UdevQt::Device device) |
| 23 | : Solid::Ifaces::Device() |
| 24 | , m_device(device) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | UDevDevice::~UDevDevice() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | QString UDevDevice::udi() const |
| 33 | { |
| 34 | return devicePath(); |
| 35 | } |
| 36 | |
| 37 | QString UDevDevice::parentUdi() const |
| 38 | { |
| 39 | return QStringLiteral(UDEV_UDI_PREFIX); |
| 40 | } |
| 41 | |
| 42 | QString UDevDevice::vendor() const |
| 43 | { |
| 44 | QString vendor = m_device.sysfsProperty(QStringLiteral("manufacturer")).toString(); |
| 45 | if (vendor.isEmpty()) { |
| 46 | if (queryDeviceInterface(type: Solid::DeviceInterface::Processor)) { |
| 47 | // sysfs doesn't have anything useful here |
| 48 | vendor = extractCpuVendor(processorNumber: deviceNumber()); |
| 49 | } |
| 50 | |
| 51 | if (vendor.isEmpty()) { |
| 52 | vendor = m_device.deviceProperty(QStringLiteral("ID_VENDOR")).toString().replace(before: QLatin1Char('_'), after: QLatin1Char(' ')); |
| 53 | } |
| 54 | } |
| 55 | return vendor; |
| 56 | } |
| 57 | |
| 58 | QString UDevDevice::product() const |
| 59 | { |
| 60 | QString product = m_device.sysfsProperty(QStringLiteral("product")).toString(); |
| 61 | if (product.isEmpty()) { |
| 62 | if (queryDeviceInterface(type: Solid::DeviceInterface::Processor)) { |
| 63 | // sysfs doesn't have anything useful here |
| 64 | product = extractCpuModel(processorNumber: deviceNumber()); |
| 65 | } |
| 66 | |
| 67 | if (product.isEmpty()) { |
| 68 | product = m_device.deviceProperty(QStringLiteral("ID_MODEL")).toString().replace(before: QLatin1Char('_'), after: QLatin1Char(' ')); |
| 69 | } |
| 70 | } |
| 71 | return product; |
| 72 | } |
| 73 | |
| 74 | QString UDevDevice::icon() const |
| 75 | { |
| 76 | if (parentUdi().isEmpty()) { |
| 77 | return QLatin1String("computer"); |
| 78 | } |
| 79 | |
| 80 | if (queryDeviceInterface(type: Solid::DeviceInterface::Processor)) { |
| 81 | return QLatin1String("cpu"); |
| 82 | } else if (queryDeviceInterface(type: Solid::DeviceInterface::PortableMediaPlayer)) { |
| 83 | // TODO: check out special cases like iPod |
| 84 | return QLatin1String("multimedia-player"); |
| 85 | } else if (queryDeviceInterface(type: Solid::DeviceInterface::Camera)) { |
| 86 | return QLatin1String("camera-photo"); |
| 87 | } |
| 88 | |
| 89 | return QString(); |
| 90 | } |
| 91 | |
| 92 | QStringList UDevDevice::emblems() const |
| 93 | { |
| 94 | return QStringList(); |
| 95 | } |
| 96 | |
| 97 | QString UDevDevice::description() const |
| 98 | { |
| 99 | if (parentUdi().isEmpty()) { |
| 100 | return tr(s: "Computer"); |
| 101 | } |
| 102 | |
| 103 | if (queryDeviceInterface(type: Solid::DeviceInterface::Processor)) { |
| 104 | return tr(s: "Processor"); |
| 105 | } else if (queryDeviceInterface(type: 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(s: "Portable Media Player"); |
| 116 | } |
| 117 | } else if (queryDeviceInterface(type: Solid::DeviceInterface::Camera)) { |
| 118 | return tr(s: "Camera"); |
| 119 | } |
| 120 | |
| 121 | return QString(); |
| 122 | } |
| 123 | |
| 124 | bool 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 | |
| 147 | QObject *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(msg: "Shouldn't happen"); |
| 171 | return nullptr; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | QString UDevDevice::device() const |
| 176 | { |
| 177 | return devicePath(); |
| 178 | } |
| 179 | |
| 180 | QVariant UDevDevice::property(const QString &key) const |
| 181 | { |
| 182 | const QVariant res = m_device.deviceProperty(name: key); |
| 183 | if (res.isValid()) { |
| 184 | return res; |
| 185 | } |
| 186 | return m_device.sysfsProperty(name: key); |
| 187 | } |
| 188 | |
| 189 | QMap<QString, QVariant> UDevDevice::allProperties() const |
| 190 | { |
| 191 | QMap<QString, QVariant> res; |
| 192 | const QStringList properties = m_device.deviceProperties(); |
| 193 | for (const QString &prop : properties) { |
| 194 | res[prop] = property(key: prop); |
| 195 | } |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | bool UDevDevice::propertyExists(const QString &key) const |
| 200 | { |
| 201 | return m_device.deviceProperties().contains(str: key); |
| 202 | } |
| 203 | |
| 204 | QString UDevDevice::systemAttribute(const char *attribute) const |
| 205 | { |
| 206 | return m_device.sysfsProperty(name: QString::fromLatin1(ba: attribute)).toString(); |
| 207 | } |
| 208 | |
| 209 | QString UDevDevice::deviceName() const |
| 210 | { |
| 211 | return m_device.sysfsPath(); |
| 212 | } |
| 213 | |
| 214 | int UDevDevice::deviceNumber() const |
| 215 | { |
| 216 | return m_device.sysfsNumber(); |
| 217 | } |
| 218 | |
| 219 | QString UDevDevice::devicePath() const |
| 220 | { |
| 221 | return QStringLiteral(UDEV_UDI_PREFIX) + deviceName(); |
| 222 | } |
| 223 | |
| 224 | UdevQt::Device UDevDevice::udevDevice() |
| 225 | { |
| 226 | return m_device; |
| 227 | } |
| 228 | |
| 229 | #include "moc_udevdevice.cpp" |
| 230 |
