LibreOffice Module desktop (master)  1
appinit.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 
21 #include <algorithm>
22 
23 #include <app.hxx>
24 #include <dp_shared.hxx>
25 #include "cmdlineargs.hxx"
26 #include <strings.hrc>
27 #include <com/sun/star/registry/XSimpleRegistry.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/uno/Exception.hpp>
31 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
32 #include <cppuhelper/bootstrap.hxx>
33 #include <officecfg/Setup.hxx>
34 #include <osl/file.hxx>
35 #include <rtl/bootstrap.hxx>
36 #include <sal/log.hxx>
37 #include <tools/diagnose_ex.h>
38 
40 #include <unotools/ucbhelper.hxx>
41 #include <unotools/tempfile.hxx>
42 #include <vcl/svapp.hxx>
43 #include <unotools/pathoptions.hxx>
44 #include <map>
45 
46 using namespace desktop;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::registry;
51 using namespace ::com::sun::star::ucb;
52 
53 namespace desktop
54 {
55 
56 
57 static void configureUcb()
58 {
59  // For backwards compatibility, in case some code still uses plain
60  // createInstance w/o args directly to obtain an instance:
62 }
63 
65 {
66  Reference<XMultiServiceFactory> sm;
67 #ifdef ANDROID
68  OUString aUnoRc( "file:///assets/program/unorc" );
69  sm.set(
70  cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
71  UNO_QUERY_THROW);
72 #elif defined(IOS)
73  OUString uri( "$APP_DATA_DIR" );
74  rtl_bootstrap_expandMacros( &uri.pData );
75  OUString aUnoRc("file://" + uri + "/unorc");
76  sm.set(
77  cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
78  UNO_QUERY_THROW);
79 #else
80  sm.set(
82  UNO_QUERY_THROW);
83 #endif
85 }
86 
88 {
90  return;
91 
92  // interpret command line arguments
93  CommandLineArgs& rCmdLine = GetCommandLineArgs();
94 
95  // Headless mode for FAT Office, auto cancels any dialogs that popup
96  if (rCmdLine.IsEventTesting())
98  else if (rCmdLine.IsHeadless())
100 
101  // read accept string from configuration
102  OUString conDcpCfg(
103  officecfg::Setup::Office::ooSetupConnectionURL::get());
104  if (!conDcpCfg.isEmpty()) {
105  createAcceptor(conDcpCfg);
106  }
107 
108  std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
109  for (auto const& elem : conDcp)
110  {
111  createAcceptor(elem);
112  }
113 
114  configureUcb();
115 
117  m_bServicesRegistered = true;
118 }
119 
120 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
121 
122 namespace
123 {
124  AcceptorMap& acceptorMap()
125  {
126  static AcceptorMap SINGLETON;
127  return SINGLETON;
128  }
129  OUString& CurrentTempURL()
130  {
131  static OUString SINGLETON;
132  return SINGLETON;
133  }
134 }
135 
136 static bool bAccept = false;
137 
138 void Desktop::createAcceptor(const OUString& aAcceptString)
139 {
140  // check whether the requested acceptor already exists
141  AcceptorMap &rMap = acceptorMap();
142  AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
143  if (pIter != rMap.end() )
144  return;
145 
146  Sequence< Any > aSeq{ Any(aAcceptString), Any(bAccept) };
147  Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
148  Reference<XInitialization> rAcceptor(
149  xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
150  UNO_QUERY );
151  if ( rAcceptor.is() )
152  {
153  try
154  {
155  rAcceptor->initialize( aSeq );
156  rMap.emplace(aAcceptString, rAcceptor);
157  }
158  catch (const css::uno::Exception&)
159  {
160  // no error handling needed...
161  // acceptor just won't come up
162  TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
163  }
164  }
165  else
166  {
167  // there is already an acceptor with this description
168  SAL_WARN( "desktop.app", "Acceptor already exists.");
169  }
170 }
171 
172 namespace {
173 
174 class enable
175 {
176  private:
177  Sequence<Any> m_aSeq{ Any(true) };
178  public:
179  enable() = default;
180  void operator() (const AcceptorMap::value_type& val) {
181  if (val.second.is()) {
182  val.second->initialize(m_aSeq);
183  }
184  }
185 };
186 
187 }
188 
189 // enable acceptors
190 IMPL_STATIC_LINK_NOARG(Desktop, EnableAcceptors_Impl, void*, void)
191 {
192  if (!bAccept)
193  {
194  // from now on, all new acceptors are enabled
195  bAccept = true;
196  // enable existing acceptors by calling initialize(true)
197  // on all existing acceptors
198  AcceptorMap &rMap = acceptorMap();
199  std::for_each(rMap.begin(), rMap.end(), enable());
200  }
201 }
202 
203 void Desktop::destroyAcceptor(const OUString& aAcceptString)
204 {
205  // special case stop all acceptors
206  AcceptorMap &rMap = acceptorMap();
207  if (aAcceptString == "all") {
208  rMap.clear();
209 
210  } else {
211  // try to remove acceptor from map
212  AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
213  if (pIter != rMap.end() ) {
214  // remove reference from map
215  // this is the last reference and the acceptor will be destructed
216  rMap.erase(aAcceptString);
217  } else {
218  SAL_WARN( "desktop.app", "Found no acceptor to remove");
219  }
220  }
221 }
222 
223 
225 {
226  // stop all acceptors by clearing the map
227  acceptorMap().clear();
228 }
229 
231 {
232  OUString aTempBaseURL;
233  try
234  {
235  SvtPathOptions aOpt;
236  aTempBaseURL = aOpt.GetTempPath();
237  }
238  catch (RuntimeException& e)
239  {
240  // Catch runtime exception here: We have to add language dependent info
241  // to the exception message. Fallback solution uses hard coded string.
242  OUString aMsg = DpResId(STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE);
243  e.Message = aMsg + e.Message;
244  throw;
245  }
246 
247  // create new current temporary directory
248  OUString aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
249  if ( aTempPath.isEmpty()
250  && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
251  {
252  aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
253  }
254 
255  // set new current temporary directory
256  OUString aRet;
257  if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
258  != osl::FileBase::E_None)
259  {
260  aRet.clear();
261  }
262  CurrentTempURL() = aRet;
263 }
264 
266 {
267  // remove current temporary directory
268  OUString &rCurrentTempURL = CurrentTempURL();
269  if ( !rCurrentTempURL.isEmpty() )
270  {
271  ::utl::UCBContentHelper::Kill( rCurrentTempURL );
272  }
273 }
274 
275 } // namespace desktop
276 
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::map< OUString, css::uno::Reference< css::lang::XInitialization > > AcceptorMap
Definition: appinit.cxx:120
static void destroyAcceptor(const OUString &aDescription)
Definition: appinit.cxx:203
Sequence< Any > m_aSeq
Definition: appinit.cxx:177
const OUString & GetTempPath() const
static void CreateTemporaryDirectory()
Definition: appinit.cxx:230
void RegisterServices()
Definition: appinit.cxx:87
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
Definition: app.cxx:164
bool IsHeadless() const
Definition: cmdlineargs.hxx:67
static void InitApplicationServiceManager()
Definition: appinit.cxx:64
static void DeregisterServices()
Definition: appinit.cxx:224
#define TOOLS_WARN_EXCEPTION(area, stream)
static void RemoveTemporaryDirectory()
Definition: appinit.cxx:265
static void EnableEventTestingMode()
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
static CommandLineArgs & GetCommandLineArgs()
Definition: app.cxx:389
bool IsEventTesting() const
Definition: cmdlineargs.hxx:68
static void createAcceptor(const OUString &aDescription)
Definition: appinit.cxx:138
IMPL_STATIC_LINK_NOARG(Desktop, AsyncInitFirstRun, Timer *, void)
Definition: app.cxx:2489
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:2429
static OUString SetTempNameBaseDirectory(const OUString &rBaseName)
bool m_bServicesRegistered
Definition: app.hxx:163
std::vector< OUString > const & GetAccept() const
Reference< XComponentContext > getProcessComponentContext()
Sequence< sal_Int8 > aSeq
static void EnableHeadlessMode(bool dialogsAreFatal)
#define SAL_WARN(area, stream)
static void configureUcb()
Definition: appinit.cxx:57
OUString DpResId(TranslateId aId)
Definition: dp_misc.cxx:554
static bool bAccept
Definition: appinit.cxx:136
exports com.sun.star. uri