11#include "private/qcore_mac_p.h"
18using namespace Qt::StringLiterals;
24
25
26
27
28
29
35 static const int NumKnights = 3;
36 static const char knightsOfTheRoundTable[NumKnights] = {
'/',
'.',
'\xb7' };
39 for (
int i = 0; i < result.size(); ++i) {
40 for (
int j = 0; j < NumKnights; ++j) {
41 if (result.at(i) == QLatin1Char(knightsOfTheRoundTable[j])) {
42 result[i] = QLatin1Char(knightsOfTheRoundTable[(j + shift) % NumKnights]).unicode();
52 return rotateSlashesDotsAndMiddots(key, Macify).toCFString();
57 return rotateSlashesDotsAndMiddots(QString::fromCFString(cfkey), Qtify);
65 QVarLengthArray<QCFType<CFPropertyListRef>> cfvalues(n);
66 for (
int i = 0; i < n; ++i)
67 cfvalues[i] = macValue(list.at(i));
68 return CFArrayCreate(kCFAllocatorDefault,
reinterpret_cast<
const void **>(cfvalues.data()),
69 CFIndex(n), &kCFTypeArrayCallBacks);
74 CFPropertyListRef result = 0;
76 switch (value.metaType().id()) {
77 case QMetaType::QByteArray:
79 QByteArray ba = value.toByteArray();
80 result = CFDataCreate(kCFAllocatorDefault,
reinterpret_cast<
const UInt8 *>(ba.data()),
85 case QMetaType::QVariantList:
86 case QMetaType::QStringList:
87 case QMetaType::QPolygon:
88 result = macList(value.toList());
90 case QMetaType::QVariantMap:
92 const QVariantMap &map = value.toMap();
93 const int mapSize = map.size();
95 QVarLengthArray<QCFType<CFPropertyListRef>> cfkeys;
96 cfkeys.reserve(mapSize);
97 std::transform(map.keyBegin(), map.keyEnd(),
98 std::back_inserter(cfkeys),
99 [](
const auto &key) {
return key.toCFString(); });
101 QVarLengthArray<QCFType<CFPropertyListRef>> cfvalues;
102 cfvalues.reserve(mapSize);
103 std::transform(map.begin(), map.end(),
104 std::back_inserter(cfvalues),
105 [](
const auto &value) {
return macValue(value); });
107 result = CFDictionaryCreate(kCFAllocatorDefault,
108 reinterpret_cast<
const void **>(cfkeys.data()),
109 reinterpret_cast<
const void **>(cfvalues.data()),
111 &kCFTypeDictionaryKeyCallBacks,
112 &kCFTypeDictionaryValueCallBacks);
115 case QMetaType::QDateTime:
117 QDateTime dateTime = value.toDateTime();
119 if (dateTime.timeSpec() == Qt::LocalTime)
120 result = dateTime.toCFDate();
125 case QMetaType::Bool:
126 result = value.toBool() ? kCFBooleanTrue : kCFBooleanFalse;
129 case QMetaType::UInt:
131 int n = value.toInt();
132 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n);
135 case QMetaType::Double:
137 double n = value.toDouble();
138 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &n);
141 case QMetaType::LongLong:
142 case QMetaType::ULongLong:
144 qint64 n = value.toLongLong();
145 result = CFNumberCreate(0, kCFNumberLongLongType, &n);
148 case QMetaType::QString:
151 QString string = QSettingsPrivate::variantToString(value);
152 if (string.contains(QChar::Null))
153 result = std::move(string).toUtf8().toCFData();
155 result = string.toCFString();
165 CFTypeID typeId = CFGetTypeID(cfvalue);
168
169
170 if (typeId == CFStringGetTypeID()) {
171 return QSettingsPrivate::stringToVariant(QString::fromCFString(
static_cast<CFStringRef>(cfvalue)));
172 }
else if (typeId == CFNumberGetTypeID()) {
173 CFNumberRef cfnumber =
static_cast<CFNumberRef>(cfvalue);
174 if (CFNumberIsFloatType(cfnumber)) {
176 CFNumberGetValue(cfnumber, kCFNumberDoubleType, &d);
182 if (CFNumberGetType(cfnumber) == kCFNumberIntType) {
183 CFNumberGetValue(cfnumber, kCFNumberIntType, &i);
186 CFNumberGetValue(cfnumber, kCFNumberLongLongType, &ll);
189 }
else if (typeId == CFArrayGetTypeID()) {
190 CFArrayRef cfarray =
static_cast<CFArrayRef>(cfvalue);
191 QList<QVariant> list;
192 CFIndex size = CFArrayGetCount(cfarray);
193 bool metNonString =
false;
194 for (CFIndex i = 0; i < size; ++i) {
195 QVariant value = qtValue(CFArrayGetValueAtIndex(cfarray, i));
196 if (value.typeId() != QMetaType::QString)
203 return QVariant(list).toStringList();
204 }
else if (typeId == CFBooleanGetTypeID()) {
205 return (
bool)CFBooleanGetValue(
static_cast<CFBooleanRef>(cfvalue));
206 }
else if (typeId == CFDataGetTypeID()) {
207 QByteArray byteArray = QByteArray::fromRawCFData(
static_cast<CFDataRef>(cfvalue));
211 if (!byteArray.startsWith(
'@')) {
216 const QString str = QString::fromUtf8(byteArray.constData(), byteArray.size());
217 QVariant variant = QSettingsPrivate::stringToVariant(str);
218 if (variant == QVariant(str)) {
226 }
else if (typeId == CFDictionaryGetTypeID()) {
227 CFDictionaryRef cfdict =
static_cast<CFDictionaryRef>(cfvalue);
228 CFTypeID arrayTypeId = CFArrayGetTypeID();
229 int size = (
int)CFDictionaryGetCount(cfdict);
230 QVarLengthArray<CFPropertyListRef> keys(size);
231 QVarLengthArray<CFPropertyListRef> values(size);
232 CFDictionaryGetKeysAndValues(cfdict, keys.data(), values.data());
235 for (
int i = 0; i < size; ++i) {
236 QString key = QString::fromCFString(
static_cast<CFStringRef>(keys[i]));
238 if (CFGetTypeID(values[i]) == arrayTypeId) {
239 CFArrayRef cfarray =
static_cast<CFArrayRef>(values[i]);
240 CFIndex arraySize = CFArrayGetCount(cfarray);
242 list.reserve(arraySize);
243 for (CFIndex j = 0; j < arraySize; ++j)
244 list.append(qtValue(CFArrayGetValueAtIndex(cfarray, j)));
245 map.insert(key, list);
247 map.insert(key, qtValue(values[i]));
251 }
else if (typeId == CFDateGetTypeID()) {
252 return QDateTime::fromCFDate(
static_cast<CFDateRef>(cfvalue));
259 for (
int i = organization.size() - 1; i >= 0; --i) {
260 QChar ch = organization.at(i);
261 if (ch == u'.' || ch == QChar(0x3002) || ch == QChar(0xff0e)
262 || ch == QChar(0xff61)) {
263 QString suffix = organization.mid(i + 1).toLower();
264 if (suffix.size() == 2 || suffix ==
"com"_L1 || suffix ==
"org"_L1
265 || suffix ==
"net"_L1 || suffix ==
"edu"_L1 || suffix ==
"gov"_L1
266 || suffix ==
"mil"_L1 || suffix ==
"biz"_L1 || suffix ==
"info"_L1
267 || suffix ==
"name"_L1 || suffix ==
"pro"_L1 || suffix ==
"aero"_L1
268 || suffix ==
"coop"_L1 || suffix ==
"museum"_L1) {
270 result.replace(u'/', u' ');
275 int uc = ch.unicode();
276 if ((uc <
'a' || uc >
'z') && (uc <
'A' || uc >
'Z'))
281 for (
int i = 0; i < organization.size(); ++i) {
282 QChar ch = organization.at(i);
283 int uc = ch.unicode();
284 if ((uc >=
'a' && uc <=
'z') || (uc >=
'0' && uc <=
'9')) {
286 }
else if (uc >=
'A' && uc <=
'Z') {
287 domain += ch.toLower();
292 domain = domain.simplified();
293 domain.replace(u' ', u'-');
294 if (!domain.isEmpty())
295 domain.append(
".com"_L1);
306 void remove(
const QString &key)
override;
307 void set(
const QString &key,
const QVariant &value)
override;
319 CFStringRef userName;
320 CFStringRef applicationOrSuiteId;
323 QCFString applicationId;
326 SearchDomain domains[6];
331 const QString &application)
332 : QSettingsPrivate(QSettings::NativeFormat, scope, organization, application)
339 QString domainName = comify(organization);
341 if (domainName.isEmpty()) {
342 CFBundleRef main_bundle = CFBundleGetMainBundle();
343 if (main_bundle != NULL) {
344 CFStringRef main_bundle_identifier = CFBundleGetIdentifier(main_bundle);
345 if (main_bundle_identifier != NULL) {
346 QString bundle_identifier(qtKey(main_bundle_identifier));
348 QStringList bundle_identifier_components = bundle_identifier.split(u'/');
350 QStringList bundle_identifier_components_reversed;
351 for (
int i=0; i<bundle_identifier_components.size(); ++i) {
352 const QString &bundle_identifier_component = bundle_identifier_components.at(i);
353 bundle_identifier_components_reversed.push_front(bundle_identifier_component);
355 domainName = bundle_identifier_components_reversed.join(u'.');
360 if (domainName.isEmpty())
361 domainName =
"unknown-organization.trolltech.com"_L1;
363 while ((nextDot = domainName.indexOf(u'.', curPos)) != -1) {
364 javaPackageName.prepend(QStringView{domainName}.mid(curPos, nextDot - curPos));
365 javaPackageName.prepend(u'.');
366 curPos = nextDot + 1;
368 javaPackageName.prepend(QStringView{domainName}.mid(curPos));
369 javaPackageName = std::move(javaPackageName).toLower();
371 javaPackageName.prepend(
"com."_L1);
372 suiteId = javaPackageName;
374 if (!application.isEmpty()) {
375 javaPackageName += u'.' + application;
376 applicationId = javaPackageName;
380 for (
int i = (scope == QSettings::SystemScope) ? 1 : 0; i < 2; ++i) {
381 for (
int j = (application.isEmpty()) ? 1 : 0; j < 3; ++j) {
382 SearchDomain &domain = domains[numDomains++];
383 domain.userName = (i == 0) ? kCFPreferencesCurrentUser : kCFPreferencesAnyUser;
385 domain.applicationOrSuiteId = applicationId;
387 domain.applicationOrSuiteId = suiteId;
389 domain.applicationOrSuiteId = kCFPreferencesAnyApplication;
393 hostName = (scope == QSettings::SystemScope) ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
403 QStringList keys = children(key + u'/', AllKeys);
406 for (
int i = -1; i < keys.size(); ++i) {
410 subKey += keys.at(i);
412 CFPreferencesSetValue(macKey(subKey), 0, domains[0].applicationOrSuiteId,
413 domains[0].userName, hostName);
419 CFPreferencesSetValue(macKey(key), macValue(value), domains[0].applicationOrSuiteId,
420 domains[0].userName, hostName);
425 QCFString k = macKey(key);
426 for (
int i = 0; i < numDomains; ++i) {
428 QCFType<CFPropertyListRef> ret =
429 CFPreferencesCopyValue(k, domains[i].applicationOrSuiteId, domains[i].userName,
444 int startPos = prefix.size();
446 for (
int i = 0; i < numDomains; ++i) {
448 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[i].applicationOrSuiteId,
452 CFIndex size = CFArrayGetCount(cfarray);
453 for (CFIndex k = 0; k < size; ++k) {
455 qtKey(
static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfarray, k)));
456 if (currentKey.startsWith(prefix))
457 processChild(QStringView{currentKey}.mid(startPos), spec, result);
465 std::sort(result.begin(), result.end());
466 result.erase(
std::unique(result.begin(), result.end()),
473 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[0].applicationOrSuiteId,
474 domains[0].userName, hostName);
475 CFPreferencesSetMultiple(0, cfarray, domains[0].applicationOrSuiteId, domains[0].userName,
481 for (
int i = 0; i < numDomains; ++i) {
483 Boolean ok = CFPreferencesSynchronize(domains[i].applicationOrSuiteId,
484 domains[i].userName, hostNames[j]);
486 if (!ok && i == 0 && hostNames[j] == hostName && status == QSettings::NoError) {
487 setStatus(QSettings::AccessError);
501 QString impossibleKey(
"qt_internal/"_L1);
503 QSettings::Status oldStatus = that->status;
504 that->status = QSettings::NoError;
506 that->set(impossibleKey, QVariant());
508 bool writable = (status == QSettings::NoError) && that->get(impossibleKey).has_value();
509 that->remove(impossibleKey);
512 that->status = oldStatus;
519 if (scope == QSettings::UserScope)
520 result = QDir::homePath();
521 result +=
"/Library/Preferences/"_L1;
522 result += QString::fromCFString(domains[0].applicationOrSuiteId);
523 result +=
".plist"_L1;
527QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format,
528 QSettings::Scope scope,
529 const QString &organization,
530 const QString &application)
532#ifndef QT_BOOTSTRAPPED
533 if (organization ==
"Qt"_L1)
535 QString organizationDomain = QCoreApplication::organizationDomain();
536 QString applicationName = QCoreApplication::applicationName();
538 QSettingsPrivate *newSettings;
539 if (format == QSettings::NativeFormat) {
540 newSettings =
new QMacSettingsPrivate(scope, organizationDomain, applicationName);
542 newSettings =
new QConfFileSettingsPrivate(format, scope, organizationDomain, applicationName);
545 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(organization)));
546 if (!application.isEmpty())
547 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(application)));
552 if (format == QSettings::NativeFormat) {
553 return new QMacSettingsPrivate(scope, organization, application);
555 return new QConfFileSettingsPrivate(format, scope, organization, application);
561 QCFType<CFDataRef> cfData = data.toRawCFData();
562 QCFType<CFPropertyListRef> propertyList =
563 CFPropertyListCreateWithData(kCFAllocatorDefault, cfData, kCFPropertyListImmutable,
nullptr,
nullptr);
567 if (CFGetTypeID(propertyList) != CFDictionaryGetTypeID())
570 CFDictionaryRef cfdict =
571 static_cast<CFDictionaryRef>(
static_cast<CFPropertyListRef>(propertyList));
572 int size = (
int)CFDictionaryGetCount(cfdict);
573 QVarLengthArray<CFPropertyListRef> keys(size);
574 QVarLengthArray<CFPropertyListRef> values(size);
575 CFDictionaryGetKeysAndValues(cfdict, keys.data(), values.data());
577 for (
int i = 0; i < size; ++i) {
578 QString key = qtKey(
static_cast<CFStringRef>(keys[i]));
579 map->insert(QSettingsKey(key, Qt::CaseSensitive), qtValue(values[i]));
586 QVarLengthArray<QCFType<CFStringRef> > cfkeys(map.size());
587 QVarLengthArray<QCFType<CFPropertyListRef> > cfvalues(map.size());
589 ParsedSettingsMap::const_iterator j;
590 for (j = map.constBegin(); j != map.constEnd(); ++j) {
591 cfkeys[i] = macKey(j.key());
592 cfvalues[i] = macValue(j.value());
596 QCFType<CFDictionaryRef> propertyList =
597 CFDictionaryCreate(kCFAllocatorDefault,
598 reinterpret_cast<
const void **>(cfkeys.data()),
599 reinterpret_cast<
const void **>(cfvalues.data()),
601 &kCFTypeDictionaryKeyCallBacks,
602 &kCFTypeDictionaryValueCallBacks);
604 QCFType<CFDataRef> xmlData = CFPropertyListCreateData(
605 kCFAllocatorDefault, propertyList, kCFPropertyListXMLFormat_v1_0, 0, 0);
607 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
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