fix toolbar import
[ooovba.git] / package / source / xstor / ocompinstream.cxx
blob33c42e3ca901a24c83973efa70cd2914a4d1b273
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ocompinstream.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
34 #include "ocompinstream.hxx"
35 #include <com/sun/star/lang/DisposedException.hpp>
36 #include <osl/diagnose.h>
38 #include "owriteablestream.hxx"
39 #include "xstorage.hxx"
41 using namespace ::com::sun::star;
43 //-----------------------------------------------
44 OInputCompStream::OInputCompStream( OWriteStream_Impl& aImpl,
45 uno::Reference < io::XInputStream > xStream,
46 const uno::Sequence< beans::PropertyValue >& aProps,
47 sal_Int16 nStorageType )
48 : m_pImpl( &aImpl )
49 , m_rMutexRef( m_pImpl->m_rMutexRef )
50 , m_xStream( xStream )
51 , m_pInterfaceContainer( NULL )
52 , m_aProperties( aProps )
53 , m_bDisposed( sal_False )
54 , m_nStorageType( nStorageType )
56 OSL_ENSURE( m_pImpl->m_rMutexRef.Is(), "No mutex is provided!\n" );
57 if ( !m_pImpl->m_rMutexRef.Is() )
58 throw uno::RuntimeException(); // just a disaster
60 OSL_ENSURE( xStream.is(), "No stream is provided!\n" );
63 //-----------------------------------------------
64 OInputCompStream::OInputCompStream( uno::Reference < io::XInputStream > xStream,
65 const uno::Sequence< beans::PropertyValue >& aProps,
66 sal_Int16 nStorageType )
67 : m_pImpl( NULL )
68 , m_rMutexRef( new SotMutexHolder )
69 , m_xStream( xStream )
70 , m_pInterfaceContainer( NULL )
71 , m_aProperties( aProps )
72 , m_bDisposed( sal_False )
73 , m_nStorageType( nStorageType )
75 OSL_ENSURE( xStream.is(), "No stream is provided!\n" );
78 //-----------------------------------------------
79 OInputCompStream::~OInputCompStream()
82 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
84 if ( !m_bDisposed )
86 m_refCount++;
87 dispose();
90 if ( m_pInterfaceContainer )
91 delete m_pInterfaceContainer;
95 //-----------------------------------------------
96 uno::Any SAL_CALL OInputCompStream::queryInterface( const uno::Type& rType )
97 throw( uno::RuntimeException )
99 uno::Any aReturn;
101 // common interfaces
102 aReturn <<= ::cppu::queryInterface
103 ( rType
104 , static_cast<io::XInputStream*> ( this )
105 , static_cast<io::XStream*> ( this )
106 , static_cast<lang::XComponent*> ( this )
107 , static_cast<beans::XPropertySet*> ( this )
108 , static_cast<embed::XExtendedStorageStream*> ( this ) );
110 if ( aReturn.hasValue() == sal_True )
111 return aReturn ;
113 if ( m_nStorageType == OFOPXML_STORAGE )
115 aReturn <<= ::cppu::queryInterface
116 ( rType
117 , static_cast<embed::XRelationshipAccess*> ( this ) );
119 if ( aReturn.hasValue() == sal_True )
120 return aReturn ;
123 return OWeakObject::queryInterface( rType );
126 //-----------------------------------------------
127 sal_Int32 SAL_CALL OInputCompStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
128 throw ( io::NotConnectedException,
129 io::BufferSizeExceededException,
130 io::IOException,
131 uno::RuntimeException )
133 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
134 if ( m_bDisposed )
135 throw lang::DisposedException();
137 if ( !m_xStream.is() )
138 throw uno::RuntimeException();
140 return m_xStream->readBytes( aData, nBytesToRead );
143 //-----------------------------------------------
144 sal_Int32 SAL_CALL OInputCompStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
145 throw ( io::NotConnectedException,
146 io::BufferSizeExceededException,
147 io::IOException,
148 uno::RuntimeException )
150 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
151 if ( m_bDisposed )
152 throw lang::DisposedException();
154 if ( !m_xStream.is() )
155 throw uno::RuntimeException();
157 return m_xStream->readSomeBytes( aData, nMaxBytesToRead );
161 //-----------------------------------------------
162 void SAL_CALL OInputCompStream::skipBytes( sal_Int32 nBytesToSkip )
163 throw ( io::NotConnectedException,
164 io::BufferSizeExceededException,
165 io::IOException,
166 uno::RuntimeException )
168 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
169 if ( m_bDisposed )
170 throw lang::DisposedException();
172 if ( !m_xStream.is() )
173 throw uno::RuntimeException();
175 m_xStream->skipBytes( nBytesToSkip );
179 //-----------------------------------------------
180 sal_Int32 SAL_CALL OInputCompStream::available( )
181 throw ( io::NotConnectedException,
182 io::IOException,
183 uno::RuntimeException )
185 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
186 if ( m_bDisposed )
187 throw lang::DisposedException();
189 if ( !m_xStream.is() )
190 throw uno::RuntimeException();
192 return m_xStream->available();
196 //-----------------------------------------------
197 void SAL_CALL OInputCompStream::closeInput( )
198 throw ( io::NotConnectedException,
199 io::IOException,
200 uno::RuntimeException )
202 dispose();
205 //-----------------------------------------------
206 uno::Reference< io::XInputStream > SAL_CALL OInputCompStream::getInputStream()
207 throw ( uno::RuntimeException )
209 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
210 if ( m_bDisposed )
211 throw lang::DisposedException();
213 if ( !m_xStream.is() )
214 return uno::Reference< io::XInputStream >();
216 return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
219 //-----------------------------------------------
220 uno::Reference< io::XOutputStream > SAL_CALL OInputCompStream::getOutputStream()
221 throw ( uno::RuntimeException )
223 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
224 if ( m_bDisposed )
225 throw lang::DisposedException();
227 return uno::Reference< io::XOutputStream >();
230 //-----------------------------------------------
231 void OInputCompStream::InternalDispose()
233 // can be called only by OWriteStream_Impl
234 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
235 if ( m_bDisposed )
236 throw lang::DisposedException();
238 // the source object is also a kind of locker for the current object
239 // since the listeners could dispose the object while being notified
240 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
242 if ( m_pInterfaceContainer )
243 m_pInterfaceContainer->disposeAndClear( aSource );
247 m_xStream->closeInput();
249 catch( uno::Exception& )
252 m_pImpl = NULL;
253 m_bDisposed = sal_True;
256 //-----------------------------------------------
257 void SAL_CALL OInputCompStream::dispose( )
258 throw ( uno::RuntimeException )
260 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
261 if ( m_bDisposed )
262 throw lang::DisposedException();
264 if ( m_pInterfaceContainer )
266 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
267 m_pInterfaceContainer->disposeAndClear( aSource );
270 m_xStream->closeInput();
272 if ( m_pImpl )
274 m_pImpl->InputStreamDisposed( this );
275 m_pImpl = NULL;
278 m_bDisposed = sal_True;
281 //-----------------------------------------------
282 void SAL_CALL OInputCompStream::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
283 throw ( uno::RuntimeException )
285 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
286 if ( m_bDisposed )
287 throw lang::DisposedException();
289 if ( !m_pInterfaceContainer )
290 m_pInterfaceContainer = new ::cppu::OInterfaceContainerHelper( m_rMutexRef->GetMutex() );
292 m_pInterfaceContainer->addInterface( xListener );
295 //-----------------------------------------------
296 void SAL_CALL OInputCompStream::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
297 throw ( uno::RuntimeException )
299 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
300 if ( m_bDisposed )
301 throw lang::DisposedException();
303 if ( m_pInterfaceContainer )
304 m_pInterfaceContainer->removeInterface( xListener );
307 //-----------------------------------------------
308 sal_Bool SAL_CALL OInputCompStream::hasByID( const ::rtl::OUString& sID )
309 throw ( io::IOException,
310 uno::RuntimeException )
312 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
314 if ( m_bDisposed )
315 throw lang::DisposedException();
317 if ( m_nStorageType != OFOPXML_STORAGE )
318 throw uno::RuntimeException();
322 getRelationshipByID( sID );
323 return sal_True;
325 catch( container::NoSuchElementException& )
328 return sal_False;
331 //-----------------------------------------------
332 ::rtl::OUString SAL_CALL OInputCompStream::getTargetByID( const ::rtl::OUString& sID )
333 throw ( container::NoSuchElementException,
334 io::IOException,
335 uno::RuntimeException )
337 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
339 if ( m_bDisposed )
340 throw lang::DisposedException();
342 if ( m_nStorageType != OFOPXML_STORAGE )
343 throw uno::RuntimeException();
345 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
346 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
347 if ( aSeq[nInd].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Target" ) ) )
348 return aSeq[nInd].Second;
350 return ::rtl::OUString();
353 //-----------------------------------------------
354 ::rtl::OUString SAL_CALL OInputCompStream::getTypeByID( const ::rtl::OUString& sID )
355 throw ( container::NoSuchElementException,
356 io::IOException,
357 uno::RuntimeException )
359 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
361 if ( m_bDisposed )
362 throw lang::DisposedException();
364 if ( m_nStorageType != OFOPXML_STORAGE )
365 throw uno::RuntimeException();
367 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
368 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
369 if ( aSeq[nInd].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Type" ) ) )
370 return aSeq[nInd].Second;
372 return ::rtl::OUString();
375 //-----------------------------------------------
376 uno::Sequence< beans::StringPair > SAL_CALL OInputCompStream::getRelationshipByID( const ::rtl::OUString& sID )
377 throw ( container::NoSuchElementException,
378 io::IOException,
379 uno::RuntimeException )
381 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
383 if ( m_bDisposed )
384 throw lang::DisposedException();
386 if ( m_nStorageType != OFOPXML_STORAGE )
387 throw uno::RuntimeException();
389 // TODO/LATER: in future the unification of the ID could be checked
390 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
391 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
392 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
393 if ( aSeq[nInd1][nInd2].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Id" ) ) )
395 if ( aSeq[nInd1][nInd2].Second.equals( sID ) )
396 return aSeq[nInd1];
397 break;
400 throw container::NoSuchElementException();
403 //-----------------------------------------------
404 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getRelationshipsByType( const ::rtl::OUString& sType )
405 throw ( io::IOException,
406 uno::RuntimeException )
408 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
410 if ( m_bDisposed )
411 throw lang::DisposedException();
413 if ( m_nStorageType != OFOPXML_STORAGE )
414 throw uno::RuntimeException();
416 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
417 sal_Int32 nEntriesNum = 0;
419 // TODO/LATER: in future the unification of the ID could be checked
420 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
421 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
422 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
423 if ( aSeq[nInd1][nInd2].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Type" ) ) )
425 if ( aSeq[nInd1][nInd2].Second.equals( sType ) )
427 aResult.realloc( nEntriesNum );
428 aResult[nEntriesNum-1] = aSeq[nInd1];
430 break;
433 return aResult;
436 //-----------------------------------------------
437 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getAllRelationships()
438 throw (io::IOException, uno::RuntimeException)
440 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
442 if ( m_bDisposed )
443 throw lang::DisposedException();
445 if ( m_nStorageType != OFOPXML_STORAGE )
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.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "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 //-----------------------------------------------
463 void SAL_CALL OInputCompStream::insertRelationshipByID( const ::rtl::OUString& /*sID*/, const uno::Sequence< beans::StringPair >& /*aEntry*/, ::sal_Bool /*bReplace*/ )
464 throw ( container::ElementExistException,
465 io::IOException,
466 uno::RuntimeException )
468 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
470 if ( m_bDisposed )
471 throw lang::DisposedException();
473 if ( m_nStorageType != OFOPXML_STORAGE )
474 throw uno::RuntimeException();
476 throw io::IOException(); // TODO: Access denied
479 //-----------------------------------------------
480 void SAL_CALL OInputCompStream::removeRelationshipByID( const ::rtl::OUString& /*sID*/ )
481 throw ( container::NoSuchElementException,
482 io::IOException,
483 uno::RuntimeException )
485 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
487 if ( m_bDisposed )
488 throw lang::DisposedException();
490 if ( m_nStorageType != OFOPXML_STORAGE )
491 throw uno::RuntimeException();
493 throw io::IOException(); // TODO: Access denied
496 //-----------------------------------------------
497 void SAL_CALL OInputCompStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& /*aEntries*/, ::sal_Bool /*bReplace*/ )
498 throw ( container::ElementExistException,
499 io::IOException,
500 uno::RuntimeException )
502 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
504 if ( m_bDisposed )
505 throw lang::DisposedException();
507 if ( m_nStorageType != OFOPXML_STORAGE )
508 throw uno::RuntimeException();
510 throw io::IOException(); // TODO: Access denied
513 //-----------------------------------------------
514 void SAL_CALL OInputCompStream::clearRelationships()
515 throw ( io::IOException,
516 uno::RuntimeException )
518 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
520 if ( m_bDisposed )
521 throw lang::DisposedException();
523 if ( m_nStorageType != OFOPXML_STORAGE )
524 throw uno::RuntimeException();
526 throw io::IOException(); // TODO: Access denied
529 //-----------------------------------------------
530 uno::Reference< beans::XPropertySetInfo > SAL_CALL OInputCompStream::getPropertySetInfo()
531 throw ( uno::RuntimeException )
533 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
535 if ( m_bDisposed )
536 throw lang::DisposedException();
538 //TODO:
539 return uno::Reference< beans::XPropertySetInfo >();
542 //-----------------------------------------------
543 void SAL_CALL OInputCompStream::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& /*aValue*/ )
544 throw ( beans::UnknownPropertyException,
545 beans::PropertyVetoException,
546 lang::IllegalArgumentException,
547 lang::WrappedTargetException,
548 uno::RuntimeException )
550 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
552 if ( m_bDisposed )
553 throw lang::DisposedException();
555 // all the provided properties are accessible
556 for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ )
558 if ( m_aProperties[aInd].Name.equals( aPropertyName ) )
560 throw beans::PropertyVetoException(); // TODO
564 throw beans::UnknownPropertyException(); // TODO
568 //-----------------------------------------------
569 uno::Any SAL_CALL OInputCompStream::getPropertyValue( const ::rtl::OUString& aProp )
570 throw ( beans::UnknownPropertyException,
571 lang::WrappedTargetException,
572 uno::RuntimeException )
574 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
576 if ( m_bDisposed )
577 throw lang::DisposedException();
579 ::rtl::OUString aPropertyName;
580 if ( aProp.equalsAscii( "IsEncrypted" ) )
581 aPropertyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Encrypted" ) );
582 else
583 aPropertyName = aProp;
585 if ( aPropertyName.equalsAscii( "RelationsInfo" ) )
586 throw beans::UnknownPropertyException(); // TODO
588 // all the provided properties are accessible
589 for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ )
591 if ( m_aProperties[aInd].Name.equals( aPropertyName ) )
593 return m_aProperties[aInd].Value;
597 throw beans::UnknownPropertyException(); // TODO
601 //-----------------------------------------------
602 void SAL_CALL OInputCompStream::addPropertyChangeListener(
603 const ::rtl::OUString& /*aPropertyName*/,
604 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
605 throw ( beans::UnknownPropertyException,
606 lang::WrappedTargetException,
607 uno::RuntimeException )
609 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
611 if ( m_bDisposed )
612 throw lang::DisposedException();
614 //TODO:
618 //-----------------------------------------------
619 void SAL_CALL OInputCompStream::removePropertyChangeListener(
620 const ::rtl::OUString& /*aPropertyName*/,
621 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
622 throw ( beans::UnknownPropertyException,
623 lang::WrappedTargetException,
624 uno::RuntimeException )
626 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
628 if ( m_bDisposed )
629 throw lang::DisposedException();
631 //TODO:
635 //-----------------------------------------------
636 void SAL_CALL OInputCompStream::addVetoableChangeListener(
637 const ::rtl::OUString& /*PropertyName*/,
638 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
639 throw ( beans::UnknownPropertyException,
640 lang::WrappedTargetException,
641 uno::RuntimeException )
643 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
645 if ( m_bDisposed )
646 throw lang::DisposedException();
648 //TODO:
652 //-----------------------------------------------
653 void SAL_CALL OInputCompStream::removeVetoableChangeListener(
654 const ::rtl::OUString& /*PropertyName*/,
655 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
656 throw ( beans::UnknownPropertyException,
657 lang::WrappedTargetException,
658 uno::RuntimeException )
660 ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
662 if ( m_bDisposed )
663 throw lang::DisposedException();
665 //TODO: