bump product version to 6.4.0.3
[LibreOffice.git] / cui / source / dialogs / hlinettp.cxx
blobfda37be4a3cd3d642f860bd1bb370feced95027a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <unotools/useroptions.hxx>
21 #include <svl/adrparse.hxx>
23 #include <hlinettp.hxx>
24 #include <hlmarkwn_def.hxx>
26 sal_Char const sAnonymous[] = "anonymous";
27 sal_Char const sFTPScheme[] = INET_FTP_SCHEME;
29 /*************************************************************************
31 |* Constructor / Destructor
33 |************************************************************************/
34 SvxHyperlinkInternetTp::SvxHyperlinkInternetTp(weld::Container* pParent,
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"))
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);
58 m_xCbbTarget->SetSmartProtocol(INetProtocol::Http);
60 InitStdControls();
62 m_xCbbTarget->show();
64 SetExchangeSupport ();
66 // set defaults
67 m_xRbtLinktypInternet->set_active(true);
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 ) );
80 SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp()
84 /*************************************************************************
86 |* Fill the all dialog-controls except controls in groupbox "more..."
88 |************************************************************************/
89 void SvxHyperlinkInternetTp::FillDlgFields(const OUString& rStrURL)
91 INetURLObject aURL(rStrURL);
92 OUString aStrScheme(GetSchemeFromURL(rStrURL));
94 // set additional controls for FTP: Username / Password
95 if (aStrScheme.startsWith(sFTPScheme))
97 if ( aURL.GetUser().toAsciiLowerCase().startsWith( sAnonymous ) )
98 setAnonymousFTPUser();
99 else
100 setFTPUser(aURL.GetUser(), aURL.GetPass());
102 //do not show password and user in url
103 if(!aURL.GetUser().isEmpty() || !aURL.GetPass().isEmpty() )
104 aURL.SetUserAndPass("", "");
107 // set URL-field
108 // Show the scheme, #72740
109 if ( aURL.GetProtocol() != INetProtocol::NotValid )
110 m_xCbbTarget->set_entry_text( aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ) );
111 else
112 m_xCbbTarget->set_entry_text(rStrURL);
114 SetScheme(aStrScheme);
117 void SvxHyperlinkInternetTp::setAnonymousFTPUser()
119 m_xEdLogin->set_text(sAnonymous);
120 SvAddressParser aAddress(SvtUserOptions().GetEmail());
121 m_xEdPassword->set_text(aAddress.Count() ? aAddress.GetEmailAddress(0) : OUString());
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);
130 void SvxHyperlinkInternetTp::setFTPUser(const OUString& rUser, const OUString& rPassword)
132 m_xEdLogin->set_text(rUser);
133 m_xEdPassword->set_text(rPassword);
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);
142 /*************************************************************************
144 |* retrieve and prepare data from dialog-fields
146 |************************************************************************/
148 void SvxHyperlinkInternetTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
149 OUString& aStrIntName, OUString& aStrFrame,
150 SvxLinkInsertMode& eMode )
152 rStrURL = CreateAbsoluteURL();
153 GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
156 OUString SvxHyperlinkInternetTp::CreateAbsoluteURL() const
158 // erase leading and trailing whitespaces
159 OUString aStrURL(m_xCbbTarget->get_active_text().trim());
161 INetURLObject aURL(aStrURL);
163 if( aURL.GetProtocol() == INetProtocol::NotValid )
165 aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
166 aURL.SetSmartURL(aStrURL);
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() );
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;
179 /*************************************************************************
181 |* static method to create Tabpage
183 |************************************************************************/
185 std::unique_ptr<IconChoicePage> SvxHyperlinkInternetTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
187 return std::make_unique<SvxHyperlinkInternetTp>(pWindow, pDlg, pItemSet);
190 /*************************************************************************
192 |* Set initial focus
194 |************************************************************************/
195 void SvxHyperlinkInternetTp::SetInitFocus()
197 m_xCbbTarget->grab_focus();
200 /*************************************************************************
202 |* Contents of editfield "Target" modified
204 |************************************************************************/
205 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox&, void)
207 OUString aScheme = GetSchemeFromURL( m_xCbbTarget->get_active_text() );
208 if( !aScheme.isEmpty() )
209 SetScheme( aScheme );
211 // start timer
212 maTimer.SetTimeout( 2500 );
213 maTimer.Start();
216 /*************************************************************************
218 |* If target-field was modify, to browse the new doc after timeout
220 |************************************************************************/
221 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, void)
223 RefreshMarkWindow();
226 /*************************************************************************
228 |* Contents of editfield "Login" modified
230 |************************************************************************/
231 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, weld::Entry&, void)
233 OUString aStrLogin ( m_xEdLogin->get_text() );
234 if ( aStrLogin.equalsIgnoreAsciiCase( sAnonymous ) )
236 m_xCbAnonymous->set_active(true);
237 ClickAnonymousHdl_Impl(*m_xCbAnonymous);
241 void SvxHyperlinkInternetTp::SetScheme(const OUString& rScheme)
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;
247 //update protocol button selection:
248 m_xRbtLinktypFTP->set_active(bFTP);
249 m_xRbtLinktypInternet->set_active(bInternet);
251 //update target:
252 RemoveImproperProtocol(rScheme);
253 m_xCbbTarget->SetSmartProtocol( GetSmartProtocolFromButtons() );
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 );
262 //update 'link target in document'-window and opening-button
263 if (rScheme.startsWith(INET_HTTP_SCHEME) || rScheme.isEmpty())
265 if ( m_bMarkWndOpen )
266 ShowMarkWnd ();
268 else
270 //disable for https and ftp
271 if ( m_bMarkWndOpen )
272 HideMarkWnd ();
276 /*************************************************************************
278 |* Remove protocol if it does not fit to the current button selection
280 |************************************************************************/
282 void SvxHyperlinkInternetTp::RemoveImproperProtocol(const OUString& aProperScheme)
284 OUString aStrURL ( m_xCbbTarget->get_active_text() );
285 if ( !aStrURL.isEmpty() )
287 OUString aStrScheme(GetSchemeFromURL(aStrURL));
288 if ( !aStrScheme.isEmpty() && aStrScheme != aProperScheme )
290 aStrURL = aStrURL.copy( aStrScheme.getLength() );
291 m_xCbbTarget->set_entry_text( aStrURL );
296 OUString SvxHyperlinkInternetTp::GetSchemeFromButtons() const
298 if( m_xRbtLinktypFTP->get_active() )
299 return INET_FTP_SCHEME;
300 return INET_HTTP_SCHEME;
303 INetProtocol SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
305 if( m_xRbtLinktypFTP->get_active() )
307 return INetProtocol::Ftp;
309 return INetProtocol::Http;
312 /*************************************************************************
314 |* Click on Radiobutton : Internet or FTP
316 |************************************************************************/
317 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Button&, void)
319 OUString aScheme = GetSchemeFromButtons();
320 SetScheme(aScheme);
323 /*************************************************************************
325 |* Click on Checkbox : Anonymous user
327 |************************************************************************/
328 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl, weld::Button&, void)
330 // disable login-editfields if checked
331 if ( m_xCbAnonymous->get_active() )
333 if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
335 maStrOldUser.clear();
336 maStrOldPassword.clear();
338 else
340 maStrOldUser = m_xEdLogin->get_text();
341 maStrOldPassword = m_xEdPassword->get_text();
344 setAnonymousFTPUser();
346 else
347 setFTPUser(maStrOldUser, maStrOldPassword);
350 /*************************************************************************
352 |* Combobox Target lost the focus
354 |************************************************************************/
355 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
357 RefreshMarkWindow();
360 void SvxHyperlinkInternetTp::RefreshMarkWindow()
362 if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
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 );
373 /*************************************************************************
375 |* Get String from Bookmark-Wnd
377 |************************************************************************/
378 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
380 OUString aStrURL(m_xCbbTarget->get_active_text());
382 const sal_Unicode sUHash = '#';
383 sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );
385 if( nPos != -1 )
386 aStrURL = aStrURL.copy(0, nPos);
388 aStrURL += OUStringChar(sUHash) + aStrMark;
390 m_xCbbTarget->set_entry_text(aStrURL);
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */