31 #include <rtl/ustring.hxx>
34 #include <com/sun/star/io/IOException.hpp>
35 #include <com/sun/star/util/XCloseable.hpp>
36 #include <com/sun/star/util/CloseVetoException.hpp>
37 #include <com/sun/star/task/InteractionHandler.hpp>
38 #include <com/sun/star/util/URL.hpp>
39 #include <com/sun/star/frame/Desktop.hpp>
40 #include <com/sun/star/container/XContainerQuery.hpp>
41 #include <com/sun/star/container/XEnumeration.hpp>
42 #include <com/sun/star/frame/XDispatch.hpp>
43 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/view/XPrintable.hpp>
46 #include <com/sun/star/util/URLTransformer.hpp>
47 #include <com/sun/star/util/XURLTransformer.hpp>
48 #include <com/sun/star/document/MacroExecMode.hpp>
49 #include <com/sun/star/document/XTypeDetection.hpp>
50 #include <com/sun/star/document/UpdateDocMode.hpp>
51 #include <com/sun/star/frame/XStorable.hpp>
52 #include <com/sun/star/script/XLibraryContainer2.hpp>
53 #include <com/sun/star/document/XEmbeddedScripts.hpp>
62 #include <osl/thread.hxx>
63 #include <osl/file.hxx>
65 #include <string_view>
67 using namespace ::
osl;
78 namespace document = ::com::sun::star::document;
87 DispatchHolder(
const URL& rURL, Reference< XDispatch >
const & rDispatch ) :
94 std::shared_ptr<const SfxFilter> impl_lookupExportFilterForUrl( std::u16string_view rUrl, std::u16string_view rFactory )
97 OUString sQuery =
"getSortedFilterList()"
99 OUString::Concat(rFactory) +
101 OUString::number(static_cast<sal_Int32>(SfxFilterFlags::EXPORT)) +
106 const Reference< XContainerQuery > xFilterFactory(
107 xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.FilterFactory", xContext ),
110 std::shared_ptr<const SfxFilter> pBestMatch;
112 const Reference< XEnumeration > xFilterEnum(
113 xFilterFactory->createSubSetEnumerationByQuery( sQuery ), UNO_SET_THROW );
114 while ( xFilterEnum->hasMoreElements() )
117 const OUString
aName( aFilterProps.getUnpackedValueOrDefault(
"Name", OUString() ) );
118 if ( !
aName.isEmpty() )
121 if ( pFilter && pFilter->CanExport() && pFilter->GetWildcard().Matches( rUrl ) )
123 if ( !pBestMatch || ( SfxFilterFlags::PREFERED & pFilter->GetFilterFlags() ) )
124 pBestMatch = pFilter;
132 std::shared_ptr<const SfxFilter> impl_getExportFilterFromUrl(
133 const OUString& rUrl,
const OUString& rFactory)
138 const Reference< document::XTypeDetection > xTypeDetector(
139 xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.TypeDetection",
xContext ),
141 const OUString aTypeName( xTypeDetector->queryTypeByURL( rUrl ) );
143 std::shared_ptr<const SfxFilter> pFilter(
SfxFilterMatcher( rFactory ).GetFilter4EA( aTypeName, SfxFilterFlags::EXPORT ) );
145 pFilter = impl_lookupExportFilterForUrl( rUrl, rFactory );
149 FileBase::getSystemPathFromFileURL( rUrl, aTempName );
151 std::cerr <<
"Error: no export filter for " << aSource <<
" found, aborting." << std::endl;
156 catch (
const Exception& )
162 OUString impl_GuessFilter(
const OUString& rUrlOut,
const OUString& rDocService )
165 std::shared_ptr<const SfxFilter> pOutFilter = impl_getExportFilterFromUrl( rUrlOut, rDocService );
167 aOutFilter = pOutFilter->GetFilterName();
173 void scriptCat(
const Reference< XModel >& xDoc )
175 Reference< XEmbeddedScripts > xScriptAccess( xDoc, UNO_QUERY );
178 std::cout <<
"No script access\n";
183 Reference< css::script::XLibraryContainer2 > xLibraries(
184 xScriptAccess->getBasicLibraries() );
186 if ( !xLibraries.is() )
188 std::cout <<
"No script libraries\n";
193 std::cout <<
"Libraries: " << aLibNames.getLength() <<
"\n";
194 for (OUString
const & libName : aLibNames)
196 std::cout <<
"Library: '" << libName <<
"' children: ";
197 Reference< XNameContainer > xContainer;
199 if (!xLibraries->isLibraryLoaded( libName ))
200 xLibraries->loadLibrary( libName );
201 xContainer = Reference< XNameContainer >(
202 xLibraries->getByName( libName ), UNO_QUERY );
204 catch (
const css::uno::Exception &e)
206 std::cout <<
"[" << libName <<
"] - failed to load library: " << e.Message <<
"\n";
209 if( !xContainer.is() )
215 std::cout << aObjectNames.getLength() <<
"\n\n";
216 for ( sal_Int32 j = 0 ; j < aObjectNames.getLength() ; ++j )
218 const OUString &rObjectName = aObjectNames[j];
222 Any aCode = xContainer->getByName( rObjectName );
223 OUString aCodeString;
225 if (! (aCode >>= aCodeString ) )
226 std::cout <<
"[" << rObjectName <<
"] - error fetching code\n";
228 std::cout <<
"[" << rObjectName <<
"]\n"
229 << aCodeString.trim()
230 <<
"\n[/" << rObjectName <<
"]\n";
232 catch (
const css::uno::Exception &e)
234 std::cout <<
"[" << rObjectName <<
"] - exception " << e.Message <<
" fetching code\n";
237 if (j < aObjectNames.getLength() - 1)
238 std::cout <<
"\n----------------------------------------------------------\n";
246 void batchPrint( std::u16string_view rPrinterName,
const Reference< XPrintable > &xDoc,
250 OUString aPrinterName;
251 size_t nPathIndex = rPrinterName.rfind(
';' );
252 if( nPathIndex != std::u16string_view::npos )
253 aFilterOut=rPrinterName.substr( nPathIndex+1 );
254 if( nPathIndex != 0 )
255 aPrinterName=rPrinterName.substr( 0, nPathIndex );
258 aOutFilename.SetExtension( u
"pdf" );
259 FileBase::getFileURLFromSystemPath( aFilterOut, aFilterOut );
260 OUString aOutFile = aFilterOut +
"/" + aOutFilename.getName();
263 FileBase::getSystemPathFromFileURL( aName, aTempName );
265 FileBase::getSystemPathFromFileURL( aOutFile, aTempName );
266 OString aTargetURL8 =
OUStringToOString(aTempName, osl_getThreadTextEncoding() );
268 std::cout <<
"print " << aSource8 <<
" -> " << aTargetURL8;
269 std::cout <<
" using " << (aPrinterName.isEmpty() ?
"<default_printer>" :
OUStringToOString( aPrinterName, osl_getThreadTextEncoding() ));
270 std::cout << std::endl;
273 Sequence < PropertyValue > aPrinterArgs;
274 if( !aPrinterName.isEmpty() )
277 xDoc->setPrinter( aPrinterArgs );
283 xDoc->print( aPrinterArgs );
303 std::vector< DispatchHolder > aDispatches;
304 bool bSetInputFilter =
false;
305 OUString aForcedInputFilter;
307 for (
auto const & aDispatchRequest: aDispatchRequestsList)
312 bSetInputFilter =
true;
313 aForcedInputFilter = aDispatchRequest.aURL;
319 std::vector<PropertyValue> aArgs;
322 aArgs.emplace_back(
"Referer", 0,
Any(OUString(
"private:OpenEvent")),
323 PropertyState_DIRECT_VALUE);
325 OUString aTarget(
"_default");
336 aArgs.emplace_back(
"ReadOnly", 0,
Any(
true), PropertyState_DIRECT_VALUE);
338 aArgs.emplace_back(
"OpenNewView", 0,
Any(
true), PropertyState_DIRECT_VALUE);
340 aArgs.emplace_back(
"Hidden", 0,
Any(
true), PropertyState_DIRECT_VALUE);
342 aArgs.emplace_back(
"Silent", 0,
Any(
true), PropertyState_DIRECT_VALUE);
349 Reference < XInteractionHandler2 > xInteraction(
352 aArgs.emplace_back(
"InteractionHandler", 0,
Any(xInteraction),
353 PropertyState_DIRECT_VALUE);
355 aArgs.emplace_back(
"MacroExecutionMode", 0,
356 Any(css::document::MacroExecMode::USE_CONFIG),
357 PropertyState_DIRECT_VALUE);
359 aArgs.emplace_back(
"UpdateDocMode", 0,
360 Any(css::document::UpdateDocMode::ACCORDING_TO_CONFIG),
361 PropertyState_DIRECT_VALUE);
364 if ( !aDispatchRequest.aPreselectedFactory.isEmpty() )
367 Any(aDispatchRequest.aPreselectedFactory),
368 PropertyState_DIRECT_VALUE);
371 OUString aName(
GetURL_Impl( aDispatchRequest.aURL, aDispatchRequest.aCwdUrl ) );
375 Reference < XPrintable > xDoc;
377 ( aName.startsWith(
".uno" ) ) ||
378 ( aName.startsWith(
"slot:" ) ) ||
379 ( aName.startsWith(
"macro:" ) ) ||
380 ( aName.startsWith(
"vnd.sun.star.script") )
386 aURL.Complete = aName;
388 Reference < XDispatch > xDispatcher ;
392 xParser->parseStrict( aURL );
394 xDispatcher = xDesktop->queryDispatch( aURL, OUString(), 0 );
396 !xDispatcher.is(),
"desktop.app",
397 "unsupported dispatch request <" << aName <<
">");
398 if( xDispatcher.is() )
401 osl::MutexGuard aGuard(
m_mutex);
408 aDispatches.emplace_back( aURL, xDispatcher );
411 else if ( aName.startsWith(
"service:" ) )
415 aURL.Complete = aName;
417 Reference < XDispatch > xDispatcher ;
421 xParser->parseStrict( aURL );
423 xDispatcher = xDesktop->queryDispatch( aURL, OUString(), 0 );
425 if( xDispatcher.is() )
434 Reference < XNotifyingDispatch > xDisp( xDispatcher, UNO_QUERY );
436 xDisp->dispatchWithNotification( aURL, aArgs2,
this );
438 xDispatcher->dispatch( aURL, aArgs2 );
440 catch (
const css::uno::Exception&)
444 "Desktop::OpenDefault() ignoring Exception while calling XNotifyingDispatch");
451 if ( aObj.
GetProtocol() == INetProtocol::PrivSoffice )
452 aTarget =
"_default";
458 aArgs.emplace_back(
"AsTemplate", 0,
460 PropertyState_DIRECT_VALUE);
465 aArgs.emplace_back(
"ReadOnly", 0,
Any(
true), PropertyState_DIRECT_VALUE);
470 aArgs.emplace_back(
"StartPresentation", 0,
Any(
true), PropertyState_DIRECT_VALUE);
474 if( bSetInputFilter )
476 sal_Int32 nFilterOptionsIndex = 0;
477 aArgs.emplace_back(
"FilterName", 0,
478 Any(aForcedInputFilter.getToken(0,
':', nFilterOptionsIndex)),
479 PropertyState_DIRECT_VALUE);
481 if (0 < nFilterOptionsIndex)
483 aArgs.emplace_back(
"FilterOptions", 0,
484 Any(aForcedInputFilter.copy(nFilterOptionsIndex)),
485 PropertyState_DIRECT_VALUE);
496 catch (
const css::lang::IllegalArgumentException&)
500 "Dispatchwatcher IllegalArgumentException while calling loadComponentFromURL");
502 catch (
const css::io::IOException&)
506 "Dispatchwatcher IOException while calling loadComponentFromURL");
531 Reference< XStorable > xStorable( xDoc, UNO_QUERY );
532 if ( xStorable.is() ) {
533 OUString aParam = aDispatchRequest.aPrinterName;
534 sal_Int32 nPathIndex = aParam.lastIndexOf(
';' );
535 sal_Int32 nFilterIndex = aParam.indexOf(
':' );
536 sal_Int32 nImgFilterIndex = aParam.lastIndexOf(
'|' );
537 if( nPathIndex < nFilterIndex )
546 if( nFilterIndex >= 0 )
548 aFilter = aParam.copy( nFilterIndex+1, nPathIndex-nFilterIndex-1 );
549 aFilterExt = aParam.copy( 0, nFilterIndex );
555 aFilterExt = aParam.copy( 0, nPathIndex );
558 if( nImgFilterIndex >= 0 )
560 aImgOut = aParam.copy( nImgFilterIndex+1 );
561 aFilterOut = aParam.copy( nPathIndex+1, nImgFilterIndex-nPathIndex-1 );
564 aFilterOut = aParam.copy( nPathIndex+1 );
566 FileBase::getFileURLFromSystemPath( aFilterOut, aFilterOut );
574 std::unique_ptr<utl::TempFile> fileForCat;
577 fileForCat = std::make_unique<utl::TempFile>();
578 if (fileForCat->IsValid())
579 fileForCat->EnableKillingFile();
581 std::cerr <<
"Error: Cannot create temporary file..." << std::endl ;
582 aOutFile = fileForCat->GetURL();
587 OUString aDocService;
588 Reference< XModel >
xModel( xDoc, UNO_QUERY );
594 aFilter = impl_GuessFilter( aOutFile, aDocService );
597 bool bMultiFileTarget =
false;
599 if (aFilter.isEmpty())
601 std::cerr <<
"Error: no export filter" << std::endl;
605 sal_Int32 nFilterOptionsIndex = aFilter.indexOf(
':');
606 sal_Int32 nProps = ( 0 < nFilterOptionsIndex ) ? 4 : 3;
608 if ( !aImgOut.isEmpty() )
610 Sequence<PropertyValue> conversionProperties( nProps );
611 auto pconversionProperties = conversionProperties.getArray();
612 pconversionProperties[0].Name =
"ConversionRequestOrigin";
613 pconversionProperties[0].Value <<= OUString(
"CommandLine");
614 pconversionProperties[1].Name =
"Overwrite";
615 pconversionProperties[1].Value <<=
true;
617 pconversionProperties[2].Name =
"FilterName";
618 if( 0 < nFilterOptionsIndex )
620 OUString sFilterName = aFilter.copy(0, nFilterOptionsIndex);
621 OUString sFilterOptions = aFilter.copy(nFilterOptionsIndex + 1);
623 if (sFilterName ==
"Text - txt - csv (StarCalc)")
636 const OUString aTok(sFilterOptions.getToken(11,
',', nIdx));
639 bMultiFileTarget = (!aTok.isEmpty() && aTok.toInt32() != 0);
642 pconversionProperties[2].Value <<= sFilterName;
644 pconversionProperties[3].Name =
"FilterOptions";
645 pconversionProperties[3].Value <<= sFilterOptions;
649 pconversionProperties[2].Value <<= aFilter;
652 if ( !aImgOut.isEmpty() )
654 assert(conversionProperties[nProps-1].
Name.isEmpty());
655 pconversionProperties[nProps-1].Name =
"ImageFilter";
656 pconversionProperties[nProps-1].Value <<= aImgOut;
660 FileBase::getSystemPathFromFileURL(aName, aTempName);
662 FileBase::getSystemPathFromFileURL(aOutFile, aTempName);
666 std::cout <<
"convert " << aSource8;
667 if (!bMultiFileTarget)
668 std::cout <<
" -> " << aTargetURL8;
669 std::cout <<
" using filter : " <<
OUStringToOString(aFilter, osl_getThreadTextEncoding()) << std::endl;
671 std::cout <<
"Overwriting: " <<
OUStringToOString(aTempName, osl_getThreadTextEncoding()) << std::endl ;
675 xStorable->storeToURL(aOutFile, conversionProperties);
679 std::cerr <<
"Error: Please verify input parameters...";
680 if (!rException.Message.isEmpty())
681 std::cerr <<
" (" << rException.Message <<
")";
682 std::cerr << std::endl;
685 if (fileForCat && fileForCat->IsValid())
687 SvStream* aStream = fileForCat->GetStream(StreamMode::STD_READ);
688 while (aStream->
good())
692 for (sal_Int32
i = 0;
i < aStr.getLength(); ++
i)
694 std::cout << aStr[
i];
696 std::cout << std::endl;
704 Reference< XModel >
xModel( xDoc, UNO_QUERY );
710 batchPrint( aDispatchRequest.aPrinterName, xDoc, aObj, aName );
718 "Name", aDispatchRequest.aPrinterName) };
719 xDoc->setPrinter( aPrinterArgs );
725 xDoc->print( aPrinterArgs );
730 std::cerr <<
"Error: source file could not be loaded" << std::endl;
736 Reference < XCloseable > xClose( xDoc, UNO_QUERY );
738 xClose->close(
true );
741 Reference < XComponent > xComp( xDoc, UNO_QUERY );
746 catch (
const css::util::CloseVetoException&)
756 if ( !aDispatches.empty() )
759 Sequence < PropertyValue > aArgs{
764 for (
const DispatchHolder & aDispatche : aDispatches)
766 Reference< XDispatch >
xDispatch = aDispatche.xDispatch;
767 Reference < XNotifyingDispatch > xDisp( xDispatch, UNO_QUERY );
769 xDisp->dispatchWithNotification( aDispatche.aURL, aArgs,
this );
773 osl::MutexGuard aGuard(
m_mutex);
776 xDispatch->dispatch( aDispatche.aURL, aArgs );
781 ::osl::ClearableMutexGuard aGuard(
m_mutex);
788 if ( bEmpty && !bNoTerminate )
791 Reference< XElementAccess > xList = xDesktop->getFrames();
793 if ( !xList->hasElements() )
796 return xDesktop->terminate();
811 osl::ClearableMutexGuard aGuard(
m_mutex);
819 Reference< XElementAccess > xList = xDesktop->getFrames();
821 if ( !xList->hasElements() )
824 xDesktop->terminate();
SVL_DLLPUBLIC bool IsDocument(const OUString &rURL)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
OUString GetURL_Impl(const OUString &rName, std::optional< OUString > const &cwdUrl)
bool ReadLine(OStringBuffer &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
#define SFX_FILTER_NOTINSTALLED
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
static COMPHELPER_DLLPUBLIC css::uno::Reference< css::lang::XComponent > dispatch(const css::uno::Reference< css::uno::XInterface > &xStartPoint, const OUString &sURL, const OUString &sTarget, const css::uno::Sequence< css::beans::PropertyValue > &lArguments)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
#define TOOLS_WARN_EXCEPTION(area, stream)
static bool AreRequestsPending()
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static void RequestsCompleted()
static std::shared_ptr< const SfxFilter > GetFilterByName(const OUString &rName)
sal_Int16 m_nRequestCount
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static uno::Reference< css::uno::XComponentContext > xContext
Reference< XDispatch > xDispatch
#define SAL_WARN_IF(condition, area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
INetProtocol GetProtocol() const
Reference< XComponentContext > getProcessComponentContext()
bool Append(std::u16string_view rTheSegment, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
void SetExtension(std::u16string_view rTheExtension)
Reference< XModel > xModel
virtual void SAL_CALL dispatchFinished(const css::frame::DispatchResultEvent &aEvent) override
virtual ~DispatchWatcher() override
bool executeDispatchRequests(const std::vector< DispatchRequest > &aDispatches, bool bNoTerminate)