Bump version to 6.4-15
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blobdec66895e94024461b9d9cbbf5ed4c10c11c7b25
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 <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(),
45 rURL,
46 aNormalizedURL )
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;
64 else
65 rURL = aNormalizedURL
66 .copy( 0, aNormalizedURL.getLength() - 1 );
68 return true;
73 return false;
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 )
83 : m_xCtx( 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)
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 );
225 m_xUserDir =
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 );
239 namespace {
241 struct Instance {
242 explicit Instance(
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;
251 struct Singleton:
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: */