LCOV - code coverage report
Current view: top level - comphelper/source/officeinstdir - officeinstallationdirectories.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 98 77.6 %
Date: 2012-08-25 Functions: 12 17 70.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 55 114 48.2 %

           Branch data     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 "comphelper_module.hxx"
      22                 :            : 
      23                 :            : /**************************************************************************
      24                 :            :                                 TODO
      25                 :            :  **************************************************************************
      26                 :            : 
      27                 :            :  *************************************************************************/
      28                 :            : 
      29                 :            : #include "osl/file.hxx"
      30                 :            : #include "com/sun/star/beans/XPropertySet.hpp"
      31                 :            : #include "com/sun/star/util/XMacroExpander.hpp"
      32                 :            : 
      33                 :            : #include "officeinstallationdirectories.hxx"
      34                 :            : 
      35                 :            : using namespace com::sun::star;
      36                 :            : 
      37                 :            : using namespace comphelper;
      38                 :            : 
      39                 :            : //=========================================================================
      40                 :            : // helpers
      41                 :            : //=========================================================================
      42                 :            : 
      43                 :            : //=========================================================================
      44                 :         34 : static bool makeCanonicalFileURL( rtl::OUString & rURL )
      45                 :            : {
      46                 :            :     OSL_ENSURE( rURL.matchAsciiL( "file:", sizeof( "file:" ) - 1 , 0 ) ,
      47                 :            :                 "File URL expected!" );
      48                 :            : 
      49                 :         34 :     rtl::OUString aNormalizedURL;
      50         [ +  - ]:         34 :     if ( osl::FileBase::getAbsoluteFileURL( rtl::OUString(),
      51                 :            :                                             rURL,
      52         [ +  - ]:         34 :                                             aNormalizedURL )
      53                 :            :             == osl::DirectoryItem::E_None )
      54                 :            :     {
      55                 :         34 :         osl::DirectoryItem aDirItem;
      56 [ +  - ][ +  - ]:         34 :         if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
      57                 :            :                 == osl::DirectoryItem::E_None )
      58                 :            :         {
      59                 :         34 :             osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
      60                 :            : 
      61 [ +  - ][ +  - ]:         34 :             if ( aDirItem.getFileStatus( aFileStatus )
      62                 :            :                     == osl::DirectoryItem::E_None )
      63                 :            :             {
      64         [ +  - ]:         34 :                 aNormalizedURL = aFileStatus.getFileURL();
      65                 :            : 
      66         [ +  - ]:         34 :                 if ( !aNormalizedURL.isEmpty() )
      67                 :            :                 {
      68         [ +  - ]:         34 :                     if ( aNormalizedURL
      69                 :         34 :                             .getStr()[ aNormalizedURL.getLength() - 1 ]
      70                 :            :                                 != sal_Unicode( '/' ) )
      71                 :         34 :                         rURL = aNormalizedURL;
      72                 :            :                     else
      73                 :            :                         rURL = aNormalizedURL
      74                 :          0 :                                 .copy( 0, aNormalizedURL.getLength() - 1 );
      75                 :            : 
      76                 :         34 :                     return true;
      77                 :            :                 }
      78         [ -  + ]:         34 :             }
      79 [ +  - ][ -  + ]:         34 :         }
      80                 :            :     }
      81                 :         34 :     return false;
      82                 :            : }
      83                 :            : 
      84                 :            : //=========================================================================
      85                 :            : //=========================================================================
      86                 :            : //
      87                 :            : // OfficeInstallationDirectories Implementation.
      88                 :            : //
      89                 :            : //=========================================================================
      90                 :            : //=========================================================================
      91                 :            : 
      92                 :          2 : OfficeInstallationDirectories::OfficeInstallationDirectories(
      93                 :            :         const uno::Reference< uno::XComponentContext > & xCtx )
      94                 :            : : m_aOfficeBrandDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(brandbaseurl)" ) ),
      95                 :            :   m_aOfficeBaseDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(baseinsturl)" ) ),
      96                 :            :   m_aUserDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(userdataurl)" ) ),
      97                 :            :   m_xCtx( xCtx ),
      98                 :            :   m_pOfficeBrandDir( 0 ),
      99 [ +  - ][ +  - ]:          2 :   m_pUserDir( 0 )
         [ +  - ][ +  - ]
     100                 :            : {
     101                 :          2 : }
     102                 :            : 
     103                 :            : //=========================================================================
     104                 :            : // virtual
     105         [ +  - ]:          2 : OfficeInstallationDirectories::~OfficeInstallationDirectories()
     106                 :            : {
     107         [ +  - ]:          2 :     delete m_pOfficeBrandDir;
     108         [ +  - ]:          2 :     delete m_pUserDir;
     109         [ -  + ]:          4 : }
     110                 :            : 
     111                 :            : //=========================================================================
     112                 :            : // util::XOfficeInstallationDirectories
     113                 :            : //=========================================================================
     114                 :            : 
     115                 :            : // virtual
     116                 :            : rtl::OUString SAL_CALL
     117                 :          0 : OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
     118                 :            :     throw ( uno::RuntimeException )
     119                 :            : {
     120                 :          0 :     initDirs();
     121                 :          0 :     return rtl::OUString( *m_pOfficeBrandDir );
     122                 :            : }
     123                 :            : 
     124                 :            : //=========================================================================
     125                 :            : // virtual
     126                 :            : rtl::OUString SAL_CALL
     127                 :          0 : OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
     128                 :            :     throw ( uno::RuntimeException )
     129                 :            : {
     130                 :          0 :     initDirs();
     131                 :          0 :     return rtl::OUString( *m_pUserDir );
     132                 :            : }
     133                 :            : 
     134                 :            : 
     135                 :            : //=========================================================================
     136                 :            : // virtual
     137                 :            : rtl::OUString SAL_CALL
     138                 :         30 : OfficeInstallationDirectories::makeRelocatableURL( const rtl::OUString& URL )
     139                 :            :     throw ( uno::RuntimeException )
     140                 :            : {
     141         [ +  - ]:         30 :     if ( !URL.isEmpty() )
     142                 :            :     {
     143         [ +  - ]:         30 :         initDirs();
     144                 :            : 
     145                 :         30 :         rtl::OUString aCanonicalURL( URL );
     146         [ +  - ]:         30 :         makeCanonicalFileURL( aCanonicalURL );
     147                 :            : 
     148                 :         30 :         sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeBrandDir );
     149         [ +  + ]:         30 :         if ( nIndex  != -1 )
     150                 :            :         {
     151                 :            :             return rtl::OUString(
     152                 :            :                 aCanonicalURL.replaceAt( nIndex,
     153                 :            :                                          m_pOfficeBrandDir->getLength(),
     154                 :         26 :                                          m_aOfficeBrandDirMacro ) );
     155                 :            :         }
     156                 :            :         else
     157                 :            :         {
     158                 :          4 :             nIndex = aCanonicalURL.indexOf( *m_pUserDir );
     159         [ +  - ]:          4 :             if ( nIndex  != -1 )
     160                 :            :             {
     161                 :            :                 return rtl::OUString(
     162                 :            :                     aCanonicalURL.replaceAt( nIndex,
     163                 :            :                                              m_pUserDir->getLength(),
     164                 :          4 :                                              m_aUserDirMacro ) );
     165                 :            :             }
     166         [ -  + ]:         30 :         }
     167                 :            :     }
     168                 :         30 :     return rtl::OUString( URL );
     169                 :            : }
     170                 :            : 
     171                 :            : //=========================================================================
     172                 :            : // virtual
     173                 :            : rtl::OUString SAL_CALL
     174                 :         20 : OfficeInstallationDirectories::makeAbsoluteURL( const rtl::OUString& URL )
     175                 :            :     throw ( uno::RuntimeException )
     176                 :            : {
     177         [ +  - ]:         20 :     if ( !URL.isEmpty() )
     178                 :            :     {
     179                 :         20 :         sal_Int32 nIndex = URL.indexOf( m_aOfficeBrandDirMacro );
     180         [ +  - ]:         20 :         if ( nIndex != -1 )
     181                 :            :         {
     182                 :         20 :             initDirs();
     183                 :            : 
     184                 :            :             return rtl::OUString(
     185                 :            :                 URL.replaceAt( nIndex,
     186                 :            :                                m_aOfficeBrandDirMacro.getLength(),
     187                 :         20 :                                *m_pOfficeBrandDir ) );
     188                 :            :         }
     189                 :            :         else
     190                 :            :         {
     191                 :          0 :             nIndex = URL.indexOf( m_aUserDirMacro );
     192         [ #  # ]:          0 :             if ( nIndex != -1 )
     193                 :            :             {
     194                 :          0 :                 initDirs();
     195                 :            : 
     196                 :            :                 return rtl::OUString(
     197                 :            :                     URL.replaceAt( nIndex,
     198                 :            :                                    m_aUserDirMacro.getLength(),
     199                 :          0 :                                    *m_pUserDir ) );
     200                 :            :             }
     201                 :            :         }
     202                 :            :     }
     203                 :         20 :     return rtl::OUString( URL );
     204                 :            : }
     205                 :            : 
     206                 :            : //=========================================================================
     207                 :            : // lang::XServiceInfo
     208                 :            : //=========================================================================
     209                 :            : 
     210                 :            : // virtual
     211                 :            : rtl::OUString SAL_CALL
     212                 :          0 : OfficeInstallationDirectories::getImplementationName()
     213                 :            :     throw ( uno::RuntimeException )
     214                 :            : {
     215                 :          0 :     return getImplementationName_static();
     216                 :            : }
     217                 :            : 
     218                 :            : //=========================================================================
     219                 :            : // virtual
     220                 :            : sal_Bool SAL_CALL
     221                 :          0 : OfficeInstallationDirectories::supportsService( const rtl::OUString& ServiceName )
     222                 :            :     throw ( uno::RuntimeException )
     223                 :            : {
     224                 :            :     const uno::Sequence< rtl::OUString > & aNames
     225                 :          0 :         = getSupportedServiceNames();
     226                 :          0 :     const rtl::OUString * p = aNames.getConstArray();
     227         [ #  # ]:          0 :     for ( sal_Int32 nPos = 0; nPos < aNames.getLength(); nPos++ )
     228                 :            :     {
     229         [ #  # ]:          0 :         if ( p[ nPos ].equals( ServiceName ) )
     230                 :          0 :             return sal_True;
     231                 :            :     }
     232                 :          0 :     return sal_False;
     233                 :            : 
     234                 :            : }
     235                 :            : 
     236                 :            : //=========================================================================
     237                 :            : // virtual
     238                 :            : uno::Sequence< ::rtl::OUString > SAL_CALL
     239                 :          0 : OfficeInstallationDirectories::getSupportedServiceNames()
     240                 :            :     throw ( uno::RuntimeException )
     241                 :            : {
     242                 :          0 :     return getSupportedServiceNames_static();
     243                 :            : }
     244                 :            : 
     245                 :            : //=========================================================================
     246                 :            : // static
     247                 :            : rtl::OUString SAL_CALL
     248                 :        227 : OfficeInstallationDirectories::getImplementationName_static()
     249                 :            : {
     250                 :            :     return rtl::OUString(
     251                 :            :         RTL_CONSTASCII_USTRINGPARAM(
     252                 :        227 :             "com.sun.star.comp.util.OfficeInstallationDirectories" ) );
     253                 :            : }
     254                 :            : 
     255                 :            : //=========================================================================
     256                 :            : // static
     257                 :            : uno::Sequence< ::rtl::OUString > SAL_CALL
     258                 :        227 : OfficeInstallationDirectories::getSupportedServiceNames_static()
     259                 :            : {
     260                 :            :     const rtl::OUString aServiceName(
     261                 :            :         RTL_CONSTASCII_USTRINGPARAM(
     262         [ +  - ]:        227 :             "com.sun.star.util.OfficeInstallationDirectories" ) );
     263         [ +  - ]:        227 :     return uno::Sequence< rtl::OUString >( &aServiceName, 1 );
     264                 :            : }
     265                 :            : 
     266                 :            : //=========================================================================
     267                 :            : // static
     268                 :        227 : rtl::OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
     269                 :            : {
     270                 :            :     return rtl::OUString(
     271                 :            :         RTL_CONSTASCII_USTRINGPARAM(
     272                 :        227 :             "com.sun.star.util.theOfficeInstallationDirectories" ) );
     273                 :            : }
     274                 :            : 
     275                 :            : //=========================================================================
     276                 :            : // static
     277                 :            : uno::Reference< uno::XInterface > SAL_CALL
     278                 :          2 : OfficeInstallationDirectories::Create(
     279                 :            :         const uno::Reference< uno::XComponentContext > & rxContext )
     280                 :            : {
     281                 :            :     return static_cast< cppu::OWeakObject * >(
     282         [ +  - ]:          2 :         new OfficeInstallationDirectories( rxContext ) );
     283                 :            : }
     284                 :            : 
     285                 :            : //=========================================================================
     286                 :            : // non-UNO
     287                 :            : //=========================================================================
     288                 :            : 
     289                 :         50 : void OfficeInstallationDirectories::initDirs()
     290                 :            : {
     291         [ +  + ]:         50 :     if ( m_pOfficeBrandDir == 0 )
     292                 :            :     {
     293         [ +  - ]:          2 :         osl::MutexGuard aGuard( m_aMutex );
     294         [ +  - ]:          2 :         if ( m_pOfficeBrandDir == 0 )
     295                 :            :         {
     296         [ +  - ]:          2 :             m_pOfficeBrandDir = new rtl::OUString;
     297         [ +  - ]:          2 :             m_pUserDir        = new rtl::OUString;
     298                 :            : 
     299                 :          2 :             uno::Reference< util::XMacroExpander > xExpander;
     300                 :            : 
     301         [ +  - ]:          2 :             m_xCtx->getValueByName(
     302                 :            :                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
     303                 :          2 :                     "/singletons/com.sun.star.util.theMacroExpander" ) ) )
     304 [ +  - ][ +  - ]:          2 :             >>= xExpander;
                 [ +  - ]
     305                 :            : 
     306                 :            :             OSL_ENSURE( xExpander.is(),
     307                 :            :                         "Unable to obtain macro expander singleton!" );
     308                 :            : 
     309         [ +  - ]:          2 :             if ( xExpander.is() )
     310                 :            :             {
     311                 :            :                 *m_pOfficeBrandDir =
     312         [ +  - ]:          2 :                     xExpander->expandMacros(
     313 [ +  - ][ +  - ]:          2 :                          rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$BRAND_BASE_DIR" ) ) );
     314                 :            : 
     315                 :            :                 OSL_ENSURE( !m_pOfficeBrandDir->isEmpty(),
     316                 :            :                             "Unable to obtain office brand installation directory!" );
     317                 :            : 
     318         [ +  - ]:          2 :                 makeCanonicalFileURL( *m_pOfficeBrandDir );
     319                 :            : 
     320                 :            :                 *m_pUserDir =
     321         [ +  - ]:          2 :                     xExpander->expandMacros(
     322                 :            :                         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
     323 [ +  - ][ +  - ]:          2 :                             "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) ) );
     324                 :            : 
     325                 :            :                 OSL_ENSURE( !m_pUserDir->isEmpty(),
     326                 :            :                             "Unable to obtain office user data directory!" );
     327                 :            : 
     328         [ +  - ]:          2 :                 makeCanonicalFileURL( *m_pUserDir );
     329                 :          2 :             }
     330         [ +  - ]:          2 :         }
     331                 :            :     }
     332                 :         50 : }
     333                 :            : 
     334                 :        227 : void createRegistryInfo_OfficeInstallationDirectories()
     335                 :            : {
     336 [ +  - ][ +  - ]:        227 :     static ::comphelper::module::OSingletonRegistration< OfficeInstallationDirectories > aAutoRegistration;
         [ +  - ][ #  # ]
     337                 :        227 : }
     338                 :            : 
     339                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10