LibreOffice Module sw (master)  1
acchyperlink.cxx
Go to the documentation of this file.
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 
22 #include <com/sun/star/frame/XDesktop.hpp>
23 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <swurl.hxx>
27 #include <vcl/keycodes.hxx>
28 #include <vcl/svapp.hxx>
29 #include <txtinet.hxx>
30 #include "accpara.hxx"
31 #include "acchyperlink.hxx"
32 
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::accessibility;
35 using ::com::sun::star::lang::IndexOutOfBoundsException;
36 
38  SwAccessibleParagraph & rAccPara,
39  sal_Int32 const nStt, sal_Int32 const nEnd)
40  : m_pHyperlink(const_cast<SwFormatINetFormat*>(&rTextAttr.GetINetFormat()))
41  , m_xParagraph(&rAccPara)
42  , m_nStartIndex( nStt )
43  , m_nEndIndex( nEnd )
44 {
46 }
47 
49 {
50  Invalidate(); // with SolarMutex!
51 }
52 
53 // when the pool item dies, invalidate! this is the only reason for Listener...
55 {
56  if (rHint.GetId() == SfxHintId::Dying)
57  {
58  Invalidate();
59  }
60 }
61 
62 // both the parent SwAccessibleParagraph and the pool-item must be valid
64 {
65  return (m_xParagraph.is() && m_xParagraph->GetMap())
66  ? m_pHyperlink
67  : nullptr;
68 }
69 
70 // XAccessibleAction
72 {
73  return isValid() ? 1 : 0;
74 }
75 
77 {
78  SolarMutexGuard aGuard;
79 
80  bool bRet = false;
81 
82  if(nIndex != 0)
83  throw lang::IndexOutOfBoundsException();
84  SwFormatINetFormat const*const pINetFormat = GetTextAttr();
85  if (pINetFormat && !pINetFormat->GetValue().isEmpty())
86  {
87  SwViewShell *pVSh = m_xParagraph->GetShell();
88  if (pVSh)
89  {
90  LoadURL(*pVSh, pINetFormat->GetValue(), LoadUrlFlags::NONE,
91  pINetFormat->GetTargetFrame());
92  const SwTextINetFormat *const pTextAttr = pINetFormat->GetTextINetFormat();
93  if (pTextAttr)
94  {
95  const_cast<SwTextINetFormat*>(pTextAttr)->SetVisited(true);
96  const_cast<SwTextINetFormat*>(pTextAttr)->SetVisitedValid(true);
97  }
98  bRet = true;
99  }
100  }
101 
102  return bRet;
103 }
104 
106  sal_Int32 nIndex )
107 {
108  if(nIndex != 0)
109  throw lang::IndexOutOfBoundsException();
110 
111  SolarMutexGuard g;
112  if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
113  {
114  return pINetFormat->GetValue();
115  }
116 
117  return OUString();
118 }
119 
120 uno::Reference< XAccessibleKeyBinding > SAL_CALL
122 {
123  uno::Reference< XAccessibleKeyBinding > xKeyBinding;
124 
125  if( isValid() )
126  {
127  ::comphelper::OAccessibleKeyBindingHelper* pKeyBindingHelper =
128  new ::comphelper::OAccessibleKeyBindingHelper();
129  xKeyBinding = pKeyBindingHelper;
130 
131  awt::KeyStroke aKeyStroke;
132  aKeyStroke.Modifiers = 0;
133  aKeyStroke.KeyCode = KEY_RETURN;
134  aKeyStroke.KeyChar = 0;
135  aKeyStroke.KeyFunc = 0;
136  pKeyBindingHelper->AddKeyBinding( aKeyStroke );
137  }
138 
139  return xKeyBinding;
140 }
141 
142 // XAccessibleHyperlink
144  sal_Int32 nIndex)
145 {
146  SolarMutexGuard g;
147 
148  uno::Any aRet;
149  if(nIndex != 0)
150  throw lang::IndexOutOfBoundsException();
151  OUString text( m_xParagraph->GetString() );
152  OUString retText = text.copy(m_nStartIndex, m_nEndIndex - m_nStartIndex);
153  aRet <<= retText;
154  return aRet;
155 }
156 
158  sal_Int32 nIndex )
159 {
160  SolarMutexGuard g;
161 
162  if(nIndex != 0)
163  throw lang::IndexOutOfBoundsException();
164  OUString retText;
165  if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
166  {
167  retText = pINetFormat->GetValue();
168  }
169  uno::Any aRet;
170  aRet <<= retText;
171  return aRet;
172 }
173 
175 {
176  return m_nStartIndex;
177 }
178 
180 {
181  return m_nEndIndex;
182 }
183 
185 {
186  SolarMutexGuard aGuard;
187  if (m_xParagraph.is())
188  {
189  if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
190  {
191  OUString const sText(pINetFormat->GetValue());
192  OUString sToken = "#";
193  sal_Int32 nPos = sText.indexOf(sToken);
194  if (nPos==0)//document link
195  {
196  uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
197  if( ! xFactory.is() )
198  return false;
199  uno::Reference< css::frame::XDesktop > xDesktop( xFactory->createInstance( "com.sun.star.frame.Desktop" ),
200  uno::UNO_QUERY );
201  if( !xDesktop.is() )
202  return false;
203  uno::Reference< lang::XComponent > xComp = xDesktop->getCurrentComponent();
204  if( !xComp.is() )
205  return false;
206  uno::Reference< css::document::XLinkTargetSupplier > xLTS(xComp, uno::UNO_QUERY);
207  if ( !xLTS.is())
208  return false;
209 
210  uno::Reference< css::container::XNameAccess > xLinks = xLTS->getLinks();
211  uno::Reference< css::container::XNameAccess > xSubLinks;
212  const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
213 
214  for( const OUString& aLink : aNames )
215  {
216  uno::Any aAny = xLinks->getByName( aLink );
217  aAny >>= xSubLinks;
218  if (xSubLinks->hasByName(sText.copy(1)) )
219  return true;
220  }
221  }
222  else//internet
223  return true;
224  }
225  }//xpara valid
226  return false;
227 }
228 
230 {
231  SolarMutexGuard aGuard;
232  m_xParagraph = nullptr;
233  m_pHyperlink = nullptr;
234  EndListeningAll();
235 }
236 
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxHintId GetId() const
void AddKeyBinding(const css::uno::Sequence< css::awt::KeyStroke > &rKeyBinding)
const SwTextINetFormat * GetTextINetFormat() const
Definition: fmtinfmt.hxx:70
void LoadURL(SwViewShell &rVSh, const OUString &rURL, LoadUrlFlags nFilter, const OUString &rTargetFrameName)
Definition: wrtsh2.cxx:491
const OUString & GetValue() const
Definition: fmtinfmt.hxx:75
void EndListeningAll()
unsigned char sal_Bool
bool StartListening(SvtBroadcaster &rBroadcaster)
SvtBroadcaster & GetNotifier()
Definition: calbck.hxx:93
#define KEY_RETURN
Reference< XMultiServiceFactory > getProcessServiceFactory()
const OUString & GetTargetFrame() const
Definition: fmtinfmt.hxx:89
def text(shape, st)
sal_Int32 nPos