2017-02-05 22:41:43 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_intl_IntlOSPreferences_h__
|
|
|
|
#define mozilla_intl_IntlOSPreferences_h__
|
|
|
|
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2021-03-10 13:47:47 +03:00
|
|
|
#include "nsTHashMap.h"
|
2017-02-05 22:41:43 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2017-02-09 04:17:51 +03:00
|
|
|
#include "mozIOSPreferences.h"
|
|
|
|
|
2017-02-05 22:41:43 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace intl {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OSPreferences API provides a set of methods for retrieving information from
|
|
|
|
* the host environment on topics such as:
|
|
|
|
* - Internationalization
|
|
|
|
* - Localization
|
|
|
|
* - Regional preferences
|
|
|
|
*
|
|
|
|
* The API is meant to remain as simple as possible, relaying information from
|
|
|
|
* the host environment to the user without too much logic.
|
|
|
|
*
|
|
|
|
* Saying that, there are two exceptions to that paradigm.
|
|
|
|
*
|
|
|
|
* First one is normalization. We do intend to translate host environment
|
|
|
|
* concepts to unified Intl/L10n vocabulary used by Mozilla.
|
|
|
|
* That means that we will format locale IDs, timezone names, currencies etc.
|
|
|
|
* into a chosen format.
|
|
|
|
*
|
|
|
|
* Second is caching. This API does cache values and where possible will
|
|
|
|
* hook into the environment for some event-driven cache invalidation.
|
|
|
|
*
|
|
|
|
* This means that on platforms that do not support a mechanism to
|
|
|
|
* notify apps about changes, new OS-level settings may not be reflected
|
|
|
|
* in the app until it is relaunched.
|
|
|
|
*/
|
2017-02-09 04:17:51 +03:00
|
|
|
class OSPreferences : public mozIOSPreferences {
|
2017-02-05 22:41:43 +03:00
|
|
|
public:
|
2021-05-14 20:45:16 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2017-02-09 04:17:51 +03:00
|
|
|
NS_DECL_MOZIOSPREFERENCES
|
|
|
|
|
|
|
|
enum class DateTimeFormatStyle {
|
|
|
|
Invalid = -1,
|
|
|
|
None,
|
|
|
|
Short, // e.g. time: HH:mm, date: Y/m/d
|
|
|
|
Medium, // likely same as Short
|
|
|
|
Long, // e.g. time: including seconds, date: including weekday
|
|
|
|
Full // e.g. time: with timezone, date: with long weekday, month
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:03:30 +03:00
|
|
|
/**
|
|
|
|
* Constructor, to do any necessary initialization such as registering for
|
|
|
|
* notifications from the system when prefs are modified.
|
|
|
|
*/
|
|
|
|
OSPreferences();
|
|
|
|
|
2017-02-09 04:17:51 +03:00
|
|
|
/**
|
|
|
|
* Create (if necessary) and return a raw pointer to the singleton instance.
|
|
|
|
* Use this accessor in C++ code that just wants to call a method on the
|
|
|
|
* instance, but does not need to hold a reference, as in
|
|
|
|
* nsAutoCString str;
|
|
|
|
* OSPreferences::GetInstance()->GetSystemLocale(str);
|
2020-11-07 20:29:48 +03:00
|
|
|
*
|
|
|
|
* NOTE that this is not safe for off-main-thread use, because it is possible
|
|
|
|
* that XPCOM shutdown on the main thread could invalidate it at any moment!
|
2017-02-09 04:17:51 +03:00
|
|
|
*/
|
2017-02-05 22:41:43 +03:00
|
|
|
static OSPreferences* GetInstance();
|
|
|
|
|
2017-02-09 04:17:51 +03:00
|
|
|
/**
|
|
|
|
* Return an addRef'd pointer to the singleton instance. This is used by the
|
|
|
|
* XPCOM constructor that exists to support usage from JS.
|
|
|
|
*/
|
2020-11-07 20:29:48 +03:00
|
|
|
static already_AddRefed<OSPreferences> GetInstanceAddRefed();
|
2017-02-09 04:17:51 +03:00
|
|
|
|
2020-10-27 23:45:19 +03:00
|
|
|
static bool GetPatternForSkeleton(const nsACString& aSkeleton,
|
2020-10-27 23:45:17 +03:00
|
|
|
const nsACString& aLocale,
|
2020-10-27 23:45:19 +03:00
|
|
|
nsACString& aRetVal);
|
2020-10-27 23:45:17 +03:00
|
|
|
|
2017-04-13 18:17:40 +03:00
|
|
|
static bool GetDateTimeConnectorPattern(const nsACString& aLocale,
|
2020-10-27 23:45:19 +03:00
|
|
|
nsACString& aRetVal);
|
2017-04-13 18:17:40 +03:00
|
|
|
|
2017-09-08 00:53:36 +03:00
|
|
|
/**
|
|
|
|
* Triggers a refresh of retrieving data from host environment.
|
|
|
|
*
|
|
|
|
* If the result differs from the previous list, it will additionally
|
|
|
|
* trigger global events for changed values:
|
|
|
|
*
|
|
|
|
* * SystemLocales: "intl:system-locales-changed"
|
|
|
|
*
|
|
|
|
* This method should not be called from anywhere except of per-platform
|
|
|
|
* hooks into OS events.
|
|
|
|
*/
|
|
|
|
void Refresh();
|
|
|
|
|
2017-02-05 22:41:43 +03:00
|
|
|
protected:
|
|
|
|
nsTArray<nsCString> mSystemLocales;
|
2017-07-13 20:40:40 +03:00
|
|
|
nsTArray<nsCString> mRegionalPrefsLocales;
|
2017-02-05 22:41:43 +03:00
|
|
|
|
2018-12-18 14:42:00 +03:00
|
|
|
const size_t kMaxCachedPatterns = 15;
|
2021-03-10 13:47:47 +03:00
|
|
|
nsTHashMap<nsCStringHashKey, nsCString> mPatternCache;
|
2018-12-18 14:42:00 +03:00
|
|
|
|
2017-02-05 22:41:43 +03:00
|
|
|
private:
|
2017-11-21 14:03:30 +03:00
|
|
|
virtual ~OSPreferences();
|
2017-02-09 04:17:51 +03:00
|
|
|
|
|
|
|
static StaticRefPtr<OSPreferences> sInstance;
|
2017-02-05 22:41:43 +03:00
|
|
|
|
|
|
|
static bool CanonicalizeLanguageTag(nsCString& aLoc);
|
|
|
|
|
2017-02-09 04:17:51 +03:00
|
|
|
/**
|
|
|
|
* Helper methods to get formats from ICU; these will return false
|
|
|
|
* in case of error, in which case the caller cannot rely on aRetVal.
|
|
|
|
*/
|
|
|
|
bool GetDateTimePatternForStyle(DateTimeFormatStyle aDateStyle,
|
|
|
|
DateTimeFormatStyle aTimeStyle,
|
|
|
|
const nsACString& aLocale,
|
2020-10-27 23:45:19 +03:00
|
|
|
nsACString& aRetVal);
|
2017-02-09 04:17:51 +03:00
|
|
|
|
|
|
|
bool GetDateTimeSkeletonForStyle(DateTimeFormatStyle aDateStyle,
|
|
|
|
DateTimeFormatStyle aTimeStyle,
|
|
|
|
const nsACString& aLocale,
|
2020-10-27 23:45:19 +03:00
|
|
|
nsACString& aRetVal);
|
2017-02-09 04:17:51 +03:00
|
|
|
|
2020-10-27 23:41:58 +03:00
|
|
|
bool OverrideDateTimePattern(DateTimeFormatStyle aDateStyle,
|
|
|
|
DateTimeFormatStyle aTimeStyle,
|
|
|
|
const nsACString& aLocale, nsACString& aRetVal);
|
|
|
|
|
2017-02-05 22:41:43 +03:00
|
|
|
/**
|
|
|
|
* This is a host environment specific method that will be implemented
|
|
|
|
* separately for each platform.
|
|
|
|
*
|
|
|
|
* It is only called when the cache is empty or invalidated.
|
|
|
|
*
|
|
|
|
* The return value indicates whether the function successfully
|
|
|
|
* resolved at least one locale.
|
|
|
|
*/
|
|
|
|
bool ReadSystemLocales(nsTArray<nsCString>& aRetVal);
|
2017-02-09 04:17:51 +03:00
|
|
|
|
2017-07-13 20:40:40 +03:00
|
|
|
bool ReadRegionalPrefsLocales(nsTArray<nsCString>& aRetVal);
|
|
|
|
|
2017-02-09 04:17:51 +03:00
|
|
|
/**
|
|
|
|
* This is a host environment specific method that will be implemented
|
|
|
|
* separately for each platform.
|
|
|
|
*
|
|
|
|
* It is `best-effort` kind of API that attempts to construct the best
|
|
|
|
* possible date/time pattern for the given styles and locales.
|
|
|
|
*
|
|
|
|
* In case we fail to, or don't know how to retrieve the pattern in a
|
|
|
|
* given environment this function will return false.
|
|
|
|
* Callers should always be prepared to handle that scenario.
|
|
|
|
*
|
|
|
|
* The heuristic may depend on the OS API and HIG guidelines.
|
|
|
|
*/
|
|
|
|
bool ReadDateTimePattern(DateTimeFormatStyle aDateFormatStyle,
|
|
|
|
DateTimeFormatStyle aTimeFormatStyle,
|
2020-10-27 23:45:19 +03:00
|
|
|
const nsACString& aLocale, nsACString& aRetVal);
|
2020-10-27 23:41:58 +03:00
|
|
|
|
2021-01-15 22:28:12 +03:00
|
|
|
/**
|
|
|
|
* This is called to override the hour cycle in the skeleton based upon
|
|
|
|
* the OS preference for AM/PM or 24 hour display.
|
|
|
|
*/
|
|
|
|
void OverrideSkeletonHourCycle(bool aIs24Hour, nsAutoCString& aSkeleton);
|
|
|
|
|
2020-10-27 23:41:58 +03:00
|
|
|
/**
|
|
|
|
* This is called by the destructor to clean up any OS specific observers
|
|
|
|
* that are registered.
|
|
|
|
*/
|
|
|
|
void RemoveObservers();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called by the destructor to clean up any OS specific observers
|
|
|
|
* that are registered.
|
|
|
|
*/
|
|
|
|
static void PreferenceChanged(const char* aPrefName, void* /* aClosure */);
|
2017-02-05 22:41:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace intl
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_intl_IntlOSPreferences_h__ */
|