Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / ucbhelper / ucbstreamhelper.cxx
blobe3437a864fbf5d12629ba0e90156acd55ee7d81a
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::Any( 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 rtl::Reference<::utl::OInputStreamWrapper> xInput = new ::utl::OInputStreamWrapper( aStream );
86 ::ucbhelper::Content aContent(
87 rFileName, Reference < XCommandEnvironment >(),
88 comphelper::getProcessComponentContext() );
89 InsertCommandArgument aInsertArg;
90 aInsertArg.Data = xInput;
92 aInsertArg.ReplaceExisting = false;
93 Any aCmdArg;
94 aCmdArg <<= aInsertArg;
95 aContent.executeCommand( "insert", aCmdArg );
98 // it is NOT an error when the stream already exists and no truncation was desired
99 catch ( const CommandAbortedException& )
101 // currently never an error is detected !
103 catch ( const ContentCreationException& )
106 catch ( const Exception& )
114 // create LockBytes using UCB
115 ::ucbhelper::Content aContent(
116 rFileName, Reference < XCommandEnvironment >(),
117 comphelper::getProcessComponentContext() );
118 xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
119 eOpenMode, xInteractionHandler );
120 if ( xLockBytes.is() )
122 pStream.reset( new SvStream( xLockBytes.get() ) );
123 pStream->SetBufferSize( 4096 );
124 pStream->SetError( xLockBytes->GetError() );
127 catch ( const CommandAbortedException& )
130 catch ( const ContentCreationException& )
133 catch ( const Exception& )
137 return pStream;
140 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode, css::uno::Reference<css::awt::XWindow> xParentWin)
142 // related tdf#99312
143 // create a specialized interaction handler to manages Web certificates and Web credentials when needed
144 Reference< XInteractionHandler > xIH(
145 css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin));
146 Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH));
148 return lcl_CreateStream( rFileName, eOpenMode, xIHScoped, true /* bEnsureFileExists */ );
151 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode,
152 bool bFileExists, css::uno::Reference<css::awt::XWindow> xParentWin)
154 // related tdf#99312
155 // create a specialized interaction handler to manages Web certificates and Web credentials when needed
156 Reference< XInteractionHandler > xIH(
157 css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin));
158 Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH));
159 return lcl_CreateStream( rFileName, eOpenMode, xIHScoped,!bFileExists );
163 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream )
165 std::unique_ptr<SvStream> pStream;
166 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
167 if ( xLockBytes.is() )
169 pStream.reset( new SvStream( xLockBytes.get() ) );
170 pStream->SetBufferSize( 4096 );
171 pStream->SetError( xLockBytes->GetError() );
174 return pStream;
177 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream )
179 std::unique_ptr<SvStream> pStream;
180 if ( xStream->getOutputStream().is() )
182 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
183 if ( xLockBytes.is() )
185 pStream.reset( new SvStream( xLockBytes.get() ) );
186 pStream->SetBufferSize( 4096 );
187 pStream->SetError( xLockBytes->GetError() );
190 else
191 return CreateStream( xStream->getInputStream() );
193 return pStream;
196 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream, bool bCloseStream )
198 std::unique_ptr<SvStream> pStream;
199 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
200 if ( xLockBytes.is() )
202 if ( !bCloseStream )
203 xLockBytes->setDontClose();
205 pStream.reset( new SvStream( xLockBytes.get() ) );
206 pStream->SetBufferSize( 4096 );
207 pStream->SetError( xLockBytes->GetError() );
210 return pStream;
213 std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream, bool bCloseStream )
215 std::unique_ptr<SvStream> pStream;
216 if ( xStream->getOutputStream().is() )
218 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
219 if ( xLockBytes.is() )
221 if ( !bCloseStream )
222 xLockBytes->setDontClose();
224 pStream.reset( new SvStream( xLockBytes.get() ) );
225 pStream->SetBufferSize( 4096 );
226 pStream->SetError( xLockBytes->GetError() );
229 else
230 return CreateStream( xStream->getInputStream(), bCloseStream );
232 return pStream;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */