android: Update app-specific/MIME type icons
[LibreOffice.git] / cui / source / dialogs / hlinettp.cxx
bloba01673550851544c442d9768fe0eaaa723500032
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 <o3tl/string_view.hxx>
21 #include <unotools/useroptions.hxx>
22 #include <svl/adrparse.hxx>
24 #include <hlinettp.hxx>
25 #include <hlmarkwn_def.hxx>
27 constexpr OUStringLiteral sAnonymous = u"anonymous";
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::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 ) );
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(INET_FTP_SCHEME))
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(u"", u"");
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::GetCurrentItemData ( 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, GetSmartProtocolFromButtons());
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() );
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;
173 /*************************************************************************
175 |* static method to create Tabpage
177 |************************************************************************/
179 std::unique_ptr<IconChoicePage> SvxHyperlinkInternetTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
181 return std::make_unique<SvxHyperlinkInternetTp>(pWindow, pDlg, pItemSet);
184 /*************************************************************************
186 |* Set initial focus
188 |************************************************************************/
189 void SvxHyperlinkInternetTp::SetInitFocus()
191 m_xCbbTarget->grab_focus();
194 /*************************************************************************
196 |* Contents of editfield "Target" modified
198 |************************************************************************/
199 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox&, void)
201 OUString aScheme = GetSchemeFromURL( m_xCbbTarget->get_active_text() );
202 if( !aScheme.isEmpty() )
203 SetScheme( aScheme );
205 // start timer
206 maTimer.SetTimeout( 2500 );
207 maTimer.Start();
210 /*************************************************************************
212 |* If target-field was modify, to browse the new doc after timeout
214 |************************************************************************/
215 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, void)
217 RefreshMarkWindow();
220 /*************************************************************************
222 |* Contents of editfield "Login" modified
224 |************************************************************************/
225 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, weld::Entry&, void)
227 OUString aStrLogin ( m_xEdLogin->get_text() );
228 if ( aStrLogin.equalsIgnoreAsciiCase( sAnonymous ) )
230 m_xCbAnonymous->set_active(true);
231 ClickAnonymousHdl_Impl(*m_xCbAnonymous);
235 void SvxHyperlinkInternetTp::SetScheme(std::u16string_view rScheme)
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;
241 //update protocol button selection:
242 m_xRbtLinktypFTP->set_active(bFTP);
243 m_xRbtLinktypInternet->set_active(bInternet);
245 //update target:
246 RemoveImproperProtocol(rScheme);
247 m_xCbbTarget->SetSmartProtocol( GetSmartProtocolFromButtons() );
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 );
256 //update 'link target in document'-window and opening-button
257 if (o3tl::starts_with(rScheme, INET_HTTP_SCHEME) || rScheme.empty())
259 if ( m_bMarkWndOpen )
260 ShowMarkWnd ();
262 else
264 //disable for https and ftp
265 if ( m_bMarkWndOpen )
266 HideMarkWnd ();
270 /*************************************************************************
272 |* Remove protocol if it does not fit to the current button selection
274 |************************************************************************/
276 void SvxHyperlinkInternetTp::RemoveImproperProtocol(std::u16string_view aProperScheme)
278 OUString aStrURL ( m_xCbbTarget->get_active_text() );
279 if ( !aStrURL.isEmpty() )
281 OUString aStrScheme(GetSchemeFromURL(aStrURL));
282 if ( !aStrScheme.isEmpty() && aStrScheme != aProperScheme )
284 aStrURL = aStrURL.copy( aStrScheme.getLength() );
285 m_xCbbTarget->set_entry_text( aStrURL );
290 OUString SvxHyperlinkInternetTp::GetSchemeFromButtons() const
292 if( m_xRbtLinktypFTP->get_active() )
293 return INET_FTP_SCHEME;
294 return INET_HTTP_SCHEME;
297 INetProtocol SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
299 if( m_xRbtLinktypFTP->get_active() )
301 return INetProtocol::Ftp;
303 return INetProtocol::Http;
306 /*************************************************************************
308 |* Click on Radiobutton : Internet or FTP
310 |************************************************************************/
311 IMPL_LINK(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Toggleable&, rButton, void)
313 if (!rButton.get_active())
314 return;
315 OUString aScheme = GetSchemeFromButtons();
316 SetScheme(aScheme);
319 /*************************************************************************
321 |* Click on Checkbox : Anonymous user
323 |************************************************************************/
324 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl, weld::Toggleable&, void)
326 // disable login-editfields if checked
327 if ( m_xCbAnonymous->get_active() )
329 if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
331 maStrOldUser.clear();
332 maStrOldPassword.clear();
334 else
336 maStrOldUser = m_xEdLogin->get_text();
337 maStrOldPassword = m_xEdPassword->get_text();
340 setAnonymousFTPUser();
342 else
343 setFTPUser(maStrOldUser, maStrOldPassword);
346 /*************************************************************************
348 |* Combobox Target lost the focus
350 |************************************************************************/
351 IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
353 RefreshMarkWindow();
356 void SvxHyperlinkInternetTp::RefreshMarkWindow()
358 if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
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 );
369 /*************************************************************************
371 |* Get String from Bookmark-Wnd
373 |************************************************************************/
374 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
376 OUString aStrURL(m_xCbbTarget->get_active_text());
378 const sal_Unicode sUHash = '#';
379 sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );
381 if( nPos != -1 )
382 aStrURL = aStrURL.copy(0, nPos);
384 aStrURL += OUStringChar(sUHash) + aStrMark;
386 m_xCbbTarget->set_entry_text(aStrURL);
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */