Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blobd7c8fbd20c085a9c3538893123ac2c23d6dfdaaf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_folders.h>
22 #include "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
24 #include <cppuhelper/supportsservice.hxx>
26 /**************************************************************************
27 TODO
28 **************************************************************************
30 *************************************************************************/
32 #include <osl/file.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/util/theMacroExpander.hpp>
36 #include "officeinstallationdirectories.hxx"
38 using namespace com::sun::star;
40 using namespace comphelper;
43 // helpers
47 static bool makeCanonicalFileURL( OUString & rURL )
49 OSL_ENSURE( rURL.matchAsciiL( "file:", sizeof( "file:" ) - 1 , 0 ) ,
50 "File URL expected!" );
52 OUString aNormalizedURL;
53 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
54 rURL,
55 aNormalizedURL )
56 == osl::DirectoryItem::E_None )
58 osl::DirectoryItem aDirItem;
59 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
60 == osl::DirectoryItem::E_None )
62 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
64 if ( aDirItem.getFileStatus( aFileStatus )
65 == osl::DirectoryItem::E_None )
67 aNormalizedURL = aFileStatus.getFileURL();
69 if ( !aNormalizedURL.isEmpty() )
71 if ( !aNormalizedURL.endsWith("/") )
72 rURL = aNormalizedURL;
73 else
74 rURL = aNormalizedURL
75 .copy( 0, aNormalizedURL.getLength() - 1 );
77 return true;
82 return false;
88 // OfficeInstallationDirectories Implementation.
93 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 m_pUserDir( 0 )
105 // virtual
106 OfficeInstallationDirectories::~OfficeInstallationDirectories()
108 delete m_pOfficeBrandDir;
109 delete m_pUserDir;
113 // util::XOfficeInstallationDirectories
116 // virtual
117 OUString SAL_CALL
118 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
119 throw ( uno::RuntimeException, std::exception )
121 initDirs();
122 return OUString( *m_pOfficeBrandDir );
126 // virtual
127 OUString SAL_CALL
128 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
129 throw ( uno::RuntimeException, std::exception )
131 initDirs();
132 return OUString( *m_pUserDir );
137 // virtual
138 OUString SAL_CALL
139 OfficeInstallationDirectories::makeRelocatableURL( const OUString& URL )
140 throw ( uno::RuntimeException, std::exception )
142 if ( !URL.isEmpty() )
144 initDirs();
146 OUString aCanonicalURL( URL );
147 makeCanonicalFileURL( aCanonicalURL );
149 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeBrandDir );
150 if ( nIndex != -1 )
152 return OUString(
153 aCanonicalURL.replaceAt( nIndex,
154 m_pOfficeBrandDir->getLength(),
155 m_aOfficeBrandDirMacro ) );
157 else
159 nIndex = aCanonicalURL.indexOf( *m_pUserDir );
160 if ( nIndex != -1 )
162 return OUString(
163 aCanonicalURL.replaceAt( nIndex,
164 m_pUserDir->getLength(),
165 m_aUserDirMacro ) );
169 return OUString( URL );
173 // virtual
174 OUString SAL_CALL
175 OfficeInstallationDirectories::makeAbsoluteURL( const OUString& URL )
176 throw ( uno::RuntimeException, std::exception )
178 if ( !URL.isEmpty() )
180 sal_Int32 nIndex = URL.indexOf( m_aOfficeBrandDirMacro );
181 if ( nIndex != -1 )
183 initDirs();
185 return OUString(
186 URL.replaceAt( nIndex,
187 m_aOfficeBrandDirMacro.getLength(),
188 *m_pOfficeBrandDir ) );
190 else
192 nIndex = URL.indexOf( m_aUserDirMacro );
193 if ( nIndex != -1 )
195 initDirs();
197 return OUString(
198 URL.replaceAt( nIndex,
199 m_aUserDirMacro.getLength(),
200 *m_pUserDir ) );
204 return OUString( URL );
208 // lang::XServiceInfo
211 // virtual
212 OUString SAL_CALL
213 OfficeInstallationDirectories::getImplementationName()
214 throw ( uno::RuntimeException, std::exception )
216 return getImplementationName_static();
219 // virtual
220 sal_Bool SAL_CALL
221 OfficeInstallationDirectories::supportsService( const OUString& ServiceName )
222 throw ( uno::RuntimeException, std::exception )
224 return cppu::supportsService(this, ServiceName);
227 // virtual
228 uno::Sequence< OUString > SAL_CALL
229 OfficeInstallationDirectories::getSupportedServiceNames()
230 throw ( uno::RuntimeException, std::exception )
232 return getSupportedServiceNames_static();
236 // static
237 OUString SAL_CALL
238 OfficeInstallationDirectories::getImplementationName_static()
240 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
244 // static
245 uno::Sequence< OUString > SAL_CALL
246 OfficeInstallationDirectories::getSupportedServiceNames_static()
248 const OUString aServiceName("com.sun.star.util.OfficeInstallationDirectories");
249 return uno::Sequence< OUString >( &aServiceName, 1 );
253 // static
254 OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
256 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
260 // static
261 uno::Reference< uno::XInterface > SAL_CALL
262 OfficeInstallationDirectories::Create(
263 const uno::Reference< uno::XComponentContext > & rxContext )
265 return static_cast< cppu::OWeakObject * >(
266 new OfficeInstallationDirectories( rxContext ) );
270 // non-UNO
273 void OfficeInstallationDirectories::initDirs()
275 if ( m_pOfficeBrandDir == 0 )
277 osl::MutexGuard aGuard( m_aMutex );
278 if ( m_pOfficeBrandDir == 0 )
280 m_pOfficeBrandDir = new OUString;
281 m_pUserDir = new OUString;
283 uno::Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(m_xCtx);
285 *m_pOfficeBrandDir =
286 xExpander->expandMacros(
287 OUString( "$BRAND_BASE_DIR" ) );
289 OSL_ENSURE( !m_pOfficeBrandDir->isEmpty(),
290 "Unable to obtain office brand installation directory!" );
292 makeCanonicalFileURL( *m_pOfficeBrandDir );
294 *m_pUserDir =
295 xExpander->expandMacros(
296 OUString("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) );
298 OSL_ENSURE( !m_pUserDir->isEmpty(),
299 "Unable to obtain office user data directory!" );
301 makeCanonicalFileURL( *m_pUserDir );
306 void createRegistryInfo_OfficeInstallationDirectories()
308 static ::comphelper::module::OSingletonRegistration< OfficeInstallationDirectories > aAutoRegistration;
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */