1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /**************************************************************************
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
;
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(),
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
;
75 .copy( 0, aNormalizedURL
.getLength() - 1 );
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)" ),
99 m_pOfficeBrandDir( 0 ),
106 OfficeInstallationDirectories::~OfficeInstallationDirectories()
108 delete m_pOfficeBrandDir
;
113 // util::XOfficeInstallationDirectories
118 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
119 throw ( uno::RuntimeException
, std::exception
)
122 return OUString( *m_pOfficeBrandDir
);
128 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
129 throw ( uno::RuntimeException
, std::exception
)
132 return OUString( *m_pUserDir
);
139 OfficeInstallationDirectories::makeRelocatableURL( const OUString
& URL
)
140 throw ( uno::RuntimeException
, std::exception
)
142 if ( !URL
.isEmpty() )
146 OUString
aCanonicalURL( URL
);
147 makeCanonicalFileURL( aCanonicalURL
);
149 sal_Int32 nIndex
= aCanonicalURL
.indexOf( *m_pOfficeBrandDir
);
153 aCanonicalURL
.replaceAt( nIndex
,
154 m_pOfficeBrandDir
->getLength(),
155 m_aOfficeBrandDirMacro
) );
159 nIndex
= aCanonicalURL
.indexOf( *m_pUserDir
);
163 aCanonicalURL
.replaceAt( nIndex
,
164 m_pUserDir
->getLength(),
169 return OUString( URL
);
175 OfficeInstallationDirectories::makeAbsoluteURL( const OUString
& URL
)
176 throw ( uno::RuntimeException
, std::exception
)
178 if ( !URL
.isEmpty() )
180 sal_Int32 nIndex
= URL
.indexOf( m_aOfficeBrandDirMacro
);
186 URL
.replaceAt( nIndex
,
187 m_aOfficeBrandDirMacro
.getLength(),
188 *m_pOfficeBrandDir
) );
192 nIndex
= URL
.indexOf( m_aUserDirMacro
);
198 URL
.replaceAt( nIndex
,
199 m_aUserDirMacro
.getLength(),
204 return OUString( URL
);
208 // lang::XServiceInfo
213 OfficeInstallationDirectories::getImplementationName()
214 throw ( uno::RuntimeException
, std::exception
)
216 return getImplementationName_static();
221 OfficeInstallationDirectories::supportsService( const OUString
& ServiceName
)
222 throw ( uno::RuntimeException
, std::exception
)
224 return cppu::supportsService(this, ServiceName
);
228 uno::Sequence
< OUString
> SAL_CALL
229 OfficeInstallationDirectories::getSupportedServiceNames()
230 throw ( uno::RuntimeException
, std::exception
)
232 return getSupportedServiceNames_static();
238 OfficeInstallationDirectories::getImplementationName_static()
240 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
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 );
254 OUString SAL_CALL
OfficeInstallationDirectories::getSingletonName_static()
256 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
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
) );
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
);
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
);
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: */