LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - exceldetect.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 85 0.0 %
Date: 2014-04-14 Functions: 0 13 0.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             : 
      10             : #include "exceldetect.hxx"
      11             : 
      12             : #include <com/sun/star/io/XInputStream.hpp>
      13             : #include <com/sun/star/ucb/XContent.hpp>
      14             : #include <cppuhelper/supportsservice.hxx>
      15             : 
      16             : #include "svl/itemset.hxx"
      17             : #include "svl/eitem.hxx"
      18             : #include "sfx2/app.hxx"
      19             : #include "sfx2/docfile.hxx"
      20             : #include "sfx2/sfxsids.hrc"
      21             : #include "unotools/mediadescriptor.hxx"
      22             : #include "sot/storage.hxx"
      23             : 
      24             : using namespace com::sun::star;
      25             : using utl::MediaDescriptor;
      26             : 
      27           0 : ScExcelBiffDetect::ScExcelBiffDetect( const uno::Reference<uno::XComponentContext>& /*xContext*/ ) {}
      28           0 : ScExcelBiffDetect::~ScExcelBiffDetect() {}
      29             : 
      30           0 : OUString ScExcelBiffDetect::getImplementationName() throw (uno::RuntimeException, std::exception)
      31             : {
      32           0 :     return impl_getStaticImplementationName();
      33             : }
      34             : 
      35           0 : sal_Bool ScExcelBiffDetect::supportsService( const OUString& aName ) throw (uno::RuntimeException, std::exception)
      36             : {
      37           0 :     return cppu::supportsService(this, aName);
      38             : }
      39             : 
      40           0 : uno::Sequence<OUString> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException, std::exception)
      41             : {
      42           0 :     return impl_getStaticSupportedServiceNames();
      43             : }
      44             : 
      45             : namespace {
      46             : 
      47           0 : bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
      48             : {
      49           0 :     SfxMedium aMedium;
      50           0 :     aMedium.UseInteractionHandler(false);
      51           0 :     aMedium.setStreamToLoadFrom(xInStream, true);
      52           0 :     SvStream* pStream = aMedium.GetInStream();
      53           0 :     if (!pStream)
      54           0 :         return false;
      55             : 
      56           0 :     pStream->Seek(STREAM_SEEK_TO_END);
      57           0 :     sal_Size nSize = pStream->Tell();
      58           0 :     pStream->Seek(0);
      59             : 
      60           0 :     if (!nSize)
      61             :         // 0-size stream.  Failed.
      62           0 :         return false;
      63             : 
      64           0 :     SotStorageRef xStorage = new SotStorage(pStream, false);
      65           0 :     if (!xStorage.Is() || xStorage->GetError())
      66           0 :         return false;
      67             : 
      68           0 :     return xStorage->IsStream(rName);
      69             : }
      70             : 
      71             : /**
      72             :  * We detect BIFF 2, 3 and 4 file types together since the only thing that
      73             :  * set them apart is the BOF ID.
      74             :  */
      75           0 : bool isExcel40(const uno::Reference<io::XInputStream>& xInStream)
      76             : {
      77           0 :     SfxMedium aMedium;
      78           0 :     aMedium.UseInteractionHandler(false);
      79           0 :     aMedium.setStreamToLoadFrom(xInStream, true);
      80           0 :     SvStream* pStream = aMedium.GetInStream();
      81           0 :     if (!pStream)
      82           0 :         return false;
      83             : 
      84           0 :     pStream->Seek(STREAM_SEEK_TO_END);
      85           0 :     sal_Size nSize = pStream->Tell();
      86           0 :     pStream->Seek(0);
      87             : 
      88           0 :     if (nSize < 4)
      89           0 :         return false;
      90             : 
      91             :     sal_uInt16 nBofId, nBofSize;
      92           0 :     pStream->ReadUInt16( nBofId ).ReadUInt16( nBofSize );
      93             : 
      94           0 :     switch (nBofId)
      95             :     {
      96             :         case 0x0009: // Excel 2.1 worksheet (BIFF 2)
      97             :         case 0x0209: // Excel 3.0 worksheet (BIFF 3)
      98             :         case 0x0409: // Excel 4.0 worksheet (BIFF 4)
      99             :         case 0x0809: // Excel 5.0 worksheet (BIFF 5), some apps create such files (fdo#70100)
     100           0 :             break;
     101             :         default:
     102           0 :             return false;
     103             :     }
     104             : 
     105           0 :     if (nBofSize < 4 || 16 < nBofSize)
     106             :         // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
     107           0 :         return false;
     108             : 
     109           0 :     sal_Size nPos = pStream->Tell();
     110           0 :     if (nSize - nPos < nBofSize)
     111             :         // BOF record doesn't have required bytes.
     112           0 :         return false;
     113             : 
     114           0 :     return true;
     115             : }
     116             : 
     117           0 : bool isTemplate(const OUString& rType)
     118             : {
     119           0 :     return rType.indexOf("_VorlageTemplate") != -1;
     120             : }
     121             : 
     122             : }
     123             : 
     124           0 : OUString ScExcelBiffDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
     125             :     throw (uno::RuntimeException, std::exception)
     126             : {
     127           0 :     MediaDescriptor aMediaDesc(lDescriptor);
     128           0 :     OUString aType;
     129           0 :     aMediaDesc[MediaDescriptor::PROP_TYPENAME()] >>= aType;
     130           0 :     if (aType.isEmpty())
     131             :         // Type is not given.  We can't proceed.
     132           0 :         return OUString();
     133             : 
     134           0 :     aMediaDesc.addInputStream();
     135           0 :     uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY);
     136           0 :     if (!xInStream.is())
     137             :         // No input stream.
     138           0 :         return OUString();
     139             : 
     140           0 :     if (aType == "calc_MS_Excel_97" || aType == "calc_MS_Excel_97_VorlageTemplate")
     141             :     {
     142             :         // See if this stream is a Excel 97/XP/2003 (BIFF8) stream.
     143           0 :         if (!hasStream(xInStream, "Workbook"))
     144             :             // BIFF8 is expected to contain a stream named "Workbook".
     145           0 :             return OUString();
     146             : 
     147           0 :         aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
     148             :     }
     149             : 
     150           0 :     else if (aType == "calc_MS_Excel_95" || aType == "calc_MS_Excel_95_VorlageTemplate")
     151             :     {
     152             :         // See if this stream is a Excel 95 (BIFF5) stream.
     153           0 :         if (!hasStream(xInStream, "Book"))
     154           0 :             return OUString();
     155             : 
     156           0 :         aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
     157             :     }
     158             : 
     159           0 :     else if (aType == "calc_MS_Excel_5095" || aType == "calc_MS_Excel_5095_VorlageTemplate")
     160             :     {
     161             :         // See if this stream is a Excel 5.0/95 stream.
     162           0 :         if (!hasStream(xInStream, "Book"))
     163           0 :             return OUString();
     164             : 
     165           0 :         aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
     166             :     }
     167             : 
     168           0 :     else if (aType == "calc_MS_Excel_40" || aType == "calc_MS_Excel_40_VorlageTemplate")
     169             :     {
     170             :         // See if this stream is a Excel 4.0 stream.
     171           0 :         if (!isExcel40(xInStream))
     172           0 :             return OUString();
     173             : 
     174           0 :         aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
     175             :     }
     176             : 
     177             :     else
     178             :         // Nothing to detect.
     179           0 :         return OUString();
     180             : 
     181           0 :     aMediaDesc >> lDescriptor;
     182           0 :     return aType;
     183             : }
     184             : 
     185           0 : uno::Sequence<OUString> ScExcelBiffDetect::impl_getStaticSupportedServiceNames()
     186             : {
     187           0 :     uno::Sequence<OUString> aNames(1);
     188           0 :     aNames[0] = "com.sun.star.frame.ExtendedTypeDetection";
     189           0 :     return aNames;
     190             : }
     191             : 
     192           0 : OUString ScExcelBiffDetect::impl_getStaticImplementationName()
     193             : {
     194           0 :     return OUString("com.sun.star.comp.calc.ExcelBiffFormatDetector");
     195             : }
     196             : 
     197           0 : uno::Reference<uno::XInterface> ScExcelBiffDetect::impl_createInstance(
     198             :     const uno::Reference<uno::XComponentContext>& xContext )
     199             :         throw (com::sun::star::uno::Exception)
     200             : {
     201           0 :     return static_cast<cppu::OWeakObject*>(new ScExcelBiffDetect(xContext));
     202             : }
     203             : 
     204             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10