QSettings: Remove calls to deprecated API on macOS

CFURLCreateDataAndPropertiesFromResource and
CFURLWriteDataAndPropertiestoResource have been
deprecated since 10.9. We replace them with simple
QFile access.

Code cleaning and included.

Change-Id: I19c7ceac41c8c511962f1128bd8e210e3adb434c
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
Reviewed-by: Jake Petroules <jake.petroules@qt.io>
This commit is contained in:
Gabriel de Dietrich 2016-06-29 16:20:31 -07:00 committed by Jake Petroules
parent 4c1c68b2ba
commit bc1b45ff9f
6 changed files with 58 additions and 74 deletions

6
configure vendored
View File

@ -1746,11 +1746,13 @@ if true; then ###[ '!' -f "$outpath/bin/qmake" ];
EXTRA_OBJS="$EXTRA_OBJS \
qsettings_mac.o \
qcore_mac.o \
qcore_mac_objc.o"
qcore_mac_objc.o \
qcore_foundation.o"
EXTRA_SRCS="$EXTRA_SRCS \
\"\$(SOURCE_PATH)/src/corelib/io/qsettings_mac.cpp\" \
\"\$(SOURCE_PATH)/src/corelib/kernel/qcore_mac.cpp\" \
\"\$(SOURCE_PATH)/src/corelib/kernel/qcore_mac_objc.mm\""
\"\$(SOURCE_PATH)/src/corelib/kernel/qcore_mac_objc.mm\" \
\"\$(SOURCE_PATH)/src/corelib/kernel/qcore_foundation.mm\""
fi
echo >>"$mkfile"

View File

@ -75,6 +75,7 @@ DEPEND_SRC = \
$(SOURCE_PATH)/src/corelib/tools/qlinkedlist.cpp \
$(SOURCE_PATH)/src/corelib/tools/qhash.cpp $(SOURCE_PATH)/src/corelib/kernel/qcore_mac.cpp \
$(SOURCE_PATH)/src/corelib/kernel/qcore_mac_objc.mm \
$(SOURCE_PATH)/src/corelib/kernel/qcore_foundation.mm \
$(SOURCE_PATH)/src/corelib/io/qtemporaryfile.cpp $(SOURCE_PATH)/src/corelib/kernel/qmetatype.cpp \
$(SOURCE_PATH)/src/corelib/io/qsettings.cpp $(SOURCE_PATH)/src/corelib/kernel/qvariant.cpp \
$(SOURCE_PATH)/src/corelib/global/qlibraryinfo.cpp $(SOURCE_PATH)/src/corelib/tools/qcryptographichash.cpp \
@ -274,6 +275,9 @@ qcore_mac.o: $(SOURCE_PATH)/src/corelib/kernel/qcore_mac.cpp
qcore_mac_objc.o: $(SOURCE_PATH)/src/corelib/kernel/qcore_mac_objc.mm
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/kernel/qcore_mac_objc.mm
qcore_foundation.o: $(SOURCE_PATH)/src/corelib/kernel/qcore_foundation.mm
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/kernel/qcore_foundation.mm
qutfcodec.o: $(SOURCE_PATH)/src/corelib/codecs/qutfcodec.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/codecs/qutfcodec.cpp

View File

@ -1355,7 +1355,6 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo)
{
QConfFile *confFile = confFiles[confFileNo].data();
bool readOnly = confFile->addedKeys.isEmpty() && confFile->removedKeys.isEmpty();
bool ok;
/*
We can often optimize the read-only case, if the file on disk
@ -1415,31 +1414,26 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo)
because they don't exist) are treated as empty files.
*/
if (file.isReadable() && fileInfo.size() != 0) {
bool ok = false;
#ifdef Q_OS_MAC
if (format == QSettings::NativeFormat) {
ok = readPlistFile(confFile->name, &confFile->originalKeys);
QByteArray data = file.readAll();
ok = readPlistFile(data, &confFile->originalKeys);
} else
#endif
{
if (format <= QSettings::IniFormat) {
QByteArray data = file.readAll();
ok = readIniFile(data, &confFile->unparsedIniSections);
} else {
if (readFunc) {
QSettings::SettingsMap tempNewKeys;
ok = readFunc(file, tempNewKeys);
if (format <= QSettings::IniFormat) {
QByteArray data = file.readAll();
ok = readIniFile(data, &confFile->unparsedIniSections);
} else if (readFunc) {
QSettings::SettingsMap tempNewKeys;
ok = readFunc(file, tempNewKeys);
if (ok) {
QSettings::SettingsMap::const_iterator i = tempNewKeys.constBegin();
while (i != tempNewKeys.constEnd()) {
confFile->originalKeys.insert(QSettingsKey(i.key(),
caseSensitivity),
i.value());
++i;
}
}
} else {
ok = false;
if (ok) {
QSettings::SettingsMap::const_iterator i = tempNewKeys.constBegin();
while (i != tempNewKeys.constEnd()) {
confFile->originalKeys.insert(QSettingsKey(i.key(), caseSensitivity),
i.value());
++i;
}
}
}
@ -1457,45 +1451,43 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo)
so everything is under control.
*/
if (!readOnly) {
bool ok = false;
ensureAllSectionsParsed(confFile);
ParsedSettingsMap mergedKeys = confFile->mergedKeyMap();
#ifndef QT_BOOTSTRAPPED
QSaveFile sf(confFile->name);
#else
QFile sf(confFile->name);
#endif
if (!sf.open(QIODevice::WriteOnly)) {
setStatus(QSettings::AccessError);
return;
}
#ifdef Q_OS_MAC
if (format == QSettings::NativeFormat) {
ok = writePlistFile(confFile->name, mergedKeys);
ok = writePlistFile(sf, mergedKeys);
} else
#endif
{
#ifndef QT_BOOTSTRAPPED
QSaveFile sf(confFile->name);
#else
QFile sf(confFile->name);
#endif
if (!sf.open(QIODevice::WriteOnly)) {
setStatus(QSettings::AccessError);
ok = false;
} else if (format <= QSettings::IniFormat) {
ok = writeIniFile(sf, mergedKeys);
} else {
if (writeFunc) {
QSettings::SettingsMap tempOriginalKeys;
if (format <= QSettings::IniFormat) {
ok = writeIniFile(sf, mergedKeys);
} else if (writeFunc) {
QSettings::SettingsMap tempOriginalKeys;
ParsedSettingsMap::const_iterator i = mergedKeys.constBegin();
while (i != mergedKeys.constEnd()) {
tempOriginalKeys.insert(i.key(), i.value());
++i;
}
ok = writeFunc(sf, tempOriginalKeys);
} else {
ok = false;
}
ParsedSettingsMap::const_iterator i = mergedKeys.constBegin();
while (i != mergedKeys.constEnd()) {
tempOriginalKeys.insert(i.key(), i.value());
++i;
}
#ifndef QT_BOOTSTRAPPED
if (ok)
ok = sf.commit();
#endif
ok = writeFunc(sf, tempOriginalKeys);
}
#ifndef QT_BOOTSTRAPPED
if (ok)
ok = sf.commit();
#endif
if (ok) {
confFile->unparsedIniSections.clear();
confFile->originalKeys = mergedKeys;

View File

@ -613,24 +613,11 @@ QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format,
}
}
static QCFType<CFURLRef> urlFromFileName(const QString &fileName)
bool QConfFileSettingsPrivate::readPlistFile(const QByteArray &data, ParsedSettingsMap *map) const
{
return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, QCFString(fileName),
kCFURLPOSIXPathStyle, false);
}
bool QConfFileSettingsPrivate::readPlistFile(const QString &fileName, ParsedSettingsMap *map) const
{
QCFType<CFDataRef> resource;
SInt32 code;
if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, urlFromFileName(fileName),
&resource, 0, 0, &code))
return false;
QCFString errorStr;
QCFType<CFDataRef> cfData = data.toRawCFData();
QCFType<CFPropertyListRef> propertyList =
CFPropertyListCreateFromXMLData(kCFAllocatorDefault, resource, kCFPropertyListImmutable,
&errorStr);
CFPropertyListCreateWithData(kCFAllocatorDefault, cfData, kCFPropertyListImmutable, Q_NULLPTR, Q_NULLPTR);
if (!propertyList)
return true;
@ -651,8 +638,7 @@ bool QConfFileSettingsPrivate::readPlistFile(const QString &fileName, ParsedSett
return true;
}
bool QConfFileSettingsPrivate::writePlistFile(const QString &fileName,
const ParsedSettingsMap &map) const
bool QConfFileSettingsPrivate::writePlistFile(QIODevice &file, const ParsedSettingsMap &map) const
{
QVarLengthArray<QCFType<CFStringRef> > cfkeys(map.size());
QVarLengthArray<QCFType<CFPropertyListRef> > cfvalues(map.size());
@ -675,8 +661,7 @@ bool QConfFileSettingsPrivate::writePlistFile(const QString &fileName,
QCFType<CFDataRef> xmlData = CFPropertyListCreateData(
kCFAllocatorDefault, propertyList, kCFPropertyListXMLFormat_v1_0, 0, 0);
SInt32 code;
return CFURLWriteDataAndPropertiesToResource(urlFromFileName(fileName), xmlData, 0, &code);
return file.write(QByteArray::fromRawCFData(xmlData)) == CFDataGetLength(xmlData);
}
QT_END_NAMESPACE

View File

@ -296,8 +296,8 @@ private:
void syncConfFile(int confFileNo);
bool writeIniFile(QIODevice &device, const ParsedSettingsMap &map);
#ifdef Q_OS_MAC
bool readPlistFile(const QString &fileName, ParsedSettingsMap *map) const;
bool writePlistFile(const QString &fileName, const ParsedSettingsMap &map) const;
bool readPlistFile(const QByteArray &data, ParsedSettingsMap *map) const;
bool writePlistFile(QIODevice &file, const ParsedSettingsMap &map) const;
#endif
void ensureAllSectionsParsed(QConfFile *confFile) const;
void ensureSectionParsed(QConfFile *confFile, const QSettingsKey &key) const;

View File

@ -119,7 +119,8 @@ mac {
../../corelib/kernel/qcoreapplication_mac.cpp \
../../corelib/kernel/qcore_mac.cpp
OBJECTIVE_SOURCES += \
../../corelib/kernel/qcore_mac_objc.mm
../../corelib/kernel/qcore_mac_objc.mm \
../../corelib/kernel/qcore_foundation.mm
LIBS += -framework Foundation
osx: LIBS_PRIVATE += -framework CoreServices