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 <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
38 using namespace com::sun::star::uno
;
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 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
>
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
;
305 case SvtSysLocaleOptions::EOption::DecimalSeparator
:
307 bReadOnly
= m_bRODecimalSeparator
;
310 case SvtSysLocaleOptions::EOption::IgnoreLanguageChange
:
312 bReadOnly
= m_bROIgnoreLanguageChange
;
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
++ )
335 case PROPERTYHANDLE_LOCALE
:
339 pNames
[nRealCount
] = aOrgNames
[nProp
];
340 pValues
[nRealCount
] <<= m_aLocaleString
;
345 case PROPERTYHANDLE_UILOCALE
:
349 pNames
[nRealCount
] = aOrgNames
[nProp
];
350 pValues
[nRealCount
] <<= m_aUILocaleString
;
355 case PROPERTYHANDLE_CURRENCY
:
359 pNames
[nRealCount
] = aOrgNames
[nProp
];
360 pValues
[nRealCount
] <<= m_aCurrencyString
;
365 case PROPERTYHANDLE_DECIMALSEPARATOR
:
366 if( !m_bRODecimalSeparator
)
368 pNames
[nRealCount
] = aOrgNames
[nProp
];
369 pValues
[nRealCount
] <<= m_bDecimalSeparator
;
373 case PROPERTYHANDLE_DATEPATTERNS
:
374 if (!m_bRODatePatterns
)
376 pNames
[nRealCount
] = aOrgNames
[nProp
];
377 pValues
[nRealCount
] <<= m_aDatePatternsString
;
381 case PROPERTYHANDLE_IGNORELANGCHANGE
:
382 if (!m_bROIgnoreLanguageChange
)
384 pNames
[nRealCount
] = aOrgNames
[nProp
];
385 pValues
[nRealCount
] <<= m_bIgnoreLanguageChange
;
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
)
407 m_aLocaleString
= rStr
;
409 LanguageTag::setConfiguredSystemLanguage( m_aRealLocale
.getLanguageType() );
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
)
425 m_aUILocaleString
= rStr
;
427 // as we can't switch UILocale at runtime, we only store changes in the configuration
431 NotifyListeners( ConfigurationHints::UiLocale
);
434 void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString
& rStr
)
437 MutexGuard
aGuard( GetMutex() );
438 if (m_bROCurrency
|| rStr
== m_aCurrencyString
)
442 m_aCurrencyString
= rStr
;
445 NotifyListeners( ConfigurationHints::Currency
);
448 void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString
& rStr
)
451 MutexGuard
aGuard( GetMutex() );
452 if (m_bRODatePatterns
|| rStr
== m_aDatePatternsString
)
456 m_aDatePatternsString
= rStr
;
459 NotifyListeners( ConfigurationHints::DatePatterns
);
462 void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet
)
465 MutexGuard
aGuard( GetMutex() );
466 if(bSet
== m_bDecimalSeparator
)
470 m_bDecimalSeparator
= bSet
;
473 NotifyListeners( ConfigurationHints::DecSep
);
476 void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet
)
479 MutexGuard
aGuard( GetMutex() );
480 if(bSet
== m_bIgnoreLanguageChange
)
484 m_bIgnoreLanguageChange
= bSet
;
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
;
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
;
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();
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);
566 bool SvtSysLocaleOptions::IsModified() const
568 MutexGuard
aGuard( GetMutex() );
569 return pImpl
->IsModified();
572 void SvtSysLocaleOptions::Commit()
574 MutexGuard
aGuard( GetMutex() );
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
);
645 void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString
& rAbbrev
,
647 const OUString
& rConfigString
)
649 sal_Int32 nDelim
= rConfigString
.indexOf( '-' );
652 rAbbrev
= rConfigString
.copy( 0, nDelim
);
653 OUString
aIsoStr( rConfigString
.copy( nDelim
+1 ) );
654 eLang
= LanguageTag::convertToLanguageTypeWithFallback( aIsoStr
);
658 rAbbrev
= rConfigString
;
659 eLang
= (rAbbrev
.isEmpty() ? LANGUAGE_SYSTEM
: LANGUAGE_NONE
);
664 OUString
SvtSysLocaleOptions::CreateCurrencyConfigString(
665 const OUString
& rAbbrev
, LanguageType eLang
)
667 OUString
aIsoStr( LanguageTag::convertToBcp47( eLang
) );
668 if ( !aIsoStr
.isEmpty() )
670 return rAbbrev
+ "-" + aIsoStr
;
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
;
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: */