1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.wiki; 25 26 import com.sun.star.awt.MessageBoxButtons; 27 import com.sun.star.awt.MessageBoxType; 28 import com.sun.star.awt.XControl; 29 import com.sun.star.awt.XControlContainer; 30 import com.sun.star.awt.XDialog; 31 import com.sun.star.awt.XMessageBox; 32 import com.sun.star.awt.XMessageBoxFactory; 33 import com.sun.star.awt.XWindowPeer; 34 import com.sun.star.beans.NamedValue; 35 import com.sun.star.beans.PropertyValue; 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.container.XContainerQuery; 38 import com.sun.star.container.XEnumeration; 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.container.XNameContainer; 41 import com.sun.star.document.XDocumentInfoSupplier; 42 import com.sun.star.frame.XModel; 43 import com.sun.star.frame.XModuleManager; 44 import com.sun.star.io.XInputStream; 45 import com.sun.star.io.XOutputStream; 46 import com.sun.star.io.XSeekable; 47 import com.sun.star.io.XStream; 48 import com.sun.star.lang.XMultiComponentFactory; 49 import com.sun.star.lang.XMultiServiceFactory; 50 import com.sun.star.lang.XComponent; 51 import com.sun.star.system.SystemShellExecuteFlags; 52 import com.sun.star.system.XSystemShellExecute; 53 import com.sun.star.task.UrlRecord; 54 import com.sun.star.task.XInteractionHandler; 55 import com.sun.star.task.XMasterPasswordHandling; 56 import com.sun.star.task.XPasswordContainer; 57 import com.sun.star.uno.AnyConverter; 58 import com.sun.star.uno.UnoRuntime; 59 import com.sun.star.uno.XComponentContext; 60 import com.sun.star.util.XChangesBatch; 61 import java.net.*; 62 import java.io.*; 63 import java.util.ArrayList; 64 import java.util.HashSet; 65 import java.util.Iterator; 66 import java.util.List; 67 import java.util.Set; 68 import java.util.concurrent.Callable; 69 import javax.swing.text.html.HTMLEditorKit; 70 import org.w3c.dom.Document; 71 import org.w3c.dom.Element; 72 73 public class Helper 74 { 75 public final static int GENERALSEND_ERROR = 0; 76 public final static int NOWIKIFILTER_ERROR = 1; 77 public final static int NOURLCONNECTION_ERROR = 2; 78 public final static int WRONGLOGIN_ERROR = 3; 79 public final static int INVALIDURL_ERROR = 4; 80 public final static int NOURL_ERROR = 5; 81 82 public final static int DLG_SENDTITLE = 6; 83 public final static int DLG_WIKIARTICLE = 7; 84 public final static int DLG_NO = 8; 85 public final static int DLG_OK = 9; 86 public final static int DLG_YES = 10; 87 // 11 is reserved 88 public final static int DLG_ADDBUTTON = 12; 89 public final static int DLG_EDITBUTTON = 13; 90 public final static int DLG_SENDBUTTON = 14; 91 public final static int DLG_REMOVEBUTTON = 15; 92 93 public final static int DLG_EDITSETTING_URLLABEL = 16; 94 public final static int DLG_EDITSETTING_USERNAMELABEL = 17; 95 public final static int DLG_EDITSETTING_PASSWORDLABEL = 18; 96 public final static int DLG_NEWWIKIPAGE_LABEL1 = 19; 97 public final static int DLG_SENDTOMEDIAWIKI_LABEL1 = 20; 98 public final static int DLG_SENDTOMEDIAWIKI_LABEL2 = 21; 99 public final static int DLG_SENDTOMEDIAWIKI_LABEL3 = 22; 100 public final static int DLG_SENDTOMEDIAWIKI_MINORCHECK = 23; 101 public final static int DLG_SENDTOMEDIAWIKI_BROWSERCHECK = 24; 102 public final static int UNKNOWNCERT_ERROR = 25; 103 public final static int DLG_MEDIAWIKI_TITLE = 26; 104 public final static int DLG_EDITSETTING_ACCOUNTLINE = 27; 105 public final static int DLG_EDITSETTING_WIKILINE = 28; 106 public final static int DLG_EDITSETTING_SAVEBOX = 29; 107 public final static int CANCELSENDING_ERROR = 30; 108 public final static int DLG_MEDIAWIKIEXTENSION_STRING = 31; 109 public final static int DLG_WIKIPAGEEXISTS_LABEL1 = 32; 110 111 public final static int STRINGS_NUM = 33; 112 113 private final static String[] m_pEntryNames = { "GeneralSendError", 114 "NoWikiFilter", 115 "NoConnectionToURL", 116 "WrongLogin", 117 "InvalidURL", 118 "NoURL", 119 "Dlg_SendTitle", 120 "Dlg_WikiArticle", 121 "Dlg_No", 122 "Dlg_OK", 123 "Dlg_Yes", 124 null, // reserved 125 "Dlg_AddButton", 126 "Dlg_EditButton", 127 "Dlg_SendButton", 128 "Dlg_RemoveButton", 129 "Dlg_EditSetting_UrlLabel", 130 "Dlg_EditSetting_UsernameLabel", 131 "Dlg_EditSetting_PasswordLabel", 132 "Dlg_NewWikiPage_Label1", 133 "Dlg_SendToMediaWiki_Label1", 134 "Dlg_SendToMediaWiki_Label2", 135 "Dlg_SendToMediaWiki_Label3", 136 "Dlg_SendToMediaWiki_MinorCheck", 137 "Dlg_SendToMediaWiki_BrowserCheck", 138 "UnknownCert", 139 "Dlg_MediaWiki_Title", 140 "Dlg_EditSetting_AccountLine", 141 "Dlg_EditSetting_WikiLine", 142 "Dlg_EditSetting_SaveBox", 143 "CancelSending", 144 "Dlg_MediaWiki_Extension_String", 145 "Dlg_WikiPageExists_Label1" }; 146 147 private static String[] m_pConfigStrings; 148 149 private static final String sHTMLHeader = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title></title></head><body>"; 150 private static final String sHTMLFooter = "</body></html>"; 151 152 private static Set<HttpURLConnection> m_aActiveConnections = new HashSet<HttpURLConnection>(); 153 private static boolean m_bAllowConnection = true; 154 155 private static Boolean m_bShowInBrowser = null; 156 157 private static XPasswordContainer m_xPasswordContainer; 158 private static XInteractionHandler m_xInteractionHandler; 159 GetLocalizedString( XComponentContext xContext, int nID )160 synchronized protected static String GetLocalizedString( XComponentContext xContext, int nID ) 161 throws com.sun.star.uno.Exception 162 { 163 if ( nID >= STRINGS_NUM ) 164 throw new com.sun.star.uno.RuntimeException(); 165 166 if ( m_pConfigStrings == null ) 167 { 168 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Strings" ); 169 170 String[] pStrings = new String[STRINGS_NUM]; 171 for ( int nInd = 0; nInd < STRINGS_NUM; nInd++ ) 172 if ( m_pEntryNames[nInd] != null ) 173 pStrings[nInd] = AnyConverter.toString( xNameAccess.getByName( m_pEntryNames[nInd] ) ); 174 else 175 pStrings[nInd] = ""; 176 177 m_pConfigStrings = pStrings; 178 } 179 180 return m_pConfigStrings[nID]; 181 } 182 RegisterConnection( HttpURLConnection aConn )183 synchronized protected static void RegisterConnection( HttpURLConnection aConn ) 184 throws WikiCancelException 185 { 186 if ( !m_bAllowConnection ) 187 throw new WikiCancelException(); 188 189 m_aActiveConnections.add( aConn ); 190 } 191 UnregisterConnection( HttpURLConnection aConn )192 synchronized protected static void UnregisterConnection( HttpURLConnection aConn ) 193 { 194 m_aActiveConnections.remove( aConn ); 195 } 196 AllowConnection( boolean bAllow )197 synchronized protected static void AllowConnection( boolean bAllow ) 198 { 199 m_bAllowConnection = bAllow; 200 if ( !bAllow ) 201 { 202 for ( Iterator<HttpURLConnection> aIter = m_aActiveConnections.iterator(); aIter.hasNext(); ) 203 aIter.next().disconnect(); 204 m_aActiveConnections.clear(); 205 } 206 } 207 IsConnectionAllowed()208 synchronized protected static boolean IsConnectionAllowed() 209 { 210 return m_bAllowConnection; 211 } 212 GetShowInBrowserByDefault( XComponentContext xContext )213 synchronized protected static boolean GetShowInBrowserByDefault( XComponentContext xContext ) 214 { 215 if ( m_bShowInBrowser == null ) 216 { 217 try 218 { 219 XNameAccess xAccess = Helper.GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" ); 220 m_bShowInBrowser = new Boolean( AnyConverter.toBoolean( xAccess.getByName( "PreselectShowBrowser" ) ) ); 221 } 222 catch( com.sun.star.uno.Exception e ) 223 { 224 e.printStackTrace(); 225 } 226 } 227 228 return m_bShowInBrowser.booleanValue(); 229 } 230 SetShowInBrowserByDefault( XComponentContext xContext, boolean bValue )231 synchronized protected static void SetShowInBrowserByDefault( XComponentContext xContext, boolean bValue ) 232 { 233 try 234 { 235 m_bShowInBrowser = new Boolean( bValue ); 236 237 XPropertySet xProps = Helper.GetConfigProps( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" ); 238 xProps.setPropertyValue( "PreselectShowBrowser", new Boolean( bValue ) ); 239 XChangesBatch xBatch = UnoRuntime.queryInterface( XChangesBatch.class, xProps ); 240 if ( xBatch != null ) 241 xBatch.commitChanges(); 242 } 243 catch( Exception e ) 244 { 245 e.printStackTrace(); 246 } 247 } 248 GetPasswordContainer( XComponentContext xContext )249 synchronized protected static XPasswordContainer GetPasswordContainer( XComponentContext xContext ) 250 throws com.sun.star.uno.Exception 251 { 252 if ( m_xPasswordContainer == null && xContext != null ) 253 { 254 XMultiComponentFactory xFactory = xContext.getServiceManager(); 255 if ( xFactory != null ) 256 m_xPasswordContainer = UnoRuntime.queryInterface( 257 XPasswordContainer.class, 258 xFactory.createInstanceWithContext( "com.sun.star.task.PasswordContainer", xContext ) ); 259 } 260 261 if ( m_xPasswordContainer == null ) 262 throw new com.sun.star.uno.RuntimeException(); 263 264 return m_xPasswordContainer; 265 } 266 GetInteractionHandler( XComponentContext xContext )267 synchronized protected static XInteractionHandler GetInteractionHandler( XComponentContext xContext ) 268 throws com.sun.star.uno.Exception 269 { 270 if ( m_xInteractionHandler == null && xContext != null ) 271 { 272 XMultiComponentFactory xFactory = xContext.getServiceManager(); 273 if ( xFactory != null ) 274 m_xInteractionHandler = UnoRuntime.queryInterface( 275 XInteractionHandler.class, 276 xFactory.createInstanceWithContext( "com.sun.star.task.InteractionHandler", xContext ) ); 277 } 278 279 if ( m_xInteractionHandler == null ) 280 throw new com.sun.star.uno.RuntimeException(); 281 282 return m_xInteractionHandler; 283 } 284 285 /** Returns the wiki's main URL (the directory holding index.php and api.php), or "" if the page is no MediaWiki page. */ GetMainURL( String sWebPage, URL aPageURL )286 protected static String GetMainURL( String sWebPage, URL aPageURL ) 287 { 288 String sResultURL = ""; 289 try 290 { 291 StringReader aReader = new StringReader( sWebPage ); 292 HTMLEditorKit.Parser aParser = GetHTMLParser(); 293 EditPageParser aCallback = new EditPageParser(); 294 295 aParser.parse( aReader, aCallback, true ); 296 if ( aCallback.m_sMainURL.length() > 0 ) 297 sResultURL = new URL( aPageURL, aCallback.m_sMainURL ).toString(); 298 } 299 catch( Exception e ) 300 { 301 e.printStackTrace(); 302 } 303 304 if ( sResultURL.length() == 0 ) 305 { 306 // usually that should not happen 307 // workaround: try to get index.php from the provided URL 308 String sVURL = aPageURL.toString(); 309 int nIndex = sVURL.indexOf( "index.php" ); 310 if ( nIndex >= 0 ) 311 sResultURL = sVURL.substring( 0, nIndex ); 312 } 313 314 return sResultURL; 315 } 316 GetRedirectURL( String sWebPage, URL aPageURL )317 protected static String GetRedirectURL( String sWebPage, URL aPageURL ) 318 { 319 // scrape the HTML source and find the EditURL 320 // TODO/LATER: Use parser in future 321 322 String sResultURL = ""; 323 int nInd = sWebPage.indexOf( "http-equiv=\"refresh\"" ); 324 if ( nInd != -1 ) 325 { 326 int nContent = sWebPage.indexOf( "content=", nInd ); 327 if ( nContent > 0 ) 328 { 329 int nURL = sWebPage.indexOf( "URL=", nContent ); 330 if ( nURL > 0 ) 331 { 332 int nEndURL = sWebPage.indexOf( "\"", nURL ); 333 if ( nEndURL > 0 ) 334 sResultURL = sWebPage.substring( nURL + 4, nEndURL ); 335 } 336 } 337 338 try 339 { 340 if ( sResultURL.length() > 0 ) 341 sResultURL = new URL( aPageURL, sResultURL ).toString(); 342 } 343 catch ( MalformedURLException ex ) 344 { 345 ex.printStackTrace(); 346 sResultURL = ""; 347 } 348 } 349 350 return sResultURL; 351 } 352 SaveHTMLTemp( XComponentContext xContext, String sArticle )353 protected static XInputStream SaveHTMLTemp( XComponentContext xContext, String sArticle ) 354 { 355 XInputStream xResult = null; 356 357 if ( xContext != null ) 358 { 359 try 360 { 361 Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); 362 XStream xStream = UnoRuntime.queryInterface( XStream.class, oTempFile ); 363 XSeekable xSeekable = UnoRuntime.queryInterface( XSeekable.class, oTempFile ); 364 if ( xStream != null && xSeekable != null ) 365 { 366 XOutputStream xOutputStream = xStream.getOutputStream(); 367 XInputStream xInputStream = xStream.getInputStream(); 368 if ( xOutputStream != null && xInputStream != null ) 369 { 370 String sHTML = sHTMLHeader.concat( sArticle ); 371 sHTML = sHTML.concat( sHTMLFooter ); 372 xOutputStream.writeBytes( sHTML.getBytes( "UTF-8" ) ); 373 // xOutputStream.closeOutput(); 374 xSeekable.seek( 0 ); 375 376 xResult = xInputStream; 377 } 378 } 379 } 380 catch ( Exception ex ) 381 { 382 ex.printStackTrace(); 383 } 384 } 385 386 return xResult; 387 } 388 389 CreateTempFile( XComponentContext xContext )390 protected static String CreateTempFile( XComponentContext xContext ) 391 { 392 String sURL = ""; 393 try 394 { 395 Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); 396 XPropertySet xPropertySet = UnoRuntime.queryInterface( XPropertySet.class, oTempFile ); 397 xPropertySet.setPropertyValue( "RemoveFile", Boolean.FALSE ); 398 sURL = ( String ) xPropertySet.getPropertyValue( "Uri" ); 399 400 XInputStream xInputStream = UnoRuntime.queryInterface( XInputStream.class, oTempFile ); 401 xInputStream.closeInput(); 402 XOutputStream xOutputStream = UnoRuntime.queryInterface( XOutputStream.class, oTempFile ); 403 xOutputStream.closeOutput(); 404 } catch ( com.sun.star.uno.Exception ex ) 405 { 406 ex.printStackTrace(); 407 } 408 return sURL; 409 } 410 EachLine( String sURL )411 protected static String EachLine( String sURL ) 412 { 413 String sText = ""; 414 try 415 { 416 URL aURL = new URL( sURL ); 417 File aFile = new File( aURL.getFile() ); 418 InputStreamReader aInputReader = new InputStreamReader( new FileInputStream( aFile ), "UTF-8" ); 419 BufferedReader aBufReader = new BufferedReader( aInputReader ); 420 421 StringBuilder aBuf = new StringBuilder(); 422 String sEachLine = aBufReader.readLine(); 423 424 while( sEachLine != null ) 425 { 426 aBuf.append( sEachLine ); 427 aBuf.append( "\n" ); 428 429 sEachLine = aBufReader.readLine(); 430 } 431 sText = aBuf.toString(); 432 } catch ( Exception e ) 433 { 434 e.printStackTrace(); 435 } 436 return sText; 437 } 438 GetDocTitle( XModel xDoc )439 protected static String GetDocTitle( XModel xDoc ) 440 { 441 String sTitle = ""; 442 XDocumentInfoSupplier xDocInfoSup = UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); 443 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); 444 try 445 { 446 sTitle = ( String ) xPropSet.getPropertyValue( "Title" ); 447 } catch ( Exception ex ) 448 { 449 ex.printStackTrace(); 450 } 451 return sTitle; 452 } 453 SetDocTitle( XModel xDoc, String sTitle )454 protected static void SetDocTitle( XModel xDoc, String sTitle ) 455 { 456 XDocumentInfoSupplier xDocInfoSup = UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); 457 if ( xDocInfoSup != null ) 458 { 459 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); 460 if ( xPropSet != null ) 461 { 462 try 463 { 464 xPropSet.setPropertyValue( "Title", sTitle ); 465 } 466 catch ( Exception ex ) 467 { 468 ex.printStackTrace(); 469 } 470 } 471 } 472 } 473 GetDocServiceName( XComponentContext xContext, XModel xModel )474 protected static String GetDocServiceName( XComponentContext xContext, XModel xModel ) 475 { 476 String aDocServiceName = ""; 477 if ( xModel != null && xContext != null ) 478 { 479 try 480 { 481 XMultiComponentFactory xFactory = xContext.getServiceManager(); 482 if ( xFactory == null ) 483 throw new com.sun.star.uno.RuntimeException(); 484 485 Object oModuleManager = xFactory.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xContext ); 486 XModuleManager xModuleManager = UnoRuntime.queryInterface( XModuleManager.class, oModuleManager ); 487 if ( xModuleManager != null ) 488 aDocServiceName = xModuleManager.identify( xModel ); 489 } 490 catch( java.lang.Exception e ) 491 { 492 e.printStackTrace(); 493 } 494 } 495 496 return aDocServiceName; 497 } 498 GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName )499 protected static String GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName ) 500 { 501 String aFilterName = ""; 502 if ( xContext != null && aTypeName != null && aTypeName.length() != 0 503 && aDocServiceName != null && aDocServiceName.length() != 0 ) 504 { 505 try 506 { 507 Object oFilterFactory = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.document.FilterFactory", xContext ); 508 XContainerQuery xQuery = UnoRuntime.queryInterface( XContainerQuery.class, oFilterFactory ); 509 if ( xQuery != null ) 510 { 511 NamedValue[] aRequest = new NamedValue[2]; 512 aRequest[0] = new NamedValue( "Type", aTypeName ); 513 aRequest[1] = new NamedValue( "DocumentService", aDocServiceName ); 514 515 XEnumeration xSet = xQuery.createSubSetEnumerationByProperties( aRequest ); 516 if ( xSet != null ) 517 { 518 boolean bAcceptable = false; 519 while ( xSet.hasMoreElements() && !bAcceptable ) 520 { 521 PropertyValue[] pFilterProps = ( PropertyValue[] )AnyConverter.toArray( xSet.nextElement() ); 522 if ( pFilterProps != null ) 523 { 524 int nLen = pFilterProps.length; 525 String aTmpFilter = null; 526 527 for ( int nInd = 0; nInd < nLen; nInd++ ) 528 { 529 if ( pFilterProps[nInd].Name.equals( "Name" ) ) 530 aTmpFilter = AnyConverter.toString( pFilterProps[nInd].Value ); 531 else if ( pFilterProps[nInd].Name.equals( "Flags" ) ) 532 bAcceptable = ( ( AnyConverter.toInt( pFilterProps[nInd].Value ) & 2 ) == 2 ); // must allow export 533 } 534 535 if ( bAcceptable ) 536 aFilterName = aTmpFilter; 537 } 538 } 539 } 540 } 541 } 542 catch( java.lang.Exception e ) 543 { 544 e.printStackTrace(); 545 } 546 } 547 548 return aFilterName; 549 } 550 GetConfigurationProvider( XComponentContext xContext )551 protected static XMultiServiceFactory GetConfigurationProvider( XComponentContext xContext ) 552 throws com.sun.star.uno.Exception 553 { 554 XMultiServiceFactory xConfigurationProvider = null; 555 if ( xContext != null ) 556 { 557 XMultiComponentFactory xFactory = xContext.getServiceManager(); 558 Object oConfigProvider = xFactory.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext ); 559 xConfigurationProvider = UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider ); 560 } 561 562 if ( xConfigurationProvider == null ) 563 throw new com.sun.star.uno.RuntimeException(); 564 565 return xConfigurationProvider; 566 } 567 GetConfig( XComponentContext xContext, String sNodepath, boolean bWriteAccess )568 protected static Object GetConfig( XComponentContext xContext, String sNodepath, boolean bWriteAccess ) 569 throws com.sun.star.uno.Exception 570 { 571 if ( xContext == null || sNodepath == null ) 572 throw new com.sun.star.uno.RuntimeException(); 573 574 PropertyValue aVal = new PropertyValue(); 575 aVal.Name = "nodepath"; 576 aVal.Value = sNodepath; 577 Object[] aArgs = new Object[1]; 578 aArgs[0] = aVal; 579 580 return GetConfigurationProvider( xContext ).createInstanceWithArguments( 581 ( bWriteAccess ? "com.sun.star.configuration.ConfigurationUpdateAccess" 582 : "com.sun.star.configuration.ConfigurationAccess" ), 583 aArgs ); 584 } 585 GetConfigProps( XComponentContext xContext, String sNodepath )586 protected static XPropertySet GetConfigProps( XComponentContext xContext, String sNodepath ) 587 throws com.sun.star.uno.Exception 588 { 589 XPropertySet xProps = UnoRuntime.queryInterface( XPropertySet.class, GetConfig( xContext, sNodepath, true ) ); 590 if ( xProps == null ) 591 throw new com.sun.star.uno.RuntimeException(); 592 593 return xProps; 594 } 595 596 GetConfigNameContainer( XComponentContext xContext, String sNodepath )597 protected static XNameContainer GetConfigNameContainer( XComponentContext xContext, String sNodepath ) 598 throws com.sun.star.uno.Exception 599 { 600 XNameContainer xContainer = UnoRuntime.queryInterface( XNameContainer.class, GetConfig( xContext, sNodepath, true ) ); 601 if ( xContainer == null ) 602 throw new com.sun.star.uno.RuntimeException(); 603 604 return xContainer; 605 } 606 GetConfigNameAccess( XComponentContext xContext, String sNodepath )607 protected static XNameAccess GetConfigNameAccess( XComponentContext xContext, String sNodepath ) 608 throws com.sun.star.uno.Exception 609 { 610 XNameAccess xNameAccess = UnoRuntime.queryInterface( XNameAccess.class, GetConfig( xContext, sNodepath, false ) ); 611 if ( xNameAccess == null ) 612 throw new com.sun.star.uno.RuntimeException(); 613 614 return xNameAccess; 615 } 616 617 /** Returns the proxy configured in the office for aURL, or null to use the system proxy settings. */ GetProxy( XComponentContext xContext, URL aURL )618 protected static Proxy GetProxy( XComponentContext xContext, URL aURL ) 619 { 620 if ( xContext == null ) 621 return null; 622 623 try 624 { 625 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Inet/Settings" ); 626 627 int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) ); 628 if ( nProxyType == 0 ) 629 return Proxy.NO_PROXY; 630 else if ( nProxyType == 2 ) 631 { 632 String aProxyNameProp = "ooInetHTTPProxyName"; 633 String aProxyPortProp = "ooInetHTTPProxyPort"; 634 635 if ( aURL.getProtocol().equals( "https" ) ) 636 { 637 aProxyNameProp = "ooInetHTTPSProxyName"; 638 aProxyPortProp = "ooInetHTTPSProxyPort"; 639 } 640 641 String aProxyName = AnyConverter.toString( xNameAccess.getByName( aProxyNameProp ) ); 642 if ( aProxyName.length() == 0 ) 643 return Proxy.NO_PROXY; 644 645 int nProxyPort = 80; 646 647 Object aPortNo = xNameAccess.getByName( aProxyPortProp ); 648 if ( !AnyConverter.isVoid( aPortNo ) ) 649 nProxyPort = AnyConverter.toInt( aPortNo ); 650 651 if ( nProxyPort == -1 ) 652 nProxyPort = 80; 653 654 // TODO: check whether the URL is in the NoProxy list 655 return new Proxy( Proxy.Type.HTTP, InetSocketAddress.createUnresolved( aProxyName, nProxyPort ) ); 656 } 657 } 658 catch( java.lang.Exception e ) 659 { 660 e.printStackTrace(); 661 } 662 663 return null; 664 } 665 ShowURLInBrowser( XComponentContext xContext, String sURL )666 protected static void ShowURLInBrowser( XComponentContext xContext, String sURL ) 667 { 668 if ( xContext != null && sURL != null && sURL.length() > 0 ) 669 { 670 try 671 { 672 XSystemShellExecute xSystemShell = com.sun.star.system.SystemShellExecute.create( xContext ); 673 if ( xSystemShell != null ) 674 xSystemShell.execute( sURL, "", SystemShellExecuteFlags.DEFAULTS ); 675 } 676 catch( Exception e ) 677 { 678 e.printStackTrace(); 679 } 680 } 681 } 682 683 static private class HTMLParse extends HTMLEditorKit 684 { 685 getParser()686 public HTMLEditorKit.Parser getParser() 687 { 688 return super.getParser(); 689 } 690 } 691 GetHTMLParser()692 static protected HTMLEditorKit.Parser GetHTMLParser() 693 { 694 return new HTMLParse().getParser(); 695 } 696 697 /** Logs in through the MediaWiki Action API; returns the logged-in session, or null if the login was refused. */ Login( URL aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext )698 static protected WikiSession Login( URL aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext ) 699 throws java.io.IOException, WikiCancelException 700 { 701 if ( sWikiUser == null || sWikiUser.length() == 0 || sWikiPass == null || xContext == null ) 702 return null; 703 704 WikiSession aSession = new WikiSession( xContext, aMainURL ); 705 String sToken = aSession.GetToken( "login" ); 706 if ( sToken == null ) 707 return null; 708 709 List<String[]> aParams = new ArrayList<String[]>(); 710 boolean bBotPassword = ( sWikiUser.indexOf( '@' ) >= 0 ); 711 if ( bBotPassword ) 712 { 713 // "user@botname" bot passwords are only accepted by action=login, user passwords by action=clientlogin 714 aParams.add( new String[] { "action", "login" } ); 715 aParams.add( new String[] { "lgname", sWikiUser } ); 716 aParams.add( new String[] { "lgpassword", sWikiPass } ); 717 } 718 else 719 { 720 aParams.add( new String[] { "action", "clientlogin" } ); 721 aParams.add( new String[] { "username", sWikiUser } ); 722 aParams.add( new String[] { "password", sWikiPass } ); 723 aParams.add( new String[] { "loginreturnurl", aMainURL.toString() } ); 724 } 725 726 String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() ); 727 if ( pArgs != null ) 728 for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ ) 729 if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null ) 730 aParams.add( pArgs[nArgInd] ); 731 732 aParams.add( new String[] { bBotPassword ? "lgtoken" : "logintoken", sToken } ); 733 734 Document aResult = aSession.CallAPI( aParams.toArray( new String[aParams.size()][] ) ); 735 boolean bSuccess; 736 if ( bBotPassword ) 737 { 738 Element aLogin = WikiSession.GetElement( aResult, "login" ); 739 bSuccess = ( aLogin != null && "Success".equals( aLogin.getAttribute( "result" ) ) ); 740 } 741 else 742 { 743 Element aLogin = WikiSession.GetElement( aResult, "clientlogin" ); 744 bSuccess = ( aLogin != null && "PASS".equals( aLogin.getAttribute( "status" ) ) ); 745 } 746 747 return bSuccess ? aSession : null; 748 } 749 GetControlFromDialog( XDialog xDialog, String aControlName )750 private static XControl GetControlFromDialog( XDialog xDialog, String aControlName ) 751 { 752 XControl xResult = null; 753 XControlContainer xControlCont = UnoRuntime.queryInterface( XControlContainer.class, xDialog ); 754 755 if ( xControlCont != null ) 756 { 757 Object oControl = xControlCont.getControl( aControlName ); 758 xResult = UnoRuntime.queryInterface( XControl.class, oControl ); 759 } 760 761 return xResult; 762 } 763 GetSubControlPropSet( XDialog xDialog, String aControlName )764 private static XPropertySet GetSubControlPropSet( XDialog xDialog, String aControlName ) 765 { 766 XControl xControl = GetControlFromDialog( xDialog, aControlName ); 767 if ( xControl != null ) 768 return UnoRuntime.queryInterface( XPropertySet.class, xControl.getModel() ); 769 770 return null; 771 } 772 SetControlPropInDialog( XDialog xDialog, String aControlName, String aPropName, Object aPropValue )773 protected static void SetControlPropInDialog( XDialog xDialog, String aControlName, String aPropName, Object aPropValue ) 774 { 775 if ( xDialog != null && aControlName != null && aPropName != null && aPropValue != null ) 776 { 777 try 778 { 779 XPropertySet xPropSet = GetSubControlPropSet( xDialog, aControlName ); 780 if ( xPropSet != null ) 781 xPropSet.setPropertyValue( aPropName, aPropValue ); 782 } 783 catch ( com.sun.star.uno.Exception e ) 784 { 785 e.printStackTrace(); 786 } 787 } 788 } 789 GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName )790 protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName ) 791 { 792 String[] aResult = null; 793 794 try 795 { 796 if ( xContext != null && sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 ) 797 { 798 UrlRecord aRec = GetPasswordContainer( xContext ).findForName( sURL, sUserName, GetInteractionHandler( xContext ) ); 799 if ( aRec != null && aRec.UserList != null && aRec.UserList.length > 0 800 && aRec.UserList[0].UserName.equals( sUserName ) ) 801 aResult = aRec.UserList[0].Passwords; 802 } 803 } 804 catch( Exception e ) 805 { 806 e.printStackTrace(); 807 } 808 809 return aResult; 810 } 811 PasswordStoringIsAllowed( XComponentContext xContext )812 protected static boolean PasswordStoringIsAllowed( XComponentContext xContext ) 813 { 814 boolean bResult = false; 815 try 816 { 817 XMasterPasswordHandling xMasterHdl = UnoRuntime.queryInterface( XMasterPasswordHandling.class, GetPasswordContainer( xContext ) ); 818 if ( xMasterHdl != null ) 819 bResult = xMasterHdl.isPersistentStoringAllowed(); 820 } 821 catch( Exception e ) 822 { 823 e.printStackTrace(); 824 } 825 826 return bResult; 827 } 828 ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery )829 protected static void ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 830 { 831 XWindowPeer xPeer = null; 832 XControl xControl = UnoRuntime.queryInterface( XControl.class, xDialog ); 833 if ( xControl != null ) 834 xPeer = xControl.getPeer(); 835 ShowError( xContext, xPeer, nTitleID, nErrorID, sArg, bQuery ); 836 } 837 ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery )838 protected static boolean ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 839 { 840 boolean bResult = false; 841 842 if ( xContext != null && nErrorID >= 0 && nErrorID < STRINGS_NUM ) 843 { 844 String sError = null; 845 String sTitle = ""; 846 847 try 848 { 849 sError = GetLocalizedString( xContext, nErrorID ); 850 if ( sError != null && sArg != null ) 851 sError = sError.replaceAll( "\\$ARG1", sArg ); 852 853 sTitle = GetLocalizedString( xContext, nTitleID ); 854 } 855 catch( Exception e ) 856 { 857 e.printStackTrace(); 858 } 859 860 if ( sError == null ) 861 sError = "Error: " + nErrorID; 862 863 if ( xParentPeer != null ) 864 { 865 final XComponentContext xCtx = xContext; 866 final XWindowPeer xPeer = xParentPeer; 867 final boolean bAsk = bQuery; 868 final String sBoxTitle = sTitle; 869 final String sBoxText = sError; 870 bResult = MainThreadDialogExecutor.Run( xContext, new Callable<Boolean>() 871 { 872 public Boolean call() throws Exception 873 { 874 XMessageBoxFactory xMBFactory = UnoRuntime.queryInterface( 875 XMessageBoxFactory.class, 876 xCtx.getServiceManager().createInstanceWithContext( "com.sun.star.awt.Toolkit", xCtx ) ); 877 if ( xMBFactory == null ) 878 return Boolean.FALSE; 879 880 XMessageBox xMB; 881 if ( bAsk ) 882 { 883 xMB = xMBFactory.createMessageBox( 884 xPeer, 885 MessageBoxType.QUERYBOX, 886 MessageBoxButtons.BUTTONS_YES_NO | MessageBoxButtons.DEFAULT_BUTTON_NO, 887 sBoxTitle, 888 sBoxText ); 889 } 890 else 891 { 892 xMB = xMBFactory.createMessageBox( 893 xPeer, 894 MessageBoxType.ERRORBOX, 895 MessageBoxButtons.BUTTONS_OK, 896 sBoxTitle, 897 sBoxText ); 898 } 899 if ( xMB == null ) 900 return Boolean.FALSE; 901 902 try 903 { 904 short nRes = xMB.execute(); 905 return Boolean.valueOf( nRes == com.sun.star.awt.MessageBoxResults.OK 906 || nRes == com.sun.star.awt.MessageBoxResults.YES ); 907 } 908 finally 909 { 910 Dispose( xMB ); 911 } 912 } 913 } ); 914 } 915 } 916 917 return bResult; 918 } 919 AllowUnknownCert( XComponentContext xContext, String aURL )920 protected static boolean AllowUnknownCert( XComponentContext xContext, String aURL ) 921 { 922 try 923 { 924 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 925 if ( xNameAccess.hasByName( aURL ) ) 926 { 927 XNameAccess xEntry = UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 928 if ( xEntry != null && xEntry.hasByName( "AllowUnknownCertificate" ) ) 929 return AnyConverter.toBoolean( xEntry.getByName( "AllowUnknownCertificate" ) ); 930 } 931 } 932 catch( Exception e ) 933 { 934 e.printStackTrace(); 935 } 936 937 return false; 938 } 939 GetSpecialArgs( XComponentContext xContext, String aURL )940 private static String[][] GetSpecialArgs( XComponentContext xContext, String aURL ) 941 { 942 try 943 { 944 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 945 if ( xNameAccess.hasByName( aURL ) ) 946 { 947 XNameAccess xEntry = UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 948 if ( xEntry != null ) 949 { 950 XNameAccess xArgs = UnoRuntime.queryInterface( XNameAccess.class, xEntry.getByName( "AdditionalLoginArguments" ) ); 951 if ( xArgs != null ) 952 { 953 String[] pNames = xArgs.getElementNames(); 954 if ( pNames != null && pNames.length > 0 ) 955 { 956 String[][] pResult = new String[pNames.length][2]; 957 for ( int nInd = 0; nInd < pNames.length; nInd++ ) 958 { 959 XNameAccess xArgument = UnoRuntime.queryInterface( XNameAccess.class, xArgs.getByName( pNames[nInd] ) ); 960 if ( xArgument == null ) 961 throw new com.sun.star.uno.RuntimeException(); 962 963 pResult[nInd][0] = pNames[nInd]; 964 pResult[nInd][1] = AnyConverter.toString( xArgument.getByName( "Value" ) ); 965 } 966 967 return pResult; 968 } 969 } 970 } 971 } 972 } 973 catch( Exception e ) 974 { 975 e.printStackTrace(); 976 } 977 978 return null; 979 } 980 AllowThreadUsage( XComponentContext xContext )981 protected static boolean AllowThreadUsage( XComponentContext xContext ) 982 { 983 if ( xContext != null ) 984 { 985 try 986 { 987 XMultiComponentFactory xFactory = xContext.getServiceManager(); 988 if ( xFactory == null ) 989 throw new com.sun.star.uno.RuntimeException(); 990 991 Object oCheckCallback = xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ); 992 return ( oCheckCallback != null ); 993 } 994 catch( Exception e ) 995 { 996 e.printStackTrace(); 997 } 998 } 999 1000 return false; 1001 } 1002 Dispose( Object oObject )1003 public static void Dispose( Object oObject ) 1004 { 1005 if ( oObject != null ) 1006 { 1007 try 1008 { 1009 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, oObject ); 1010 if ( xComp != null ) 1011 xComp.dispose(); 1012 } 1013 catch( Exception e ) 1014 { 1015 e.printStackTrace(); 1016 } 1017 } 1018 } 1019 } 1020