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

Generated by: LCOV version 1.11