LibreOffice Module comphelper (master) 1
officeinstallationdirectories.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <config_folders.h>
21
23
24/**************************************************************************
25 TODO
26 **************************************************************************
27
28 *************************************************************************/
29
30#include <osl/file.hxx>
31#include <com/sun/star/util/theMacroExpander.hpp>
33#include <utility>
34
36
37using namespace com::sun::star;
38
39static bool makeCanonicalFileURL( OUString & rURL )
40{
41 OSL_ENSURE(comphelper::isFileUrl(rURL), "File URL expected!");
42
43 OUString aNormalizedURL;
44 if ( osl::FileBase::getAbsoluteFileURL( OUString(),
45 rURL,
46 aNormalizedURL )
47 != osl::DirectoryItem::E_None )
48 return false;
49
50 osl::DirectoryItem aDirItem;
51 if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
52 != osl::DirectoryItem::E_None )
53 return false;
54
55 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
56
57 if ( aDirItem.getFileStatus( aFileStatus )
58 == osl::DirectoryItem::E_None )
59 {
60 aNormalizedURL = aFileStatus.getFileURL();
61
62 if ( !aNormalizedURL.isEmpty() )
63 {
64 if ( !aNormalizedURL.endsWith("/") )
65 rURL = aNormalizedURL;
66 else
67 rURL = aNormalizedURL
68 .copy( 0, aNormalizedURL.getLength() - 1 );
69
70 return true;
71 }
72 }
73 return false;
74}
75
76namespace comphelper {
77
78constexpr OUStringLiteral g_aOfficeBrandDirMacro(u"$(brandbaseurl)");
79constexpr OUStringLiteral g_aUserDirMacro(u"$(userdataurl)");
80
82 uno::Reference< uno::XComponentContext > xCtx )
83: m_xCtx(std::move( xCtx ))
84{
85}
86
87
88// virtual
90{
91}
92
93
94// util::XOfficeInstallationDirectories
95
96
97// virtual
98OUString SAL_CALL
100{
101 initDirs();
102 return *m_xOfficeBrandDir;
103}
104
105
106// virtual
107OUString SAL_CALL
109{
110 initDirs();
111 return *m_xUserDir;
112}
113
114
115
116// virtual
117OUString SAL_CALL
119{
120 if ( !URL.isEmpty() )
121 {
122 initDirs();
123
124 OUString aCanonicalURL( URL );
125 makeCanonicalFileURL( aCanonicalURL );
126
127 sal_Int32 nIndex = aCanonicalURL.indexOf( *m_xOfficeBrandDir );
128 if ( nIndex != -1 )
129 {
130 return
131 aCanonicalURL.replaceAt( nIndex,
132 m_xOfficeBrandDir->getLength(),
134 }
135 else
136 {
137 nIndex = aCanonicalURL.indexOf( *m_xUserDir );
138 if ( nIndex != -1 )
139 {
140 return
141 aCanonicalURL.replaceAt( nIndex,
142 m_xUserDir->getLength(),
144 }
145 }
146 }
147 return URL;
148}
149
150
151// virtual
152OUString SAL_CALL
154{
155 if ( !URL.isEmpty() )
156 {
157 sal_Int32 nIndex = URL.indexOf( g_aOfficeBrandDirMacro );
158 if ( nIndex != -1 )
159 {
160 initDirs();
161
162 return
163 URL.replaceAt( nIndex,
164 g_aOfficeBrandDirMacro.getLength(),
166 }
167 else
168 {
169 nIndex = URL.indexOf( g_aUserDirMacro );
170 if ( nIndex != -1 )
171 {
172 initDirs();
173
174 return
175 URL.replaceAt( nIndex,
176 g_aUserDirMacro.getLength(),
177 *m_xUserDir );
178 }
179 }
180 }
181 return URL;
182}
183
184
185// lang::XServiceInfo
186
187
188// virtual
189OUString SAL_CALL
191{
192 return "com.sun.star.comp.util.OfficeInstallationDirectories";
193}
194
195// virtual
196sal_Bool SAL_CALL
198{
200}
201
202// virtual
203uno::Sequence< OUString > SAL_CALL
205{
206 return { "com.sun.star.util.OfficeInstallationDirectories" };
207}
208
210{
212 return;
213
214 std::unique_lock aGuard( m_aMutex );
215 if ( m_xOfficeBrandDir )
216 return;
217
218 uno::Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(m_xCtx);
219
220 m_xOfficeBrandDir = xExpander->expandMacros( "$BRAND_BASE_DIR" );
221
222 OSL_ENSURE( !m_xOfficeBrandDir->isEmpty(),
223 "Unable to obtain office brand installation directory!" );
224
226
227 m_xUserDir =
228 xExpander->expandMacros(
229 "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" );
230
231 OSL_ENSURE( !m_xUserDir->isEmpty(),
232 "Unable to obtain office user data directory!" );
233
235}
236
237}
238
239
240extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
242 css::uno::XComponentContext *context,
243 css::uno::Sequence<css::uno::Any> const &)
244{
245 return cppu::acquire(
247}
248
249/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xCtx
virtual OUString SAL_CALL getImplementationName() override
virtual OUString SAL_CALL makeRelocatableURL(const OUString &URL) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL makeAbsoluteURL(const OUString &URL) override
css::uno::Reference< css::uno::XComponentContext > m_xCtx
virtual OUString SAL_CALL getOfficeInstallationDirectoryURL() override
OfficeInstallationDirectories(css::uno::Reference< css::uno::XComponentContext > xCtx)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual OUString SAL_CALL getOfficeUserDataDirectoryURL() override
#define SAL_CONFIGFILE(name)
sal_Int32 nIndex
constexpr OUStringLiteral g_aUserDirMacro(u"$(userdataurl)")
COMPHELPER_DLLPUBLIC bool isFileUrl(std::u16string_view url)
Definition: fileurl.cxx:26
constexpr OUStringLiteral g_aOfficeBrandDirMacro(u"$(brandbaseurl)")
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
static bool makeCanonicalFileURL(OUString &rURL)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_util_OfficeInstallationDirectories(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool