2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.wiki
;
21 import java
.util
.HashMap
;
24 import javax
.net
.ssl
.SSLException
;
26 import org
.apache
.commons
.httpclient
.HostConfiguration
;
27 import org
.apache
.commons
.httpclient
.URI
;
28 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
30 import com
.sun
.star
.awt
.XDialog
;
31 import com
.sun
.star
.beans
.XPropertySet
;
32 import com
.sun
.star
.lang
.EventObject
;
33 import com
.sun
.star
.uno
.XComponentContext
;
35 public class WikiEditSettingDialog
extends WikiDialog
38 private static final String sOKMethod
= "OK";
40 private final String
[] Methods
= {sOKMethod
};
41 private final Map
<String
,String
> setting
;
42 private final boolean addMode
;
43 private boolean m_bAllowURLChange
= true;
45 public WikiEditSettingDialog( XComponentContext xContext
, String DialogURL
)
47 super( xContext
, DialogURL
);
48 super.setMethods( Methods
);
49 setting
= new HashMap
<String
,String
>();
52 InsertThrobber( 184, 20, 10, 10 );
53 InitStrings( xContext
);
54 InitSaveCheckbox( xContext
, false );
57 public WikiEditSettingDialog( XComponentContext xContext
, String DialogURL
, Map
<String
,String
> ht
, boolean bAllowURLChange
)
59 super( xContext
, DialogURL
);
60 super.setMethods( Methods
);
63 boolean bInitSaveCheckBox
= false;
67 XPropertySet xUrlField
= GetPropSet( "UrlField" );
69 xUrlField
.setPropertyValue( "Text", ht
.get( "Url" ) );
71 GetPropSet( "UsernameField" ).setPropertyValue( "Text", ht
.get( "Username" ) );
73 if ( Helper
.PasswordStoringIsAllowed( m_xContext
) )
75 String
[] pPasswords
= Helper
.GetPasswordsForURLAndUser( m_xContext
, ht
.get( "Url" ), ht
.get( "Username" ) );
76 bInitSaveCheckBox
= ( pPasswords
!= null && pPasswords
.length
> 0 && pPasswords
[0].equals( ht
.get( "Password" ) ) );
79 // the password should be entered by the user or the Cancel should be pressed
80 // GetPropSet( "PasswordField" ).setPropertyValue( "Text", ht.get( "Password" ));
82 catch ( Exception ex
)
88 m_bAllowURLChange
= bAllowURLChange
;
90 InsertThrobber( 184, 20, 10, 10 );
91 InitStrings( xContext
);
92 InitSaveCheckbox( xContext
, bInitSaveCheckBox
);
96 public boolean show( )
98 SetThrobberVisible( false );
99 EnableControls( true );
100 boolean bResult
= super.show();
104 if ( bResult
&& Helper
.PasswordStoringIsAllowed( m_xContext
)
105 && ( (Short
)( GetPropSet( "SaveBox" ).getPropertyValue("State") ) ).shortValue() != (short)0 )
107 String sURL
= setting
.get( "Url" );
108 String sUserName
= setting
.get( "Username" );
109 String sPassword
= setting
.get( "Password" );
111 if ( sURL
!= null && sURL
.length() > 0 && sUserName
!= null && sUserName
.length() > 0 && sPassword
!= null && sPassword
.length() > 0 )
113 String
[] pPasswords
= { sPassword
};
114 Helper
.GetPasswordContainer( m_xContext
).addPersistent( sURL
, sUserName
, pPasswords
, Helper
.GetInteractionHandler( m_xContext
) );
126 private void EnableControls( boolean bEnable
)
129 SetFocusTo( "CancelButton" );
133 GetPropSet( "UsernameField" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
134 GetPropSet( "PasswordField" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
135 GetPropSet( "OkButton" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
136 GetPropSet( "HelpButton" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
140 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean
.valueOf( m_bAllowURLChange
) );
141 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean
.valueOf( Helper
.PasswordStoringIsAllowed( m_xContext
) ) );
142 if ( m_bAllowURLChange
)
143 SetFocusTo( "UrlField" );
145 SetFocusTo( "UsernameField" );
149 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean
.FALSE
);
150 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean
.FALSE
);
153 catch ( Exception ex
)
155 ex
.printStackTrace();
159 private void InitStrings( XComponentContext xContext
)
163 SetTitle( Helper
.GetLocalizedString( xContext
, Helper
.DLG_MEDIAWIKI_TITLE
) );
164 GetPropSet( "UrlLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_URLLABEL
) );
165 GetPropSet( "UsernameLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_USERNAMELABEL
) );
166 GetPropSet( "PasswordLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_PASSWORDLABEL
) );
167 GetPropSet( "AccountLine" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_ACCOUNTLINE
) );
168 GetPropSet( "WikiLine" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_WIKILINE
) );
169 GetPropSet( "SaveBox" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_SAVEBOX
) );
170 GetPropSet( "OkButton" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_OK
) );
178 private void InitSaveCheckbox( XComponentContext xContext
, boolean bInitSaveCheckBox
)
180 XPropertySet xSaveCheck
= GetPropSet( "SaveBox" );
183 xSaveCheck
.setPropertyValue( "State", Short
.valueOf( bInitSaveCheckBox ?
(short)1 : (short)0 ) );
184 xSaveCheck
.setPropertyValue( "Enabled", Boolean
.valueOf( Helper
.PasswordStoringIsAllowed( xContext
) ) );
192 private void DoLogin()
194 String sRedirectURL
= "";
198 sURL
= ( String
) GetPropSet( "UrlField" ).getPropertyValue( "Text" );
199 String sUserName
= ( String
) GetPropSet( "UsernameField" ).getPropertyValue( "Text" );
200 String sPassword
= ( String
) GetPropSet( "PasswordField" ).getPropertyValue( "Text" );
202 HostConfiguration aHostConfig
= new HostConfiguration();
203 boolean bInitHost
= true;
204 boolean bAllowIndex
= true;
208 if ( sRedirectURL
.length() > 0 )
214 if ( sURL
.length() > 0 )
216 URI aURI
= new URI( sURL
, false );
217 GetMethod aRequest
= new GetMethod( aURI
.getEscapedPathQuery() );
218 aRequest
.setFollowRedirects( false );
219 Helper
.ExecuteMethod( aRequest
, aHostConfig
, aURI
, m_xContext
, bInitHost
);
222 int nResultCode
= aRequest
.getStatusCode();
223 String sWebPage
= null;
224 if ( nResultCode
== 200 )
225 sWebPage
= aRequest
.getResponseBodyAsString();
226 else if ( nResultCode
>= 301 && nResultCode
<= 303 || nResultCode
== 307 )
227 sRedirectURL
= aRequest
.getResponseHeader( "Location" ).getValue();
229 aRequest
.releaseConnection();
231 if ( sWebPage
!= null && sWebPage
.length() > 0 )
234 String sMainURL
= Helper
.GetMainURL( sWebPage
, sURL
);
236 if ( sMainURL
.length() == 0 )
239 // it's not a Wiki Page, check first whether a redirect is requested
240 // happens usually in case of https
241 sRedirectURL
= Helper
.GetRedirectURL( sWebPage
, sURL
);
242 if ( sRedirectURL
.length() == 0 )
245 Helper
.ShowError( m_xContext
,
247 Helper
.DLG_MEDIAWIKI_TITLE
,
248 Helper
.NOURLCONNECTION_ERROR
,
255 URI aMainURI
= new URI( sMainURL
, true ); // it must be an escaped URL, otherwise an exception should be thrown
257 if ( ( sUserName
.length() > 0 || sPassword
.length() > 0 )
258 && Helper
.Login( aMainURI
, sUserName
, sPassword
, m_xContext
) == null )
260 // a wrong login information is provided
262 Helper
.ShowError( m_xContext
,
264 Helper
.DLG_MEDIAWIKI_TITLE
,
265 Helper
.WRONGLOGIN_ERROR
,
271 setting
.put( "Url", aMainURI
.getEscapedURI() );
272 setting
.put( "Username", sUserName
);
273 setting
.put( "Password", sPassword
);
276 // no cleaning of the settings is necessary
277 Settings
.getSettings( m_xContext
).addWikiCon( setting
);
278 Settings
.getSettings( m_xContext
).storeConfiguration();
285 else if ( sRedirectURL
== null || sRedirectURL
.length() == 0 )
287 if ( sURL
.length() > 0 && !sURL
.endsWith( "index.php" ) && bAllowIndex
)
289 // the used MainURL is not always directly accessible
290 // add the suffix as workaround, but only once
291 sRedirectURL
= sURL
+ "/index.php";
298 Helper
.ShowError( m_xContext
,
300 Helper
.DLG_MEDIAWIKI_TITLE
,
301 Helper
.INVALIDURL_ERROR
,
311 Helper
.ShowError( m_xContext
,
313 Helper
.DLG_MEDIAWIKI_TITLE
,
318 } while ( sRedirectURL
.length() > 0 );
320 catch ( WikiCancelException ce
)
323 catch ( SSLException essl
)
325 if ( Helper
.IsConnectionAllowed() )
327 Helper
.ShowError( m_xContext
,
329 Helper
.DLG_MEDIAWIKI_TITLE
,
330 Helper
.UNKNOWNCERT_ERROR
,
334 essl
.printStackTrace();
336 catch ( Exception ex
)
338 if ( Helper
.IsConnectionAllowed() )
340 Helper
.ShowError( m_xContext
,
342 Helper
.DLG_MEDIAWIKI_TITLE
,
343 Helper
.NOURLCONNECTION_ERROR
,
347 ex
.printStackTrace();
352 public boolean callHandlerMethod( XDialog xDialog
, Object EventObject
, String MethodName
)
354 if ( MethodName
.equals( sOKMethod
) )
356 EnableControls( false );
357 SetThrobberVisible( true );
358 SetThrobberActive( true );
360 if ( Helper
.AllowThreadUsage( m_xContext
) )
362 final XDialog xDialogForThread
= xDialog
;
363 final XComponentContext xContext
= m_xContext
;
365 // the thread name is used to allow the error dialogs
366 m_bThreadFinished
= false;
367 m_aThread
= new Thread( "com.sun.star.thread.WikiEditorSendingThread" )
375 } catch( java
.lang
.Exception e
){}
378 WikiEditSettingDialog
.this.EnableControls( true );
379 WikiEditSettingDialog
.this.SetThrobberActive( false );
380 WikiEditSettingDialog
.this.SetThrobberVisible( false );
385 MainThreadDialogExecutor
.Close( xContext
, xDialogForThread
);
396 } catch( java
.lang
.Exception e
)
400 EnableControls( true );
401 SetThrobberActive( false );
402 SetThrobberVisible( false );
405 xDialog
.endExecute();
407 Helper
.AllowConnection( true );
418 public void windowClosed( EventObject e
)