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 :
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>
29 :
30 : #include <ucbhelper/content.hxx>
31 : #include <unotools/streamwrap.hxx>
32 :
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;
40 :
41 : namespace utl
42 : {
43 :
44 1988 : static SvStream* lcl_CreateStream( const String& rFileName, StreamMode eOpenMode,
45 : Reference < XInteractionHandler > xInteractionHandler,
46 : UcbLockBytesHandler* pHandler, sal_Bool bEnsureFileExists )
47 : {
48 1988 : SvStream* pStream = NULL;
49 : Reference< XUniversalContentBroker > ucb(
50 : UniversalContentBroker::create(
51 1988 : comphelper::getProcessComponentContext() ) );
52 1988 : UcbLockBytesRef xLockBytes;
53 1988 : if ( eOpenMode & STREAM_WRITE )
54 : {
55 1549 : sal_Bool bTruncate = ( eOpenMode & STREAM_TRUNC ) != 0;
56 1549 : if ( bTruncate )
57 : {
58 : try
59 : {
60 : // truncate is implemented with deleting the original file
61 : ::ucbhelper::Content aCnt(
62 : rFileName, Reference < XCommandEnvironment >(),
63 3 : comphelper::getProcessComponentContext() );
64 3 : aCnt.executeCommand( ::rtl::OUString("delete"), makeAny( sal_Bool( sal_True ) ) );
65 : }
66 :
67 0 : catch ( const CommandAbortedException& )
68 : {
69 : // couldn't truncate/delete
70 : }
71 0 : catch ( const ContentCreationException& )
72 : {
73 : }
74 0 : catch ( const Exception& )
75 : {
76 : }
77 : }
78 :
79 1549 : if ( bEnsureFileExists || bTruncate )
80 : {
81 : try
82 : {
83 : // make sure that the desired file exists before trying to open
84 60 : SvMemoryStream aStream(0,0);
85 60 : ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
86 60 : Reference< XInputStream > xInput( pInput );
87 :
88 : ::ucbhelper::Content aContent(
89 : rFileName, Reference < XCommandEnvironment >(),
90 60 : comphelper::getProcessComponentContext() );
91 60 : InsertCommandArgument aInsertArg;
92 60 : aInsertArg.Data = xInput;
93 :
94 60 : aInsertArg.ReplaceExisting = sal_False;
95 60 : Any aCmdArg;
96 60 : aCmdArg <<= aInsertArg;
97 117 : aContent.executeCommand( ::rtl::OUString("insert"), aCmdArg );
98 : }
99 :
100 : // it is NOT an error when the stream already exists and no truncation was desired
101 0 : catch ( const CommandAbortedException& )
102 : {
103 : // currently never an error is detected !
104 : }
105 0 : catch ( const ContentCreationException& )
106 : {
107 : }
108 57 : catch ( const Exception& )
109 : {
110 : }
111 : }
112 : }
113 :
114 : try
115 : {
116 : // create LockBytes using UCB
117 : ::ucbhelper::Content aContent(
118 : rFileName, Reference < XCommandEnvironment >(),
119 1990 : comphelper::getProcessComponentContext() );
120 : xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
121 1986 : eOpenMode, xInteractionHandler, pHandler );
122 1986 : if ( xLockBytes.Is() )
123 : {
124 1986 : pStream = new SvStream( xLockBytes );
125 1986 : pStream->SetBufferSize( 4096 );
126 1986 : pStream->SetError( xLockBytes->GetError() );
127 1986 : }
128 : }
129 0 : catch ( const CommandAbortedException& )
130 : {
131 : }
132 2 : catch ( const ContentCreationException& )
133 : {
134 : }
135 0 : catch ( const Exception& )
136 : {
137 : }
138 :
139 1988 : return pStream;
140 : }
141 :
142 : //============================================================================
143 :
144 499 : SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
145 : UcbLockBytesHandler* pHandler )
146 : {
147 499 : return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, sal_True /* bEnsureFileExists */ );
148 : }
149 :
150 0 : SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
151 : Reference < XInteractionHandler > xInteractionHandler,
152 : UcbLockBytesHandler* pHandler )
153 : {
154 0 : return lcl_CreateStream( rFileName, eOpenMode, xInteractionHandler, pHandler, sal_True /* bEnsureFileExists */ );
155 : }
156 :
157 1489 : SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
158 : sal_Bool bFileExists,
159 : UcbLockBytesHandler* pHandler )
160 : {
161 1489 : return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
162 : }
163 :
164 2801 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
165 : {
166 2801 : SvStream* pStream = NULL;
167 2801 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
168 2801 : if ( xLockBytes.Is() )
169 : {
170 2801 : pStream = new SvStream( xLockBytes );
171 2801 : pStream->SetBufferSize( 4096 );
172 2801 : pStream->SetError( xLockBytes->GetError() );
173 : }
174 :
175 2801 : return pStream;
176 : }
177 :
178 304 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
179 : {
180 304 : SvStream* pStream = NULL;
181 304 : if ( xStream->getOutputStream().is() )
182 : {
183 299 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
184 299 : if ( xLockBytes.Is() )
185 : {
186 299 : pStream = new SvStream( xLockBytes );
187 299 : pStream->SetBufferSize( 4096 );
188 299 : pStream->SetError( xLockBytes->GetError() );
189 299 : }
190 : }
191 : else
192 5 : return CreateStream( xStream->getInputStream() );
193 :
194 299 : return pStream;
195 : }
196 :
197 142 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, sal_Bool bCloseStream )
198 : {
199 142 : SvStream* pStream = NULL;
200 142 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
201 142 : if ( xLockBytes.Is() )
202 : {
203 142 : if ( !bCloseStream )
204 4 : xLockBytes->setDontClose_Impl();
205 :
206 142 : pStream = new SvStream( xLockBytes );
207 142 : pStream->SetBufferSize( 4096 );
208 142 : pStream->SetError( xLockBytes->GetError() );
209 : }
210 :
211 142 : return pStream;
212 : };
213 :
214 50 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, sal_Bool bCloseStream )
215 : {
216 50 : SvStream* pStream = NULL;
217 50 : if ( xStream->getOutputStream().is() )
218 : {
219 50 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
220 50 : if ( xLockBytes.Is() )
221 : {
222 50 : if ( !bCloseStream )
223 21 : xLockBytes->setDontClose_Impl();
224 :
225 50 : pStream = new SvStream( xLockBytes );
226 50 : pStream->SetBufferSize( 4096 );
227 50 : pStream->SetError( xLockBytes->GetError() );
228 50 : }
229 : }
230 : else
231 0 : return CreateStream( xStream->getInputStream(), bCloseStream );
232 :
233 50 : return pStream;
234 : };
235 :
236 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|