Line data Source code
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 <sal/config.h>
21 :
22 : #include <vcl/svapp.hxx>
23 :
24 : #include <classes/propertysethelper.hxx>
25 : #include <threadhelp/transactionguard.hxx>
26 :
27 : namespace framework{
28 :
29 0 : PropertySetHelper::PropertySetHelper( osl::Mutex & mutex,
30 : TransactionManager* pExternalTransactionManager ,
31 : bool bReleaseLockOnCall )
32 : : m_lSimpleChangeListener(mutex)
33 : , m_lVetoChangeListener (mutex)
34 : , m_bReleaseLockOnCall (bReleaseLockOnCall )
35 0 : , m_rTransactionManager (*pExternalTransactionManager )
36 : {
37 0 : }
38 :
39 0 : PropertySetHelper::~PropertySetHelper()
40 : {
41 0 : }
42 :
43 0 : void PropertySetHelper::impl_setPropertyChangeBroadcaster(const css::uno::Reference< css::uno::XInterface >& xBroadcaster)
44 : {
45 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
46 :
47 0 : SolarMutexGuard g;
48 0 : m_xBroadcaster = xBroadcaster;
49 0 : }
50 :
51 0 : void SAL_CALL PropertySetHelper::impl_addPropertyInfo(const css::beans::Property& aProperty)
52 : throw(css::beans::PropertyExistException,
53 : css::uno::Exception )
54 : {
55 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
56 :
57 0 : SolarMutexGuard g;
58 :
59 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(aProperty.Name);
60 0 : if (pIt != m_lProps.end())
61 0 : throw css::beans::PropertyExistException();
62 :
63 0 : m_lProps[aProperty.Name] = aProperty;
64 0 : }
65 :
66 0 : void SAL_CALL PropertySetHelper::impl_removePropertyInfo(const OUString& sProperty)
67 : throw(css::beans::UnknownPropertyException,
68 : css::uno::Exception )
69 : {
70 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
71 :
72 0 : SolarMutexGuard g;
73 :
74 0 : PropertySetHelper::TPropInfoHash::iterator pIt = m_lProps.find(sProperty);
75 0 : if (pIt == m_lProps.end())
76 0 : throw css::beans::UnknownPropertyException();
77 :
78 0 : m_lProps.erase(pIt);
79 0 : }
80 :
81 0 : void SAL_CALL PropertySetHelper::impl_enablePropertySet()
82 : {
83 0 : }
84 :
85 0 : void SAL_CALL PropertySetHelper::impl_disablePropertySet()
86 : {
87 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
88 :
89 0 : SolarMutexGuard g;
90 :
91 0 : css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::beans::XPropertySet* >(this), css::uno::UNO_QUERY);
92 0 : css::lang::EventObject aEvent(xThis);
93 :
94 0 : m_lSimpleChangeListener.disposeAndClear(aEvent);
95 0 : m_lVetoChangeListener.disposeAndClear(aEvent);
96 0 : m_lProps.free();
97 0 : }
98 :
99 0 : bool PropertySetHelper::impl_existsVeto(const css::beans::PropertyChangeEvent& aEvent)
100 : {
101 : /* Dont use the lock here!
102 : The used helper is threadsafe and it lives for the whole lifetime of
103 : our own object.
104 : */
105 0 : ::cppu::OInterfaceContainerHelper* pVetoListener = m_lVetoChangeListener.getContainer(aEvent.PropertyName);
106 0 : if (! pVetoListener)
107 0 : return false;
108 :
109 0 : ::cppu::OInterfaceIteratorHelper pListener(*pVetoListener);
110 0 : while (pListener.hasMoreElements())
111 : {
112 : try
113 : {
114 : css::uno::Reference< css::beans::XVetoableChangeListener > xListener(
115 0 : ((css::beans::XVetoableChangeListener*)pListener.next()),
116 0 : css::uno::UNO_QUERY_THROW);
117 0 : xListener->vetoableChange(aEvent);
118 : }
119 0 : catch(const css::uno::RuntimeException&)
120 0 : { pListener.remove(); }
121 0 : catch(const css::beans::PropertyVetoException&)
122 0 : { return true; }
123 : }
124 :
125 0 : return false;
126 : }
127 :
128 0 : void PropertySetHelper::impl_notifyChangeListener(const css::beans::PropertyChangeEvent& aEvent)
129 : {
130 : /* Dont use the lock here!
131 : The used helper is threadsafe and it lives for the whole lifetime of
132 : our own object.
133 : */
134 0 : ::cppu::OInterfaceContainerHelper* pSimpleListener = m_lSimpleChangeListener.getContainer(aEvent.PropertyName);
135 0 : if (! pSimpleListener)
136 0 : return;
137 :
138 0 : ::cppu::OInterfaceIteratorHelper pListener(*pSimpleListener);
139 0 : while (pListener.hasMoreElements())
140 : {
141 : try
142 : {
143 : css::uno::Reference< css::beans::XPropertyChangeListener > xListener(
144 0 : ((css::beans::XVetoableChangeListener*)pListener.next()),
145 0 : css::uno::UNO_QUERY_THROW);
146 0 : xListener->propertyChange(aEvent);
147 : }
148 0 : catch(const css::uno::RuntimeException&)
149 0 : { pListener.remove(); }
150 0 : }
151 : }
152 :
153 0 : css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL PropertySetHelper::getPropertySetInfo()
154 : throw(css::uno::RuntimeException, std::exception)
155 : {
156 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
157 :
158 0 : css::uno::Reference< css::beans::XPropertySetInfo > xInfo(static_cast< css::beans::XPropertySetInfo* >(this), css::uno::UNO_QUERY_THROW);
159 0 : return xInfo;
160 : }
161 :
162 0 : void SAL_CALL PropertySetHelper::setPropertyValue(const OUString& sProperty,
163 : const css::uno::Any& aValue )
164 : throw(css::beans::UnknownPropertyException,
165 : css::beans::PropertyVetoException ,
166 : css::lang::IllegalArgumentException ,
167 : css::lang::WrappedTargetException ,
168 : css::uno::RuntimeException, std::exception )
169 : {
170 : // TODO look for e.g. readonly props and reject setProp() call!
171 :
172 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
173 :
174 : // SAFE ->
175 0 : SolarMutexResettableGuard aWriteLock;
176 :
177 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
178 0 : if (pIt == m_lProps.end())
179 0 : throw css::beans::UnknownPropertyException();
180 :
181 0 : css::beans::Property aPropInfo = pIt->second;
182 :
183 0 : bool bLocked = true;
184 0 : if (m_bReleaseLockOnCall)
185 : {
186 0 : aWriteLock.clear();
187 0 : bLocked = false;
188 : // <- SAFE
189 : }
190 :
191 0 : css::uno::Any aCurrentValue = impl_getPropertyValue(aPropInfo.Name, aPropInfo.Handle);
192 :
193 0 : if (! bLocked)
194 : {
195 : // SAFE ->
196 0 : aWriteLock.reset();
197 0 : bLocked = true;
198 : }
199 :
200 0 : bool bWillBeChanged = (aCurrentValue != aValue);
201 0 : if (! bWillBeChanged)
202 0 : return;
203 :
204 0 : css::beans::PropertyChangeEvent aEvent;
205 0 : aEvent.PropertyName = aPropInfo.Name;
206 0 : aEvent.Further = sal_False;
207 0 : aEvent.PropertyHandle = aPropInfo.Handle;
208 0 : aEvent.OldValue = aCurrentValue;
209 0 : aEvent.NewValue = aValue;
210 0 : aEvent.Source = css::uno::Reference< css::uno::XInterface >(m_xBroadcaster.get(), css::uno::UNO_QUERY);
211 :
212 0 : if (m_bReleaseLockOnCall)
213 : {
214 0 : aWriteLock.clear();
215 0 : bLocked = false;
216 : // <- SAFE
217 : }
218 :
219 0 : if (impl_existsVeto(aEvent))
220 0 : throw css::beans::PropertyVetoException();
221 :
222 0 : impl_setPropertyValue(aPropInfo.Name, aPropInfo.Handle, aValue);
223 :
224 0 : impl_notifyChangeListener(aEvent);
225 : }
226 :
227 0 : css::uno::Any SAL_CALL PropertySetHelper::getPropertyValue(const OUString& sProperty)
228 : throw(css::beans::UnknownPropertyException,
229 : css::lang::WrappedTargetException ,
230 : css::uno::RuntimeException, std::exception )
231 : {
232 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
233 :
234 : // SAFE ->
235 0 : SolarMutexClearableGuard aReadLock;
236 :
237 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
238 0 : if (pIt == m_lProps.end())
239 0 : throw css::beans::UnknownPropertyException();
240 :
241 0 : css::beans::Property aPropInfo = pIt->second;
242 :
243 0 : if (m_bReleaseLockOnCall)
244 0 : aReadLock.clear();
245 :
246 0 : return impl_getPropertyValue(aPropInfo.Name, aPropInfo.Handle);
247 : }
248 :
249 0 : void SAL_CALL PropertySetHelper::addPropertyChangeListener(const OUString& sProperty,
250 : const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener)
251 : throw(css::beans::UnknownPropertyException,
252 : css::lang::WrappedTargetException ,
253 : css::uno::RuntimeException, std::exception )
254 : {
255 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
256 :
257 : // SAFE ->
258 0 : SolarMutexClearableGuard aReadLock;
259 :
260 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
261 0 : if (pIt == m_lProps.end())
262 0 : throw css::beans::UnknownPropertyException();
263 :
264 0 : aReadLock.clear();
265 : // <- SAFE
266 :
267 0 : m_lSimpleChangeListener.addInterface(sProperty, xListener);
268 0 : }
269 :
270 0 : void SAL_CALL PropertySetHelper::removePropertyChangeListener(const OUString& sProperty,
271 : const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener)
272 : throw(css::beans::UnknownPropertyException,
273 : css::lang::WrappedTargetException ,
274 : css::uno::RuntimeException, std::exception )
275 : {
276 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
277 :
278 : // SAFE ->
279 0 : SolarMutexClearableGuard aReadLock;
280 :
281 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
282 0 : if (pIt == m_lProps.end())
283 0 : throw css::beans::UnknownPropertyException();
284 :
285 0 : aReadLock.clear();
286 : // <- SAFE
287 :
288 0 : m_lSimpleChangeListener.removeInterface(sProperty, xListener);
289 0 : }
290 :
291 0 : void SAL_CALL PropertySetHelper::addVetoableChangeListener(const OUString& sProperty,
292 : const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener)
293 : throw(css::beans::UnknownPropertyException,
294 : css::lang::WrappedTargetException ,
295 : css::uno::RuntimeException, std::exception )
296 : {
297 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
298 :
299 : // SAFE ->
300 0 : SolarMutexClearableGuard aReadLock;
301 :
302 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
303 0 : if (pIt == m_lProps.end())
304 0 : throw css::beans::UnknownPropertyException();
305 :
306 0 : aReadLock.clear();
307 : // <- SAFE
308 :
309 0 : m_lVetoChangeListener.addInterface(sProperty, xListener);
310 0 : }
311 :
312 0 : void SAL_CALL PropertySetHelper::removeVetoableChangeListener(const OUString& sProperty,
313 : const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener)
314 : throw(css::beans::UnknownPropertyException,
315 : css::lang::WrappedTargetException ,
316 : css::uno::RuntimeException, std::exception )
317 : {
318 0 : TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
319 :
320 : // SAFE ->
321 0 : SolarMutexClearableGuard aReadLock;
322 :
323 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
324 0 : if (pIt == m_lProps.end())
325 0 : throw css::beans::UnknownPropertyException();
326 :
327 0 : aReadLock.clear();
328 : // <- SAFE
329 :
330 0 : m_lVetoChangeListener.removeInterface(sProperty, xListener);
331 0 : }
332 :
333 0 : css::uno::Sequence< css::beans::Property > SAL_CALL PropertySetHelper::getProperties()
334 : throw(css::uno::RuntimeException, std::exception)
335 : {
336 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
337 :
338 0 : SolarMutexGuard g;
339 :
340 0 : sal_Int32 c = (sal_Int32)m_lProps.size();
341 0 : css::uno::Sequence< css::beans::Property > lProps(c);
342 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt;
343 :
344 0 : for ( pIt = m_lProps.begin();
345 0 : pIt != m_lProps.end();
346 : ++pIt )
347 : {
348 0 : lProps[--c] = pIt->second;
349 : }
350 :
351 0 : return lProps;
352 : }
353 :
354 0 : css::beans::Property SAL_CALL PropertySetHelper::getPropertyByName(const OUString& sName)
355 : throw(css::beans::UnknownPropertyException,
356 : css::uno::RuntimeException, std::exception )
357 : {
358 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
359 :
360 0 : SolarMutexGuard g;
361 :
362 0 : PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sName);
363 0 : if (pIt == m_lProps.end())
364 0 : throw css::beans::UnknownPropertyException();
365 :
366 0 : return pIt->second;
367 : }
368 :
369 0 : sal_Bool SAL_CALL PropertySetHelper::hasPropertyByName(const OUString& sName)
370 : throw(css::uno::RuntimeException, std::exception)
371 : {
372 0 : TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
373 :
374 0 : SolarMutexGuard g;
375 :
376 0 : PropertySetHelper::TPropInfoHash::iterator pIt = m_lProps.find(sName);
377 0 : bool bExist = (pIt != m_lProps.end());
378 :
379 0 : return bExist;
380 : }
381 :
382 : } // namespace framework
383 :
384 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|