CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blobebeedc92839daf65f0f7230b8447172197187d40
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include "comphelper_module.hxx"
33 /**************************************************************************
34 TODO
35 **************************************************************************
37 *************************************************************************/
39 #include "osl/file.hxx"
40 #include "com/sun/star/beans/XPropertySet.hpp"
41 #include "com/sun/star/util/XMacroExpander.hpp"
43 #include "officeinstallationdirectories.hxx"
45 using namespace com::sun::star;
47 using namespace comphelper;
49 //=========================================================================
50 // helpers
51 //=========================================================================
53 //=========================================================================
54 static bool makeCanonicalFileURL( rtl::OUString & rURL )
56 OSL_ENSURE( rURL.matchAsciiL( "file:", sizeof( "file:" ) - 1 , 0 ) ,
57 "File URL expected!" );
59 rtl::OUString aNormalizedURL;
60 if ( osl::FileBase::getAbsoluteFileURL( rtl::OUString(),
61 rURL,
62 aNormalizedURL )
63 == osl::DirectoryItem::E_None )
65 osl::DirectoryItem aDirItem;
66 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
67 == osl::DirectoryItem::E_None )
69 osl::FileStatus aFileStatus( FileStatusMask_FileURL );
71 if ( aDirItem.getFileStatus( aFileStatus )
72 == osl::DirectoryItem::E_None )
74 aNormalizedURL = aFileStatus.getFileURL();
76 if ( aNormalizedURL.getLength() > 0 )
78 if ( aNormalizedURL
79 .getStr()[ aNormalizedURL.getLength() - 1 ]
80 != sal_Unicode( '/' ) )
81 rURL = aNormalizedURL;
82 else
83 rURL = aNormalizedURL
84 .copy( 0, aNormalizedURL.getLength() - 1 );
86 return true;
91 return false;
94 //=========================================================================
95 //=========================================================================
97 // OfficeInstallationDirectories Implementation.
99 //=========================================================================
100 //=========================================================================
102 OfficeInstallationDirectories::OfficeInstallationDirectories(
103 const uno::Reference< uno::XComponentContext > & xCtx )
104 : m_aOfficeBrandDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(brandbaseurl)" ) ),
105 m_aOfficeBaseDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(baseinsturl)" ) ),
106 m_aUserDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(userdataurl)" ) ),
107 m_xCtx( xCtx ),
108 m_pOfficeBrandDir( 0 ),
109 m_pOfficeBaseDir( 0 ),
110 m_pUserDir( 0 )
114 //=========================================================================
115 // virtual
116 OfficeInstallationDirectories::~OfficeInstallationDirectories()
118 delete m_pOfficeBrandDir;
119 delete m_pOfficeBaseDir;
120 delete m_pUserDir;
123 //=========================================================================
124 // util::XOfficeInstallationDirectories
125 //=========================================================================
127 // virtual
128 rtl::OUString SAL_CALL
129 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
130 throw ( uno::RuntimeException )
132 initDirs();
133 return rtl::OUString( *m_pOfficeBrandDir );
136 //=========================================================================
137 // virtual
138 rtl::OUString SAL_CALL
139 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
140 throw ( uno::RuntimeException )
142 initDirs();
143 return rtl::OUString( *m_pUserDir );
147 //=========================================================================
148 // virtual
149 rtl::OUString SAL_CALL
150 OfficeInstallationDirectories::makeRelocatableURL( const rtl::OUString& URL )
151 throw ( uno::RuntimeException )
153 if ( URL.getLength() > 0 )
155 initDirs();
157 rtl::OUString aCanonicalURL( URL );
158 makeCanonicalFileURL( aCanonicalURL );
160 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeBrandDir );
161 if ( nIndex != -1 )
163 return rtl::OUString(
164 URL.replaceAt( nIndex,
165 m_pOfficeBrandDir->getLength(),
166 m_aOfficeBrandDirMacro ) );
168 else
170 nIndex = aCanonicalURL.indexOf( *m_pOfficeBaseDir );
171 if ( nIndex != -1 )
173 return rtl::OUString(
174 URL.replaceAt( nIndex,
175 m_pOfficeBaseDir->getLength(),
176 m_aOfficeBaseDirMacro ) );
178 else
180 nIndex = aCanonicalURL.indexOf( *m_pUserDir );
181 if ( nIndex != -1 )
183 return rtl::OUString(
184 URL.replaceAt( nIndex,
185 m_pUserDir->getLength(),
186 m_aUserDirMacro ) );
191 return rtl::OUString( URL );
194 //=========================================================================
195 // virtual
196 rtl::OUString SAL_CALL
197 OfficeInstallationDirectories::makeAbsoluteURL( const rtl::OUString& URL )
198 throw ( uno::RuntimeException )
200 if ( URL.getLength() > 0 )
202 sal_Int32 nIndex = URL.indexOf( m_aOfficeBrandDirMacro );
203 if ( nIndex != -1 )
205 initDirs();
207 return rtl::OUString(
208 URL.replaceAt( nIndex,
209 m_aOfficeBrandDirMacro.getLength(),
210 *m_pOfficeBrandDir ) );
212 else
214 nIndex = URL.indexOf( m_aOfficeBaseDirMacro );
215 if ( nIndex != -1 )
217 initDirs();
219 return rtl::OUString(
220 URL.replaceAt( nIndex,
221 m_aOfficeBaseDirMacro.getLength(),
222 *m_pOfficeBaseDir ) );
224 else
226 nIndex = URL.indexOf( m_aUserDirMacro );
227 if ( nIndex != -1 )
229 initDirs();
231 return rtl::OUString(
232 URL.replaceAt( nIndex,
233 m_aUserDirMacro.getLength(),
234 *m_pUserDir ) );
239 return rtl::OUString( URL );
242 //=========================================================================
243 // lang::XServiceInfo
244 //=========================================================================
246 // virtual
247 rtl::OUString SAL_CALL
248 OfficeInstallationDirectories::getImplementationName()
249 throw ( uno::RuntimeException )
251 return getImplementationName_static();
254 //=========================================================================
255 // virtual
256 sal_Bool SAL_CALL
257 OfficeInstallationDirectories::supportsService( const rtl::OUString& ServiceName )
258 throw ( uno::RuntimeException )
260 const uno::Sequence< rtl::OUString > & aNames
261 = getSupportedServiceNames();
262 const rtl::OUString * p = aNames.getConstArray();
263 for ( sal_Int32 nPos = 0; nPos < aNames.getLength(); nPos++ )
265 if ( p[ nPos ].equals( ServiceName ) )
266 return sal_True;
268 return sal_False;
272 //=========================================================================
273 // virtual
274 uno::Sequence< ::rtl::OUString > SAL_CALL
275 OfficeInstallationDirectories::getSupportedServiceNames()
276 throw ( uno::RuntimeException )
278 return getSupportedServiceNames_static();
281 //=========================================================================
282 // static
283 rtl::OUString SAL_CALL
284 OfficeInstallationDirectories::getImplementationName_static()
286 return rtl::OUString(
287 RTL_CONSTASCII_USTRINGPARAM(
288 "com.sun.star.comp.util.OfficeInstallationDirectories" ) );
291 //=========================================================================
292 // static
293 uno::Sequence< ::rtl::OUString > SAL_CALL
294 OfficeInstallationDirectories::getSupportedServiceNames_static()
296 const rtl::OUString aServiceName(
297 RTL_CONSTASCII_USTRINGPARAM(
298 "com.sun.star.util.OfficeInstallationDirectories" ) );
299 return uno::Sequence< rtl::OUString >( &aServiceName, 1 );
302 //=========================================================================
303 // static
304 rtl::OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
306 return rtl::OUString(
307 RTL_CONSTASCII_USTRINGPARAM(
308 "com.sun.star.util.theOfficeInstallationDirectories" ) );
311 //=========================================================================
312 // static
313 uno::Reference< uno::XInterface > SAL_CALL
314 OfficeInstallationDirectories::Create(
315 const uno::Reference< uno::XComponentContext > & rxContext )
317 return static_cast< cppu::OWeakObject * >(
318 new OfficeInstallationDirectories( rxContext ) );
321 //=========================================================================
322 // non-UNO
323 //=========================================================================
325 void OfficeInstallationDirectories::initDirs()
327 if ( m_pOfficeBrandDir == 0 )
329 osl::MutexGuard aGuard( m_aMutex );
330 if ( m_pOfficeBrandDir == 0 )
332 m_pOfficeBrandDir = new rtl::OUString;
333 m_pOfficeBaseDir = new rtl::OUString;
334 m_pUserDir = new rtl::OUString;
336 uno::Reference< util::XMacroExpander > xExpander;
338 m_xCtx->getValueByName(
339 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
340 "/singletons/com.sun.star.util.theMacroExpander" ) ) )
341 >>= xExpander;
343 OSL_ENSURE( xExpander.is(),
344 "Unable to obtain macro expander singleton!" );
346 if ( xExpander.is() )
348 *m_pOfficeBrandDir =
349 xExpander->expandMacros(
350 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$BRAND_BASE_DIR" ) ) );
352 OSL_ENSURE( m_pOfficeBrandDir->getLength() > 0,
353 "Unable to obtain office brand installation directory!" );
355 makeCanonicalFileURL( *m_pOfficeBrandDir );
357 *m_pOfficeBaseDir =
358 xExpander->expandMacros(
359 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
360 "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":BaseInstallation}" ) ) );
362 OSL_ENSURE( m_pOfficeBaseDir->getLength() > 0,
363 "Unable to obtain office base installation directory!" );
365 makeCanonicalFileURL( *m_pOfficeBaseDir );
367 *m_pUserDir =
368 xExpander->expandMacros(
369 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
370 "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) ) );
372 OSL_ENSURE( m_pUserDir->getLength() > 0,
373 "Unable to obtain office user data directory!" );
375 makeCanonicalFileURL( *m_pUserDir );
381 void createRegistryInfo_OfficeInstallationDirectories()
383 static ::comphelper::module::OSingletonRegistration< OfficeInstallationDirectories > aAutoRegistration;