From 9a4d0581c72653e60562d1b8e2121772d21f8a9e Mon Sep 17 00:00:00 2001 From: Mathias Bauer Date: Mon, 12 Oct 2009 11:49:13 +0200 Subject: #i103496#: move SysLocale stuff to unotools --- unotools/source/misc/syslocale.cxx | 205 +++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 unotools/source/misc/syslocale.cxx (limited to 'unotools/source/misc/syslocale.cxx') diff --git a/unotools/source/misc/syslocale.cxx b/unotools/source/misc/syslocale.cxx new file mode 100644 index 000000000000..a299867f83e8 --- /dev/null +++ b/unotools/source/misc/syslocale.cxx @@ -0,0 +1,205 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: syslocale.cxx,v $ + * $Revision: 1.11 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_unotools.hxx" +#ifndef GCC +#endif + +#include +#include +#include +#include +#include +#include + +using namespace osl; +using namespace com::sun::star; + + +SvtSysLocale_Impl* SvtSysLocale::pImpl = NULL; +sal_Int32 SvtSysLocale::nRefCount = 0; + + +class SvtSysLocale_Impl : public utl::ConfigurationListener +{ + friend class SvtSysLocale; + + SvtSysLocaleOptions aSysLocaleOptions; + LocaleDataWrapper* pLocaleData; + CharClass* pCharClass; + com::sun::star::lang::Locale maLocale; + +public: + SvtSysLocale_Impl(); + virtual ~SvtSysLocale_Impl(); + + CharClass* GetCharClass(); + SvtSysLocaleOptions& GetOptions() { return aSysLocaleOptions; } + void ConfigurationChanged( utl::ConfigurationBroadcaster* ); + com::sun::star::lang::Locale GetLocale(); +}; + +com::sun::star::lang::Locale SvtSysLocale_Impl::GetLocale() +{ + // ask configuration + rtl::OUString aLocaleString = aSysLocaleOptions.GetLocaleConfigString(); + if (!aLocaleString.getLength()) + // if no configuration is set, use system locale + return maLocale; + + com::sun::star::lang::Locale aLocale; + sal_Int32 nSep = aLocaleString.indexOf('-'); + if (nSep < 0) + aLocale.Language = aLocaleString; + else + { + aLocale.Language = aLocaleString.copy(0, nSep); + if (nSep < aLocaleString.getLength()) + aLocale.Country = aLocaleString.copy(nSep+1, aLocaleString.getLength() - (nSep+1)); + } + + return aLocale; +} + +// ----------------------------------------------------------------------- + +SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL) +{ + // first initialize maLocale with system locale + MsLangId::convertLanguageToLocale( MsLangId::getSystemLanguage(), maLocale ); + + pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), GetLocale() ); + + // listen for further changes + aSysLocaleOptions.AddListener( this ); +} + + +SvtSysLocale_Impl::~SvtSysLocale_Impl() +{ + aSysLocaleOptions.RemoveListener( this ); + delete pCharClass; + delete pLocaleData; +} + +CharClass* SvtSysLocale_Impl::GetCharClass() +{ + if ( !pCharClass ) + pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), GetLocale() ); + return pCharClass; +} + +void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster* ) +{ + MutexGuard aGuard( SvtSysLocale::GetMutex() ); + lang::Locale aLocale = GetLocale(); + pLocaleData->setLocale( aLocale ); + GetCharClass()->setLocale( aLocale ); +} + +// ==================================================================== + +SvtSysLocale::SvtSysLocale() +{ + MutexGuard aGuard( GetMutex() ); + if ( !pImpl ) + pImpl = new SvtSysLocale_Impl; + ++nRefCount; +} + + +SvtSysLocale::~SvtSysLocale() +{ + MutexGuard aGuard( GetMutex() ); + if ( !--nRefCount ) + { + delete pImpl; + pImpl = NULL; + } +} + + +// static +Mutex& SvtSysLocale::GetMutex() +{ + static Mutex* pMutex = NULL; + if( !pMutex ) + { + MutexGuard aGuard( Mutex::getGlobalMutex() ); + if( !pMutex ) + { + // #i77768# Due to a static reference in the toolkit lib + // we need a mutex that lives longer than the svl library. + // Otherwise the dtor would use a destructed mutex!! + pMutex = new Mutex; + } + } + return *pMutex; +} + + +const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const +{ + return *(pImpl->pLocaleData); +} + + +const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const +{ + return pImpl->pLocaleData; +} + + +const CharClass& SvtSysLocale::GetCharClass() const +{ + return *(pImpl->GetCharClass()); +} + + +const CharClass* SvtSysLocale::GetCharClassPtr() const +{ + return pImpl->GetCharClass(); +} + +SvtSysLocaleOptions& SvtSysLocale::GetOptions() const +{ + return pImpl->GetOptions(); +} + +com::sun::star::lang::Locale SvtSysLocale::GetLocale() const +{ + return pImpl->GetLocale(); +} + +LanguageType SvtSysLocale::GetLanguage() const +{ + return MsLangId::convertLocaleToLanguage( pImpl->GetLocale() ); +} \ No newline at end of file -- cgit From ad482351a6c12cddb06575f6a9a00ec1b72d92fb Mon Sep 17 00:00:00 2001 From: Mathias Bauer Date: Fri, 16 Oct 2009 00:05:16 +0200 Subject: #i103496#: split svtools; improve ConfitItems --- unotools/source/misc/syslocale.cxx | 96 ++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 35 deletions(-) (limited to 'unotools/source/misc/syslocale.cxx') diff --git a/unotools/source/misc/syslocale.cxx b/unotools/source/misc/syslocale.cxx index a299867f83e8..f108cd2ed3df 100644 --- a/unotools/source/misc/syslocale.cxx +++ b/unotools/source/misc/syslocale.cxx @@ -50,53 +50,61 @@ sal_Int32 SvtSysLocale::nRefCount = 0; class SvtSysLocale_Impl : public utl::ConfigurationListener { - friend class SvtSysLocale; - +public: SvtSysLocaleOptions aSysLocaleOptions; LocaleDataWrapper* pLocaleData; CharClass* pCharClass; com::sun::star::lang::Locale maLocale; + com::sun::star::lang::Locale maUILocale; + LanguageType meLanguage; + LanguageType meUILanguage; -public: - SvtSysLocale_Impl(); - virtual ~SvtSysLocale_Impl(); + SvtSysLocale_Impl(); + virtual ~SvtSysLocale_Impl(); - CharClass* GetCharClass(); - SvtSysLocaleOptions& GetOptions() { return aSysLocaleOptions; } - void ConfigurationChanged( utl::ConfigurationBroadcaster* ); - com::sun::star::lang::Locale GetLocale(); + CharClass* GetCharClass(); + virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 ); + void GetLocale(); + void GetUILocale(); }; -com::sun::star::lang::Locale SvtSysLocale_Impl::GetLocale() +void SvtSysLocale_Impl::GetLocale() { // ask configuration - rtl::OUString aLocaleString = aSysLocaleOptions.GetLocaleConfigString(); - if (!aLocaleString.getLength()) - // if no configuration is set, use system locale - return maLocale; - - com::sun::star::lang::Locale aLocale; - sal_Int32 nSep = aLocaleString.indexOf('-'); - if (nSep < 0) - aLocale.Language = aLocaleString; + maLocale = aSysLocaleOptions.GetLocale(); + if ( maLocale.Language.getLength() ) + { + meLanguage = MsLangId::convertLocaleToLanguage( maLocale ); + } else { - aLocale.Language = aLocaleString.copy(0, nSep); - if (nSep < aLocaleString.getLength()) - aLocale.Country = aLocaleString.copy(nSep+1, aLocaleString.getLength() - (nSep+1)); + meLanguage = MsLangId::getSystemLanguage(); + MsLangId::convertLanguageToLocale( meLanguage, maLocale ); } +} - return aLocale; +void SvtSysLocale_Impl::GetUILocale() +{ + maLocale = aSysLocaleOptions.GetLocale(); + if ( maUILocale.Language.getLength() ) + { + meUILanguage = MsLangId::convertLocaleToLanguage( maUILocale ); + } + else + { + meUILanguage = MsLangId::getSystemUILanguage(); + MsLangId::convertLanguageToLocale( meUILanguage, maUILocale ); + } } // ----------------------------------------------------------------------- SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL) { - // first initialize maLocale with system locale - MsLangId::convertLanguageToLocale( MsLangId::getSystemLanguage(), maLocale ); + GetLocale(); + GetUILocale(); - pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), GetLocale() ); + pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), maLocale ); // listen for further changes aSysLocaleOptions.AddListener( this ); @@ -113,16 +121,22 @@ SvtSysLocale_Impl::~SvtSysLocale_Impl() CharClass* SvtSysLocale_Impl::GetCharClass() { if ( !pCharClass ) - pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), GetLocale() ); + pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), maLocale ); return pCharClass; } -void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster* ) +void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 nHint ) { MutexGuard aGuard( SvtSysLocale::GetMutex() ); - lang::Locale aLocale = GetLocale(); - pLocaleData->setLocale( aLocale ); - GetCharClass()->setLocale( aLocale ); + if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE ) + { + GetLocale(); + pLocaleData->setLocale( maLocale ); + GetCharClass()->setLocale( maLocale ); + } + + if ( nHint & SYSLOCALEOPTIONS_HINT_UILOCALE ) + GetUILocale(); } // ==================================================================== @@ -191,15 +205,27 @@ const CharClass* SvtSysLocale::GetCharClassPtr() const SvtSysLocaleOptions& SvtSysLocale::GetOptions() const { - return pImpl->GetOptions(); + return pImpl->aSysLocaleOptions; } com::sun::star::lang::Locale SvtSysLocale::GetLocale() const { - return pImpl->GetLocale(); + return pImpl->maLocale; } LanguageType SvtSysLocale::GetLanguage() const { - return MsLangId::convertLocaleToLanguage( pImpl->GetLocale() ); -} \ No newline at end of file + return pImpl->meLanguage; +} + +com::sun::star::lang::Locale SvtSysLocale::GetUILocale() const +{ + return pImpl->maUILocale; +} + +LanguageType SvtSysLocale::GetUILanguage() const +{ + return pImpl->meUILanguage; +} + + -- cgit From acae9170ea69e3b54b31ebf0322b53e4818172a3 Mon Sep 17 00:00:00 2001 From: Mathias Bauer Date: Mon, 26 Oct 2009 15:10:09 +0100 Subject: #i103496#: shorten timespan when locale settings are inconsistent as they are stored at three different locations --- unotools/source/misc/syslocale.cxx | 59 ++++++-------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) (limited to 'unotools/source/misc/syslocale.cxx') diff --git a/unotools/source/misc/syslocale.cxx b/unotools/source/misc/syslocale.cxx index f108cd2ed3df..8e9d75c6ce0c 100644 --- a/unotools/source/misc/syslocale.cxx +++ b/unotools/source/misc/syslocale.cxx @@ -54,57 +54,19 @@ public: SvtSysLocaleOptions aSysLocaleOptions; LocaleDataWrapper* pLocaleData; CharClass* pCharClass; - com::sun::star::lang::Locale maLocale; - com::sun::star::lang::Locale maUILocale; - LanguageType meLanguage; - LanguageType meUILanguage; SvtSysLocale_Impl(); virtual ~SvtSysLocale_Impl(); CharClass* GetCharClass(); virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, sal_uInt32 ); - void GetLocale(); - void GetUILocale(); }; -void SvtSysLocale_Impl::GetLocale() -{ - // ask configuration - maLocale = aSysLocaleOptions.GetLocale(); - if ( maLocale.Language.getLength() ) - { - meLanguage = MsLangId::convertLocaleToLanguage( maLocale ); - } - else - { - meLanguage = MsLangId::getSystemLanguage(); - MsLangId::convertLanguageToLocale( meLanguage, maLocale ); - } -} - -void SvtSysLocale_Impl::GetUILocale() -{ - maLocale = aSysLocaleOptions.GetLocale(); - if ( maUILocale.Language.getLength() ) - { - meUILanguage = MsLangId::convertLocaleToLanguage( maUILocale ); - } - else - { - meUILanguage = MsLangId::getSystemUILanguage(); - MsLangId::convertLanguageToLocale( meUILanguage, maUILocale ); - } -} - // ----------------------------------------------------------------------- SvtSysLocale_Impl::SvtSysLocale_Impl() : pCharClass(NULL) { - GetLocale(); - GetUILocale(); - - pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), maLocale ); + pLocaleData = new LocaleDataWrapper( ::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() ); // listen for further changes aSysLocaleOptions.AddListener( this ); @@ -121,7 +83,7 @@ SvtSysLocale_Impl::~SvtSysLocale_Impl() CharClass* SvtSysLocale_Impl::GetCharClass() { if ( !pCharClass ) - pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), maLocale ); + pCharClass = new CharClass(::comphelper::getProcessServiceFactory(), aSysLocaleOptions.GetRealLocale() ); return pCharClass; } @@ -130,13 +92,10 @@ void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, sa MutexGuard aGuard( SvtSysLocale::GetMutex() ); if ( nHint & SYSLOCALEOPTIONS_HINT_LOCALE ) { - GetLocale(); - pLocaleData->setLocale( maLocale ); - GetCharClass()->setLocale( maLocale ); + com::sun::star::lang::Locale aLocale( aSysLocaleOptions.GetRealLocale() ); + pLocaleData->setLocale( aLocale ); + GetCharClass()->setLocale( aLocale ); } - - if ( nHint & SYSLOCALEOPTIONS_HINT_UILOCALE ) - GetUILocale(); } // ==================================================================== @@ -210,22 +169,22 @@ SvtSysLocaleOptions& SvtSysLocale::GetOptions() const com::sun::star::lang::Locale SvtSysLocale::GetLocale() const { - return pImpl->maLocale; + return pImpl->aSysLocaleOptions.GetRealLocale(); } LanguageType SvtSysLocale::GetLanguage() const { - return pImpl->meLanguage; + return pImpl->aSysLocaleOptions.GetRealLanguage(); } com::sun::star::lang::Locale SvtSysLocale::GetUILocale() const { - return pImpl->maUILocale; + return pImpl->aSysLocaleOptions.GetRealUILocale(); } LanguageType SvtSysLocale::GetUILanguage() const { - return pImpl->meUILanguage; + return pImpl->aSysLocaleOptions.GetRealUILanguage(); } -- cgit