1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_shell.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "localebackend.hxx" 32*cdf0e10cSrcweir #include <com/sun/star/beans/Optional.hpp> 33*cdf0e10cSrcweir #include <osl/time.h> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <stdio.h> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #if defined(LINUX) || defined(SOLARIS) || defined(NETBSD) || defined(FREEBSD) || defined(OS2) 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 40*cdf0e10cSrcweir #include <locale.h> 41*cdf0e10cSrcweir #include <string.h> 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /* 44*cdf0e10cSrcweir * Note: setlocale is not at all thread safe, so is this code. It could 45*cdf0e10cSrcweir * especially interfere with the stuff VCL is doing, so make sure this 46*cdf0e10cSrcweir * is called from the main thread only. 47*cdf0e10cSrcweir */ 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir static rtl::OUString ImplGetLocale(int category) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir const char *locale = setlocale(category, ""); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir // Return "en-US" for C locales 54*cdf0e10cSrcweir if( (locale == NULL) || ( locale[0] == 'C' && locale[1] == '\0' ) ) 55*cdf0e10cSrcweir return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "en-US" ) ); 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir const char *cp; 59*cdf0e10cSrcweir const char *uscore = NULL; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir // locale string have the format lang[_ctry][.encoding][@modifier] 62*cdf0e10cSrcweir // we are only interested in the first two items, so we handle 63*cdf0e10cSrcweir // '.' and '@' as string end. 64*cdf0e10cSrcweir for (cp = locale; *cp; cp++) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir if (*cp == '_') 67*cdf0e10cSrcweir uscore = cp; 68*cdf0e10cSrcweir if (*cp == '.' || *cp == '@') 69*cdf0e10cSrcweir break; 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir rtl::OUStringBuffer aLocaleBuffer; 73*cdf0e10cSrcweir if( uscore != NULL ) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir aLocaleBuffer.appendAscii(locale, uscore++ - locale); 76*cdf0e10cSrcweir aLocaleBuffer.appendAscii("-"); 77*cdf0e10cSrcweir aLocaleBuffer.appendAscii(uscore, cp - uscore); 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir else 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir aLocaleBuffer.appendAscii(locale, cp - locale); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir return aLocaleBuffer.makeStringAndClear(); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir #elif defined(MACOSX) 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 90*cdf0e10cSrcweir #include <locale.h> 91*cdf0e10cSrcweir #include <string.h> 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir #include <premac.h> 94*cdf0e10cSrcweir #include <CoreServices/CoreServices.h> 95*cdf0e10cSrcweir #include <CoreFoundation/CoreFoundation.h> 96*cdf0e10cSrcweir #include <postmac.h> 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir namespace /* private */ 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir void OUStringBufferAppendCFString(rtl::OUStringBuffer& buffer, const CFStringRef s) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir CFIndex lstr = CFStringGetLength(s); 104*cdf0e10cSrcweir for (CFIndex i = 0; i < lstr; i++) 105*cdf0e10cSrcweir buffer.append(CFStringGetCharacterAtIndex(s, i)); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir template <typename T> 109*cdf0e10cSrcweir class CFGuard 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir public: 112*cdf0e10cSrcweir explicit CFGuard(T& rT) : rT_(rT) {} 113*cdf0e10cSrcweir ~CFGuard() { if (rT_) CFRelease(rT_); } 114*cdf0e10cSrcweir private: 115*cdf0e10cSrcweir T& rT_; 116*cdf0e10cSrcweir }; 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir typedef CFGuard<CFArrayRef> CFArrayGuard; 119*cdf0e10cSrcweir typedef CFGuard<CFStringRef> CFStringGuard; 120*cdf0e10cSrcweir typedef CFGuard<CFTypeRef> CFTypeRefGuard; 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir /* For more information on the Apple locale concept please refer to 123*cdf0e10cSrcweir http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFLocales/Articles/CFLocaleConcepts.html 124*cdf0e10cSrcweir According to this documentation a locale identifier has the format: language[_country][_variant]* 125*cdf0e10cSrcweir e.g. es_ES_PREEURO -> spain prior Euro support 126*cdf0e10cSrcweir Note: The calling code should be able to handle locales with only language information e.g. 'en' for certain 127*cdf0e10cSrcweir UI languages just the language code will be returned. 128*cdf0e10cSrcweir */ 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir CFStringRef ImplGetAppPreference(const char* pref) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir CFStringRef csPref = CFStringCreateWithCString(NULL, pref, kCFStringEncodingASCII); 133*cdf0e10cSrcweir CFStringGuard csRefGuard(csPref); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir CFTypeRef ref = CFPreferencesCopyAppValue(csPref, kCFPreferencesCurrentApplication); 136*cdf0e10cSrcweir CFTypeRefGuard refGuard(ref); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir if (ref == NULL) 139*cdf0e10cSrcweir return NULL; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir CFStringRef sref = (CFGetTypeID(ref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)ref, 0) : (CFStringRef)ref; 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // NOTE: this API is only available with Mac OS X >=10.3. We need to use it because 144*cdf0e10cSrcweir // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't 145*cdf0e10cSrcweir // upgrade those values during upgrade to newer Mac OS X versions. See also #i54337# 146*cdf0e10cSrcweir return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir rtl::OUString ImplGetLocale(const char* pref) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir CFStringRef sref = ImplGetAppPreference(pref); 152*cdf0e10cSrcweir CFStringGuard srefGuard(sref); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir rtl::OUStringBuffer aLocaleBuffer; 155*cdf0e10cSrcweir aLocaleBuffer.appendAscii("en-US"); // initialize with fallback value 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if (sref != NULL) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir // split the string into substrings; the first two (if there are two) substrings 160*cdf0e10cSrcweir // are language and country 161*cdf0e10cSrcweir CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_")); 162*cdf0e10cSrcweir CFArrayGuard subsGuard(subs); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir if (subs != NULL) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir aLocaleBuffer.setLength(0); // clear buffer which still contains fallback value 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0); 169*cdf0e10cSrcweir OUStringBufferAppendCFString(aLocaleBuffer, lang); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // country also available? Assumption: if the array contains more than one 172*cdf0e10cSrcweir // value the second value is always the country! 173*cdf0e10cSrcweir if (CFArrayGetCount(subs) > 1) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir aLocaleBuffer.appendAscii("-"); 176*cdf0e10cSrcweir CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1); 177*cdf0e10cSrcweir OUStringBufferAppendCFString(aLocaleBuffer, country); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir return aLocaleBuffer.makeStringAndClear(); 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir } // namespace /* private */ 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir #endif 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir // ------------------------------------------------------------------------------- 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir #ifdef WNT 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir #ifdef WINVER 193*cdf0e10cSrcweir #undef WINVER 194*cdf0e10cSrcweir #endif 195*cdf0e10cSrcweir #define WINVER 0x0501 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir #if defined _MSC_VER 198*cdf0e10cSrcweir #pragma warning(push, 1) 199*cdf0e10cSrcweir #endif 200*cdf0e10cSrcweir #include <windows.h> 201*cdf0e10cSrcweir #if defined _MSC_VER 202*cdf0e10cSrcweir #pragma warning(pop) 203*cdf0e10cSrcweir #endif 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir rtl::OUString ImplGetLocale(LCID lcid) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir TCHAR buffer[8]; 208*cdf0e10cSrcweir LPTSTR cp = buffer; 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir cp += GetLocaleInfo( lcid, LOCALE_SISO639LANGNAME , buffer, 4 ); 211*cdf0e10cSrcweir if( cp > buffer ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir if( 0 < GetLocaleInfo( lcid, LOCALE_SISO3166CTRYNAME, cp, buffer + 8 - cp) ) 214*cdf0e10cSrcweir // #i50822# minus character must be written before cp 215*cdf0e10cSrcweir *(cp - 1) = '-'; 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir return rtl::OUString::createFromAscii(buffer); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir return rtl::OUString(); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir #endif // WNT 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir // ------------------------------------------------------------------------------- 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir LocaleBackend::LocaleBackend() 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir //------------------------------------------------------------------------------ 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir LocaleBackend::~LocaleBackend(void) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir //------------------------------------------------------------------------------ 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir LocaleBackend* LocaleBackend::createInstance() 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir return new LocaleBackend; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // --------------------------------------------------------------------------------------- 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir rtl::OUString LocaleBackend::getLocale(void) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir #if defined(LINUX) || defined(SOLARIS) || defined(NETBSD) || defined(FREEBSD) || defined(OS2) 249*cdf0e10cSrcweir return ImplGetLocale(LC_CTYPE); 250*cdf0e10cSrcweir #elif defined (MACOSX) 251*cdf0e10cSrcweir return ImplGetLocale("AppleLocale"); 252*cdf0e10cSrcweir #elif defined WNT 253*cdf0e10cSrcweir return ImplGetLocale( GetUserDefaultLCID() ); 254*cdf0e10cSrcweir #endif 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir //------------------------------------------------------------------------------ 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir rtl::OUString LocaleBackend::getUILocale(void) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir #if defined(LINUX) || defined(SOLARIS) || defined(NETBSD) || defined(FREEBSD) || defined(OS2) 262*cdf0e10cSrcweir return ImplGetLocale(LC_MESSAGES); 263*cdf0e10cSrcweir #elif defined(MACOSX) 264*cdf0e10cSrcweir return ImplGetLocale("AppleLanguages"); 265*cdf0e10cSrcweir #elif defined WNT 266*cdf0e10cSrcweir return ImplGetLocale( MAKELCID(GetUserDefaultUILanguage(), SORT_DEFAULT) ); 267*cdf0e10cSrcweir #endif 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir // --------------------------------------------------------------------------------------- 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir rtl::OUString LocaleBackend::getSystemLocale(void) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir // note: the implementation differs from getLocale() only on Windows 275*cdf0e10cSrcweir #if defined WNT 276*cdf0e10cSrcweir return ImplGetLocale( GetSystemDefaultLCID() ); 277*cdf0e10cSrcweir #else 278*cdf0e10cSrcweir return getLocale(); 279*cdf0e10cSrcweir #endif 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir //------------------------------------------------------------------------------ 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir void LocaleBackend::setPropertyValue( 284*cdf0e10cSrcweir rtl::OUString const &, css::uno::Any const &) 285*cdf0e10cSrcweir throw ( 286*cdf0e10cSrcweir css::beans::UnknownPropertyException, css::beans::PropertyVetoException, 287*cdf0e10cSrcweir css::lang::IllegalArgumentException, css::lang::WrappedTargetException, 288*cdf0e10cSrcweir css::uno::RuntimeException) 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir throw css::lang::IllegalArgumentException( 291*cdf0e10cSrcweir rtl::OUString( 292*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")), 293*cdf0e10cSrcweir static_cast< cppu::OWeakObject * >(this), -1); 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir css::uno::Any LocaleBackend::getPropertyValue( 297*cdf0e10cSrcweir rtl::OUString const & PropertyName) 298*cdf0e10cSrcweir throw ( 299*cdf0e10cSrcweir css::beans::UnknownPropertyException, css::lang::WrappedTargetException, 300*cdf0e10cSrcweir css::uno::RuntimeException) 301*cdf0e10cSrcweir { 302*cdf0e10cSrcweir if (PropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Locale"))) { 303*cdf0e10cSrcweir return css::uno::makeAny( 304*cdf0e10cSrcweir css::beans::Optional< css::uno::Any >( 305*cdf0e10cSrcweir true, css::uno::makeAny(getLocale()))); 306*cdf0e10cSrcweir } else if (PropertyName.equalsAsciiL( 307*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("SystemLocale"))) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir return css::uno::makeAny( 310*cdf0e10cSrcweir css::beans::Optional< css::uno::Any >( 311*cdf0e10cSrcweir true, css::uno::makeAny(getSystemLocale()))); 312*cdf0e10cSrcweir } else if (PropertyName.equalsAsciiL( 313*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("UILocale"))) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir return css::uno::makeAny( 316*cdf0e10cSrcweir css::beans::Optional< css::uno::Any >( 317*cdf0e10cSrcweir true, css::uno::makeAny(getUILocale()))); 318*cdf0e10cSrcweir } else { 319*cdf0e10cSrcweir throw css::beans::UnknownPropertyException( 320*cdf0e10cSrcweir PropertyName, static_cast< cppu::OWeakObject * >(this)); 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir //------------------------------------------------------------------------------ 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir rtl::OUString SAL_CALL LocaleBackend::getBackendName(void) { 327*cdf0e10cSrcweir return rtl::OUString::createFromAscii("com.sun.star.comp.configuration.backend.LocaleBackend") ; 328*cdf0e10cSrcweir } 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir //------------------------------------------------------------------------------ 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir rtl::OUString SAL_CALL LocaleBackend::getImplementationName(void) 333*cdf0e10cSrcweir throw (uno::RuntimeException) 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir return getBackendName() ; 336*cdf0e10cSrcweir } 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir //------------------------------------------------------------------------------ 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL LocaleBackend::getBackendServiceNames(void) 341*cdf0e10cSrcweir { 342*cdf0e10cSrcweir uno::Sequence<rtl::OUString> aServiceNameList(1); 343*cdf0e10cSrcweir aServiceNameList[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LocaleBackend")) ; 344*cdf0e10cSrcweir return aServiceNameList ; 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir //------------------------------------------------------------------------------ 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir sal_Bool SAL_CALL LocaleBackend::supportsService(const rtl::OUString& aServiceName) 350*cdf0e10cSrcweir throw (uno::RuntimeException) 351*cdf0e10cSrcweir { 352*cdf0e10cSrcweir uno::Sequence< rtl::OUString > const svc = getBackendServiceNames(); 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir for(sal_Int32 i = 0; i < svc.getLength(); ++i ) 355*cdf0e10cSrcweir if(svc[i] == aServiceName) 356*cdf0e10cSrcweir return true; 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir return false; 359*cdf0e10cSrcweir } 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir //------------------------------------------------------------------------------ 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL LocaleBackend::getSupportedServiceNames(void) 364*cdf0e10cSrcweir throw (uno::RuntimeException) 365*cdf0e10cSrcweir { 366*cdf0e10cSrcweir return getBackendServiceNames() ; 367*cdf0e10cSrcweir } 368