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