Skip to content

Commit

Permalink
macOS: Initialize standard NSUserDefaults as early as possible
Browse files Browse the repository at this point in the history
The standard user defaults are initialized from an ordered list of domains,
as documented by NSUserDefaults.standardUserDefaults. This includes e.g.
parsing command line arguments, such as -AppleLocale "fr_FR", as well as
global defaults. AppKit does this during [NSUserDefaults init], which in
turn initializes the locale returned by CFLocaleCopyCurrent() and
NSLocale.currentLocale.

If those functions are called before NSUserDefaults does its thing the
locale will already have been created, and the logic in NSUserDefaults
won't have any effect -- nor is there any way for us to set/override
the locale after this.

To ensure that the -AppleLocale command line override is available through
the lower level Core Foundation preferences APIs, we need to initialize
the user defaults as early as possible via the Foundation-API.

Change-Id: I906a5a8b05a7216e60020ec45f8da725b801d2c5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
  • Loading branch information
torarnv committed Feb 11, 2021
1 parent 9bc849f commit 62af655
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/corelib/kernel/qcore_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@

// --------------------------------------------------------------------------

static void initializeStandardUserDefaults()
{
// The standard user defaults are initialized from an ordered list of domains,
// as documented by NSUserDefaults.standardUserDefaults. This includes e.g.
// parsing command line arguments, such as -AppleFooBar "baz", as well as
// global defaults. To ensure that these defaults are available through
// the lower level Core Foundation preferences APIs, we need to initialize
// them as early as possible via the Foundation-API, as the lower level APIs
// do not do this initialization.
Q_UNUSED(NSUserDefaults.standardUserDefaults);
}
Q_CONSTRUCTOR_FUNCTION(initializeStandardUserDefaults);

// --------------------------------------------------------------------------

QCFString::operator QString() const
{
if (string.isEmpty() && value)
Expand Down

0 comments on commit 62af655

Please sign in to comment.