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