Solid

dadictionary.cpp
1/*
2 SPDX-FileCopyrightText: 2018 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 "dadictionary_p.h"
8
9using namespace Solid::Backends::IOKit;
10
11DADictionary::DADictionary(const IOKitDevice *device)
12 : device(device)
13 , daSession(DASessionCreate(kCFAllocatorDefault))
14 , daDict(nullptr)
15{
16 if (daSession) {
17 const QString devName = device->property(QStringLiteral("BSD Name")).toString();
18 daRef = DADiskCreateFromBSDName(kCFAllocatorDefault, daSession, devName.toStdString().c_str());
19 } else {
20 daRef = nullptr;
21 }
22}
23
24DADictionary::~DADictionary()
25{
26 releaseDict();
27 if (daRef) {
28 CFRelease(daRef);
29 daRef = nullptr;
30 }
31 if (daSession) {
32 CFRelease(daSession);
33 daSession = nullptr;
34 }
35}
36
37bool DADictionary::getDict()
38{
39 // daDict may cache the latest disk description dict;
40 // we will refresh it now.
41 releaseDict();
42 if (daRef) {
43 daDict = DADiskCopyDescription(daRef);
44 }
45 return daDict != nullptr;
46}
47
48void DADictionary::releaseDict()
49{
50 if (daDict) {
51 CFRelease(daDict);
52 daDict = nullptr;
53 }
54}
55
56const QString DADictionary::stringForKey(const CFStringRef key)
57{
58 QString ret;
59 if (getDict()) {
60 ret = QString::fromCFString((const CFStringRef)CFDictionaryGetValue(daDict, key));
61 releaseDict();
62 }
63 return ret;
64}
65
66CFURLRef DADictionary::cfUrLRefForKey(const CFStringRef key)
67{
68 CFURLRef ret = nullptr;
69 if (getDict()) {
70 ret = (const CFURLRef)CFDictionaryGetValue(daDict, key);
71 }
72 // we cannot release the dictionary here, or else we'd need to
73 // copy the CFURLRef and oblige our caller to release the return value.
74 return ret;
75}
76
77bool DADictionary::boolForKey(const CFStringRef key, bool &value)
78{
79 if (getDict()) {
80 const CFBooleanRef boolRef = (const CFBooleanRef)CFDictionaryGetValue(daDict, key);
81 if (boolRef) {
82 value = CFBooleanGetValue(boolRef);
83 }
84 releaseDict();
85 return boolRef != nullptr;
86 }
87 return false;
88}
QString fromCFString(CFStringRef string)
std::string toStdString() 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:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.