android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / access / acchyperlink.cxx
blob76a2721ec172263bdcbd719c9b5058bfbc7bde9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/accessiblekeybindinghelper.hxx>
21 #include <comphelper/processfactory.hxx>
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"
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::accessibility;
35 using ::com::sun::star::lang::IndexOutOfBoundsException;
37 SwAccessibleHyperlink::SwAccessibleHyperlink(const SwTextAttr & rTextAttr,
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 )
45 StartListening(m_pHyperlink->GetNotifier());
48 SwAccessibleHyperlink::~SwAccessibleHyperlink()
50 Invalidate(); // with SolarMutex!
53 // when the pool item dies, invalidate! this is the only reason for Listener...
54 void SwAccessibleHyperlink::Notify(SfxHint const& rHint)
56 if (rHint.GetId() == SfxHintId::Dying)
58 Invalidate();
62 // both the parent SwAccessibleParagraph and the pool-item must be valid
63 const SwFormatINetFormat *SwAccessibleHyperlink::GetTextAttr() const
65 return (m_xParagraph.is() && m_xParagraph->GetMap())
66 ? m_pHyperlink
67 : nullptr;
70 // XAccessibleAction
71 sal_Int32 SAL_CALL SwAccessibleHyperlink::getAccessibleActionCount()
73 return isValid() ? 1 : 0;
76 sal_Bool SAL_CALL SwAccessibleHyperlink::doAccessibleAction( sal_Int32 nIndex )
78 SolarMutexGuard aGuard;
80 bool bRet = false;
82 if(nIndex != 0)
83 throw lang::IndexOutOfBoundsException();
84 SwFormatINetFormat const*const pINetFormat = GetTextAttr();
85 if (pINetFormat && !pINetFormat->GetValue().isEmpty())
87 SwViewShell *pVSh = m_xParagraph->GetShell();
88 if (pVSh)
90 LoadURL(*pVSh, pINetFormat->GetValue(), LoadUrlFlags::NONE,
91 pINetFormat->GetTargetFrame());
92 const SwTextINetFormat *const pTextAttr = pINetFormat->GetTextINetFormat();
93 if (pTextAttr)
95 const_cast<SwTextINetFormat*>(pTextAttr)->SetVisited(true);
96 const_cast<SwTextINetFormat*>(pTextAttr)->SetVisitedValid(true);
98 bRet = true;
102 return bRet;
105 OUString SAL_CALL SwAccessibleHyperlink::getAccessibleActionDescription(
106 sal_Int32 nIndex )
108 if(nIndex != 0)
109 throw lang::IndexOutOfBoundsException();
111 SolarMutexGuard g;
112 if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
114 return pINetFormat->GetValue();
117 return OUString();
120 uno::Reference< XAccessibleKeyBinding > SAL_CALL
121 SwAccessibleHyperlink::getAccessibleActionKeyBinding( sal_Int32 )
123 uno::Reference< XAccessibleKeyBinding > xKeyBinding;
125 if( isValid() )
127 rtl::Reference<::comphelper::OAccessibleKeyBindingHelper> pKeyBindingHelper =
128 new ::comphelper::OAccessibleKeyBindingHelper();
129 xKeyBinding = pKeyBindingHelper;
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 );
139 return xKeyBinding;
142 // XAccessibleHyperlink
143 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionAnchor(
144 sal_Int32 nIndex)
146 SolarMutexGuard g;
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;
157 uno::Any SAL_CALL SwAccessibleHyperlink::getAccessibleActionObject(
158 sal_Int32 nIndex )
160 SolarMutexGuard g;
162 if(nIndex != 0)
163 throw lang::IndexOutOfBoundsException();
164 OUString retText;
165 if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
167 retText = pINetFormat->GetValue();
169 uno::Any aRet;
170 aRet <<= retText;
171 return aRet;
174 sal_Int32 SAL_CALL SwAccessibleHyperlink::getStartIndex()
176 return m_nStartIndex;
179 sal_Int32 SAL_CALL SwAccessibleHyperlink::getEndIndex()
181 return m_nEndIndex;
184 sal_Bool SAL_CALL SwAccessibleHyperlink::isValid( )
186 SolarMutexGuard aGuard;
187 if (m_xParagraph.is())
189 if (SwFormatINetFormat const*const pINetFormat = GetTextAttr())
191 OUString const sText(pINetFormat->GetValue());
192 sal_Int32 nPos = sText.indexOf("#");
193 if (nPos==0)//document link
195 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
196 if( ! xFactory.is() )
197 return false;
198 uno::Reference< css::frame::XDesktop > xDesktop( xFactory->createInstance( "com.sun.star.frame.Desktop" ),
199 uno::UNO_QUERY );
200 if( !xDesktop.is() )
201 return false;
202 uno::Reference< lang::XComponent > xComp = xDesktop->getCurrentComponent();
203 if( !xComp.is() )
204 return false;
205 uno::Reference< css::document::XLinkTargetSupplier > xLTS(xComp, uno::UNO_QUERY);
206 if ( !xLTS.is())
207 return false;
209 uno::Reference< css::container::XNameAccess > xLinks = xLTS->getLinks();
210 uno::Reference< css::container::XNameAccess > xSubLinks;
211 const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
213 for( const OUString& aLink : aNames )
215 uno::Any aAny = xLinks->getByName( aLink );
216 aAny >>= xSubLinks;
217 if (xSubLinks->hasByName(sText.copy(1)) )
218 return true;
221 else//internet
222 return true;
224 }//xpara valid
225 return false;
228 void SwAccessibleHyperlink::Invalidate()
230 SolarMutexGuard aGuard;
231 m_xParagraph = nullptr;
232 m_pHyperlink = nullptr;
233 EndListeningAll();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */