LCOV - code coverage report
Current view: top level - sw/source/ui/uno - swdetect.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 39 55 70.9 %
Date: 2015-06-13 12:38:46 Functions: 5 8 62.5 %
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        1168 : SwFilterDetect::SwFilterDetect()
      38             : {
      39        1168 : }
      40             : 
      41        2336 : SwFilterDetect::~SwFilterDetect()
      42             : {
      43        2336 : }
      44             : 
      45        1168 : OUString SAL_CALL SwFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
      46             : {
      47        1168 :     MediaDescriptor aMediaDesc( lDescriptor );
      48        2336 :     OUString aTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
      49        2336 :     uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
      50        1168 :     if ( !xInStream.is() )
      51           0 :         return OUString();
      52             : 
      53        2336 :     SfxMedium aMedium;
      54        1168 :     aMedium.UseInteractionHandler( false );
      55        1168 :     aMedium.setStreamToLoadFrom( xInStream, true );
      56             : 
      57        1168 :     SvStream *pInStrm = aMedium.GetInStream();
      58        1168 :     if ( !pInStrm || pInStrm->GetError() )
      59           0 :         return OUString();
      60             : 
      61        1168 :     bool bIsDetected = false;
      62             : 
      63        1168 :     if ( aTypeName == "writer_Rich_Text_Format" )
      64             :     {
      65         425 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      66         425 :         bIsDetected = ( read_uInt8s_ToOString( *pInStrm, 5 ) == "{\\rtf" );
      67             :     }
      68         743 :     else if ( aTypeName == "writer_MS_WinWord_5" )
      69             :     {
      70         105 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      71         105 :         const sal_uInt8 nBufSize = 3;
      72             :         sal_uInt8 nBuffer[ nBufSize ];
      73         105 :         if ( pInStrm->Read( nBuffer, nBufSize ) < nBufSize )
      74           0 :             return OUString();
      75             : 
      76         105 :         bIsDetected = (nBuffer[0] == 0x9B && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21)  // WinWord 1
      77         105 :                    || (nBuffer[0] == 0x9C && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21)  // PMWord 1
      78         105 :                    || (nBuffer[0] == 0xDB && nBuffer[1] == 0xA5 && nBuffer[2] == 0x2D)  // WinWord 2
      79         210 :                    || (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         638 :         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
      88         638 :         if ( pInStrm->remainingSize() == 0 )
      89           0 :             return OUString();
      90             : 
      91             :         try
      92             :         {
      93         638 :             tools::SvRef<SotStorage> aStorage = new SotStorage ( pInStrm, false );
      94         638 :             if ( !aStorage->GetError() )
      95             :             {
      96         110 :                 bIsDetected = aStorage->IsContained( "WordDocument" );
      97         110 :                 if ( bIsDetected && aTypeName.startsWith( "writer_MS_Word_97" ) )
      98          99 :                     bIsDetected = ( aStorage->IsContained("0Table") || aStorage->IsContained("1Table") );
      99         638 :             }
     100             :         }
     101           0 :         catch (...)
     102             :         {
     103           0 :             bIsDetected = false;
     104             :         }
     105             :     }
     106             : 
     107        1168 :     if ( bIsDetected )
     108         510 :         return aTypeName;
     109             : 
     110        1826 :     return OUString();
     111             : }
     112             : 
     113             : /* XServiceInfo */
     114           0 : OUString SAL_CALL SwFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
     115             : {
     116           0 :     return OUString("com.sun.star.comp.writer.FormatDetector" );
     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 :     Sequence< OUString > seqServiceNames( 3 );
     129           0 :     seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
     130           0 :     seqServiceNames.getArray() [1] = "com.sun.star.text.FormatDetector";
     131           0 :     seqServiceNames.getArray() [2] = "com.sun.star.text.W4WFormatDetector";
     132           0 :     return seqServiceNames ;
     133             : }
     134             : 
     135             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     136        1168 : com_sun_star_comp_writer_FormatDetector_get_implementation(::com::sun::star::uno::XComponentContext*,
     137             :                                                            ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     138             : {
     139        1168 :     return cppu::acquire(new SwFilterDetect());
     140             : }
     141             : 
     142             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11