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