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