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/ucbstreamhelper.hxx>
22 : #include <comphelper/processfactory.hxx>
23 : #include <com/sun/star/ucb/CommandAbortedException.hpp>
24 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
25 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
26 : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
27 : #include <com/sun/star/io/XActiveDataStreamer.hpp>
28 :
29 : #include <ucbhelper/content.hxx>
30 : #include <unotools/streamwrap.hxx>
31 : #include <ucblockbytes.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 19691 : static SvStream* lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode,
44 : Reference < XInteractionHandler > xInteractionHandler,
45 : UcbLockBytesHandler* pHandler, bool bEnsureFileExists )
46 : {
47 19691 : SvStream* pStream = NULL;
48 : Reference< XUniversalContentBroker > ucb(
49 : UniversalContentBroker::create(
50 19691 : comphelper::getProcessComponentContext() ) );
51 39382 : UcbLockBytesRef xLockBytes;
52 19691 : if ( eOpenMode & StreamMode::WRITE )
53 : {
54 19414 : bool bTruncate = bool( eOpenMode & StreamMode::TRUNC );
55 19414 : if ( bTruncate )
56 : {
57 : try
58 : {
59 : // truncate is implemented with deleting the original file
60 : ::ucbhelper::Content aCnt(
61 : rFileName, Reference < XCommandEnvironment >(),
62 225 : comphelper::getProcessComponentContext() );
63 236 : 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 11 : catch ( const Exception& )
74 : {
75 : }
76 : }
77 :
78 19414 : if ( bEnsureFileExists || bTruncate )
79 : {
80 : try
81 : {
82 : // make sure that the desired file exists before trying to open
83 3817 : SvMemoryStream aStream(0,0);
84 3817 : ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
85 7634 : Reference< XInputStream > xInput( pInput );
86 :
87 : ::ucbhelper::Content aContent(
88 : rFileName, Reference < XCommandEnvironment >(),
89 7634 : comphelper::getProcessComponentContext() );
90 7634 : InsertCommandArgument aInsertArg;
91 3817 : aInsertArg.Data = xInput;
92 :
93 3817 : aInsertArg.ReplaceExisting = sal_False;
94 7634 : Any aCmdArg;
95 3817 : aCmdArg <<= aInsertArg;
96 11181 : 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 3547 : 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 19713 : comphelper::getProcessComponentContext() );
119 39338 : xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
120 19669 : eOpenMode, xInteractionHandler, pHandler );
121 19669 : if ( xLockBytes.Is() )
122 : {
123 19669 : pStream = new SvStream( xLockBytes );
124 19669 : pStream->SetBufferSize( 4096 );
125 19669 : pStream->SetError( xLockBytes->GetError() );
126 19669 : }
127 : }
128 0 : catch ( const CommandAbortedException& )
129 : {
130 : }
131 22 : catch ( const ContentCreationException& )
132 : {
133 : }
134 0 : catch ( const Exception& )
135 : {
136 : }
137 :
138 39382 : return pStream;
139 : }
140 :
141 4086 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
142 : UcbLockBytesHandler* pHandler )
143 : {
144 4086 : 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 15605 : SvStream* UcbStreamHelper::CreateStream( const OUString& rFileName, StreamMode eOpenMode,
155 : bool bFileExists,
156 : UcbLockBytesHandler* pHandler )
157 : {
158 15605 : return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
159 : }
160 :
161 21353 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
162 : {
163 21353 : SvStream* pStream = NULL;
164 21353 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
165 21353 : if ( xLockBytes.Is() )
166 : {
167 21353 : pStream = new SvStream( xLockBytes );
168 21353 : pStream->SetBufferSize( 4096 );
169 21353 : pStream->SetError( xLockBytes->GetError() );
170 : }
171 :
172 21353 : return pStream;
173 : }
174 :
175 3476 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
176 : {
177 3476 : SvStream* pStream = NULL;
178 3476 : if ( xStream->getOutputStream().is() )
179 : {
180 3120 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
181 3120 : if ( xLockBytes.Is() )
182 : {
183 3120 : pStream = new SvStream( xLockBytes );
184 3120 : pStream->SetBufferSize( 4096 );
185 3120 : pStream->SetError( xLockBytes->GetError() );
186 3120 : }
187 : }
188 : else
189 356 : return CreateStream( xStream->getInputStream() );
190 :
191 3120 : return pStream;
192 : }
193 :
194 2259 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, bool bCloseStream )
195 : {
196 2259 : SvStream* pStream = NULL;
197 2259 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
198 2259 : if ( xLockBytes.Is() )
199 : {
200 2259 : if ( !bCloseStream )
201 1382 : xLockBytes->setDontClose_Impl();
202 :
203 2259 : pStream = new SvStream( xLockBytes );
204 2259 : pStream->SetBufferSize( 4096 );
205 2259 : pStream->SetError( xLockBytes->GetError() );
206 : }
207 :
208 2259 : return pStream;
209 : };
210 :
211 382 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, bool bCloseStream )
212 : {
213 382 : SvStream* pStream = NULL;
214 382 : if ( xStream->getOutputStream().is() )
215 : {
216 371 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
217 371 : if ( xLockBytes.Is() )
218 : {
219 371 : if ( !bCloseStream )
220 276 : xLockBytes->setDontClose_Impl();
221 :
222 371 : pStream = new SvStream( xLockBytes );
223 371 : pStream->SetBufferSize( 4096 );
224 371 : pStream->SetError( xLockBytes->GetError() );
225 371 : }
226 : }
227 : else
228 11 : return CreateStream( xStream->getInputStream(), bCloseStream );
229 :
230 371 : return pStream;
231 : };
232 :
233 : }
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|