113efc523SAndrew Rist /************************************************************** 2*a98dafebSmseidel * 313efc523SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 413efc523SAndrew Rist * or more contributor license agreements. See the NOTICE file 513efc523SAndrew Rist * distributed with this work for additional information 613efc523SAndrew Rist * regarding copyright ownership. The ASF licenses this file 713efc523SAndrew Rist * to you under the Apache License, Version 2.0 (the 813efc523SAndrew Rist * "License"); you may not use this file except in compliance 913efc523SAndrew Rist * with the License. You may obtain a copy of the License at 10*a98dafebSmseidel * 1113efc523SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a98dafebSmseidel * 1313efc523SAndrew Rist * Unless required by applicable law or agreed to in writing, 1413efc523SAndrew Rist * software distributed under the License is distributed on an 1513efc523SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1613efc523SAndrew Rist * KIND, either express or implied. See the License for the 1713efc523SAndrew Rist * specific language governing permissions and limitations 1813efc523SAndrew Rist * under the License. 19*a98dafebSmseidel * 2013efc523SAndrew Rist *************************************************************/ 2113efc523SAndrew Rist 2213efc523SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.wiki; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 27cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 28cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 29cdf0e10cSrcweir import com.sun.star.container.XNameReplace; 30cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 31cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 34cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 35cdf0e10cSrcweir import java.util.Enumeration; 36cdf0e10cSrcweir import java.util.Hashtable; 37cdf0e10cSrcweir import java.util.Vector; 38cdf0e10cSrcweir 39cdf0e10cSrcweir public class Settings 40cdf0e10cSrcweir { 41*a98dafebSmseidel 42cdf0e10cSrcweir private XComponentContext m_xContext; 43cdf0e10cSrcweir private int lastUsedWikiServer = 0; 44*a98dafebSmseidel 45*a98dafebSmseidel 46cdf0e10cSrcweir /* Singelton */ 47cdf0e10cSrcweir private static Settings m_instance; 48*a98dafebSmseidel 49*a98dafebSmseidel 50cdf0e10cSrcweir private Vector m_WikiConnections = new Vector(); 51cdf0e10cSrcweir private Vector m_aWikiDocs = new Vector(); 52*a98dafebSmseidel Settings( XComponentContext ctx )53cdf0e10cSrcweir private Settings( XComponentContext ctx ) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir m_xContext=ctx; 56cdf0e10cSrcweir loadConfiguration(); 57cdf0e10cSrcweir } 58*a98dafebSmseidel 59*a98dafebSmseidel getSettings( XComponentContext ctx )60cdf0e10cSrcweir public static synchronized Settings getSettings( XComponentContext ctx ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir if ( m_instance == null ) 63cdf0e10cSrcweir m_instance = new Settings( ctx ); 64cdf0e10cSrcweir // m_instance.loadSettings(); 65cdf0e10cSrcweir return m_instance; 66cdf0e10cSrcweir } 67*a98dafebSmseidel 68*a98dafebSmseidel addWikiCon( Hashtable wikiCon )69cdf0e10cSrcweir public void addWikiCon ( Hashtable wikiCon ) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir m_WikiConnections.add( wikiCon ); 72cdf0e10cSrcweir } 73*a98dafebSmseidel 74*a98dafebSmseidel getWikiCons()75cdf0e10cSrcweir public Vector getWikiCons() 76cdf0e10cSrcweir { 77cdf0e10cSrcweir return m_WikiConnections; 78cdf0e10cSrcweir } 79*a98dafebSmseidel getWikiConUrlByNumber( int num )80cdf0e10cSrcweir public String getWikiConUrlByNumber( int num ) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir String url = ""; 83cdf0e10cSrcweir if ( num >=0 && num < m_WikiConnections.size() ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir Hashtable ht = ( Hashtable ) m_WikiConnections.get( num ); 86cdf0e10cSrcweir url = ( String ) ht.get( "Url" ); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir return url; 89cdf0e10cSrcweir } 90*a98dafebSmseidel 91*a98dafebSmseidel addWikiDoc( Hashtable aWikiDoc )92cdf0e10cSrcweir public void addWikiDoc ( Hashtable aWikiDoc ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir String sURL = ( String ) aWikiDoc.get( "CompleteUrl" ); 95cdf0e10cSrcweir Hashtable aEntry = getDocByCompleteUrl( sURL ); 96cdf0e10cSrcweir 97cdf0e10cSrcweir if ( aEntry != null ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir // add doc to the end, even if it has been added before 100cdf0e10cSrcweir m_aWikiDocs.remove( aEntry ); 101cdf0e10cSrcweir } 102cdf0e10cSrcweir else if ( m_aWikiDocs.size() > 10 ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir // if the number of elements has reached maximum the oldest element should be removed 105cdf0e10cSrcweir m_aWikiDocs.remove( 0 ); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir m_aWikiDocs.add( aWikiDoc ); 109cdf0e10cSrcweir } 110*a98dafebSmseidel 111*a98dafebSmseidel getWikiDocs()112cdf0e10cSrcweir public Vector getWikiDocs() 113cdf0e10cSrcweir { 114cdf0e10cSrcweir return m_aWikiDocs; 115cdf0e10cSrcweir } 116*a98dafebSmseidel getWikiDocList( int serverid, int num )117cdf0e10cSrcweir public Object[] getWikiDocList( int serverid, int num ) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir String wikiserverurl = getWikiConUrlByNumber( serverid ); 120cdf0e10cSrcweir Vector theDocs = new Vector(); 121cdf0e10cSrcweir String [] docs = new String[0]; 122cdf0e10cSrcweir for ( int i=0; i<m_aWikiDocs.size(); i++ ) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); 125cdf0e10cSrcweir String docurl = ( String ) ht.get( "Url" ); 126cdf0e10cSrcweir if ( docurl.equals( wikiserverurl ) ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir theDocs.add( (String ) ht.get( "Doc" ) ); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir } 131cdf0e10cSrcweir return theDocs.toArray( docs ); 132cdf0e10cSrcweir } 133*a98dafebSmseidel getLastUsedWikiServer()134cdf0e10cSrcweir public int getLastUsedWikiServer() 135cdf0e10cSrcweir { 136cdf0e10cSrcweir return lastUsedWikiServer; 137cdf0e10cSrcweir } 138*a98dafebSmseidel setLastUsedWikiServer( int l )139cdf0e10cSrcweir public void setLastUsedWikiServer( int l ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir lastUsedWikiServer = l; 142cdf0e10cSrcweir } 143*a98dafebSmseidel getWikiURLs()144cdf0e10cSrcweir public String[] getWikiURLs() 145cdf0e10cSrcweir { 146cdf0e10cSrcweir String [] WikiList = new String [m_WikiConnections.size()]; 147cdf0e10cSrcweir for ( int i=0; i<m_WikiConnections.size(); i++ ) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); 150cdf0e10cSrcweir WikiList[i] = ( String ) ht.get( "Url" ); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir return WikiList; 153cdf0e10cSrcweir } 154*a98dafebSmseidel 155*a98dafebSmseidel getSettingByUrl( String sUrl )156cdf0e10cSrcweir public Hashtable getSettingByUrl( String sUrl ) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir Hashtable ht = null; 159cdf0e10cSrcweir for( int i=0;i<m_WikiConnections.size();i++ ) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); 162cdf0e10cSrcweir String u1 = ( String ) h1.get( "Url" ); 163cdf0e10cSrcweir if ( u1.equals( sUrl ) ) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir ht = h1; 166cdf0e10cSrcweir try 167cdf0e10cSrcweir { 168cdf0e10cSrcweir String sUserName = (String)ht.get( "Username" ); 169cdf0e10cSrcweir String aPassword = (String)ht.get( "Password" ); 170cdf0e10cSrcweir if ( sUserName != null && sUserName.length() > 0 && ( aPassword == null || aPassword.length() == 0 ) ) 171cdf0e10cSrcweir { 172cdf0e10cSrcweir String[] pPasswords = Helper.GetPasswordsForURLAndUser( m_xContext, sUrl, sUserName ); 173cdf0e10cSrcweir if ( pPasswords != null && pPasswords.length > 0 ) 174cdf0e10cSrcweir ht.put( "Password", pPasswords[0] ); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir catch( Exception e ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir e.printStackTrace(); 180cdf0e10cSrcweir } 181cdf0e10cSrcweir 182cdf0e10cSrcweir break; 183cdf0e10cSrcweir } 184cdf0e10cSrcweir } 185cdf0e10cSrcweir return ht; 186cdf0e10cSrcweir } 187*a98dafebSmseidel getDocByCompleteUrl( String curl )188cdf0e10cSrcweir public Hashtable getDocByCompleteUrl( String curl ) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir Hashtable ht = null; 191cdf0e10cSrcweir for( int i=0;i<m_aWikiDocs.size();i++ ) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir Hashtable h1 = ( Hashtable ) m_aWikiDocs.get( i ); 194cdf0e10cSrcweir String u1 = ( String ) h1.get( "CompleteUrl" ); 195cdf0e10cSrcweir if ( u1.equals( curl ) ) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir ht = h1; 198cdf0e10cSrcweir } 199cdf0e10cSrcweir } 200cdf0e10cSrcweir return ht; 201cdf0e10cSrcweir } 202*a98dafebSmseidel 203*a98dafebSmseidel removeSettingByUrl( String sUrl )204cdf0e10cSrcweir public void removeSettingByUrl( String sUrl ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir Hashtable ht = null; 207cdf0e10cSrcweir for( int i=0;i<m_WikiConnections.size();i++ ) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); 210cdf0e10cSrcweir String u1 = ( String ) h1.get( "Url" ); 211cdf0e10cSrcweir if ( u1.equals( sUrl ) ) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir m_WikiConnections.remove( i ); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir } 216cdf0e10cSrcweir } 217*a98dafebSmseidel 218*a98dafebSmseidel storeConfiguration()219cdf0e10cSrcweir public void storeConfiguration() 220cdf0e10cSrcweir { 221cdf0e10cSrcweir try 222cdf0e10cSrcweir { 223cdf0e10cSrcweir // remove stored connection information 224cdf0e10cSrcweir XNameContainer xContainer = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/ConnectionList" ); 225cdf0e10cSrcweir String[] pNames = xContainer.getElementNames(); 226cdf0e10cSrcweir for( int i=0; i<pNames.length; i++ ) 227*a98dafebSmseidel { 228cdf0e10cSrcweir xContainer.removeByName( pNames[i] ); 229cdf0e10cSrcweir } 230*a98dafebSmseidel 231cdf0e10cSrcweir // store all connections 232cdf0e10cSrcweir XSingleServiceFactory xConnectionFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer ); 233cdf0e10cSrcweir for ( int i=0; i< m_WikiConnections.size(); i++ ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir Object oNewConnection = xConnectionFactory.createInstance(); 236cdf0e10cSrcweir Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); 237cdf0e10cSrcweir XNameReplace xNewConn = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewConnection ); 238*a98dafebSmseidel 239cdf0e10cSrcweir if ( xNewConn != null ) 240cdf0e10cSrcweir xNewConn.replaceByName( "UserName", ht.get( "Username" ) ); 241*a98dafebSmseidel 242cdf0e10cSrcweir xContainer.insertByName( (String)ht.get( "Url" ), xNewConn ); 243cdf0e10cSrcweir } 244cdf0e10cSrcweir // commit changes 245cdf0e10cSrcweir XChangesBatch xBatch = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer ); 246cdf0e10cSrcweir xBatch.commitChanges(); 247*a98dafebSmseidel 248cdf0e10cSrcweir // remove stored connection information 249cdf0e10cSrcweir XNameContainer xContainer2 = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/RecentDocs" ); 250cdf0e10cSrcweir String[] pNames2 = xContainer2.getElementNames(); 251cdf0e10cSrcweir for( int i=0; i<pNames2.length; i++ ) 252*a98dafebSmseidel { 253cdf0e10cSrcweir xContainer2.removeByName( pNames2[i] ); 254cdf0e10cSrcweir } 255cdf0e10cSrcweir // store all Docs 256cdf0e10cSrcweir XSingleServiceFactory xDocListFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer2 ); 257cdf0e10cSrcweir for ( int i=0; i< m_aWikiDocs.size(); i++ ) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); 260*a98dafebSmseidel 261cdf0e10cSrcweir Object oNewDoc = xDocListFactory.createInstance(); 262cdf0e10cSrcweir XNameReplace xNewDoc = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewDoc ); 263*a98dafebSmseidel 264cdf0e10cSrcweir Enumeration e = ht.keys(); 265cdf0e10cSrcweir while ( e.hasMoreElements() ) 266cdf0e10cSrcweir { 267cdf0e10cSrcweir String key = ( String ) e.nextElement(); 268cdf0e10cSrcweir xNewDoc.replaceByName( key, ht.get( key ) ); 269cdf0e10cSrcweir } 270*a98dafebSmseidel 271cdf0e10cSrcweir xContainer2.insertByName( "d" + i, xNewDoc ); 272cdf0e10cSrcweir } 273cdf0e10cSrcweir // commit changes 274cdf0e10cSrcweir XChangesBatch xBatch2 = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer2 ); 275cdf0e10cSrcweir xBatch2.commitChanges(); 276*a98dafebSmseidel 277cdf0e10cSrcweir } 278cdf0e10cSrcweir catch ( Exception ex ) 279cdf0e10cSrcweir { 280cdf0e10cSrcweir ex.printStackTrace(); 281*a98dafebSmseidel } 282cdf0e10cSrcweir } 283*a98dafebSmseidel loadConfiguration()284cdf0e10cSrcweir public void loadConfiguration() 285cdf0e10cSrcweir { 286cdf0e10cSrcweir m_WikiConnections.clear(); 287cdf0e10cSrcweir try 288cdf0e10cSrcweir { 289cdf0e10cSrcweir // get configuration service 290cdf0e10cSrcweir // connect to configmanager 291cdf0e10cSrcweir XNameAccess xAccess = Helper.GetConfigNameAccess( m_xContext, "org.openoffice.Office.Custom.WikiExtension" ); 292*a98dafebSmseidel 293cdf0e10cSrcweir if ( xAccess != null ) 294cdf0e10cSrcweir { 295cdf0e10cSrcweir Object oList = xAccess.getByName( "ConnectionList" ); 296cdf0e10cSrcweir XNameAccess xConnectionList = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oList ); 297cdf0e10cSrcweir String [] allCons = xConnectionList.getElementNames(); 298cdf0e10cSrcweir for ( int i=0; i<allCons.length; i++ ) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir Hashtable ht = new Hashtable(); 301cdf0e10cSrcweir ht.put( "Url", allCons[i] ); 302cdf0e10cSrcweir ht.put( "Username", "" ); 303cdf0e10cSrcweir ht.put( "Password", "" ); 304cdf0e10cSrcweir 305cdf0e10cSrcweir try 306cdf0e10cSrcweir { 307cdf0e10cSrcweir XPropertySet xProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xConnectionList.getByName( allCons[i] ) ); 308cdf0e10cSrcweir if ( xProps != null ) 309cdf0e10cSrcweir { 310cdf0e10cSrcweir String aUsername = AnyConverter.toString( xProps.getPropertyValue( "UserName" ) ); 311cdf0e10cSrcweir if ( aUsername != null && aUsername.length() > 0 ) 312cdf0e10cSrcweir ht.put( "Username", aUsername ); 313cdf0e10cSrcweir } 314cdf0e10cSrcweir } 315cdf0e10cSrcweir catch( Exception e ) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir e.printStackTrace(); 318cdf0e10cSrcweir } 319*a98dafebSmseidel 320cdf0e10cSrcweir addWikiCon( ht ); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir 323cdf0e10cSrcweir Object oDocs = xAccess.getByName( "RecentDocs" ); 324cdf0e10cSrcweir XNameAccess xRecentDocs = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDocs ); 325cdf0e10cSrcweir String [] allDocs = xRecentDocs.getElementNames(); 326cdf0e10cSrcweir for ( int i=0; i<allDocs.length; i++ ) 327cdf0e10cSrcweir { 328cdf0e10cSrcweir Object oDoc = xRecentDocs.getByName( allDocs[i] ); 329cdf0e10cSrcweir XNameAccess xDoc = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDoc ); 330cdf0e10cSrcweir Hashtable ht = new Hashtable(); 331cdf0e10cSrcweir ht.put( "Url", xDoc.getByName( "Url" ) ); 332cdf0e10cSrcweir ht.put( "CompleteUrl", xDoc.getByName( "CompleteUrl" ) ); 333cdf0e10cSrcweir ht.put( "Doc", xDoc.getByName( "Doc" ) ); 334cdf0e10cSrcweir addWikiDoc( ht ); 335cdf0e10cSrcweir } 336cdf0e10cSrcweir } 337cdf0e10cSrcweir } 338cdf0e10cSrcweir catch ( Exception ex ) 339cdf0e10cSrcweir { 340cdf0e10cSrcweir ex.printStackTrace(); 341*a98dafebSmseidel } 342cdf0e10cSrcweir } 343cdf0e10cSrcweir } 344*a98dafebSmseidel 345