Line data Source code
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 <comphelper/string.hxx>
21 : #include <unotools/pathoptions.hxx>
22 : #include <unotools/useroptions.hxx>
23 : #include <svl/adrparse.hxx>
24 :
25 : #include "hlinettp.hxx"
26 : #include "hyperdlg.hrc"
27 : #include "hlmarkwn_def.hxx"
28 :
29 : sal_Char const sAnonymous[] = "anonymous";
30 : sal_Char const sHTTPScheme[] = INET_HTTP_SCHEME;
31 : sal_Char const sHTTPSScheme[] = INET_HTTPS_SCHEME;
32 : sal_Char const sFTPScheme[] = INET_FTP_SCHEME;
33 :
34 : /*************************************************************************
35 : |*
36 : |* Constructor / Destructor
37 : |*
38 : |************************************************************************/
39 :
40 0 : SvxHyperlinkInternetTp::SvxHyperlinkInternetTp ( Window *pParent,
41 : const SfxItemSet& rItemSet)
42 0 : : SvxHyperlinkTabPageBase ( pParent, CUI_RES( RID_SVXPAGE_HYPERLINK_INTERNET ),
43 : rItemSet ) ,
44 0 : maGrpLinkTyp ( this, CUI_RES (GRP_LINKTYPE) ),
45 0 : maRbtLinktypInternet ( this, CUI_RES (RB_LINKTYP_INTERNET) ),
46 0 : maRbtLinktypFTP ( this, CUI_RES (RB_LINKTYP_FTP) ),
47 0 : maFtTarget ( this, CUI_RES (FT_TARGET_HTML) ),
48 : maCbbTarget ( this, INET_PROT_HTTP ),
49 0 : maBtBrowse ( this, CUI_RES (BTN_BROWSE) ),
50 0 : maFtLogin ( this, CUI_RES (FT_LOGIN) ),
51 0 : maEdLogin ( this, CUI_RES (ED_LOGIN) ),
52 0 : maBtTarget ( this, CUI_RES (BTN_TARGET) ),
53 0 : maFtPassword ( this, CUI_RES (FT_PASSWD) ),
54 0 : maEdPassword ( this, CUI_RES (ED_PASSWD) ),
55 0 : maCbAnonymous ( this, CUI_RES (CBX_ANONYMOUS) ),
56 0 : mbMarkWndOpen ( sal_False )
57 : {
58 : // Disable display of bitmap names.
59 0 : maBtBrowse.EnableTextDisplay (sal_False);
60 0 : maBtTarget.EnableTextDisplay (sal_False);
61 :
62 0 : InitStdControls();
63 0 : FreeResource();
64 :
65 : // Init URL-Box (pos&size, Open-Handler)
66 0 : maCbbTarget.SetPosSizePixel ( LogicToPixel( Point( COL_2, 25 ), MAP_APPFONT ),
67 0 : LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
68 0 : maCbbTarget.Show();
69 0 : maCbbTarget.SetHelpId( HID_HYPERDLG_INET_PATH );
70 :
71 0 : SetExchangeSupport ();
72 :
73 : ///////////////////////////////////////
74 : // set defaults
75 0 : maRbtLinktypInternet.Check ();
76 0 : maFtLogin.Show( sal_False );
77 0 : maFtPassword.Show( sal_False );
78 0 : maEdLogin.Show( sal_False );
79 0 : maEdPassword.Show( sal_False );
80 0 : maCbAnonymous.Show( sal_False );
81 0 : maBtTarget.Enable( sal_False );
82 0 : maBtBrowse.Enable( sal_True );
83 :
84 : ///////////////////////////////////////
85 : // overload handlers
86 0 : Link aLink( LINK ( this, SvxHyperlinkInternetTp, Click_SmartProtocol_Impl ) );
87 0 : maRbtLinktypInternet.SetClickHdl( aLink );
88 0 : maRbtLinktypFTP.SetClickHdl ( aLink );
89 0 : maCbAnonymous.SetClickHdl ( LINK ( this, SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl ) );
90 0 : maBtBrowse.SetClickHdl ( LINK ( this, SvxHyperlinkInternetTp, ClickBrowseHdl_Impl ) );
91 0 : maBtTarget.SetClickHdl ( LINK ( this, SvxHyperlinkInternetTp, ClickTargetHdl_Impl ) );
92 0 : maEdLogin.SetModifyHdl ( LINK ( this, SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl ) );
93 0 : maCbbTarget.SetLoseFocusHdl ( LINK ( this, SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl ) );
94 0 : maCbbTarget.SetModifyHdl ( LINK ( this, SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl ) );
95 0 : maTimer.SetTimeoutHdl ( LINK ( this, SvxHyperlinkInternetTp, TimeoutHdl_Impl ) );
96 :
97 0 : maFtTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
98 0 : maCbbTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
99 0 : maBtTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
100 0 : maBtTarget.SetAccessibleRelationLabeledBy( &maFtTarget );
101 0 : maBtBrowse.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
102 0 : maBtBrowse.SetAccessibleRelationLabeledBy( &maFtTarget );
103 0 : }
104 :
105 0 : SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp ()
106 : {
107 0 : }
108 :
109 : /*************************************************************************
110 : |*
111 : |* Fill the all dialog-controls except controls in groupbox "more..."
112 : |*
113 : |************************************************************************/
114 :
115 0 : void SvxHyperlinkInternetTp::FillDlgFields ( String& aStrURL )
116 : {
117 0 : INetURLObject aURL( aStrURL );
118 0 : String aStrScheme = GetSchemeFromURL( aStrURL );
119 :
120 : // set additional controls for FTP: Username / Password
121 0 : if ( aStrScheme.SearchAscii( sFTPScheme ) == 0 )
122 : {
123 0 : if ( String(aURL.GetUser()).ToLowerAscii().SearchAscii ( sAnonymous ) == 0 )
124 0 : setAnonymousFTPUser();
125 : else
126 0 : setFTPUser(aURL.GetUser(), aURL.GetPass());
127 :
128 : //do not show password and user in url
129 0 : if(!aURL.GetUser().isEmpty() || !aURL.GetPass().isEmpty() )
130 0 : aURL.SetUserAndPass(aEmptyStr,aEmptyStr);
131 : }
132 :
133 : // set URL-field
134 : // Show the scheme, #72740
135 0 : if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
136 0 : maCbbTarget.SetText( aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) );
137 : else
138 0 : maCbbTarget.SetText( aStrURL ); // #77696#
139 :
140 0 : SetScheme( aStrScheme );
141 0 : }
142 :
143 0 : void SvxHyperlinkInternetTp::setAnonymousFTPUser()
144 : {
145 0 : maEdLogin.SetText(OUString(sAnonymous));
146 0 : SvAddressParser aAddress( SvtUserOptions().GetEmail() );
147 0 : maEdPassword.SetText( aAddress.Count() ? aAddress.GetEmailAddress(0) : OUString() );
148 :
149 0 : maFtLogin.Disable ();
150 0 : maFtPassword.Disable ();
151 0 : maEdLogin.Disable ();
152 0 : maEdPassword.Disable ();
153 0 : maCbAnonymous.Check();
154 0 : }
155 :
156 0 : void SvxHyperlinkInternetTp::setFTPUser(const String& rUser, const String& rPassword)
157 : {
158 0 : maEdLogin.SetText ( rUser );
159 0 : maEdPassword.SetText ( rPassword );
160 :
161 0 : maFtLogin.Enable ();
162 0 : maFtPassword.Enable ();
163 0 : maEdLogin.Enable ();
164 0 : maEdPassword.Enable ();
165 0 : maCbAnonymous.Check(sal_False);
166 0 : }
167 :
168 : /*************************************************************************
169 : |*
170 : |* retrieve and prepare data from dialog-fields
171 : |*
172 : |************************************************************************/
173 :
174 0 : void SvxHyperlinkInternetTp::GetCurentItemData ( String& aStrURL, String& aStrName,
175 : String& aStrIntName, String& aStrFrame,
176 : SvxLinkInsertMode& eMode )
177 : {
178 0 : aStrURL = CreateAbsoluteURL();
179 0 : GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
180 0 : }
181 :
182 0 : String SvxHyperlinkInternetTp::CreateAbsoluteURL() const
183 : {
184 0 : String aStrURL = maCbbTarget.GetText();
185 :
186 0 : INetURLObject aURL(aStrURL);
187 :
188 0 : if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
189 : {
190 0 : aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
191 0 : aURL.SetSmartURL(aStrURL);
192 : }
193 :
194 : // username and password for ftp-url
195 0 : if( aURL.GetProtocol() == INET_PROT_FTP && !maEdLogin.GetText().isEmpty() )
196 0 : aURL.SetUserAndPass ( maEdLogin.GetText(), maEdPassword.GetText() );
197 :
198 0 : if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
199 0 : return aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET );
200 : else //#105788# always create a URL even if it is not valid
201 0 : return aStrURL;
202 : }
203 :
204 : /*************************************************************************
205 : |*
206 : |* static method to create Tabpage
207 : |*
208 : |************************************************************************/
209 :
210 0 : IconChoicePage* SvxHyperlinkInternetTp::Create( Window* pWindow, const SfxItemSet& rItemSet )
211 : {
212 0 : return( new SvxHyperlinkInternetTp( pWindow, rItemSet ) );
213 : }
214 :
215 : /*************************************************************************
216 : |*
217 : |* Set initial focus
218 : |*
219 : |************************************************************************/
220 :
221 0 : void SvxHyperlinkInternetTp::SetInitFocus()
222 : {
223 0 : maCbbTarget.GrabFocus();
224 0 : }
225 :
226 : /*************************************************************************
227 : |*
228 : |* Contents of editfield "Target" modified
229 : |*
230 : |************************************************************************/
231 :
232 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl)
233 : {
234 0 : String aScheme = GetSchemeFromURL( maCbbTarget.GetText() );
235 0 : if(aScheme.Len()!=0)
236 0 : SetScheme( aScheme );
237 :
238 : // start timer
239 0 : maTimer.SetTimeout( 2500 );
240 0 : maTimer.Start();
241 :
242 0 : return( 0L );
243 : }
244 :
245 : /*************************************************************************
246 : |*
247 : |* If target-field was modify, to browse the new doc after timeout
248 : |*
249 : |************************************************************************/
250 :
251 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl)
252 : {
253 0 : RefreshMarkWindow();
254 0 : return( 0L );
255 : }
256 :
257 : /*************************************************************************
258 : |*
259 : |* Contents of editfield "Login" modified
260 : |*
261 : |************************************************************************/
262 :
263 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl)
264 : {
265 0 : String aStrLogin ( maEdLogin.GetText() );
266 0 : if ( aStrLogin.EqualsIgnoreCaseAscii( sAnonymous ) )
267 : {
268 0 : maCbAnonymous.Check();
269 0 : ClickAnonymousHdl_Impl(NULL);
270 : }
271 :
272 0 : return( 0L );
273 : }
274 :
275 : /*************************************************************************
276 : |************************************************************************/
277 :
278 0 : void SvxHyperlinkInternetTp::SetScheme( const String& aScheme )
279 : {
280 : //if aScheme is empty or unknown the default beaviour is like it where HTTP
281 :
282 0 : sal_Bool bFTP = aScheme.SearchAscii( sFTPScheme ) == 0;
283 0 : sal_Bool bInternet = !(bFTP);
284 :
285 : //update protocol button selection:
286 0 : maRbtLinktypFTP.Check(bFTP);
287 0 : maRbtLinktypInternet.Check(bInternet);
288 :
289 : //update target:
290 0 : RemoveImproperProtocol(aScheme);
291 0 : maCbbTarget.SetSmartProtocol( GetSmartProtocolFromButtons() );
292 :
293 : //show/hide special fields for FTP:
294 0 : maFtLogin.Show( bFTP );
295 0 : maFtPassword.Show( bFTP );
296 0 : maEdLogin.Show( bFTP );
297 0 : maEdPassword.Show( bFTP );
298 0 : maCbAnonymous.Show( bFTP );
299 :
300 : //update 'link target in document'-window and opening-button
301 0 : if( aScheme.SearchAscii( sHTTPScheme ) == 0 || aScheme.Len() == 0 )
302 : {
303 0 : maBtTarget.Enable();
304 0 : if ( mbMarkWndOpen )
305 0 : ShowMarkWnd ();
306 : }
307 : else
308 : {
309 : //disable for https and ftp
310 0 : maBtTarget.Disable();
311 0 : if ( mbMarkWndOpen )
312 0 : HideMarkWnd ();
313 : }
314 0 : }
315 :
316 : /*************************************************************************
317 : |*
318 : |* Remove protocol if it does not fit to the current button selection
319 : |*
320 : |************************************************************************/
321 :
322 0 : void SvxHyperlinkInternetTp::RemoveImproperProtocol(const String& aProperScheme)
323 : {
324 0 : String aStrURL ( maCbbTarget.GetText() );
325 0 : if ( aStrURL != aEmptyStr )
326 : {
327 0 : String aStrScheme = GetSchemeFromURL( aStrURL );
328 0 : if ( aStrScheme != aEmptyStr && aStrScheme != aProperScheme )
329 : {
330 0 : aStrURL.Erase ( 0, aStrScheme.Len() );
331 0 : maCbbTarget.SetText ( aStrURL );
332 0 : }
333 0 : }
334 0 : }
335 :
336 0 : String SvxHyperlinkInternetTp::GetSchemeFromButtons() const
337 : {
338 0 : if( maRbtLinktypFTP.IsChecked() )
339 0 : return OUString(INET_FTP_SCHEME);
340 0 : return OUString(INET_HTTP_SCHEME);
341 : }
342 :
343 0 : INetProtocol SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
344 : {
345 0 : if( maRbtLinktypFTP.IsChecked() )
346 : {
347 0 : return INET_PROT_FTP;
348 : }
349 0 : return INET_PROT_HTTP;
350 : }
351 :
352 : /*************************************************************************
353 : |*
354 : |* Click on Radiobutton : Internet or FTP
355 : |*
356 : |************************************************************************/
357 :
358 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl)
359 : {
360 0 : String aScheme = GetSchemeFromButtons();
361 0 : SetScheme( aScheme );
362 0 : return( 0L );
363 : }
364 :
365 : /*************************************************************************
366 : |*
367 : |* Click on Checkbox : Anonymous user
368 : |*
369 : |************************************************************************/
370 :
371 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl)
372 : {
373 : // disable login-editfields if checked
374 0 : if ( maCbAnonymous.IsChecked() )
375 : {
376 0 : if ( maEdLogin.GetText().toAsciiLowerCase().indexOf ( sAnonymous ) == 0 )
377 : {
378 0 : maStrOldUser = aEmptyStr;
379 0 : maStrOldPassword = aEmptyStr;
380 : }
381 : else
382 : {
383 0 : maStrOldUser = maEdLogin.GetText();
384 0 : maStrOldPassword = maEdPassword.GetText();
385 : }
386 :
387 0 : setAnonymousFTPUser();
388 : }
389 : else
390 0 : setFTPUser(maStrOldUser, maStrOldPassword);
391 :
392 0 : return( 0L );
393 : }
394 :
395 : /*************************************************************************
396 : |*
397 : |* Combobox Target lost the focus
398 : |*
399 : |************************************************************************/
400 :
401 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl)
402 : {
403 0 : RefreshMarkWindow();
404 0 : return (0L);
405 : }
406 :
407 : /*************************************************************************
408 : |*
409 : |* Click on imagebutton : Browse
410 : |*
411 : |************************************************************************/
412 :
413 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickBrowseHdl_Impl)
414 : {
415 : /////////////////////////////////////////////////
416 : // Open URL if available
417 :
418 0 : SfxStringItem aName( SID_FILE_NAME, OUString("http://") );
419 0 : SfxStringItem aRefererItem( SID_REFERER, OUString("private:user") );
420 0 : SfxBoolItem aNewView( SID_OPEN_NEW_VIEW, sal_True );
421 0 : SfxBoolItem aSilent( SID_SILENT, sal_True );
422 0 : SfxBoolItem aReadOnly( SID_DOC_READONLY, sal_True );
423 :
424 0 : SfxBoolItem aBrowse( SID_BROWSE, sal_True );
425 :
426 0 : const SfxPoolItem *ppItems[] = { &aName, &aNewView, &aSilent, &aReadOnly, &aRefererItem, &aBrowse, NULL };
427 0 : (((SvxHpLinkDlg*)mpDialog)->GetBindings())->Execute( SID_OPENDOC, ppItems, 0, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD );
428 :
429 0 : return( 0L );
430 : }
431 :
432 : /*************************************************************************
433 : |*
434 : |* Click on imagebutton : Target
435 : |*
436 : |************************************************************************/
437 :
438 0 : IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickTargetHdl_Impl)
439 : {
440 0 : RefreshMarkWindow();
441 0 : ShowMarkWnd ();
442 0 : mbMarkWndOpen = IsMarkWndVisible ();
443 :
444 0 : return( 0L );
445 : }
446 :
447 0 : void SvxHyperlinkInternetTp::RefreshMarkWindow()
448 : {
449 0 : if ( maRbtLinktypInternet.IsChecked() && IsMarkWndVisible() )
450 : {
451 0 : EnterWait();
452 0 : String aStrURL( CreateAbsoluteURL() );
453 0 : if ( aStrURL != aEmptyStr )
454 0 : mpMarkWnd->RefreshTree ( aStrURL );
455 : else
456 0 : mpMarkWnd->SetError( LERR_DOCNOTOPEN );
457 0 : LeaveWait();
458 : }
459 :
460 0 : }
461 :
462 : /*************************************************************************
463 : |*
464 : |* Get String from Bookmark-Wnd
465 : |*
466 : |************************************************************************/
467 :
468 0 : void SvxHyperlinkInternetTp::SetMarkStr ( String& aStrMark )
469 : {
470 0 : String aStrURL ( maCbbTarget.GetText() );
471 :
472 0 : const sal_Unicode sUHash = '#';
473 0 : xub_StrLen nPos = aStrURL.SearchBackward( sUHash );
474 :
475 0 : if( nPos != STRING_NOTFOUND )
476 0 : aStrURL.Erase ( nPos );
477 :
478 0 : aStrURL += sUHash;
479 0 : aStrURL += aStrMark;
480 :
481 0 : maCbbTarget.SetText ( aStrURL );
482 0 : }
483 :
484 : /*************************************************************************
485 : |*
486 : |* Enable Browse-Button in subject to the office is in onlinemode
487 : |*
488 : |************************************************************************/
489 :
490 0 : void SvxHyperlinkInternetTp::SetOnlineMode( sal_Bool /*bEnable*/ )
491 : {
492 : // State of target-button in subject to the current url-string
493 : // ( Can't display any targets in an document, if there is no
494 : // valid url to a document )
495 0 : String aStrCurrentTarget(comphelper::string::stripEnd(maCbbTarget.GetText(), ' '));
496 :
497 0 : if( aStrCurrentTarget == aEmptyStr ||
498 0 : aStrCurrentTarget.EqualsIgnoreCaseAscii( sHTTPScheme ) ||
499 0 : aStrCurrentTarget.EqualsIgnoreCaseAscii( sHTTPSScheme ) )
500 0 : maBtTarget.Enable( sal_False );
501 : else
502 0 : maBtTarget.Enable( sal_True );
503 0 : }
504 :
505 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|