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 5627 : static SvStream* lcl_CreateStream( const String& rFileName, StreamMode eOpenMode,
45 : Reference < XInteractionHandler > xInteractionHandler,
46 : UcbLockBytesHandler* pHandler, sal_Bool bEnsureFileExists )
47 : {
48 5627 : SvStream* pStream = NULL;
49 : Reference< XUniversalContentBroker > ucb(
50 : UniversalContentBroker::create(
51 5627 : comphelper::getProcessComponentContext() ) );
52 5627 : UcbLockBytesRef xLockBytes;
53 5627 : if ( eOpenMode & STREAM_WRITE )
54 : {
55 3585 : sal_Bool bTruncate = ( eOpenMode & STREAM_TRUNC ) != 0;
56 3585 : if ( bTruncate )
57 : {
58 : try
59 : {
60 : // truncate is implemented with deleting the original file
61 : ::ucbhelper::Content aCnt(
62 : rFileName, Reference < XCommandEnvironment >(),
63 6 : comphelper::getProcessComponentContext() );
64 6 : 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 3585 : if ( bEnsureFileExists || bTruncate )
80 : {
81 : try
82 : {
83 : // make sure that the desired file exists before trying to open
84 236 : SvMemoryStream aStream(0,0);
85 236 : ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
86 236 : Reference< XInputStream > xInput( pInput );
87 :
88 : ::ucbhelper::Content aContent(
89 : rFileName, Reference < XCommandEnvironment >(),
90 236 : comphelper::getProcessComponentContext() );
91 236 : InsertCommandArgument aInsertArg;
92 236 : aInsertArg.Data = xInput;
93 :
94 236 : aInsertArg.ReplaceExisting = sal_False;
95 236 : Any aCmdArg;
96 236 : aCmdArg <<= aInsertArg;
97 466 : 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 230 : 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 5631 : comphelper::getProcessComponentContext() );
120 : xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(),
121 5623 : eOpenMode, xInteractionHandler, pHandler );
122 5623 : if ( xLockBytes.Is() )
123 : {
124 5623 : pStream = new SvStream( xLockBytes );
125 5623 : pStream->SetBufferSize( 4096 );
126 5623 : pStream->SetError( xLockBytes->GetError() );
127 5623 : }
128 : }
129 0 : catch ( const CommandAbortedException& )
130 : {
131 : }
132 4 : catch ( const ContentCreationException& )
133 : {
134 : }
135 0 : catch ( const Exception& )
136 : {
137 : }
138 :
139 5627 : return pStream;
140 : }
141 :
142 : //============================================================================
143 :
144 2278 : SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
145 : UcbLockBytesHandler* pHandler )
146 : {
147 2278 : 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 3349 : SvStream* UcbStreamHelper::CreateStream( const String& rFileName, StreamMode eOpenMode,
158 : sal_Bool bFileExists,
159 : UcbLockBytesHandler* pHandler )
160 : {
161 3349 : return lcl_CreateStream( rFileName, eOpenMode, Reference < XInteractionHandler >(), pHandler, !bFileExists );
162 : }
163 :
164 5638 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream )
165 : {
166 5638 : SvStream* pStream = NULL;
167 5638 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
168 5638 : if ( xLockBytes.Is() )
169 : {
170 5638 : pStream = new SvStream( xLockBytes );
171 5638 : pStream->SetBufferSize( 4096 );
172 5638 : pStream->SetError( xLockBytes->GetError() );
173 : }
174 :
175 5638 : return pStream;
176 : }
177 :
178 641 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream )
179 : {
180 641 : SvStream* pStream = NULL;
181 641 : if ( xStream->getOutputStream().is() )
182 : {
183 627 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
184 627 : if ( xLockBytes.Is() )
185 : {
186 627 : pStream = new SvStream( xLockBytes );
187 627 : pStream->SetBufferSize( 4096 );
188 627 : pStream->SetError( xLockBytes->GetError() );
189 627 : }
190 : }
191 : else
192 14 : return CreateStream( xStream->getInputStream() );
193 :
194 627 : return pStream;
195 : }
196 :
197 280 : SvStream* UcbStreamHelper::CreateStream( Reference < XInputStream > xStream, sal_Bool bCloseStream )
198 : {
199 280 : SvStream* pStream = NULL;
200 280 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream );
201 280 : if ( xLockBytes.Is() )
202 : {
203 280 : if ( !bCloseStream )
204 12 : xLockBytes->setDontClose_Impl();
205 :
206 280 : pStream = new SvStream( xLockBytes );
207 280 : pStream->SetBufferSize( 4096 );
208 280 : pStream->SetError( xLockBytes->GetError() );
209 : }
210 :
211 280 : return pStream;
212 : };
213 :
214 122 : SvStream* UcbStreamHelper::CreateStream( Reference < XStream > xStream, sal_Bool bCloseStream )
215 : {
216 122 : SvStream* pStream = NULL;
217 122 : if ( xStream->getOutputStream().is() )
218 : {
219 122 : UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream );
220 122 : if ( xLockBytes.Is() )
221 : {
222 122 : if ( !bCloseStream )
223 66 : xLockBytes->setDontClose_Impl();
224 :
225 122 : pStream = new SvStream( xLockBytes );
226 122 : pStream->SetBufferSize( 4096 );
227 122 : pStream->SetError( xLockBytes->GetError() );
228 122 : }
229 : }
230 : else
231 0 : return CreateStream( xStream->getInputStream(), bCloseStream );
232 :
233 122 : return pStream;
234 : };
235 :
236 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|