1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertiesfilterednotifier.cxx,v $
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 "propertiesfilterednotifier.hxx"
36 #include <cppuhelper/queryinterface.hxx>
40 namespace uno
= ::com::sun::star::uno
;
41 namespace lang
= ::com::sun::star::lang
;
42 namespace beans
= ::com::sun::star::beans
;
44 //-----------------------------------------------------------------------------
45 // class PropertiesFilteredNotifier
46 //-----------------------------------------------------------------------------
48 PropertiesFilteredNotifier::PropertiesFilteredNotifier(
49 uno::Reference
< beans::XPropertiesChangeListener
>const& xTarget
,
50 uno::Sequence
< rtl::OUString
> const& aFilterNames
54 , m_aFilterNames(aFilterNames
)
56 OSL_ENSURE(xTarget
.is(),"PropertiesFilteredNotifier: FORWARDING TO NULL LISTENER");
57 OSL_ENSURE(aFilterNames
.getLength() > 0,"PropertiesFilteredNotifier: FILTER IS EMPTY (no target)");
59 //-----------------------------------------------------------------------------
61 PropertiesFilteredNotifier::~PropertiesFilteredNotifier()
64 //-----------------------------------------------------------------------------
66 void SAL_CALL
PropertiesFilteredNotifier::acquire() throw()
68 m_aRefCount
.acquire();
70 //-----------------------------------------------------------------------------
72 void SAL_CALL
PropertiesFilteredNotifier::release( ) throw()
74 if (m_aRefCount
.release() == 0)
77 //-----------------------------------------------------------------------------
79 uno::Any SAL_CALL
PropertiesFilteredNotifier::queryInterface( const uno::Type
& aType
)
80 throw(uno::RuntimeException
)
82 return cppu::queryInterface(aType
83 , static_cast< beans::XPropertiesChangeListener
*>(this)
84 , static_cast< lang::XEventListener
*>(this)
85 , static_cast< uno::XInterface
*>(this)
88 //-----------------------------------------------------------------------------
90 void SAL_CALL
PropertiesFilteredNotifier::disposing( const lang::EventObject
& Source
)
91 throw(uno::RuntimeException
)
94 m_xTarget
->disposing(Source
);
96 //-----------------------------------------------------------------------------
98 inline // private and only used twice
99 bool PropertiesFilteredNotifier::implAccept(const ::com::sun::star::beans::PropertyChangeEvent
& evt
) const
101 // todo: optimize by presorting and binary searching
102 sal_Int32
const nCount
= m_aFilterNames
.getLength();
104 for (sal_Int32 i
= 0; i
<nCount
; ++i
)
105 if (evt
.PropertyName
== m_aFilterNames
[i
])
109 //-----------------------------------------------------------------------------
111 // private and only used once
112 uno::Sequence
< beans::PropertyChangeEvent
> PropertiesFilteredNotifier::implFilter(const uno::Sequence
< beans::PropertyChangeEvent
>& evt
) const
114 sal_Int32
const nSize
= evt
.getLength();
115 sal_Int32 nAccepted
= 0;
117 while ( nAccepted
< nSize
&& implAccept(evt
[nAccepted
]) )
120 if (nAccepted
== nSize
) // all accepted
123 // create a modified copy
124 uno::Sequence
< beans::PropertyChangeEvent
> aResult(evt
);
125 for (sal_Int32 nCur
= nAccepted
+1; nCur
<nSize
; ++nCur
)
127 if (implAccept(evt
[nCur
]))
129 aResult
[nAccepted
++] = evt
[nCur
];
132 aResult
.realloc(nAccepted
);
133 OSL_ASSERT(aResult
.getLength() == nAccepted
);
137 //-----------------------------------------------------------------------------
139 void SAL_CALL
PropertiesFilteredNotifier::propertiesChange( const uno::Sequence
< beans::PropertyChangeEvent
>& evt
)
140 throw(uno::RuntimeException
)
142 uno::Sequence
< beans::PropertyChangeEvent
> aFilteredEvt( implFilter(evt
) );
144 if (aFilteredEvt
.getLength() > 0)
147 m_xTarget
->propertiesChange(aFilteredEvt
);
150 //-----------------------------------------------------------------------------