nss: upgrade to release 3.73
[LibreOffice.git] / desktop / source / app / appinit.cxx
blobcff0a684cfcf056113757d0a15c91028bbf21975
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <algorithm>
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>
39 #include <rtl/instance.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <unotools/ucbhelper.hxx>
42 #include <unotools/tempfile.hxx>
43 #include <vcl/svapp.hxx>
44 #include <unotools/pathoptions.hxx>
45 #include <map>
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;
54 namespace desktop
58 static void configureUcb()
60 // For backwards compatibility, in case some code still uses plain
61 // createInstance w/o args directly to obtain an instance:
62 UniversalContentBroker::create(comphelper::getProcessComponentContext());
65 void Desktop::InitApplicationServiceManager()
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(
82 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
83 UNO_QUERY_THROW);
84 #endif
85 comphelper::setProcessServiceFactory(sm);
88 void Desktop::RegisterServices(Reference< XComponentContext > const & context)
90 if( m_bServicesRegistered )
91 return;
93 // interpret command line arguments
94 CommandLineArgs& rCmdLine = GetCommandLineArgs();
96 // Headless mode for FAT Office, auto cancels any dialogs that popup
97 if (rCmdLine.IsEventTesting())
98 Application::EnableEventTestingMode();
99 else if (rCmdLine.IsHeadless())
100 Application::EnableHeadlessMode(false);
102 // read accept string from configuration
103 OUString conDcpCfg(
104 officecfg::Setup::Office::ooSetupConnectionURL::get(context));
105 if (!conDcpCfg.isEmpty()) {
106 createAcceptor(conDcpCfg);
109 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
110 for (auto const& elem : conDcp)
112 createAcceptor(elem);
115 configureUcb();
117 CreateTemporaryDirectory();
118 m_bServicesRegistered = true;
121 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
123 namespace
125 struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
126 struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
129 static bool bAccept = false;
131 void Desktop::createAcceptor(const OUString& aAcceptString)
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;
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() )
150 rAcceptor->initialize( aSeq );
151 rMap.emplace(aAcceptString, rAcceptor);
153 catch (const css::uno::Exception&)
155 // no error handling needed...
156 // acceptor just won't come up
157 TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
160 else
162 // there is already an acceptor with this description
163 SAL_WARN( "desktop.app", "Acceptor already exists.");
167 namespace {
169 class enable
171 private:
172 Sequence<Any> m_aSeq;
173 public:
174 enable() : m_aSeq(1) {
175 m_aSeq[0] <<= true;
177 void operator() (const AcceptorMap::value_type& val) {
178 if (val.second.is()) {
179 val.second->initialize(m_aSeq);
186 // enable acceptors
187 IMPL_STATIC_LINK_NOARG(Desktop, EnableAcceptors_Impl, void*, void)
189 if (!bAccept)
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());
200 void Desktop::destroyAcceptor(const OUString& aAcceptString)
202 // special case stop all acceptors
203 AcceptorMap &rMap = acceptorMap::get();
204 if (aAcceptString == "all") {
205 rMap.clear();
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");
221 void Desktop::DeregisterServices()
223 // stop all acceptors by clearing the map
224 acceptorMap::get().clear();
227 void Desktop::CreateTemporaryDirectory()
229 OUString aTempBaseURL;
232 SvtPathOptions aOpt;
233 aTempBaseURL = aOpt.GetTempPath();
235 catch (RuntimeException& e)
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;
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 )
249 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
252 // set new current temporary directory
253 OUString aRet;
254 if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
255 != osl::FileBase::E_None)
257 aRet.clear();
259 CurrentTempURL::get() = aRet;
262 void Desktop::RemoveTemporaryDirectory()
264 // remove current temporary directory
265 OUString &rCurrentTempURL = CurrentTempURL::get();
266 if ( !rCurrentTempURL.isEmpty() )
268 ::utl::UCBContentHelper::Kill( rCurrentTempURL );
272 } // namespace desktop
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */