bump product version to 5.0.4.1
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiEditSettingDialog.java
blobaef6cb6a29b63b0cfd5fb4fbec3cb81a7a8c5dac
1 /*
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;
22 import java.util.Map;
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>();
50 addMode = true;
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 );
61 setting = ht;
63 boolean bInitSaveCheckBox = false;
65 try
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 )
84 ex.printStackTrace();
87 addMode = false;
88 m_bAllowURLChange = bAllowURLChange;
90 InsertThrobber( 184, 20, 10, 10 );
91 InitStrings( xContext );
92 InitSaveCheckbox( xContext, bInitSaveCheckBox );
95 @Override
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 ) );
118 catch( Exception e )
120 e.printStackTrace();
123 return bResult;
126 private void EnableControls( boolean bEnable )
128 if ( !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 ) );
138 if ( 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" );
144 else
145 SetFocusTo( "UsernameField" );
147 else
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 ) );
172 catch( Exception e )
174 e.printStackTrace();
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 ) ) );
186 catch( Exception e )
188 e.printStackTrace();
192 private void DoLogin()
194 String sRedirectURL = "";
195 String sURL = "";
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 )
210 sURL = sRedirectURL;
211 sRedirectURL = "";
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 );
220 bInitHost = false;
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 )
233 //the URL is valid
234 String sMainURL = Helper.GetMainURL( sWebPage, sURL );
236 if ( sMainURL.length() == 0 )
238 // TODO:
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 )
244 // show error
245 Helper.ShowError( m_xContext,
246 m_xDialog,
247 Helper.DLG_MEDIAWIKI_TITLE,
248 Helper.NOURLCONNECTION_ERROR,
249 sURL,
250 false );
253 else
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
261 // show error
262 Helper.ShowError( m_xContext,
263 m_xDialog,
264 Helper.DLG_MEDIAWIKI_TITLE,
265 Helper.WRONGLOGIN_ERROR,
266 null,
267 false );
269 else
271 setting.put( "Url", aMainURI.getEscapedURI() );
272 setting.put( "Username", sUserName );
273 setting.put( "Password", sPassword );
274 if ( addMode )
276 // no cleaning of the settings is necessary
277 Settings.getSettings( m_xContext ).addWikiCon( setting );
278 Settings.getSettings( m_xContext ).storeConfiguration();
281 m_bAction = true;
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";
292 bAllowIndex = false;
294 else
296 // URL invalid
297 // show error
298 Helper.ShowError( m_xContext,
299 m_xDialog,
300 Helper.DLG_MEDIAWIKI_TITLE,
301 Helper.INVALIDURL_ERROR,
302 null,
303 false );
307 else
309 // URL field empty
310 // show error
311 Helper.ShowError( m_xContext,
312 m_xDialog,
313 Helper.DLG_MEDIAWIKI_TITLE,
314 Helper.NOURL_ERROR,
315 null,
316 false );
318 } while ( sRedirectURL.length() > 0 );
320 catch ( WikiCancelException ce )
323 catch ( SSLException essl )
325 if ( Helper.IsConnectionAllowed() )
327 Helper.ShowError( m_xContext,
328 m_xDialog,
329 Helper.DLG_MEDIAWIKI_TITLE,
330 Helper.UNKNOWNCERT_ERROR,
331 null,
332 false );
334 essl.printStackTrace();
336 catch ( Exception ex )
338 if ( Helper.IsConnectionAllowed() )
340 Helper.ShowError( m_xContext,
341 m_xDialog,
342 Helper.DLG_MEDIAWIKI_TITLE,
343 Helper.NOURLCONNECTION_ERROR,
344 sURL,
345 false );
347 ex.printStackTrace();
351 @Override
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" )
369 @Override
370 public void run()
374 Thread.yield();
375 } catch( java.lang.Exception e ){}
377 DoLogin();
378 WikiEditSettingDialog.this.EnableControls( true );
379 WikiEditSettingDialog.this.SetThrobberActive( false );
380 WikiEditSettingDialog.this.SetThrobberVisible( false );
382 ThreadStop( true );
384 if ( m_bAction )
385 MainThreadDialogExecutor.Close( xContext, xDialogForThread );
389 m_aThread.start();
391 else
395 DoLogin();
396 } catch( java.lang.Exception e )
398 finally
400 EnableControls( true );
401 SetThrobberActive( false );
402 SetThrobberVisible( false );
404 if ( m_bAction )
405 xDialog.endExecute();
407 Helper.AllowConnection( true );
411 return true;
414 return false;
417 @Override
418 public void windowClosed( EventObject e )
420 ThreadStop( false );