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