update emoji autocorrect entries from po-files
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiEditorImpl.java
blob1e89e5c32da698b3e95a27ee69f78a599d5f063f
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.net.URISyntaxException;
24 import java.util.HashMap;
25 import java.util.Map;
27 import javax.net.ssl.SSLException;
29 import com.sun.star.awt.XWindowPeer;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.frame.DispatchDescriptor;
32 import com.sun.star.frame.XController;
33 import com.sun.star.frame.XDispatch;
34 import com.sun.star.frame.XDispatchProvider;
35 import com.sun.star.frame.XFrame;
36 import com.sun.star.frame.XModel;
37 import com.sun.star.frame.XStorable;
38 import com.sun.star.lang.XInitialization;
39 import com.sun.star.lang.XSingleComponentFactory;
40 import com.sun.star.lib.uno.helper.Factory;
41 import com.sun.star.lib.uno.helper.WeakBase;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XComponentContext;
46 public final class WikiEditorImpl extends WeakBase
47 implements com.sun.star.lang.XServiceInfo, XDispatchProvider, XDispatch, XInitialization
50 private static final String m_implementationName = WikiEditorImpl.class.getName();
51 private static final String[] m_serviceNames = {"com.sun.star.wiki.WikiEditor" };
55 // protocol name that this protocol handler handles
56 private static final String protocolName = "vnd.com.sun.star.wiki:";
58 private final XComponentContext m_xContext;
59 private final Map<String, com.sun.star.frame.XStatusListener> m_statusListeners = new HashMap<String, com.sun.star.frame.XStatusListener>();
60 private XFrame m_xFrame;
61 private XModel m_xModel;
62 private final Settings m_aSettings;
63 private String m_aFilterName;
65 public WikiEditorImpl( XComponentContext xContext )
67 // Helper.trustAllSSL();
68 m_xContext = xContext;
69 m_aSettings = Settings.getSettings( m_xContext );
72 public static XSingleComponentFactory __getComponentFactory( String sImplementationName )
74 XSingleComponentFactory xFactory = null;
76 if ( sImplementationName.equals( m_implementationName ) )
77 xFactory = Factory.createComponentFactory( WikiEditorImpl.class, m_serviceNames );
78 else if ( sImplementationName.equals( WikiOptionsEventHandlerImpl.m_sImplementationName ) )
79 xFactory = Factory.createComponentFactory( WikiOptionsEventHandlerImpl.class,
80 WikiOptionsEventHandlerImpl.m_pServiceNames );
82 return xFactory;
85 // com.sun.star.lang.XServiceInfo:
86 public String getImplementationName()
88 return m_implementationName;
91 public boolean supportsService( String sService )
93 int len = m_serviceNames.length;
95 for( int i=0; i < len; i++ )
97 if ( sService.equals( m_serviceNames[i] ))
98 return true;
100 return false;
103 public String[] getSupportedServiceNames()
105 return m_serviceNames;
109 public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception
111 if ( args.length > 0 )
113 m_xFrame = UnoRuntime.queryInterface( XFrame.class, args[0] );
119 public void dispatch(
120 final com.sun.star.util.URL aURL,
121 com.sun.star.beans.PropertyValue[] propertyValue )
123 final com.sun.star.util.URL myURL = aURL;
124 if ( aURL.Protocol.equals(protocolName) )
128 if ( myURL.Path.equals("send") )
130 sendArticle();
132 } catch( java.lang.Throwable t )
139 public com.sun.star.frame.XDispatch queryDispatch(
140 com.sun.star.util.URL aURL,
141 String str,
142 int param )
144 if ( aURL.Protocol.equals( protocolName ))
147 // by default, we are responsible
148 return this;
149 } else
151 return null;
155 public XDispatch[] queryDispatches( DispatchDescriptor[] seqDescripts )
157 int nCount = seqDescripts.length;
158 XDispatch[] lDispatcher = new XDispatch[nCount];
160 for( int i=0; i<nCount; ++i )
161 lDispatcher[i] = queryDispatch(
162 seqDescripts[i].FeatureURL,
163 seqDescripts[i].FrameName,
164 seqDescripts[i].SearchFlags );
165 return lDispatcher;
169 public void removeStatusListener(
170 com.sun.star.frame.XStatusListener xStatusListener,
171 com.sun.star.util.URL aURL )
176 public void addStatusListener(
177 com.sun.star.frame.XStatusListener listener,
178 com.sun.star.util.URL url )
180 String urlstring = url.Complete;
181 m_statusListeners.put( urlstring, listener );
182 // synchroneous callback required!!!
183 callStatusListener( urlstring );
188 private void callStatusListener( String uristring )
192 new URI( uristring );
194 // check whether any blogs are live...
195 setListenerState( "command");
196 } catch ( URISyntaxException ex )
198 ex.printStackTrace();
203 private void setListenerState( String urlstring)
205 com.sun.star.util.URL url = new com.sun.star.util.URL();
206 url.Complete = urlstring;
209 private void sendArticle()
211 if ( m_xFrame != null )
213 WikiPropDialog aSendDialog = null;
216 if ( m_xModel == null )
218 XController xController = m_xFrame.getController();
219 if ( xController != null )
220 m_xModel = xController.getModel();
223 if ( m_xModel != null )
225 // The related Wiki filter must be detected from the typename
226 String aServiceName = Helper.GetDocServiceName( m_xContext, m_xModel );
227 m_aFilterName = Helper.GetFilterName( m_xContext, "MediaWiki", aServiceName );
229 if ( m_aFilterName == null || m_aFilterName.length() == 0 )
231 Helper.ShowError( m_xContext,
232 UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ),
233 Helper.DLG_SENDTITLE,
234 Helper.NOWIKIFILTER_ERROR,
235 null,
236 false );
237 throw new com.sun.star.uno.RuntimeException();
240 m_aSettings.loadConfiguration(); // throw away all the noncommited changes
241 // show the send dialog
242 aSendDialog = new WikiPropDialog( m_xContext, "vnd.sun.star.script:WikiEditor.SendToMediaWiki?location=application", this );
243 aSendDialog.fillWikiList();
244 aSendDialog.SetWikiTitle( Helper.GetDocTitle( m_xModel ) );
245 aSendDialog.show(); // triggers the sending
248 catch ( Exception e )
250 // TODO: Error handling here
251 e.printStackTrace();
253 finally
255 if ( aSendDialog != null )
256 aSendDialog.DisposeDialog();
261 public boolean SendArticleImpl( WikiPropDialog aSendDialog, Map<String,String> aWikiSetting )
263 boolean bResult = false;
265 if ( aSendDialog != null )
267 String sTemp2Url = null;
271 // TODO: stop progress spinning
272 WikiArticle aArticle = new WikiArticle( m_xContext, aSendDialog.GetWikiTitle(), aWikiSetting, true, aSendDialog );
274 boolean bAllowSending = true;
275 if ( !aArticle.NotExist() )
277 // ask whether creation of a new page is allowed
278 aSendDialog.SetThrobberActive( false );
279 bAllowSending = Helper.ShowError(
280 m_xContext,
281 UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ),
282 Helper.DLG_SENDTITLE,
283 Helper.DLG_WIKIPAGEEXISTS_LABEL1,
284 aSendDialog.GetWikiTitle(),
285 true );
286 aSendDialog.SetThrobberActive( true );
289 if ( bAllowSending )
291 PropertyValue[] lProperties = new PropertyValue[2];
292 lProperties[0] = new PropertyValue();
293 lProperties[0].Name = "FilterName";
294 lProperties[0].Value = m_aFilterName;
295 lProperties[1] = new PropertyValue();
296 lProperties[1].Name = "Overwrite";
297 lProperties[1].Value = Boolean.TRUE;
299 sTemp2Url = Helper.CreateTempFile( m_xContext );
301 XStorable xStore = UnoRuntime.queryInterface ( XStorable.class, m_xModel );
302 if ( xStore == null )
303 throw new com.sun.star.uno.RuntimeException();
305 xStore.storeToURL( sTemp2Url, lProperties );
306 String sWikiCode = Helper.EachLine( sTemp2Url );
308 if ( aArticle.setArticle( sWikiCode, aSendDialog.m_sWikiComment, aSendDialog.m_bWikiMinorEdit ) )
310 bResult = true;
311 Helper.SetDocTitle( m_xModel, aArticle.GetTitle() );
312 Map<String,Object> aDocInfo = new HashMap<String,Object>();
313 aDocInfo.put( "Doc", aArticle.GetTitle() );
314 aDocInfo.put( "Url", aArticle.GetMainURL() );
315 aDocInfo.put( "CompleteUrl", aArticle.GetMainURL() + aArticle.GetTitle() );
316 m_aSettings.addWikiDoc( aDocInfo );
317 m_aSettings.storeConfiguration();
319 else
321 Helper.ShowError( m_xContext,
322 UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ),
323 Helper.DLG_SENDTITLE,
324 Helper.GENERALSEND_ERROR,
325 null,
326 false );
330 catch( WikiCancelException ec )
332 // nothing to do, the sending was cancelled
334 catch( SSLException essl )
336 if ( Helper.IsConnectionAllowed() )
338 // report the error only if sending was not cancelled
339 Helper.ShowError( m_xContext,
340 UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ),
341 Helper.DLG_SENDTITLE,
342 Helper.UNKNOWNCERT_ERROR,
343 null,
344 false );
347 catch( Exception e )
349 if ( Helper.IsConnectionAllowed() )
351 // report the error only if sending was not cancelled
352 Helper.ShowError( m_xContext,
353 UnoRuntime.queryInterface( XWindowPeer.class, m_xFrame.getContainerWindow() ),
354 Helper.DLG_SENDTITLE,
355 Helper.GENERALSEND_ERROR,
356 null,
357 false );
359 e.printStackTrace();
362 if ( sTemp2Url != null )
366 // remove the temporary file
367 File aFile = new File( new URI( sTemp2Url ) );
368 aFile.delete();
370 catch ( java.lang.Exception e )
372 e.printStackTrace();
377 return bResult;