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