LibreOffice Module cui (master)  1
hlinettp.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 
20 #include <o3tl/string_view.hxx>
21 #include <unotools/useroptions.hxx>
22 #include <svl/adrparse.hxx>
23 
24 #include <hlinettp.hxx>
25 #include <hlmarkwn_def.hxx>
26 
27 constexpr OUStringLiteral sAnonymous = u"anonymous";
28 
29 /*************************************************************************
30 |*
31 |* Constructor / Destructor
32 |*
33 |************************************************************************/
35  SvxHpLinkDlg* pDlg,
36  const SfxItemSet* pItemSet)
37  : SvxHyperlinkTabPageBase(pParent, pDlg, "cui/ui/hyperlinkinternetpage.ui", "HyperlinkInternetPage",
38  pItemSet)
39  , m_bMarkWndOpen(false)
40  , m_xRbtLinktypInternet(xBuilder->weld_radio_button("linktyp_internet"))
41  , m_xRbtLinktypFTP(xBuilder->weld_radio_button("linktyp_ftp"))
42  , m_xCbbTarget(new SvxHyperURLBox(xBuilder->weld_combo_box("target")))
43  , m_xFtTarget(xBuilder->weld_label("target_label"))
44  , m_xFtLogin(xBuilder->weld_label("login_label"))
45  , m_xEdLogin(xBuilder->weld_entry("login"))
46  , m_xFtPassword(xBuilder->weld_label("password_label"))
47  , m_xEdPassword(xBuilder->weld_entry("password"))
48  , m_xCbAnonymous(xBuilder->weld_check_button("anonymous"))
49 {
50  // gtk_size_group_set_ignore_hidden, "Measuring the size of hidden widgets
51  // ... they will report a size of 0 nowadays, and thus, their size will
52  // not affect the other size group members", which is unfortunate. So here
53  // before we hide the labels, take the size group width and set it as
54  // explicit preferred size on a label that won't be hidden
55  auto nLabelWidth = m_xFtTarget->get_preferred_size().Width();
56  m_xFtTarget->set_size_request(nLabelWidth, -1);
57 
58  m_xCbbTarget->SetSmartProtocol(INetProtocol::Http);
59 
61 
62  m_xCbbTarget->show();
63 
65 
66  // set defaults
67  m_xRbtLinktypInternet->set_active(true);
68 
69  // set handlers
70  Link<weld::Toggleable&, void> aLink( LINK ( this, SvxHyperlinkInternetTp, Click_SmartProtocol_Impl ) );
71  m_xRbtLinktypInternet->connect_toggled( aLink );
72  m_xRbtLinktypFTP->connect_toggled( aLink );
73  m_xCbAnonymous->connect_toggled( LINK ( this, SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl ) );
74  m_xEdLogin->connect_changed( LINK ( this, SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl ) );
75  m_xCbbTarget->connect_focus_out( LINK ( this, SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl ) );
76  m_xCbbTarget->connect_changed( LINK ( this, SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl ) );
77  maTimer.SetInvokeHandler ( LINK ( this, SvxHyperlinkInternetTp, TimeoutHdl_Impl ) );
78 }
79 
81 {
82 }
83 
84 /*************************************************************************
85 |*
86 |* Fill the all dialog-controls except controls in groupbox "more..."
87 |*
88 |************************************************************************/
89 void SvxHyperlinkInternetTp::FillDlgFields(const OUString& rStrURL)
90 {
91  INetURLObject aURL(rStrURL);
92  OUString aStrScheme(GetSchemeFromURL(rStrURL));
93 
94  // set additional controls for FTP: Username / Password
95  if (aStrScheme.startsWith(INET_FTP_SCHEME))
96  {
97  if ( aURL.GetUser().toAsciiLowerCase().startsWith( sAnonymous ) )
99  else
100  setFTPUser(aURL.GetUser(), aURL.GetPass());
101 
102  //do not show password and user in url
103  if(!aURL.GetUser().isEmpty() || !aURL.GetPass().isEmpty() )
104  aURL.SetUserAndPass(u"", u"");
105  }
106 
107  // set URL-field
108  // Show the scheme, #72740
109  if ( aURL.GetProtocol() != INetProtocol::NotValid )
111  else
112  m_xCbbTarget->set_entry_text(rStrURL);
113 
114  SetScheme(aStrScheme);
115 }
116 
118 {
119  m_xEdLogin->set_text(sAnonymous);
120  SvAddressParser aAddress(SvtUserOptions().GetEmail());
121  m_xEdPassword->set_text(aAddress.Count() ? aAddress.GetEmailAddress(0) : OUString());
122 
123  m_xFtLogin->set_sensitive(false);
124  m_xFtPassword->set_sensitive(false);
125  m_xEdLogin->set_sensitive(false);
126  m_xEdPassword->set_sensitive(false);
127  m_xCbAnonymous->set_active(true);
128 }
129 
130 void SvxHyperlinkInternetTp::setFTPUser(const OUString& rUser, const OUString& rPassword)
131 {
132  m_xEdLogin->set_text(rUser);
133  m_xEdPassword->set_text(rPassword);
134 
135  m_xFtLogin->set_sensitive(true);
136  m_xFtPassword->set_sensitive(true);
137  m_xEdLogin->set_sensitive(true);
138  m_xEdPassword->set_sensitive(true);
139  m_xCbAnonymous->set_active(false);
140 }
141 
142 /*************************************************************************
143 |*
144 |* retrieve and prepare data from dialog-fields
145 |*
146 |************************************************************************/
147 
148 void SvxHyperlinkInternetTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
149  OUString& aStrIntName, OUString& aStrFrame,
150  SvxLinkInsertMode& eMode )
151 {
152  rStrURL = CreateAbsoluteURL();
153  GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
154 }
155 
157 {
158  // erase leading and trailing whitespaces
159  OUString aStrURL(m_xCbbTarget->get_active_text().trim());
160 
162 
163  // username and password for ftp-url
164  if( aURL.GetProtocol() == INetProtocol::Ftp && !m_xEdLogin->get_text().isEmpty() )
165  aURL.SetUserAndPass ( m_xEdLogin->get_text(), m_xEdPassword->get_text() );
166 
167  if ( aURL.GetProtocol() != INetProtocol::NotValid )
168  return aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
169  else //#105788# always create a URL even if it is not valid
170  return aStrURL;
171 }
172 
173 /*************************************************************************
174 |*
175 |* static method to create Tabpage
176 |*
177 |************************************************************************/
178 
179 std::unique_ptr<IconChoicePage> SvxHyperlinkInternetTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
180 {
181  return std::make_unique<SvxHyperlinkInternetTp>(pWindow, pDlg, pItemSet);
182 }
183 
184 /*************************************************************************
185 |*
186 |* Set initial focus
187 |*
188 |************************************************************************/
190 {
191  m_xCbbTarget->grab_focus();
192 }
193 
194 /*************************************************************************
195 |*
196 |* Contents of editfield "Target" modified
197 |*
198 |************************************************************************/
199 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox&, void)
200 {
201  OUString aScheme = GetSchemeFromURL( m_xCbbTarget->get_active_text() );
202  if( !aScheme.isEmpty() )
203  SetScheme( aScheme );
204 
205  // start timer
206  maTimer.SetTimeout( 2500 );
207  maTimer.Start();
208 }
209 
210 /*************************************************************************
211 |*
212 |* If target-field was modify, to browse the new doc after timeout
213 |*
214 |************************************************************************/
215 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, void)
216 {
217  RefreshMarkWindow();
218 }
219 
220 /*************************************************************************
221 |*
222 |* Contents of editfield "Login" modified
223 |*
224 |************************************************************************/
225 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, weld::Entry&, void)
226 {
227  OUString aStrLogin ( m_xEdLogin->get_text() );
228  if ( aStrLogin.equalsIgnoreAsciiCase( sAnonymous ) )
229  {
230  m_xCbAnonymous->set_active(true);
231  ClickAnonymousHdl_Impl(*m_xCbAnonymous);
232  }
233 }
234 
235 void SvxHyperlinkInternetTp::SetScheme(std::u16string_view rScheme)
236 {
237  //if rScheme is empty or unknown the default behaviour is like it where HTTP
238  bool bFTP = o3tl::starts_with(rScheme, INET_FTP_SCHEME);
239  bool bInternet = !bFTP;
240 
241  //update protocol button selection:
242  m_xRbtLinktypFTP->set_active(bFTP);
243  m_xRbtLinktypInternet->set_active(bInternet);
244 
245  //update target:
246  RemoveImproperProtocol(rScheme);
247  m_xCbbTarget->SetSmartProtocol( GetSmartProtocolFromButtons() );
248 
249  //show/hide special fields for FTP:
250  m_xFtLogin->set_visible( bFTP );
251  m_xFtPassword->set_visible( bFTP );
252  m_xEdLogin->set_visible( bFTP );
253  m_xEdPassword->set_visible( bFTP );
254  m_xCbAnonymous->set_visible( bFTP );
255 
256  //update 'link target in document'-window and opening-button
257  if (o3tl::starts_with(rScheme, INET_HTTP_SCHEME) || rScheme.empty())
258  {
259  if ( m_bMarkWndOpen )
260  ShowMarkWnd ();
261  }
262  else
263  {
264  //disable for https and ftp
265  if ( m_bMarkWndOpen )
266  HideMarkWnd ();
267  }
268 }
269 
270 /*************************************************************************
271 |*
272 |* Remove protocol if it does not fit to the current button selection
273 |*
274 |************************************************************************/
275 
276 void SvxHyperlinkInternetTp::RemoveImproperProtocol(std::u16string_view aProperScheme)
277 {
278  OUString aStrURL ( m_xCbbTarget->get_active_text() );
279  if ( !aStrURL.isEmpty() )
280  {
281  OUString aStrScheme(GetSchemeFromURL(aStrURL));
282  if ( !aStrScheme.isEmpty() && aStrScheme != aProperScheme )
283  {
284  aStrURL = aStrURL.copy( aStrScheme.getLength() );
285  m_xCbbTarget->set_entry_text( aStrURL );
286  }
287  }
288 }
289 
291 {
292  if( m_xRbtLinktypFTP->get_active() )
293  return INET_FTP_SCHEME;
294  return INET_HTTP_SCHEME;
295 }
296 
298 {
299  if( m_xRbtLinktypFTP->get_active() )
300  {
301  return INetProtocol::Ftp;
302  }
303  return INetProtocol::Http;
304 }
305 
306 /*************************************************************************
307 |*
308 |* Click on Radiobutton : Internet or FTP
309 |*
310 |************************************************************************/
311 IMPL_LINK(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Toggleable&, rButton, void)
312 {
313  if (!rButton.get_active())
314  return;
315  OUString aScheme = GetSchemeFromButtons();
316  SetScheme(aScheme);
317 }
318 
319 /*************************************************************************
320 |*
321 |* Click on Checkbox : Anonymous user
322 |*
323 |************************************************************************/
325 {
326  // disable login-editfields if checked
327  if ( m_xCbAnonymous->get_active() )
328  {
329  if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
330  {
331  maStrOldUser.clear();
332  maStrOldPassword.clear();
333  }
334  else
335  {
336  maStrOldUser = m_xEdLogin->get_text();
337  maStrOldPassword = m_xEdPassword->get_text();
338  }
339 
340  setAnonymousFTPUser();
341  }
342  else
343  setFTPUser(maStrOldUser, maStrOldPassword);
344 }
345 
346 /*************************************************************************
347 |*
348 |* Combobox Target lost the focus
349 |*
350 |************************************************************************/
351 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
352 {
353  RefreshMarkWindow();
354 }
355 
357 {
358  if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
359  {
360  weld::WaitObject aWait(mpDialog->getDialog());
361  OUString aStrURL( CreateAbsoluteURL() );
362  if ( !aStrURL.isEmpty() )
363  mxMarkWnd->RefreshTree ( aStrURL );
364  else
365  mxMarkWnd->SetError( LERR_DOCNOTOPEN );
366  }
367 }
368 
369 /*************************************************************************
370 |*
371 |* Get String from Bookmark-Wnd
372 |*
373 |************************************************************************/
374 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
375 {
376  OUString aStrURL(m_xCbbTarget->get_active_text());
377 
378  const sal_Unicode sUHash = '#';
379  sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );
380 
381  if( nPos != -1 )
382  aStrURL = aStrURL.copy(0, nPos);
383 
384  aStrURL += OUStringChar(sUHash) + aStrMark;
385 
386  m_xCbbTarget->set_entry_text(aStrURL);
387 }
388 
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral INET_FTP_SCHEME
URL aURL
std::unique_ptr< weld::Label > m_xFtLogin
Definition: hlinettp.hxx:46
void setFTPUser(const OUString &rUser, const OUString &rPassword)
Definition: hlinettp.cxx:130
const OUString & GetEmailAddress(sal_Int32 nIndex) const
std::unique_ptr< weld::Label > m_xFtPassword
Definition: hlinettp.hxx:48
std::unique_ptr< SvxHyperURLBox > m_xCbbTarget
Definition: hlinettp.hxx:44
static std::unique_ptr< IconChoicePage > Create(weld::Container *pWindow, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hlinettp.cxx:179
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
#define LERR_DOCNOTOPEN
sal_uInt16 sal_Unicode
void RemoveImproperProtocol(std::u16string_view rProperScheme)
Definition: hlinettp.cxx:276
OUString GetUser(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
void GetDataFromCommonFields(OUString &aStrName, OUString &aStrIntName, OUString &aStrFrame, SvxLinkInsertMode &eMode)
Definition: hltpbase.cxx:422
INetProtocol
virtual void SetMarkStr(const OUString &aStrMark) override
Definition: hlinettp.cxx:374
std::shared_ptr< SvxHlinkDlgMarkWnd > mxMarkWnd
Definition: hltpbase.hxx:73
constexpr OUStringLiteral INET_HTTP_SCHEME
INetProtocol GetSmartProtocolFromButtons() const
Definition: hlinettp.cxx:297
std::unique_ptr< weld::Label > m_xFtTarget
Definition: hlinettp.hxx:45
OUString CreateAbsoluteURL() const
Definition: hlinettp.cxx:156
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox &, void)
Definition: hlinettp.cxx:199
IMPL_LINK(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Toggleable &, rButton, void)
Definition: hlinettp.cxx:311
SvxLinkInsertMode
float u
SvxHpLinkDlg * mpDialog
Definition: hltpbase.hxx:63
void SetScheme(std::u16string_view rScheme)
Definition: hlinettp.cxx:235
std::unique_ptr< weld::Entry > m_xEdLogin
Definition: hlinettp.hxx:47
std::unique_ptr< weld::RadioButton > m_xRbtLinktypInternet
Definition: hlinettp.hxx:42
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
void SetExchangeSupport()
Definition: iconcdlg.hxx:75
std::unique_ptr< weld::RadioButton > m_xRbtLinktypFTP
Definition: hlinettp.hxx:43
virtual void FillDlgFields(const OUString &rStrURL) override
Definition: hlinettp.cxx:89
INetProtocol GetProtocol() const
constexpr OUStringLiteral sAnonymous
Definition: hlinettp.cxx:27
sal_Int32 Count() const
virtual void SetInitFocus() override
Definition: hlinettp.cxx:189
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void GetCurentItemData(OUString &rStrURL, OUString &aStrName, OUString &aStrIntName, OUString &aStrFrame, SvxLinkInsertMode &eMode) override
Definition: hlinettp.cxx:148
ComboBox-Control for URL's with History and Autocompletion.
Definition: hltpbase.hxx:35
SvxHyperlinkInternetTp(weld::Container *pParent, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hlinettp.cxx:34
std::unique_ptr< weld::CheckButton > m_xCbAnonymous
Definition: hlinettp.hxx:50
bool IsMarkWndVisible() const
Definition: hltpbase.hxx:116
bool SetUserAndPass(std::u16string_view rTheUser, std::u16string_view rThePassword)
OUString GetSchemeFromButtons() const
Definition: hlinettp.cxx:290
OUString GetPass(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
Tabpage : Basisclass.
Definition: hltpbase.hxx:46
static OUString GetSchemeFromURL(const OUString &rStrURL)
Definition: hltpbase.cxx:389
std::unique_ptr< weld::Entry > m_xEdPassword
Definition: hlinettp.hxx:49
sal_uInt16 nPos
virtual ~SvxHyperlinkInternetTp() override
Definition: hlinettp.cxx:80
bool m_bDetectedRangeSegmentation false