GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / desktop / source / app / appinit.cxx
blob1d6c9f562d18357c9e4edad184d1af32fbe92ccf
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 "cmdlineargs.hxx"
27 #include "desktopresid.hxx"
28 #include "desktop.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>
47 #include <tools/rcid.h>
49 #include <rtl/instance.hxx>
50 #include <comphelper/processfactory.hxx>
51 #include <unotools/localfilehelper.hxx>
52 #include <unotools/ucbhelper.hxx>
53 #include <unotools/tempfile.hxx>
54 #include <vcl/svapp.hxx>
55 #include <unotools/pathoptions.hxx>
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
67 // -----------------------------------------------------------------------------
69 static void configureUcb()
71 SAL_INFO( "desktop.app", "desktop (sb93797) ::configureUcb" );
73 // For backwards compatibility, in case some code still uses plain
74 // createInstance w/o args directly to obtain an instance:
75 UniversalContentBroker::create(comphelper::getProcessComponentContext());
77 #if ENABLE_GNOME_VFS
78 try {
79 // Instantiate GNOME-VFS UCP in the thread that initialized GNOME in order
80 // to avoid a deadlock that may occur in case the UCP gets initialized from
81 // a different thread (which may happen when calling remotely via UNO); this
82 // is not a fix, just a workaround:
83 Reference< XCurrentContext > xCurrentContext(getCurrentContext());
84 Any aValue(xCurrentContext->getValueByName("system.desktop-environment"));
85 OUString aDesktopEnvironment;
86 if ((aValue >>= aDesktopEnvironment) && aDesktopEnvironment == "GNOME")
88 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
89 UniversalContentBroker::create(xContext)
90 ->registerContentProvider(
91 Reference<XContentProvider>(
92 xContext->getServiceManager()->createInstanceWithContext(
93 "com.sun.star.ucb.GnomeVFSContentProvider", xContext),
94 UNO_QUERY_THROW),
95 ".*", false);
98 catch ( const Exception & )
100 SAL_WARN( "desktop.app", "missing gnome-vfs component to initialize thread workaround" );
102 #endif // ENABLE_GNOME_VFS
105 void Desktop::InitApplicationServiceManager()
107 SAL_INFO( "desktop.app", "desktop (cd100003) ::createApplicationServiceManager" );
108 Reference<XMultiServiceFactory> sm;
109 #ifdef ANDROID
110 OUString aUnoRc( OUString( "file:///assets/program/unorc" ) );
111 sm.set(
112 cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
113 UNO_QUERY_THROW);
114 #else
115 sm.set(
116 cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
117 UNO_QUERY_THROW);
118 #endif
119 comphelper::setProcessServiceFactory(sm);
122 void Desktop::RegisterServices(Reference< XComponentContext > const & context)
124 if( !m_bServicesRegistered )
126 SAL_INFO( "desktop.app", "desktop (cd100003) ::registerServices" );
128 // interpret command line arguments
129 CommandLineArgs& rCmdLine = GetCommandLineArgs();
131 // Headless mode for FAT Office
132 sal_Bool bHeadlessMode = rCmdLine.IsHeadless();
133 if ( bHeadlessMode )
134 Application::EnableHeadlessMode(false);
136 // read accept string from configuration
137 OUString conDcpCfg(
138 officecfg::Setup::Office::ooSetupConnectionURL::get(context));
139 if (!conDcpCfg.isEmpty()) {
140 createAcceptor(conDcpCfg);
143 std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
144 for (std::vector< OUString >::const_iterator i(conDcp.begin());
145 i != conDcp.end(); ++i)
147 createAcceptor(*i);
150 configureUcb();
152 CreateTemporaryDirectory();
153 m_bServicesRegistered = true;
157 namespace
159 struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
160 struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
163 static sal_Bool bAccept = sal_False;
165 void Desktop::createAcceptor(const OUString& aAcceptString)
167 // check whether the requested acceptor already exists
168 AcceptorMap &rMap = acceptorMap::get();
169 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
170 if (pIter == rMap.end() )
172 Sequence< Any > aSeq( 2 );
173 aSeq[0] <<= aAcceptString;
174 aSeq[1] <<= bAccept;
175 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
176 Reference<XInitialization> rAcceptor(
177 xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
178 UNO_QUERY );
179 if ( rAcceptor.is() )
183 rAcceptor->initialize( aSeq );
184 rMap.insert(AcceptorMap::value_type(aAcceptString, rAcceptor));
186 catch (const com::sun::star::uno::Exception& e)
188 // no error handling needed...
189 // acceptor just won't come up
190 SAL_WARN( "desktop.app", "Acceptor could not be created: " << e.Message);
193 else
195 // there is already an acceptor with this description
196 SAL_WARN( "desktop.app", "Acceptor already exists.");
201 class enable
203 private:
204 Sequence<Any> m_aSeq;
205 public:
206 enable() : m_aSeq(1) {
207 m_aSeq[0] <<= sal_True;
209 void operator() (const AcceptorMap::value_type& val) {
210 if (val.second.is()) {
211 val.second->initialize(m_aSeq);
216 void Desktop::enableAcceptors()
218 SAL_INFO( "desktop.app", "desktop (lo119109) Desktop::enableAcceptors");
219 if (!bAccept)
221 // from now on, all new acceptors are enabled
222 bAccept = sal_True;
223 // enable existing acceptors by calling initialize(true)
224 // on all existing acceptors
225 AcceptorMap &rMap = acceptorMap::get();
226 std::for_each(rMap.begin(), rMap.end(), enable());
230 void Desktop::destroyAcceptor(const OUString& aAcceptString)
232 // special case stop all acceptors
233 AcceptorMap &rMap = acceptorMap::get();
234 if (aAcceptString.equalsAscii("all")) {
235 rMap.clear();
237 } else {
238 // try to remove acceptor from map
239 AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
240 if (pIter != rMap.end() ) {
241 // remove reference from map
242 // this is the last reference and the acceptor will be destructed
243 rMap.erase(aAcceptString);
244 } else {
245 SAL_WARN( "desktop.app", "Found no acceptor to remove");
251 void Desktop::DeregisterServices()
253 // stop all acceptors by clearing the map
254 acceptorMap::get().clear();
257 void Desktop::CreateTemporaryDirectory()
259 SAL_INFO( "desktop.app", "desktop (cd100003) ::createTemporaryDirectory" );
261 OUString aTempBaseURL;
264 SvtPathOptions aOpt;
265 aTempBaseURL = aOpt.GetTempPath();
267 catch (RuntimeException& e)
269 // Catch runtime exception here: We have to add language dependent info
270 // to the exception message. Fallback solution uses hard coded string.
271 OUString aMsg;
272 DesktopResId aResId( STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE );
273 aResId.SetRT( RSC_STRING );
274 if ( aResId.GetResMgr()->IsAvailable( aResId ))
275 aMsg = OUString( aResId );
276 else
277 aMsg = "The path manager is not available.\n";
278 e.Message = aMsg + e.Message;
279 throw;
282 // set temp base directory
283 sal_Int32 nLength = aTempBaseURL.getLength();
284 if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
285 aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
287 OUString aRet;
288 OUString aTempPath( aTempBaseURL );
290 // create new current temporary directory
291 ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTempBaseURL, aRet );
292 ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
293 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
294 if ( aTempPath.isEmpty() )
296 ::osl::File::getTempDirURL( aTempBaseURL );
298 nLength = aTempBaseURL.getLength();
299 if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
300 aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
302 aTempPath = aTempBaseURL;
303 ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
304 aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
307 // set new current temporary directory
308 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aTempPath, aRet );
309 CurrentTempURL::get() = aRet;
312 void Desktop::RemoveTemporaryDirectory()
314 SAL_INFO( "desktop.app", "desktop (cd100003) ::removeTemporaryDirectory" );
316 // remove current temporary directory
317 OUString &rCurrentTempURL = CurrentTempURL::get();
318 if ( !rCurrentTempURL.isEmpty() )
320 ::utl::UCBContentHelper::Kill( rCurrentTempURL );
324 } // namespace desktop
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */