Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsettings_mac.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsettings.h"
5
6#include "qsettings_p.h"
7#include "qdatetime.h"
8#include "qdir.h"
10#include "private/qcore_mac_p.h"
11#ifndef QT_NO_QOBJECT
13#endif // QT_NO_QOBJECT
14
16
17using namespace Qt::StringLiterals;
18
19static const CFStringRef hostNames[2] = { kCFPreferencesCurrentHost, kCFPreferencesAnyHost };
20static const int numHostNames = 2;
21
22/*
23 On the Mac, it is more natural to use '.' as the key separator
24 than '/'. Therefore, it makes sense to replace '/' with '.' in
25 keys. Then we replace '.' with middle dots (which we can't show
26 here) and middle dots with '/'. A key like "4.0/BrowserCommand"
27 becomes "4<middot>0.BrowserCommand".
28*/
29
30enum RotateShift { Macify = 1, Qtify = 2 };
31
32static QString rotateSlashesDotsAndMiddots(const QString &key, int shift)
33{
34 static const int NumKnights = 3;
35 static const char knightsOfTheRoundTable[NumKnights] = { '/', '.', '\xb7' };
36 QString result = key;
37
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();
42 break;
43 }
44 }
45 }
46 return result;
47}
48
49static QCFType<CFStringRef> macKey(const QString &key)
50{
51 return rotateSlashesDotsAndMiddots(key, Macify).toCFString();
52}
53
54static QString qtKey(CFStringRef cfkey)
55{
56 return rotateSlashesDotsAndMiddots(QString::fromCFString(cfkey), Qtify);
57}
58
59static QCFType<CFPropertyListRef> macValue(const QVariant &value);
60
61static CFArrayRef macList(const QList<QVariant> &list)
62{
63 int n = list.size();
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);
69}
70
71static QCFType<CFPropertyListRef> macValue(const QVariant &value)
72{
73 CFPropertyListRef result = 0;
74
75 switch (value.metaType().id()) {
76 case QMetaType::QByteArray:
77 {
78 QByteArray ba = value.toByteArray();
79 result = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(ba.data()),
80 CFIndex(ba.size()));
81 }
82 break;
83 // should be same as below (look for LIST)
84 case QMetaType::QVariantList:
85 case QMetaType::QStringList:
86 case QMetaType::QPolygon:
87 result = macList(value.toList());
88 break;
89 case QMetaType::QVariantMap:
90 {
91 const QVariantMap &map = value.toMap();
92 const int mapSize = map.size();
93
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(); });
99
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); });
105
106 result = CFDictionaryCreate(kCFAllocatorDefault,
107 reinterpret_cast<const void **>(cfkeys.data()),
108 reinterpret_cast<const void **>(cfvalues.data()),
109 CFIndex(mapSize),
110 &kCFTypeDictionaryKeyCallBacks,
111 &kCFTypeDictionaryValueCallBacks);
112 }
113 break;
114 case QMetaType::QDateTime:
115 {
116 QDateTime dateTime = value.toDateTime();
117 // CFDate, unlike QDateTime, doesn't store timezone information
118 if (dateTime.timeSpec() == Qt::LocalTime)
119 result = dateTime.toCFDate();
120 else
121 goto string_case;
122 }
123 break;
124 case QMetaType::Bool:
125 result = value.toBool() ? kCFBooleanTrue : kCFBooleanFalse;
126 break;
127 case QMetaType::Int:
128 case QMetaType::UInt:
129 {
130 int n = value.toInt();
131 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n);
132 }
133 break;
134 case QMetaType::Double:
135 {
136 double n = value.toDouble();
137 result = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &n);
138 }
139 break;
140 case QMetaType::LongLong:
141 case QMetaType::ULongLong:
142 {
143 qint64 n = value.toLongLong();
144 result = CFNumberCreate(0, kCFNumberLongLongType, &n);
145 }
146 break;
147 case QMetaType::QString:
148 string_case:
149 default:
150 QString string = QSettingsPrivate::variantToString(value);
151 if (string.contains(QChar::Null))
152 result = std::move(string).toUtf8().toCFData();
153 else
154 result = string.toCFString();
155 }
156 return result;
157}
158
159static QVariant qtValue(CFPropertyListRef cfvalue)
160{
161 if (!cfvalue)
162 return QVariant();
163
164 CFTypeID typeId = CFGetTypeID(cfvalue);
165
166 /*
167 Sorted grossly from most to least frequent type.
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)) {
174 double d;
175 CFNumberGetValue(cfnumber, kCFNumberDoubleType, &d);
176 return d;
177 } else {
178 int i;
179 qint64 ll;
180
181 if (CFNumberGetType(cfnumber) == kCFNumberIntType) {
182 CFNumberGetValue(cfnumber, kCFNumberIntType, &i);
183 return i;
184 }
185 CFNumberGetValue(cfnumber, kCFNumberLongLongType, &ll);
186 return ll;
187 }
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)
196 metNonString = true;
197 list << value;
198 }
199 if (metNonString)
200 return list;
201 else
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));
207
208 // Fast-path for QByteArray, so that we don't have to go
209 // though the expensive and lossy conversion via UTF-8.
210 if (!byteArray.startsWith('@')) {
211 byteArray.detach();
212 return byteArray;
213 }
214
215 const QString str = QString::fromUtf8(byteArray.constData(), byteArray.size());
216 QVariant variant = QSettingsPrivate::stringToVariant(str);
217 if (variant == QVariant(str)) {
218 // We did not find an encoded variant in the string,
219 // so return the raw byte array instead.
220 byteArray.detach();
221 return byteArray;
222 }
223
224 return variant;
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());
232
233 QVariantMap map;
234 for (int i = 0; i < size; ++i) {
235 QString key = QString::fromCFString(static_cast<CFStringRef>(keys[i]));
236
237 if (CFGetTypeID(values[i]) == arrayTypeId) {
238 CFArrayRef cfarray = static_cast<CFArrayRef>(values[i]);
239 CFIndex arraySize = CFArrayGetCount(cfarray);
240 QVariantList list;
241 list.reserve(arraySize);
242 for (CFIndex j = 0; j < arraySize; ++j)
243 list.append(qtValue(CFArrayGetValueAtIndex(cfarray, j)));
244 map.insert(key, list);
245 } else {
246 map.insert(key, qtValue(values[i]));
247 }
248 }
249 return map;
250 } else if (typeId == CFDateGetTypeID()) {
251 return QDateTime::fromCFDate(static_cast<CFDateRef>(cfvalue));
252 }
253 return QVariant();
254}
255
256static QString comify(const QString &organization)
257{
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) {
268 QString result = organization;
269 result.replace(u'/', u' ');
270 return result;
271 }
272 break;
273 }
274 int uc = ch.unicode();
275 if ((uc < 'a' || uc > 'z') && (uc < 'A' || uc > 'Z'))
276 break;
277 }
278
279 QString domain;
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')) {
284 domain += ch;
285 } else if (uc >= 'A' && uc <= 'Z') {
286 domain += ch.toLower();
287 } else {
288 domain += u' ';
289 }
290 }
291 domain = domain.simplified();
292 domain.replace(u' ', u'-');
293 if (!domain.isEmpty())
294 domain.append(".com"_L1);
295 return domain;
296}
297
299{
300public:
302 const QString &application);
304
305 void remove(const QString &key) override;
306 void set(const QString &key, const QVariant &value) override;
307 std::optional<QVariant> get(const QString &key) const override;
308 QStringList children(const QString &prefix, ChildSpec spec) const override;
312 bool isWritable() const override;
313 QString fileName() const override;
314
315private:
316 struct SearchDomain
317 {
318 CFStringRef userName;
319 CFStringRef applicationOrSuiteId;
320 };
321
322 QCFString applicationId;
323 QCFString suiteId;
324 QCFString hostName;
325 SearchDomain domains[6];
326 int numDomains;
327};
328
329QMacSettingsPrivate::QMacSettingsPrivate(QSettings::Scope scope, const QString &organization,
330 const QString &application)
331 : QSettingsPrivate(QSettings::NativeFormat, scope, organization, application)
332{
333 QString javaPackageName;
334 int curPos = 0;
335 int nextDot;
336
337 // attempt to use the organization parameter
338 QString domainName = comify(organization);
339 // if not found, attempt to use the bundle identifier.
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));
346 // CFBundleGetIdentifier returns identifier separated by slashes rather than periods.
347 QStringList bundle_identifier_components = bundle_identifier.split(u'/');
348 // pre-reverse them so that when they get reversed again below, they are in the com.company.product format.
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);
353 }
354 domainName = bundle_identifier_components_reversed.join(u'.');
355 }
356 }
357 }
358 // if no bundle identifier yet. use a hard coded string.
359 if (domainName.isEmpty())
360 domainName = "unknown-organization.trolltech.com"_L1;
361
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;
366 }
367 javaPackageName.prepend(QStringView{domainName}.mid(curPos));
368 javaPackageName = std::move(javaPackageName).toLower();
369 if (curPos == 0)
370 javaPackageName.prepend("com."_L1);
371 suiteId = javaPackageName;
372
373 if (!application.isEmpty()) {
374 javaPackageName += u'.' + application;
375 applicationId = javaPackageName;
376 }
377
378 numDomains = 0;
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;
383 if (j == 0)
384 domain.applicationOrSuiteId = applicationId;
385 else if (j == 1)
386 domain.applicationOrSuiteId = suiteId;
387 else
388 domain.applicationOrSuiteId = kCFPreferencesAnyApplication;
389 }
390 }
391
392 hostName = (scope == QSettings::SystemScope) ? kCFPreferencesCurrentHost : kCFPreferencesAnyHost;
393 sync();
394}
395
399
400void QMacSettingsPrivate::remove(const QString &key)
401{
402 QStringList keys = children(key + u'/', AllKeys);
403
404 // If i == -1, then delete "key" itself.
405 for (int i = -1; i < keys.size(); ++i) {
406 QString subKey = key;
407 if (i >= 0) {
408 subKey += u'/';
409 subKey += keys.at(i);
410 }
411 CFPreferencesSetValue(macKey(subKey), 0, domains[0].applicationOrSuiteId,
412 domains[0].userName, hostName);
413 }
414}
415
416void QMacSettingsPrivate::set(const QString &key, const QVariant &value)
417{
418 CFPreferencesSetValue(macKey(key), macValue(value), domains[0].applicationOrSuiteId,
419 domains[0].userName, hostName);
420}
421
422std::optional<QVariant> QMacSettingsPrivate::get(const QString &key) const
423{
424 QCFString k = macKey(key);
425 for (int i = 0; i < numDomains; ++i) {
426 for (int j = 0; j < numHostNames; ++j) {
427 QCFType<CFPropertyListRef> ret =
428 CFPreferencesCopyValue(k, domains[i].applicationOrSuiteId, domains[i].userName,
429 hostNames[j]);
430 if (ret)
431 return qtValue(ret);
432 }
433
434 if (!fallbacks)
435 break;
436 }
437 return std::nullopt;
438}
439
440QStringList QMacSettingsPrivate::children(const QString &prefix, ChildSpec spec) const
441{
442 QStringList result;
443 int startPos = prefix.size();
444
445 for (int i = 0; i < numDomains; ++i) {
446 for (int j = 0; j < numHostNames; ++j) {
447 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[i].applicationOrSuiteId,
448 domains[i].userName,
449 hostNames[j]);
450 if (cfarray) {
451 CFIndex size = CFArrayGetCount(cfarray);
452 for (CFIndex k = 0; k < size; ++k) {
453 QString currentKey =
454 qtKey(static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfarray, k)));
455 if (currentKey.startsWith(prefix))
456 processChild(QStringView{currentKey}.mid(startPos), spec, result);
457 }
458 }
459 }
460
461 if (!fallbacks)
462 break;
463 }
464 std::sort(result.begin(), result.end());
465 result.erase(std::unique(result.begin(), result.end()),
466 result.end());
467 return result;
468}
469
471{
472 QCFType<CFArrayRef> cfarray = CFPreferencesCopyKeyList(domains[0].applicationOrSuiteId,
473 domains[0].userName, hostName);
474 CFPreferencesSetMultiple(0, cfarray, domains[0].applicationOrSuiteId, domains[0].userName,
475 hostName);
476}
477
479{
480 for (int i = 0; i < numDomains; ++i) {
481 for (int j = 0; j < numHostNames; ++j) {
482 Boolean ok = CFPreferencesSynchronize(domains[i].applicationOrSuiteId,
483 domains[i].userName, hostNames[j]);
484 // only report failures for the primary file (the one we write to)
485 if (!ok && i == 0 && hostNames[j] == hostName && status == QSettings::NoError) {
486 setStatus(QSettings::AccessError);
487 }
488 }
489 }
490}
491
493{
494 sync();
495}
496
498{
499 QMacSettingsPrivate *that = const_cast<QMacSettingsPrivate *>(this);
500 QString impossibleKey("qt_internal/"_L1);
501
502 QSettings::Status oldStatus = that->status;
503 that->status = QSettings::NoError;
504
505 that->set(impossibleKey, QVariant());
506 that->sync();
507 bool writable = (status == QSettings::NoError) && that->get(impossibleKey).has_value();
508 that->remove(impossibleKey);
509 that->sync();
510
511 that->status = oldStatus;
512 return writable;
513}
514
516{
517 QString result;
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;
523 return result;
524}
525
526QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format,
527 QSettings::Scope scope,
528 const QString &organization,
529 const QString &application)
530{
531#ifndef QT_BOOTSTRAPPED
532 if (organization == "Qt"_L1)
533 {
534 QString organizationDomain = QCoreApplication::organizationDomain();
535 QString applicationName = QCoreApplication::applicationName();
536
537 QSettingsPrivate *newSettings;
538 if (format == QSettings::NativeFormat) {
539 newSettings = new QMacSettingsPrivate(scope, organizationDomain, applicationName);
540 } else {
541 newSettings = new QConfFileSettingsPrivate(format, scope, organizationDomain, applicationName);
542 }
543
544 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(organization)));
545 if (!application.isEmpty())
546 newSettings->beginGroupOrArray(QSettingsGroup(normalizedKey(application)));
547
548 return newSettings;
549 }
550#endif
551 if (format == QSettings::NativeFormat) {
552 return new QMacSettingsPrivate(scope, organization, application);
553 } else {
554 return new QConfFileSettingsPrivate(format, scope, organization, application);
555 }
556}
557
558bool QConfFileSettingsPrivate::readPlistFile(const QByteArray &data, ParsedSettingsMap *map) const
559{
560 QCFType<CFDataRef> cfData = data.toRawCFData();
561 QCFType<CFPropertyListRef> propertyList =
562 CFPropertyListCreateWithData(kCFAllocatorDefault, cfData, kCFPropertyListImmutable, nullptr, nullptr);
563
564 if (!propertyList)
565 return true;
566 if (CFGetTypeID(propertyList) != CFDictionaryGetTypeID())
567 return false;
568
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());
575
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]));
579 }
580 return true;
581}
582
583bool QConfFileSettingsPrivate::writePlistFile(QIODevice &file, const ParsedSettingsMap &map) const
584{
585 QVarLengthArray<QCFType<CFStringRef> > cfkeys(map.size());
586 QVarLengthArray<QCFType<CFPropertyListRef> > cfvalues(map.size());
587 int i = 0;
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());
592 ++i;
593 }
594
595 QCFType<CFDictionaryRef> propertyList =
596 CFDictionaryCreate(kCFAllocatorDefault,
597 reinterpret_cast<const void **>(cfkeys.data()),
598 reinterpret_cast<const void **>(cfvalues.data()),
599 CFIndex(map.size()),
600 &kCFTypeDictionaryKeyCallBacks,
601 &kCFTypeDictionaryValueCallBacks);
602
603 QCFType<CFDataRef> xmlData = CFPropertyListCreateData(
604 kCFAllocatorDefault, propertyList, kCFPropertyListXMLFormat_v1_0, 0, 0);
605
606 return file.write(QByteArray::fromRawCFData(xmlData)) == CFDataGetLength(xmlData);
607}
608
609QT_END_NAMESPACE
std::optional< QVariant > get(const QString &key) const override
void clear() override
bool isWritable() const override
void remove(const QString &key) override
void sync() override
void flush() 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]
RotateShift
@ Qtify
@ Macify
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
Definition qsettings_p.h:78