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