xref: /aoo41x/main/unotools/source/misc/syslocale.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #ifndef GCC
31 #endif
32 
33 #include <unotools/syslocale.hxx>
34 #include <tools/string.hxx>
35 #include <unotools/syslocaleoptions.hxx>
36 #include <unotools/localedatawrapper.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <i18npool/mslangid.hxx>
39 #include <rtl/tencinfo.h>
40 #include <rtl/locale.h>
41 #include <osl/nlsupport.h>
42 
43 using namespace osl;
44 using namespace com::sun::star;
45 
46 
47 SvtSysLocale_Impl*  SvtSysLocale::pImpl = NULL;
48 sal_Int32           SvtSysLocale::nRefCount = 0;
49 
50 
51 class SvtSysLocale_Impl : public utl::ConfigurationListener
52 {
53 public:
54         SvtSysLocaleOptions     aSysLocaleOptions;
55         LocaleDataWrapper*      pLocaleData;
56         CharClass*              pCharClass;
57 
58                                 	SvtSysLocale_Impl();
59     virtual                     	~SvtSysLocale_Impl();
60 
61     CharClass*                  	GetCharClass();
62 	virtual void 					ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 );
63 };
64 
65 // -----------------------------------------------------------------------
66 
67 SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
68 {
69     pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
70 
71 	// listen for further changes
72     aSysLocaleOptions.AddListener( this );
73 }
74 
75 
76 SvtSysLocale_Impl::~SvtSysLocale_Impl()
77 {
78     aSysLocaleOptions.RemoveListener( this );
79     delete pCharClass;
80     delete pLocaleData;
81 }
82 
83 CharClass* SvtSysLocale_Impl::GetCharClass()
84 {
85     if ( !pCharClass )
86         pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
87     return pCharClass;
88 }
89 
90 void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint )
91 {
92     MutexGuard aGuard( SvtSysLocale::GetMutex() );
93 	if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE )
94 	{
95 		com::sun::star::lang::Locale aLocale( aSysLocaleOptions.GetRealLocale() );
96 		pLocaleData->setLocale( aLocale );
97 		GetCharClass()->setLocale( aLocale );
98 	}
99 }
100 
101 // ====================================================================
102 
103 SvtSysLocale::SvtSysLocale()
104 {
105     MutexGuard aGuard( GetMutex() );
106     if ( !pImpl )
107         pImpl = new SvtSysLocale_Impl;
108     ++nRefCount;
109 }
110 
111 
112 SvtSysLocale::~SvtSysLocale()
113 {
114     MutexGuard aGuard( GetMutex() );
115     if ( !--nRefCount )
116 	{
117         delete pImpl;
118         pImpl = NULL;
119 	}
120 }
121 
122 
123 // static
124 Mutex& SvtSysLocale::GetMutex()
125 {
126     static Mutex* pMutex = NULL;
127     if( !pMutex )
128     {
129         MutexGuard aGuard( Mutex::getGlobalMutex() );
130         if( !pMutex )
131         {
132             // #i77768# Due to a static reference in the toolkit lib
133             // we need a mutex that lives longer than the svl library.
134             // Otherwise the dtor would use a destructed mutex!!
135             pMutex = new Mutex;
136         }
137     }
138     return *pMutex;
139 }
140 
141 
142 const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
143 {
144     return *(pImpl->pLocaleData);
145 }
146 
147 
148 const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
149 {
150     return pImpl->pLocaleData;
151 }
152 
153 
154 const CharClass& SvtSysLocale::GetCharClass() const
155 {
156     return *(pImpl->GetCharClass());
157 }
158 
159 
160 const CharClass* SvtSysLocale::GetCharClassPtr() const
161 {
162     return pImpl->GetCharClass();
163 }
164 
165 SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
166 {
167 	return pImpl->aSysLocaleOptions;
168 }
169 
170 com::sun::star::lang::Locale SvtSysLocale::GetLocale() const
171 {
172 	return pImpl->aSysLocaleOptions.GetRealLocale();
173 }
174 
175 LanguageType SvtSysLocale::GetLanguage() const
176 {
177 	return pImpl->aSysLocaleOptions.GetRealLanguage();
178 }
179 
180 com::sun::star::lang::Locale SvtSysLocale::GetUILocale() const
181 {
182 	return pImpl->aSysLocaleOptions.GetRealUILocale();
183 }
184 
185 LanguageType SvtSysLocale::GetUILanguage() const
186 {
187 	return pImpl->aSysLocaleOptions.GetRealUILanguage();
188 }
189 
190 //------------------------------------------------------------------------
191 
192 // static
193 rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding()
194 {
195     const sal_Char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding(
196             gsl_getSystemTextEncoding() );
197     if ( !pCharSet )
198     {
199         // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
200         // language if possible.
201         ::com::sun::star::lang::Locale aLocale( SvtSysLocale().GetUILocale() );
202         rtl_Locale * pLocale = rtl_locale_register( aLocale.Language.getStr(),
203                 aLocale.Country.getStr(), aLocale.Variant.getStr() );
204         rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale );
205         pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc );
206     }
207     rtl_TextEncoding nRet;
208     if ( pCharSet )
209         nRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
210     else
211         nRet = RTL_TEXTENCODING_UTF8;
212     return nRet;
213 }
214 
215