Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / desktop / source / app / appinit.cxx
blob642733efceb208c0ba4924601c42fbe73f783c55
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 <config_vclplug.h>
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 #include <tools/diagnose_ex.h>
49 #include <rtl/instance.hxx>
50 #include <comphelper/processfactory.hxx>
51 #include <unotools/ucbhelper.hxx>
52 #include <unotools/tempfile.hxx>
53 #include <vcl/svapp.hxx>
54 #include <unotools/pathoptions.hxx>
55 #include <sfx2/safemode.hxx>
56 #include <map>
58 using namespace desktop;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::registry;
63 using namespace ::com::sun::star::ucb;
65 namespace desktop
69 static void configureUcb()
71 // For backwards compatibility, in case some code still uses plain
72 // createInstance w/o args directly to obtain an instance:
73 UniversalContentBroker::create(comphelper::getProcessComponentContext());
76 void Desktop::InitApplicationServiceManager()
78 Reference<XMultiServiceFactory> sm;
79 #ifdef ANDROID
80 OUString aUnoRc( "file:///assets/program/unorc" );
81 sm.set(
82 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
83 UNO_QUERY_THROW);
84 #elif defined(IOS)
85 OUString uri( "$APP_DATA_DIR" );
86 rtl_bootstrap_expandMacros( &uri.pData );
87 OUString aUnoRc("file://" + uri + "/unorc");
88 sm.set(
89 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
90 UNO_QUERY_THROW);
91 #else
92 sm.set(
93 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
94 UNO_QUERY_THROW);
95 #endif
96 comphelper::setProcessServiceFactory(sm);
99 void Desktop::RegisterServices(Reference< XComponentContext > const & context)
101 if( !m_bServicesRegistered )
103 // interpret command line arguments
104 CommandLineArgs& rCmdLine = GetCommandLineArgs();
106 // Headless mode for FAT Office, auto cancels any dialogs that popup
107 if (rCmdLine.IsEventTesting())
108 Application::EnableEventTestingMode();
109 else if (rCmdLine.IsHeadless())
110 Application::EnableHeadlessMode(false);
112 // read accept string from configuration
113 OUString conDcpCfg(
114 officecfg::Setup::Office::ooSetupConnectionURL::get(context));
115 if (!conDcpCfg.isEmpty()) {
116 createAcceptor(conDcpCfg);
119 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
120 for (auto const& elem : conDcp)
122 createAcceptor(elem);
125 configureUcb();
127 CreateTemporaryDirectory();
128 m_bServicesRegistered = true;
132 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
134 namespace
136 struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
137 struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
140 static bool bAccept = false;
142 void Desktop::createAcceptor(const OUString& aAcceptString)
144 // check whether the requested acceptor already exists
145 AcceptorMap &rMap = acceptorMap::get();
146 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
147 if (pIter == rMap.end() )
149 Sequence< Any > aSeq( 2 );
150 aSeq[0] <<= aAcceptString;
151 aSeq[1] <<= bAccept;
152 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
153 Reference<XInitialization> rAcceptor(
154 xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
155 UNO_QUERY );
156 if ( rAcceptor.is() )
160 rAcceptor->initialize( aSeq );
161 rMap.emplace(aAcceptString, rAcceptor);
163 catch (const css::uno::Exception&)
165 // no error handling needed...
166 // acceptor just won't come up
167 TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
170 else
172 // there is already an acceptor with this description
173 SAL_WARN( "desktop.app", "Acceptor already exists.");
178 class enable
180 private:
181 Sequence<Any> m_aSeq;
182 public:
183 enable() : m_aSeq(1) {
184 m_aSeq[0] <<= true;
186 void operator() (const AcceptorMap::value_type& val) {
187 if (val.second.is()) {
188 val.second->initialize(m_aSeq);
193 // enable acceptors
194 IMPL_STATIC_LINK_NOARG(Desktop, EnableAcceptors_Impl, void*, void)
196 if (!bAccept)
198 // from now on, all new acceptors are enabled
199 bAccept = true;
200 // enable existing acceptors by calling initialize(true)
201 // on all existing acceptors
202 AcceptorMap &rMap = acceptorMap::get();
203 std::for_each(rMap.begin(), rMap.end(), enable());
207 void Desktop::destroyAcceptor(const OUString& aAcceptString)
209 // special case stop all acceptors
210 AcceptorMap &rMap = acceptorMap::get();
211 if (aAcceptString == "all") {
212 rMap.clear();
214 } else {
215 // try to remove acceptor from map
216 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
217 if (pIter != rMap.end() ) {
218 // remove reference from map
219 // this is the last reference and the acceptor will be destructed
220 rMap.erase(aAcceptString);
221 } else {
222 SAL_WARN( "desktop.app", "Found no acceptor to remove");
228 void Desktop::DeregisterServices()
230 // stop all acceptors by clearing the map
231 acceptorMap::get().clear();
234 void Desktop::CreateTemporaryDirectory()
236 OUString aTempBaseURL;
239 SvtPathOptions aOpt;
240 aTempBaseURL = aOpt.GetTempPath();
242 catch (RuntimeException& e)
244 // Catch runtime exception here: We have to add language dependent info
245 // to the exception message. Fallback solution uses hard coded string.
246 OUString aMsg = DpResId(STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE);
247 e.Message = aMsg + e.Message;
248 throw;
251 // create new current temporary directory
252 OUString aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
253 if ( aTempPath.isEmpty()
254 && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
256 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
259 // set new current temporary directory
260 OUString aRet;
261 if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
262 != osl::FileBase::E_None)
264 aRet.clear();
266 CurrentTempURL::get() = aRet;
269 void Desktop::RemoveTemporaryDirectory()
271 // remove current temporary directory
272 OUString &rCurrentTempURL = CurrentTempURL::get();
273 if ( !rCurrentTempURL.isEmpty() )
275 ::utl::UCBContentHelper::Kill( rCurrentTempURL );
279 } // namespace desktop
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */