diff --git a/modules/libpref/public/nsIPref.idl b/modules/libpref/public/nsIPref.idl index bd9b959f23af..e42d624122e9 100644 --- a/modules/libpref/public/nsIPref.idl +++ b/modules/libpref/public/nsIPref.idl @@ -106,6 +106,7 @@ interface nsIPref : nsISupports { /* set preferences */ void SetCharPref(in string pref, in string value); + void SetUnicharPref(in string pref, in wstring value); void SetIntPref(in string pref, in long value); void SetBoolPref(in string pref, in boolean value); [noscript] void SetBinaryPref(in string pref, in voidStar value, in unsigned long size); @@ -143,6 +144,7 @@ interface nsIPref : nsISupports { /* copy versions of getters */ string CopyCharPref(in string pref); + wstring CopyUnicharPref(in string pref); [noscript] voidStar CopyBinaryPref(in string pref, out long size); string CopyDefaultCharPref(in string pref); diff --git a/modules/libpref/src/nsPref.cpp b/modules/libpref/src/nsPref.cpp index d6db706e8db1..ce364e9d0aa6 100644 --- a/modules/libpref/src/nsPref.cpp +++ b/modules/libpref/src/nsPref.cpp @@ -42,6 +42,8 @@ #include "nsIProfile.h" #include "nsQuickSort.h" +#include "nsTextFormater.h" + #include "plhash.h" #include "prmem.h" #include "plstr.h" @@ -596,6 +598,21 @@ NS_IMETHODIMP nsPref::SetCharPref(const char *pref,const char* value) return _convertRes(PREF_SetCharPref(pref, value)); } +NS_IMETHODIMP nsPref::SetUnicharPref(const char *pref, const PRUnichar *value) +{ + nsresult rv; + nsAutoString str(value); + + char *utf8String = str.ToNewUTF8String(); + + if (!utf8String) return NS_ERROR_OUT_OF_MEMORY; + + rv = SetCharPref(pref, utf8String); + nsCRT::free(utf8String); + + return rv; +} + NS_IMETHODIMP nsPref::SetIntPref(const char *pref,PRInt32 value) { return _convertRes(PREF_SetIntPref(pref, value)); @@ -726,6 +743,37 @@ NS_IMETHODIMP nsPref::CopyCharPref(const char *pref, char ** return_buf) return _convertRes(PREF_CopyCharPref(pref, return_buf)); } +// unicode "%s" format string +static const PRUnichar unicodeFormatter[] = { + (PRUnichar)'%', + (PRUnichar)'s', + (PRUnichar)0, +}; + +NS_IMETHODIMP nsPref::CopyUnicharPref(const char *pref, PRUnichar ** return_buf) +{ + nsresult rv; + + // get the UTF8 string for conversion + char *utf8String; + rv = CopyCharPref(pref, &utf8String); + if (NS_FAILED(rv)) return rv; + + // convert to PRUnichar using nsTextFormatter + // this is so ugly, it allocates memory at least 4 times :( + PRUnichar *unicodeString = + nsTextFormater::smprintf(unicodeFormatter, utf8String); + PL_strfree(utf8String); + if (!unicodeString) return NS_ERROR_OUT_OF_MEMORY; + + // use the right allocator + *return_buf = nsCRT::strdup(unicodeString); + nsTextFormater::smprintf_free(unicodeString); + if (!*return_buf) return NS_ERROR_OUT_OF_MEMORY; + + return NS_OK; +} + NS_IMETHODIMP nsPref::CopyBinaryPref(const char *pref, int *size, void ** return_value) {