xref: /aoo42x/main/desktop/source/app/langselect.cxx (revision 07a3d7f1)
12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
102722ceddSAndrew Rist  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
192722ceddSAndrew Rist  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "app.hxx"
28cdf0e10cSrcweir #include "langselect.hxx"
29cdf0e10cSrcweir #include "cmdlineargs.hxx"
30cdf0e10cSrcweir #include <stdio.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <rtl/string.hxx>
33cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
34cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
35cdf0e10cSrcweir #include <tools/resid.hxx>
36cdf0e10cSrcweir #include <tools/config.hxx>
37cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
38cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
39cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
41cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
42cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
43cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
44cdf0e10cSrcweir #include <com/sun/star/lang/XLocalizable.hpp>
45cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
46cdf0e10cSrcweir #include "com/sun/star/util/XFlushable.hpp"
47cdf0e10cSrcweir #include <rtl/locale.hxx>
48cdf0e10cSrcweir #include <rtl/instance.hxx>
49cdf0e10cSrcweir #include <osl/process.h>
50cdf0e10cSrcweir #include <osl/file.hxx>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir using namespace com::sun::star::uno;
54cdf0e10cSrcweir using namespace com::sun::star::lang;
55cdf0e10cSrcweir using namespace com::sun::star::container;
56cdf0e10cSrcweir using namespace com::sun::star::beans;
57cdf0e10cSrcweir using namespace com::sun::star::util;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir namespace desktop {
60cdf0e10cSrcweir 
61cdf0e10cSrcweir static char const SOFFICE_BOOTSTRAP[] = "Bootstrap";
62cdf0e10cSrcweir static char const SOFFICE_STARTLANG[] = "STARTLANG";
63cdf0e10cSrcweir 
64cdf0e10cSrcweir sal_Bool LanguageSelection::bFoundLanguage = sal_False;
65cdf0e10cSrcweir OUString LanguageSelection::aFoundLanguage;
66cdf0e10cSrcweir LanguageSelection::LanguageSelectionStatus LanguageSelection::m_eStatus = LS_STATUS_OK;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir const OUString LanguageSelection::usFallbackLanguage = OUString::createFromAscii("en-US");
69cdf0e10cSrcweir 
existsURL(OUString const & sURL)70cdf0e10cSrcweir static sal_Bool existsURL( OUString const& sURL )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     using namespace osl;
73cdf0e10cSrcweir 	DirectoryItem aDirItem;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if (sURL.getLength() != 0)
76cdf0e10cSrcweir         return ( DirectoryItem::get( sURL, aDirItem ) == DirectoryItem::E_None );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     return sal_False;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir // locate soffice.ini/.rc file
locateSofficeIniFile()82cdf0e10cSrcweir static OUString locateSofficeIniFile()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	OUString aUserDataPath;
85cdf0e10cSrcweir 	OUString aSofficeIniFileURL;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	// Retrieve the default file URL for the soffice.ini/rc
88cdf0e10cSrcweir 	rtl::Bootstrap().getIniName( aSofficeIniFileURL );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	if ( utl::Bootstrap::locateUserData( aUserDataPath ) == utl::Bootstrap::PATH_EXISTS )
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		const char CONFIG_DIR[] = "/config";
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 		sal_Int32 nIndex = aSofficeIniFileURL.lastIndexOf( '/');
95cdf0e10cSrcweir 		if ( nIndex > 0 )
96cdf0e10cSrcweir 		{
97cdf0e10cSrcweir 			OUString		aUserSofficeIniFileURL;
98cdf0e10cSrcweir 			OUStringBuffer	aBuffer( aUserDataPath );
99cdf0e10cSrcweir 			aBuffer.appendAscii( CONFIG_DIR );
100cdf0e10cSrcweir 			aBuffer.append( aSofficeIniFileURL.copy( nIndex ));
101cdf0e10cSrcweir 			aUserSofficeIniFileURL = aBuffer.makeStringAndClear();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			if ( existsURL( aUserSofficeIniFileURL ))
104cdf0e10cSrcweir 				return aUserSofficeIniFileURL;
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir 	// Fallback try to use the soffice.ini/rc from program folder
108cdf0e10cSrcweir 	return aSofficeIniFileURL;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
IsoStringToLocale(const OUString & str)111cdf0e10cSrcweir Locale LanguageSelection::IsoStringToLocale(const OUString& str)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     Locale l;
114cdf0e10cSrcweir     sal_Int32 index=0;
115cdf0e10cSrcweir     l.Language = str.getToken(0, '-', index);
116cdf0e10cSrcweir     if (index >= 0) l.Country = str.getToken(0, '-', index);
117cdf0e10cSrcweir     if (index >= 0) l.Variant = str.getToken(0, '-', index);
118cdf0e10cSrcweir     return l;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
prepareLanguage()121cdf0e10cSrcweir bool LanguageSelection::prepareLanguage()
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     m_eStatus = LS_STATUS_OK;
124cdf0e10cSrcweir     OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
125cdf0e10cSrcweir     Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
126cdf0e10cSrcweir     Reference< XLocalizable > theConfigProvider;
127cdf0e10cSrcweir     try
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         theConfigProvider = Reference< XLocalizable >(theMSF->createInstance( sConfigSrvc ),UNO_QUERY_THROW );
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir     catch(const Exception&)
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     if(!theConfigProvider.is())
137cdf0e10cSrcweir         return false;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     // #i42730#get the windows 16Bit locale - it should be preferred over the UI language
142cdf0e10cSrcweir     try
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         Reference< XPropertySet > xProp(getConfigAccess("org.openoffice.System/L10N/", sal_False), UNO_QUERY_THROW);
145cdf0e10cSrcweir         Any aWin16SysLocale = xProp->getPropertyValue(OUString::createFromAscii("SystemLocale"));
146cdf0e10cSrcweir         ::rtl::OUString sWin16SysLocale;
147cdf0e10cSrcweir         aWin16SysLocale >>= sWin16SysLocale;
148cdf0e10cSrcweir         if( sWin16SysLocale.getLength())
149cdf0e10cSrcweir             setDefaultLanguage(sWin16SysLocale);
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir     catch(const Exception&)
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     // #i32939# use system locale to set document default locale
157cdf0e10cSrcweir     try
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         OUString usLocale;
160cdf0e10cSrcweir         Reference< XPropertySet > xLocaleProp(getConfigAccess(
161cdf0e10cSrcweir             "org.openoffice.System/L10N", sal_True), UNO_QUERY_THROW);
162cdf0e10cSrcweir         xLocaleProp->getPropertyValue(OUString::createFromAscii("Locale")) >>= usLocale;
163cdf0e10cSrcweir             setDefaultLanguage(usLocale);
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir     catch (Exception&)
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     // get the selected UI language as string
171cdf0e10cSrcweir     bool     bCmdLanguage( false );
172cdf0e10cSrcweir     bool     bIniLanguage( false );
173cdf0e10cSrcweir     OUString aEmpty;
174cdf0e10cSrcweir     OUString aLocaleString = getUserUILanguage();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     if ( aLocaleString.getLength() == 0 )
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         CommandLineArgs* pCmdLineArgs = Desktop::GetCommandLineArgs();
179cdf0e10cSrcweir         if ( pCmdLineArgs )
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir             pCmdLineArgs->GetLanguage(aLocaleString);
182cdf0e10cSrcweir             if (isInstalledLanguage(aLocaleString, sal_False))
183cdf0e10cSrcweir             {
184cdf0e10cSrcweir                 bCmdLanguage   = true;
185cdf0e10cSrcweir                 bFoundLanguage = true;
186cdf0e10cSrcweir                 aFoundLanguage = aLocaleString;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir             else
189cdf0e10cSrcweir                 aLocaleString = aEmpty;
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         if ( !bCmdLanguage )
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             OUString aSOfficeIniURL = locateSofficeIniFile();
195cdf0e10cSrcweir 	        Config aConfig(aSOfficeIniURL);
196cdf0e10cSrcweir 	        aConfig.SetGroup( SOFFICE_BOOTSTRAP );
197cdf0e10cSrcweir             OString sLang = aConfig.ReadKey( SOFFICE_STARTLANG );
198cdf0e10cSrcweir             aLocaleString = OUString( sLang.getStr(), sLang.getLength(), RTL_TEXTENCODING_ASCII_US );
199cdf0e10cSrcweir             if (isInstalledLanguage(aLocaleString, sal_False))
200cdf0e10cSrcweir             {
201cdf0e10cSrcweir                 bIniLanguage   = true;
202cdf0e10cSrcweir                 bFoundLanguage = true;
203cdf0e10cSrcweir                 aFoundLanguage = aLocaleString;
204cdf0e10cSrcweir             }
205cdf0e10cSrcweir             else
206cdf0e10cSrcweir                 aLocaleString = aEmpty;
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     // user further fallbacks for the UI language
211cdf0e10cSrcweir     if ( aLocaleString.getLength() == 0 )
212cdf0e10cSrcweir         aLocaleString = getLanguageString();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     if ( aLocaleString.getLength() > 0 )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         try
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             // prepare default config provider by localizing it to the selected locale
219*07a3d7f1SPedro Giffuni             // this will ensure localized configuration settings to be selected according to the
220cdf0e10cSrcweir             // UI language.
221cdf0e10cSrcweir             Locale loc = LanguageSelection::IsoStringToLocale(aLocaleString);
222cdf0e10cSrcweir             // flush any data already written to the configuration (which
223cdf0e10cSrcweir             // currently uses independent caches for different locales and thus
224cdf0e10cSrcweir             // would ignore data written to another cache):
225cdf0e10cSrcweir             Reference< XFlushable >(theConfigProvider, UNO_QUERY_THROW)->
226cdf0e10cSrcweir                 flush();
227cdf0e10cSrcweir             theConfigProvider->setLocale(loc);
228cdf0e10cSrcweir 
229cdf0e10cSrcweir             Reference< XPropertySet > xProp(getConfigAccess("org.openoffice.Setup/L10N/", sal_True), UNO_QUERY_THROW);
230cdf0e10cSrcweir             if ( !bCmdLanguage )
231cdf0e10cSrcweir             {
232cdf0e10cSrcweir                 // Store language only
233cdf0e10cSrcweir                 xProp->setPropertyValue(OUString::createFromAscii("ooLocale"), makeAny(aLocaleString));
234cdf0e10cSrcweir                 Reference< XChangesBatch >(xProp, UNO_QUERY_THROW)->commitChanges();
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             if ( bIniLanguage )
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 // Store language only
240cdf0e10cSrcweir                 Reference< XPropertySet > xProp2(getConfigAccess("org.openoffice.Office.Linguistic/General/", sal_True), UNO_QUERY_THROW);
241cdf0e10cSrcweir                 xProp2->setPropertyValue(OUString::createFromAscii("UILocale"), makeAny(aLocaleString));
242cdf0e10cSrcweir                 Reference< XChangesBatch >(xProp2, UNO_QUERY_THROW)->commitChanges();
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir             MsLangId::setConfiguredSystemUILanguage( MsLangId::convertLocaleToLanguage(loc) );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 			OUString sLocale;
248cdf0e10cSrcweir 			xProp->getPropertyValue(OUString::createFromAscii("ooSetupSystemLocale")) >>= sLocale;
249cdf0e10cSrcweir 			if ( sLocale.getLength() )
250cdf0e10cSrcweir 			{
251cdf0e10cSrcweir 				loc = LanguageSelection::IsoStringToLocale(sLocale);
252cdf0e10cSrcweir 				MsLangId::setConfiguredSystemLanguage( MsLangId::convertLocaleToLanguage(loc) );
253cdf0e10cSrcweir 			}
254cdf0e10cSrcweir 			else
255cdf0e10cSrcweir 				MsLangId::setConfiguredSystemLanguage( MsLangId::getSystemLanguage() );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir             bSuccess = sal_True;
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir         catch ( PropertyVetoException& )
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir             // we are not allowed to change this
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         catch (Exception& e)
264cdf0e10cSrcweir         {
265cdf0e10cSrcweir             OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
266cdf0e10cSrcweir             OSL_ENSURE(sal_False, aMsg.getStr());
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         }
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     // #i32939# setting of default document locale
272cdf0e10cSrcweir     // #i32939# this should not be based on the UI language
273cdf0e10cSrcweir     setDefaultLanguage(aLocaleString);
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     return bSuccess;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
setDefaultLanguage(const OUString & sLocale)278cdf0e10cSrcweir void LanguageSelection::setDefaultLanguage(const OUString& sLocale)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir     // #i32939# setting of default document language
281cdf0e10cSrcweir     //
282cdf0e10cSrcweir     // See #i42730# for rules for determining source of settings
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     // determine script type of locale
285cdf0e10cSrcweir     LanguageType nLang = MsLangId::convertIsoStringToLanguage(sLocale);
286cdf0e10cSrcweir     sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage(nLang);
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     switch (nScriptType)
289cdf0e10cSrcweir     {
290cdf0e10cSrcweir         case SCRIPTTYPE_ASIAN:
291cdf0e10cSrcweir             MsLangId::setConfiguredAsianFallback( nLang );
292cdf0e10cSrcweir             break;
293cdf0e10cSrcweir         case SCRIPTTYPE_COMPLEX:
294cdf0e10cSrcweir             MsLangId::setConfiguredComplexFallback( nLang );
295cdf0e10cSrcweir             break;
296cdf0e10cSrcweir         default:
297cdf0e10cSrcweir             MsLangId::setConfiguredWesternFallback( nLang );
298cdf0e10cSrcweir             break;
299cdf0e10cSrcweir     }
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
getUserUILanguage()302cdf0e10cSrcweir OUString LanguageSelection::getUserUILanguage()
303cdf0e10cSrcweir {
304cdf0e10cSrcweir     // check whether the user has selected a specific language
305cdf0e10cSrcweir     OUString aUserLanguage = getUserLanguage();
306cdf0e10cSrcweir     if (aUserLanguage.getLength() > 0 )
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir         if (isInstalledLanguage(aUserLanguage))
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             // all is well
311cdf0e10cSrcweir             bFoundLanguage = sal_True;
312cdf0e10cSrcweir             aFoundLanguage = aUserLanguage;
313cdf0e10cSrcweir             return aFoundLanguage;
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir         else
316cdf0e10cSrcweir         {
317cdf0e10cSrcweir             // selected language is not/no longer installed
318cdf0e10cSrcweir             resetUserLanguage();
319cdf0e10cSrcweir         }
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     return aUserLanguage;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir 
getLanguageString()325cdf0e10cSrcweir OUString LanguageSelection::getLanguageString()
326cdf0e10cSrcweir {
327cdf0e10cSrcweir     // did we already find a language?
328cdf0e10cSrcweir     if (bFoundLanguage)
329cdf0e10cSrcweir         return aFoundLanguage;
330cdf0e10cSrcweir 
331cdf0e10cSrcweir     // check whether the user has selected a specific language
332cdf0e10cSrcweir     OUString aUserLanguage = getUserUILanguage();
333cdf0e10cSrcweir     if (aUserLanguage.getLength() > 0 )
334cdf0e10cSrcweir         return aUserLanguage ;
335cdf0e10cSrcweir 
336cdf0e10cSrcweir     // try to use system default
337cdf0e10cSrcweir     aUserLanguage = getSystemLanguage();
338cdf0e10cSrcweir     if (aUserLanguage.getLength() > 0 )
339cdf0e10cSrcweir     {
340cdf0e10cSrcweir         if (isInstalledLanguage(aUserLanguage, sal_False))
341cdf0e10cSrcweir         {
342cdf0e10cSrcweir             // great, system default language is available
343cdf0e10cSrcweir             bFoundLanguage = sal_True;
344cdf0e10cSrcweir             aFoundLanguage = aUserLanguage;
345cdf0e10cSrcweir             return aFoundLanguage;
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir     }
348cdf0e10cSrcweir     // fallback 1: en-US
349cdf0e10cSrcweir     OUString usFB = usFallbackLanguage;
350cdf0e10cSrcweir     if (isInstalledLanguage(usFB))
351cdf0e10cSrcweir     {
352cdf0e10cSrcweir         bFoundLanguage = sal_True;
353cdf0e10cSrcweir         aFoundLanguage = usFallbackLanguage;
354cdf0e10cSrcweir         return aFoundLanguage;
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     // fallback didn't work use first installed language
358cdf0e10cSrcweir     aUserLanguage = getFirstInstalledLanguage();
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     bFoundLanguage = sal_True;
361cdf0e10cSrcweir     aFoundLanguage = aUserLanguage;
362cdf0e10cSrcweir     return aFoundLanguage;
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
getConfigAccess(const sal_Char * pPath,sal_Bool bUpdate)365cdf0e10cSrcweir Reference< XNameAccess > LanguageSelection::getConfigAccess(const sal_Char* pPath, sal_Bool bUpdate)
366cdf0e10cSrcweir {
367cdf0e10cSrcweir     Reference< XNameAccess > xNameAccess;
368cdf0e10cSrcweir     try{
369cdf0e10cSrcweir         OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
370cdf0e10cSrcweir         OUString sAccessSrvc;
371cdf0e10cSrcweir         if (bUpdate)
372cdf0e10cSrcweir             sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess");
373cdf0e10cSrcweir         else
374cdf0e10cSrcweir             sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess");
375cdf0e10cSrcweir 
376cdf0e10cSrcweir         OUString sConfigURL = OUString::createFromAscii(pPath);
377cdf0e10cSrcweir 
378cdf0e10cSrcweir         // get configuration provider
379cdf0e10cSrcweir         Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
380cdf0e10cSrcweir         if (theMSF.is()) {
381cdf0e10cSrcweir             Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory > (
382cdf0e10cSrcweir                 theMSF->createInstance( sConfigSrvc ),UNO_QUERY_THROW );
383cdf0e10cSrcweir 
384cdf0e10cSrcweir             // access the provider
385cdf0e10cSrcweir             Sequence< Any > theArgs(1);
386cdf0e10cSrcweir             theArgs[ 0 ] <<= sConfigURL;
387cdf0e10cSrcweir             xNameAccess = Reference< XNameAccess > (
388cdf0e10cSrcweir                 theConfigProvider->createInstanceWithArguments(
389cdf0e10cSrcweir                     sAccessSrvc, theArgs ), UNO_QUERY_THROW );
390cdf0e10cSrcweir         }
391cdf0e10cSrcweir     } catch (com::sun::star::uno::Exception& e)
392cdf0e10cSrcweir     {
393cdf0e10cSrcweir         OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
394cdf0e10cSrcweir         OSL_ENSURE(sal_False, aMsg.getStr());
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir     return xNameAccess;
397cdf0e10cSrcweir }
398cdf0e10cSrcweir 
getInstalledLanguages()399cdf0e10cSrcweir Sequence< OUString > LanguageSelection::getInstalledLanguages()
400cdf0e10cSrcweir {
401cdf0e10cSrcweir     Sequence< OUString > seqLanguages;
402cdf0e10cSrcweir     Reference< XNameAccess > xAccess = getConfigAccess("org.openoffice.Setup/Office/InstalledLocales", sal_False);
403cdf0e10cSrcweir     if (!xAccess.is()) return seqLanguages;
404cdf0e10cSrcweir     seqLanguages = xAccess->getElementNames();
405cdf0e10cSrcweir     return seqLanguages;
406cdf0e10cSrcweir }
407cdf0e10cSrcweir 
408cdf0e10cSrcweir // FIXME
409cdf0e10cSrcweir // it's not very clever to handle language fallbacks here, but
410cdf0e10cSrcweir // right now, there is no place that handles those fallbacks globally
_getFallbackLocales(const OUString & aIsoLang)411cdf0e10cSrcweir static Sequence< OUString > _getFallbackLocales(const OUString& aIsoLang)
412cdf0e10cSrcweir {
413cdf0e10cSrcweir     Sequence< OUString > seqFallbacks;
414cdf0e10cSrcweir     if (aIsoLang.equalsAscii("zh-HK")) {
415cdf0e10cSrcweir         seqFallbacks = Sequence< OUString >(1);
416cdf0e10cSrcweir         seqFallbacks[0] = OUString::createFromAscii("zh-TW");
417cdf0e10cSrcweir     }
418cdf0e10cSrcweir     return seqFallbacks;
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
isInstalledLanguage(OUString & usLocale,sal_Bool bExact)421cdf0e10cSrcweir sal_Bool LanguageSelection::isInstalledLanguage(OUString& usLocale, sal_Bool bExact)
422cdf0e10cSrcweir {
423cdf0e10cSrcweir     sal_Bool bInstalled = sal_False;
424cdf0e10cSrcweir     Sequence< OUString > seqLanguages = getInstalledLanguages();
425cdf0e10cSrcweir     for (sal_Int32 i=0; i<seqLanguages.getLength(); i++)
426cdf0e10cSrcweir     {
427cdf0e10cSrcweir         if (usLocale.equals(seqLanguages[i]))
428cdf0e10cSrcweir         {
429cdf0e10cSrcweir             bInstalled = sal_True;
430cdf0e10cSrcweir             break;
431cdf0e10cSrcweir         }
432cdf0e10cSrcweir     }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir     if (!bInstalled && !bExact)
435cdf0e10cSrcweir     {
436cdf0e10cSrcweir         // try fallback locales
437cdf0e10cSrcweir         Sequence< OUString > seqFallbacks = _getFallbackLocales(usLocale);
438cdf0e10cSrcweir         for (sal_Int32 j=0; j<seqFallbacks.getLength(); j++)
439cdf0e10cSrcweir         {
440cdf0e10cSrcweir             for (sal_Int32 i=0; i<seqLanguages.getLength(); i++)
441cdf0e10cSrcweir             {
442cdf0e10cSrcweir                 if (seqFallbacks[j].equals(seqLanguages[i]))
443cdf0e10cSrcweir                 {
444cdf0e10cSrcweir                     bInstalled = sal_True;
445cdf0e10cSrcweir                     usLocale = seqFallbacks[j];
446cdf0e10cSrcweir                     break;
447cdf0e10cSrcweir                 }
448cdf0e10cSrcweir             }
449cdf0e10cSrcweir         }
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     if (!bInstalled && !bExact)
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         // no exact match was found, well try to find a substitute
455cdf0e10cSrcweir         OUString aInstalledLocale;
456cdf0e10cSrcweir         for (sal_Int32 i=0; i<seqLanguages.getLength(); i++)
457cdf0e10cSrcweir         {
458cdf0e10cSrcweir             if (usLocale.indexOf(seqLanguages[i]) == 0)
459cdf0e10cSrcweir             {
460cdf0e10cSrcweir                 // requested locale starts with the installed locale
461cdf0e10cSrcweir                 // (i.e. installed locale has index 0 in requested locale)
462cdf0e10cSrcweir                 bInstalled = sal_True;
463cdf0e10cSrcweir                 usLocale   = seqLanguages[i];
464cdf0e10cSrcweir                 break;
465cdf0e10cSrcweir             }
466cdf0e10cSrcweir         }
467cdf0e10cSrcweir     }
468cdf0e10cSrcweir     return bInstalled;
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
getFirstInstalledLanguage()471cdf0e10cSrcweir OUString LanguageSelection::getFirstInstalledLanguage()
472cdf0e10cSrcweir {
473cdf0e10cSrcweir     OUString aLanguage;
474cdf0e10cSrcweir     Sequence< OUString > seqLanguages = getInstalledLanguages();
475cdf0e10cSrcweir     if (seqLanguages.getLength() > 0)
476cdf0e10cSrcweir         aLanguage = seqLanguages[0];
477cdf0e10cSrcweir     return aLanguage;
478cdf0e10cSrcweir }
479cdf0e10cSrcweir 
getUserLanguage()480cdf0e10cSrcweir OUString LanguageSelection::getUserLanguage()
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     OUString aUserLanguage;
483cdf0e10cSrcweir     Reference< XNameAccess > xAccess(getConfigAccess("org.openoffice.Office.Linguistic/General", sal_False));
484cdf0e10cSrcweir     if (xAccess.is())
485cdf0e10cSrcweir     {
486cdf0e10cSrcweir         try
487cdf0e10cSrcweir         {
488cdf0e10cSrcweir             xAccess->getByName(OUString::createFromAscii("UILocale")) >>= aUserLanguage;
489cdf0e10cSrcweir         }
490cdf0e10cSrcweir         catch ( NoSuchElementException const & )
491cdf0e10cSrcweir         {
492cdf0e10cSrcweir             m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
493cdf0e10cSrcweir             return OUString();
494cdf0e10cSrcweir         }
495cdf0e10cSrcweir         catch ( WrappedTargetException const & )
496cdf0e10cSrcweir         {
497cdf0e10cSrcweir             m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
498cdf0e10cSrcweir             return OUString();
499cdf0e10cSrcweir         }
500cdf0e10cSrcweir     }
501cdf0e10cSrcweir     return aUserLanguage;
502cdf0e10cSrcweir }
503cdf0e10cSrcweir 
getSystemLanguage()504cdf0e10cSrcweir OUString LanguageSelection::getSystemLanguage()
505cdf0e10cSrcweir {
506cdf0e10cSrcweir     OUString aUserLanguage;
507cdf0e10cSrcweir     Reference< XNameAccess > xAccess(getConfigAccess("org.openoffice.System/L10N", sal_False));
508cdf0e10cSrcweir     if (xAccess.is())
509cdf0e10cSrcweir     {
510cdf0e10cSrcweir         try
511cdf0e10cSrcweir         {
512cdf0e10cSrcweir             xAccess->getByName(OUString::createFromAscii("UILocale")) >>= aUserLanguage;
513cdf0e10cSrcweir         }
514cdf0e10cSrcweir         catch ( NoSuchElementException const & )
515cdf0e10cSrcweir         {
516cdf0e10cSrcweir             m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
517cdf0e10cSrcweir             return OUString();
518cdf0e10cSrcweir         }
519cdf0e10cSrcweir         catch ( WrappedTargetException const & )
520cdf0e10cSrcweir         {
521cdf0e10cSrcweir             m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
522cdf0e10cSrcweir             return OUString();
523cdf0e10cSrcweir         }
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir     return aUserLanguage;
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 
resetUserLanguage()529cdf0e10cSrcweir void LanguageSelection::resetUserLanguage()
530cdf0e10cSrcweir {
531cdf0e10cSrcweir     try
532cdf0e10cSrcweir     {
533cdf0e10cSrcweir         Reference< XPropertySet > xProp(getConfigAccess("org.openoffice.Office.Linguistic/General", sal_True), UNO_QUERY_THROW);
534cdf0e10cSrcweir         xProp->setPropertyValue(OUString::createFromAscii("UILocale"), makeAny(OUString::createFromAscii("")));
535cdf0e10cSrcweir         Reference< XChangesBatch >(xProp, UNO_QUERY_THROW)->commitChanges();
536cdf0e10cSrcweir     }
537cdf0e10cSrcweir     catch ( PropertyVetoException& )
538cdf0e10cSrcweir     {
539cdf0e10cSrcweir         // we are not allowed to change this
540cdf0e10cSrcweir     }
541cdf0e10cSrcweir     catch ( Exception& e)
542cdf0e10cSrcweir     {
543cdf0e10cSrcweir         OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
544cdf0e10cSrcweir         OSL_ENSURE(sal_False, aMsg.getStr());
545cdf0e10cSrcweir         m_eStatus = LS_STATUS_CONFIGURATIONACCESS_BROKEN;
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir }
548cdf0e10cSrcweir 
getStatus()549cdf0e10cSrcweir LanguageSelection::LanguageSelectionStatus LanguageSelection::getStatus()
550cdf0e10cSrcweir {
551cdf0e10cSrcweir     return m_eStatus;
552cdf0e10cSrcweir }
553cdf0e10cSrcweir 
554cdf0e10cSrcweir } // namespace desktop
555