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 <comphelper/processfactory.hxx>
24 : #include <rtl/tencinfo.h>
25 : #include <rtl/locale.h>
26 : #include <osl/nlsupport.h>
27 : #include <vector>
28 :
29 : using namespace osl;
30 : using namespace com::sun::star;
31 :
32 :
33 : SvtSysLocale_Impl* SvtSysLocale::pImpl = NULL;
34 : sal_Int32 SvtSysLocale::nRefCount = 0;
35 :
36 :
37 : class SvtSysLocale_Impl : public utl::ConfigurationListener
38 : {
39 : public:
40 : SvtSysLocaleOptions aSysLocaleOptions;
41 : LocaleDataWrapper* pLocaleData;
42 : CharClass* pCharClass;
43 :
44 : SvtSysLocale_Impl();
45 : virtual ~SvtSysLocale_Impl();
46 :
47 : CharClass* GetCharClass();
48 : virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 );
49 :
50 : private:
51 : void setDateAcceptancePatternsConfig();
52 : };
53 :
54 : // -----------------------------------------------------------------------
55 :
56 26 : SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
57 : {
58 26 : pLocaleData = new LocaleDataWrapper( aSysLocaleOptions.GetRealLanguageTag() );
59 26 : setDateAcceptancePatternsConfig();
60 :
61 : // listen for further changes
62 26 : aSysLocaleOptions.AddListener( this );
63 26 : }
64 :
65 :
66 0 : SvtSysLocale_Impl::~SvtSysLocale_Impl()
67 : {
68 0 : aSysLocaleOptions.RemoveListener( this );
69 0 : delete pCharClass;
70 0 : delete pLocaleData;
71 0 : }
72 :
73 5 : CharClass* SvtSysLocale_Impl::GetCharClass()
74 : {
75 5 : if ( !pCharClass )
76 5 : pCharClass = new CharClass( aSysLocaleOptions.GetRealLanguageTag() );
77 5 : return pCharClass;
78 : }
79 :
80 0 : void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint )
81 : {
82 0 : MutexGuard aGuard( SvtSysLocale::GetMutex() );
83 :
84 0 : if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE )
85 : {
86 0 : const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
87 0 : pLocaleData->setLanguageTag( rLanguageTag );
88 0 : GetCharClass()->setLanguageTag( rLanguageTag );
89 : }
90 0 : if ( nHint & SYSLOCALEOPTIONS_HINT_DATEPATTERNS )
91 : {
92 0 : setDateAcceptancePatternsConfig();
93 0 : }
94 0 : }
95 :
96 26 : void SvtSysLocale_Impl::setDateAcceptancePatternsConfig()
97 : {
98 26 : OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
99 26 : if (aStr.isEmpty())
100 26 : pLocaleData->setDateAcceptancePatterns( uno::Sequence<OUString>()); // reset
101 : else
102 : {
103 0 : ::std::vector< OUString > aVec;
104 0 : for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
105 : {
106 0 : OUString aTok( aStr.getToken( 0, ';', nIndex));
107 0 : if (!aTok.isEmpty())
108 0 : aVec.push_back( aTok);
109 0 : }
110 0 : uno::Sequence< OUString > aSeq( aVec.size());
111 0 : for (sal_Int32 i=0; i < aSeq.getLength(); ++i)
112 0 : aSeq[i] = aVec[i];
113 0 : pLocaleData->setDateAcceptancePatterns( aSeq);
114 26 : }
115 26 : }
116 :
117 : // ====================================================================
118 :
119 11349 : SvtSysLocale::SvtSysLocale()
120 : {
121 11349 : MutexGuard aGuard( GetMutex() );
122 11349 : if ( !pImpl )
123 26 : pImpl = new SvtSysLocale_Impl;
124 11349 : ++nRefCount;
125 11349 : }
126 :
127 :
128 9465 : SvtSysLocale::~SvtSysLocale()
129 : {
130 9465 : MutexGuard aGuard( GetMutex() );
131 9465 : if ( !--nRefCount )
132 : {
133 0 : delete pImpl;
134 0 : pImpl = NULL;
135 9465 : }
136 9465 : }
137 :
138 :
139 : // static
140 20814 : Mutex& SvtSysLocale::GetMutex()
141 : {
142 : static Mutex* pMutex = NULL;
143 20814 : if( !pMutex )
144 : {
145 26 : MutexGuard aGuard( Mutex::getGlobalMutex() );
146 26 : if( !pMutex )
147 : {
148 : // #i77768# Due to a static reference in the toolkit lib
149 : // we need a mutex that lives longer than the svl library.
150 : // Otherwise the dtor would use a destructed mutex!!
151 26 : pMutex = new Mutex;
152 26 : }
153 : }
154 20814 : return *pMutex;
155 : }
156 :
157 :
158 907 : const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
159 : {
160 907 : return *(pImpl->pLocaleData);
161 : }
162 :
163 :
164 4734 : const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
165 : {
166 4734 : return pImpl->pLocaleData;
167 : }
168 :
169 :
170 0 : const CharClass& SvtSysLocale::GetCharClass() const
171 : {
172 0 : return *(pImpl->GetCharClass());
173 : }
174 :
175 :
176 5 : const CharClass* SvtSysLocale::GetCharClassPtr() const
177 : {
178 5 : return pImpl->GetCharClass();
179 : }
180 :
181 52 : SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
182 : {
183 52 : return pImpl->aSysLocaleOptions;
184 : }
185 :
186 11283 : const LanguageTag& SvtSysLocale::GetLanguageTag() const
187 : {
188 11283 : return pImpl->aSysLocaleOptions.GetRealLanguageTag();
189 : }
190 :
191 26 : const LanguageTag& SvtSysLocale::GetUILanguageTag() const
192 : {
193 26 : return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
194 : }
195 :
196 : //------------------------------------------------------------------------
197 :
198 : // static
199 0 : rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding()
200 : {
201 : const sal_Char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding(
202 0 : osl_getThreadTextEncoding() );
203 0 : if ( !pCharSet )
204 : {
205 : // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
206 : // language if possible.
207 0 : ::com::sun::star::lang::Locale aLocale( SvtSysLocale().GetUILanguageTag().getLocale() );
208 : rtl_Locale * pLocale = rtl_locale_register( aLocale.Language.getStr(),
209 0 : aLocale.Country.getStr(), aLocale.Variant.getStr() );
210 0 : rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale );
211 0 : pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc );
212 : }
213 : rtl_TextEncoding nRet;
214 0 : if ( pCharSet )
215 0 : nRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
216 : else
217 0 : nRet = RTL_TEXTENCODING_UTF8;
218 0 : return nRet;
219 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|