Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / tox / ToxLinkProcessor.cxx
blob6d431e9976517869aff84ebdde26bdc5aede74f4
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/.
8 */
10 #include <memory>
11 #include <ToxLinkProcessor.hxx>
13 #include <SwStyleNameMapper.hxx>
14 #include <ndtxt.hxx>
15 #include <sal/log.hxx>
16 #include <rtl/uri.hxx>
18 namespace sw {
20 void
21 ToxLinkProcessor::StartNewLink(sal_Int32 startPosition, const OUString& characterStyle)
23 SAL_INFO_IF(m_oStartedLink, "sw.core", "ToxLinkProcessor: LS without LE");
24 m_oStartedLink.emplace(startPosition, characterStyle);
27 void ToxLinkProcessor::CloseLink(sal_Int32 endPosition, const OUString& url, bool bRelative)
29 if (!m_oStartedLink)
31 SAL_INFO("sw.core", "ToxLinkProcessor: LE without LS");
32 return;
35 if (url.isEmpty()) {
36 return;
39 OUString uri;
40 if (bRelative)
42 // url contains '|' which must be encoded; also in some cases contains
43 // arbitrary strings that need to be encoded
44 assert(url[0] == '#'); // all links are internal
45 uri = "#"
46 + rtl::Uri::encode(url.copy(1), rtl_UriCharClassUricNoSlash,
47 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8);
49 else
51 uri = url;
54 std::unique_ptr<ClosedLink> pClosedLink(
55 new ClosedLink(uri, m_oStartedLink->mStartPosition, endPosition));
57 const OUString& characterStyle = m_oStartedLink->mCharacterStyle;
58 sal_uInt16 poolId = ObtainPoolId(characterStyle);
59 pClosedLink->mINetFormat.SetVisitedFormatAndId(characterStyle, poolId);
60 pClosedLink->mINetFormat.SetINetFormatAndId(characterStyle, poolId);
62 m_ClosedLinks.push_back(std::move(pClosedLink));
63 m_oStartedLink.reset();
66 sal_uInt16
67 ToxLinkProcessor::ObtainPoolId(const OUString& characterStyle) const
69 if (characterStyle.isEmpty()) {
70 return USHRT_MAX;
72 else {
73 return SwStyleNameMapper::GetPoolIdFromUIName(characterStyle, SwGetPoolIdFromName::ChrFmt);
78 void
79 ToxLinkProcessor::InsertLinkAttributes(SwTextNode& node)
81 for (auto const& clink : m_ClosedLinks)
83 node.InsertItem(clink->mINetFormat, clink->mStartTextPos, clink->mEndTextPos);
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */