LCOV - code coverage report
Current view: top level - libreoffice/desktop/source/deployment/misc - dp_resource.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 53 83 63.9 %
Date: 2012-12-17 Functions: 7 10 70.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 "dp_misc.h"
      22             : #include "dp_resource.h"
      23             : #include "osl/module.hxx"
      24             : #include "osl/mutex.hxx"
      25             : #include <tools/resmgr.hxx>
      26             : #include "rtl/ustring.h"
      27             : #include "cppuhelper/implbase1.hxx"
      28             : #include "unotools/configmgr.hxx"
      29             : 
      30             : 
      31             : using namespace ::com::sun::star;
      32             : using namespace ::com::sun::star::uno;
      33             : using ::rtl::OUString;
      34             : 
      35             : namespace dp_misc {
      36             : namespace {
      37             : 
      38             : struct OfficeLocale :
      39             :         public rtl::StaticWithInit<OUString, OfficeLocale> {
      40           2 :     const OUString operator () () {
      41           2 :         OUString slang(utl::ConfigManager::getLocale());
      42             :         //fallback, the locale is currently only set when the user starts the
      43             :         //office for the first time.
      44           2 :         if (slang.isEmpty())
      45           0 :             slang =  rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
      46           2 :         return slang;
      47             :     }
      48             : };
      49             : 
      50             : struct DeploymentResMgr : public rtl::StaticWithInit<
      51             :     ResMgr *, DeploymentResMgr> {
      52           2 :     ResMgr * operator () () {
      53           2 :         return ResMgr::CreateResMgr( "deployment", getOfficeLocale() );
      54             :     }
      55             : };
      56             : 
      57             : class theResourceMutex : public rtl::Static<osl::Mutex, theResourceMutex> {};
      58             : 
      59             : } // anon namespace
      60             : 
      61             : //==============================================================================
      62           0 : ResId getResId( sal_uInt16 id )
      63             : {
      64           0 :     const osl::MutexGuard guard( theResourceMutex::get() );
      65           0 :     return ResId( id, *DeploymentResMgr::get() );
      66             : }
      67             : 
      68             : //==============================================================================
      69          72 : String getResourceString( sal_uInt16 id )
      70             : {
      71          72 :     const osl::MutexGuard guard( theResourceMutex::get() );
      72          72 :     String ret( ResId( id, *DeploymentResMgr::get() ) );
      73             :     ret.SearchAndReplaceAllAscii(
      74          72 :         "%PRODUCTNAME", utl::ConfigManager::getProductName() );
      75          72 :     return ret;
      76             : }
      77             : 
      78             : //throws an Exception on failure
      79             : //primary subtag 2 or three letters(A-Z, a-z), i or x
      80          12 : void checkPrimarySubtag(::rtl::OUString const & tag)
      81             : {
      82          12 :     sal_Int32 len = tag.getLength();
      83          12 :     sal_Unicode const * arLang = tag.getStr();
      84          12 :     if (len < 1 || len > 3)
      85           0 :         throw Exception(OUSTR("Invalid language string."), 0);
      86             : 
      87          12 :     if (len == 1
      88             :         && (arLang[0] != 'i' && arLang[0] != 'x'))
      89           0 :         throw Exception(OUSTR("Invalid language string."), 0);
      90             : 
      91          12 :     if (len == 2 || len == 3)
      92             :     {
      93          36 :         for (sal_Int32 i = 0; i < len; i++)
      94             :         {
      95          24 :             if ( !((arLang[i] >= 'A' && arLang[i] <= 'Z')
      96          24 :                 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
      97             :             {
      98           0 :                 throw Exception(OUSTR("Invalid language string."), 0);
      99             :             }
     100             :         }
     101             :     }
     102          12 : }
     103             : 
     104             : //throws an Exception on failure
     105             : //second subtag 2 letter country code or 3-8 letter other code(A-Z, a-z, 0-9)
     106          12 : void checkSecondSubtag(::rtl::OUString const & tag, bool & bIsCountry)
     107             : {
     108          12 :     sal_Int32 len = tag.getLength();
     109          12 :     sal_Unicode const * arLang = tag.getStr();
     110          12 :     if (len < 2 || len > 8)
     111           0 :         throw Exception(OUSTR("Invalid language string."), 0);
     112             :     //country code
     113          12 :     bIsCountry = false;
     114          12 :     if (len == 2)
     115             :     {
     116          36 :         for (sal_Int32 i = 0; i < 2; i++)
     117             :         {
     118          24 :             if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
     119          24 :                 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
     120             :             {
     121           0 :                 throw Exception(OUSTR("Invalid language string."), 0);
     122             :             }
     123             :         }
     124          12 :         bIsCountry = true;
     125             :     }
     126             : 
     127          12 :     if (len > 2)
     128             :     {
     129           0 :         for (sal_Int32 i = 0; i < len; i++)
     130             :         {
     131           0 :             if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
     132           0 :                 || (arLang[i] >= 'a' && arLang[i] <= 'z')
     133           0 :                 || (arLang[i] >= '0' && arLang[i] <= '9') ))
     134             :             {
     135           0 :                 throw Exception(OUSTR("Invalid language string."), 0);
     136             :             }
     137             :         }
     138             :     }
     139          12 : }
     140             : 
     141           0 : void checkThirdSubtag(::rtl::OUString const & tag)
     142             : {
     143           0 :     sal_Int32 len = tag.getLength();
     144           0 :     sal_Unicode const * arLang = tag.getStr();
     145           0 :     if (len < 1 || len > 8)
     146           0 :         throw Exception(OUSTR("Invalid language string."), 0);
     147             : 
     148           0 :     for (sal_Int32 i = 0; i < len; i++)
     149             :     {
     150           0 :         if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
     151           0 :             || (arLang[i] >= 'a' && arLang[i] <= 'z')
     152           0 :             || (arLang[i] >= '0' && arLang[i] <= '9') ))
     153             :         {
     154           0 :             throw Exception(OUSTR("Invalid language string."), 0);
     155             :         }
     156             :     }
     157           0 : }
     158             : 
     159             : //=============================================================================
     160             : 
     161             : //We parse the string acording to RFC 3066
     162             : //We only use the primary sub-tag and two subtags. That is lang-country-variant
     163             : //We do some simple tests if the string is correct. Actually this should do a
     164             : //validating parser
     165             : //We may have the case that there is no country tag, for example en-welsh
     166          12 : ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
     167             : {
     168          12 :     OUString _sLang = slang.trim();
     169          12 :     ::com::sun::star::lang::Locale locale;
     170          12 :     sal_Int32 nIndex = 0;
     171          12 :     OUString lang = _sLang.getToken( 0, '-', nIndex );
     172          12 :     checkPrimarySubtag(lang);
     173          12 :     locale.Language = lang;
     174          12 :     OUString country = _sLang.getToken( 0, '-', nIndex );
     175          12 :     if (!country.isEmpty())
     176             :     {
     177          12 :         bool bIsCountry = false;
     178          12 :         checkSecondSubtag(country, bIsCountry);
     179          12 :         if (bIsCountry)
     180             :         {
     181          12 :             locale.Country = country;
     182             :         }
     183             :         else
     184             :         {
     185           0 :              locale.Variant = country;
     186             :         }
     187             :     }
     188          12 :     if (locale.Variant.isEmpty())
     189             :     {
     190          12 :         OUString variant = _sLang.getToken( 0, '-', nIndex );
     191          12 :         if (!variant.isEmpty())
     192             :         {
     193           0 :             checkThirdSubtag(variant);
     194           0 :             locale.Variant = variant;
     195          12 :         }
     196             :     }
     197             : 
     198          12 :     return locale;
     199             : }
     200             : 
     201             : //==============================================================================
     202          12 : lang::Locale getOfficeLocale()
     203             : {
     204          12 :     return toLocale(OfficeLocale::get());
     205             : }
     206             : 
     207           0 : ::rtl::OUString getOfficeLocaleString()
     208             : {
     209           0 :     return OfficeLocale::get();
     210             : }
     211             : 
     212             : }
     213             : 
     214             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10