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 <com/sun/star/util/theMacroExpander.hpp>
32 #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
)
50 osl::DirectoryItem aDirItem
;
51 if ( osl::DirectoryItem::get( aNormalizedURL
, aDirItem
)
52 != osl::DirectoryItem::E_None
)
55 osl::FileStatus
aFileStatus( osl_FileStatus_Mask_FileURL
);
57 if ( aDirItem
.getFileStatus( aFileStatus
)
58 == osl::DirectoryItem::E_None
)
60 aNormalizedURL
= aFileStatus
.getFileURL();
62 if ( !aNormalizedURL
.isEmpty() )
64 if ( !aNormalizedURL
.endsWith("/") )
65 rURL
= aNormalizedURL
;
68 .copy( 0, aNormalizedURL
.getLength() - 1 );
76 namespace comphelper
{
78 constexpr OUStringLiteral
g_aOfficeBrandDirMacro(u
"$(brandbaseurl)");
79 constexpr OUStringLiteral
g_aUserDirMacro(u
"$(userdataurl)");
81 OfficeInstallationDirectories::OfficeInstallationDirectories(
82 uno::Reference
< uno::XComponentContext
> xCtx
)
83 : m_xCtx(std::move( 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
)
214 std::unique_lock
aGuard( m_aMutex
);
215 if ( m_xOfficeBrandDir
)
218 uno::Reference
< util::XMacroExpander
> xExpander
= util::theMacroExpander::get(m_xCtx
);
220 m_xOfficeBrandDir
= xExpander
->expandMacros( "$BRAND_BASE_DIR" );
222 OSL_ENSURE( !m_xOfficeBrandDir
->isEmpty(),
223 "Unable to obtain office brand installation directory!" );
225 makeCanonicalFileURL( *m_xOfficeBrandDir
);
228 xExpander
->expandMacros(
229 "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" );
231 OSL_ENSURE( !m_xUserDir
->isEmpty(),
232 "Unable to obtain office user data directory!" );
234 makeCanonicalFileURL( *m_xUserDir
);
240 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
241 com_sun_star_comp_util_OfficeInstallationDirectories(
242 css::uno::XComponentContext
*context
,
243 css::uno::Sequence
<css::uno::Any
> const &)
245 return cppu::acquire(
246 new comphelper::OfficeInstallationDirectories(context
));
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */