1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /**************************************************************************
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 //=========================================================================
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(),
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() )
69 .getStr()[ aNormalizedURL
.getLength() - 1 ]
70 != sal_Unicode( '/' ) )
71 rURL
= aNormalizedURL
;
74 .copy( 0, aNormalizedURL
.getLength() - 1 );
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)" ),
98 m_pOfficeBrandDir( 0 ),
103 //=========================================================================
105 OfficeInstallationDirectories::~OfficeInstallationDirectories()
107 delete m_pOfficeBrandDir
;
111 //=========================================================================
112 // util::XOfficeInstallationDirectories
113 //=========================================================================
117 OfficeInstallationDirectories::getOfficeInstallationDirectoryURL()
118 throw ( uno::RuntimeException
)
121 return OUString( *m_pOfficeBrandDir
);
124 //=========================================================================
127 OfficeInstallationDirectories::getOfficeUserDataDirectoryURL()
128 throw ( uno::RuntimeException
)
131 return OUString( *m_pUserDir
);
135 //=========================================================================
138 OfficeInstallationDirectories::makeRelocatableURL( const OUString
& URL
)
139 throw ( uno::RuntimeException
)
141 if ( !URL
.isEmpty() )
145 OUString
aCanonicalURL( URL
);
146 makeCanonicalFileURL( aCanonicalURL
);
148 sal_Int32 nIndex
= aCanonicalURL
.indexOf( *m_pOfficeBrandDir
);
152 aCanonicalURL
.replaceAt( nIndex
,
153 m_pOfficeBrandDir
->getLength(),
154 m_aOfficeBrandDirMacro
) );
158 nIndex
= aCanonicalURL
.indexOf( *m_pUserDir
);
162 aCanonicalURL
.replaceAt( nIndex
,
163 m_pUserDir
->getLength(),
168 return OUString( URL
);
171 //=========================================================================
174 OfficeInstallationDirectories::makeAbsoluteURL( const OUString
& URL
)
175 throw ( uno::RuntimeException
)
177 if ( !URL
.isEmpty() )
179 sal_Int32 nIndex
= URL
.indexOf( m_aOfficeBrandDirMacro
);
185 URL
.replaceAt( nIndex
,
186 m_aOfficeBrandDirMacro
.getLength(),
187 *m_pOfficeBrandDir
) );
191 nIndex
= URL
.indexOf( m_aUserDirMacro
);
197 URL
.replaceAt( nIndex
,
198 m_aUserDirMacro
.getLength(),
203 return OUString( URL
);
206 //=========================================================================
207 // lang::XServiceInfo
208 //=========================================================================
212 OfficeInstallationDirectories::getImplementationName()
213 throw ( uno::RuntimeException
)
215 return getImplementationName_static();
218 //=========================================================================
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
) )
236 //=========================================================================
238 uno::Sequence
< OUString
> SAL_CALL
239 OfficeInstallationDirectories::getSupportedServiceNames()
240 throw ( uno::RuntimeException
)
242 return getSupportedServiceNames_static();
245 //=========================================================================
248 OfficeInstallationDirectories::getImplementationName_static()
250 return OUString("com.sun.star.comp.util.OfficeInstallationDirectories");
253 //=========================================================================
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 //=========================================================================
264 OUString SAL_CALL
OfficeInstallationDirectories::getSingletonName_static()
266 return OUString("com.sun.star.util.theOfficeInstallationDirectories");
269 //=========================================================================
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 //=========================================================================
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
);
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
);
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: */