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