diff options
author | Mikhail Voitenko <mav@openoffice.org> | 2007-11-28 10:15:42 +0000 |
---|---|---|
committer | Mikhail Voitenko <mav@openoffice.org> | 2007-11-28 10:15:42 +0000 |
commit | 34e59cafbaa112cd4e8be1f99e0f881da354582c (patch) | |
tree | b8ce3d5cb2a7270909c816851370ac79fbd81d51 /swext/mediawiki/src/com | |
parent | eb0b59c122e30dbfd31435a0f59c3d6d0c8727a3 (diff) |
initial commit
Diffstat (limited to 'swext/mediawiki/src/com')
12 files changed, 3374 insertions, 0 deletions
diff --git a/swext/mediawiki/src/com/sun/star/wiki/EditPageParser.java b/swext/mediawiki/src/com/sun/star/wiki/EditPageParser.java new file mode 100644 index 000000000000..7e7c2edec80b --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/EditPageParser.java @@ -0,0 +1,166 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: EditPageParser.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:13:17 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import javax.swing.text.html.*; +import javax.swing.text.MutableAttributeSet; + +public class EditPageParser extends HTMLEditorKit.ParserCallback +{ + + protected String m_sEditTime = ""; + protected String m_sEditToken = ""; + private int m_nWikiArticleHash = 0; + private boolean m_bHTMLStartFound = false; + protected int m_nWikiArticleStart = -1; + protected int m_nWikiArticleEnd = -1; + protected int m_nHTMLArticleStart = -1; + protected int m_nHTMLArticleEnd = -1; + protected int m_nNoArticleInd = -1; + protected int m_nErrorInd = -1; + + /** Creates a new instance of WikiHTMLParser */ + public EditPageParser() + { + } + + public void handleComment(char[] data,int pos) + { + // insert code to handle comments + } + + public void handleEndTag(HTML.Tag t,int pos) + { + if (t == HTML.Tag.TEXTAREA) + { + m_nWikiArticleEnd = pos; + } + if (t == HTML.Tag.DIV) + { + if (m_bHTMLStartFound) + { + m_nHTMLArticleStart = pos+6; + m_bHTMLStartFound = false; + } + } + } + + public void handleError(String errorMsg,int pos) + { + //System.out.println(errorMsg); + } + + public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a,int pos) + { + // insert code to handle simple tags + + if (t == HTML.Tag.INPUT) + { + String sName = (String) a.getAttribute(HTML.Attribute.NAME); + if (sName != null) + { + if (sName.equalsIgnoreCase("wpEdittime")) + { + this.m_sEditTime = (String) a.getAttribute(HTML.Attribute.VALUE); + } + if (sName.equalsIgnoreCase("wpEditToken")) + { + this.m_sEditToken = (String) a.getAttribute(HTML.Attribute.VALUE); + } + } + + } + + } + + public void handleStartTag(HTML.Tag t, MutableAttributeSet a,int pos) + { + // insert code to handle starting tags + String sName = ""; + String sId = ""; + String sClass = ""; + + if (t == HTML.Tag.TEXTAREA) + { + sName = (String) a.getAttribute(HTML.Attribute.NAME); + if (sName != null) + { + if (sName.equalsIgnoreCase("wpTextbox1")) + { + m_nWikiArticleHash = t.hashCode(); + m_nWikiArticleStart = pos; + } + } + } + else if (t == HTML.Tag.DIV) + { + sId = (String) a.getAttribute(HTML.Attribute.ID); + sClass = (String) a.getAttribute(HTML.Attribute.CLASS); + if (sId != null) + { + if (sId.equalsIgnoreCase("contentSub")) + { + m_bHTMLStartFound = true; + } + } + if (sClass != null) + { + if (sClass.equalsIgnoreCase("printfooter")) + { + m_nHTMLArticleEnd = pos; + } + else if ( sClass.equalsIgnoreCase( "noarticletext" ) ) + { + m_nNoArticleInd = pos; + } + else if ( sClass.equalsIgnoreCase( "errorbox" ) ) + { + m_nErrorInd = pos; + } + } + } + else if ( t == HTML.Tag.P ) + { + sClass = (String) a.getAttribute(HTML.Attribute.CLASS); + if ( sClass != null && sClass.equalsIgnoreCase( "error" ) ) + { + m_nErrorInd = pos; + } + } + } + + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/Helper.java b/swext/mediawiki/src/com/sun/star/wiki/Helper.java new file mode 100644 index 000000000000..1a907dd8a572 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/Helper.java @@ -0,0 +1,611 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: Helper.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:13:38 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XDialog; +import com.sun.star.beans.NamedValue; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.XContainerQuery; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XNameAccess; +import com.sun.star.document.XDocumentInfoSupplier; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XModuleManager; +import com.sun.star.io.XInputStream; +import com.sun.star.io.XOutputStream; +import com.sun.star.io.XSeekable; +import com.sun.star.io.XStream; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.system.SystemShellExecuteFlags; +import com.sun.star.system.XSystemShellExecute; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import java.net.*; +import java.io.*; +import java.util.Hashtable; +import java.util.Random; +import javax.net.ssl.SSLException; +import javax.swing.text.html.HTMLEditorKit; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.cookie.CookiePolicy; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.URI; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; + +public class Helper +{ + + private static final String sHTMLHeader = "<HTML><HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><TITLE></TITLE></HEAD><BODY>"; + private static final String sHTMLFooter = "</BODY></HTML>"; + + private static Random m_aRandom; + private static MultiThreadedHttpConnectionManager m_aConnectionManager; + private static HttpClient m_aClient; + private static boolean m_bAllowConnection = true; + private static Hashtable m_aAcceptedUnknownCerts; + + synchronized protected static HttpClient GetHttpClient() + throws WikiCancelException + { + if ( !m_bAllowConnection ) + throw new WikiCancelException(); + + if ( m_aConnectionManager == null ) + m_aConnectionManager = new MultiThreadedHttpConnectionManager(); + + if ( m_aClient == null ) + { + m_aClient = new HttpClient( m_aConnectionManager ); + m_aClient.getParams().setParameter( "http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY ); + m_aClient.getParams().setParameter( "http.protocol.single-cookie-header", Boolean.TRUE ); + m_aClient.getParams().setParameter( "http.protocol.content-charset", "UTF-8" ); + } + + return m_aClient; + } + + synchronized protected static void AllowConnection( boolean bAllow ) + { + m_bAllowConnection = bAllow; + if ( !bAllow && m_aConnectionManager != null ) + { + m_aClient = null; + m_aConnectionManager.shutdown(); + m_aConnectionManager = null; + } + } + + synchronized protected static boolean IsConnectionAllowed() + { + return m_bAllowConnection; + } + + protected static Protocol GetOwnHttps( int nPort ) + { + return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort < 0 ) ? 443 : nPort ) ); + } + + protected static String GetMainURL( String sWebPage, String sVURL ) + { + //scrape the HTML source and find the EditURL + String sResultURL = ""; + int i = sWebPage.indexOf( "action=edit" ); + if ( i!=-1 ) + { + int t = sWebPage.lastIndexOf( "a href=", i ); + int z = sWebPage.indexOf( "index.php", t ); + sResultURL = sWebPage.substring( t+8,z ); + try + { + if ( !sResultURL.startsWith( "http" )) + { + //if the url is only relative then complete it + URL aURL = new URL( sVURL ); + sResultURL = aURL.getProtocol()+"://"+aURL.getHost()+sResultURL; + } + } + catch ( MalformedURLException ex ) + { + ex.printStackTrace(); + } + } + + return sResultURL; + } + + protected static String GetRedirectURL( String sWebPage, String sURL ) + { + //scrape the HTML source and find the EditURL + String sResultURL = ""; + int nInd = sWebPage.indexOf( "http-equiv=\"refresh\"" ); + if ( nInd != -1 ) + { + int nContent = sWebPage.indexOf( "content=", nInd ); + if ( nContent > 0 ) + { + int nURL = sWebPage.indexOf( "URL=", nContent ); + if ( nURL > 0 ) + { + int nEndURL = sWebPage.indexOf( "\"", nURL ); + if ( nEndURL > 0 ) + sResultURL = sWebPage.substring( nURL + 4, nEndURL ); + } + } + + try + { + URL aURL = new URL( sURL ); + if ( !sResultURL.startsWith( aURL.getProtocol() )) + { + //if the url is only relative then complete it + if ( sResultURL.startsWith( "/" ) ) + sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + sResultURL; + else + sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + aURL.getPath() + sResultURL; + } + } + catch ( MalformedURLException ex ) + { + ex.printStackTrace(); + } + } + + return sResultURL; + + } + + protected static XInputStream SaveHTMLTemp( XComponentContext xContext, String sArticle ) + { + XInputStream xResult = null; + + if ( xContext != null ) + { + try + { + Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); + XStream xStream = ( XStream ) UnoRuntime.queryInterface( XStream.class, oTempFile ); + XSeekable xSeekable = ( XSeekable ) UnoRuntime.queryInterface( XSeekable.class, oTempFile ); + if ( xStream != null && xSeekable != null ) + { + XOutputStream xOutputStream = xStream.getOutputStream(); + XInputStream xInputStream = xStream.getInputStream(); + if ( xOutputStream != null && xInputStream != null ) + { + String sHTML = sHTMLHeader.concat( sArticle ); + sHTML = sHTML.concat( sHTMLFooter ); + xOutputStream.writeBytes( sHTML.getBytes( "UTF-8" ) ); + // xOutputStream.closeOutput(); + xSeekable.seek( 0 ); + + xResult = xInputStream; + } + } + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + + return xResult; + } + + + protected static String CreateTempFile( XComponentContext xContext ) + { + String sURL = ""; + try + { + Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); + XPropertySet xPropertySet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, oTempFile ); + xPropertySet.setPropertyValue( "RemoveFile", Boolean.FALSE ); + sURL = ( String ) xPropertySet.getPropertyValue( "Uri" ); + + XInputStream xInputStream = ( XInputStream ) UnoRuntime.queryInterface( XInputStream.class, oTempFile ); + xInputStream.closeInput(); + XOutputStream xOutputStream = ( XOutputStream ) UnoRuntime.queryInterface( XOutputStream.class, oTempFile ); + xOutputStream.closeOutput(); + } catch ( com.sun.star.uno.Exception ex ) + { + ex.printStackTrace(); + } + return sURL; + } + + protected static String EachLine( String sURL ) + { + String sText = ""; + try + { + URL aURL = new URL( sURL ); + File aFile = new File( aURL.getFile() ); + InputStreamReader aInputReader = new InputStreamReader( new FileInputStream( aFile ), "UTF-8" ); + BufferedReader aBufReader = new BufferedReader( aInputReader ); + + StringBuffer aBuf = new StringBuffer(); + String sEachLine = aBufReader.readLine(); + + while( sEachLine != null ) + { + aBuf.append( sEachLine ); + aBuf.append( "\n" ); + + sEachLine = aBufReader.readLine(); + } + sText = aBuf.toString(); + } catch ( Exception e ) + { + e.printStackTrace(); + } + return sText; + } + + protected static String GetDocTitle( XModel xDoc ) + { + String sTitle = ""; + XDocumentInfoSupplier xDocInfoSup = ( XDocumentInfoSupplier ) UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); + XPropertySet xPropSet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); + try + { + sTitle = ( String ) xPropSet.getPropertyValue( "Title" ); + } catch ( Exception ex ) + { + ex.printStackTrace(); + } + return sTitle; + } + + protected static void SetDocTitle( XModel xDoc, String sTitle ) + { + XDocumentInfoSupplier xDocInfoSup = ( XDocumentInfoSupplier ) UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); + if ( xDocInfoSup != null ) + { + XPropertySet xPropSet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); + if ( xPropSet != null ) + { + try + { + xPropSet.setPropertyValue( "Title", sTitle ); + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + } + } + + protected static String GetDocServiceName( XComponentContext xContext, XModel xModel ) + { + String aDocServiceName = ""; + if ( xModel != null && xContext != null ) + { + try + { + XMultiComponentFactory xFactory = xContext.getServiceManager(); + if ( xFactory == null ) + throw new com.sun.star.uno.RuntimeException(); + + Object oModuleManager = xFactory.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xContext ); + XModuleManager xModuleManager = ( XModuleManager ) UnoRuntime.queryInterface( XModuleManager.class, oModuleManager ); + if ( xModuleManager != null ) + aDocServiceName = xModuleManager.identify( xModel ); + } + catch( java.lang.Exception e ) + { + e.printStackTrace(); + } + } + + return aDocServiceName; + } + + protected static String GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName ) + { + String aFilterName = ""; + if ( xContext != null && aTypeName != null && aTypeName.length() != 0 + && aDocServiceName != null && aDocServiceName.length() != 0 ) + { + try + { + Object oFilterFactory = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.document.FilterFactory", xContext ); + XContainerQuery xQuery = ( XContainerQuery )UnoRuntime.queryInterface( XContainerQuery.class, oFilterFactory ); + if ( xQuery != null ) + { + NamedValue[] aRequest = new NamedValue[2]; + aRequest[0] = new NamedValue( "Type", aTypeName ); + aRequest[1] = new NamedValue( "DocumentService", aDocServiceName ); + + XEnumeration xSet = xQuery.createSubSetEnumerationByProperties( aRequest ); + if ( xSet != null ) + { + boolean bAcceptable = false; + while ( xSet.hasMoreElements() && !bAcceptable ) + { + PropertyValue[] pFilterProps = ( PropertyValue[] )AnyConverter.toArray( xSet.nextElement() ); + if ( pFilterProps != null ) + { + int nLen = pFilterProps.length; + String aTmpFilter = null; + + for ( int nInd = 0; nInd < nLen; nInd++ ) + { + if ( pFilterProps[nInd].Name.equals( "Name" ) ) + aTmpFilter = AnyConverter.toString( pFilterProps[nInd].Value ); + else if ( pFilterProps[nInd].Name.equals( "Flags" ) ) + bAcceptable = ( ( AnyConverter.toInt( pFilterProps[nInd].Value ) & 2 ) == 2 ); // must allow export + } + + if ( bAcceptable ) + aFilterName = aTmpFilter; + } + } + } + } + } + catch( java.lang.Exception e ) + { + e.printStackTrace(); + } + } + + return aFilterName; + } + + protected static XMultiServiceFactory GetConfigurationProvider( XComponentContext xContext ) + throws com.sun.star.uno.Exception + { + XMultiServiceFactory xConfigurationProvider = null; + if ( xContext != null ) + { + XMultiComponentFactory xFactory = xContext.getServiceManager(); + Object oConfigProvider = xFactory.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext ); + xConfigurationProvider = ( XMultiServiceFactory ) UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider ); + } + + if ( xConfigurationProvider == null ) + throw new com.sun.star.uno.RuntimeException(); + + return xConfigurationProvider; + } + + protected static void SetConfigurationProxy( HostConfiguration aHostConfig, XComponentContext xContext ) + { + if ( aHostConfig == null || xContext == null ) + return; + + try + { + PropertyValue aVal = new PropertyValue(); + aVal.Name = "nodepath"; + aVal.Value = "org.openoffice.Inet/Settings"; + Object[] aArgs = new Object[1]; + aArgs[0] = aVal; + + Object oSettings = Helper.GetConfigurationProvider( xContext ).createInstanceWithArguments( + "com.sun.star.configuration.ConfigurationAccess", + aArgs ); + XNameAccess xNameAccess = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oSettings ); + + if ( xNameAccess == null ) + return; + + int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) ); + String aNoProxyList = AnyConverter.toString( xNameAccess.getByName( "ooInetNoProxy" ) ); + String aProxyName = AnyConverter.toString( xNameAccess.getByName( "ooInetHTTPProxyName" ) ); + + int nProxyPort = AnyConverter.toInt( xNameAccess.getByName( "ooInetHTTPProxyPort" ) ); + + if ( nProxyPort == -1 ) + nProxyPort = 80; + + if ( nProxyType == 0 ) + aHostConfig.setProxy( "", 0 ); + else + { + // TODO: check whether the URL is in the NoProxy list + aHostConfig.setProxy( aProxyName, nProxyPort ); + } + } + catch( java.lang.Exception e ) + { + e.printStackTrace(); + } + } + + protected static void ShowURLInBrowser( XComponentContext xContext, String sURL ) + { + if ( xContext != null && sURL != null && sURL.length() > 0 ) + { + try + { + Object oSystemShell = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.system.SystemShellExecute", xContext ); + XSystemShellExecute xSystemShell = (XSystemShellExecute)UnoRuntime.queryInterface( XSystemShellExecute.class, oSystemShell ); + if ( xSystemShell != null ) + xSystemShell.execute( sURL, "", SystemShellExecuteFlags.DEFAULTS ); + } + catch( Exception e ) + { + e.printStackTrace(); + } + } + } + + protected static void ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost ) + throws WikiCancelException, IOException + { + if ( aMethod != null && aHostConfig != null && aURI != null && xContext != null ) + { + if ( bSetHost ) + { + aHostConfig.setHost( aURI ); + SetConfigurationProxy( aHostConfig, xContext ); + } + + boolean bNoUnknownCertNotification = false; + if ( aHostConfig.getProtocol().getScheme().equals( "https" ) && m_aAcceptedUnknownCerts != null ) + { + Boolean bAccepted = (Boolean)m_aAcceptedUnknownCerts.get( aHostConfig.getHost() ); + bNoUnknownCertNotification = ( bAccepted != null && bAccepted.booleanValue() ); + } + + if ( !bNoUnknownCertNotification ) + { + try + { + Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); + } + catch ( SSLException e ) + { + if ( aURI.getScheme().equals( "https" ) ) + { + // the complete secure connection seems to be impossible + XDialog xAskDialog = WikiDialog.CreateSimpleDialog( xContext, "vnd.sun.star.script:WikiEditor.UnknownCertDialog?location=application" ); + if ( xAskDialog != null && MainThreadDialogExecutor.Execute( xContext, xAskDialog ) ) + { + if ( m_aAcceptedUnknownCerts == null ) + m_aAcceptedUnknownCerts = new Hashtable(); + m_aAcceptedUnknownCerts.put( aURI.getHost(), Boolean.TRUE ); + bNoUnknownCertNotification = true; + } + } + } + } + + if ( bNoUnknownCertNotification ) + { + { + { + aHostConfig.setHost( aHostConfig.getHost(), ( aURI.getPort() < 0 ? 443 : aURI.getPort() ), Helper.GetOwnHttps( aURI.getPort() ) ); + Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); + } + } + } + } + } + + static private class HTMLParse extends HTMLEditorKit + { + + public HTMLEditorKit.Parser getParser() + { + return super.getParser(); + } + } + + static protected HTMLEditorKit.Parser GetHTMLParser() + { + return new HTMLParse().getParser(); + } + + static protected boolean LoginReportsError( String sRespond ) + { + boolean bResult = true; + if ( sRespond != null ) + { + try + { + StringReader aReader = new StringReader( sRespond ); + HTMLEditorKit.Parser aParser = GetHTMLParser(); + EditPageParser aCallback = new EditPageParser(); + + aParser.parse( aReader, aCallback, true ); + bResult = ( aCallback.m_nErrorInd >= 0 ); + } + catch( Exception e ) + { + e.printStackTrace(); + } + } + + return bResult; + } + + static protected HostConfiguration Login( URI aMainURL, String sWikiUser, String sWikiPass, String sWikiDomain, XComponentContext xContext ) + throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + HostConfiguration aHostConfig = null; + + if ( sWikiUser != null && sWikiUser.length() > 0 && sWikiPass != null && sWikiDomain != null && xContext != null ) + { + HostConfiguration aNewHostConfig = new HostConfiguration(); + + URI aURI = new URI( aMainURL.toString() + "index.php?title=Special:Userlogin" ); + GetMethod aGetCookie = new GetMethod( aURI.getEscapedPathQuery() ); + + ExecuteMethod( aGetCookie, aNewHostConfig, aURI, xContext, true ); + + int nResultCode = aGetCookie.getStatusCode(); + aGetCookie.releaseConnection(); + + if ( nResultCode == 200 ) + { + PostMethod aPost = new PostMethod(); + URI aPostURI = new URI( aMainURL.getPath() + "index.php?title=Special:Userlogin&action=submitlogin" ); + aPost.setPath( aPostURI.getEscapedPathQuery() ); + + aPost.addParameter( "wpName", sWikiUser ); + aPost.addParameter( "wpRemember", "1" ); + aPost.addParameter( "wpPassword", sWikiPass ); + aPost.addParameter( "wpDomain", sWikiDomain ); + + ExecuteMethod( aPost, aNewHostConfig, aPostURI, xContext, false ); + + nResultCode = aPost.getStatusCode(); + if ( nResultCode == 200 ) + { + String sResult = aPost.getResponseBodyAsString(); + if ( !LoginReportsError( sResult ) ) + aHostConfig = aNewHostConfig; + } + + aPost.releaseConnection(); + } + } + + return aHostConfig; + } + +} + diff --git a/swext/mediawiki/src/com/sun/star/wiki/MainThreadDialogExecutor.java b/swext/mediawiki/src/com/sun/star/wiki/MainThreadDialogExecutor.java new file mode 100644 index 000000000000..7898088e5459 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/MainThreadDialogExecutor.java @@ -0,0 +1,136 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: MainThreadDialogExecutor.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:13:59 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.uno.Any; +import com.sun.star.awt.XDialog; +import com.sun.star.awt.XCallback; +import com.sun.star.awt.XRequestCallback; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class MainThreadDialogExecutor implements XCallback +{ + private WikiDialog m_aWikiDialog; + private XDialog m_xDialog; + private boolean m_bResult = false; + private boolean m_bCalled = false; + + static public boolean Show( XComponentContext xContext, WikiDialog aWikiDialog ) + { + MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( aWikiDialog ); + return GetCallback( xContext, aExecutor ); + } + + static public boolean Execute( XComponentContext xContext, XDialog xDialog ) + { + MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog ); + return GetCallback( xContext, aExecutor ); + } + + static private boolean GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor ) + { + try + { + if ( aExecutor != null ) + { + String aThreadName = null; + Thread aCurThread = Thread.currentThread(); + if ( aCurThread != null ) + aThreadName = aCurThread.getName(); + + if ( aThreadName != null && aThreadName.equals( "com.sun.star.thread.WikiEditorSendingThread" ) ) + { + // the main thread should be accessed asynchronously + XMultiComponentFactory xFactory = xContext.getServiceManager(); + if ( xFactory == null ) + throw new com.sun.star.uno.RuntimeException(); + + XRequestCallback xRequest = (XRequestCallback)UnoRuntime.queryInterface( + XRequestCallback.class, + xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ) ); + if ( xRequest != null ) + { + xRequest.addCallback( aExecutor, Any.VOID ); + do + { + Thread.yield(); + } + while( !aExecutor.m_bCalled ); + } + } + else + { + // handle it as a main thread + aExecutor.notify( Any.VOID ); + } + } + } + catch( Exception e ) + { + e.printStackTrace(); + } + + return aExecutor.GetResult(); + } + + private MainThreadDialogExecutor( WikiDialog aWikiDialog ) + { + m_aWikiDialog = aWikiDialog; + } + + private MainThreadDialogExecutor( XDialog xDialog ) + { + m_xDialog = xDialog; + } + + private boolean GetResult() + { + return m_bResult; + } + + public void notify( Object aData ) + { + if ( m_aWikiDialog != null ) + m_bResult = m_aWikiDialog.show(); + else if ( m_xDialog != null ) + m_bResult = ( m_xDialog.execute() == 1 ); + + m_bCalled = true; + } +}; + diff --git a/swext/mediawiki/src/com/sun/star/wiki/Settings.java b/swext/mediawiki/src/com/sun/star/wiki/Settings.java new file mode 100644 index 000000000000..7277fe3d028c --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/Settings.java @@ -0,0 +1,399 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: Settings.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:14:11 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.beans.NamedValue; +import com.sun.star.container.XNameAccess; +import com.sun.star.container.XNameContainer; +import com.sun.star.container.XNameReplace; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.util.XChangesBatch; +import java.net.InetSocketAddress; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +public class Settings +{ + + private XComponentContext m_context; + private int lastUsedWikiServer = 0; + + + /* Singelton */ + private static Settings m_instance; + + + private Vector m_WikiConnections = new Vector(); + private Vector m_aWikiDocs = new Vector(); + + private Settings( XComponentContext ctx ) + { + m_context=ctx; + loadConfiguration(); + } + + + public static synchronized Settings getSettings( XComponentContext ctx ) + { + if ( m_instance == null ) + m_instance = new Settings( ctx ); + // m_instance.loadSettings(); + return m_instance; + } + + + public void addWikiCon ( Hashtable wikiCon ) + { + m_WikiConnections.add( wikiCon ); + } + + + public Vector getWikiCons() + { + return m_WikiConnections; + } + + public String getWikiConUrlByNumber( int num ) + { + String url = ""; + if ( num >=0 && num < m_WikiConnections.size() ) + { + Hashtable ht = ( Hashtable ) m_WikiConnections.get( num ); + url = ( String ) ht.get( "Url" ); + } + return url; + } + + + public void addWikiDoc ( Hashtable aWikiDoc ) + { + String sURL = ( String ) aWikiDoc.get( "CompleteUrl" ); + Hashtable aEntry = getDocByCompleteUrl( sURL ); + + if ( aEntry != null ) + { + // add doc to the end, even if it has been added before + m_aWikiDocs.remove( aEntry ); + } + else if ( m_aWikiDocs.size() > 10 ) + { + // if the number of elements has reached maximum the oldest element should be removed + m_aWikiDocs.remove( 0 ); + } + + m_aWikiDocs.add( aWikiDoc ); + } + + + public Vector getWikiDocs() + { + return m_aWikiDocs; + } + + public Object[] getWikiDocList( int serverid, int num ) + { + String wikiserverurl = getWikiConUrlByNumber( serverid ); + Vector theDocs = new Vector(); + String [] docs = new String[0]; + for ( int i=0; i<m_aWikiDocs.size(); i++ ) + { + Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); + String docurl = ( String ) ht.get( "Url" ); + if ( docurl.equals( wikiserverurl ) ) + { + theDocs.add( (String ) ht.get( "Doc" ) ); + } + } + return theDocs.toArray( docs ); + } + + public int getLastUsedWikiServer() + { + return lastUsedWikiServer; + } + + public void setLastUsedWikiServer( int l ) + { + lastUsedWikiServer = l; + } + + public String[] getWikiURLs() + { + String [] WikiList = new String [m_WikiConnections.size()]; + for ( int i=0; i<m_WikiConnections.size(); i++ ) + { + Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); + WikiList[i] = ( String ) ht.get( "Url" ); + } + return WikiList; + } + + + public Hashtable getSettingByUrl( String sUrl ) + { + Hashtable ht = null; + for( int i=0;i<m_WikiConnections.size();i++ ) + { + Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); + String u1 = ( String ) h1.get( "Url" ); + if ( u1.equals( sUrl ) ) + { + ht = h1; + } + } + return ht; + } + + public Hashtable getDocByCompleteUrl( String curl ) + { + Hashtable ht = null; + for( int i=0;i<m_aWikiDocs.size();i++ ) + { + Hashtable h1 = ( Hashtable ) m_aWikiDocs.get( i ); + String u1 = ( String ) h1.get( "CompleteUrl" ); + if ( u1.equals( curl ) ) + { + ht = h1; + } + } + return ht; + } + + + public void removeSettingByUrl( String sUrl ) + { + Hashtable ht = null; + for( int i=0;i<m_WikiConnections.size();i++ ) + { + Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); + String u1 = ( String ) h1.get( "Url" ); + if ( u1.equals( sUrl ) ) + { + m_WikiConnections.remove( i ); + } + } + } + + + public void storeConfiguration() + { + try + { + Object oList = getConfigurationUpdateAccess( "org.openoffice.Office.Custom.WikiExtension/ConnectionList" ); + + // remove stored connection information + XNameAccess xList = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oList ); + String[] names = xList.getElementNames(); + XNameContainer xContainer = ( XNameContainer ) UnoRuntime.queryInterface( XNameContainer.class, xList ); + for( int i=0; i<names.length; i++ ) + { + xContainer.removeByName( names[i] ); + } + + // store all connections + XSingleServiceFactory xConnectionFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer ); + for ( int i=0; i< m_WikiConnections.size(); i++ ) + { + Object oNewConnection = xConnectionFactory.createInstance(); + Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); + xContainer.insertByName( (String)ht.get( "Url" ), oNewConnection ); + } + // commit changes + XChangesBatch batch = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, oList ); + batch.commitChanges(); + + Object oDocs = getConfigurationUpdateAccess( "org.openoffice.Office.Custom.WikiExtension/RecentDocs" ); + // remove stored connection information + XNameAccess xDocs = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDocs ); + String[] names2 = xDocs.getElementNames(); + XNameContainer xContainer2 = ( XNameContainer ) UnoRuntime.queryInterface( XNameContainer.class, xDocs ); + for( int i=0; i<names2.length; i++ ) + { + xContainer2.removeByName( names2[i] ); + } + // store all Docs + XSingleServiceFactory xDocListFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer2 ); + for ( int i=0; i< m_aWikiDocs.size(); i++ ) + { + Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); + + Object oNewDoc = xDocListFactory.createInstance(); + XNameReplace xNewDoc = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewDoc ); + + Enumeration e = ht.keys(); + while ( e.hasMoreElements() ) + { + String key = ( String ) e.nextElement(); + xNewDoc.replaceByName( key, ht.get( key ) ); + } + + xContainer2.insertByName( "d" + i, xNewDoc ); + } + // commit changes + XChangesBatch batch2 = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, oDocs ); + batch2.commitChanges(); + + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + + + public void loadConfiguration() + { + m_WikiConnections.clear(); + try + { + // get configuration service + // connect to configmanager + Object oAccess = getConfigurationAccess( "org.openoffice.Office.Custom.WikiExtension" ); + + XNameAccess xAccess = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oAccess ); + + if ( xAccess != null ) + { + Object oList = xAccess.getByName( "ConnectionList" ); + XNameAccess xConnectionList = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oList ); + String [] allCons = xConnectionList.getElementNames(); + for ( int i=0; i<allCons.length; i++ ) + { + Hashtable ht = new Hashtable(); + ht.put( "Url", allCons[i] ); + ht.put( "Username", "" ); + ht.put( "Password", "" ); + addWikiCon( ht ); + } + + Object oDocs = xAccess.getByName( "RecentDocs" ); + XNameAccess xRecentDocs = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDocs ); + String [] allDocs = xRecentDocs.getElementNames(); + for ( int i=0; i<allDocs.length; i++ ) + { + Object oDoc = xRecentDocs.getByName( allDocs[i] ); + XNameAccess xDoc = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDoc ); + Hashtable ht = new Hashtable(); + ht.put( "Url", xDoc.getByName( "Url" ) ); + ht.put( "CompleteUrl", xDoc.getByName( "CompleteUrl" ) ); + ht.put( "Doc", xDoc.getByName( "Doc" ) ); + addWikiDoc( ht ); + } + } + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + + + public String getPropertyString( String path, String name ) + { + String result = null; + try + { + Object obj = getConfigurationAccess( path ); + XNameAccess xna = ( XNameAccess ) UnoRuntime.queryInterface( + XNameAccess.class, obj ); + result = xna.getByName( name ).toString(); + } + catch ( com.sun.star.uno.Exception e ) + { + e.printStackTrace(); + } + return result; + } + + + private Object getConfigurationAccess( String path ) + { + return getConfigurationAccess( path, false ); + } + + + private Object getConfigurationUpdateAccess( String path ) + { + return getConfigurationAccess( path, true ); + } + + + private Object getConfigurationAccess( String path, boolean update ) + { + Object oAccess = null; + try + { + String access = "com.sun.star.configuration.ConfigurationAccess"; + if ( update ) + { + access = "com.sun.star.configuration.ConfigurationUpdateAccess"; + } + XMultiServiceFactory cfg = getConfigurationProvider(); + oAccess = cfg.createInstanceWithArguments( access, new Object[] + {new NamedValue( "nodepath", path )} +); + } + catch ( com.sun.star.uno.Exception e ) + { + //Exception trying to get configuration access + } + return oAccess; + } + + + private XMultiServiceFactory getConfigurationProvider() + { + XMultiServiceFactory cfg = null; + try + { + cfg = ( XMultiServiceFactory ) UnoRuntime.queryInterface( + XMultiServiceFactory.class, m_context.getServiceManager().createInstanceWithContext( + "com.sun.star.configuration.DefaultProvider", m_context ) ); + + } + catch ( com.sun.star.uno.Exception e ) + { + e.printStackTrace(); + } + return cfg; + } + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiArticle.java b/swext/mediawiki/src/com/sun/star/wiki/WikiArticle.java new file mode 100644 index 000000000000..13d115a28fd4 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiArticle.java @@ -0,0 +1,297 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiArticle.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:14:22 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import java.io.*; +import java.net.MalformedURLException; +import java.util.Hashtable; +import javax.swing.text.html.*; +import org.w3c.tidy.Configuration; +import org.w3c.tidy.Tidy; +import org.xml.sax.InputSource; +import com.sun.star.uno.XComponentContext; + +import org.apache.commons.httpclient.*; +import org.apache.commons.httpclient.methods.*; + + +public class WikiArticle +{ + private XComponentContext m_xContext; + + private String m_sEditTime = ""; + private String m_sEditToken = ""; + + protected String m_sHTMLCode; + private boolean m_bNoArticle = true; + + protected String m_sWikiUser; + protected String m_sWikiPass; + protected String m_sWikiDomain = "sun"; + + protected String m_sTitle = ""; + + private URI m_aMainURI; + private HostConfiguration m_aHostConfig; + + + /** Creates a new instance of WikiArticle */ + public WikiArticle( XComponentContext xContext, String sTitle, Hashtable wikiSettings, boolean bLogin, WikiPropDialog aPropDialog ) + throws java.net.MalformedURLException, com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + m_xContext = xContext; + + String sMainUrl = (String) wikiSettings.get("Url"); + m_sWikiUser = (String) wikiSettings.get("Username"); + m_sWikiPass = (String) wikiSettings.get("Password"); + m_sTitle = sTitle; + + m_aMainURI = new URI( sMainUrl ); + +// viewURL = sMainUrl + "index.php?title=" + m_sTitle; +// editURL = sMainUrl + "index.php?title=" + m_sTitle + "&action=edit"; +// submitURL = sMainUrl + "index.php?title=" + m_sTitle + "&action=submit"; +// loginURL = sMainUrl + "index.php?title=Special:Userlogin"; +// loginSubmitURL = sMainUrl + "index.php?title=Special:Userlogin&action=submitlogin"; + + while( bLogin && !Login() ) + { + // TODO: be sure that this is no main thread + WikiEditSettingDialog wd = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", wikiSettings ); + + if ( aPropDialog != null ) + aPropDialog.SetThrobberActive( false ); + + if ( MainThreadDialogExecutor.Show( xContext, wd ) ) + { + m_sWikiUser = (String) wikiSettings.get("Username"); + m_sWikiPass = (String) wikiSettings.get("Password"); + } + else + throw new WikiCancelException(); + + if ( aPropDialog != null ) + { + aPropDialog.SetThrobberActive( true ); + Thread.yield(); + } + } + + // in case of loading the html contents are used + // in case of saving the contents should be checked whether they are empty + InitArticleHTML(); + + // getArticleWiki(); + } + + public String GetMainURL() + { + return m_aMainURI.toString(); + } + + public String GetTitle() + { + return m_sTitle; + } + + public String GetViewURL() + { + return m_aMainURI.toString() + "index.php?title=" + m_sTitle; + } + + private String getArticleWiki() + throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + String sWikiCode = null; + + if ( m_aHostConfig != null ) + { + URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit" ); + GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); + + Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false ); + + int nResultCode = aRequest.getStatusCode(); + String sWebPage = null; + if ( nResultCode == 200 ) + sWebPage = aRequest.getResponseBodyAsString(); + + if ( sWebPage != null ) + { + StringReader r = new StringReader(sWebPage); + HTMLEditorKit.Parser parse = Helper.GetHTMLParser(); + EditPageParser callback = new EditPageParser(); + + parse.parse(r,callback,true); + m_sEditTime = callback.m_sEditTime; + m_sEditToken = callback.m_sEditToken; + + int iPosStart = callback.m_nWikiArticleStart; + int iPosEnd = callback.m_nWikiArticleEnd; + + if ( iPosStart >= 0 && iPosEnd > 0 ) + { + String sArticle = sWebPage.substring(iPosStart, iPosEnd); + iPosStart = sArticle.indexOf(">") + 1; + sWikiCode = sArticle.substring( iPosStart, sArticle.length() ); + } + } + } + + return sWikiCode; + } + + private void InitArticleHTML() + throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + if ( m_aHostConfig != null ) + { + URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle ); + GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); + + Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false ); + + int nResultCode = aRequest.getStatusCode(); + String sWebPage = null; + if ( nResultCode == 200 ) + sWebPage = aRequest.getResponseBodyAsString(); + + if ( sWebPage != null ) + { + StringReader r = new StringReader(sWebPage); + HTMLEditorKit.Parser parse = Helper.GetHTMLParser(); + EditPageParser callback = new EditPageParser(); + + parse.parse(r,callback,true); + + int iPosStart = callback.m_nHTMLArticleStart; + int iPosEnd = callback.m_nHTMLArticleEnd; + int nPosNoArt = callback.m_nNoArticleInd; + + if ( iPosStart >= 0 && iPosEnd > 0 ) + { + m_sHTMLCode = sWebPage.substring(iPosStart, iPosEnd); + m_bNoArticle = ( nPosNoArt >= 0 && nPosNoArt >= iPosStart && nPosNoArt <= iPosEnd ); + } + } + } + } + + protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit ) + throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + boolean bResult = false; + + if ( m_aHostConfig != null && sWikiCode != null && sWikiComment != null ) + { + // get the edit time and token + getArticleWiki(); + + URI aURI = new URI( m_aMainURI.getPath() + "index.php?title=" + m_sTitle + "&action=submit" ); + PostMethod aPost = new PostMethod(); + aPost.setPath( aURI.getEscapedPathQuery() ); + + // aPost.addParameter( "title", m_sTitle ); + // aPost.addParameter( "action", "submit" ); + aPost.addParameter( "wpTextbox1", sWikiCode ); + aPost.addParameter( "wpSummary", sWikiComment ); + aPost.addParameter( "wpSection", "" ); + aPost.addParameter( "wpEdittime", m_sEditTime ); + aPost.addParameter( "wpSave", "Save page" ); + aPost.addParameter( "wpEditToken", m_sEditToken ); + + if ( bMinorEdit ) + aPost.addParameter( "wpMinoredit", "1" ); + + Helper.ExecuteMethod( aPost, m_aHostConfig, aURI, m_xContext, false ); + + int nResultCode = aPost.getStatusCode(); + if ( nResultCode < 400 ) + bResult = true; + + String aResult = aPost.getResponseBodyAsString(); + + // TODO: remove the debug printing, try to detect the error + System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult ); + } + + return bResult; + } + + protected boolean Login() + throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException + { + m_aHostConfig = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_sWikiDomain, m_xContext ); + return ( m_aHostConfig != null ); + } + + protected void cleanHTML() + { + if ( m_sHTMLCode != null ) + { + //Welcome to regex hell ;) + + //strip comments + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<![ \\r\\n\\t]*(--([^\\-]|[\\r\\n]|-[^\\-])*--[ \\r\\n\\t]*)\\>",""); + + //strip edit section links + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<div class=\"editsection\".*?\\</div\\>",""); + + //strip huge spaces + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<p\\>\\<br /\\>[ \r\n\t]*?\\</p\\>",""); + + //strip toc + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<table.*id=\"toc\"(.|[\r\n])*?\\</table\\>",""); + + //strip jump-to-nav + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<div id=\"jump-to-nav\".*?\\</div\\>",""); + + //strip Javascript + m_sHTMLCode = m_sHTMLCode.replaceAll("\\<script(.|[\r\n])*?\\</script\\>",""); + } + } + + + protected boolean NotExist() + { + boolean bResult = true; + if ( m_sHTMLCode != null ) + bResult = m_bNoArticle; + + return bResult; + } + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiCancelException.java b/swext/mediawiki/src/com/sun/star/wiki/WikiCancelException.java new file mode 100644 index 000000000000..6be90384e71c --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiCancelException.java @@ -0,0 +1,41 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiCancelException.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:14:34 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +class WikiCancelException extends java.lang.Exception +{ +}; + diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiDialog.java b/swext/mediawiki/src/com/sun/star/wiki/WikiDialog.java new file mode 100644 index 000000000000..365f9806c9e9 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiDialog.java @@ -0,0 +1,126 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiDialog.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:14:46 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XControl; +import com.sun.star.awt.XControlContainer; +import com.sun.star.awt.XDialog; +import com.sun.star.awt.XDialogEventHandler; +import com.sun.star.awt.XDialogProvider2; +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class WikiDialog implements XDialogEventHandler{ + + XComponentContext m_xContext; + XControlContainer m_xControlContainer; + XDialog m_xDialog; + String[] m_aMethods; + boolean m_bAction = false; + Settings m_aSettings; + + + /** Creates a new instance of WikiDialog */ + public WikiDialog(XComponentContext c, String DialogURL) { + this.m_xContext = c; + XMultiComponentFactory xMCF = m_xContext.getServiceManager(); + m_aSettings = Settings.getSettings(m_xContext); + try { + Object obj; + obj = xMCF.createInstanceWithContext("com.sun.star.awt.DialogProvider2", m_xContext ); + XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, obj ); + + m_xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this ); + m_xControlContainer = (XControlContainer)UnoRuntime.queryInterface(XControlContainer.class, m_xDialog ); + } catch (com.sun.star.uno.Exception ex) { + ex.printStackTrace(); + } + } + + + protected void setMethods (String [] Methods) { + this.m_aMethods = Methods; + } + + + public boolean show( ) { + if( m_xDialog != null ) m_xDialog.execute(); + return m_bAction; + } + + + public String[] getSupportedMethodNames() { + return m_aMethods; + } + + + public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName ) { + return true; + } + + + protected XPropertySet getPropSet(String sControl) { + XControl xControl = m_xControlContainer.getControl(sControl); + XPropertySet xPS = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel() ); + return xPS; + } + + public static XDialog CreateSimpleDialog( XComponentContext xContext, String sURL ) + { + XDialog xResult = null; + + if ( xContext != null && sURL != null && sURL.length() > 0 ) + { + try + { + Object oDialogProvider = xContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.DialogProvider2", xContext ); + XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, oDialogProvider ); + + if ( xDialogProvider != null ) + xResult = xDialogProvider.createDialog( sURL ); + } + catch (com.sun.star.uno.Exception ex) + { + ex.printStackTrace(); + } + } + + return xResult; + } + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiEditSettingDialog.java b/swext/mediawiki/src/com/sun/star/wiki/WikiEditSettingDialog.java new file mode 100644 index 000000000000..99c583756290 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiEditSettingDialog.java @@ -0,0 +1,206 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiEditSettingDialog.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:15:09 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XDialog; +import com.sun.star.uno.XComponentContext; +import java.util.Hashtable; +import javax.net.ssl.SSLException; + +import org.apache.commons.httpclient.*; +import org.apache.commons.httpclient.methods.*; +import org.apache.commons.httpclient.protocol.Protocol; + +public class WikiEditSettingDialog extends WikiDialog +{ + + private final String sOKMethod = "OK"; + private final String sCancelMethod = "Cancel"; + private final String sHelpMethod = "Help"; + + String[] Methods = + {sOKMethod, sCancelMethod, sHelpMethod}; + private Hashtable setting; + private boolean addMode; + + public WikiEditSettingDialog( XComponentContext c, String DialogURL ) + { + super( c, DialogURL ); + super.setMethods( Methods ); + setting = new Hashtable(); + addMode = true; + } + + public WikiEditSettingDialog( XComponentContext c, String DialogURL, Hashtable ht ) + { + super( c, DialogURL ); + super.setMethods( Methods ); + setting = ht; + try + { + getPropSet( "UrlField" ).setPropertyValue( "Text", ht.get( "Url" )); + getPropSet( "UsernameField" ).setPropertyValue( "Text", ht.get( "Username" )); + getPropSet( "PasswordField" ).setPropertyValue( "Text", ht.get( "Password" )); + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + addMode = false; + } + + public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName ) + { + if ( MethodName.equals( sOKMethod ) ) + { + try + { + String sURL = ( String ) getPropSet( "UrlField" ).getPropertyValue( "Text" ); + String sUserName = ( String ) getPropSet( "UsernameField" ).getPropertyValue( "Text" ); + String sPassword = ( String ) getPropSet( "PasswordField" ).getPropertyValue( "Text" ); + String sRedirectURL = ""; + + HostConfiguration aHostConfig = new HostConfiguration(); + boolean bInitHost = true; + + do + { + if ( sRedirectURL.length() > 0 ) + { + sURL = sRedirectURL; + sRedirectURL = ""; + } + + if ( sURL.length() > 0 ) + { + URI aURI = new URI( sURL ); + GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); + aRequest.setFollowRedirects( false ); + Helper.ExecuteMethod( aRequest, aHostConfig, aURI, m_xContext, bInitHost ); + bInitHost = false; + + int nResultCode = aRequest.getStatusCode(); + String sWebPage = null; + if ( nResultCode == 200 ) + sWebPage = aRequest.getResponseBodyAsString(); + else if ( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 ) + sRedirectURL = aRequest.getResponseHeader( "Location" ).getValue(); + + aRequest.releaseConnection(); + + if ( sWebPage != null && sWebPage.length() > 0 ) + { + //the URL is valid + String sMainURL = Helper.GetMainURL( sWebPage, sURL ); + + if ( sMainURL.equals( "" ) ) + { + // TODO: + // it's not a Wiki Page, check first whether a redirect is requested + // happens usually in case of https + sRedirectURL = Helper.GetRedirectURL( sWebPage, sURL ); + if ( sRedirectURL.equals( "" ) ) + { + // show error + ErrorDialog ed = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "This is not a Wiki Page!" ); + ed.show(); + } + } + else + { + if ( ( sUserName.length() > 0 || sPassword.length() > 0 ) + && Helper.Login( new URI( sMainURL ), sUserName, sPassword, "sun", m_xContext ) == null ) + { + // a wrong login information is provided + // show error + ErrorDialog ed = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "The login information is wrong! It should either be empty or correct." ); + ed.show(); + } + else + { + setting.put( "Url",sMainURL ); + setting.put( "Username", sUserName ); + setting.put( "Password", sPassword ); + if ( addMode ) + Settings.getSettings( m_xContext ).addWikiCon( setting ); + + m_bAction = true; + xDialog.endExecute(); + } + } + } + else if ( sRedirectURL == null || sRedirectURL.length() == 0 ) + { + // URL invalid + // show error + ErrorDialog ed = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "The URl is invalid!" ); + ed.show(); + } + } + else + { + // URL field empty + // show error + ErrorDialog ed = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "The URL field is empty!" ); + ed.show(); + + } + } while ( sRedirectURL.length() > 0 ); + } + catch ( Exception ex ) + { + ErrorDialog ed = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "Can not access the provided URL for any reason!" ); + ed.show(); + + ex.printStackTrace(); + } + return true; + } + else if ( MethodName.equals( sCancelMethod ) ) + { + xDialog.endExecute(); + return true; + } + else if ( MethodName.equals( sHelpMethod ) ) + { + return true; + } + + return false; + } + + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiEditorImpl.java b/swext/mediawiki/src/com/sun/star/wiki/WikiEditorImpl.java new file mode 100644 index 000000000000..a11c01d785d9 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiEditorImpl.java @@ -0,0 +1,527 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiEditorImpl.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:14:58 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XDialog; +import com.sun.star.beans.PropertyValue; +import com.sun.star.frame.DispatchDescriptor; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XController; +import com.sun.star.frame.XDesktop; +import com.sun.star.frame.XDispatch; +import com.sun.star.frame.XDispatchProvider; +import com.sun.star.frame.XFrame; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XStatusListener; +import com.sun.star.frame.XStorable; +import com.sun.star.io.XInputStream; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XInitialization; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.lib.uno.helper.Factory; +import com.sun.star.lang.XSingleComponentFactory; +import com.sun.star.registry.XRegistryKey; +import com.sun.star.lib.uno.helper.WeakBase; +import com.sun.star.util.XCloseBroadcaster; +import com.sun.star.view.XSelectionSupplier; +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + + +public final class WikiEditorImpl extends WeakBase + implements com.sun.star.lang.XServiceInfo, XDispatchProvider, XDispatch, XInitialization +{ + + private final XComponentContext m_xContext; + private static final String m_implementationName = WikiEditorImpl.class.getName(); + private static final String[] m_serviceNames = {"com.sun.star.wiki.WikiEditor" }; + + // information needed for component registration + public static final String[] supportedServiceNames = {"com.sun.star.frame.ProtocolHandler"}; + public static final Class implementationClass = WikiEditorImpl.class; + // protocol name that this protocol handler handles + public static final String protocolName = "vnd.com.sun.star.wiki:"; + + private Map m_statusListeners = new HashMap(); + + + private XComponent xComp; + private String sTempUrl; + + private XFrame m_xFrame; + private XModel m_xModel; + private Settings m_settings; + + private String m_aFilterName; + + public WikiEditorImpl( XComponentContext xContext ) + { + // Helper.trustAllSSL(); + m_xContext = xContext; + m_settings = Settings.getSettings( m_xContext ); + }; + + public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) + { + XSingleComponentFactory xFactory = null; + + if ( sImplementationName.equals( m_implementationName ) ) + xFactory = Factory.createComponentFactory( WikiEditorImpl.class, m_serviceNames ); + else if ( sImplementationName.equals( WikiOptionsEventHandlerImpl.m_sImplementationName ) ) + xFactory = Factory.createComponentFactory( WikiOptionsEventHandlerImpl.class, + WikiOptionsEventHandlerImpl.m_pServiceNames ); + + return xFactory; + } + + public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) + { + boolean bResult = Factory.writeRegistryServiceInfo( m_implementationName, + m_serviceNames, + xRegistryKey ); + return ( bResult && Factory.writeRegistryServiceInfo( WikiOptionsEventHandlerImpl.m_sImplementationName, + WikiOptionsEventHandlerImpl.m_pServiceNames, + xRegistryKey ) ); + } + + // com.sun.star.lang.XServiceInfo: + public String getImplementationName() + { + return m_implementationName; + } + + public boolean supportsService( String sService ) + { + int len = m_serviceNames.length; + + for( int i=0; i < len; i++ ) + { + if ( sService.equals( m_serviceNames[i] )) + return true; + } + return false; + } + + public String[] getSupportedServiceNames() + { + return m_serviceNames; + } + + + private XSelectionSupplier m_sel; + private XController m_ctrl; + private boolean m_bInitialized; + public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception + { + if ( m_bInitialized ) + { + //logger.log( Level.SEVERE, "Extension instance was initialized again" ); + } + if ( args.length > 0 ) + { + m_bInitialized = true; + m_xFrame = ( XFrame )UnoRuntime.queryInterface( XFrame.class, args[0] ); + // become close listener + XCloseBroadcaster cb = ( XCloseBroadcaster )UnoRuntime.queryInterface( + XCloseBroadcaster.class, m_xFrame ); + } + } + + + + public void dispatch( + final com.sun.star.util.URL aURL, + com.sun.star.beans.PropertyValue[] propertyValue ) + { + final com.sun.star.util.URL myURL = aURL; + //logger.log( Level.INFO, "received dispatch request for: "+aURL.Complete ); + if ( aURL.Protocol.compareTo( protocolName ) == 0 ) + { + /* + synchronized( this ) + { + if( m_bClosing ) return; + else m_bActive = true; + } + **/ + + try + { + if ( myURL.Path.compareTo( "load" ) == 0 ) + { + loadArticle(); + } + if ( myURL.Path.compareTo( "send" ) == 0 ) + { + sendArticle(); + } + } catch( java.lang.Throwable t ) + { + //logger.log( Level.WARNING, "exception while handeling dispatch", t ); + } + + /* + synchronized( this ) + { + m_bActive = false; + // if we became owner while we were active + // we are responsible for closing the m_xFrame now + if ( m_bOwner && m_xFrame != null ) + { + try + { + XCloseable xclose = ( XCloseable )UnoRuntime.queryInterface( + XCloseable.class, m_xFrame ); + xclose.close( true ); + } catch ( CloseVetoException cve ) + { + logger.log( Level.SEVERE, "cannot close owned frame" ); + } + // relase reference to the m_xFrame; + m_xFrame = null; + } + } + */ + } + } + + + public com.sun.star.frame.XDispatch queryDispatch( + com.sun.star.util.URL aURL, + String str, + int param ) + { + if ( aURL.Protocol.equals( protocolName )) + { + + // by default, we are responsible + return this; + } else + { + return null; + } + } + + public XDispatch[] queryDispatches( DispatchDescriptor[] seqDescripts ) + { + int nCount = seqDescripts.length; + XDispatch[] lDispatcher = new XDispatch[nCount]; + + for( int i=0; i<nCount; ++i ) + lDispatcher[i] = queryDispatch( + seqDescripts[i].FeatureURL, + seqDescripts[i].FrameName, + seqDescripts[i].SearchFlags ); + return lDispatcher; + } + + + public void removeStatusListener( + com.sun.star.frame.XStatusListener xStatusListener, + com.sun.star.util.URL aURL ) + { + } + + + public void addStatusListener( + com.sun.star.frame.XStatusListener listener, + com.sun.star.util.URL url ) + { + String urlstring = url.Complete; + m_statusListeners.put( urlstring, listener ); + // synchroneous callback required!!! + callStatusListener( urlstring, listener ); + } + + public void callStatusListeners() + { + Set entries = m_statusListeners.entrySet(); + Iterator iter = entries.iterator(); + while ( iter.hasNext() ) + { + Map.Entry entry = ( Map.Entry ) iter.next(); + String uristring = ( String ) entry.getKey(); + XStatusListener listener = ( XStatusListener ) entry.getValue(); + callStatusListener( uristring, listener ); + } + } + + public void callStatusListener( String uristring, XStatusListener listener ) + { + try + { + + URI uri = new URI( uristring ); + + // check whether any blogs are live... + setListenerState( listener, "command", false ); + } catch ( URISyntaxException ex ) + { + ex.printStackTrace(); + } + } + + + private void setListenerState( XStatusListener listener, String urlstring, boolean state ) + { + com.sun.star.util.URL url = new com.sun.star.util.URL(); + url.Complete = urlstring; + //listener.statusChanged( new FeatureStateEvent( this, url, "", state, false, null )); + + } + + + public void loadArticle() + { + try + { + WikiPropDialog aPropDialog = new WikiPropDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Load?location=application", null ); + aPropDialog.fillWikiList(); + aPropDialog.fillDocList(); + if ( aPropDialog.show() ) + { + Hashtable wikiSettings = m_settings.getSettingByUrl( aPropDialog.m_sWikiEngineURL ); + + WikiArticle aArticle = new WikiArticle( m_xContext, aPropDialog.m_sWikiTitle, wikiSettings, false, null ); + + if ( aArticle.m_sHTMLCode == null ) + { + // the loading has failed + // TODO: error handling + return; + } + + aArticle.cleanHTML(); + + XInputStream xInputStream = Helper.SaveHTMLTemp( m_xContext, aArticle.m_sHTMLCode ); + if ( xInputStream == null ) + throw new com.sun.star.io.IOException(); + + Object oDesktop = m_xContext.getServiceManager().createInstanceWithContext( "com.sun.star.frame.Desktop",m_xContext ); + XComponentLoader xCL = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); + if ( xCL == null ) + throw new com.sun.star.uno.RuntimeException(); + + PropertyValue [] props = new PropertyValue[4]; + props[0] = new PropertyValue(); + props[0].Name = "FilterName"; + props[0].Value = "HTML"; + props[1] = new PropertyValue(); + props[1].Name = "DocumentTitle"; + props[1].Value = aArticle.GetTitle(); + props[2] = new PropertyValue(); + props[2].Name = "InputStream"; + props[2].Value = xInputStream; + props[3] = new PropertyValue(); + props[3].Name = "DocumentBaseURL"; + props[3].Value = aArticle.GetMainURL() + aArticle.GetTitle(); + + // the following argument seems to let office crash + // props[4] = new PropertyValue(); + // props[4].Name = "AsTemplate"; + // props[4].Value = true; + + xComp = xCL.loadComponentFromURL( "private:stream", "_blank", 0, props ); + + Hashtable htd = new Hashtable(); + + htd.put( "Doc", aArticle.GetTitle() ); + htd.put( "Url", aArticle.GetMainURL() ); + htd.put( "CompleteUrl", aArticle.GetMainURL() + aArticle.GetTitle() ); + m_settings.addWikiDoc( htd ); + m_settings.storeConfiguration(); + } + + } catch ( Exception e ) + { + e.printStackTrace(); + } + } + + public void sendArticle() + { + if ( m_xFrame != null ) + { + try + { + if ( m_xModel == null ) + { + XController xController = m_xFrame.getController(); + if ( xController != null ) + m_xModel = xController.getModel(); + } + + if ( m_xModel != null ) + { + // The related Wiki filter must be detected from the typename + String aServiceName = Helper.GetDocServiceName( m_xContext, m_xModel ); + m_aFilterName = Helper.GetFilterName( m_xContext, "MediaWiki", aServiceName ); + + if ( m_aFilterName == null || m_aFilterName.length() == 0 ) + { + ErrorDialog ed = new ErrorDialog(m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "MediaWiki filter is requered to be able to export!"); + ed.show(); + throw new com.sun.star.uno.RuntimeException(); + } + + // show the send dialog + WikiPropDialog aSendDialog = new WikiPropDialog( m_xContext, "vnd.sun.star.script:WikiEditor.SendToMediaWiki?location=application", this ); + aSendDialog.fillWikiList(); + aSendDialog.m_sWikiTitle = Helper.GetDocTitle( m_xModel ); + aSendDialog.show(); // triggers the sending + } + } + catch ( Exception e ) + { + // TODO: Error handling here + e.printStackTrace(); + } + } + } + + public boolean SendArticleImpl( WikiPropDialog aSendDialog ) + { + boolean bResult = false; + + if ( aSendDialog != null ) + { + String sTemp2Url = null; + + try + { + Hashtable wikiSettings = m_settings.getSettingByUrl( aSendDialog.m_sWikiEngineURL ); + + // TODO: stop progress spinning + WikiArticle aArticle = new WikiArticle( m_xContext, aSendDialog.m_sWikiTitle, wikiSettings, true, aSendDialog ); + + boolean bAllowSending = true; + if ( aArticle.NotExist() ) + { + // ask whether creation of a new page is allowed + XDialog xDialog = WikiDialog.CreateSimpleDialog( m_xContext, "vnd.sun.star.script:WikiEditor.NewWikiPage?location=application" ); + if ( xDialog != null ) + { + aSendDialog.SetThrobberActive( false ); + bAllowSending = MainThreadDialogExecutor.Execute( m_xContext, xDialog ); + aSendDialog.SetThrobberActive( true ); + } + else + throw new WikiCancelException(); + } + + if ( bAllowSending ) + { + PropertyValue[] lProperties = new PropertyValue[2]; + lProperties[0] = new PropertyValue(); + lProperties[0].Name = "FilterName"; + lProperties[0].Value = m_aFilterName; + lProperties[1] = new PropertyValue(); + lProperties[1].Name = "Overwrite"; + lProperties[1].Value = new Boolean( true ); + + sTemp2Url = Helper.CreateTempFile( m_xContext ); + + XStorable xStore = ( com.sun.star.frame.XStorable )UnoRuntime.queryInterface ( XStorable.class, m_xModel ); + if ( xStore == null ) + throw new com.sun.star.uno.RuntimeException(); + + xStore.storeToURL( sTemp2Url, lProperties ); + String sWikiCode = Helper.EachLine( sTemp2Url ); + + if ( aArticle.setArticle( sWikiCode, aSendDialog.m_sWikiComment, aSendDialog.m_bWikiMinorEdit ) ) + { + bResult = true; + Object desktop = m_xContext.getServiceManager().createInstanceWithContext( "com.sun.star.frame.Desktop", m_xContext ); + XDesktop xDesktop = ( XDesktop ) UnoRuntime.queryInterface( com.sun.star.frame.XDesktop.class, desktop ); + Helper.SetDocTitle( m_xModel, aArticle.GetTitle() ); + Hashtable aDocInfo = new Hashtable(); + aDocInfo.put( "Doc", aArticle.GetTitle() ); + aDocInfo.put( "Url", aArticle.GetMainURL() ); + aDocInfo.put( "CompleteUrl", aArticle.GetMainURL() + aArticle.GetTitle() ); + m_settings.addWikiDoc( aDocInfo ); + m_settings.storeConfiguration(); + + if ( aSendDialog.m_bWikiShowBrowser ) + Helper.ShowURLInBrowser( m_xContext, aArticle.GetViewURL() ); + } + else + { + ErrorDialog ed = new ErrorDialog(m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "Sending of the document has failed!"); + MainThreadDialogExecutor.Show( m_xContext, ed ); + } + } + } + catch( WikiCancelException ec ) + { + // nothing to do, the sending was cancelled + } + catch( Exception e ) + { + if ( Helper.IsConnectionAllowed() ) + { + // report the error only if sending was not cancelled + ErrorDialog ed = new ErrorDialog(m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "Sending of the document has failed!"); + MainThreadDialogExecutor.Show( m_xContext, ed ); + } + e.printStackTrace(); + } + + if ( sTemp2Url != null ) + { + try + { + // remove the temporary file + File aFile = new File( new URI( sTemp2Url ) ); + aFile.delete(); + } + catch ( java.lang.Exception e ) + { + e.printStackTrace(); + } + } + } + + return bResult; + } + +} diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiOptionsEventHandlerImpl.java b/swext/mediawiki/src/com/sun/star/wiki/WikiOptionsEventHandlerImpl.java new file mode 100644 index 000000000000..05ec8dd00c3f --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiOptionsEventHandlerImpl.java @@ -0,0 +1,290 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiOptionsEventHandlerImpl.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:15:20 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XContainerWindowEventHandler; +import com.sun.star.awt.XControl; +import com.sun.star.awt.XControlContainer; +import com.sun.star.awt.XDialog; +import com.sun.star.awt.XDialogEventHandler; +import com.sun.star.awt.XWindow; +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lib.uno.helper.WeakBase; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import java.util.Hashtable; + +public final class WikiOptionsEventHandlerImpl extends WeakBase + implements XServiceInfo, XContainerWindowEventHandler, XDialogEventHandler +{ + static final String[] m_pServiceNames = { "com.sun.star.wiki.WikiOptionsEventHandler" }; + static final String m_sImplementationName = WikiOptionsEventHandlerImpl.class.getName(); + + static final String sExternalEvent = "external_event"; + static final String sAdd = "Add"; + static final String sEdit = "Edit"; + static final String sRemove = "Remove"; + static final String sListStatus = "ListStatus"; + static final String sInitialize = "initialize"; + static final String sOk = "ok"; + static final String sBack = "back"; + + private XComponentContext m_xContext; + private XDialog m_xDialog; + private XControlContainer m_xControlContainer; + + Settings m_aSettings; + + public WikiOptionsEventHandlerImpl( XComponentContext xContext ) + { + m_xContext = xContext; + } + + protected XPropertySet GetPropSet( String sControl ) + { + if ( m_xControlContainer != null ) + { + XControl xControl = m_xControlContainer.getControl(sControl); + XPropertySet xListProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel() ); + return xListProps; + } + + return null; + } + + private void RefreshView() + { + if ( m_aSettings != null ) + { + String[] pWikiList = m_aSettings.getWikiURLs(); + XPropertySet xListProps = GetPropSet( "WikiList" ); + if ( xListProps != null ) + { + try + { + xListProps.setPropertyValue( "StringItemList", pWikiList ); + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + } + } + + private void CheckButtonState() + { + XPropertySet xListProps = GetPropSet( "WikiList" ); + if ( xListProps != null ) + { + try + { + short [] pSel = (short []) xListProps.getPropertyValue( "SelectedItems" ); + XPropertySet xEditProps = GetPropSet( "EditButton" ); + XPropertySet xRemoveProps = GetPropSet( "RemoveButton" ); + Boolean bState = new Boolean( pSel.length != 0 ); + + xEditProps.setPropertyValue( "Enabled", bState ); + xRemoveProps.setPropertyValue( "Enabled", bState ); + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + } + } + + private void AddSetting() + { + WikiEditSettingDialog aSettingDialog = new WikiEditSettingDialog( m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application" ); + if ( aSettingDialog.show() ) + RefreshView(); + } + + private void EditSetting() + { + XPropertySet xListProps = GetPropSet( "WikiList" ); + if ( xListProps != null ) + { + Hashtable ht = null; + try + { + short[] pSel = (short []) xListProps.getPropertyValue( "SelectedItems" ); + String[] pItems = (String []) xListProps.getPropertyValue("StringItemList"); + if ( pSel.length > 0 && pItems.length > pSel[0] ) + { + String selName = pItems[pSel[0]]; + ht = m_aSettings.getSettingByUrl( pItems[pSel[0]] ); + } + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + + WikiEditSettingDialog aSettingDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", ht); + if ( aSettingDialog.show() ) + RefreshView(); + } + } + + private void RemoveSetting() + { + XPropertySet xListProps = GetPropSet("WikiList"); + if ( xListProps != null ) + { + try + { + short[] pSel = (short []) xListProps.getPropertyValue("SelectedItems"); + String[] pItems = (String []) GetPropSet("WikiList").getPropertyValue("StringItemList"); + if ( pSel.length > 0 && pItems.length > pSel[0] ) + { + m_aSettings.removeSettingByUrl( pItems[pSel[0]] ); + RefreshView(); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } + + // com.sun.star.lang.XServiceInfo: + public String getImplementationName() + { + return m_sImplementationName; + } + + public boolean supportsService( String sService ) + { + int len = m_pServiceNames.length; + + for( int i=0; i < len; i++ ) + { + if ( sService.equals( m_pServiceNames[i] )) + return true; + } + return false; + } + + public String[] getSupportedServiceNames() + { + return m_pServiceNames; + } + + // XContainerWindowEventHandler + public boolean callHandlerMethod( XWindow xWindow, Object aEventObject, String sMethod ) + throws WrappedTargetException, com.sun.star.uno.RuntimeException + { + if ( sMethod.equals( sExternalEvent ) ) + { + try + { + String sEvent = (String)AnyConverter.toString( aEventObject ); + if ( sEvent != null ) + { + if ( sEvent.equals( sOk ) ) + { + if ( m_aSettings != null ) + m_aSettings.storeConfiguration(); + } + else if ( sEvent.equals( sInitialize ) || sEvent.equals( sBack ) ) + { + if ( sEvent.equals( sInitialize ) ) + { + m_xDialog = (XDialog)UnoRuntime.queryInterface( XDialog.class, xWindow ); + m_xControlContainer = (XControlContainer)UnoRuntime.queryInterface( + XControlContainer.class, m_xDialog ); + m_aSettings = Settings.getSettings( m_xContext ); + } + else if ( m_aSettings != null ) + m_aSettings.loadConfiguration(); // throw away all the changes + + RefreshView(); + CheckButtonState(); + } + } + } + catch ( com.sun.star.uno.RuntimeException r ) + { + throw r; + } + catch ( com.sun.star.uno.Exception e ) + { + throw new WrappedTargetException( sMethod, this, e ); + } + } + + return true; + } + + public boolean callHandlerMethod( XDialog xDialog, Object aEventObject, String sMethod ) + throws WrappedTargetException, com.sun.star.uno.RuntimeException + { + if ( m_xDialog != null && xDialog == m_xDialog ) + { + if ( sMethod.equals( sAdd ) ) + { + AddSetting(); + } + else if ( sMethod.equals( sEdit ) ) + { + EditSetting(); + } + else if ( sMethod.equals( sRemove ) ) + { + RemoveSetting(); + CheckButtonState(); + } + else if ( sMethod.equals( sListStatus ) ) + { + CheckButtonState(); + } + } + + return true; + } + + public String[] getSupportedMethodNames() + { + return new String[] { sExternalEvent, sAdd, sEdit, sRemove }; + } +}; + diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiPropDialog.java b/swext/mediawiki/src/com/sun/star/wiki/WikiPropDialog.java new file mode 100644 index 000000000000..2bed67061f96 --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiPropDialog.java @@ -0,0 +1,399 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiPropDialog.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:15:31 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import com.sun.star.awt.XControl; +import com.sun.star.awt.XControlModel; +import com.sun.star.awt.XDialog; +import com.sun.star.awt.XThrobber; +import com.sun.star.beans.UnknownPropertyException; +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.XNameContainer; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class WikiPropDialog extends WikiDialog{ + + WikiEditorImpl m_aWikiEditor; + + private final String sSendMethod = "Send"; + private final String sCancelMethod = "Cancel"; + private final String sHelpMethod = "Help"; + private final String sLoadMethod = "Load"; + private final String sWikiListMethod = "WikiListChange"; + private final String sArticleTextMethod = "ArticleTextChange"; + private final String sAddWikiMethod = "AddWiki"; + + String[] Methods = {sSendMethod, sCancelMethod, sHelpMethod, sLoadMethod, sWikiListMethod, sArticleTextMethod, sAddWikiMethod}; + + protected String m_sWikiEngineURL = ""; + protected String m_sWikiTitle = ""; + protected String m_sWikiComment = ""; + protected boolean m_bWikiMinorEdit = false; + protected boolean m_bWikiShowBrowser = false; + + private Thread m_aSendingThread; + + /** Creates a new instance of WikiPropDialog */ + public WikiPropDialog(XComponentContext c, String DialogURL, WikiEditorImpl aWikiEditorForThrobber ) + { + super(c, DialogURL); + super.setMethods(Methods); + + if ( aWikiEditorForThrobber != null ) + { + InsertThrobber(); + m_aWikiEditor = aWikiEditorForThrobber; + } + } + + + public void fillWikiList() + { + String [] WikiList = m_aSettings.getWikiURLs(); + + try + { + XPropertySet xPS = getPropSet("WikiList"); + xPS.setPropertyValue("StringItemList", WikiList); + // short [] nSel = new short[1]; + // nSel[0] = (short) m_aSettings.getLastUsedWikiServer(); + // xPS.setPropertyValue("SelectedItems", sel); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + public void fillDocList() + { + XPropertySet xPS = getPropSet("ArticleText"); + try + { + short [] sel = (short[]) getPropSet("WikiList").getPropertyValue("SelectedItems"); + xPS.setPropertyValue("StringItemList", m_aSettings.getWikiDocList(sel[0], 5)); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + + public void setm_sWikiTitle(String sArticle) + { + this.m_sWikiTitle = sArticle; + try + { + XPropertySet xPS = getPropSet("ArticleText"); + xPS.setPropertyValue("Text", sArticle); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + + public void switchSendButtonIfNecessary() + { + XPropertySet xSendButton = getPropSet( "SendButton" ); + if ( xSendButton != null ) + { + XPropertySet xWikiListProps = getPropSet( "WikiList" ); + XPropertySet xArticleProps = getPropSet( "ArticleText" ); + if ( xWikiListProps != null && xArticleProps != null ) + { + try + { + short [] pSel = (short[]) getPropSet("WikiList").getPropertyValue("SelectedItems"); + String sArticle = (String)xArticleProps.getPropertyValue( "Text" ); + if ( pSel != null && pSel.length > 0 && sArticle != null && sArticle.length() != 0 ) + xSendButton.setPropertyValue( "Enabled", Boolean.TRUE ); + else + xSendButton.setPropertyValue( "Enabled", Boolean.FALSE ); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } + } + + + public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName ) + { + if ( MethodName.equals( sSendMethod ) ) + { + try + { + XPropertySet aWikiListProps = getPropSet( "WikiList" ); + XPropertySet aArticleTextProps = getPropSet( "ArticleText" ); + XPropertySet aCommentTextProps = getPropSet( "CommentText" ); + XPropertySet aMinorCheckProps = getPropSet( "MinorCheck" ); + XPropertySet aBrowserCheckProps = getPropSet( "BrowserCheck" ); + XPropertySet aHelpButtonProps = getPropSet( "HelpButton" ); + XPropertySet aSendButtonProps = getPropSet( "SendButton" ); + XPropertySet aAddButtonProps = getPropSet( "AddButton" ); + + short [] sel = (short[]) aWikiListProps.getPropertyValue("SelectedItems"); + String [] items = (String []) aWikiListProps.getPropertyValue("StringItemList"); + m_sWikiEngineURL = items[sel[0]]; + m_aSettings.setLastUsedWikiServer(sel[0]); + m_sWikiTitle = (String) aArticleTextProps.getPropertyValue("Text"); + m_sWikiComment = (String) aCommentTextProps.getPropertyValue("Text"); + + short minorState = ((Short) aMinorCheckProps.getPropertyValue("State")).shortValue(); + if (minorState != 0) + m_bWikiMinorEdit = true; + else + m_bWikiMinorEdit = false; + + short nBrowserState = ((Short) aBrowserCheckProps.getPropertyValue("State")).shortValue(); + if ( nBrowserState != 0 ) + m_bWikiShowBrowser = true; + else + m_bWikiShowBrowser = false; + + XPropertySet[] aToDisable = { aWikiListProps, aArticleTextProps, aCommentTextProps, aMinorCheckProps, aBrowserCheckProps, aHelpButtonProps, aSendButtonProps, aAddButtonProps }; + for ( int nInd = 0; nInd < aToDisable.length; nInd++ ) + aToDisable[nInd].setPropertyValue( "Enabled", Boolean.FALSE ); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + // TODO: In future the result of storing will be interesting + // TODO: do not do it in OOo2.2 + final WikiPropDialog aThisDialog = this; + final XDialog xDialogToClose = xDialog; + + boolean bAllowThreadUsage = false; + try + { + XMultiComponentFactory xFactory = m_xContext.getServiceManager(); + if ( xFactory == null ) + throw new com.sun.star.uno.RuntimeException(); + + Object oCheckCallback = xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", m_xContext ); + bAllowThreadUsage = ( oCheckCallback != null ); + } + catch( Exception e ) + { + e.printStackTrace(); + } + + // start spinning + SetThrobberActive( true ); + + if ( bAllowThreadUsage ) + { + m_aSendingThread = new Thread( "com.sun.star.thread.WikiEditorSendingThread" ) + { + public void run() + { + try + { + if ( m_aWikiEditor != null ) + { + Thread.yield(); + m_aWikiEditor.SendArticleImpl( aThisDialog ); + m_bAction = true; + } + } catch( java.lang.Exception e ) + {} + finally + { + xDialogToClose.endExecute(); + Helper.AllowConnection( true ); + } + } + }; + + m_aSendingThread.start(); + } + else + { + try + { + if ( m_aWikiEditor != null ) + { + m_aWikiEditor.SendArticleImpl( aThisDialog ); + m_bAction = true; + } + } catch( java.lang.Exception e ) + {} + finally + { + xDialogToClose.endExecute(); + Helper.AllowConnection( true ); + } + } + + return true; + } + else if ( MethodName.equals( sLoadMethod ) ) + { + try + { + short [] sel = (short[]) getPropSet("WikiList").getPropertyValue("SelectedItems"); + String [] items = (String []) getPropSet("WikiList").getPropertyValue("StringItemList"); + m_sWikiEngineURL = items[sel[0]]; + m_aSettings.setLastUsedWikiServer(sel[0]); + m_sWikiTitle = (String) getPropSet("ArticleText").getPropertyValue("Text"); + } + catch (UnknownPropertyException ex) + { + ex.printStackTrace(); + } + catch (WrappedTargetException ex) + { + ex.printStackTrace(); + } + m_bAction = true; + xDialog.endExecute(); + return true; + } + else if ( MethodName.equals( sCancelMethod ) ) + { + // disallow any connection till the dialog is closed + Helper.AllowConnection( false ); + + if ( m_aSendingThread == null ) + { + m_bAction = false; + xDialog.endExecute(); + } + else + { + ErrorDialog aED = new ErrorDialog( m_xContext, "vnd.sun.star.script:WikiEditor.Error?location=application", "The sending process has been stopped. But the wiki-page might be already changed!" ); + aED.show(); + } + + return true; + } + else if ( MethodName.equals( sHelpMethod ) ) + { + m_bAction = false; + //xDialog.endExecute(); + return true; + } + else if ( MethodName.equals( sWikiListMethod ) ) + { + fillDocList(); + switchSendButtonIfNecessary(); + return true; + } + else if ( MethodName.equals( sArticleTextMethod ) ) + { + switchSendButtonIfNecessary(); + return true; + } + else if ( MethodName.equals( sAddWikiMethod ) ) + { + WikiEditSettingDialog xAddDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application"); + if ( xAddDialog.show() ) + fillWikiList(); + + return true; + } + + return false; + } + + private void InsertThrobber() + { + try + { + XControl xDialogControl = ( XControl ) UnoRuntime.queryInterface( XControl.class, m_xDialog ); + XControlModel xDialogModel = null; + if ( xDialogControl != null ) + xDialogModel = xDialogControl.getModel(); + + XMultiServiceFactory xDialogFactory = ( XMultiServiceFactory ) UnoRuntime.queryInterface( XMultiServiceFactory.class, xDialogModel ); + if ( xDialogFactory != null ) + { + XControlModel xThrobberModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, xDialogFactory.createInstance( "com.sun.star.awt.UnoThrobberControlModel" ) ); + XPropertySet xThrobberProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xThrobberModel ); + if ( xThrobberProps != null ) + { + xThrobberProps.setPropertyValue( "Name", "WikiThrobber" ); + xThrobberProps.setPropertyValue( "PositionX", new Integer( 242 ) ); + xThrobberProps.setPropertyValue( "PositionY", new Integer( 42 ) ); + xThrobberProps.setPropertyValue( "Height", new Integer( 16 ) ); + xThrobberProps.setPropertyValue( "Width", new Integer( 16 ) ); + + XNameContainer xDialogContainer = (XNameContainer)UnoRuntime.queryInterface( XNameContainer.class, xDialogModel ); + xDialogContainer.insertByName( "WikiThrobber", xThrobberModel ); + } + } + } + catch( Exception e ) + { + e.printStackTrace(); + } + } + + public void SetThrobberActive( boolean bActive ) + { + if ( m_xControlContainer != null ) + { + try + { + XThrobber xThrobber = (XThrobber)UnoRuntime.queryInterface( XThrobber.class, m_xControlContainer.getControl( "WikiThrobber" ) ); + if ( xThrobber != null ) + { + if ( bActive ) + xThrobber.start(); + else + xThrobber.stop(); + } + } + catch( Exception e ) + { + e.printStackTrace(); + } + } + } + +} + diff --git a/swext/mediawiki/src/com/sun/star/wiki/WikiProtocolSocketFactory.java b/swext/mediawiki/src/com/sun/star/wiki/WikiProtocolSocketFactory.java new file mode 100644 index 000000000000..fd4d19225acd --- /dev/null +++ b/swext/mediawiki/src/com/sun/star/wiki/WikiProtocolSocketFactory.java @@ -0,0 +1,176 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WikiProtocolSocketFactory.java,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: mav $ $Date: 2007-11-28 11:15:42 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +package com.sun.star.wiki; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.KeyStore; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import org.apache.commons.httpclient.ConnectTimeoutException; +import org.apache.commons.httpclient.HttpClientError; +import org.apache.commons.httpclient.params.HttpConnectionParams; +import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; + +class WikiProtocolSocketFactory implements SecureProtocolSocketFactory +{ + private SSLContext m_aSSLContext; + + public WikiProtocolSocketFactory() + { + super(); + } + + public synchronized SSLContext GetNotSoSecureSSLContext() + { + if ( m_aSSLContext == null ) + { + TrustManager[] pTrustUnknownCerts = new TrustManager[] + { + new X509TrustManager() { + private X509TrustManager m_aOrgTrustManager; + + private X509TrustManager GetOrgTrustManager() + { + if ( m_aOrgTrustManager == null ) + { + try + { + TrustManagerFactory aFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); + aFactory.init( (KeyStore)null ); + TrustManager[] pTrustmanagers = aFactory.getTrustManagers(); + if ( pTrustmanagers.length != 0 && pTrustmanagers[0] != null ) + m_aOrgTrustManager = (X509TrustManager)pTrustmanagers[0]; + } + catch( Exception e ) + { + throw new RuntimeException( "No access to the default trust manager!" ); + } + } + + return m_aOrgTrustManager; + } + + public X509Certificate[] getAcceptedIssuers() + { + return GetOrgTrustManager().getAcceptedIssuers(); + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException + { + GetOrgTrustManager().checkClientTrusted( certs, authType ); + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException + { + if ( certs == null || certs.length == 0 ) + GetOrgTrustManager().checkServerTrusted( certs, authType ); + else + for ( int nInd = 0; nInd < certs.length; nInd++ ) + certs[nInd].checkValidity(); + } + } + }; + + try + { + SSLContext aContext = SSLContext.getInstance("SSL"); + if ( aContext != null ) + { + aContext.init( null, pTrustUnknownCerts, null ); + m_aSSLContext = aContext; + } + } + catch ( Exception e ) + { + } + } + + if ( m_aSSLContext == null ) + throw new HttpClientError(); + + return m_aSSLContext; + } + + public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort ) + throws IOException, UnknownHostException + { + return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort ); + } + + public Socket createSocket( final String sHost, final int nPort, final InetAddress aLocalAddress, final int nLocalPort, final HttpConnectionParams params ) + throws IOException, UnknownHostException, ConnectTimeoutException + { + if ( params == null ) + return createSocket( sHost, nPort, aLocalAddress, nLocalPort ); + + int nTimeout = params.getConnectionTimeout(); + Socket aSocket = GetNotSoSecureSSLContext().getSocketFactory().createSocket(); + aSocket.bind( new InetSocketAddress( aLocalAddress, nLocalPort ) ); + aSocket.connect( new InetSocketAddress( sHost, nPort ), nTimeout ); + return aSocket; + } + + public Socket createSocket( String sHost, int nPort ) + throws IOException, UnknownHostException + { + return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort ); + } + + public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose ) + throws IOException, UnknownHostException + { + return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose ); + } + + public boolean equals(Object obj) + { + return ((obj != null) && obj.getClass().equals(WikiProtocolSocketFactory.class)); + } + + public int hashCode() + { + return WikiProtocolSocketFactory.class.hashCode(); + } +}; + |