1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
28*cdf0e10cSrcweir #include <XTempFile.hxx>
29*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
30*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
31*cdf0e10cSrcweir #include <unotools/tempfile.hxx>
32*cdf0e10cSrcweir #include <osl/file.hxx>
33*cdf0e10cSrcweir #include <unotools/configmgr.hxx>
34*cdf0e10cSrcweir #include <tools/urlobj.hxx>
35*cdf0e10cSrcweir #include <tools/debug.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace css = com::sun::star;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // copy define from desktop\source\app\appinit.cxx
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define DESKTOP_TEMPNAMEBASE_DIR	"/temp/soffice.tmp"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir OTempFileService::OTempFileService(::css::uno::Reference< ::css::uno::XComponentContext > const & context)
44*cdf0e10cSrcweir : ::cppu::PropertySetMixin< ::css::io::XTempFile >(
45*cdf0e10cSrcweir     context
46*cdf0e10cSrcweir     , static_cast< Implements >( IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET | IMPLEMENTS_PROPERTY_ACCESS )
47*cdf0e10cSrcweir     , com::sun::star::uno::Sequence< rtl::OUString >() )
48*cdf0e10cSrcweir , mpStream( NULL )
49*cdf0e10cSrcweir , mbRemoveFile( sal_True )
50*cdf0e10cSrcweir , mbInClosed( sal_False )
51*cdf0e10cSrcweir , mbOutClosed( sal_False )
52*cdf0e10cSrcweir , mnCachedPos( 0 )
53*cdf0e10cSrcweir , mbHasCachedPos( sal_False )
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	mpTempFile = new ::utl::TempFile;
57*cdf0e10cSrcweir 	mpTempFile->EnableKillingFile ( sal_True );
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir OTempFileService::~OTempFileService ()
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	if ( mpTempFile )
63*cdf0e10cSrcweir 		delete mpTempFile;
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir // XInterface
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir ::css::uno::Any SAL_CALL OTempFileService::queryInterface( ::css::uno::Type const & aType )
70*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir 	::css::uno::Any aResult( OTempFileBase::queryInterface( aType ) );
73*cdf0e10cSrcweir 	if (!aResult.hasValue())
74*cdf0e10cSrcweir 		aResult = cppu::PropertySetMixin< ::css::io::XTempFile >::queryInterface( aType ) ;
75*cdf0e10cSrcweir 	return aResult;
76*cdf0e10cSrcweir };
77*cdf0e10cSrcweir void SAL_CALL OTempFileService::acquire(  )
78*cdf0e10cSrcweir throw ()
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	OTempFileBase::acquire();
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir void SAL_CALL OTempFileService::release(  )
83*cdf0e10cSrcweir throw ()
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	OTempFileBase::release();
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //	XTypeProvider
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir ::css::uno::Sequence< ::css::uno::Type > SAL_CALL OTempFileService::getTypes(  )
91*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	static ::cppu::OTypeCollection* pTypeCollection = NULL;
94*cdf0e10cSrcweir 	if ( pTypeCollection == NULL )
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 		if ( pTypeCollection == NULL )
99*cdf0e10cSrcweir 		{
100*cdf0e10cSrcweir 			static ::cppu::OTypeCollection aTypeCollection(
101*cdf0e10cSrcweir 				::getCppuType( ( const ::css::uno::Reference< ::css::beans::XPropertySet >*)NULL )
102*cdf0e10cSrcweir 				,OTempFileBase::getTypes() );
103*cdf0e10cSrcweir 			pTypeCollection = &aTypeCollection;
104*cdf0e10cSrcweir 		}
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 	return pTypeCollection->getTypes();
107*cdf0e10cSrcweir };
108*cdf0e10cSrcweir ::css::uno::Sequence< sal_Int8 > SAL_CALL OTempFileService::getImplementationId(  )
109*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	return OTempFileBase::getImplementationId();
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir //	XTempFile
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir sal_Bool SAL_CALL OTempFileService::getRemoveFile()
117*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	if ( !mpTempFile )
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		// the stream is already disconnected
124*cdf0e10cSrcweir 		throw ::css::uno::RuntimeException();
125*cdf0e10cSrcweir 		}
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	return mbRemoveFile;
128*cdf0e10cSrcweir };
129*cdf0e10cSrcweir void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile )
130*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	if ( !mpTempFile )
135*cdf0e10cSrcweir 	{
136*cdf0e10cSrcweir 		// the stream is already disconnected
137*cdf0e10cSrcweir 		throw ::css::uno::RuntimeException();
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	mbRemoveFile = _removefile;
141*cdf0e10cSrcweir 	mpTempFile->EnableKillingFile( mbRemoveFile );
142*cdf0e10cSrcweir };
143*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OTempFileService::getUri()
144*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	if ( !mpTempFile )
149*cdf0e10cSrcweir 	{
150*cdf0e10cSrcweir 		throw ::css::uno::RuntimeException();
151*cdf0e10cSrcweir 	}
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	return ::rtl::OUString( mpTempFile->GetURL() );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir };
156*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OTempFileService::getResourceName()
157*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	if ( !mpTempFile )
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		throw ::css::uno::RuntimeException();
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	return ::rtl::OUString( mpTempFile->GetFileName() );
167*cdf0e10cSrcweir };
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir // XInputStream
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir sal_Int32 SAL_CALL OTempFileService::readBytes( ::css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
174*cdf0e10cSrcweir throw (::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
177*cdf0e10cSrcweir 	if ( mbInClosed )
178*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	checkConnected();
181*cdf0e10cSrcweir 	if (nBytesToRead < 0)
182*cdf0e10cSrcweir 		throw ::css::io::BufferSizeExceededException( ::rtl::OUString(), static_cast< ::css::uno::XWeak * >(this));
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	aData.realloc(nBytesToRead);
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	sal_uInt32 nRead = mpStream->Read(static_cast < void* > ( aData.getArray() ), nBytesToRead);
187*cdf0e10cSrcweir 	checkError();
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	if (nRead < static_cast < sal_uInt32 > ( nBytesToRead ) )
190*cdf0e10cSrcweir 		aData.realloc( nRead );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	if ( sal::static_int_cast<sal_uInt32>(nBytesToRead) > nRead )
193*cdf0e10cSrcweir 	{
194*cdf0e10cSrcweir 		// usually that means that the stream was read till the end
195*cdf0e10cSrcweir 		// 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? )
196*cdf0e10cSrcweir 		mnCachedPos = mpStream->Tell();
197*cdf0e10cSrcweir 		mbHasCachedPos = sal_True;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		mpStream = NULL;
200*cdf0e10cSrcweir 		if ( mpTempFile )
201*cdf0e10cSrcweir 			mpTempFile->CloseStream();
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	return nRead;
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir sal_Int32 SAL_CALL OTempFileService::readSomeBytes( ::css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
207*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
210*cdf0e10cSrcweir 	if ( mbInClosed )
211*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	checkConnected();
214*cdf0e10cSrcweir 	checkError();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	if (nMaxBytesToRead < 0)
217*cdf0e10cSrcweir 		throw ::css::io::BufferSizeExceededException( ::rtl::OUString(), static_cast < ::css::uno::XWeak * >( this ) );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	if (mpStream->IsEof())
220*cdf0e10cSrcweir 	{
221*cdf0e10cSrcweir 		aData.realloc(0);
222*cdf0e10cSrcweir 		return 0;
223*cdf0e10cSrcweir 	}
224*cdf0e10cSrcweir 	else
225*cdf0e10cSrcweir 		return readBytes(aData, nMaxBytesToRead);
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip )
228*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
231*cdf0e10cSrcweir 	if ( mbInClosed )
232*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	checkConnected();
235*cdf0e10cSrcweir 	checkError();
236*cdf0e10cSrcweir 	mpStream->SeekRel(nBytesToSkip);
237*cdf0e10cSrcweir 	checkError();
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir sal_Int32 SAL_CALL OTempFileService::available(  )
240*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::IOException, ::css::uno::RuntimeException )
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
243*cdf0e10cSrcweir 	if ( mbInClosed )
244*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 	checkConnected();
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	sal_uInt32 nPos = mpStream->Tell();
249*cdf0e10cSrcweir 	checkError();
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 	mpStream->Seek(STREAM_SEEK_TO_END);
252*cdf0e10cSrcweir 	checkError();
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 	sal_Int32 nAvailable = (sal_Int32)mpStream->Tell() - nPos;
255*cdf0e10cSrcweir 	mpStream->Seek(nPos);
256*cdf0e10cSrcweir 	checkError();
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 	return nAvailable;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir void SAL_CALL OTempFileService::closeInput(  )
261*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::IOException, ::css::uno::RuntimeException )
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
264*cdf0e10cSrcweir 	if ( mbInClosed )
265*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak  * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	mbInClosed = sal_True;
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 	if ( mbOutClosed )
270*cdf0e10cSrcweir 	{
271*cdf0e10cSrcweir 		// stream will be deleted by TempFile implementation
272*cdf0e10cSrcweir 		mpStream = NULL;
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 		if ( mpTempFile )
275*cdf0e10cSrcweir 		{
276*cdf0e10cSrcweir 			delete mpTempFile;
277*cdf0e10cSrcweir 			mpTempFile = NULL;
278*cdf0e10cSrcweir 		}
279*cdf0e10cSrcweir 	}
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir // XOutputStream
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir void SAL_CALL OTempFileService::writeBytes( const ::css::uno::Sequence< sal_Int8 >& aData )
285*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
286*cdf0e10cSrcweir {
287*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
288*cdf0e10cSrcweir 	if ( mbOutClosed )
289*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 	checkConnected();
292*cdf0e10cSrcweir 	sal_uInt32 nWritten = mpStream->Write(aData.getConstArray(),aData.getLength());
293*cdf0e10cSrcweir 	checkError();
294*cdf0e10cSrcweir 	if	( nWritten != (sal_uInt32)aData.getLength())
295*cdf0e10cSrcweir 		throw ::css::io::BufferSizeExceededException( ::rtl::OUString(),static_cast < ::css::uno::XWeak * > ( this ) );
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir void SAL_CALL OTempFileService::flush(  )
298*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
301*cdf0e10cSrcweir 	if ( mbOutClosed )
302*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 	checkConnected();
305*cdf0e10cSrcweir 	mpStream->Flush();
306*cdf0e10cSrcweir 	checkError();
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir void SAL_CALL OTempFileService::closeOutput(  )
309*cdf0e10cSrcweir throw ( ::css::io::NotConnectedException, ::css::io::BufferSizeExceededException, ::css::io::IOException, ::css::uno::RuntimeException )
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
312*cdf0e10cSrcweir 	if ( mbOutClosed )
313*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir 	mbOutClosed = sal_True;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 	// 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? )
318*cdf0e10cSrcweir 	if ( mpStream )
319*cdf0e10cSrcweir 	{
320*cdf0e10cSrcweir 		mnCachedPos = mpStream->Tell();
321*cdf0e10cSrcweir 		mbHasCachedPos = sal_True;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 		mpStream = NULL;
324*cdf0e10cSrcweir 		if ( mpTempFile )
325*cdf0e10cSrcweir 			mpTempFile->CloseStream();
326*cdf0e10cSrcweir 	}
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 	if ( mbInClosed )
329*cdf0e10cSrcweir 	{
330*cdf0e10cSrcweir 		// stream will be deleted by TempFile implementation
331*cdf0e10cSrcweir 		mpStream = NULL;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		if ( mpTempFile )
334*cdf0e10cSrcweir 		{
335*cdf0e10cSrcweir 			delete mpTempFile;
336*cdf0e10cSrcweir 			mpTempFile = NULL;
337*cdf0e10cSrcweir 		}
338*cdf0e10cSrcweir 	}
339*cdf0e10cSrcweir }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir void OTempFileService::checkError () const
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir 	if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
345*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
346*cdf0e10cSrcweir }
347*cdf0e10cSrcweir void OTempFileService::checkConnected ()
348*cdf0e10cSrcweir {
349*cdf0e10cSrcweir 	if (!mpStream && mpTempFile)
350*cdf0e10cSrcweir 	{
351*cdf0e10cSrcweir 		mpStream = mpTempFile->GetStream( STREAM_STD_READWRITE );
352*cdf0e10cSrcweir 		if ( mpStream && mbHasCachedPos )
353*cdf0e10cSrcweir 		{
354*cdf0e10cSrcweir 			mpStream->Seek( sal::static_int_cast<sal_Size>(mnCachedPos) );
355*cdf0e10cSrcweir 			if ( mpStream->SvStream::GetError () == ERRCODE_NONE )
356*cdf0e10cSrcweir 			{
357*cdf0e10cSrcweir 				mbHasCachedPos = sal_False;
358*cdf0e10cSrcweir 				mnCachedPos = 0;
359*cdf0e10cSrcweir 			}
360*cdf0e10cSrcweir 			else
361*cdf0e10cSrcweir 			{
362*cdf0e10cSrcweir 				mpStream = NULL;
363*cdf0e10cSrcweir 				mpTempFile->CloseStream();
364*cdf0e10cSrcweir 			}
365*cdf0e10cSrcweir 		}
366*cdf0e10cSrcweir 	}
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 	if (!mpStream)
369*cdf0e10cSrcweir 		throw ::css::io::NotConnectedException ( ::rtl::OUString(), const_cast < ::css::uno::XWeak * > ( static_cast < const ::css::uno::XWeak * > (this ) ) );
370*cdf0e10cSrcweir }
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir // XSeekable
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir void SAL_CALL OTempFileService::seek( sal_Int64 nLocation )
375*cdf0e10cSrcweir throw ( ::css::lang::IllegalArgumentException, ::css::io::IOException, ::css::uno::RuntimeException )
376*cdf0e10cSrcweir {
377*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
378*cdf0e10cSrcweir 	checkConnected();
379*cdf0e10cSrcweir 	if ( nLocation < 0 || nLocation > getLength() )
380*cdf0e10cSrcweir 		throw ::css::lang::IllegalArgumentException();
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir 	mpStream->Seek((sal_uInt32) nLocation );
383*cdf0e10cSrcweir 	checkError();
384*cdf0e10cSrcweir }
385*cdf0e10cSrcweir sal_Int64 SAL_CALL OTempFileService::getPosition(  )
386*cdf0e10cSrcweir throw ( ::css::io::IOException, ::css::uno::RuntimeException )
387*cdf0e10cSrcweir {
388*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
389*cdf0e10cSrcweir 	checkConnected();
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 	sal_uInt32 nPos = mpStream->Tell();
392*cdf0e10cSrcweir 	checkError();
393*cdf0e10cSrcweir 	return (sal_Int64)nPos;
394*cdf0e10cSrcweir }
395*cdf0e10cSrcweir sal_Int64 SAL_CALL OTempFileService::getLength(  )
396*cdf0e10cSrcweir throw ( ::css::io::IOException, ::css::uno::RuntimeException )
397*cdf0e10cSrcweir {
398*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
399*cdf0e10cSrcweir 	checkConnected();
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 	sal_uInt32 nCurrentPos = mpStream->Tell();
402*cdf0e10cSrcweir 	checkError();
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir 	mpStream->Seek(STREAM_SEEK_TO_END);
405*cdf0e10cSrcweir 	sal_uInt32 nEndPos = mpStream->Tell();
406*cdf0e10cSrcweir 	mpStream->Seek(nCurrentPos);
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir 	checkError();
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 	return (sal_Int64)nEndPos;
411*cdf0e10cSrcweir }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir // XStream
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir ::css::uno::Reference< ::css::io::XInputStream > SAL_CALL OTempFileService::getInputStream()
417*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
418*cdf0e10cSrcweir 	{
419*cdf0e10cSrcweir 	return ::css::uno::Reference< ::css::io::XInputStream >( *this, ::css::uno::UNO_QUERY );
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir ::css::uno::Reference< ::css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream()
423*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
424*cdf0e10cSrcweir 	{
425*cdf0e10cSrcweir 	return ::css::uno::Reference< ::css::io::XOutputStream >( *this, ::css::uno::UNO_QUERY );
426*cdf0e10cSrcweir 	}
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir // XTruncate
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir void SAL_CALL OTempFileService::truncate()
431*cdf0e10cSrcweir throw ( ::css::io::IOException, ::css::uno::RuntimeException )
432*cdf0e10cSrcweir {
433*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( maMutex );
434*cdf0e10cSrcweir 	checkConnected();
435*cdf0e10cSrcweir 	// SetStreamSize() call does not change the position
436*cdf0e10cSrcweir 	mpStream->Seek( 0 );
437*cdf0e10cSrcweir 	mpStream->SetStreamSize( 0 );
438*cdf0e10cSrcweir 	checkError();
439*cdf0e10cSrcweir }
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir // XServiceInfo
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OTempFileService::getImplementationName()
444*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
445*cdf0e10cSrcweir {
446*cdf0e10cSrcweir 	return getImplementationName_Static();
447*cdf0e10cSrcweir }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir sal_Bool SAL_CALL OTempFileService::supportsService( ::rtl::OUString const & rServiceName )
450*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
451*cdf0e10cSrcweir {
452*cdf0e10cSrcweir 	::css::uno::Sequence< ::rtl::OUString > aServices(getSupportedServiceNames_Static());
453*cdf0e10cSrcweir 	return rServiceName == aServices[0];
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir ::css::uno::Sequence < ::rtl::OUString > SAL_CALL OTempFileService::getSupportedServiceNames()
457*cdf0e10cSrcweir throw ( ::css::uno::RuntimeException )
458*cdf0e10cSrcweir {
459*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
460*cdf0e10cSrcweir }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir ::rtl::OUString OTempFileService::getImplementationName_Static ()
465*cdf0e10cSrcweir {
466*cdf0e10cSrcweir 	return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.comp.TempFile" ) );
467*cdf0e10cSrcweir }
468*cdf0e10cSrcweir ::css::uno::Sequence < ::rtl::OUString > OTempFileService::getSupportedServiceNames_Static()
469*cdf0e10cSrcweir {
470*cdf0e10cSrcweir 	::css::uno::Sequence < ::rtl::OUString > aNames ( 1 );
471*cdf0e10cSrcweir 	aNames[0] = ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) );
472*cdf0e10cSrcweir 	return aNames;
473*cdf0e10cSrcweir }
474*cdf0e10cSrcweir ::css::uno::Reference < ::css::uno::XInterface >SAL_CALL XTempFile_createInstance(
475*cdf0e10cSrcweir 	css::uno::Reference< ::css::uno::XComponentContext > const & context)
476*cdf0e10cSrcweir 	SAL_THROW( ( css::uno::Exception ) )
477*cdf0e10cSrcweir {
478*cdf0e10cSrcweir 	return static_cast< ::cppu::OWeakObject * >( new OTempFileService(context) );
479*cdf0e10cSrcweir }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir ::css::uno::Reference < ::css::lang::XSingleComponentFactory > OTempFileService::createServiceFactory_Static( ::css::uno::Reference < ::css::lang::XMultiServiceFactory > const & )
482*cdf0e10cSrcweir {
483*cdf0e10cSrcweir 	return ::cppu::createSingleComponentFactory( XTempFile_createInstance, getImplementationName_Static(), getSupportedServiceNames_Static() );
484*cdf0e10cSrcweir }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir // C functions to implement this as a component
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
489*cdf0e10cSrcweir     			const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
490*cdf0e10cSrcweir {
491*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
492*cdf0e10cSrcweir }
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir /**
495*cdf0e10cSrcweir  * This function is called to get service factories for an implementation.
496*cdf0e10cSrcweir  * @param pImplName name of implementation
497*cdf0e10cSrcweir  * @param pServiceManager generic uno interface providing a service manager to instantiate components
498*cdf0e10cSrcweir  * @param pRegistryKey registry data key to read and write component persistent data
499*cdf0e10cSrcweir  * @return a component factory (generic uno interface)
500*cdf0e10cSrcweir  */
501*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
502*cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
503*cdf0e10cSrcweir {
504*cdf0e10cSrcweir     void * pRet = 0;
505*cdf0e10cSrcweir 	::css::uno::Reference< ::css::lang::XMultiServiceFactory > xSMgr(
506*cdf0e10cSrcweir 		reinterpret_cast< ::css::lang::XMultiServiceFactory * >( pServiceManager ) );
507*cdf0e10cSrcweir 	::css::uno::Reference< ::css::lang::XSingleComponentFactory > xFactory;
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir 	if (OTempFileService::getImplementationName_Static().compareToAscii( pImplName ) == 0)
510*cdf0e10cSrcweir 		xFactory = OTempFileService::createServiceFactory_Static ( xSMgr );
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir 	if ( xFactory.is() )
513*cdf0e10cSrcweir 	{
514*cdf0e10cSrcweir 		xFactory->acquire();
515*cdf0e10cSrcweir 		pRet = xFactory.get();
516*cdf0e10cSrcweir 	}
517*cdf0e10cSrcweir     return pRet;
518*cdf0e10cSrcweir }
519