tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / unotools / source / config / syslocaleoptions.cxx
blobc9f2df16352243ad5650aa08eb3e9a7701b1b9f3
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 <com/sun/star/uno/Sequence.hxx>
21 #include <comphelper/configuration.hxx>
22 #include <sal/log.hxx>
23 #include <i18nlangtag/mslangid.hxx>
24 #include <i18nlangtag/languagetag.hxx>
25 #include <tools/debug.hxx>
26 #include <tools/link.hxx>
27 #include <unotools/syslocaleoptions.hxx>
28 #include <unotools/configmgr.hxx>
29 #include <unotools/configitem.hxx>
30 #include <com/sun/star/uno/Any.hxx>
32 #include "itemholder1.hxx"
34 #define CFG_READONLY_DEFAULT false
36 using namespace osl;
37 using namespace utl;
38 using namespace com::sun::star::uno;
40 namespace
42 std::weak_ptr<SvtSysLocaleOptions_Impl> g_pSysLocaleOptions;
43 Link<LinkParamNone*,void> g_CurrencyChangeLink;
45 Mutex& GetMutex()
47 // #i77768# Due to a static reference in the toolkit lib
48 // we need a mutex that lives longer than the svl library.
49 // Otherwise the dtor would use a destructed mutex!!
50 static Mutex* persistentMutex(new Mutex);
52 return *persistentMutex;
57 class SvtSysLocaleOptions_Impl : public utl::ConfigItem
59 LanguageTag m_aRealLocale;
60 LanguageTag m_aRealUILocale;
61 OUString m_aLocaleString; // en-US or de-DE or empty for SYSTEM
62 OUString m_aUILocaleString; // en-US or de-DE or empty for SYSTEM
63 OUString m_aCurrencyString; // USD-en-US or EUR-de-DE
64 OUString m_aDatePatternsString; // "Y-M-D;M-D"
65 bool m_bDecimalSeparator; //use decimal separator same as locale
66 bool m_bIgnoreLanguageChange; //OS language change doesn't affect LO document language
68 bool m_bROLocale;
69 bool m_bROUILocale;
70 bool m_bROCurrency;
71 bool m_bRODatePatterns;
72 bool m_bRODecimalSeparator;
73 bool m_bROIgnoreLanguageChange;
75 static Sequence<OUString> GetPropertyNames();
76 void MakeRealLocale();
77 void MakeRealUILocale();
79 virtual void ImplCommit() override;
81 public:
82 SvtSysLocaleOptions_Impl();
83 virtual ~SvtSysLocaleOptions_Impl() override;
85 virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override;
87 const OUString& GetLocaleString() const
88 { return m_aLocaleString; }
89 void SetLocaleString( const OUString& rStr );
91 void SetUILocaleString( const OUString& rStr );
93 const OUString& GetCurrencyString() const
94 { return m_aCurrencyString; }
95 void SetCurrencyString( const OUString& rStr );
97 const OUString& GetDatePatternsString() const
98 { return m_aDatePatternsString; }
99 void SetDatePatternsString( const OUString& rStr );
101 bool IsDecimalSeparatorAsLocale() const { return m_bDecimalSeparator;}
102 void SetDecimalSeparatorAsLocale( bool bSet);
104 bool IsIgnoreLanguageChange() const { return m_bIgnoreLanguageChange;}
105 void SetIgnoreLanguageChange( bool bSet);
107 bool IsReadOnly( SvtSysLocaleOptions::EOption eOption ) const;
108 const LanguageTag& GetRealLocale() const { return m_aRealLocale; }
109 const LanguageTag& GetRealUILocale() const { return m_aRealUILocale; }
112 constexpr OUStringLiteral ROOTNODE_SYSLOCALE = u"Setup/L10N";
114 constexpr OUString PROPERTYNAME_LOCALE = u"ooSetupSystemLocale"_ustr;
115 constexpr OUString PROPERTYNAME_UILOCALE = u"ooLocale"_ustr;
116 constexpr OUString PROPERTYNAME_CURRENCY = u"ooSetupCurrency"_ustr;
117 constexpr OUString PROPERTYNAME_DECIMALSEPARATOR = u"DecimalSeparatorAsLocale"_ustr;
118 constexpr OUString PROPERTYNAME_DATEPATTERNS = u"DateAcceptancePatterns"_ustr;
119 constexpr OUString PROPERTYNAME_IGNORELANGCHANGE = u"IgnoreLanguageChange"_ustr;
121 #define PROPERTYHANDLE_LOCALE 0
122 #define PROPERTYHANDLE_UILOCALE 1
123 #define PROPERTYHANDLE_CURRENCY 2
124 #define PROPERTYHANDLE_DECIMALSEPARATOR 3
125 #define PROPERTYHANDLE_DATEPATTERNS 4
126 #define PROPERTYHANDLE_IGNORELANGCHANGE 5
128 Sequence< OUString > SvtSysLocaleOptions_Impl::GetPropertyNames()
130 return Sequence< OUString >
132 PROPERTYNAME_LOCALE,
133 PROPERTYNAME_UILOCALE,
134 PROPERTYNAME_CURRENCY,
135 PROPERTYNAME_DECIMALSEPARATOR,
136 PROPERTYNAME_DATEPATTERNS,
137 PROPERTYNAME_IGNORELANGCHANGE
141 SvtSysLocaleOptions_Impl::SvtSysLocaleOptions_Impl()
142 : ConfigItem( ROOTNODE_SYSLOCALE )
143 , m_aRealLocale( LANGUAGE_SYSTEM)
144 , m_aRealUILocale( LANGUAGE_SYSTEM)
145 , m_bDecimalSeparator( true )
146 , m_bIgnoreLanguageChange( false)
147 , m_bROLocale(CFG_READONLY_DEFAULT)
148 , m_bROUILocale(CFG_READONLY_DEFAULT)
149 , m_bROCurrency(CFG_READONLY_DEFAULT)
150 , m_bRODatePatterns(CFG_READONLY_DEFAULT)
151 , m_bRODecimalSeparator(false)
152 , m_bROIgnoreLanguageChange(false)
155 const Sequence< OUString > aNames = GetPropertyNames();
156 Sequence< Any > aValues = GetProperties( aNames );
157 Sequence< sal_Bool > aROStates = GetReadOnlyStates( aNames );
158 const Any* pValues = aValues.getConstArray();
159 const sal_Bool* pROStates = aROStates.getConstArray();
160 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
161 DBG_ASSERT( aROStates.getLength() == aNames.getLength(), "GetReadOnlyStates failed" );
162 if ( aValues.getLength() == aNames.getLength() && aROStates.getLength() == aNames.getLength() )
164 for ( sal_Int32 nProp = 0; nProp < aNames.getLength(); nProp++ )
166 if ( pValues[nProp].hasValue() )
168 switch ( nProp )
170 case PROPERTYHANDLE_LOCALE :
172 OUString aStr;
173 if ( pValues[nProp] >>= aStr )
174 m_aLocaleString = aStr;
175 else
177 SAL_WARN( "unotools.config", "Wrong property type!" );
179 m_bROLocale = pROStates[nProp];
181 break;
182 case PROPERTYHANDLE_UILOCALE :
184 OUString aStr;
185 if ( pValues[nProp] >>= aStr )
186 m_aUILocaleString = aStr;
187 else
189 SAL_WARN( "unotools.config", "Wrong property type!" );
191 m_bROUILocale = pROStates[nProp];
193 break;
194 case PROPERTYHANDLE_CURRENCY :
196 OUString aStr;
197 if ( pValues[nProp] >>= aStr )
198 m_aCurrencyString = aStr;
199 else
201 SAL_WARN( "unotools.config", "Wrong property type!" );
203 m_bROCurrency = pROStates[nProp];
205 break;
206 case PROPERTYHANDLE_DECIMALSEPARATOR:
208 bool bValue = false;
209 if ( pValues[nProp] >>= bValue )
210 m_bDecimalSeparator = bValue;
211 else
213 SAL_WARN( "unotools.config", "Wrong property type!" );
215 m_bRODecimalSeparator = pROStates[nProp];
217 break;
218 case PROPERTYHANDLE_DATEPATTERNS :
220 OUString aStr;
221 if ( pValues[nProp] >>= aStr )
222 m_aDatePatternsString = aStr;
223 else
225 SAL_WARN( "unotools.config", "Wrong property type!" );
227 m_bRODatePatterns = pROStates[nProp];
229 break;
230 case PROPERTYHANDLE_IGNORELANGCHANGE :
232 bool bValue = false;
233 if ( pValues[nProp] >>= bValue )
234 m_bIgnoreLanguageChange = bValue;
235 else
237 SAL_WARN( "unotools.config", "Wrong property type!" );
239 m_bROIgnoreLanguageChange = pROStates[nProp];
241 break;
242 default:
243 SAL_WARN( "unotools.config", "Wrong property type!" );
248 EnableNotification( aNames );
250 MakeRealLocale();
251 MakeRealUILocale();
254 SvtSysLocaleOptions_Impl::~SvtSysLocaleOptions_Impl()
256 assert(!IsModified()); // should have been committed
259 void SvtSysLocaleOptions_Impl::MakeRealLocale()
261 if (m_aLocaleString.isEmpty())
263 LanguageType nLang = MsLangId::getConfiguredSystemLanguage();
264 m_aRealLocale.reset( nLang).makeFallback();
266 else
268 m_aRealLocale.reset( m_aLocaleString).makeFallback();
272 void SvtSysLocaleOptions_Impl::MakeRealUILocale()
274 if (m_aUILocaleString.isEmpty())
276 LanguageType nLang = MsLangId::getConfiguredSystemUILanguage();
277 m_aRealUILocale.reset( nLang).makeFallback();
279 else
281 m_aRealUILocale.reset( m_aUILocaleString).makeFallback();
285 bool SvtSysLocaleOptions_Impl::IsReadOnly( SvtSysLocaleOptions::EOption eOption ) const
287 bool bReadOnly = CFG_READONLY_DEFAULT;
288 switch(eOption)
290 case SvtSysLocaleOptions::EOption::Locale :
292 bReadOnly = m_bROLocale;
293 break;
295 case SvtSysLocaleOptions::EOption::Currency :
297 bReadOnly = m_bROCurrency;
298 break;
300 case SvtSysLocaleOptions::EOption::DatePatterns :
302 bReadOnly = m_bRODatePatterns;
303 break;
305 case SvtSysLocaleOptions::EOption::DecimalSeparator:
307 bReadOnly = m_bRODecimalSeparator;
308 break;
310 case SvtSysLocaleOptions::EOption::IgnoreLanguageChange:
312 bReadOnly = m_bROIgnoreLanguageChange;
313 break;
316 return bReadOnly;
319 void SvtSysLocaleOptions_Impl::ImplCommit()
321 const Sequence< OUString > aOrgNames = GetPropertyNames();
322 sal_Int32 nOrgCount = aOrgNames.getLength();
324 Sequence< OUString > aNames( nOrgCount );
325 Sequence< Any > aValues( nOrgCount );
327 OUString* pNames = aNames.getArray();
328 Any* pValues = aValues.getArray();
329 sal_Int32 nRealCount = 0;
331 for ( sal_Int32 nProp = 0; nProp < nOrgCount; nProp++ )
333 switch ( nProp )
335 case PROPERTYHANDLE_LOCALE :
337 if (!m_bROLocale)
339 pNames[nRealCount] = aOrgNames[nProp];
340 pValues[nRealCount] <<= m_aLocaleString;
341 ++nRealCount;
344 break;
345 case PROPERTYHANDLE_UILOCALE :
347 if (!m_bROUILocale)
349 pNames[nRealCount] = aOrgNames[nProp];
350 pValues[nRealCount] <<= m_aUILocaleString;
351 ++nRealCount;
354 break;
355 case PROPERTYHANDLE_CURRENCY :
357 if (!m_bROCurrency)
359 pNames[nRealCount] = aOrgNames[nProp];
360 pValues[nRealCount] <<= m_aCurrencyString;
361 ++nRealCount;
364 break;
365 case PROPERTYHANDLE_DECIMALSEPARATOR:
366 if( !m_bRODecimalSeparator )
368 pNames[nRealCount] = aOrgNames[nProp];
369 pValues[nRealCount] <<= m_bDecimalSeparator;
370 ++nRealCount;
372 break;
373 case PROPERTYHANDLE_DATEPATTERNS :
374 if (!m_bRODatePatterns)
376 pNames[nRealCount] = aOrgNames[nProp];
377 pValues[nRealCount] <<= m_aDatePatternsString;
378 ++nRealCount;
380 break;
381 case PROPERTYHANDLE_IGNORELANGCHANGE :
382 if (!m_bROIgnoreLanguageChange)
384 pNames[nRealCount] = aOrgNames[nProp];
385 pValues[nRealCount] <<= m_bIgnoreLanguageChange;
386 ++nRealCount;
388 break;
389 default:
390 SAL_WARN( "unotools.config", "invalid index to save a path" );
393 aNames.realloc(nRealCount);
394 aValues.realloc(nRealCount);
395 PutProperties( aNames, aValues );
398 void SvtSysLocaleOptions_Impl::SetLocaleString( const OUString& rStr )
400 ConfigurationHints nHint = ConfigurationHints::Locale;
402 MutexGuard aGuard( GetMutex() );
403 if (m_bROLocale || rStr == m_aLocaleString )
405 return;
407 m_aLocaleString = rStr;
408 MakeRealLocale();
409 LanguageTag::setConfiguredSystemLanguage( m_aRealLocale.getLanguageType() );
410 SetModified();
411 if ( m_aCurrencyString.isEmpty() )
412 nHint |= ConfigurationHints::Currency;
414 NotifyListeners( nHint );
417 void SvtSysLocaleOptions_Impl::SetUILocaleString( const OUString& rStr )
420 MutexGuard aGuard( GetMutex() );
421 if (m_bROUILocale || rStr == m_aUILocaleString )
423 return;
425 m_aUILocaleString = rStr;
427 // as we can't switch UILocale at runtime, we only store changes in the configuration
428 MakeRealUILocale();
429 SetModified();
431 NotifyListeners( ConfigurationHints::UiLocale );
434 void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString& rStr )
437 MutexGuard aGuard( GetMutex() );
438 if (m_bROCurrency || rStr == m_aCurrencyString )
440 return;
442 m_aCurrencyString = rStr;
443 SetModified();
445 NotifyListeners( ConfigurationHints::Currency );
448 void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString& rStr )
451 MutexGuard aGuard( GetMutex() );
452 if (m_bRODatePatterns || rStr == m_aDatePatternsString )
454 return;
456 m_aDatePatternsString = rStr;
457 SetModified();
459 NotifyListeners( ConfigurationHints::DatePatterns );
462 void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet)
465 MutexGuard aGuard( GetMutex() );
466 if(bSet == m_bDecimalSeparator)
468 return;
470 m_bDecimalSeparator = bSet;
471 SetModified();
473 NotifyListeners( ConfigurationHints::DecSep );
476 void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet)
479 MutexGuard aGuard( GetMutex() );
480 if(bSet == m_bIgnoreLanguageChange)
482 return;
484 m_bIgnoreLanguageChange = bSet;
485 SetModified();
487 NotifyListeners( ConfigurationHints::IgnoreLang );
490 void SvtSysLocaleOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
492 ConfigurationHints nHint = ConfigurationHints::NONE;
493 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
494 Sequence< sal_Bool > seqROStates = GetReadOnlyStates( seqPropertyNames );
495 sal_Int32 nCount = seqPropertyNames.getLength();
496 for( sal_Int32 nProp = 0; nProp < nCount; ++nProp )
498 if( seqPropertyNames[nProp] == PROPERTYNAME_LOCALE )
500 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" );
501 seqValues[nProp] >>= m_aLocaleString;
502 m_bROLocale = seqROStates[nProp];
503 nHint |= ConfigurationHints::Locale;
504 if ( m_aCurrencyString.isEmpty() )
505 nHint |= ConfigurationHints::Currency;
506 MakeRealLocale();
508 if( seqPropertyNames[nProp] == PROPERTYNAME_UILOCALE )
510 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" );
511 seqValues[nProp] >>= m_aUILocaleString;
512 m_bROUILocale = seqROStates[nProp];
513 nHint |= ConfigurationHints::UiLocale;
514 MakeRealUILocale();
516 else if( seqPropertyNames[nProp] == PROPERTYNAME_CURRENCY )
518 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Currency property type" );
519 seqValues[nProp] >>= m_aCurrencyString;
520 m_bROCurrency = seqROStates[nProp];
521 nHint |= ConfigurationHints::Currency;
523 else if( seqPropertyNames[nProp] == PROPERTYNAME_DECIMALSEPARATOR )
525 seqValues[nProp] >>= m_bDecimalSeparator;
526 m_bRODecimalSeparator = seqROStates[nProp];
528 else if( seqPropertyNames[nProp] == PROPERTYNAME_IGNORELANGCHANGE )
530 seqValues[nProp] >>= m_bIgnoreLanguageChange;
531 m_bROIgnoreLanguageChange = seqROStates[nProp];
533 else if( seqPropertyNames[nProp] == PROPERTYNAME_DATEPATTERNS )
535 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "DatePatterns property type" );
536 seqValues[nProp] >>= m_aDatePatternsString;
537 m_bRODatePatterns = seqROStates[nProp];
538 nHint |= ConfigurationHints::DatePatterns;
541 if ( nHint != ConfigurationHints::NONE )
542 NotifyListeners( nHint );
545 SvtSysLocaleOptions::SvtSysLocaleOptions()
547 MutexGuard aGuard( GetMutex() );
548 pImpl = g_pSysLocaleOptions.lock();
549 if ( !pImpl )
551 pImpl = std::make_shared<SvtSysLocaleOptions_Impl>();
552 g_pSysLocaleOptions = pImpl;
553 if (!comphelper::IsFuzzing())
554 ItemHolder1::holdConfigItem(EItem::SysLocaleOptions);
556 pImpl->AddListener(this);
559 SvtSysLocaleOptions::~SvtSysLocaleOptions()
561 MutexGuard aGuard( GetMutex() );
562 pImpl->RemoveListener(this);
563 pImpl.reset();
566 bool SvtSysLocaleOptions::IsModified() const
568 MutexGuard aGuard( GetMutex() );
569 return pImpl->IsModified();
572 void SvtSysLocaleOptions::Commit()
574 MutexGuard aGuard( GetMutex() );
575 pImpl->Commit();
578 void SvtSysLocaleOptions::BlockBroadcasts( bool bBlock )
580 MutexGuard aGuard( GetMutex() );
581 pImpl->BlockBroadcasts( bBlock );
584 void SvtSysLocaleOptions::SetLocaleConfigString( const OUString& rStr )
586 pImpl->SetLocaleString( rStr );
589 void SvtSysLocaleOptions::SetUILocaleConfigString( const OUString& rStr )
591 pImpl->SetUILocaleString( rStr );
594 const OUString& SvtSysLocaleOptions::GetCurrencyConfigString() const
596 MutexGuard aGuard( GetMutex() );
597 return pImpl->GetCurrencyString();
600 void SvtSysLocaleOptions::SetCurrencyConfigString( const OUString& rStr )
602 pImpl->SetCurrencyString( rStr );
605 const OUString& SvtSysLocaleOptions::GetDatePatternsConfigString() const
607 MutexGuard aGuard( GetMutex() );
608 return pImpl->GetDatePatternsString();
611 void SvtSysLocaleOptions::SetDatePatternsConfigString( const OUString& rStr )
613 pImpl->SetDatePatternsString( rStr );
616 bool SvtSysLocaleOptions::IsDecimalSeparatorAsLocale() const
618 MutexGuard aGuard( GetMutex() );
619 return pImpl->IsDecimalSeparatorAsLocale();
622 void SvtSysLocaleOptions::SetDecimalSeparatorAsLocale( bool bSet)
624 pImpl->SetDecimalSeparatorAsLocale(bSet);
627 bool SvtSysLocaleOptions::IsIgnoreLanguageChange() const
629 MutexGuard aGuard( GetMutex() );
630 return pImpl->IsIgnoreLanguageChange();
633 void SvtSysLocaleOptions::SetIgnoreLanguageChange( bool bSet)
635 pImpl->SetIgnoreLanguageChange(bSet);
638 bool SvtSysLocaleOptions::IsReadOnly( EOption eOption ) const
640 MutexGuard aGuard( GetMutex() );
641 return pImpl->IsReadOnly( eOption );
644 // static
645 void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString& rAbbrev,
646 LanguageType& eLang,
647 const OUString& rConfigString )
649 sal_Int32 nDelim = rConfigString.indexOf( '-' );
650 if ( nDelim >= 0 )
652 rAbbrev = rConfigString.copy( 0, nDelim );
653 OUString aIsoStr( rConfigString.copy( nDelim+1 ) );
654 eLang = LanguageTag::convertToLanguageTypeWithFallback( aIsoStr );
656 else
658 rAbbrev = rConfigString;
659 eLang = (rAbbrev.isEmpty() ? LANGUAGE_SYSTEM : LANGUAGE_NONE);
663 // static
664 OUString SvtSysLocaleOptions::CreateCurrencyConfigString(
665 const OUString& rAbbrev, LanguageType eLang )
667 OUString aIsoStr( LanguageTag::convertToBcp47( eLang ) );
668 if ( !aIsoStr.isEmpty() )
670 return rAbbrev + "-" + aIsoStr;
672 else
673 return rAbbrev;
676 // static
677 void SvtSysLocaleOptions::SetCurrencyChangeLink( const Link<LinkParamNone*,void>& rLink )
679 MutexGuard aGuard( GetMutex() );
680 DBG_ASSERT( !g_CurrencyChangeLink.IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" );
681 g_CurrencyChangeLink = rLink;
684 // static
685 const Link<LinkParamNone*,void>& SvtSysLocaleOptions::GetCurrencyChangeLink()
687 MutexGuard aGuard( GetMutex() );
688 return g_CurrencyChangeLink;
691 void SvtSysLocaleOptions::ConfigurationChanged( utl::ConfigurationBroadcaster* p, ConfigurationHints nHint )
693 if ( nHint & ConfigurationHints::Currency )
695 const Link<LinkParamNone*,void>& rLink = GetCurrencyChangeLink();
696 rLink.Call( nullptr );
699 ::utl::detail::Options::ConfigurationChanged( p, nHint );
702 LanguageTag SvtSysLocaleOptions::GetLanguageTag() const
704 MutexGuard aGuard( GetMutex() );
705 return LanguageTag( pImpl->GetLocaleString() );
708 const LanguageTag & SvtSysLocaleOptions::GetRealLanguageTag() const
710 return pImpl->GetRealLocale();
713 const LanguageTag & SvtSysLocaleOptions::GetRealUILanguageTag() const
715 return pImpl->GetRealUILocale();
718 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */