diff --git a/modules/libpref/public/Preferences.h b/modules/libpref/public/Preferences.h index a4ebea4647e..324636bc283 100644 --- a/modules/libpref/public/Preferences.h +++ b/modules/libpref/public/Preferences.h @@ -54,6 +54,8 @@ class nsIFile; class nsCString; class nsString; +class nsAdoptingString; +class nsAdoptingCString; namespace mozilla { @@ -126,6 +128,11 @@ public: return result; } + static nsAdoptingCString GetCString(const char* aPref); + static nsAdoptingString GetString(const char* aPref); + static nsAdoptingCString GetLocalizedCString(const char* aPref); + static nsAdoptingString GetLocalizedString(const char* aPref); + /** * Gets int or bool type pref value with raw return value of nsIPrefBranch. * @@ -152,9 +159,9 @@ public: * @param aResult Must not be NULL. The value is never modified when * these methods fail. */ - static nsresult GetChar(const char* aPref, nsACString* aResult); - static nsresult GetChar(const char* aPref, nsAString* aResult); - static nsresult GetLocalizedString(const char* aPref, nsACString* aResult); + static nsresult GetCString(const char* aPref, nsACString* aResult); + static nsresult GetString(const char* aPref, nsAString* aResult); + static nsresult GetLocalizedCString(const char* aPref, nsACString* aResult); static nsresult GetLocalizedString(const char* aPref, nsAString* aResult); /** @@ -166,10 +173,10 @@ public: { return SetInt(aPref, static_cast(aValue)); } - static nsresult SetChar(const char* aPref, const char* aValue); - static nsresult SetChar(const char* aPref, const nsACString &aValue); - static nsresult SetChar(const char* aPref, const PRUnichar* aValue); - static nsresult SetChar(const char* aPref, const nsAString &aValue); + static nsresult SetCString(const char* aPref, const char* aValue); + static nsresult SetCString(const char* aPref, const nsACString &aValue); + static nsresult SetString(const char* aPref, const PRUnichar* aValue); + static nsresult SetString(const char* aPref, const nsAString &aValue); /** * Clears user set pref. diff --git a/modules/libpref/src/Preferences.cpp b/modules/libpref/src/Preferences.cpp index f0330d2eb35..dd8b5e630c1 100644 --- a/modules/libpref/src/Preferences.cpp +++ b/modules/libpref/src/Preferences.cpp @@ -1019,13 +1019,31 @@ Preferences::GetInt(const char* aPref, PRInt32* aResult) return sPreferences->mRootBranch->GetIntPref(aPref, aResult); } +// static +nsAdoptingCString +Preferences::GetCString(const char* aPref) +{ + nsAdoptingCString result; + GetCString(aPref, &result); + return result; +} + +// static +nsAdoptingString +Preferences::GetString(const char* aPref) +{ + nsAdoptingString result; + GetString(aPref, &result); + return result; +} + // static nsresult -Preferences::GetChar(const char* aPref, nsACString* aResult) +Preferences::GetCString(const char* aPref, nsACString* aResult) { NS_PRECONDITION(aResult, "aResult must not be NULL"); NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE); - nsAdoptingCString result; + nsCAutoString result; nsresult rv = sPreferences->mRootBranch->GetCharPref(aPref, getter_Copies(result)); if (NS_SUCCEEDED(rv)) { @@ -1036,11 +1054,11 @@ Preferences::GetChar(const char* aPref, nsACString* aResult) // static nsresult -Preferences::GetChar(const char* aPref, nsAString* aResult) +Preferences::GetString(const char* aPref, nsAString* aResult) { NS_PRECONDITION(aResult, "aResult must not be NULL"); NS_ENSURE_TRUE(InitStaticMembers(), NS_ERROR_NOT_AVAILABLE); - nsAdoptingCString result; + nsCAutoString result; nsresult rv = sPreferences->mRootBranch->GetCharPref(aPref, getter_Copies(result)); if (NS_SUCCEEDED(rv)) { @@ -1049,9 +1067,27 @@ Preferences::GetChar(const char* aPref, nsAString* aResult) return rv; } +// static +nsAdoptingCString +Preferences::GetLocalizedCString(const char* aPref) +{ + nsAdoptingCString result; + GetLocalizedCString(aPref, &result); + return result; +} + +// static +nsAdoptingString +Preferences::GetLocalizedString(const char* aPref) +{ + nsAdoptingString result; + GetLocalizedString(aPref, &result); + return result; +} + // static nsresult -Preferences::GetLocalizedString(const char* aPref, nsACString* aResult) +Preferences::GetLocalizedCString(const char* aPref, nsACString* aResult) { NS_PRECONDITION(aResult, "aResult must not be NULL"); nsAutoString result; @@ -1073,18 +1109,15 @@ Preferences::GetLocalizedString(const char* aPref, nsAString* aResult) NS_GET_IID(nsIPrefLocalizedString), getter_AddRefs(prefLocalString)); if (NS_SUCCEEDED(rv)) { - if (prefLocalString) { - prefLocalString->GetData(getter_Copies(*aResult)); - } else { - aResult->Truncate(); - } + NS_ASSERTION(prefLocalString, "Succeeded but the result is NULL"); + prefLocalString->GetData(getter_Copies(*aResult)); } return rv; } // static nsresult -Preferences::SetChar(const char* aPref, const char* aValue) +Preferences::SetCString(const char* aPref, const char* aValue) { NS_ENSURE_TRUE(InitStaticMembers(), PR_FALSE); return sPreferences->mRootBranch->SetCharPref(aPref, aValue); @@ -1092,25 +1125,25 @@ Preferences::SetChar(const char* aPref, const char* aValue) // static nsresult -Preferences::SetChar(const char* aPref, const nsACString &aValue) +Preferences::SetCString(const char* aPref, const nsACString &aValue) { - return SetChar(aPref, nsPromiseFlatCString(aValue).get()); + return SetCString(aPref, PromiseFlatCString(aValue).get()); } // static nsresult -Preferences::SetChar(const char* aPref, const PRUnichar* aValue) +Preferences::SetString(const char* aPref, const PRUnichar* aValue) { NS_ConvertUTF16toUTF8 utf8(aValue); - return SetChar(aPref, utf8.get()); + return SetCString(aPref, utf8.get()); } // static nsresult -Preferences::SetChar(const char* aPref, const nsAString &aValue) +Preferences::SetString(const char* aPref, const nsAString &aValue) { NS_ConvertUTF16toUTF8 utf8(aValue); - return SetChar(aPref, utf8.get()); + return SetCString(aPref, utf8.get()); } // static diff --git a/widget/src/xpwidgets/GfxInfoBase.cpp b/widget/src/xpwidgets/GfxInfoBase.cpp index 15878c7b4c1..2205e938495 100644 --- a/widget/src/xpwidgets/GfxInfoBase.cpp +++ b/widget/src/xpwidgets/GfxInfoBase.cpp @@ -197,13 +197,14 @@ RemovePrefForFeature(PRInt32 aFeature) static bool GetPrefValueForDriverVersion(nsCString& aVersion) { - return NS_SUCCEEDED(Preferences::GetChar(SUGGESTED_VERSION_PREF, &aVersion)); + return NS_SUCCEEDED(Preferences::GetCString(SUGGESTED_VERSION_PREF, + &aVersion)); } static void -SetPrefValueForDriverVersion(const nsString& aVersion) +SetPrefValueForDriverVersion(const nsAString& aVersion) { - Preferences::SetChar(SUGGESTED_VERSION_PREF, aVersion); + Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion); } static void diff --git a/widget/src/xpwidgets/nsPrintOptionsImpl.cpp b/widget/src/xpwidgets/nsPrintOptionsImpl.cpp index cd9e3aadcff..8152eb1289e 100644 --- a/widget/src/xpwidgets/nsPrintOptionsImpl.cpp +++ b/widget/src/xpwidgets/nsPrintOptionsImpl.cpp @@ -308,7 +308,7 @@ nsPrintOptions::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName, #define GETSTRPREF(_prefname, _retval) \ NS_SUCCEEDED( \ - Preferences::GetChar( \ + Preferences::GetString( \ GetPrefName(_prefname, aPrinterName), _retval \ ) \ ) @@ -623,7 +623,7 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, DUMP_DBL(kWriteStr, kPrintPaperHeight, height); WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height); DUMP_STR(kWriteStr, kPrintPaperName, name); - Preferences::SetChar(GetPrefName(kPrintPaperName, aPrinterName), name); + Preferences::SetString(GetPrefName(kPrintPaperName, aPrinterName), name); } } @@ -652,48 +652,48 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(&uStr))) { DUMP_STR(kWriteStr, kPrintHeaderStrLeft, uStr); - Preferences::SetChar(GetPrefName(kPrintHeaderStrLeft, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName), + uStr); } } if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(&uStr))) { DUMP_STR(kWriteStr, kPrintHeaderStrCenter, uStr); - Preferences::SetChar(GetPrefName(kPrintHeaderStrCenter, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName), + uStr); } } if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { if (NS_SUCCEEDED(aPS->GetHeaderStrRight(&uStr))) { DUMP_STR(kWriteStr, kPrintHeaderStrRight, uStr); - Preferences::SetChar(GetPrefName(kPrintHeaderStrRight, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName), + uStr); } } if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { if (NS_SUCCEEDED(aPS->GetFooterStrLeft(&uStr))) { DUMP_STR(kWriteStr, kPrintFooterStrLeft, uStr); - Preferences::SetChar(GetPrefName(kPrintFooterStrLeft, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName), + uStr); } } if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { if (NS_SUCCEEDED(aPS->GetFooterStrCenter(&uStr))) { DUMP_STR(kWriteStr, kPrintFooterStrCenter, uStr); - Preferences::SetChar(GetPrefName(kPrintFooterStrCenter, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName), + uStr); } } if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { if (NS_SUCCEEDED(aPS->GetFooterStrRight(&uStr))) { DUMP_STR(kWriteStr, kPrintFooterStrRight, uStr); - Preferences::SetChar(GetPrefName(kPrintFooterStrRight, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName), + uStr); } } @@ -728,7 +728,7 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, if (aFlags & nsIPrintSettings::kInitSavePlexName) { if (NS_SUCCEEDED(aPS->GetPlexName(&uStr))) { DUMP_STR(kWriteStr, kPrintPlexName, uStr); - Preferences::SetChar(GetPrefName(kPrintPlexName, aPrinterName), uStr); + Preferences::SetString(GetPrefName(kPrintPlexName, aPrinterName), uStr); } } @@ -743,15 +743,15 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, if (aFlags & nsIPrintSettings::kInitSaveColorspace) { if (NS_SUCCEEDED(aPS->GetColorspace(&uStr))) { DUMP_STR(kWriteStr, kPrintColorspace, uStr); - Preferences::SetChar(GetPrefName(kPrintColorspace, aPrinterName), uStr); + Preferences::SetString(GetPrefName(kPrintColorspace, aPrinterName), uStr); } } if (aFlags & nsIPrintSettings::kInitSaveResolutionName) { if (NS_SUCCEEDED(aPS->GetResolutionName(&uStr))) { DUMP_STR(kWriteStr, kPrintResolutionName, uStr); - Preferences::SetChar(GetPrefName(kPrintResolutionName, aPrinterName), - uStr); + Preferences::SetString(GetPrefName(kPrintResolutionName, aPrinterName), + uStr); } } @@ -772,7 +772,7 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, if (aFlags & nsIPrintSettings::kInitSavePrintCommand) { if (NS_SUCCEEDED(aPS->GetPrintCommand(&uStr))) { DUMP_STR(kWriteStr, kPrintCommand, uStr); - Preferences::SetChar(GetPrefName(kPrintCommand, aPrinterName), uStr); + Preferences::SetString(GetPrefName(kPrintCommand, aPrinterName), uStr); } } @@ -781,7 +781,7 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, && aPrinterName.IsEmpty()) { if (NS_SUCCEEDED(aPS->GetPrinterName(&uStr))) { DUMP_STR(kWriteStr, kPrinterName, uStr); - Preferences::SetChar(kPrinterName, uStr); + Preferences::SetString(kPrinterName, uStr); } } @@ -795,7 +795,7 @@ nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, if (aFlags & nsIPrintSettings::kInitSaveToFileName) { if (NS_SUCCEEDED(aPS->GetToFileName(&uStr))) { DUMP_STR(kWriteStr, kPrintToFileName, uStr); - Preferences::SetChar(GetPrefName(kPrintToFileName, aPrinterName), uStr); + Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr); } } @@ -906,7 +906,7 @@ nsPrintOptions::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName) // Look up the printer from the last print job nsAutoString lastPrinterName; - Preferences::GetChar(kPrinterName, &lastPrinterName); + Preferences::GetString(kPrinterName, &lastPrinterName); if (!lastPrinterName.IsEmpty()) { // Verify it's still a valid printer nsCOMPtr printers; @@ -1096,7 +1096,7 @@ nsPrintOptions::ReadPrefDouble(const char * aPrefId, double& aVal) NS_ENSURE_ARG_POINTER(aPrefId); nsCAutoString str; - nsresult rv = Preferences::GetChar(aPrefId, &str); + nsresult rv = Preferences::GetCString(aPrefId, &str); if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { aVal = atof(str.get()); } @@ -1111,7 +1111,7 @@ nsPrintOptions::WritePrefDouble(const char * aPrefId, double aVal) nsPrintfCString str("%6.2f", aVal); NS_ENSURE_TRUE(!str.IsEmpty(), NS_ERROR_FAILURE); - return Preferences::SetChar(aPrefId, str); + return Preferences::SetCString(aPrefId, str); } void @@ -1119,9 +1119,9 @@ nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId, PRInt32& aTwips, const char * aMarginPref) { nsCAutoString str; - nsresult rv = Preferences::GetChar(aPrefId, &str); + nsresult rv = Preferences::GetCString(aPrefId, &str); if (NS_FAILED(rv) || str.IsEmpty()) { - rv = Preferences::GetChar(aMarginPref, &str); + rv = Preferences::GetCString(aMarginPref, &str); } if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { nsAutoString justStr; @@ -1143,7 +1143,7 @@ nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId, PRInt32 aTwips) nsCAutoString inchesStr; inchesStr.AppendFloat(inches); - Preferences::SetChar(aPrefId, inchesStr); + Preferences::SetCString(aPrefId, inchesStr); } void @@ -1175,7 +1175,7 @@ nsPrintOptions::ReadJustification(const char * aPrefId, PRInt16& aJust, { aJust = aInitValue; nsAutoString justStr; - if (NS_SUCCEEDED(Preferences::GetChar(aPrefId, &justStr))) { + if (NS_SUCCEEDED(Preferences::GetString(aPrefId, &justStr))) { if (justStr.EqualsASCII(kJustRight)) { aJust = nsIPrintSettings::kJustRight; } else if (justStr.EqualsASCII(kJustCenter)) { @@ -1192,15 +1192,15 @@ nsPrintOptions::WriteJustification(const char * aPrefId, PRInt16 aJust) { switch (aJust) { case nsIPrintSettings::kJustLeft: - Preferences::SetChar(aPrefId, kJustLeft); + Preferences::SetCString(aPrefId, kJustLeft); break; case nsIPrintSettings::kJustCenter: - Preferences::SetChar(aPrefId, kJustCenter); + Preferences::SetCString(aPrefId, kJustCenter); break; case nsIPrintSettings::kJustRight: - Preferences::SetChar(aPrefId, kJustRight); + Preferences::SetCString(aPrefId, kJustRight); break; } //switch } diff --git a/widget/src/xpwidgets/nsXPLookAndFeel.cpp b/widget/src/xpwidgets/nsXPLookAndFeel.cpp index e9a213ebcd9..594b481d070 100644 --- a/widget/src/xpwidgets/nsXPLookAndFeel.cpp +++ b/widget/src/xpwidgets/nsXPLookAndFeel.cpp @@ -259,7 +259,7 @@ void nsXPLookAndFeel::ColorPrefChanged (unsigned int index, const char *prefName) { nsAutoString colorStr; - nsresult rv = Preferences::GetChar(prefName, &colorStr); + nsresult rv = Preferences::GetString(prefName, &colorStr); if (NS_FAILED(rv)) { return; } @@ -314,7 +314,7 @@ void nsXPLookAndFeel::InitColorFromPref(PRInt32 i) { nsAutoString colorStr; - nsresult rv = Preferences::GetChar(sColorPrefs[i], &colorStr); + nsresult rv = Preferences::GetString(sColorPrefs[i], &colorStr); if (NS_FAILED(rv) || colorStr.IsEmpty()) { return; }