LCOV - code coverage report
Current view: top level - unotools/source/ucbhelper - ucbstreamhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 78 86 90.7 %
Date: 2014-11-03 Functions: 7 8 87.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 <rtl/ustring.hxx>
      21             : #include <unotools/ucblockbytes.hxx>
      22             : #include <unotools/ucbstreamhelper.hxx>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <com/sun/star/ucb/CommandAbortedException.hpp>
      25             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      26             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      27             : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
      28             : #include <com/sun/star/io/XActiveDataStreamer.hpp>
      29             : 
      30             : #include <ucbhelper/content.hxx>
      31             : #include <unotools/streamwrap.hxx>
      32             : 
      33             : using namespace ::com::sun::star::uno;
      34             : using namespace ::com::sun::star::io;
      35             : using namespace ::com::sun::star::ucb;
      36             : using namespace ::com::sun::star::task;
      37             : using namespace ::com::sun::star::lang;
      38             : using namespace ::com::sun::star::beans;
      39             : 
      40             : namespace utl
      41             : {
      42             : 
      43       31387 : static SvStream* lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode,
      44             :                                    Reference < XInteractionHandler > xInteractionHandler,
      45             :                                    UcbLockBytesHandler* pHandler, bool bEnsureFileExists )
      46             : {
      47       31387 :     SvStream* pStream = NULL;
      48             :     Reference< XUniversalContentBroker > ucb(
      49             :         UniversalContentBroker::create(
      50       31387 :             comphelper::getProcessComponentContext() ) );
      51       62774 :     UcbLockBytesRef xLockBytes;
      52       31387 :     if ( eOpenMode & STREAM_WRITE )
      53             :     {
      54       31048 :         bool bTruncate = ( eOpenMode & STREAM_TRUNC ) != 0;
      55       31048 :         if ( bTruncate )
      56             :         {
      57             :             try
      58             :             {
      59             :                 // truncate is implemented with deleting the original file
      60             :                 ::ucbhelper::Content aCnt(
      61             :                     rFileName, Reference < XCommandEnvironment >(),
      62         420 :                     comphelper::getProcessComponentContext() );
      63         431 :                 aCnt.executeCommand( OUString("delete"), makeAny( true ) );
      64             :             }
      65             : 
      66           0 :             catch ( const CommandAbortedException& )
      67             :             {
      68             :                 // couldn't truncate/delete
      69             :             }
      70           0 :             catch ( const ContentCreationException& )
      71             :             {
      72             :             }
      73          11 :             catch ( const Exception& )
      74             :             {
      75             :             }
      76             :         }
      77             : 
      78       31048 :         if ( bEnsureFileExists || bTruncate )
      79             :         {
      80             :             try
      81             :             {
      82             :                 // make sure that the desired file exists before trying to open
      83        4090 :                 SvMemoryStream aStream(0,0);
      84        4090 :                 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
      85        8180 :                 Reference< XInputStream > xInput( pInput );
      86             : 
      87             :                 ::ucbhelper::Content aContent(
      88             :                     rFileName, Reference < XCommandEnvironment >(),
      89        8180 :                     comphelper::getProcessComponentContext() );
      90        8180 :                 InsertCommandArgument aInsertArg;
      91        4090 :                 aInsertArg.Data = xInput;
      92             : 
      93        4090 :                 aInsertArg.ReplaceExisting = sal_False;
      94        8180 :                 Any aCmdArg;
      95        4090 :                 aCmdArg <<= aInsertArg;
      96       11808 :                 aContent.executeCommand( OUString("insert"), aCmdArg );
      97             :             }
      98             : 
      99             :             // it is NOT an error when the stream already exists and no truncation was desired
     100           0 :             catch ( const CommandAbortedException& )
     101             :             {
     102             :                 // currently never an error is detected !
     103             :             }
     104           0 :             catch ( const ContentCreationException& )
     105             :             {
     106             :             }
     107        3628 :             catch ( const Exception& )
     108             :             {
     109             :             }
     110             :         }
     111             :     }
     112             : 
     113             :     try
     114             :     {
     115             :         // create LockBytes using UCB
     116             :         ::ucbhelper::Content aContent(
     117             :             rFileName, Reference < XCommandEnvironment >(),
     118       31399 :             comphelper::getProcessComponentContext() );
     119       62750 :         xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
     120       31375 :                                                     eOpenMode, xInteractionHandler, pHandler );
     121       31375 :         if ( xLockBytes.Is() )
     122             :         {
     123       31375 :             pStream = new SvStream( xLockBytes );
     124       31375 :             pStream->SetBufferSize( 4096 );
     125       31375 :             pStream->SetError( xLockBytes->GetError() );
     126       31375 :         }
     127             :     }
     128           0 :     catch ( const CommandAbortedException& )
     129             :     {
     130             :     }
     131          12 :     catch ( const ContentCreationException& )
     132             :     {
     133             :     }
     134           0 :     catch ( const Exception& )
     135             :     {
     136             :     }
     137             : 
     138       62774 :     return pStream;
     139             : }
     140             : 
     141        4413 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
     142             :                                          UcbLockBytesHandler* pHandler )
     143             : {
     144        4413 :     return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, true /* bEnsureFileExists */ );
     145             : }
     146             : 
     147           0 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
     148             :                                          Reference < XInteractionHandler > xInteractionHandler,
     149             :                                          UcbLockBytesHandler* pHandler )
     150             : {
     151           0 :     return lcl_CreateStream( rFileName, eOpenMode, xInteractionHandler, pHandler, true /* bEnsureFileExists */ );
     152             : }
     153             : 
     154       26974 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
     155             :                                          bool bFileExists,
     156             :                                          UcbLockBytesHandler* pHandler )
     157             : {
     158       26974 :     return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
     159             : }
     160             : 
     161       18890 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
     162             : {
     163       18890 :     SvStream* pStream = NULL;
     164       18890 :     UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
     165       18890 :     if ( xLockBytes.Is() )
     166             :     {
     167       18890 :         pStream = new SvStream( xLockBytes );
     168       18890 :         pStream->SetBufferSize( 4096 );
     169       18890 :         pStream->SetError( xLockBytes->GetError() );
     170             :     }
     171             : 
     172       18890 :     return pStream;
     173             : }
     174             : 
     175        5091 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
     176             : {
     177        5091 :     SvStream* pStream = NULL;
     178        5091 :     if ( xStream->getOutputStream().is() )
     179             :     {
     180        4849 :         UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
     181        4849 :         if ( xLockBytes.Is() )
     182             :         {
     183        4849 :             pStream = new SvStream( xLockBytes );
     184        4849 :             pStream->SetBufferSize( 4096 );
     185        4849 :             pStream->SetError( xLockBytes->GetError() );
     186        4849 :         }
     187             :     }
     188             :     else
     189         242 :         return CreateStream( xStream->getInputStream() );
     190             : 
     191        4849 :     return pStream;
     192             : }
     193             : 
     194        1532 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, bool bCloseStream )
     195             : {
     196        1532 :     SvStream* pStream = NULL;
     197        1532 :     UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
     198        1532 :     if ( xLockBytes.Is() )
     199             :     {
     200        1532 :         if ( !bCloseStream )
     201          88 :             xLockBytes->setDontClose_Impl();
     202             : 
     203        1532 :         pStream = new SvStream( xLockBytes );
     204        1532 :         pStream->SetBufferSize( 4096 );
     205        1532 :         pStream->SetError( xLockBytes->GetError() );
     206             :     }
     207             : 
     208        1532 :     return pStream;
     209             : };
     210             : 
     211         650 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, bool bCloseStream )
     212             : {
     213         650 :     SvStream* pStream = NULL;
     214         650 :     if ( xStream->getOutputStream().is() )
     215             :     {
     216         626 :         UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
     217         626 :         if ( xLockBytes.Is() )
     218             :         {
     219         626 :             if ( !bCloseStream )
     220         490 :                 xLockBytes->setDontClose_Impl();
     221             : 
     222         626 :             pStream = new SvStream( xLockBytes );
     223         626 :             pStream->SetBufferSize( 4096 );
     224         626 :             pStream->SetError( xLockBytes->GetError() );
     225         626 :         }
     226             :     }
     227             :     else
     228          24 :         return CreateStream( xStream->getInputStream(), bCloseStream );
     229             : 
     230         626 :     return pStream;
     231             : };
     232             : 
     233             : }
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10