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