29 #include <sbIPropertyInfo.h>
35 #include <nsArrayEnumerator.h>
37 #include <nsEnumeratorUtils.h>
38 #include <nsServiceManagerUtils.h>
47 PRUnichar decimalPoint =
'.';
48 lconv *localInfo = localeconv();
50 decimalPoint = localInfo->decimal_point[0];
68 NS_ASSERTION(mLock,
"sbPropertyUnit::mLock failed to create lock!");
73 const nsAString& aShortName,
77 , mShortName(aShortName)
82 NS_ASSERTION(
mLock,
"sbPropertyOperator::mLock failed to create lock!");
89 PR_DestroyLock(
mLock);
95 const nsAString & aShortName,
96 const nsAString & aID)
99 NS_ENSURE_TRUE(
mInitialized == PR_FALSE, NS_ERROR_ALREADY_INITIALIZED);
113 NS_IMETHODIMP sbPropertyUnit::GetName(nsAString &
aName)
124 NS_IMETHODIMP sbPropertyUnit::GetShortName(nsAString & aShortName)
136 NS_IMETHODIMP sbPropertyUnit::GetId(nsAString &aID)
153 , mNativeInternal((PRUint32)-1)
156 mLock = PR_NewLock();
157 NS_ASSERTION(mLock,
"sbPropertyUnitConverter::mLock failed to create lock!");
166 PR_DestroyLock(
mLock);
174 const nsAString &aUnitExternalID,
175 const nsAString &aUnitName,
176 const nsAString &aUnitShortName,
189 nsString
key(aUnitExternalID);
196 PRFloat64 &aOutValue) {
198 NS_ConvertUTF16toUTF8 narrow(aValue);
199 if(PR_sscanf(narrow.get(),
gsFmtFloatIn, &aOutValue) != 1) {
201 return NS_ERROR_INVALID_ARG;
208 nsAString &aOutValue) {
211 if(PR_snprintf(out, 63,
gsFmtFloatOut, aValue) == (PRUint32)-1) {
212 aOutValue = EmptyString();
213 return NS_ERROR_FAILURE;
215 NS_ConvertUTF8toUTF16 wide(out);
225 NS_ENSURE_SUCCESS(rv, rv);
229 NS_ENSURE_SUCCESS(rv, rv);
236 sbPropertyUnitConverter::Convert(
const nsAString &
aValue,
237 const nsAString & aFromUnitID,
238 const nsAString & aToUnitID,
239 PRInt32 aMinDecimals,
240 PRInt32 aMaxDecimals,
246 if (aFromUnitID.Equals(aToUnitID)) {
252 nsString fromKey(aFromUnitID);
253 propertyUnitMap::iterator fromUnitIterator =
mUnitsMap.find(fromKey);
256 return NS_ERROR_INVALID_ARG;
259 nsString toKey(aToUnitID);
260 propertyUnitMap::iterator toUnitIterator =
mUnitsMap.find(toKey);
263 return NS_ERROR_INVALID_ARG;
266 PRUint32 fromUnit = (*fromUnitIterator).second.mInternalId;
267 PRUint32 toUnit = (*toUnitIterator).second.mInternalId;
269 PRFloat64 floatValue;
271 NS_ENSURE_SUCCESS(rv, rv);
277 NS_ENSURE_SUCCESS(rv, rv);
289 if (decimal != (PRUint32)-1) {
290 while (aValue.CharAt(aValue.Length()-1) ==
'0')
291 aValue.Cut(aValue.Length()-1, 1);
292 if (aValue.Length() == decimal+1)
293 aValue.Cut(decimal, 1);
299 PRUint32 aDecimals) {
301 if (decimal != (PRUint32)-1) {
302 PRUint32
p = decimal + aDecimals;
303 if (aValue.Length() > p+1) {
304 aValue.Cut(p+1, aValue.Length()-1-
p);
311 PRUint32 aDecimals) {
313 if (decimal == (PRUint32)-1) {
315 decimal = aValue.Length()-1;
317 PRUint32 n = aValue.Length() - decimal - 1;
318 for (;n<aDecimals;n++) {
319 aValue += NS_LITERAL_STRING(
"0");
327 PRInt32 aMinDecimals,
328 PRInt32 aMaxDecimals) {
330 if (aMaxDecimals != -1) {
338 if (aMinDecimals != -1) {
347 NS_ENSURE_ARG_POINTER(aUnits);
350 nsCOMArray<sbIPropertyUnit>
array;
351 propertyUnitList::iterator it =
mUnits.begin();
352 for(; it !=
mUnits.end(); it++) {
353 propertyUnit u = (*it);
354 nsCOMPtr<sbIPropertyUnit> unit = u.mUnit;
355 array.AppendObject(unit);
358 return NS_NewArrayEnumerator(aUnits, array);
362 NS_IMETHODIMP sbPropertyUnitConverter::GetNativeUnitId(nsAString &aNativeUnit)
370 NS_IMETHODIMP sbPropertyUnitConverter::GetStringBundle(nsAString &aStringBundle)
385 sbPropertyUnitConverter::AutoFormat(
const nsAString &aValue,
386 PRInt32 aMinDecimals,
387 PRInt32 aMaxDecimals,
388 nsAString &_retval) {
398 return NS_ERROR_FAILURE;
399 nsCOMPtr<sbIPropertyInfo> propInfo = do_QueryReferent(
mPropertyInfo, &rv);
400 if (NS_FAILED(rv) || !propInfo)
401 return NS_ERROR_FAILURE;
402 return propInfo->Format(aValue, _retval);
413 return NS_ERROR_FAILURE;
414 nsCOMPtr<sbIPropertyInfo> propInfo = do_QueryReferent(
mPropertyInfo, &rv);
415 if (NS_FAILED(rv) || !propInfo)
416 return NS_ERROR_FAILURE;
418 nsresult rv = propInfo->Format(aValue, _retval);
419 NS_ENSURE_SUCCESS(rv, rv);
434 out += NS_LITERAL_STRING(
" ");
437 propertyUnitMapInternal::iterator
441 return NS_ERROR_FAILURE;
443 propertyUnit u = (*toUnitIterator).second;
444 nsCOMPtr<sbIPropertyUnit> unit = u.mUnit;
447 nsresult rv = unit->GetShortName(shortName);
448 NS_ENSURE_SUCCESS(rv, rv);
451 if (shortName.First() ==
'&' &&
452 shortName.CharAt(shortName.Length()-1) !=
';') {
457 nsCOMPtr<nsIStringBundleService> stringBundleService =
458 do_GetService(
"@mozilla.org/intl/stringbundle;1", &rv);
461 rv = stringBundleService->CreateBundle(
url.get(),
463 NS_ENSURE_SUCCESS(rv, rv);
470 NS_ENSURE_SUCCESS(rv, rv);
492 return NS_ERROR_ALREADY_INITIALIZED;
496 NS_ENSURE_SUCCESS(rv, rv);
504 NS_ENSURE_ARG_POINTER(_retval);
509 return NS_ERROR_NOT_INITIALIZED;
512 nsCOMPtr<sbIPropertyInfo> propInfo = do_QueryReferent(
mPropertyInfo, &rv);
513 NS_ENSURE_SUCCESS(rv, rv);
NS_DECL_ISUPPORTS NS_DECL_SBIPROPERTYUNIT sbPropertyUnit()
propertyUnitMapInternal mUnitsMapInternal
void ApplyDecimalLimits(nsAString &aValue, PRInt32 aMinDecimals, PRInt32 aMaxDecimals)
nsresult SprintfFloat64(const PRFloat64 aValue, nsAString &aOutValue)
static const char * gsFmtFloatIn
Property unit interface This interface describes a property unit.
virtual ~sbPropertyUnitConverter()
nsresult SscanfFloat64(const nsAString &aValue, PRFloat64 &aOutValue)
NS_IMETHODIMP Init(const nsAString &aName, const nsAString &aShortName, const nsAString &aID)
void ForceToNDecimals(nsAString &aValue, PRUint32 aDecimals)
nsCOMPtr< nsIWeakReference > mPropertyInfo
virtual ~sbPropertyUnit()
void RemoveTrailingZeroes(nsAString &aValue)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
NS_IMETHOD ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)=0
propertyUnitMap mUnitsMap
static PRUnichar GetDecimalPoint()
void LimitToNDecimals(nsAString &aValue, PRUint32 aDecimals)
An interface used to describe a metadata property for use by the UI and other sbILibrary interfaces (...
nsCOMPtr< nsIStringBundle > mStringBundleObject
static const char * gsFmtFloatOut
virtual PRInt32 GetAutoUnit(PRFloat64 aValue)
Property unit converter interface This is an interface songbird property converter, used to convert a property value between its various units representations.
void RegisterUnit(PRUint32 aUnitInternalID, const nsAString &aUnitExternalID, const nsAString &aUnitName, const nsAString &aUnitShortName, PRBool isNative=PR_FALSE)
nsresult PerformConversion(PRFloat64 &aValue, PRUint32 aFromUnit, PRUint32 aToUnit)
NS_IMETHOD ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)=0
void SetStringBundle(const nsAString &aStringBundle)