Bump version to 6.0-36
[LibreOffice.git] / desktop / source / app / appinit.cxx
blob853eaf8734aee522a777b8cd11438d87117c7120
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>
48 #include <rtl/instance.hxx>
49 #include <comphelper/processfactory.hxx>
50 #include <unotools/ucbhelper.hxx>
51 #include <unotools/tempfile.hxx>
52 #include <vcl/svapp.hxx>
53 #include <unotools/pathoptions.hxx>
54 #include <sfx2/safemode.hxx>
55 #include <map>
57 using namespace desktop;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::registry;
62 using namespace ::com::sun::star::ucb;
64 namespace desktop
68 static void configureUcb()
70 // For backwards compatibility, in case some code still uses plain
71 // createInstance w/o args directly to obtain an instance:
72 UniversalContentBroker::create(comphelper::getProcessComponentContext());
75 void Desktop::InitApplicationServiceManager()
77 Reference<XMultiServiceFactory> sm;
78 #ifdef ANDROID
79 OUString aUnoRc( "file:///assets/program/unorc" );
80 sm.set(
81 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
82 UNO_QUERY_THROW);
83 #else
84 sm.set(
85 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
86 UNO_QUERY_THROW);
87 #endif
88 comphelper::setProcessServiceFactory(sm);
91 void Desktop::RegisterServices(Reference< XComponentContext > const & context)
93 if( !m_bServicesRegistered )
95 // interpret command line arguments
96 CommandLineArgs& rCmdLine = GetCommandLineArgs();
98 // Headless mode for FAT Office, auto cancels any dialogs that popup
99 if (rCmdLine.IsEventTesting())
100 Application::EnableEventTestingMode();
101 else if (rCmdLine.IsHeadless())
102 Application::EnableHeadlessMode(false);
104 // read accept string from configuration
105 OUString conDcpCfg(
106 officecfg::Setup::Office::ooSetupConnectionURL::get(context));
107 if (!conDcpCfg.isEmpty()) {
108 createAcceptor(conDcpCfg);
111 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
112 for (std::vector< OUString >::const_iterator i(conDcp.begin());
113 i != conDcp.end(); ++i)
115 createAcceptor(*i);
118 configureUcb();
120 CreateTemporaryDirectory();
121 m_bServicesRegistered = true;
125 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
127 namespace
129 struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
130 struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
133 static bool bAccept = false;
135 void Desktop::createAcceptor(const OUString& aAcceptString)
137 // check whether the requested acceptor already exists
138 AcceptorMap &rMap = acceptorMap::get();
139 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
140 if (pIter == rMap.end() )
142 Sequence< Any > aSeq( 2 );
143 aSeq[0] <<= aAcceptString;
144 aSeq[1] <<= bAccept;
145 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
146 Reference<XInitialization> rAcceptor(
147 xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
148 UNO_QUERY );
149 if ( rAcceptor.is() )
153 rAcceptor->initialize( aSeq );
154 rMap.emplace(aAcceptString, rAcceptor);
156 catch (const css::uno::Exception& e)
158 // no error handling needed...
159 // acceptor just won't come up
160 SAL_WARN( "desktop.app", "Acceptor could not be created: " << e);
163 else
165 // there is already an acceptor with this description
166 SAL_WARN( "desktop.app", "Acceptor already exists.");
171 class enable
173 private:
174 Sequence<Any> m_aSeq;
175 public:
176 enable() : m_aSeq(1) {
177 m_aSeq[0] <<= true;
179 void operator() (const AcceptorMap::value_type& val) {
180 if (val.second.is()) {
181 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 // set temp base directory
245 if ( aTempBaseURL.endsWith( "/" ) )
246 aTempBaseURL = aTempBaseURL.copy( 0, aTempBaseURL.getLength() - 1 );
248 OUString aRet;
249 OUString aTempPath( aTempBaseURL );
251 // create new current temporary directory
252 osl::FileBase::getSystemPathFromFileURL( aTempBaseURL, aRet );
253 ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
254 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
255 if ( aTempPath.isEmpty() )
257 ::osl::File::getTempDirURL( aTempBaseURL );
259 if ( aTempBaseURL.endsWith( "/" ) )
260 aTempBaseURL = aTempBaseURL.copy( 0, aTempBaseURL.getLength() - 1 );
262 aTempPath = aTempBaseURL;
263 ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
264 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
267 // set new current temporary directory
268 if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
269 != osl::FileBase::E_None)
271 aRet.clear();
273 CurrentTempURL::get() = aRet;
276 void Desktop::RemoveTemporaryDirectory()
278 // remove current temporary directory
279 OUString &rCurrentTempURL = CurrentTempURL::get();
280 if ( !rCurrentTempURL.isEmpty() )
282 ::utl::UCBContentHelper::Kill( rCurrentTempURL );
286 } // namespace desktop
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */