Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / ucbhelper / ucbstreamhelper.cxx
blob6f72c00bc98504cdff832543c01c3ade7ae1796c
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 .
20 #include <rtl/ustring.hxx>
21 #include <unotools/ucbstreamhelper.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <com/sun/star/task/InteractionHandler.hpp>
24 #include <com/sun/star/ucb/ContentCreationException.hpp>
25 #include <com/sun/star/ucb/CommandAbortedException.hpp>
26 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
27 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <comphelper/simplefileaccessinteraction.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <unotools/streamwrap.hxx>
32 #include "ucblockbytes.hxx"
34 namespace com::sun::star::ucb { class XCommandEnvironment; }
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::ucb;
39 using namespace ::com::sun::star::task;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::beans;
43 namespace utl
46 static std::unique_ptr<SvStream> lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode,
47 const Reference < XInteractionHandler >& xInteractionHandler,
48 bool bEnsureFileExists )
50 std::unique_ptr<SvStream> pStream;
51 UcbLockBytesRef xLockBytes;
52 if ( eOpenMode & StreamMode::WRITE )
54 bool bTruncate = bool( eOpenMode & StreamMode::TRUNC );
55 if ( bTruncate )
57 try
59 // truncate is implemented with deleting the original file
60 ::ucbhelper::Content aCnt(
61 rFileName, Reference < XCommandEnvironment >(),
62 comphelper::getProcessComponentContext() );
63 aCnt.executeCommand( "delete", css::uno::makeAny( true ) );
66 catch ( const CommandAbortedException& )
68 // couldn't truncate/delete
70 catch ( const ContentCreationException& )
73 catch ( const Exception& )
78 if ( bEnsureFileExists || bTruncate )
80 try
82 // make sure that the desired file exists before trying to open
83 SvMemoryStream aStream(0,0);
84 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
85 Reference< XInputStream > xInput( pInput );
87 ::ucbhelper::Content aContent(
88 rFileName, Reference < XCommandEnvironment >(),
89 comphelper::getProcessComponentContext() );
90 InsertCommandArgument aInsertArg;
91 aInsertArg.Data = xInput;
93 aInsertArg.ReplaceExisting = false;
94 Any aCmdArg;
95 aCmdArg <<= aInsertArg;
96 aContent.executeCommand( "insert", aCmdArg );
99 // it is NOT an error when the stream already exists and no truncation was desired
100 catch ( const CommandAbortedException& )
102 // currently never an error is detected !
104 catch ( const ContentCreationException& )
107 catch ( const Exception& )
115 // create LockBytes using UCB
116 ::ucbhelper::Content aContent(
117 rFileName, Reference < XCommandEnvironment >(),
118 comphelper::getProcessComponentContext() );
119 xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
120 eOpenMode, xInteractionHandler );
121 if ( xLockBytes.is() )
123 pStream.reset( new SvStream( xLockBytes.get() ) );
124 pStream->SetBufferSize( 4096 );
125 pStream->SetError( xLockBytes->GetError() );
128 catch ( const CommandAbortedException& )
131 catch ( const ContentCreationException& )
134 catch ( const Exception& )
138 return pStream;
141 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode, css::uno::Reference<css::awt::XWindow> xParentWin)
143 // related tdf#99312
144 // create a specialized interaction handler to manages Web certificates and Web credentials when needed
145 Reference< XInteractionHandler > xIH(
146 css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin));
147 Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH));
149 return lcl_CreateStream( rFileName, eOpenMode, xIHScoped, true /* bEnsureFileExists */ );
152 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode,
153 bool bFileExists, css::uno::Reference<css::awt::XWindow> xParentWin)
155 // related tdf#99312
156 // create a specialized interaction handler to manages Web certificates and Web credentials when needed
157 Reference< XInteractionHandler > xIH(
158 css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin));
159 Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH));
160 return lcl_CreateStream( rFileName, eOpenMode, xIHScoped,!bFileExists );
164 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream )
166 std::unique_ptr<SvStream> pStream;
167 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
168 if ( xLockBytes.is() )
170 pStream.reset( new SvStream( xLockBytes.get() ) );
171 pStream->SetBufferSize( 4096 );
172 pStream->SetError( xLockBytes->GetError() );
175 return pStream;
178 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream )
180 std::unique_ptr<SvStream> pStream;
181 if ( xStream->getOutputStream().is() )
183 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
184 if ( xLockBytes.is() )
186 pStream.reset( new SvStream( xLockBytes.get() ) );
187 pStream->SetBufferSize( 4096 );
188 pStream->SetError( xLockBytes->GetError() );
191 else
192 return CreateStream( xStream->getInputStream() );
194 return pStream;
197 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream, bool bCloseStream )
199 std::unique_ptr<SvStream> pStream;
200 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
201 if ( xLockBytes.is() )
203 if ( !bCloseStream )
204 xLockBytes->setDontClose_Impl();
206 pStream.reset( new SvStream( xLockBytes.get() ) );
207 pStream->SetBufferSize( 4096 );
208 pStream->SetError( xLockBytes->GetError() );
211 return pStream;
214 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream, bool bCloseStream )
216 std::unique_ptr<SvStream> pStream;
217 if ( xStream->getOutputStream().is() )
219 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
220 if ( xLockBytes.is() )
222 if ( !bCloseStream )
223 xLockBytes->setDontClose_Impl();
225 pStream.reset( new SvStream( xLockBytes.get() ) );
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: */