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_sw.hxx"
26 #include <comphelper/accessiblekeybindinghelper.hxx>
27 #include <swurl.hxx>
28 #include <vos/mutex.hxx>
29 #include <vcl/svapp.hxx>
30 #include <ndtxt.hxx>
31 #include <txtinet.hxx>
32 #include <accpara.hxx>
33 #include <acchyperlink.hxx>
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::accessibility;
37 using ::rtl::OUString;
38 
39 SwAccessibleHyperlink::SwAccessibleHyperlink( sal_uInt16 nHPos,
40 	SwAccessibleParagraph *p, sal_Int32 nStt, sal_Int32 nEnd ) :
41 	nHintPos( nHPos ),
42 	xPara( p ),
43 	nStartIdx( nStt ),
44 	nEndIdx( nEnd )
45 {
46 }
47 
48 const SwTxtAttr *SwAccessibleHyperlink::GetTxtAttr() const
49 {
50 	const SwTxtAttr *pTxtAttr = 0;
51 	if( xPara.isValid() && xPara->GetMap() )
52 	{
53 		const SwTxtNode *pTxtNd = xPara->GetTxtNode();
54 		const SwpHints *pHints = pTxtNd->GetpSwpHints();
55 		if( pHints && nHintPos < pHints->Count() )
56 		{
57 			const SwTxtAttr *pHt = (*pHints)[nHintPos];
58 			if( RES_TXTATR_INETFMT == pHt->Which() )
59 				pTxtAttr = pHt;
60 		}
61 	}
62 
63 	return pTxtAttr;
64 }
65 
66 
67 // XAccessibleAction
68 sal_Int32 SAL_CALL SwAccessibleHyperlink::getAccessibleActionCount()
69 		throw (uno::RuntimeException)
70 {
71 	 return isValid() ? 1 : 0;
72 }
73 
74 sal_Bool SAL_CALL SwAccessibleHyperlink::doAccessibleAction( sal_Int32 nIndex )
75 		throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
76 {
77 	vos::OGuard aGuard(Application::GetSolarMutex());
78 
79 	sal_Bool bRet = sal_False;
80 
81 	const SwTxtAttr *pTxtAttr = GetTxtAttr();
82 	if( pTxtAttr && 0 == nIndex )
83 	{
84 		const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
85 		if( rINetFmt.GetValue().Len() )
86 		{
87 			ViewShell *pVSh = xPara->GetShell();
88 			if( pVSh )
89 			{
90 				LoadURL( rINetFmt.GetValue(), pVSh, URLLOAD_NOFILTER,
91 						 &rINetFmt.GetTargetFrame() );
92 				ASSERT( pTxtAttr == rINetFmt.GetTxtINetFmt(),
93 					 	"lost my txt attr" );
94 				const SwTxtINetFmt* pTxtAttr2 = rINetFmt.GetTxtINetFmt();
95 				if( pTxtAttr2 )
96 				{
97                     const_cast<SwTxtINetFmt*>(pTxtAttr2)->SetVisited(true);
98                     const_cast<SwTxtINetFmt*>(pTxtAttr2)->SetVisitedValid(true);
99 				}
100 				bRet = sal_True;
101 			}
102 		}
103 	}
104 
105 	return bRet;
106 }
107 
108 OUString SAL_CALL SwAccessibleHyperlink::getAccessibleActionDescription(
109 		sal_Int32 nIndex )
110 		throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
111 {
112 	OUString sDesc;
113 
114 	const SwTxtAttr *pTxtAttr = GetTxtAttr();
115 	if( pTxtAttr && 0 == nIndex )
116 	{
117 		const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
118 		sDesc = OUString( rINetFmt.GetValue() );
119 	}
120 
121 	return sDesc;
122 }
123 
124 uno::Reference< XAccessibleKeyBinding > SAL_CALL
125 	SwAccessibleHyperlink::getAccessibleActionKeyBinding( sal_Int32 nIndex )
126 	throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
127 {
128 	uno::Reference< XAccessibleKeyBinding > xKeyBinding;
129 
130 	if( isValid() && 0==nIndex )
131 	{
132 		::comphelper::OAccessibleKeyBindingHelper* pKeyBindingHelper =
133 		   	new ::comphelper::OAccessibleKeyBindingHelper();
134 		xKeyBinding = pKeyBindingHelper;
135 
136         awt::KeyStroke aKeyStroke;
137 		aKeyStroke.Modifiers = 0;
138 		aKeyStroke.KeyCode = KEY_RETURN;
139 		aKeyStroke.KeyChar = 0;
140 		aKeyStroke.KeyFunc = 0;
141 		pKeyBindingHelper->AddKeyBinding( aKeyStroke );
142 	}
143 
144 	return xKeyBinding;
145 }
146 
147 // XAccessibleHyperlink
148 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionAnchor(
149         sal_Int32 /*nIndex*/ )
150 		throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
151 {
152 	return uno::Any();
153 }
154 
155 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionObject(
156             sal_Int32 /*nIndex*/ )
157 	throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
158 {
159 	return uno::Any();
160 }
161 
162 sal_Int32 SAL_CALL SwAccessibleHyperlink::getStartIndex()
163 		throw (uno::RuntimeException)
164 {
165 	return nStartIdx;
166 }
167 
168 sal_Int32 SAL_CALL SwAccessibleHyperlink::getEndIndex()
169 		throw (uno::RuntimeException)
170 {
171 	return nEndIdx;
172 }
173 
174 sal_Bool SAL_CALL SwAccessibleHyperlink::isValid(  )
175 		throw (uno::RuntimeException)
176 {
177 	vos::OGuard aGuard(Application::GetSolarMutex());
178 	return xPara.isValid();
179 }
180 
181 void SwAccessibleHyperlink::Invalidate()
182 {
183 	vos::OGuard aGuard(Application::GetSolarMutex());
184 	xPara = 0;
185 }
186 
187