1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ondemand.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_SVTOOLS_ONDEMAND_HXX
32 #define INCLUDED_SVTOOLS_ONDEMAND_HXX
34 #ifndef INCLUDED_SVTOOLS_SYSLOCALE_HXX
35 #include <bf_svtools/syslocale.hxx>
38 #ifndef INCLUDED_I18NPOOL_LANG_H
39 #include <i18npool/lang.h>
42 #ifndef _UNOTOOLS_LOCALEDATAWRAPPER_HXX
43 #include <unotools/localedatawrapper.hxx>
45 #ifndef _UNOTOOLS_CALENDARWRAPPER_HXX
46 #include <unotools/calendarwrapper.hxx>
48 #ifndef _UNOTOOLS_COLLATORWRAPPER_HXX
49 #include <unotools/collatorwrapper.hxx>
51 #ifndef _COM_SUN_STAR_I18N_COLLATOROPTIONS_HPP_
52 #include <com/sun/star/i18n/CollatorOptions.hpp>
54 #ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
55 #include <unotools/transliterationwrapper.hxx>
57 #ifndef _COM_SUN_STAR_I18N_TRANSLITERATIONMODULES_HPP_
58 #include <com/sun/star/i18n/TransliterationModules.hpp>
60 #ifndef _UNOTOOLS_NATIVENUMBERWRAPPER_HXX
61 #include <unotools/nativenumberwrapper.hxx>
63 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HXX_
64 #include <com/sun/star/uno/Reference.hxx>
66 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
67 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
74 On demand instanciation and initialization of several i18n wrappers,
75 helping the number formatter to not perform worse than it already does.
79 Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
81 SvNumberformatter uses it upon switching locales.
84 Avoids reloading and analysing of locale data again and again.
87 If the default ctor is used the init() method MUST be called before
88 accessing any locale data. The passed parameters Locale and LanguageType
89 must match each other.
92 class OnDemandLocaleDataWrapper
94 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xSMgr
;
95 SvtSysLocale aSysLocale
;
96 LanguageType eCurrentLanguage
;
97 LanguageType eLastAnyLanguage
;
98 const LocaleDataWrapper
* pSystem
;
99 const LocaleDataWrapper
* pEnglish
;
100 LocaleDataWrapper
* pAny
;
101 const LocaleDataWrapper
* pCurrent
;
105 OnDemandLocaleDataWrapper()
106 : eLastAnyLanguage( LANGUAGE_DONTKNOW
)
109 , bInitialized(false)
111 pCurrent
= pSystem
= aSysLocale
.GetLocaleDataPtr();
112 eCurrentLanguage
= LANGUAGE_SYSTEM
;
114 OnDemandLocaleDataWrapper(
115 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
116 ::com::sun::star::lang::Locale
& rLocale
,
122 , bInitialized(false)
124 pSystem
= aSysLocale
.GetLocaleDataPtr();
125 init( rxSMgr
, rLocale
, eLang
);
127 ~OnDemandLocaleDataWrapper()
133 bool isInitialized() const { return bInitialized
; }
135 bool is() const { return pCurrent
!= NULL
; }
138 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
139 ::com::sun::star::lang::Locale
& rLocale
,
144 changeLocale( rLocale
, eLang
);
148 void changeLocale( ::com::sun::star::lang::Locale
& rLocale
, LanguageType eLang
)
152 case LANGUAGE_SYSTEM
:
155 case LANGUAGE_ENGLISH_US
:
157 pEnglish
= new LocaleDataWrapper( xSMgr
, rLocale
);
163 pAny
= new LocaleDataWrapper( xSMgr
, rLocale
);
164 eLastAnyLanguage
= eLang
;
166 else if ( eLastAnyLanguage
!= eLang
)
168 pAny
->setLocale( rLocale
);
169 eLastAnyLanguage
= eLang
;
173 eCurrentLanguage
= eLang
;
176 LanguageType
getCurrentLanguage() const
177 { return eCurrentLanguage
; }
179 LocaleDataWrapper
* getAnyLocale()
183 pAny
= new LocaleDataWrapper( xSMgr
, pCurrent
->getLocale() );
184 eLastAnyLanguage
= eCurrentLanguage
;
186 else if ( pCurrent
!= pAny
)
188 pAny
->setLocale( pCurrent
->getLocale() );
189 eLastAnyLanguage
= eCurrentLanguage
;
194 const LocaleDataWrapper
* get() const { return pCurrent
; }
195 const LocaleDataWrapper
* operator->() const { return get(); }
196 const LocaleDataWrapper
& operator*() const { return *get(); }
199 /** Load a calendar only if it's needed.
200 SvNumberformatter uses it upon switching locales.
201 @ATTENTION If the default ctor is used the init() method MUST be called
202 before accessing the calendar.
204 class OnDemandCalendarWrapper
206 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xSMgr
;
207 ::com::sun::star::lang::Locale aLocale
;
208 mutable CalendarWrapper
* pPtr
;
213 OnDemandCalendarWrapper()
216 , bInitialized(false)
218 OnDemandCalendarWrapper(
219 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
220 ::com::sun::star::lang::Locale
& rLocale
223 , bInitialized(false)
225 init( rxSMgr
, rLocale
);
227 ~OnDemandCalendarWrapper()
232 bool isInitialized() const { return bInitialized
; }
234 bool is() const { return pPtr
!= NULL
; }
237 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
238 ::com::sun::star::lang::Locale
& rLocale
242 changeLocale( rLocale
);
251 void changeLocale( ::com::sun::star::lang::Locale
& rLocale
)
257 CalendarWrapper
* get() const
262 pPtr
= new CalendarWrapper( xSMgr
);
263 pPtr
->loadDefaultCalendar( aLocale
);
269 CalendarWrapper
* operator->() { return get(); }
270 CalendarWrapper
& operator*() { return *get(); }
273 /** Load a collator only if it's needed.
274 SvNumberformatter uses it upon switching locales.
275 @ATTENTION If the default ctor is used the init() method MUST be called
276 before accessing the collator.
278 class OnDemandCollatorWrapper
280 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xSMgr
;
281 ::com::sun::star::lang::Locale aLocale
;
282 mutable CollatorWrapper
* pPtr
;
287 OnDemandCollatorWrapper()
290 , bInitialized(false)
292 OnDemandCollatorWrapper(
293 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
294 ::com::sun::star::lang::Locale
& rLocale
297 , bInitialized(false)
299 init( rxSMgr
, rLocale
);
301 ~OnDemandCollatorWrapper()
306 bool isInitialized() const { return bInitialized
; }
308 bool is() const { return pPtr
!= NULL
; }
311 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
312 ::com::sun::star::lang::Locale
& rLocale
316 changeLocale( rLocale
);
325 void changeLocale( ::com::sun::star::lang::Locale
& rLocale
)
331 const CollatorWrapper
* get() const
336 pPtr
= new CollatorWrapper( xSMgr
);
337 pPtr
->loadDefaultCollator( aLocale
, ::com::sun::star::i18n::CollatorOptions::CollatorOptions_IGNORE_CASE
);
343 const CollatorWrapper
* operator->() const { return get(); }
344 const CollatorWrapper
& operator*() const { return *get(); }
347 /** Load a transliteration only if it's needed.
348 SvNumberformatter uses it upon switching locales.
349 @ATTENTION If the default ctor is used the init() method MUST be called
350 before accessing the transliteration.
352 class OnDemandTransliterationWrapper
354 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xSMgr
;
355 LanguageType eLanguage
;
356 ::com::sun::star::i18n::TransliterationModules nType
;
357 mutable ::utl::TransliterationWrapper
* pPtr
;
362 OnDemandTransliterationWrapper()
363 : eLanguage( LANGUAGE_SYSTEM
)
366 , bInitialized(false)
368 OnDemandTransliterationWrapper(
369 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
371 ::com::sun::star::i18n::TransliterationModules nTypeP
374 , bInitialized(false)
376 init( rxSMgr
, eLang
, nTypeP
);
378 ~OnDemandTransliterationWrapper()
383 bool isInitialized() const { return bInitialized
; }
385 bool is() const { return pPtr
!= NULL
; }
388 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
,
390 ::com::sun::star::i18n::TransliterationModules nTypeP
395 changeLocale( eLang
);
404 void changeLocale( LanguageType eLang
)
410 const ::utl::TransliterationWrapper
* get() const
415 pPtr
= new ::utl::TransliterationWrapper( xSMgr
, nType
);
416 pPtr
->loadModuleIfNeeded( eLanguage
);
422 const ::utl::TransliterationWrapper
* getForModule( const String
& rModule
, LanguageType eLang
) const
425 pPtr
= new ::utl::TransliterationWrapper( xSMgr
, nType
);
426 pPtr
->loadModuleByImplName( rModule
, eLang
);
427 bValid
= false; // reforce settings change in get()
431 const ::utl::TransliterationWrapper
* operator->() const { return get(); }
432 const ::utl::TransliterationWrapper
& operator*() const { return *get(); }
435 /** Load a native number service wrapper only if it's needed.
436 SvNumberformatter uses it.
439 If the default ctor is used the init() method MUST be called
440 before accessing the native number supplier.
442 class OnDemandNativeNumberWrapper
444 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> xSMgr
;
445 mutable NativeNumberWrapper
* pPtr
;
449 OnDemandNativeNumberWrapper()
451 , bInitialized(false)
453 OnDemandNativeNumberWrapper(
454 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
457 , bInitialized(false)
461 ~OnDemandNativeNumberWrapper()
466 bool isInitialized() const { return bInitialized
; }
469 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rxSMgr
481 bool is() const { return pPtr
!= NULL
; }
483 NativeNumberWrapper
* get() const
486 pPtr
= new NativeNumberWrapper( xSMgr
);
490 NativeNumberWrapper
* operator->() { return get(); }
491 NativeNumberWrapper
& operator*() { return *get(); }
496 #endif // INCLUDED_SVTOOLS_ONDEMAND_HXX