1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::lang
;
42 std::weak_ptr
<SvtSysLocaleOptions_Impl
> g_pSysLocaleOptions
;
43 Link
<LinkParamNone
*,void> g_CurrencyChangeLink
;
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
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
;
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
>
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() )
170 case PROPERTYHANDLE_LOCALE
:
173 if ( pValues
[nProp
] >>= aStr
)
174 m_aLocaleString
= aStr
;
177 SAL_WARN( "unotools.config", "Wrong property type!" );
179 m_bROLocale
= pROStates
[nProp
];
182 case PROPERTYHANDLE_UILOCALE
:
185 if ( pValues
[nProp
] >>= aStr
)
186 m_aUILocaleString
= aStr
;
189 SAL_WARN( "unotools.config", "Wrong property type!" );
191 m_bROUILocale
= pROStates
[nProp
];
194 case PROPERTYHANDLE_CURRENCY
:
197 if ( pValues
[nProp
] >>= aStr
)
198 m_aCurrencyString
= aStr
;
201 SAL_WARN( "unotools.config", "Wrong property type!" );
203 m_bROCurrency
= pROStates
[nProp
];
206 case PROPERTYHANDLE_DECIMALSEPARATOR
:
209 if ( pValues
[nProp
] >>= bValue
)
210 m_bDecimalSeparator
= bValue
;
213 SAL_WARN( "unotools.config", "Wrong property type!" );
215 m_bRODecimalSeparator
= pROStates
[nProp
];
218 case PROPERTYHANDLE_DATEPATTERNS
:
221 if ( pValues
[nProp
] >>= aStr
)
222 m_aDatePatternsString
= aStr
;
225 SAL_WARN( "unotools.config", "Wrong property type!" );
227 m_bRODatePatterns
= pROStates
[nProp
];
230 case PROPERTYHANDLE_IGNORELANGCHANGE
:
233 if ( pValues
[nProp
] >>= bValue
)
234 m_bIgnoreLanguageChange
= bValue
;
237 SAL_WARN( "unotools.config", "Wrong property type!" );
239 m_bROIgnoreLanguageChange
= pROStates
[nProp
];
243 SAL_WARN( "unotools.config", "Wrong property type!" );
248 EnableNotification( aNames
);
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();
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();
281 m_aRealUILocale
.reset( m_aUILocaleString
).makeFallback();
285 bool SvtSysLocaleOptions_Impl::IsReadOnly( SvtSysLocaleOptions::EOption eOption
) const
287 bool bReadOnly
= CFG_READONLY_DEFAULT
;
290 case SvtSysLocaleOptions::EOption::Locale
:
292 bReadOnly
= m_bROLocale
;
295 case SvtSysLocaleOptions::EOption::Currency
:
297 bReadOnly
= m_bROCurrency
;
300 case SvtSysLocaleOptions::EOption::DatePatterns
:
302 bReadOnly
= m_bRODatePatterns
;
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
++ )
325 case PROPERTYHANDLE_LOCALE
:
329 pNames
[nRealCount
] = aOrgNames
[nProp
];
330 pValues
[nRealCount
] <<= m_aLocaleString
;
335 case PROPERTYHANDLE_UILOCALE
:
339 pNames
[nRealCount
] = aOrgNames
[nProp
];
340 pValues
[nRealCount
] <<= m_aUILocaleString
;
345 case PROPERTYHANDLE_CURRENCY
:
349 pNames
[nRealCount
] = aOrgNames
[nProp
];
350 pValues
[nRealCount
] <<= m_aCurrencyString
;
355 case PROPERTYHANDLE_DECIMALSEPARATOR
:
356 if( !m_bRODecimalSeparator
)
358 pNames
[nRealCount
] = aOrgNames
[nProp
];
359 pValues
[nRealCount
] <<= m_bDecimalSeparator
;
363 case PROPERTYHANDLE_DATEPATTERNS
:
364 if (!m_bRODatePatterns
)
366 pNames
[nRealCount
] = aOrgNames
[nProp
];
367 pValues
[nRealCount
] <<= m_aDatePatternsString
;
371 case PROPERTYHANDLE_IGNORELANGCHANGE
:
372 if (!m_bROIgnoreLanguageChange
)
374 pNames
[nRealCount
] = aOrgNames
[nProp
];
375 pValues
[nRealCount
] <<= m_bIgnoreLanguageChange
;
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
)
397 m_aLocaleString
= rStr
;
399 LanguageTag::setConfiguredSystemLanguage( m_aRealLocale
.getLanguageType() );
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
)
415 m_aUILocaleString
= rStr
;
417 // as we can't switch UILocale at runtime, we only store changes in the configuration
421 NotifyListeners( ConfigurationHints::UiLocale
);
424 void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString
& rStr
)
427 MutexGuard
aGuard( GetMutex() );
428 if (m_bROCurrency
|| rStr
== m_aCurrencyString
)
432 m_aCurrencyString
= rStr
;
435 NotifyListeners( ConfigurationHints::Currency
);
438 void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString
& rStr
)
441 MutexGuard
aGuard( GetMutex() );
442 if (m_bRODatePatterns
|| rStr
== m_aDatePatternsString
)
446 m_aDatePatternsString
= rStr
;
449 NotifyListeners( ConfigurationHints::DatePatterns
);
452 void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet
)
455 MutexGuard
aGuard( GetMutex() );
456 if(bSet
== m_bDecimalSeparator
)
460 m_bDecimalSeparator
= bSet
;
463 NotifyListeners( ConfigurationHints::DecSep
);
466 void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet
)
469 MutexGuard
aGuard( GetMutex() );
470 if(bSet
== m_bIgnoreLanguageChange
)
474 m_bIgnoreLanguageChange
= bSet
;
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
;
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
;
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();
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);
556 bool SvtSysLocaleOptions::IsModified() const
558 MutexGuard
aGuard( GetMutex() );
559 return pImpl
->IsModified();
562 void SvtSysLocaleOptions::Commit()
564 MutexGuard
aGuard( GetMutex() );
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
);
635 void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString
& rAbbrev
,
637 const OUString
& rConfigString
)
639 sal_Int32 nDelim
= rConfigString
.indexOf( '-' );
642 rAbbrev
= rConfigString
.copy( 0, nDelim
);
643 OUString
aIsoStr( rConfigString
.copy( nDelim
+1 ) );
644 eLang
= LanguageTag::convertToLanguageTypeWithFallback( aIsoStr
);
648 rAbbrev
= rConfigString
;
649 eLang
= (rAbbrev
.isEmpty() ? LANGUAGE_SYSTEM
: LANGUAGE_NONE
);
654 OUString
SvtSysLocaleOptions::CreateCurrencyConfigString(
655 const OUString
& rAbbrev
, LanguageType eLang
)
657 OUString
aIsoStr( LanguageTag::convertToBcp47( eLang
) );
658 if ( !aIsoStr
.isEmpty() )
660 return rAbbrev
+ "-" + aIsoStr
;
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
;
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: */