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