LCOV - code coverage report
Current view: top level - sw/source/ui/uno - swdetect.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 47 59 79.7 %
Date: 2014-11-03 Functions: 7 10 70.0 %
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 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             : #include "swdetect.hxx"
      21             : 
      22             : #include <cppuhelper/supportsservice.hxx>
      23             : #include <com/sun/star/container/XNameAccess.hpp>
      24             : #include <com/sun/star/io/XInputStream.hpp>
      25             : #include <sfx2/docfile.hxx>
      26             : #include <sot/storage.hxx>
      27             : #include <unotools/mediadescriptor.hxx>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : using namespace ::com::sun::star::uno;
      31             : using namespace ::com::sun::star::io;
      32             : using namespace ::com::sun::star::task;
      33             : using namespace ::com::sun::star::beans;
      34             : using namespace ::com::sun::star::lang;
      35             : using utl::MediaDescriptor;
      36             : 
      37        1688 : SwFilterDetect::SwFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
      38             : {
      39        1688 : }
      40             : 
      41        3376 : SwFilterDetect::~SwFilterDetect()
      42             : {
      43        3376 : }
      44             : 
      45        1688 : OUString SAL_CALL SwFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
      46             : {
      47        1688 :     MediaDescriptor aMediaDesc( lDescriptor );
      48        3376 :     OUString aTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
      49        3376 :     uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
      50        1688 :     if ( !xInStream.is() )
      51           0 :         return OUString();
      52             : 
      53        3376 :     SfxMedium aMedium;
      54        1688 :     aMedium.UseInteractionHandler( false );
      55        1688 :     aMedium.setStreamToLoadFrom( xInStream, true );
      56             : 
      57        1688 :     SvStream *pInStrm = aMedium.GetInStream();
      58        1688 :     if ( !pInStrm || pInStrm->GetError() )
      59           0 :         return OUString();
      60             : 
      61        1688 :     bool bIsDetected = false;
      62             : 
      63        1688 :     if ( aTypeName == "writer_Rich_Text_Format" )
      64             :     {
      65         660 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      66         660 :         bIsDetected = ( read_uInt8s_ToOString( *pInStrm, 5 ) == "{\\rtf" );
      67             :     }
      68        1028 :     else if ( aTypeName == "writer_MS_WinWord_5" )
      69             :     {
      70         146 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      71         146 :         const sal_uInt8 nBufSize = 3;
      72             :         sal_uInt8 nBuffer[ nBufSize ];
      73         146 :         if ( pInStrm->Read( nBuffer, nBufSize ) < nBufSize )
      74           0 :             return OUString();
      75             : 
      76         146 :         bIsDetected = (nBuffer[0] == 0x9B && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21)  // WinWord 1
      77         146 :                    || (nBuffer[0] == 0x9C && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21)  // PMWord 1
      78         146 :                    || (nBuffer[0] == 0xDB && nBuffer[1] == 0xA5 && nBuffer[2] == 0x2D)  // WinWord 2
      79         292 :                    || (nBuffer[0] == 0xDC && nBuffer[1] == 0xA5 && nBuffer[2] == 0x65); // WinWord 6.0/95, as a single stream file
      80             :     }
      81             :     else
      82             :     {
      83             :         // Do not attempt to create an SotStorage on a
      84             :         // 0-length stream as that would create the compound
      85             :         // document header on the stream and effectively write to
      86             :         // disk!
      87         882 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      88         882 :         if ( pInStrm->remainingSize() == 0 )
      89           0 :             return OUString();
      90             : 
      91             :         try
      92             :         {
      93         882 :             SotStorageRef aStorage = new SotStorage ( pInStrm, false );
      94         882 :             if ( !aStorage->GetError() )
      95             :             {
      96         156 :                 bIsDetected = aStorage->IsContained( "WordDocument" );
      97         156 :                 if ( bIsDetected && aTypeName.startsWith( "writer_MS_Word_97" ) )
      98         134 :                     bIsDetected = ( aStorage->IsContained("0Table") || aStorage->IsContained("1Table") );
      99         882 :             }
     100             :         }
     101           0 :         catch (...)
     102             :         {
     103           0 :             bIsDetected = false;
     104             :         }
     105             :     }
     106             : 
     107        1688 :     if ( bIsDetected )
     108         776 :         return aTypeName;
     109             : 
     110        2600 :     return OUString();
     111             : }
     112             : 
     113             : /* XServiceInfo */
     114           0 : OUString SAL_CALL SwFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
     115             : {
     116           0 :     return impl_getStaticImplementationName();
     117             : }
     118             : 
     119             : /* XServiceInfo */
     120           0 : sal_Bool SAL_CALL SwFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
     121             : {
     122           0 :     return cppu::supportsService(this, sServiceName);
     123             : }
     124             : 
     125             : /* XServiceInfo */
     126           0 : Sequence< OUString > SAL_CALL SwFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
     127             : {
     128           0 :     return impl_getStaticSupportedServiceNames();
     129             : }
     130             : 
     131             : /* Helper for XServiceInfo */
     132          20 : Sequence< OUString > SwFilterDetect::impl_getStaticSupportedServiceNames()
     133             : {
     134          20 :     Sequence< OUString > seqServiceNames( 3 );
     135          20 :     seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
     136          20 :     seqServiceNames.getArray() [1] = "com.sun.star.text.FormatDetector";
     137          20 :     seqServiceNames.getArray() [2] = "com.sun.star.text.W4WFormatDetector";
     138          20 :     return seqServiceNames ;
     139             : }
     140             : 
     141             : /* Helper for XServiceInfo */
     142          40 : OUString SwFilterDetect::impl_getStaticImplementationName()
     143             : {
     144          40 :     return OUString("com.sun.star.comp.writer.FormatDetector" );
     145             : }
     146             : 
     147             : /* Helper for registry */
     148        1688 : Reference< XInterface > SAL_CALL SwFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
     149             : {
     150        1688 :     return Reference< XInterface >( *new SwFilterDetect( xServiceManager ) );
     151             : }
     152             : 
     153             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10