1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "Functions.hxx"
24 #include "Function.hxx"
25 #include <tools/debug.hxx>
26 #include "core_resource.hxx"
27 #ifndef REPORTDESIGN_CORE_RESOURCE_HRC_
28 #include "core_resource.hrc"
29 #endif
30 #include <comphelper/property.hxx>
31 #include <boost/bind.hpp>
32 #include <algorithm>
33 // =============================================================================
34 namespace reportdesign
35 {
36 // =============================================================================
37 	using namespace com::sun::star;
DBG_NAME(rpt_OFunctions)38 DBG_NAME( rpt_OFunctions )
39 // -----------------------------------------------------------------------------
40 OFunctions::OFunctions(const uno::Reference< report::XFunctionsSupplier >& _xParent,const uno::Reference< uno::XComponentContext >& context)
41 :FunctionsBase(m_aMutex)
42 ,m_aContainerListeners(m_aMutex)
43 ,m_xContext(context)
44 ,m_xParent(_xParent)
45 {
46 	DBG_CTOR( rpt_OFunctions,NULL);
47 }
48 //--------------------------------------------------------------------------
49 // TODO: VirtualFunctionFinder: This is virtual function!
50 //
~OFunctions()51 OFunctions::~OFunctions()
52 {
53     DBG_DTOR( rpt_OFunctions,NULL);
54 }
55 //--------------------------------------------------------------------------
dispose()56 void SAL_CALL OFunctions::dispose() throw(uno::RuntimeException)
57 {
58 	cppu::WeakComponentImplHelperBase::dispose();
59 }
60 // -----------------------------------------------------------------------------
61 // TODO: VirtualFunctionFinder: This is virtual function!
62 //
disposing()63 void SAL_CALL OFunctions::disposing()
64 {
65     ::std::for_each(m_aFunctions.begin(),m_aFunctions.end(),::boost::mem_fn(&com::sun::star::report::XFunction::dispose));
66     m_aFunctions.clear();
67     lang::EventObject aDisposeEvent( static_cast< ::cppu::OWeakObject* >( this ) );
68     m_aContainerListeners.disposeAndClear( aDisposeEvent );
69     m_xContext.clear();
70 }
71 // -----------------------------------------------------------------------------
72 // XFunctionsSupplier
73 // -----------------------------------------------------------------------------
createFunction()74 uno::Reference< report::XFunction > SAL_CALL OFunctions::createFunction(  ) throw (uno::RuntimeException)
75 {
76 	return new OFunction(m_xContext);
77 }
78 // -----------------------------------------------------------------------------
79 // XIndexContainer
insertByIndex(::sal_Int32 Index,const uno::Any & aElement)80 void SAL_CALL OFunctions::insertByIndex( ::sal_Int32 Index, const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
81 {
82 	{
83 		::osl::MutexGuard aGuard(m_aMutex);
84 		sal_Bool bAdd = (Index == static_cast<sal_Int32>(m_aFunctions.size()));
85 		if ( !bAdd )
86 			checkIndex(Index);
87 		uno::Reference< report::XFunction > xFunction(aElement,uno::UNO_QUERY);
88 		if ( !xFunction.is() )
89 			throw lang::IllegalArgumentException(RPT_RESSTRING(RID_STR_ARGUMENT_IS_NULL,m_xContext->getServiceManager()),*this,2);
90 
91 		if ( bAdd )
92 			m_aFunctions.push_back(xFunction);
93 		else
94 		{
95 			TFunctions::iterator aPos = m_aFunctions.begin();
96 			::std::advance(aPos,Index);
97 			m_aFunctions.insert(aPos, xFunction);
98 		}
99         xFunction->setParent(*this);
100 	}
101 	// notify our container listeners
102 	container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), aElement, uno::Any());
103 	m_aContainerListeners.notifyEach(&container::XContainerListener::elementInserted,aEvent);
104 }
105 
106 // -----------------------------------------------------------------------------
removeByIndex(::sal_Int32 Index)107 void SAL_CALL OFunctions::removeByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
108 {
109 	uno::Reference< report::XFunction > xFunction;
110 	{
111 		::osl::MutexGuard aGuard(m_aMutex);
112 		checkIndex(Index);
113 		TFunctions::iterator aPos = m_aFunctions.begin();
114 		::std::advance(aPos,Index);
115 		xFunction = *aPos;
116 		m_aFunctions.erase(aPos);
117         xFunction->setParent(NULL);
118 	}
119 	container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), uno::makeAny(xFunction), uno::Any());
120 	m_aContainerListeners.notifyEach(&container::XContainerListener::elementRemoved,aEvent);
121 }
122 // -----------------------------------------------------------------------------
123 // XIndexReplace
replaceByIndex(::sal_Int32 Index,const uno::Any & Element)124 void SAL_CALL OFunctions::replaceByIndex( ::sal_Int32 Index, const uno::Any& Element ) throw (lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
125 {
126 	uno::Any aOldElement;
127 	{
128 		::osl::MutexGuard aGuard(m_aMutex);
129 		checkIndex(Index);
130 		uno::Reference< report::XFunction > xFunction(Element,uno::UNO_QUERY);
131 		if ( !xFunction.is() )
132 			throw lang::IllegalArgumentException(RPT_RESSTRING(RID_STR_ARGUMENT_IS_NULL,m_xContext->getServiceManager()),*this,2);
133 		TFunctions::iterator aPos = m_aFunctions.begin();
134 		::std::advance(aPos,Index);
135 		aOldElement <<= *aPos;
136 		*aPos = xFunction;
137 	}
138 
139 	container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), Element, aOldElement);
140 	m_aContainerListeners.notifyEach(&container::XContainerListener::elementReplaced,aEvent);
141 }
142 // -----------------------------------------------------------------------------
143 // XIndexAccess
getCount()144 ::sal_Int32 SAL_CALL OFunctions::getCount(  ) throw (uno::RuntimeException)
145 {
146 	::osl::MutexGuard aGuard(m_aMutex);
147 	return m_aFunctions.size();
148 }
149 // -----------------------------------------------------------------------------
getByIndex(::sal_Int32 Index)150 uno::Any SAL_CALL OFunctions::getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
151 {
152 	::osl::MutexGuard aGuard(m_aMutex);
153 	checkIndex(Index);
154 	TFunctions::iterator aPos = m_aFunctions.begin();
155 	::std::advance(aPos,Index);
156 	return uno::makeAny(*aPos);
157 }
158 // -----------------------------------------------------------------------------
159 // XElementAccess
getElementType()160 uno::Type SAL_CALL OFunctions::getElementType(  ) throw (uno::RuntimeException)
161 {
162 	return ::getCppuType(static_cast< uno::Reference<report::XFunction>*>(NULL));
163 }
164 // -----------------------------------------------------------------------------
hasElements()165 ::sal_Bool SAL_CALL OFunctions::hasElements(  ) throw (uno::RuntimeException)
166 {
167 	::osl::MutexGuard aGuard(m_aMutex);
168 	return !m_aFunctions.empty();
169 }
170 // -----------------------------------------------------------------------------
171 // XChild
getParent()172 uno::Reference< uno::XInterface > SAL_CALL OFunctions::getParent(  ) throw (uno::RuntimeException)
173 {
174 	return m_xParent;
175 }
176 // -----------------------------------------------------------------------------
setParent(const uno::Reference<uno::XInterface> &)177 void SAL_CALL OFunctions::setParent( const uno::Reference< uno::XInterface >& /*Parent*/ ) throw (lang::NoSupportException, uno::RuntimeException)
178 {
179 	throw lang::NoSupportException();
180 }
181 // -----------------------------------------------------------------------------
182 // XContainer
addContainerListener(const uno::Reference<container::XContainerListener> & xListener)183 void SAL_CALL OFunctions::addContainerListener( const uno::Reference< container::XContainerListener >& xListener ) throw (uno::RuntimeException)
184 {
185 	m_aContainerListeners.addInterface(xListener);
186 }
187 // -----------------------------------------------------------------------------
removeContainerListener(const uno::Reference<container::XContainerListener> & xListener)188 void SAL_CALL OFunctions::removeContainerListener( const uno::Reference< container::XContainerListener >& xListener ) throw (uno::RuntimeException)
189 {
190 	m_aContainerListeners.removeInterface(xListener);
191 }
192 // -----------------------------------------------------------------------------
checkIndex(sal_Int32 _nIndex)193 void OFunctions::checkIndex(sal_Int32 _nIndex)
194 {
195 	if ( _nIndex < 0 || static_cast<sal_Int32>(m_aFunctions.size()) <= _nIndex )
196 		throw lang::IndexOutOfBoundsException();
197 }
198 // =============================================================================
199 }
200 // =============================================================================
201