Updated core
[LibreOffice.git] / comphelper / source / officeinstdir / officeinstallationdirectories.cxx
blob30915a7a0a287986ba75077259a272304482da2a
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 .
21 #include "comphelper_module.hxx"
23 /**************************************************************************
24 TODO
25 **************************************************************************
27 *************************************************************************/
29 #include "osl/file.hxx"
30 #include "com/sun/star/beans/XPropertySet.hpp"
31 #include "com/sun/star/util/theMacroExpander.hpp"
33 #include "officeinstallationdirectories.hxx"
35 using namespace com::sun::star;
37 using namespace comphelper;
39 //=========================================================================
40 // helpers
41 //=========================================================================
43 //=========================================================================
44 static bool makeCanonicalFileURL( OUString & rURL )
46 OSL_ENSURE( rURL.matchAsciiL( "file:", sizeof( "file:" ) - 1 , 0 ) ,
47 "File URL expected!" );
49 OUString aNormalizedURL;
50 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
51 rURL,
52 aNormalizedURL )
53 == osl::DirectoryItem::E_None )
55 osl::DirectoryItem aDirItem;
56 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
57 == osl::DirectoryItem::E_None )
59 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
61 if ( aDirItem.getFileStatus( aFileStatus )
62 == osl::DirectoryItem::E_None )
64 aNormalizedURL = aFileStatus.getFileURL();
66 if ( !aNormalizedURL.isEmpty() )
68 if ( aNormalizedURL
69 .getStr()[ aNormalizedURL.getLength() - 1 ]
70 != sal_Unicode( '/' ) )
71 rURL = aNormalizedURL;
72 else
73 rURL = aNormalizedURL
74 .copy( 0, aNormalizedURL.getLength() - 1 );
76 return true;
81 return false;
84 //=========================================================================
85 //=========================================================================
87 // OfficeInstallationDirectories Implementation.
89 //=========================================================================
90 //=========================================================================
92 OfficeInstallationDirectories::OfficeInstallationDirectories(
93 const uno::Reference< uno::XComponentContext > & xCtx )
94 : m_aOfficeBrandDirMacro( "$(brandbaseurl)" ),
95 m_aOfficeBaseDirMacro( "$(baseinsturl)" ),
96 m_aUserDirMacro( "$(userdataurl)" ),
97 m_xCtx( xCtx ),
98 m_pOfficeBrandDir( 0 ),
99 m_pUserDir( 0 )
103 //=========================================================================
104 // virtual
105 OfficeInstallationDirectories::~OfficeInstallationDirectories()
107 delete m_pOfficeBrandDir;
108 delete m_pUserDir;
111 //=========================================================================
112 // util::XOfficeInstallationDirectories
113 //=========================================================================
115 // virtual
116 OUString SAL_CALL
117 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
118 throw ( uno::RuntimeException )
120 initDirs();
121 return OUString( *m_pOfficeBrandDir );
124 //=========================================================================
125 // virtual
126 OUString SAL_CALL
127 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
128 throw ( uno::RuntimeException )
130 initDirs();
131 return OUString( *m_pUserDir );
135 //=========================================================================
136 // virtual
137 OUString SAL_CALL
138 OfficeInstallationDirectories::makeRelocatableURL( const OUString& URL )
139 throw ( uno::RuntimeException )
141 if ( !URL.isEmpty() )
143 initDirs();
145 OUString aCanonicalURL( URL );
146 makeCanonicalFileURL( aCanonicalURL );
148 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_pOfficeBrandDir );
149 if ( nIndex != -1 )
151 return OUString(
152 aCanonicalURL.replaceAt( nIndex,
153 m_pOfficeBrandDir->getLength(),
154 m_aOfficeBrandDirMacro ) );
156 else
158 nIndex = aCanonicalURL.indexOf( *m_pUserDir );
159 if ( nIndex != -1 )
161 return OUString(
162 aCanonicalURL.replaceAt( nIndex,
163 m_pUserDir->getLength(),
164 m_aUserDirMacro ) );
168 return OUString( URL );
171 //=========================================================================
172 // virtual
173 OUString SAL_CALL
174 OfficeInstallationDirectories::makeAbsoluteURL( const OUString& URL )
175 throw ( uno::RuntimeException )
177 if ( !URL.isEmpty() )
179 sal_Int32 nIndex = URL.indexOf( m_aOfficeBrandDirMacro );
180 if ( nIndex != -1 )
182 initDirs();
184 return OUString(
185 URL.replaceAt( nIndex,
186 m_aOfficeBrandDirMacro.getLength(),
187 *m_pOfficeBrandDir ) );
189 else
191 nIndex = URL.indexOf( m_aUserDirMacro );
192 if ( nIndex != -1 )
194 initDirs();
196 return OUString(
197 URL.replaceAt( nIndex,
198 m_aUserDirMacro.getLength(),
199 *m_pUserDir ) );
203 return OUString( URL );
206 //=========================================================================
207 // lang::XServiceInfo
208 //=========================================================================
210 // virtual
211 OUString SAL_CALL
212 OfficeInstallationDirectories::getImplementationName()
213 throw ( uno::RuntimeException )
215 return getImplementationName_static();
218 //=========================================================================
219 // virtual
220 sal_Bool SAL_CALL
221 OfficeInstallationDirectories::supportsService( const OUString& ServiceName )
222 throw ( uno::RuntimeException )
224 const uno::Sequence< OUString > & aNames
225 = getSupportedServiceNames();
226 const OUString * p = aNames.getConstArray();
227 for ( sal_Int32 nPos = 0; nPos < aNames.getLength(); nPos++ )
229 if ( p[ nPos ].equals( ServiceName ) )
230 return sal_True;
232 return sal_False;
236 //=========================================================================
237 // virtual
238 uno::Sequence< OUString > SAL_CALL
239 OfficeInstallationDirectories::getSupportedServiceNames()
240 throw ( uno::RuntimeException )
242 return getSupportedServiceNames_static();
245 //=========================================================================
246 // static
247 OUString SAL_CALL
248 OfficeInstallationDirectories::getImplementationName_static()
250 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
253 //=========================================================================
254 // static
255 uno::Sequence< OUString > SAL_CALL
256 OfficeInstallationDirectories::getSupportedServiceNames_static()
258 const OUString aServiceName("com.sun.star.util.OfficeInstallationDirectories");
259 return uno::Sequence< OUString >( &aServiceName, 1 );
262 //=========================================================================
263 // static
264 OUString SAL_CALL OfficeInstallationDirectories::getSingletonName_static()
266 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
269 //=========================================================================
270 // static
271 uno::Reference< uno::XInterface > SAL_CALL
272 OfficeInstallationDirectories::Create(
273 const uno::Reference< uno::XComponentContext > & rxContext )
275 return static_cast< cppu::OWeakObject * >(
276 new OfficeInstallationDirectories( rxContext ) );
279 //=========================================================================
280 // non-UNO
281 //=========================================================================
283 void OfficeInstallationDirectories::initDirs()
285 if ( m_pOfficeBrandDir == 0 )
287 osl::MutexGuard aGuard( m_aMutex );
288 if ( m_pOfficeBrandDir == 0 )
290 m_pOfficeBrandDir = new OUString;
291 m_pUserDir = new OUString;
293 uno::Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(m_xCtx);
295 *m_pOfficeBrandDir =
296 xExpander->expandMacros(
297 OUString( "$BRAND_BASE_DIR" ) );
299 OSL_ENSURE( !m_pOfficeBrandDir->isEmpty(),
300 "Unable to obtain office brand installation directory!" );
302 makeCanonicalFileURL( *m_pOfficeBrandDir );
304 *m_pUserDir =
305 xExpander->expandMacros(
306 OUString("${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" ) );
308 OSL_ENSURE( !m_pUserDir->isEmpty(),
309 "Unable to obtain office user data directory!" );
311 makeCanonicalFileURL( *m_pUserDir );
316 void createRegistryInfo_OfficeInstallationDirectories()
318 static ::comphelper::module::OSingletonRegistration< OfficeInstallationDirectories > aAutoRegistration;
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */