1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SVL_ONDEMAND_HXX
21 #define INCLUDED_SVL_ONDEMAND_HXX
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>
37 On demand instantiation and initialization of several i18n wrappers,
38 helping the number formatter to not perform worse than it already does.
42 Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
44 SvNumberformatter uses it upon switching locales.
47 Avoids reloading and analysing of locale data again and again.
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.
55 class OnDemandLocaleDataWrapper
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
;
68 OnDemandLocaleDataWrapper()
69 : eLastAnyLanguage( LANGUAGE_DONTKNOW
)
74 pCurrent
= pSystem
= aSysLocale
.GetLocaleDataPtr();
75 eCurrentLanguage
= LANGUAGE_SYSTEM
;
77 OnDemandLocaleDataWrapper(
78 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
79 const LanguageTag
& rLanguageTag
86 pSystem
= aSysLocale
.GetLocaleDataPtr();
87 init( rxContext
, rLanguageTag
);
89 ~OnDemandLocaleDataWrapper()
95 bool isInitialized() const { return bInitialized
; }
97 bool is() const { return pCurrent
!= NULL
; }
100 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
101 const LanguageTag
& rLanguageTag
104 m_xContext
= rxContext
;
105 changeLocale( rLanguageTag
);
109 void changeLocale( const LanguageTag
& rLanguageTag
)
111 LanguageType eLang
= rLanguageTag
.getLanguageType( false);
114 case LANGUAGE_SYSTEM
:
117 case LANGUAGE_ENGLISH_US
:
119 pEnglish
= new LocaleDataWrapper( m_xContext
, rLanguageTag
);
125 pAny
= new LocaleDataWrapper( m_xContext
, rLanguageTag
);
126 eLastAnyLanguage
= eLang
;
128 else if ( eLastAnyLanguage
!= eLang
)
130 pAny
->setLanguageTag( rLanguageTag
);
131 eLastAnyLanguage
= eLang
;
135 eCurrentLanguage
= eLang
;
138 LanguageType
getCurrentLanguage() const
139 { return eCurrentLanguage
; }
141 LocaleDataWrapper
* getAnyLocale()
145 pAny
= new LocaleDataWrapper( m_xContext
, pCurrent
->getLanguageTag() );
146 eLastAnyLanguage
= eCurrentLanguage
;
148 else if ( pCurrent
!= pAny
)
150 pAny
->setLanguageTag( pCurrent
->getLanguageTag() );
151 eLastAnyLanguage
= eCurrentLanguage
;
156 const LocaleDataWrapper
* get() const { return pCurrent
; }
157 const LocaleDataWrapper
* operator->() const { return get(); }
158 const LocaleDataWrapper
& operator*() const { return *get(); }
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.
166 class OnDemandCalendarWrapper
168 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
169 ::com::sun::star::lang::Locale aLocale
;
170 mutable CalendarWrapper
* pPtr
;
175 OnDemandCalendarWrapper()
178 , bInitialized(false)
180 OnDemandCalendarWrapper(
181 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
182 ::com::sun::star::lang::Locale
& rLocale
185 , bInitialized(false)
187 init( rxContext
, rLocale
);
189 ~OnDemandCalendarWrapper()
194 bool isInitialized() const { return bInitialized
; }
196 bool is() const { return pPtr
!= NULL
; }
199 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
200 const ::com::sun::star::lang::Locale
& rLocale
203 m_xContext
= rxContext
;
204 changeLocale( rLocale
);
213 void changeLocale( const ::com::sun::star::lang::Locale
& rLocale
)
219 CalendarWrapper
* get() const
224 pPtr
= new CalendarWrapper( m_xContext
);
225 pPtr
->loadDefaultCalendar( aLocale
);
231 CalendarWrapper
* operator->() { return get(); }
232 CalendarWrapper
& operator*() { return *get(); }
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.
240 class OnDemandTransliterationWrapper
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
;
250 OnDemandTransliterationWrapper()
251 : eLanguage( LANGUAGE_SYSTEM
)
252 , nType(::com::sun::star::i18n::TransliterationModules_END_OF_MODULE
)
255 , bInitialized(false)
257 OnDemandTransliterationWrapper(
258 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
260 ::com::sun::star::i18n::TransliterationModules nTypeP
263 , bInitialized(false)
265 init( rxContext
, eLang
, nTypeP
);
267 ~OnDemandTransliterationWrapper()
272 bool isInitialized() const { return bInitialized
; }
274 bool is() const { return pPtr
!= NULL
; }
277 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
279 ::com::sun::star::i18n::TransliterationModules nTypeP
282 m_xContext
= rxContext
;
284 changeLocale( eLang
);
293 void changeLocale( LanguageType eLang
)
299 const ::utl::TransliterationWrapper
* get() const
304 pPtr
= new ::utl::TransliterationWrapper( m_xContext
, nType
);
305 pPtr
->loadModuleIfNeeded( eLanguage
);
311 const ::utl::TransliterationWrapper
* getForModule( const OUString
& rModule
, LanguageType eLang
) const
314 pPtr
= new ::utl::TransliterationWrapper( m_xContext
, nType
);
315 pPtr
->loadModuleByImplName( rModule
, eLang
);
316 bValid
= false; // reforce settings change in get()
320 const ::utl::TransliterationWrapper
* operator->() const { return get(); }
321 const ::utl::TransliterationWrapper
& operator*() const { return *get(); }
324 /** Load a native number service wrapper only if it's needed.
325 SvNumberformatter uses it.
328 If the default ctor is used the init() method MUST be called
329 before accessing the native number supplier.
331 class OnDemandNativeNumberWrapper
333 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
334 mutable NativeNumberWrapper
* pPtr
;
338 OnDemandNativeNumberWrapper()
340 , bInitialized(false)
342 OnDemandNativeNumberWrapper(
343 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
346 , bInitialized(false)
350 ~OnDemandNativeNumberWrapper()
355 bool isInitialized() const { return bInitialized
; }
358 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
361 m_xContext
= rxContext
;
370 bool is() const { return pPtr
!= NULL
; }
372 NativeNumberWrapper
* get() const
375 pPtr
= new NativeNumberWrapper( m_xContext
);
379 NativeNumberWrapper
* operator->() { return get(); }
380 NativeNumberWrapper
& operator*() { return *get(); }
383 #endif // INCLUDED_SVL_ONDEMAND_HXX
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */