File indexing completed on 2024-06-09 04:00:39

0001 /*
0002     SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "iokitstorage.h"
0008 
0009 #include <CoreFoundation/CoreFoundation.h>
0010 #include <DiskArbitration/DiskArbitration.h>
0011 
0012 using namespace Solid::Backends::IOKit;
0013 
0014 IOKitStorage::IOKitStorage(IOKitDevice *device)
0015     : Block(device)
0016     , daDict(new DADictionary(device))
0017 {
0018 }
0019 
0020 IOKitStorage::IOKitStorage(const IOKitDevice *device)
0021     : Block(device)
0022     , daDict(new DADictionary(device))
0023 {
0024 }
0025 
0026 IOKitStorage::~IOKitStorage()
0027 {
0028     delete daDict;
0029 }
0030 
0031 Solid::StorageDrive::Bus IOKitStorage::bus() const
0032 {
0033     const QString udi = m_device->udi();
0034     // TODO: figure out how to return something useful here.
0035     if (udi.contains(QStringLiteral("/SATA@"))) {
0036         return Solid::StorageDrive::Sata;
0037     }
0038     if (udi.contains(QStringLiteral("/SDXC@"))) {
0039         // TODO: return something finer grained; the built-in card reader
0040         // is NOT connected via USB on Macs, for instance (but there's no PCI option)
0041         return Solid::StorageDrive::Usb;
0042     }
0043     if (udi.contains(QStringLiteral("/IOUSBInterface@"))) {
0044         return Solid::StorageDrive::Usb;
0045     }
0046     if (daDict->stringForKey(kDADiskDescriptionDeviceProtocolKey) == QStringLiteral("USB")) {
0047         return Solid::StorageDrive::Usb;
0048     }
0049     return Solid::StorageDrive::Platform;
0050 }
0051 
0052 Solid::StorageDrive::DriveType IOKitStorage::driveType() const
0053 {
0054     const QString udi = m_device->udi();
0055     const QString type = m_device->property(QLatin1String("className")).toString();
0056 
0057     if (type == QStringLiteral("IOCDMedia") //
0058         || type == QStringLiteral("IOBDMedia") //
0059         || type == QStringLiteral("IODVDMedia")) {
0060         return Solid::StorageDrive::CdromDrive;
0061     }
0062     if (udi.contains(QStringLiteral("/SDXC@"))) {
0063         return Solid::StorageDrive::SdMmc;
0064     }
0065     if (daDict->stringForKey(kDADiskDescriptionDeviceModelKey) == QStringLiteral("Compact Flash")) {
0066         return Solid::StorageDrive::CompactFlash;
0067     }
0068     return Solid::StorageDrive::HardDisk;
0069 }
0070 
0071 bool IOKitStorage::isRemovable() const
0072 {
0073     bool isInternal = false;
0074     daDict->boolForKey(kDADiskDescriptionDeviceInternalKey, isInternal);
0075     return !isInternal || m_device->property(QLatin1String("Removable")).toBool();
0076 }
0077 
0078 bool IOKitStorage::isHotpluggable() const
0079 {
0080     const Solid::StorageDrive::DriveType type = driveType();
0081     return bus() == Solid::StorageDrive::Usb //
0082         || type == Solid::StorageDrive::CdromDrive //
0083         || type == Solid::StorageDrive::SdMmc;
0084 }
0085 
0086 qulonglong IOKitStorage::size() const
0087 {
0088     return m_device->property(QLatin1String("Size")).toULongLong();
0089 }
0090 
0091 QString IOKitStorage::vendor() const
0092 {
0093     return daDict->stringForKey(kDADiskDescriptionDeviceVendorKey);
0094 }
0095 
0096 QString IOKitStorage::product() const
0097 {
0098     return daDict->stringForKey(kDADiskDescriptionDeviceModelKey);
0099 }
0100 
0101 QString IOKitStorage::description() const
0102 {
0103     return daDict->stringForKey(kDADiskDescriptionMediaNameKey);
0104 }
0105 
0106 #include "moc_iokitstorage.cpp"