Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <algorithm>
22 :
23 : #include <config_vclplug.h>
24 :
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>
46 :
47 : #include <tools/rcid.h>
48 :
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>
56 : #include <map>
57 :
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;
64 :
65 : namespace desktop
66 : {
67 :
68 :
69 :
70 160 : static void configureUcb()
71 : {
72 : SAL_INFO( "desktop.app", "desktop (sb93797) ::configureUcb" );
73 :
74 : // For backwards compatibility, in case some code still uses plain
75 : // createInstance w/o args directly to obtain an instance:
76 160 : UniversalContentBroker::create(comphelper::getProcessComponentContext());
77 :
78 : #if ENABLE_GNOME_VFS
79 : try {
80 : // Instantiate GNOME-VFS UCP in the thread that initialized GNOME in order
81 : // to avoid a deadlock that may occur in case the UCP gets initialized from
82 : // a different thread (which may happen when calling remotely via UNO); this
83 : // is not a fix, just a workaround:
84 : Reference< XCurrentContext > xCurrentContext(getCurrentContext());
85 : Any aValue(xCurrentContext->getValueByName("system.desktop-environment"));
86 : OUString aDesktopEnvironment;
87 : if ((aValue >>= aDesktopEnvironment) && aDesktopEnvironment == "GNOME")
88 : {
89 : Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
90 : UniversalContentBroker::create(xContext)
91 : ->registerContentProvider(
92 : Reference<XContentProvider>(
93 : xContext->getServiceManager()->createInstanceWithContext(
94 : "com.sun.star.ucb.GnomeVFSContentProvider", xContext),
95 : UNO_QUERY_THROW),
96 : ".*", false);
97 : }
98 : }
99 : catch ( const Exception & )
100 : {
101 : SAL_WARN( "desktop.app", "missing gnome-vfs component to initialize thread workaround" );
102 : }
103 : #endif // ENABLE_GNOME_VFS
104 160 : }
105 :
106 160 : void Desktop::InitApplicationServiceManager()
107 : {
108 : SAL_INFO( "desktop.app", "desktop (cd100003) ::createApplicationServiceManager" );
109 160 : Reference<XMultiServiceFactory> sm;
110 : #ifdef ANDROID
111 : OUString aUnoRc( "file:///assets/program/unorc" );
112 : sm.set(
113 : cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
114 : UNO_QUERY_THROW);
115 : #else
116 : sm.set(
117 320 : cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
118 160 : UNO_QUERY_THROW);
119 : #endif
120 160 : comphelper::setProcessServiceFactory(sm);
121 160 : }
122 :
123 160 : void Desktop::RegisterServices(Reference< XComponentContext > const & context)
124 : {
125 160 : if( !m_bServicesRegistered )
126 : {
127 : SAL_INFO( "desktop.app", "desktop (cd100003) ::registerServices" );
128 :
129 : // interpret command line arguments
130 160 : CommandLineArgs& rCmdLine = GetCommandLineArgs();
131 :
132 : // Headless mode for FAT Office
133 160 : bool bHeadlessMode = rCmdLine.IsHeadless();
134 160 : if ( bHeadlessMode )
135 160 : Application::EnableHeadlessMode(false);
136 :
137 : // read accept string from configuration
138 : OUString conDcpCfg(
139 160 : officecfg::Setup::Office::ooSetupConnectionURL::get(context));
140 160 : if (!conDcpCfg.isEmpty()) {
141 0 : createAcceptor(conDcpCfg);
142 : }
143 :
144 160 : std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
145 960 : for (std::vector< OUString >::const_iterator i(conDcp.begin());
146 640 : i != conDcp.end(); ++i)
147 : {
148 160 : createAcceptor(*i);
149 : }
150 :
151 160 : configureUcb();
152 :
153 160 : CreateTemporaryDirectory();
154 160 : m_bServicesRegistered = true;
155 : }
156 160 : }
157 :
158 : typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
159 :
160 : namespace
161 : {
162 : struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
163 : struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
164 : }
165 :
166 : static bool bAccept = false;
167 :
168 160 : void Desktop::createAcceptor(const OUString& aAcceptString)
169 : {
170 : // check whether the requested acceptor already exists
171 160 : AcceptorMap &rMap = acceptorMap::get();
172 160 : AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
173 160 : if (pIter == rMap.end() )
174 : {
175 160 : Sequence< Any > aSeq( 2 );
176 160 : aSeq[0] <<= aAcceptString;
177 160 : aSeq[1] <<= bAccept;
178 320 : Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
179 : Reference<XInitialization> rAcceptor(
180 320 : xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
181 320 : UNO_QUERY );
182 160 : if ( rAcceptor.is() )
183 : {
184 : try
185 : {
186 160 : rAcceptor->initialize( aSeq );
187 160 : rMap.insert(AcceptorMap::value_type(aAcceptString, rAcceptor));
188 : }
189 0 : catch (const com::sun::star::uno::Exception& e)
190 : {
191 : // no error handling needed...
192 : // acceptor just won't come up
193 : SAL_WARN( "desktop.app", "Acceptor could not be created: " << e.Message);
194 : }
195 : }
196 : else
197 : {
198 : // there is already an acceptor with this description
199 : SAL_WARN( "desktop.app", "Acceptor already exists.");
200 160 : }
201 : }
202 160 : }
203 :
204 288 : class enable
205 : {
206 : private:
207 : Sequence<Any> m_aSeq;
208 : public:
209 96 : enable() : m_aSeq(1) {
210 96 : m_aSeq[0] <<= sal_True;
211 96 : }
212 96 : void operator() (const AcceptorMap::value_type& val) {
213 96 : if (val.second.is()) {
214 96 : val.second->initialize(m_aSeq);
215 : }
216 96 : }
217 : };
218 :
219 96 : void Desktop::enableAcceptors()
220 : {
221 : SAL_INFO( "desktop.app", "desktop (lo119109) Desktop::enableAcceptors");
222 96 : if (!bAccept)
223 : {
224 : // from now on, all new acceptors are enabled
225 96 : bAccept = true;
226 : // enable existing acceptors by calling initialize(true)
227 : // on all existing acceptors
228 96 : AcceptorMap &rMap = acceptorMap::get();
229 96 : std::for_each(rMap.begin(), rMap.end(), enable());
230 : }
231 96 : }
232 :
233 0 : void Desktop::destroyAcceptor(const OUString& aAcceptString)
234 : {
235 : // special case stop all acceptors
236 0 : AcceptorMap &rMap = acceptorMap::get();
237 0 : if (aAcceptString.equalsAscii("all")) {
238 0 : rMap.clear();
239 :
240 : } else {
241 : // try to remove acceptor from map
242 0 : AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
243 0 : if (pIter != rMap.end() ) {
244 : // remove reference from map
245 : // this is the last reference and the acceptor will be destructed
246 0 : rMap.erase(aAcceptString);
247 : } else {
248 : SAL_WARN( "desktop.app", "Found no acceptor to remove");
249 : }
250 : }
251 0 : }
252 :
253 :
254 160 : void Desktop::DeregisterServices()
255 : {
256 : // stop all acceptors by clearing the map
257 160 : acceptorMap::get().clear();
258 160 : }
259 :
260 160 : void Desktop::CreateTemporaryDirectory()
261 : {
262 : SAL_INFO( "desktop.app", "desktop (cd100003) ::createTemporaryDirectory" );
263 :
264 160 : OUString aTempBaseURL;
265 : try
266 : {
267 160 : SvtPathOptions aOpt;
268 160 : aTempBaseURL = aOpt.GetTempPath();
269 : }
270 0 : catch (RuntimeException& e)
271 : {
272 : // Catch runtime exception here: We have to add language dependent info
273 : // to the exception message. Fallback solution uses hard coded string.
274 0 : OUString aMsg;
275 0 : DesktopResId aResId( STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE );
276 0 : aResId.SetRT( RSC_STRING );
277 0 : if ( aResId.GetResMgr()->IsAvailable( aResId ))
278 0 : aMsg = OUString( aResId );
279 : else
280 0 : aMsg = "The path manager is not available.\n";
281 0 : e.Message = aMsg + e.Message;
282 0 : throw;
283 : }
284 :
285 : // set temp base directory
286 160 : sal_Int32 nLength = aTempBaseURL.getLength();
287 160 : if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
288 0 : aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
289 :
290 320 : OUString aRet;
291 320 : OUString aTempPath( aTempBaseURL );
292 :
293 : // create new current temporary directory
294 160 : ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTempBaseURL, aRet );
295 160 : ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
296 160 : aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
297 160 : if ( aTempPath.isEmpty() )
298 : {
299 0 : ::osl::File::getTempDirURL( aTempBaseURL );
300 :
301 0 : nLength = aTempBaseURL.getLength();
302 0 : if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
303 0 : aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
304 :
305 0 : aTempPath = aTempBaseURL;
306 0 : ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
307 0 : aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
308 : }
309 :
310 : // set new current temporary directory
311 160 : ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aTempPath, aRet );
312 320 : CurrentTempURL::get() = aRet;
313 160 : }
314 :
315 160 : void Desktop::RemoveTemporaryDirectory()
316 : {
317 : SAL_INFO( "desktop.app", "desktop (cd100003) ::removeTemporaryDirectory" );
318 :
319 : // remove current temporary directory
320 160 : OUString &rCurrentTempURL = CurrentTempURL::get();
321 160 : if ( !rCurrentTempURL.isEmpty() )
322 : {
323 160 : ::utl::UCBContentHelper::Kill( rCurrentTempURL );
324 : }
325 160 : }
326 :
327 480 : } // namespace desktop
328 :
329 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|