xref: /trunk/main/cui/source/dialogs/hlinettp.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 
31 #include <unotools/pathoptions.hxx>
32 #include <unotools/useroptions.hxx>
33 #include <svl/adrparse.hxx>
34 
35 #include "hlinettp.hxx"
36 #include "hyperdlg.hrc"
37 #include "hlmarkwn_def.hxx" //ADD CHINA001
38 
39 #define STD_DOC_SUBPATH		"internal"
40 #define STD_DOC_NAME		"url_transfer.htm"
41 
42 sal_Char __READONLY_DATA sAnonymous[]    = "anonymous";
43 sal_Char __READONLY_DATA sHTTPScheme[]   = INET_HTTP_SCHEME;
44 sal_Char __READONLY_DATA sHTTPSScheme[]  = INET_HTTPS_SCHEME;
45 sal_Char __READONLY_DATA sFTPScheme[]    = INET_FTP_SCHEME;
46 sal_Char __READONLY_DATA sTelnetScheme[] = INET_TELNET_SCHEME;
47 
48 /*************************************************************************
49 |*
50 |* Contructor / Destructor
51 |*
52 |************************************************************************/
53 
54 SvxHyperlinkInternetTp::SvxHyperlinkInternetTp ( Window *pParent,
55 												 const SfxItemSet& rItemSet)
56 :   SvxHyperlinkTabPageBase ( pParent, CUI_RES( RID_SVXPAGE_HYPERLINK_INTERNET ),
57                               rItemSet ) ,
58     maGrpLinkTyp           ( this, CUI_RES (GRP_LINKTYPE) ),
59 	maRbtLinktypInternet	( this, CUI_RES (RB_LINKTYP_INTERNET) ),
60 	maRbtLinktypFTP			( this, CUI_RES (RB_LINKTYP_FTP) ),
61 	maRbtLinktypTelnet		( this, CUI_RES (RB_LINKTYP_TELNET) ),
62 	maFtTarget				( this, CUI_RES (FT_TARGET_HTML) ),
63 	maCbbTarget				( this, INET_PROT_HTTP ),
64 	maBtBrowse				( this, CUI_RES (BTN_BROWSE) ),
65 	maFtLogin				( this, CUI_RES (FT_LOGIN) ),
66 	maEdLogin				( this, CUI_RES (ED_LOGIN) ),
67 	maBtTarget				( this, CUI_RES (BTN_TARGET) ),
68 	maFtPassword			( this, CUI_RES (FT_PASSWD) ),
69 	maEdPassword			( this, CUI_RES (ED_PASSWD) ),
70 	maCbAnonymous			( this, CUI_RES (CBX_ANONYMOUS) ),
71     mbMarkWndOpen           ( sal_False )
72 {
73 	// Set HC bitmaps and display display of bitmap names.
74 	maBtBrowse.SetModeImage( Image( CUI_RES( IMG_BROWSE_HC ) ), BMP_COLOR_HIGHCONTRAST );
75     maBtBrowse.EnableTextDisplay (sal_False);
76 	maBtTarget.SetModeImage( Image( CUI_RES( IMG_TARGET_HC ) ), BMP_COLOR_HIGHCONTRAST );
77     maBtTarget.EnableTextDisplay (sal_False);
78 
79 	InitStdControls();
80 	FreeResource();
81 
82 	// Init URL-Box (pos&size, Open-Handler)
83 	maCbbTarget.SetPosSizePixel ( LogicToPixel( Point( COL_2, 25 ), MAP_APPFONT ),
84 		                          LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
85 	maCbbTarget.Show();
86 	maCbbTarget.SetHelpId( HID_HYPERDLG_INET_PATH );
87 
88 	// Find Path to Std-Doc
89 	String aStrBasePaths( SvtPathOptions().GetTemplatePath() );
90 	for( xub_StrLen n = 0; n < aStrBasePaths.GetTokenCount(); n++ )
91 	{
92 	    INetURLObject aURL( aStrBasePaths.GetToken( n ) );
93 		aURL.Append( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( STD_DOC_SUBPATH ) ) );
94 		aURL.Append( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( STD_DOC_NAME ) ) );
95 		if ( FileExists( aURL ) )
96         {
97 			maStrStdDocURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
98             break;
99         }
100 	}
101 	SetExchangeSupport ();
102 
103 	///////////////////////////////////////
104 	// set defaults
105 	maRbtLinktypInternet.Check ();
106 	maFtLogin.Show( sal_False );
107 	maFtPassword.Show( sal_False );
108 	maEdLogin.Show( sal_False );
109 	maEdPassword.Show( sal_False );
110 	maCbAnonymous.Show( sal_False );
111 	maBtTarget.Enable( sal_False );
112 	maBtBrowse.Enable( maStrStdDocURL != aEmptyStr );
113 
114 	///////////////////////////////////////
115 	// overload handlers
116     Link aLink( LINK ( this, SvxHyperlinkInternetTp, Click_SmartProtocol_Impl ) );
117     maRbtLinktypInternet.SetClickHdl( aLink );
118     maRbtLinktypFTP.SetClickHdl     ( aLink );
119     maRbtLinktypTelnet.SetClickHdl  ( aLink );
120 	maCbAnonymous.SetClickHdl       ( LINK ( this, SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl ) );
121 	maBtBrowse.SetClickHdl          ( LINK ( this, SvxHyperlinkInternetTp, ClickBrowseHdl_Impl ) );
122 	maBtTarget.SetClickHdl          ( LINK ( this, SvxHyperlinkInternetTp, ClickTargetHdl_Impl ) );
123 	maEdLogin.SetModifyHdl			( LINK ( this, SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl ) );
124 	maCbbTarget.SetLoseFocusHdl		( LINK ( this, SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl ) );
125 	maCbbTarget.SetModifyHdl		( LINK ( this, SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl ) );
126 	maTimer.SetTimeoutHdl			( LINK ( this, SvxHyperlinkInternetTp, TimeoutHdl_Impl ) );
127 
128 	maFtTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
129 	maCbbTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
130 	maBtTarget.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
131 	maBtTarget.SetAccessibleRelationLabeledBy( &maFtTarget );
132 	maBtBrowse.SetAccessibleRelationMemberOf( &maGrpLinkTyp );
133 	maBtBrowse.SetAccessibleRelationLabeledBy( &maFtTarget );
134 }
135 
136 SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp ()
137 {
138 }
139 
140 /*************************************************************************
141 |*
142 |* Fill the all dialog-controls except controls in groupbox "more..."
143 |*
144 |************************************************************************/
145 
146 void SvxHyperlinkInternetTp::FillDlgFields ( String& aStrURL )
147 {
148 	INetURLObject aURL( aStrURL );
149 	String aStrScheme = GetSchemeFromURL( aStrURL );
150 
151     // set additional controls for FTP: Username / Password
152 	if ( aStrScheme.SearchAscii( sFTPScheme ) == 0 )
153 	{
154 		if ( String(aURL.GetUser()).ToLowerAscii().SearchAscii ( sAnonymous ) == 0 )
155 			setAnonymousFTPUser();
156 		else
157 			setFTPUser(aURL.GetUser(), aURL.GetPass());
158 
159         //do not show password and user in url
160         if(aURL.GetUser().getLength()!=0 || aURL.GetPass().getLength()!=0 )
161             aURL.SetUserAndPass(aEmptyStr,aEmptyStr);
162 	}
163 
164 	// set URL-field
165     // Show the scheme, #72740
166     if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
167 		maCbbTarget.SetText( aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) );
168     else
169         maCbbTarget.SetText( aStrURL ); // #77696#
170 
171     SetScheme( aStrScheme );
172 }
173 
174 void SvxHyperlinkInternetTp::setAnonymousFTPUser()
175 {
176 	maEdLogin.SetText( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM ( sAnonymous ) ) );
177 	SvAddressParser aAddress( SvtUserOptions().GetEmail() );
178 	maEdPassword.SetText( aAddress.Count() ? aAddress.GetEmailAddress(0) : String() );
179 
180 	maFtLogin.Disable ();
181 	maFtPassword.Disable ();
182 	maEdLogin.Disable ();
183 	maEdPassword.Disable ();
184 	maCbAnonymous.Check();
185 }
186 
187 void SvxHyperlinkInternetTp::setFTPUser(const String& rUser, const String& rPassword)
188 {
189 	maEdLogin.SetText ( rUser );
190 	maEdPassword.SetText ( rPassword );
191 
192 	maFtLogin.Enable ();
193 	maFtPassword.Enable ();
194 	maEdLogin.Enable ();
195 	maEdPassword.Enable ();
196 	maCbAnonymous.Check(sal_False);
197 }
198 
199 /*************************************************************************
200 |*
201 |* retrieve and prepare data from dialog-fields
202 |*
203 |************************************************************************/
204 
205 void SvxHyperlinkInternetTp::GetCurentItemData ( String& aStrURL, String& aStrName,
206 												 String& aStrIntName, String& aStrFrame,
207 												 SvxLinkInsertMode& eMode )
208 {
209     aStrURL = CreateAbsoluteURL();
210 	GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
211 }
212 
213 String SvxHyperlinkInternetTp::CreateAbsoluteURL() const
214 {
215     String aStrURL = maCbbTarget.GetText();
216     String aScheme = GetSchemeFromURL(aStrURL);
217 
218     INetURLObject aURL(aStrURL);
219 
220 	if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
221     {
222         aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
223         aURL.SetSmartURL(aStrURL);
224     }
225 
226 	// username and password for ftp-url
227 	if( aURL.GetProtocol() == INET_PROT_FTP && maEdLogin.GetText().Len()!=0 )
228 		aURL.SetUserAndPass ( maEdLogin.GetText(), maEdPassword.GetText() );
229 
230 	if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
231         return aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET );
232     else //#105788# always create a URL even if it is not valid
233         return aStrURL;
234 }
235 
236 /*************************************************************************
237 |*
238 |* static method to create Tabpage
239 |*
240 |************************************************************************/
241 
242 IconChoicePage* SvxHyperlinkInternetTp::Create( Window* pWindow, const SfxItemSet& rItemSet )
243 {
244 	return( new SvxHyperlinkInternetTp( pWindow, rItemSet ) );
245 }
246 
247 /*************************************************************************
248 |*
249 |* Set initial focus
250 |*
251 |************************************************************************/
252 
253 void SvxHyperlinkInternetTp::SetInitFocus()
254 {
255 	maCbbTarget.GrabFocus();
256 }
257 
258 /*************************************************************************
259 |*
260 |* Contens of editfield "Taregt" modified
261 |*
262 |************************************************************************/
263 
264 IMPL_LINK ( SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, void *, EMPTYARG )
265 {
266     String aScheme = GetSchemeFromURL( maCbbTarget.GetText() );
267     if(aScheme.Len()!=0)
268         SetScheme( aScheme );
269 
270 	// start timer
271 	maTimer.SetTimeout( 2500 );
272 	maTimer.Start();
273 
274 	return( 0L );
275 }
276 
277 /*************************************************************************
278 |*
279 |* If target-field was modify, to browse the new doc afeter timeout
280 |*
281 |************************************************************************/
282 
283 IMPL_LINK ( SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, EMPTYARG )
284 {
285     RefreshMarkWindow();
286 	return( 0L );
287 }
288 
289 /*************************************************************************
290 |*
291 |* Contens of editfield "Login" modified
292 |*
293 |************************************************************************/
294 
295 IMPL_LINK ( SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, void *, EMPTYARG )
296 {
297 	String aStrLogin ( maEdLogin.GetText() );
298 	if ( aStrLogin.EqualsIgnoreCaseAscii( sAnonymous ) )
299 	{
300 		maCbAnonymous.Check();
301 		ClickAnonymousHdl_Impl(NULL);
302 	}
303 
304 	return( 0L );
305 }
306 
307 /*************************************************************************
308 |************************************************************************/
309 
310 void SvxHyperlinkInternetTp::SetScheme( const String& aScheme )
311 {
312     //if  aScheme is empty or unknown the default beaviour is like it where HTTP
313 
314     sal_Bool bFTP = aScheme.SearchAscii( sFTPScheme ) == 0;
315     sal_Bool bTelnet = sal_False;
316     if( !bFTP )
317         bTelnet = aScheme.SearchAscii( sTelnetScheme ) == 0;
318     sal_Bool bInternet = !(bFTP || bTelnet);
319 
320     //update protocol button selection:
321     maRbtLinktypFTP.Check(bFTP);
322     maRbtLinktypTelnet.Check(bTelnet);
323     maRbtLinktypInternet.Check(bInternet);
324 
325     //update target:
326     RemoveImproperProtocol(aScheme);
327     maCbbTarget.SetSmartProtocol( GetSmartProtocolFromButtons() );
328 
329     //show/hide  special fields for FTP:
330     maFtLogin.Show( bFTP );
331 	maFtPassword.Show( bFTP );
332 	maEdLogin.Show( bFTP );
333 	maEdPassword.Show( bFTP );
334 	maCbAnonymous.Show( bFTP );
335 
336     //update 'link target in document'-window and opening-button
337     if( aScheme.SearchAscii( sHTTPScheme ) == 0 || aScheme.Len() == 0 )
338     {
339         maBtTarget.Enable();
340 	    if ( mbMarkWndOpen )
341 		    ShowMarkWnd ();
342     }
343     else
344     {
345         //disable for https, ftp and telnet
346         maBtTarget.Disable();
347 		if ( mbMarkWndOpen )
348 			HideMarkWnd ();
349     }
350 }
351 
352 /*************************************************************************
353 |*
354 |* Remove protocol if it does not fit to the current button selection
355 |*
356 |************************************************************************/
357 
358 void SvxHyperlinkInternetTp::RemoveImproperProtocol(const String& aProperScheme)
359 {
360     String aStrURL ( maCbbTarget.GetText() );
361 	if ( aStrURL != aEmptyStr )
362 	{
363 		String aStrScheme = GetSchemeFromURL( aStrURL );
364 		if ( aStrScheme != aEmptyStr && aStrScheme != aProperScheme )
365 		{
366 			aStrURL.Erase ( 0, aStrScheme.Len() );
367             maCbbTarget.SetText ( aStrURL );
368 		}
369 	}
370 }
371 
372 String SvxHyperlinkInternetTp::GetSchemeFromButtons() const
373 {
374     if( maRbtLinktypFTP.IsChecked() )
375 	{
376 		return String::CreateFromAscii( INET_FTP_SCHEME );
377 	}
378 	else if( maRbtLinktypTelnet.IsChecked() )
379 	{
380         return String::CreateFromAscii( INET_TELNET_SCHEME );
381 	}
382 	return String::CreateFromAscii( INET_HTTP_SCHEME );
383 }
384 
385 INetProtocol SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
386 {
387 	if( maRbtLinktypFTP.IsChecked() )
388 	{
389 		return INET_PROT_FTP;
390 	}
391 	else if( maRbtLinktypTelnet.IsChecked() )
392 	{
393         return INET_PROT_TELNET;
394 	}
395 	return INET_PROT_HTTP;
396 }
397 
398 /*************************************************************************
399 |*
400 |* Click on Radiobutton : Internet, FTP or Telnet
401 |*
402 |************************************************************************/
403 
404 IMPL_LINK ( SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, void*, EMPTYARG )
405 {
406     String aScheme = GetSchemeFromButtons();
407     SetScheme( aScheme );
408 	return( 0L );
409 }
410 
411 /*************************************************************************
412 |*
413 |* Click on Checkbox : Anonymous user
414 |*
415 |************************************************************************/
416 
417 IMPL_LINK ( SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl, void *, EMPTYARG )
418 {
419 	// disable login-editfields if checked
420 	if ( maCbAnonymous.IsChecked() )
421 	{
422         if ( maEdLogin.GetText().ToLowerAscii().SearchAscii ( sAnonymous ) == 0 )
423         {
424             maStrOldUser = aEmptyStr;
425 	        maStrOldPassword = aEmptyStr;
426         }
427         else
428         {
429             maStrOldUser = maEdLogin.GetText();
430 	        maStrOldPassword = maEdPassword.GetText();
431         }
432 
433 		setAnonymousFTPUser();
434 	}
435 	else
436 		setFTPUser(maStrOldUser, maStrOldPassword);
437 
438 	return( 0L );
439 }
440 
441 /*************************************************************************
442 |*
443 |* Combobox Target lost the focus
444 |*
445 |************************************************************************/
446 
447 IMPL_LINK ( SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, void *, EMPTYARG )
448 {
449 	RefreshMarkWindow();
450 	return (0L);
451 }
452 
453 /*************************************************************************
454 |*
455 |* Click on imagebutton : Browse
456 |*
457 |************************************************************************/
458 
459 IMPL_LINK ( SvxHyperlinkInternetTp, ClickBrowseHdl_Impl, void *, EMPTYARG )
460 {
461 	/////////////////////////////////////////////////
462 	// Open URL if available
463 
464     SfxStringItem aName( SID_FILE_NAME, maStrStdDocURL );
465 	SfxStringItem aRefererItem( SID_REFERER, UniString::CreateFromAscii(
466 		                        RTL_CONSTASCII_STRINGPARAM( "private:user" ) ) );
467 	SfxBoolItem aNewView( SID_OPEN_NEW_VIEW, sal_True );
468 	SfxBoolItem aSilent( SID_SILENT, sal_True );
469 	SfxBoolItem aReadOnly( SID_DOC_READONLY, sal_True );
470 
471     SfxBoolItem aBrowse( SID_BROWSE, sal_True );
472 
473     const SfxPoolItem *ppItems[] = { &aName, &aNewView, &aSilent, &aReadOnly, &aRefererItem, &aBrowse, NULL };
474     (((SvxHpLinkDlg*)mpDialog)->GetBindings())->Execute( SID_OPENDOC, ppItems, 0, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD );
475 
476 	return( 0L );
477 }
478 
479 /*************************************************************************
480 |*
481 |* Click on imagebutton : Target
482 |*
483 |************************************************************************/
484 
485 IMPL_LINK ( SvxHyperlinkInternetTp, ClickTargetHdl_Impl, void *, EMPTYARG )
486 {
487 	RefreshMarkWindow();
488 	ShowMarkWnd ();
489 	mbMarkWndOpen = IsMarkWndVisible ();
490 
491 	return( 0L );
492 }
493 
494 void SvxHyperlinkInternetTp::RefreshMarkWindow()
495 {
496     if ( maRbtLinktypInternet.IsChecked() && IsMarkWndVisible() )
497     {
498         EnterWait();
499 		String aStrURL( CreateAbsoluteURL() );
500 		if ( aStrURL != aEmptyStr )
501 			mpMarkWnd->RefreshTree ( aStrURL );
502         else
503             mpMarkWnd->SetError( LERR_DOCNOTOPEN );
504         LeaveWait();
505 	}
506 
507 }
508 
509 /*************************************************************************
510 |*
511 |* Get String from Bookmark-Wnd
512 |*
513 |************************************************************************/
514 
515 void SvxHyperlinkInternetTp::SetMarkStr ( String& aStrMark )
516 {
517 	String aStrURL ( maCbbTarget.GetText() );
518 
519 	const sal_Unicode sUHash = '#';
520 	xub_StrLen nPos = aStrURL.SearchBackward( sUHash );
521 
522 	if( nPos != STRING_NOTFOUND )
523 		aStrURL.Erase ( nPos );
524 
525 	aStrURL += sUHash;
526 	aStrURL += aStrMark;
527 
528 	maCbbTarget.SetText ( aStrURL );
529 }
530 
531 /*************************************************************************
532 |*
533 |* Enable Browse-Button in subject to the office is in onlinemode
534 |*
535 |************************************************************************/
536 
537 void SvxHyperlinkInternetTp::SetOnlineMode( sal_Bool /*bEnable*/ )
538 {
539 	// State of target-button in subject to the current url-string
540 	// ( Can't display any targets in an document, if there is no
541 	//   valid url to a document )
542 	String aStrCurrentTarget( maCbbTarget.GetText() );
543 	aStrCurrentTarget.EraseTrailingChars();
544 
545 	if( aStrCurrentTarget == aEmptyStr                ||
546 		aStrCurrentTarget.EqualsIgnoreCaseAscii( sHTTPScheme )  ||
547 		aStrCurrentTarget.EqualsIgnoreCaseAscii( sHTTPSScheme ) )
548 		maBtTarget.Enable( sal_False );
549 	else
550 		maBtTarget.Enable( sal_True );
551 }
552