1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b5088357SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b5088357SAndrew Rist * distributed with this work for additional information
6*b5088357SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b5088357SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist * with the License. You may obtain a copy of the License at
10*b5088357SAndrew Rist *
11*b5088357SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist *
13*b5088357SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist * software distributed under the License is distributed on an
15*b5088357SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist * KIND, either express or implied. See the License for the
17*b5088357SAndrew Rist * specific language governing permissions and limitations
18*b5088357SAndrew Rist * under the License.
19*b5088357SAndrew Rist *
20*b5088357SAndrew Rist *************************************************************/
21*b5088357SAndrew Rist
22*b5088357SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir #ifndef GCC
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <unotools/syslocale.hxx>
30cdf0e10cSrcweir #include <tools/string.hxx>
31cdf0e10cSrcweir #include <unotools/syslocaleoptions.hxx>
32cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
33cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
34cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
35cdf0e10cSrcweir #include <rtl/tencinfo.h>
36cdf0e10cSrcweir #include <rtl/locale.h>
37cdf0e10cSrcweir #include <osl/nlsupport.h>
38cdf0e10cSrcweir
39cdf0e10cSrcweir using namespace osl;
40cdf0e10cSrcweir using namespace com::sun::star;
41cdf0e10cSrcweir
42cdf0e10cSrcweir
43cdf0e10cSrcweir SvtSysLocale_Impl* SvtSysLocale::pImpl = NULL;
44cdf0e10cSrcweir sal_Int32 SvtSysLocale::nRefCount = 0;
45cdf0e10cSrcweir
46cdf0e10cSrcweir
47cdf0e10cSrcweir class SvtSysLocale_Impl : public utl::ConfigurationListener
48cdf0e10cSrcweir {
49cdf0e10cSrcweir public:
50cdf0e10cSrcweir SvtSysLocaleOptions aSysLocaleOptions;
51cdf0e10cSrcweir LocaleDataWrapper* pLocaleData;
52cdf0e10cSrcweir CharClass* pCharClass;
53cdf0e10cSrcweir
54cdf0e10cSrcweir SvtSysLocale_Impl();
55cdf0e10cSrcweir virtual ~SvtSysLocale_Impl();
56cdf0e10cSrcweir
57cdf0e10cSrcweir CharClass* GetCharClass();
58cdf0e10cSrcweir virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 );
59cdf0e10cSrcweir };
60cdf0e10cSrcweir
61cdf0e10cSrcweir // -----------------------------------------------------------------------
62cdf0e10cSrcweir
SvtSysLocale_Impl()63cdf0e10cSrcweir SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
66cdf0e10cSrcweir
67cdf0e10cSrcweir // listen for further changes
68cdf0e10cSrcweir aSysLocaleOptions.AddListener( this );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir
~SvtSysLocale_Impl()72cdf0e10cSrcweir SvtSysLocale_Impl::~SvtSysLocale_Impl()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir aSysLocaleOptions.RemoveListener( this );
75cdf0e10cSrcweir delete pCharClass;
76cdf0e10cSrcweir delete pLocaleData;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
GetCharClass()79cdf0e10cSrcweir CharClass* SvtSysLocale_Impl::GetCharClass()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir if ( !pCharClass )
82cdf0e10cSrcweir pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
83cdf0e10cSrcweir return pCharClass;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
ConfigurationChanged(utl::ConfigurationBroadcaster *,sal_uInt32 nHint)86cdf0e10cSrcweir void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir MutexGuard aGuard( SvtSysLocale::GetMutex() );
89cdf0e10cSrcweir if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir com::sun::star::lang::Locale aLocale( aSysLocaleOptions.GetRealLocale() );
92cdf0e10cSrcweir pLocaleData->setLocale( aLocale );
93cdf0e10cSrcweir GetCharClass()->setLocale( aLocale );
94cdf0e10cSrcweir }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir
97cdf0e10cSrcweir // ====================================================================
98cdf0e10cSrcweir
SvtSysLocale()99cdf0e10cSrcweir SvtSysLocale::SvtSysLocale()
100cdf0e10cSrcweir {
101cdf0e10cSrcweir MutexGuard aGuard( GetMutex() );
102cdf0e10cSrcweir if ( !pImpl )
103cdf0e10cSrcweir pImpl = new SvtSysLocale_Impl;
104cdf0e10cSrcweir ++nRefCount;
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir
~SvtSysLocale()108cdf0e10cSrcweir SvtSysLocale::~SvtSysLocale()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir MutexGuard aGuard( GetMutex() );
111cdf0e10cSrcweir if ( !--nRefCount )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir delete pImpl;
114cdf0e10cSrcweir pImpl = NULL;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir
119cdf0e10cSrcweir // static
GetMutex()120cdf0e10cSrcweir Mutex& SvtSysLocale::GetMutex()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir static Mutex* pMutex = NULL;
123cdf0e10cSrcweir if( !pMutex )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir MutexGuard aGuard( Mutex::getGlobalMutex() );
126cdf0e10cSrcweir if( !pMutex )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir // #i77768# Due to a static reference in the toolkit lib
129cdf0e10cSrcweir // we need a mutex that lives longer than the svl library.
130cdf0e10cSrcweir // Otherwise the dtor would use a destructed mutex!!
131cdf0e10cSrcweir pMutex = new Mutex;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir }
134cdf0e10cSrcweir return *pMutex;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir
GetLocaleData() const138cdf0e10cSrcweir const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
139cdf0e10cSrcweir {
140cdf0e10cSrcweir return *(pImpl->pLocaleData);
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
143cdf0e10cSrcweir
GetLocaleDataPtr() const144cdf0e10cSrcweir const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir return pImpl->pLocaleData;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir
149cdf0e10cSrcweir
GetCharClass() const150cdf0e10cSrcweir const CharClass& SvtSysLocale::GetCharClass() const
151cdf0e10cSrcweir {
152cdf0e10cSrcweir return *(pImpl->GetCharClass());
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir
GetCharClassPtr() const156cdf0e10cSrcweir const CharClass* SvtSysLocale::GetCharClassPtr() const
157cdf0e10cSrcweir {
158cdf0e10cSrcweir return pImpl->GetCharClass();
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
GetOptions() const161cdf0e10cSrcweir SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir return pImpl->aSysLocaleOptions;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
GetLocale() const166cdf0e10cSrcweir com::sun::star::lang::Locale SvtSysLocale::GetLocale() const
167cdf0e10cSrcweir {
168cdf0e10cSrcweir return pImpl->aSysLocaleOptions.GetRealLocale();
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
GetLanguage() const171cdf0e10cSrcweir LanguageType SvtSysLocale::GetLanguage() const
172cdf0e10cSrcweir {
173cdf0e10cSrcweir return pImpl->aSysLocaleOptions.GetRealLanguage();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir
GetUILocale() const176cdf0e10cSrcweir com::sun::star::lang::Locale SvtSysLocale::GetUILocale() const
177cdf0e10cSrcweir {
178cdf0e10cSrcweir return pImpl->aSysLocaleOptions.GetRealUILocale();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
GetUILanguage() const181cdf0e10cSrcweir LanguageType SvtSysLocale::GetUILanguage() const
182cdf0e10cSrcweir {
183cdf0e10cSrcweir return pImpl->aSysLocaleOptions.GetRealUILanguage();
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir //------------------------------------------------------------------------
187cdf0e10cSrcweir
188cdf0e10cSrcweir // static
GetBestMimeEncoding()189cdf0e10cSrcweir rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding()
190cdf0e10cSrcweir {
191cdf0e10cSrcweir const sal_Char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding(
192cdf0e10cSrcweir gsl_getSystemTextEncoding() );
193cdf0e10cSrcweir if ( !pCharSet )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
196cdf0e10cSrcweir // language if possible.
197cdf0e10cSrcweir ::com::sun::star::lang::Locale aLocale( SvtSysLocale().GetUILocale() );
198cdf0e10cSrcweir rtl_Locale * pLocale = rtl_locale_register( aLocale.Language.getStr(),
199cdf0e10cSrcweir aLocale.Country.getStr(), aLocale.Variant.getStr() );
200cdf0e10cSrcweir rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale );
201cdf0e10cSrcweir pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc );
202cdf0e10cSrcweir }
203cdf0e10cSrcweir rtl_TextEncoding nRet;
204cdf0e10cSrcweir if ( pCharSet )
205cdf0e10cSrcweir nRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
206cdf0e10cSrcweir else
207cdf0e10cSrcweir nRet = RTL_TEXTENCODING_UTF8;
208cdf0e10cSrcweir return nRet;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211