/************************************************************************* * * $RCSfile: langselect.cxx,v $ * * $Revision: 1.12 $ * last change: $Author: rt $ $Date: 2004-11-09 15:15:15 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses * * - GNU Lesser General Public License Version 2.1 * - Sun Industry Standards Source License Version 1.1 * * Sun Microsystems Inc., October, 2000 * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2000 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library 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 for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * * Sun Industry Standards Source License Version 1.1 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.1 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://www.openoffice.org/license.html. * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2000 by Sun Microsystems, Inc. * * All Rights Reserved. * * Contributor(s): _______________________________________ * * ************************************************************************/ #include "app.hxx" #include "langselect.hxx" #include #ifndef _RTL_STRING_HXX #include #endif #ifndef _SVTOOLS_PATHOPTIONS_HXX #include #endif #ifndef _TOOLS_RESID_HXX #include #endif #ifndef _TOOLS_ISOLANG_HXX #include #endif #ifndef _COMPHELPER_PROCESSFACTORY_HXX_ #include #endif #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_ #include #endif #include #include #include #include #include #include #include #ifndef INCLUDED_RTL_INSTANCE_HXX #include #endif #include using namespace rtl; using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::container; using namespace com::sun::star::beans; using namespace com::sun::star::util; namespace desktop { sal_Bool LanguageSelection::bFoundLanguage = sal_False; OUString LanguageSelection::aFoundLanguage; const OUString LanguageSelection::usFallbackLanguage = OUString::createFromAscii("en-US"); Locale LanguageSelection::IsoStringToLocale(const OUString& str) { Locale l; sal_Int32 index=0; l.Language = str.getToken(0, '-', index); if (index >= 0) l.Country = str.getToken(0, '-', index); if (index >= 0) l.Variant = str.getToken(0, '-', index); return l; } bool LanguageSelection::prepareLanguage() { OUString aLocaleString = getLanguageString(); if ( aLocaleString.getLength() > 0 ) { OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"); Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory(); try { Reference< XLocalizable > theConfigProvider( theMSF->createInstance( sConfigSrvc ),UNO_QUERY_THROW ); Locale loc = LanguageSelection::IsoStringToLocale(aLocaleString); theConfigProvider->setLocale(loc); Reference< XPropertySet > xProp(getConfigAccess("org.openoffice.Setup/L10N/", sal_True), UNO_QUERY_THROW); xProp->setPropertyValue(OUString::createFromAscii("ooLocale"), makeAny(aLocaleString)); Reference< XChangesBatch >(xProp, UNO_QUERY_THROW)->commitChanges(); return true; } catch (Exception& e) { OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US); OSL_ENSURE(sal_False, aMsg.getStr()); } } return false; } OUString LanguageSelection::getLanguageString() { // did we already find a language? if (bFoundLanguage) return aFoundLanguage; // check whether the user has selected a specific language OUString aUserLanguage = getUserLanguage(); if (aUserLanguage.getLength() > 0 ) { if (isInstalledLanguage(aUserLanguage)) { // all is well bFoundLanguage = sal_True; aFoundLanguage = aUserLanguage; return aFoundLanguage; } else { // selected language is not/no longer installed resetUserLanguage(); } } // try to use system default aUserLanguage = getSystemLanguage(); if (aUserLanguage.getLength() > 0 ) { if (isInstalledLanguage(aUserLanguage, sal_False)) { // great, system default language is available bFoundLanguage = sal_True; aFoundLanguage = aUserLanguage; return aFoundLanguage; } } // fallback 1: en-US OUString usFB = usFallbackLanguage; if (isInstalledLanguage(usFB)) { bFoundLanguage = sal_True; aFoundLanguage = usFallbackLanguage; return aFoundLanguage; } // fallback didn't work use first installed language aUserLanguage = getFirstInstalledLanguage(); bFoundLanguage = sal_True; aFoundLanguage = aUserLanguage; return aFoundLanguage; } Reference< XNameAccess > LanguageSelection::getConfigAccess(const sal_Char* pPath, sal_Bool bUpdate) { Reference< XNameAccess > xNameAccess; try{ OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"); OUString sAccessSrvc; if (bUpdate) sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"); else sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"); OUString sConfigURL = OUString::createFromAscii(pPath); // get configuration provider Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory(); Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory > ( theMSF->createInstance( sConfigSrvc ),UNO_QUERY_THROW ); // access the provider Sequence< Any > theArgs(1); theArgs[ 0 ] <<= sConfigURL; xNameAccess = Reference< XNameAccess > ( theConfigProvider->createInstanceWithArguments( sAccessSrvc, theArgs ), UNO_QUERY_THROW ); } catch (com::sun::star::uno::Exception& e) { OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US); OSL_ENSURE(sal_False, aMsg.getStr()); } return xNameAccess; } Sequence< OUString > LanguageSelection::getInstalledLanguages() { Sequence< OUString > seqLanguages; Reference< XNameAccess > xAccess = getConfigAccess("org.openoffice.Setup/Office/InstalledLocales", sal_False); if (!xAccess.is()) return seqLanguages; seqLanguages = xAccess->getElementNames(); return seqLanguages; } sal_Bool LanguageSelection::isInstalledLanguage(OUString& usLocale, sal_Bool bExact) { sal_Bool bInstalled = sal_False; Sequence< OUString > seqLanguages = getInstalledLanguages(); for (sal_Int32 i=0; i seqLanguages = getInstalledLanguages(); if (seqLanguages.getLength() > 0) aLanguage = seqLanguages[0]; return aLanguage; } OUString LanguageSelection::getUserLanguage() { OUString aUserLanguage; Reference< XNameAccess > xAccess(getConfigAccess("org.openoffice.Office.Linguistic/General", sal_False)); if (xAccess.is()) { try { xAccess->getByName(OUString::createFromAscii("UILocale")) >>= aUserLanguage; } catch ( NoSuchElementException const & ) { return OUString(); } catch ( WrappedTargetException const & ) { return OUString(); } } return aUserLanguage; } OUString LanguageSelection::getSystemLanguage() { OUString aUserLanguage; Reference< XNameAccess > xAccess(getConfigAccess("org.openoffice.System/L10N", sal_False)); if (xAccess.is()) { try { xAccess->getByName(OUString::createFromAscii("UILocale")) >>= aUserLanguage; } catch ( NoSuchElementException const & ) { return OUString(); } catch ( WrappedTargetException const & ) { return OUString(); } } return aUserLanguage; } void LanguageSelection::resetUserLanguage() { try { Reference< XPropertySet > xProp(getConfigAccess("org.openoffice.Office.Linguistic/General", sal_True), UNO_QUERY_THROW); xProp->setPropertyValue(OUString::createFromAscii("UILocale"), makeAny(OUString::createFromAscii(""))); Reference< XChangesBatch >(xProp, UNO_QUERY_THROW)->commitChanges(); } catch ( Exception& e) { OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US); OSL_ENSURE(sal_False, aMsg.getStr()); } } } // namespace desktop