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