10#include "private/qcore_mac_p.h"
17using namespace Qt::StringLiterals;
23
24
25
26
27
28
34 static const int NumKnights = 3;
35 static const char knightsOfTheRoundTable[NumKnights] = {
'/',
'.',
'\xb7' };
38 for (
int i = 0; i < result.size(); ++i) {
39 for (
int j = 0; j < NumKnights; ++j) {
40 if (result.at(i) == QLatin1Char(knightsOfTheRoundTable[j])) {
41 result[i] = QLatin1Char(knightsOfTheRoundTable[(j + shift) % NumKnights]).unicode();
51 return rotateSlashesDotsAndMiddots(key, Macify).toCFString();
56 return rotateSlashesDotsAndMiddots(QString::fromCFString(cfkey), Qtify);
64 QVarLengthArray<QCFType<CFPropertyListRef>> cfvalues(n);
65 for (
int i = 0; i < n; ++i)
66 cfvalues[i] = macValue(list.at(i));
67 return CFArrayCreate(kCFAllocatorDefault,
reinterpret_cast<
const void **>(cfvalues.data()),
68 CFIndex(n), &kCFTypeArrayCallBacks);
73 CFPropertyListRef result = 0;
75 switch (value.metaType().id()) {
76 case QMetaType::QByteArray:
78 QByteArray ba = value.toByteArray();
79 result = CFDataCreate(kCFAllocatorDefault,
reinterpret_cast<
const UInt8 *>(ba.data()),
84 case QMetaType::QVariantList:
85 case QMetaType::QStringList:
86 case QMetaType::QPolygon:
87 result = macList(value.toList());
89 case QMetaType::QVariantMap:
91 const QVariantMap &map = value.toMap();
92 const int mapSize = map.size();
94 QVarLengthArray<QCFType<CFPropertyListRef>> cfkeys;
95 cfkeys.reserve(mapSize);
96 std::transform(map.keyBegin(), map.keyEnd(),
97 std::back_inserter(cfkeys),
98 [](
const auto &key) {
return key.toCFString(); });
100 QVarLengthArray<QCFType<CFPropertyListRef>> cfvalues;
101 cfvalues.reserve(mapSize);
102 std::transform(map.begin(), map.end(),
103 std::back_inserter(cfvalues),
104 [](
const auto &value) {
return macValue(value); });
106 result = CFDictionaryCreate(kCFAllocatorDefault,
107 reinterpret_cast<
const void **>(cfkeys.data()),
108 reinterpret_cast<
const void **>(cfvalues.data()),
110 &kCFTypeDictionaryKeyCallBacks,
111 &kCFTypeDictionaryValueCallBacks);
114 case QMetaType::QDateTime:
116 QDateTime dateTime = value.toDateTime();
118 if (dateTime.timeSpec() == Qt::LocalTime)
119 result = dateTime.toCFDate();
124 case QMetaType::Bool:
125 result = value.toBool() ? kCFBooleanTrue : kCFBooleanFalse;
128 case QMetaType::UInt:
130 int n = value.toInt();
131 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n);
134 case QMetaType::Double:
136 double n = value.toDouble();
137 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &n);
140 case QMetaType::LongLong:
141 case QMetaType::ULongLong:
143 qint64 n = value.toLongLong();
144 result = CFNumberCreate(0, kCFNumberLongLongType, &n);
147 case QMetaType::QString:
150 QString string = QSettingsPrivate::variantToString(value);
151 if (string.contains(QChar::Null))
152 result = std::move(string).toUtf8().toCFData();
154 result = string.toCFString();
164 CFTypeID typeId = CFGetTypeID(cfvalue);
167
168
169 if (typeId == CFStringGetTypeID()) {
170 return QSettingsPrivate::stringToVariant(QString::fromCFString(
static_cast<CFStringRef>(cfvalue)));
171 }
else if (typeId == CFNumberGetTypeID()) {
172 CFNumberRef cfnumber =
static_cast<CFNumberRef>(cfvalue);
173 if (CFNumberIsFloatType(cfnumber)) {
175 CFNumberGetValue(cfnumber, kCFNumberDoubleType, &d);
181 if (CFNumberGetType(cfnumber) == kCFNumberIntType) {
182 CFNumberGetValue(cfnumber, kCFNumberIntType, &i);
185 CFNumberGetValue(cfnumber, kCFNumberLongLongType, &ll);
188 }
else if (typeId == CFArrayGetTypeID()) {
189 CFArrayRef cfarray =
static_cast<CFArrayRef>(cfvalue);
190 QList<QVariant> list;
191 CFIndex size = CFArrayGetCount(cfarray);
192 bool metNonString =
false;
193 for (CFIndex i = 0; i < size; ++i) {
194 QVariant value = qtValue(CFArrayGetValueAtIndex(cfarray, i));
195 if (value.typeId() != QMetaType::QString)
202 return QVariant(list).toStringList();
203 }
else if (typeId == CFBooleanGetTypeID()) {
204 return (
bool)CFBooleanGetValue(
static_cast<CFBooleanRef>(cfvalue));
205 }
else if (typeId == CFDataGetTypeID()) {
206 QByteArray byteArray = QByteArray::fromRawCFData(
static_cast<CFDataRef>(cfvalue));
210 if (!byteArray.startsWith(
'@')) {
215 const QString str = QString::fromUtf8(byteArray.constData(), byteArray.size());
216 QVariant variant = QSettingsPrivate::stringToVariant(str);
217 if (variant == QVariant(str)) {
225 }
else if (typeId == CFDictionaryGetTypeID()) {
226 CFDictionaryRef cfdict =
static_cast<CFDictionaryRef>(cfvalue);
227 CFTypeID arrayTypeId = CFArrayGetTypeID();
228 int size = (
int)CFDictionaryGetCount(cfdict);
229 QVarLengthArray<CFPropertyListRef> keys(size);
230 QVarLengthArray<CFPropertyListRef> values(size);
231 CFDictionaryGetKeysAndValues(cfdict, keys.data(), values.data());
234 for (
int i = 0; i < size; ++i) {
235 QString key = QString::fromCFString(
static_cast<CFStringRef>(keys[i]));
237 if (CFGetTypeID(values[i]) == arrayTypeId) {
238 CFArrayRef cfarray =
static_cast<CFArrayRef>(values[i]);
239 CFIndex arraySize = CFArrayGetCount(cfarray);
241 list.reserve(arraySize);
242 for (CFIndex j = 0; j < arraySize; ++j)
243 list.append(qtValue(CFArrayGetValueAtIndex(cfarray, j)));
244 map.insert(key, list);
246 map.insert(key, qtValue(values[i]));
250 }
else if (typeId == CFDateGetTypeID()) {
251 return QDateTime::fromCFDate(
static_cast<CFDateRef>(cfvalue));
258 for (
int i = organization.size() - 1; i >= 0; --i) {
259 QChar ch = organization.at(i);
260 if (ch == u'.' || ch == QChar(0x3002) || ch == QChar(0xff0e)
261 || ch == QChar(0xff61)) {
262 QString suffix = organization.mid(i + 1).toLower();
263 if (suffix.size() == 2 || suffix ==
"com"_L1 || suffix ==
"org"_L1
264 || suffix ==
"net"_L1 || suffix ==
"edu"_L1 || suffix ==
"gov"_L1
265 || suffix ==
"mil"_L1 || suffix ==
"biz"_L1 || suffix ==
"info"_L1
266 || suffix ==
"name"_L1 || suffix ==
"pro"_L1 || suffix ==
"aero"_L1
267 || suffix ==
"coop"_L1 || suffix ==
"museum"_L1) {
269 result.replace(u'/', u' ');
274 int uc = ch.unicode();
275 if ((uc <
'a' || uc >
'z') && (uc <
'A' || uc >
'Z'))
280 for (
int i = 0; i < organization.size(); ++i) {
281 QChar ch = organization.at(i);
282 int uc = ch.unicode();
283 if ((uc >=
'a' && uc <=
'z') || (uc >=
'0' && uc <=
'9')) {
285 }
else if (uc >=
'A' && uc <=
'Z') {
286 domain += ch.toLower();
291 domain = domain.simplified();
292 domain.replace(u' ', u'-');
293 if (!domain.isEmpty())
294 domain.append(
".com"_L1);
305 void remove(
const QString &key)
override;
306 void set(
const QString &key,
const QVariant &value)
override;
318 CFStringRef userName;
319 CFStringRef applicationOrSuiteId;
322 QCFString applicationId;
325 SearchDomain domains[6];
330 const QString &application)
331 : QSettingsPrivate(QSettings::NativeFormat, scope, organization, application)
338 QString domainName = comify(organization);
340 if (domainName.isEmpty()) {
341 CFBundleRef main_bundle = CFBundleGetMainBundle();
342 if (main_bundle != NULL) {
343 CFStringRef main_bundle_identifier = CFBundleGetIdentifier(main_bundle);
344 if (main_bundle_identifier != NULL) {
345 QString bundle_identifier(qtKey(main_bundle_identifier));
347 QStringList bundle_identifier_components = bundle_identifier.split(u'/');
349 QStringList bundle_identifier_components_reversed;
350 for (
int i=0; i<bundle_identifier_components.size(); ++i) {
351 const QString &bundle_identifier_component = bundle_identifier_components.at(i);
352 bundle_identifier_components_reversed.push_front(bundle_identifier_component);
354 domainName = bundle_identifier_components_reversed.join(u'.');
359 if (domainName.isEmpty())
360 domainName =
"unknown-organization.trolltech.com"_L1;
362 while ((nextDot = domainName.indexOf(u'.', curPos)) != -1) {
363 javaPackageName.prepend(QStringView{domainName}.mid(curPos, nextDot - curPos));
364 javaPackageName.prepend(u'.');
365 curPos = nextDot + 1;
367 javaPackageName.prepend(QStringView{domainName}.mid(curPos));
368 javaPackageName = std::move(javaPackageName).toLower();
370 javaPackageName.prepend(
"com."_L1);
371 suiteId = javaPackageName;
373 if (!application.isEmpty()) {
374 javaPackageName += u'.' + application;
375 applicationId = javaPackageName;
379 for (
int i = (scope == QSettings::SystemScope) ? 1 : 0; i < 2; ++i) {
380 for (
int j = (application.isEmpty()) ? 1 : 0; j < 3; ++j) {
381 SearchDomain &domain = domains[numDomains++];
382 domain.userName = (i == 0) ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
384 domain.applicationOrSuiteId = applicationId;
386 domain.applicationOrSuiteId = suiteId;
388 domain.applicationOrSuiteId = kCFPreferencesAnyApplication;
392 hostName = (scope == QSettings::SystemScope) ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
402 QStringList keys = children(key + u'/', AllKeys);
405 for (
int i = -1; i < keys.size(); ++i) {
409 subKey += keys.at(i);
411 CFPreferencesSetValue(macKey(subKey), 0, domains[0].applicationOrSuiteId,
412 domains[0].userName, hostName);
418 CFPreferencesSetValue(macKey(key), macValue(value), domains[0].applicationOrSuiteId,
419 domains[0].userName, hostName);
424 QCFString k = macKey(key);
425 for (
int i = 0; i < numDomains; ++i) {
427 QCFType<CFPropertyListRef> ret =
428 CFPreferencesCopyValue(k, domains[i].applicationOrSuiteId, domains[i].userName,
443 int startPos = prefix.size();
445 for (
int i = 0; i < numDomains; ++i) {
447 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[i].applicationOrSuiteId,
451 CFIndex size = CFArrayGetCount(cfarray);
452 for (CFIndex k = 0; k < size; ++k) {
454 qtKey(
static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfarray, k)));
455 if (currentKey.startsWith(prefix))
456 processChild(QStringView{currentKey}.mid(startPos), spec, result);
464 std::sort(result.begin(), result.end());
465 result.erase(
std::unique(result.begin(), result.end()),
472 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[0].applicationOrSuiteId,
473 domains[0].userName, hostName);
474 CFPreferencesSetMultiple(0, cfarray, domains[0].applicationOrSuiteId, domains[0].userName,
480 for (
int i = 0; i < numDomains; ++i) {
482 Boolean ok = CFPreferencesSynchronize(domains[i].applicationOrSuiteId,
483 domains[i].userName, hostNames[j]);
485 if (!ok && i == 0 && hostNames[j] == hostName && status == QSettings::NoError) {
486 setStatus(QSettings::AccessError);
500 QString impossibleKey(
"qt_internal/"_L1);
502 QSettings::Status oldStatus = that->status;
503 that->status = QSettings::NoError;
505 that->set(impossibleKey, QVariant());
507 bool writable = (status == QSettings::NoError) && that->get(impossibleKey).has_value();
508 that->remove(impossibleKey);
511 that->status = oldStatus;
518 if (scope == QSettings::UserScope)
519 result = QDir::homePath();
520 result +=
"/Library/Preferences/"_L1;
521 result += QString::fromCFString(domains[0].applicationOrSuiteId);
522 result +=
".plist"_L1;
526QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format,
527 QSettings::Scope scope,
528 const QString &organization,
529 const QString &application)
531#ifndef QT_BOOTSTRAPPED
532 if (organization ==
"Qt"_L1)
534 QString organizationDomain = QCoreApplication::organizationDomain();
535 QString applicationName = QCoreApplication::applicationName();
537 QSettingsPrivate *newSettings;
538 if (format == QSettings::NativeFormat) {
539 newSettings =
new QMacSettingsPrivate(scope, organizationDomain, applicationName);
541 newSettings =
new QConfFileSettingsPrivate(format, scope, organizationDomain, applicationName);
544 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(organization)));
545 if (!application.isEmpty())
546 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(application)));
551 if (format == QSettings::NativeFormat) {
552 return new QMacSettingsPrivate(scope, organization, application);
554 return new QConfFileSettingsPrivate(format, scope, organization, application);
560 QCFType<CFDataRef> cfData = data.toRawCFData();
561 QCFType<CFPropertyListRef> propertyList =
562 CFPropertyListCreateWithData(kCFAllocatorDefault, cfData, kCFPropertyListImmutable,
nullptr,
nullptr);
566 if (CFGetTypeID(propertyList) != CFDictionaryGetTypeID())
569 CFDictionaryRef cfdict =
570 static_cast<CFDictionaryRef>(
static_cast<CFPropertyListRef>(propertyList));
571 int size = (
int)CFDictionaryGetCount(cfdict);
572 QVarLengthArray<CFPropertyListRef> keys(size);
573 QVarLengthArray<CFPropertyListRef> values(size);
574 CFDictionaryGetKeysAndValues(cfdict, keys.data(), values.data());
576 for (
int i = 0; i < size; ++i) {
577 QString key = qtKey(
static_cast<CFStringRef>(keys[i]));
578 map->insert(QSettingsKey(key, Qt::CaseSensitive), qtValue(values[i]));
585 QVarLengthArray<QCFType<CFStringRef> > cfkeys(map.size());
586 QVarLengthArray<QCFType<CFPropertyListRef> > cfvalues(map.size());
588 ParsedSettingsMap::const_iterator j;
589 for (j = map.constBegin(); j != map.constEnd(); ++j) {
590 cfkeys[i] = macKey(j.key());
591 cfvalues[i] = macValue(j.value());
595 QCFType<CFDictionaryRef> propertyList =
596 CFDictionaryCreate(kCFAllocatorDefault,
597 reinterpret_cast<
const void **>(cfkeys.data()),
598 reinterpret_cast<
const void **>(cfvalues.data()),
600 &kCFTypeDictionaryKeyCallBacks,
601 &kCFTypeDictionaryValueCallBacks);
603 QCFType<CFDataRef> xmlData = CFPropertyListCreateData(
604 kCFAllocatorDefault, propertyList, kCFPropertyListXMLFormat_v1_0, 0, 0);
606 return file.write(QByteArray::fromRawCFData(xmlData)) == CFDataGetLength(xmlData);
std::optional< QVariant > get(const QString &key) const override
bool isWritable() const override
void remove(const QString &key) override
QString fileName() const override
QMacSettingsPrivate(QSettings::Scope scope, const QString &organization, const QString &application)
QStringList children(const QString &prefix, ChildSpec spec) const override
void set(const QString &key, const QVariant &value) override
Combined button and popup list for selecting options.
static QVariant qtValue(CFPropertyListRef cfvalue)
static CFArrayRef macList(const QList< QVariant > &list)
static QCFType< CFStringRef > macKey(const QString &key)
static const CFStringRef hostNames[2]
static const int numHostNames
static QString rotateSlashesDotsAndMiddots(const QString &key, int shift)
static QCFType< CFPropertyListRef > macValue(const QVariant &value)
static QString qtKey(CFStringRef cfkey)
static QString comify(const QString &organization)
QMap< QSettingsKey, QVariant > ParsedSettingsMap