cid#1607171 Data race condition
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiEditorImpl.java
blob3d1bee21e7352ada5126fea9cf387638b7114b2d
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.wiki;
21 import java.io.File;
22 import java.net.URI;
23 import java.util.HashMap;
24 import java.util.Map;
26 import javax.net.ssl.SSLException;
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;
45 public final class WikiEditorImpl extends WeakBase
46 implements com.sun.star.lang.XServiceInfo, XDispatchProvider, XDispatch, XInitialization
49 private static final String m_implementationName = WikiEditorImpl.class.getName();
50 private static final String[] m_serviceNames = {"com.sun.star.wiki.WikiEditor" };
54 // protocol name that this protocol handler handles
55 private static final String protocolName = "vnd.com.sun.star.wiki:";
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;
63 public WikiEditorImpl( XComponentContext xContext )
65 // Helper.trustAllSSL();
66 m_xContext = xContext;
67 m_aSettings = Settings.getSettings( m_xContext );
70 public static XSingleComponentFactory __getComponentFactory( String sImplementationName )
72 XSingleComponentFactory xFactory = null;
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 );
80 return xFactory;
83 // com.sun.star.lang.XServiceInfo:
84 public String getImplementationName()
86 return m_implementationName;
89 public boolean supportsService( String sService )
91 int len = m_serviceNames.length;
93 for( int i=0; i < len; i++ )
95 if ( sService.equals( m_serviceNames[i] ))
96 return true;
98 return false;
101 public String[] getSupportedServiceNames()
103 return m_serviceNames;
107 public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception
109 if ( args.length > 0 )
111 m_xFrame = UnoRuntime.queryInterface( XFrame.class, args[0] );
117 public void dispatch(
118 final com.sun.star.util.URL aURL,
119 com.sun.star.beans.PropertyValue[] propertyValue )
121 final com.sun.star.util.URL myURL = aURL;
122 if ( aURL.Protocol.equals(protocolName) )
126 if ( myURL.Path.equals("send") )
128 sendArticle();
130 } catch( java.lang.Throwable t )
137 public com.sun.star.frame.XDispatch queryDispatch(
138 com.sun.star.util.URL aURL,
139 String str,
140 int param )
142 if ( aURL.Protocol.equals( protocolName ))
145 // by default, we are responsible
146 return this;
147 } else
149 return null;
153 public XDispatch[] queryDispatches( DispatchDescriptor[] seqDescripts )
155 int nCount = seqDescripts.length;
156 XDispatch[] lDispatcher = new XDispatch[nCount];
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;
167 public void removeStatusListener(
168 com.sun.star.frame.XStatusListener xStatusListener,
169 com.sun.star.util.URL aURL )
174 public void addStatusListener(
175 com.sun.star.frame.XStatusListener listener,
176 com.sun.star.util.URL url )
180 private void sendArticle()
182 if ( m_xFrame != null )
184 WikiPropDialog aSendDialog = null;
187 if ( m_xModel == null )
189 XController xController = m_xFrame.getController();
190 if ( xController != null )
191 m_xModel = xController.getModel();
194 if ( m_xModel != null )
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 );
200 if ( m_aFilterName == null || m_aFilterName.length() == 0 )
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();
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
219 catch ( Exception e )
221 // TODO: Error handling here
222 e.printStackTrace();
224 finally
226 if ( aSendDialog != null )
227 aSendDialog.DisposeDialog();
232 public boolean SendArticleImpl( WikiPropDialog aSendDialog, Map<String,String> aWikiSetting )
234 boolean bResult = false;
236 if ( aSendDialog != null )
238 String sTemp2Url = null;
242 // TODO: stop progress spinning
243 WikiArticle aArticle = new WikiArticle( m_xContext, aSendDialog.GetWikiTitle(), aWikiSetting, true, aSendDialog );
245 boolean bAllowSending = true;
246 if ( !aArticle.NotExist() )
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 );
260 if ( bAllowSending )
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;
270 sTemp2Url = Helper.CreateTempFile( m_xContext );
272 XStorable xStore = UnoRuntime.queryInterface ( XStorable.class, m_xModel );
273 if ( xStore == null )
274 throw new com.sun.star.uno.RuntimeException();
276 xStore.storeToURL( sTemp2Url, lProperties );
277 String sWikiCode = Helper.EachLine( sTemp2Url );
279 if ( aArticle.setArticle( sWikiCode, aSendDialog.m_sWikiComment, aSendDialog.m_bWikiMinorEdit ) )
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();
290 else
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 );
301 catch( WikiCancelException ec )
303 // nothing to do, the sending was cancelled
305 catch( SSLException essl )
307 if ( Helper.IsConnectionAllowed() )
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 );
318 catch( Exception e )
320 if ( Helper.IsConnectionAllowed() )
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 );
330 e.printStackTrace();
333 if ( sTemp2Url != null )
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);
343 catch ( java.lang.Exception e )
345 e.printStackTrace();
350 return bResult;