PDA

View Full Version : MacOSX: editing hierarchical .plist files with QSettings



Max
13th November 2010, 22:35
I would like to mark a file extension as "Safe to open from the Internet".
To do so I need to create a preferences file ~/Library/Preferences/com.apple.DownloadAssessment.plist and add the extension to that.

If the file already exists, I need to preserve its existing settings, so simply overwriting the file is not an option.
I tried to use QSettings to edit it like this:



void MainWindow::_markExtensionSafe(const QString &ext)
{
qDebug() << "Marking extension" << ext << "as safe to open instead of download";

QString filename = QDesktopServices::storageLocation(QDesktopServices ::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";
QString key = "LSRiskCategorySafe/LSRiskCategoryExtensions";

QSettings plist(filename, QSettings::NativeFormat);
QVariant currentSetting = plist.value(key);
QStringList exts = currentSetting.toStringList();

if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
plist.setValue(key, exts);
}
}


But it generates:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe.LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</plist>


While the format supposedly has to look like this:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</dict>
</plist>


Is there a way to tell QSettings to create proper subkeys?
Or am I better off editing the file using the XML functions instead?



Added after 1 20 minutes:

Also tried this:



void MainWindow::_markExtensionSafe(const QString &ext)
{
qDebug() << "Marking extension" << ext << "as safe to open instead of download";

QString filename = QDesktopServices::storageLocation(QDesktopServices ::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";

QString key = "LSRiskCategorySafe";
QString subkey = "LSRiskCategoryExtensions";

QSettings plist(filename, QSettings::NativeFormat);
QVariantMap map = plist.value(key).toMap();
QStringList exts = map.value(subkey).toStringList();

if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
map[subkey] = exts;
plist.setValue(key, map);
}
}

But then it generates:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<array>
<string>myextension</string>
</array>
</array>
</dict>
</dict>
</plist>


Don't understand why it generates <array> two times.

yurenjimi
20th March 2014, 10:36
Don't understand why it generates <array> two times.


Yes, i has the same problem with Qt5.2 and OS X 10.9..
Has you solved the problem ?