1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "unotools/accessiblerelationsethelper.hxx"
33*cdf0e10cSrcweir #include <rtl/uuid.h>
34*cdf0e10cSrcweir #include <vector>
35*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir using namespace	::utl;
38*cdf0e10cSrcweir using namespace	::rtl;
39*cdf0e10cSrcweir using namespace	::com::sun::star;
40*cdf0e10cSrcweir using namespace	::com::sun::star::accessibility;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir class AccessibleRelationSetHelperImpl
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir public:
45*cdf0e10cSrcweir 	AccessibleRelationSetHelperImpl();
46*cdf0e10cSrcweir 	AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl);
47*cdf0e10cSrcweir 	~AccessibleRelationSetHelperImpl();
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     sal_Int32 getRelationCount(  )
50*cdf0e10cSrcweir 		throw (uno::RuntimeException);
51*cdf0e10cSrcweir     AccessibleRelation getRelation( sal_Int32 nIndex )
52*cdf0e10cSrcweir 			throw (lang::IndexOutOfBoundsException,
53*cdf0e10cSrcweir 					uno::RuntimeException);
54*cdf0e10cSrcweir     sal_Bool containsRelation( sal_Int16 aRelationType )
55*cdf0e10cSrcweir 		throw (uno::RuntimeException);
56*cdf0e10cSrcweir     AccessibleRelation getRelationByType( sal_Int16 aRelationType )
57*cdf0e10cSrcweir 			throw (uno::RuntimeException);
58*cdf0e10cSrcweir 	void AddRelation(const AccessibleRelation& rRelation)
59*cdf0e10cSrcweir 			throw (uno::RuntimeException);
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir private:
62*cdf0e10cSrcweir 	std::vector<AccessibleRelation> maRelations;
63*cdf0e10cSrcweir };
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl()
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl)
70*cdf0e10cSrcweir 	: maRelations(rImpl.maRelations)
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir AccessibleRelationSetHelperImpl::~AccessibleRelationSetHelperImpl()
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir sal_Int32 AccessibleRelationSetHelperImpl::getRelationCount(  )
79*cdf0e10cSrcweir 	throw (uno::RuntimeException)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	return maRelations.size();
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir AccessibleRelation AccessibleRelationSetHelperImpl::getRelation( sal_Int32 nIndex )
85*cdf0e10cSrcweir 	throw (lang::IndexOutOfBoundsException,
86*cdf0e10cSrcweir 			uno::RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	if ((nIndex < 0) || (static_cast<sal_uInt32>(nIndex) >= maRelations.size()))
89*cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
90*cdf0e10cSrcweir 	return maRelations[nIndex];
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir sal_Bool AccessibleRelationSetHelperImpl::containsRelation( sal_Int16 aRelationType )
94*cdf0e10cSrcweir 	throw (uno::RuntimeException)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     AccessibleRelation defaultRelation; // default is INVALID
97*cdf0e10cSrcweir     AccessibleRelation relationByType = getRelationByType(aRelationType);
98*cdf0e10cSrcweir 	return relationByType.RelationType != defaultRelation.RelationType;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir AccessibleRelation AccessibleRelationSetHelperImpl::getRelationByType( sal_Int16 aRelationType )
102*cdf0e10cSrcweir 	throw (uno::RuntimeException)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	sal_Int32 nCount(getRelationCount());
105*cdf0e10cSrcweir 	sal_Int32 i(0);
106*cdf0e10cSrcweir 	sal_Bool bFound(sal_False);
107*cdf0e10cSrcweir 	while ((i < nCount) && !bFound)
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		if (maRelations[i].RelationType == aRelationType)
110*cdf0e10cSrcweir 			return maRelations[i];
111*cdf0e10cSrcweir 		else
112*cdf0e10cSrcweir 			i++;
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 	return AccessibleRelation();
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir void AccessibleRelationSetHelperImpl::AddRelation(const AccessibleRelation& rRelation)
118*cdf0e10cSrcweir 	throw (uno::RuntimeException)
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir 	sal_Int32 nCount(getRelationCount());
121*cdf0e10cSrcweir 	sal_Int32 i(0);
122*cdf0e10cSrcweir 	sal_Bool bFound(sal_False);
123*cdf0e10cSrcweir 	while ((i < nCount) && !bFound)
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		if (maRelations[i].RelationType == rRelation.RelationType)
126*cdf0e10cSrcweir 			bFound = sal_True;
127*cdf0e10cSrcweir 		else
128*cdf0e10cSrcweir 			i++;
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 	if (bFound)
131*cdf0e10cSrcweir 		maRelations[i].TargetSet = comphelper::concatSequences(maRelations[i].TargetSet, rRelation.TargetSet);
132*cdf0e10cSrcweir 	else
133*cdf0e10cSrcweir 		maRelations.push_back(rRelation);
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir //=====  internal  ============================================================
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir AccessibleRelationSetHelper::AccessibleRelationSetHelper ()
139*cdf0e10cSrcweir 	: mpHelperImpl(NULL)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	mpHelperImpl = new AccessibleRelationSetHelperImpl();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir AccessibleRelationSetHelper::AccessibleRelationSetHelper (const AccessibleRelationSetHelper& rHelper)
145*cdf0e10cSrcweir 	: cppu::WeakImplHelper1<XAccessibleRelationSet>()
146*cdf0e10cSrcweir     , mpHelperImpl(NULL)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	if (rHelper.mpHelperImpl)
149*cdf0e10cSrcweir 		mpHelperImpl = new AccessibleRelationSetHelperImpl(*rHelper.mpHelperImpl);
150*cdf0e10cSrcweir 	else
151*cdf0e10cSrcweir 		mpHelperImpl = new AccessibleRelationSetHelperImpl();
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir AccessibleRelationSetHelper::~AccessibleRelationSetHelper(void)
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir 	delete mpHelperImpl;
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //=====  XAccessibleRelationSet  ==============================================
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	/** Returns the number of relations in this relation set.
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         @return
164*cdf0e10cSrcweir             Returns the number of relations or zero if there are none.
165*cdf0e10cSrcweir 	*/
166*cdf0e10cSrcweir sal_Int32 SAL_CALL
167*cdf0e10cSrcweir 	AccessibleRelationSetHelper::getRelationCount(  )
168*cdf0e10cSrcweir 		throw (uno::RuntimeException)
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
171*cdf0e10cSrcweir 	return mpHelperImpl->getRelationCount();
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	/** Returns the relation of this relation set that is specified by
175*cdf0e10cSrcweir         the given index.
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         @param nIndex
178*cdf0e10cSrcweir             This index specifies the relatio to return.
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         @return
181*cdf0e10cSrcweir             For a valid index, i.e. inside the range 0 to the number of
182*cdf0e10cSrcweir             relations minus one, the returned value is the requested
183*cdf0e10cSrcweir             relation.  If the index is invalid then the returned relation
184*cdf0e10cSrcweir             has the type INVALID.
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	*/
187*cdf0e10cSrcweir  AccessibleRelation SAL_CALL
188*cdf0e10cSrcweir 		AccessibleRelationSetHelper::getRelation( sal_Int32 nIndex )
189*cdf0e10cSrcweir 			throw (lang::IndexOutOfBoundsException,
190*cdf0e10cSrcweir 					uno::RuntimeException)
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
193*cdf0e10cSrcweir 	return mpHelperImpl->getRelation(nIndex);
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	/** Tests whether the relation set contains a relation matching the
197*cdf0e10cSrcweir         specified key.
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         @param aRelationType
200*cdf0e10cSrcweir             The type of relation to look for in this set of relations.  This
201*cdf0e10cSrcweir             has to be one of the constants of
202*cdf0e10cSrcweir             <type>AccessibleRelationType</type>.
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         @return
205*cdf0e10cSrcweir             Returns <TRUE/> if there is a (at least one) relation of the
206*cdf0e10cSrcweir             given type and <FALSE/> if there is no such relation in the set.
207*cdf0e10cSrcweir 	*/
208*cdf0e10cSrcweir sal_Bool SAL_CALL
209*cdf0e10cSrcweir 	AccessibleRelationSetHelper::containsRelation( sal_Int16 aRelationType )
210*cdf0e10cSrcweir 		throw (uno::RuntimeException)
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
213*cdf0e10cSrcweir 	return mpHelperImpl->containsRelation(aRelationType);
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	/** Retrieve and return the relation with the given relation type.
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         @param aRelationType
219*cdf0e10cSrcweir             The type of the relation to return.  This has to be one of the
220*cdf0e10cSrcweir             constants of <type>AccessibleRelationType</type>.
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         @return
223*cdf0e10cSrcweir             If a relation with the given type could be found than (a copy
224*cdf0e10cSrcweir             of) this relation is returned.  Otherwise a relation with the
225*cdf0e10cSrcweir             type INVALID is returned.
226*cdf0e10cSrcweir 	*/
227*cdf0e10cSrcweir AccessibleRelation SAL_CALL
228*cdf0e10cSrcweir 		AccessibleRelationSetHelper::getRelationByType( sal_Int16 aRelationType )
229*cdf0e10cSrcweir 			throw (uno::RuntimeException)
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
232*cdf0e10cSrcweir 	return mpHelperImpl->getRelationByType(aRelationType);
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir void AccessibleRelationSetHelper::AddRelation(const AccessibleRelation& rRelation)
236*cdf0e10cSrcweir 			throw (uno::RuntimeException)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
239*cdf0e10cSrcweir 	mpHelperImpl->AddRelation(rRelation);
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir //=====  XTypeProvider  =======================================================
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir uno::Sequence< ::com::sun::star::uno::Type>
245*cdf0e10cSrcweir 	AccessibleRelationSetHelper::getTypes (void)
246*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
249*cdf0e10cSrcweir 	const ::com::sun::star::uno::Type aTypeList[] = {
250*cdf0e10cSrcweir     	::getCppuType((const uno::Reference<
251*cdf0e10cSrcweir         	XAccessibleRelationSet>*)0),
252*cdf0e10cSrcweir     	::getCppuType((const uno::Reference<
253*cdf0e10cSrcweir         	lang::XTypeProvider>*)0)
254*cdf0e10cSrcweir 		};
255*cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type>
256*cdf0e10cSrcweir     	aTypeSequence (aTypeList, 2);
257*cdf0e10cSrcweir 	return aTypeSequence;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir uno::Sequence<sal_Int8> SAL_CALL
261*cdf0e10cSrcweir 	AccessibleRelationSetHelper::getImplementationId (void)
262*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
265*cdf0e10cSrcweir 	static uno::Sequence<sal_Int8> aId;
266*cdf0e10cSrcweir 	if (aId.getLength() == 0)
267*cdf0e10cSrcweir 	{
268*cdf0e10cSrcweir 		aId.realloc (16);
269*cdf0e10cSrcweir 		rtl_createUuid ((sal_uInt8 *)aId.getArray(), 0, sal_True);
270*cdf0e10cSrcweir 	}
271*cdf0e10cSrcweir 	return aId;
272*cdf0e10cSrcweir }
273