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
.startsWithIgnoreAsciiCase("file:"), "File URL expected!");
51 OUString aNormalizedURL
;
52 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
55 == osl::DirectoryItem::E_None
)
57 osl::DirectoryItem aDirItem
;
58 if ( osl::DirectoryItem::get( aNormalizedURL
, aDirItem
)
59 == osl::DirectoryItem::E_None
)
61 osl::FileStatus
aFileStatus( osl_FileStatus_Mask_FileURL
);
63 if ( aDirItem
.getFileStatus( aFileStatus
)
64 == osl::DirectoryItem::E_None
)
66 aNormalizedURL
= aFileStatus
.getFileURL();
68 if ( !aNormalizedURL
.isEmpty() )
70 if ( !aNormalizedURL
.endsWith("/") )
71 rURL
= aNormalizedURL
;
74 .copy( 0, aNormalizedURL
.getLength() - 1 );
87 // OfficeInstallationDirectories Implementation.
92 OfficeInstallationDirectories::OfficeInstallationDirectories(
93 const uno::Reference
< uno::XComponentContext
> & xCtx
)
94 : m_aOfficeBrandDirMacro( "$(brandbaseurl)" ),
95 m_aUserDirMacro( "$(userdataurl)" ),
97 m_pOfficeBrandDir( 0 ),
104 OfficeInstallationDirectories::~OfficeInstallationDirectories()
106 delete m_pOfficeBrandDir
;
111 // util::XOfficeInstallationDirectories
116 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
117 throw ( uno::RuntimeException
, std::exception
)
120 return OUString( *m_pOfficeBrandDir
);
126 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
127 throw ( uno::RuntimeException
, std::exception
)
130 return OUString( *m_pUserDir
);
137 OfficeInstallationDirectories::makeRelocatableURL( const OUString
& URL
)
138 throw ( uno::RuntimeException
, std::exception
)
140 if ( !URL
.isEmpty() )
144 OUString
aCanonicalURL( URL
);
145 makeCanonicalFileURL( aCanonicalURL
);
147 sal_Int32 nIndex
= aCanonicalURL
.indexOf( *m_pOfficeBrandDir
);
151 aCanonicalURL
.replaceAt( nIndex
,
152 m_pOfficeBrandDir
->getLength(),
153 m_aOfficeBrandDirMacro
) );
157 nIndex
= aCanonicalURL
.indexOf( *m_pUserDir
);
161 aCanonicalURL
.replaceAt( nIndex
,
162 m_pUserDir
->getLength(),
167 return OUString( URL
);
173 OfficeInstallationDirectories::makeAbsoluteURL( const OUString
& URL
)
174 throw ( uno::RuntimeException
, std::exception
)
176 if ( !URL
.isEmpty() )
178 sal_Int32 nIndex
= URL
.indexOf( m_aOfficeBrandDirMacro
);
184 URL
.replaceAt( nIndex
,
185 m_aOfficeBrandDirMacro
.getLength(),
186 *m_pOfficeBrandDir
) );
190 nIndex
= URL
.indexOf( m_aUserDirMacro
);
196 URL
.replaceAt( nIndex
,
197 m_aUserDirMacro
.getLength(),
202 return OUString( URL
);
206 // lang::XServiceInfo
211 OfficeInstallationDirectories::getImplementationName()
212 throw ( uno::RuntimeException
, std::exception
)
214 return getImplementationName_static();
219 OfficeInstallationDirectories::supportsService( const OUString
& ServiceName
)
220 throw ( uno::RuntimeException
, std::exception
)
222 return cppu::supportsService(this, ServiceName
);
226 uno::Sequence
< OUString
> SAL_CALL
227 OfficeInstallationDirectories::getSupportedServiceNames()
228 throw ( uno::RuntimeException
, std::exception
)
230 return getSupportedServiceNames_static();
236 OfficeInstallationDirectories::getImplementationName_static()
238 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
243 uno::Sequence
< OUString
> SAL_CALL
244 OfficeInstallationDirectories::getSupportedServiceNames_static()
246 const OUString
aServiceName("com.sun.star.util.OfficeInstallationDirectories");
247 return uno::Sequence
< OUString
>( &aServiceName
, 1 );
252 OUString SAL_CALL
OfficeInstallationDirectories::getSingletonName_static()
254 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
259 uno::Reference
< uno::XInterface
> SAL_CALL
260 OfficeInstallationDirectories::Create(
261 const uno::Reference
< uno::XComponentContext
> & rxContext
)
263 return static_cast< cppu::OWeakObject
* >(
264 new OfficeInstallationDirectories( rxContext
) );
271 void OfficeInstallationDirectories::initDirs()
273 if ( m_pOfficeBrandDir
== 0 )
275 osl::MutexGuard
aGuard( m_aMutex
);
276 if ( m_pOfficeBrandDir
== 0 )
278 m_pOfficeBrandDir
= new OUString
;
279 m_pUserDir
= new OUString
;
281 uno::Reference
< util::XMacroExpander
> xExpander
= util::theMacroExpander::get(m_xCtx
);
284 xExpander
->expandMacros(
285 OUString( "$BRAND_BASE_DIR" ) );
287 OSL_ENSURE( !m_pOfficeBrandDir
->isEmpty(),
288 "Unable to obtain office brand installation directory!" );
290 makeCanonicalFileURL( *m_pOfficeBrandDir
);
293 xExpander
->expandMacros(
294 OUString("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) );
296 OSL_ENSURE( !m_pUserDir
->isEmpty(),
297 "Unable to obtain office user data directory!" );
299 makeCanonicalFileURL( *m_pUserDir
);
304 void createRegistryInfo_OfficeInstallationDirectories()
306 static ::comphelper::module::OSingletonRegistration
< OfficeInstallationDirectories
> aAutoRegistration
;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */