Bump version to 5.0-14
[LibreOffice.git] / package / source / xstor / ocompinstream.cxx
blobbcf502798b5dcb30a30aa9120ca57867a33be918
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 "ocompinstream.hxx"
21 #include <com/sun/star/embed/StorageFormats.hpp>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <cppuhelper/queryinterface.hxx>
24 #include <osl/diagnose.h>
26 #include "owriteablestream.hxx"
27 #include "xstorage.hxx"
29 using namespace ::com::sun::star;
31 OInputCompStream::OInputCompStream( OWriteStream_Impl& aImpl,
32 uno::Reference < io::XInputStream > xStream,
33 const uno::Sequence< beans::PropertyValue >& aProps,
34 sal_Int32 nStorageType )
35 : m_pImpl( &aImpl )
36 , m_rMutexRef( m_pImpl->m_rMutexRef )
37 , m_xStream( xStream )
38 , m_pInterfaceContainer( NULL )
39 , m_aProperties( aProps )
40 , m_bDisposed( false )
41 , m_nStorageType( nStorageType )
43 OSL_ENSURE( m_pImpl->m_rMutexRef.Is(), "No mutex is provided!\n" );
44 if ( !m_pImpl->m_rMutexRef.Is() )
45 throw uno::RuntimeException(); // just a disaster
47 assert(m_xStream.is());
50 OInputCompStream::OInputCompStream( uno::Reference < io::XInputStream > xStream,
51 const uno::Sequence< beans::PropertyValue >& aProps,
52 sal_Int32 nStorageType )
53 : m_pImpl( NULL )
54 , m_rMutexRef( new SotMutexHolder )
55 , m_xStream( xStream )
56 , m_pInterfaceContainer( NULL )
57 , m_aProperties( aProps )
58 , m_bDisposed( false )
59 , m_nStorageType( nStorageType )
61 assert(m_xStream.is());
64 OInputCompStream::~OInputCompStream()
67 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
69 if ( !m_bDisposed )
71 m_refCount++;
72 dispose();
75 if ( m_pInterfaceContainer )
76 delete m_pInterfaceContainer;
80 uno::Any SAL_CALL OInputCompStream::queryInterface( const uno::Type& rType )
81 throw( uno::RuntimeException, std::exception )
83 uno::Any aReturn;
85 // common interfaces
86 aReturn <<= ::cppu::queryInterface
87 ( rType
88 , static_cast<io::XInputStream*> ( this )
89 , static_cast<io::XStream*> ( this )
90 , static_cast<lang::XComponent*> ( this )
91 , static_cast<beans::XPropertySet*> ( this )
92 , static_cast<embed::XExtendedStorageStream*> ( this ) );
94 if ( aReturn.hasValue() )
95 return aReturn ;
97 if ( m_nStorageType == embed::StorageFormats::OFOPXML )
99 aReturn <<= ::cppu::queryInterface
100 ( rType
101 , static_cast<embed::XRelationshipAccess*> ( this ) );
103 if ( aReturn.hasValue() )
104 return aReturn ;
107 return OWeakObject::queryInterface( rType );
110 sal_Int32 SAL_CALL OInputCompStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
111 throw ( io::NotConnectedException,
112 io::BufferSizeExceededException,
113 io::IOException,
114 uno::RuntimeException, std::exception )
116 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
117 if ( m_bDisposed )
119 SAL_INFO("package.xstor", "Disposed!");
120 throw lang::DisposedException();
123 return m_xStream->readBytes( aData, nBytesToRead );
126 sal_Int32 SAL_CALL OInputCompStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
127 throw ( io::NotConnectedException,
128 io::BufferSizeExceededException,
129 io::IOException,
130 uno::RuntimeException, std::exception )
132 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
133 if ( m_bDisposed )
135 SAL_INFO("package.xstor", "Disposed!");
136 throw lang::DisposedException();
139 return m_xStream->readSomeBytes( aData, nMaxBytesToRead );
143 void SAL_CALL OInputCompStream::skipBytes( sal_Int32 nBytesToSkip )
144 throw ( io::NotConnectedException,
145 io::BufferSizeExceededException,
146 io::IOException,
147 uno::RuntimeException, std::exception )
149 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
150 if ( m_bDisposed )
152 SAL_INFO("package.xstor", "Disposed!");
153 throw lang::DisposedException();
156 m_xStream->skipBytes( nBytesToSkip );
160 sal_Int32 SAL_CALL OInputCompStream::available( )
161 throw ( io::NotConnectedException,
162 io::IOException,
163 uno::RuntimeException, std::exception )
165 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
166 if ( m_bDisposed )
168 SAL_INFO("package.xstor", "Disposed!");
169 throw lang::DisposedException();
172 return m_xStream->available();
176 void SAL_CALL OInputCompStream::closeInput( )
177 throw ( io::NotConnectedException,
178 io::IOException,
179 uno::RuntimeException, std::exception )
181 dispose();
184 uno::Reference< io::XInputStream > SAL_CALL OInputCompStream::getInputStream()
185 throw ( uno::RuntimeException, std::exception )
187 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
188 if ( m_bDisposed )
190 SAL_INFO("package.xstor", "Disposed!");
191 throw lang::DisposedException();
194 return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
197 uno::Reference< io::XOutputStream > SAL_CALL OInputCompStream::getOutputStream()
198 throw ( uno::RuntimeException, std::exception )
200 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
201 if ( m_bDisposed )
203 SAL_INFO("package.xstor", "Disposed!");
204 throw lang::DisposedException();
207 return uno::Reference< io::XOutputStream >();
210 void OInputCompStream::InternalDispose()
212 // can be called only by OWriteStream_Impl
213 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
214 if ( m_bDisposed )
216 SAL_INFO("package.xstor", "Disposed!");
217 throw lang::DisposedException();
220 // the source object is also a kind of locker for the current object
221 // since the listeners could dispose the object while being notified
222 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
224 if ( m_pInterfaceContainer )
225 m_pInterfaceContainer->disposeAndClear( aSource );
229 m_xStream->closeInput();
231 catch( uno::Exception& )
234 m_pImpl = NULL;
235 m_bDisposed = true;
238 void SAL_CALL OInputCompStream::dispose( )
239 throw ( uno::RuntimeException, std::exception )
241 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
242 if ( m_bDisposed )
244 SAL_INFO("package.xstor", "Disposed!");
245 throw lang::DisposedException();
248 if ( m_pInterfaceContainer )
250 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
251 m_pInterfaceContainer->disposeAndClear( aSource );
254 m_xStream->closeInput();
256 if ( m_pImpl )
258 m_pImpl->InputStreamDisposed( this );
259 m_pImpl = NULL;
262 m_bDisposed = true;
265 void SAL_CALL OInputCompStream::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
266 throw ( uno::RuntimeException, std::exception )
268 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
269 if ( m_bDisposed )
271 SAL_INFO("package.xstor", "Disposed!");
272 throw lang::DisposedException();
275 if ( !m_pInterfaceContainer )
276 m_pInterfaceContainer = new ::cppu::OInterfaceContainerHelper( m_rMutexRef->GetMutex() );
278 m_pInterfaceContainer->addInterface( xListener );
281 void SAL_CALL OInputCompStream::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
282 throw ( uno::RuntimeException, std::exception )
284 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
285 if ( m_bDisposed )
287 SAL_INFO("package.xstor", "Disposed!");
288 throw lang::DisposedException();
291 if ( m_pInterfaceContainer )
292 m_pInterfaceContainer->removeInterface( xListener );
295 sal_Bool SAL_CALL OInputCompStream::hasByID( const OUString& sID )
296 throw ( io::IOException,
297 uno::RuntimeException, std::exception )
299 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
301 if ( m_bDisposed )
303 SAL_INFO("package.xstor", "Disposed!");
304 throw lang::DisposedException();
307 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
308 throw uno::RuntimeException();
312 getRelationshipByID( sID );
313 return sal_True;
315 catch( container::NoSuchElementException& )
318 return sal_False;
321 OUString SAL_CALL OInputCompStream::getTargetByID( const OUString& sID )
322 throw ( container::NoSuchElementException,
323 io::IOException,
324 uno::RuntimeException, std::exception )
326 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
328 if ( m_bDisposed )
330 SAL_INFO("package.xstor", "Disposed!");
331 throw lang::DisposedException();
334 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
335 throw uno::RuntimeException();
337 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
338 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
339 if ( aSeq[nInd].First == "Target" )
340 return aSeq[nInd].Second;
342 return OUString();
345 OUString SAL_CALL OInputCompStream::getTypeByID( const OUString& sID )
346 throw ( container::NoSuchElementException,
347 io::IOException,
348 uno::RuntimeException, std::exception )
350 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
352 if ( m_bDisposed )
354 SAL_INFO("package.xstor", "Disposed!");
355 throw lang::DisposedException();
358 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
359 throw uno::RuntimeException();
361 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
362 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
363 if ( aSeq[nInd].First == "Type" )
364 return aSeq[nInd].Second;
366 return OUString();
369 uno::Sequence< beans::StringPair > SAL_CALL OInputCompStream::getRelationshipByID( const OUString& sID )
370 throw ( container::NoSuchElementException,
371 io::IOException,
372 uno::RuntimeException, std::exception )
374 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
376 if ( m_bDisposed )
378 SAL_INFO("package.xstor", "Disposed!");
379 throw lang::DisposedException();
382 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
383 throw uno::RuntimeException();
385 // TODO/LATER: in future the unification of the ID could be checked
386 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
387 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
388 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
389 if ( aSeq[nInd1][nInd2].First == "Id" )
391 if ( aSeq[nInd1][nInd2].Second.equals( sID ) )
392 return aSeq[nInd1];
393 break;
396 throw container::NoSuchElementException();
399 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getRelationshipsByType( const OUString& sType )
400 throw ( io::IOException,
401 uno::RuntimeException, std::exception )
403 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
405 if ( m_bDisposed )
407 SAL_INFO("package.xstor", "Disposed!");
408 throw lang::DisposedException();
411 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
412 throw uno::RuntimeException();
414 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
415 sal_Int32 nEntriesNum = 0;
417 // TODO/LATER: in future the unification of the ID could be checked
418 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
419 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
420 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
421 if ( aSeq[nInd1][nInd2].First == "Type" )
423 if ( aSeq[nInd1][nInd2].Second.equals( sType ) )
425 aResult.realloc( nEntriesNum );
426 aResult[nEntriesNum-1] = aSeq[nInd1];
428 break;
431 return aResult;
434 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getAllRelationships()
435 throw (io::IOException, uno::RuntimeException, std::exception)
437 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
439 if ( m_bDisposed )
441 SAL_INFO("package.xstor", "Disposed!");
442 throw lang::DisposedException();
445 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
446 throw uno::RuntimeException();
448 // TODO/LATER: in future the information could be taken directly from m_pImpl when possible
449 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
450 for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ )
451 if ( m_aProperties[aInd].Name == "RelationsInfo" )
453 if ( m_aProperties[aInd].Value >>= aResult )
454 return aResult;
456 break;
459 throw io::IOException(); // the relations info could not be read
462 void SAL_CALL OInputCompStream::insertRelationshipByID( const OUString& /*sID*/, const uno::Sequence< beans::StringPair >& /*aEntry*/, sal_Bool /*bReplace*/ )
463 throw ( container::ElementExistException,
464 io::IOException,
465 uno::RuntimeException, std::exception )
467 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
469 if ( m_bDisposed )
471 SAL_INFO("package.xstor", "Disposed!");
472 throw lang::DisposedException();
475 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
476 throw uno::RuntimeException();
478 throw io::IOException(); // TODO: Access denied
481 void SAL_CALL OInputCompStream::removeRelationshipByID( const OUString& /*sID*/ )
482 throw ( container::NoSuchElementException,
483 io::IOException,
484 uno::RuntimeException, std::exception )
486 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
488 if ( m_bDisposed )
490 SAL_INFO("package.xstor", "Disposed!");
491 throw lang::DisposedException();
494 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
495 throw uno::RuntimeException();
497 throw io::IOException(); // TODO: Access denied
500 void SAL_CALL OInputCompStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& /*aEntries*/, sal_Bool /*bReplace*/ )
501 throw ( container::ElementExistException,
502 io::IOException,
503 uno::RuntimeException, std::exception )
505 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
507 if ( m_bDisposed )
509 SAL_INFO("package.xstor", "Disposed!");
510 throw lang::DisposedException();
513 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
514 throw uno::RuntimeException();
516 throw io::IOException(); // TODO: Access denied
519 void SAL_CALL OInputCompStream::clearRelationships()
520 throw ( io::IOException,
521 uno::RuntimeException, std::exception )
523 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
525 if ( m_bDisposed )
527 SAL_INFO("package.xstor", "Disposed!");
528 throw lang::DisposedException();
531 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
532 throw uno::RuntimeException();
534 throw io::IOException(); // TODO: Access denied
537 uno::Reference< beans::XPropertySetInfo > SAL_CALL OInputCompStream::getPropertySetInfo()
538 throw ( uno::RuntimeException, std::exception )
540 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
542 if ( m_bDisposed )
544 SAL_INFO("package.xstor", "Disposed!");
545 throw lang::DisposedException();
548 //TODO:
549 return uno::Reference< beans::XPropertySetInfo >();
552 void SAL_CALL OInputCompStream::setPropertyValue( const OUString& aPropertyName, const uno::Any& /*aValue*/ )
553 throw ( beans::UnknownPropertyException,
554 beans::PropertyVetoException,
555 lang::IllegalArgumentException,
556 lang::WrappedTargetException,
557 uno::RuntimeException, std::exception )
559 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
561 if ( m_bDisposed )
563 SAL_INFO("package.xstor", "Disposed!");
564 throw lang::DisposedException();
567 // all the provided properties are accessible
568 for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ )
570 if ( m_aProperties[aInd].Name.equals( aPropertyName ) )
572 throw beans::PropertyVetoException(); // TODO
576 throw beans::UnknownPropertyException(); // TODO
579 uno::Any SAL_CALL OInputCompStream::getPropertyValue( const OUString& aProp )
580 throw ( beans::UnknownPropertyException,
581 lang::WrappedTargetException,
582 uno::RuntimeException, std::exception )
584 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
586 if ( m_bDisposed )
588 SAL_INFO("package.xstor", "Disposed!");
589 throw lang::DisposedException();
592 OUString aPropertyName;
593 if ( aProp == "IsEncrypted" )
594 aPropertyName = "Encrypted";
595 else
596 aPropertyName = aProp;
598 if ( aPropertyName == "RelationsInfo" )
599 throw beans::UnknownPropertyException(); // TODO
601 // all the provided properties are accessible
602 for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ )
604 if ( m_aProperties[aInd].Name.equals( aPropertyName ) )
606 return m_aProperties[aInd].Value;
610 throw beans::UnknownPropertyException(); // TODO
613 void SAL_CALL OInputCompStream::addPropertyChangeListener(
614 const OUString& /*aPropertyName*/,
615 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
616 throw ( beans::UnknownPropertyException,
617 lang::WrappedTargetException,
618 uno::RuntimeException, std::exception )
620 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
622 if ( m_bDisposed )
624 SAL_INFO("package.xstor", "Disposed!");
625 throw lang::DisposedException();
628 //TODO:
631 void SAL_CALL OInputCompStream::removePropertyChangeListener(
632 const OUString& /*aPropertyName*/,
633 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
634 throw ( beans::UnknownPropertyException,
635 lang::WrappedTargetException,
636 uno::RuntimeException, std::exception )
638 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
640 if ( m_bDisposed )
642 SAL_INFO("package.xstor", "Disposed!");
643 throw lang::DisposedException();
646 //TODO:
649 void SAL_CALL OInputCompStream::addVetoableChangeListener(
650 const OUString& /*PropertyName*/,
651 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
652 throw ( beans::UnknownPropertyException,
653 lang::WrappedTargetException,
654 uno::RuntimeException, std::exception )
656 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
658 if ( m_bDisposed )
660 SAL_INFO("package.xstor", "Disposed!");
661 throw lang::DisposedException();
664 //TODO:
667 void SAL_CALL OInputCompStream::removeVetoableChangeListener(
668 const OUString& /*PropertyName*/,
669 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
670 throw ( beans::UnknownPropertyException,
671 lang::WrappedTargetException,
672 uno::RuntimeException, std::exception )
674 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
676 if ( m_bDisposed )
678 SAL_INFO("package.xstor", "Disposed!");
679 throw lang::DisposedException();
682 //TODO:
685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */