1dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5dde7d3faSAndrew Rist * distributed with this work for additional information 6dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10dde7d3faSAndrew Rist * 11dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12dde7d3faSAndrew Rist * 13dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17dde7d3faSAndrew Rist * specific language governing permissions and limitations 18dde7d3faSAndrew Rist * under the License. 19dde7d3faSAndrew Rist * 20dde7d3faSAndrew Rist *************************************************************/ 21dde7d3faSAndrew Rist 22dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp> 27cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp> 28cdf0e10cSrcweir #include <com/sun/star/embed/XEmbedObjectCreator.hpp> 29cdf0e10cSrcweir #include <com/sun/star/embed/XLinkCreator.hpp> 30cdf0e10cSrcweir #include <com/sun/star/embed/XEmbedPersist.hpp> 31cdf0e10cSrcweir #include <com/sun/star/embed/XLinkageSupport.hpp> 32cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp> 33cdf0e10cSrcweir #include <com/sun/star/embed/XOptimizedStorage.hpp> 34cdf0e10cSrcweir #include <com/sun/star/embed/EntryInitModes.hpp> 35cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp> 36cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp> 37cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp> 38cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp> 39cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp> 40cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 41cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp> 42cdf0e10cSrcweir #include <com/sun/star/embed/EmbedMisc.hpp> 43cdf0e10cSrcweir 44cdf0e10cSrcweir #include <comphelper/seqstream.hxx> 45cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 46cdf0e10cSrcweir #include <comphelper/storagehelper.hxx> 47cdf0e10cSrcweir #include <comphelper/embeddedobjectcontainer.hxx> 48cdf0e10cSrcweir #include <comphelper/sequence.hxx> 49cdf0e10cSrcweir #include <cppuhelper/weakref.hxx> 50cdf0e10cSrcweir #include <hash_map> 51cdf0e10cSrcweir #include <algorithm> 52cdf0e10cSrcweir 53cdf0e10cSrcweir #include <rtl/logfile.hxx> 54cdf0e10cSrcweir 55cdf0e10cSrcweir using namespace ::com::sun::star; 56cdf0e10cSrcweir 57cdf0e10cSrcweir namespace comphelper 58cdf0e10cSrcweir { 59cdf0e10cSrcweir 60cdf0e10cSrcweir struct hashObjectName_Impl 61cdf0e10cSrcweir { 62cdf0e10cSrcweir size_t operator()(const ::rtl::OUString Str) const 63cdf0e10cSrcweir { 64cdf0e10cSrcweir return (size_t)Str.hashCode(); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir }; 67cdf0e10cSrcweir 68cdf0e10cSrcweir struct eqObjectName_Impl 69cdf0e10cSrcweir { 70cdf0e10cSrcweir sal_Bool operator()(const ::rtl::OUString Str1, const ::rtl::OUString Str2) const 71cdf0e10cSrcweir { 72cdf0e10cSrcweir return ( Str1 == Str2 ); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir }; 75cdf0e10cSrcweir 76cdf0e10cSrcweir typedef std::hash_map 77cdf0e10cSrcweir < 78cdf0e10cSrcweir ::rtl::OUString, 79cdf0e10cSrcweir ::com::sun::star::uno::Reference < com::sun::star::embed::XEmbeddedObject >, 80cdf0e10cSrcweir hashObjectName_Impl, 81cdf0e10cSrcweir eqObjectName_Impl 82cdf0e10cSrcweir > 83cdf0e10cSrcweir EmbeddedObjectContainerNameMap; 84cdf0e10cSrcweir 85cdf0e10cSrcweir struct EmbedImpl 86cdf0e10cSrcweir { 87cdf0e10cSrcweir // TODO/LATER: remove objects from temp. Container storage when object is disposed 88cdf0e10cSrcweir EmbeddedObjectContainerNameMap maObjectContainer; 89cdf0e10cSrcweir uno::Reference < embed::XStorage > mxStorage; 90cdf0e10cSrcweir EmbeddedObjectContainer* mpTempObjectContainer; 91cdf0e10cSrcweir uno::Reference < embed::XStorage > mxImageStorage; 92cdf0e10cSrcweir uno::WeakReference < uno::XInterface > m_xModel; 93cdf0e10cSrcweir //EmbeddedObjectContainerNameMap maTempObjectContainer; 94cdf0e10cSrcweir //uno::Reference < embed::XStorage > mxTempStorage; 95cdf0e10cSrcweir sal_Bool bOwnsStorage; 96cdf0e10cSrcweir 97cdf0e10cSrcweir const uno::Reference < embed::XStorage >& GetReplacements(); 98cdf0e10cSrcweir }; 99cdf0e10cSrcweir 100cdf0e10cSrcweir const uno::Reference < embed::XStorage >& EmbedImpl::GetReplacements() 101cdf0e10cSrcweir { 102cdf0e10cSrcweir if ( !mxImageStorage.is() ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir try 105cdf0e10cSrcweir { 106cdf0e10cSrcweir mxImageStorage = mxStorage->openStorageElement( 107cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "ObjectReplacements" ), embed::ElementModes::READWRITE ); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir catch ( uno::Exception& ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir mxImageStorage = mxStorage->openStorageElement( 112cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "ObjectReplacements" ), embed::ElementModes::READ ); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir if ( !mxImageStorage.is() ) 117cdf0e10cSrcweir throw io::IOException(); 118cdf0e10cSrcweir 119cdf0e10cSrcweir return mxImageStorage; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir EmbeddedObjectContainer::EmbeddedObjectContainer() 123cdf0e10cSrcweir { 124cdf0e10cSrcweir pImpl = new EmbedImpl; 125cdf0e10cSrcweir pImpl->mxStorage = ::comphelper::OStorageHelper::GetTemporaryStorage(); 126cdf0e10cSrcweir pImpl->bOwnsStorage = sal_True; 127cdf0e10cSrcweir pImpl->mpTempObjectContainer = 0; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor ) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir pImpl = new EmbedImpl; 133cdf0e10cSrcweir pImpl->mxStorage = rStor; 134cdf0e10cSrcweir pImpl->bOwnsStorage = sal_False; 135cdf0e10cSrcweir pImpl->mpTempObjectContainer = 0; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor, const uno::Reference < uno::XInterface >& xModel ) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir pImpl = new EmbedImpl; 141cdf0e10cSrcweir pImpl->mxStorage = rStor; 142cdf0e10cSrcweir pImpl->bOwnsStorage = sal_False; 143cdf0e10cSrcweir pImpl->mpTempObjectContainer = 0; 144cdf0e10cSrcweir pImpl->m_xModel = xModel; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir void EmbeddedObjectContainer::SwitchPersistence( const uno::Reference < embed::XStorage >& rStor ) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir ReleaseImageSubStorage(); 150cdf0e10cSrcweir 151cdf0e10cSrcweir if ( pImpl->bOwnsStorage ) 152cdf0e10cSrcweir pImpl->mxStorage->dispose(); 153cdf0e10cSrcweir 154cdf0e10cSrcweir pImpl->mxStorage = rStor; 155cdf0e10cSrcweir pImpl->bOwnsStorage = sal_False; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::CommitImageSubStorage() 159cdf0e10cSrcweir { 160cdf0e10cSrcweir if ( pImpl->mxImageStorage.is() ) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir try 163cdf0e10cSrcweir { 164cdf0e10cSrcweir sal_Bool bReadOnlyMode = sal_True; 165cdf0e10cSrcweir uno::Reference < beans::XPropertySet > xSet(pImpl->mxImageStorage,uno::UNO_QUERY); 166cdf0e10cSrcweir if ( xSet.is() ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir // get the open mode from the parent storage 169cdf0e10cSrcweir sal_Int32 nMode = 0; 170cdf0e10cSrcweir uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString::createFromAscii("OpenMode") ); 171cdf0e10cSrcweir if ( aAny >>= nMode ) 172cdf0e10cSrcweir bReadOnlyMode = !(nMode & embed::ElementModes::WRITE ); 173cdf0e10cSrcweir } // if ( xSet.is() ) 174cdf0e10cSrcweir if ( !bReadOnlyMode ) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir uno::Reference< embed::XTransactedObject > xTransact( pImpl->mxImageStorage, uno::UNO_QUERY_THROW ); 177cdf0e10cSrcweir xTransact->commit(); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir } 180cdf0e10cSrcweir catch( uno::Exception& ) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir return sal_False; 183cdf0e10cSrcweir } 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir return sal_True; 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir void EmbeddedObjectContainer::ReleaseImageSubStorage() 190cdf0e10cSrcweir { 191cdf0e10cSrcweir CommitImageSubStorage(); 192cdf0e10cSrcweir 193cdf0e10cSrcweir if ( pImpl->mxImageStorage.is() ) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir try 196cdf0e10cSrcweir { 197cdf0e10cSrcweir pImpl->mxImageStorage->dispose(); 198cdf0e10cSrcweir pImpl->mxImageStorage = uno::Reference< embed::XStorage >(); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir catch( uno::Exception& ) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir OSL_ASSERT( "Problems releasing image substorage!\n" ); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir EmbeddedObjectContainer::~EmbeddedObjectContainer() 208cdf0e10cSrcweir { 209cdf0e10cSrcweir ReleaseImageSubStorage(); 210cdf0e10cSrcweir 211cdf0e10cSrcweir if ( pImpl->bOwnsStorage ) 212cdf0e10cSrcweir pImpl->mxStorage->dispose(); 213cdf0e10cSrcweir 214cdf0e10cSrcweir delete pImpl->mpTempObjectContainer; 215cdf0e10cSrcweir delete pImpl; 216cdf0e10cSrcweir } 217cdf0e10cSrcweir 218cdf0e10cSrcweir void EmbeddedObjectContainer::CloseEmbeddedObjects() 219cdf0e10cSrcweir { 220cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 221cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 222cdf0e10cSrcweir { 223cdf0e10cSrcweir uno::Reference < util::XCloseable > xClose( (*aIt).second, uno::UNO_QUERY ); 224cdf0e10cSrcweir if ( xClose.is() ) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir try 227cdf0e10cSrcweir { 228cdf0e10cSrcweir xClose->close( sal_True ); 229cdf0e10cSrcweir } 230cdf0e10cSrcweir catch ( uno::Exception& ) 231cdf0e10cSrcweir { 232cdf0e10cSrcweir } 233cdf0e10cSrcweir } 234cdf0e10cSrcweir 235cdf0e10cSrcweir aIt++; 236cdf0e10cSrcweir } 237cdf0e10cSrcweir } 238cdf0e10cSrcweir 239cdf0e10cSrcweir ::rtl::OUString EmbeddedObjectContainer::CreateUniqueObjectName() 240cdf0e10cSrcweir { 241cdf0e10cSrcweir ::rtl::OUString aPersistName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Object ") ); 242cdf0e10cSrcweir ::rtl::OUString aStr; 243cdf0e10cSrcweir sal_Int32 i=1; 244cdf0e10cSrcweir do 245cdf0e10cSrcweir { 246cdf0e10cSrcweir aStr = aPersistName; 247cdf0e10cSrcweir aStr += ::rtl::OUString::valueOf( i++ ); 248cdf0e10cSrcweir } 249cdf0e10cSrcweir while( HasEmbeddedObject( aStr ) ); 250cdf0e10cSrcweir // TODO/LATER: should we consider deleted objects? 251cdf0e10cSrcweir 252cdf0e10cSrcweir return aStr; 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir uno::Sequence < ::rtl::OUString > EmbeddedObjectContainer::GetObjectNames() 256cdf0e10cSrcweir { 257cdf0e10cSrcweir uno::Sequence < ::rtl::OUString > aSeq( pImpl->maObjectContainer.size() ); 258cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 259cdf0e10cSrcweir sal_Int32 nIdx=0; 260cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 261cdf0e10cSrcweir aSeq[nIdx++] = (*aIt++).first; 262cdf0e10cSrcweir return aSeq; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 265cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::HasEmbeddedObjects() 266cdf0e10cSrcweir { 267cdf0e10cSrcweir return pImpl->maObjectContainer.size() != 0; 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const ::rtl::OUString& rName ) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName ); 273cdf0e10cSrcweir if ( aIt == pImpl->maObjectContainer.end() ) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY ); 276cdf0e10cSrcweir return xAccess->hasByName(rName); 277cdf0e10cSrcweir } 278cdf0e10cSrcweir else 279cdf0e10cSrcweir return sal_True; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj ) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 285cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 286cdf0e10cSrcweir { 287cdf0e10cSrcweir if ( (*aIt).second == xObj ) 288cdf0e10cSrcweir return sal_True; 289cdf0e10cSrcweir else 290cdf0e10cSrcweir aIt++; 291cdf0e10cSrcweir } 292cdf0e10cSrcweir 293cdf0e10cSrcweir return sal_False; 294cdf0e10cSrcweir } 295cdf0e10cSrcweir 296cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::HasInstantiatedEmbeddedObject( const ::rtl::OUString& rName ) 297cdf0e10cSrcweir { 298cdf0e10cSrcweir // allows to detect whether the object was already instantiated 299cdf0e10cSrcweir // currently the filter instantiate it on loading, so this method allows 300cdf0e10cSrcweir // to avoid objects pointing to the same persistence 301cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName ); 302cdf0e10cSrcweir return ( aIt != pImpl->maObjectContainer.end() ); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir ::rtl::OUString EmbeddedObjectContainer::GetEmbeddedObjectName( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj ) 306cdf0e10cSrcweir { 307cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 308cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 309cdf0e10cSrcweir { 310cdf0e10cSrcweir if ( (*aIt).second == xObj ) 311cdf0e10cSrcweir return (*aIt).first; 312cdf0e10cSrcweir else 313cdf0e10cSrcweir aIt++; 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir OSL_ENSURE( 0, "Unknown object!" ); 317cdf0e10cSrcweir return ::rtl::OUString(); 318cdf0e10cSrcweir } 319cdf0e10cSrcweir 320cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::GetEmbeddedObject( const ::rtl::OUString& rName ) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetEmbeddedObject" ); 323cdf0e10cSrcweir 324*49b34792SHerbert Dürr OSL_ENSURE( !rName.isEmpty(), "Empty object name!"); 325cdf0e10cSrcweir 326cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 327cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName ); 328cdf0e10cSrcweir 329cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 330cdf0e10cSrcweir uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY ); 331cdf0e10cSrcweir uno::Sequence< ::rtl::OUString> aSeq = xAccess->getElementNames(); 332cdf0e10cSrcweir const ::rtl::OUString* pIter = aSeq.getConstArray(); 333cdf0e10cSrcweir const ::rtl::OUString* pEnd = pIter + aSeq.getLength(); 334cdf0e10cSrcweir for(;pIter != pEnd;++pIter) 335cdf0e10cSrcweir { 336cdf0e10cSrcweir (void)*pIter; 337cdf0e10cSrcweir } 338cdf0e10cSrcweir OSL_ENSURE( aIt != pImpl->maObjectContainer.end() || xAccess->hasByName(rName), "Could not return object!" ); 339cdf0e10cSrcweir #endif 340cdf0e10cSrcweir 341cdf0e10cSrcweir // check if object was already created 342cdf0e10cSrcweir if ( aIt != pImpl->maObjectContainer.end() ) 343cdf0e10cSrcweir xObj = (*aIt).second; 344cdf0e10cSrcweir else 345cdf0e10cSrcweir xObj = Get_Impl( rName, uno::Reference < embed::XEmbeddedObject >() ); 346cdf0e10cSrcweir 347cdf0e10cSrcweir return xObj; 348cdf0e10cSrcweir } 349cdf0e10cSrcweir 350cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::Get_Impl( const ::rtl::OUString& rName, const uno::Reference < embed::XEmbeddedObject >& xCopy ) 351cdf0e10cSrcweir { 352cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 353cdf0e10cSrcweir try 354cdf0e10cSrcweir { 355cdf0e10cSrcweir // create the object from the storage 356cdf0e10cSrcweir uno::Reference < beans::XPropertySet > xSet( pImpl->mxStorage, uno::UNO_QUERY ); 357cdf0e10cSrcweir sal_Bool bReadOnlyMode = sal_True; 358cdf0e10cSrcweir if ( xSet.is() ) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir // get the open mode from the parent storage 361cdf0e10cSrcweir sal_Int32 nMode = 0; 362cdf0e10cSrcweir uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString::createFromAscii("OpenMode") ); 363cdf0e10cSrcweir if ( aAny >>= nMode ) 364cdf0e10cSrcweir bReadOnlyMode = !(nMode & embed::ElementModes::WRITE ); 365cdf0e10cSrcweir } 366cdf0e10cSrcweir 367cdf0e10cSrcweir // object was not added until now - should happen only by calling this method from "inside" 368cdf0e10cSrcweir //TODO/LATER: it would be good to detect an error when an object should be created already, but isn't (not an "inside" call) 369cdf0e10cSrcweir uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance( 370cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY ); 371cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( xCopy.is() ? 2 : 1 ); 372cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 373cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 374cdf0e10cSrcweir if ( xCopy.is() ) 375cdf0e10cSrcweir { 376cdf0e10cSrcweir aObjDescr[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CloneFrom" ) ); 377cdf0e10cSrcweir aObjDescr[1].Value <<= xCopy; 378cdf0e10cSrcweir } 379cdf0e10cSrcweir 380cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aMediaDescr( 1 ); 381cdf0e10cSrcweir aMediaDescr[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")); 382cdf0e10cSrcweir aMediaDescr[0].Value <<= bReadOnlyMode; 383cdf0e10cSrcweir xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromEntry( 384cdf0e10cSrcweir pImpl->mxStorage, rName, 385cdf0e10cSrcweir aMediaDescr, aObjDescr ), uno::UNO_QUERY ); 386cdf0e10cSrcweir 387cdf0e10cSrcweir // insert object into my list 388cdf0e10cSrcweir AddEmbeddedObject( xObj, rName ); 389cdf0e10cSrcweir } 390cdf0e10cSrcweir catch ( uno::Exception& ) 391cdf0e10cSrcweir { 392cdf0e10cSrcweir } 393cdf0e10cSrcweir 394cdf0e10cSrcweir return xObj; 395cdf0e10cSrcweir } 396cdf0e10cSrcweir 397cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId, 398cdf0e10cSrcweir const uno::Sequence < beans::PropertyValue >& rArgs, ::rtl::OUString& rNewName ) 399cdf0e10cSrcweir { 400cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CreateEmbeddedObject" ); 401cdf0e10cSrcweir 402*49b34792SHerbert Dürr if ( rNewName.isEmpty() ) 403cdf0e10cSrcweir rNewName = CreateUniqueObjectName(); 404cdf0e10cSrcweir 405cdf0e10cSrcweir OSL_ENSURE( !HasEmbeddedObject(rNewName), "Object to create already exists!"); 406cdf0e10cSrcweir 407cdf0e10cSrcweir // create object from classid by inserting it into storage 408cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 409cdf0e10cSrcweir try 410cdf0e10cSrcweir { 411cdf0e10cSrcweir uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance( 412cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY ); 413cdf0e10cSrcweir 414cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( rArgs.getLength() + 1 ); 415cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 416cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 417cdf0e10cSrcweir ::std::copy( rArgs.getConstArray(), rArgs.getConstArray() + rArgs.getLength(), aObjDescr.getArray() + 1 ); 418cdf0e10cSrcweir xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew( 419cdf0e10cSrcweir rClassId, ::rtl::OUString(), pImpl->mxStorage, rNewName, 420cdf0e10cSrcweir aObjDescr ), uno::UNO_QUERY ); 421cdf0e10cSrcweir 422cdf0e10cSrcweir AddEmbeddedObject( xObj, rNewName ); 423cdf0e10cSrcweir 424cdf0e10cSrcweir OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED, 425cdf0e10cSrcweir "A freshly create object should be running always!\n" ); 426cdf0e10cSrcweir } 427cdf0e10cSrcweir catch ( uno::Exception& ) 428cdf0e10cSrcweir { 429cdf0e10cSrcweir } 430cdf0e10cSrcweir 431cdf0e10cSrcweir return xObj; 432cdf0e10cSrcweir } 433cdf0e10cSrcweir 434cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId, ::rtl::OUString& rNewName ) 435cdf0e10cSrcweir { 436cdf0e10cSrcweir return CreateEmbeddedObject( rClassId, uno::Sequence < beans::PropertyValue >(), rNewName ); 437cdf0e10cSrcweir } 438cdf0e10cSrcweir 439cdf0e10cSrcweir void EmbeddedObjectContainer::AddEmbeddedObject( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, const ::rtl::OUString& rName ) 440cdf0e10cSrcweir { 441cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::AddEmbeddedObject" ); 442cdf0e10cSrcweir 443cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 444*49b34792SHerbert Dürr OSL_ENSURE( !rName.isEmpty(), "Added object doesn't have a name!"); 445cdf0e10cSrcweir uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY ); 446cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xEmb( xObj, uno::UNO_QUERY ); 447cdf0e10cSrcweir uno::Reference < embed::XLinkageSupport > xLink( xEmb, uno::UNO_QUERY ); 448cdf0e10cSrcweir // if the object has a persistance and the object is not a link than it must have persistence entry in the storage 449cdf0e10cSrcweir OSL_ENSURE( !( xEmb.is() && ( !xLink.is() || !xLink->isLink() ) ) || xAccess->hasByName(rName), 450cdf0e10cSrcweir "Added element not in storage!" ); 451cdf0e10cSrcweir #endif 452cdf0e10cSrcweir 453cdf0e10cSrcweir // remember object - it needs to be in storage already 454cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName ); 455cdf0e10cSrcweir OSL_ENSURE( aIt == pImpl->maObjectContainer.end(), "Element already inserted!" ); 456cdf0e10cSrcweir pImpl->maObjectContainer[ rName ] = xObj; 457cdf0e10cSrcweir uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY ); 458cdf0e10cSrcweir if ( xChild.is() && xChild->getParent() != pImpl->m_xModel.get() ) 459cdf0e10cSrcweir xChild->setParent( pImpl->m_xModel.get() ); 460cdf0e10cSrcweir 461cdf0e10cSrcweir // look for object in temorary container 462cdf0e10cSrcweir if ( pImpl->mpTempObjectContainer ) 463cdf0e10cSrcweir { 464cdf0e10cSrcweir aIt = pImpl->mpTempObjectContainer->pImpl->maObjectContainer.begin(); 465cdf0e10cSrcweir while ( aIt != pImpl->mpTempObjectContainer->pImpl->maObjectContainer.end() ) 466cdf0e10cSrcweir { 467cdf0e10cSrcweir if ( (*aIt).second == xObj ) 468cdf0e10cSrcweir { 469cdf0e10cSrcweir // copy replacement image from temporary container (if there is any) 470cdf0e10cSrcweir ::rtl::OUString aTempName = (*aIt).first; 471cdf0e10cSrcweir ::rtl::OUString aMediaType; 472cdf0e10cSrcweir uno::Reference < io::XInputStream > xStream = pImpl->mpTempObjectContainer->GetGraphicStream( xObj, &aMediaType ); 473cdf0e10cSrcweir if ( xStream.is() ) 474cdf0e10cSrcweir { 475cdf0e10cSrcweir InsertGraphicStream( xStream, rName, aMediaType ); 476cdf0e10cSrcweir xStream = 0; 477cdf0e10cSrcweir pImpl->mpTempObjectContainer->RemoveGraphicStream( aTempName ); 478cdf0e10cSrcweir } 479cdf0e10cSrcweir 480cdf0e10cSrcweir // remove object from storage of temporary container 481cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 482cdf0e10cSrcweir if ( xPersist.is() ) 483cdf0e10cSrcweir { 484cdf0e10cSrcweir try 485cdf0e10cSrcweir { 486cdf0e10cSrcweir pImpl->mpTempObjectContainer->pImpl->mxStorage->removeElement( aTempName ); 487cdf0e10cSrcweir } 488cdf0e10cSrcweir catch ( uno::Exception& ) 489cdf0e10cSrcweir { 490cdf0e10cSrcweir } 491cdf0e10cSrcweir } 492cdf0e10cSrcweir 493cdf0e10cSrcweir // temp. container needs to forget the object 494cdf0e10cSrcweir pImpl->mpTempObjectContainer->pImpl->maObjectContainer.erase( aIt ); 495cdf0e10cSrcweir break; 496cdf0e10cSrcweir } 497cdf0e10cSrcweir else 498cdf0e10cSrcweir aIt++; 499cdf0e10cSrcweir } 500cdf0e10cSrcweir } 501cdf0e10cSrcweir } 502cdf0e10cSrcweir 503cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::StoreEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName, sal_Bool bCopy ) 504cdf0e10cSrcweir { 505cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::StoreEmbeddedObject" ); 506cdf0e10cSrcweir 507cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 508*49b34792SHerbert Dürr if ( rName.isEmpty() ) 509cdf0e10cSrcweir rName = CreateUniqueObjectName(); 510cdf0e10cSrcweir 511cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 512cdf0e10cSrcweir uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY ); 513cdf0e10cSrcweir OSL_ENSURE( !xPersist.is() || !xAccess->hasByName(rName), "Inserting element already present in storage!" ); 514cdf0e10cSrcweir OSL_ENSURE( xPersist.is() || xObj->getCurrentState() == embed::EmbedStates::RUNNING, "Non persistent object inserted!"); 515cdf0e10cSrcweir #endif 516cdf0e10cSrcweir 517cdf0e10cSrcweir // insert objects' storage into the container storage (if object has one) 518cdf0e10cSrcweir try 519cdf0e10cSrcweir { 520cdf0e10cSrcweir if ( xPersist.is() ) 521cdf0e10cSrcweir { 522cdf0e10cSrcweir uno::Sequence < beans::PropertyValue > aSeq; 523cdf0e10cSrcweir if ( bCopy ) 524cdf0e10cSrcweir xPersist->storeToEntry( pImpl->mxStorage, rName, aSeq, aSeq ); 525cdf0e10cSrcweir else 526cdf0e10cSrcweir { 527cdf0e10cSrcweir //TODO/LATER: possible optimisation, don't store immediately 528cdf0e10cSrcweir //xPersist->setPersistentEntry( pImpl->mxStorage, rName, embed::EntryInitModes::ENTRY_NO_INIT, aSeq, aSeq ); 529cdf0e10cSrcweir xPersist->storeAsEntry( pImpl->mxStorage, rName, aSeq, aSeq ); 530cdf0e10cSrcweir xPersist->saveCompleted( sal_True ); 531cdf0e10cSrcweir } 532cdf0e10cSrcweir } 533cdf0e10cSrcweir } 534cdf0e10cSrcweir catch ( uno::Exception& ) 535cdf0e10cSrcweir { 536cdf0e10cSrcweir // TODO/LATER: better error recovery should keep storage intact 537cdf0e10cSrcweir return sal_False; 538cdf0e10cSrcweir } 539cdf0e10cSrcweir 540cdf0e10cSrcweir return sal_True; 541cdf0e10cSrcweir } 542cdf0e10cSrcweir 543cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName ) 544cdf0e10cSrcweir { 545cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( Object )" ); 546cdf0e10cSrcweir // store it into the container storage 547cdf0e10cSrcweir if ( StoreEmbeddedObject( xObj, rName, sal_False ) ) 548cdf0e10cSrcweir { 549cdf0e10cSrcweir // remember object 550cdf0e10cSrcweir AddEmbeddedObject( xObj, rName ); 551cdf0e10cSrcweir return sal_True; 552cdf0e10cSrcweir } 553cdf0e10cSrcweir else 554cdf0e10cSrcweir return sal_False; 555cdf0e10cSrcweir } 556cdf0e10cSrcweir 557cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < io::XInputStream >& xStm, ::rtl::OUString& rNewName ) 558cdf0e10cSrcweir { 559cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( InputStream )" ); 560cdf0e10cSrcweir 561*49b34792SHerbert Dürr if ( rNewName.isEmpty() ) 562cdf0e10cSrcweir rNewName = CreateUniqueObjectName(); 563cdf0e10cSrcweir 564cdf0e10cSrcweir // store it into the container storage 565cdf0e10cSrcweir sal_Bool bIsStorage = sal_False; 566cdf0e10cSrcweir try 567cdf0e10cSrcweir { 568cdf0e10cSrcweir // first try storage persistence 569cdf0e10cSrcweir uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromInputStream( xStm ); 570cdf0e10cSrcweir 571cdf0e10cSrcweir // storage was created from stream successfully 572cdf0e10cSrcweir bIsStorage = sal_True; 573cdf0e10cSrcweir 574cdf0e10cSrcweir uno::Reference < embed::XStorage > xNewStore = pImpl->mxStorage->openStorageElement( rNewName, embed::ElementModes::READWRITE ); 575cdf0e10cSrcweir xStore->copyToStorage( xNewStore ); 576cdf0e10cSrcweir } 577cdf0e10cSrcweir catch ( uno::Exception& ) 578cdf0e10cSrcweir { 579cdf0e10cSrcweir if ( bIsStorage ) 580cdf0e10cSrcweir // it is storage persistence, but opening of new substorage or copying to it failed 581cdf0e10cSrcweir return uno::Reference < embed::XEmbeddedObject >(); 582cdf0e10cSrcweir 583cdf0e10cSrcweir // stream didn't contain a storage, now try stream persistence 584cdf0e10cSrcweir try 585cdf0e10cSrcweir { 586cdf0e10cSrcweir uno::Reference < io::XStream > xNewStream = pImpl->mxStorage->openStreamElement( rNewName, embed::ElementModes::READWRITE ); 587cdf0e10cSrcweir ::comphelper::OStorageHelper::CopyInputToOutput( xStm, xNewStream->getOutputStream() ); 588cdf0e10cSrcweir 589cdf0e10cSrcweir // No mediatype is provided so the default for OLE objects value is used 590cdf0e10cSrcweir // it is correct so for now, but what if somebody introduces a new stream based embedded object? 591cdf0e10cSrcweir // Probably introducing of such an object must be restricted ( a storage must be used! ). 592cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xProps( xNewStream, uno::UNO_QUERY_THROW ); 593cdf0e10cSrcweir xProps->setPropertyValue( 594cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ), 595cdf0e10cSrcweir uno::makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.oleobject" ) ) ) ); 596cdf0e10cSrcweir } 597cdf0e10cSrcweir catch ( uno::Exception& ) 598cdf0e10cSrcweir { 599cdf0e10cSrcweir // complete disaster! 600cdf0e10cSrcweir return uno::Reference < embed::XEmbeddedObject >(); 601cdf0e10cSrcweir } 602cdf0e10cSrcweir } 603cdf0e10cSrcweir 604cdf0e10cSrcweir // stream was copied into the container storage in either way, now try to open something form it 605cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xRet = GetEmbeddedObject( rNewName ); 606cdf0e10cSrcweir try 607cdf0e10cSrcweir { 608cdf0e10cSrcweir if ( !xRet.is() ) 609cdf0e10cSrcweir // no object could be created, so withdraw insertion 610cdf0e10cSrcweir pImpl->mxStorage->removeElement( rNewName ); 611cdf0e10cSrcweir } 612cdf0e10cSrcweir catch ( uno::Exception& ) 613cdf0e10cSrcweir { 614cdf0e10cSrcweir } 615cdf0e10cSrcweir 616cdf0e10cSrcweir return xRet; 617cdf0e10cSrcweir } 618cdf0e10cSrcweir 619cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, ::rtl::OUString& rNewName ) 620cdf0e10cSrcweir { 621cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( MediaDescriptor )" ); 622cdf0e10cSrcweir 623*49b34792SHerbert Dürr if ( rNewName.isEmpty() ) 624cdf0e10cSrcweir rNewName = CreateUniqueObjectName(); 625cdf0e10cSrcweir 626cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 627cdf0e10cSrcweir try 628cdf0e10cSrcweir { 629cdf0e10cSrcweir uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance( 630cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY ); 631cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( 1 ); 632cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 633cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 634cdf0e10cSrcweir xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromMediaDescriptor( 635cdf0e10cSrcweir pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY ); 636cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 637cdf0e10cSrcweir 638cdf0e10cSrcweir OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED, 639cdf0e10cSrcweir "A freshly create object should be running always!\n" ); 640cdf0e10cSrcweir 641cdf0e10cSrcweir // possible optimization: store later! 642cdf0e10cSrcweir if ( xPersist.is()) 643cdf0e10cSrcweir xPersist->storeOwn(); 644cdf0e10cSrcweir 645cdf0e10cSrcweir AddEmbeddedObject( xObj, rNewName ); 646cdf0e10cSrcweir } 647cdf0e10cSrcweir catch ( uno::Exception& ) 648cdf0e10cSrcweir { 649cdf0e10cSrcweir } 650cdf0e10cSrcweir 651cdf0e10cSrcweir return xObj; 652cdf0e10cSrcweir } 653cdf0e10cSrcweir 654cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedLink( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, ::rtl::OUString& rNewName ) 655cdf0e10cSrcweir { 656cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedLink" ); 657cdf0e10cSrcweir 658*49b34792SHerbert Dürr if ( rNewName.isEmpty() ) 659cdf0e10cSrcweir rNewName = CreateUniqueObjectName(); 660cdf0e10cSrcweir 661cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 662cdf0e10cSrcweir try 663cdf0e10cSrcweir { 664cdf0e10cSrcweir uno::Reference < embed::XLinkCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance( 665cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY ); 666cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( 1 ); 667cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 668cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 669cdf0e10cSrcweir xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceLink( 670cdf0e10cSrcweir pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY ); 671cdf0e10cSrcweir 672cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 673cdf0e10cSrcweir 674cdf0e10cSrcweir OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED, 675cdf0e10cSrcweir "A freshly create object should be running always!\n" ); 676cdf0e10cSrcweir 677cdf0e10cSrcweir // possible optimization: store later! 678cdf0e10cSrcweir if ( xPersist.is()) 679cdf0e10cSrcweir xPersist->storeOwn(); 680cdf0e10cSrcweir 681cdf0e10cSrcweir AddEmbeddedObject( xObj, rNewName ); 682cdf0e10cSrcweir } 683cdf0e10cSrcweir catch ( uno::Exception& ) 684cdf0e10cSrcweir { 685cdf0e10cSrcweir } 686cdf0e10cSrcweir 687cdf0e10cSrcweir return xObj; 688cdf0e10cSrcweir } 689cdf0e10cSrcweir 690cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::TryToCopyGraphReplacement( EmbeddedObjectContainer& rSrc, 691cdf0e10cSrcweir const ::rtl::OUString& aOrigName, 692cdf0e10cSrcweir const ::rtl::OUString& aTargetName ) 693cdf0e10cSrcweir { 694cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::TryToCopyGraphReplacement" ); 695cdf0e10cSrcweir 696cdf0e10cSrcweir sal_Bool bResult = sal_False; 697cdf0e10cSrcweir 698*49b34792SHerbert Dürr if ( ( &rSrc != this || !aOrigName.equals( aTargetName ) ) && !aOrigName.isEmpty() && !aTargetName.isEmpty() ) 699cdf0e10cSrcweir { 700cdf0e10cSrcweir ::rtl::OUString aMediaType; 701cdf0e10cSrcweir uno::Reference < io::XInputStream > xGrStream = rSrc.GetGraphicStream( aOrigName, &aMediaType ); 702cdf0e10cSrcweir if ( xGrStream.is() ) 703cdf0e10cSrcweir bResult = InsertGraphicStream( xGrStream, aTargetName, aMediaType ); 704cdf0e10cSrcweir } 705cdf0e10cSrcweir 706cdf0e10cSrcweir return bResult; 707cdf0e10cSrcweir } 708cdf0e10cSrcweir 709cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::CopyEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName ) 710cdf0e10cSrcweir { 711cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CopyEmbeddedObject" ); 712cdf0e10cSrcweir 713cdf0e10cSrcweir OSL_ENSURE( sal_False, 714cdf0e10cSrcweir "This method is depricated! Use EmbeddedObjectContainer::CopyAndGetEmbeddedObject() to copy object!\n" ); 715cdf0e10cSrcweir 716cdf0e10cSrcweir // get the object name before(!) it is assigned to a new storage 717cdf0e10cSrcweir ::rtl::OUString aOrigName; 718cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 719cdf0e10cSrcweir if ( xPersist.is() ) 720cdf0e10cSrcweir aOrigName = xPersist->getEntryName(); 721cdf0e10cSrcweir 722*49b34792SHerbert Dürr if ( rName.isEmpty() ) 723cdf0e10cSrcweir rName = CreateUniqueObjectName(); 724cdf0e10cSrcweir 725cdf0e10cSrcweir if ( StoreEmbeddedObject( xObj, rName, sal_True ) ) 726cdf0e10cSrcweir { 727cdf0e10cSrcweir TryToCopyGraphReplacement( rSrc, aOrigName, rName ); 728cdf0e10cSrcweir return sal_True; 729cdf0e10cSrcweir } 730cdf0e10cSrcweir 731cdf0e10cSrcweir return sal_False; 732cdf0e10cSrcweir } 733cdf0e10cSrcweir 734cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CopyAndGetEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName ) 735cdf0e10cSrcweir { 736cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CopyAndGetEmbeddedObject" ); 737cdf0e10cSrcweir 738cdf0e10cSrcweir uno::Reference< embed::XEmbeddedObject > xResult; 739cdf0e10cSrcweir 740cdf0e10cSrcweir // TODO/LATER: For now only objects that implement XEmbedPersist have a replacement image, it might change in future 741cdf0e10cSrcweir // do an incompatible change so that object name is provided in all the move and copy methods 742cdf0e10cSrcweir ::rtl::OUString aOrigName; 743cdf0e10cSrcweir try 744cdf0e10cSrcweir { 745cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY_THROW ); 746cdf0e10cSrcweir aOrigName = xPersist->getEntryName(); 747cdf0e10cSrcweir } 748cdf0e10cSrcweir catch( uno::Exception& ) 749cdf0e10cSrcweir {} 750cdf0e10cSrcweir 751*49b34792SHerbert Dürr if ( rName.isEmpty() ) 752cdf0e10cSrcweir rName = CreateUniqueObjectName(); 753cdf0e10cSrcweir 754cdf0e10cSrcweir // objects without persistance are not really stored by the method 755cdf0e10cSrcweir if ( xObj.is() && StoreEmbeddedObject( xObj, rName, sal_True ) ) 756cdf0e10cSrcweir { 757cdf0e10cSrcweir xResult = Get_Impl( rName, xObj); 758cdf0e10cSrcweir if ( !xResult.is() ) 759cdf0e10cSrcweir { 760cdf0e10cSrcweir // this is a case when object has no real persistence 761cdf0e10cSrcweir // in such cases a new object should be explicitly created and initialized with the data of the old one 762cdf0e10cSrcweir try 763cdf0e10cSrcweir { 764cdf0e10cSrcweir uno::Reference< embed::XLinkageSupport > xOrigLinkage( xObj, uno::UNO_QUERY ); 765cdf0e10cSrcweir if ( xOrigLinkage.is() && xOrigLinkage->isLink() ) 766cdf0e10cSrcweir { 767cdf0e10cSrcweir // this is a OOo link, it has no persistence 768cdf0e10cSrcweir ::rtl::OUString aURL = xOrigLinkage->getLinkURL(); 769*49b34792SHerbert Dürr if ( aURL.isEmpty() ) 770cdf0e10cSrcweir throw uno::RuntimeException(); 771cdf0e10cSrcweir 772cdf0e10cSrcweir // create new linked object from the URL the link is based on 773cdf0e10cSrcweir uno::Reference < embed::XLinkCreator > xCreator( 774cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance( 775cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator") ) ), 776cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 777cdf0e10cSrcweir 778cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aMediaDescr( 1 ); 779cdf0e10cSrcweir aMediaDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ); 780cdf0e10cSrcweir aMediaDescr[0].Value <<= aURL; 781cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( 1 ); 782cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 783cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 784cdf0e10cSrcweir xResult = uno::Reference < embed::XEmbeddedObject >( 785cdf0e10cSrcweir xCreator->createInstanceLink( 786cdf0e10cSrcweir pImpl->mxStorage, 787cdf0e10cSrcweir rName, 788cdf0e10cSrcweir aMediaDescr, 789cdf0e10cSrcweir aObjDescr ), 790cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 791cdf0e10cSrcweir } 792cdf0e10cSrcweir else 793cdf0e10cSrcweir { 794cdf0e10cSrcweir // the component is required for copying of this object 795cdf0e10cSrcweir if ( xObj->getCurrentState() == embed::EmbedStates::LOADED ) 796cdf0e10cSrcweir xObj->changeState( embed::EmbedStates::RUNNING ); 797cdf0e10cSrcweir 798cdf0e10cSrcweir // this must be an object based on properties, otherwise we can not copy it currently 799cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xOrigProps( xObj->getComponent(), uno::UNO_QUERY_THROW ); 800cdf0e10cSrcweir 801cdf0e10cSrcweir // use object class ID to create a new one and tranfer all the properties 802cdf0e10cSrcweir uno::Reference < embed::XEmbedObjectCreator > xCreator( 803cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance( 804cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator") ) ), 805cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 806cdf0e10cSrcweir 807cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aObjDescr( 1 ); 808cdf0e10cSrcweir aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" ) ); 809cdf0e10cSrcweir aObjDescr[0].Value <<= pImpl->m_xModel.get(); 810cdf0e10cSrcweir xResult = uno::Reference < embed::XEmbeddedObject >( 811cdf0e10cSrcweir xCreator->createInstanceInitNew( 812cdf0e10cSrcweir xObj->getClassID(), 813cdf0e10cSrcweir xObj->getClassName(), 814cdf0e10cSrcweir pImpl->mxStorage, 815cdf0e10cSrcweir rName, 816cdf0e10cSrcweir aObjDescr ), 817cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 818cdf0e10cSrcweir 819cdf0e10cSrcweir if ( xResult->getCurrentState() == embed::EmbedStates::LOADED ) 820cdf0e10cSrcweir xResult->changeState( embed::EmbedStates::RUNNING ); 821cdf0e10cSrcweir 822cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xTargetProps( xResult->getComponent(), uno::UNO_QUERY_THROW ); 823cdf0e10cSrcweir 824cdf0e10cSrcweir // copy all the properties from xOrigProps to xTargetProps 825cdf0e10cSrcweir uno::Reference< beans::XPropertySetInfo > xOrigInfo = xOrigProps->getPropertySetInfo(); 826cdf0e10cSrcweir if ( !xOrigInfo.is() ) 827cdf0e10cSrcweir throw uno::RuntimeException(); 828cdf0e10cSrcweir 829cdf0e10cSrcweir uno::Sequence< beans::Property > aPropertiesList = xOrigInfo->getProperties(); 830cdf0e10cSrcweir for ( sal_Int32 nInd = 0; nInd < aPropertiesList.getLength(); nInd++ ) 831cdf0e10cSrcweir { 832cdf0e10cSrcweir try 833cdf0e10cSrcweir { 834cdf0e10cSrcweir xTargetProps->setPropertyValue( 835cdf0e10cSrcweir aPropertiesList[nInd].Name, 836cdf0e10cSrcweir xOrigProps->getPropertyValue( aPropertiesList[nInd].Name ) ); 837cdf0e10cSrcweir } 838cdf0e10cSrcweir catch( beans::PropertyVetoException& ) 839cdf0e10cSrcweir { 840cdf0e10cSrcweir // impossibility to copy readonly property is not treated as an error for now 841cdf0e10cSrcweir // but the assertion is helpful to detect such scenarios and review them 842cdf0e10cSrcweir OSL_ENSURE( sal_False, "Could not copy readonly property!\n" ); 843cdf0e10cSrcweir } 844cdf0e10cSrcweir } 845cdf0e10cSrcweir } 846cdf0e10cSrcweir 847cdf0e10cSrcweir if ( xResult.is() ) 848cdf0e10cSrcweir AddEmbeddedObject( xResult, rName ); 849cdf0e10cSrcweir } 850cdf0e10cSrcweir catch( uno::Exception& ) 851cdf0e10cSrcweir { 852cdf0e10cSrcweir if ( xResult.is() ) 853cdf0e10cSrcweir { 854cdf0e10cSrcweir try 855cdf0e10cSrcweir { 856cdf0e10cSrcweir xResult->close( sal_True ); 857cdf0e10cSrcweir } 858cdf0e10cSrcweir catch( uno::Exception& ) 859cdf0e10cSrcweir {} 860cdf0e10cSrcweir xResult = uno::Reference< embed::XEmbeddedObject >(); 861cdf0e10cSrcweir } 862cdf0e10cSrcweir } 863cdf0e10cSrcweir } 864cdf0e10cSrcweir } 865cdf0e10cSrcweir 866cdf0e10cSrcweir OSL_ENSURE( xResult.is(), "Can not copy embedded object that has no persistance!\n" ); 867cdf0e10cSrcweir 868cdf0e10cSrcweir if ( xResult.is() ) 869cdf0e10cSrcweir { 870cdf0e10cSrcweir // the object is successfully copied, try to copy graphical replacement 871*49b34792SHerbert Dürr if ( !aOrigName.isEmpty() ) 872cdf0e10cSrcweir TryToCopyGraphReplacement( rSrc, aOrigName, rName ); 873cdf0e10cSrcweir 874cdf0e10cSrcweir // the object might need the size to be set 875cdf0e10cSrcweir try 876cdf0e10cSrcweir { 877cdf0e10cSrcweir if ( xResult->getStatus( embed::Aspects::MSOLE_CONTENT ) & embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD ) 878cdf0e10cSrcweir xResult->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, 879cdf0e10cSrcweir xObj->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT ) ); 880cdf0e10cSrcweir } 881cdf0e10cSrcweir catch( uno::Exception& ) 882cdf0e10cSrcweir {} 883cdf0e10cSrcweir } 884cdf0e10cSrcweir 885cdf0e10cSrcweir return xResult; 886cdf0e10cSrcweir } 887cdf0e10cSrcweir 888cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName ) 889cdf0e10cSrcweir { 890cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Object )" ); 891cdf0e10cSrcweir 892cdf0e10cSrcweir // get the object name before(!) it is assigned to a new storage 893cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 894cdf0e10cSrcweir ::rtl::OUString aName; 895cdf0e10cSrcweir if ( xPersist.is() ) 896cdf0e10cSrcweir aName = xPersist->getEntryName(); 897cdf0e10cSrcweir 898cdf0e10cSrcweir // now move the object to the new container; the returned name is the new persist name in this container 899cdf0e10cSrcweir sal_Bool bRet; 900cdf0e10cSrcweir 901cdf0e10cSrcweir try 902cdf0e10cSrcweir { 903cdf0e10cSrcweir bRet = InsertEmbeddedObject( xObj, rName ); 904cdf0e10cSrcweir if ( bRet ) 905cdf0e10cSrcweir TryToCopyGraphReplacement( rSrc, aName, rName ); 906cdf0e10cSrcweir } 907cdf0e10cSrcweir catch ( uno::Exception& e ) 908cdf0e10cSrcweir { 909cdf0e10cSrcweir (void)e; 910cdf0e10cSrcweir OSL_ENSURE( sal_False, "Failed to insert embedded object into storage!" ); 911cdf0e10cSrcweir bRet = sal_False; 912cdf0e10cSrcweir } 913cdf0e10cSrcweir 914cdf0e10cSrcweir if ( bRet ) 915cdf0e10cSrcweir { 916cdf0e10cSrcweir // now remove the object from the former container 917cdf0e10cSrcweir bRet = sal_False; 918cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = rSrc.pImpl->maObjectContainer.begin(); 919cdf0e10cSrcweir while ( aIt != rSrc.pImpl->maObjectContainer.end() ) 920cdf0e10cSrcweir { 921cdf0e10cSrcweir if ( (*aIt).second == xObj ) 922cdf0e10cSrcweir { 923cdf0e10cSrcweir rSrc.pImpl->maObjectContainer.erase( aIt ); 924cdf0e10cSrcweir bRet = sal_True; 925cdf0e10cSrcweir break; 926cdf0e10cSrcweir } 927cdf0e10cSrcweir 928cdf0e10cSrcweir aIt++; 929cdf0e10cSrcweir } 930cdf0e10cSrcweir 931cdf0e10cSrcweir OSL_ENSURE( bRet, "Object not found for removal!" ); 932cdf0e10cSrcweir if ( xPersist.is() ) 933cdf0e10cSrcweir { 934cdf0e10cSrcweir // now it's time to remove the storage from the container storage 935cdf0e10cSrcweir try 936cdf0e10cSrcweir { 937cdf0e10cSrcweir if ( xPersist.is() ) 938cdf0e10cSrcweir rSrc.pImpl->mxStorage->removeElement( aName ); 939cdf0e10cSrcweir } 940cdf0e10cSrcweir catch ( uno::Exception& ) 941cdf0e10cSrcweir { 942cdf0e10cSrcweir OSL_ENSURE( sal_False, "Failed to remove object from storage!" ); 943cdf0e10cSrcweir bRet = sal_False; 944cdf0e10cSrcweir } 945cdf0e10cSrcweir } 946cdf0e10cSrcweir 947cdf0e10cSrcweir // rSrc.RemoveGraphicStream( aName ); 948cdf0e10cSrcweir } 949cdf0e10cSrcweir 950cdf0e10cSrcweir return bRet; 951cdf0e10cSrcweir } 952cdf0e10cSrcweir 9536170fa3cSArmin Le Grand //sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const ::rtl::OUString& rName, sal_Bool bClose ) 9546170fa3cSArmin Le Grand // #i119941, bKeepToTempStorage: use to specify whether store the removed object to temporary storage+ 9556170fa3cSArmin Le Grand sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const ::rtl::OUString& rName, sal_Bool bClose, sal_Bool bKeepToTempStorage ) 956cdf0e10cSrcweir { 957cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Name )" ); 958cdf0e10cSrcweir 959cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( rName ); 960cdf0e10cSrcweir if ( xObj.is() ) 9616170fa3cSArmin Le Grand //return RemoveEmbeddedObject( xObj, bClose ); 9626170fa3cSArmin Le Grand return RemoveEmbeddedObject( xObj, bClose, bKeepToTempStorage ); 963cdf0e10cSrcweir else 964cdf0e10cSrcweir return sal_False; 965cdf0e10cSrcweir } 966cdf0e10cSrcweir 967cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( const ::rtl::OUString& rName, EmbeddedObjectContainer& rCnt ) 968cdf0e10cSrcweir { 969cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Name )" ); 970cdf0e10cSrcweir 971cdf0e10cSrcweir // find object entry 972cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt2 = rCnt.pImpl->maObjectContainer.find( rName ); 973cdf0e10cSrcweir OSL_ENSURE( aIt2 == rCnt.pImpl->maObjectContainer.end(), "Object does already exist in target container!" ); 974cdf0e10cSrcweir 975cdf0e10cSrcweir if ( aIt2 != rCnt.pImpl->maObjectContainer.end() ) 976cdf0e10cSrcweir return sal_False; 977cdf0e10cSrcweir 978cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj; 979cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName ); 980cdf0e10cSrcweir if ( aIt != pImpl->maObjectContainer.end() ) 981cdf0e10cSrcweir { 982cdf0e10cSrcweir xObj = (*aIt).second; 983cdf0e10cSrcweir try 984cdf0e10cSrcweir { 985cdf0e10cSrcweir if ( xObj.is() ) 986cdf0e10cSrcweir { 987cdf0e10cSrcweir // move object 988cdf0e10cSrcweir ::rtl::OUString aName( rName ); 989cdf0e10cSrcweir rCnt.InsertEmbeddedObject( xObj, aName ); 990cdf0e10cSrcweir pImpl->maObjectContainer.erase( aIt ); 991cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 992cdf0e10cSrcweir if ( xPersist.is() ) 993cdf0e10cSrcweir pImpl->mxStorage->removeElement( rName ); 994cdf0e10cSrcweir } 995cdf0e10cSrcweir else 996cdf0e10cSrcweir { 997cdf0e10cSrcweir // copy storages; object *must* have persistence! 998cdf0e10cSrcweir uno::Reference < embed::XStorage > xOld = pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READ ); 999cdf0e10cSrcweir uno::Reference < embed::XStorage > xNew = rCnt.pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READWRITE ); 1000cdf0e10cSrcweir xOld->copyToStorage( xNew ); 1001cdf0e10cSrcweir } 1002cdf0e10cSrcweir 1003cdf0e10cSrcweir rCnt.TryToCopyGraphReplacement( *this, rName, rName ); 1004cdf0e10cSrcweir // RemoveGraphicStream( rName ); 1005cdf0e10cSrcweir 1006cdf0e10cSrcweir return sal_True; 1007cdf0e10cSrcweir } 1008cdf0e10cSrcweir catch ( uno::Exception& ) 1009cdf0e10cSrcweir { 1010cdf0e10cSrcweir OSL_ENSURE(0,"Could not move object!"); 1011cdf0e10cSrcweir return sal_False; 1012cdf0e10cSrcweir } 1013cdf0e10cSrcweir 1014cdf0e10cSrcweir } 1015cdf0e10cSrcweir else 1016cdf0e10cSrcweir OSL_ENSURE(0,"Unknown object!"); 1017cdf0e10cSrcweir return sal_False; 1018cdf0e10cSrcweir } 1019cdf0e10cSrcweir 10206170fa3cSArmin Le Grand //sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, sal_Bool bClose ) 10216170fa3cSArmin Le Grand // #i119941, bKeepToTempStorage: use to specify whether store the removed object to temporary storage+ 10226170fa3cSArmin Le Grand sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, sal_Bool bClose, sal_Bool bKeepToTempStorage ) 1023cdf0e10cSrcweir { 1024cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Object )" ); 1025cdf0e10cSrcweir 1026cdf0e10cSrcweir uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 1027cdf0e10cSrcweir ::rtl::OUString aName; 1028cdf0e10cSrcweir if ( xPersist.is() ) 1029cdf0e10cSrcweir aName = xPersist->getEntryName(); 1030cdf0e10cSrcweir 1031cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 1032cdf0e10cSrcweir uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY ); 1033cdf0e10cSrcweir uno::Reference < embed::XLinkageSupport > xLink( xPersist, uno::UNO_QUERY ); 1034cdf0e10cSrcweir sal_Bool bIsNotEmbedded = !xPersist.is() || xLink.is() && xLink->isLink(); 1035cdf0e10cSrcweir 1036cdf0e10cSrcweir // if the object has a persistance and the object is not a link than it must have persistence entry in the storage 1037cdf0e10cSrcweir OSL_ENSURE( bIsNotEmbedded || xAccess->hasByName(aName), "Removing element not present in storage!" ); 1038cdf0e10cSrcweir #endif 1039cdf0e10cSrcweir 1040cdf0e10cSrcweir // try to close it if permitted 1041cdf0e10cSrcweir if ( bClose ) 1042cdf0e10cSrcweir { 1043cdf0e10cSrcweir uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY ); 1044cdf0e10cSrcweir try 1045cdf0e10cSrcweir { 1046cdf0e10cSrcweir xClose->close( sal_True ); 1047cdf0e10cSrcweir } 1048cdf0e10cSrcweir catch ( util::CloseVetoException& ) 1049cdf0e10cSrcweir { 1050cdf0e10cSrcweir bClose = sal_False; 1051cdf0e10cSrcweir } 1052cdf0e10cSrcweir } 1053cdf0e10cSrcweir 1054cdf0e10cSrcweir if ( !bClose ) 1055cdf0e10cSrcweir { 1056cdf0e10cSrcweir // somebody still needs the object, so we must assign a temporary persistence 1057cdf0e10cSrcweir try 1058cdf0e10cSrcweir { 10596170fa3cSArmin Le Grand // if ( xPersist.is() ) 10606170fa3cSArmin Le Grand if ( xPersist.is() && bKeepToTempStorage ) // #i119941 1061cdf0e10cSrcweir { 1062cdf0e10cSrcweir /* 1063cdf0e10cSrcweir //TODO/LATER: needs storage handling! Why not letting the object do it?! 1064cdf0e10cSrcweir if ( !pImpl->mxTempStorage.is() ) 1065cdf0e10cSrcweir pImpl->mxTempStorage = ::comphelper::OStorageHelper::GetTemporaryStorage(); 1066cdf0e10cSrcweir uno::Sequence < beans::PropertyValue > aSeq; 1067cdf0e10cSrcweir 1068cdf0e10cSrcweir ::rtl::OUString aTmpPersistName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Object ") ); 1069cdf0e10cSrcweir aTmpPersistName += ::rtl::OUString::valueOf( (sal_Int32) pImpl->maTempObjectContainer.size() ); 1070cdf0e10cSrcweir 1071cdf0e10cSrcweir xPersist->storeAsEntry( pImpl->mxTempStorage, aTmpPersistName, aSeq, aSeq ); 1072cdf0e10cSrcweir xPersist->saveCompleted( sal_True ); 1073cdf0e10cSrcweir 1074cdf0e10cSrcweir pImpl->maTempObjectContainer[ aTmpPersistName ] = uno::Reference < embed::XEmbeddedObject >(); 1075cdf0e10cSrcweir */ 1076cdf0e10cSrcweir 1077cdf0e10cSrcweir if ( !pImpl->mpTempObjectContainer ) 1078cdf0e10cSrcweir { 1079cdf0e10cSrcweir pImpl->mpTempObjectContainer = new EmbeddedObjectContainer(); 1080cdf0e10cSrcweir try 1081cdf0e10cSrcweir { 1082cdf0e10cSrcweir // TODO/LATER: in future probably the temporary container will have two storages ( of two formats ) 1083cdf0e10cSrcweir // the media type will be provided with object insertion 1084cdf0e10cSrcweir ::rtl::OUString aOrigStorMediaType; 1085cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xStorProps( pImpl->mxStorage, uno::UNO_QUERY_THROW ); 1086cdf0e10cSrcweir static const ::rtl::OUString s_sMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType")); 1087cdf0e10cSrcweir xStorProps->getPropertyValue( s_sMediaType ) >>= aOrigStorMediaType; 1088cdf0e10cSrcweir 1089*49b34792SHerbert Dürr OSL_ENSURE( !aOrigStorMediaType.isEmpty(), "No valuable media type in the storage!\n" ); 1090cdf0e10cSrcweir 1091cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xTargetStorProps( 1092cdf0e10cSrcweir pImpl->mpTempObjectContainer->pImpl->mxStorage, 1093cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 1094cdf0e10cSrcweir xTargetStorProps->setPropertyValue( s_sMediaType,uno::makeAny( aOrigStorMediaType ) ); 1095cdf0e10cSrcweir } 1096cdf0e10cSrcweir catch( uno::Exception& ) 1097cdf0e10cSrcweir { 1098cdf0e10cSrcweir OSL_ENSURE( sal_False, "Can not set the new media type to a storage!\n" ); 1099cdf0e10cSrcweir } 1100cdf0e10cSrcweir } 1101cdf0e10cSrcweir 1102cdf0e10cSrcweir ::rtl::OUString aTempName, aMediaType; 1103cdf0e10cSrcweir pImpl->mpTempObjectContainer->InsertEmbeddedObject( xObj, aTempName ); 1104cdf0e10cSrcweir 1105cdf0e10cSrcweir uno::Reference < io::XInputStream > xStream = GetGraphicStream( xObj, &aMediaType ); 1106cdf0e10cSrcweir if ( xStream.is() ) 1107cdf0e10cSrcweir pImpl->mpTempObjectContainer->InsertGraphicStream( xStream, aTempName, aMediaType ); 1108cdf0e10cSrcweir 1109cdf0e10cSrcweir // object is stored, so at least it can be set to loaded state 1110cdf0e10cSrcweir xObj->changeState( embed::EmbedStates::LOADED ); 1111cdf0e10cSrcweir } 1112cdf0e10cSrcweir else 1113cdf0e10cSrcweir // objects without persistence need to stay in running state if they shall not be closed 1114cdf0e10cSrcweir xObj->changeState( embed::EmbedStates::RUNNING ); 1115cdf0e10cSrcweir } 1116cdf0e10cSrcweir catch ( uno::Exception& ) 1117cdf0e10cSrcweir { 1118cdf0e10cSrcweir return sal_False; 1119cdf0e10cSrcweir } 1120cdf0e10cSrcweir } 1121cdf0e10cSrcweir 1122cdf0e10cSrcweir sal_Bool bFound = sal_False; 1123cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 1124cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 1125cdf0e10cSrcweir { 1126cdf0e10cSrcweir if ( (*aIt).second == xObj ) 1127cdf0e10cSrcweir { 1128cdf0e10cSrcweir pImpl->maObjectContainer.erase( aIt ); 1129cdf0e10cSrcweir bFound = sal_True; 1130cdf0e10cSrcweir uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY ); 1131cdf0e10cSrcweir if ( xChild.is() ) 1132cdf0e10cSrcweir xChild->setParent( uno::Reference < uno::XInterface >() ); 1133cdf0e10cSrcweir break; 1134cdf0e10cSrcweir } 1135cdf0e10cSrcweir 1136cdf0e10cSrcweir aIt++; 1137cdf0e10cSrcweir } 1138cdf0e10cSrcweir 1139cdf0e10cSrcweir OSL_ENSURE( bFound, "Object not found for removal!" ); 11406170fa3cSArmin Le Grand // if ( xPersist.is() ) 11416170fa3cSArmin Le Grand if ( xPersist.is() && bKeepToTempStorage ) // #i119941 1142cdf0e10cSrcweir { 1143cdf0e10cSrcweir // remove replacement image (if there is one) 1144cdf0e10cSrcweir RemoveGraphicStream( aName ); 1145cdf0e10cSrcweir 1146cdf0e10cSrcweir // now it's time to remove the storage from the container storage 1147cdf0e10cSrcweir try 1148cdf0e10cSrcweir { 1149cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 1150cdf0e10cSrcweir // if the object has a persistance and the object is not a link than it must have persistence entry in storage 1151cdf0e10cSrcweir OSL_ENSURE( bIsNotEmbedded || pImpl->mxStorage->hasByName( aName ), "The object has no persistence entry in the storage!" ); 1152cdf0e10cSrcweir #endif 1153cdf0e10cSrcweir if ( xPersist.is() && pImpl->mxStorage->hasByName( aName ) ) 1154cdf0e10cSrcweir pImpl->mxStorage->removeElement( aName ); 1155cdf0e10cSrcweir } 1156cdf0e10cSrcweir catch ( uno::Exception& ) 1157cdf0e10cSrcweir { 1158cdf0e10cSrcweir OSL_ENSURE( sal_False, "Failed to remove object from storage!" ); 1159cdf0e10cSrcweir return sal_False; 1160cdf0e10cSrcweir } 1161cdf0e10cSrcweir } 1162cdf0e10cSrcweir 1163cdf0e10cSrcweir return sal_True; 1164cdf0e10cSrcweir } 1165cdf0e10cSrcweir 1166cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::CloseEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj ) 1167cdf0e10cSrcweir { 1168cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CloseEmbeddedObject" ); 1169cdf0e10cSrcweir 1170cdf0e10cSrcweir // disconnect the object from the container and close it if possible 1171cdf0e10cSrcweir 1172cdf0e10cSrcweir sal_Bool bFound = sal_False; 1173cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 1174cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 1175cdf0e10cSrcweir { 1176cdf0e10cSrcweir if ( (*aIt).second == xObj ) 1177cdf0e10cSrcweir { 1178cdf0e10cSrcweir pImpl->maObjectContainer.erase( aIt ); 1179cdf0e10cSrcweir bFound = sal_True; 1180cdf0e10cSrcweir break; 1181cdf0e10cSrcweir } 1182cdf0e10cSrcweir 1183cdf0e10cSrcweir aIt++; 1184cdf0e10cSrcweir } 1185cdf0e10cSrcweir 1186cdf0e10cSrcweir if ( bFound ) 1187cdf0e10cSrcweir { 1188cdf0e10cSrcweir uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY ); 1189cdf0e10cSrcweir try 1190cdf0e10cSrcweir { 1191cdf0e10cSrcweir xClose->close( sal_True ); 1192cdf0e10cSrcweir } 1193cdf0e10cSrcweir catch ( uno::Exception& ) 1194cdf0e10cSrcweir { 1195cdf0e10cSrcweir // it is no problem if the object is already closed 1196cdf0e10cSrcweir // TODO/LATER: what if the object can not be closed? 1197cdf0e10cSrcweir } 1198cdf0e10cSrcweir } 1199cdf0e10cSrcweir 1200cdf0e10cSrcweir return bFound; 1201cdf0e10cSrcweir } 1202cdf0e10cSrcweir 1203cdf0e10cSrcweir uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const ::rtl::OUString& aName, rtl::OUString* pMediaType ) 1204cdf0e10cSrcweir { 1205cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Name )" ); 1206cdf0e10cSrcweir 1207cdf0e10cSrcweir uno::Reference < io::XInputStream > xStream; 1208cdf0e10cSrcweir 1209*49b34792SHerbert Dürr OSL_ENSURE( !aName.isEmpty(), "Retrieving graphic for unknown object!" ); 1210*49b34792SHerbert Dürr if ( !aName.isEmpty() ) 1211cdf0e10cSrcweir { 1212cdf0e10cSrcweir try 1213cdf0e10cSrcweir { 1214cdf0e10cSrcweir uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements(); 1215cdf0e10cSrcweir uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( aName, embed::ElementModes::READ ); 1216cdf0e10cSrcweir xStream = xGraphicStream->getInputStream(); 1217cdf0e10cSrcweir if ( pMediaType ) 1218cdf0e10cSrcweir { 1219cdf0e10cSrcweir uno::Reference < beans::XPropertySet > xSet( xStream, uno::UNO_QUERY ); 1220cdf0e10cSrcweir if ( xSet.is() ) 1221cdf0e10cSrcweir { 1222cdf0e10cSrcweir uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString::createFromAscii("MediaType") ); 1223cdf0e10cSrcweir aAny >>= *pMediaType; 1224cdf0e10cSrcweir } 1225cdf0e10cSrcweir } 1226cdf0e10cSrcweir } 1227cdf0e10cSrcweir catch ( uno::Exception& ) 1228cdf0e10cSrcweir { 1229cdf0e10cSrcweir } 1230cdf0e10cSrcweir } 1231cdf0e10cSrcweir 1232cdf0e10cSrcweir return xStream; 1233cdf0e10cSrcweir } 1234cdf0e10cSrcweir 1235cdf0e10cSrcweir uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, rtl::OUString* pMediaType ) 1236cdf0e10cSrcweir { 1237cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Object )" ); 1238cdf0e10cSrcweir 1239cdf0e10cSrcweir // get the object name 1240cdf0e10cSrcweir ::rtl::OUString aName; 1241cdf0e10cSrcweir EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin(); 1242cdf0e10cSrcweir while ( aIt != pImpl->maObjectContainer.end() ) 1243cdf0e10cSrcweir { 1244cdf0e10cSrcweir if ( (*aIt).second == xObj ) 1245cdf0e10cSrcweir { 1246cdf0e10cSrcweir aName = (*aIt).first; 1247cdf0e10cSrcweir break; 1248cdf0e10cSrcweir } 1249cdf0e10cSrcweir 1250cdf0e10cSrcweir aIt++; 1251cdf0e10cSrcweir } 1252cdf0e10cSrcweir 1253cdf0e10cSrcweir // try to load it from the container storage 1254cdf0e10cSrcweir return GetGraphicStream( aName, pMediaType ); 1255cdf0e10cSrcweir } 1256cdf0e10cSrcweir 1257cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::InsertGraphicStream( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const ::rtl::OUString& rObjectName, const rtl::OUString& rMediaType ) 1258cdf0e10cSrcweir { 1259cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStream" ); 1260cdf0e10cSrcweir 1261cdf0e10cSrcweir try 1262cdf0e10cSrcweir { 1263cdf0e10cSrcweir uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements(); 1264cdf0e10cSrcweir 1265cdf0e10cSrcweir // store it into the subfolder 1266cdf0e10cSrcweir uno::Reference < io::XOutputStream > xOutStream; 1267cdf0e10cSrcweir uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( rObjectName, 1268cdf0e10cSrcweir embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE ); 1269cdf0e10cSrcweir xOutStream = xGraphicStream->getOutputStream(); 1270cdf0e10cSrcweir ::comphelper::OStorageHelper::CopyInputToOutput( rStream, xOutStream ); 1271cdf0e10cSrcweir xOutStream->flush(); 1272cdf0e10cSrcweir 1273cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPropSet( xGraphicStream, uno::UNO_QUERY ); 1274cdf0e10cSrcweir if ( !xPropSet.is() ) 1275cdf0e10cSrcweir throw uno::RuntimeException(); 1276cdf0e10cSrcweir 1277cdf0e10cSrcweir xPropSet->setPropertyValue( ::rtl::OUString::createFromAscii( "UseCommonStoragePasswordEncryption" ), 1278cdf0e10cSrcweir uno::makeAny( (sal_Bool)sal_True ) ); 1279cdf0e10cSrcweir uno::Any aAny; 1280cdf0e10cSrcweir aAny <<= rMediaType; 1281cdf0e10cSrcweir xPropSet->setPropertyValue( ::rtl::OUString::createFromAscii("MediaType"), aAny ); 1282cdf0e10cSrcweir 1283cdf0e10cSrcweir xPropSet->setPropertyValue( ::rtl::OUString::createFromAscii( "Compressed" ), 1284cdf0e10cSrcweir uno::makeAny( (sal_Bool)sal_True ) ); 1285cdf0e10cSrcweir } 1286cdf0e10cSrcweir catch( uno::Exception& ) 1287cdf0e10cSrcweir { 1288cdf0e10cSrcweir return sal_False; 1289cdf0e10cSrcweir } 1290cdf0e10cSrcweir 1291cdf0e10cSrcweir return sal_True; 1292cdf0e10cSrcweir } 1293cdf0e10cSrcweir 1294cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::InsertGraphicStreamDirectly( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const ::rtl::OUString& rObjectName, const rtl::OUString& rMediaType ) 1295cdf0e10cSrcweir { 1296cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStreamDirectly" ); 1297cdf0e10cSrcweir 1298cdf0e10cSrcweir try 1299cdf0e10cSrcweir { 1300cdf0e10cSrcweir uno::Reference < embed::XStorage > xReplacement = pImpl->GetReplacements(); 1301cdf0e10cSrcweir uno::Reference < embed::XOptimizedStorage > xOptRepl( xReplacement, uno::UNO_QUERY_THROW ); 1302cdf0e10cSrcweir 1303cdf0e10cSrcweir // store it into the subfolder 1304cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aProps( 3 ); 1305cdf0e10cSrcweir aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ); 1306cdf0e10cSrcweir aProps[0].Value <<= rMediaType; 1307cdf0e10cSrcweir aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseCommonStoragePasswordEncryption" ) ); 1308cdf0e10cSrcweir aProps[1].Value <<= (sal_Bool)sal_True; 1309cdf0e10cSrcweir aProps[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ); 1310cdf0e10cSrcweir aProps[2].Value <<= (sal_Bool)sal_True; 1311cdf0e10cSrcweir 1312cdf0e10cSrcweir if ( xReplacement->hasByName( rObjectName ) ) 1313cdf0e10cSrcweir xReplacement->removeElement( rObjectName ); 1314cdf0e10cSrcweir 1315cdf0e10cSrcweir xOptRepl->insertStreamElementDirect( rObjectName, rStream, aProps ); 1316cdf0e10cSrcweir } 1317cdf0e10cSrcweir catch( uno::Exception& ) 1318cdf0e10cSrcweir { 1319cdf0e10cSrcweir return sal_False; 1320cdf0e10cSrcweir } 1321cdf0e10cSrcweir 1322cdf0e10cSrcweir return sal_True; 1323cdf0e10cSrcweir } 1324cdf0e10cSrcweir 1325cdf0e10cSrcweir 1326cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::RemoveGraphicStream( const ::rtl::OUString& rObjectName ) 1327cdf0e10cSrcweir { 1328cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveGraphicStream" ); 1329cdf0e10cSrcweir 1330cdf0e10cSrcweir try 1331cdf0e10cSrcweir { 1332cdf0e10cSrcweir uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements(); 1333cdf0e10cSrcweir xReplacements->removeElement( rObjectName ); 1334cdf0e10cSrcweir } 1335cdf0e10cSrcweir catch( uno::Exception& ) 1336cdf0e10cSrcweir { 1337cdf0e10cSrcweir return sal_False; 1338cdf0e10cSrcweir } 1339cdf0e10cSrcweir 1340cdf0e10cSrcweir return sal_True; 1341cdf0e10cSrcweir } 1342cdf0e10cSrcweir namespace { 1343cdf0e10cSrcweir void InsertStreamIntoPicturesStorage_Impl( const uno::Reference< embed::XStorage >& xDocStor, 1344cdf0e10cSrcweir const uno::Reference< io::XInputStream >& xInStream, 1345cdf0e10cSrcweir const ::rtl::OUString& aStreamName ) 1346cdf0e10cSrcweir { 1347*49b34792SHerbert Dürr OSL_ENSURE( !aStreamName.isEmpty() && xInStream.is() && xDocStor.is(), "Misuse of the method!\n" ); 1348cdf0e10cSrcweir 1349cdf0e10cSrcweir try 1350cdf0e10cSrcweir { 1351cdf0e10cSrcweir uno::Reference< embed::XStorage > xPictures = xDocStor->openStorageElement( 1352cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pictures" ) ), 1353cdf0e10cSrcweir embed::ElementModes::READWRITE ); 1354cdf0e10cSrcweir uno::Reference< io::XStream > xObjReplStr = xPictures->openStreamElement( 1355cdf0e10cSrcweir aStreamName, 1356cdf0e10cSrcweir embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE ); 1357cdf0e10cSrcweir uno::Reference< io::XOutputStream > xOutStream( 1358cdf0e10cSrcweir xObjReplStr->getInputStream(), uno::UNO_QUERY_THROW ); 1359cdf0e10cSrcweir 1360cdf0e10cSrcweir ::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xOutStream ); 1361cdf0e10cSrcweir xOutStream->closeOutput(); 1362cdf0e10cSrcweir 1363cdf0e10cSrcweir uno::Reference< embed::XTransactedObject > xTransact( xPictures, uno::UNO_QUERY ); 1364cdf0e10cSrcweir if ( xTransact.is() ) 1365cdf0e10cSrcweir xTransact->commit(); 1366cdf0e10cSrcweir } 1367cdf0e10cSrcweir catch( uno::Exception& ) 1368cdf0e10cSrcweir { 1369cdf0e10cSrcweir OSL_ENSURE( sal_False, "The pictures storage is not available!\n" ); 1370cdf0e10cSrcweir } 1371cdf0e10cSrcweir } 1372cdf0e10cSrcweir 1373cdf0e10cSrcweir } 1374cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1375cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::StoreAsChildren(sal_Bool _bOasisFormat,sal_Bool _bCreateEmbedded,const uno::Reference < embed::XStorage >& _xStorage) 1376cdf0e10cSrcweir { 1377cdf0e10cSrcweir sal_Bool bResult = sal_False; 1378cdf0e10cSrcweir try 1379cdf0e10cSrcweir { 1380cdf0e10cSrcweir comphelper::EmbeddedObjectContainer aCnt( _xStorage ); 1381cdf0e10cSrcweir const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames(); 1382cdf0e10cSrcweir const ::rtl::OUString* pIter = aNames.getConstArray(); 1383cdf0e10cSrcweir const ::rtl::OUString* pEnd = pIter + aNames.getLength(); 1384cdf0e10cSrcweir for(;pIter != pEnd;++pIter) 1385cdf0e10cSrcweir { 1386cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter ); 1387cdf0e10cSrcweir OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" ); 1388cdf0e10cSrcweir if ( xObj.is() ) 1389cdf0e10cSrcweir { 1390cdf0e10cSrcweir sal_Bool bSwitchBackToLoaded = sal_False; 1391cdf0e10cSrcweir uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY ); 1392cdf0e10cSrcweir 1393cdf0e10cSrcweir uno::Reference < io::XInputStream > xStream; 1394cdf0e10cSrcweir ::rtl::OUString aMediaType; 1395cdf0e10cSrcweir 1396cdf0e10cSrcweir sal_Int32 nCurState = xObj->getCurrentState(); 1397cdf0e10cSrcweir if ( nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING ) 1398cdf0e10cSrcweir { 1399cdf0e10cSrcweir // means that the object is not active 1400cdf0e10cSrcweir // copy replacement image from old to new container 1401cdf0e10cSrcweir xStream = GetGraphicStream( xObj, &aMediaType ); 1402cdf0e10cSrcweir } 1403cdf0e10cSrcweir 1404cdf0e10cSrcweir if ( !xStream.is() ) 1405cdf0e10cSrcweir { 1406cdf0e10cSrcweir // the image must be regenerated 1407cdf0e10cSrcweir // TODO/LATER: another aspect could be used 1408cdf0e10cSrcweir if ( xObj->getCurrentState() == embed::EmbedStates::LOADED ) 1409cdf0e10cSrcweir bSwitchBackToLoaded = sal_True; 1410cdf0e10cSrcweir 1411cdf0e10cSrcweir xStream = GetGraphicReplacementStream( 1412cdf0e10cSrcweir embed::Aspects::MSOLE_CONTENT, 1413cdf0e10cSrcweir xObj, 1414cdf0e10cSrcweir &aMediaType ); 1415cdf0e10cSrcweir } 1416cdf0e10cSrcweir 1417cdf0e10cSrcweir if ( _bOasisFormat || (xLink.is() && xLink->isLink()) ) 1418cdf0e10cSrcweir { 1419cdf0e10cSrcweir if ( xStream.is() ) 1420cdf0e10cSrcweir { 1421cdf0e10cSrcweir if ( _bOasisFormat ) 1422cdf0e10cSrcweir { 1423cdf0e10cSrcweir // if it is an embedded object or the optimized inserting fails the normal inserting should be done 1424cdf0e10cSrcweir if ( _bCreateEmbedded 1425cdf0e10cSrcweir || !aCnt.InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) ) 1426cdf0e10cSrcweir aCnt.InsertGraphicStream( xStream, *pIter, aMediaType ); 1427cdf0e10cSrcweir } 1428cdf0e10cSrcweir else 1429cdf0e10cSrcweir { 1430cdf0e10cSrcweir // it is a linked object exported into SO7 format 1431cdf0e10cSrcweir InsertStreamIntoPicturesStorage_Impl( _xStorage, xStream, *pIter ); 1432cdf0e10cSrcweir } 1433cdf0e10cSrcweir } 1434cdf0e10cSrcweir } 1435cdf0e10cSrcweir 1436cdf0e10cSrcweir uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 1437cdf0e10cSrcweir if ( xPersist.is() ) 1438cdf0e10cSrcweir { 1439cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aArgs( _bOasisFormat ? 2 : 3 ); 1440cdf0e10cSrcweir aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StoreVisualReplacement" ) ); 1441cdf0e10cSrcweir aArgs[0].Value <<= (sal_Bool)( !_bOasisFormat ); 1442cdf0e10cSrcweir 1443cdf0e10cSrcweir // if it is an embedded object or the optimized inserting fails the normal inserting should be done 1444cdf0e10cSrcweir aArgs[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CanTryOptimization" ) ); 1445cdf0e10cSrcweir aArgs[1].Value <<= !_bCreateEmbedded; 1446cdf0e10cSrcweir if ( !_bOasisFormat ) 1447cdf0e10cSrcweir { 1448cdf0e10cSrcweir // if object has no cached replacement it will use this one 1449cdf0e10cSrcweir aArgs[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VisualReplacement" ) ); 1450cdf0e10cSrcweir aArgs[2].Value <<= xStream; 1451cdf0e10cSrcweir } 1452cdf0e10cSrcweir 1453cdf0e10cSrcweir xPersist->storeAsEntry( _xStorage, 1454cdf0e10cSrcweir xPersist->getEntryName(), 1455cdf0e10cSrcweir uno::Sequence< beans::PropertyValue >(), 1456cdf0e10cSrcweir aArgs ); 1457cdf0e10cSrcweir } 1458cdf0e10cSrcweir 1459cdf0e10cSrcweir if ( bSwitchBackToLoaded ) 1460cdf0e10cSrcweir // switch back to loaded state; that way we have a minimum cache confusion 1461cdf0e10cSrcweir xObj->changeState( embed::EmbedStates::LOADED ); 1462cdf0e10cSrcweir } 1463cdf0e10cSrcweir } 1464cdf0e10cSrcweir 1465cdf0e10cSrcweir bResult = aCnt.CommitImageSubStorage(); 1466cdf0e10cSrcweir 1467cdf0e10cSrcweir } 1468cdf0e10cSrcweir catch ( uno::Exception& ) 1469cdf0e10cSrcweir { 1470cdf0e10cSrcweir // TODO/LATER: error handling 1471cdf0e10cSrcweir bResult = sal_False; 1472cdf0e10cSrcweir } 1473cdf0e10cSrcweir 1474cdf0e10cSrcweir // the old SO6 format does not store graphical replacements 1475cdf0e10cSrcweir if ( !_bOasisFormat && bResult ) 1476cdf0e10cSrcweir { 1477cdf0e10cSrcweir try 1478cdf0e10cSrcweir { 1479cdf0e10cSrcweir // the substorage still can not be locked by the embedded object conteiner 1480cdf0e10cSrcweir ::rtl::OUString aObjReplElement( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" ) ); 1481cdf0e10cSrcweir if ( _xStorage->hasByName( aObjReplElement ) && _xStorage->isStorageElement( aObjReplElement ) ) 1482cdf0e10cSrcweir _xStorage->removeElement( aObjReplElement ); 1483cdf0e10cSrcweir } 1484cdf0e10cSrcweir catch ( uno::Exception& ) 1485cdf0e10cSrcweir { 1486cdf0e10cSrcweir // TODO/LATER: error handling; 1487cdf0e10cSrcweir bResult = sal_False; 1488cdf0e10cSrcweir } 1489cdf0e10cSrcweir } 1490cdf0e10cSrcweir return bResult; 1491cdf0e10cSrcweir } 1492cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1493cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::StoreChildren(sal_Bool _bOasisFormat,sal_Bool _bObjectsOnly) 1494cdf0e10cSrcweir { 1495cdf0e10cSrcweir sal_Bool bResult = sal_True; 1496cdf0e10cSrcweir const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames(); 1497cdf0e10cSrcweir const ::rtl::OUString* pIter = aNames.getConstArray(); 1498cdf0e10cSrcweir const ::rtl::OUString* pEnd = pIter + aNames.getLength(); 1499cdf0e10cSrcweir for(;pIter != pEnd;++pIter) 1500cdf0e10cSrcweir { 1501cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter ); 1502cdf0e10cSrcweir OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" ); 1503cdf0e10cSrcweir if ( xObj.is() ) 1504cdf0e10cSrcweir { 1505cdf0e10cSrcweir sal_Int32 nCurState = xObj->getCurrentState(); 1506cdf0e10cSrcweir if ( _bOasisFormat && nCurState != embed::EmbedStates::LOADED && nCurState != embed::EmbedStates::RUNNING ) 1507cdf0e10cSrcweir { 1508cdf0e10cSrcweir // means that the object is active 1509cdf0e10cSrcweir // the image must be regenerated 1510cdf0e10cSrcweir ::rtl::OUString aMediaType; 1511cdf0e10cSrcweir 1512cdf0e10cSrcweir // TODO/LATER: another aspect could be used 1513cdf0e10cSrcweir uno::Reference < io::XInputStream > xStream = 1514cdf0e10cSrcweir GetGraphicReplacementStream( 1515cdf0e10cSrcweir embed::Aspects::MSOLE_CONTENT, 1516cdf0e10cSrcweir xObj, 1517cdf0e10cSrcweir &aMediaType ); 1518cdf0e10cSrcweir if ( xStream.is() ) 1519cdf0e10cSrcweir { 1520cdf0e10cSrcweir if ( !InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) ) 1521cdf0e10cSrcweir InsertGraphicStream( xStream, *pIter, aMediaType ); 1522cdf0e10cSrcweir } 1523cdf0e10cSrcweir } 1524cdf0e10cSrcweir 1525cdf0e10cSrcweir // TODO/LATER: currently the object by default does not cache replacement image 1526cdf0e10cSrcweir // that means that if somebody loads SO7 document and store its objects using 1527cdf0e10cSrcweir // this method the images might be lost. 1528cdf0e10cSrcweir // Currently this method is only used on storing to alien formats, that means 1529cdf0e10cSrcweir // that SO7 documents storing does not use it, and all other filters are 1530cdf0e10cSrcweir // based on OASIS format. But if it changes the method must be fixed. The fix 1531cdf0e10cSrcweir // must be done only on demand since it can affect performance. 1532cdf0e10cSrcweir 1533cdf0e10cSrcweir uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 1534cdf0e10cSrcweir if ( xPersist.is() ) 1535cdf0e10cSrcweir { 1536cdf0e10cSrcweir try 1537cdf0e10cSrcweir { 1538cdf0e10cSrcweir //TODO/LATER: only storing if changed! 153908c6ef97SAndre Fischer //xPersist->storeOwn(); //commented, i120168 154008c6ef97SAndre Fischer 154108c6ef97SAndre Fischer // begin:all charts will be persited as xml format on disk when saving, which is time consuming. 154208c6ef97SAndre Fischer // '_bObjectsOnly' mean we are storing to alien formats. 154308c6ef97SAndre Fischer // 'isStorageElement' mean current object is NOT an MS OLE format. (may also include in future), i120168 154408c6ef97SAndre Fischer if (_bObjectsOnly && (nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING) 154508c6ef97SAndre Fischer && (pImpl->mxStorage->isStorageElement( *pIter ) )) 154608c6ef97SAndre Fischer { 154708c6ef97SAndre Fischer uno::Reference< util::XModifiable > xModifiable( xObj->getComponent(), uno::UNO_QUERY ); 154808c6ef97SAndre Fischer if ( xModifiable.is() && xModifiable->isModified()) 154908c6ef97SAndre Fischer { 155008c6ef97SAndre Fischer xPersist->storeOwn(); 155108c6ef97SAndre Fischer } 155208c6ef97SAndre Fischer else 155308c6ef97SAndre Fischer { 155408c6ef97SAndre Fischer //do nothing.embeded model is not modified, no need to persist. 155508c6ef97SAndre Fischer } 155608c6ef97SAndre Fischer } 155708c6ef97SAndre Fischer else //the embeded object is in active status, always store back it. 155808c6ef97SAndre Fischer { 155908c6ef97SAndre Fischer xPersist->storeOwn(); 156008c6ef97SAndre Fischer } 156108c6ef97SAndre Fischer //end i120168 1562cdf0e10cSrcweir } 1563cdf0e10cSrcweir catch( uno::Exception& ) 1564cdf0e10cSrcweir { 1565cdf0e10cSrcweir // TODO/LATER: error handling 1566cdf0e10cSrcweir bResult = sal_False; 1567cdf0e10cSrcweir break; 1568cdf0e10cSrcweir } 1569cdf0e10cSrcweir } 1570cdf0e10cSrcweir 1571cdf0e10cSrcweir if ( !_bOasisFormat && !_bObjectsOnly ) 1572cdf0e10cSrcweir { 1573cdf0e10cSrcweir // copy replacement images for linked objects 1574cdf0e10cSrcweir try 1575cdf0e10cSrcweir { 1576cdf0e10cSrcweir uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY ); 1577cdf0e10cSrcweir if ( xLink.is() && xLink->isLink() ) 1578cdf0e10cSrcweir { 1579cdf0e10cSrcweir ::rtl::OUString aMediaType; 1580cdf0e10cSrcweir uno::Reference < io::XInputStream > xInStream = GetGraphicStream( xObj, &aMediaType ); 1581cdf0e10cSrcweir if ( xInStream.is() ) 1582cdf0e10cSrcweir InsertStreamIntoPicturesStorage_Impl( pImpl->mxStorage, xInStream, *pIter ); 1583cdf0e10cSrcweir } 1584cdf0e10cSrcweir } 1585cdf0e10cSrcweir catch( uno::Exception& ) 1586cdf0e10cSrcweir { 1587cdf0e10cSrcweir } 1588cdf0e10cSrcweir } 1589cdf0e10cSrcweir } 1590cdf0e10cSrcweir } 1591cdf0e10cSrcweir 1592cdf0e10cSrcweir if ( bResult && _bOasisFormat ) 1593cdf0e10cSrcweir bResult = CommitImageSubStorage(); 1594cdf0e10cSrcweir 1595cdf0e10cSrcweir if ( bResult && !_bObjectsOnly ) 1596cdf0e10cSrcweir { 1597cdf0e10cSrcweir try 1598cdf0e10cSrcweir { 1599cdf0e10cSrcweir ReleaseImageSubStorage(); 1600cdf0e10cSrcweir ::rtl::OUString aObjReplElement( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" ) ); 1601cdf0e10cSrcweir if ( !_bOasisFormat && pImpl->mxStorage->hasByName( aObjReplElement ) && pImpl->mxStorage->isStorageElement( aObjReplElement ) ) 1602cdf0e10cSrcweir pImpl->mxStorage->removeElement( aObjReplElement ); 1603cdf0e10cSrcweir } 1604cdf0e10cSrcweir catch( uno::Exception& ) 1605cdf0e10cSrcweir { 1606cdf0e10cSrcweir // TODO/LATER: error handling 1607cdf0e10cSrcweir bResult = sal_False; 1608cdf0e10cSrcweir } 1609cdf0e10cSrcweir } 1610cdf0e10cSrcweir return bResult; 1611cdf0e10cSrcweir } 1612cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1613cdf0e10cSrcweir uno::Reference< io::XInputStream > EmbeddedObjectContainer::GetGraphicReplacementStream( 1614cdf0e10cSrcweir sal_Int64 nViewAspect, 1615cdf0e10cSrcweir const uno::Reference< embed::XEmbeddedObject >& xObj, 1616cdf0e10cSrcweir ::rtl::OUString* pMediaType ) 1617cdf0e10cSrcweir { 1618cdf0e10cSrcweir uno::Reference< io::XInputStream > xInStream; 1619cdf0e10cSrcweir if ( xObj.is() ) 1620cdf0e10cSrcweir { 1621cdf0e10cSrcweir try 1622cdf0e10cSrcweir { 1623cdf0e10cSrcweir // retrieving of the visual representation can switch object to running state 1624cdf0e10cSrcweir embed::VisualRepresentation aRep = xObj->getPreferredVisualRepresentation( nViewAspect ); 1625cdf0e10cSrcweir if ( pMediaType ) 1626cdf0e10cSrcweir *pMediaType = aRep.Flavor.MimeType; 1627cdf0e10cSrcweir 1628cdf0e10cSrcweir uno::Sequence < sal_Int8 > aSeq; 1629cdf0e10cSrcweir aRep.Data >>= aSeq; 1630cdf0e10cSrcweir xInStream = new ::comphelper::SequenceInputStream( aSeq ); 1631cdf0e10cSrcweir } 1632cdf0e10cSrcweir catch ( uno::Exception& ) 1633cdf0e10cSrcweir { 1634cdf0e10cSrcweir } 1635cdf0e10cSrcweir } 1636cdf0e10cSrcweir 1637cdf0e10cSrcweir return xInStream; 1638cdf0e10cSrcweir } 1639cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1640cdf0e10cSrcweir sal_Bool EmbeddedObjectContainer::SetPersistentEntries(const uno::Reference< embed::XStorage >& _xStorage,bool _bClearModifedFlag) 1641cdf0e10cSrcweir { 1642cdf0e10cSrcweir sal_Bool bError = sal_False; 1643cdf0e10cSrcweir const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames(); 1644cdf0e10cSrcweir const ::rtl::OUString* pIter = aNames.getConstArray(); 1645cdf0e10cSrcweir const ::rtl::OUString* pEnd = pIter + aNames.getLength(); 1646cdf0e10cSrcweir for(;pIter != pEnd;++pIter) 1647cdf0e10cSrcweir { 1648cdf0e10cSrcweir uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter ); 1649cdf0e10cSrcweir OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" ); 1650cdf0e10cSrcweir if ( xObj.is() ) 1651cdf0e10cSrcweir { 1652cdf0e10cSrcweir uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY ); 1653cdf0e10cSrcweir if ( xPersist.is() ) 1654cdf0e10cSrcweir { 1655cdf0e10cSrcweir try 1656cdf0e10cSrcweir { 1657cdf0e10cSrcweir xPersist->setPersistentEntry( _xStorage, 1658cdf0e10cSrcweir *pIter, 1659cdf0e10cSrcweir embed::EntryInitModes::NO_INIT, 1660cdf0e10cSrcweir uno::Sequence< beans::PropertyValue >(), 1661cdf0e10cSrcweir uno::Sequence< beans::PropertyValue >() ); 1662cdf0e10cSrcweir 1663cdf0e10cSrcweir } 1664cdf0e10cSrcweir catch( uno::Exception& ) 1665cdf0e10cSrcweir { 1666cdf0e10cSrcweir // TODO/LATER: error handling 1667cdf0e10cSrcweir bError = sal_True; 1668cdf0e10cSrcweir break; 1669cdf0e10cSrcweir } 1670cdf0e10cSrcweir } 1671cdf0e10cSrcweir if ( _bClearModifedFlag ) 1672cdf0e10cSrcweir { 1673cdf0e10cSrcweir // if this method is used as part of SaveCompleted the object must stay unmodified after execution 1674cdf0e10cSrcweir try 1675cdf0e10cSrcweir { 1676cdf0e10cSrcweir uno::Reference< util::XModifiable > xModif( xObj->getComponent(), uno::UNO_QUERY_THROW ); 1677cdf0e10cSrcweir if ( xModif->isModified() ) 1678cdf0e10cSrcweir xModif->setModified( sal_False ); 1679cdf0e10cSrcweir } 1680cdf0e10cSrcweir catch( uno::Exception& ) 1681cdf0e10cSrcweir { 1682cdf0e10cSrcweir } 1683cdf0e10cSrcweir } 1684cdf0e10cSrcweir } 1685cdf0e10cSrcweir } 1686cdf0e10cSrcweir return bError; 1687cdf0e10cSrcweir } 1688cdf0e10cSrcweir } 1689