Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / sot / source / unoolestorage / xolesimplestorage.cxx
blob5a7f561b0a22bda5c269dc50608b753b8466f4b1
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 "xolesimplestorage.hxx"
22 #include <com/sun/star/embed/OLESimpleStorage.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/NoSupportException.hpp>
25 #include <com/sun/star/io/IOException.hpp>
26 #include <com/sun/star/io/XStream.hpp>
27 #include <com/sun/star/io/XInputStream.hpp>
28 #include <com/sun/star/io/XSeekable.hpp>
29 #include <com/sun/star/io/XTruncate.hpp>
30 #include <com/sun/star/io/TempFile.hpp>
32 #include <comphelper/interfacecontainer2.hxx>
33 #include <comphelper/storagehelper.hxx>
34 #include <unotools/ucbstreamhelper.hxx>
35 #include <cppuhelper/exc_hlp.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <sot/stg.hxx>
38 #include <sot/storinfo.hxx>
40 using namespace ::com::sun::star;
42 const sal_Int32 nBytesCount = 32000;
45 OLESimpleStorage::OLESimpleStorage(
46 css::uno::Reference<css::uno::XComponentContext> const & xContext,
47 css::uno::Sequence<css::uno::Any> const &aArguments)
48 : m_bDisposed( false )
49 , m_pListenersContainer( nullptr )
50 , m_xContext( xContext )
51 , m_bNoTemporaryCopy( false )
53 sal_Int32 nArgNum = aArguments.getLength();
54 if ( nArgNum < 1 || nArgNum > 2 )
55 throw lang::IllegalArgumentException(); // TODO:
57 uno::Reference< io::XStream > xStream;
58 uno::Reference< io::XInputStream > xInputStream;
59 if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
60 throw lang::IllegalArgumentException(); // TODO:
62 if ( nArgNum == 2 )
64 if ( !( aArguments[1] >>= m_bNoTemporaryCopy ) )
65 throw lang::IllegalArgumentException(); // TODO:
68 if ( m_bNoTemporaryCopy )
70 // TODO: ???
71 // If the temporary stream is not created, the original stream must be wrapped
72 // since SvStream wrapper closes the stream is owns
73 if ( xInputStream.is() )
75 // the stream must be seekable for direct access
76 uno::Reference< io::XSeekable > xSeek( xInputStream, uno::UNO_QUERY_THROW );
77 m_pStream = ::utl::UcbStreamHelper::CreateStream( xInputStream, false );
79 else if ( xStream.is() )
81 // the stream must be seekable for direct access
82 uno::Reference< io::XSeekable > xSeek( xStream, uno::UNO_QUERY_THROW );
83 m_pStream = ::utl::UcbStreamHelper::CreateStream( xStream, false );
85 else
86 throw lang::IllegalArgumentException(); // TODO:
88 else
90 uno::Reference < io::XStream > xTempFile( io::TempFile::create(m_xContext),
91 uno::UNO_QUERY_THROW );
92 uno::Reference < io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY_THROW );
93 uno::Reference< io::XOutputStream > xTempOut = xTempFile->getOutputStream();
94 if ( !xTempOut.is() )
95 throw uno::RuntimeException();
97 if ( xInputStream.is() )
99 try
101 uno::Reference< io::XSeekable > xSeek( xInputStream, uno::UNO_QUERY_THROW );
102 xSeek->seek( 0 );
104 catch( uno::Exception& )
107 ::comphelper::OStorageHelper::CopyInputToOutput( xInputStream, xTempOut );
108 xTempOut->closeOutput();
109 xTempSeek->seek( 0 );
110 uno::Reference< io::XInputStream > xTempInput = xTempFile->getInputStream();
111 m_pStream = ::utl::UcbStreamHelper::CreateStream( xTempInput, false );
113 else if ( xStream.is() )
115 // not sure that the storage flashes the stream on commit
116 m_xStream = xStream;
117 m_xTempStream = xTempFile;
119 uno::Reference< io::XSeekable > xSeek( xStream, uno::UNO_QUERY_THROW );
120 xSeek->seek( 0 );
121 uno::Reference< io::XInputStream > xInpStream = xStream->getInputStream();
122 if ( !xInpStream.is() || !xStream->getOutputStream().is() )
123 throw uno::RuntimeException();
125 ::comphelper::OStorageHelper::CopyInputToOutput( xInpStream, xTempOut );
126 xTempOut->flush();
127 xTempSeek->seek( 0 );
129 m_pStream = ::utl::UcbStreamHelper::CreateStream( xTempFile, false );
131 else
132 throw lang::IllegalArgumentException(); // TODO:
135 if ( !m_pStream || m_pStream->GetError() )
136 throw io::IOException(); // TODO
138 m_pStorage.reset(new Storage( *m_pStream, false ));
141 OLESimpleStorage::~OLESimpleStorage()
143 try {
144 osl_atomic_increment(&m_refCount);
145 dispose();
146 } catch( uno::Exception& )
149 if ( m_pListenersContainer )
151 delete m_pListenersContainer;
152 m_pListenersContainer = nullptr;
156 void OLESimpleStorage::UpdateOriginal_Impl()
158 if ( m_bNoTemporaryCopy )
159 return;
161 uno::Reference< io::XSeekable > xSeek( m_xStream, uno::UNO_QUERY_THROW );
162 xSeek->seek( 0 );
164 uno::Reference< io::XSeekable > xTempSeek( m_xTempStream, uno::UNO_QUERY_THROW );
165 sal_Int64 nPos = xTempSeek->getPosition();
166 xTempSeek->seek( 0 );
168 uno::Reference< io::XInputStream > xTempInp = m_xTempStream->getInputStream();
169 uno::Reference< io::XOutputStream > xOutputStream = m_xStream->getOutputStream();
170 if ( !xTempInp.is() || !xOutputStream.is() )
171 throw uno::RuntimeException();
173 uno::Reference< io::XTruncate > xTrunc( xOutputStream, uno::UNO_QUERY_THROW );
174 xTrunc->truncate();
176 ::comphelper::OStorageHelper::CopyInputToOutput( xTempInp, xOutputStream );
177 xOutputStream->flush();
178 xTempSeek->seek( nPos );
182 void OLESimpleStorage::InsertInputStreamToStorage_Impl( BaseStorage* pStorage, const OUString & aName, const uno::Reference< io::XInputStream >& xInputStream )
184 if ( !pStorage || aName.isEmpty() || !xInputStream.is() )
185 throw uno::RuntimeException();
187 if ( pStorage->IsContained( aName ) )
188 throw container::ElementExistException(); // TODO:
190 std::unique_ptr<BaseStorageStream> pNewStream(pStorage->OpenStream( aName ));
191 if ( !pNewStream || pNewStream->GetError() || pStorage->GetError() )
193 pNewStream.reset();
194 pStorage->ResetError();
195 throw io::IOException(); // TODO
200 uno::Sequence< sal_Int8 > aData( nBytesCount );
201 sal_Int32 nRead = 0;
204 nRead = xInputStream->readBytes( aData, nBytesCount );
206 sal_Int32 nWritten = pNewStream->Write( aData.getArray(), nRead );
207 if ( nWritten < nRead )
208 throw io::IOException();
209 } while( nRead == nBytesCount );
211 catch( uno::Exception& )
213 pNewStream.reset();
214 pStorage->Remove( aName );
216 throw;
221 void OLESimpleStorage::InsertNameAccessToStorage_Impl( BaseStorage* pStorage, const OUString & aName, const uno::Reference< container::XNameAccess >& xNameAccess )
223 if ( !pStorage || aName.isEmpty() || !xNameAccess.is() )
224 throw uno::RuntimeException();
226 if ( pStorage->IsContained( aName ) )
227 throw container::ElementExistException(); // TODO:
229 std::unique_ptr<BaseStorage> pNewStorage(pStorage->OpenStorage( aName ));
230 if ( !pNewStorage || pNewStorage->GetError() || pStorage->GetError() )
232 pNewStorage.reset();
233 pStorage->ResetError();
234 throw io::IOException(); // TODO
239 const uno::Sequence< OUString > aElements = xNameAccess->getElementNames();
240 for ( const auto& rElement : aElements )
242 uno::Reference< io::XInputStream > xInputStream;
243 uno::Reference< container::XNameAccess > xSubNameAccess;
244 uno::Any aAny = xNameAccess->getByName( rElement );
245 if ( aAny >>= xInputStream )
246 InsertInputStreamToStorage_Impl( pNewStorage.get(), rElement, xInputStream );
247 else if ( aAny >>= xSubNameAccess )
248 InsertNameAccessToStorage_Impl( pNewStorage.get(), rElement, xSubNameAccess );
251 catch( uno::Exception& )
253 pNewStorage.reset();
254 pStorage->Remove( aName );
256 throw;
261 // XNameContainer
264 void SAL_CALL OLESimpleStorage::insertByName( const OUString& aName, const uno::Any& aElement )
266 ::osl::MutexGuard aGuard( m_aMutex );
268 if ( m_bDisposed )
269 throw lang::DisposedException();
271 if ( !m_pStorage )
272 throw uno::RuntimeException();
274 uno::Reference< io::XStream > xStream;
275 uno::Reference< io::XInputStream > xInputStream;
276 uno::Reference< container::XNameAccess > xNameAccess;
280 if ( !m_bNoTemporaryCopy && !m_xStream.is() )
281 throw io::IOException(); // TODO
283 if ( aElement >>= xStream )
284 xInputStream = xStream->getInputStream();
285 else if ( !( aElement >>= xInputStream ) && !( aElement >>= xNameAccess ) )
286 throw lang::IllegalArgumentException(); // TODO:
288 if ( xInputStream.is() )
289 InsertInputStreamToStorage_Impl( m_pStorage.get(), aName, xInputStream );
290 else if ( xNameAccess.is() )
291 InsertNameAccessToStorage_Impl( m_pStorage.get(), aName, xNameAccess );
292 else
293 throw uno::RuntimeException();
295 catch( uno::RuntimeException& )
297 throw;
299 catch( container::ElementExistException& )
301 throw;
303 catch( const uno::Exception& )
305 css::uno::Any anyEx = cppu::getCaughtException();
306 throw lang::WrappedTargetException("Insert has failed!",
307 uno::Reference< uno::XInterface >(),
308 anyEx );
313 void SAL_CALL OLESimpleStorage::removeByName( const OUString& aName )
315 ::osl::MutexGuard aGuard( m_aMutex );
317 if ( m_bDisposed )
318 throw lang::DisposedException();
320 if ( !m_pStorage )
321 throw uno::RuntimeException();
323 if ( !m_bNoTemporaryCopy && !m_xStream.is() )
324 throw lang::WrappedTargetException(); // io::IOException(); // TODO
326 if ( !m_pStorage->IsContained( aName ) )
327 throw container::NoSuchElementException(); // TODO:
329 m_pStorage->Remove( aName );
331 if ( m_pStorage->GetError() )
333 m_pStorage->ResetError();
334 throw lang::WrappedTargetException(); // io::IOException(); // TODO
339 void SAL_CALL OLESimpleStorage::replaceByName( const OUString& aName, const uno::Any& aElement )
341 ::osl::MutexGuard aGuard( m_aMutex );
343 if ( m_bDisposed )
344 throw lang::DisposedException();
346 removeByName( aName );
350 insertByName( aName, aElement );
352 catch( container::ElementExistException& )
354 uno::Any aCaught( ::cppu::getCaughtException() );
356 throw lang::WrappedTargetException("Can't copy raw stream",
357 uno::Reference< uno::XInterface >(),
358 aCaught );
363 uno::Any SAL_CALL OLESimpleStorage::getByName( const OUString& aName )
365 ::osl::MutexGuard aGuard( m_aMutex );
367 if ( m_bDisposed )
368 throw lang::DisposedException();
370 if ( !m_pStorage )
371 throw uno::RuntimeException();
373 if ( !m_pStorage->IsContained( aName ) )
374 throw container::NoSuchElementException(); // TODO:
376 uno::Any aResult;
378 uno::Reference< io::XStream > xTempFile = io::TempFile::create(m_xContext);
379 uno::Reference< io::XSeekable > xSeekable( xTempFile, uno::UNO_QUERY_THROW );
380 uno::Reference< io::XOutputStream > xOutputStream = xTempFile->getOutputStream();
381 uno::Reference< io::XInputStream > xInputStream = xTempFile->getInputStream();
382 if ( !xOutputStream.is() || !xInputStream.is() )
383 throw uno::RuntimeException();
385 if ( m_pStorage->IsStorage( aName ) )
387 std::unique_ptr<BaseStorage> pStrg(m_pStorage->OpenStorage( aName ));
388 m_pStorage->ResetError();
389 if ( !pStrg )
390 throw lang::WrappedTargetException(); // io::IOException(); // TODO
392 std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( xTempFile, false ); // do not close the original stream
393 if ( !pStream )
394 throw uno::RuntimeException();
396 std::unique_ptr<BaseStorage> pNewStor(new Storage( *pStream, false ));
397 bool bSuccess = ( pStrg->CopyTo( pNewStor.get() ) && pNewStor->Commit() &&
398 !pNewStor->GetError() && !pStrg->GetError() );
400 pNewStor.reset();
401 pStrg.reset();
402 pStream.reset();
404 if ( !bSuccess )
405 throw uno::RuntimeException();
407 uno::Reference< container::XNameContainer > xResultNameContainer(
408 css::embed::OLESimpleStorage::createFromInputStream(m_xContext, xInputStream, true),
409 uno::UNO_QUERY_THROW );
411 aResult <<= xResultNameContainer;
413 else
415 std::unique_ptr<BaseStorageStream> pStream(m_pStorage->OpenStream( aName, StreamMode::READ | StreamMode::SHARE_DENYALL | StreamMode::NOCREATE ));
418 if ( !pStream || pStream->GetError() || m_pStorage->GetError() )
420 m_pStorage->ResetError();
421 throw io::IOException(); // TODO
424 uno::Sequence< sal_Int8 > aData( nBytesCount );
425 sal_Int32 nSize = nBytesCount;
426 sal_Int32 nRead = 0;
427 while( 0 != ( nRead = pStream->Read( aData.getArray(), nSize ) ) )
429 if ( nRead < nSize )
431 nSize = nRead;
432 aData.realloc( nSize );
435 xOutputStream->writeBytes( aData );
438 if ( pStream->GetError() )
439 throw io::IOException(); // TODO
441 xOutputStream->closeOutput();
442 xSeekable->seek( 0 );
444 catch (const uno::RuntimeException&)
446 throw;
448 catch (const uno::Exception& ex)
450 css::uno::Any anyEx = cppu::getCaughtException();
451 throw css::lang::WrappedTargetException( ex.Message,
452 nullptr, anyEx );
455 pStream.reset();
457 aResult <<= xInputStream;
460 return aResult;
464 uno::Sequence< OUString > SAL_CALL OLESimpleStorage::getElementNames()
466 ::osl::MutexGuard aGuard( m_aMutex );
468 if ( m_bDisposed )
469 throw lang::DisposedException();
471 if ( !m_pStorage )
472 throw uno::RuntimeException();
474 SvStorageInfoList aList;
475 m_pStorage->FillInfoList( &aList );
477 if ( m_pStorage->GetError() )
479 m_pStorage->ResetError();
480 throw uno::RuntimeException(); // TODO:
483 uno::Sequence< OUString > aSeq( aList.size() );
484 for ( size_t nInd = 0; nInd < aList.size(); nInd++ )
485 aSeq[nInd] = aList[nInd].GetName();
487 return aSeq;
491 sal_Bool SAL_CALL OLESimpleStorage::hasByName( const OUString& aName )
493 ::osl::MutexGuard aGuard( m_aMutex );
495 if ( m_bDisposed )
496 throw lang::DisposedException();
498 if ( !m_pStorage )
499 throw uno::RuntimeException();
501 bool bResult = m_pStorage->IsContained( aName );
503 if ( m_pStorage->GetError() )
505 m_pStorage->ResetError();
506 throw uno::RuntimeException(); // TODO:
509 return bResult;
513 uno::Type SAL_CALL OLESimpleStorage::getElementType()
515 ::osl::MutexGuard aGuard( m_aMutex );
517 if ( m_bDisposed )
518 throw lang::DisposedException();
520 return cppu::UnoType<io::XInputStream>::get();
524 sal_Bool SAL_CALL OLESimpleStorage::hasElements()
526 ::osl::MutexGuard aGuard( m_aMutex );
528 if ( m_bDisposed )
529 throw lang::DisposedException();
531 if ( !m_pStorage )
532 throw uno::RuntimeException();
534 SvStorageInfoList aList;
535 m_pStorage->FillInfoList( &aList );
537 if ( m_pStorage->GetError() )
539 m_pStorage->ResetError();
540 throw uno::RuntimeException(); // TODO:
543 return !aList.empty();
547 // XComponent
550 void SAL_CALL OLESimpleStorage::dispose()
552 ::osl::MutexGuard aGuard( m_aMutex );
554 if ( m_bDisposed )
555 throw lang::DisposedException();
557 if ( m_pListenersContainer )
559 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
560 m_pListenersContainer->disposeAndClear( aSource );
563 m_pStorage.reset();
564 m_pStream.reset();
566 m_xStream.clear();
567 m_xTempStream.clear();
569 m_bDisposed = true;
573 void SAL_CALL OLESimpleStorage::addEventListener(
574 const uno::Reference< lang::XEventListener >& xListener )
576 ::osl::MutexGuard aGuard( m_aMutex );
578 if ( m_bDisposed )
579 throw lang::DisposedException();
581 if ( !m_pListenersContainer )
582 m_pListenersContainer = new ::comphelper::OInterfaceContainerHelper2( m_aMutex );
584 m_pListenersContainer->addInterface( xListener );
588 void SAL_CALL OLESimpleStorage::removeEventListener(
589 const uno::Reference< lang::XEventListener >& xListener )
591 ::osl::MutexGuard aGuard( m_aMutex );
593 if ( m_bDisposed )
594 throw lang::DisposedException();
596 if ( m_pListenersContainer )
597 m_pListenersContainer->removeInterface( xListener );
601 // XTransactedObject
604 void SAL_CALL OLESimpleStorage::commit()
606 ::osl::MutexGuard aGuard( m_aMutex );
608 if ( m_bDisposed )
609 throw lang::DisposedException();
611 if ( !m_pStorage )
612 throw uno::RuntimeException();
614 if ( !m_bNoTemporaryCopy && !m_xStream.is() )
615 throw io::IOException(); // TODO
617 if ( !m_pStorage->Commit() || m_pStorage->GetError() )
619 m_pStorage->ResetError();
620 throw io::IOException(); // TODO
623 UpdateOriginal_Impl();
627 void SAL_CALL OLESimpleStorage::revert()
629 ::osl::MutexGuard aGuard( m_aMutex );
631 if ( m_bDisposed )
632 throw lang::DisposedException();
634 if ( !m_pStorage )
635 throw uno::RuntimeException();
637 if ( !m_bNoTemporaryCopy && !m_xStream.is() )
638 throw io::IOException(); // TODO
640 if ( !m_pStorage->Revert() || m_pStorage->GetError() )
642 m_pStorage->ResetError();
643 throw io::IOException(); // TODO
646 UpdateOriginal_Impl();
650 // XClassifiedObject
653 uno::Sequence< sal_Int8 > SAL_CALL OLESimpleStorage::getClassID()
655 ::osl::MutexGuard aGuard( m_aMutex );
657 if ( m_bDisposed )
658 throw lang::DisposedException();
660 if ( !m_pStorage )
661 throw uno::RuntimeException();
663 return m_pStorage->GetClassName().GetByteSequence();
666 OUString SAL_CALL OLESimpleStorage::getClassName()
668 return OUString();
671 void SAL_CALL OLESimpleStorage::setClassInfo( const uno::Sequence< sal_Int8 >& /*aClassID*/,
672 const OUString& /*sClassName*/ )
674 throw lang::NoSupportException();
677 // XServiceInfo
678 OUString SAL_CALL OLESimpleStorage::getImplementationName()
680 return "com.sun.star.comp.embed.OLESimpleStorage";
683 sal_Bool SAL_CALL OLESimpleStorage::supportsService( const OUString& ServiceName )
685 return cppu::supportsService(this, ServiceName);
688 uno::Sequence< OUString > SAL_CALL OLESimpleStorage::getSupportedServiceNames()
690 return { "com.sun.star.embed.OLESimpleStorage" };
693 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
694 com_sun_star_comp_embed_OLESimpleStorage(
695 css::uno::XComponentContext *context,
696 css::uno::Sequence<css::uno::Any> const &arguments)
698 return cppu::acquire(new OLESimpleStorage(context, arguments));
701 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */