LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/package/source/xstor - xfactory.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 83 115 72.2 %
Date: 2013-07-09 Functions: 6 9 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 <com/sun/star/ucb/SimpleFileAccess.hpp>
      21             : #include <com/sun/star/embed/ElementModes.hpp>
      22             : #include <com/sun/star/embed/StorageFormats.hpp>
      23             : #include <com/sun/star/beans/PropertyValue.hpp>
      24             : #include <com/sun/star/io/TempFile.hpp>
      25             : #include <com/sun/star/io/XSeekable.hpp>
      26             : 
      27             : #include <comphelper/processfactory.hxx>
      28             : #include <comphelper/storagehelper.hxx>
      29             : 
      30             : #include "xfactory.hxx"
      31             : #include "xstorage.hxx"
      32             : 
      33             : 
      34             : using namespace ::com::sun::star;
      35             : 
      36             : //-------------------------------------------------------------------------
      37        2389 : sal_Bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
      38             :                                      const uno::Reference< io::XSeekable >& xSeekable )
      39             : {
      40        2389 :     if ( !xInputStream.is() || !xSeekable.is() )
      41           0 :         throw uno::RuntimeException();
      42             : 
      43        2389 :     if ( xSeekable->getLength() )
      44             :     {
      45        2204 :         uno::Sequence< sal_Int8 > aData( 4 );
      46        2204 :         xSeekable->seek( 0 );
      47        2204 :         sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
      48        2204 :         xSeekable->seek( 0 );
      49             : 
      50             :         // TODO/LATER: should the disk spanned files be supported?
      51             :         // 0x50, 0x4b, 0x07, 0x08
      52        2204 :         return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
      53             :     }
      54             :     else
      55         185 :         return sal_True; // allow to create a storage based on empty stream
      56             : }
      57             : 
      58             : //-------------------------------------------------------------------------
      59          68 : uno::Sequence< OUString > SAL_CALL OStorageFactory::impl_staticGetSupportedServiceNames()
      60             : {
      61          68 :     uno::Sequence< OUString > aRet(2);
      62          68 :     aRet[0] = "com.sun.star.embed.StorageFactory";
      63          68 :     aRet[1] = "com.sun.star.comp.embed.StorageFactory";
      64          68 :     return aRet;
      65             : }
      66             : 
      67             : //-------------------------------------------------------------------------
      68         136 : OUString SAL_CALL OStorageFactory::impl_staticGetImplementationName()
      69             : {
      70         136 :     return OUString("com.sun.star.comp.embed.StorageFactory");
      71             : }
      72             : 
      73             : //-------------------------------------------------------------------------
      74          68 : uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::impl_staticCreateSelfInstance(
      75             :             const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
      76             : {
      77          68 :     return uno::Reference< uno::XInterface >( *new OStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
      78             : }
      79             : 
      80             : //-------------------------------------------------------------------------
      81        1401 : uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
      82             :     throw ( uno::Exception,
      83             :             uno::RuntimeException )
      84             : {
      85             :     // TODO: reimplement TempStream service to support XStream interface
      86             :     uno::Reference < io::XStream > xTempStream(
      87             :                         io::TempFile::create(m_xContext),
      88        1401 :                         uno::UNO_QUERY_THROW );
      89             : 
      90             :     return uno::Reference< uno::XInterface >(
      91             :                 static_cast< OWeakObject* >( new OStorage(  xTempStream,
      92             :                                                             embed::ElementModes::READWRITE,
      93             :                                                             uno::Sequence< beans::PropertyValue >(),
      94             :                                                             m_xContext,
      95        2802 :                                                             embed::StorageFormats::PACKAGE ) ),
      96        2802 :                 uno::UNO_QUERY );
      97             : }
      98             : 
      99             : //-------------------------------------------------------------------------
     100        2389 : uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
     101             :             const uno::Sequence< uno::Any >& aArguments )
     102             :     throw ( uno::Exception,
     103             :             uno::RuntimeException )
     104             : {
     105             :     // The request for storage can be done with up to three arguments
     106             : 
     107             :     // The first argument specifies a source for the storage
     108             :     // it can be URL, XStream, XInputStream.
     109             :     // The second value is a mode the storage should be open in.
     110             :     // And the third value is a media descriptor.
     111             : 
     112        2389 :     sal_Int32 nArgNum = aArguments.getLength();
     113             :     OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
     114             : 
     115        2389 :     if ( !nArgNum )
     116           0 :         return createInstance();
     117             : 
     118             :     // first try to retrieve storage open mode if any
     119             :     // by default the storage will be open in readonly mode
     120        2389 :     sal_Int32 nStorageMode = embed::ElementModes::READ;
     121        2389 :     if ( nArgNum >= 2 )
     122             :     {
     123        2389 :         if( !( aArguments[1] >>= nStorageMode ) )
     124             :         {
     125             :             OSL_FAIL( "Wrong second argument!\n" );
     126           0 :             throw lang::IllegalArgumentException(); // TODO:
     127             :         }
     128             :         // it's always possible to read written storage in this implementation
     129        2389 :         nStorageMode |= embed::ElementModes::READ;
     130             :     }
     131             : 
     132        2389 :     if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
     133         146 :       && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
     134           0 :         throw lang::IllegalArgumentException(); // TODO:
     135             : 
     136             :     // retrieve storage source stream
     137        2389 :     OUString aURL;
     138        4778 :     uno::Reference< io::XStream > xStream;
     139        4778 :     uno::Reference< io::XInputStream > xInputStream;
     140             : 
     141        2389 :     if ( aArguments[0] >>= aURL )
     142             :     {
     143          94 :         if ( aURL.isEmpty() )
     144             :         {
     145             :             OSL_FAIL( "Empty URL is provided!\n" );
     146           0 :             throw lang::IllegalArgumentException(); // TODO:
     147             :         }
     148             : 
     149          94 :         if ( aURL.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
     150             :         {
     151             :             OSL_FAIL( "Packages URL's are not valid for storages!\n" ); // ???
     152           0 :             throw lang::IllegalArgumentException(); // TODO:
     153             :         }
     154             : 
     155             :         uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
     156             :             ucb::SimpleFileAccess::create(
     157          94 :                 m_xContext ) );
     158             : 
     159          94 :         if ( nStorageMode & embed::ElementModes::WRITE )
     160          89 :             xStream = xTempAccess->openFileReadWrite( aURL );
     161             :         else
     162           5 :             xInputStream = xTempAccess->openFileRead( aURL );
     163             :     }
     164        2295 :     else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
     165             :     {
     166             :         OSL_FAIL( "Wrong first argument!\n" );
     167           0 :         throw uno::Exception(); // TODO: Illegal argument
     168             :     }
     169             : 
     170             :     // retrieve mediadescriptor and set storage properties
     171        4778 :     uno::Sequence< beans::PropertyValue > aDescr;
     172        4778 :     uno::Sequence< beans::PropertyValue > aPropsToSet;
     173             : 
     174        2389 :     sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
     175             : 
     176        2389 :     if ( nArgNum >= 3 )
     177             :     {
     178        1509 :         if( aArguments[2] >>= aDescr )
     179             :         {
     180        1509 :             if ( !aURL.isEmpty() )
     181             :             {
     182           0 :                 aPropsToSet.realloc(1);
     183           0 :                 aPropsToSet[0].Name = "URL";
     184           0 :                 aPropsToSet[0].Value <<= aURL;
     185             :             }
     186             : 
     187        3078 :             for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
     188             :             {
     189        3138 :                 if ( aDescr[nInd].Name == "InteractionHandler"
     190        1569 :                   || aDescr[nInd].Name == "Password"
     191        1569 :                   || aDescr[nInd].Name == "RepairPackage"
     192        3078 :                   || aDescr[nInd].Name == "StatusIndicator" )
     193             :                   // || aDescr[nInd].Name == "Unpacked" ) // TODO:
     194             :                 {
     195          60 :                     aPropsToSet.realloc( ++nNumArgs );
     196          60 :                     aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
     197          60 :                     aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
     198             :                 }
     199        1509 :                 else if ( aDescr[nInd].Name == "StorageFormat" )
     200             :                 {
     201        1509 :                     OUString aFormatName;
     202        1509 :                     sal_Int32 nFormatID = 0;
     203        1509 :                     if ( aDescr[nInd].Value >>= aFormatName )
     204             :                     {
     205        1509 :                         if ( aFormatName.equals( PACKAGE_STORAGE_FORMAT_STRING ) )
     206           0 :                             nStorageType = embed::StorageFormats::PACKAGE;
     207        1509 :                         else if ( aFormatName.equals( ZIP_STORAGE_FORMAT_STRING ) )
     208         922 :                             nStorageType = embed::StorageFormats::ZIP;
     209         587 :                         else if ( aFormatName.equals( OFOPXML_STORAGE_FORMAT_STRING ) )
     210         587 :                             nStorageType = embed::StorageFormats::OFOPXML;
     211             :                         else
     212           0 :                             throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
     213             :                     }
     214           0 :                     else if ( aDescr[nInd].Value >>= nFormatID )
     215             :                     {
     216           0 :                         if ( nFormatID != embed::StorageFormats::PACKAGE
     217           0 :                           && nFormatID != embed::StorageFormats::ZIP
     218           0 :                           && nFormatID != embed::StorageFormats::OFOPXML )
     219           0 :                             throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
     220             : 
     221           0 :                         nStorageType = nFormatID;
     222             :                     }
     223             :                     else
     224           0 :                         throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
     225             :                 }
     226             :                 else
     227             :                     OSL_FAIL( "Unacceptable property, will be ignored!\n" );
     228             :             }
     229             :         }
     230             :         else
     231             :         {
     232             :             OSL_FAIL( "Wrong third argument!\n" );
     233           0 :             throw uno::Exception(); // TODO: Illegal argument
     234             :         }
     235             : 
     236             :     }
     237             : 
     238             :     // create storage based on source
     239        2389 :     if ( xInputStream.is() )
     240             :     {
     241             :         // if xInputStream is set the storage should be open from it
     242        1430 :         if ( ( nStorageMode & embed::ElementModes::WRITE ) )
     243           0 :               throw uno::Exception(); // TODO: access denied
     244             : 
     245        1430 :         uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
     246        1430 :         if ( !xSeekable.is() )
     247             :         {
     248             :             // TODO: wrap stream to let it be seekable
     249             :             OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
     250             :         }
     251             : 
     252        1430 :         if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
     253         177 :             throw io::IOException(); // TODO: this is not a package file
     254             : 
     255             :         return uno::Reference< uno::XInterface >(
     256        2506 :                     static_cast< OWeakObject* >( new OStorage( xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
     257        2683 :                     uno::UNO_QUERY );
     258             :     }
     259         959 :     else if ( xStream.is() )
     260             :     {
     261        3709 :                if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
     262        3836 :           || !xStream->getInputStream().is() )
     263           0 :               throw uno::Exception(); // TODO: access denied
     264             : 
     265         959 :         uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
     266         959 :         if ( !xSeekable.is() )
     267             :         {
     268             :             // TODO: wrap stream to let it be seekable
     269             :             OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
     270             :         }
     271             : 
     272         959 :         if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
     273         214 :             throw io::IOException(); // TODO: this is not a package file
     274             : 
     275             :         return uno::Reference< uno::XInterface >(
     276        1490 :                     static_cast< OWeakObject* >( new OStorage( xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
     277        1704 :                     uno::UNO_QUERY );
     278             :     }
     279             : 
     280        2389 :     throw uno::Exception(); // general error during creation
     281             : }
     282             : 
     283             : //-------------------------------------------------------------------------
     284           0 : OUString SAL_CALL OStorageFactory::getImplementationName()
     285             :     throw ( uno::RuntimeException )
     286             : {
     287           0 :     return impl_staticGetImplementationName();
     288             : }
     289             : 
     290             : //-------------------------------------------------------------------------
     291           0 : sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName )
     292             :     throw ( uno::RuntimeException )
     293             : {
     294           0 :     uno::Sequence< OUString > aSeq = impl_staticGetSupportedServiceNames();
     295             : 
     296           0 :     for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
     297           0 :         if ( ServiceName == aSeq[nInd] )
     298           0 :             return sal_True;
     299             : 
     300           0 :     return sal_False;
     301             : }
     302             : 
     303             : //-------------------------------------------------------------------------
     304           0 : uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
     305             :     throw ( uno::RuntimeException )
     306             : {
     307           0 :     return impl_staticGetSupportedServiceNames();
     308             : }
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10