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 <cppuhelper/supportsservice.hxx>
24 /**************************************************************************
26 **************************************************************************
28 *************************************************************************/
30 #include <osl/file.hxx>
31 #include <rtl/ref.hxx>
32 #include <com/sun/star/util/theMacroExpander.hpp>
33 #include <comphelper/fileurl.hxx>
35 #include "officeinstallationdirectories.hxx"
37 using namespace com::sun::star
;
39 static bool makeCanonicalFileURL( OUString
& rURL
)
41 OSL_ENSURE(comphelper::isFileUrl(rURL
), "File URL expected!");
43 OUString aNormalizedURL
;
44 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
47 == osl::DirectoryItem::E_None
)
49 osl::DirectoryItem aDirItem
;
50 if ( osl::DirectoryItem::get( aNormalizedURL
, aDirItem
)
51 == osl::DirectoryItem::E_None
)
53 osl::FileStatus
aFileStatus( osl_FileStatus_Mask_FileURL
);
55 if ( aDirItem
.getFileStatus( aFileStatus
)
56 == osl::DirectoryItem::E_None
)
58 aNormalizedURL
= aFileStatus
.getFileURL();
60 if ( !aNormalizedURL
.isEmpty() )
62 if ( !aNormalizedURL
.endsWith("/") )
63 rURL
= aNormalizedURL
;
66 .copy( 0, aNormalizedURL
.getLength() - 1 );
76 namespace comphelper
{
78 static OUString
const g_aOfficeBrandDirMacro("$(brandbaseurl)");
79 static OUString
const g_aUserDirMacro("$(userdataurl)");
81 OfficeInstallationDirectories::OfficeInstallationDirectories(
82 const uno::Reference
< uno::XComponentContext
> & xCtx
)
89 OfficeInstallationDirectories::~OfficeInstallationDirectories()
94 // util::XOfficeInstallationDirectories
99 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
102 return *m_xOfficeBrandDir
;
108 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
118 OfficeInstallationDirectories::makeRelocatableURL( const OUString
& URL
)
120 if ( !URL
.isEmpty() )
124 OUString
aCanonicalURL( URL
);
125 makeCanonicalFileURL( aCanonicalURL
);
127 sal_Int32 nIndex
= aCanonicalURL
.indexOf( *m_xOfficeBrandDir
);
131 aCanonicalURL
.replaceAt( nIndex
,
132 m_xOfficeBrandDir
->getLength(),
133 g_aOfficeBrandDirMacro
);
137 nIndex
= aCanonicalURL
.indexOf( *m_xUserDir
);
141 aCanonicalURL
.replaceAt( nIndex
,
142 m_xUserDir
->getLength(),
153 OfficeInstallationDirectories::makeAbsoluteURL( const OUString
& URL
)
155 if ( !URL
.isEmpty() )
157 sal_Int32 nIndex
= URL
.indexOf( g_aOfficeBrandDirMacro
);
163 URL
.replaceAt( nIndex
,
164 g_aOfficeBrandDirMacro
.getLength(),
165 *m_xOfficeBrandDir
);
169 nIndex
= URL
.indexOf( g_aUserDirMacro
);
175 URL
.replaceAt( nIndex
,
176 g_aUserDirMacro
.getLength(),
185 // lang::XServiceInfo
190 OfficeInstallationDirectories::getImplementationName()
192 return "com.sun.star.comp.util.OfficeInstallationDirectories";
197 OfficeInstallationDirectories::supportsService( const OUString
& ServiceName
)
199 return cppu::supportsService(this, ServiceName
);
203 uno::Sequence
< OUString
> SAL_CALL
204 OfficeInstallationDirectories::getSupportedServiceNames()
206 return { "com.sun.star.util.OfficeInstallationDirectories" };
209 void OfficeInstallationDirectories::initDirs()
211 if ( !m_xOfficeBrandDir
)
213 osl::MutexGuard
aGuard( m_aMutex
);
214 if ( !m_xOfficeBrandDir
)
216 uno::Reference
< util::XMacroExpander
> xExpander
= util::theMacroExpander::get(m_xCtx
);
218 m_xOfficeBrandDir
= xExpander
->expandMacros( "$BRAND_BASE_DIR" );
220 OSL_ENSURE( !m_xOfficeBrandDir
->isEmpty(),
221 "Unable to obtain office brand installation directory!" );
223 makeCanonicalFileURL( *m_xOfficeBrandDir
);
226 xExpander
->expandMacros(
227 "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" );
229 OSL_ENSURE( !m_xUserDir
->isEmpty(),
230 "Unable to obtain office user data directory!" );
232 makeCanonicalFileURL( *m_xUserDir
);
243 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
244 instance(static_cast<cppu::OWeakObject
*>(
245 new comphelper::OfficeInstallationDirectories(context
)))
248 rtl::Reference
<css::uno::XInterface
> instance
;
252 public rtl::StaticWithArg
<
253 Instance
, css::uno::Reference
<css::uno::XComponentContext
>, Singleton
>
258 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
259 com_sun_star_comp_util_OfficeInstallationDirectories(
260 css::uno::XComponentContext
*context
,
261 css::uno::Sequence
<css::uno::Any
> const &)
263 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
264 Singleton::get(context
).instance
.get()));
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */