Results 1 to 2 of 2

Thread: MacOSX: editing hierarchical .plist files with QSettings

  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MacOSX: editing hierarchical .plist files with QSettings

    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:

    Qt Code:
    1. void MainWindow::_markExtensionSafe(const QString &ext)
    2. {
    3. qDebug() << "Marking extension" << ext << "as safe to open instead of download";
    4.  
    5. QString filename = QDesktopServices::storageLocation(QDesktopServices::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";
    6. QString key = "LSRiskCategorySafe/LSRiskCategoryExtensions";
    7.  
    8. QSettings plist(filename, QSettings::NativeFormat);
    9. QVariant currentSetting = plist.value(key);
    10. QStringList exts = currentSetting.toStringList();
    11.  
    12. if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
    13. {
    14. exts << ext;
    15. plist.setValue(key, exts);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    But it generates:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4. <dict>
    5. <key>LSRiskCategorySafe.LSRiskCategoryExtensions</key>
    6. <array>
    7. <string>myextension</string>
    8. </array>
    9. </dict>
    10. </plist>
    To copy to clipboard, switch view to plain text mode 

    While the format supposedly has to look like this:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
    3. "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    4. <plist version="1.0">
    5. <dict>
    6. <key>LSRiskCategorySafe</key>
    7. <dict>
    8. <key>LSRiskCategoryExtensions</key>
    9. <array>
    10. <string>myextension</string>
    11. </array>
    12. </dict>
    13. </dict>
    14. </plist>
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void MainWindow::_markExtensionSafe(const QString &ext)
    2. {
    3. qDebug() << "Marking extension" << ext << "as safe to open instead of download";
    4.  
    5. QString filename = QDesktopServices::storageLocation(QDesktopServices::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";
    6.  
    7. QString key = "LSRiskCategorySafe";
    8. QString subkey = "LSRiskCategoryExtensions";
    9.  
    10. QSettings plist(filename, QSettings::NativeFormat);
    11. QVariantMap map = plist.value(key).toMap();
    12. QStringList exts = map.value(subkey).toStringList();
    13.  
    14. if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
    15. {
    16. exts << ext;
    17. map[subkey] = exts;
    18. plist.setValue(key, map);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    But then it generates:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4. <dict>
    5. <key>LSRiskCategorySafe</key>
    6. <dict>
    7. <key>LSRiskCategoryExtensions</key>
    8. <array>
    9. <array>
    10. <string>myextension</string>
    11. </array>
    12. </array>
    13. </dict>
    14. </dict>
    15. </plist>
    To copy to clipboard, switch view to plain text mode 

    Don't understand why it generates <array> two times.
    Last edited by Max; 13th November 2010 at 23:01.

  2. #2
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: MacOSX: editing hierarchical .plist files with QSettings

    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 ?

Similar Threads

  1. Reading .plist files on Windows
    By alexivanov91 in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2010, 10:07
  2. QSettings to read .plist on Mac
    By lemmiwinks in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2010, 21:13
  3. Replies: 3
    Last Post: 8th December 2009, 03:29
  4. Replies: 3
    Last Post: 20th November 2009, 14:32
  5. How to edit .plist file in mac
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2008, 13:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.