LCOV - code coverage report
Current view: top level - include/svl - ondemand.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 102 112 91.1 %
Date: 2014-04-11 Functions: 22 25 88.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             : #ifndef INCLUDED_SVL_ONDEMAND_HXX
      21             : #define INCLUDED_SVL_ONDEMAND_HXX
      22             : 
      23             : #include <unotools/syslocale.hxx>
      24             : #include <i18nlangtag/lang.h>
      25             : #include <unotools/localedatawrapper.hxx>
      26             : #include <unotools/calendarwrapper.hxx>
      27             : #include <unotools/collatorwrapper.hxx>
      28             : #include <com/sun/star/i18n/CollatorOptions.hpp>
      29             : #include <unotools/transliterationwrapper.hxx>
      30             : #include <com/sun/star/i18n/TransliterationModules.hpp>
      31             : #include <unotools/nativenumberwrapper.hxx>
      32             : #include <com/sun/star/uno/Reference.hxx>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <comphelper/processfactory.hxx>
      35             : 
      36             : /*
      37             :     On demand instanciation and initialization of several i18n wrappers,
      38             :     helping the number formatter to not perform worse than it already does.
      39             :  */
      40             : 
      41             : /** @short
      42             :     Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
      43             :     LocaleDataWrapper.
      44             :     SvNumberformatter uses it upon switching locales.
      45             : 
      46             :     @descr
      47             :     Avoids reloading and analysing of locale data again and again.
      48             : 
      49             :     @ATTENTION
      50             :     If the default ctor is used the init() method MUST be called before
      51             :     accessing any locale data. The passed parameters Locale and LanguageType
      52             :     must match each other.
      53             :  */
      54             : 
      55             : class OnDemandLocaleDataWrapper
      56             : {
      57             :         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
      58             :             SvtSysLocale        aSysLocale;
      59             :             LanguageType        eCurrentLanguage;
      60             :             LanguageType        eLastAnyLanguage;
      61             :     const   LocaleDataWrapper*  pSystem;
      62             :     const   LocaleDataWrapper*  pEnglish;
      63             :             LocaleDataWrapper*  pAny;
      64             :     const   LocaleDataWrapper*  pCurrent;
      65             :             bool                bInitialized;
      66             : 
      67             : public:
      68       28573 :                                 OnDemandLocaleDataWrapper()
      69             :                                     : eLastAnyLanguage( LANGUAGE_DONTKNOW )
      70             :                                     , pEnglish(0)
      71             :                                     , pAny(0)
      72       28573 :                                     , bInitialized(false)
      73             :                                     {
      74       28573 :                                         pCurrent = pSystem = aSysLocale.GetLocaleDataPtr();
      75       28573 :                                         eCurrentLanguage = LANGUAGE_SYSTEM;
      76       28573 :                                     }
      77             :                                 OnDemandLocaleDataWrapper(
      78             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
      79             :                                     const LanguageTag& rLanguageTag
      80             :                                     )
      81             :                                     : pEnglish(0)
      82             :                                     , pAny(0)
      83             :                                     , pCurrent(0)
      84             :                                     , bInitialized(false)
      85             :                                     {
      86             :                                         pSystem = aSysLocale.GetLocaleDataPtr();
      87             :                                         init( rxContext, rLanguageTag );
      88             :                                     }
      89       26634 :                                 ~OnDemandLocaleDataWrapper()
      90       26634 :                                     {
      91       26634 :                                         delete pEnglish;
      92       26634 :                                         delete pAny;
      93       26634 :                                     }
      94             : 
      95           0 :             bool                isInitialized() const   { return bInitialized; }
      96             : 
      97             :             bool                is() const      { return pCurrent != NULL; }
      98             : 
      99        2758 :             void                init(
     100             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
     101             :                                     const LanguageTag& rLanguageTag
     102             :                                     )
     103             :                                     {
     104        2758 :                                         m_xContext = rxContext;
     105        2758 :                                         changeLocale( rLanguageTag );
     106        2758 :                                         bInitialized = true;
     107        2758 :                                     }
     108             : 
     109       16699 :             void                changeLocale( const LanguageTag& rLanguageTag )
     110             :                                     {
     111       16699 :                                         LanguageType eLang = rLanguageTag.getLanguageType( false);
     112       16699 :                                         switch ( eLang )
     113             :                                         {
     114             :                                             case LANGUAGE_SYSTEM :
     115        9404 :                                                 pCurrent = pSystem;
     116        9404 :                                             break;
     117             :                                             case LANGUAGE_ENGLISH_US :
     118        7190 :                                                 if ( !pEnglish )
     119         576 :                                                     pEnglish = new LocaleDataWrapper( m_xContext, rLanguageTag );
     120        7190 :                                                 pCurrent = pEnglish;
     121        7190 :                                             break;
     122             :                                             default:
     123         105 :                                                 if ( !pAny )
     124             :                                                 {
     125          47 :                                                     pAny = new LocaleDataWrapper( m_xContext, rLanguageTag );
     126          47 :                                                     eLastAnyLanguage = eLang;
     127             :                                                 }
     128          58 :                                                 else if ( eLastAnyLanguage != eLang )
     129             :                                                 {
     130          36 :                                                     pAny->setLanguageTag( rLanguageTag );
     131          36 :                                                     eLastAnyLanguage = eLang;
     132             :                                                 }
     133         105 :                                                 pCurrent = pAny;
     134             :                                         }
     135       16699 :                                         eCurrentLanguage = eLang;
     136       16699 :                                     }
     137             : 
     138           0 :             LanguageType        getCurrentLanguage() const
     139           0 :                                     { return eCurrentLanguage; }
     140             : 
     141             :             LocaleDataWrapper*  getAnyLocale()
     142             :                                     {
     143             :                                         if ( !pAny )
     144             :                                         {
     145             :                                             pAny = new LocaleDataWrapper( m_xContext, pCurrent->getLanguageTag() );
     146             :                                             eLastAnyLanguage = eCurrentLanguage;
     147             :                                         }
     148             :                                         else if ( pCurrent != pAny )
     149             :                                         {
     150             :                                             pAny->setLanguageTag( pCurrent->getLanguageTag() );
     151             :                                             eLastAnyLanguage = eCurrentLanguage;
     152             :                                         }
     153             :                                         return pAny;
     154             :                                     }
     155             : 
     156     2931954 :     const   LocaleDataWrapper*  get() const         { return pCurrent; }
     157       13974 :     const   LocaleDataWrapper*  operator->() const  { return get(); }
     158         672 :     const   LocaleDataWrapper&  operator*() const   { return *get(); }
     159             : };
     160             : 
     161             : /** Load a calendar only if it's needed.
     162             :     SvNumberformatter uses it upon switching locales.
     163             :     @ATTENTION If the default ctor is used the init() method MUST be called
     164             :     before accessing the calendar.
     165             :  */
     166             : class OnDemandCalendarWrapper
     167             : {
     168             :             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
     169             :             ::com::sun::star::lang::Locale  aLocale;
     170             :     mutable CalendarWrapper*    pPtr;
     171             :     mutable bool                bValid;
     172             :             bool                bInitialized;
     173             : 
     174             : public:
     175        2758 :                                 OnDemandCalendarWrapper()
     176             :                                     : pPtr(0)
     177             :                                     , bValid(false)
     178        2758 :                                     , bInitialized(false)
     179        2758 :                                     {}
     180             :                                 OnDemandCalendarWrapper(
     181             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
     182             :                                     ::com::sun::star::lang::Locale& rLocale
     183             :                                     )
     184             :                                     : bValid(false)
     185             :                                     , bInitialized(false)
     186             :                                     {
     187             :                                         init( rxContext, rLocale );
     188             :                                     }
     189        2534 :                                 ~OnDemandCalendarWrapper()
     190        2534 :                                     {
     191        2534 :                                         delete pPtr;
     192        2534 :                                     }
     193             : 
     194             :             bool                isInitialized() const   { return bInitialized; }
     195             : 
     196             :             bool                is() const      { return pPtr != NULL; }
     197             : 
     198        2758 :             void                init(
     199             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
     200             :                                     const ::com::sun::star::lang::Locale& rLocale
     201             :                                     )
     202             :                                     {
     203        2758 :                                         m_xContext = rxContext;
     204        2758 :                                         changeLocale( rLocale );
     205        2758 :                                         if ( pPtr )
     206             :                                         {
     207           0 :                                             delete pPtr;
     208           0 :                                             pPtr = NULL;
     209             :                                         }
     210        2758 :                                         bInitialized = true;
     211        2758 :                                     }
     212             : 
     213       16699 :             void                changeLocale( const ::com::sun::star::lang::Locale& rLocale )
     214             :                                     {
     215       16699 :                                         bValid = false;
     216       16699 :                                         aLocale = rLocale;
     217       16699 :                                     }
     218             : 
     219        8884 :             CalendarWrapper*    get() const
     220             :                                     {
     221        8884 :                                         if ( !bValid )
     222             :                                         {
     223         592 :                                             if ( !pPtr )
     224         120 :                                                 pPtr = new CalendarWrapper( m_xContext );
     225         592 :                                             pPtr->loadDefaultCalendar( aLocale );
     226         592 :                                             bValid = true;
     227             :                                         }
     228        8884 :                                         return pPtr;
     229             :                                     }
     230             : 
     231             :             CalendarWrapper*    operator->()    { return get(); }
     232             :             CalendarWrapper&    operator*()     { return *get(); }
     233             : };
     234             : 
     235             : /** Load a transliteration only if it's needed.
     236             :     SvNumberformatter uses it upon switching locales.
     237             :     @ATTENTION If the default ctor is used the init() method MUST be called
     238             :     before accessing the transliteration.
     239             :  */
     240             : class OnDemandTransliterationWrapper
     241             : {
     242             :         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
     243             :             LanguageType        eLanguage;
     244             :             ::com::sun::star::i18n::TransliterationModules  nType;
     245             :     mutable ::utl::TransliterationWrapper*  pPtr;
     246             :     mutable bool                bValid;
     247             :             bool                bInitialized;
     248             : 
     249             : public:
     250       28573 :                                 OnDemandTransliterationWrapper()
     251             :                                     : eLanguage( LANGUAGE_SYSTEM )
     252             :                                     , nType(::com::sun::star::i18n::TransliterationModules_END_OF_MODULE)
     253             :                                     , pPtr(0)
     254             :                                     , bValid(false)
     255       28573 :                                     , bInitialized(false)
     256       28573 :                                     {}
     257             :                                 OnDemandTransliterationWrapper(
     258             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
     259             :                                     LanguageType eLang,
     260             :                                     ::com::sun::star::i18n::TransliterationModules nTypeP
     261             :                                     )
     262             :                                     : bValid(false)
     263             :                                     , bInitialized(false)
     264             :                                     {
     265             :                                         init( rxContext, eLang, nTypeP );
     266             :                                     }
     267       26634 :                                 ~OnDemandTransliterationWrapper()
     268       26634 :                                     {
     269       26634 :                                         delete pPtr;
     270       26634 :                                     }
     271             : 
     272           0 :             bool                isInitialized() const   { return bInitialized; }
     273             : 
     274             :             bool                is() const      { return pPtr != NULL; }
     275             : 
     276        2758 :             void                init(
     277             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
     278             :                                     LanguageType eLang,
     279             :                                     ::com::sun::star::i18n::TransliterationModules nTypeP
     280             :                                     )
     281             :                                     {
     282        2758 :                                         m_xContext = rxContext;
     283        2758 :                                         nType = nTypeP;
     284        2758 :                                         changeLocale( eLang );
     285        2758 :                                         if ( pPtr )
     286             :                                         {
     287           0 :                                             delete pPtr;
     288           0 :                                             pPtr = NULL;
     289             :                                         }
     290        2758 :                                         bInitialized = true;
     291        2758 :                                     }
     292             : 
     293       16699 :             void                changeLocale( LanguageType eLang )
     294             :                                     {
     295       16699 :                                         bValid = false;
     296       16699 :                                         eLanguage = eLang;
     297       16699 :                                     }
     298             : 
     299        4382 :     const   ::utl::TransliterationWrapper*  get() const
     300             :                                     {
     301        4382 :                                         if ( !bValid )
     302             :                                         {
     303         602 :                                             if ( !pPtr )
     304         101 :                                                 pPtr = new ::utl::TransliterationWrapper( m_xContext, nType );
     305         602 :                                             pPtr->loadModuleIfNeeded( eLanguage );
     306         602 :                                             bValid = true;
     307             :                                         }
     308        4382 :                                         return pPtr;
     309             :                                     }
     310             : 
     311             :     const   ::utl::TransliterationWrapper*  getForModule( const OUString& rModule, LanguageType eLang ) const
     312             :                                     {
     313             :                                         if ( !pPtr )
     314             :                                             pPtr = new ::utl::TransliterationWrapper( m_xContext, nType );
     315             :                                         pPtr->loadModuleByImplName( rModule, eLang );
     316             :                                         bValid = false; // reforce settings change in get()
     317             :                                         return pPtr;
     318             :                                     }
     319             : 
     320           2 :     const   ::utl::TransliterationWrapper*  operator->() const  { return get(); }
     321             :     const   ::utl::TransliterationWrapper&  operator*() const   { return *get(); }
     322             : };
     323             : 
     324             : /** Load a native number service wrapper only if it's needed.
     325             :     SvNumberformatter uses it.
     326             : 
     327             :     @ATTENTION
     328             :     If the default ctor is used the init() method MUST be called
     329             :     before accessing the native number supplier.
     330             :  */
     331             : class OnDemandNativeNumberWrapper
     332             : {
     333             :         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
     334             :     mutable NativeNumberWrapper*    pPtr;
     335             :             bool                bInitialized;
     336             : 
     337             : public:
     338        2758 :                                 OnDemandNativeNumberWrapper()
     339             :                                     : pPtr(0)
     340        2758 :                                     , bInitialized(false)
     341        2758 :                                     {}
     342             :                                 OnDemandNativeNumberWrapper(
     343             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext
     344             :                                     )
     345             :                                     : pPtr(0)
     346             :                                     , bInitialized(false)
     347             :                                     {
     348             :                                         init( rxContext );
     349             :                                     }
     350        2534 :                                 ~OnDemandNativeNumberWrapper()
     351        2534 :                                     {
     352        2534 :                                         delete pPtr;
     353        2534 :                                     }
     354             : 
     355             :             bool                isInitialized() const   { return bInitialized; }
     356             : 
     357        2758 :             void                init(
     358             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext
     359             :                                     )
     360             :                                     {
     361        2758 :                                         m_xContext = rxContext;
     362        2758 :                                         if ( pPtr )
     363             :                                         {
     364           0 :                                             delete pPtr;
     365           0 :                                             pPtr = NULL;
     366             :                                         }
     367        2758 :                                         bInitialized = true;
     368        2758 :                                     }
     369             : 
     370             :             bool                is() const      { return pPtr != NULL; }
     371             : 
     372           2 :             NativeNumberWrapper*    get() const
     373             :                                     {
     374           2 :                                         if ( !pPtr )
     375           1 :                                             pPtr = new NativeNumberWrapper( m_xContext );
     376           2 :                                         return pPtr;
     377             :                                     }
     378             : 
     379             :             NativeNumberWrapper*    operator->()    { return get(); }
     380             :             NativeNumberWrapper&    operator*()     { return *get(); }
     381             : };
     382             : 
     383             : #endif // INCLUDED_SVL_ONDEMAND_HXX
     384             : 
     385             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10