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

Generated by: LCOV version 1.10