merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / backend / backendnotifier.cxx
blob2deb00738253e2946aa166f38ecafb13af1362d9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: backendnotifier.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "backendnotifier.hxx"
36 #ifndef INCLUDED_ALGORITHM
37 #include <algorithm>
38 #define INCLUDED_ALGORITHM
39 #endif
40 #include "configpath.hxx"
42 namespace configmgr
44 // ---------------------------------------------------------------------------
45 namespace backend
49 //----------------------------------------------------------------------------
50 BackendChangeNotifier::BackendChangeNotifier(const uno::Reference<backenduno::XBackend>& _xBackend)
51 : m_aMutex()
52 ,m_aListeners()
53 ,m_aBackend(_xBackend, uno::UNO_QUERY)
57 // ---------------------------------------------------------------------------
59 BackendChangeNotifier::~BackendChangeNotifier()
61 m_aListeners.clear();
63 // ---------------------------------------------------------------------------
64 void SAL_CALL BackendChangeNotifier::componentDataChanged(const backenduno::ComponentChangeEvent& _aEvent)
65 throw (::com::sun::star::uno::RuntimeException)
67 rtl::OUString aComponentName = _aEvent.Component;
68 std::map<rtl::OUString, ComponentNotifier>::iterator aIter = m_aListeners.find(aComponentName);
69 if(aIter != m_aListeners.end())
71 aIter->second.notifyListeners(aComponentName);
74 // -----------------------------------------------------------------------------
75 void SAL_CALL BackendChangeNotifier::disposing( lang::EventObject const & /*rSource*/ )
76 throw (uno::RuntimeException)
78 osl::MutexGuard aListGuard(m_aMutex);
79 if (m_aBackend.is())
81 m_aBackend.clear();
83 m_aListeners.clear();
85 // -----------------------------------------------------------------------------
86 void BackendChangeNotifier::addListener(INodeDataListener * _xListener, const ComponentRequest& _aRequest) SAL_THROW(())
88 osl::MutexGuard aListGuard(m_aMutex);
90 OSL_PRECOND(_xListener, "ERROR: trying to register a NULL listener");
91 ComponentListener aComponentListener(_xListener, _aRequest.getOptions());
93 const rtl::OUString aComponentName = _aRequest.getComponentName();
95 //Check if we have a Listener registered for that Component
96 std::map<rtl::OUString, ComponentNotifier>::iterator aIter;
97 aIter = m_aListeners.find(aComponentName);
98 if (aIter == m_aListeners.end())
100 ComponentNotifier aComponentNotifier;
101 aComponentNotifier.addListenerToList(aComponentListener);
102 m_aListeners[aComponentName] = aComponentNotifier;
104 //Now need to register Listener with MultiStratumBackend for that Component
105 if (m_aBackend.is())
107 m_aBackend->addChangesListener(this, aComponentName);
110 else
112 //Add Listener and Option to ComponentNotifier list
113 aIter->second.addListenerToList(aComponentListener);
118 // ---------------------------------------------------------------------------
120 void BackendChangeNotifier::removeListener(INodeDataListener * _xListener, const ComponentRequest& _aRequest) SAL_THROW(())
122 osl::MutexGuard aListGuard(m_aMutex);
123 OSL_PRECOND(!m_aListeners.empty(),
124 "BackendChangeNotifier:Cannot Remove Listener, no Listeners Registered");
125 OSL_PRECOND(_xListener, "ERROR: trying to remove a NULL listener");
127 std::map<rtl::OUString, ComponentNotifier>::iterator aIter;
128 rtl::OUString aComponentName = _aRequest.getComponentName();
130 aIter = m_aListeners.find(aComponentName);
131 if (aIter == m_aListeners.end())
133 OSL_TRACE("BackendChangeNotifier: removeListener: no listener registered for component %s",
134 aComponentName.getStr());
136 else
138 ComponentListener aComponentListener(_xListener, _aRequest.getOptions());
139 aIter->second.removeListenerFromList(aComponentListener);
140 if (aIter->second.isListEmpty())
142 m_aListeners.erase(aIter);
143 //Need all to de-register listeners from the lower layers
144 if (m_aBackend.is())
146 m_aBackend->removeChangesListener(this, aComponentName);
152 // ---------------------------------------------------------------------------
153 ComponentNotifier::ComponentNotifier():m_aListenerList()
156 // ---------------------------------------------------------------------------
159 void ComponentNotifier::addListenerToList(const ComponentListener& _aListener)
161 m_aListenerList.push_back(_aListener);
162 //REM TO REMOVE
163 ComponentListener aListener = m_aListenerList.front();
166 // ---------------------------------------------------------------------------
167 void ComponentNotifier::removeListenerFromList(const ComponentListener& _aListener)
169 OSL_PRECOND(!m_aListenerList.empty(),
170 "ComponentNotifier:Cannot Remove Listener, no Listeners Registered");
172 std::list<ComponentListener>::iterator aIter;
174 //Remove first instance that matches in the list
175 aIter = std::find(m_aListenerList.begin(), m_aListenerList.end(),_aListener);
176 if(aIter != m_aListenerList.end())
178 m_aListenerList.erase(aIter);
181 // ---------------------------------------------------------------------------
182 void ComponentNotifier::notifyListeners(const rtl::OUString& _aComponent)
185 for( std::list<ComponentListener>::iterator aIter = m_aListenerList.begin();
186 aIter != m_aListenerList.end(); aIter++)
188 ComponentRequest aRequest(_aComponent, (*aIter).m_aOptions);
190 (*aIter).m_aListener->dataChanged(aRequest);
194 // ---------------------------------------------------------------------------
195 } // namespace backend
197 // ---------------------------------------------------------------------------
198 } // namespace configmgr