Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / package / source / xstor / ocompinstream.cxx
blobb5931c0c0918053565724b432767990b6071f6bb
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/io/IOException.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <comphelper/sequence.hxx>
25 #include <cppuhelper/queryinterface.hxx>
26 #include <osl/diagnose.h>
27 #include <sal/log.hxx>
28 #include <utility>
30 #include "owriteablestream.hxx"
32 using namespace ::com::sun::star;
34 OInputCompStream::OInputCompStream( OWriteStream_Impl& aImpl,
35 uno::Reference < io::XInputStream > xStream,
36 const uno::Sequence< beans::PropertyValue >& aProps,
37 sal_Int32 nStorageType )
38 : m_pImpl( &aImpl )
39 , m_xMutex( m_pImpl->m_xMutex )
40 , m_xStream(std::move( xStream ))
41 , m_aProperties( aProps )
42 , m_bDisposed( false )
43 , m_nStorageType( nStorageType )
45 OSL_ENSURE( m_pImpl->m_xMutex.is(), "No mutex is provided!" );
46 if ( !m_pImpl->m_xMutex.is() )
47 throw uno::RuntimeException(); // just a disaster
49 assert(m_xStream.is());
52 OInputCompStream::OInputCompStream( uno::Reference < io::XInputStream > xStream,
53 const uno::Sequence< beans::PropertyValue >& aProps,
54 sal_Int32 nStorageType )
55 : m_pImpl( nullptr )
56 , m_xMutex( new comphelper::RefCountedMutex )
57 , m_xStream(std::move( xStream ))
58 , m_aProperties( aProps )
59 , m_bDisposed( false )
60 , m_nStorageType( nStorageType )
62 assert(m_xStream.is());
65 OInputCompStream::~OInputCompStream()
67 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
69 if ( !m_bDisposed )
71 osl_atomic_increment(&m_refCount);
72 dispose();
76 uno::Any SAL_CALL OInputCompStream::queryInterface( const uno::Type& rType )
78 // common interfaces
79 uno::Any aReturn = ::cppu::queryInterface
80 ( rType
81 , static_cast<io::XInputStream*> ( this )
82 , static_cast<io::XStream*> ( this )
83 , static_cast<lang::XComponent*> ( this )
84 , static_cast<beans::XPropertySet*> ( this )
85 , static_cast<embed::XExtendedStorageStream*> ( this ) );
87 if ( aReturn.hasValue() )
88 return aReturn ;
90 if ( m_nStorageType == embed::StorageFormats::OFOPXML )
92 aReturn = ::cppu::queryInterface
93 ( rType
94 , static_cast<embed::XRelationshipAccess*> ( this ) );
96 if ( aReturn.hasValue() )
97 return aReturn ;
100 return OWeakObject::queryInterface( rType );
103 sal_Int32 SAL_CALL OInputCompStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
105 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
106 if ( m_bDisposed )
108 SAL_INFO("package.xstor", "Disposed!");
109 throw lang::DisposedException();
112 return m_xStream->readBytes( aData, nBytesToRead );
115 sal_Int32 SAL_CALL OInputCompStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
117 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
118 if ( m_bDisposed )
120 SAL_INFO("package.xstor", "Disposed!");
121 throw lang::DisposedException();
124 return m_xStream->readSomeBytes( aData, nMaxBytesToRead );
128 void SAL_CALL OInputCompStream::skipBytes( sal_Int32 nBytesToSkip )
130 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
131 if ( m_bDisposed )
133 SAL_INFO("package.xstor", "Disposed!");
134 throw lang::DisposedException();
137 m_xStream->skipBytes( nBytesToSkip );
141 sal_Int32 SAL_CALL OInputCompStream::available( )
143 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
144 if ( m_bDisposed )
146 SAL_INFO("package.xstor", "Disposed!");
147 throw lang::DisposedException();
150 return m_xStream->available();
154 void SAL_CALL OInputCompStream::closeInput( )
156 dispose();
159 uno::Reference< io::XInputStream > SAL_CALL OInputCompStream::getInputStream()
161 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
162 if ( m_bDisposed )
164 SAL_INFO("package.xstor", "Disposed!");
165 throw lang::DisposedException();
168 return this;
171 uno::Reference< io::XOutputStream > SAL_CALL OInputCompStream::getOutputStream()
173 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
174 if ( m_bDisposed )
176 SAL_INFO("package.xstor", "Disposed!");
177 throw lang::DisposedException();
180 return uno::Reference< io::XOutputStream >();
183 void OInputCompStream::InternalDispose()
185 // can be called only by OWriteStream_Impl
186 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
187 if ( m_bDisposed )
188 return;
190 // the source object is also a kind of locker for the current object
191 // since the listeners could dispose the object while being notified
192 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
194 if ( m_pInterfaceContainer )
195 m_pInterfaceContainer->disposeAndClear( aSource );
199 m_xStream->closeInput();
201 catch( uno::Exception& )
204 m_pImpl = nullptr;
205 m_bDisposed = true;
208 void SAL_CALL OInputCompStream::dispose( )
210 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
211 if ( m_bDisposed )
212 return;
214 if ( m_pInterfaceContainer )
216 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
217 m_pInterfaceContainer->disposeAndClear( aSource );
220 m_xStream->closeInput();
222 if ( m_pImpl )
224 m_pImpl->InputStreamDisposed( this );
225 m_pImpl = nullptr;
228 m_bDisposed = true;
231 void SAL_CALL OInputCompStream::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
233 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
234 if ( m_bDisposed )
236 SAL_INFO("package.xstor", "Disposed!");
237 throw lang::DisposedException();
240 if ( !m_pInterfaceContainer )
241 m_pInterfaceContainer.reset( new ::comphelper::OInterfaceContainerHelper3<css::lang::XEventListener>( m_xMutex->GetMutex() ) );
243 m_pInterfaceContainer->addInterface( xListener );
246 void SAL_CALL OInputCompStream::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
248 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
249 if ( m_bDisposed )
251 SAL_INFO("package.xstor", "Disposed!");
252 throw lang::DisposedException();
255 if ( m_pInterfaceContainer )
256 m_pInterfaceContainer->removeInterface( xListener );
259 sal_Bool SAL_CALL OInputCompStream::hasByID( const OUString& sID )
261 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
263 if ( m_bDisposed )
265 SAL_INFO("package.xstor", "Disposed!");
266 throw lang::DisposedException();
269 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
270 throw uno::RuntimeException();
274 getRelationshipByID( sID );
275 return true;
277 catch( container::NoSuchElementException& )
280 return false;
283 namespace
286 const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName)
288 return std::find_if(rSeq.begin(), rSeq.end(),
289 [&rName](const beans::StringPair& rPair) { return rPair.First == rName; });
294 OUString SAL_CALL OInputCompStream::getTargetByID( const OUString& sID )
296 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
298 if ( m_bDisposed )
300 SAL_INFO("package.xstor", "Disposed!");
301 throw lang::DisposedException();
304 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
305 throw uno::RuntimeException();
307 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
308 auto pRel = lcl_findPairByName(aSeq, "Target");
309 if (pRel != aSeq.end())
310 return pRel->Second;
312 return OUString();
315 OUString SAL_CALL OInputCompStream::getTypeByID( const OUString& sID )
317 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
319 if ( m_bDisposed )
321 SAL_INFO("package.xstor", "Disposed!");
322 throw lang::DisposedException();
325 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
326 throw uno::RuntimeException();
328 const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
329 auto pRel = lcl_findPairByName(aSeq, "Type");
330 if (pRel != aSeq.end())
331 return pRel->Second;
333 return OUString();
336 uno::Sequence< beans::StringPair > SAL_CALL OInputCompStream::getRelationshipByID( const OUString& sID )
338 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
340 if ( m_bDisposed )
342 SAL_INFO("package.xstor", "Disposed!");
343 throw lang::DisposedException();
346 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
347 throw uno::RuntimeException();
349 // TODO/LATER: in future the unification of the ID could be checked
350 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
351 const beans::StringPair aIDRel("Id", sID);
352 auto pRel = std::find_if(aSeq.begin(), aSeq.end(),
353 [&aIDRel](const uno::Sequence<beans::StringPair>& rRel){
354 return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); });
355 if (pRel != aSeq.end())
356 return *pRel;
358 throw container::NoSuchElementException();
361 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getRelationshipsByType( const OUString& sType )
363 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
365 if ( m_bDisposed )
367 SAL_INFO("package.xstor", "Disposed!");
368 throw lang::DisposedException();
371 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
372 throw uno::RuntimeException();
374 // TODO/LATER: in future the unification of the ID could be checked
375 const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
376 const beans::StringPair aTypeRel("Type", sType);
377 std::vector< uno::Sequence<beans::StringPair> > aResult;
378 aResult.reserve(aSeq.getLength());
380 std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult),
381 [&aTypeRel](const uno::Sequence<beans::StringPair>& rRel) {
382 return std::find(rRel.begin(), rRel.end(), aTypeRel) != rRel.end(); });
384 return comphelper::containerToSequence(aResult);
387 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getAllRelationships()
389 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
391 if ( m_bDisposed )
393 SAL_INFO("package.xstor", "Disposed!");
394 throw lang::DisposedException();
397 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
398 throw uno::RuntimeException();
400 // TODO/LATER: in future the information could be taken directly from m_pImpl when possible
401 auto pProp = std::find_if(std::cbegin(m_aProperties), std::cend(m_aProperties),
402 [](const beans::PropertyValue& rProp) { return rProp.Name == "RelationsInfo"; });
403 if (pProp != std::cend(m_aProperties))
405 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
406 if (pProp->Value >>= aResult)
407 return aResult;
410 throw io::IOException("relations info could not be read"); // the relations info could not be read
413 void SAL_CALL OInputCompStream::insertRelationshipByID( const OUString& /*sID*/, const uno::Sequence< beans::StringPair >& /*aEntry*/, sal_Bool /*bReplace*/ )
415 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
417 if ( m_bDisposed )
419 SAL_INFO("package.xstor", "Disposed!");
420 throw lang::DisposedException();
423 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
424 throw uno::RuntimeException();
426 throw io::IOException(); // TODO: Access denied
429 void SAL_CALL OInputCompStream::removeRelationshipByID( const OUString& /*sID*/ )
431 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
433 if ( m_bDisposed )
435 SAL_INFO("package.xstor", "Disposed!");
436 throw lang::DisposedException();
439 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
440 throw uno::RuntimeException();
442 throw io::IOException(); // TODO: Access denied
445 void SAL_CALL OInputCompStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& /*aEntries*/, sal_Bool /*bReplace*/ )
447 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
449 if ( m_bDisposed )
451 SAL_INFO("package.xstor", "Disposed!");
452 throw lang::DisposedException();
455 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
456 throw uno::RuntimeException();
458 throw io::IOException(); // TODO: Access denied
461 void SAL_CALL OInputCompStream::clearRelationships()
463 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
465 if ( m_bDisposed )
467 SAL_INFO("package.xstor", "Disposed!");
468 throw lang::DisposedException();
471 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
472 throw uno::RuntimeException();
474 throw io::IOException(); // TODO: Access denied
477 uno::Reference< beans::XPropertySetInfo > SAL_CALL OInputCompStream::getPropertySetInfo()
479 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
481 if ( m_bDisposed )
483 SAL_INFO("package.xstor", "Disposed!");
484 throw lang::DisposedException();
487 //TODO:
488 return uno::Reference< beans::XPropertySetInfo >();
491 void SAL_CALL OInputCompStream::setPropertyValue( const OUString& aPropertyName, const uno::Any& /*aValue*/ )
493 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
495 if ( m_bDisposed )
497 SAL_INFO("package.xstor", "Disposed!");
498 throw lang::DisposedException();
501 // all the provided properties are accessible
502 if (std::any_of(std::cbegin(m_aProperties), std::cend(m_aProperties),
503 [&aPropertyName](const beans::PropertyValue& rProp) { return rProp.Name == aPropertyName; }))
504 throw beans::PropertyVetoException(); // TODO
506 throw beans::UnknownPropertyException(aPropertyName); // TODO
509 uno::Any SAL_CALL OInputCompStream::getPropertyValue( const OUString& aProp )
511 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
513 if ( m_bDisposed )
515 SAL_INFO("package.xstor", "Disposed!");
516 throw lang::DisposedException();
519 OUString aPropertyName;
520 if ( aProp == "IsEncrypted" )
521 aPropertyName = "Encrypted";
522 else
523 aPropertyName = aProp;
525 if ( aPropertyName == "RelationsInfo" )
526 throw beans::UnknownPropertyException(aPropertyName); // TODO
528 // all the provided properties are accessible
529 auto pProp = std::find_if(std::cbegin(m_aProperties), std::cend(m_aProperties),
530 [&aPropertyName](const beans::PropertyValue& rProp) { return rProp.Name == aPropertyName; });
531 if (pProp != std::cend(m_aProperties))
532 return pProp->Value;
534 throw beans::UnknownPropertyException(aPropertyName); // TODO
537 void SAL_CALL OInputCompStream::addPropertyChangeListener(
538 const OUString& /*aPropertyName*/,
539 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
541 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
543 if ( m_bDisposed )
545 SAL_INFO("package.xstor", "Disposed!");
546 throw lang::DisposedException();
549 //TODO:
552 void SAL_CALL OInputCompStream::removePropertyChangeListener(
553 const OUString& /*aPropertyName*/,
554 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
556 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
558 if ( m_bDisposed )
560 SAL_INFO("package.xstor", "Disposed!");
561 throw lang::DisposedException();
564 //TODO:
567 void SAL_CALL OInputCompStream::addVetoableChangeListener(
568 const OUString& /*PropertyName*/,
569 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
571 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
573 if ( m_bDisposed )
575 SAL_INFO("package.xstor", "Disposed!");
576 throw lang::DisposedException();
579 //TODO:
582 void SAL_CALL OInputCompStream::removeVetoableChangeListener(
583 const OUString& /*PropertyName*/,
584 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
586 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
588 if ( m_bDisposed )
590 SAL_INFO("package.xstor", "Disposed!");
591 throw lang::DisposedException();
594 //TODO:
597 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */