Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / app / appinit.cxx
blob926ad1bfa6b798700b35744563150073f271dc1a
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 <comphelper/diagnose_ex.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <unotools/ucbhelper.hxx>
41 #include <unotools/tempfile.hxx>
42 #include <vcl/svapp.hxx>
43 #include <unotools/pathoptions.hxx>
44 #include <map>
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::registry;
50 using namespace ::com::sun::star::ucb;
52 namespace desktop
56 static void configureUcb()
58 // For backwards compatibility, in case some code still uses plain
59 // createInstance w/o args directly to obtain an instance:
60 UniversalContentBroker::create(comphelper::getProcessComponentContext());
63 void Desktop::InitApplicationServiceManager()
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(
80 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
81 UNO_QUERY_THROW);
82 #endif
83 comphelper::setProcessServiceFactory(sm);
86 void Desktop::RegisterServices()
88 if( m_bServicesRegistered )
89 return;
91 // interpret command line arguments
92 CommandLineArgs& rCmdLine = GetCommandLineArgs();
94 // Headless mode for FAT Office, auto cancels any dialogs that popup
95 if (rCmdLine.IsHeadless())
96 Application::EnableHeadlessMode(false);
98 // read accept string from configuration
99 OUString conDcpCfg(
100 officecfg::Setup::Office::ooSetupConnectionURL::get());
101 if (!conDcpCfg.isEmpty()) {
102 createAcceptor(conDcpCfg);
105 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
106 for (auto const& elem : conDcp)
108 createAcceptor(elem);
111 configureUcb();
113 CreateTemporaryDirectory();
114 m_bServicesRegistered = true;
117 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
119 namespace
121 AcceptorMap& acceptorMap()
123 static AcceptorMap SINGLETON;
124 return SINGLETON;
126 OUString& CurrentTempURL()
128 static OUString SINGLETON;
129 return SINGLETON;
133 static bool bAccept = false;
135 void Desktop::createAcceptor(const OUString& aAcceptString)
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;
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() )
152 rAcceptor->initialize( aSeq );
153 rMap.emplace(aAcceptString, rAcceptor);
155 catch (const css::uno::Exception&)
157 // no error handling needed...
158 // acceptor just won't come up
159 TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
162 else
164 // there is already an acceptor with this description
165 SAL_WARN( "desktop.app", "Acceptor already exists.");
169 namespace {
171 class enable
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);
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();
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();
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().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::SetTempNameBaseDirectory( aTempBaseURL );
246 if ( aTempPath.isEmpty()
247 && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
249 aTempPath = ::utl::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() = aRet;
262 void Desktop::RemoveTemporaryDirectory()
264 // remove current temporary directory
265 OUString &rCurrentTempURL = CurrentTempURL();
266 if ( !rCurrentTempURL.isEmpty() )
268 ::utl::UCBContentHelper::Kill( rCurrentTempURL );
272 } // namespace desktop
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */