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