bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / numbers / numfmuno.cxx
blobe74572275ed6cafe581772485c6399a08596fae6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/color.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <osl/mutex.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/ustring.hxx>
26 #include <com/sun/star/util/Date.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <cppuhelper/supportsservice.hxx>
30 #include "numfmuno.hxx"
31 #include <svl/numuno.hxx>
32 #include <svl/zforlist.hxx>
33 #include <svl/zformat.hxx>
34 #include <svl/itemprop.hxx>
36 using namespace com::sun::star;
38 #define PROPERTYNAME_FMTSTR "FormatString"
39 #define PROPERTYNAME_LOCALE "Locale"
40 #define PROPERTYNAME_TYPE "Type"
41 #define PROPERTYNAME_COMMENT "Comment"
42 #define PROPERTYNAME_CURREXT "CurrencyExtension"
43 #define PROPERTYNAME_CURRSYM "CurrencySymbol"
44 #define PROPERTYNAME_CURRABB "CurrencyAbbreviation"
45 #define PROPERTYNAME_DECIMALS "Decimals"
46 #define PROPERTYNAME_LEADING "LeadingZeros"
47 #define PROPERTYNAME_NEGRED "NegativeRed"
48 #define PROPERTYNAME_STDFORM "StandardFormat"
49 #define PROPERTYNAME_THOUS "ThousandsSeparator"
50 #define PROPERTYNAME_USERDEF "UserDefined"
52 #define PROPERTYNAME_NOZERO "NoZero"
53 #define PROPERTYNAME_NULLDATE "NullDate"
54 #define PROPERTYNAME_STDDEC "StandardDecimals"
55 #define PROPERTYNAME_TWODIGIT "TwoDigitDateStart"
57 // All without a Which-ID, Map only for PropertySetInfo
59 static const SfxItemPropertyMapEntry* lcl_GetNumberFormatPropertyMap()
61 static const SfxItemPropertyMapEntry aNumberFormatPropertyMap_Impl[] =
63 {OUString(PROPERTYNAME_FMTSTR), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
64 {OUString(PROPERTYNAME_LOCALE), 0, cppu::UnoType<lang::Locale>::get(),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
65 {OUString(PROPERTYNAME_TYPE), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
66 {OUString(PROPERTYNAME_COMMENT), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
67 {OUString(PROPERTYNAME_CURREXT), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
68 {OUString(PROPERTYNAME_CURRSYM), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
69 {OUString(PROPERTYNAME_DECIMALS), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
70 {OUString(PROPERTYNAME_LEADING), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
71 {OUString(PROPERTYNAME_NEGRED), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
72 {OUString(PROPERTYNAME_STDFORM), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
73 {OUString(PROPERTYNAME_THOUS), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
74 {OUString(PROPERTYNAME_USERDEF), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
75 {OUString(PROPERTYNAME_CURRABB), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
76 { OUString(), 0, css::uno::Type(), 0, 0 }
78 return aNumberFormatPropertyMap_Impl;
81 static const SfxItemPropertyMapEntry* lcl_GetNumberSettingsPropertyMap()
83 static const SfxItemPropertyMapEntry aNumberSettingsPropertyMap_Impl[] =
85 {OUString(PROPERTYNAME_NOZERO), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND, 0},
86 {OUString(PROPERTYNAME_NULLDATE), 0, cppu::UnoType<util::Date>::get(), beans::PropertyAttribute::BOUND, 0},
87 {OUString(PROPERTYNAME_STDDEC), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
88 {OUString(PROPERTYNAME_TWODIGIT), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
89 { OUString(), 0, css::uno::Type(), 0, 0 }
91 return aNumberSettingsPropertyMap_Impl;
94 static LanguageType lcl_GetLanguage( const lang::Locale& rLocale )
96 // empty language -> LANGUAGE_SYSTEM
97 if ( rLocale.Language.isEmpty() )
98 return LANGUAGE_SYSTEM;
100 LanguageType eRet = LanguageTag::convertToLanguageType( rLocale, false);
101 if ( eRet == LANGUAGE_NONE )
102 eRet = LANGUAGE_SYSTEM; //! or throw an exception?
104 return eRet;
107 SvNumberFormatterServiceObj::SvNumberFormatterServiceObj()
108 :m_aMutex()
112 SvNumberFormatterServiceObj::~SvNumberFormatterServiceObj()
116 // XNumberFormatter
118 void SAL_CALL SvNumberFormatterServiceObj::attachNumberFormatsSupplier( const uno::Reference<util::XNumberFormatsSupplier>& _xSupplier )
119 throw(uno::RuntimeException, std::exception)
121 ::rtl::Reference< SvNumberFormatsSupplierObj > xAutoReleaseOld;
123 // SYNCHRONIZED ->
125 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
127 SvNumberFormatsSupplierObj* pNew = SvNumberFormatsSupplierObj::getImplementation( _xSupplier );
128 if (!pNew)
129 throw uno::RuntimeException(); // wrong object
131 xAutoReleaseOld = xSupplier;
133 xSupplier = pNew;
134 m_aMutex = xSupplier->getSharedMutex();
136 // <- SYNCHRONIZED
139 uno::Reference<util::XNumberFormatsSupplier> SAL_CALL SvNumberFormatterServiceObj::getNumberFormatsSupplier()
140 throw(uno::RuntimeException, std::exception)
142 ::osl::MutexGuard aGuard( m_aMutex );
143 return xSupplier.get();
146 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::detectNumberFormat( sal_Int32 nKey, const OUString& aString )
147 throw(util::NotNumericException, uno::RuntimeException, std::exception)
149 ::osl::MutexGuard aGuard( m_aMutex );
151 sal_Int32 nRet = 0;
152 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
153 if (pFormatter)
155 sal_uInt32 nUKey = nKey;
156 double fValue = 0.0;
157 if ( pFormatter->IsNumberFormat(aString, nUKey, fValue) )
158 nRet = nUKey;
159 else
160 throw util::NotNumericException();
162 else
163 throw uno::RuntimeException();
165 return nRet;
168 double SAL_CALL SvNumberFormatterServiceObj::convertStringToNumber( sal_Int32 nKey, const OUString& aString )
169 throw(util::NotNumericException, uno::RuntimeException, std::exception)
171 ::osl::MutexGuard aGuard( m_aMutex );
173 double fRet = 0.0;
174 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
175 if (pFormatter)
177 sal_uInt32 nUKey = nKey;
178 double fValue = 0.0;
179 if ( pFormatter->IsNumberFormat(aString, nUKey, fValue) )
180 fRet = fValue;
181 else
182 throw util::NotNumericException();
184 else
185 throw uno::RuntimeException();
187 return fRet;
190 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToString( sal_Int32 nKey, double fValue )
191 throw(uno::RuntimeException, std::exception)
193 ::osl::MutexGuard aGuard( m_aMutex );
195 OUString aRet;
196 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
197 if (pFormatter)
199 Color* pColor = NULL;
200 pFormatter->GetOutputString(fValue, nKey, aRet, &pColor);
202 else
203 throw uno::RuntimeException();
205 return aRet;
208 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForNumber( sal_Int32 nKey,
209 double fValue,
210 util::Color aDefaultColor )
211 throw(uno::RuntimeException, std::exception)
213 ::osl::MutexGuard aGuard( m_aMutex );
215 util::Color nRet = aDefaultColor; // color = sal_Int32
216 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
217 if (pFormatter)
219 OUString aStr;
220 Color* pColor = NULL;
221 pFormatter->GetOutputString(fValue, nKey, aStr, &pColor);
222 if (pColor)
223 nRet = pColor->GetColor();
224 // Else keep Default
226 else
227 throw uno::RuntimeException();
229 return nRet;
232 OUString SAL_CALL SvNumberFormatterServiceObj::formatString( sal_Int32 nKey,
233 const OUString& aString )
234 throw(uno::RuntimeException, std::exception)
236 ::osl::MutexGuard aGuard( m_aMutex );
238 OUString aRet;
239 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
240 if (pFormatter)
242 Color* pColor = NULL;
243 pFormatter->GetOutputString(aString, nKey, aRet, &pColor);
245 else
247 throw uno::RuntimeException();
249 return aRet;
252 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForString( sal_Int32 nKey,
253 const OUString& aString,
254 util::Color aDefaultColor )
255 throw(uno::RuntimeException, std::exception)
257 ::osl::MutexGuard aGuard( m_aMutex );
259 util::Color nRet = aDefaultColor; // color = sal_Int32
260 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
261 if (pFormatter)
263 OUString aStr;
264 Color* pColor = NULL;
265 pFormatter->GetOutputString(aString, nKey, aStr, &pColor);
266 if (pColor)
268 nRet = pColor->GetColor();
270 // Else keep Default
272 else
274 throw uno::RuntimeException();
277 return nRet;
280 OUString SAL_CALL SvNumberFormatterServiceObj::getInputString( sal_Int32 nKey, double fValue )
281 throw(uno::RuntimeException, std::exception)
283 ::osl::MutexGuard aGuard( m_aMutex );
285 OUString aRet;
286 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
287 if (pFormatter)
288 pFormatter->GetInputLineString(fValue, nKey, aRet);
289 else
290 throw uno::RuntimeException();
292 return aRet;
295 // XNumberFormatPreviewer
297 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToPreviewString( const OUString& aFormat,
298 double fValue,
299 const lang::Locale& nLocale,
300 sal_Bool bAllowEnglish )
301 throw(util::MalformedNumberFormatException, uno::RuntimeException, std::exception)
303 ::osl::MutexGuard aGuard( m_aMutex );
305 OUString aRet;
306 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
307 if (pFormatter)
309 LanguageType eLang = lcl_GetLanguage( nLocale );
310 Color* pColor = NULL;
312 bool bOk;
313 if ( bAllowEnglish )
314 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aRet, &pColor, eLang );
315 else
316 bOk = pFormatter->GetPreviewString( aFormat, fValue, aRet, &pColor, eLang );
318 if (!bOk)
319 throw util::MalformedNumberFormatException();
321 else
322 throw uno::RuntimeException();
324 return aRet;
327 util::Color SAL_CALL SvNumberFormatterServiceObj::queryPreviewColorForNumber( const OUString& aFormat,
328 double fValue,
329 const lang::Locale& nLocale,
330 sal_Bool bAllowEnglish,
331 util::Color aDefaultColor )
332 throw(util::MalformedNumberFormatException, uno::RuntimeException, std::exception)
334 ::osl::MutexGuard aGuard( m_aMutex );
336 util::Color nRet = aDefaultColor; // color = sal_Int32
337 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
338 if (pFormatter)
340 OUString aOutString;
341 LanguageType eLang = lcl_GetLanguage( nLocale );
342 Color* pColor = NULL;
344 bool bOk;
345 if ( bAllowEnglish )
346 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aOutString, &pColor, eLang );
347 else
348 bOk = pFormatter->GetPreviewString( aFormat, fValue, aOutString, &pColor, eLang );
350 if (bOk)
352 if (pColor)
353 nRet = pColor->GetColor();
354 // Else keep Default
356 else
357 throw util::MalformedNumberFormatException();
359 else
360 throw uno::RuntimeException();
362 return nRet;
365 // XServiceInfo
367 OUString SAL_CALL SvNumberFormatterServiceObj::getImplementationName()
368 throw(uno::RuntimeException, std::exception)
370 return OUString("com.sun.star.uno.util.numbers.SvNumberFormatterServiceObject");
373 sal_Bool SAL_CALL SvNumberFormatterServiceObj::supportsService( const OUString& ServiceName )
374 throw(uno::RuntimeException, std::exception)
376 return cppu::supportsService( this, ServiceName );
379 uno::Sequence<OUString> SAL_CALL SvNumberFormatterServiceObj::getSupportedServiceNames()
380 throw(uno::RuntimeException, std::exception)
382 uno::Sequence<OUString> aRet(1);
383 OUString* pArray = aRet.getArray();
384 pArray[0] = "com.sun.star.util.NumberFormatter";
385 return aRet;
388 SvNumberFormatsObj::SvNumberFormatsObj( SvNumberFormatsSupplierObj& _rParent, ::comphelper::SharedMutex& _rMutex )
389 :rSupplier( _rParent )
390 ,m_aMutex( _rMutex )
392 rSupplier.acquire();
395 SvNumberFormatsObj::~SvNumberFormatsObj()
397 rSupplier.release();
400 // XNumberFormats
402 uno::Reference<beans::XPropertySet> SAL_CALL SvNumberFormatsObj::getByKey( sal_Int32 nKey )
403 throw(uno::RuntimeException, std::exception)
405 ::osl::MutexGuard aGuard( m_aMutex );
407 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
408 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
409 if (pFormat)
410 return new SvNumberFormatObj( rSupplier, nKey, m_aMutex );
411 else
412 throw uno::RuntimeException();
415 uno::Sequence<sal_Int32> SAL_CALL SvNumberFormatsObj::queryKeys( sal_Int16 nType,
416 const lang::Locale& nLocale,
417 sal_Bool bCreate )
418 throw(uno::RuntimeException, std::exception)
420 ::osl::MutexGuard aGuard( m_aMutex );
422 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
423 if ( pFormatter )
425 sal_uInt32 nIndex = 0;
426 LanguageType eLang = lcl_GetLanguage( nLocale );
427 SvNumberFormatTable& rTable = bCreate ?
428 pFormatter->ChangeCL( nType, nIndex, eLang ) :
429 pFormatter->GetEntryTable( nType, nIndex, eLang );
430 sal_uInt32 nCount = rTable.size();
431 uno::Sequence<sal_Int32> aSeq(nCount);
432 sal_Int32* pAry = aSeq.getArray();
433 sal_uInt32 i=0;
434 for (SvNumberFormatTable::iterator it = rTable.begin(); it != rTable.end(); ++it, ++i)
435 pAry[i] = it->first;
437 return aSeq;
439 else
440 throw uno::RuntimeException();
443 sal_Int32 SAL_CALL SvNumberFormatsObj::queryKey( const OUString& aFormat,
444 const lang::Locale& nLocale,
445 sal_Bool bScan )
446 throw(uno::RuntimeException, std::exception)
448 ::osl::MutexGuard aGuard( m_aMutex );
450 sal_Int32 nRet = 0;
451 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
452 if (pFormatter)
454 LanguageType eLang = lcl_GetLanguage( nLocale );
455 if (bScan)
457 //! FIXME: Something still needs to happen here ...
459 nRet = pFormatter->GetEntryKey( aFormat, eLang );
461 else
462 throw uno::RuntimeException();
464 return nRet;
467 sal_Int32 SAL_CALL SvNumberFormatsObj::addNew( const OUString& aFormat,
468 const lang::Locale& nLocale )
469 throw(util::MalformedNumberFormatException, uno::RuntimeException, std::exception)
471 ::osl::MutexGuard aGuard( m_aMutex );
473 sal_Int32 nRet = 0;
474 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
475 if (pFormatter)
477 OUString aFormStr = aFormat;
478 LanguageType eLang = lcl_GetLanguage( nLocale );
479 sal_uInt32 nKey = 0;
480 sal_Int32 nCheckPos = 0;
481 short nType = 0;
482 bool bOk = pFormatter->PutEntry( aFormStr, nCheckPos, nType, nKey, eLang );
483 if (bOk)
484 nRet = nKey;
485 else if (nCheckPos)
487 throw util::MalformedNumberFormatException(); // Invalid Format
489 else
490 throw uno::RuntimeException(); // Other error (e.g. already added)
492 else
493 throw uno::RuntimeException();
495 return nRet;
498 sal_Int32 SAL_CALL SvNumberFormatsObj::addNewConverted( const OUString& aFormat,
499 const lang::Locale& nLocale,
500 const lang::Locale& nNewLocale )
501 throw(util::MalformedNumberFormatException, uno::RuntimeException, std::exception)
503 ::osl::MutexGuard aGuard( m_aMutex );
505 sal_Int32 nRet = 0;
506 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
507 if (pFormatter)
509 OUString aFormStr = aFormat;
510 LanguageType eLang = lcl_GetLanguage( nLocale );
511 LanguageType eNewLang = lcl_GetLanguage( nNewLocale );
512 sal_uInt32 nKey = 0;
513 sal_Int32 nCheckPos = 0;
514 short nType = 0;
515 bool bOk = pFormatter->PutandConvertEntry( aFormStr, nCheckPos, nType, nKey, eLang, eNewLang );
516 if (bOk || nKey > 0)
517 nRet = nKey;
518 else if (nCheckPos)
520 throw util::MalformedNumberFormatException(); // Invalid format
522 else
523 throw uno::RuntimeException(); // Other error (e.g. already added)
525 else
526 throw uno::RuntimeException();
528 return nRet;
531 void SAL_CALL SvNumberFormatsObj::removeByKey( sal_Int32 nKey ) throw(uno::RuntimeException, std::exception)
533 ::osl::MutexGuard aGuard( m_aMutex );
534 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
536 if (pFormatter)
538 pFormatter->DeleteEntry(nKey);
542 OUString SAL_CALL SvNumberFormatsObj::generateFormat( sal_Int32 nBaseKey,
543 const lang::Locale& nLocale,
544 sal_Bool bThousands,
545 sal_Bool bRed, sal_Int16 nDecimals,
546 sal_Int16 nLeading )
547 throw(uno::RuntimeException, std::exception)
549 ::osl::MutexGuard aGuard( m_aMutex );
551 OUString aRet;
552 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
553 if (pFormatter)
555 LanguageType eLang = lcl_GetLanguage( nLocale );
556 aRet = pFormatter->GenerateFormat(nBaseKey, eLang, bThousands, bRed, nDecimals, nLeading);
558 else
559 throw uno::RuntimeException();
561 return aRet;
564 // XNumberFormatTypes
566 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardIndex( const lang::Locale& nLocale )
567 throw(uno::RuntimeException, std::exception)
569 ::osl::MutexGuard aGuard( m_aMutex );
571 sal_Int32 nRet = 0;
572 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
573 if (pFormatter)
575 LanguageType eLang = lcl_GetLanguage( nLocale );
576 nRet = pFormatter->GetStandardIndex(eLang);
578 else
579 throw uno::RuntimeException();
581 return nRet;
584 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardFormat( sal_Int16 nType, const lang::Locale& nLocale )
585 throw(uno::RuntimeException, std::exception)
587 ::osl::MutexGuard aGuard( m_aMutex );
589 sal_Int32 nRet = 0;
590 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
591 if (pFormatter)
593 LanguageType eLang = lcl_GetLanguage( nLocale );
594 // Mask out "defined" bit, so type from an existing number format
595 // can directly be used for getStandardFormat
596 nType &= ~css::util::NumberFormat::DEFINED;
597 nRet = pFormatter->GetStandardFormat(nType, eLang);
599 else
600 throw uno::RuntimeException();
602 return nRet;
605 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatIndex( sal_Int16 nIndex, const lang::Locale& nLocale )
606 throw(uno::RuntimeException, std::exception)
608 ::osl::MutexGuard aGuard( m_aMutex );
610 sal_Int32 nRet = 0;
611 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
612 if (pFormatter)
614 LanguageType eLang = lcl_GetLanguage( nLocale );
615 nRet = pFormatter->GetFormatIndex( (NfIndexTableOffset)nIndex, eLang );
617 else
618 throw uno::RuntimeException();
620 return nRet;
623 sal_Bool SAL_CALL SvNumberFormatsObj::isTypeCompatible( sal_Int16 nOldType, sal_Int16 nNewType )
624 throw(uno::RuntimeException, std::exception)
626 ::osl::MutexGuard aGuard( m_aMutex );
628 bool bRet = false;
629 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
630 if (pFormatter)
631 bRet = pFormatter->IsCompatible( nOldType, nNewType );
632 else
633 throw uno::RuntimeException();
635 return bRet;
638 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatForLocale( sal_Int32 nKey, const lang::Locale& nLocale )
639 throw(uno::RuntimeException, std::exception)
641 ::osl::MutexGuard aGuard( m_aMutex );
643 sal_Int32 nRet = 0;
644 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
645 if (pFormatter)
647 LanguageType eLang = lcl_GetLanguage( nLocale );
648 nRet = pFormatter->GetFormatForLanguageIfBuiltIn(nKey, eLang);
650 else
651 throw uno::RuntimeException();
653 return nRet;
656 // XServiceInfo
658 OUString SAL_CALL SvNumberFormatsObj::getImplementationName()
659 throw(uno::RuntimeException, std::exception)
661 return OUString("SvNumberFormatsObj");
664 sal_Bool SAL_CALL SvNumberFormatsObj::supportsService( const OUString& ServiceName )
665 throw(uno::RuntimeException, std::exception)
667 return cppu::supportsService( this, ServiceName );
670 uno::Sequence<OUString> SAL_CALL SvNumberFormatsObj::getSupportedServiceNames()
671 throw(uno::RuntimeException, std::exception)
673 uno::Sequence<OUString> aRet(1);
674 OUString* pArray = aRet.getArray();
675 pArray[0] = "com.sun.star.util.NumberFormats";
676 return aRet;
679 SvNumberFormatObj::SvNumberFormatObj( SvNumberFormatsSupplierObj& rParent, sal_uLong nK, const ::comphelper::SharedMutex& _rMutex )
680 :rSupplier( rParent )
681 ,nKey( nK )
682 ,m_aMutex( _rMutex )
684 rSupplier.acquire();
687 SvNumberFormatObj::~SvNumberFormatObj()
689 rSupplier.release();
692 // XPropertySet
694 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatObj::getPropertySetInfo()
695 throw(uno::RuntimeException, std::exception)
697 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
698 static uno::Reference<beans::XPropertySetInfo> aRef =
699 new SfxItemPropertySetInfo( lcl_GetNumberFormatPropertyMap() );
700 return aRef;
703 void SAL_CALL SvNumberFormatObj::setPropertyValue( const OUString&,
704 const uno::Any& )
705 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
706 lang::IllegalArgumentException, lang::WrappedTargetException,
707 uno::RuntimeException, std::exception)
709 throw beans::UnknownPropertyException(); // Everything is read-only
712 uno::Any SAL_CALL SvNumberFormatObj::getPropertyValue( const OUString& aPropertyName )
713 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
714 uno::RuntimeException, std::exception)
716 ::osl::MutexGuard aGuard( m_aMutex );
718 uno::Any aRet;
719 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
720 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
721 if (pFormat)
723 bool bThousand, bRed;
724 sal_uInt16 nDecimals, nLeading;
726 if (aPropertyName == PROPERTYNAME_FMTSTR)
728 aRet <<= OUString( pFormat->GetFormatstring() );
730 else if (aPropertyName == PROPERTYNAME_LOCALE)
732 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
733 aRet <<= aLocale;
735 else if (aPropertyName == PROPERTYNAME_TYPE)
737 aRet <<= (sal_Int16)( pFormat->GetType() );
739 else if (aPropertyName == PROPERTYNAME_COMMENT)
741 aRet <<= OUString( pFormat->GetComment() );
743 else if (aPropertyName == PROPERTYNAME_STDFORM)
745 //! Pass through SvNumberformat Member bStandard?
746 sal_Bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
747 aRet.setValue( &bStandard, cppu::UnoType<bool>::get() );
749 else if (aPropertyName == PROPERTYNAME_USERDEF)
751 sal_Bool bUserDef = ( ( pFormat->GetType() & css::util::NumberFormat::DEFINED ) != 0 );
752 aRet.setValue( &bUserDef, cppu::UnoType<bool>::get() );
754 else if (aPropertyName == PROPERTYNAME_DECIMALS)
756 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
757 aRet <<= (sal_Int16)( nDecimals );
759 else if (aPropertyName == PROPERTYNAME_LEADING)
761 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
762 aRet <<= (sal_Int16)( nLeading );
764 else if (aPropertyName == PROPERTYNAME_NEGRED)
766 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
767 aRet.setValue( &bRed, cppu::UnoType<bool>::get() );
769 else if (aPropertyName == PROPERTYNAME_THOUS)
771 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
772 aRet.setValue( &bThousand, cppu::UnoType<bool>::get() );
774 else if (aPropertyName == PROPERTYNAME_CURRSYM)
776 OUString aSymbol, aExt;
777 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
778 aRet <<= aSymbol;
780 else if (aPropertyName == PROPERTYNAME_CURREXT)
782 OUString aSymbol, aExt;
783 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
784 aRet <<= aExt;
786 else if (aPropertyName == PROPERTYNAME_CURRABB)
788 OUString aSymbol, aExt;
789 bool bBank = false;
790 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
791 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
792 aSymbol, aExt, pFormat->GetLanguage() );
793 if ( pCurr )
794 aRet <<= OUString( pCurr->GetBankSymbol() );
795 else
796 aRet <<= OUString();
798 else
799 throw beans::UnknownPropertyException();
801 else
802 throw uno::RuntimeException();
804 return aRet;
807 void SAL_CALL SvNumberFormatObj::addPropertyChangeListener( const OUString&,
808 const uno::Reference<beans::XPropertyChangeListener>&)
809 throw(beans::UnknownPropertyException,
810 lang::WrappedTargetException, uno::RuntimeException, std::exception)
812 OSL_FAIL("not implemented");
815 void SAL_CALL SvNumberFormatObj::removePropertyChangeListener( const OUString&,
816 const uno::Reference<beans::XPropertyChangeListener>&)
817 throw(beans::UnknownPropertyException,
818 lang::WrappedTargetException, uno::RuntimeException, std::exception)
820 OSL_FAIL("not implemented");
823 void SAL_CALL SvNumberFormatObj::addVetoableChangeListener( const OUString&,
824 const uno::Reference<beans::XVetoableChangeListener>&)
825 throw(beans::UnknownPropertyException,
826 lang::WrappedTargetException, uno::RuntimeException, std::exception)
828 OSL_FAIL("not implemented");
831 void SAL_CALL SvNumberFormatObj::removeVetoableChangeListener( const OUString&,
832 const uno::Reference<beans::XVetoableChangeListener>&)
833 throw(beans::UnknownPropertyException,
834 lang::WrappedTargetException, uno::RuntimeException, std::exception)
836 OSL_FAIL("not implemented");
839 // XPropertyAccess
841 uno::Sequence<beans::PropertyValue> SAL_CALL SvNumberFormatObj::getPropertyValues()
842 throw(uno::RuntimeException, std::exception)
844 ::osl::MutexGuard aGuard( m_aMutex );
846 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
847 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
848 if (pFormat)
850 OUString aSymbol, aExt;
851 OUString aAbb;
852 bool bBank = false;
853 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
854 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
855 aSymbol, aExt, pFormat->GetLanguage() );
856 if ( pCurr )
857 aAbb = pCurr->GetBankSymbol();
859 OUString aFmtStr = pFormat->GetFormatstring();
860 OUString aComment = pFormat->GetComment();
861 sal_Bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
862 //! Pass through SvNumberformat Member bStandard?
863 sal_Bool bUserDef = ( ( pFormat->GetType() & css::util::NumberFormat::DEFINED ) != 0 );
864 bool bThousand, bRed;
865 sal_uInt16 nDecimals, nLeading;
866 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
867 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
869 uno::Sequence<beans::PropertyValue> aSeq(13);
870 beans::PropertyValue* pArray = aSeq.getArray();
872 pArray[0].Name = PROPERTYNAME_FMTSTR;
873 pArray[0].Value <<= aFmtStr;
874 pArray[1].Name = PROPERTYNAME_LOCALE;
875 pArray[1].Value <<= aLocale;
876 pArray[2].Name = PROPERTYNAME_TYPE;
877 pArray[2].Value <<= (sal_Int16)( pFormat->GetType() );
878 pArray[3].Name = PROPERTYNAME_COMMENT;
879 pArray[3].Value <<= aComment;
880 pArray[4].Name = PROPERTYNAME_STDFORM;
881 pArray[4].Value.setValue( &bStandard, cppu::UnoType<bool>::get() );
882 pArray[5].Name = PROPERTYNAME_USERDEF;
883 pArray[5].Value.setValue( &bUserDef, cppu::UnoType<bool>::get() );
884 pArray[6].Name = PROPERTYNAME_DECIMALS;
885 pArray[6].Value <<= (sal_Int16)( nDecimals );
886 pArray[7].Name = PROPERTYNAME_LEADING;
887 pArray[7].Value <<= (sal_Int16)( nLeading );
888 pArray[8].Name = PROPERTYNAME_NEGRED;
889 pArray[8].Value.setValue( &bRed, cppu::UnoType<bool>::get() );
890 pArray[9].Name = PROPERTYNAME_THOUS;
891 pArray[9].Value.setValue( &bThousand, cppu::UnoType<bool>::get() );
892 pArray[10].Name = PROPERTYNAME_CURRSYM;
893 pArray[10].Value <<= aSymbol;
894 pArray[11].Name = PROPERTYNAME_CURREXT;
895 pArray[11].Value <<= aExt;
896 pArray[12].Name = PROPERTYNAME_CURRABB;
897 pArray[12].Value <<= aAbb;
899 return aSeq;
901 else
902 throw uno::RuntimeException();
905 void SAL_CALL SvNumberFormatObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& )
906 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
907 lang::IllegalArgumentException, lang::WrappedTargetException,
908 uno::RuntimeException, std::exception)
910 throw beans::UnknownPropertyException(); // Everything is read-only
913 // XServiceInfo
915 OUString SAL_CALL SvNumberFormatObj::getImplementationName()
916 throw(uno::RuntimeException, std::exception)
918 return OUString("SvNumberFormatObj");
921 sal_Bool SAL_CALL SvNumberFormatObj::supportsService( const OUString& ServiceName )
922 throw(uno::RuntimeException, std::exception)
924 return cppu::supportsService( this, ServiceName );
927 uno::Sequence<OUString> SAL_CALL SvNumberFormatObj::getSupportedServiceNames()
928 throw(uno::RuntimeException, std::exception)
930 uno::Sequence<OUString> aRet(1);
931 OUString* pArray = aRet.getArray();
932 pArray[0] = "com.sun.star.util.NumberFormatProperties";
933 return aRet;
936 SvNumberFormatSettingsObj::SvNumberFormatSettingsObj( SvNumberFormatsSupplierObj& rParent, const ::comphelper::SharedMutex& _rMutex )
937 :rSupplier( rParent )
938 ,m_aMutex( _rMutex )
940 rSupplier.acquire();
943 SvNumberFormatSettingsObj::~SvNumberFormatSettingsObj()
945 rSupplier.release();
948 // XPropertySet
950 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatSettingsObj::getPropertySetInfo()
951 throw(uno::RuntimeException, std::exception)
953 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
954 static uno::Reference<beans::XPropertySetInfo> aRef =
955 new SfxItemPropertySetInfo( lcl_GetNumberSettingsPropertyMap() );
956 return aRef;
959 void SAL_CALL SvNumberFormatSettingsObj::setPropertyValue( const OUString& aPropertyName,
960 const uno::Any& aValue )
961 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
962 lang::IllegalArgumentException, lang::WrappedTargetException,
963 uno::RuntimeException, std::exception)
965 ::osl::MutexGuard aGuard( m_aMutex );
967 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
968 if (pFormatter)
970 if (aPropertyName == PROPERTYNAME_NOZERO)
972 // operator >>= shouldn't be used for bool (?)
973 if ( aValue.getValueTypeClass() == uno::TypeClass_BOOLEAN )
974 pFormatter->SetNoZero( *static_cast<sal_Bool const *>(aValue.getValue()) );
976 else if (aPropertyName == PROPERTYNAME_NULLDATE)
978 util::Date aDate;
979 if ( aValue >>= aDate )
980 pFormatter->ChangeNullDate( aDate.Day, aDate.Month, aDate.Year );
982 else if (aPropertyName == PROPERTYNAME_STDDEC)
984 sal_Int16 nInt16 = sal_Int16();
985 if ( aValue >>= nInt16 )
986 pFormatter->ChangeStandardPrec( nInt16 );
988 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
990 sal_Int16 nInt16 = sal_Int16();
991 if ( aValue >>= nInt16 )
992 pFormatter->SetYear2000( nInt16 );
994 else
995 throw beans::UnknownPropertyException();
997 else
998 throw uno::RuntimeException();
1001 uno::Any SAL_CALL SvNumberFormatSettingsObj::getPropertyValue( const OUString& aPropertyName )
1002 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
1003 uno::RuntimeException, std::exception)
1005 ::osl::MutexGuard aGuard( m_aMutex );
1007 uno::Any aRet;
1008 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
1009 if (pFormatter)
1011 if (aPropertyName == PROPERTYNAME_NOZERO)
1013 sal_Bool bNoZero = pFormatter->GetNoZero();
1014 aRet.setValue( &bNoZero, cppu::UnoType<bool>::get() );
1016 else if (aPropertyName == PROPERTYNAME_NULLDATE)
1018 Date* pDate = pFormatter->GetNullDate();
1019 if (pDate)
1021 aRet <<= pDate->GetUNODate();
1024 else if (aPropertyName == PROPERTYNAME_STDDEC)
1025 aRet <<= (sal_Int16)( pFormatter->GetStandardPrec() );
1026 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
1027 aRet <<= (sal_Int16)( pFormatter->GetYear2000() );
1028 else
1029 throw beans::UnknownPropertyException();
1031 else
1032 throw uno::RuntimeException();
1034 return aRet;
1037 void SAL_CALL SvNumberFormatSettingsObj::addPropertyChangeListener( const OUString&,
1038 const uno::Reference<beans::XPropertyChangeListener>&)
1039 throw(beans::UnknownPropertyException,
1040 lang::WrappedTargetException, uno::RuntimeException, std::exception)
1042 OSL_FAIL("not implemented");
1045 void SAL_CALL SvNumberFormatSettingsObj::removePropertyChangeListener( const OUString&,
1046 const uno::Reference<beans::XPropertyChangeListener>&)
1047 throw(beans::UnknownPropertyException,
1048 lang::WrappedTargetException, uno::RuntimeException, std::exception)
1050 OSL_FAIL("not implemented");
1053 void SAL_CALL SvNumberFormatSettingsObj::addVetoableChangeListener( const OUString&,
1054 const uno::Reference<beans::XVetoableChangeListener>&)
1055 throw(beans::UnknownPropertyException,
1056 lang::WrappedTargetException, uno::RuntimeException, std::exception)
1058 OSL_FAIL("not implemented");
1061 void SAL_CALL SvNumberFormatSettingsObj::removeVetoableChangeListener( const OUString&,
1062 const uno::Reference<beans::XVetoableChangeListener>&)
1063 throw(beans::UnknownPropertyException,
1064 lang::WrappedTargetException, uno::RuntimeException, std::exception)
1066 OSL_FAIL("not implemented");
1069 // XServiceInfo
1071 OUString SAL_CALL SvNumberFormatSettingsObj::getImplementationName()
1072 throw(uno::RuntimeException, std::exception)
1074 return OUString("SvNumberFormatSettingsObj");
1077 sal_Bool SAL_CALL SvNumberFormatSettingsObj::supportsService( const OUString& ServiceName )
1078 throw(uno::RuntimeException, std::exception)
1080 return cppu::supportsService( this, ServiceName );
1083 uno::Sequence<OUString> SAL_CALL SvNumberFormatSettingsObj::getSupportedServiceNames()
1084 throw(uno::RuntimeException, std::exception)
1086 uno::Sequence<OUString> aRet(1);
1087 OUString* pArray = aRet.getArray();
1088 pArray[0] = "com.sun.star.util.NumberFormatSettings";
1089 return aRet;
1093 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
1094 com_sun_star_uno_util_numbers_SvNumberFormatterServiceObject_get_implementation(::com::sun::star::uno::XComponentContext*,
1095 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
1097 return cppu::acquire(new SvNumberFormatterServiceObj());
1102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */