Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blob582bdc125f78d8b2d3bf22975580de83ad1bc049
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 <cppuhelper/supportsservice.hxx>
24 /**************************************************************************
25 TODO
26 **************************************************************************
28 *************************************************************************/
30 #include <osl/file.hxx>
31 #include <com/sun/star/util/theMacroExpander.hpp>
32 #include <comphelper/fileurl.hxx>
33 #include <utility>
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(),
45 rURL,
46 aNormalizedURL )
47 != osl::DirectoryItem::E_None )
48 return false;
50 osl::DirectoryItem aDirItem;
51 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
52 != osl::DirectoryItem::E_None )
53 return false;
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;
66 else
67 rURL = aNormalizedURL
68 .copy( 0, aNormalizedURL.getLength() - 1 );
70 return true;
73 return false;
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 ))
88 // virtual
89 OfficeInstallationDirectories::~OfficeInstallationDirectories()
94 // util::XOfficeInstallationDirectories
97 // virtual
98 OUString SAL_CALL
99 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
101 initDirs();
102 return *m_xOfficeBrandDir;
106 // virtual
107 OUString SAL_CALL
108 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
110 initDirs();
111 return *m_xUserDir;
116 // virtual
117 OUString SAL_CALL
118 OfficeInstallationDirectories::makeRelocatableURL( const OUString& URL )
120 if ( !URL.isEmpty() )
122 initDirs();
124 OUString aCanonicalURL( URL );
125 makeCanonicalFileURL( aCanonicalURL );
127 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_xOfficeBrandDir );
128 if ( nIndex != -1 )
130 return
131 aCanonicalURL.replaceAt( nIndex,
132 m_xOfficeBrandDir->getLength(),
133 g_aOfficeBrandDirMacro );
135 else
137 nIndex = aCanonicalURL.indexOf( *m_xUserDir );
138 if ( nIndex != -1 )
140 return
141 aCanonicalURL.replaceAt( nIndex,
142 m_xUserDir->getLength(),
143 g_aUserDirMacro );
147 return URL;
151 // virtual
152 OUString SAL_CALL
153 OfficeInstallationDirectories::makeAbsoluteURL( const OUString& URL )
155 if ( !URL.isEmpty() )
157 sal_Int32 nIndex = URL.indexOf( g_aOfficeBrandDirMacro );
158 if ( nIndex != -1 )
160 initDirs();
162 return
163 URL.replaceAt( nIndex,
164 g_aOfficeBrandDirMacro.getLength(),
165 *m_xOfficeBrandDir );
167 else
169 nIndex = URL.indexOf( g_aUserDirMacro );
170 if ( nIndex != -1 )
172 initDirs();
174 return
175 URL.replaceAt( nIndex,
176 g_aUserDirMacro.getLength(),
177 *m_xUserDir );
181 return URL;
185 // lang::XServiceInfo
188 // virtual
189 OUString SAL_CALL
190 OfficeInstallationDirectories::getImplementationName()
192 return "com.sun.star.comp.util.OfficeInstallationDirectories";
195 // virtual
196 sal_Bool SAL_CALL
197 OfficeInstallationDirectories::supportsService( const OUString& ServiceName )
199 return cppu::supportsService(this, ServiceName);
202 // virtual
203 uno::Sequence< OUString > SAL_CALL
204 OfficeInstallationDirectories::getSupportedServiceNames()
206 return { "com.sun.star.util.OfficeInstallationDirectories" };
209 void OfficeInstallationDirectories::initDirs()
211 if ( m_xOfficeBrandDir)
212 return;
214 std::unique_lock aGuard( m_aMutex );
215 if ( m_xOfficeBrandDir )
216 return;
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 );
227 m_xUserDir =
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: */