LibreOffice Module unotools (master) 1
ucbstreamhelper.cxx
Go to the documentation of this file.
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>
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>
28
30#include <ucbhelper/content.hxx>
32#include "ucblockbytes.hxx"
33
34namespace com::sun::star::ucb { class XCommandEnvironment; }
35
36using namespace ::com::sun::star::uno;
37using namespace ::com::sun::star::io;
38using namespace ::com::sun::star::ucb;
39using namespace ::com::sun::star::task;
40using namespace ::com::sun::star::lang;
41using namespace ::com::sun::star::beans;
42
43namespace utl
44{
45
46static std::unique_ptr<SvStream> lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode,
47 const Reference < XInteractionHandler >& xInteractionHandler,
48 bool bEnsureFileExists )
49{
50 std::unique_ptr<SvStream> pStream;
51 UcbLockBytesRef xLockBytes;
52 if ( eOpenMode & StreamMode::WRITE )
53 {
54 bool bTruncate = bool( eOpenMode & StreamMode::TRUNC );
55 if ( bTruncate )
56 {
57 try
58 {
59 // truncate is implemented with deleting the original file
61 rFileName, Reference < XCommandEnvironment >(),
63 aCnt.executeCommand( "delete", css::uno::Any( true ) );
64 }
65
66 catch ( const CommandAbortedException& )
67 {
68 // couldn't truncate/delete
69 }
70 catch ( const ContentCreationException& )
71 {
72 }
73 catch ( const Exception& )
74 {
75 }
76 }
77
78 if ( bEnsureFileExists || bTruncate )
79 {
80 try
81 {
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 );
85
86 ::ucbhelper::Content aContent(
87 rFileName, Reference < XCommandEnvironment >(),
89 InsertCommandArgument aInsertArg;
90 aInsertArg.Data = xInput;
91
92 aInsertArg.ReplaceExisting = false;
93 Any aCmdArg;
94 aCmdArg <<= aInsertArg;
95 aContent.executeCommand( "insert", aCmdArg );
96 }
97
98 // it is NOT an error when the stream already exists and no truncation was desired
99 catch ( const CommandAbortedException& )
100 {
101 // currently never an error is detected !
102 }
103 catch ( const ContentCreationException& )
104 {
105 }
106 catch ( const Exception& )
107 {
108 }
109 }
110 }
111
112 try
113 {
114 // create LockBytes using UCB
115 ::ucbhelper::Content aContent(
116 rFileName, Reference < XCommandEnvironment >(),
118 xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
119 eOpenMode, xInteractionHandler );
120 if ( xLockBytes.is() )
121 {
122 pStream.reset( new SvStream( xLockBytes.get() ) );
123 pStream->SetBufferSize( 4096 );
124 pStream->SetError( xLockBytes->GetError() );
125 }
126 }
127 catch ( const CommandAbortedException& )
128 {
129 }
130 catch ( const ContentCreationException& )
131 {
132 }
133 catch ( const Exception& )
134 {
135 }
136
137 return pStream;
138}
139
140std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode, css::uno::Reference<css::awt::XWindow> xParentWin)
141{
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));
147
148 return lcl_CreateStream( rFileName, eOpenMode, xIHScoped, true /* bEnsureFileExists */ );
149}
150
151std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode,
152 bool bFileExists, css::uno::Reference<css::awt::XWindow> xParentWin)
153{
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 );
160}
161
162
163std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream )
164{
165 std::unique_ptr<SvStream> pStream;
167 if ( xLockBytes.is() )
168 {
169 pStream.reset( new SvStream( xLockBytes.get() ) );
170 pStream->SetBufferSize( 4096 );
171 pStream->SetError( xLockBytes->GetError() );
172 }
173
174 return pStream;
175}
176
177std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream )
178{
179 std::unique_ptr<SvStream> pStream;
180 if ( xStream->getOutputStream().is() )
181 {
182 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
183 if ( xLockBytes.is() )
184 {
185 pStream.reset( new SvStream( xLockBytes.get() ) );
186 pStream->SetBufferSize( 4096 );
187 pStream->SetError( xLockBytes->GetError() );
188 }
189 }
190 else
191 return CreateStream( xStream->getInputStream() );
192
193 return pStream;
194}
195
196std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream, bool bCloseStream )
197{
198 std::unique_ptr<SvStream> pStream;
200 if ( xLockBytes.is() )
201 {
202 if ( !bCloseStream )
203 xLockBytes->setDontClose();
204
205 pStream.reset( new SvStream( xLockBytes.get() ) );
206 pStream->SetBufferSize( 4096 );
207 pStream->SetError( xLockBytes->GetError() );
208 }
209
210 return pStream;
211};
212
213std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream, bool bCloseStream )
214{
215 std::unique_ptr<SvStream> pStream;
216 if ( xStream->getOutputStream().is() )
217 {
218 UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
219 if ( xLockBytes.is() )
220 {
221 if ( !bCloseStream )
222 xLockBytes->setDontClose();
223
224 pStream.reset( new SvStream( xLockBytes.get() ) );
225 pStream->SetBufferSize( 4096 );
226 pStream->SetError( xLockBytes->GetError() );
227 }
228 }
229 else
230 return CreateStream( xStream->getInputStream(), bCloseStream );
231
232 return pStream;
233};
234
235}
236
237/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
T * get() const
bool is() const
css::uno::Any executeCommand(const OUString &rCommandName, const css::uno::Any &rCommandArgument)
css::uno::Reference< css::ucb::XContent > get() const
static UcbLockBytesRef CreateInputLockBytes(const css::uno::Reference< css::io::XInputStream > &xContent)
static UcbLockBytesRef CreateLockBytes(const css::uno::Reference< css::ucb::XContent > &xContent, const css::uno::Sequence< css::beans::PropertyValue > &rProps, StreamMode eMode, const css::uno::Reference< css::task::XInteractionHandler > &xInter)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
@ Exception
Reference< XComponentContext > getProcessComponentContext()
tools::SvRef< UcbLockBytes > UcbLockBytesRef
static std::unique_ptr< SvStream > lcl_CreateStream(const OUString &rFileName, StreamMode eOpenMode, const Reference< XInteractionHandler > &xInteractionHandler, bool bEnsureFileExists)
StreamMode
ElementOpenMode eOpenMode