Changeset View
Changeset View
Standalone View
Standalone View
src/solid/devices/backends/iokit/cfhelper.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright 2009 Harald Fernengel <harry@kdevelop.org> | 2 | Copyright 2009 Harald Fernengel <harry@kdevelop.org> | ||
3 | Copyright 2017 René J.V. Bertin <rjvbertin@gmail.com> | ||||
3 | 4 | | |||
4 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | 7 | 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 | 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 | 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 | 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 | act as a proxy defined in Section 6 of version 3 of the license. | ||
Show All 11 Lines | |||||
22 | #include <QtCore/qstring.h> | 23 | #include <QtCore/qstring.h> | ||
23 | #include <QtCore/qstringlist.h> | 24 | #include <QtCore/qstringlist.h> | ||
24 | #include <QtCore/qvariant.h> | 25 | #include <QtCore/qvariant.h> | ||
25 | #include <QtCore/qdatetime.h> | 26 | #include <QtCore/qdatetime.h> | ||
26 | #include <QtCore/qdebug.h> | 27 | #include <QtCore/qdebug.h> | ||
27 | 28 | | |||
28 | #include <CoreFoundation/CoreFoundation.h> | 29 | #include <CoreFoundation/CoreFoundation.h> | ||
29 | 30 | | |||
31 | #include <sys/sysctl.h> | ||||
32 | | ||||
30 | /* helper classes to convert from CF types to Qt */ | 33 | /* helper classes to convert from CF types to Qt */ | ||
31 | 34 | | |||
32 | static QString q_toString(const CFStringRef &str) | 35 | static QString q_toString(const CFStringRef &str) | ||
33 | { | 36 | { | ||
34 | CFIndex length = CFStringGetLength(str); | 37 | CFIndex length = CFStringGetLength(str); | ||
35 | QVarLengthArray<UniChar> buffer(length); | 38 | QVarLengthArray<UniChar> buffer(length); | ||
36 | 39 | | |||
37 | CFRange range = { 0, length }; | 40 | CFRange range = { 0, length }; | ||
38 | CFStringGetCharacters(str, range, buffer.data()); | 41 | CFStringGetCharacters(str, range, buffer.data()); | ||
39 | return QString(reinterpret_cast<const QChar *>(buffer.data()), length); | 42 | return QString(reinterpret_cast<const QChar *>(buffer.data()), length); | ||
40 | } | 43 | } | ||
41 | 44 | | |||
42 | template <typename T> | 45 | template <typename T> | ||
43 | static inline T convertCFNumber(const CFNumberRef &num, CFNumberType type) | 46 | static inline T convertCFNumber(const CFNumberRef &num, CFNumberType type) | ||
44 | { | 47 | { | ||
45 | T n; | 48 | T n; | ||
46 | CFNumberGetValue(num, type, &n); | 49 | CFNumberGetValue(num, type, &n); | ||
47 | return n; | 50 | return n; | ||
48 | } | 51 | } | ||
49 | 52 | | |||
50 | static QVariant q_toVariant(const CFTypeRef &obj) | 53 | static QVariant q_toVariant(const CFTypeRef &obj) | ||
51 | { | 54 | { | ||
52 | const CFTypeID typeId = CFGetTypeID(obj); | 55 | const CFTypeID typeId = CFGetTypeID(obj); | ||
53 | 56 | | |||
anthonyfieroni: Remove verbose | |||||
54 | if (typeId == CFStringGetTypeID()) { | 57 | if (typeId == CFStringGetTypeID()) { | ||
55 | return QVariant(q_toString(static_cast<const CFStringRef>(obj))); | 58 | return QVariant(q_toString(static_cast<const CFStringRef>(obj))); | ||
56 | } | 59 | } | ||
57 | 60 | | |||
58 | if (typeId == CFNumberGetTypeID()) { | 61 | if (typeId == CFNumberGetTypeID()) { | ||
59 | const CFNumberRef num = static_cast<const CFNumberRef>(obj); | 62 | const CFNumberRef num = static_cast<const CFNumberRef>(obj); | ||
60 | const CFNumberType type = CFNumberGetType(num); | 63 | const CFNumberType type = CFNumberGetType(num); | ||
61 | switch (type) { | 64 | switch (type) { | ||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | 172 | for (int i = 0; i < count; ++i) { | |||
170 | const QString key = q_toString((CFStringRef)keys[i]); | 173 | const QString key = q_toString((CFStringRef)keys[i]); | ||
171 | const QVariant value = q_toVariant((CFTypeRef)values[i]); | 174 | const QVariant value = q_toVariant((CFTypeRef)values[i]); | ||
172 | result[key] = value; | 175 | result[key] = value; | ||
173 | } | 176 | } | ||
174 | 177 | | |||
175 | return result; | 178 | return result; | ||
176 | } | 179 | } | ||
177 | 180 | | |||
181 | bool q_sysctlbyname(const char *name, QString &result) | ||||
182 | { | ||||
183 | char *property = nullptr; | ||||
184 | size_t size = 0; | ||||
185 | int error = 0; | ||||
186 | if (name && sysctlbyname(name, nullptr, &size, nullptr, 0) == 0 && size > 0) { | ||||
187 | property = new char [size]; | ||||
188 | error = sysctlbyname(name, property, &size, nullptr, 0); | ||||
189 | if (!error) { | ||||
190 | result = QLatin1String(property); | ||||
191 | } | ||||
192 | delete[] property; | ||||
193 | } | ||||
194 | return !error; | ||||
195 | } |
Remove verbose