xref: /trunk/main/sw/source/core/access/acchyperlink.cxx (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
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 //IAccessibility2 Implementation 2009-----
36 #include <comphelper/processfactory.hxx>
37 #ifndef _COM_SUN_STAR_FRAME_XDESKTOP_HPP_
38 #include <com/sun/star/frame/XDesktop.hpp>
39 #endif
40 #ifndef _COM_SUN_STAR_FRAME_XCOMPONENTLOADER_HPP_
41 #include <com/sun/star/frame/XComponentLoader.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_DOCUMENT_XLINKTARGETSUPPLIER_HPP_
44 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
45 #endif
46 //-----IAccessibility2 Implementation 2009
47 
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::accessibility;
50 using ::rtl::OUString;
51 using ::com::sun::star::lang::IndexOutOfBoundsException;
52 
53 SwAccessibleHyperlink::SwAccessibleHyperlink( sal_uInt16 nHPos,
54     SwAccessibleParagraph *p, sal_Int32 nStt, sal_Int32 nEnd ) :
55     nHintPos( nHPos ),
56     xPara( p ),
57     nStartIdx( nStt ),
58     nEndIdx( nEnd )
59 {
60 }
61 
62 const SwTxtAttr *SwAccessibleHyperlink::GetTxtAttr() const
63 {
64     const SwTxtAttr *pTxtAttr = 0;
65     if( xPara.isValid() && xPara->GetMap() )
66     {
67         const SwTxtNode *pTxtNd = xPara->GetTxtNode();
68         const SwpHints *pHints = pTxtNd->GetpSwpHints();
69         if( pHints && nHintPos < pHints->Count() )
70         {
71             const SwTxtAttr *pHt = (*pHints)[nHintPos];
72             if( RES_TXTATR_INETFMT == pHt->Which() )
73                 pTxtAttr = pHt;
74         }
75     }
76 
77     return pTxtAttr;
78 }
79 
80 
81 // XAccessibleAction
82 sal_Int32 SAL_CALL SwAccessibleHyperlink::getAccessibleActionCount()
83         throw (uno::RuntimeException)
84 {
85      return isValid() ? 1 : 0;
86 }
87 
88 sal_Bool SAL_CALL SwAccessibleHyperlink::doAccessibleAction( sal_Int32 nIndex )
89         throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
90 {
91     vos::OGuard aGuard(Application::GetSolarMutex());
92 
93     sal_Bool bRet = sal_False;
94 
95     //IAccessibility2 Implementation 2009-----
96     if(nIndex != 0)
97         throw new IndexOutOfBoundsException;
98     const SwTxtAttr *pTxtAttr = GetTxtAttr();
99     if( pTxtAttr /*&& 0 == nIndex*/ )
100     //-----IAccessibility2 Implementation 2009
101     {
102         const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
103         if( rINetFmt.GetValue().Len() )
104         {
105             ViewShell *pVSh = xPara->GetShell();
106             if( pVSh )
107             {
108                 LoadURL( rINetFmt.GetValue(), pVSh, URLLOAD_NOFILTER,
109                          &rINetFmt.GetTargetFrame() );
110                 ASSERT( pTxtAttr == rINetFmt.GetTxtINetFmt(),
111                         "lost my txt attr" );
112                 const SwTxtINetFmt* pTxtAttr2 = rINetFmt.GetTxtINetFmt();
113                 if( pTxtAttr2 )
114                 {
115                     const_cast<SwTxtINetFmt*>(pTxtAttr2)->SetVisited(true);
116                     const_cast<SwTxtINetFmt*>(pTxtAttr2)->SetVisitedValid(true);
117                 }
118                 bRet = sal_True;
119             }
120         }
121     }
122 
123     return bRet;
124 }
125 
126 OUString SAL_CALL SwAccessibleHyperlink::getAccessibleActionDescription(
127         sal_Int32 nIndex )
128         throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
129 {
130     OUString sDesc;
131 
132     //IAccessibility2 Implementation 2009-----
133     if(nIndex != 0)
134         throw new IndexOutOfBoundsException;
135     const SwTxtAttr *pTxtAttr = GetTxtAttr();
136     if( pTxtAttr /*&& 0 == nIndex*/ )
137     {
138         const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
139         sDesc = OUString( rINetFmt.GetValue() );
140     }
141     //-----IAccessibility2 Implementation 2009
142     return sDesc;
143 }
144 
145 uno::Reference< XAccessibleKeyBinding > SAL_CALL
146     SwAccessibleHyperlink::getAccessibleActionKeyBinding( sal_Int32 )
147     throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
148 {
149     uno::Reference< XAccessibleKeyBinding > xKeyBinding;
150 
151     //IAccessibility2 Implementation 2009-----
152     if( isValid() /*&& 0 == nIndex*/ )
153     //-----IAccessibility2 Implementation 2009
154     {
155         ::comphelper::OAccessibleKeyBindingHelper* pKeyBindingHelper =
156             new ::comphelper::OAccessibleKeyBindingHelper();
157         xKeyBinding = pKeyBindingHelper;
158 
159         awt::KeyStroke aKeyStroke;
160         aKeyStroke.Modifiers = 0;
161         aKeyStroke.KeyCode = KEY_RETURN;
162         aKeyStroke.KeyChar = 0;
163         aKeyStroke.KeyFunc = 0;
164         pKeyBindingHelper->AddKeyBinding( aKeyStroke );
165     }
166 
167     return xKeyBinding;
168 }
169 
170 // XAccessibleHyperlink
171 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionAnchor(
172         sal_Int32 nIndex)
173         throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
174 {
175     uno::Any aRet;
176     //IAccessibility2 Implementation 2009-----
177     if(nIndex != 0)
178         throw new IndexOutOfBoundsException;
179     //End Added.
180     ::rtl::OUString text = OUString( xPara->GetString() );
181     ::rtl::OUString retText =  text.copy(nStartIdx, nEndIdx - nStartIdx);
182     aRet <<= retText;
183     //-----IAccessibility2 Implementation 2009
184     return aRet;
185 }
186 
187 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionObject(
188             sal_Int32 nIndex )
189     throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
190 {
191     //IAccessibility2 Implementation 2009-----
192     if(nIndex != 0)
193         throw new IndexOutOfBoundsException;
194     //End Added.
195     const SwTxtAttr *pTxtAttr = GetTxtAttr();
196     ::rtl::OUString retText;
197     if( pTxtAttr /*&& 0 == nIndex*/ )
198     {
199         const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
200         retText = OUString( rINetFmt.GetValue() );
201     }
202     uno::Any aRet;
203     aRet <<= retText;
204     return aRet;
205     //-----IAccessibility2 Implementation 2009
206 }
207 
208 sal_Int32 SAL_CALL SwAccessibleHyperlink::getStartIndex()
209         throw (uno::RuntimeException)
210 {
211     return nStartIdx;
212 }
213 
214 sal_Int32 SAL_CALL SwAccessibleHyperlink::getEndIndex()
215         throw (uno::RuntimeException)
216 {
217     return nEndIdx;
218 }
219 
220 sal_Bool SAL_CALL SwAccessibleHyperlink::isValid(  )
221         throw (uno::RuntimeException)
222 {
223     vos::OGuard aGuard(Application::GetSolarMutex());
224     //IAccessibility2 Implementation 2009-----
225     //  return xPara.isValid();
226     if (xPara.isValid())
227     {
228         const SwTxtAttr *pTxtAttr = GetTxtAttr();
229         ::rtl::OUString sText;
230         if( pTxtAttr )
231         {
232             const SwFmtINetFmt& rINetFmt = pTxtAttr->GetINetFmt();
233             sText = OUString( rINetFmt.GetValue() );
234             ::rtl::OUString sToken = ::rtl::OUString::createFromAscii("#");
235             sal_Int32 nPos = sText.indexOf(sToken);
236             if (nPos==0)//document link
237             {
238                 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
239                 if( ! xFactory.is() )
240                     return sal_False;
241                 uno::Reference< com::sun::star::frame::XDesktop > xDesktop( xFactory->createInstance( OUString::createFromAscii( "com.sun.star.frame.Desktop" ) ),
242                     uno::UNO_QUERY );
243                 if( !xDesktop.is() )
244                     return sal_False;
245                 uno::Reference< lang::XComponent > xComp;
246                 xComp = xDesktop->getCurrentComponent();
247                 if( !xComp.is() )
248                     return sal_False;
249                 uno::Reference< com::sun::star::document::XLinkTargetSupplier >  xLTS(xComp, uno::UNO_QUERY);
250                 if ( !xLTS.is())
251                     return sal_False;
252 
253                 uno::Reference< ::com::sun::star::container::XNameAccess > xLinks = xLTS->getLinks();
254                 uno::Reference< ::com::sun::star::container::XNameAccess > xSubLinks;
255                 const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
256                 const sal_uLong nLinks = aNames.getLength();
257                 const OUString* pNames = aNames.getConstArray();
258 
259                 for( sal_uLong i = 0; i < nLinks; i++ )
260                 {
261                     uno::Any aAny;
262                     OUString aLink( *pNames++ );
263                     aAny = xLinks->getByName( aLink );
264                     aAny >>= xSubLinks;
265                     if (xSubLinks->hasByName(sText.copy(1)) )
266                         return sal_True;
267                 }
268             }
269             else//internet
270                 return sal_True;
271         }
272     }//xpara valid
273     return sal_False;
274     //-----IAccessibility2 Implementation 2009
275 }
276 
277 void SwAccessibleHyperlink::Invalidate()
278 {
279     vos::OGuard aGuard(Application::GetSolarMutex());
280     xPara = 0;
281 }
282 
283