bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blob044eeaa59287236a35cc8136d204b2c0b8664e9c
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 "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
24 #include <cppuhelper/supportsservice.hxx>
26 /**************************************************************************
27 TODO
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;
43 // helpers
47 static bool makeCanonicalFileURL( OUString & rURL )
49 OSL_ENSURE(rURL.startsWithIgnoreAsciiCase("file:"), "File URL expected!");
51 OUString aNormalizedURL;
52 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
53 rURL,
54 aNormalizedURL )
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;
72 else
73 rURL = aNormalizedURL
74 .copy( 0, aNormalizedURL.getLength() - 1 );
76 return true;
81 return false;
87 // OfficeInstallationDirectories Implementation.
92 OfficeInstallationDirectories::OfficeInstallationDirectories(
93 const uno::Reference< uno::XComponentContext > & xCtx )
94 : m_aOfficeBrandDirMacro( "$(brandbaseurl)" ),
95 m_aUserDirMacro( "$(userdataurl)" ),
96 m_xCtx( xCtx ),
97 m_pOfficeBrandDir( 0 ),
98 m_pUserDir( 0 )
103 // virtual
104 OfficeInstallationDirectories::~OfficeInstallationDirectories()
106 delete m_pOfficeBrandDir;
107 delete m_pUserDir;
111 // util::XOfficeInstallationDirectories
114 // virtual
115 OUString SAL_CALL
116 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
117 throw ( uno::RuntimeException, std::exception )
119 initDirs();
120 return OUString( *m_pOfficeBrandDir );
124 // virtual
125 OUString SAL_CALL
126 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
127 throw ( uno::RuntimeException, std::exception )
129 initDirs();
130 return OUString( *m_pUserDir );
135 // virtual
136 OUString SAL_CALL
137 OfficeInstallationDirectories::makeRelocatableURL( const OUString& URL )
138 throw ( uno::RuntimeException, std::exception )
140 if ( !URL.isEmpty() )
142 initDirs();
144 OUString aCanonicalURL( URL );
145 makeCanonicalFileURL( aCanonicalURL );
147 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeBrandDir );
148 if ( nIndex != -1 )
150 return OUString(
151 aCanonicalURL.replaceAt( nIndex,
152 m_pOfficeBrandDir->getLength(),
153 m_aOfficeBrandDirMacro ) );
155 else
157 nIndex = aCanonicalURL.indexOf( *m_pUserDir );
158 if ( nIndex != -1 )
160 return OUString(
161 aCanonicalURL.replaceAt( nIndex,
162 m_pUserDir->getLength(),
163 m_aUserDirMacro ) );
167 return OUString( URL );
171 // virtual
172 OUString SAL_CALL
173 OfficeInstallationDirectories::makeAbsoluteURL( const OUString& URL )
174 throw ( uno::RuntimeException, std::exception )
176 if ( !URL.isEmpty() )
178 sal_Int32 nIndex = URL.indexOf( m_aOfficeBrandDirMacro );
179 if ( nIndex != -1 )
181 initDirs();
183 return OUString(
184 URL.replaceAt( nIndex,
185 m_aOfficeBrandDirMacro.getLength(),
186 *m_pOfficeBrandDir ) );
188 else
190 nIndex = URL.indexOf( m_aUserDirMacro );
191 if ( nIndex != -1 )
193 initDirs();
195 return OUString(
196 URL.replaceAt( nIndex,
197 m_aUserDirMacro.getLength(),
198 *m_pUserDir ) );
202 return OUString( URL );
206 // lang::XServiceInfo
209 // virtual
210 OUString SAL_CALL
211 OfficeInstallationDirectories::getImplementationName()
212 throw ( uno::RuntimeException, std::exception )
214 return getImplementationName_static();
217 // virtual
218 sal_Bool SAL_CALL
219 OfficeInstallationDirectories::supportsService( const OUString& ServiceName )
220 throw ( uno::RuntimeException, std::exception )
222 return cppu::supportsService(this, ServiceName);
225 // virtual
226 uno::Sequence< OUString > SAL_CALL
227 OfficeInstallationDirectories::getSupportedServiceNames()
228 throw ( uno::RuntimeException, std::exception )
230 return getSupportedServiceNames_static();
234 // static
235 OUString SAL_CALL
236 OfficeInstallationDirectories::getImplementationName_static()
238 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
242 // static
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 );
251 // static
252 OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
254 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
258 // static
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 ) );
268 // non-UNO
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);
283 *m_pOfficeBrandDir =
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 );
292 *m_pUserDir =
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: */