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 <unotools/useroptions.hxx>
21 #include <svl/adrparse.hxx>
22 
23 #include <hlinettp.hxx>
24 #include <hlmarkwn_def.hxx>
25 
26 char const sAnonymous[] = "anonymous";
27 char const sFTPScheme[] = INET_FTP_SCHEME;
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::Button&, void> aLink( LINK ( this, SvxHyperlinkInternetTp, Click_SmartProtocol_Impl ) );
71  m_xRbtLinktypInternet->connect_clicked( aLink );
72  m_xRbtLinktypFTP->connect_clicked( aLink );
73  m_xCbAnonymous->connect_clicked( 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(sFTPScheme))
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("", "");
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(const OUString& rScheme)
242 {
243  //if rScheme is empty or unknown the default behaviour is like it where HTTP
244  bool bFTP = rScheme.startsWith(sFTPScheme);
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 (rScheme.startsWith(INET_HTTP_SCHEME) || rScheme.isEmpty())
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(const OUString& 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_NOARG(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Button&, void)
318 {
319  OUString aScheme = GetSchemeFromButtons();
320  SetScheme(aScheme);
321 }
322 
323 /*************************************************************************
324 |*
325 |* Click on Checkbox : Anonymous user
326 |*
327 |************************************************************************/
328 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl, weld::Button&, void)
329 {
330  // disable login-editfields if checked
331  if ( m_xCbAnonymous->get_active() )
332  {
333  if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
334  {
335  maStrOldUser.clear();
336  maStrOldPassword.clear();
337  }
338  else
339  {
340  maStrOldUser = m_xEdLogin->get_text();
341  maStrOldPassword = m_xEdPassword->get_text();
342  }
343 
344  setAnonymousFTPUser();
345  }
346  else
347  setFTPUser(maStrOldUser, maStrOldPassword);
348 }
349 
350 /*************************************************************************
351 |*
352 |* Combobox Target lost the focus
353 |*
354 |************************************************************************/
355 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
356 {
357  RefreshMarkWindow();
358 }
359 
361 {
362  if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
363  {
364  weld::WaitObject aWait(mpDialog->getDialog());
365  OUString aStrURL( CreateAbsoluteURL() );
366  if ( !aStrURL.isEmpty() )
367  mxMarkWnd->RefreshTree ( aStrURL );
368  else
369  mxMarkWnd->SetError( LERR_DOCNOTOPEN );
370  }
371 }
372 
373 /*************************************************************************
374 |*
375 |* Get String from Bookmark-Wnd
376 |*
377 |************************************************************************/
378 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
379 {
380  OUString aStrURL(m_xCbbTarget->get_active_text());
381 
382  const sal_Unicode sUHash = '#';
383  sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );
384 
385  if( nPos != -1 )
386  aStrURL = aStrURL.copy(0, nPos);
387 
388  aStrURL += OUStringChar(sUHash) + aStrMark;
389 
390  m_xCbbTarget->set_entry_text(aStrURL);
391 }
392 
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
std::unique_ptr< weld::Label > m_xFtLogin
Definition: hlinettp.hxx:43
void RemoveImproperProtocol(const OUString &rProperScheme)
Definition: hlinettp.cxx:282
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:45
std::unique_ptr< SvxHyperURLBox > m_xCbbTarget
Definition: hlinettp.hxx:41
#define INET_HTTP_SCHEME
static std::unique_ptr< IconChoicePage > Create(weld::Container *pWindow, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hlinettp.cxx:185
void SetScheme(const OUString &rScheme)
Definition: hlinettp.cxx:241
#define LERR_DOCNOTOPEN
sal_uInt16 sal_Unicode
OUString GetUser(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
char const sFTPScheme[]
Definition: hlinettp.cxx:27
void GetDataFromCommonFields(OUString &aStrName, OUString &aStrIntName, OUString &aStrFrame, SvxLinkInsertMode &eMode)
Definition: hltpbase.cxx:430
INetProtocol
virtual void SetMarkStr(const OUString &aStrMark) override
Definition: hlinettp.cxx:378
std::shared_ptr< SvxHlinkDlgMarkWnd > mxMarkWnd
Definition: hltpbase.hxx:74
char const sAnonymous[]
Definition: hlinettp.cxx:26
INetProtocol GetSmartProtocolFromButtons() const
Definition: hlinettp.cxx:303
bool SetUserAndPass(OUString const &rTheUser, OUString const &rThePassword)
std::unique_ptr< weld::Label > m_xFtTarget
Definition: hlinettp.hxx:42
OUString CreateAbsoluteURL() const
Definition: hlinettp.cxx:156
#define INET_FTP_SCHEME
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox &, void)
Definition: hlinettp.cxx:205
SvxLinkInsertMode
SvxHpLinkDlg * mpDialog
Definition: hltpbase.hxx:64
std::unique_ptr< weld::Entry > m_xEdLogin
Definition: hlinettp.hxx:44
std::unique_ptr< weld::RadioButton > m_xRbtLinktypInternet
Definition: hlinettp.hxx:39
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:40
virtual void FillDlgFields(const OUString &rStrURL) override
Definition: hlinettp.cxx:89
INetProtocol GetProtocol() const
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:36
SvxHyperlinkInternetTp(weld::Container *pParent, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hlinettp.cxx:34
std::unique_ptr< weld::CheckButton > m_xCbAnonymous
Definition: hlinettp.hxx:47
bool IsMarkWndVisible() const
Definition: hltpbase.hxx:118
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:47
static OUString GetSchemeFromURL(const OUString &rStrURL)
Definition: hltpbase.cxx:397
std::unique_ptr< weld::Entry > m_xEdPassword
Definition: hlinettp.hxx:46
sal_uInt16 nPos
virtual ~SvxHyperlinkInternetTp() override
Definition: hlinettp.cxx:80