1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/pathoptions.hxx>
21 #include <unotools/useroptions.hxx>
22 #include <svl/adrparse.hxx>
24 #include <hlinettp.hxx>
25 #include <hlmarkwn_def.hxx>
27 sal_Char
const sAnonymous
[] = "anonymous";
28 sal_Char
const sFTPScheme
[] = INET_FTP_SCHEME
;
30 /*************************************************************************
32 |* Constructor / Destructor
34 |************************************************************************/
36 SvxHyperlinkInternetTp::SvxHyperlinkInternetTp ( vcl::Window
*pParent
,
37 IconChoiceDialog
* pDlg
,
38 const SfxItemSet
* pItemSet
)
39 : SvxHyperlinkTabPageBase ( pParent
, pDlg
, "HyperlinkInternetPage", "cui/ui/hyperlinkinternetpage.ui",
41 mbMarkWndOpen ( false )
43 get(m_pRbtLinktypInternet
, "linktyp_internet");
44 get(m_pRbtLinktypFTP
, "linktyp_ftp");
45 get(m_pCbbTarget
, "target");
46 m_pCbbTarget
->SetSmartProtocol(INetProtocol::Http
);
47 get(m_pFtLogin
, "login_label");
48 get(m_pEdLogin
, "login");
49 get(m_pFtPassword
, "password_label");
50 get(m_pEdPassword
, "password");
51 get(m_pCbAnonymous
, "anonymous");
57 SetExchangeSupport ();
61 m_pRbtLinktypInternet
->Check ();
65 Link
<Button
*, void> aLink( LINK ( this, SvxHyperlinkInternetTp
, Click_SmartProtocol_Impl
) );
66 m_pRbtLinktypInternet
->SetClickHdl( aLink
);
67 m_pRbtLinktypFTP
->SetClickHdl ( aLink
);
68 m_pCbAnonymous
->SetClickHdl ( LINK ( this, SvxHyperlinkInternetTp
, ClickAnonymousHdl_Impl
) );
69 m_pEdLogin
->SetModifyHdl ( LINK ( this, SvxHyperlinkInternetTp
, ModifiedLoginHdl_Impl
) );
70 m_pCbbTarget
->SetLoseFocusHdl ( LINK ( this, SvxHyperlinkInternetTp
, LostFocusTargetHdl_Impl
) );
71 m_pCbbTarget
->SetModifyHdl ( LINK ( this, SvxHyperlinkInternetTp
, ModifiedTargetHdl_Impl
) );
72 maTimer
.SetInvokeHandler ( LINK ( this, SvxHyperlinkInternetTp
, TimeoutHdl_Impl
) );
75 SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp()
80 void SvxHyperlinkInternetTp::dispose()
82 m_pRbtLinktypInternet
.clear();
83 m_pRbtLinktypFTP
.clear();
87 m_pFtPassword
.clear();
88 m_pEdPassword
.clear();
89 m_pCbAnonymous
.clear();
90 SvxHyperlinkTabPageBase::dispose();
93 /*************************************************************************
95 |* Fill the all dialog-controls except controls in groupbox "more..."
97 |************************************************************************/
99 void SvxHyperlinkInternetTp::FillDlgFields(const OUString
& rStrURL
)
101 INetURLObject
aURL(rStrURL
);
102 OUString
aStrScheme(GetSchemeFromURL(rStrURL
));
104 // set additional controls for FTP: Username / Password
105 if (aStrScheme
.startsWith(sFTPScheme
))
107 if ( aURL
.GetUser().toAsciiLowerCase().startsWith( sAnonymous
) )
108 setAnonymousFTPUser();
110 setFTPUser(aURL
.GetUser(), aURL
.GetPass());
112 //do not show password and user in url
113 if(!aURL
.GetUser().isEmpty() || !aURL
.GetPass().isEmpty() )
114 aURL
.SetUserAndPass("", "");
118 // Show the scheme, #72740
119 if ( aURL
.GetProtocol() != INetProtocol::NotValid
)
120 m_pCbbTarget
->SetText( aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
) );
122 m_pCbbTarget
->SetText(rStrURL
);
124 SetScheme(aStrScheme
);
127 void SvxHyperlinkInternetTp::setAnonymousFTPUser()
129 m_pEdLogin
->SetText(sAnonymous
);
130 SvAddressParser
aAddress( SvtUserOptions().GetEmail() );
131 m_pEdPassword
->SetText( aAddress
.Count() ? aAddress
.GetEmailAddress(0) : OUString() );
133 m_pFtLogin
->Disable ();
134 m_pFtPassword
->Disable ();
135 m_pEdLogin
->Disable ();
136 m_pEdPassword
->Disable ();
137 m_pCbAnonymous
->Check();
140 void SvxHyperlinkInternetTp::setFTPUser(const OUString
& rUser
, const OUString
& rPassword
)
142 m_pEdLogin
->SetText ( rUser
);
143 m_pEdPassword
->SetText ( rPassword
);
145 m_pFtLogin
->Enable ();
146 m_pFtPassword
->Enable ();
147 m_pEdLogin
->Enable ();
148 m_pEdPassword
->Enable ();
149 m_pCbAnonymous
->Check(false);
152 /*************************************************************************
154 |* retrieve and prepare data from dialog-fields
156 |************************************************************************/
158 void SvxHyperlinkInternetTp::GetCurentItemData ( OUString
& rStrURL
, OUString
& aStrName
,
159 OUString
& aStrIntName
, OUString
& aStrFrame
,
160 SvxLinkInsertMode
& eMode
)
162 rStrURL
= CreateAbsoluteURL();
163 GetDataFromCommonFields( aStrName
, aStrIntName
, aStrFrame
, eMode
);
166 OUString
SvxHyperlinkInternetTp::CreateAbsoluteURL() const
168 // erase leading and trailing whitespaces
169 OUString
aStrURL( m_pCbbTarget
->GetText().trim() );
171 INetURLObject
aURL(aStrURL
);
173 if( aURL
.GetProtocol() == INetProtocol::NotValid
)
175 aURL
.SetSmartProtocol( GetSmartProtocolFromButtons() );
176 aURL
.SetSmartURL(aStrURL
);
179 // username and password for ftp-url
180 if( aURL
.GetProtocol() == INetProtocol::Ftp
&& !m_pEdLogin
->GetText().isEmpty() )
181 aURL
.SetUserAndPass ( m_pEdLogin
->GetText(), m_pEdPassword
->GetText() );
183 if ( aURL
.GetProtocol() != INetProtocol::NotValid
)
184 return aURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
);
185 else //#105788# always create a URL even if it is not valid
189 /*************************************************************************
191 |* static method to create Tabpage
193 |************************************************************************/
195 VclPtr
<IconChoicePage
> SvxHyperlinkInternetTp::Create( vcl::Window
* pWindow
, IconChoiceDialog
* pDlg
, const SfxItemSet
* pItemSet
)
197 return VclPtr
<SvxHyperlinkInternetTp
>::Create( pWindow
, pDlg
, pItemSet
);
200 /*************************************************************************
204 |************************************************************************/
206 void SvxHyperlinkInternetTp::SetInitFocus()
208 m_pCbbTarget
->GrabFocus();
211 /*************************************************************************
213 |* Contents of editfield "Target" modified
215 |************************************************************************/
217 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, ModifiedTargetHdl_Impl
, Edit
&, void)
219 OUString aScheme
= GetSchemeFromURL( m_pCbbTarget
->GetText() );
220 if( !aScheme
.isEmpty() )
221 SetScheme( aScheme
);
224 maTimer
.SetTimeout( 2500 );
228 /*************************************************************************
230 |* If target-field was modify, to browse the new doc after timeout
232 |************************************************************************/
234 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, TimeoutHdl_Impl
, Timer
*, void)
239 /*************************************************************************
241 |* Contents of editfield "Login" modified
243 |************************************************************************/
245 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, ModifiedLoginHdl_Impl
, Edit
&, void)
247 OUString
aStrLogin ( m_pEdLogin
->GetText() );
248 if ( aStrLogin
.equalsIgnoreAsciiCase( sAnonymous
) )
250 m_pCbAnonymous
->Check();
251 ClickAnonymousHdl_Impl(nullptr);
255 void SvxHyperlinkInternetTp::SetScheme(const OUString
& rScheme
)
257 //if rScheme is empty or unknown the default behaviour is like it where HTTP
258 bool bFTP
= rScheme
.startsWith(sFTPScheme
);
259 bool bInternet
= !bFTP
;
261 //update protocol button selection:
262 m_pRbtLinktypFTP
->Check(bFTP
);
263 m_pRbtLinktypInternet
->Check(bInternet
);
266 RemoveImproperProtocol(rScheme
);
267 m_pCbbTarget
->SetSmartProtocol( GetSmartProtocolFromButtons() );
269 //show/hide special fields for FTP:
270 m_pFtLogin
->Show( bFTP
);
271 m_pFtPassword
->Show( bFTP
);
272 m_pEdLogin
->Show( bFTP
);
273 m_pEdPassword
->Show( bFTP
);
274 m_pCbAnonymous
->Show( bFTP
);
276 //update 'link target in document'-window and opening-button
277 if (rScheme
.startsWith(INET_HTTP_SCHEME
) || rScheme
.isEmpty())
284 //disable for https and ftp
290 /*************************************************************************
292 |* Remove protocol if it does not fit to the current button selection
294 |************************************************************************/
296 void SvxHyperlinkInternetTp::RemoveImproperProtocol(const OUString
& aProperScheme
)
298 OUString
aStrURL ( m_pCbbTarget
->GetText() );
299 if ( !aStrURL
.isEmpty() )
301 OUString
aStrScheme(GetSchemeFromURL(aStrURL
));
302 if ( !aStrScheme
.isEmpty() && aStrScheme
!= aProperScheme
)
304 aStrURL
= aStrURL
.copy( aStrScheme
.getLength() );
305 m_pCbbTarget
->SetText ( aStrURL
);
310 OUString
SvxHyperlinkInternetTp::GetSchemeFromButtons() const
312 if( m_pRbtLinktypFTP
->IsChecked() )
313 return OUString(INET_FTP_SCHEME
);
314 return OUString(INET_HTTP_SCHEME
);
317 INetProtocol
SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
319 if( m_pRbtLinktypFTP
->IsChecked() )
321 return INetProtocol::Ftp
;
323 return INetProtocol::Http
;
326 /*************************************************************************
328 |* Click on Radiobutton : Internet or FTP
330 |************************************************************************/
332 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, Click_SmartProtocol_Impl
, Button
*, void)
334 OUString aScheme
= GetSchemeFromButtons();
338 /*************************************************************************
340 |* Click on Checkbox : Anonymous user
342 |************************************************************************/
344 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, ClickAnonymousHdl_Impl
, Button
*, void)
346 // disable login-editfields if checked
347 if ( m_pCbAnonymous
->IsChecked() )
349 if ( m_pEdLogin
->GetText().toAsciiLowerCase().startsWith( sAnonymous
) )
351 maStrOldUser
.clear();
352 maStrOldPassword
.clear();
356 maStrOldUser
= m_pEdLogin
->GetText();
357 maStrOldPassword
= m_pEdPassword
->GetText();
360 setAnonymousFTPUser();
363 setFTPUser(maStrOldUser
, maStrOldPassword
);
366 /*************************************************************************
368 |* Combobox Target lost the focus
370 |************************************************************************/
372 IMPL_LINK_NOARG(SvxHyperlinkInternetTp
, LostFocusTargetHdl_Impl
, Control
&, void)
377 void SvxHyperlinkInternetTp::RefreshMarkWindow()
379 if ( m_pRbtLinktypInternet
->IsChecked() && IsMarkWndVisible() )
382 OUString
aStrURL( CreateAbsoluteURL() );
383 if ( !aStrURL
.isEmpty() )
384 mpMarkWnd
->RefreshTree ( aStrURL
);
386 mpMarkWnd
->SetError( LERR_DOCNOTOPEN
);
392 /*************************************************************************
394 |* Get String from Bookmark-Wnd
396 |************************************************************************/
398 void SvxHyperlinkInternetTp::SetMarkStr ( const OUString
& aStrMark
)
400 OUString
aStrURL ( m_pCbbTarget
->GetText() );
402 const sal_Unicode sUHash
= '#';
403 sal_Int32 nPos
= aStrURL
.lastIndexOf( sUHash
);
406 aStrURL
= aStrURL
.copy(0, nPos
);
408 aStrURL
+= OUStringLiteral1(sUHash
) + aStrMark
;
410 m_pCbbTarget
->SetText ( aStrURL
);
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */