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 <cppuhelper/factory.hxx>
21 : #include <cppuhelper/supportsservice.hxx>
22 : #include <cppuhelper/typeprovider.hxx>
23 : #include <osl/file.hxx>
24 : #include <unotools/configmgr.hxx>
25 : #include <unotools/tempfile.hxx>
26 : #include <XTempFile.hxx>
27 :
28 0 : OTempFileService::OTempFileService(css::uno::Reference< css::uno::XComponentContext > const & context)
29 : : ::cppu::PropertySetMixin< css::io::XTempFile >(
30 : context
31 : , static_cast< Implements >( IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET | IMPLEMENTS_PROPERTY_ACCESS )
32 : , com::sun::star::uno::Sequence< OUString >() )
33 : , mpStream( NULL )
34 : , mbRemoveFile( true )
35 : , mbInClosed( false )
36 : , mbOutClosed( false )
37 : , mnCachedPos( 0 )
38 0 : , mbHasCachedPos( false )
39 :
40 : {
41 0 : mpTempFile = new ::utl::TempFile;
42 0 : mpTempFile->EnableKillingFile ( true );
43 0 : }
44 :
45 0 : OTempFileService::~OTempFileService ()
46 : {
47 0 : if ( mpTempFile )
48 0 : delete mpTempFile;
49 0 : }
50 :
51 : // XInterface
52 :
53 0 : css::uno::Any SAL_CALL OTempFileService::queryInterface( css::uno::Type const & aType )
54 : throw ( css::uno::RuntimeException, std::exception )
55 : {
56 0 : css::uno::Any aResult( OTempFileBase::queryInterface( aType ) );
57 0 : if (!aResult.hasValue())
58 0 : aResult = cppu::PropertySetMixin< css::io::XTempFile >::queryInterface( aType );
59 0 : return aResult;
60 : };
61 0 : void SAL_CALL OTempFileService::acquire( )
62 : throw ()
63 : {
64 0 : OTempFileBase::acquire();
65 0 : }
66 0 : void SAL_CALL OTempFileService::release( )
67 : throw ()
68 : {
69 0 : OTempFileBase::release();
70 0 : }
71 :
72 : // XTypeProvider
73 :
74 0 : css::uno::Sequence< css::uno::Type > SAL_CALL OTempFileService::getTypes( )
75 : throw ( css::uno::RuntimeException, std::exception )
76 : {
77 : static ::cppu::OTypeCollection* pTypeCollection = NULL;
78 0 : if ( pTypeCollection == NULL )
79 : {
80 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
81 :
82 0 : if ( pTypeCollection == NULL )
83 : {
84 : static ::cppu::OTypeCollection aTypeCollection(
85 0 : ::getCppuType( ( const css::uno::Reference< css::beans::XPropertySet >*)NULL )
86 0 : ,OTempFileBase::getTypes() );
87 0 : pTypeCollection = &aTypeCollection;
88 0 : }
89 : }
90 0 : return pTypeCollection->getTypes();
91 : };
92 0 : css::uno::Sequence< sal_Int8 > SAL_CALL OTempFileService::getImplementationId( )
93 : throw ( css::uno::RuntimeException, std::exception )
94 : {
95 0 : return OTempFileBase::getImplementationId();
96 : }
97 :
98 : // XTempFile
99 :
100 0 : sal_Bool SAL_CALL OTempFileService::getRemoveFile()
101 : throw ( css::uno::RuntimeException, std::exception )
102 : {
103 0 : ::osl::MutexGuard aGuard( maMutex );
104 :
105 0 : if ( !mpTempFile )
106 : {
107 : // the stream is already disconnected
108 0 : throw css::uno::RuntimeException();
109 : }
110 :
111 0 : return mbRemoveFile;
112 : };
113 0 : void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile )
114 : throw ( css::uno::RuntimeException, std::exception )
115 : {
116 0 : ::osl::MutexGuard aGuard( maMutex );
117 :
118 0 : if ( !mpTempFile )
119 : {
120 : // the stream is already disconnected
121 0 : throw css::uno::RuntimeException();
122 : }
123 :
124 0 : mbRemoveFile = _removefile;
125 0 : mpTempFile->EnableKillingFile( mbRemoveFile );
126 0 : };
127 0 : OUString SAL_CALL OTempFileService::getUri()
128 : throw ( css::uno::RuntimeException, std::exception )
129 : {
130 0 : ::osl::MutexGuard aGuard( maMutex );
131 :
132 0 : if ( !mpTempFile )
133 : {
134 0 : throw css::uno::RuntimeException();
135 : }
136 :
137 0 : return OUString( mpTempFile->GetURL() );
138 :
139 : };
140 0 : OUString SAL_CALL OTempFileService::getResourceName()
141 : throw ( css::uno::RuntimeException, std::exception )
142 : {
143 0 : ::osl::MutexGuard aGuard( maMutex );
144 :
145 0 : if ( !mpTempFile )
146 : {
147 0 : throw css::uno::RuntimeException();
148 : }
149 :
150 0 : return OUString( mpTempFile->GetFileName() );
151 : };
152 :
153 : // XInputStream
154 :
155 0 : sal_Int32 SAL_CALL OTempFileService::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
156 : throw (css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
157 : {
158 0 : ::osl::MutexGuard aGuard( maMutex );
159 0 : if ( mbInClosed )
160 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
161 :
162 0 : checkConnected();
163 0 : if (nBytesToRead < 0)
164 0 : throw css::io::BufferSizeExceededException( OUString(), static_cast< css::uno::XWeak * >(this));
165 :
166 0 : aData.realloc(nBytesToRead);
167 :
168 0 : sal_uInt32 nRead = mpStream->Read(static_cast < void* > ( aData.getArray() ), nBytesToRead);
169 0 : checkError();
170 :
171 0 : if (nRead < static_cast < sal_uInt32 > ( nBytesToRead ) )
172 0 : aData.realloc( nRead );
173 :
174 0 : if ( sal::static_int_cast<sal_uInt32>(nBytesToRead) > nRead )
175 : {
176 : // usually that means that the stream was read till the end
177 : // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
178 0 : mnCachedPos = mpStream->Tell();
179 0 : mbHasCachedPos = true;
180 :
181 0 : mpStream = NULL;
182 0 : if ( mpTempFile )
183 0 : mpTempFile->CloseStream();
184 : }
185 :
186 0 : return nRead;
187 : }
188 0 : sal_Int32 SAL_CALL OTempFileService::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
189 : throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
190 : {
191 0 : ::osl::MutexGuard aGuard( maMutex );
192 0 : if ( mbInClosed )
193 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
194 :
195 0 : checkConnected();
196 0 : checkError();
197 :
198 0 : if (nMaxBytesToRead < 0)
199 0 : throw css::io::BufferSizeExceededException( OUString(), static_cast < css::uno::XWeak * >( this ) );
200 :
201 0 : if (mpStream->IsEof())
202 : {
203 0 : aData.realloc(0);
204 0 : return 0;
205 : }
206 : else
207 0 : return readBytes(aData, nMaxBytesToRead);
208 : }
209 0 : void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip )
210 : throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
211 : {
212 0 : ::osl::MutexGuard aGuard( maMutex );
213 0 : if ( mbInClosed )
214 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
215 :
216 0 : checkConnected();
217 0 : checkError();
218 0 : mpStream->SeekRel(nBytesToSkip);
219 0 : checkError();
220 0 : }
221 0 : sal_Int32 SAL_CALL OTempFileService::available( )
222 : throw ( css::io::NotConnectedException, css::io::IOException, css::uno::RuntimeException, std::exception )
223 : {
224 0 : ::osl::MutexGuard aGuard( maMutex );
225 0 : if ( mbInClosed )
226 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
227 :
228 0 : checkConnected();
229 :
230 0 : sal_uInt32 nPos = mpStream->Tell();
231 0 : checkError();
232 :
233 0 : mpStream->Seek(STREAM_SEEK_TO_END);
234 0 : checkError();
235 :
236 0 : sal_Int32 nAvailable = (sal_Int32)mpStream->Tell() - nPos;
237 0 : mpStream->Seek(nPos);
238 0 : checkError();
239 :
240 0 : return nAvailable;
241 : }
242 0 : void SAL_CALL OTempFileService::closeInput( )
243 : throw ( css::io::NotConnectedException, css::io::IOException, css::uno::RuntimeException, std::exception )
244 : {
245 0 : ::osl::MutexGuard aGuard( maMutex );
246 0 : if ( mbInClosed )
247 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
248 :
249 0 : mbInClosed = true;
250 :
251 0 : if ( mbOutClosed )
252 : {
253 : // stream will be deleted by TempFile implementation
254 0 : mpStream = NULL;
255 :
256 0 : if ( mpTempFile )
257 : {
258 0 : delete mpTempFile;
259 0 : mpTempFile = NULL;
260 : }
261 0 : }
262 0 : }
263 :
264 : // XOutputStream
265 :
266 0 : void SAL_CALL OTempFileService::writeBytes( const css::uno::Sequence< sal_Int8 >& aData )
267 : throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
268 : {
269 0 : ::osl::MutexGuard aGuard( maMutex );
270 0 : if ( mbOutClosed )
271 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
272 :
273 0 : checkConnected();
274 0 : sal_uInt32 nWritten = mpStream->Write(aData.getConstArray(),aData.getLength());
275 0 : checkError();
276 0 : if ( nWritten != (sal_uInt32)aData.getLength())
277 0 : throw css::io::BufferSizeExceededException( OUString(),static_cast < css::uno::XWeak * > ( this ) );
278 0 : }
279 0 : void SAL_CALL OTempFileService::flush( )
280 : throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
281 : {
282 0 : ::osl::MutexGuard aGuard( maMutex );
283 0 : if ( mbOutClosed )
284 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
285 :
286 0 : checkConnected();
287 0 : mpStream->Flush();
288 0 : checkError();
289 0 : }
290 0 : void SAL_CALL OTempFileService::closeOutput( )
291 : throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
292 : {
293 0 : ::osl::MutexGuard aGuard( maMutex );
294 0 : if ( mbOutClosed )
295 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
296 :
297 0 : mbOutClosed = true;
298 :
299 : // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
300 0 : if ( mpStream )
301 : {
302 0 : mnCachedPos = mpStream->Tell();
303 0 : mbHasCachedPos = true;
304 :
305 0 : mpStream = NULL;
306 0 : if ( mpTempFile )
307 0 : mpTempFile->CloseStream();
308 : }
309 :
310 0 : if ( mbInClosed )
311 : {
312 : // stream will be deleted by TempFile implementation
313 0 : mpStream = NULL;
314 :
315 0 : if ( mpTempFile )
316 : {
317 0 : delete mpTempFile;
318 0 : mpTempFile = NULL;
319 : }
320 0 : }
321 0 : }
322 :
323 0 : void OTempFileService::checkError () const
324 : {
325 0 : if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
326 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
327 0 : }
328 0 : void OTempFileService::checkConnected ()
329 : {
330 0 : if (!mpStream && mpTempFile)
331 : {
332 0 : mpStream = mpTempFile->GetStream( STREAM_STD_READWRITE );
333 0 : if ( mpStream && mbHasCachedPos )
334 : {
335 0 : mpStream->Seek( sal::static_int_cast<sal_Size>(mnCachedPos) );
336 0 : if ( mpStream->SvStream::GetError () == ERRCODE_NONE )
337 : {
338 0 : mbHasCachedPos = false;
339 0 : mnCachedPos = 0;
340 : }
341 : else
342 : {
343 0 : mpStream = NULL;
344 0 : mpTempFile->CloseStream();
345 : }
346 : }
347 : }
348 :
349 0 : if (!mpStream)
350 0 : throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
351 0 : }
352 :
353 : // XSeekable
354 :
355 0 : void SAL_CALL OTempFileService::seek( sal_Int64 nLocation )
356 : throw ( css::lang::IllegalArgumentException, css::io::IOException, css::uno::RuntimeException, std::exception )
357 : {
358 0 : ::osl::MutexGuard aGuard( maMutex );
359 0 : checkConnected();
360 0 : if ( nLocation < 0 || nLocation > getLength() )
361 0 : throw css::lang::IllegalArgumentException();
362 :
363 0 : mpStream->Seek((sal_uInt32) nLocation );
364 0 : checkError();
365 0 : }
366 0 : sal_Int64 SAL_CALL OTempFileService::getPosition( )
367 : throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
368 : {
369 0 : ::osl::MutexGuard aGuard( maMutex );
370 0 : checkConnected();
371 :
372 0 : sal_uInt32 nPos = mpStream->Tell();
373 0 : checkError();
374 0 : return (sal_Int64)nPos;
375 : }
376 0 : sal_Int64 SAL_CALL OTempFileService::getLength( )
377 : throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
378 : {
379 0 : ::osl::MutexGuard aGuard( maMutex );
380 0 : checkConnected();
381 :
382 0 : sal_uInt32 nCurrentPos = mpStream->Tell();
383 0 : checkError();
384 :
385 0 : mpStream->Seek(STREAM_SEEK_TO_END);
386 0 : sal_uInt32 nEndPos = mpStream->Tell();
387 0 : mpStream->Seek(nCurrentPos);
388 :
389 0 : checkError();
390 :
391 0 : return (sal_Int64)nEndPos;
392 : }
393 :
394 : // XStream
395 :
396 0 : css::uno::Reference< css::io::XInputStream > SAL_CALL OTempFileService::getInputStream()
397 : throw ( css::uno::RuntimeException, std::exception )
398 : {
399 0 : return css::uno::Reference< css::io::XInputStream >( *this, css::uno::UNO_QUERY );
400 : }
401 :
402 0 : css::uno::Reference< css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream()
403 : throw ( css::uno::RuntimeException, std::exception )
404 : {
405 0 : return css::uno::Reference< css::io::XOutputStream >( *this, css::uno::UNO_QUERY );
406 : }
407 :
408 : // XTruncate
409 :
410 0 : void SAL_CALL OTempFileService::truncate()
411 : throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
412 : {
413 0 : ::osl::MutexGuard aGuard( maMutex );
414 0 : checkConnected();
415 : // SetStreamSize() call does not change the position
416 0 : mpStream->Seek( 0 );
417 0 : mpStream->SetStreamSize( 0 );
418 0 : checkError();
419 0 : }
420 :
421 : // XServiceInfo
422 :
423 0 : OUString SAL_CALL OTempFileService::getImplementationName()
424 : throw ( css::uno::RuntimeException, std::exception )
425 : {
426 0 : return getImplementationName_Static();
427 : }
428 :
429 0 : sal_Bool SAL_CALL OTempFileService::supportsService( OUString const & rServiceName )
430 : throw ( css::uno::RuntimeException, std::exception )
431 : {
432 0 : return cppu::supportsService(this, rServiceName);
433 : }
434 :
435 0 : css::uno::Sequence < OUString > SAL_CALL OTempFileService::getSupportedServiceNames()
436 : throw ( css::uno::RuntimeException, std::exception )
437 : {
438 0 : return getSupportedServiceNames_Static();
439 : }
440 :
441 0 : OUString OTempFileService::getImplementationName_Static ()
442 : {
443 0 : return OUString ( "com.sun.star.io.comp.TempFile" );
444 : }
445 0 : css::uno::Sequence < OUString > OTempFileService::getSupportedServiceNames_Static()
446 : {
447 0 : css::uno::Sequence < OUString > aNames ( 1 );
448 0 : aNames[0] = "com.sun.star.io.TempFile";
449 0 : return aNames;
450 : }
451 0 : css::uno::Reference < css::uno::XInterface >SAL_CALL XTempFile_createInstance(
452 : css::uno::Reference< css::uno::XComponentContext > const & context)
453 : SAL_THROW( ( css::uno::Exception ) )
454 : {
455 0 : return static_cast< ::cppu::OWeakObject * >( new OTempFileService(context) );
456 : }
457 :
458 0 : css::uno::Reference < css::lang::XSingleComponentFactory > OTempFileService::createServiceFactory_Static()
459 : {
460 0 : return ::cppu::createSingleComponentFactory( XTempFile_createInstance, getImplementationName_Static(), getSupportedServiceNames_Static() );
461 : }
462 :
463 : /**
464 : * This function is called to get service factories for an implementation.
465 : * @param pImplName name of implementation
466 : * @param pServiceManager generic uno interface providing a service manager to instantiate components
467 : * @param pRegistryKey registry data key to read and write component persistent data
468 : * @return a component factory (generic uno interface)
469 : */
470 0 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL utl_component_getFactory(
471 : const sal_Char * pImplName, void * pServiceManager,
472 : SAL_UNUSED_PARAMETER void * /*pRegistryKey*/ )
473 : {
474 0 : void * pRet = 0;
475 : css::uno::Reference< css::lang::XMultiServiceFactory > xSMgr(
476 0 : reinterpret_cast< css::lang::XMultiServiceFactory * >( pServiceManager ) );
477 0 : css::uno::Reference< css::lang::XSingleComponentFactory > xFactory;
478 :
479 0 : if (OTempFileService::getImplementationName_Static().equalsAscii( pImplName ) )
480 0 : xFactory = OTempFileService::createServiceFactory_Static();
481 :
482 0 : if ( xFactory.is() )
483 : {
484 0 : xFactory->acquire();
485 0 : pRet = xFactory.get();
486 : }
487 0 : return pRet;
488 : }
489 :
490 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|