Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <unotools/accessiblerelationsethelper.hxx>
21 : #include <vector>
22 : #include <comphelper/sequence.hxx>
23 : #include <comphelper/servicehelper.hxx>
24 :
25 : using namespace ::utl;
26 : using namespace ::com::sun::star;
27 : using namespace ::com::sun::star::accessibility;
28 :
29 : class AccessibleRelationSetHelperImpl
30 : {
31 : public:
32 : AccessibleRelationSetHelperImpl();
33 : AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl);
34 : ~AccessibleRelationSetHelperImpl();
35 :
36 : sal_Int32 getRelationCount( )
37 : throw (uno::RuntimeException);
38 : AccessibleRelation getRelation( sal_Int32 nIndex )
39 : throw (lang::IndexOutOfBoundsException,
40 : uno::RuntimeException);
41 : bool containsRelation( sal_Int16 aRelationType )
42 : throw (uno::RuntimeException);
43 : AccessibleRelation getRelationByType( sal_Int16 aRelationType )
44 : throw (uno::RuntimeException);
45 : void AddRelation(const AccessibleRelation& rRelation)
46 : throw (uno::RuntimeException);
47 :
48 : private:
49 : std::vector<AccessibleRelation> maRelations;
50 : };
51 :
52 36 : AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl()
53 : {
54 36 : }
55 :
56 1 : AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl)
57 1 : : maRelations(rImpl.maRelations)
58 : {
59 1 : }
60 :
61 37 : AccessibleRelationSetHelperImpl::~AccessibleRelationSetHelperImpl()
62 : {
63 37 : }
64 :
65 6 : sal_Int32 AccessibleRelationSetHelperImpl::getRelationCount( )
66 : throw (uno::RuntimeException)
67 : {
68 6 : return maRelations.size();
69 : }
70 :
71 6 : AccessibleRelation AccessibleRelationSetHelperImpl::getRelation( sal_Int32 nIndex )
72 : throw (lang::IndexOutOfBoundsException,
73 : uno::RuntimeException)
74 : {
75 6 : if ((nIndex < 0) || (static_cast<sal_uInt32>(nIndex) >= maRelations.size()))
76 0 : throw lang::IndexOutOfBoundsException();
77 6 : return maRelations[nIndex];
78 : }
79 :
80 0 : bool AccessibleRelationSetHelperImpl::containsRelation( sal_Int16 aRelationType )
81 : throw (uno::RuntimeException)
82 : {
83 0 : AccessibleRelation defaultRelation; // default is INVALID
84 0 : AccessibleRelation relationByType = getRelationByType(aRelationType);
85 0 : return relationByType.RelationType != defaultRelation.RelationType;
86 : }
87 :
88 0 : AccessibleRelation AccessibleRelationSetHelperImpl::getRelationByType( sal_Int16 aRelationType )
89 : throw (uno::RuntimeException)
90 : {
91 0 : sal_Int32 nCount(getRelationCount());
92 0 : sal_Int32 i(0);
93 0 : bool bFound(false);
94 0 : while ((i < nCount) && !bFound)
95 : {
96 0 : if (maRelations[i].RelationType == aRelationType)
97 0 : return maRelations[i];
98 : else
99 0 : i++;
100 : }
101 0 : return AccessibleRelation();
102 : }
103 :
104 4 : void AccessibleRelationSetHelperImpl::AddRelation(const AccessibleRelation& rRelation)
105 : throw (uno::RuntimeException)
106 : {
107 4 : sal_Int32 nCount(getRelationCount());
108 4 : sal_Int32 i(0);
109 4 : bool bFound(false);
110 9 : while ((i < nCount) && !bFound)
111 : {
112 1 : if (maRelations[i].RelationType == rRelation.RelationType)
113 0 : bFound = true;
114 : else
115 1 : i++;
116 : }
117 4 : if (bFound)
118 0 : maRelations[i].TargetSet = comphelper::concatSequences(maRelations[i].TargetSet, rRelation.TargetSet);
119 : else
120 4 : maRelations.push_back(rRelation);
121 4 : }
122 :
123 : //===== internal ============================================================
124 :
125 36 : AccessibleRelationSetHelper::AccessibleRelationSetHelper ()
126 36 : : mpHelperImpl(NULL)
127 : {
128 36 : mpHelperImpl = new AccessibleRelationSetHelperImpl();
129 36 : }
130 :
131 1 : AccessibleRelationSetHelper::AccessibleRelationSetHelper (const AccessibleRelationSetHelper& rHelper)
132 : : cppu::WeakImplHelper1<XAccessibleRelationSet>()
133 1 : , mpHelperImpl(NULL)
134 : {
135 1 : if (rHelper.mpHelperImpl)
136 1 : mpHelperImpl = new AccessibleRelationSetHelperImpl(*rHelper.mpHelperImpl);
137 : else
138 0 : mpHelperImpl = new AccessibleRelationSetHelperImpl();
139 1 : }
140 :
141 111 : AccessibleRelationSetHelper::~AccessibleRelationSetHelper()
142 : {
143 37 : delete mpHelperImpl;
144 74 : }
145 :
146 : //===== XAccessibleRelationSet ==============================================
147 :
148 : /** Returns the number of relations in this relation set.
149 :
150 : @return
151 : Returns the number of relations or zero if there are none.
152 : */
153 : sal_Int32 SAL_CALL
154 2 : AccessibleRelationSetHelper::getRelationCount( )
155 : throw (uno::RuntimeException, std::exception)
156 : {
157 2 : osl::MutexGuard aGuard (maMutex);
158 2 : return mpHelperImpl->getRelationCount();
159 : }
160 :
161 : /** Returns the relation of this relation set that is specified by
162 : the given index.
163 :
164 : @param nIndex
165 : This index specifies the relatio to return.
166 :
167 : @return
168 : For a valid index, i.e. inside the range 0 to the number of
169 : relations minus one, the returned value is the requested
170 : relation. If the index is invalid then the returned relation
171 : has the type INVALID.
172 :
173 : */
174 : AccessibleRelation SAL_CALL
175 6 : AccessibleRelationSetHelper::getRelation( sal_Int32 nIndex )
176 : throw (lang::IndexOutOfBoundsException,
177 : uno::RuntimeException, std::exception)
178 : {
179 6 : osl::MutexGuard aGuard (maMutex);
180 6 : return mpHelperImpl->getRelation(nIndex);
181 : }
182 :
183 : /** Tests whether the relation set contains a relation matching the
184 : specified key.
185 :
186 : @param aRelationType
187 : The type of relation to look for in this set of relations. This
188 : has to be one of the constants of
189 : <type>AccessibleRelationType</type>.
190 :
191 : @return
192 : Returns <TRUE/> if there is a (at least one) relation of the
193 : given type and <FALSE/> if there is no such relation in the set.
194 : */
195 : sal_Bool SAL_CALL
196 0 : AccessibleRelationSetHelper::containsRelation( sal_Int16 aRelationType )
197 : throw (uno::RuntimeException, std::exception)
198 : {
199 0 : osl::MutexGuard aGuard (maMutex);
200 0 : return mpHelperImpl->containsRelation(aRelationType);
201 : }
202 :
203 : /** Retrieve and return the relation with the given relation type.
204 :
205 : @param aRelationType
206 : The type of the relation to return. This has to be one of the
207 : constants of <type>AccessibleRelationType</type>.
208 :
209 : @return
210 : If a relation with the given type could be found than (a copy
211 : of) this relation is returned. Otherwise a relation with the
212 : type INVALID is returned.
213 : */
214 : AccessibleRelation SAL_CALL
215 0 : AccessibleRelationSetHelper::getRelationByType( sal_Int16 aRelationType )
216 : throw (uno::RuntimeException, std::exception)
217 : {
218 0 : osl::MutexGuard aGuard (maMutex);
219 0 : return mpHelperImpl->getRelationByType(aRelationType);
220 : }
221 :
222 4 : void AccessibleRelationSetHelper::AddRelation(const AccessibleRelation& rRelation)
223 : throw (uno::RuntimeException)
224 : {
225 4 : osl::MutexGuard aGuard (maMutex);
226 4 : mpHelperImpl->AddRelation(rRelation);
227 4 : }
228 :
229 : //===== XTypeProvider =======================================================
230 :
231 : uno::Sequence< ::com::sun::star::uno::Type>
232 0 : AccessibleRelationSetHelper::getTypes()
233 : throw (::com::sun::star::uno::RuntimeException, std::exception)
234 : {
235 0 : osl::MutexGuard aGuard (maMutex);
236 : const ::com::sun::star::uno::Type aTypeList[] = {
237 0 : cppu::UnoType<XAccessibleRelationSet>::get(),
238 0 : cppu::UnoType<lang::XTypeProvider>::get()
239 0 : };
240 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type>
241 0 : aTypeSequence (aTypeList, 2);
242 0 : return aTypeSequence;
243 : }
244 :
245 : uno::Sequence<sal_Int8> SAL_CALL
246 0 : AccessibleRelationSetHelper::getImplementationId()
247 : throw (::com::sun::star::uno::RuntimeException, std::exception)
248 : {
249 0 : return css::uno::Sequence<sal_Int8>();
250 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|