Solid

udisksstoragedrive.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Michael Zanetti <mzanetti@kde.org>
3 SPDX-FileCopyrightText: 2010-2012 Lukáš Tinkl <ltinkl@redhat.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "udisksstoragedrive.h"
9
10#include <QDebug>
11
12using namespace Solid::Backends::UDisks2;
13
14StorageDrive::StorageDrive(Device *dev)
15 : Block(dev)
16{
17#if UDEV_FOUND
18 UdevQt::Client client(this);
19 m_udevDevice = client.deviceByDeviceFile(device());
20 m_udevDevice.deviceProperties();
21#endif
22}
23
24StorageDrive::~StorageDrive()
25{
26}
27
28qulonglong StorageDrive::size() const
29{
30 return m_device->prop(QStringLiteral("Size")).toULongLong();
31}
32
33bool StorageDrive::isHotpluggable() const
34{
35#if UDEV_FOUND
36 const Solid::StorageDrive::Bus _bus = bus();
37 /* clang-format off */
38 return _bus == Solid::StorageDrive::Usb
39 || _bus == Solid::StorageDrive::Ieee1394
40 || (m_udevDevice.deviceProperty(QStringLiteral("UDISKS_SYSTEM")).isValid()
41 && !m_udevDevice.deviceProperty(QStringLiteral("UDISKS_SYSTEM")).toBool());
42 /* clang-format on */
43#elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
44 return m_device->prop(QStringLiteral("bsdisks_IsHotpluggable")).toBool();
45#else
46#error Implement this or stub this out for your platform
47#endif
48}
49
50bool StorageDrive::isRemovable() const
51{
52 return m_device->prop(QStringLiteral("MediaRemovable")).toBool() || m_device->prop(QStringLiteral("Removable")).toBool();
53}
54
55Solid::StorageDrive::DriveType StorageDrive::driveType() const
56{
57 const QStringList mediaTypes = m_device->prop(QStringLiteral("MediaCompatibility")).toStringList();
58
59 if (m_device->isOpticalDrive()) { // optical disks
60 return Solid::StorageDrive::CdromDrive;
61 } else if (mediaTypes.contains(QStringLiteral("floppy"))) {
62 return Solid::StorageDrive::Floppy;
63 }
64#if 0 // TODO add to Solid
65 else if (mediaTypes.contains("floppy_jaz")) {
66 return Solid::StorageDrive::Jaz;
67 } else if (mediaTypes.contains("floppy_zip")) {
68 return Solid::StorageDrive::Zip;
69 } else if (mediaTypes.contains("flash")) {
70 return Solid::StorageDrive::Flash;
71 }
72#endif
73 else if (mediaTypes.contains(QStringLiteral("flash_cf"))) {
74 return Solid::StorageDrive::CompactFlash;
75 } else if (mediaTypes.contains(QStringLiteral("flash_ms"))) {
76 return Solid::StorageDrive::MemoryStick;
77 } else if (mediaTypes.contains(QStringLiteral("flash_sm"))) {
78 return Solid::StorageDrive::SmartMedia;
79 } else if (mediaTypes.contains(QStringLiteral("flash_sd")) //
80 || mediaTypes.contains(QStringLiteral("flash_sdhc")) //
81 || mediaTypes.contains(QStringLiteral("flash_mmc")) //
82 || mediaTypes.contains(QStringLiteral("flash_sdxc"))) {
83 return Solid::StorageDrive::SdMmc;
84 }
85 // FIXME: udisks2 doesn't know about xD cards
86 else {
87 return Solid::StorageDrive::HardDisk;
88 }
89}
90
91Solid::StorageDrive::Bus StorageDrive::bus() const
92{
93 const QString bus = m_device->prop(QStringLiteral("ConnectionBus")).toString();
94 const QString udevBus =
95#if UDEV_FOUND
96 m_udevDevice.deviceProperty(QStringLiteral("ID_BUS")).toString();
97#elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
98 m_device->prop(QStringLiteral("bsdisks_ConnectionBus")).toString();
99#else
100#error Implement this or stub this out for your platform
101#endif
102
103 // qDebug() << "bus:" << bus << "udev bus:" << udevBus;
104
105 if (udevBus == QLatin1String("ata")) {
106#if UDEV_FOUND
107 if (m_udevDevice.deviceProperty(QStringLiteral("ID_ATA_SATA")).toInt() == 1) { // serial ATA
108 return Solid::StorageDrive::Sata;
109 } else { // parallel (classical) ATA
110 return Solid::StorageDrive::Ide;
111 }
112#elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
113 if (m_device->prop(QStringLiteral("bsdisks_AtaSata")).toString() == QLatin1String("sata")) { // serial ATA
114 return Solid::StorageDrive::Sata;
115 } else { // parallel (classical) ATA
116 return Solid::StorageDrive::Ide;
117 }
118#else
119#error Implement this or stub this out for your platform
120#endif
121 } else if (bus == QLatin1String("usb")) {
122 return Solid::StorageDrive::Usb;
123 } else if (bus == QLatin1String("ieee1394")) {
124 return Solid::StorageDrive::Ieee1394;
125 } else if (udevBus == QLatin1String("scsi")) {
126 return Solid::StorageDrive::Scsi;
127 }
128#if 0 // TODO add these to Solid
129 else if (bus == "sdio") {
130 return Solid::StorageDrive::SDIO;
131 } else if (bus == "virtual") {
132 return Solid::StorageDrive::Virtual;
133 }
134#endif
135 else {
136 return Solid::StorageDrive::Platform;
137 }
138}
139
140QDateTime StorageDrive::timeDetected() const
141{
142 bool conversionValid;
143 const qulonglong microSecondsSinceEpoch = m_device->prop(QStringLiteral("TimeDetected")).toULongLong(&conversionValid);
144 if (!conversionValid) {
145 return QDateTime();
146 }
147 return QDateTime::fromMSecsSinceEpoch(microSecondsSinceEpoch / 1000);
148}
149
150QDateTime StorageDrive::timeMediaDetected() const
151{
152 bool conversionValid;
153 const qulonglong microSecondsSinceEpoch = m_device->prop(QStringLiteral("TimeMediaDetected")).toULongLong(&conversionValid);
154 if (!conversionValid) {
155 return QDateTime();
156 }
157 return QDateTime::fromMSecsSinceEpoch(microSecondsSinceEpoch / 1000);
158}
159
160#include "moc_udisksstoragedrive.cpp"
DriveType
This enum type defines the type of drive a storage device can be.
Bus
This enum type defines the type of bus a storage device is attached to.
char * toString(const EngineQuery &query)
bool isValid(QStringView ifopt)
QDateTime fromMSecsSinceEpoch(qint64 msecs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
bool toBool() const const
QString toString() const const
QStringList toStringList() const const
qulonglong toULongLong(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.