Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include <unotools/syslocale.hxx>
22 : : #include <unotools/syslocaleoptions.hxx>
23 : : #include <unotools/localedatawrapper.hxx>
24 : : #include <comphelper/processfactory.hxx>
25 : : #include <i18npool/mslangid.hxx>
26 : : #include <rtl/tencinfo.h>
27 : : #include <rtl/locale.h>
28 : : #include <osl/nlsupport.h>
29 : :
30 : : using namespace osl;
31 : : using namespace com::sun::star;
32 : :
33 : :
34 : : SvtSysLocale_Impl* SvtSysLocale::pImpl = NULL;
35 : : sal_Int32 SvtSysLocale::nRefCount = 0;
36 : :
37 : :
38 : : class SvtSysLocale_Impl : public utl::ConfigurationListener
39 : : {
40 : : public:
41 : : SvtSysLocaleOptions aSysLocaleOptions;
42 : : LocaleDataWrapper* pLocaleData;
43 : : CharClass* pCharClass;
44 : :
45 : : SvtSysLocale_Impl();
46 : : virtual ~SvtSysLocale_Impl();
47 : :
48 : : CharClass* GetCharClass();
49 : : virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 );
50 : : };
51 : :
52 : : // -----------------------------------------------------------------------
53 : :
54 [ + - ]: 251 : SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
55 : : {
56 [ + - ][ + - ]: 251 : pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
[ + - ][ + - ]
57 : :
58 : : // listen for further changes
59 [ + - ]: 251 : aSysLocaleOptions.AddListener( this );
60 : 251 : }
61 : :
62 : :
63 [ + - ]: 150 : SvtSysLocale_Impl::~SvtSysLocale_Impl()
64 : : {
65 [ + - ]: 150 : aSysLocaleOptions.RemoveListener( this );
66 [ + + ][ + - ]: 150 : delete pCharClass;
67 [ + - ][ + - ]: 150 : delete pLocaleData;
68 [ - + ]: 300 : }
69 : :
70 : 26879 : CharClass* SvtSysLocale_Impl::GetCharClass()
71 : : {
72 [ + + ]: 26879 : if ( !pCharClass )
73 [ + - ][ + - ]: 59 : pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() );
[ + - ]
74 : 26879 : return pCharClass;
75 : : }
76 : :
77 : 0 : void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint )
78 : : {
79 [ # # ][ # # ]: 0 : MutexGuard aGuard( SvtSysLocale::GetMutex() );
80 [ # # ]: 0 : if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE )
81 : : {
82 [ # # ]: 0 : com::sun::star::lang::Locale aLocale( aSysLocaleOptions.GetRealLocale() );
83 [ # # ]: 0 : pLocaleData->setLocale( aLocale );
84 [ # # ][ # # ]: 0 : GetCharClass()->setLocale( aLocale );
85 [ # # ]: 0 : }
86 : 0 : }
87 : :
88 : : // ====================================================================
89 : :
90 : 81224 : SvtSysLocale::SvtSysLocale()
91 : : {
92 [ + - ][ + - ]: 81224 : MutexGuard aGuard( GetMutex() );
93 [ + + ]: 81224 : if ( !pImpl )
94 [ + - ][ + - ]: 251 : pImpl = new SvtSysLocale_Impl;
95 [ + - ]: 81224 : ++nRefCount;
96 : 81224 : }
97 : :
98 : :
99 : 80495 : SvtSysLocale::~SvtSysLocale()
100 : : {
101 [ + - ][ + - ]: 80495 : MutexGuard aGuard( GetMutex() );
102 [ + + ]: 80495 : if ( !--nRefCount )
103 : : {
104 [ + - ][ + - ]: 150 : delete pImpl;
105 : 150 : pImpl = NULL;
106 [ + - ]: 80495 : }
107 : 80495 : }
108 : :
109 : :
110 : : // static
111 : 161719 : Mutex& SvtSysLocale::GetMutex()
112 : : {
113 : : static Mutex* pMutex = NULL;
114 [ + + ]: 161719 : if( !pMutex )
115 : : {
116 [ + - ][ + - ]: 251 : MutexGuard aGuard( Mutex::getGlobalMutex() );
117 [ + - ]: 251 : if( !pMutex )
118 : : {
119 : : // #i77768# Due to a static reference in the toolkit lib
120 : : // we need a mutex that lives longer than the svl library.
121 : : // Otherwise the dtor would use a destructed mutex!!
122 [ + - ][ + - ]: 251 : pMutex = new Mutex;
123 [ + - ]: 251 : }
124 : : }
125 : 161719 : return *pMutex;
126 : : }
127 : :
128 : :
129 : 6728 : const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
130 : : {
131 : 6728 : return *(pImpl->pLocaleData);
132 : : }
133 : :
134 : :
135 : 33046 : const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
136 : : {
137 : 33046 : return pImpl->pLocaleData;
138 : : }
139 : :
140 : :
141 : 26828 : const CharClass& SvtSysLocale::GetCharClass() const
142 : : {
143 : 26828 : return *(pImpl->GetCharClass());
144 : : }
145 : :
146 : :
147 : 51 : const CharClass* SvtSysLocale::GetCharClassPtr() const
148 : : {
149 : 51 : return pImpl->GetCharClass();
150 : : }
151 : :
152 : 660 : SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
153 : : {
154 : 660 : return pImpl->aSysLocaleOptions;
155 : : }
156 : :
157 : 172 : com::sun::star::lang::Locale SvtSysLocale::GetLocale() const
158 : : {
159 : 172 : return pImpl->aSysLocaleOptions.GetRealLocale();
160 : : }
161 : :
162 : 1906510 : LanguageType SvtSysLocale::GetLanguage() const
163 : : {
164 : 1906510 : return pImpl->aSysLocaleOptions.GetRealLanguage();
165 : : }
166 : :
167 : 1006 : com::sun::star::lang::Locale SvtSysLocale::GetUILocale() const
168 : : {
169 : 1006 : return pImpl->aSysLocaleOptions.GetRealUILocale();
170 : : }
171 : :
172 : 602661 : LanguageType SvtSysLocale::GetUILanguage() const
173 : : {
174 : 602661 : return pImpl->aSysLocaleOptions.GetRealUILanguage();
175 : : }
176 : :
177 : : //------------------------------------------------------------------------
178 : :
179 : : // static
180 : 2 : rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding()
181 : : {
182 : : const sal_Char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding(
183 : 2 : osl_getThreadTextEncoding() );
184 [ - + ]: 2 : if ( !pCharSet )
185 : : {
186 : : // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
187 : : // language if possible.
188 [ # # ][ # # ]: 0 : ::com::sun::star::lang::Locale aLocale( SvtSysLocale().GetUILocale() );
[ # # ]
189 : : rtl_Locale * pLocale = rtl_locale_register( aLocale.Language.getStr(),
190 [ # # ]: 0 : aLocale.Country.getStr(), aLocale.Variant.getStr() );
191 [ # # ]: 0 : rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale );
192 [ # # ]: 0 : pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc );
193 : : }
194 : : rtl_TextEncoding nRet;
195 [ + - ]: 2 : if ( pCharSet )
196 : 2 : nRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
197 : : else
198 : 0 : nRet = RTL_TEXTENCODING_UTF8;
199 : 2 : return nRet;
200 : : }
201 : :
202 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|