LCOV - code coverage report
Current view: top level - filter/source/storagefilterdetect - filterdetect.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 53 93 57.0 %
Date: 2014-11-03 Functions: 8 12 66.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 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 "filterdetect.hxx"
      21             : 
      22             : #include <comphelper/documentconstants.hxx>
      23             : #include <comphelper/storagehelper.hxx>
      24             : #include <comphelper/types.hxx>
      25             : #include <cppuhelper/supportsservice.hxx>
      26             : #include <unotools/mediadescriptor.hxx>
      27             : #include <tools/urlobj.hxx>
      28             : #include <sfx2/brokenpackageint.hxx>
      29             : 
      30             : #include <com/sun/star/beans/XPropertySet.hpp>
      31             : #include <com/sun/star/embed/XStorage.hpp>
      32             : #include <com/sun/star/io/XInputStream.hpp>
      33             : #include <com/sun/star/packages/zip/ZipIOException.hpp>
      34             : #include <com/sun/star/task/XInteractionHandler.hpp>
      35             : 
      36             : using namespace ::com::sun::star;
      37             : using utl::MediaDescriptor;
      38             : 
      39             : namespace {
      40             : 
      41         434 : OUString getInternalFromMediaType(const OUString& aMediaType)
      42             : {
      43             :     // OpenDocument types
      44         434 :     if (      aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII )                  return OUString("writer8");
      45         148 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII )         return OUString("writer8_template");
      46         140 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII )              return OUString("writerweb8_writer_template");
      47         140 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII )           return OUString("writerglobal8");
      48         140 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_TEMPLATE_ASCII )  return OUString("writerglobal8_template");
      49         140 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII )               return OUString("draw8");
      50         138 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII )      return OUString("draw8_template");
      51         138 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII )          return OUString("impress8");
      52         134 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII ) return OUString("impress8_template");
      53         134 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII )           return OUString("calc8");
      54          18 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII )  return OUString("calc8_template");
      55          18 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII )                 return OUString("chart8");
      56          18 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII )               return OUString("math8");
      57          18 :     else if ( aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII )          return OUString("StarBaseReportChart");
      58             : 
      59             :     // OOo legacy types
      60          18 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_ASCII )           return OUString("writer_StarOffice_XML_Writer");
      61          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_TEMPLATE_ASCII )  return OUString("writer_StarOffice_XML_Writer_Template");
      62          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII )       return OUString("writer_web_StarOffice_XML_Writer_Web_Template");
      63          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII )    return OUString("writer_globaldocument_StarOffice_XML_Writer_GlobalDocument");
      64          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_ASCII )             return OUString("draw_StarOffice_XML_Draw");
      65          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_DRAW_TEMPLATE_ASCII )    return OUString("draw_StarOffice_XML_Draw_Template");
      66          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_ASCII )          return OUString("impress_StarOffice_XML_Impress");
      67          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_IMPRESS_TEMPLATE_ASCII ) return OUString("impress_StarOffice_XML_Impress_Template");
      68          16 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_ASCII )             return OUString("calc_StarOffice_XML_Calc");
      69           6 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_CALC_TEMPLATE_ASCII )    return OUString("calc_StarOffice_XML_Calc_Template");
      70           6 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_CHART_ASCII )            return OUString("chart_StarOffice_XML_Chart");
      71           0 :     else if ( aMediaType == MIMETYPE_VND_SUN_XML_MATH_ASCII )             return OUString("math_StarOffice_XML_Math");
      72             : 
      73             :     // Unknown type
      74           0 :     return OUString();
      75             : }
      76             : 
      77             : }
      78             : 
      79        4634 : StorageFilterDetect::StorageFilterDetect(const uno::Reference<uno::XComponentContext>& xCxt) :
      80        4634 :     mxCxt(xCxt) {}
      81             : 
      82        9268 : StorageFilterDetect::~StorageFilterDetect() {}
      83             : 
      84        4634 : OUString SAL_CALL StorageFilterDetect::detect(uno::Sequence<beans::PropertyValue>& rDescriptor)
      85             :     throw (uno::RuntimeException, std::exception)
      86             : {
      87        4634 :     MediaDescriptor aMediaDesc( rDescriptor );
      88        9268 :     OUString aTypeName;
      89             : 
      90             :     try
      91             :     {
      92        4634 :         uno::Reference< io::XInputStream > xInStream( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
      93        4634 :         if ( !xInStream.is() )
      94           0 :             return OUString();
      95             : 
      96        5068 :         uno::Reference< embed::XStorage > xStorage( ::comphelper::OStorageHelper::GetStorageFromInputStream( xInStream, mxCxt ), uno::UNO_QUERY );
      97         434 :         if ( !xStorage.is() )
      98           0 :             return OUString();
      99             : 
     100         868 :         uno::Reference< beans::XPropertySet > xStorageProperties( xStorage, uno::UNO_QUERY );
     101         434 :         if ( !xStorageProperties.is() )
     102           0 :             return OUString();
     103             : 
     104         868 :         OUString aMediaType;
     105         434 :         xStorageProperties->getPropertyValue( "MediaType" ) >>= aMediaType;
     106        5068 :         aTypeName = getInternalFromMediaType( aMediaType );
     107             :     }
     108             : 
     109           0 :     catch( const lang::WrappedTargetException& aWrap )
     110             :     {
     111           0 :         packages::zip::ZipIOException aZipException;
     112             :         // We don't do any type detection on broken packages (f.e. because it might be impossible),
     113             :         // so for repairing we'll use the requested type, which was detected by the flat detection.
     114           0 :         OUString aRequestedTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
     115           0 :         if ( ( aWrap.TargetException >>= aZipException ) && !aRequestedTypeName.isEmpty() )
     116             :         {
     117             :             // The package is a broken one.
     118             :             uno::Reference< task::XInteractionHandler > xInteraction =
     119           0 :                 aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INTERACTIONHANDLER(), uno::Reference< task::XInteractionHandler >() );
     120             : 
     121           0 :             if ( xInteraction.is() )
     122             :             {
     123           0 :                 INetURLObject aParser( aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() ) );
     124           0 :                 OUString aDocumentTitle = aParser.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     125           0 :                 bool bRepairPackage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
     126             :                 // fdo#46310 Don't try to repair if the user rejected it once.
     127           0 :                 bool bRepairAllowed = aMediaDesc.getUnpackedValueOrDefault( "RepairAllowed", true );
     128             : 
     129           0 :                 if ( !bRepairPackage && bRepairAllowed )
     130             :                 {
     131             :                     // Ask the user whether he wants to try to repair.
     132           0 :                     RequestPackageReparation aRequest( aDocumentTitle );
     133           0 :                     xInteraction->handle( aRequest.GetRequest() );
     134             : 
     135           0 :                     if ( aRequest.isApproved() )
     136             :                     {
     137           0 :                         aTypeName = aRequestedTypeName;
     138           0 :                         aMediaDesc[MediaDescriptor::PROP_DOCUMENTTITLE()] <<= aDocumentTitle;
     139           0 :                         aMediaDesc[MediaDescriptor::PROP_ASTEMPLATE()] <<= true;
     140           0 :                         aMediaDesc["RepairPackage"] <<= true;
     141             :                     }
     142             :                     else
     143             :                     {
     144             :                         // Repair either not allowed or not successful.
     145           0 :                         NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
     146           0 :                         xInteraction->handle( aNotifyRequest.GetRequest() );
     147           0 :                         aMediaDesc["RepairAllowed"] <<= false;
     148             :                     }
     149             : 
     150             :                     // Write the changes back.
     151           0 :                     aMediaDesc >> rDescriptor;
     152           0 :                 }
     153           0 :             }
     154           0 :         }
     155             :     }
     156           0 :     catch( uno::RuntimeException& )
     157             :     {
     158           0 :         throw;
     159             :     }
     160        4200 :     catch( uno::Exception& )
     161             :     {}
     162             : 
     163        9268 :     return aTypeName;
     164             : }
     165             : 
     166             : // XInitialization
     167           0 : void SAL_CALL StorageFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
     168           0 :     throw (uno::Exception, uno::RuntimeException, std::exception) {}
     169             : 
     170          80 : OUString StorageFilterDetect_getImplementationName()
     171             : {
     172          80 :     return OUString("com.sun.star.comp.filters.StorageFilterDetect");
     173             : }
     174             : 
     175          80 : uno::Sequence<OUString> StorageFilterDetect_getSupportedServiceNames()
     176             : {
     177          80 :     uno::Sequence<OUString> aRet(2);
     178          80 :     OUString* pArray = aRet.getArray();
     179          80 :     pArray[0] = "com.sun.star.document.ExtendedTypeDetection";
     180          80 :     pArray[1] = "com.sun.star.comp.filters.StorageFilterDetect";
     181          80 :     return aRet;
     182             : }
     183             : 
     184        4634 : uno::Reference<uno::XInterface> StorageFilterDetect_createInstance(
     185             :     const uno::Reference<uno::XComponentContext> & rCxt)
     186             : {
     187        4634 :     return static_cast<cppu::OWeakObject*>(new StorageFilterDetect(rCxt));
     188             : }
     189             : 
     190             : // XServiceInfo
     191           0 : OUString SAL_CALL StorageFilterDetect::getImplementationName()
     192             :     throw (uno::RuntimeException, std::exception)
     193             : {
     194           0 :     return StorageFilterDetect_getImplementationName();
     195             : }
     196             : 
     197           0 : sal_Bool SAL_CALL StorageFilterDetect::supportsService(const OUString& rServiceName)
     198             :     throw (uno::RuntimeException, std::exception)
     199             : {
     200           0 :     return cppu::supportsService(this, rServiceName);
     201             : }
     202             : 
     203           0 : uno::Sequence<OUString> SAL_CALL StorageFilterDetect::getSupportedServiceNames()
     204             :     throw (uno::RuntimeException, std::exception)
     205             : {
     206           0 :     return StorageFilterDetect_getSupportedServiceNames();
     207             : }
     208             : 
     209             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10