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