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>
33#include <officecfg/Setup.hxx>
34#include <osl/file.hxx>
35#include <rtl/bootstrap.hxx>
36#include <sal/log.hxx>
38
41#include <unotools/tempfile.hxx>
42#include <vcl/svapp.hxx>
44#include <map>
45
46using namespace ::com::sun::star::uno;
47using namespace ::com::sun::star::lang;
48using namespace ::com::sun::star::beans;
49using namespace ::com::sun::star::registry;
50using namespace ::com::sun::star::ucb;
51
52namespace desktop
53{
54
55
56static void configureUcb()
57{
58 // For backwards compatibility, in case some code still uses plain
59 // createInstance w/o args directly to obtain an instance:
61}
62
64{
65 Reference<XMultiServiceFactory> sm;
66#ifdef ANDROID
67 OUString aUnoRc( "file:///assets/program/unorc" );
68 sm.set(
69 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
70 UNO_QUERY_THROW);
71#elif defined(IOS)
72 OUString uri( "$APP_DATA_DIR" );
73 rtl_bootstrap_expandMacros( &uri.pData );
74 OUString aUnoRc("file://" + uri + "/unorc");
75 sm.set(
76 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
77 UNO_QUERY_THROW);
78#else
79 sm.set(
81 UNO_QUERY_THROW);
82#endif
84}
85
87{
89 return;
90
91 // interpret command line arguments
93
94 // Headless mode for FAT Office, auto cancels any dialogs that popup
95 if (rCmdLine.IsHeadless())
97
98 // read accept string from configuration
99 OUString conDcpCfg(
100 officecfg::Setup::Office::ooSetupConnectionURL::get());
101 if (!conDcpCfg.isEmpty()) {
102 createAcceptor(conDcpCfg);
103 }
104
105 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
106 for (auto const& elem : conDcp)
107 {
108 createAcceptor(elem);
109 }
110
111 configureUcb();
112
115}
116
117typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
118
119namespace
120{
121 AcceptorMap& acceptorMap()
122 {
123 static AcceptorMap SINGLETON;
124 return SINGLETON;
125 }
126 OUString& CurrentTempURL()
127 {
128 static OUString SINGLETON;
129 return SINGLETON;
130 }
131}
132
133static bool bAccept = false;
134
135void Desktop::createAcceptor(const OUString& aAcceptString)
136{
137 // check whether the requested acceptor already exists
138 AcceptorMap &rMap = acceptorMap();
139 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
140 if (pIter != rMap.end() )
141 return;
142
143 Sequence< Any > aSeq{ Any(aAcceptString), Any(bAccept) };
144 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
145 Reference<XInitialization> rAcceptor(
146 xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
147 UNO_QUERY );
148 if ( rAcceptor.is() )
149 {
150 try
151 {
152 rAcceptor->initialize( aSeq );
153 rMap.emplace(aAcceptString, rAcceptor);
154 }
155 catch (const css::uno::Exception&)
156 {
157 // no error handling needed...
158 // acceptor just won't come up
159 TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
160 }
161 }
162 else
163 {
164 // there is already an acceptor with this description
165 SAL_WARN( "desktop.app", "Acceptor already exists.");
166 }
167}
168
169namespace {
170
171class enable
172{
173 private:
174 Sequence<Any> m_aSeq{ Any(true) };
175 public:
176 enable() = default;
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
187IMPL_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();
196 std::for_each(rMap.begin(), rMap.end(), enable());
197 }
198}
199
200void Desktop::destroyAcceptor(const OUString& aAcceptString)
201{
202 // special case stop all acceptors
203 AcceptorMap &rMap = acceptorMap();
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().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::SetTempNameBaseDirectory( aTempBaseURL );
246 if ( aTempPath.isEmpty()
247 && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
248 {
249 aTempPath = ::utl::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() = aRet;
260}
261
263{
264 // remove current temporary directory
265 OUString &rCurrentTempURL = CurrentTempURL();
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: */
Sequence< Any > m_aSeq
Definition: appinit.cxx:174
static void EnableHeadlessMode(bool dialogsAreFatal)
const OUString & GetTempPath() const
std::vector< OUString > const & GetAccept() const
static void RemoveTemporaryDirectory()
Definition: appinit.cxx:262
static void DeregisterServices()
Definition: appinit.cxx:221
static void destroyAcceptor(const OUString &aDescription)
Definition: appinit.cxx:200
static void InitApplicationServiceManager()
Definition: appinit.cxx:63
static void createAcceptor(const OUString &aDescription)
Definition: appinit.cxx:135
bool m_bServicesRegistered
Definition: app.hxx:163
void RegisterServices()
Definition: appinit.cxx:86
static CommandLineArgs & GetCommandLineArgs()
Definition: app.cxx:393
static void CreateTemporaryDirectory()
Definition: appinit.cxx:227
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString DpResId(TranslateId aId)
Definition: dp_misc.cxx:555
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:2642
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
Reference< XComponentContext > getProcessComponentContext()
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
Definition: app.cxx:167
std::map< OUString, css::uno::Reference< css::lang::XInitialization > > AcceptorMap
Definition: appinit.cxx:117
IMPL_STATIC_LINK_NOARG(Desktop, AsyncInitFirstRun, Timer *, void)
Definition: app.cxx:2472
static void configureUcb()
Definition: appinit.cxx:56
static bool bAccept
Definition: appinit.cxx:133
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)