update emoji autocorrect entries from po-files
[LibreOffice.git] / unotools / source / ucbhelper / xtempfile.cxx
blobda8321aa84af3e7d2e749614476b73473dfc7f64
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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"
28 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 , mbHasCachedPos( false )
41 mpTempFile = new ::utl::TempFile;
42 mpTempFile->EnableKillingFile ( true );
45 OTempFileService::~OTempFileService ()
47 if ( mpTempFile )
48 delete mpTempFile;
51 // XInterface
53 css::uno::Any SAL_CALL OTempFileService::queryInterface( css::uno::Type const & aType )
54 throw ( css::uno::RuntimeException, std::exception )
56 css::uno::Any aResult( OTempFileBase::queryInterface( aType ) );
57 if (!aResult.hasValue())
58 aResult = cppu::PropertySetMixin< css::io::XTempFile >::queryInterface( aType );
59 return aResult;
61 void SAL_CALL OTempFileService::acquire( )
62 throw ()
64 OTempFileBase::acquire();
66 void SAL_CALL OTempFileService::release( )
67 throw ()
69 OTempFileBase::release();
72 // XTypeProvider
74 css::uno::Sequence< css::uno::Type > SAL_CALL OTempFileService::getTypes( )
75 throw ( css::uno::RuntimeException, std::exception )
77 static ::cppu::OTypeCollection* pTypeCollection = NULL;
78 if ( pTypeCollection == NULL )
80 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
82 if ( pTypeCollection == NULL )
84 static ::cppu::OTypeCollection aTypeCollection(
85 cppu::UnoType<css::beans::XPropertySet>::get()
86 ,OTempFileBase::getTypes() );
87 pTypeCollection = &aTypeCollection;
90 return pTypeCollection->getTypes();
92 css::uno::Sequence< sal_Int8 > SAL_CALL OTempFileService::getImplementationId( )
93 throw ( css::uno::RuntimeException, std::exception )
95 return OTempFileBase::getImplementationId();
98 // XTempFile
100 sal_Bool SAL_CALL OTempFileService::getRemoveFile()
101 throw ( css::uno::RuntimeException, std::exception )
103 ::osl::MutexGuard aGuard( maMutex );
105 if ( !mpTempFile )
107 // the stream is already disconnected
108 throw css::uno::RuntimeException();
111 return mbRemoveFile;
113 void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile )
114 throw ( css::uno::RuntimeException, std::exception )
116 ::osl::MutexGuard aGuard( maMutex );
118 if ( !mpTempFile )
120 // the stream is already disconnected
121 throw css::uno::RuntimeException();
124 mbRemoveFile = _removefile;
125 mpTempFile->EnableKillingFile( mbRemoveFile );
127 OUString SAL_CALL OTempFileService::getUri()
128 throw ( css::uno::RuntimeException, std::exception )
130 ::osl::MutexGuard aGuard( maMutex );
132 if ( !mpTempFile )
134 throw css::uno::RuntimeException();
137 return OUString( mpTempFile->GetURL() );
140 OUString SAL_CALL OTempFileService::getResourceName()
141 throw ( css::uno::RuntimeException, std::exception )
143 ::osl::MutexGuard aGuard( maMutex );
145 if ( !mpTempFile )
147 throw css::uno::RuntimeException();
150 return OUString( mpTempFile->GetFileName() );
153 // XInputStream
155 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 )
158 ::osl::MutexGuard aGuard( maMutex );
159 if ( mbInClosed )
160 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
162 checkConnected();
163 if (nBytesToRead < 0)
164 throw css::io::BufferSizeExceededException( OUString(), static_cast< css::uno::XWeak * >(this));
166 aData.realloc(nBytesToRead);
168 sal_uInt32 nRead = mpStream->Read(static_cast < void* > ( aData.getArray() ), nBytesToRead);
169 checkError();
171 if (nRead < static_cast < sal_uInt32 > ( nBytesToRead ) )
172 aData.realloc( nRead );
174 if ( sal::static_int_cast<sal_uInt32>(nBytesToRead) > nRead )
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 mnCachedPos = mpStream->Tell();
179 mbHasCachedPos = true;
181 mpStream = NULL;
182 if ( mpTempFile )
183 mpTempFile->CloseStream();
186 return nRead;
188 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 )
191 ::osl::MutexGuard aGuard( maMutex );
192 if ( mbInClosed )
193 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
195 checkConnected();
196 checkError();
198 if (nMaxBytesToRead < 0)
199 throw css::io::BufferSizeExceededException( OUString(), static_cast < css::uno::XWeak * >( this ) );
201 if (mpStream->IsEof())
203 aData.realloc(0);
204 return 0;
206 else
207 return readBytes(aData, nMaxBytesToRead);
209 void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip )
210 throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
212 ::osl::MutexGuard aGuard( maMutex );
213 if ( mbInClosed )
214 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
216 checkConnected();
217 checkError();
218 mpStream->SeekRel(nBytesToSkip);
219 checkError();
221 sal_Int32 SAL_CALL OTempFileService::available( )
222 throw ( css::io::NotConnectedException, css::io::IOException, css::uno::RuntimeException, std::exception )
224 ::osl::MutexGuard aGuard( maMutex );
225 if ( mbInClosed )
226 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
228 checkConnected();
230 sal_uInt32 const nAvailable =
231 static_cast<sal_uInt32>(mpStream->remainingSize());
232 checkError();
234 return nAvailable;
236 void SAL_CALL OTempFileService::closeInput( )
237 throw ( css::io::NotConnectedException, css::io::IOException, css::uno::RuntimeException, std::exception )
239 ::osl::MutexGuard aGuard( maMutex );
240 if ( mbInClosed )
241 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
243 mbInClosed = true;
245 if ( mbOutClosed )
247 // stream will be deleted by TempFile implementation
248 mpStream = NULL;
250 if ( mpTempFile )
252 delete mpTempFile;
253 mpTempFile = NULL;
258 // XOutputStream
260 void SAL_CALL OTempFileService::writeBytes( const css::uno::Sequence< sal_Int8 >& aData )
261 throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
263 ::osl::MutexGuard aGuard( maMutex );
264 if ( mbOutClosed )
265 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
267 checkConnected();
268 sal_uInt32 nWritten = mpStream->Write(aData.getConstArray(),aData.getLength());
269 checkError();
270 if ( nWritten != (sal_uInt32)aData.getLength())
271 throw css::io::BufferSizeExceededException( OUString(),static_cast < css::uno::XWeak * > ( this ) );
273 void SAL_CALL OTempFileService::flush( )
274 throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
276 ::osl::MutexGuard aGuard( maMutex );
277 if ( mbOutClosed )
278 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
280 checkConnected();
281 mpStream->Flush();
282 checkError();
284 void SAL_CALL OTempFileService::closeOutput( )
285 throw ( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::io::IOException, css::uno::RuntimeException, std::exception )
287 ::osl::MutexGuard aGuard( maMutex );
288 if ( mbOutClosed )
289 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
291 mbOutClosed = true;
293 // 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? )
294 if ( mpStream )
296 mnCachedPos = mpStream->Tell();
297 mbHasCachedPos = true;
299 mpStream = NULL;
300 if ( mpTempFile )
301 mpTempFile->CloseStream();
304 if ( mbInClosed )
306 // stream will be deleted by TempFile implementation
307 mpStream = NULL;
309 if ( mpTempFile )
311 delete mpTempFile;
312 mpTempFile = NULL;
317 void OTempFileService::checkError () const
319 if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
320 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
322 void OTempFileService::checkConnected ()
324 if (!mpStream && mpTempFile)
326 mpStream = mpTempFile->GetStream( STREAM_STD_READWRITE );
327 if ( mpStream && mbHasCachedPos )
329 mpStream->Seek( sal::static_int_cast<sal_Size>(mnCachedPos) );
330 if ( mpStream->SvStream::GetError () == ERRCODE_NONE )
332 mbHasCachedPos = false;
333 mnCachedPos = 0;
335 else
337 mpStream = NULL;
338 mpTempFile->CloseStream();
343 if (!mpStream)
344 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
347 // XSeekable
349 void SAL_CALL OTempFileService::seek( sal_Int64 nLocation )
350 throw ( css::lang::IllegalArgumentException, css::io::IOException, css::uno::RuntimeException, std::exception )
352 ::osl::MutexGuard aGuard( maMutex );
353 checkConnected();
354 if ( nLocation < 0 || nLocation > getLength() )
355 throw css::lang::IllegalArgumentException();
357 mpStream->Seek((sal_uInt32) nLocation );
358 checkError();
360 sal_Int64 SAL_CALL OTempFileService::getPosition( )
361 throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
363 ::osl::MutexGuard aGuard( maMutex );
364 checkConnected();
366 sal_uInt32 nPos = mpStream->Tell();
367 checkError();
368 return (sal_Int64)nPos;
370 sal_Int64 SAL_CALL OTempFileService::getLength( )
371 throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
373 ::osl::MutexGuard aGuard( maMutex );
374 checkConnected();
376 sal_uInt32 nCurrentPos = mpStream->Tell();
377 checkError();
379 mpStream->Seek(STREAM_SEEK_TO_END);
380 sal_uInt32 nEndPos = mpStream->Tell();
381 mpStream->Seek(nCurrentPos);
383 checkError();
385 return (sal_Int64)nEndPos;
388 // XStream
390 css::uno::Reference< css::io::XInputStream > SAL_CALL OTempFileService::getInputStream()
391 throw ( css::uno::RuntimeException, std::exception )
393 return css::uno::Reference< css::io::XInputStream >( *this, css::uno::UNO_QUERY );
396 css::uno::Reference< css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream()
397 throw ( css::uno::RuntimeException, std::exception )
399 return css::uno::Reference< css::io::XOutputStream >( *this, css::uno::UNO_QUERY );
402 // XTruncate
404 void SAL_CALL OTempFileService::truncate()
405 throw ( css::io::IOException, css::uno::RuntimeException, std::exception )
407 ::osl::MutexGuard aGuard( maMutex );
408 checkConnected();
409 // SetStreamSize() call does not change the position
410 mpStream->Seek( 0 );
411 mpStream->SetStreamSize( 0 );
412 checkError();
415 // XServiceInfo
417 OUString SAL_CALL OTempFileService::getImplementationName()
418 throw ( css::uno::RuntimeException, std::exception )
420 return getImplementationName_Static();
423 sal_Bool SAL_CALL OTempFileService::supportsService( OUString const & rServiceName )
424 throw ( css::uno::RuntimeException, std::exception )
426 return cppu::supportsService(this, rServiceName);
429 css::uno::Sequence < OUString > SAL_CALL OTempFileService::getSupportedServiceNames()
430 throw ( css::uno::RuntimeException, std::exception )
432 return getSupportedServiceNames_Static();
435 OUString OTempFileService::getImplementationName_Static ()
437 return OUString ( "com.sun.star.io.comp.TempFile" );
439 css::uno::Sequence < OUString > OTempFileService::getSupportedServiceNames_Static()
441 css::uno::Sequence < OUString > aNames ( 1 );
442 aNames[0] = "com.sun.star.io.TempFile";
443 return aNames;
445 css::uno::Reference < css::uno::XInterface >SAL_CALL XTempFile_createInstance(
446 css::uno::Reference< css::uno::XComponentContext > const & context)
448 return static_cast< ::cppu::OWeakObject * >( new OTempFileService(context) );
451 css::uno::Reference < css::lang::XSingleComponentFactory > OTempFileService::createServiceFactory_Static()
453 return ::cppu::createSingleComponentFactory( XTempFile_createInstance, getImplementationName_Static(), getSupportedServiceNames_Static() );
457 * This function is called to get service factories for an implementation.
458 * @param pImplName name of implementation
459 * @param pServiceManager generic uno interface providing a service manager to instantiate components
460 * @param pRegistryKey registry data key to read and write component persistent data
461 * @return a component factory (generic uno interface)
463 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL utl_component_getFactory(
464 const sal_Char * pImplName, void * pServiceManager,
465 SAL_UNUSED_PARAMETER void * /*pRegistryKey*/ )
467 void * pRet = 0;
468 css::uno::Reference< css::lang::XMultiServiceFactory > xSMgr(
469 static_cast< css::lang::XMultiServiceFactory * >( pServiceManager ) );
470 css::uno::Reference< css::lang::XSingleComponentFactory > xFactory;
472 if (OTempFileService::getImplementationName_Static().equalsAscii( pImplName ) )
473 xFactory = OTempFileService::createServiceFactory_Static();
475 if ( xFactory.is() )
477 xFactory->acquire();
478 pRet = xFactory.get();
480 return pRet;
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */