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