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