Update ooo320-m1
[ooovba.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blobac5c1c84f38a3048cffc52379bcc51e6decf7cb9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: officeinstallationdirectories.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper_module.hxx"
36 /**************************************************************************
37 TODO
38 **************************************************************************
40 *************************************************************************/
42 #include "osl/file.hxx"
43 #include "com/sun/star/beans/XPropertySet.hpp"
44 #include "com/sun/star/util/XMacroExpander.hpp"
46 #include "officeinstallationdirectories.hxx"
48 using namespace com::sun::star;
50 using namespace comphelper;
52 //=========================================================================
53 // helpers
54 //=========================================================================
56 //=========================================================================
57 static bool makeCanonicalFileURL( rtl::OUString & rURL )
59 OSL_ENSURE( rURL.matchAsciiL( "file:", sizeof( "file:" ) - 1 , 0 ) ,
60 "File URL expected!" );
62 rtl::OUString aNormalizedURL;
63 if ( osl::FileBase::getAbsoluteFileURL( rtl::OUString(),
64 rURL,
65 aNormalizedURL )
66 == osl::DirectoryItem::E_None )
68 osl::DirectoryItem aDirItem;
69 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
70 == osl::DirectoryItem::E_None )
72 osl::FileStatus aFileStatus( FileStatusMask_FileURL );
74 if ( aDirItem.getFileStatus( aFileStatus )
75 == osl::DirectoryItem::E_None )
77 aNormalizedURL = aFileStatus.getFileURL();
79 if ( aNormalizedURL.getLength() > 0 )
81 if ( aNormalizedURL
82 .getStr()[ aNormalizedURL.getLength() - 1 ]
83 != sal_Unicode( '/' ) )
84 rURL = aNormalizedURL;
85 else
86 rURL = aNormalizedURL
87 .copy( 0, aNormalizedURL.getLength() - 1 );
89 return true;
94 return false;
97 //=========================================================================
98 //=========================================================================
100 // OfficeInstallationDirectories Implementation.
102 //=========================================================================
103 //=========================================================================
105 OfficeInstallationDirectories::OfficeInstallationDirectories(
106 const uno::Reference< uno::XComponentContext > & xCtx )
107 : m_aOfficeDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(baseinsturl)" ) ),
108 m_aUserDirMacro( RTL_CONSTASCII_USTRINGPARAM( "$(userdataurl)" ) ),
109 m_xCtx( xCtx ),
110 m_pOfficeDir( 0 ),
111 m_pUserDir( 0 )
115 //=========================================================================
116 // virtual
117 OfficeInstallationDirectories::~OfficeInstallationDirectories()
121 //=========================================================================
122 // util::XOfficeInstallationDirectories
123 //=========================================================================
125 // virtual
126 rtl::OUString SAL_CALL
127 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
128 throw ( uno::RuntimeException )
130 // late init m_pOfficeDir and m_pUserDir
131 initDirs();
132 return rtl::OUString( *m_pOfficeDir );
135 //=========================================================================
136 // virtual
137 rtl::OUString SAL_CALL
138 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
139 throw ( uno::RuntimeException )
141 // late init m_pOfficeDir and m_pUserDir
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 // late init m_pOfficeDir and m_pUserDir
156 initDirs();
158 rtl::OUString aCanonicalURL( URL );
159 makeCanonicalFileURL( aCanonicalURL );
161 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeDir );
162 if ( nIndex != -1 )
164 return rtl::OUString(
165 aCanonicalURL.replaceAt( nIndex,
166 m_pOfficeDir->getLength(),
167 m_aOfficeDirMacro ) );
169 else
171 nIndex = aCanonicalURL.indexOf( *m_pUserDir );
172 if ( nIndex != -1 )
174 return rtl::OUString(
175 aCanonicalURL.replaceAt( nIndex,
176 m_pUserDir->getLength(),
177 m_aUserDirMacro ) );
181 return rtl::OUString( URL );
184 //=========================================================================
185 // virtual
186 rtl::OUString SAL_CALL
187 OfficeInstallationDirectories::makeAbsoluteURL( const rtl::OUString& URL )
188 throw ( uno::RuntimeException )
190 if ( URL.getLength() > 0 )
192 sal_Int32 nIndex = URL.indexOf( m_aOfficeDirMacro );
193 if ( nIndex != -1 )
195 // late init m_pOfficeDir and m_pUserDir
196 initDirs();
198 return rtl::OUString(
199 URL.replaceAt( nIndex,
200 m_aOfficeDirMacro.getLength(),
201 *m_pOfficeDir ) );
203 else
205 nIndex = URL.indexOf( m_aUserDirMacro );
206 if ( nIndex != -1 )
208 // late init m_pOfficeDir and m_pUserDir
209 initDirs();
211 return rtl::OUString(
212 URL.replaceAt( nIndex,
213 m_aUserDirMacro.getLength(),
214 *m_pUserDir ) );
218 return rtl::OUString( URL );
221 //=========================================================================
222 // lang::XServiceInfo
223 //=========================================================================
225 // virtual
226 rtl::OUString SAL_CALL
227 OfficeInstallationDirectories::getImplementationName()
228 throw ( uno::RuntimeException )
230 return getImplementationName_static();
233 //=========================================================================
234 // virtual
235 sal_Bool SAL_CALL
236 OfficeInstallationDirectories::supportsService( const rtl::OUString& ServiceName )
237 throw ( uno::RuntimeException )
239 const uno::Sequence< rtl::OUString > & aNames
240 = getSupportedServiceNames();
241 const rtl::OUString * p = aNames.getConstArray();
242 for ( sal_Int32 nPos = 0; nPos < aNames.getLength(); nPos++ )
244 if ( p[ nPos ].equals( ServiceName ) )
245 return sal_True;
247 return sal_False;
251 //=========================================================================
252 // virtual
253 uno::Sequence< ::rtl::OUString > SAL_CALL
254 OfficeInstallationDirectories::getSupportedServiceNames()
255 throw ( uno::RuntimeException )
257 return getSupportedServiceNames_static();
260 //=========================================================================
261 // static
262 rtl::OUString SAL_CALL
263 OfficeInstallationDirectories::getImplementationName_static()
265 return rtl::OUString(
266 RTL_CONSTASCII_USTRINGPARAM(
267 "com.sun.star.comp.util.OfficeInstallationDirectories" ) );
270 //=========================================================================
271 // static
272 uno::Sequence< ::rtl::OUString > SAL_CALL
273 OfficeInstallationDirectories::getSupportedServiceNames_static()
275 const rtl::OUString aServiceName(
276 RTL_CONSTASCII_USTRINGPARAM(
277 "com.sun.star.util.OfficeInstallationDirectories" ) );
278 return uno::Sequence< rtl::OUString >( &aServiceName, 1 );
281 //=========================================================================
282 // static
283 rtl::OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
285 return rtl::OUString(
286 RTL_CONSTASCII_USTRINGPARAM(
287 "com.sun.star.util.theOfficeInstallationDirectories" ) );
290 //=========================================================================
291 // static
292 uno::Reference< uno::XInterface > SAL_CALL
293 OfficeInstallationDirectories::Create(
294 const uno::Reference< uno::XComponentContext > & rxContext )
296 return static_cast< cppu::OWeakObject * >(
297 new OfficeInstallationDirectories( rxContext ) );
300 //=========================================================================
301 // non-UNO
302 //=========================================================================
304 void OfficeInstallationDirectories::initDirs()
306 if ( m_pOfficeDir == 0 )
308 osl::MutexGuard aGuard( m_aMutex );
309 if ( m_pOfficeDir == 0 )
311 m_pOfficeDir = new rtl::OUString;
312 m_pUserDir = new rtl::OUString;
314 uno::Reference< util::XMacroExpander > xExpander;
316 m_xCtx->getValueByName(
317 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
318 "/singletons/com.sun.star.util.theMacroExpander" ) ) )
319 >>= xExpander;
321 OSL_ENSURE( xExpander.is(),
322 "Unable to obtain macro expander singleton!" );
324 if ( xExpander.is() )
326 *m_pOfficeDir =
327 xExpander->expandMacros(
328 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
329 "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":BaseInstallation}" ) ) );
331 OSL_ENSURE( m_pOfficeDir->getLength() > 0,
332 "Unable to obtain office installation directory!" );
334 makeCanonicalFileURL( *m_pOfficeDir );
336 *m_pUserDir =
337 xExpander->expandMacros(
338 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
339 "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) ) );
341 OSL_ENSURE( m_pUserDir->getLength() > 0,
342 "Unable to obtain office user data directory!" );
344 makeCanonicalFileURL( *m_pUserDir );
350 void createRegistryInfo_OfficeInstallationDirectories()
352 static ::comphelper::module::OSingletonRegistration< OfficeInstallationDirectories > aAutoRegistration;