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