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