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 <rtl/ustrbuf.hxx>
22 #include <rtl/instance.hxx>
23 #include <i18nlangtag/mslangid.hxx>
24 #include <i18nlangtag/languagetag.hxx>
25 #include <tools/debug.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
;
40 SvtSysLocaleOptions_Impl
* SvtSysLocaleOptions::pOptions
= NULL
;
41 sal_Int32
SvtSysLocaleOptions::nRefCount
= 0;
44 struct CurrencyChangeLink
45 : public rtl::Static
<Link
, CurrencyChangeLink
> {};
48 class SvtSysLocaleOptions_Impl
: public utl::ConfigItem
50 LanguageTag m_aRealLocale
;
51 LanguageTag m_aRealUILocale
;
52 OUString m_aLocaleString
; // en-US or de-DE or empty for SYSTEM
53 OUString m_aUILocaleString
; // en-US or de-DE or empty for SYSTEM
54 OUString m_aCurrencyString
; // USD-en-US or EUR-de-DE
55 OUString m_aDatePatternsString
; // "Y-M-D;M-D"
56 bool m_bDecimalSeparator
; //use decimal separator same as locale
57 bool m_bIgnoreLanguageChange
; //OS language change doesn't affect LO document language
62 bool m_bRODatePatterns
;
63 bool m_bRODecimalSeparator
;
64 bool m_bROIgnoreLanguageChange
;
66 static const Sequence
< /* const */ OUString
> GetPropertyNames();
67 void MakeRealLocale();
68 void MakeRealUILocale();
71 SvtSysLocaleOptions_Impl();
72 virtual ~SvtSysLocaleOptions_Impl();
74 virtual void Notify( const com::sun::star::uno::Sequence
< OUString
>& aPropertyNames
) SAL_OVERRIDE
;
75 virtual void Commit() SAL_OVERRIDE
;
77 const OUString
& GetLocaleString() const
78 { return m_aLocaleString
; }
79 void SetLocaleString( const OUString
& rStr
);
81 void SetUILocaleString( const OUString
& rStr
);
83 const OUString
& GetCurrencyString() const
84 { return m_aCurrencyString
; }
85 void SetCurrencyString( const OUString
& rStr
);
87 const OUString
& GetDatePatternsString() const
88 { return m_aDatePatternsString
; }
89 void SetDatePatternsString( const OUString
& rStr
);
91 bool IsDecimalSeparatorAsLocale() const { return m_bDecimalSeparator
;}
92 void SetDecimalSeparatorAsLocale( bool bSet
);
94 bool IsIgnoreLanguageChange() const { return m_bIgnoreLanguageChange
;}
95 void SetIgnoreLanguageChange( bool bSet
);
97 bool IsReadOnly( SvtSysLocaleOptions::EOption eOption
) const;
98 const LanguageTag
& GetRealLocale() { return m_aRealLocale
; }
99 const LanguageTag
& GetRealUILocale() { return m_aRealUILocale
; }
102 #define ROOTNODE_SYSLOCALE OUString("Setup/L10N")
104 #define PROPERTYNAME_LOCALE OUString("ooSetupSystemLocale")
105 #define PROPERTYNAME_UILOCALE OUString("ooLocale")
106 #define PROPERTYNAME_CURRENCY OUString("ooSetupCurrency")
107 #define PROPERTYNAME_DECIMALSEPARATOR OUString("DecimalSeparatorAsLocale")
108 #define PROPERTYNAME_DATEPATTERNS OUString("DateAcceptancePatterns")
109 #define PROPERTYNAME_IGNORELANGCHANGE OUString("IgnoreLanguageChange")
111 #define PROPERTYHANDLE_LOCALE 0
112 #define PROPERTYHANDLE_UILOCALE 1
113 #define PROPERTYHANDLE_CURRENCY 2
114 #define PROPERTYHANDLE_DECIMALSEPARATOR 3
115 #define PROPERTYHANDLE_DATEPATTERNS 4
116 #define PROPERTYHANDLE_IGNORELANGCHANGE 5
118 //#define PROPERTYCOUNT 5
119 #define PROPERTYCOUNT 6
121 const Sequence
< OUString
> SvtSysLocaleOptions_Impl::GetPropertyNames()
123 const OUString pProperties
[] =
126 PROPERTYNAME_UILOCALE
,
127 PROPERTYNAME_CURRENCY
,
128 PROPERTYNAME_DECIMALSEPARATOR
,
129 PROPERTYNAME_DATEPATTERNS
,
130 PROPERTYNAME_IGNORELANGCHANGE
132 const Sequence
< OUString
> seqPropertyNames( pProperties
, PROPERTYCOUNT
);
133 return seqPropertyNames
;
136 SvtSysLocaleOptions_Impl::SvtSysLocaleOptions_Impl()
137 : ConfigItem( ROOTNODE_SYSLOCALE
)
138 , m_aRealLocale( LANGUAGE_SYSTEM
)
139 , m_aRealUILocale( LANGUAGE_SYSTEM
)
140 , m_bDecimalSeparator( true )
141 , m_bIgnoreLanguageChange( false)
142 , m_bROLocale(CFG_READONLY_DEFAULT
)
143 , m_bROUILocale(CFG_READONLY_DEFAULT
)
144 , m_bROCurrency(CFG_READONLY_DEFAULT
)
145 , m_bRODatePatterns(CFG_READONLY_DEFAULT
)
146 , m_bRODecimalSeparator(false)
147 , m_bROIgnoreLanguageChange(false)
150 const Sequence
< OUString
> aNames
= GetPropertyNames();
151 Sequence
< Any
> aValues
= GetProperties( aNames
);
152 Sequence
< sal_Bool
> aROStates
= GetReadOnlyStates( aNames
);
153 const Any
* pValues
= aValues
.getConstArray();
154 const sal_Bool
* pROStates
= aROStates
.getConstArray();
155 DBG_ASSERT( aValues
.getLength() == aNames
.getLength(), "GetProperties failed" );
156 DBG_ASSERT( aROStates
.getLength() == aNames
.getLength(), "GetReadOnlyStates failed" );
157 if ( aValues
.getLength() == aNames
.getLength() && aROStates
.getLength() == aNames
.getLength() )
159 for ( sal_Int32 nProp
= 0; nProp
< aNames
.getLength(); nProp
++ )
161 if ( pValues
[nProp
].hasValue() )
165 case PROPERTYHANDLE_LOCALE
:
168 if ( pValues
[nProp
] >>= aStr
)
169 m_aLocaleString
= aStr
;
172 SAL_WARN( "unotools.config", "Wrong property type!" );
174 m_bROLocale
= pROStates
[nProp
];
177 case PROPERTYHANDLE_UILOCALE
:
180 if ( pValues
[nProp
] >>= aStr
)
181 m_aUILocaleString
= aStr
;
184 SAL_WARN( "unotools.config", "Wrong property type!" );
186 m_bROUILocale
= pROStates
[nProp
];
189 case PROPERTYHANDLE_CURRENCY
:
192 if ( pValues
[nProp
] >>= aStr
)
193 m_aCurrencyString
= aStr
;
196 SAL_WARN( "unotools.config", "Wrong property type!" );
198 m_bROCurrency
= pROStates
[nProp
];
201 case PROPERTYHANDLE_DECIMALSEPARATOR
:
204 if ( pValues
[nProp
] >>= bValue
)
205 m_bDecimalSeparator
= bValue
;
208 SAL_WARN( "unotools.config", "Wrong property type!" );
210 m_bRODecimalSeparator
= pROStates
[nProp
];
213 case PROPERTYHANDLE_DATEPATTERNS
:
216 if ( pValues
[nProp
] >>= aStr
)
217 m_aDatePatternsString
= aStr
;
220 SAL_WARN( "unotools.config", "Wrong property type!" );
222 m_bRODatePatterns
= pROStates
[nProp
];
225 case PROPERTYHANDLE_IGNORELANGCHANGE
:
228 if ( pValues
[nProp
] >>= bValue
)
229 m_bIgnoreLanguageChange
= bValue
;
232 SAL_WARN( "unotools.config", "Wrong property type!" );
234 m_bROIgnoreLanguageChange
= pROStates
[nProp
];
238 SAL_WARN( "unotools.config", "Wrong property type!" );
243 EnableNotification( aNames
);
249 SvtSysLocaleOptions_Impl::~SvtSysLocaleOptions_Impl()
255 void SvtSysLocaleOptions_Impl::MakeRealLocale()
257 if (m_aLocaleString
.isEmpty())
259 LanguageType nLang
= MsLangId::getSystemLanguage();
260 m_aRealLocale
.reset( nLang
).makeFallback();
264 m_aRealLocale
.reset( m_aLocaleString
).makeFallback();
268 void SvtSysLocaleOptions_Impl::MakeRealUILocale()
270 if (m_aUILocaleString
.isEmpty())
272 LanguageType nLang
= MsLangId::getSystemUILanguage();
273 m_aRealUILocale
.reset( nLang
).makeFallback();
277 m_aRealUILocale
.reset( m_aUILocaleString
).makeFallback();
281 bool SvtSysLocaleOptions_Impl::IsReadOnly( SvtSysLocaleOptions::EOption eOption
) const
283 bool bReadOnly
= CFG_READONLY_DEFAULT
;
286 case SvtSysLocaleOptions::E_LOCALE
:
288 bReadOnly
= m_bROLocale
;
291 case SvtSysLocaleOptions::E_UILOCALE
:
293 bReadOnly
= m_bROUILocale
;
296 case SvtSysLocaleOptions::E_CURRENCY
:
298 bReadOnly
= m_bROCurrency
;
301 case SvtSysLocaleOptions::E_DATEPATTERNS
:
303 bReadOnly
= m_bRODatePatterns
;
310 void SvtSysLocaleOptions_Impl::Commit()
312 const Sequence
< OUString
> aOrgNames
= GetPropertyNames();
313 sal_Int32 nOrgCount
= aOrgNames
.getLength();
315 Sequence
< OUString
> aNames( nOrgCount
);
316 Sequence
< Any
> aValues( nOrgCount
);
318 OUString
* pNames
= aNames
.getArray();
319 Any
* pValues
= aValues
.getArray();
320 sal_Int32 nRealCount
= 0;
322 for ( sal_Int32 nProp
= 0; nProp
< nOrgCount
; nProp
++ )
326 case PROPERTYHANDLE_LOCALE
:
330 pNames
[nRealCount
] = aOrgNames
[nProp
];
331 pValues
[nRealCount
] <<= m_aLocaleString
;
336 case PROPERTYHANDLE_UILOCALE
:
340 pNames
[nRealCount
] = aOrgNames
[nProp
];
341 pValues
[nRealCount
] <<= m_aUILocaleString
;
346 case PROPERTYHANDLE_CURRENCY
:
350 pNames
[nRealCount
] = aOrgNames
[nProp
];
351 pValues
[nRealCount
] <<= m_aCurrencyString
;
356 case PROPERTYHANDLE_DECIMALSEPARATOR
:
357 if( !m_bRODecimalSeparator
)
359 pNames
[nRealCount
] = aOrgNames
[nProp
];
360 pValues
[nRealCount
] <<= m_bDecimalSeparator
;
364 case PROPERTYHANDLE_DATEPATTERNS
:
365 if (!m_bRODatePatterns
)
367 pNames
[nRealCount
] = aOrgNames
[nProp
];
368 pValues
[nRealCount
] <<= m_aDatePatternsString
;
372 case PROPERTYHANDLE_IGNORELANGCHANGE
:
373 if (!m_bROIgnoreLanguageChange
)
375 pNames
[nRealCount
] = aOrgNames
[nProp
];
376 pValues
[nRealCount
] <<= m_bIgnoreLanguageChange
;
381 SAL_WARN( "unotools.config", "invalid index to save a path" );
384 aNames
.realloc(nRealCount
);
385 aValues
.realloc(nRealCount
);
386 PutProperties( aNames
, aValues
);
390 void SvtSysLocaleOptions_Impl::SetLocaleString( const OUString
& rStr
)
392 if (!m_bROLocale
&& rStr
!= m_aLocaleString
)
394 m_aLocaleString
= rStr
;
396 LanguageTag::setConfiguredSystemLanguage( m_aRealLocale
.getLanguageType() );
398 sal_uLong nHint
= SYSLOCALEOPTIONS_HINT_LOCALE
;
399 if ( m_aCurrencyString
.isEmpty() )
400 nHint
|= SYSLOCALEOPTIONS_HINT_CURRENCY
;
401 NotifyListeners( nHint
);
405 void SvtSysLocaleOptions_Impl::SetUILocaleString( const OUString
& rStr
)
407 if (!m_bROUILocale
&& rStr
!= m_aUILocaleString
)
409 m_aUILocaleString
= rStr
;
411 // as we can't switch UILocale at runtime, we only store changes in the configuration
414 NotifyListeners( SYSLOCALEOPTIONS_HINT_UILOCALE
);
418 void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString
& rStr
)
420 if (!m_bROCurrency
&& rStr
!= m_aCurrencyString
)
422 m_aCurrencyString
= rStr
;
424 NotifyListeners( SYSLOCALEOPTIONS_HINT_CURRENCY
);
428 void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString
& rStr
)
430 if (!m_bRODatePatterns
&& rStr
!= m_aDatePatternsString
)
432 m_aDatePatternsString
= rStr
;
434 NotifyListeners( SYSLOCALEOPTIONS_HINT_DATEPATTERNS
);
438 void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet
)
440 if(bSet
!= m_bDecimalSeparator
)
442 m_bDecimalSeparator
= bSet
;
444 NotifyListeners( SYSLOCALEOPTIONS_HINT_DECSEP
);
448 void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet
)
450 if(bSet
!= m_bIgnoreLanguageChange
)
452 m_bIgnoreLanguageChange
= bSet
;
454 NotifyListeners( SYSLOCALEOPTIONS_HINT_IGNORELANG
);
458 void SvtSysLocaleOptions_Impl::Notify( const Sequence
< OUString
>& seqPropertyNames
)
461 Sequence
< Any
> seqValues
= GetProperties( seqPropertyNames
);
462 Sequence
< sal_Bool
> seqROStates
= GetReadOnlyStates( seqPropertyNames
);
463 sal_Int32 nCount
= seqPropertyNames
.getLength();
464 for( sal_Int32 nProp
= 0; nProp
< nCount
; ++nProp
)
466 if( seqPropertyNames
[nProp
] == PROPERTYNAME_LOCALE
)
468 DBG_ASSERT( seqValues
[nProp
].getValueTypeClass() == TypeClass_STRING
, "Locale property type" );
469 seqValues
[nProp
] >>= m_aLocaleString
;
470 m_bROLocale
= seqROStates
[nProp
];
471 nHint
|= SYSLOCALEOPTIONS_HINT_LOCALE
;
472 if ( m_aCurrencyString
.isEmpty() )
473 nHint
|= SYSLOCALEOPTIONS_HINT_CURRENCY
;
476 if( seqPropertyNames
[nProp
] == PROPERTYNAME_UILOCALE
)
478 DBG_ASSERT( seqValues
[nProp
].getValueTypeClass() == TypeClass_STRING
, "Locale property type" );
479 seqValues
[nProp
] >>= m_aUILocaleString
;
480 m_bROUILocale
= seqROStates
[nProp
];
481 nHint
|= SYSLOCALEOPTIONS_HINT_UILOCALE
;
484 else if( seqPropertyNames
[nProp
] == PROPERTYNAME_CURRENCY
)
486 DBG_ASSERT( seqValues
[nProp
].getValueTypeClass() == TypeClass_STRING
, "Currency property type" );
487 seqValues
[nProp
] >>= m_aCurrencyString
;
488 m_bROCurrency
= seqROStates
[nProp
];
489 nHint
|= SYSLOCALEOPTIONS_HINT_CURRENCY
;
491 else if( seqPropertyNames
[nProp
] == PROPERTYNAME_DECIMALSEPARATOR
)
493 seqValues
[nProp
] >>= m_bDecimalSeparator
;
494 m_bRODecimalSeparator
= seqROStates
[nProp
];
496 else if( seqPropertyNames
[nProp
] == PROPERTYNAME_IGNORELANGCHANGE
)
498 seqValues
[nProp
] >>= m_bIgnoreLanguageChange
;
499 m_bROIgnoreLanguageChange
= seqROStates
[nProp
];
501 else if( seqPropertyNames
[nProp
] == PROPERTYNAME_DATEPATTERNS
)
503 DBG_ASSERT( seqValues
[nProp
].getValueTypeClass() == TypeClass_STRING
, "DatePatterns property type" );
504 seqValues
[nProp
] >>= m_aDatePatternsString
;
505 m_bRODatePatterns
= seqROStates
[nProp
];
506 nHint
|= SYSLOCALEOPTIONS_HINT_DATEPATTERNS
;
510 NotifyListeners( nHint
);
513 SvtSysLocaleOptions::SvtSysLocaleOptions()
515 MutexGuard
aGuard( GetMutex() );
518 pOptions
= new SvtSysLocaleOptions_Impl
;
520 ItemHolder1::holdConfigItem(E_SYSLOCALEOPTIONS
);
523 pOptions
->AddListener(this);
526 SvtSysLocaleOptions::~SvtSysLocaleOptions()
528 MutexGuard
aGuard( GetMutex() );
529 pOptions
->RemoveListener(this);
538 Mutex
& SvtSysLocaleOptions::GetMutex()
540 static Mutex
* pMutex
= NULL
;
543 MutexGuard
aGuard( Mutex::getGlobalMutex() );
546 // #i77768# Due to a static reference in the toolkit lib
547 // we need a mutex that lives longer than the svl library.
548 // Otherwise the dtor would use a destructed mutex!!
555 bool SvtSysLocaleOptions::IsModified()
557 MutexGuard
aGuard( GetMutex() );
558 return pOptions
->IsModified();
561 void SvtSysLocaleOptions::Commit()
563 MutexGuard
aGuard( GetMutex() );
567 void SvtSysLocaleOptions::BlockBroadcasts( bool bBlock
)
569 MutexGuard
aGuard( GetMutex() );
570 pOptions
->BlockBroadcasts( bBlock
);
573 const OUString
& SvtSysLocaleOptions::GetLocaleConfigString() const
575 MutexGuard
aGuard( GetMutex() );
576 return pOptions
->GetLocaleString();
579 void SvtSysLocaleOptions::SetLocaleConfigString( const OUString
& rStr
)
581 MutexGuard
aGuard( GetMutex() );
582 pOptions
->SetLocaleString( rStr
);
585 void SvtSysLocaleOptions::SetUILocaleConfigString( const OUString
& rStr
)
587 MutexGuard
aGuard( GetMutex() );
588 pOptions
->SetUILocaleString( rStr
);
591 const OUString
& SvtSysLocaleOptions::GetCurrencyConfigString() const
593 MutexGuard
aGuard( GetMutex() );
594 return pOptions
->GetCurrencyString();
597 void SvtSysLocaleOptions::SetCurrencyConfigString( const OUString
& rStr
)
599 MutexGuard
aGuard( GetMutex() );
600 pOptions
->SetCurrencyString( rStr
);
603 const OUString
& SvtSysLocaleOptions::GetDatePatternsConfigString() const
605 MutexGuard
aGuard( GetMutex() );
606 return pOptions
->GetDatePatternsString();
609 void SvtSysLocaleOptions::SetDatePatternsConfigString( const OUString
& rStr
)
611 MutexGuard
aGuard( GetMutex() );
612 pOptions
->SetDatePatternsString( rStr
);
615 bool SvtSysLocaleOptions::IsDecimalSeparatorAsLocale() const
617 MutexGuard
aGuard( GetMutex() );
618 return pOptions
->IsDecimalSeparatorAsLocale();
621 void SvtSysLocaleOptions::SetDecimalSeparatorAsLocale( bool bSet
)
623 MutexGuard
aGuard( GetMutex() );
624 pOptions
->SetDecimalSeparatorAsLocale(bSet
);
627 bool SvtSysLocaleOptions::IsIgnoreLanguageChange() const
629 MutexGuard
aGuard( GetMutex() );
630 return pOptions
->IsIgnoreLanguageChange();
633 void SvtSysLocaleOptions::SetIgnoreLanguageChange( bool bSet
)
635 MutexGuard
aGuard( GetMutex() );
636 pOptions
->SetIgnoreLanguageChange(bSet
);
639 bool SvtSysLocaleOptions::IsReadOnly( EOption eOption
) const
641 MutexGuard
aGuard( GetMutex() );
642 return pOptions
->IsReadOnly( eOption
);
646 void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString
& rAbbrev
,
648 const OUString
& rConfigString
)
650 sal_Int32 nDelim
= rConfigString
.indexOf( '-' );
653 rAbbrev
= rConfigString
.copy( 0, nDelim
);
654 OUString
aIsoStr( rConfigString
.copy( nDelim
+1 ) );
655 eLang
= LanguageTag::convertToLanguageTypeWithFallback( aIsoStr
);
659 rAbbrev
= rConfigString
;
660 eLang
= (rAbbrev
.isEmpty() ? LANGUAGE_SYSTEM
: LANGUAGE_NONE
);
665 OUString
SvtSysLocaleOptions::CreateCurrencyConfigString(
666 const OUString
& rAbbrev
, LanguageType eLang
)
668 OUString
aIsoStr( LanguageTag::convertToBcp47( eLang
) );
669 if ( !aIsoStr
.isEmpty() )
671 OUStringBuffer
aStr( rAbbrev
.getLength() + 1 + aIsoStr
.getLength() );
672 aStr
.append( rAbbrev
);
674 aStr
.append( aIsoStr
);
675 return aStr
.makeStringAndClear();
682 void SvtSysLocaleOptions::SetCurrencyChangeLink( const Link
& rLink
)
684 MutexGuard
aGuard( GetMutex() );
685 DBG_ASSERT( !CurrencyChangeLink::get().IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" );
686 CurrencyChangeLink::get() = rLink
;
690 const Link
& SvtSysLocaleOptions::GetCurrencyChangeLink()
692 MutexGuard
aGuard( GetMutex() );
693 return CurrencyChangeLink::get();
696 void SvtSysLocaleOptions::ConfigurationChanged( utl::ConfigurationBroadcaster
* p
, sal_uInt32 nHint
)
698 if ( nHint
& SYSLOCALEOPTIONS_HINT_CURRENCY
)
700 const Link
& rLink
= GetCurrencyChangeLink();
705 ::utl::detail::Options::ConfigurationChanged( p
, nHint
);
708 LanguageTag
SvtSysLocaleOptions::GetLanguageTag() const
710 return LanguageTag( GetLocaleConfigString() );
713 const LanguageTag
& SvtSysLocaleOptions::GetRealLanguageTag() const
715 return pOptions
->GetRealLocale();
718 const LanguageTag
& SvtSysLocaleOptions::GetRealUILanguageTag() const
720 return pOptions
->GetRealUILocale();
723 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */