From f36e242d95181ca77ee88f307e1e2d40f9729b2e Mon Sep 17 00:00:00 2001 From: "bnesse%netscape.com" Date: Thu, 12 Apr 2001 23:28:43 +0000 Subject: [PATCH] Fixes for handling default localized unichar prefs. [Not part of the build] --- modules/libpref/src/nsPrefBranch.cpp | 66 +++++++++++++++++++++++++--- modules/libpref/src/nsPrefBranch.h | 3 +- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/modules/libpref/src/nsPrefBranch.cpp b/modules/libpref/src/nsPrefBranch.cpp index 83dc8b384b5..ee504435034 100644 --- a/modules/libpref/src/nsPrefBranch.cpp +++ b/modules/libpref/src/nsPrefBranch.cpp @@ -31,6 +31,7 @@ #include "prefapi.h" #include "prmem.h" #include "nsScriptSecurityManager.h" +#include "nsIStringBundle.h" #include "nsIFileSpec.h" // this should be removed eventually #include "prefapi_private_data.h" @@ -48,6 +49,7 @@ struct PrefCallbackData { static NS_DEFINE_CID(kSecurityManagerCID, NS_SCRIPTSECURITYMANAGER_CID); +static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID); // Prototypes extern "C" PrefResult pref_UnlockPref(const char *key); @@ -293,17 +295,38 @@ NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & } if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) { + PRBool bNeedDefault; nsCOMPtr theString(do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv)); - if (NS_SUCCEEDED(rv)) { - rv = theString->SetData(NS_ConvertASCIItoUCS2(utf8String).get()); - if (NS_SUCCEEDED(rv)) { - nsIPrefLocalizedString *temp = theString; - - NS_ADDREF(temp); - *_retval = (void *)temp; + if (mIsDefault) { + bNeedDefault = PR_TRUE; + } else { + // if there is no user (or locked) value + if (!PREF_HasUserPref(pref) && !PREF_PrefIsLocked(pref)) { + bNeedDefault = PR_TRUE; } } + + // if we need to fetch the default value, do that instead, otherwise use the + // value we pulled in at the top of this function + if (bNeedDefault) { + nsXPIDLString utf16String; + rv = GetDefaultFromPropertiesFile(pref, getter_Copies(utf16String)); + if (NS_SUCCEEDED(rv)) { + rv = theString->SetData(utf16String.get()); + } + } else { + if (NS_SUCCEEDED(rv)) { + rv = theString->SetData(NS_ConvertASCIItoUCS2(utf8String).get()); + } + } + + if (NS_SUCCEEDED(rv)) { + nsIPrefLocalizedString *temp = theString; + + NS_ADDREF(temp); + *_retval = (void *)temp; + } return rv; } @@ -585,6 +608,35 @@ static int PR_CALLBACK NotifyObserver(const char *newpref, void *data) } +nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName, PRUnichar **return_buf) +{ + nsresult rv; + + // the default value contains a URL to a .properties file + + nsXPIDLCString propertyFileURL; + rv = _convertRes(PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), PR_TRUE)); + if (NS_FAILED(rv)) + return rv; + + nsCOMPtr bundleService = + do_GetService(kStringBundleServiceCID, &rv); + if (NS_FAILED(rv)) + return rv; + + nsCOMPtr bundle; + rv = bundleService->CreateBundle(propertyFileURL, nsnull, + getter_AddRefs(bundle)); + if (NS_FAILED(rv)) + return rv; + + // string names are in unicdoe + nsAutoString stringId; + stringId.AssignWithConversion(aPrefName); + + return bundle->GetStringFromName(stringId.GetUnicode(), return_buf); +} + const char *nsPrefBranch::getPrefName(const char *aPrefName) { // for speed, avoid strcpy if we can: diff --git a/modules/libpref/src/nsPrefBranch.h b/modules/libpref/src/nsPrefBranch.h index 2d5b90b4f17..b7caf005fd8 100644 --- a/modules/libpref/src/nsPrefBranch.h +++ b/modules/libpref/src/nsPrefBranch.h @@ -49,8 +49,9 @@ protected: nsPrefBranch() /* disallow use of this constructer */ { }; + nsresult GetDefaultFromPropertiesFile(const char *aPrefName, PRUnichar **return_buf); const char *getPrefName(const char *aPrefName); - nsresult QueryObserver(const char *aPrefName); + nsresult QueryObserver(const char *aPrefName); private: PRInt32 mPrefRootLength;