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