1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
;
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
;
69 OUString
aUnoRc( "file:///assets/program/unorc" );
71 cppu::defaultBootstrap_InitialComponentContext( aUnoRc
)->getServiceManager(),
74 OUString
uri( "$APP_DATA_DIR" );
75 rtl_bootstrap_expandMacros( &uri
.pData
);
76 OUString
aUnoRc("file://" + uri
+ "/unorc");
78 cppu::defaultBootstrap_InitialComponentContext( aUnoRc
)->getServiceManager(),
82 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
85 comphelper::setProcessServiceFactory(sm
);
88 void Desktop::RegisterServices(Reference
< XComponentContext
> const & context
)
90 if( m_bServicesRegistered
)
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
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
);
117 CreateTemporaryDirectory();
118 m_bServicesRegistered
= true;
121 typedef std::map
< OUString
, css::uno::Reference
<css::lang::XInitialization
> > AcceptorMap
;
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() )
139 Sequence
< Any
> aSeq( 2 );
140 aSeq
[0] <<= aAcceptString
;
142 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
143 Reference
<XInitialization
> rAcceptor(
144 xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext
),
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");
162 // there is already an acceptor with this description
163 SAL_WARN( "desktop.app", "Acceptor already exists.");
172 Sequence
<Any
> m_aSeq
;
174 enable() : m_aSeq(1) {
177 void operator() (const AcceptorMap::value_type
& val
) {
178 if (val
.second
.is()) {
179 val
.second
->initialize(m_aSeq
);
187 IMPL_STATIC_LINK_NOARG(Desktop
, EnableAcceptors_Impl
, void*, void)
191 // from now on, all new acceptors are enabled
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") {
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
);
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
;
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
;
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
254 if (osl::FileBase::getFileURLFromSystemPath( aTempPath
, aRet
)
255 != osl::FileBase::E_None
)
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: */