Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / ucbhelper / ucbstreamhelper.cxx
blob67483f1904b0b81bb69c298662974e2bc1ecfdc4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 #include <ucbhelper/content.hxx>
31 #include <unotools/streamwrap.hxx>
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::io;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::ucb;
37 using namespace ::com::sun::star::task;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
41 namespace utl
44 static SvStream* lcl_CreateStream( const String& rFileName, StreamMode eOpenMode,
45 Reference < XInteractionHandler > xInteractionHandler,
46 UcbLockBytesHandler* pHandler, sal_Bool bEnsureFileExists )
48 SvStream* pStream = NULL;
49 Reference< XUniversalContentBroker > ucb(
50 UniversalContentBroker::create(
51 comphelper::getProcessComponentContext() ) );
52 UcbLockBytesRef xLockBytes;
53 if ( eOpenMode & STREAM_WRITE )
55 sal_Bool bTruncate = ( eOpenMode & STREAM_TRUNC ) != 0;
56 if ( bTruncate )
58 try
60 // truncate is implemented with deleting the original file
61 ::ucbhelper::Content aCnt(
62 rFileName, Reference < XCommandEnvironment >(),
63 comphelper::getProcessComponentContext() );
64 aCnt.executeCommand( ::rtl::OUString("delete"), makeAny( sal_Bool( sal_True ) ) );
67 catch ( const CommandAbortedException& )
69 // couldn't truncate/delete
71 catch ( const ContentCreationException& )
74 catch ( const Exception& )
79 if ( bEnsureFileExists || bTruncate )
81 try
83 // make sure that the desired file exists before trying to open
84 SvMemoryStream aStream(0,0);
85 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
86 Reference< XInputStream > xInput( pInput );
88 ::ucbhelper::Content aContent(
89 rFileName, Reference < XCommandEnvironment >(),
90 comphelper::getProcessComponentContext() );
91 InsertCommandArgument aInsertArg;
92 aInsertArg.Data = xInput;
94 aInsertArg.ReplaceExisting = sal_False;
95 Any aCmdArg;
96 aCmdArg <<= aInsertArg;
97 aContent.executeCommand( ::rtl::OUString("insert"), aCmdArg );
100 // it is NOT an error when the stream already exists and no truncation was desired
101 catch ( const CommandAbortedException& )
103 // currently never an error is detected !
105 catch ( const ContentCreationException& )
108 catch ( const Exception& )
116 // create LockBytes using UCB
117 ::ucbhelper::Content aContent(
118 rFileName, Reference < XCommandEnvironment >(),
119 comphelper::getProcessComponentContext() );
120 xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
121 eOpenMode, xInteractionHandler, pHandler );
122 if ( xLockBytes.Is() )
124 pStream = new SvStream( xLockBytes );
125 pStream->SetBufferSize( 4096 );
126 pStream->SetError( xLockBytes->GetError() );
129 catch ( const CommandAbortedException& )
132 catch ( const ContentCreationException& )
135 catch ( const Exception& )
139 return pStream;
142 //============================================================================
144 SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
145 UcbLockBytesHandler* pHandler )
147 return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, sal_True /* bEnsureFileExists */ );
150 SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
151 Reference < XInteractionHandler > xInteractionHandler,
152 UcbLockBytesHandler* pHandler )
154 return lcl_CreateStream( rFileName, eOpenMode, xInteractionHandler, pHandler, sal_True /* bEnsureFileExists */ );
157 SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
158 sal_Bool bFileExists,
159 UcbLockBytesHandler* pHandler )
161 return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
164 SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
166 SvStream* pStream = NULL;
167 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
168 if ( xLockBytes.Is() )
170 pStream = new SvStream( xLockBytes );
171 pStream->SetBufferSize( 4096 );
172 pStream->SetError( xLockBytes->GetError() );
175 return pStream;
178 SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
180 SvStream* pStream = NULL;
181 if ( xStream->getOutputStream().is() )
183 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
184 if ( xLockBytes.Is() )
186 pStream = new SvStream( xLockBytes );
187 pStream->SetBufferSize( 4096 );
188 pStream->SetError( xLockBytes->GetError() );
191 else
192 return CreateStream( xStream->getInputStream() );
194 return pStream;
197 SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, sal_Bool bCloseStream )
199 SvStream* pStream = NULL;
200 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
201 if ( xLockBytes.Is() )
203 if ( !bCloseStream )
204 xLockBytes->setDontClose_Impl();
206 pStream = new SvStream( xLockBytes );
207 pStream->SetBufferSize( 4096 );
208 pStream->SetError( xLockBytes->GetError() );
211 return pStream;
214 SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, sal_Bool bCloseStream )
216 SvStream* pStream = NULL;
217 if ( xStream->getOutputStream().is() )
219 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
220 if ( xLockBytes.Is() )
222 if ( !bCloseStream )
223 xLockBytes->setDontClose_Impl();
225 pStream = new SvStream( xLockBytes );
226 pStream->SetBufferSize( 4096 );
227 pStream->SetError( xLockBytes->GetError() );
230 else
231 return CreateStream( xStream->getInputStream(), bCloseStream );
233 return pStream;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */