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 <rtl/ref.hxx>
32 #include <com/sun/star/util/theMacroExpander.hpp>
33 #include <comphelper/fileurl.hxx>
34 
36 
37 using namespace com::sun::star;
38 
39 static 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  {
49  osl::DirectoryItem aDirItem;
50  if ( osl::DirectoryItem::get( aNormalizedURL, aDirItem )
51  == osl::DirectoryItem::E_None )
52  {
53  osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
54 
55  if ( aDirItem.getFileStatus( aFileStatus )
56  == osl::DirectoryItem::E_None )
57  {
58  aNormalizedURL = aFileStatus.getFileURL();
59 
60  if ( !aNormalizedURL.isEmpty() )
61  {
62  if ( !aNormalizedURL.endsWith("/") )
63  rURL = aNormalizedURL;
64  else
65  rURL = aNormalizedURL
66  .copy( 0, aNormalizedURL.getLength() - 1 );
67 
68  return true;
69  }
70  }
71  }
72  }
73  return false;
74 }
75 
76 namespace comphelper {
77 
78 static OUString const g_aOfficeBrandDirMacro("$(brandbaseurl)");
79 static OUString const g_aUserDirMacro("$(userdataurl)");
80 
81 OfficeInstallationDirectories::OfficeInstallationDirectories(
82  const uno::Reference< uno::XComponentContext > & xCtx )
83 : m_xCtx( xCtx )
84 {
85 }
86 
87 
88 // virtual
90 {
91 }
92 
93 
94 // util::XOfficeInstallationDirectories
95 
96 
97 // virtual
98 OUString SAL_CALL
100 {
101  initDirs();
102  return *m_xOfficeBrandDir;
103 }
104 
105 
106 // virtual
107 OUString SAL_CALL
109 {
110  initDirs();
111  return *m_xUserDir;
112 }
113 
114 
115 
116 // virtual
117 OUString 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(),
143  g_aUserDirMacro );
144  }
145  }
146  }
147  return URL;
148 }
149 
150 
151 // virtual
152 OUString 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
189 OUString SAL_CALL
191 {
192  return "com.sun.star.comp.util.OfficeInstallationDirectories";
193 }
194 
195 // virtual
196 sal_Bool SAL_CALL
198 {
199  return cppu::supportsService(this, ServiceName);
200 }
201 
202 // virtual
203 uno::Sequence< OUString > SAL_CALL
205 {
206  return { "com.sun.star.util.OfficeInstallationDirectories" };
207 }
208 
210 {
211  if ( !m_xOfficeBrandDir)
212  {
213  osl::MutexGuard aGuard( m_aMutex );
214  if ( !m_xOfficeBrandDir )
215  {
216  uno::Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(m_xCtx);
217 
218  m_xOfficeBrandDir = xExpander->expandMacros( "$BRAND_BASE_DIR" );
219 
220  OSL_ENSURE( !m_xOfficeBrandDir->isEmpty(),
221  "Unable to obtain office brand installation directory!" );
222 
224 
225  m_xUserDir =
226  xExpander->expandMacros(
227  "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap" ) ":UserInstallation}" );
228 
229  OSL_ENSURE( !m_xUserDir->isEmpty(),
230  "Unable to obtain office user data directory!" );
231 
233  }
234  }
235 }
236 
237 }
238 
239 namespace {
240 
241 struct Instance {
242  explicit Instance(
243  css::uno::Reference<css::uno::XComponentContext> const & context):
244  instance(static_cast<cppu::OWeakObject *>(
245  new comphelper::OfficeInstallationDirectories(context)))
246  {}
247 
249 };
250 
251 struct Singleton:
252  public rtl::StaticWithArg<
253  Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
254 {};
255 
256 }
257 
258 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
260  css::uno::XComponentContext *context,
261  css::uno::Sequence<css::uno::Any> const &)
262 {
263  return cppu::acquire(static_cast<cppu::OWeakObject *>(
264  Singleton::get(context).instance.get()));
265 }
266 
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL getOfficeInstallationDirectoryURL() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
css::uno::Reference< css::uno::XComponentContext > m_xCtx
virtual OUString SAL_CALL makeAbsoluteURL(const OUString &URL) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static OUString const g_aOfficeBrandDirMacro("$(brandbaseurl)")
virtual OUString SAL_CALL getOfficeUserDataDirectoryURL() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
static OUString const g_aUserDirMacro("$(userdataurl)")
#define SAL_CONFIGFILE(name)
unsigned char sal_Bool
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 &)
COMPHELPER_DLLPUBLIC bool isFileUrl(OUString const &url)
Definition: fileurl.cxx:25
virtual OUString SAL_CALL makeRelocatableURL(const OUString &URL) override