LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / svl / source / numbers / numfmuno.cxx
blob84812f9c5eef4a564c7aa34a95c5b0a2320fc86a
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 <o3tl/any.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/util/MalformedNumberFormatException.hpp>
28 #include <com/sun/star/util/NotNumericException.hpp>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <comphelper/propertysequence.hxx>
31 #include <cppuhelper/supportsservice.hxx>
33 #include "numfmuno.hxx"
34 #include <svl/numformat.hxx>
35 #include <svl/numuno.hxx>
36 #include <svl/zforlist.hxx>
37 #include <svl/zformat.hxx>
38 #include <svl/itemprop.hxx>
40 using namespace com::sun::star;
42 constexpr OUStringLiteral PROPERTYNAME_FMTSTR = u"FormatString";
43 constexpr OUStringLiteral PROPERTYNAME_LOCALE = u"Locale";
44 constexpr OUStringLiteral PROPERTYNAME_TYPE = u"Type";
45 constexpr OUStringLiteral PROPERTYNAME_COMMENT = u"Comment";
46 constexpr OUStringLiteral PROPERTYNAME_CURREXT = u"CurrencyExtension";
47 constexpr OUStringLiteral PROPERTYNAME_CURRSYM = u"CurrencySymbol";
48 constexpr OUStringLiteral PROPERTYNAME_CURRABB = u"CurrencyAbbreviation";
49 constexpr OUStringLiteral PROPERTYNAME_DECIMALS = u"Decimals";
50 constexpr OUStringLiteral PROPERTYNAME_LEADING = u"LeadingZeros";
51 constexpr OUStringLiteral PROPERTYNAME_NEGRED = u"NegativeRed";
52 constexpr OUStringLiteral PROPERTYNAME_STDFORM = u"StandardFormat";
53 constexpr OUStringLiteral PROPERTYNAME_THOUS = u"ThousandsSeparator";
54 constexpr OUStringLiteral PROPERTYNAME_USERDEF = u"UserDefined";
56 constexpr OUStringLiteral PROPERTYNAME_NOZERO = u"NoZero";
57 constexpr OUStringLiteral PROPERTYNAME_NULLDATE = u"NullDate";
58 constexpr OUStringLiteral PROPERTYNAME_STDDEC = u"StandardDecimals";
59 constexpr OUStringLiteral PROPERTYNAME_TWODIGIT = u"TwoDigitDateStart";
61 // All without a Which-ID, Map only for PropertySetInfo
63 static const SfxItemPropertyMapEntry* lcl_GetNumberFormatPropertyMap()
65 static const SfxItemPropertyMapEntry aNumberFormatPropertyMap_Impl[] =
67 {PROPERTYNAME_FMTSTR, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
68 {PROPERTYNAME_LOCALE, 0, cppu::UnoType<lang::Locale>::get(),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
69 {PROPERTYNAME_TYPE, 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
70 {PROPERTYNAME_COMMENT, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
71 {PROPERTYNAME_CURREXT, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
72 {PROPERTYNAME_CURRSYM, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
73 {PROPERTYNAME_DECIMALS, 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
74 {PROPERTYNAME_LEADING, 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
75 {PROPERTYNAME_NEGRED, 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
76 {PROPERTYNAME_STDFORM, 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
77 {PROPERTYNAME_THOUS, 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
78 {PROPERTYNAME_USERDEF, 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
79 {PROPERTYNAME_CURRABB, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
80 { u"", 0, css::uno::Type(), 0, 0 }
82 return aNumberFormatPropertyMap_Impl;
85 static const SfxItemPropertyMapEntry* lcl_GetNumberSettingsPropertyMap()
87 static const SfxItemPropertyMapEntry aNumberSettingsPropertyMap_Impl[] =
89 {PROPERTYNAME_NOZERO, 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND, 0},
90 {PROPERTYNAME_NULLDATE, 0, cppu::UnoType<util::Date>::get(), beans::PropertyAttribute::BOUND, 0},
91 {PROPERTYNAME_STDDEC, 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
92 {PROPERTYNAME_TWODIGIT, 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
93 { u"", 0, css::uno::Type(), 0, 0 }
95 return aNumberSettingsPropertyMap_Impl;
98 static LanguageType lcl_GetLanguage( const lang::Locale& rLocale )
100 LanguageType eRet = LanguageTag::convertToLanguageTypeWithFallback( rLocale, false);
101 if ( eRet == LANGUAGE_NONE )
102 eRet = LANGUAGE_SYSTEM; //! or throw an exception?
104 return eRet;
107 SvNumberFormatterServiceObj::SvNumberFormatterServiceObj()
111 SvNumberFormatterServiceObj::~SvNumberFormatterServiceObj()
115 // XNumberFormatter
117 void SAL_CALL SvNumberFormatterServiceObj::attachNumberFormatsSupplier( const uno::Reference<util::XNumberFormatsSupplier>& _xSupplier )
119 ::rtl::Reference< SvNumberFormatsSupplierObj > xAutoReleaseOld;
121 // SYNCHRONIZED ->
123 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
125 SvNumberFormatsSupplierObj* pNew = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>( _xSupplier );
126 if (!pNew)
127 throw uno::RuntimeException(); // wrong object
129 xAutoReleaseOld = xSupplier;
131 xSupplier = pNew;
132 m_aMutex = xSupplier->getSharedMutex();
134 // <- SYNCHRONIZED
137 uno::Reference<util::XNumberFormatsSupplier> SAL_CALL SvNumberFormatterServiceObj::getNumberFormatsSupplier()
139 ::osl::MutexGuard aGuard( m_aMutex );
140 return xSupplier;
143 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::detectNumberFormat( sal_Int32 nKey, const OUString& aString )
145 ::osl::MutexGuard aGuard( m_aMutex );
147 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
148 if (!pFormatter)
149 throw uno::RuntimeException();
151 sal_uInt32 nUKey = nKey;
152 double fValue = 0.0;
153 if ( !pFormatter->IsNumberFormat(aString, nUKey, fValue) )
154 throw util::NotNumericException();
156 return nUKey;
159 double SAL_CALL SvNumberFormatterServiceObj::convertStringToNumber( sal_Int32 nKey, const OUString& aString )
161 ::osl::MutexGuard aGuard( m_aMutex );
163 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
164 if (!pFormatter)
165 throw uno::RuntimeException();
167 sal_uInt32 nUKey = nKey;
168 double fValue = 0.0;
169 if ( !pFormatter->IsNumberFormat(aString, nUKey, fValue) )
170 throw util::NotNumericException();
172 return fValue;
175 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToString( sal_Int32 nKey, double fValue )
177 ::osl::MutexGuard aGuard( m_aMutex );
179 OUString aRet;
180 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
181 if (!pFormatter)
182 throw uno::RuntimeException();
184 const Color* pColor = nullptr;
185 pFormatter->GetOutputString(fValue, nKey, aRet, &pColor);
187 return aRet;
190 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::queryColorForNumber( sal_Int32 nKey,
191 double fValue,
192 sal_Int32 aDefaultColor )
194 ::osl::MutexGuard aGuard( m_aMutex );
196 util::Color nRet = aDefaultColor; // color = sal_Int32
197 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
198 if (!pFormatter)
199 throw uno::RuntimeException();
201 OUString aStr;
202 const Color* pColor = nullptr;
203 pFormatter->GetOutputString(fValue, nKey, aStr, &pColor);
204 if (pColor)
205 nRet = sal_uInt32(*pColor);
206 // Else keep Default
208 return nRet;
211 OUString SAL_CALL SvNumberFormatterServiceObj::formatString( sal_Int32 nKey,
212 const OUString& aString )
214 ::osl::MutexGuard aGuard( m_aMutex );
216 OUString aRet;
217 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
218 if (!pFormatter)
220 throw uno::RuntimeException();
223 const Color* pColor = nullptr;
224 pFormatter->GetOutputString(aString, nKey, aRet, &pColor);
226 return aRet;
229 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::queryColorForString( sal_Int32 nKey,
230 const OUString& aString,
231 sal_Int32 aDefaultColor )
233 ::osl::MutexGuard aGuard( m_aMutex );
235 util::Color nRet = aDefaultColor; // color = sal_Int32
236 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
237 if (!pFormatter)
239 throw uno::RuntimeException();
242 OUString aStr;
243 const Color* pColor = nullptr;
244 pFormatter->GetOutputString(aString, nKey, aStr, &pColor);
245 if (pColor)
247 nRet = sal_uInt32(*pColor);
249 // Else keep Default
251 return nRet;
254 OUString SAL_CALL SvNumberFormatterServiceObj::getInputString( sal_Int32 nKey, double fValue )
256 ::osl::MutexGuard aGuard( m_aMutex );
258 OUString aRet;
259 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
260 if (!pFormatter)
261 throw uno::RuntimeException();
263 pFormatter->GetInputLineString(fValue, nKey, aRet);
265 return aRet;
268 // XNumberFormatPreviewer
270 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToPreviewString( const OUString& aFormat,
271 double fValue,
272 const lang::Locale& nLocale,
273 sal_Bool bAllowEnglish )
275 ::osl::MutexGuard aGuard( m_aMutex );
277 OUString aRet;
278 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
279 if (!pFormatter)
280 throw uno::RuntimeException();
282 LanguageType eLang = lcl_GetLanguage( nLocale );
283 const Color* pColor = nullptr;
285 bool bOk;
286 if ( bAllowEnglish )
287 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aRet, &pColor, eLang );
288 else
289 bOk = pFormatter->GetPreviewString( aFormat, fValue, aRet, &pColor, eLang );
291 if (!bOk)
292 throw util::MalformedNumberFormatException();
294 return aRet;
297 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::queryPreviewColorForNumber( const OUString& aFormat,
298 double fValue,
299 const lang::Locale& nLocale,
300 sal_Bool bAllowEnglish,
301 sal_Int32 aDefaultColor )
303 ::osl::MutexGuard aGuard( m_aMutex );
305 util::Color nRet = aDefaultColor; // color = sal_Int32
306 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
307 if (!pFormatter)
308 throw uno::RuntimeException();
310 OUString aOutString;
311 LanguageType eLang = lcl_GetLanguage( nLocale );
312 const Color* pColor = nullptr;
314 bool bOk;
315 if ( bAllowEnglish )
316 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aOutString, &pColor, eLang );
317 else
318 bOk = pFormatter->GetPreviewString( aFormat, fValue, aOutString, &pColor, eLang );
320 if (!bOk)
321 throw util::MalformedNumberFormatException();
323 if (pColor)
324 nRet = sal_uInt32(*pColor);
325 // Else keep Default
327 return nRet;
330 // XServiceInfo
332 OUString SAL_CALL SvNumberFormatterServiceObj::getImplementationName()
334 return "com.sun.star.uno.util.numbers.SvNumberFormatterServiceObject";
337 sal_Bool SAL_CALL SvNumberFormatterServiceObj::supportsService( const OUString& ServiceName )
339 return cppu::supportsService( this, ServiceName );
342 uno::Sequence<OUString> SAL_CALL SvNumberFormatterServiceObj::getSupportedServiceNames()
344 return { "com.sun.star.util.NumberFormatter" };
347 SvNumberFormatsObj::SvNumberFormatsObj( SvNumberFormatsSupplierObj& _rParent, ::comphelper::SharedMutex const & _rMutex )
348 :m_xSupplier( &_rParent )
349 ,m_aMutex( _rMutex )
353 SvNumberFormatsObj::~SvNumberFormatsObj()
357 // XNumberFormats
359 uno::Reference<beans::XPropertySet> SAL_CALL SvNumberFormatsObj::getByKey( sal_Int32 nKey )
361 ::osl::MutexGuard aGuard( m_aMutex );
363 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
364 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
365 if (!pFormat)
366 throw uno::RuntimeException();
368 return new SvNumberFormatObj( *m_xSupplier, nKey, m_aMutex );
371 uno::Sequence<sal_Int32> SAL_CALL SvNumberFormatsObj::queryKeys( sal_Int16 nType,
372 const lang::Locale& nLocale,
373 sal_Bool bCreate )
375 ::osl::MutexGuard aGuard( m_aMutex );
377 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
378 if ( !pFormatter )
379 throw uno::RuntimeException();
381 sal_uInt32 nIndex = 0;
382 LanguageType eLang = lcl_GetLanguage( nLocale );
383 SvNumberFormatTable& rTable = bCreate ?
384 pFormatter->ChangeCL( static_cast<SvNumFormatType>(nType), nIndex, eLang ) :
385 pFormatter->GetEntryTable( static_cast<SvNumFormatType>(nType), nIndex, eLang );
386 sal_uInt32 nCount = rTable.size();
387 uno::Sequence<sal_Int32> aSeq(nCount);
388 sal_Int32* pAry = aSeq.getArray();
389 sal_uInt32 i=0;
390 for (const auto& rEntry : rTable)
392 pAry[i] = rEntry.first;
393 ++i;
395 return aSeq;
398 sal_Int32 SAL_CALL SvNumberFormatsObj::queryKey( const OUString& aFormat,
399 const lang::Locale& nLocale,
400 sal_Bool bScan )
402 ::osl::MutexGuard aGuard( m_aMutex );
404 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
405 if (!pFormatter)
406 throw uno::RuntimeException();
408 LanguageType eLang = lcl_GetLanguage( nLocale );
409 if (bScan)
411 //! FIXME: Something still needs to happen here ...
413 sal_Int32 nRet = pFormatter->GetEntryKey( aFormat, eLang );
414 return nRet;
417 sal_Int32 SAL_CALL SvNumberFormatsObj::addNew( const OUString& aFormat,
418 const lang::Locale& nLocale )
420 ::osl::MutexGuard aGuard( m_aMutex );
422 sal_Int32 nRet = 0;
423 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
424 if (!pFormatter)
425 throw uno::RuntimeException();
427 OUString aFormStr = aFormat;
428 LanguageType eLang = lcl_GetLanguage( nLocale );
429 sal_uInt32 nKey = 0;
430 sal_Int32 nCheckPos = 0;
431 SvNumFormatType nType = SvNumFormatType::ALL;
432 bool bOk = pFormatter->PutEntry( aFormStr, nCheckPos, nType, nKey, eLang );
433 if (bOk)
434 nRet = nKey;
435 else if (nCheckPos)
437 throw util::MalformedNumberFormatException(); // Invalid Format
439 else
440 throw uno::RuntimeException(); // Other error (e.g. already added)
442 return nRet;
445 sal_Int32 SAL_CALL SvNumberFormatsObj::addNewConverted( const OUString& aFormat,
446 const lang::Locale& nLocale,
447 const lang::Locale& nNewLocale )
449 ::osl::MutexGuard aGuard( m_aMutex );
451 sal_Int32 nRet = 0;
452 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
453 if (!pFormatter)
454 throw uno::RuntimeException();
456 OUString aFormStr = aFormat;
457 LanguageType eLang = lcl_GetLanguage( nLocale );
458 LanguageType eNewLang = lcl_GetLanguage( nNewLocale );
459 sal_uInt32 nKey = 0;
460 sal_Int32 nCheckPos = 0;
461 SvNumFormatType nType = SvNumFormatType::ALL;
462 // This is used also when reading OOXML documents, there's no indicator
463 // whether to convert date particle order as well, so don't. See tdf#119013
464 bool bOk = pFormatter->PutandConvertEntry( aFormStr, nCheckPos, nType, nKey, eLang, eNewLang, false);
465 if (bOk || nKey > 0)
466 nRet = nKey;
467 else if (nCheckPos)
469 throw util::MalformedNumberFormatException(); // Invalid format
471 else
472 throw uno::RuntimeException(); // Other error (e.g. already added)
474 return nRet;
477 void SAL_CALL SvNumberFormatsObj::removeByKey( sal_Int32 nKey )
479 ::osl::MutexGuard aGuard( m_aMutex );
480 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
482 if (pFormatter)
484 pFormatter->DeleteEntry(nKey);
488 OUString SAL_CALL SvNumberFormatsObj::generateFormat( sal_Int32 nBaseKey,
489 const lang::Locale& nLocale,
490 sal_Bool bThousands,
491 sal_Bool bRed, sal_Int16 nDecimals,
492 sal_Int16 nLeading )
494 ::osl::MutexGuard aGuard( m_aMutex );
496 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
497 if (!pFormatter)
498 throw uno::RuntimeException();
500 LanguageType eLang = lcl_GetLanguage( nLocale );
501 OUString aRet = pFormatter->GenerateFormat(nBaseKey, eLang, bThousands, bRed, nDecimals, nLeading);
502 return aRet;
505 // XNumberFormatTypes
507 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardIndex( const lang::Locale& nLocale )
509 ::osl::MutexGuard aGuard( m_aMutex );
511 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
512 if (!pFormatter)
513 throw uno::RuntimeException();
515 LanguageType eLang = lcl_GetLanguage( nLocale );
516 sal_Int32 nRet = pFormatter->GetStandardIndex(eLang);
517 return nRet;
520 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardFormat( sal_Int16 nType, const lang::Locale& nLocale )
522 ::osl::MutexGuard aGuard( m_aMutex );
524 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
525 if (!pFormatter)
526 throw uno::RuntimeException();
528 LanguageType eLang = lcl_GetLanguage( nLocale );
529 // Mask out "defined" bit, so type from an existing number format
530 // can directly be used for getStandardFormat
531 SvNumFormatType nType2 = static_cast<SvNumFormatType>(nType);
532 nType2 &= ~SvNumFormatType::DEFINED;
533 sal_Int32 nRet = pFormatter->GetStandardFormat(nType2, eLang);
534 return nRet;
537 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatIndex( sal_Int16 nIndex, const lang::Locale& nLocale )
539 ::osl::MutexGuard aGuard( m_aMutex );
541 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
542 if (!pFormatter)
543 throw uno::RuntimeException();
545 LanguageType eLang = lcl_GetLanguage( nLocale );
546 sal_Int32 nRet = pFormatter->GetFormatIndex( static_cast<NfIndexTableOffset>(nIndex), eLang );
547 return nRet;
550 sal_Bool SAL_CALL SvNumberFormatsObj::isTypeCompatible( sal_Int16 nOldType, sal_Int16 nNewType )
552 ::osl::MutexGuard aGuard( m_aMutex );
554 return SvNumberFormatter::IsCompatible( static_cast<SvNumFormatType>(nOldType), static_cast<SvNumFormatType>(nNewType) );
557 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatForLocale( sal_Int32 nKey, const lang::Locale& nLocale )
559 ::osl::MutexGuard aGuard( m_aMutex );
561 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
562 if (!pFormatter)
563 throw uno::RuntimeException();
565 LanguageType eLang = lcl_GetLanguage( nLocale );
566 sal_Int32 nRet = pFormatter->GetFormatForLanguageIfBuiltIn(nKey, eLang);
567 return nRet;
570 // XServiceInfo
572 OUString SAL_CALL SvNumberFormatsObj::getImplementationName()
574 return "SvNumberFormatsObj";
577 sal_Bool SAL_CALL SvNumberFormatsObj::supportsService( const OUString& ServiceName )
579 return cppu::supportsService( this, ServiceName );
582 uno::Sequence<OUString> SAL_CALL SvNumberFormatsObj::getSupportedServiceNames()
584 return { "com.sun.star.util.NumberFormats" };
587 SvNumberFormatObj::SvNumberFormatObj( SvNumberFormatsSupplierObj& rParent, sal_uLong nK, const ::comphelper::SharedMutex& _rMutex )
588 :m_xSupplier( &rParent )
589 ,nKey( nK )
590 ,m_aMutex( _rMutex )
594 SvNumberFormatObj::~SvNumberFormatObj()
598 // XPropertySet
600 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatObj::getPropertySetInfo()
602 static uno::Reference<beans::XPropertySetInfo> aRef =
603 new SfxItemPropertySetInfo( lcl_GetNumberFormatPropertyMap() );
604 return aRef;
607 void SAL_CALL SvNumberFormatObj::setPropertyValue( const OUString&,
608 const uno::Any& )
610 throw beans::UnknownPropertyException(); // Everything is read-only
613 uno::Any SAL_CALL SvNumberFormatObj::getPropertyValue( const OUString& aPropertyName )
615 ::osl::MutexGuard aGuard( m_aMutex );
617 uno::Any aRet;
618 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
619 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
620 if (!pFormat)
621 throw uno::RuntimeException();
623 bool bThousand, bRed;
624 sal_uInt16 nDecimals, nLeading;
626 if (aPropertyName == PROPERTYNAME_FMTSTR)
628 aRet <<= pFormat->GetFormatstring();
630 else if (aPropertyName == PROPERTYNAME_LOCALE)
632 lang::Locale aLocale( LanguageTag::convertToLocale( pFormat->GetLanguage(), false));
633 aRet <<= aLocale;
635 else if (aPropertyName == PROPERTYNAME_TYPE)
637 aRet <<= static_cast<sal_Int16>( pFormat->GetType() );
639 else if (aPropertyName == PROPERTYNAME_COMMENT)
641 aRet <<= pFormat->GetComment();
643 else if (aPropertyName == PROPERTYNAME_STDFORM)
645 //! Pass through SvNumberformat Member bStandard?
646 aRet <<= ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
648 else if (aPropertyName == PROPERTYNAME_USERDEF)
650 aRet <<= bool( pFormat->GetType() & SvNumFormatType::DEFINED );
652 else if (aPropertyName == PROPERTYNAME_DECIMALS)
654 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
655 aRet <<= static_cast<sal_Int16>(nDecimals);
657 else if (aPropertyName == PROPERTYNAME_LEADING)
659 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
660 aRet <<= static_cast<sal_Int16>(nLeading);
662 else if (aPropertyName == PROPERTYNAME_NEGRED)
664 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
665 aRet <<= bRed;
667 else if (aPropertyName == PROPERTYNAME_THOUS)
669 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
670 aRet <<= bThousand;
672 else if (aPropertyName == PROPERTYNAME_CURRSYM)
674 OUString aSymbol, aExt;
675 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
676 aRet <<= aSymbol;
678 else if (aPropertyName == PROPERTYNAME_CURREXT)
680 OUString aSymbol, aExt;
681 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
682 aRet <<= aExt;
684 else if (aPropertyName == PROPERTYNAME_CURRABB)
686 OUString aSymbol, aExt;
687 bool bBank = false;
688 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
689 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
690 aSymbol, aExt, pFormat->GetLanguage() );
691 if ( pCurr )
692 aRet <<= pCurr->GetBankSymbol();
693 else
694 aRet <<= OUString();
696 else
697 throw beans::UnknownPropertyException(aPropertyName);
699 return aRet;
702 void SAL_CALL SvNumberFormatObj::addPropertyChangeListener( const OUString&,
703 const uno::Reference<beans::XPropertyChangeListener>&)
705 OSL_FAIL("not implemented");
708 void SAL_CALL SvNumberFormatObj::removePropertyChangeListener( const OUString&,
709 const uno::Reference<beans::XPropertyChangeListener>&)
711 OSL_FAIL("not implemented");
714 void SAL_CALL SvNumberFormatObj::addVetoableChangeListener( const OUString&,
715 const uno::Reference<beans::XVetoableChangeListener>&)
717 OSL_FAIL("not implemented");
720 void SAL_CALL SvNumberFormatObj::removeVetoableChangeListener( const OUString&,
721 const uno::Reference<beans::XVetoableChangeListener>&)
723 OSL_FAIL("not implemented");
726 // XPropertyAccess
728 uno::Sequence<beans::PropertyValue> SAL_CALL SvNumberFormatObj::getPropertyValues()
730 ::osl::MutexGuard aGuard( m_aMutex );
732 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
733 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
734 if (!pFormat)
735 throw uno::RuntimeException();
737 OUString aSymbol, aExt;
738 OUString aAbb;
739 bool bBank = false;
740 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
741 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
742 aSymbol, aExt, pFormat->GetLanguage() );
743 if ( pCurr )
744 aAbb = pCurr->GetBankSymbol();
746 OUString aFmtStr = pFormat->GetFormatstring();
747 OUString aComment = pFormat->GetComment();
748 bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
749 //! Pass through SvNumberformat Member bStandard?
750 bool bUserDef( pFormat->GetType() & SvNumFormatType::DEFINED );
751 bool bThousand, bRed;
752 sal_uInt16 nDecimals, nLeading;
753 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
754 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
756 uno::Sequence<beans::PropertyValue> aSeq( comphelper::InitPropertySequence({
757 { PROPERTYNAME_FMTSTR, uno::Any(aFmtStr) },
758 { PROPERTYNAME_LOCALE, uno::Any(aLocale) },
759 { PROPERTYNAME_TYPE, uno::Any(sal_Int16( pFormat->GetType() )) },
760 { PROPERTYNAME_COMMENT, uno::Any(aComment) },
761 { PROPERTYNAME_STDFORM, uno::Any(bStandard) },
762 { PROPERTYNAME_USERDEF, uno::Any(bUserDef) },
763 { PROPERTYNAME_DECIMALS, uno::Any(sal_Int16( nDecimals )) },
764 { PROPERTYNAME_LEADING, uno::Any(sal_Int16( nLeading )) },
765 { PROPERTYNAME_NEGRED, uno::Any(bRed) },
766 { PROPERTYNAME_THOUS, uno::Any(bThousand) },
767 { PROPERTYNAME_CURRSYM, uno::Any(aSymbol) },
768 { PROPERTYNAME_CURREXT, uno::Any(aExt) },
769 { PROPERTYNAME_CURRABB, uno::Any(aAbb) }
770 }));
772 return aSeq;
775 void SAL_CALL SvNumberFormatObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& )
777 throw beans::UnknownPropertyException(); // Everything is read-only
780 // XServiceInfo
782 OUString SAL_CALL SvNumberFormatObj::getImplementationName()
784 return "SvNumberFormatObj";
787 sal_Bool SAL_CALL SvNumberFormatObj::supportsService( const OUString& ServiceName )
789 return cppu::supportsService( this, ServiceName );
792 uno::Sequence<OUString> SAL_CALL SvNumberFormatObj::getSupportedServiceNames()
794 return { "com.sun.star.util.NumberFormatProperties" };
797 SvNumberFormatSettingsObj::SvNumberFormatSettingsObj( SvNumberFormatsSupplierObj& rParent, const ::comphelper::SharedMutex& _rMutex )
798 :m_xSupplier( &rParent )
799 ,m_aMutex( _rMutex )
803 SvNumberFormatSettingsObj::~SvNumberFormatSettingsObj()
807 // XPropertySet
809 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatSettingsObj::getPropertySetInfo()
811 static uno::Reference<beans::XPropertySetInfo> aRef =
812 new SfxItemPropertySetInfo( lcl_GetNumberSettingsPropertyMap() );
813 return aRef;
816 void SAL_CALL SvNumberFormatSettingsObj::setPropertyValue( const OUString& aPropertyName,
817 const uno::Any& aValue )
819 ::osl::MutexGuard aGuard( m_aMutex );
821 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
822 if (!pFormatter)
823 throw uno::RuntimeException();
825 if (aPropertyName == PROPERTYNAME_NOZERO)
827 // operator >>= shouldn't be used for bool (?)
828 if ( auto b = o3tl::tryAccess<bool>(aValue) )
829 pFormatter->SetNoZero( *b );
831 else if (aPropertyName == PROPERTYNAME_NULLDATE)
833 util::Date aDate;
834 if ( aValue >>= aDate )
835 pFormatter->ChangeNullDate( aDate.Day, aDate.Month, aDate.Year );
837 else if (aPropertyName == PROPERTYNAME_STDDEC)
839 sal_Int16 nInt16 = sal_Int16();
840 if ( aValue >>= nInt16 )
841 pFormatter->ChangeStandardPrec( nInt16 );
843 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
845 sal_Int16 nInt16 = sal_Int16();
846 if ( aValue >>= nInt16 )
847 pFormatter->SetYear2000( nInt16 );
849 else
850 throw beans::UnknownPropertyException(aPropertyName);
854 uno::Any SAL_CALL SvNumberFormatSettingsObj::getPropertyValue( const OUString& aPropertyName )
856 ::osl::MutexGuard aGuard( m_aMutex );
858 uno::Any aRet;
859 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
860 if (!pFormatter)
861 throw uno::RuntimeException();
863 if (aPropertyName == PROPERTYNAME_NOZERO)
865 aRet <<= pFormatter->GetNoZero();
867 else if (aPropertyName == PROPERTYNAME_NULLDATE)
869 const Date& rDate = pFormatter->GetNullDate();
870 aRet <<= rDate.GetUNODate();
872 else if (aPropertyName == PROPERTYNAME_STDDEC)
873 aRet <<= static_cast<sal_Int16>( pFormatter->GetStandardPrec() );
874 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
875 aRet <<= static_cast<sal_Int16>( pFormatter->GetYear2000() );
876 else
877 throw beans::UnknownPropertyException(aPropertyName);
879 return aRet;
882 void SAL_CALL SvNumberFormatSettingsObj::addPropertyChangeListener( const OUString&,
883 const uno::Reference<beans::XPropertyChangeListener>&)
885 OSL_FAIL("not implemented");
888 void SAL_CALL SvNumberFormatSettingsObj::removePropertyChangeListener( const OUString&,
889 const uno::Reference<beans::XPropertyChangeListener>&)
891 OSL_FAIL("not implemented");
894 void SAL_CALL SvNumberFormatSettingsObj::addVetoableChangeListener( const OUString&,
895 const uno::Reference<beans::XVetoableChangeListener>&)
897 OSL_FAIL("not implemented");
900 void SAL_CALL SvNumberFormatSettingsObj::removeVetoableChangeListener( const OUString&,
901 const uno::Reference<beans::XVetoableChangeListener>&)
903 OSL_FAIL("not implemented");
906 // XServiceInfo
908 OUString SAL_CALL SvNumberFormatSettingsObj::getImplementationName()
910 return "SvNumberFormatSettingsObj";
913 sal_Bool SAL_CALL SvNumberFormatSettingsObj::supportsService( const OUString& ServiceName )
915 return cppu::supportsService( this, ServiceName );
918 uno::Sequence<OUString> SAL_CALL SvNumberFormatSettingsObj::getSupportedServiceNames()
920 return { "com.sun.star.util.NumberFormatSettings" };
924 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
925 com_sun_star_uno_util_numbers_SvNumberFormatterServiceObject_get_implementation(css::uno::XComponentContext*,
926 css::uno::Sequence<css::uno::Any> const &)
928 return cppu::acquire(new SvNumberFormatterServiceObj());
932 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */