Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / config / syslocaleoptions.cxx
blob8f08202a7a23b6dd3e6247698781e2432f265910
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 <sal/log.hxx>
22 #include <i18nlangtag/mslangid.hxx>
23 #include <i18nlangtag/languagetag.hxx>
24 #include <tools/debug.hxx>
25 #include <tools/link.hxx>
26 #include <unotools/syslocaleoptions.hxx>
27 #include <unotools/configmgr.hxx>
28 #include <unotools/configitem.hxx>
29 #include <com/sun/star/uno/Any.hxx>
31 #include "itemholder1.hxx"
33 #define CFG_READONLY_DEFAULT false
35 using namespace osl;
36 using namespace utl;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::lang;
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 OUStringLiteral PROPERTYNAME_LOCALE = u"ooSetupSystemLocale";
115 constexpr OUStringLiteral PROPERTYNAME_UILOCALE = u"ooLocale";
116 constexpr OUStringLiteral PROPERTYNAME_CURRENCY = u"ooSetupCurrency";
117 constexpr OUStringLiteral PROPERTYNAME_DECIMALSEPARATOR = u"DecimalSeparatorAsLocale";
118 constexpr OUStringLiteral PROPERTYNAME_DATEPATTERNS = u"DateAcceptancePatterns";
119 constexpr OUStringLiteral PROPERTYNAME_IGNORELANGCHANGE = u"IgnoreLanguageChange";
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;
306 return bReadOnly;
309 void SvtSysLocaleOptions_Impl::ImplCommit()
311 const Sequence< OUString > aOrgNames = GetPropertyNames();
312 sal_Int32 nOrgCount = aOrgNames.getLength();
314 Sequence< OUString > aNames( nOrgCount );
315 Sequence< Any > aValues( nOrgCount );
317 OUString* pNames = aNames.getArray();
318 Any* pValues = aValues.getArray();
319 sal_Int32 nRealCount = 0;
321 for ( sal_Int32 nProp = 0; nProp < nOrgCount; nProp++ )
323 switch ( nProp )
325 case PROPERTYHANDLE_LOCALE :
327 if (!m_bROLocale)
329 pNames[nRealCount] = aOrgNames[nProp];
330 pValues[nRealCount] <<= m_aLocaleString;
331 ++nRealCount;
334 break;
335 case PROPERTYHANDLE_UILOCALE :
337 if (!m_bROUILocale)
339 pNames[nRealCount] = aOrgNames[nProp];
340 pValues[nRealCount] <<= m_aUILocaleString;
341 ++nRealCount;
344 break;
345 case PROPERTYHANDLE_CURRENCY :
347 if (!m_bROCurrency)
349 pNames[nRealCount] = aOrgNames[nProp];
350 pValues[nRealCount] <<= m_aCurrencyString;
351 ++nRealCount;
354 break;
355 case PROPERTYHANDLE_DECIMALSEPARATOR:
356 if( !m_bRODecimalSeparator )
358 pNames[nRealCount] = aOrgNames[nProp];
359 pValues[nRealCount] <<= m_bDecimalSeparator;
360 ++nRealCount;
362 break;
363 case PROPERTYHANDLE_DATEPATTERNS :
364 if (!m_bRODatePatterns)
366 pNames[nRealCount] = aOrgNames[nProp];
367 pValues[nRealCount] <<= m_aDatePatternsString;
368 ++nRealCount;
370 break;
371 case PROPERTYHANDLE_IGNORELANGCHANGE :
372 if (!m_bROIgnoreLanguageChange)
374 pNames[nRealCount] = aOrgNames[nProp];
375 pValues[nRealCount] <<= m_bIgnoreLanguageChange;
376 ++nRealCount;
378 break;
379 default:
380 SAL_WARN( "unotools.config", "invalid index to save a path" );
383 aNames.realloc(nRealCount);
384 aValues.realloc(nRealCount);
385 PutProperties( aNames, aValues );
388 void SvtSysLocaleOptions_Impl::SetLocaleString( const OUString& rStr )
390 ConfigurationHints nHint = ConfigurationHints::Locale;
392 MutexGuard aGuard( GetMutex() );
393 if (m_bROLocale || rStr == m_aLocaleString )
395 return;
397 m_aLocaleString = rStr;
398 MakeRealLocale();
399 LanguageTag::setConfiguredSystemLanguage( m_aRealLocale.getLanguageType() );
400 SetModified();
401 if ( m_aCurrencyString.isEmpty() )
402 nHint |= ConfigurationHints::Currency;
404 NotifyListeners( nHint );
407 void SvtSysLocaleOptions_Impl::SetUILocaleString( const OUString& rStr )
410 MutexGuard aGuard( GetMutex() );
411 if (m_bROUILocale || rStr == m_aUILocaleString )
413 return;
415 m_aUILocaleString = rStr;
417 // as we can't switch UILocale at runtime, we only store changes in the configuration
418 MakeRealUILocale();
419 SetModified();
421 NotifyListeners( ConfigurationHints::UiLocale );
424 void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString& rStr )
427 MutexGuard aGuard( GetMutex() );
428 if (m_bROCurrency || rStr == m_aCurrencyString )
430 return;
432 m_aCurrencyString = rStr;
433 SetModified();
435 NotifyListeners( ConfigurationHints::Currency );
438 void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString& rStr )
441 MutexGuard aGuard( GetMutex() );
442 if (m_bRODatePatterns || rStr == m_aDatePatternsString )
444 return;
446 m_aDatePatternsString = rStr;
447 SetModified();
449 NotifyListeners( ConfigurationHints::DatePatterns );
452 void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet)
455 MutexGuard aGuard( GetMutex() );
456 if(bSet == m_bDecimalSeparator)
458 return;
460 m_bDecimalSeparator = bSet;
461 SetModified();
463 NotifyListeners( ConfigurationHints::DecSep );
466 void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet)
469 MutexGuard aGuard( GetMutex() );
470 if(bSet == m_bIgnoreLanguageChange)
472 return;
474 m_bIgnoreLanguageChange = bSet;
475 SetModified();
477 NotifyListeners( ConfigurationHints::IgnoreLang );
480 void SvtSysLocaleOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
482 ConfigurationHints nHint = ConfigurationHints::NONE;
483 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
484 Sequence< sal_Bool > seqROStates = GetReadOnlyStates( seqPropertyNames );
485 sal_Int32 nCount = seqPropertyNames.getLength();
486 for( sal_Int32 nProp = 0; nProp < nCount; ++nProp )
488 if( seqPropertyNames[nProp] == PROPERTYNAME_LOCALE )
490 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" );
491 seqValues[nProp] >>= m_aLocaleString;
492 m_bROLocale = seqROStates[nProp];
493 nHint |= ConfigurationHints::Locale;
494 if ( m_aCurrencyString.isEmpty() )
495 nHint |= ConfigurationHints::Currency;
496 MakeRealLocale();
498 if( seqPropertyNames[nProp] == PROPERTYNAME_UILOCALE )
500 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" );
501 seqValues[nProp] >>= m_aUILocaleString;
502 m_bROUILocale = seqROStates[nProp];
503 nHint |= ConfigurationHints::UiLocale;
504 MakeRealUILocale();
506 else if( seqPropertyNames[nProp] == PROPERTYNAME_CURRENCY )
508 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Currency property type" );
509 seqValues[nProp] >>= m_aCurrencyString;
510 m_bROCurrency = seqROStates[nProp];
511 nHint |= ConfigurationHints::Currency;
513 else if( seqPropertyNames[nProp] == PROPERTYNAME_DECIMALSEPARATOR )
515 seqValues[nProp] >>= m_bDecimalSeparator;
516 m_bRODecimalSeparator = seqROStates[nProp];
518 else if( seqPropertyNames[nProp] == PROPERTYNAME_IGNORELANGCHANGE )
520 seqValues[nProp] >>= m_bIgnoreLanguageChange;
521 m_bROIgnoreLanguageChange = seqROStates[nProp];
523 else if( seqPropertyNames[nProp] == PROPERTYNAME_DATEPATTERNS )
525 DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "DatePatterns property type" );
526 seqValues[nProp] >>= m_aDatePatternsString;
527 m_bRODatePatterns = seqROStates[nProp];
528 nHint |= ConfigurationHints::DatePatterns;
531 if ( nHint != ConfigurationHints::NONE )
532 NotifyListeners( nHint );
535 SvtSysLocaleOptions::SvtSysLocaleOptions()
537 MutexGuard aGuard( GetMutex() );
538 pImpl = g_pSysLocaleOptions.lock();
539 if ( !pImpl )
541 pImpl = std::make_shared<SvtSysLocaleOptions_Impl>();
542 g_pSysLocaleOptions = pImpl;
543 if (!utl::ConfigManager::IsFuzzing())
544 ItemHolder1::holdConfigItem(EItem::SysLocaleOptions);
546 pImpl->AddListener(this);
549 SvtSysLocaleOptions::~SvtSysLocaleOptions()
551 MutexGuard aGuard( GetMutex() );
552 pImpl->RemoveListener(this);
553 pImpl.reset();
556 bool SvtSysLocaleOptions::IsModified() const
558 MutexGuard aGuard( GetMutex() );
559 return pImpl->IsModified();
562 void SvtSysLocaleOptions::Commit()
564 MutexGuard aGuard( GetMutex() );
565 pImpl->Commit();
568 void SvtSysLocaleOptions::BlockBroadcasts( bool bBlock )
570 MutexGuard aGuard( GetMutex() );
571 pImpl->BlockBroadcasts( bBlock );
574 void SvtSysLocaleOptions::SetLocaleConfigString( const OUString& rStr )
576 pImpl->SetLocaleString( rStr );
579 void SvtSysLocaleOptions::SetUILocaleConfigString( const OUString& rStr )
581 pImpl->SetUILocaleString( rStr );
584 const OUString& SvtSysLocaleOptions::GetCurrencyConfigString() const
586 MutexGuard aGuard( GetMutex() );
587 return pImpl->GetCurrencyString();
590 void SvtSysLocaleOptions::SetCurrencyConfigString( const OUString& rStr )
592 pImpl->SetCurrencyString( rStr );
595 const OUString& SvtSysLocaleOptions::GetDatePatternsConfigString() const
597 MutexGuard aGuard( GetMutex() );
598 return pImpl->GetDatePatternsString();
601 void SvtSysLocaleOptions::SetDatePatternsConfigString( const OUString& rStr )
603 pImpl->SetDatePatternsString( rStr );
606 bool SvtSysLocaleOptions::IsDecimalSeparatorAsLocale() const
608 MutexGuard aGuard( GetMutex() );
609 return pImpl->IsDecimalSeparatorAsLocale();
612 void SvtSysLocaleOptions::SetDecimalSeparatorAsLocale( bool bSet)
614 pImpl->SetDecimalSeparatorAsLocale(bSet);
617 bool SvtSysLocaleOptions::IsIgnoreLanguageChange() const
619 MutexGuard aGuard( GetMutex() );
620 return pImpl->IsIgnoreLanguageChange();
623 void SvtSysLocaleOptions::SetIgnoreLanguageChange( bool bSet)
625 pImpl->SetIgnoreLanguageChange(bSet);
628 bool SvtSysLocaleOptions::IsReadOnly( EOption eOption ) const
630 MutexGuard aGuard( GetMutex() );
631 return pImpl->IsReadOnly( eOption );
634 // static
635 void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString& rAbbrev,
636 LanguageType& eLang,
637 const OUString& rConfigString )
639 sal_Int32 nDelim = rConfigString.indexOf( '-' );
640 if ( nDelim >= 0 )
642 rAbbrev = rConfigString.copy( 0, nDelim );
643 OUString aIsoStr( rConfigString.copy( nDelim+1 ) );
644 eLang = LanguageTag::convertToLanguageTypeWithFallback( aIsoStr );
646 else
648 rAbbrev = rConfigString;
649 eLang = (rAbbrev.isEmpty() ? LANGUAGE_SYSTEM : LANGUAGE_NONE);
653 // static
654 OUString SvtSysLocaleOptions::CreateCurrencyConfigString(
655 const OUString& rAbbrev, LanguageType eLang )
657 OUString aIsoStr( LanguageTag::convertToBcp47( eLang ) );
658 if ( !aIsoStr.isEmpty() )
660 return rAbbrev + "-" + aIsoStr;
662 else
663 return rAbbrev;
666 // static
667 void SvtSysLocaleOptions::SetCurrencyChangeLink( const Link<LinkParamNone*,void>& rLink )
669 MutexGuard aGuard( GetMutex() );
670 DBG_ASSERT( !g_CurrencyChangeLink.IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" );
671 g_CurrencyChangeLink = rLink;
674 // static
675 const Link<LinkParamNone*,void>& SvtSysLocaleOptions::GetCurrencyChangeLink()
677 MutexGuard aGuard( GetMutex() );
678 return g_CurrencyChangeLink;
681 void SvtSysLocaleOptions::ConfigurationChanged( utl::ConfigurationBroadcaster* p, ConfigurationHints nHint )
683 if ( nHint & ConfigurationHints::Currency )
685 const Link<LinkParamNone*,void>& rLink = GetCurrencyChangeLink();
686 rLink.Call( nullptr );
689 ::utl::detail::Options::ConfigurationChanged( p, nHint );
692 LanguageTag SvtSysLocaleOptions::GetLanguageTag() const
694 MutexGuard aGuard( GetMutex() );
695 return LanguageTag( pImpl->GetLocaleString() );
698 const LanguageTag & SvtSysLocaleOptions::GetRealLanguageTag() const
700 return pImpl->GetRealLocale();
703 const LanguageTag & SvtSysLocaleOptions::GetRealUILanguageTag() const
705 return pImpl->GetRealUILocale();
708 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */