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 <sfx2/docfile.hxx>
22 : #include <sfx2/docfilt.hxx>
23 : #include <sfx2/fcontnr.hxx>
24 : #include "osl/file.hxx"
25 : #include "sfx2/app.hxx"
26 : #include <svl/fstathelper.hxx>
27 :
28 : #include "dispatchwatcher.hxx"
29 : #include <rtl/ustring.hxx>
30 : #include <tools/string.hxx>
31 : #include <comphelper/processfactory.hxx>
32 : #include <comphelper/synchronousdispatch.hxx>
33 : #include <com/sun/star/util/XCloseable.hpp>
34 : #include <com/sun/star/util/CloseVetoException.hpp>
35 : #include <com/sun/star/task/InteractionHandler.hpp>
36 : #include <com/sun/star/util/URL.hpp>
37 : #include <com/sun/star/frame/Desktop.hpp>
38 : #include <com/sun/star/container/XEnumeration.hpp>
39 : #include <com/sun/star/frame/XFramesSupplier.hpp>
40 : #include <com/sun/star/frame/XDispatch.hpp>
41 : #include <com/sun/star/frame/XComponentLoader.hpp>
42 : #include <com/sun/star/beans/PropertyValue.hpp>
43 : #include <com/sun/star/view/XPrintable.hpp>
44 : #include <com/sun/star/frame/XDispatchProvider.hpp>
45 : #include <com/sun/star/util/URLTransformer.hpp>
46 : #include <com/sun/star/util/XURLTransformer.hpp>
47 : #include <com/sun/star/document/MacroExecMode.hpp>
48 : #include <com/sun/star/document/UpdateDocMode.hpp>
49 : #include <com/sun/star/frame/XStorable.hpp>
50 :
51 : #include <tools/urlobj.hxx>
52 : #include <comphelper/mediadescriptor.hxx>
53 :
54 : #include <vector>
55 : #include <osl/thread.hxx>
56 : #include <rtl/instance.hxx>
57 :
58 : using ::rtl::OUString;
59 : using namespace ::osl;
60 : using namespace ::com::sun::star::uno;
61 : using namespace ::com::sun::star::util;
62 : using namespace ::com::sun::star::lang;
63 : using namespace ::com::sun::star::frame;
64 : using namespace ::com::sun::star::container;
65 : using namespace ::com::sun::star::beans;
66 : using namespace ::com::sun::star::view;
67 : using namespace ::com::sun::star::task;
68 :
69 : namespace desktop
70 : {
71 :
72 : String GetURL_Impl(
73 : const String& rName, boost::optional< rtl::OUString > const & cwdUrl );
74 :
75 0 : struct DispatchHolder
76 : {
77 0 : DispatchHolder( const URL& rURL, Reference< XDispatch >& rDispatch ) :
78 0 : aURL( rURL ), xDispatch( rDispatch ) {}
79 :
80 : URL aURL;
81 : rtl::OUString cwdUrl;
82 : Reference< XDispatch > xDispatch;
83 : };
84 :
85 0 : static String impl_GetFilterFromExt( OUString aUrl, SfxFilterFlags nFlags,
86 : String aAppl )
87 : {
88 0 : String aFilter;
89 : SfxMedium* pMedium = new SfxMedium( aUrl,
90 0 : STREAM_STD_READ );
91 :
92 0 : const SfxFilter *pSfxFilter = NULL;
93 0 : if( nFlags == SFX_FILTER_EXPORT )
94 : {
95 0 : SfxFilterMatcher( aAppl ).GuessFilterIgnoringContent( *pMedium, &pSfxFilter, nFlags, 0 );
96 : }
97 : else
98 : {
99 0 : SFX_APP()->GetFilterMatcher().GuessFilter( *pMedium, &pSfxFilter, nFlags, 0 );
100 : }
101 :
102 0 : if( pSfxFilter )
103 : {
104 0 : if (nFlags == SFX_FILTER_EXPORT)
105 0 : aFilter = pSfxFilter->GetFilterName();
106 : else
107 0 : aFilter = pSfxFilter->GetServiceName();
108 : }
109 :
110 0 : delete pMedium;
111 0 : return aFilter;
112 : }
113 0 : static OUString impl_GuessFilter( OUString aUrlIn, OUString aUrlOut )
114 : {
115 : /* aAppl can also be set to Factory like scalc, swriter... */
116 0 : String aAppl;
117 0 : aAppl = impl_GetFilterFromExt( aUrlIn, SFX_FILTER_IMPORT, aAppl );
118 0 : return impl_GetFilterFromExt( aUrlOut, SFX_FILTER_EXPORT, aAppl );
119 : }
120 :
121 : namespace
122 : {
123 : class theWatcherMutex : public rtl::Static<Mutex, theWatcherMutex> {};
124 : }
125 :
126 0 : Mutex& DispatchWatcher::GetMutex()
127 : {
128 0 : return theWatcherMutex::get();
129 : }
130 :
131 : // Create or get the dispatch watcher implementation. This implementation must be
132 : // a singleton to prevent access to the framework after it wants to terminate.
133 0 : DispatchWatcher* DispatchWatcher::GetDispatchWatcher()
134 : {
135 0 : static Reference< XInterface > xDispatchWatcher;
136 : static DispatchWatcher* pDispatchWatcher = NULL;
137 :
138 0 : if ( !xDispatchWatcher.is() )
139 : {
140 0 : ::osl::MutexGuard aGuard( GetMutex() );
141 :
142 0 : if ( !xDispatchWatcher.is() )
143 : {
144 0 : pDispatchWatcher = new DispatchWatcher();
145 :
146 : // We have to hold a reference to ourself forever to prevent our own destruction.
147 0 : xDispatchWatcher = static_cast< cppu::OWeakObject *>( pDispatchWatcher );
148 0 : }
149 : }
150 :
151 0 : return pDispatchWatcher;
152 : }
153 :
154 :
155 0 : DispatchWatcher::DispatchWatcher()
156 0 : : m_nRequestCount(0)
157 : {
158 0 : }
159 :
160 :
161 0 : DispatchWatcher::~DispatchWatcher()
162 : {
163 0 : }
164 :
165 :
166 0 : sal_Bool DispatchWatcher::executeDispatchRequests( const DispatchList& aDispatchRequestsList, bool bNoTerminate )
167 : {
168 0 : Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
169 :
170 0 : DispatchList::const_iterator p;
171 0 : std::vector< DispatchHolder > aDispatches;
172 0 : ::rtl::OUString aAsTemplateArg( RTL_CONSTASCII_USTRINGPARAM( "AsTemplate"));
173 0 : sal_Bool bSetInputFilter = sal_False;
174 0 : ::rtl::OUString aForcedInputFilter;
175 :
176 0 : for ( p = aDispatchRequestsList.begin(); p != aDispatchRequestsList.end(); ++p )
177 : {
178 0 : const DispatchRequest& aDispatchRequest = *p;
179 :
180 : // create parameter array
181 0 : sal_Int32 nCount = 4;
182 0 : if ( !aDispatchRequest.aPreselectedFactory.isEmpty() )
183 0 : nCount++;
184 :
185 : // Set Input Filter
186 0 : if ( aDispatchRequest.aRequestType == REQUEST_INFILTER )
187 : {
188 0 : bSetInputFilter = sal_True;
189 0 : aForcedInputFilter = aDispatchRequest.aURL;
190 0 : OfficeIPCThread::RequestsCompleted( 1 );
191 0 : continue;
192 : }
193 :
194 : // we need more properties for a print/print to request
195 0 : if ( aDispatchRequest.aRequestType == REQUEST_PRINT ||
196 : aDispatchRequest.aRequestType == REQUEST_PRINTTO ||
197 : aDispatchRequest.aRequestType == REQUEST_BATCHPRINT ||
198 : aDispatchRequest.aRequestType == REQUEST_CONVERSION)
199 0 : nCount++;
200 :
201 0 : Sequence < PropertyValue > aArgs( nCount );
202 :
203 : // mark request as user interaction from outside
204 0 : aArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Referer"));
205 0 : aArgs[0].Value <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("private:OpenEvent"));
206 :
207 0 : if ( aDispatchRequest.aRequestType == REQUEST_PRINT ||
208 : aDispatchRequest.aRequestType == REQUEST_PRINTTO ||
209 : aDispatchRequest.aRequestType == REQUEST_BATCHPRINT ||
210 : aDispatchRequest.aRequestType == REQUEST_CONVERSION)
211 : {
212 0 : aArgs[1].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
213 0 : aArgs[2].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OpenNewView"));
214 0 : aArgs[3].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
215 0 : aArgs[4].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Silent"));
216 : }
217 : else
218 : {
219 : Reference < XInteractionHandler2 > xInteraction(
220 0 : InteractionHandler::createWithParent(::comphelper::getProcessComponentContext(), 0) );
221 :
222 0 : aArgs[1].Name = OUString(RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ));
223 0 : aArgs[1].Value <<= xInteraction;
224 :
225 0 : sal_Int16 nMacroExecMode = ::com::sun::star::document::MacroExecMode::USE_CONFIG;
226 0 : aArgs[2].Name = OUString(RTL_CONSTASCII_USTRINGPARAM( "MacroExecutionMode" ));
227 0 : aArgs[2].Value <<= nMacroExecMode;
228 :
229 0 : sal_Int16 nUpdateDoc = ::com::sun::star::document::UpdateDocMode::ACCORDING_TO_CONFIG;
230 0 : aArgs[3].Name = OUString(RTL_CONSTASCII_USTRINGPARAM( "UpdateDocMode" ));
231 0 : aArgs[3].Value <<= nUpdateDoc;
232 : }
233 :
234 0 : if ( !aDispatchRequest.aPreselectedFactory.isEmpty() )
235 : {
236 0 : aArgs[nCount-1].Name = ::comphelper::MediaDescriptor::PROP_DOCUMENTSERVICE();
237 0 : aArgs[nCount-1].Value <<= aDispatchRequest.aPreselectedFactory;
238 : }
239 :
240 0 : String aName( GetURL_Impl( aDispatchRequest.aURL, aDispatchRequest.aCwdUrl ) );
241 0 : ::rtl::OUString aTarget( RTL_CONSTASCII_USTRINGPARAM("_default") );
242 :
243 0 : if ( aDispatchRequest.aRequestType == REQUEST_PRINT ||
244 : aDispatchRequest.aRequestType == REQUEST_PRINTTO ||
245 : aDispatchRequest.aRequestType == REQUEST_BATCHPRINT ||
246 : aDispatchRequest.aRequestType == REQUEST_CONVERSION)
247 : {
248 : // documents opened for printing are opened readonly because they must be opened as a new document and this
249 : // document could be open already
250 0 : aArgs[1].Value <<= sal_True;
251 :
252 : // always open a new document for printing, because it must be disposed afterwards
253 0 : aArgs[2].Value <<= sal_True;
254 :
255 : // printing is done in a hidden view
256 0 : aArgs[3].Value <<= sal_True;
257 :
258 : // load document for printing without user interaction
259 0 : aArgs[4].Value <<= sal_True;
260 :
261 : // hidden documents should never be put into open tasks
262 0 : aTarget = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") );
263 : }
264 : // load the document ... if they are loadable!
265 : // Otherwise try to dispatch it ...
266 0 : Reference < XPrintable > xDoc;
267 0 : if(
268 0 : ( aName.CompareToAscii( ".uno" , 4 ) == COMPARE_EQUAL ) ||
269 0 : ( aName.CompareToAscii( "slot:" , 5 ) == COMPARE_EQUAL ) ||
270 0 : ( aName.CompareToAscii( "macro:", 6 ) == COMPARE_EQUAL ) ||
271 0 : ( aName.CompareToAscii("vnd.sun.star.script", 19) == COMPARE_EQUAL)
272 : )
273 : {
274 : // Attention: URL must be parsed full. Otherwise some detections on it will fail!
275 : // It doesnt matter, if parser isn't available. Because; We try loading of URL then ...
276 0 : URL aURL ;
277 0 : aURL.Complete = aName;
278 :
279 0 : Reference < XDispatch > xDispatcher ;
280 0 : Reference < XURLTransformer > xParser ( URLTransformer::create(::comphelper::getProcessComponentContext()) );
281 :
282 0 : if( xParser.is() == sal_True )
283 0 : xParser->parseStrict( aURL );
284 :
285 0 : xDispatcher = xDesktop->queryDispatch( aURL, ::rtl::OUString(), 0 );
286 :
287 0 : if( xDispatcher.is() == sal_True )
288 : {
289 : {
290 0 : ::osl::ClearableMutexGuard aGuard( GetMutex() );
291 : // Remember request so we can find it in statusChanged!
292 0 : m_aRequestContainer.insert( DispatchWatcherHashMap::value_type( aURL.Complete, (sal_Int32)1 ) );
293 0 : m_nRequestCount++;
294 : }
295 :
296 : // Use local vector to store dispatcher because we have to fill our request container before
297 : // we can dispatch. Otherwise it would be possible that statusChanged is called before we dispatched all requests!!
298 0 : aDispatches.push_back( DispatchHolder( aURL, xDispatcher ));
299 0 : }
300 : }
301 0 : else if ( ( aName.CompareToAscii( "service:" , 8 ) == COMPARE_EQUAL ) )
302 : {
303 : // TODO: the dispatch has to be done for loadComponentFromURL as well. Please ask AS for more details.
304 0 : URL aURL ;
305 0 : aURL.Complete = aName;
306 :
307 0 : Reference < XDispatch > xDispatcher ;
308 0 : Reference < XURLTransformer > xParser ( URLTransformer::create(::comphelper::getProcessComponentContext()) );
309 :
310 0 : if( xParser.is() == sal_True )
311 0 : xParser->parseStrict( aURL );
312 :
313 0 : xDispatcher = xDesktop->queryDispatch( aURL, ::rtl::OUString(), 0 );
314 :
315 0 : if( xDispatcher.is() == sal_True )
316 : {
317 : try
318 : {
319 : // We have to be listener to catch errors during dispatching URLs.
320 : // Otherwise it would be possible to have an office running without an open
321 : // window!!
322 0 : Sequence < PropertyValue > aArgs2(1);
323 0 : aArgs2[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SynchronMode"));
324 0 : aArgs2[0].Value <<= sal_True;
325 0 : Reference < XNotifyingDispatch > xDisp( xDispatcher, UNO_QUERY );
326 0 : if ( xDisp.is() )
327 0 : xDisp->dispatchWithNotification( aURL, aArgs2, DispatchWatcher::GetDispatchWatcher() );
328 : else
329 0 : xDispatcher->dispatch( aURL, aArgs2 );
330 : }
331 0 : catch (const ::com::sun::star::uno::Exception&)
332 : {
333 : OUString aMsg = OUString(RTL_CONSTASCII_USTRINGPARAM(
334 0 : "Desktop::OpenDefault() IllegalArgumentException while calling XNotifyingDispatch: "));
335 0 : OSL_FAIL( OUStringToOString(aMsg, RTL_TEXTENCODING_ASCII_US).getStr());
336 : }
337 0 : }
338 : }
339 : else
340 : {
341 0 : INetURLObject aObj( aName );
342 0 : if ( aObj.GetProtocol() == INET_PROT_PRIVATE )
343 0 : aTarget = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("_default") );
344 :
345 : // Set "AsTemplate" argument according to request type
346 0 : if ( aDispatchRequest.aRequestType == REQUEST_FORCENEW ||
347 : aDispatchRequest.aRequestType == REQUEST_FORCEOPEN )
348 : {
349 0 : sal_Int32 nIndex = aArgs.getLength();
350 0 : aArgs.realloc( nIndex+1 );
351 0 : aArgs[nIndex].Name = aAsTemplateArg;
352 0 : if ( aDispatchRequest.aRequestType == REQUEST_FORCENEW )
353 0 : aArgs[nIndex].Value <<= sal_True;
354 : else
355 0 : aArgs[nIndex].Value <<= sal_False;
356 : }
357 :
358 : // if we are called in viewmode, open document read-only
359 0 : if(aDispatchRequest.aRequestType == REQUEST_VIEW) {
360 0 : sal_Int32 nIndex = aArgs.getLength();
361 0 : aArgs.realloc(nIndex+1);
362 0 : aArgs[nIndex].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
363 0 : aArgs[nIndex].Value <<= sal_True;
364 : }
365 :
366 : // if we are called with -start set Start in mediadescriptor
367 0 : if(aDispatchRequest.aRequestType == REQUEST_START) {
368 0 : sal_Int32 nIndex = aArgs.getLength();
369 0 : aArgs.realloc(nIndex+1);
370 0 : aArgs[nIndex].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("StartPresentation"));
371 0 : aArgs[nIndex].Value <<= sal_True;
372 : }
373 :
374 : // Force input filter, if possible
375 0 : if( bSetInputFilter )
376 : {
377 0 : sal_Int32 nIndex = aArgs.getLength();
378 0 : aArgs.realloc(nIndex+1);
379 0 : aArgs[nIndex].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName"));
380 0 : aArgs[nIndex].Value <<= aForcedInputFilter;
381 : }
382 :
383 : // This is a synchron loading of a component so we don't have to deal with our statusChanged listener mechanism.
384 : try
385 : {
386 0 : xDoc = Reference < XPrintable >( ::comphelper::SynchronousDispatch::dispatch( xDesktop, aName, aTarget, 0, aArgs ), UNO_QUERY );
387 : }
388 0 : catch (const ::com::sun::star::lang::IllegalArgumentException& iae)
389 : {
390 : OUString aMsg = OUString(RTL_CONSTASCII_USTRINGPARAM(
391 : "Dispatchwatcher IllegalArgumentException while calling loadComponentFromURL: "))
392 0 : + iae.Message;
393 0 : OSL_FAIL( OUStringToOString(aMsg, RTL_TEXTENCODING_ASCII_US).getStr());
394 : }
395 0 : catch (const com::sun::star::io::IOException& ioe)
396 : {
397 : OUString aMsg = OUString(RTL_CONSTASCII_USTRINGPARAM(
398 : "Dispatchwatcher IOException while calling loadComponentFromURL: "))
399 0 : + ioe.Message;
400 0 : OSL_FAIL( OUStringToOString(aMsg, RTL_TEXTENCODING_ASCII_US).getStr());
401 : }
402 0 : if ( aDispatchRequest.aRequestType == REQUEST_OPEN ||
403 : aDispatchRequest.aRequestType == REQUEST_VIEW ||
404 : aDispatchRequest.aRequestType == REQUEST_START ||
405 : aDispatchRequest.aRequestType == REQUEST_FORCEOPEN ||
406 : aDispatchRequest.aRequestType == REQUEST_FORCENEW )
407 : {
408 : // request is completed
409 0 : OfficeIPCThread::RequestsCompleted( 1 );
410 : }
411 0 : else if ( aDispatchRequest.aRequestType == REQUEST_PRINT ||
412 : aDispatchRequest.aRequestType == REQUEST_PRINTTO ||
413 : aDispatchRequest.aRequestType == REQUEST_BATCHPRINT ||
414 : aDispatchRequest.aRequestType == REQUEST_CONVERSION )
415 : {
416 0 : if ( xDoc.is() )
417 : {
418 0 : if ( aDispatchRequest.aRequestType == REQUEST_CONVERSION ) {
419 0 : Reference< XStorable > xStorable( xDoc, UNO_QUERY );
420 0 : if ( xStorable.is() ) {
421 0 : rtl::OUString aParam = aDispatchRequest.aPrinterName;
422 0 : sal_Int32 nPathIndex = aParam.lastIndexOf( ';' );
423 0 : sal_Int32 nFilterIndex = aParam.indexOf( ':' );
424 0 : if( nPathIndex < nFilterIndex )
425 0 : nFilterIndex = -1;
426 0 : rtl::OUString aFilterOut=aParam.copy( nPathIndex+1 );
427 0 : rtl::OUString aFilter;
428 0 : rtl::OUString aFilterExt;
429 0 : sal_Bool bGuess = sal_False;
430 :
431 0 : if( nFilterIndex >= 0 )
432 : {
433 0 : aFilter = aParam.copy( nFilterIndex+1, nPathIndex-nFilterIndex-1 );
434 0 : aFilterExt = aParam.copy( 0, nFilterIndex );
435 : }
436 : else
437 : {
438 : // Guess
439 0 : bGuess = sal_True;
440 0 : aFilterExt = aParam.copy( 0, nPathIndex );
441 : }
442 0 : INetURLObject aOutFilename( aObj );
443 0 : aOutFilename.SetExtension( aFilterExt );
444 0 : FileBase::getFileURLFromSystemPath( aFilterOut, aFilterOut );
445 : rtl::OUString aOutFile = aFilterOut+
446 0 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "/" ))+
447 0 : aOutFilename.getName();
448 :
449 0 : if ( bGuess )
450 : {
451 0 : aFilter = impl_GuessFilter( aName, aOutFile );
452 : }
453 :
454 0 : Sequence<PropertyValue> conversionProperties( 2 );
455 0 : conversionProperties[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Overwrite" ));
456 0 : conversionProperties[0].Value <<= sal_True;
457 :
458 0 : conversionProperties[1].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "FilterName" ));
459 0 : conversionProperties[1].Value <<= aFilter;
460 :
461 0 : rtl::OUString aTempName;
462 0 : FileBase::getSystemPathFromFileURL( aName, aTempName );
463 0 : rtl::OString aSource8 = ::rtl::OUStringToOString ( aTempName, RTL_TEXTENCODING_UTF8 );
464 0 : FileBase::getSystemPathFromFileURL( aOutFile, aTempName );
465 0 : rtl::OString aTargetURL8 = ::rtl::OUStringToOString(aTempName, RTL_TEXTENCODING_UTF8 );
466 : printf("convert %s -> %s using %s\n", aSource8.getStr(), aTargetURL8.getStr(),
467 0 : ::rtl::OUStringToOString( aFilter, RTL_TEXTENCODING_UTF8 ).getStr());
468 0 : if( FStatHelper::IsDocument(aOutFile) )
469 0 : printf("Overwriting: %s\n",::rtl::OUStringToOString( aTempName, RTL_TEXTENCODING_UTF8 ).getStr() );
470 : try
471 : {
472 0 : xStorable->storeToURL( aOutFile, conversionProperties );
473 : }
474 0 : catch (const Exception&)
475 : {
476 0 : fprintf( stderr, "Error: Please reverify input parameters...\n" );
477 0 : }
478 0 : }
479 0 : } else if ( aDispatchRequest.aRequestType == REQUEST_BATCHPRINT ) {
480 0 : rtl::OUString aParam = aDispatchRequest.aPrinterName;
481 0 : sal_Int32 nPathIndex = aParam.lastIndexOf( ';' );
482 :
483 0 : rtl::OUString aFilterOut;
484 0 : rtl::OUString aPrinterName;
485 0 : if( nPathIndex != -1 )
486 0 : aFilterOut=aParam.copy( nPathIndex+1 );
487 0 : if( nPathIndex != 0 )
488 0 : aPrinterName=aParam.copy( 0, nPathIndex );
489 :
490 0 : INetURLObject aOutFilename( aObj );
491 0 : aOutFilename.SetExtension( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ps")) );
492 0 : FileBase::getFileURLFromSystemPath( aFilterOut, aFilterOut );
493 : rtl::OUString aOutFile = aFilterOut+
494 0 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "/" ))+
495 0 : aOutFilename.getName();
496 :
497 0 : rtl::OUString aTempName;
498 0 : FileBase::getSystemPathFromFileURL( aName, aTempName );
499 0 : rtl::OString aSource8 = ::rtl::OUStringToOString ( aTempName, RTL_TEXTENCODING_UTF8 );
500 0 : FileBase::getSystemPathFromFileURL( aOutFile, aTempName );
501 0 : rtl::OString aTargetURL8 = ::rtl::OUStringToOString(aTempName, RTL_TEXTENCODING_UTF8 );
502 : printf("print %s -> %s using %s\n", aSource8.getStr(), aTargetURL8.getStr(),
503 0 : aPrinterName.isEmpty() ?
504 0 : "<default_printer>" : ::rtl::OUStringToOString( aPrinterName, RTL_TEXTENCODING_UTF8 ).getStr() );
505 :
506 : // create the custom printer, if given
507 0 : Sequence < PropertyValue > aPrinterArgs( 1 );
508 0 : if( !aPrinterName.isEmpty() )
509 : {
510 0 : aPrinterArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
511 0 : aPrinterArgs[0].Value <<= aPrinterName;
512 0 : xDoc->setPrinter( aPrinterArgs );
513 : }
514 :
515 : // print ( also without user interaction )
516 0 : aPrinterArgs.realloc(2);
517 0 : aPrinterArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FileName"));
518 0 : aPrinterArgs[0].Value <<= aOutFile;
519 0 : aPrinterArgs[1].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Wait"));
520 0 : aPrinterArgs[1].Value <<= ( sal_Bool ) sal_True;
521 0 : xDoc->print( aPrinterArgs );
522 : } else {
523 0 : if ( aDispatchRequest.aRequestType == REQUEST_PRINTTO )
524 : {
525 : // create the printer
526 0 : Sequence < PropertyValue > aPrinterArgs( 1 );
527 0 : aPrinterArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
528 0 : aPrinterArgs[0].Value <<= ::rtl::OUString( aDispatchRequest.aPrinterName );
529 0 : xDoc->setPrinter( aPrinterArgs );
530 : }
531 :
532 : // print ( also without user interaction )
533 0 : Sequence < PropertyValue > aPrinterArgs( 1 );
534 0 : aPrinterArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Wait"));
535 0 : aPrinterArgs[0].Value <<= ( sal_Bool ) sal_True;
536 0 : xDoc->print( aPrinterArgs );
537 : }
538 : }
539 : else
540 : {
541 : // place error message here ...
542 : }
543 :
544 : // remove the document
545 : try
546 : {
547 0 : Reference < XCloseable > xClose( xDoc, UNO_QUERY );
548 0 : if ( xClose.is() )
549 0 : xClose->close( sal_True );
550 : else
551 : {
552 0 : Reference < XComponent > xComp( xDoc, UNO_QUERY );
553 0 : if ( xComp.is() )
554 0 : xComp->dispose();
555 0 : }
556 : }
557 0 : catch (const com::sun::star::util::CloseVetoException&)
558 : {
559 : }
560 :
561 : // request is completed
562 0 : OfficeIPCThread::RequestsCompleted( 1 );
563 0 : }
564 : }
565 0 : }
566 :
567 0 : if ( !aDispatches.empty() )
568 : {
569 : // Execute all asynchronous dispatches now after we placed them into our request container!
570 0 : Sequence < PropertyValue > aArgs( 2 );
571 0 : aArgs[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Referer"));
572 0 : aArgs[0].Value <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("private:OpenEvent"));
573 0 : aArgs[1].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SynchronMode"));
574 0 : aArgs[1].Value <<= sal_True;
575 :
576 0 : for ( sal_uInt32 n = 0; n < aDispatches.size(); n++ )
577 : {
578 0 : Reference< XDispatch > xDispatch = aDispatches[n].xDispatch;
579 0 : Reference < XNotifyingDispatch > xDisp( xDispatch, UNO_QUERY );
580 0 : if ( xDisp.is() )
581 0 : xDisp->dispatchWithNotification( aDispatches[n].aURL, aArgs, this );
582 : else
583 : {
584 0 : ::osl::ClearableMutexGuard aGuard( GetMutex() );
585 0 : m_nRequestCount--;
586 0 : aGuard.clear();
587 0 : xDispatch->dispatch( aDispatches[n].aURL, aArgs );
588 : }
589 0 : }
590 : }
591 :
592 0 : ::osl::ClearableMutexGuard aGuard( GetMutex() );
593 0 : bool bEmpty = (m_nRequestCount == 0);
594 0 : aGuard.clear();
595 :
596 : // No more asynchronous requests?
597 : // The requests are removed from the request container after they called back to this
598 : // implementation via statusChanged!!
599 0 : if ( bEmpty && !bNoTerminate /*m_aRequestContainer.empty()*/ )
600 : {
601 : // We have to check if we have an open task otherwise we have to shutdown the office.
602 0 : aGuard.clear();
603 0 : Reference< XElementAccess > xList( xDesktop->getFrames(), UNO_QUERY );
604 :
605 0 : if ( !xList->hasElements() )
606 : {
607 : // We don't have any task open so we have to shutdown ourself!!
608 0 : return xDesktop->terminate();
609 0 : }
610 : }
611 :
612 0 : return sal_False;
613 : }
614 :
615 :
616 0 : void SAL_CALL DispatchWatcher::disposing( const ::com::sun::star::lang::EventObject& )
617 : throw(::com::sun::star::uno::RuntimeException)
618 : {
619 0 : }
620 :
621 :
622 0 : void SAL_CALL DispatchWatcher::dispatchFinished( const DispatchResultEvent& ) throw( RuntimeException )
623 : {
624 0 : osl::ClearableMutexGuard aGuard( GetMutex() );
625 0 : sal_Int16 nCount = --m_nRequestCount;
626 0 : aGuard.clear();
627 0 : OfficeIPCThread::RequestsCompleted( 1 );
628 0 : if ( !nCount && !OfficeIPCThread::AreRequestsPending() )
629 : {
630 : // We have to check if we have an open task otherwise we have to shutdown the office.
631 0 : Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
632 0 : Reference< XElementAccess > xList( xDesktop->getFrames(), UNO_QUERY );
633 :
634 0 : if ( !xList->hasElements() )
635 : {
636 : // We don't have any task open so we have to shutdown ourself!!
637 0 : xDesktop->terminate();
638 0 : }
639 0 : }
640 0 : }
641 :
642 : }
643 :
644 :
645 :
646 :
647 :
648 :
649 :
650 :
651 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|