Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / addinlis.cxx
blobc7a213c0e5e776e000ecd8d0bebe004eb20a0291
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sfx2/objsh.hxx>
22 #include <vcl/svapp.hxx>
24 #include "addinlis.hxx"
25 #include "miscuno.hxx"
26 #include "document.hxx"
27 #include "brdcst.hxx"
28 #include "sc.hrc"
30 using namespace com::sun::star;
32 SC_SIMPLE_SERVICE_INFO( ScAddInListener, "ScAddInListener", "stardiv.one.sheet.AddInListener" )
34 ::std::list<ScAddInListener*> ScAddInListener::aAllListeners;
36 ScAddInListener* ScAddInListener::CreateListener(
37 uno::Reference<sheet::XVolatileResult> xVR, ScDocument* pDoc )
39 ScAddInListener* pNew = new ScAddInListener( xVR, pDoc );
41 pNew->acquire(); // for aAllListeners
42 aAllListeners.push_back( pNew );
44 if ( xVR.is() )
45 xVR->addResultListener( pNew ); // after at least 1 ref exists!
47 return pNew;
50 ScAddInListener::ScAddInListener( uno::Reference<sheet::XVolatileResult> xVR, ScDocument* pDoc ) :
51 xVolRes( xVR )
53 pDocs = new ScAddInDocs();
54 pDocs->insert( pDoc );
57 ScAddInListener::~ScAddInListener()
59 delete pDocs;
62 ScAddInListener* ScAddInListener::Get( uno::Reference<sheet::XVolatileResult> xVR )
64 ScAddInListener* pLst = NULL;
65 sheet::XVolatileResult* pComp = xVR.get();
67 for(::std::list<ScAddInListener*>::iterator iter = aAllListeners.begin(); iter != aAllListeners.end(); ++iter)
69 if ( pComp == (sheet::XVolatileResult*)(*iter)->xVolRes.get() )
71 pLst = *iter;
72 break;
75 return pLst;
78 //! move to some container object?
79 void ScAddInListener::RemoveDocument( ScDocument* pDocumentP )
81 ::std::list<ScAddInListener*>::iterator iter = aAllListeners.begin();
82 while(iter != aAllListeners.end())
84 ScAddInDocs* p = (*iter)->pDocs;
85 ScAddInDocs::iterator iter2 = p->find( pDocumentP );
86 if( iter2 != p->end() )
88 p->erase( iter2 );
89 if ( p->empty() )
91 if ( (*iter)->xVolRes.is() )
92 (*iter)->xVolRes->removeResultListener( *iter );
94 (*iter)->release(); // Ref for aAllListeners - pLst may be deleted here
96 // this AddIn is no longer used
97 // dont delete, just remove the ref for the list
99 iter = aAllListeners.erase( iter );
100 continue;
103 ++iter;
107 // XResultListener
109 void SAL_CALL ScAddInListener::modified( const ::com::sun::star::sheet::ResultEvent& aEvent )
110 throw(::com::sun::star::uno::RuntimeException)
112 SolarMutexGuard aGuard; //! or generate a UserEvent
114 aResult = aEvent.Value; // store result
116 // notify document of changes
118 Broadcast( ScHint(SC_HINT_DATACHANGED, ScAddress()) );
120 for ( ScAddInDocs::iterator it = pDocs->begin(); it != pDocs->end(); ++it )
122 ScDocument* pDoc = *it;
123 pDoc->TrackFormulas();
124 pDoc->GetDocumentShell()->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
128 // XEventListener
130 void SAL_CALL ScAddInListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
131 throw(::com::sun::star::uno::RuntimeException)
133 // hold a ref so this is not deleted at removeResultListener
134 uno::Reference<sheet::XResultListener> xRef( this );
136 if ( xVolRes.is() )
138 xVolRes->removeResultListener( this );
139 xVolRes = NULL;
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */