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

Generated by: LCOV version 1.10