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
;
25 import java
.net
.HttpURLConnection
;
26 import javax
.net
.ssl
.SSLException
;
28 import com
.sun
.star
.awt
.XDialog
;
29 import com
.sun
.star
.beans
.XPropertySet
;
30 import com
.sun
.star
.lang
.EventObject
;
31 import com
.sun
.star
.uno
.XComponentContext
;
33 public class WikiEditSettingDialog
extends WikiDialog
36 private static final String sOKMethod
= "OK";
38 private final String
[] Methods
= {sOKMethod
};
39 private final Map
<String
,String
> setting
;
40 private final boolean addMode
;
41 private boolean m_bAllowURLChange
= true;
43 public WikiEditSettingDialog( XComponentContext xContext
, String DialogURL
)
45 super( xContext
, DialogURL
);
46 super.setMethods( Methods
);
47 setting
= new HashMap
<String
,String
>();
50 InsertThrobber( 184, 20, 10, 10 );
51 InitStrings( xContext
);
52 InitSaveCheckbox( xContext
, false );
55 public WikiEditSettingDialog( XComponentContext xContext
, String DialogURL
, Map
<String
,String
> ht
, boolean bAllowURLChange
)
57 super( xContext
, DialogURL
);
58 super.setMethods( Methods
);
61 boolean bInitSaveCheckBox
= false;
65 XPropertySet xUrlField
= GetPropSet( "UrlField" );
67 xUrlField
.setPropertyValue( "Text", ht
.get( "Url" ) );
69 GetPropSet( "UsernameField" ).setPropertyValue( "Text", ht
.get( "Username" ) );
71 if ( Helper
.PasswordStoringIsAllowed( m_xContext
) )
73 String
[] pPasswords
= Helper
.GetPasswordsForURLAndUser( m_xContext
, ht
.get( "Url" ), ht
.get( "Username" ) );
74 bInitSaveCheckBox
= ( pPasswords
!= null && pPasswords
.length
> 0 && pPasswords
[0].equals( ht
.get( "Password" ) ) );
77 // the password should be entered by the user or the Cancel should be pressed
78 // GetPropSet( "PasswordField" ).setPropertyValue( "Text", ht.get( "Password" ));
80 catch ( Exception ex
)
86 m_bAllowURLChange
= bAllowURLChange
;
88 InsertThrobber( 184, 20, 10, 10 );
89 InitStrings( xContext
);
90 InitSaveCheckbox( xContext
, bInitSaveCheckBox
);
94 public boolean show( )
96 SetThrobberVisible( false );
97 EnableControls( true );
98 boolean bResult
= super.show();
102 if ( bResult
&& Helper
.PasswordStoringIsAllowed( m_xContext
)
103 && ( (Short
)( GetPropSet( "SaveBox" ).getPropertyValue("State") ) ).shortValue() != (short)0 )
105 String sURL
= setting
.get( "Url" );
106 String sUserName
= setting
.get( "Username" );
107 String sPassword
= setting
.get( "Password" );
109 if ( sURL
!= null && sURL
.length() > 0 && sUserName
!= null && sUserName
.length() > 0 && sPassword
!= null && sPassword
.length() > 0 )
111 String
[] pPasswords
= { sPassword
};
112 Helper
.GetPasswordContainer( m_xContext
).addPersistent( sURL
, sUserName
, pPasswords
, Helper
.GetInteractionHandler( m_xContext
) );
124 private void EnableControls( boolean bEnable
)
127 SetFocusTo( "CancelButton" );
131 GetPropSet( "UsernameField" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
132 GetPropSet( "PasswordField" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
133 GetPropSet( "OkButton" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
134 GetPropSet( "HelpButton" ).setPropertyValue( "Enabled", Boolean
.valueOf( bEnable
) );
138 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean
.valueOf( m_bAllowURLChange
) );
139 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean
.valueOf( Helper
.PasswordStoringIsAllowed( m_xContext
) ) );
140 if ( m_bAllowURLChange
)
141 SetFocusTo( "UrlField" );
143 SetFocusTo( "UsernameField" );
147 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean
.FALSE
);
148 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean
.FALSE
);
151 catch ( Exception ex
)
153 ex
.printStackTrace();
157 private void InitStrings( XComponentContext xContext
)
161 SetTitle( Helper
.GetLocalizedString( xContext
, Helper
.DLG_MEDIAWIKI_TITLE
) );
162 GetPropSet( "UrlLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_URLLABEL
) );
163 GetPropSet( "UsernameLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_USERNAMELABEL
) );
164 GetPropSet( "PasswordLabel" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_PASSWORDLABEL
) );
165 GetPropSet( "AccountLine" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_ACCOUNTLINE
) );
166 GetPropSet( "WikiLine" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_WIKILINE
) );
167 GetPropSet( "SaveBox" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_EDITSETTING_SAVEBOX
) );
168 GetPropSet( "OkButton" ).setPropertyValue( "Label", Helper
.GetLocalizedString( xContext
, Helper
.DLG_OK
) );
176 private void InitSaveCheckbox( XComponentContext xContext
, boolean bInitSaveCheckBox
)
178 XPropertySet xSaveCheck
= GetPropSet( "SaveBox" );
181 xSaveCheck
.setPropertyValue( "State", Short
.valueOf( bInitSaveCheckBox ?
(short)1 : (short)0 ) );
182 xSaveCheck
.setPropertyValue( "Enabled", Boolean
.valueOf( Helper
.PasswordStoringIsAllowed( xContext
) ) );
190 private void DoLogin()
192 String sRedirectURL
= "";
196 sURL
= ( String
) GetPropSet( "UrlField" ).getPropertyValue( "Text" );
197 String sUserName
= ( String
) GetPropSet( "UsernameField" ).getPropertyValue( "Text" );
198 String sPassword
= ( String
) GetPropSet( "PasswordField" ).getPropertyValue( "Text" );
200 boolean bAllowIndex
= true;
204 if ( sRedirectURL
.length() > 0 )
210 if ( sURL
.length() > 0 )
212 URI aURI
= new URI(sURL
);
213 HttpURLConnection connGet
= Helper
.PrepareMethod("GET", aURI
, m_xContext
);
214 connGet
.setInstanceFollowRedirects(false);
217 int nResultCode
= connGet
.getResponseCode();
218 String sWebPage
= null;
219 if ( nResultCode
== 200 )
220 sWebPage
= Helper
.ReadResponseBody(connGet
);
221 else if ( nResultCode
>= 301 && nResultCode
<= 303 || nResultCode
== 307 )
222 sRedirectURL
= connGet
.getHeaderField("Location");
224 if ( sWebPage
!= null && sWebPage
.length() > 0 )
227 String sMainURL
= Helper
.GetMainURL( sWebPage
, sURL
);
229 if ( sMainURL
.length() == 0 )
232 // it's not a Wiki Page, check first whether a redirect is requested
233 // happens usually in case of https
234 sRedirectURL
= Helper
.GetRedirectURL( sWebPage
, sURL
);
235 if ( sRedirectURL
.length() == 0 )
238 Helper
.ShowError( m_xContext
,
240 Helper
.DLG_MEDIAWIKI_TITLE
,
241 Helper
.NOURLCONNECTION_ERROR
,
248 URI aMainURI
= new URI(sMainURL
);
250 if ( ( sUserName
.length() > 0 || sPassword
.length() > 0 )
251 && !Helper
.Login(aMainURI
, sUserName
, sPassword
, m_xContext
))
253 // a wrong login information is provided
255 Helper
.ShowError( m_xContext
,
257 Helper
.DLG_MEDIAWIKI_TITLE
,
258 Helper
.WRONGLOGIN_ERROR
,
264 setting
.put( "Url", aMainURI
.toASCIIString() );
265 setting
.put( "Username", sUserName
);
266 setting
.put( "Password", sPassword
);
269 // no cleaning of the settings is necessary
270 Settings
.getSettings( m_xContext
).addWikiCon( setting
);
271 Settings
.getSettings( m_xContext
).storeConfiguration();
278 else if ( sRedirectURL
== null || sRedirectURL
.length() == 0 )
280 if ( sURL
.length() > 0 && !sURL
.endsWith( "index.php" ) && bAllowIndex
)
282 // the used MainURL is not always directly accessible
283 // add the suffix as workaround, but only once
284 sRedirectURL
= sURL
+ "/index.php";
291 Helper
.ShowError( m_xContext
,
293 Helper
.DLG_MEDIAWIKI_TITLE
,
294 Helper
.INVALIDURL_ERROR
,
304 Helper
.ShowError( m_xContext
,
306 Helper
.DLG_MEDIAWIKI_TITLE
,
311 } while (sRedirectURL
!= null && sRedirectURL
.length() > 0);
313 catch ( WikiCancelException ce
)
316 catch ( SSLException essl
)
318 if ( Helper
.IsConnectionAllowed() )
320 Helper
.ShowError( m_xContext
,
322 Helper
.DLG_MEDIAWIKI_TITLE
,
323 Helper
.UNKNOWNCERT_ERROR
,
327 essl
.printStackTrace();
329 catch ( Exception ex
)
331 if ( Helper
.IsConnectionAllowed() )
333 Helper
.ShowError( m_xContext
,
335 Helper
.DLG_MEDIAWIKI_TITLE
,
336 Helper
.NOURLCONNECTION_ERROR
,
340 ex
.printStackTrace();
345 public boolean callHandlerMethod( XDialog xDialog
, Object EventObject
, String MethodName
)
347 if ( MethodName
.equals( sOKMethod
) )
349 EnableControls( false );
350 SetThrobberVisible( true );
351 SetThrobberActive( true );
353 if ( Helper
.AllowThreadUsage( m_xContext
) )
355 final XDialog xDialogForThread
= xDialog
;
356 final XComponentContext xContext
= m_xContext
;
358 // the thread name is used to allow the error dialogs
359 m_bThreadFinished
= false;
360 m_aThread
= new Thread( "com.sun.star.thread.WikiEditorSendingThread" )
368 } catch( java
.lang
.Exception e
){}
371 WikiEditSettingDialog
.this.EnableControls( true );
372 WikiEditSettingDialog
.this.SetThrobberActive( false );
373 WikiEditSettingDialog
.this.SetThrobberVisible( false );
378 MainThreadDialogExecutor
.Close( xContext
, xDialogForThread
);
389 } catch( java
.lang
.Exception e
)
393 EnableControls( true );
394 SetThrobberActive( false );
395 SetThrobberVisible( false );
398 xDialog
.endExecute();
400 Helper
.AllowConnection( true );
411 public void windowClosed( EventObject e
)