xref: /trunk/main/sw/source/core/ole/ndole.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_sw.hxx"
24cdf0e10cSrcweir #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
25cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
26cdf0e10cSrcweir #include <com/sun/star/embed/XEmbedPersist.hpp>
27cdf0e10cSrcweir #include <com/sun/star/embed/XLinkageSupport.hpp>
28cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
29cdf0e10cSrcweir #include <com/sun/star/embed/EmbedMisc.hpp>
30cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
31cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
32cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
33cdf0e10cSrcweir #include <com/sun/star/document/XEventBroadcaster.hpp>
346170fa3cSArmin Le Grand #include <com/sun/star/chart2/XChartDocument.hpp>   // #i119941
35cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
38cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
39cdf0e10cSrcweir #include <hintids.hxx>
40cdf0e10cSrcweir #include <tools/urlobj.hxx>
41cdf0e10cSrcweir #include <sfx2/docfile.hxx>
42cdf0e10cSrcweir #include <sfx2/app.hxx>
43cdf0e10cSrcweir #include <sfx2/linkmgr.hxx>
44cdf0e10cSrcweir #include <unotools/configitem.hxx>
45cdf0e10cSrcweir #ifndef _OUTDEV_HXX //autogen
46cdf0e10cSrcweir #include <vcl/outdev.hxx>
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir #include <fmtanchr.hxx>
49cdf0e10cSrcweir #include <frmfmt.hxx>
50cdf0e10cSrcweir #include <doc.hxx>
51cdf0e10cSrcweir #include <docsh.hxx>
52cdf0e10cSrcweir #include <pam.hxx>
53cdf0e10cSrcweir #include <section.hxx>
54cdf0e10cSrcweir #include <cntfrm.hxx>
55cdf0e10cSrcweir #include <frmatr.hxx>
56cdf0e10cSrcweir #ifndef _DOCSH_HXX
57cdf0e10cSrcweir #include <docsh.hxx>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir #include <ndole.hxx>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #include <comphelper/classids.hxx>
62cdf0e10cSrcweir #include <vcl/graph.hxx>
63cdf0e10cSrcweir #include <sot/formats.hxx>
64cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
65cdf0e10cSrcweir #include <svtools/filter.hxx>
66cdf0e10cSrcweir #ifndef _COMCORE_HRC
67cdf0e10cSrcweir #include <comcore.hrc>
68cdf0e10cSrcweir #endif
69cdf0e10cSrcweir 
70cdf0e10cSrcweir using rtl::OUString;
71cdf0e10cSrcweir using namespace utl;
72cdf0e10cSrcweir using namespace com::sun::star::uno;
73cdf0e10cSrcweir using namespace com::sun::star;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir class SwOLELRUCache : private SvPtrarr, private utl::ConfigItem
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     sal_uInt16 nLRU_InitSize;
78cdf0e10cSrcweir     sal_Bool bInUnload;
79cdf0e10cSrcweir     uno::Sequence< rtl::OUString > GetPropertyNames();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir public:
82cdf0e10cSrcweir     SwOLELRUCache();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     virtual void Notify( const uno::Sequence<
85cdf0e10cSrcweir                                 rtl::OUString>& aPropertyNames );
86cdf0e10cSrcweir     virtual void Commit();
87cdf0e10cSrcweir     void Load();
88cdf0e10cSrcweir 
SetInUnload(sal_Bool bFlag)89cdf0e10cSrcweir     void SetInUnload( sal_Bool bFlag )  { bInUnload = bFlag; }
90cdf0e10cSrcweir     using SvPtrarr::Count;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     void InsertObj( SwOLEObj& rObj );
93cdf0e10cSrcweir     void RemoveObj( SwOLEObj& rObj );
94cdf0e10cSrcweir 
RemovePtr(SwOLEObj * pObj)95cdf0e10cSrcweir     void RemovePtr( SwOLEObj* pObj )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         sal_uInt16 nPos = SvPtrarr::GetPos( pObj );
98cdf0e10cSrcweir         if( USHRT_MAX != nPos )
99cdf0e10cSrcweir             SvPtrarr::Remove( nPos );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir SwOLELRUCache* pOLELRU_Cache = 0;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir class SwOLEListener_Impl : public ::cppu::WeakImplHelper1< embed::XStateChangeListener >
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     SwOLEObj* mpObj;
108cdf0e10cSrcweir public:
109cdf0e10cSrcweir     SwOLEListener_Impl( SwOLEObj* pObj );
110cdf0e10cSrcweir     void Release();
111cdf0e10cSrcweir     virtual void SAL_CALL changingState( const lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (embed::WrongStateException, uno::RuntimeException);
112cdf0e10cSrcweir     virtual void SAL_CALL stateChanged( const lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (uno::RuntimeException);
113cdf0e10cSrcweir     virtual void SAL_CALL disposing( const lang::EventObject& aEvent ) throw (uno::RuntimeException);
114cdf0e10cSrcweir };
115cdf0e10cSrcweir 
SwOLEListener_Impl(SwOLEObj * pObj)116cdf0e10cSrcweir SwOLEListener_Impl::SwOLEListener_Impl( SwOLEObj* pObj )
117cdf0e10cSrcweir : mpObj( pObj )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     if ( mpObj->IsOleRef() && mpObj->GetOleRef()->getCurrentState() == embed::EmbedStates::RUNNING )
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         pOLELRU_Cache->InsertObj( *mpObj );
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
changingState(const lang::EventObject &,::sal_Int32,::sal_Int32)125cdf0e10cSrcweir void SAL_CALL SwOLEListener_Impl::changingState( const lang::EventObject&, ::sal_Int32 , ::sal_Int32 ) throw (embed::WrongStateException, uno::RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
stateChanged(const lang::EventObject &,::sal_Int32 nOldState,::sal_Int32 nNewState)129cdf0e10cSrcweir void SAL_CALL SwOLEListener_Impl::stateChanged( const lang::EventObject&, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     if ( mpObj && nOldState == embed::EmbedStates::LOADED && nNewState == embed::EmbedStates::RUNNING )
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         if( !pOLELRU_Cache )
134cdf0e10cSrcweir             pOLELRU_Cache = new SwOLELRUCache;
135cdf0e10cSrcweir         pOLELRU_Cache->InsertObj( *mpObj );
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir     else if ( mpObj && nNewState == embed::EmbedStates::LOADED && nOldState == embed::EmbedStates::RUNNING )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         if ( pOLELRU_Cache )
140cdf0e10cSrcweir             pOLELRU_Cache->RemoveObj( *mpObj );
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
Release()144cdf0e10cSrcweir void SwOLEListener_Impl::Release()
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     if ( mpObj && pOLELRU_Cache )
147cdf0e10cSrcweir         pOLELRU_Cache->RemoveObj( *mpObj );
148cdf0e10cSrcweir     mpObj=0;
149cdf0e10cSrcweir     release();
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
disposing(const lang::EventObject &)152cdf0e10cSrcweir void SAL_CALL SwOLEListener_Impl::disposing( const lang::EventObject& ) throw (uno::RuntimeException)
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     if ( mpObj && pOLELRU_Cache )
155cdf0e10cSrcweir         pOLELRU_Cache->RemoveObj( *mpObj );
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // --------------------
159cdf0e10cSrcweir // SwEmbedObjectLink
160cdf0e10cSrcweir // --------------------
161cdf0e10cSrcweir // TODO/LATER: actually SwEmbedObjectLink should be used here, but because different objects are used to control
162cdf0e10cSrcweir //             embedded object different link objects with the same functionality had to be implemented
163cdf0e10cSrcweir 
164cdf0e10cSrcweir class SwEmbedObjectLink : public sfx2::SvBaseLink
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     SwOLENode*          pOleNode;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir public:
169cdf0e10cSrcweir                         SwEmbedObjectLink(SwOLENode* pNode);
170cdf0e10cSrcweir     virtual             ~SwEmbedObjectLink();
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     virtual void        Closed();
173cdf0e10cSrcweir     virtual void        DataChanged( const String& rMimeType,
174cdf0e10cSrcweir                                 const uno::Any & rValue );
175cdf0e10cSrcweir 
Connect()176cdf0e10cSrcweir     sal_Bool            Connect() { return GetRealObject() != NULL; }
177cdf0e10cSrcweir };
178cdf0e10cSrcweir 
179cdf0e10cSrcweir // -----------------------------------------------------------------------------
180cdf0e10cSrcweir 
SwEmbedObjectLink(SwOLENode * pNode)181cdf0e10cSrcweir SwEmbedObjectLink::SwEmbedObjectLink(SwOLENode* pNode):
182cdf0e10cSrcweir     ::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL, SOT_FORMATSTR_ID_SVXB ),
183cdf0e10cSrcweir     pOleNode(pNode)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     SetSynchron( sal_False );
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir // -----------------------------------------------------------------------------
189cdf0e10cSrcweir 
~SwEmbedObjectLink()190cdf0e10cSrcweir SwEmbedObjectLink::~SwEmbedObjectLink()
191cdf0e10cSrcweir {
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // -----------------------------------------------------------------------------
195cdf0e10cSrcweir 
DataChanged(const String &,const uno::Any &)196cdf0e10cSrcweir void SwEmbedObjectLink::DataChanged( const String& ,
197cdf0e10cSrcweir                                 const uno::Any & )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     if ( !pOleNode->UpdateLinkURL_Impl() )
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         // the link URL was not changed
202cdf0e10cSrcweir         uno::Reference< embed::XEmbeddedObject > xObject = pOleNode->GetOLEObj().GetOleRef();
203cdf0e10cSrcweir         OSL_ENSURE( xObject.is(), "The object must exist always!\n" );
204cdf0e10cSrcweir         if ( xObject.is() )
205cdf0e10cSrcweir         {
206cdf0e10cSrcweir             // let the object reload the link
207cdf0e10cSrcweir             // TODO/LATER: reload call could be used for this case
208cdf0e10cSrcweir 
209cdf0e10cSrcweir             try
210cdf0e10cSrcweir             {
211cdf0e10cSrcweir                 sal_Int32 nState = xObject->getCurrentState();
212cdf0e10cSrcweir                 if ( nState != embed::EmbedStates::LOADED )
213cdf0e10cSrcweir                 {
214cdf0e10cSrcweir                     // in some cases the linked file probably is not locked so it could be changed
215cdf0e10cSrcweir                     xObject->changeState( embed::EmbedStates::LOADED );
216cdf0e10cSrcweir                     xObject->changeState( nState );
217cdf0e10cSrcweir                 }
218cdf0e10cSrcweir             }
219cdf0e10cSrcweir             catch ( uno::Exception& )
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir             }
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     pOleNode->GetNewReplacement();
226cdf0e10cSrcweir     // Initiate repainting
227cdf0e10cSrcweir     // pObj->SetChanged();
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir // -----------------------------------------------------------------------------
231cdf0e10cSrcweir 
Closed()232cdf0e10cSrcweir void SwEmbedObjectLink::Closed()
233cdf0e10cSrcweir {
234cdf0e10cSrcweir     pOleNode->BreakFileLink_Impl();
235cdf0e10cSrcweir     SvBaseLink::Closed();
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir // --------------------
240cdf0e10cSrcweir // SwOLENode
241cdf0e10cSrcweir // --------------------
242cdf0e10cSrcweir 
SwOLENode(const SwNodeIndex & rWhere,const svt::EmbeddedObjectRef & xObj,SwGrfFmtColl * pGrfColl,SwAttrSet * pAutoAttr)243cdf0e10cSrcweir SwOLENode::SwOLENode( const SwNodeIndex &rWhere,
244cdf0e10cSrcweir                     const svt::EmbeddedObjectRef& xObj,
245cdf0e10cSrcweir                     SwGrfFmtColl *pGrfColl,
246cdf0e10cSrcweir                     SwAttrSet* pAutoAttr ) :
247cdf0e10cSrcweir     SwNoTxtNode( rWhere, ND_OLENODE, pGrfColl, pAutoAttr ),
248cdf0e10cSrcweir     aOLEObj( xObj ),
249cdf0e10cSrcweir     pGraphic(0),
250cdf0e10cSrcweir     bOLESizeInvalid( sal_False ),
251cdf0e10cSrcweir     mpObjectLink( NULL )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     aOLEObj.SetNode( this );
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
SwOLENode(const SwNodeIndex & rWhere,const String & rString,sal_Int64 nAspect,SwGrfFmtColl * pGrfColl,SwAttrSet * pAutoAttr)256cdf0e10cSrcweir SwOLENode::SwOLENode( const SwNodeIndex &rWhere,
257cdf0e10cSrcweir                     const String &rString,
258cdf0e10cSrcweir                     sal_Int64 nAspect,
259cdf0e10cSrcweir                     SwGrfFmtColl *pGrfColl,
260cdf0e10cSrcweir                     SwAttrSet* pAutoAttr ) :
261cdf0e10cSrcweir     SwNoTxtNode( rWhere, ND_OLENODE, pGrfColl, pAutoAttr ),
262cdf0e10cSrcweir     aOLEObj( rString, nAspect ),
263cdf0e10cSrcweir     pGraphic(0),
264cdf0e10cSrcweir     bOLESizeInvalid( sal_False ),
265cdf0e10cSrcweir     mpObjectLink( NULL )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir     aOLEObj.SetNode( this );
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
~SwOLENode()270cdf0e10cSrcweir SwOLENode::~SwOLENode()
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     DisconnectFileLink_Impl();
273cdf0e10cSrcweir     delete pGraphic;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
GetGraphic()276cdf0e10cSrcweir Graphic* SwOLENode::GetGraphic()
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     if ( aOLEObj.GetOleRef().is() )
279cdf0e10cSrcweir         return aOLEObj.xOLERef.GetGraphic();
280cdf0e10cSrcweir     return pGraphic;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
GetHCGraphic()283cdf0e10cSrcweir Graphic* SwOLENode::GetHCGraphic()
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     return aOLEObj.xOLERef.GetHCGraphic();
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
SplitCntntNode(const SwPosition &)288cdf0e10cSrcweir SwCntntNode *SwOLENode::SplitCntntNode( const SwPosition & )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     // OLE-Objecte vervielfaeltigen ??
291cdf0e10cSrcweir     ASSERT( sal_False, "OleNode: can't split." );
292cdf0e10cSrcweir     return this;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir // Laden eines in den Undo-Bereich verschobenen OLE-Objekts
296cdf0e10cSrcweir 
RestorePersistentData()297cdf0e10cSrcweir sal_Bool SwOLENode::RestorePersistentData()
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     DBG_ASSERT( aOLEObj.GetOleRef().is(), "No object to restore!" );
300cdf0e10cSrcweir     if ( aOLEObj.xOLERef.is() )
301cdf0e10cSrcweir     {
302cdf0e10cSrcweir         // Falls bereits eine SvPersist-Instanz existiert, nehmen wir diese
303cdf0e10cSrcweir         SfxObjectShell* p = GetDoc()->GetPersist();
304cdf0e10cSrcweir         if( !p )
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             // TODO/LATER: reicht hier nicht ein EmbeddedObjectContainer? Was passiert mit
307cdf0e10cSrcweir             // diesem Dokument?
308870262e3SDon Lewis             ASSERT( sal_False, "why is a DocShell created here?" );
309cdf0e10cSrcweir             p = new SwDocShell( GetDoc(), SFX_CREATE_MODE_INTERNAL );
310cdf0e10cSrcweir             p->DoInitNew( NULL );
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir         uno::Reference < container::XChild > xChild( aOLEObj.xOLERef.GetObject(), uno::UNO_QUERY );
314cdf0e10cSrcweir         if ( xChild.is() )
315cdf0e10cSrcweir             xChild->setParent( p->GetModel() );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         DBG_ASSERT( aOLEObj.aName.Len(), "No object name!" );
318cdf0e10cSrcweir         ::rtl::OUString aObjName;
319cdf0e10cSrcweir         if ( !p->GetEmbeddedObjectContainer().InsertEmbeddedObject( aOLEObj.xOLERef.GetObject(), aObjName ) )
320cdf0e10cSrcweir         {
321cdf0e10cSrcweir             if ( xChild.is() )
322cdf0e10cSrcweir                 xChild->setParent( 0 );
323cdf0e10cSrcweir             DBG_ERROR( "InsertObject failed" );
324cdf0e10cSrcweir         }
325cdf0e10cSrcweir         else
326cdf0e10cSrcweir         {
327cdf0e10cSrcweir             aOLEObj.aName = aObjName;
328cdf0e10cSrcweir             aOLEObj.xOLERef.AssignToContainer( &p->GetEmbeddedObjectContainer(), aObjName );
329cdf0e10cSrcweir             CheckFileLink_Impl();
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir 
333cdf0e10cSrcweir     return sal_True;
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir // OLE object is transported into UNDO area
SavePersistentData()337cdf0e10cSrcweir sal_Bool SwOLENode::SavePersistentData()
338cdf0e10cSrcweir {
339cdf0e10cSrcweir     if( aOLEObj.xOLERef.is() )
340cdf0e10cSrcweir     {
341cdf0e10cSrcweir         comphelper::EmbeddedObjectContainer* pCnt = aOLEObj.xOLERef.GetContainer();
342cdf0e10cSrcweir 
343cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
344cdf0e10cSrcweir         SfxObjectShell* p = GetDoc()->GetPersist();
345cdf0e10cSrcweir         DBG_ASSERT( p, "No document!" );
346cdf0e10cSrcweir         if( p )
347cdf0e10cSrcweir         {
348cdf0e10cSrcweir             comphelper::EmbeddedObjectContainer& rCnt = p->GetEmbeddedObjectContainer();
349cdf0e10cSrcweir             OSL_ENSURE( !pCnt || &rCnt == pCnt, "The helper is assigned to unexpected container!\n" );
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir #endif
352cdf0e10cSrcweir 
353cdf0e10cSrcweir         if ( pCnt && pCnt->HasEmbeddedObject( aOLEObj.aName ) )
354cdf0e10cSrcweir         {
355cdf0e10cSrcweir             uno::Reference < container::XChild > xChild( aOLEObj.xOLERef.GetObject(), uno::UNO_QUERY );
356cdf0e10cSrcweir             if ( xChild.is() )
357cdf0e10cSrcweir                 xChild->setParent( 0 );
358cdf0e10cSrcweir 
3596170fa3cSArmin Le Grand           // pCnt->RemoveEmbeddedObject( aOLEObj.aName, sal_False );
360*2cdca4f8Smseidel            /* #i119941: When cut or move the chart, SwUndoFlyBase::DelFly will call SaveSection to store the content to storage.
3616170fa3cSArmin Le Grand            In this step, chart filter functions will be called. And chart filter will call chart core functions to create the chart again.
3626170fa3cSArmin Le Grand            Then chart core function will call the class ExplicitCategoryProvider to create data source.
3636170fa3cSArmin Le Grand            In this step, when SW data source provider create the data source, it will create a new SwFlyFrm.
3646170fa3cSArmin Le Grand            But later in SwUndoFlyBase::DelFly, it will clear anchor related attributes of SwFlyFrm. Then finally null pointer occur.
3656170fa3cSArmin Le Grand            Resolution:
3666170fa3cSArmin Le Grand            In pCnt->RemoveEmbeddedObject in SaveSection process of table chart, only remove the object from the object container,
3676170fa3cSArmin Le Grand            without removing it's storage and graphic stream. The chart already removed from formatter.> */
3686170fa3cSArmin Le Grand            sal_Bool bChartWithInternalProvider = sal_False;
3696170fa3cSArmin Le Grand            sal_Bool bKeepObjectToTempStorage = sal_True;
3706170fa3cSArmin Le Grand            uno::Reference < embed::XEmbeddedObject > xIP = GetOLEObj().GetOleRef();
3716170fa3cSArmin Le Grand            if ( svt::EmbeddedObjectRef::TryRunningState( xIP ) )
3726170fa3cSArmin Le Grand            {
3736170fa3cSArmin Le Grand                uno::Reference< chart2::XChartDocument > xChart( xIP->getComponent(), UNO_QUERY );
3746170fa3cSArmin Le Grand                if ( xChart.is() && xChart->hasInternalDataProvider() )
3756170fa3cSArmin Le Grand                    bChartWithInternalProvider = sal_True;
3766170fa3cSArmin Le Grand            }
3776170fa3cSArmin Le Grand 
3786170fa3cSArmin Le Grand            if ( IsChart() && sChartTblName.Len() && !bChartWithInternalProvider )
3796170fa3cSArmin Le Grand                bKeepObjectToTempStorage = sal_False;
3806170fa3cSArmin Le Grand            pCnt->RemoveEmbeddedObject( aOLEObj.aName, sal_False, bKeepObjectToTempStorage );
3816170fa3cSArmin Le Grand            // modify end
3826170fa3cSArmin Le Grand 
383cdf0e10cSrcweir 
384cdf0e10cSrcweir             // TODO/LATER: aOLEObj.aName has no meaning here, since the undo container contains the object
385*2cdca4f8Smseidel             // by different name, in future it might makes sense that the name is transported here.
386cdf0e10cSrcweir             aOLEObj.xOLERef.AssignToContainer( 0, aOLEObj.aName );
387cdf0e10cSrcweir             try
388cdf0e10cSrcweir             {
389cdf0e10cSrcweir                 // "unload" object
390cdf0e10cSrcweir                 aOLEObj.xOLERef->changeState( embed::EmbedStates::LOADED );
391cdf0e10cSrcweir             }
392cdf0e10cSrcweir             catch ( uno::Exception& )
393cdf0e10cSrcweir             {
394cdf0e10cSrcweir             }
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir     }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     DisconnectFileLink_Impl();
399cdf0e10cSrcweir 
400cdf0e10cSrcweir     return sal_True;
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 
MakeOLENode(const SwNodeIndex & rWhere,const svt::EmbeddedObjectRef & xObj,SwGrfFmtColl * pGrfColl,SwAttrSet * pAutoAttr)404cdf0e10cSrcweir SwOLENode * SwNodes::MakeOLENode( const SwNodeIndex & rWhere,
405cdf0e10cSrcweir                     const svt::EmbeddedObjectRef& xObj,
406cdf0e10cSrcweir                                     SwGrfFmtColl* pGrfColl,
407cdf0e10cSrcweir                                     SwAttrSet* pAutoAttr )
408cdf0e10cSrcweir {
409cdf0e10cSrcweir     ASSERT( pGrfColl,"SwNodes::MakeOLENode: Formatpointer ist 0." );
410cdf0e10cSrcweir 
411cdf0e10cSrcweir     SwOLENode *pNode =
412cdf0e10cSrcweir         new SwOLENode( rWhere, xObj, pGrfColl, pAutoAttr );
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     // set parent if XChild is supported
415cdf0e10cSrcweir     //!! needed to supply Math objects with a valid reference device
416cdf0e10cSrcweir     uno::Reference< container::XChild > xChild( pNode->GetOLEObj().GetObject().GetObject(), UNO_QUERY );
417cdf0e10cSrcweir     if (xChild.is())
418cdf0e10cSrcweir     {
419cdf0e10cSrcweir         SwDocShell *pDocSh = GetDoc()->GetDocShell();
420cdf0e10cSrcweir         if (pDocSh)
421cdf0e10cSrcweir             xChild->setParent( pDocSh->GetModel() );
422cdf0e10cSrcweir     }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir     return pNode;
425cdf0e10cSrcweir }
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 
MakeOLENode(const SwNodeIndex & rWhere,const String & rName,sal_Int64 nAspect,SwGrfFmtColl * pGrfColl,SwAttrSet * pAutoAttr)428cdf0e10cSrcweir SwOLENode * SwNodes::MakeOLENode( const SwNodeIndex & rWhere,
429cdf0e10cSrcweir     const String &rName, sal_Int64 nAspect, SwGrfFmtColl* pGrfColl, SwAttrSet* pAutoAttr )
430cdf0e10cSrcweir {
431cdf0e10cSrcweir     ASSERT( pGrfColl,"SwNodes::MakeOLENode: Formatpointer ist 0." );
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     SwOLENode *pNode =
434cdf0e10cSrcweir         new SwOLENode( rWhere, rName, nAspect, pGrfColl, pAutoAttr );
435cdf0e10cSrcweir 
436cdf0e10cSrcweir     // set parent if XChild is supported
437cdf0e10cSrcweir     //!! needed to supply Math objects with a valid reference device
438cdf0e10cSrcweir     uno::Reference< container::XChild > xChild( pNode->GetOLEObj().GetObject().GetObject(), UNO_QUERY );
439cdf0e10cSrcweir     if (xChild.is())
440cdf0e10cSrcweir     {
441cdf0e10cSrcweir         SwDocShell *pDocSh= GetDoc()->GetDocShell();
442cdf0e10cSrcweir         if (pDocSh)
443cdf0e10cSrcweir             xChild->setParent( pDocSh->GetModel() );
444cdf0e10cSrcweir     }
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     return pNode;
447cdf0e10cSrcweir }
448cdf0e10cSrcweir 
GetTwipSize() const449cdf0e10cSrcweir Size SwOLENode::GetTwipSize() const
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     MapMode aMapMode( MAP_TWIP );
452cdf0e10cSrcweir     return ((SwOLENode*)this)->aOLEObj.GetObject().GetSize( &aMapMode );
453cdf0e10cSrcweir }
454cdf0e10cSrcweir 
MakeCopy(SwDoc * pDoc,const SwNodeIndex & rIdx) const455cdf0e10cSrcweir SwCntntNode* SwOLENode::MakeCopy( SwDoc* pDoc, const SwNodeIndex& rIdx ) const
456cdf0e10cSrcweir {
457cdf0e10cSrcweir     // Falls bereits eine SvPersist-Instanz existiert, nehmen wir diese
458cdf0e10cSrcweir     SfxObjectShell* pPersistShell = pDoc->GetPersist();
459cdf0e10cSrcweir     if( !pPersistShell )
460cdf0e10cSrcweir     {
461cdf0e10cSrcweir         // TODO/LATER: is EmbeddedObjectContainer not enough?
462cdf0e10cSrcweir         // the created document will be closed by pDoc ( should use SfxObjectShellLock )
463cdf0e10cSrcweir         pPersistShell = new SwDocShell( pDoc, SFX_CREATE_MODE_INTERNAL );
464cdf0e10cSrcweir         pDoc->SetTmpDocShell( pPersistShell );
465cdf0e10cSrcweir         pPersistShell->DoInitNew( NULL );
466cdf0e10cSrcweir     }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     // Wir hauen das Ding auf SvPersist-Ebene rein
469cdf0e10cSrcweir     // TODO/LATER: check if using the same naming scheme for all apps works here
470cdf0e10cSrcweir     ::rtl::OUString aNewName/*( Sw3Io::UniqueName( p->GetStorage(), "Obj" ) )*/;
471cdf0e10cSrcweir     SfxObjectShell* pSrc = GetDoc()->GetPersist();
472cdf0e10cSrcweir 
473cdf0e10cSrcweir     pPersistShell->GetEmbeddedObjectContainer().CopyAndGetEmbeddedObject(
474cdf0e10cSrcweir         pSrc->GetEmbeddedObjectContainer(),
475cdf0e10cSrcweir         pSrc->GetEmbeddedObjectContainer().GetEmbeddedObject( aOLEObj.aName ),
476cdf0e10cSrcweir         aNewName );
477cdf0e10cSrcweir 
478cdf0e10cSrcweir     SwOLENode* pOLENd = pDoc->GetNodes().MakeOLENode( rIdx, aNewName, GetAspect(),
479cdf0e10cSrcweir                                     (SwGrfFmtColl*)pDoc->GetDfltGrfFmtColl(),
480cdf0e10cSrcweir                                     (SwAttrSet*)GetpSwAttrSet() );
481cdf0e10cSrcweir 
482cdf0e10cSrcweir     pOLENd->SetChartTblName( GetChartTblName() );
483cdf0e10cSrcweir     pOLENd->SetTitle( GetTitle() );
484cdf0e10cSrcweir     pOLENd->SetDescription( GetDescription() );
485cdf0e10cSrcweir     pOLENd->SetContour( HasContour(), HasAutomaticContour() );
486cdf0e10cSrcweir     pOLENd->SetAspect( GetAspect() ); // the replacement image must be already copied
487cdf0e10cSrcweir 
488cdf0e10cSrcweir     pOLENd->SetOLESizeInvalid( sal_True );
489cdf0e10cSrcweir     pDoc->SetOLEPrtNotifyPending();
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     return pOLENd;
492cdf0e10cSrcweir }
493cdf0e10cSrcweir 
IsInGlobalDocSection() const494cdf0e10cSrcweir sal_Bool SwOLENode::IsInGlobalDocSection() const
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     // suche den "Body Anchor"
497cdf0e10cSrcweir     sal_uLong nEndExtraIdx = GetNodes().GetEndOfExtras().GetIndex();
498cdf0e10cSrcweir     const SwNode* pAnchorNd = this;
499cdf0e10cSrcweir     do {
500cdf0e10cSrcweir         SwFrmFmt* pFlyFmt = pAnchorNd->GetFlyFmt();
501cdf0e10cSrcweir         if( !pFlyFmt )
502cdf0e10cSrcweir             return sal_False;
503cdf0e10cSrcweir 
504cdf0e10cSrcweir         const SwFmtAnchor& rAnchor = pFlyFmt->GetAnchor();
505cdf0e10cSrcweir         if( !rAnchor.GetCntntAnchor() )
506cdf0e10cSrcweir             return sal_False;
507cdf0e10cSrcweir 
508cdf0e10cSrcweir         pAnchorNd = &rAnchor.GetCntntAnchor()->nNode.GetNode();
509cdf0e10cSrcweir     } while( pAnchorNd->GetIndex() < nEndExtraIdx );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     const SwSectionNode* pSectNd = pAnchorNd->FindSectionNode();
512cdf0e10cSrcweir     if( !pSectNd )
513cdf0e10cSrcweir         return sal_False;
514cdf0e10cSrcweir 
515cdf0e10cSrcweir     while( pSectNd )
516cdf0e10cSrcweir     {
517cdf0e10cSrcweir         pAnchorNd = pSectNd;
518cdf0e10cSrcweir         pSectNd = pAnchorNd->StartOfSectionNode()->FindSectionNode();
519cdf0e10cSrcweir     }
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     // in pAnchorNd steht der zuletzt gefundene Section Node. Der muss
522cdf0e10cSrcweir     // jetzt die Bedingung fuers GlobalDoc erfuellen.
523cdf0e10cSrcweir     pSectNd = (SwSectionNode*)pAnchorNd;
524cdf0e10cSrcweir     return FILE_LINK_SECTION == pSectNd->GetSection().GetType() &&
525cdf0e10cSrcweir             pSectNd->GetIndex() > nEndExtraIdx;
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
IsOLEObjectDeleted() const528cdf0e10cSrcweir sal_Bool SwOLENode::IsOLEObjectDeleted() const
529cdf0e10cSrcweir {
530cdf0e10cSrcweir     sal_Bool bRet = sal_False;
531cdf0e10cSrcweir     if( aOLEObj.xOLERef.is() )
532cdf0e10cSrcweir     {
533cdf0e10cSrcweir         SfxObjectShell* p = GetDoc()->GetPersist();
534cdf0e10cSrcweir         if( p )     // muss da sein
535cdf0e10cSrcweir         {
536cdf0e10cSrcweir             return !p->GetEmbeddedObjectContainer().HasEmbeddedObject( aOLEObj.aName );
537cdf0e10cSrcweir             //SvInfoObjectRef aRef( p->Find( aOLEObj.aName ) );
538cdf0e10cSrcweir             //if( aRef.Is() )
539cdf0e10cSrcweir             //    bRet = aRef->IsDeleted();
540cdf0e10cSrcweir         }
541cdf0e10cSrcweir     }
542cdf0e10cSrcweir     return bRet;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir 
GetNewReplacement()545cdf0e10cSrcweir void SwOLENode::GetNewReplacement()
546cdf0e10cSrcweir {
547cdf0e10cSrcweir     if ( aOLEObj.xOLERef.is() )
548cdf0e10cSrcweir         aOLEObj.xOLERef.UpdateReplacement();
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
UpdateLinkURL_Impl()551cdf0e10cSrcweir sal_Bool SwOLENode::UpdateLinkURL_Impl()
552cdf0e10cSrcweir {
553cdf0e10cSrcweir     sal_Bool bResult = sal_False;
554cdf0e10cSrcweir 
555cdf0e10cSrcweir     if ( mpObjectLink )
556cdf0e10cSrcweir     {
557cdf0e10cSrcweir         String aNewLinkURL;
558cdf0e10cSrcweir         GetDoc()->GetLinkManager().GetDisplayNames( mpObjectLink, 0, &aNewLinkURL, 0, 0 );
559cdf0e10cSrcweir         if ( !aNewLinkURL.EqualsIgnoreCaseAscii( maLinkURL ) )
560cdf0e10cSrcweir         {
561cdf0e10cSrcweir             if ( !aOLEObj.xOLERef.is() )
562cdf0e10cSrcweir                 aOLEObj.GetOleRef();
563cdf0e10cSrcweir 
564cdf0e10cSrcweir             uno::Reference< embed::XEmbeddedObject > xObj = aOLEObj.xOLERef.GetObject();
565cdf0e10cSrcweir             uno::Reference< embed::XCommonEmbedPersist > xPersObj( xObj, uno::UNO_QUERY );
566cdf0e10cSrcweir             OSL_ENSURE( xPersObj.is(), "The object must exist!\n" );
567cdf0e10cSrcweir             if ( xPersObj.is() )
568cdf0e10cSrcweir             {
569cdf0e10cSrcweir                 try
570cdf0e10cSrcweir                 {
571cdf0e10cSrcweir                     sal_Int32 nCurState = xObj->getCurrentState();
572cdf0e10cSrcweir                     if ( nCurState != embed::EmbedStates::LOADED )
573cdf0e10cSrcweir                         xObj->changeState( embed::EmbedStates::LOADED );
574cdf0e10cSrcweir 
575cdf0e10cSrcweir                     // TODO/LATER: there should be possible to get current mediadescriptor settings from the object
576cdf0e10cSrcweir                     uno::Sequence< beans::PropertyValue > aArgs( 1 );
577cdf0e10cSrcweir                     aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
578cdf0e10cSrcweir                     aArgs[0].Value <<= ::rtl::OUString( aNewLinkURL );
579cdf0e10cSrcweir                     xPersObj->reload( aArgs, uno::Sequence< beans::PropertyValue >() );
580cdf0e10cSrcweir 
581cdf0e10cSrcweir                     maLinkURL = aNewLinkURL;
582cdf0e10cSrcweir                     bResult = sal_True;
583cdf0e10cSrcweir 
584cdf0e10cSrcweir                     if ( nCurState != embed::EmbedStates::LOADED )
585cdf0e10cSrcweir                         xObj->changeState( nCurState );
586cdf0e10cSrcweir                 }
587cdf0e10cSrcweir                 catch( uno::Exception& )
588cdf0e10cSrcweir                 {}
589cdf0e10cSrcweir             }
590cdf0e10cSrcweir 
591cdf0e10cSrcweir             if ( !bResult )
592cdf0e10cSrcweir             {
593cdf0e10cSrcweir                 // TODO/LATER: return the old name to the link manager, is it possible?
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir     }
597cdf0e10cSrcweir 
598cdf0e10cSrcweir     return bResult;
599cdf0e10cSrcweir }
600cdf0e10cSrcweir 
BreakFileLink_Impl()601cdf0e10cSrcweir void SwOLENode::BreakFileLink_Impl()
602cdf0e10cSrcweir {
603cdf0e10cSrcweir     SfxObjectShell* pPers = GetDoc()->GetPersist();
604cdf0e10cSrcweir 
605cdf0e10cSrcweir     if ( pPers )
606cdf0e10cSrcweir     {
607cdf0e10cSrcweir         uno::Reference< embed::XStorage > xStorage = pPers->GetStorage();
608cdf0e10cSrcweir         if ( xStorage.is() )
609cdf0e10cSrcweir         {
610cdf0e10cSrcweir             try
611cdf0e10cSrcweir             {
612cdf0e10cSrcweir                 uno::Reference< embed::XLinkageSupport > xLinkSupport( aOLEObj.GetOleRef(), uno::UNO_QUERY_THROW );
613cdf0e10cSrcweir                 xLinkSupport->breakLink( xStorage, aOLEObj.GetCurrentPersistName() );
614cdf0e10cSrcweir                 DisconnectFileLink_Impl();
615cdf0e10cSrcweir                 maLinkURL = String();
616cdf0e10cSrcweir             }
617cdf0e10cSrcweir             catch( uno::Exception& )
618cdf0e10cSrcweir             {
619cdf0e10cSrcweir             }
620cdf0e10cSrcweir         }
621cdf0e10cSrcweir     }
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
DisconnectFileLink_Impl()624cdf0e10cSrcweir void SwOLENode::DisconnectFileLink_Impl()
625cdf0e10cSrcweir {
626cdf0e10cSrcweir     if ( mpObjectLink )
627cdf0e10cSrcweir     {
628cdf0e10cSrcweir         GetDoc()->GetLinkManager().Remove( mpObjectLink );
629cdf0e10cSrcweir         mpObjectLink = NULL;
630cdf0e10cSrcweir     }
631cdf0e10cSrcweir }
632cdf0e10cSrcweir 
CheckFileLink_Impl()633cdf0e10cSrcweir void SwOLENode::CheckFileLink_Impl()
634cdf0e10cSrcweir {
635cdf0e10cSrcweir     if ( aOLEObj.xOLERef.GetObject().is() && !mpObjectLink )
636cdf0e10cSrcweir     {
637cdf0e10cSrcweir         try
638cdf0e10cSrcweir         {
639cdf0e10cSrcweir             uno::Reference< embed::XLinkageSupport > xLinkSupport( aOLEObj.xOLERef.GetObject(), uno::UNO_QUERY_THROW );
640cdf0e10cSrcweir             if ( xLinkSupport->isLink() )
641cdf0e10cSrcweir             {
642cdf0e10cSrcweir                 String aLinkURL = xLinkSupport->getLinkURL();
643cdf0e10cSrcweir                 if ( aLinkURL.Len() )
644cdf0e10cSrcweir                 {
645cdf0e10cSrcweir                     // this is a file link so the model link manager should handle it
646cdf0e10cSrcweir                     mpObjectLink = new SwEmbedObjectLink( this );
647cdf0e10cSrcweir                     maLinkURL = aLinkURL;
648cdf0e10cSrcweir                     GetDoc()->GetLinkManager().InsertFileLink( *mpObjectLink, OBJECT_CLIENT_OLE, aLinkURL, NULL, NULL );
649cdf0e10cSrcweir                     mpObjectLink->Connect();
650cdf0e10cSrcweir                 }
651cdf0e10cSrcweir             }
652cdf0e10cSrcweir         }
653cdf0e10cSrcweir         catch( uno::Exception& )
654cdf0e10cSrcweir         {
655cdf0e10cSrcweir         }
656cdf0e10cSrcweir     }
657cdf0e10cSrcweir }
658cdf0e10cSrcweir 
659cdf0e10cSrcweir // --> OD 2009-03-05 #i99665#
IsChart() const660cdf0e10cSrcweir bool SwOLENode::IsChart() const
661cdf0e10cSrcweir {
662cdf0e10cSrcweir     bool bIsChart( false );
663cdf0e10cSrcweir 
664cdf0e10cSrcweir     const uno::Reference< embed::XEmbeddedObject > xEmbObj =
665cdf0e10cSrcweir                             const_cast<SwOLEObj&>(GetOLEObj()).GetOleRef();
666cdf0e10cSrcweir     if ( xEmbObj.is() )
667cdf0e10cSrcweir     {
668cdf0e10cSrcweir         SvGlobalName aClassID( xEmbObj->getClassID() );
669cdf0e10cSrcweir         bIsChart = SotExchange::IsChart( aClassID );
670cdf0e10cSrcweir     }
671cdf0e10cSrcweir 
672cdf0e10cSrcweir     return bIsChart;
673cdf0e10cSrcweir }
674cdf0e10cSrcweir // <--
675cdf0e10cSrcweir 
SwOLEObj(const svt::EmbeddedObjectRef & xObj)676cdf0e10cSrcweir SwOLEObj::SwOLEObj( const svt::EmbeddedObjectRef& xObj ) :
677cdf0e10cSrcweir     pOLENd( 0 ),
678cdf0e10cSrcweir     pListener( 0 ),
679cdf0e10cSrcweir     xOLERef( xObj )
680cdf0e10cSrcweir {
681cdf0e10cSrcweir     xOLERef.Lock( sal_True );
682cdf0e10cSrcweir     if ( xObj.is() )
683cdf0e10cSrcweir     {
684cdf0e10cSrcweir         pListener = new SwOLEListener_Impl( this );
685cdf0e10cSrcweir         pListener->acquire();
686cdf0e10cSrcweir         xObj->addStateChangeListener( pListener );
687cdf0e10cSrcweir     }
688cdf0e10cSrcweir }
689cdf0e10cSrcweir 
690cdf0e10cSrcweir 
SwOLEObj(const String & rString,sal_Int64 nAspect)691cdf0e10cSrcweir SwOLEObj::SwOLEObj( const String &rString, sal_Int64 nAspect ) :
692cdf0e10cSrcweir     pOLENd( 0 ),
693cdf0e10cSrcweir     pListener( 0 ),
694cdf0e10cSrcweir     aName( rString )
695cdf0e10cSrcweir {
696cdf0e10cSrcweir     xOLERef.Lock( sal_True );
697cdf0e10cSrcweir     xOLERef.SetViewAspect( nAspect );
698cdf0e10cSrcweir }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir 
~SwOLEObj()701cdf0e10cSrcweir SwOLEObj::~SwOLEObj()
702cdf0e10cSrcweir {
703cdf0e10cSrcweir     if( pListener )
704cdf0e10cSrcweir     {
705cdf0e10cSrcweir         if ( xOLERef.is() )
706cdf0e10cSrcweir             xOLERef->removeStateChangeListener( pListener );
707cdf0e10cSrcweir         pListener->Release();
708cdf0e10cSrcweir     }
709cdf0e10cSrcweir 
710cdf0e10cSrcweir     if( pOLENd && !pOLENd->GetDoc()->IsInDtor() )
711cdf0e10cSrcweir     {
712cdf0e10cSrcweir         // if the model is not currently in destruction it means that this object should be removed from the model
713cdf0e10cSrcweir         comphelper::EmbeddedObjectContainer* pCnt = xOLERef.GetContainer();
714cdf0e10cSrcweir 
715cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
716cdf0e10cSrcweir         SfxObjectShell* p = pOLENd->GetDoc()->GetPersist();
717cdf0e10cSrcweir         DBG_ASSERT( p, "No document!" );
718cdf0e10cSrcweir         if( p )
719cdf0e10cSrcweir         {
720cdf0e10cSrcweir             comphelper::EmbeddedObjectContainer& rCnt = p->GetEmbeddedObjectContainer();
721cdf0e10cSrcweir             OSL_ENSURE( !pCnt || &rCnt == pCnt, "The helper is assigned to unexpected container!\n" );
722cdf0e10cSrcweir         }
723cdf0e10cSrcweir #endif
724cdf0e10cSrcweir 
725cdf0e10cSrcweir         if ( pCnt && pCnt->HasEmbeddedObject( aName ) )
726cdf0e10cSrcweir         {
727cdf0e10cSrcweir             uno::Reference < container::XChild > xChild( xOLERef.GetObject(), uno::UNO_QUERY );
728cdf0e10cSrcweir             if ( xChild.is() )
729cdf0e10cSrcweir                 xChild->setParent( 0 );
730cdf0e10cSrcweir 
731cdf0e10cSrcweir             // not already removed by deleting the object
732cdf0e10cSrcweir             xOLERef.AssignToContainer( 0, aName );
733cdf0e10cSrcweir 
734cdf0e10cSrcweir             // unlock object so that object can be closed in RemoveEmbeddedObject
735cdf0e10cSrcweir             // successful closing of the object will automatically clear the reference then
736cdf0e10cSrcweir             xOLERef.Lock(sal_False);
737cdf0e10cSrcweir 
738*2cdca4f8Smseidel             // Always remove object from container it is connected to
739cdf0e10cSrcweir             try
740cdf0e10cSrcweir             {
741cdf0e10cSrcweir                 pCnt->RemoveEmbeddedObject( aName );
742cdf0e10cSrcweir             }
743cdf0e10cSrcweir             catch ( uno::Exception& )
744cdf0e10cSrcweir             {
745cdf0e10cSrcweir             }
746cdf0e10cSrcweir         }
747cdf0e10cSrcweir 
748cdf0e10cSrcweir     }
749cdf0e10cSrcweir 
750cdf0e10cSrcweir     if ( xOLERef.is() )
751cdf0e10cSrcweir         // in case the object wasn't closed: release it
752cdf0e10cSrcweir         // in case the object was not in the container: it's still locked, try to close
753cdf0e10cSrcweir         xOLERef.Clear();
754cdf0e10cSrcweir }
755cdf0e10cSrcweir 
756cdf0e10cSrcweir 
SetNode(SwOLENode * pNode)757cdf0e10cSrcweir void SwOLEObj::SetNode( SwOLENode* pNode )
758cdf0e10cSrcweir {
759cdf0e10cSrcweir     pOLENd = pNode;
760cdf0e10cSrcweir     if ( !aName.Len() )
761cdf0e10cSrcweir     {
762cdf0e10cSrcweir         SwDoc* pDoc = pNode->GetDoc();
763cdf0e10cSrcweir 
764cdf0e10cSrcweir         // Falls bereits eine SvPersist-Instanz existiert, nehmen wir diese
765cdf0e10cSrcweir         SfxObjectShell* p = pDoc->GetPersist();
766cdf0e10cSrcweir         if( !p )
767cdf0e10cSrcweir         {
768cdf0e10cSrcweir             // TODO/LATER: reicht hier nicht ein EmbeddedObjectContainer? Was passiert mit
769cdf0e10cSrcweir             // diesem Dokument?
770870262e3SDon Lewis             ASSERT( sal_False, "why is a DocShell created here?" );
771cdf0e10cSrcweir             p = new SwDocShell( pDoc, SFX_CREATE_MODE_INTERNAL );
772cdf0e10cSrcweir             p->DoInitNew( NULL );
773cdf0e10cSrcweir         }
774cdf0e10cSrcweir 
775cdf0e10cSrcweir         ::rtl::OUString aObjName;
776cdf0e10cSrcweir         uno::Reference < container::XChild > xChild( xOLERef.GetObject(), uno::UNO_QUERY );
777cdf0e10cSrcweir         if ( xChild.is() && xChild->getParent() != p->GetModel() )
778cdf0e10cSrcweir             // it is possible that the parent was set already
779cdf0e10cSrcweir             xChild->setParent( p->GetModel() );
780cdf0e10cSrcweir         if (!p->GetEmbeddedObjectContainer().InsertEmbeddedObject( xOLERef.GetObject(), aObjName ) )
781cdf0e10cSrcweir         {
782cdf0e10cSrcweir             DBG_ERROR( "InsertObject failed" );
783cdf0e10cSrcweir         if ( xChild.is() )
784cdf0e10cSrcweir             xChild->setParent( 0 );
785cdf0e10cSrcweir         }
786cdf0e10cSrcweir         else
787cdf0e10cSrcweir             xOLERef.AssignToContainer( &p->GetEmbeddedObjectContainer(), aObjName );
788cdf0e10cSrcweir 
789cdf0e10cSrcweir         ( (SwOLENode*)pOLENd )->CheckFileLink_Impl(); // for this notification nonconst access is required
790cdf0e10cSrcweir 
791cdf0e10cSrcweir         aName = aObjName;
792cdf0e10cSrcweir     }
793cdf0e10cSrcweir }
794cdf0e10cSrcweir 
GetStyleString()795ca62e2c2SSteve Yin String SwOLEObj::GetStyleString()
796ca62e2c2SSteve Yin {
797ca62e2c2SSteve Yin     String strStyle;
798ca62e2c2SSteve Yin     if (xOLERef.is() && xOLERef.IsChart())
799ca62e2c2SSteve Yin         strStyle = xOLERef.GetChartType();
800ca62e2c2SSteve Yin     return strStyle;
801ca62e2c2SSteve Yin }
IsOleRef() const802cdf0e10cSrcweir sal_Bool SwOLEObj::IsOleRef() const
803cdf0e10cSrcweir {
804cdf0e10cSrcweir     return xOLERef.is();
805cdf0e10cSrcweir }
806cdf0e10cSrcweir 
GetOleRef()807cdf0e10cSrcweir const uno::Reference < embed::XEmbeddedObject > SwOLEObj::GetOleRef()
808cdf0e10cSrcweir {
809cdf0e10cSrcweir     if( !xOLERef.is() )
810cdf0e10cSrcweir     {
811cdf0e10cSrcweir         SfxObjectShell* p = pOLENd->GetDoc()->GetPersist();
812cdf0e10cSrcweir         ASSERT( p, "kein SvPersist vorhanden" );
813cdf0e10cSrcweir 
814cdf0e10cSrcweir         uno::Reference < embed::XEmbeddedObject > xObj = p->GetEmbeddedObjectContainer().GetEmbeddedObject( aName );
815cdf0e10cSrcweir         ASSERT( !xOLERef.is(), "rekursiver Aufruf von GetOleRef() ist nicht erlaubt" )
816cdf0e10cSrcweir 
817cdf0e10cSrcweir         if ( !xObj.is() )
818cdf0e10cSrcweir         {
819cdf0e10cSrcweir             //Das Teil konnte nicht geladen werden (wahrsch. Kaputt).
820cdf0e10cSrcweir             Rectangle aArea;
821cdf0e10cSrcweir             SwFrm *pFrm = pOLENd->getLayoutFrm(0);
822cdf0e10cSrcweir             if ( pFrm )
823cdf0e10cSrcweir             {
824cdf0e10cSrcweir                 Size aSz( pFrm->Frm().SSize() );
825cdf0e10cSrcweir                 const MapMode aSrc ( MAP_TWIP );
826cdf0e10cSrcweir                 const MapMode aDest( MAP_100TH_MM );
827cdf0e10cSrcweir                 aSz = OutputDevice::LogicToLogic( aSz, aSrc, aDest );
828cdf0e10cSrcweir                 aArea.SetSize( aSz );
829cdf0e10cSrcweir             }
830cdf0e10cSrcweir             else
831cdf0e10cSrcweir                 aArea.SetSize( Size( 5000,  5000 ) );
832cdf0e10cSrcweir             // TODO/LATER: set replacement graphic for dead object
833cdf0e10cSrcweir             // It looks as if it should work even without the object, because the replace will be generated automatically
834cdf0e10cSrcweir             ::rtl::OUString aTmpName;
835cdf0e10cSrcweir             xObj = p->GetEmbeddedObjectContainer().CreateEmbeddedObject( SvGlobalName( SO3_DUMMY_CLASSID ).GetByteSequence(), aTmpName );
836cdf0e10cSrcweir         }
837cdf0e10cSrcweir         // else
838cdf0e10cSrcweir         {
839cdf0e10cSrcweir             xOLERef.Assign( xObj, xOLERef.GetViewAspect() );
840cdf0e10cSrcweir             xOLERef.AssignToContainer( &p->GetEmbeddedObjectContainer(), aName );
841cdf0e10cSrcweir             pListener = new SwOLEListener_Impl( this );
842cdf0e10cSrcweir             pListener->acquire();
843cdf0e10cSrcweir             xObj->addStateChangeListener( pListener );
844cdf0e10cSrcweir         }
845cdf0e10cSrcweir 
846cdf0e10cSrcweir         ( (SwOLENode*)pOLENd )->CheckFileLink_Impl(); // for this notification nonconst access is required
847cdf0e10cSrcweir     }
848cdf0e10cSrcweir     else if ( xOLERef->getCurrentState() == embed::EmbedStates::RUNNING )
849cdf0e10cSrcweir     {
850cdf0e10cSrcweir         // move object to first position in cache
851cdf0e10cSrcweir         if( !pOLELRU_Cache )
852cdf0e10cSrcweir             pOLELRU_Cache = new SwOLELRUCache;
853cdf0e10cSrcweir         pOLELRU_Cache->InsertObj( *this );
854cdf0e10cSrcweir     }
855cdf0e10cSrcweir 
856cdf0e10cSrcweir     return xOLERef.GetObject();
857cdf0e10cSrcweir }
858cdf0e10cSrcweir 
GetObject()859cdf0e10cSrcweir svt::EmbeddedObjectRef& SwOLEObj::GetObject()
860cdf0e10cSrcweir {
861cdf0e10cSrcweir     GetOleRef();
862cdf0e10cSrcweir     return xOLERef;
863cdf0e10cSrcweir }
864cdf0e10cSrcweir 
UnloadObject()865cdf0e10cSrcweir sal_Bool SwOLEObj::UnloadObject()
866cdf0e10cSrcweir {
867cdf0e10cSrcweir     sal_Bool bRet = sal_True;
868cdf0e10cSrcweir     //Nicht notwendig im Doc DTor (MM)
869cdf0e10cSrcweir     //ASSERT( pOLERef && pOLERef->Is() && 1 < (*pOLERef)->GetRefCount(),
870cdf0e10cSrcweir     //        "Falscher RefCount fuers Unload" );
871cdf0e10cSrcweir     if ( pOLENd )
872cdf0e10cSrcweir     {
873cdf0e10cSrcweir         const SwDoc* pDoc = pOLENd->GetDoc();
874cdf0e10cSrcweir         bRet = UnloadObject( xOLERef.GetObject(), pDoc, xOLERef.GetViewAspect() );
875cdf0e10cSrcweir     }
876cdf0e10cSrcweir 
877cdf0e10cSrcweir     return bRet;
878cdf0e10cSrcweir }
879cdf0e10cSrcweir 
UnloadObject(uno::Reference<embed::XEmbeddedObject> xObj,const SwDoc * pDoc,sal_Int64 nAspect)880cdf0e10cSrcweir sal_Bool SwOLEObj::UnloadObject( uno::Reference< embed::XEmbeddedObject > xObj, const SwDoc* pDoc, sal_Int64 nAspect )
881cdf0e10cSrcweir {
882cdf0e10cSrcweir     if ( !pDoc )
883cdf0e10cSrcweir         return sal_False;
884cdf0e10cSrcweir 
885cdf0e10cSrcweir     sal_Bool bRet = sal_True;
886cdf0e10cSrcweir     sal_Int32 nState = xObj.is() ? xObj->getCurrentState() : embed::EmbedStates::LOADED;
887cdf0e10cSrcweir     sal_Bool bIsActive = ( nState != embed::EmbedStates::LOADED && nState != embed::EmbedStates::RUNNING );
888cdf0e10cSrcweir     sal_Int64 nMiscStatus = xObj->getStatus( nAspect );
889cdf0e10cSrcweir 
890cdf0e10cSrcweir     if( nState != embed::EmbedStates::LOADED && !pDoc->IsInDtor() && !bIsActive &&
891cdf0e10cSrcweir         embed::EmbedMisc::MS_EMBED_ALWAYSRUN != ( nMiscStatus & embed::EmbedMisc::MS_EMBED_ALWAYSRUN ) &&
892cdf0e10cSrcweir         embed::EmbedMisc::EMBED_ACTIVATEIMMEDIATELY != ( nMiscStatus & embed::EmbedMisc::EMBED_ACTIVATEIMMEDIATELY ) )
893cdf0e10cSrcweir     {
894cdf0e10cSrcweir         SfxObjectShell* p = pDoc->GetPersist();
895cdf0e10cSrcweir         if( p )
896cdf0e10cSrcweir         {
897cdf0e10cSrcweir             if( pDoc->get(IDocumentSettingAccess::PURGE_OLE) )
898cdf0e10cSrcweir             {
899cdf0e10cSrcweir                 try
900cdf0e10cSrcweir                 {
901cdf0e10cSrcweir                     uno::Reference < util::XModifiable > xMod( xObj->getComponent(), uno::UNO_QUERY );
902cdf0e10cSrcweir                     if( xMod.is() && xMod->isModified() )
903cdf0e10cSrcweir                     {
904cdf0e10cSrcweir                         uno::Reference < embed::XEmbedPersist > xPers( xObj, uno::UNO_QUERY );
905cdf0e10cSrcweir                         if ( xPers.is() )
906cdf0e10cSrcweir                             xPers->storeOwn();
907cdf0e10cSrcweir                         else {
90886e1cf34SPedro Giffuni                             DBG_ERROR("Modified object without persistence in cache!");
909cdf0e10cSrcweir                         }
910cdf0e10cSrcweir                     }
911cdf0e10cSrcweir 
912cdf0e10cSrcweir                     // setting object to loaded state will remove it from cache
913cdf0e10cSrcweir                     xObj->changeState( embed::EmbedStates::LOADED );
914cdf0e10cSrcweir                 }
915cdf0e10cSrcweir                 catch ( uno::Exception& )
916cdf0e10cSrcweir                 {
917cdf0e10cSrcweir                     bRet = sal_False;
918cdf0e10cSrcweir                 }
919cdf0e10cSrcweir             }
920cdf0e10cSrcweir             else
921cdf0e10cSrcweir                 bRet = sal_False;
922cdf0e10cSrcweir         }
923cdf0e10cSrcweir     }
924cdf0e10cSrcweir 
925cdf0e10cSrcweir     return bRet;
926cdf0e10cSrcweir }
927cdf0e10cSrcweir 
GetDescription()928cdf0e10cSrcweir String SwOLEObj::GetDescription()
929cdf0e10cSrcweir {
930cdf0e10cSrcweir     String aResult;
931cdf0e10cSrcweir     uno::Reference< embed::XEmbeddedObject > xEmbObj = GetOleRef();
932cdf0e10cSrcweir     if ( xEmbObj.is() )
933cdf0e10cSrcweir     {
934cdf0e10cSrcweir         SvGlobalName aClassID( xEmbObj->getClassID() );
935cdf0e10cSrcweir         if ( SotExchange::IsMath( aClassID ) )
936cdf0e10cSrcweir             aResult = SW_RES(STR_MATH_FORMULA);
937cdf0e10cSrcweir         else if ( SotExchange::IsChart( aClassID ) )
938cdf0e10cSrcweir             aResult = SW_RES(STR_CHART);
939cdf0e10cSrcweir         else
940cdf0e10cSrcweir             aResult = SW_RES(STR_OLE);
941cdf0e10cSrcweir     }
942cdf0e10cSrcweir 
943cdf0e10cSrcweir     return aResult;
944cdf0e10cSrcweir }
945cdf0e10cSrcweir 
946cdf0e10cSrcweir 
SwOLELRUCache()947cdf0e10cSrcweir SwOLELRUCache::SwOLELRUCache()
948cdf0e10cSrcweir     : SvPtrarr( 64, 16 ),
949cdf0e10cSrcweir     utl::ConfigItem( OUString::createFromAscii( "Office.Common/Cache" )),
950cdf0e10cSrcweir     nLRU_InitSize( 20 ),
951cdf0e10cSrcweir     bInUnload( sal_False )
952cdf0e10cSrcweir {
953cdf0e10cSrcweir     EnableNotification( GetPropertyNames() );
954cdf0e10cSrcweir     Load();
955cdf0e10cSrcweir }
956cdf0e10cSrcweir 
GetPropertyNames()957cdf0e10cSrcweir uno::Sequence< rtl::OUString > SwOLELRUCache::GetPropertyNames()
958cdf0e10cSrcweir {
959cdf0e10cSrcweir     Sequence< OUString > aNames( 1 );
960cdf0e10cSrcweir     OUString* pNames = aNames.getArray();
961cdf0e10cSrcweir     pNames[0] = OUString::createFromAscii( "Writer/OLE_Objects" );
962cdf0e10cSrcweir     return aNames;
963cdf0e10cSrcweir }
964cdf0e10cSrcweir 
Notify(const uno::Sequence<rtl::OUString> &)965cdf0e10cSrcweir void SwOLELRUCache::Notify( const uno::Sequence< rtl::OUString>&  )
966cdf0e10cSrcweir {
967cdf0e10cSrcweir     Load();
968cdf0e10cSrcweir }
969cdf0e10cSrcweir 
Commit()970cdf0e10cSrcweir void SwOLELRUCache::Commit()
971cdf0e10cSrcweir {
972cdf0e10cSrcweir }
973cdf0e10cSrcweir 
Load()974cdf0e10cSrcweir void SwOLELRUCache::Load()
975cdf0e10cSrcweir {
976cdf0e10cSrcweir     Sequence< OUString > aNames( GetPropertyNames() );
977cdf0e10cSrcweir     Sequence< Any > aValues = GetProperties( aNames );
978cdf0e10cSrcweir     const Any* pValues = aValues.getConstArray();
979cdf0e10cSrcweir     DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
980cdf0e10cSrcweir     if( aValues.getLength() == aNames.getLength() && pValues->hasValue() )
981cdf0e10cSrcweir     {
982cdf0e10cSrcweir         sal_Int32 nVal = 0;
983cdf0e10cSrcweir         *pValues >>= nVal;
984cdf0e10cSrcweir         //if( 20 > nVal )
985cdf0e10cSrcweir         //    nVal = 20;
986cdf0e10cSrcweir 
987cdf0e10cSrcweir         {
988cdf0e10cSrcweir             if( nVal < nLRU_InitSize )
989cdf0e10cSrcweir             {
990cdf0e10cSrcweir                 // size of cache has been changed
991cdf0e10cSrcweir                 sal_uInt16 nCount = SvPtrarr::Count();
992cdf0e10cSrcweir                 sal_uInt16 nPos = nCount;
993cdf0e10cSrcweir 
994cdf0e10cSrcweir                 // try to remove the last entries until new maximum size is reached
995cdf0e10cSrcweir                 while( nCount > nVal )
996cdf0e10cSrcweir                 {
997cdf0e10cSrcweir                     SwOLEObj* pObj = (SwOLEObj*) SvPtrarr::GetObject( --nPos );
998cdf0e10cSrcweir                     if ( pObj->UnloadObject() )
999cdf0e10cSrcweir                         nCount--;
1000cdf0e10cSrcweir                     if ( !nPos )
1001cdf0e10cSrcweir                         break;
1002cdf0e10cSrcweir                 }
1003cdf0e10cSrcweir             }
1004cdf0e10cSrcweir         }
1005cdf0e10cSrcweir 
1006cdf0e10cSrcweir         nLRU_InitSize = (sal_uInt16)nVal;
1007cdf0e10cSrcweir     }
1008cdf0e10cSrcweir }
1009cdf0e10cSrcweir 
InsertObj(SwOLEObj & rObj)1010cdf0e10cSrcweir void SwOLELRUCache::InsertObj( SwOLEObj& rObj )
1011cdf0e10cSrcweir {
1012cdf0e10cSrcweir     SwOLEObj* pObj = &rObj;
1013cdf0e10cSrcweir     sal_uInt16 nPos = SvPtrarr::GetPos( pObj );
1014cdf0e10cSrcweir     if( nPos )
1015cdf0e10cSrcweir     {
1016cdf0e10cSrcweir         // object is currently not the first in cache
1017cdf0e10cSrcweir         if( USHRT_MAX != nPos )
1018cdf0e10cSrcweir             SvPtrarr::Remove( nPos );
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir         SvPtrarr::Insert( pObj, 0 );
1021cdf0e10cSrcweir 
1022cdf0e10cSrcweir         // try to remove objects if necessary (of course not the freshly inserted one at nPos=0)
1023cdf0e10cSrcweir         sal_uInt16 nCount = SvPtrarr::Count();
1024cdf0e10cSrcweir         nPos = nCount-1;
1025cdf0e10cSrcweir         while( nPos && nCount > nLRU_InitSize )
1026cdf0e10cSrcweir         {
1027cdf0e10cSrcweir             pObj = (SwOLEObj*) SvPtrarr::GetObject( nPos-- );
1028cdf0e10cSrcweir             if ( pObj->UnloadObject() )
1029cdf0e10cSrcweir                 nCount--;
1030cdf0e10cSrcweir         }
1031cdf0e10cSrcweir     }
1032cdf0e10cSrcweir }
1033cdf0e10cSrcweir 
RemoveObj(SwOLEObj & rObj)1034cdf0e10cSrcweir void SwOLELRUCache::RemoveObj( SwOLEObj& rObj )
1035cdf0e10cSrcweir {
1036cdf0e10cSrcweir     sal_uInt16 nPos = SvPtrarr::GetPos( &rObj );
1037cdf0e10cSrcweir     if ( nPos != 0xFFFF )
1038cdf0e10cSrcweir         SvPtrarr::Remove( nPos );
1039cdf0e10cSrcweir     if( !Count() )
1040cdf0e10cSrcweir         DELETEZ( pOLELRU_Cache );
1041cdf0e10cSrcweir }
1042cdf0e10cSrcweir 
1043*2cdca4f8Smseidel /* vim: set noet sw=4 ts=4: */
1044