1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <o3tl/string_view.hxx>
21 #include <unotools/useroptions.hxx>
22 #include <svl/adrparse.hxx>
24 #include <hlinettp.hxx>
25 #include <hlmarkwn_def.hxx>
26 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
29 /*************************************************************************
31 |* Constructor / Destructor
33 |************************************************************************/
34 SvxHyperlinkInternetTp::SvxHyperlinkInternetTp(weld::Container
* pParent
,
36 const SfxItemSet
* pItemSet
)
37 : SvxHyperlinkTabPageBase(pParent
, pDlg
, u
"cui/ui/hyperlinkinternetpage.ui"_ustr
, u
"HyperlinkInternetPage"_ustr
,
39 , m_bMarkWndOpen(false)
40 , m_xCbbTarget(new SvxHyperURLBox(xBuilder
->weld_combo_box(u
"target"_ustr
)))
41 , m_xFtTarget(xBuilder
->weld_label(u
"target_label"_ustr
))
43 // gtk_size_group_set_ignore_hidden, "Measuring the size of hidden widgets
44 // ... they will report a size of 0 nowadays, and thus, their size will
45 // not affect the other size group members", which is unfortunate. So here
46 // before we hide the labels, take the size group width and set it as
47 // explicit preferred size on a label that won't be hidden
48 auto nLabelWidth
= m_xFtTarget
->get_preferred_size().Width();
49 m_xFtTarget
->set_size_request(nLabelWidth
, -1);
51 m_xCbbTarget
->SetSmartProtocol(INetProtocol::Http
);
57 SetExchangeSupport ();
60 m_xCbbTarget
->connect_focus_out( LINK ( this, SvxHyperlinkInternetTp
, LostFocusTargetHdl_Impl
) );
61 m_xCbbTarget
->connect_changed( LINK ( this, SvxHyperlinkInternetTp
, ModifiedTargetHdl_Impl
) );
62 maTimer
.SetInvokeHandler ( LINK ( this, SvxHyperlinkInternetTp
, TimeoutHdl_Impl
) );
65 SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp()
69 /*************************************************************************
71 |* Fill the all dialog-controls except controls in groupbox "more..."
73 |************************************************************************/
74 void SvxHyperlinkInternetTp::FillDlgFields(const OUString
& rStrURL
)
76 // tdf#146576 - propose clipboard content when inserting a hyperlink
77 OUString
aStrURL(rStrURL
);
78 if (aStrURL
.isEmpty())
80 if (auto xClipboard
= GetSystemClipboard())
82 if (auto xTransferable
= xClipboard
->getContents())
84 css::datatransfer::DataFlavor aFlavor
;
85 SotExchange::GetFormatDataFlavor(SotClipboardFormatId::STRING
, aFlavor
);
86 if (xTransferable
->isDataFlavorSupported(aFlavor
))
88 OUString aClipBoardContent
;
91 if (xTransferable
->getTransferData(aFlavor
) >>= aClipBoardContent
)
93 // tdf#162753 - allow only syntactically valid hyperlink targets
94 INetURLObject
aURL(o3tl::trim(aClipBoardContent
));
97 = aURL
.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous
);
100 // tdf#158345: Opening Hyperlink dialog leads to crash
101 // MimeType = "text/plain;charset=utf-16"
102 catch (const css::datatransfer::UnsupportedFlavorException
&)
110 INetURLObject
aURL(aStrURL
);
111 OUString
aStrScheme(GetSchemeFromURL(aStrURL
));
114 // Show the scheme, #72740
115 if ( aURL
.GetProtocol() != INetProtocol::NotValid
)
116 m_xCbbTarget
->set_entry_text( aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
) );
118 m_xCbbTarget
->set_entry_text(rStrURL
);
120 SetScheme(aStrScheme
);
123 /*************************************************************************
125 |* retrieve and prepare data from dialog-fields
127 |************************************************************************/
129 void SvxHyperlinkInternetTp::GetCurrentItemData ( OUString
& rStrURL
, OUString
& aStrName
,
130 OUString
& aStrIntName
, OUString
& aStrFrame
,
131 SvxLinkInsertMode
& eMode
)
133 rStrURL
= CreateAbsoluteURL();
134 GetDataFromCommonFields( aStrName
, aStrIntName
, aStrFrame
, eMode
);
137 OUString
SvxHyperlinkInternetTp::CreateAbsoluteURL() const
139 // erase leading and trailing whitespaces
140 OUString
aStrURL(m_xCbbTarget
->get_active_text().trim());
142 INetURLObject
aURL(aStrURL
, GetSmartProtocolFromButtons());
144 if ( aURL
.GetProtocol() != INetProtocol::NotValid
)
145 return aURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
);
146 else //#105788# always create a URL even if it is not valid
150 /*************************************************************************
152 |* static method to create Tabpage
154 |************************************************************************/
156 std::unique_ptr
<IconChoicePage
> SvxHyperlinkInternetTp::Create(weld::Container
* pWindow
, SvxHpLinkDlg
* pDlg
, const SfxItemSet
* pItemSet
)
158 return std::make_unique
<SvxHyperlinkInternetTp
>(pWindow
, pDlg
, pItemSet
);
161 /*************************************************************************
165 |************************************************************************/
166 void SvxHyperlinkInternetTp::SetInitFocus()
168 m_xCbbTarget
->grab_focus();
171 /*************************************************************************
173 |* Contents of editfield "Target" modified
175 |************************************************************************/
176 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, ModifiedTargetHdl_Impl
, weld::ComboBox
&, void)
178 OUString aScheme
= GetSchemeFromURL( m_xCbbTarget
->get_active_text() );
179 if( !aScheme
.isEmpty() )
180 SetScheme( aScheme
);
183 maTimer
.SetTimeout( 2500 );
187 /*************************************************************************
189 |* If target-field was modify, to browse the new doc after timeout
191 |************************************************************************/
192 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, TimeoutHdl_Impl
, Timer
*, void)
197 void SvxHyperlinkInternetTp::SetScheme(std::u16string_view rScheme
)
200 RemoveImproperProtocol(rScheme
);
201 m_xCbbTarget
->SetSmartProtocol( GetSmartProtocolFromButtons() );
203 //update 'link target in document'-window and opening-button
204 if (o3tl::starts_with(rScheme
, INET_HTTP_SCHEME
) || rScheme
.empty())
206 if ( m_bMarkWndOpen
)
211 //disable for https and ftp
212 if ( m_bMarkWndOpen
)
217 /*************************************************************************
219 |* Remove protocol if it does not fit to the current button selection
221 |************************************************************************/
223 void SvxHyperlinkInternetTp::RemoveImproperProtocol(std::u16string_view aProperScheme
)
225 OUString
aStrURL ( m_xCbbTarget
->get_active_text() );
226 if ( !aStrURL
.isEmpty() )
228 OUString
aStrScheme(GetSchemeFromURL(aStrURL
));
229 if ( !aStrScheme
.isEmpty() && aStrScheme
!= aProperScheme
)
231 aStrURL
= aStrURL
.copy( aStrScheme
.getLength() );
232 m_xCbbTarget
->set_entry_text( aStrURL
);
237 INetProtocol
SvxHyperlinkInternetTp::GetSmartProtocolFromButtons()
239 return INetProtocol::Http
;
242 /*************************************************************************
244 |* Combobox Target lost the focus
246 |************************************************************************/
247 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, LostFocusTargetHdl_Impl
, weld::Widget
&, void)
252 void SvxHyperlinkInternetTp::RefreshMarkWindow()
254 if (IsMarkWndVisible())
256 weld::WaitObject
aWait(mpDialog
->getDialog());
257 OUString
aStrURL( CreateAbsoluteURL() );
258 if ( !aStrURL
.isEmpty() )
259 mxMarkWnd
->RefreshTree ( aStrURL
);
261 mxMarkWnd
->SetError( LERR_DOCNOTOPEN
);
265 /*************************************************************************
267 |* Get String from Bookmark-Wnd
269 |************************************************************************/
270 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString
& aStrMark
)
272 OUString
aStrURL(m_xCbbTarget
->get_active_text());
274 const sal_Unicode sUHash
= '#';
275 sal_Int32 nPos
= aStrURL
.lastIndexOf( sUHash
);
278 aStrURL
= aStrURL
.copy(0, nPos
);
280 aStrURL
+= OUStringChar(sUHash
) + aStrMark
;
282 m_xCbbTarget
->set_entry_text(aStrURL
);
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */