LCOV - code coverage report
Current view: top level - libreoffice/shell/source/backends/localebe - localebackend.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 57 0.0 %
Date: 2012-12-27 Functions: 0 15 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10