merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / inc / confevents.hxx
blob6b1376213a72fe5862c73f39a2accfcb0e986ddf
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: confevents.hxx,v $
10 * $Revision: 1.10 $
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 #ifndef CONFIGMGR_API_EVENTS_HXX_
32 #define CONFIGMGR_API_EVENTS_HXX_
34 #include <hash_set>
35 #include <map>
36 #include <set>
38 #include "osl/mutex.hxx"
39 #include "rtl/ref.hxx"
40 #include "salhelper/simplereferenceobject.hxx"
42 #include "configpath.hxx"
43 #include "utility.hxx"
45 namespace rtl { class OUString; }
47 namespace configmgr
49 class Change;
50 struct TreeChangeList;
51 class TreeManager;
53 namespace configuration { class AbsolutePath; }
55 struct IConfigListener : public salhelper::SimpleReferenceObject
57 virtual void disposing(TreeManager * pSource) = 0;
59 struct INodeListener : IConfigListener
61 virtual void nodeChanged(Change const& aChange, configuration::AbsolutePath const& aPath, TreeManager * pSource) = 0;
62 virtual void nodeDeleted(configuration::AbsolutePath const& aPath, TreeManager * pSource) = 0;
65 namespace internal
68 ////////////////////////////////////////////////////////////////////////
69 template <class ListenerRef>
70 class BroadcastImplHelper
72 public:
73 osl::Mutex mutex;
75 public:
76 BroadcastImplHelper()
79 ~BroadcastImplHelper()
81 OSL_ENSURE(m_aInterfaces.empty(), "Configuration Broadcaster was not disposed properly");
84 public:
85 typedef std::set<ListenerRef> Interfaces;
87 public:
88 typename Interfaces::iterator addListener(ListenerRef aListener)
90 return m_aInterfaces.insert(aListener).first;
92 void removeListener(ListenerRef aListener)
94 m_aInterfaces.erase(aListener);
97 void disposing(TreeManager * pSource);
99 public:
100 typename Interfaces::const_iterator begin() const { return m_aInterfaces.begin(); }
101 typename Interfaces::const_iterator end() const { return m_aInterfaces.end(); }
103 typename Interfaces::const_iterator find(ListenerRef aListener) const { return m_aInterfaces.find(aListener); }
104 typename Interfaces::iterator findFull(ListenerRef aListener) { return m_aInterfaces.find(aListener); }
105 private:
106 Interfaces m_aInterfaces;
108 // no implementation - not copyable
109 BroadcastImplHelper(BroadcastImplHelper&);
110 void operator=(BroadcastImplHelper&);
113 ////////////////////////////////////////////////////////////////////////
114 template <class Listener>
115 void BroadcastImplHelper<Listener>::disposing(TreeManager * pSource)
117 osl::ClearableMutexGuard aGuard(this->mutex); // ensure that no notifications are running
119 Interfaces aTargets;
120 aTargets.swap(m_aInterfaces);
122 aGuard.clear();
123 for(typename Interfaces::iterator it = aTargets.begin(); it != aTargets.end(); )
125 typename Interfaces::iterator cur = it++;
126 if (*cur)
127 (*cur)->disposing(pSource);
132 /////////////////////////////////////////////////////////////////////////
134 class NodeListenerInfo
136 public:
137 typedef std::hash_set<configuration::AbsolutePath, configuration::Path::Hash, configuration::Path::Equiv> Pathes;
139 public:
140 NodeListenerInfo(rtl::Reference<INodeListener> const& pListener)
141 : m_pListener(pListener)
145 // path handling
146 Pathes const& pathList() const { return m_aPathes; }
148 void addPath(configuration::AbsolutePath const& sPath) const { m_aPathes.insert(sPath); }
149 void removePath(configuration::AbsolutePath const& sPath) const { m_aPathes.erase(sPath); }
150 //void removeChildPathes(OUString const& sPath);
152 // behave as pointer for use as a 'reference' class
153 rtl::Reference<INodeListener> get() const { return m_pListener; }
154 rtl::Reference<INodeListener> operator->() const { return get(); }
155 INodeListener& operator*() const { return *m_pListener; }
156 // needed to allow if (info) ...
157 struct HasListener;
158 operator HasListener const*() const { return reinterpret_cast<HasListener*>(m_pListener.get()); }
160 bool operator < (NodeListenerInfo const& aInfo) const
161 { return std::less<INodeListener*>()(m_pListener.get(), aInfo.m_pListener.get()); }
163 bool operator == (NodeListenerInfo const& aInfo) const
164 { return !!( m_pListener == aInfo.m_pListener); }
166 bool operator > (NodeListenerInfo const& aInfo) const
167 { return aInfo.operator < (*this); }
168 bool operator >= (NodeListenerInfo const& aInfo) const
169 { return !operator<(aInfo); }
170 bool operator <= (NodeListenerInfo const& aInfo) const
171 { return !operator>(aInfo); }
173 bool operator != (NodeListenerInfo const& aInfo) const
174 { return !operator==(aInfo); }
176 private:
177 rtl::Reference<INodeListener> m_pListener;
178 mutable Pathes m_aPathes; // hack to be mutable even as set element
180 } // namespace
182 /////////////////////////////////////////////////////////////////////////
183 class ConfigChangeBroadcastHelper // broadcasts changes for a given set of options
185 public:
186 ConfigChangeBroadcastHelper();
187 ~ConfigChangeBroadcastHelper();
189 void broadcast(TreeChangeList const& anUpdate, sal_Bool bError, TreeManager * pSource);
191 void addListener(configuration::AbsolutePath const& aName, rtl::Reference<INodeListener> const& );
192 void removeListener(rtl::Reference<INodeListener> const&);
194 void dispose(TreeManager * pSource);
196 private:
197 void add(configuration::AbsolutePath const& aPath, rtl::Reference<INodeListener> const& pListener);
198 void remove(rtl::Reference<INodeListener> const& pListener);
200 void dispatch(Change const& rBaseChange, configuration::AbsolutePath const& sChangeLocation, sal_Bool _bError, TreeManager * pSource);
201 void dispatch(TreeChangeList const& rList_, sal_Bool _bError, TreeManager * pSource);
202 void disposing(TreeManager * pSource);
204 void dispatchInner(rtl::Reference<INodeListener> const& pTarget, configuration::AbsolutePath const& sTargetPath, Change const& rBaseChange, configuration::AbsolutePath const& sChangeLocation, sal_Bool _bError, TreeManager * pSource);
205 void dispatchOuter(rtl::Reference<INodeListener> const& pTarget, configuration::AbsolutePath const& sTargetPath, Change const& rBaseChange, configuration::AbsolutePath const& sChangeLocation, sal_Bool _bError, TreeManager * pSource);
207 typedef std::multimap<configuration::AbsolutePath, internal::BroadcastImplHelper<internal::NodeListenerInfo>::Interfaces::iterator, configuration::Path::Before> PathMap;
208 internal::BroadcastImplHelper<internal::NodeListenerInfo> m_aListeners;
209 PathMap m_aPathMap;
211 } // namespace
213 #endif // CONFIGMGR_API_EVENTS_HXX_