LibreOffice Module sc (master)  1
addinlis.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sfx2/objsh.hxx>
21 #include <vcl/svapp.hxx>
22 
23 #include <addinlis.hxx>
24 #include <miscuno.hxx>
25 #include <document.hxx>
26 #include <brdcst.hxx>
27 
28 #include <com/sun/star/sheet/XVolatileResult.hpp>
29 
30 using namespace com::sun::star;
31 
32 SC_SIMPLE_SERVICE_INFO( ScAddInListener, "ScAddInListener", "stardiv.one.sheet.AddInListener" )
33 
34 ::std::vector<rtl::Reference<ScAddInListener>> ScAddInListener::aAllListeners;
35 
37  const uno::Reference<sheet::XVolatileResult>& xVR, ScDocument* pDoc )
38 {
39  rtl::Reference<ScAddInListener> xNew = new ScAddInListener( xVR, pDoc );
40 
41  aAllListeners.push_back( xNew );
42 
43  if ( xVR.is() )
44  xVR->addResultListener( xNew ); // after at least 1 ref exists!
45 
46  return xNew.get();
47 }
48 
49 ScAddInListener::ScAddInListener( uno::Reference<sheet::XVolatileResult> const & xVR, ScDocument* pDoc ) :
50  xVolRes( xVR ),
51  pDocs( new ScAddInDocs )
52 {
53  pDocs->insert( pDoc );
54 }
55 
57 {
58 }
59 
60 ScAddInListener* ScAddInListener::Get( const uno::Reference<sheet::XVolatileResult>& xVR )
61 {
62  ScAddInListener* pLst = nullptr;
63  sheet::XVolatileResult* pComp = xVR.get();
64 
65  for (auto const& listener : aAllListeners)
66  {
67  if ( pComp == listener->xVolRes.get() )
68  {
69  pLst = listener.get();
70  break;
71  }
72  }
73  return pLst;
74 }
75 
76 //TODO: move to some container object?
78 {
79  auto iter = aAllListeners.begin();
80  while(iter != aAllListeners.end())
81  {
82  ScAddInDocs* p = (*iter)->pDocs.get();
83  ScAddInDocs::iterator iter2 = p->find( pDocumentP );
84  if( iter2 != p->end() )
85  {
86  p->erase( iter2 );
87  if ( p->empty() )
88  {
89  if ( (*iter)->xVolRes.is() )
90  (*iter)->xVolRes->removeResultListener( *iter );
91 
92  iter = aAllListeners.erase( iter );
93  continue;
94  }
95  }
96  ++iter;
97  }
98 }
99 
100 // XResultListener
101 
102 void SAL_CALL ScAddInListener::modified( const css::sheet::ResultEvent& aEvent )
103 {
104  SolarMutexGuard aGuard; //TODO: or generate a UserEvent
105 
106  aResult = aEvent.Value; // store result
107 
108  // notify document of changes
109 
110  Broadcast( ScHint(SfxHintId::ScDataChanged, ScAddress()) );
111 
112  for (auto const& pDoc : *pDocs)
113  {
114  pDoc->TrackFormulas();
115  pDoc->GetDocumentShell()->Broadcast( SfxHint( SfxHintId::ScDataChanged ) );
116  }
117 }
118 
119 // XEventListener
120 
121 void SAL_CALL ScAddInListener::disposing( const css::lang::EventObject& /* Source */ )
122 {
123  // hold a ref so this is not deleted at removeResultListener
124  uno::Reference<sheet::XResultListener> xKeepAlive( this );
125 
126  if ( xVolRes.is() )
127  {
128  xVolRes->removeResultListener( this );
129  xVolRes = nullptr;
130  }
131 }
132 
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: addinlis.cxx:121
std::set< ScDocument * > ScAddInDocs
Definition: adiasync.hxx:33
Reference
ScAddInListener(css::uno::Reference< css::sheet::XVolatileResult > const &xVR, ScDocument *pD)
Definition: addinlis.cxx:49
void Broadcast(const SfxHint &rHint)
virtual ~ScAddInListener() override
Definition: addinlis.cxx:56
void TrackFormulas(SfxHintId nHintId=SfxHintId::ScDataChanged)
Definition: documen7.cxx:527
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
css::uno::Any aResult
Definition: addinlis.hxx:40
static::std::vector< rtl::Reference< ScAddInListener > > aAllListeners
Definition: addinlis.hxx:43
virtual void SAL_CALL modified(const css::sheet::ResultEvent &aEvent) override
Definition: addinlis.cxx:102
static void RemoveDocument(ScDocument *pDocument)
Definition: addinlis.cxx:77
void * p
std::unique_ptr< ScAddInDocs > pDocs
Definition: addinlis.hxx:41
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1056
static ScAddInListener * CreateListener(const css::uno::Reference< css::sheet::XVolatileResult > &xVR, ScDocument *pDoc)
static ScAddInListener * Get(const css::uno::Reference< css::sheet::XVolatileResult > &xVR)
Definition: addinlis.cxx:60
css::uno::Reference< css::sheet::XVolatileResult > xVolRes
Definition: addinlis.hxx:39