LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/writerperfect/source/writer - WordPerfectImportFilter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 3 175 1.7 %
Date: 2013-07-09 Functions: 3 31 9.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       6             :  */
       7             : /* "This product is not manufactured, approved, or supported by
       8             :  * Corel Corporation or Corel Corporation Limited."
       9             :  */
      10             : 
      11             : #include <osl/diagnose.h>
      12             : #include <rtl/tencinfo.h>
      13             : 
      14             : #include <com/sun/star/io/XInputStream.hpp>
      15             : #include <com/sun/star/xml/sax/XAttributeList.hpp>
      16             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      17             : #include <com/sun/star/xml/sax/InputSource.hpp>
      18             : #include <com/sun/star/xml/sax/XParser.hpp>
      19             : #include <com/sun/star/io/XSeekable.hpp>
      20             : #include <com/sun/star/uno/Reference.h>
      21             : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
      22             : 
      23             : #include <comphelper/componentcontext.hxx>
      24             : #include <xmloff/attrlist.hxx>
      25             : #include <sfx2/passwd.hxx>
      26             : #include <ucbhelper/content.hxx>
      27             : 
      28             : #include <libwpd/libwpd.h>
      29             : #include <libodfgen/libodfgen.hxx>
      30             : 
      31             : #include "common/DocumentHandler.hxx"
      32             : #include "common/WPXSvStream.hxx"
      33             : #include "WordPerfectImportFilter.hxx"
      34             : 
      35             : using ::ucbhelper::Content;
      36             : using com::sun::star::uno::Sequence;
      37             : using com::sun::star::uno::Reference;
      38             : using com::sun::star::uno::Any;
      39             : using com::sun::star::uno::UNO_QUERY;
      40             : using com::sun::star::uno::XInterface;
      41             : using com::sun::star::uno::Exception;
      42             : using com::sun::star::uno::RuntimeException;
      43             : using com::sun::star::uno::XComponentContext;
      44             : using com::sun::star::beans::PropertyValue;
      45             : using com::sun::star::document::XFilter;
      46             : using com::sun::star::document::XExtendedFilterDetection;
      47             : using com::sun::star::ucb::XCommandEnvironment;
      48             : 
      49             : using com::sun::star::io::XInputStream;
      50             : using com::sun::star::document::XImporter;
      51             : using com::sun::star::xml::sax::InputSource;
      52             : using com::sun::star::xml::sax::XAttributeList;
      53             : using com::sun::star::xml::sax::XDocumentHandler;
      54             : using com::sun::star::xml::sax::XParser;
      55             : 
      56             : 
      57           0 : static bool handleEmbeddedWPGObject(const WPXBinaryData &data, OdfDocumentHandler *pHandler,  const OdfStreamType streamType)
      58             : {
      59           0 :     OdgGenerator exporter(pHandler, streamType);
      60             : 
      61           0 :     libwpg::WPGFileFormat fileFormat = libwpg::WPG_AUTODETECT;
      62             : 
      63           0 :     if (!libwpg::WPGraphics::isSupported(const_cast<WPXInputStream *>(data.getDataStream())))
      64           0 :         fileFormat = libwpg::WPG_WPG1;
      65             : 
      66           0 :     return libwpg::WPGraphics::parse(const_cast<WPXInputStream *>(data.getDataStream()), &exporter, fileFormat);
      67             : }
      68             : 
      69           0 : static bool handleEmbeddedWPGImage(const WPXBinaryData &input, WPXBinaryData &output)
      70             : {
      71           0 :     WPXString svgOutput;
      72           0 :     libwpg::WPGFileFormat fileFormat = libwpg::WPG_AUTODETECT;
      73             : 
      74           0 :     if (!libwpg::WPGraphics::isSupported(const_cast<WPXInputStream *>(input.getDataStream())))
      75           0 :         fileFormat = libwpg::WPG_WPG1;
      76             : 
      77           0 :     if (!libwpg::WPGraphics::generateSVG(const_cast<WPXInputStream *>(input.getDataStream()), svgOutput, fileFormat))
      78           0 :         return false;
      79             : 
      80           0 :     output.clear();
      81           0 :     output.append((unsigned char *)svgOutput.cstr(), strlen(svgOutput.cstr()));
      82           0 :     return true;
      83             : }
      84             : 
      85           0 : sal_Bool SAL_CALL WordPerfectImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
      86             : throw (RuntimeException)
      87             : {
      88             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::importImpl");
      89             : 
      90           0 :     sal_Int32 nLength = aDescriptor.getLength();
      91           0 :     const PropertyValue *pValue = aDescriptor.getConstArray();
      92           0 :     Reference < XInputStream > xInputStream;
      93           0 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
      94             :     {
      95           0 :         if ( pValue[i].Name == "InputStream" )
      96           0 :             pValue[i].Value >>= xInputStream;
      97             :     }
      98           0 :     if ( !xInputStream.is() )
      99             :     {
     100             :         OSL_ASSERT( 0 );
     101           0 :         return sal_False;
     102             :     }
     103             : 
     104           0 :     WPXSvInputStream input( xInputStream );
     105             : 
     106           0 :     OString aUtf8Passwd;
     107             : 
     108           0 :     WPDConfidence confidence = WPDocument::isFileFormatSupported(&input);
     109             : 
     110           0 :     if (WPD_CONFIDENCE_SUPPORTED_ENCRYPTION == confidence)
     111             :     {
     112           0 :         int unsuccessfulAttempts = 0;
     113             :         while (true )
     114             :         {
     115           0 :             SfxPasswordDialog aPasswdDlg( 0 );
     116           0 :             aPasswdDlg.SetMinLen(0);
     117           0 :             if(!aPasswdDlg.Execute())
     118           0 :                 return sal_False;
     119           0 :             OUString aPasswd = aPasswdDlg.GetPassword();
     120           0 :             aUtf8Passwd = OUStringToOString(aPasswd, RTL_TEXTENCODING_UTF8);
     121           0 :             if (WPD_PASSWORD_MATCH_OK == WPDocument::verifyPassword(&input, aUtf8Passwd.getStr()))
     122           0 :                 break;
     123             :             else
     124           0 :                 unsuccessfulAttempts++;
     125           0 :             if (unsuccessfulAttempts == 3) // timeout after 3 password atempts
     126           0 :                 return sal_False;
     127           0 :         }
     128             :     }
     129             : 
     130             :     // An XML import service: what we push sax messages to..
     131           0 :     OUString sXMLImportService (  "com.sun.star.comp.Writer.XMLOasisImporter"  );
     132           0 :     Reference < XDocumentHandler > xInternalHandler( comphelper::ComponentContext( mxContext ).createComponent( sXMLImportService ), UNO_QUERY );
     133             : 
     134             :     // The XImporter sets up an empty target document for XDocumentHandler to write to..
     135           0 :     Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
     136           0 :     xImporter->setTargetDocument(mxDoc);
     137             : 
     138             :     // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
     139             :     // writes to in-memory target doc
     140           0 :     DocumentHandler xHandler(xInternalHandler);
     141             : 
     142           0 :     OdtGenerator collector(&xHandler, ODF_FLAT_XML);
     143           0 :         collector.registerEmbeddedObjectHandler("image/x-wpg", &handleEmbeddedWPGObject);
     144           0 :         collector.registerEmbeddedImageHandler("image/x-wpg", &handleEmbeddedWPGImage);
     145           0 :     if (WPD_OK == WPDocument::parse(&input, &collector, aUtf8Passwd.isEmpty() ? 0 : aUtf8Passwd.getStr()))
     146           0 :         return sal_True;
     147           0 :     return sal_False;
     148             : }
     149             : 
     150           0 : sal_Bool SAL_CALL WordPerfectImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
     151             : throw (RuntimeException)
     152             : {
     153             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::filter");
     154           0 :     return importImpl ( aDescriptor );
     155             : }
     156           0 : void SAL_CALL WordPerfectImportFilter::cancel(  )
     157             : throw (RuntimeException)
     158             : {
     159             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::cancel");
     160           0 : }
     161             : 
     162             : // XImporter
     163           0 : void SAL_CALL WordPerfectImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
     164             : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
     165             : {
     166             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::getTargetDocument");
     167           0 :     mxDoc = xDoc;
     168           0 : }
     169             : 
     170             : // XExtendedFilterDetection
     171           0 : OUString SAL_CALL WordPerfectImportFilter::detect( Sequence< PropertyValue >& Descriptor )
     172             : throw( RuntimeException )
     173             : {
     174             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::detect");
     175             : 
     176           0 :     WPDConfidence confidence = WPD_CONFIDENCE_NONE;
     177           0 :     OUString sTypeName;
     178           0 :     sal_Int32 nLength = Descriptor.getLength();
     179           0 :     sal_Int32 location = nLength;
     180           0 :     const PropertyValue *pValue = Descriptor.getConstArray();
     181           0 :     Reference < XInputStream > xInputStream;
     182           0 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
     183             :     {
     184           0 :         if ( pValue[i].Name == "TypeName" )
     185           0 :             location=i;
     186           0 :         else if ( pValue[i].Name == "InputStream" )
     187           0 :             pValue[i].Value >>= xInputStream;
     188             :     }
     189             : 
     190           0 :     if (!xInputStream.is())
     191           0 :         return OUString();
     192             : 
     193           0 :     WPXSvInputStream input( xInputStream );
     194             : 
     195           0 :     confidence = WPDocument::isFileFormatSupported(&input);
     196             : 
     197           0 :     if (confidence == WPD_CONFIDENCE_EXCELLENT || confidence == WPD_CONFIDENCE_SUPPORTED_ENCRYPTION)
     198           0 :         sTypeName = "writer_WordPerfect_Document";
     199             : 
     200           0 :     if (!sTypeName.isEmpty())
     201             :     {
     202           0 :         if ( location == nLength )
     203             :         {
     204           0 :             Descriptor.realloc(nLength+1);
     205           0 :             Descriptor[location].Name = "TypeName";
     206             :         }
     207             : 
     208           0 :         Descriptor[location].Value <<=sTypeName;
     209             :     }
     210             : 
     211           0 :     return sTypeName;
     212             : }
     213             : 
     214             : 
     215             : // XInitialization
     216           0 : void SAL_CALL WordPerfectImportFilter::initialize( const Sequence< Any >& aArguments )
     217             : throw (Exception, RuntimeException)
     218             : {
     219             :     SAL_INFO("writerperfect", "WordPerfectImportFilter::initialize");
     220           0 :     Sequence < PropertyValue > aAnySeq;
     221           0 :     sal_Int32 nLength = aArguments.getLength();
     222           0 :     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     223             :     {
     224           0 :         const PropertyValue *pValue = aAnySeq.getConstArray();
     225           0 :         nLength = aAnySeq.getLength();
     226           0 :         for ( sal_Int32 i = 0 ; i < nLength; i++)
     227             :         {
     228           0 :             if ( pValue[i].Name == "Type" )
     229             :             {
     230           0 :                 pValue[i].Value >>= msFilterName;
     231           0 :                 break;
     232             :             }
     233             :         }
     234           0 :     }
     235           0 : }
     236           2 : OUString WordPerfectImportFilter_getImplementationName ()
     237             : throw (RuntimeException)
     238             : {
     239           2 :     return OUString (  "com.sun.star.comp.Writer.WordPerfectImportFilter"  );
     240             : }
     241             : 
     242             : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
     243             : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
     244           0 : sal_Bool SAL_CALL WordPerfectImportFilter_supportsService( const OUString &ServiceName )
     245             : throw (RuntimeException)
     246             : {
     247           0 :     return ( ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
     248             : }
     249           0 : Sequence< OUString > SAL_CALL WordPerfectImportFilter_getSupportedServiceNames(  )
     250             : throw (RuntimeException)
     251             : {
     252           0 :     Sequence < OUString > aRet(2);
     253           0 :     OUString *pArray = aRet.getArray();
     254           0 :     pArray[0] =  OUString (  SERVICE_NAME1  );
     255           0 :     pArray[1] =  OUString (  SERVICE_NAME2  );
     256           0 :     return aRet;
     257             : }
     258             : #undef SERVICE_NAME2
     259             : #undef SERVICE_NAME1
     260             : 
     261           0 : Reference< XInterface > SAL_CALL WordPerfectImportFilter_createInstance( const Reference< XComponentContext > & rContext)
     262             : throw( Exception )
     263             : {
     264           0 :     return (cppu::OWeakObject *) new WordPerfectImportFilter( rContext );
     265             : }
     266             : 
     267             : // XServiceInfo
     268           0 : OUString SAL_CALL WordPerfectImportFilter::getImplementationName(  )
     269             : throw (RuntimeException)
     270             : {
     271           0 :     return WordPerfectImportFilter_getImplementationName();
     272             : }
     273           0 : sal_Bool SAL_CALL WordPerfectImportFilter::supportsService( const OUString &rServiceName )
     274             : throw (RuntimeException)
     275             : {
     276           0 :     return WordPerfectImportFilter_supportsService( rServiceName );
     277             : }
     278           0 : Sequence< OUString > SAL_CALL WordPerfectImportFilter::getSupportedServiceNames(  )
     279             : throw (RuntimeException)
     280             : {
     281           0 :     return WordPerfectImportFilter_getSupportedServiceNames();
     282             : }
     283             : 
     284             : 
     285           0 : WordPerfectImportFilterDialog::WordPerfectImportFilterDialog( const Reference< XComponentContext > & rContext) :
     286           0 :     mxContext( rContext ) {}
     287             : 
     288           0 : WordPerfectImportFilterDialog::~WordPerfectImportFilterDialog()
     289             : {
     290           0 : }
     291             : 
     292           0 : void SAL_CALL WordPerfectImportFilterDialog::setTitle( const OUString & )
     293             : throw (RuntimeException)
     294             : {
     295           0 : }
     296             : 
     297           0 : sal_Int16 SAL_CALL WordPerfectImportFilterDialog::execute()
     298             : throw (RuntimeException)
     299             : {
     300           0 :     WPXSvInputStream input( mxInputStream );
     301             : 
     302           0 :     OString aUtf8Passwd;
     303             : 
     304           0 :     WPDConfidence confidence = WPDocument::isFileFormatSupported(&input);
     305             : 
     306           0 :     if (WPD_CONFIDENCE_SUPPORTED_ENCRYPTION == confidence)
     307             :     {
     308           0 :         int unsuccessfulAttempts = 0;
     309             :         while (true )
     310             :         {
     311           0 :             SfxPasswordDialog aPasswdDlg(0);
     312           0 :             aPasswdDlg.SetMinLen(0);
     313           0 :             if(!aPasswdDlg.Execute())
     314           0 :                 return com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL;
     315           0 :             msPassword = aPasswdDlg.GetPassword().GetBuffer();
     316           0 :             aUtf8Passwd = OUStringToOString(msPassword, RTL_TEXTENCODING_UTF8);
     317           0 :             if (WPD_PASSWORD_MATCH_OK == WPDocument::verifyPassword(&input, aUtf8Passwd.getStr()))
     318           0 :                 break;
     319             :             else
     320           0 :                 unsuccessfulAttempts++;
     321           0 :             if (unsuccessfulAttempts == 3) // timeout after 3 password atempts
     322           0 :                 return com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL;
     323           0 :         }
     324             :     }
     325           0 :     return com::sun::star::ui::dialogs::ExecutableDialogResults::OK;
     326             : }
     327             : 
     328           0 : Sequence<PropertyValue> SAL_CALL WordPerfectImportFilterDialog::getPropertyValues() throw(RuntimeException)
     329             : {
     330           0 :     Sequence<PropertyValue> aRet(1);
     331           0 :     PropertyValue *pArray = aRet.getArray();
     332             : 
     333           0 :     pArray[0].Name = "Password";
     334           0 :     pArray[0].Value <<= msPassword;
     335             : 
     336           0 :     return aRet;
     337             : }
     338             : 
     339           0 : void SAL_CALL WordPerfectImportFilterDialog::setPropertyValues( const Sequence<PropertyValue>& aProps)
     340             : throw(com::sun::star::beans::UnknownPropertyException, com::sun::star::beans::PropertyVetoException,
     341             :       com::sun::star::lang::IllegalArgumentException, com::sun::star::lang::WrappedTargetException, RuntimeException)
     342             : {
     343           0 :     const PropertyValue *pPropArray = aProps.getConstArray();
     344           0 :     long nPropCount = aProps.getLength();
     345           0 :     for (long i = 0; i < nPropCount; i++)
     346             :     {
     347           0 :         const PropertyValue &rProp = pPropArray[i];
     348           0 :         OUString aPropName = rProp.Name;
     349             : 
     350           0 :         if ( aPropName == "Password" )
     351           0 :             rProp.Value >>= msPassword;
     352           0 :         else if ( aPropName == "InputStream" )
     353           0 :             rProp.Value >>= mxInputStream;
     354           0 :     }
     355           0 : }
     356             : 
     357             : 
     358             : // XServiceInfo
     359           0 : OUString SAL_CALL WordPerfectImportFilterDialog::getImplementationName(  )
     360             : throw (RuntimeException)
     361             : {
     362           0 :     return WordPerfectImportFilterDialog_getImplementationName();
     363             : }
     364             : 
     365           0 : sal_Bool SAL_CALL WordPerfectImportFilterDialog::supportsService( const OUString &rServiceName )
     366             : throw (RuntimeException)
     367             : {
     368           0 :     return WordPerfectImportFilterDialog_supportsService( rServiceName );
     369             : }
     370             : 
     371           0 : Sequence< OUString > SAL_CALL WordPerfectImportFilterDialog::getSupportedServiceNames(  )
     372             : throw (RuntimeException)
     373             : {
     374           0 :     return WordPerfectImportFilterDialog_getSupportedServiceNames();
     375             : }
     376             : 
     377           0 : OUString WordPerfectImportFilterDialog_getImplementationName ()
     378             : throw (RuntimeException)
     379             : {
     380           0 :     return OUString (  "com.sun.star.comp.Writer.WordPerfectImportFilterDialog"  );
     381             : }
     382             : 
     383             : #define SERVICE_NAME "com.sun.star.ui.dialogs.FilterOptionsDialog"
     384           0 : sal_Bool SAL_CALL WordPerfectImportFilterDialog_supportsService( const OUString &ServiceName )
     385             : throw (RuntimeException)
     386             : {
     387           0 :     return ( ServiceName == SERVICE_NAME );
     388             : }
     389             : 
     390           0 : Sequence< OUString > SAL_CALL WordPerfectImportFilterDialog_getSupportedServiceNames(  )
     391             : throw (RuntimeException)
     392             : {
     393           0 :     Sequence < OUString > aRet(1);
     394           0 :     OUString *pArray = aRet.getArray();
     395           0 :     pArray[0] =  OUString (  SERVICE_NAME  );
     396           0 :     return aRet;
     397             : }
     398             : #undef SERVICE_NAME
     399             : 
     400           0 : Reference< XInterface > SAL_CALL WordPerfectImportFilterDialog_createInstance( const Reference< XComponentContext > & rContext)
     401             : throw( Exception )
     402             : {
     403           0 :     return (cppu::OWeakObject *) new WordPerfectImportFilterDialog( rContext );
     404           6 : }
     405             : 
     406             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10