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