LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/misc - syslocale.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 52 85 61.2 %
Date: 2012-12-17 Functions: 14 17 82.4 %
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/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          86 : SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL)
      57             : {
      58          86 :     pLocaleData = new LocaleDataWrapper( aSysLocaleOptions.GetRealLanguageTag() );
      59          86 :     setDateAcceptancePatternsConfig();
      60             : 
      61             :     // listen for further changes
      62          86 :     aSysLocaleOptions.AddListener( this );
      63          86 : }
      64             : 
      65             : 
      66          24 : SvtSysLocale_Impl::~SvtSysLocale_Impl()
      67             : {
      68           8 :     aSysLocaleOptions.RemoveListener( this );
      69           8 :     delete pCharClass;
      70           8 :     delete pLocaleData;
      71          16 : }
      72             : 
      73          34 : CharClass* SvtSysLocale_Impl::GetCharClass()
      74             : {
      75          34 :     if ( !pCharClass )
      76          34 :         pCharClass = new CharClass( aSysLocaleOptions.GetRealLanguageTag() );
      77          34 :     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          86 : void SvtSysLocale_Impl::setDateAcceptancePatternsConfig()
      97             : {
      98          86 :     OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
      99          86 :     if (aStr.isEmpty())
     100          86 :         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          86 :     }
     115          86 : }
     116             : 
     117             : // ====================================================================
     118             : 
     119       24229 : SvtSysLocale::SvtSysLocale()
     120             : {
     121       24229 :     MutexGuard aGuard( GetMutex() );
     122       24229 :     if ( !pImpl )
     123          86 :         pImpl = new SvtSysLocale_Impl;
     124       24229 :     ++nRefCount;
     125       24229 : }
     126             : 
     127             : 
     128       20105 : SvtSysLocale::~SvtSysLocale()
     129             : {
     130       20105 :     MutexGuard aGuard( GetMutex() );
     131       20105 :     if ( !--nRefCount )
     132             :     {
     133           8 :         delete pImpl;
     134           8 :         pImpl = NULL;
     135       20105 :     }
     136       20105 : }
     137             : 
     138             : 
     139             : // static
     140       44334 : Mutex& SvtSysLocale::GetMutex()
     141             : {
     142             :     static Mutex* pMutex = NULL;
     143       44334 :     if( !pMutex )
     144             :     {
     145          86 :         MutexGuard aGuard( Mutex::getGlobalMutex() );
     146          86 :         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          86 :             pMutex = new Mutex;
     152          86 :         }
     153             :     }
     154       44334 :     return *pMutex;
     155             : }
     156             : 
     157             : 
     158        1869 : const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
     159             : {
     160        1869 :     return *(pImpl->pLocaleData);
     161             : }
     162             : 
     163             : 
     164       10641 : const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const
     165             : {
     166       10641 :     return pImpl->pLocaleData;
     167             : }
     168             : 
     169             : 
     170           0 : const CharClass& SvtSysLocale::GetCharClass() const
     171             : {
     172           0 :     return *(pImpl->GetCharClass());
     173             : }
     174             : 
     175             : 
     176          34 : const CharClass* SvtSysLocale::GetCharClassPtr() const
     177             : {
     178          34 :     return pImpl->GetCharClass();
     179             : }
     180             : 
     181         180 : SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
     182             : {
     183         180 :     return pImpl->aSysLocaleOptions;
     184             : }
     185             : 
     186       22603 : const LanguageTag& SvtSysLocale::GetLanguageTag() const
     187             : {
     188       22603 :     return pImpl->aSysLocaleOptions.GetRealLanguageTag();
     189             : }
     190             : 
     191          86 : const LanguageTag& SvtSysLocale::GetUILanguageTag() const
     192             : {
     193          86 :     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: */

Generated by: LCOV version 1.10