LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/unotools/source/misc - syslocale.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 69 86 80.2 %
Date: 2013-07-09 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          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/thread.h>
      27             : #include <osl/nlsupport.h>
      28             : #include <vector>
      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             : private:
      52             :     void                        setDateAcceptancePatternsConfig();
      53             : };
      54             : 
      55             : // -----------------------------------------------------------------------
      56             : 
      57         147 : SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
      58             : {
      59         147 :     pLocaleData = new LocaleDataWrapper( aSysLocaleOptions.GetRealLanguageTag() );
      60         147 :     setDateAcceptancePatternsConfig();
      61             : 
      62             :     // listen for further changes
      63         147 :     aSysLocaleOptions.AddListener( this );
      64         147 : }
      65             : 
      66             : 
      67         279 : SvtSysLocale_Impl::~SvtSysLocale_Impl()
      68             : {
      69          93 :     aSysLocaleOptions.RemoveListener( this );
      70          93 :     delete pCharClass;
      71          93 :     delete pLocaleData;
      72         186 : }
      73             : 
      74          46 : CharClass* SvtSysLocale_Impl::GetCharClass()
      75             : {
      76          46 :     if ( !pCharClass )
      77          33 :         pCharClass = new CharClass( aSysLocaleOptions.GetRealLanguageTag() );
      78          46 :     return pCharClass;
      79             : }
      80             : 
      81           1 : void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint )
      82             : {
      83           1 :     MutexGuard aGuard( SvtSysLocale::GetMutex() );
      84             : 
      85           1 :     if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE )
      86             :     {
      87           1 :         const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
      88           1 :         pLocaleData->setLanguageTag( rLanguageTag );
      89           1 :         GetCharClass()->setLanguageTag( rLanguageTag );
      90             :     }
      91           1 :     if ( nHint & SYSLOCALEOPTIONS_HINT_DATEPATTERNS )
      92             :     {
      93           0 :         setDateAcceptancePatternsConfig();
      94           1 :     }
      95           1 : }
      96             : 
      97         147 : void SvtSysLocale_Impl::setDateAcceptancePatternsConfig()
      98             : {
      99         147 :     OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
     100         147 :     if (aStr.isEmpty())
     101         147 :         pLocaleData->setDateAcceptancePatterns( uno::Sequence<OUString>());     // reset
     102             :     else
     103             :     {
     104           0 :         ::std::vector< OUString > aVec;
     105           0 :         for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
     106             :         {
     107           0 :             OUString aTok( aStr.getToken( 0, ';', nIndex));
     108           0 :             if (!aTok.isEmpty())
     109           0 :                 aVec.push_back( aTok);
     110           0 :         }
     111           0 :         uno::Sequence< OUString > aSeq( aVec.size());
     112           0 :         for (sal_Int32 i=0; i < aSeq.getLength(); ++i)
     113           0 :             aSeq[i] = aVec[i];
     114           0 :         pLocaleData->setDateAcceptancePatterns( aSeq);
     115         147 :     }
     116         147 : }
     117             : 
     118             : // ====================================================================
     119             : 
     120       48163 : SvtSysLocale::SvtSysLocale()
     121             : {
     122       48163 :     MutexGuard aGuard( GetMutex() );
     123       48163 :     if ( !pImpl )
     124         147 :         pImpl = new SvtSysLocale_Impl;
     125       48163 :     ++nRefCount;
     126       48163 : }
     127             : 
     128             : 
     129       47931 : SvtSysLocale::~SvtSysLocale()
     130             : {
     131       47931 :     MutexGuard aGuard( GetMutex() );
     132       47931 :     if ( !--nRefCount )
     133             :     {
     134          93 :         delete pImpl;
     135          93 :         pImpl = NULL;
     136       47931 :     }
     137       47931 : }
     138             : 
     139             : 
     140             : // static
     141       96095 : Mutex& SvtSysLocale::GetMutex()
     142             : {
     143             :     static Mutex* pMutex = NULL;
     144       96095 :     if( !pMutex )
     145             :     {
     146         145 :         MutexGuard aGuard( Mutex::getGlobalMutex() );
     147         145 :         if( !pMutex )
     148             :         {
     149             :             // #i77768# Due to a static reference in the toolkit lib
     150             :             // we need a mutex that lives longer than the svl library.
     151             :             // Otherwise the dtor would use a destructed mutex!!
     152         145 :             pMutex = new Mutex;
     153         145 :         }
     154             :     }
     155       96095 :     return *pMutex;
     156             : }
     157             : 
     158             : 
     159        4254 : const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
     160             : {
     161        4254 :     return *(pImpl->pLocaleData);
     162             : }
     163             : 
     164             : 
     165       21168 : const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
     166             : {
     167       21168 :     return pImpl->pLocaleData;
     168             : }
     169             : 
     170             : 
     171          14 : const CharClass& SvtSysLocale::GetCharClass() const
     172             : {
     173          14 :     return *(pImpl->GetCharClass());
     174             : }
     175             : 
     176             : 
     177          31 : const CharClass* SvtSysLocale::GetCharClassPtr() const
     178             : {
     179          31 :     return pImpl->GetCharClass();
     180             : }
     181             : 
     182         383 : SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
     183             : {
     184         383 :     return pImpl->aSysLocaleOptions;
     185             : }
     186             : 
     187       14902 : const LanguageTag& SvtSysLocale::GetLanguageTag() const
     188             : {
     189       14902 :     return pImpl->aSysLocaleOptions.GetRealLanguageTag();
     190             : }
     191             : 
     192         144 : const LanguageTag& SvtSysLocale::GetUILanguageTag() const
     193             : {
     194         144 :     return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
     195             : }
     196             : 
     197             : //------------------------------------------------------------------------
     198             : 
     199             : // static
     200           2 : rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding()
     201             : {
     202             :     const sal_Char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding(
     203           2 :             osl_getThreadTextEncoding() );
     204           2 :     if ( !pCharSet )
     205             :     {
     206             :         // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
     207             :         // language if possible.
     208           0 :         SvtSysLocale aSysLocale;
     209           0 :         const LanguageTag& rLanguageTag = aSysLocale.GetUILanguageTag();
     210             :         // Converting blindly to Locale and then to rtl_Locale may feed the
     211             :         // 'qlt' to rtl_locale_register() and the underlying system locale
     212             :         // stuff, which doesn't know about it nor about BCP47 in the Variant
     213             :         // field. So use the real language and for non-pure ISO cases hope for
     214             :         // the best.. the fallback to UTF-8 should solve these cases nowadays.
     215             :         /* FIXME-BCP47: the script needs to go in here as well, so actually
     216             :          * we'd need some variant fiddling or glibc locale string and tweak
     217             :          * rtl_locale_register() to know about it! But then again the Windows
     218             :          * implementation still wouldn't know anything about it ... */
     219             :         SAL_WARN_IF( !rLanguageTag.isIsoLocale(), "unotools.i18n",
     220             :                 "SvtSysLocale::GetBestMimeEncoding - non-ISO UI locale");
     221             :         rtl_Locale * pLocale = rtl_locale_register( rLanguageTag.getLanguage().getStr(),
     222           0 :                 rLanguageTag.getCountry().getStr(), OUString().getStr() );
     223           0 :         rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale );
     224           0 :         pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc );
     225             :     }
     226             :     rtl_TextEncoding nRet;
     227           2 :     if ( pCharSet )
     228           2 :         nRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
     229             :     else
     230           0 :         nRet = RTL_TEXTENCODING_UTF8;
     231           2 :     return nRet;
     232             : }
     233             : 
     234             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10