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 
161  INetURLObject aURL(aStrURL);
162 
163  if( aURL.GetProtocol() == INetProtocol::NotValid )
164  {
165  aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
166  aURL.SetSmartURL(aStrURL);
167  }
168 
169  // username and password for ftp-url
170  if( aURL.GetProtocol() == INetProtocol::Ftp && !m_xEdLogin->get_text().isEmpty() )
171  aURL.SetUserAndPass ( m_xEdLogin->get_text(), m_xEdPassword->get_text() );
172 
173  if ( aURL.GetProtocol() != INetProtocol::NotValid )
174  return aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
175  else //#105788# always create a URL even if it is not valid
176  return aStrURL;
177 }
178 
179 /*************************************************************************
180 |*
181 |* static method to create Tabpage
182 |*
183 |************************************************************************/
184 
185 std::unique_ptr<IconChoicePage> SvxHyperlinkInternetTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
186 {
187  return std::make_unique<SvxHyperlinkInternetTp>(pWindow, pDlg, pItemSet);
188 }
189 
190 /*************************************************************************
191 |*
192 |* Set initial focus
193 |*
194 |************************************************************************/
196 {
197  m_xCbbTarget->grab_focus();
198 }
199 
200 /*************************************************************************
201 |*
202 |* Contents of editfield "Target" modified
203 |*
204 |************************************************************************/
205 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox&, void)
206 {
207  OUString aScheme = GetSchemeFromURL( m_xCbbTarget->get_active_text() );
208  if( !aScheme.isEmpty() )
209  SetScheme( aScheme );
210 
211  // start timer
212  maTimer.SetTimeout( 2500 );
213  maTimer.Start();
214 }
215 
216 /*************************************************************************
217 |*
218 |* If target-field was modify, to browse the new doc after timeout
219 |*
220 |************************************************************************/
221 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, void)
222 {
223  RefreshMarkWindow();
224 }
225 
226 /*************************************************************************
227 |*
228 |* Contents of editfield "Login" modified
229 |*
230 |************************************************************************/
231 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, weld::Entry&, void)
232 {
233  OUString aStrLogin ( m_xEdLogin->get_text() );
234  if ( aStrLogin.equalsIgnoreAsciiCase( sAnonymous ) )
235  {
236  m_xCbAnonymous->set_active(true);
237  ClickAnonymousHdl_Impl(*m_xCbAnonymous);
238  }
239 }
240 
241 void SvxHyperlinkInternetTp::SetScheme(std::u16string_view rScheme)
242 {
243  //if rScheme is empty or unknown the default behaviour is like it where HTTP
244  bool bFTP = o3tl::starts_with(rScheme, u"" INET_FTP_SCHEME);
245  bool bInternet = !bFTP;
246 
247  //update protocol button selection:
248  m_xRbtLinktypFTP->set_active(bFTP);
249  m_xRbtLinktypInternet->set_active(bInternet);
250 
251  //update target:
252  RemoveImproperProtocol(rScheme);
253  m_xCbbTarget->SetSmartProtocol( GetSmartProtocolFromButtons() );
254 
255  //show/hide special fields for FTP:
256  m_xFtLogin->set_visible( bFTP );
257  m_xFtPassword->set_visible( bFTP );
258  m_xEdLogin->set_visible( bFTP );
259  m_xEdPassword->set_visible( bFTP );
260  m_xCbAnonymous->set_visible( bFTP );
261 
262  //update 'link target in document'-window and opening-button
263  if (o3tl::starts_with(rScheme, u"" INET_HTTP_SCHEME) || rScheme.empty())
264  {
265  if ( m_bMarkWndOpen )
266  ShowMarkWnd ();
267  }
268  else
269  {
270  //disable for https and ftp
271  if ( m_bMarkWndOpen )
272  HideMarkWnd ();
273  }
274 }
275 
276 /*************************************************************************
277 |*
278 |* Remove protocol if it does not fit to the current button selection
279 |*
280 |************************************************************************/
281 
282 void SvxHyperlinkInternetTp::RemoveImproperProtocol(std::u16string_view aProperScheme)
283 {
284  OUString aStrURL ( m_xCbbTarget->get_active_text() );
285  if ( !aStrURL.isEmpty() )
286  {
287  OUString aStrScheme(GetSchemeFromURL(aStrURL));
288  if ( !aStrScheme.isEmpty() && aStrScheme != aProperScheme )
289  {
290  aStrURL = aStrURL.copy( aStrScheme.getLength() );
291  m_xCbbTarget->set_entry_text( aStrURL );
292  }
293  }
294 }
295 
297 {
298  if( m_xRbtLinktypFTP->get_active() )
299  return INET_FTP_SCHEME;
300  return INET_HTTP_SCHEME;
301 }
302 
304 {
305  if( m_xRbtLinktypFTP->get_active() )
306  {
307  return INetProtocol::Ftp;
308  }
309  return INetProtocol::Http;
310 }
311 
312 /*************************************************************************
313 |*
314 |* Click on Radiobutton : Internet or FTP
315 |*
316 |************************************************************************/
317 IMPL_LINK(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Toggleable&, rButton, void)
318 {
319  if (!rButton.get_active())
320  return;
321  OUString aScheme = GetSchemeFromButtons();
322  SetScheme(aScheme);
323 }
324 
325 /*************************************************************************
326 |*
327 |* Click on Checkbox : Anonymous user
328 |*
329 |************************************************************************/
331 {
332  // disable login-editfields if checked
333  if ( m_xCbAnonymous->get_active() )
334  {
335  if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
336  {
337  maStrOldUser.clear();
338  maStrOldPassword.clear();
339  }
340  else
341  {
342  maStrOldUser = m_xEdLogin->get_text();
343  maStrOldPassword = m_xEdPassword->get_text();
344  }
345 
346  setAnonymousFTPUser();
347  }
348  else
349  setFTPUser(maStrOldUser, maStrOldPassword);
350 }
351 
352 /*************************************************************************
353 |*
354 |* Combobox Target lost the focus
355 |*
356 |************************************************************************/
357 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
358 {
359  RefreshMarkWindow();
360 }
361 
363 {
364  if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
365  {
366  weld::WaitObject aWait(mpDialog->getDialog());
367  OUString aStrURL( CreateAbsoluteURL() );
368  if ( !aStrURL.isEmpty() )
369  mxMarkWnd->RefreshTree ( aStrURL );
370  else
371  mxMarkWnd->SetError( LERR_DOCNOTOPEN );
372  }
373 }
374 
375 /*************************************************************************
376 |*
377 |* Get String from Bookmark-Wnd
378 |*
379 |************************************************************************/
380 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
381 {
382  OUString aStrURL(m_xCbbTarget->get_active_text());
383 
384  const sal_Unicode sUHash = '#';
385  sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );
386 
387  if( nPos != -1 )
388  aStrURL = aStrURL.copy(0, nPos);
389 
390  aStrURL += OUStringChar(sUHash) + aStrMark;
391 
392  m_xCbbTarget->set_entry_text(aStrURL);
393 }
394 
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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
#define INET_HTTP_SCHEME
static std::unique_ptr< IconChoicePage > Create(weld::Container *pWindow, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hlinettp.cxx:185
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:282
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:429
INetProtocol
virtual void SetMarkStr(const OUString &aStrMark) override
Definition: hlinettp.cxx:380
std::shared_ptr< SvxHlinkDlgMarkWnd > mxMarkWnd
Definition: hltpbase.hxx:73
INetProtocol GetSmartProtocolFromButtons() const
Definition: hlinettp.cxx:303
std::unique_ptr< weld::Label > m_xFtTarget
Definition: hlinettp.hxx:45
OUString CreateAbsoluteURL() const
Definition: hlinettp.cxx:156
#define INET_FTP_SCHEME
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox &, void)
Definition: hlinettp.cxx:205
IMPL_LINK(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Toggleable &, rButton, void)
Definition: hlinettp.cxx:317
SvxLinkInsertMode
float u
SvxHpLinkDlg * mpDialog
Definition: hltpbase.hxx:63
void SetScheme(std::u16string_view rScheme)
Definition: hlinettp.cxx:241
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:74
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:195
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:117
bool SetUserAndPass(std::u16string_view rTheUser, std::u16string_view rThePassword)
OUString GetSchemeFromButtons() const
Definition: hlinettp.cxx:296
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:396
std::unique_ptr< weld::Entry > m_xEdPassword
Definition: hlinettp.hxx:49
sal_uInt16 nPos
virtual ~SvxHyperlinkInternetTp() override
Definition: hlinettp.cxx:80