| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.wiki; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.net.URI; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
import javax.net.ssl.SSLException; |
| 27 |
|
| 28 |
import com.sun.star.awt.XWindowPeer; |
| 29 |
import com.sun.star.beans.PropertyValue; |
| 30 |
import com.sun.star.frame.DispatchDescriptor; |
| 31 |
import com.sun.star.frame.XController; |
| 32 |
import com.sun.star.frame.XDispatch; |
| 33 |
import com.sun.star.frame.XDispatchProvider; |
| 34 |
import com.sun.star.frame.XFrame; |
| 35 |
import com.sun.star.frame.XModel; |
| 36 |
import com.sun.star.frame.XStorable; |
| 37 |
import com.sun.star.lang.XInitialization; |
| 38 |
import com.sun.star.lang.XSingleComponentFactory; |
| 39 |
import com.sun.star.lib.uno.helper.Factory; |
| 40 |
import com.sun.star.lib.uno.helper.WeakBase; |
| 41 |
import com.sun.star.uno.UnoRuntime; |
| 42 |
import com.sun.star.uno.XComponentContext; |
| 43 |
|
| 44 |
|
| 45 |
public final class WikiEditorImpl extends WeakBase |
| 46 |
implements com.sun.star.lang.XServiceInfo, XDispatchProvider, XDispatch, XInitialization |
| 47 |
{ |
| 48 |
|
| 49 |
private static final String m_implementationName = WikiEditorImpl.class.getName(); |
| 50 |
private static final String[] m_serviceNames = {"com.sun.star.wiki.WikiEditor" }; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
// protocol name that this protocol handler handles |
| 55 |
private static final String protocolName = "vnd.com.sun.star.wiki:"; |
| 56 |
|
| 57 |
private final XComponentContext m_xContext; |
| 58 |
private XFrame m_xFrame; |
| 59 |
private XModel m_xModel; |
| 60 |
private final Settings m_aSettings; |
| 61 |
private String m_aFilterName; |
| 62 |
|
| 63 |
public WikiEditorImpl( XComponentContext xContext ) |
| 64 |
{ |
| 65 |
// Helper.trustAllSSL(); |
| 66 |
m_xContext = xContext; |
| 67 |
m_aSettings = Settings.getSettings( m_xContext ); |
| 68 |
} |
| 69 |
|
| 70 |
public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) |
| 71 |
{ |
| 72 |
XSingleComponentFactory xFactory = null; |
| 73 |
|
| 74 |
if ( sImplementationName.equals( m_implementationName ) ) |
| 75 |
xFactory = Factory.createComponentFactory( WikiEditorImpl.class, m_serviceNames ); |
| 76 |
else if ( sImplementationName.equals( WikiOptionsEventHandlerImpl.m_sImplementationName ) ) |
| 77 |
xFactory = Factory.createComponentFactory( WikiOptionsEventHandlerImpl.class, |
| 78 |
WikiOptionsEventHandlerImpl.m_pServiceNames ); |
| 79 |
|
| 80 |
return xFactory; |
| 81 |
} |
| 82 |
|
| 83 |
// com.sun.star.lang.XServiceInfo: |
| 84 |
public String getImplementationName() |
| 85 |
{ |
| 86 |
return m_implementationName; |
| 87 |
} |
| 88 |
|
| 89 |
public boolean supportsService( String sService ) |
| 90 |
{ |
| 91 |
int len = m_serviceNames.length; |
| 92 |
|
| 93 |
for( int i=0; i < len; i++ ) |
| 94 |
{ |
| 95 |
if ( sService.equals( m_serviceNames[i] )) |
| 96 |
return true; |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
public String[] getSupportedServiceNames() |
| 102 |
{ |
| 103 |
return m_serviceNames; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception |
| 108 |
{ |
| 109 |
if ( args.length > 0 ) |
| 110 |
{ |
| 111 |
m_xFrame = UnoRuntime.queryInterface( XFrame.class, args[0] ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void dispatch( |
| 118 |
final com.sun.star.util.URL aURL, |
| 119 |
com.sun.star.beans.PropertyValue[] propertyValue ) |
| 120 |
{ |
| 121 |
final com.sun.star.util.URL myURL = aURL; |
| 122 |
if ( aURL.Protocol.equals(protocolName) ) |
| 123 |
{ |
| 124 |
try |
| 125 |
{ |
| 126 |
if ( myURL.Path.equals("send") ) |
| 127 |
{ |
| 128 |
sendArticle(); |
| 129 |
} |
| 130 |
} catch( java.lang.Throwable t ) |
| 131 |
{ |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
public com.sun.star.frame.XDispatch queryDispatch( |
| 138 |
com.sun.star.util.URL aURL, |
| 139 |
String str, |
| 140 |
int param ) |
| 141 |
{ |
| 142 |
if ( aURL.Protocol.equals( protocolName )) |
| 143 |
{ |
| 144 |
|
| 145 |
// by default, we are responsible |
| 146 |
return this; |
| 147 |
} else |
| 148 |
{ |
| 149 |
return null; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public XDispatch[] queryDispatches( DispatchDescriptor[] seqDescripts ) |
| 154 |
{ |
| 155 |
int nCount = seqDescripts.length; |
| 156 |
XDispatch[] lDispatcher = new XDispatch[nCount]; |
| 157 |
|
| 158 |
for( int i=0; i<nCount; ++i ) |
| 159 |
lDispatcher[i] = queryDispatch( |
| 160 |
seqDescripts[i].FeatureURL, |
| 161 |
seqDescripts[i].FrameName, |
| 162 |
seqDescripts[i].SearchFlags ); |
| 163 |
return lDispatcher; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public void removeStatusListener( |
| 168 |
com.sun.star.frame.XStatusListener xStatusListener, |
| 169 |
com.sun.star.util.URL aURL ) |
| 170 |
{ |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
public void addStatusListener( |
| 175 |
com.sun.star.frame.XStatusListener listener, |
| 176 |
com.sun.star.util.URL url ) |
| 177 |
{ |
| 178 |
} |
| 179 |
|
| 180 |
private void sendArticle() |
| 181 |
{ |
| 182 |
if ( m_xFrame != null ) |
| 183 |
{ |
| 184 |
WikiPropDialog aSendDialog = null; |
| 185 |
try |
| 186 |
{ |
| 187 |
if ( m_xModel == null ) |
| 188 |
{ |
| 189 |
XController xController = m_xFrame.getController(); |
| 190 |
if ( xController != null ) |
| 191 |
m_xModel = xController.getModel(); |
| 192 |
} |
| 193 |
|
| 194 |
if ( m_xModel != null ) |
| 195 |
{ |
| 196 |
// The related Wiki filter must be detected from the typename |
| 197 |
String aServiceName = Helper.GetDocServiceName( m_xContext, m_xModel ); |
| 198 |
m_aFilterName = Helper.GetFilterName( m_xContext, "MediaWiki", aServiceName ); |
| 199 |
|
| 200 |
if ( m_aFilterName == null || m_aFilterName.length() == 0 ) |
| 201 |
{ |
| 202 |
Helper.ShowError( m_xContext, |
| 203 |
UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ), |
| 204 |
Helper.DLG_SENDTITLE, |
| 205 |
Helper.NOWIKIFILTER_ERROR, |
| 206 |
null, |
| 207 |
false ); |
| 208 |
throw new com.sun.star.uno.RuntimeException(); |
| 209 |
} |
| 210 |
|
| 211 |
m_aSettings.loadConfiguration(); // throw away all the noncommitted changes |
| 212 |
// show the send dialog |
| 213 |
aSendDialog = new WikiPropDialog( m_xContext, "vnd.sun.star.script:WikiEditor.SendToMediaWiki?location=application", this ); |
| 214 |
aSendDialog.fillWikiList(); |
| 215 |
aSendDialog.SetWikiTitle( Helper.GetDocTitle( m_xModel ) ); |
| 216 |
aSendDialog.show(); // triggers the sending |
| 217 |
} |
| 218 |
} |
| 219 |
catch ( Exception e ) |
| 220 |
{ |
| 221 |
// TODO: Error handling here |
| 222 |
e.printStackTrace(); |
| 223 |
} |
| 224 |
finally |
| 225 |
{ |
| 226 |
if ( aSendDialog != null ) |
| 227 |
aSendDialog.DisposeDialog(); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
public boolean SendArticleImpl( WikiPropDialog aSendDialog, Map<String,String> aWikiSetting ) |
| 233 |
{ |
| 234 |
boolean bResult = false; |
| 235 |
|
| 236 |
if ( aSendDialog != null ) |
| 237 |
{ |
| 238 |
String sTemp2Url = null; |
| 239 |
|
| 240 |
try |
| 241 |
{ |
| 242 |
// TODO: stop progress spinning |
| 243 |
WikiArticle aArticle = new WikiArticle( m_xContext, aSendDialog.GetWikiTitle(), aWikiSetting, true, aSendDialog ); |
| 244 |
|
| 245 |
boolean bAllowSending = true; |
| 246 |
if ( !aArticle.NotExist() ) |
| 247 |
{ |
| 248 |
// ask whether creation of a new page is allowed |
| 249 |
aSendDialog.SetThrobberActive( false ); |
| 250 |
bAllowSending = Helper.ShowError( |
| 251 |
m_xContext, |
| 252 |
UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ), |
| 253 |
Helper.DLG_SENDTITLE, |
| 254 |
Helper.DLG_WIKIPAGEEXISTS_LABEL1, |
| 255 |
aSendDialog.GetWikiTitle(), |
| 256 |
true ); |
| 257 |
aSendDialog.SetThrobberActive( true ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( bAllowSending ) |
| 261 |
{ |
| 262 |
PropertyValue[] lProperties = new PropertyValue[2]; |
| 263 |
lProperties[0] = new PropertyValue(); |
| 264 |
lProperties[0].Name = "FilterName"; |
| 265 |
lProperties[0].Value = m_aFilterName; |
| 266 |
lProperties[1] = new PropertyValue(); |
| 267 |
lProperties[1].Name = "Overwrite"; |
| 268 |
lProperties[1].Value = Boolean.TRUE; |
| 269 |
|
| 270 |
sTemp2Url = Helper.CreateTempFile( m_xContext ); |
| 271 |
|
| 272 |
XStorable xStore = UnoRuntime.queryInterface ( XStorable.class, m_xModel ); |
| 273 |
if ( xStore == null ) |
| 274 |
throw new com.sun.star.uno.RuntimeException(); |
| 275 |
|
| 276 |
xStore.storeToURL( sTemp2Url, lProperties ); |
| 277 |
String sWikiCode = Helper.EachLine( sTemp2Url ); |
| 278 |
|
| 279 |
if ( aArticle.setArticle( sWikiCode, aSendDialog.m_sWikiComment, aSendDialog.m_bWikiMinorEdit ) ) |
| 280 |
{ |
| 281 |
bResult = true; |
| 282 |
Helper.SetDocTitle( m_xModel, aArticle.GetTitle() ); |
| 283 |
Map<String,Object> aDocInfo = new HashMap<String,Object>(); |
| 284 |
aDocInfo.put( "Doc", aArticle.GetTitle() ); |
| 285 |
aDocInfo.put( "Url", aArticle.GetMainURL() ); |
| 286 |
aDocInfo.put( "CompleteUrl", aArticle.GetMainURL() + aArticle.GetTitle() ); |
| 287 |
m_aSettings.addWikiDoc( aDocInfo ); |
| 288 |
m_aSettings.storeConfiguration(); |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
Helper.ShowError( m_xContext, |
| 293 |
UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ), |
| 294 |
Helper.DLG_SENDTITLE, |
| 295 |
Helper.GENERALSEND_ERROR, |
| 296 |
null, |
| 297 |
false ); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
catch( WikiCancelException ec ) |
| 302 |
{ |
| 303 |
// nothing to do, the sending was cancelled |
| 304 |
} |
| 305 |
catch( SSLException essl ) |
| 306 |
{ |
| 307 |
if ( Helper.IsConnectionAllowed() ) |
| 308 |
{ |
| 309 |
// report the error only if sending was not cancelled |
| 310 |
Helper.ShowError( m_xContext, |
| 311 |
UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ), |
| 312 |
Helper.DLG_SENDTITLE, |
| 313 |
Helper.UNKNOWNCERT_ERROR, |
| 314 |
null, |
| 315 |
false ); |
| 316 |
} |
| 317 |
} |
| 318 |
catch( Exception e ) |
| 319 |
{ |
| 320 |
if ( Helper.IsConnectionAllowed() ) |
| 321 |
{ |
| 322 |
// report the error only if sending was not cancelled |
| 323 |
Helper.ShowError( m_xContext, |
| 324 |
UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ), |
| 325 |
Helper.DLG_SENDTITLE, |
| 326 |
Helper.GENERALSEND_ERROR, |
| 327 |
null, |
| 328 |
false ); |
| 329 |
} |
| 330 |
e.printStackTrace(); |
| 331 |
} |
| 332 |
|
| 333 |
if ( sTemp2Url != null ) |
| 334 |
{ |
| 335 |
try |
| 336 |
{ |
| 337 |
// remove the temporary file |
| 338 |
File aFile = new File( new URI( sTemp2Url ) ); |
| 339 |
if (!aFile.delete()) { |
| 340 |
throw new java.lang.Exception("could not remove" + sTemp2Url); |
| 341 |
} |
| 342 |
} |
| 343 |
catch ( java.lang.Exception e ) |
| 344 |
{ |
| 345 |
e.printStackTrace(); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return bResult; |
| 351 |
} |
| 352 |
|
| 353 |
} |