Changeset View
Changeset View
Standalone View
Standalone View
src/solid/devices/backends/iokit/dadictionary.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2018 René J.V. Bertin <rjvbertin@gmail.com> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "dadictionary_p.h" | ||||
22 | | ||||
23 | using namespace Solid::Backends::IOKit; | ||||
24 | | ||||
25 | DADictionary::DADictionary(const IOKitDevice *device) | ||||
26 | : device(device) | ||||
27 | , daDict(nullptr) | ||||
28 | { | ||||
29 | daSession = DASessionCreate(kCFAllocatorDefault); | ||||
30 | if (daSession) { | ||||
31 | const QString devName = device->property(QStringLiteral("BSD Name")).toString(); | ||||
32 | daRef = DADiskCreateFromBSDName(kCFAllocatorDefault, daSession, devName.toStdString().c_str()); | ||||
33 | } else { | ||||
34 | daRef = nullptr; | ||||
35 | } | ||||
36 | } | ||||
37 | | ||||
38 | DADictionary::~DADictionary() | ||||
39 | { | ||||
40 | releaseDict(); | ||||
41 | if (daRef) { | ||||
42 | CFRelease(daRef); | ||||
43 | daRef = nullptr; | ||||
44 | } | ||||
45 | if (daSession) { | ||||
46 | CFRelease(daSession); | ||||
47 | daSession = nullptr; | ||||
48 | } | ||||
49 | } | ||||
50 | | ||||
51 | bool DADictionary::getDict() | ||||
52 | { | ||||
53 | if (daRef) { | ||||
54 | daDict = DADiskCopyDescription(daRef); | ||||
55 | } | ||||
56 | return daRef != nullptr; | ||||
57 | } | ||||
58 | | ||||
59 | void DADictionary::releaseDict() | ||||
60 | { | ||||
61 | if (daDict) { | ||||
62 | CFRelease(daDict); | ||||
63 | daDict = nullptr; | ||||
64 | } | ||||
65 | } | ||||
66 | | ||||
67 | const QString DADictionary::stringForKey(const CFStringRef key) | ||||
68 | { | ||||
69 | QString ret; | ||||
70 | if (getDict()) { | ||||
71 | ret = QString::fromCFString((const CFStringRef) CFDictionaryGetValue(daDict, key)); | ||||
72 | } | ||||
73 | releaseDict(); | ||||
74 | return ret; | ||||
75 | } | ||||
76 | | ||||
77 | CFURLRef DADictionary::cfUrLRefForKey(const CFStringRef key) | ||||
78 | { | ||||
79 | CFURLRef ret = nullptr; | ||||
80 | if (getDict()) { | ||||
81 | ret = (const CFURLRef) CFDictionaryGetValue(daDict, key); | ||||
82 | } | ||||
83 | // we cannot release the dictionary here, or else we'd need to | ||||
84 | // copy the CFURLRef and oblige our caller to release the return value. | ||||
85 | return ret; | ||||
86 | } | ||||
87 | | ||||
88 | bool DADictionary::boolForKey(const CFStringRef key, bool &value) | ||||
89 | { | ||||
90 | if (getDict()) { | ||||
91 | const CFBooleanRef boolRef = (const CFBooleanRef) CFDictionaryGetValue(daDict, key); | ||||
92 | if (boolRef) { | ||||
93 | value = CFBooleanGetValue(boolRef); | ||||
94 | } | ||||
95 | return boolRef != nullptr; | ||||
96 | } | ||||
97 | return false; | ||||
98 | } |