1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/document/XFilter.hpp>
23 #include <com/sun/star/document/XExporter.hpp>
24 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
25 #include <com/sun/star/document/XGraphicStorageHandler.hpp>
26 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
27 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
28 #include <com/sun/star/frame/Desktop.hpp>
29 #include <com/sun/star/frame/XStorable.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/system/SystemShellExecute.hpp>
33 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
34 #include <com/sun/star/task/InteractionHandler.hpp>
35 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
36 #include <com/sun/star/xml/XImportFilter.hpp>
37 #include <com/sun/star/xml/XExportFilter.hpp>
38 #include <com/sun/star/xml/sax/Writer.hpp>
40 #include <comphelper/oslfile2streamwrap.hxx>
41 #include <comphelper/propertyvalue.hxx>
42 #include <vcl/svapp.hxx>
43 #include <sfx2/filedlghelper.hxx>
44 #include <osl/file.hxx>
45 #include <unotools/tempfile.hxx>
46 #include <comphelper/diagnose_ex.hxx>
47 #include <tools/debug.hxx>
48 #include <tools/urlobj.hxx>
49 #include <comphelper/processfactory.hxx>
51 #include "xmlfiltercommon.hxx"
52 #include "xmlfiltertestdialog.hxx"
57 using namespace comphelper
;
58 using namespace com::sun::star::lang
;
59 using namespace com::sun::star::beans
;
60 using namespace com::sun::star::container
;
61 using namespace com::sun::star::document
;
62 using namespace com::sun::star::frame
;
63 using namespace com::sun::star::task
;
64 using namespace com::sun::star::uno
;
65 using namespace com::sun::star::io
;
66 using namespace com::sun::star::system
;
67 using namespace com::sun::star::xml
;
68 using namespace com::sun::star::xml::sax
;
72 class GlobalEventListenerImpl
: public ::cppu::WeakImplHelper
< css::document::XDocumentEventListener
>
75 explicit GlobalEventListenerImpl( XMLFilterTestDialog
* pDialog
);
77 // XDocumentEventListener
78 virtual void SAL_CALL
documentEventOccured( const css::document::DocumentEvent
& Event
) override
;
80 // lang::XEventListener
81 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
83 XMLFilterTestDialog
* mpDialog
;
88 GlobalEventListenerImpl::GlobalEventListenerImpl( XMLFilterTestDialog
* pDialog
)
93 void SAL_CALL
GlobalEventListenerImpl::documentEventOccured( const css::document::DocumentEvent
& Event
)
95 ::SolarMutexGuard aGuard
;
96 if( Event
.EventName
== "OnFocus" || Event
.EventName
== "OnUnload" )
98 Reference
< XComponent
> xComp( Event
.Source
, UNO_QUERY
);
99 mpDialog
->updateCurrentDocumentButtonState( &xComp
);
103 void SAL_CALL
GlobalEventListenerImpl::disposing( const css::lang::EventObject
& /* Source */ )
107 /** returns true if the given component supports the given service */
108 static bool checkComponent( Reference
< XComponent
> const & rxComponent
, const OUString
& rServiceName
)
112 Reference
< XServiceInfo
> xInfo( rxComponent
, UNO_QUERY
);
115 if( xInfo
->supportsService( rServiceName
) )
117 // special case for impress documents which supports same service as draw documents
118 if ( rServiceName
== "com.sun.star.drawing.DrawingDocument" )
120 // so if we want a draw we need to check if it's not an impress
121 if( !xInfo
->supportsService("com.sun.star.presentation.PresentationDocument") )
131 catch( const Exception
& )
133 TOOLS_WARN_EXCEPTION("filter.xslt", "");
139 XMLFilterTestDialog::XMLFilterTestDialog(weld::Window
* pParent
,
140 const Reference
<XComponentContext
>& rxContext
)
141 : GenericDialogController(pParent
, "filter/ui/testxmlfilter.ui", "TestXMLFilterDialog")
142 , mxContext(rxContext
)
143 , m_xExport(m_xBuilder
->weld_widget("export"))
144 , m_xFTExportXSLTFile(m_xBuilder
->weld_label("exportxsltfile"))
145 , m_xPBExportBrowse(m_xBuilder
->weld_button("exportbrowse"))
146 , m_xPBCurrentDocument(m_xBuilder
->weld_button("currentdocument"))
147 , m_xFTNameOfCurrentFile(m_xBuilder
->weld_label("currentfilename"))
148 , m_xImport(m_xBuilder
->weld_widget("import"))
149 , m_xFTImportXSLTFile(m_xBuilder
->weld_label("importxsltfile"))
150 , m_xFTImportTemplate(m_xBuilder
->weld_label("templateimport"))
151 , m_xFTImportTemplateFile(m_xBuilder
->weld_label("importxslttemplate"))
152 , m_xCBXDisplaySource(m_xBuilder
->weld_check_button("displaysource"))
153 , m_xPBImportBrowse(m_xBuilder
->weld_button("importbrowse"))
154 , m_xPBRecentFile(m_xBuilder
->weld_button("recentfile"))
155 , m_xFTNameOfRecentFile(m_xBuilder
->weld_label("recentfilename"))
156 , m_xPBClose(m_xBuilder
->weld_button("close"))
158 m_xPBExportBrowse
->connect_clicked(LINK( this, XMLFilterTestDialog
, ClickHdl_Impl
) );
159 m_xPBCurrentDocument
->connect_clicked(LINK( this, XMLFilterTestDialog
, ClickHdl_Impl
) );
160 m_xPBImportBrowse
->connect_clicked(LINK( this, XMLFilterTestDialog
, ClickHdl_Impl
) );
161 m_xPBRecentFile
->connect_clicked(LINK( this, XMLFilterTestDialog
, ClickHdl_Impl
) );
162 m_xPBClose
->connect_clicked(LINK( this, XMLFilterTestDialog
, ClickHdl_Impl
) );
164 m_sDialogTitle
= m_xDialog
->get_title();
168 mxGlobalBroadcaster
= theGlobalEventBroadcaster::get(mxContext
);
169 mxGlobalEventListener
= new GlobalEventListenerImpl( this );
170 mxGlobalBroadcaster
->addDocumentEventListener( mxGlobalEventListener
);
172 catch( const Exception
& )
174 TOOLS_WARN_EXCEPTION("filter.xslt", "");
178 XMLFilterTestDialog::~XMLFilterTestDialog()
182 if( mxGlobalBroadcaster
.is() )
183 mxGlobalBroadcaster
->removeDocumentEventListener( mxGlobalEventListener
);
185 catch( const Exception
& )
187 TOOLS_WARN_EXCEPTION("filter.xslt", "");
191 void XMLFilterTestDialog::test( const filter_info_impl
& rFilterInfo
)
193 m_xFilterInfo
.reset(new filter_info_impl( rFilterInfo
));
195 m_sImportRecentFile
.clear();
202 static OUString
getFileNameFromURL( std::u16string_view rURL
)
204 INetURLObject
aURL( rURL
);
205 OUString
aName( aURL
.getName(INetURLObject::LAST_SEGMENT
, true, INetURLObject::DecodeMechanism::WithCharset
) );
209 void XMLFilterTestDialog::updateCurrentDocumentButtonState( Reference
< XComponent
> const * pRef
/* = NULL */ )
211 if( pRef
&& pRef
->is() )
213 if( checkComponent( *pRef
, m_xFilterInfo
->maDocumentService
) )
214 mxLastFocusModel
= *pRef
;
217 bool bExport
= (m_xFilterInfo
->maFlags
& 2) == 2;
218 Reference
< XComponent
> xCurrentDocument
;
220 xCurrentDocument
= getFrontMostDocument( m_xFilterInfo
->maDocumentService
);
221 m_xPBCurrentDocument
->set_sensitive( bExport
&& xCurrentDocument
.is() );
222 m_xFTNameOfCurrentFile
->set_sensitive( bExport
&& xCurrentDocument
.is() );
224 if( !xCurrentDocument
.is() )
228 Reference
< XDocumentPropertiesSupplier
> xDPS( xCurrentDocument
, UNO_QUERY
);
231 Reference
< XDocumentProperties
> xProps( xDPS
->getDocumentProperties() );
234 aTitle
= xProps
->getTitle();
238 if( aTitle
.isEmpty() )
240 Reference
< XStorable
> xStorable( xCurrentDocument
, UNO_QUERY
);
243 if( xStorable
->hasLocation() )
245 OUString
aURL( xStorable
->getLocation() );
246 aTitle
= getFileNameFromURL( aURL
);
251 m_xFTNameOfCurrentFile
->set_label( aTitle
);
254 void XMLFilterTestDialog::initDialog()
256 DBG_ASSERT( m_xFilterInfo
, "i need a filter I can test!" );
257 if( nullptr == m_xFilterInfo
)
260 OUString
aTitle( m_sDialogTitle
);
261 aTitle
= aTitle
.replaceFirst( "%s", m_xFilterInfo
->maFilterName
);
262 m_xDialog
->set_title( aTitle
);
264 bool bImport
= (m_xFilterInfo
->maFlags
& 1) == 1;
265 bool bExport
= (m_xFilterInfo
->maFlags
& 2) == 2;
267 updateCurrentDocumentButtonState();
269 m_xExport
->set_sensitive(bExport
);
270 m_xFTExportXSLTFile
->set_label( getFileNameFromURL( m_xFilterInfo
->maExportXSLT
) );
273 m_xImport
->set_sensitive(bImport
);
274 m_xFTImportTemplate
->set_sensitive(bImport
&& !m_xFilterInfo
->maImportTemplate
.isEmpty());
275 m_xFTImportTemplateFile
->set_sensitive(bImport
&& !m_xFilterInfo
->maImportTemplate
.isEmpty());
276 m_xPBRecentFile
->set_sensitive(bImport
&& !m_sImportRecentFile
.isEmpty());
277 m_xFTNameOfRecentFile
->set_sensitive(bImport
&& !m_sImportRecentFile
.isEmpty());
279 m_xFTImportXSLTFile
->set_label( getFileNameFromURL( m_xFilterInfo
->maImportXSLT
) );
280 m_xFTImportTemplateFile
->set_label( getFileNameFromURL( m_xFilterInfo
->maImportTemplate
) );
281 m_xFTNameOfRecentFile
->set_label( getFileNameFromURL( m_sImportRecentFile
) );
284 void XMLFilterTestDialog::onExportBrowse()
288 // Open Fileopen-Dialog
289 ::sfx2::FileDialogHelper
aDlg(
290 css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
291 FileDialogFlags::NONE
, m_xDialog
.get());
293 Reference
< XNameAccess
> xFilterContainer( mxContext
->getServiceManager()->createInstanceWithContext( "com.sun.star.document.FilterFactory", mxContext
), UNO_QUERY
);
294 Reference
< XNameAccess
> xTypeDetection( mxContext
->getServiceManager()->createInstanceWithContext( "com.sun.star.document.TypeDetection", mxContext
), UNO_QUERY
);
295 if( xFilterContainer
.is() && xTypeDetection
.is() )
297 const Sequence
< OUString
> aFilterNames( xFilterContainer
->getElementNames() );
299 for( OUString
const & filterName
: aFilterNames
)
301 Sequence
< PropertyValue
> aValues
;
303 Any
aAny( xFilterContainer
->getByName( filterName
) );
304 if( !(aAny
>>= aValues
) )
307 OUString aInterfaceName
;
308 OUString aType
, aService
;
309 sal_Int32
nFlags( 0 );
313 for( const PropertyValue
& rValue
: std::as_const(aValues
) )
315 if ( rValue
.Name
== "Type" )
317 rValue
.Value
>>= aType
;
320 else if ( rValue
.Name
== "DocumentService" )
322 rValue
.Value
>>= aService
;
325 else if ( rValue
.Name
== "Flags" )
327 rValue
.Value
>>= nFlags
;
330 else if ( rValue
.Name
== "UIName" )
332 rValue
.Value
>>= aInterfaceName
;
340 if( (nFound
== 15) && (!aType
.isEmpty() && aService
== m_xFilterInfo
->maDocumentService
) )
342 // see if this filter is not suppressed in dialog
343 if( (nFlags
& 0x1000) == 0 )
345 aAny
= xTypeDetection
->getByName( aType
);
346 Sequence
< PropertyValue
> aValues2
;
348 if( aAny
>>= aValues2
)
351 for( const PropertyValue
& rProp
: std::as_const(aValues2
) )
353 if ( rProp
.Name
== "Extensions" )
355 Sequence
< OUString
> aExtensions
;
356 if( rProp
.Value
>>= aExtensions
)
358 const sal_Int32
nCount( aExtensions
.getLength() );
359 OUString
* pExtensions
= aExtensions
.getArray();
361 for( n
= 0; n
< nCount
; n
++ )
365 aExtension
+= "*." + *pExtensions
++;
371 OUString
aFilterName( aInterfaceName
+ " (" + aExtension
+ ")" );
373 aDlg
.AddFilter( aFilterName
, aExtension
);
375 if( (nFlags
& 0x100) == 0x100 )
376 aDlg
.SetCurrentFilter( aFilterName
);
384 aDlg
.SetDisplayDirectory( m_sExportRecentFile
);
386 if ( aDlg
.Execute() == ERRCODE_NONE
)
388 m_sExportRecentFile
= aDlg
.GetPath();
390 Reference
< XDesktop2
> xLoader
= Desktop::create( mxContext
);
391 Reference
< XInteractionHandler2
> xInter
= InteractionHandler::createWithParent(mxContext
, nullptr);
392 Sequence
< PropertyValue
> aArguments
{ comphelper::makePropertyValue("InteractionHandler",
394 Reference
< XComponent
> xComp( xLoader
->loadComponentFromURL( m_sExportRecentFile
, "_default", 0, aArguments
) );
401 catch(const Exception
&)
403 TOOLS_WARN_EXCEPTION("filter.xslt", "");
409 void XMLFilterTestDialog::onExportCurrentDocument()
411 doExport( getFrontMostDocument( m_xFilterInfo
->maDocumentService
) );
414 void XMLFilterTestDialog::doExport( const Reference
< XComponent
>& xComp
)
418 Reference
< XStorable
> xStorable( xComp
, UNO_QUERY
);
421 utl::TempFileNamed
aTempFile(u
"", true, u
".xml");
422 OUString
aTempFileURL( aTempFile
.GetURL() );
424 const application_info_impl
* pAppInfo
= getApplicationInfo( m_xFilterInfo
->maExportService
);
427 File
aOutputFile( aTempFileURL
);
428 (void)aOutputFile
.open( osl_File_OpenFlag_Write
);
430 // create xslt exporter
431 Reference
< XOutputStream
> xIS( new comphelper::OSLOutputStreamWrapper( aOutputFile
) );
432 int bUseDocType
= m_xFilterInfo
->maDocType
.isEmpty() ? 0 : 1;
433 Sequence
< PropertyValue
> aSourceData( 2 + bUseDocType
);
434 auto pSourceData
= aSourceData
.getArray();
438 pSourceData
[i
].Name
= "OutputStream";
439 pSourceData
[i
++].Value
<<= xIS
;
441 pSourceData
[i
].Name
= "Indent";
442 pSourceData
[i
++].Value
<<= true;
446 pSourceData
[i
].Name
= "DocType_Public";
447 pSourceData
[i
++].Value
<<= m_xFilterInfo
->maDocType
;
450 Reference
< XExportFilter
> xExporter( mxContext
->getServiceManager()->createInstanceWithContext( "com.sun.star.documentconversion.XSLTFilter", mxContext
), UNO_QUERY
);
451 Reference
< XDocumentHandler
> xHandler( xExporter
, UNO_QUERY
);
454 Sequence
< OUString
> aFilterUserData( m_xFilterInfo
->getFilterUserData() );
455 xExporter
->exporter( aSourceData
, aFilterUserData
);
457 Reference
< XMultiServiceFactory
> xDocFac( xComp
, UNO_QUERY
);
459 Reference
< XEmbeddedObjectResolver
> xObjectResolver
;
460 Reference
<XGraphicStorageHandler
> xGraphicStorageHandler
;
466 xGraphicStorageHandler
.set(xDocFac
->createInstance("com.sun.star.document.ExportGraphicStorageHandler"), UNO_QUERY
);
467 xObjectResolver
.set( xDocFac
->createInstance("com.sun.star.document.ExportEmbeddedObjectResolver"), UNO_QUERY
);
469 catch( const Exception
& )
474 Sequence
< Any
> aArgs( 1 + ( xGraphicStorageHandler
.is() ? 1 : 0 ) + ( xObjectResolver
.is() ? 1 : 0 ) );
475 Any
* pArgs
= aArgs
.getArray();
476 if (xGraphicStorageHandler
.is())
477 *pArgs
++ <<= xGraphicStorageHandler
;
479 if (xObjectResolver
.is())
480 *pArgs
++ <<= xObjectResolver
;
482 // *pArgs++ <<= xInfoSet;
485 Reference
< XFilter
> xFilter( mxContext
->getServiceManager()->createInstanceWithArgumentsAndContext( pAppInfo
->maXMLExporter
, aArgs
, mxContext
), UNO_QUERY
);
488 Reference
< XExporter
> xExporter2( xFilter
, UNO_QUERY
);
489 if( xExporter2
.is() )
491 xExporter2
->setSourceDocument( xComp
);
493 Sequence
< PropertyValue
> aDescriptor
{comphelper::makePropertyValue(
494 "FileName", aTempFileURL
) };
496 if( xFilter
->filter( aDescriptor
) )
497 displayXMLFile( aTempFileURL
);
504 catch( const Exception
& )
506 TOOLS_WARN_EXCEPTION("filter.xslt", "");
510 void XMLFilterTestDialog::displayXMLFile( const OUString
& rURL
)
512 Reference
< XSystemShellExecute
> xSystemShellExecute(
513 SystemShellExecute::create(comphelper::getProcessComponentContext()) );
514 xSystemShellExecute
->execute( rURL
, OUString(), SystemShellExecuteFlags::URIS_ONLY
);
517 void XMLFilterTestDialog::onImportBrowse()
519 // Open Fileopen-Dialog
520 ::sfx2::FileDialogHelper
aDlg(
521 css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
522 FileDialogFlags::NONE
, m_xDialog
.get());
523 OUString
aFilterName( m_xFilterInfo
->maInterfaceName
);
524 OUString aExtensions
;
527 int nCurrentIndex
= 0;
528 for( int i
= 0; nLastIndex
!= -1; i
++ )
530 nLastIndex
= m_xFilterInfo
->maExtension
.indexOf( ';', nLastIndex
);
537 if( nLastIndex
== -1 )
540 aExtensions
+= m_xFilterInfo
->maExtension
.subView( nCurrentIndex
);
544 aExtensions
+= m_xFilterInfo
->maExtension
.subView( nCurrentIndex
, nLastIndex
- nCurrentIndex
);
545 nCurrentIndex
= nLastIndex
+ 1;
546 nLastIndex
= nCurrentIndex
;
550 aFilterName
+= " (" + aExtensions
+ ")";
552 aDlg
.AddFilter( aFilterName
, aExtensions
);
553 aDlg
.SetDisplayDirectory( m_sImportRecentFile
);
555 if ( aDlg
.Execute() == ERRCODE_NONE
)
557 m_sImportRecentFile
= aDlg
.GetPath();
558 import( m_sImportRecentFile
);
564 void XMLFilterTestDialog::import( const OUString
& rURL
)
568 Reference
< XDesktop2
> xLoader
= Desktop::create( mxContext
);
569 Reference
< XInteractionHandler2
> xInter
= InteractionHandler::createWithParent(mxContext
, nullptr);
571 Sequence
< PropertyValue
> aArguments
{
572 comphelper::makePropertyValue("FilterName", m_xFilterInfo
->maFilterName
),
573 comphelper::makePropertyValue("InteractionHandler", xInter
)
576 xLoader
->loadComponentFromURL( rURL
, "_default", 0, aArguments
);
578 if( m_xCBXDisplaySource
->get_active() )
580 TempFileNamed
aTempFile(u
"", true, u
".xml");
581 OUString
aTempFileURL( aTempFile
.GetURL() );
583 Reference
< XImportFilter
> xImporter( mxContext
->getServiceManager()->createInstanceWithContext( "com.sun.star.documentconversion.XSLTFilter", mxContext
), UNO_QUERY
);
586 osl::File
aInputFile( rURL
);
587 (void)aInputFile
.open( osl_File_OpenFlag_Read
);
589 Reference
< XInputStream
> xIS( new comphelper::OSLInputStreamWrapper( aInputFile
) );
591 Sequence
< PropertyValue
> aSourceData
{
592 comphelper::makePropertyValue("InputStream", xIS
),
593 comphelper::makePropertyValue("FileName", rURL
),
594 comphelper::makePropertyValue("Indent", true)
597 Reference
< XWriter
> xWriter
= Writer::create( mxContext
);
599 File
aOutputFile( aTempFileURL
);
600 (void)aOutputFile
.open( osl_File_OpenFlag_Write
);
602 Reference
< XOutputStream
> xOS( new OSLOutputStreamWrapper( aOutputFile
) );
603 xWriter
->setOutputStream( xOS
);
605 Sequence
< OUString
> aFilterUserData( m_xFilterInfo
->getFilterUserData() );
606 xImporter
->importer( aSourceData
, xWriter
, aFilterUserData
);
609 displayXMLFile( aTempFileURL
);
612 catch(const Exception
&)
614 TOOLS_WARN_EXCEPTION("filter.xslt", "");
618 IMPL_LINK(XMLFilterTestDialog
, ClickHdl_Impl
, weld::Button
&, rButton
, void )
620 if (m_xPBExportBrowse
.get() == &rButton
)
624 else if (m_xPBCurrentDocument
.get() == &rButton
)
626 onExportCurrentDocument();
628 else if (m_xPBImportBrowse
.get() == &rButton
)
632 else if (m_xPBRecentFile
.get() == &rButton
)
634 import( m_sImportRecentFile
);
636 else if (m_xPBClose
.get() == &rButton
)
638 m_xDialog
->response(RET_CLOSE
);
642 /** returns the front most open component that supports the given service */
643 Reference
< XComponent
> XMLFilterTestDialog::getFrontMostDocument( const OUString
& rServiceName
)
645 Reference
< XComponent
> xRet
;
649 Reference
< XDesktop2
> xDesktop
= Desktop::create( mxContext
);
650 Reference
< XComponent
> xTest( mxLastFocusModel
);
651 if( checkComponent( xTest
, rServiceName
) )
657 xTest
= xDesktop
->getCurrentComponent();
659 if( checkComponent( xTest
, rServiceName
) )
665 Reference
< XEnumerationAccess
> xAccess( xDesktop
->getComponents() );
668 Reference
< XEnumeration
> xEnum( xAccess
->createEnumeration() );
671 while( xEnum
->hasMoreElements() )
673 if( (xEnum
->nextElement() >>= xTest
) && xTest
.is() )
675 if( checkComponent( xTest
, rServiceName
) )
687 catch( const Exception
& )
689 TOOLS_WARN_EXCEPTION("filter.xslt", "");
695 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */