Solid

iokitstorageaccess.cpp
1/*
2 SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "iokitstorageaccess.h"
8
9#include <CoreFoundation/CoreFoundation.h>
10#include <DiskArbitration/DiskArbitration.h>
11
12using namespace Solid::Backends::IOKit;
13
14IOKitStorageAccess::IOKitStorageAccess(IOKitDevice *device)
15 : DeviceInterface(device)
16 , daDict(new DADictionary(device))
17{
18 connect(device, SIGNAL(propertyChanged(QMap<QString, int>)), this, SLOT(onPropertyChanged(QMap<QString, int>)));
19}
20
21IOKitStorageAccess::IOKitStorageAccess(const IOKitDevice *device)
22 : DeviceInterface(device)
23 , daDict(new DADictionary(device))
24{
25 connect(device, SIGNAL(propertyChanged(QMap<QString, int>)), this, SLOT(onPropertyChanged(QMap<QString, int>)));
26}
27
28IOKitStorageAccess::~IOKitStorageAccess()
29{
30 delete daDict;
31}
32
33bool IOKitStorageAccess::isAccessible() const
34{
35 filePath();
36 const QVariant isMounted = m_device->property(QStringLiteral("isMounted"));
37 return isMounted.isValid() && isMounted.toBool();
38}
39
40QString IOKitStorageAccess::filePath() const
41{
42 // mount points can change (but can they between invocations of filePath()?)
43 QString mountPoint;
44 if (const CFURLRef urlRef = daDict->cfUrLRefForKey(kDADiskDescriptionVolumePathKey)) {
45 const CFStringRef mpRef = CFURLCopyFileSystemPath(urlRef, kCFURLPOSIXPathStyle);
46 mountPoint = QString::fromCFString(mpRef);
47 CFRelease(mpRef);
48 m_device->setProperty("mountPoint", QVariant(mountPoint));
49 bool isMounted = !mountPoint.isEmpty();
50 const QString isMountedKey = QStringLiteral("isMounted");
51 const QVariant wasMounted = m_device->property(isMountedKey);
52 if (wasMounted.isValid() && wasMounted.toBool() != isMounted) {
53 IOKitStorageAccess(m_device).onPropertyChanged(QMap<QString, int>{{isMountedKey, isMounted}});
54 }
55 m_device->setProperty("isMounted", QVariant(isMounted));
56 }
57 return mountPoint;
58}
59
60bool IOKitStorageAccess::isIgnored() const
61{
62 if (m_device->iOKitPropertyExists(QStringLiteral("Open"))) {
63 // ignore storage volumes that aren't mounted
64 bool isIgnored = m_device->property(QStringLiteral("Open")).toBool() == false;
65 m_device->setProperty("isIgnored", QVariant(isIgnored));
66 return isIgnored;
67 }
68 const QVariant isIgnored = m_device->property(QStringLiteral("isIgnored"));
69 return isIgnored.isValid() && isIgnored.toBool();
70}
71
72bool IOKitStorageAccess::isEncrypted() const
73{
74 // TODO: Implementation left for IOKit developer
75 return false;
76}
77
78bool IOKitStorageAccess::setup()
79{
80 // TODO?
81 return false;
82}
83
84bool IOKitStorageAccess::teardown()
85{
86 // TODO?
87 return false;
88}
89
90void IOKitStorageAccess::onPropertyChanged(const QMap<QString, int> &changes)
91{
92 for (auto it = changes.cbegin(); it != changes.cend(); ++it) {
93 if (it.key() == QLatin1String("isMounted")) {
94 Q_EMIT accessibilityChanged(m_device->property(QStringLiteral("isMounted")).toBool(), m_device->udi());
95 }
96 }
97}
98
99#include "moc_iokitstorageaccess.cpp"
const_iterator cbegin() const const
const_iterator cend() const const
Q_EMITQ_EMIT
bool setProperty(const char *name, QVariant &&value)
QString fromCFString(CFStringRef string)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isValid() const const
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:56:28 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.