LCOV - code coverage report
Current view: top level - unotools/source/ucbhelper - ucbstreamhelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 78 86 90.7 %
Date: 2014-04-11 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       18441 : static SvStream* lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode,
      44             :                                    Reference < XInteractionHandler > xInteractionHandler,
      45             :                                    UcbLockBytesHandler* pHandler, bool bEnsureFileExists )
      46             : {
      47       18441 :     SvStream* pStream = NULL;
      48             :     Reference< XUniversalContentBroker > ucb(
      49             :         UniversalContentBroker::create(
      50       18441 :             comphelper::getProcessComponentContext() ) );
      51       36882 :     UcbLockBytesRef xLockBytes;
      52       18441 :     if ( eOpenMode & STREAM_WRITE )
      53             :     {
      54       18148 :         bool bTruncate = ( eOpenMode & STREAM_TRUNC ) != 0;
      55       18148 :         if ( bTruncate )
      56             :         {
      57             :             try
      58             :             {
      59             :                 // truncate is implemented with deleting the original file
      60             :                 ::ucbhelper::Content aCnt(
      61             :                     rFileName, Reference < XCommandEnvironment >(),
      62         150 :                     comphelper::getProcessComponentContext() );
      63         161 :                 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       18148 :         if ( bEnsureFileExists || bTruncate )
      79             :         {
      80             :             try
      81             :             {
      82             :                 // make sure that the desired file exists before trying to open
      83        7538 :                 SvMemoryStream aStream(0,0);
      84        7538 :                 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
      85       15076 :                 Reference< XInputStream > xInput( pInput );
      86             : 
      87             :                 ::ucbhelper::Content aContent(
      88             :                     rFileName, Reference < XCommandEnvironment >(),
      89       15076 :                     comphelper::getProcessComponentContext() );
      90       15076 :                 InsertCommandArgument aInsertArg;
      91        7538 :                 aInsertArg.Data = xInput;
      92             : 
      93        7538 :                 aInsertArg.ReplaceExisting = sal_False;
      94       15076 :                 Any aCmdArg;
      95        7538 :                 aCmdArg <<= aInsertArg;
      96       22421 :                 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        7345 :             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       18442 :             comphelper::getProcessComponentContext() );
     119       36880 :         xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
     120       18440 :                                                     eOpenMode, xInteractionHandler, pHandler );
     121       18440 :         if ( xLockBytes.Is() )
     122             :         {
     123       18440 :             pStream = new SvStream( xLockBytes );
     124       18440 :             pStream->SetBufferSize( 4096 );
     125       18440 :             pStream->SetError( xLockBytes->GetError() );
     126       18440 :         }
     127             :     }
     128           0 :     catch ( const CommandAbortedException& )
     129             :     {
     130             :     }
     131           1 :     catch ( const ContentCreationException& )
     132             :     {
     133             :     }
     134           0 :     catch ( const Exception& )
     135             :     {
     136             :     }
     137             : 
     138       36882 :     return pStream;
     139             : }
     140             : 
     141        7823 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
     142             :                                          UcbLockBytesHandler* pHandler )
     143             : {
     144        7823 :     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       10618 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
     155             :                                          bool bFileExists,
     156             :                                          UcbLockBytesHandler* pHandler )
     157             : {
     158       10618 :     return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
     159             : }
     160             : 
     161        9161 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
     162             : {
     163        9161 :     SvStream* pStream = NULL;
     164        9161 :     UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
     165        9161 :     if ( xLockBytes.Is() )
     166             :     {
     167        9161 :         pStream = new SvStream( xLockBytes );
     168        9161 :         pStream->SetBufferSize( 4096 );
     169        9161 :         pStream->SetError( xLockBytes->GetError() );
     170             :     }
     171             : 
     172        9161 :     return pStream;
     173             : }
     174             : 
     175        1813 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
     176             : {
     177        1813 :     SvStream* pStream = NULL;
     178        1813 :     if ( xStream->getOutputStream().is() )
     179             :     {
     180        1728 :         UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
     181        1728 :         if ( xLockBytes.Is() )
     182             :         {
     183        1728 :             pStream = new SvStream( xLockBytes );
     184        1728 :             pStream->SetBufferSize( 4096 );
     185        1728 :             pStream->SetError( xLockBytes->GetError() );
     186        1728 :         }
     187             :     }
     188             :     else
     189          85 :         return CreateStream( xStream->getInputStream() );
     190             : 
     191        1728 :     return pStream;
     192             : }
     193             : 
     194         537 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, bool bCloseStream )
     195             : {
     196         537 :     SvStream* pStream = NULL;
     197         537 :     UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
     198         537 :     if ( xLockBytes.Is() )
     199             :     {
     200         537 :         if ( !bCloseStream )
     201          44 :             xLockBytes->setDontClose_Impl();
     202             : 
     203         537 :         pStream = new SvStream( xLockBytes );
     204         537 :         pStream->SetBufferSize( 4096 );
     205         537 :         pStream->SetError( xLockBytes->GetError() );
     206             :     }
     207             : 
     208         537 :     return pStream;
     209             : };
     210             : 
     211         285 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, bool bCloseStream )
     212             : {
     213         285 :     SvStream* pStream = NULL;
     214         285 :     if ( xStream->getOutputStream().is() )
     215             :     {
     216         273 :         UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
     217         273 :         if ( xLockBytes.Is() )
     218             :         {
     219         273 :             if ( !bCloseStream )
     220         218 :                 xLockBytes->setDontClose_Impl();
     221             : 
     222         273 :             pStream = new SvStream( xLockBytes );
     223         273 :             pStream->SetBufferSize( 4096 );
     224         273 :             pStream->SetError( xLockBytes->GetError() );
     225         273 :         }
     226             :     }
     227             :     else
     228          12 :         return CreateStream( xStream->getInputStream(), bCloseStream );
     229             : 
     230         273 :     return pStream;
     231             : };
     232             : 
     233             : }
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10