1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package com.sun.star.wiki; 29 30 import com.sun.star.beans.XPropertySet; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.container.XNameContainer; 33 import com.sun.star.container.XNameReplace; 34 import com.sun.star.lang.XSingleServiceFactory; 35 import com.sun.star.uno.AnyConverter; 36 import com.sun.star.uno.UnoRuntime; 37 import com.sun.star.uno.XComponentContext; 38 import com.sun.star.util.XChangesBatch; 39 import java.util.Enumeration; 40 import java.util.Hashtable; 41 import java.util.Vector; 42 43 public class Settings 44 { 45 46 private XComponentContext m_xContext; 47 private int lastUsedWikiServer = 0; 48 49 50 /* Singelton */ 51 private static Settings m_instance; 52 53 54 private Vector m_WikiConnections = new Vector(); 55 private Vector m_aWikiDocs = new Vector(); 56 57 private Settings( XComponentContext ctx ) 58 { 59 m_xContext=ctx; 60 loadConfiguration(); 61 } 62 63 64 public static synchronized Settings getSettings( XComponentContext ctx ) 65 { 66 if ( m_instance == null ) 67 m_instance = new Settings( ctx ); 68 // m_instance.loadSettings(); 69 return m_instance; 70 } 71 72 73 public void addWikiCon ( Hashtable wikiCon ) 74 { 75 m_WikiConnections.add( wikiCon ); 76 } 77 78 79 public Vector getWikiCons() 80 { 81 return m_WikiConnections; 82 } 83 84 public String getWikiConUrlByNumber( int num ) 85 { 86 String url = ""; 87 if ( num >=0 && num < m_WikiConnections.size() ) 88 { 89 Hashtable ht = ( Hashtable ) m_WikiConnections.get( num ); 90 url = ( String ) ht.get( "Url" ); 91 } 92 return url; 93 } 94 95 96 public void addWikiDoc ( Hashtable aWikiDoc ) 97 { 98 String sURL = ( String ) aWikiDoc.get( "CompleteUrl" ); 99 Hashtable aEntry = getDocByCompleteUrl( sURL ); 100 101 if ( aEntry != null ) 102 { 103 // add doc to the end, even if it has been added before 104 m_aWikiDocs.remove( aEntry ); 105 } 106 else if ( m_aWikiDocs.size() > 10 ) 107 { 108 // if the number of elements has reached maximum the oldest element should be removed 109 m_aWikiDocs.remove( 0 ); 110 } 111 112 m_aWikiDocs.add( aWikiDoc ); 113 } 114 115 116 public Vector getWikiDocs() 117 { 118 return m_aWikiDocs; 119 } 120 121 public Object[] getWikiDocList( int serverid, int num ) 122 { 123 String wikiserverurl = getWikiConUrlByNumber( serverid ); 124 Vector theDocs = new Vector(); 125 String [] docs = new String[0]; 126 for ( int i=0; i<m_aWikiDocs.size(); i++ ) 127 { 128 Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); 129 String docurl = ( String ) ht.get( "Url" ); 130 if ( docurl.equals( wikiserverurl ) ) 131 { 132 theDocs.add( (String ) ht.get( "Doc" ) ); 133 } 134 } 135 return theDocs.toArray( docs ); 136 } 137 138 public int getLastUsedWikiServer() 139 { 140 return lastUsedWikiServer; 141 } 142 143 public void setLastUsedWikiServer( int l ) 144 { 145 lastUsedWikiServer = l; 146 } 147 148 public String[] getWikiURLs() 149 { 150 String [] WikiList = new String [m_WikiConnections.size()]; 151 for ( int i=0; i<m_WikiConnections.size(); i++ ) 152 { 153 Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); 154 WikiList[i] = ( String ) ht.get( "Url" ); 155 } 156 return WikiList; 157 } 158 159 160 public Hashtable getSettingByUrl( String sUrl ) 161 { 162 Hashtable ht = null; 163 for( int i=0;i<m_WikiConnections.size();i++ ) 164 { 165 Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); 166 String u1 = ( String ) h1.get( "Url" ); 167 if ( u1.equals( sUrl ) ) 168 { 169 ht = h1; 170 try 171 { 172 String sUserName = (String)ht.get( "Username" ); 173 String aPassword = (String)ht.get( "Password" ); 174 if ( sUserName != null && sUserName.length() > 0 && ( aPassword == null || aPassword.length() == 0 ) ) 175 { 176 String[] pPasswords = Helper.GetPasswordsForURLAndUser( m_xContext, sUrl, sUserName ); 177 if ( pPasswords != null && pPasswords.length > 0 ) 178 ht.put( "Password", pPasswords[0] ); 179 } 180 } 181 catch( Exception e ) 182 { 183 e.printStackTrace(); 184 } 185 186 break; 187 } 188 } 189 return ht; 190 } 191 192 public Hashtable getDocByCompleteUrl( String curl ) 193 { 194 Hashtable ht = null; 195 for( int i=0;i<m_aWikiDocs.size();i++ ) 196 { 197 Hashtable h1 = ( Hashtable ) m_aWikiDocs.get( i ); 198 String u1 = ( String ) h1.get( "CompleteUrl" ); 199 if ( u1.equals( curl ) ) 200 { 201 ht = h1; 202 } 203 } 204 return ht; 205 } 206 207 208 public void removeSettingByUrl( String sUrl ) 209 { 210 Hashtable ht = null; 211 for( int i=0;i<m_WikiConnections.size();i++ ) 212 { 213 Hashtable h1 = ( Hashtable ) m_WikiConnections.get( i ); 214 String u1 = ( String ) h1.get( "Url" ); 215 if ( u1.equals( sUrl ) ) 216 { 217 m_WikiConnections.remove( i ); 218 } 219 } 220 } 221 222 223 public void storeConfiguration() 224 { 225 try 226 { 227 // remove stored connection information 228 XNameContainer xContainer = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/ConnectionList" ); 229 String[] pNames = xContainer.getElementNames(); 230 for( int i=0; i<pNames.length; i++ ) 231 { 232 xContainer.removeByName( pNames[i] ); 233 } 234 235 // store all connections 236 XSingleServiceFactory xConnectionFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer ); 237 for ( int i=0; i< m_WikiConnections.size(); i++ ) 238 { 239 Object oNewConnection = xConnectionFactory.createInstance(); 240 Hashtable ht = ( Hashtable ) m_WikiConnections.get( i ); 241 XNameReplace xNewConn = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewConnection ); 242 243 if ( xNewConn != null ) 244 xNewConn.replaceByName( "UserName", ht.get( "Username" ) ); 245 246 xContainer.insertByName( (String)ht.get( "Url" ), xNewConn ); 247 } 248 // commit changes 249 XChangesBatch xBatch = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer ); 250 xBatch.commitChanges(); 251 252 // remove stored connection information 253 XNameContainer xContainer2 = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/RecentDocs" ); 254 String[] pNames2 = xContainer2.getElementNames(); 255 for( int i=0; i<pNames2.length; i++ ) 256 { 257 xContainer2.removeByName( pNames2[i] ); 258 } 259 // store all Docs 260 XSingleServiceFactory xDocListFactory = ( XSingleServiceFactory ) UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer2 ); 261 for ( int i=0; i< m_aWikiDocs.size(); i++ ) 262 { 263 Hashtable ht = ( Hashtable ) m_aWikiDocs.get( i ); 264 265 Object oNewDoc = xDocListFactory.createInstance(); 266 XNameReplace xNewDoc = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewDoc ); 267 268 Enumeration e = ht.keys(); 269 while ( e.hasMoreElements() ) 270 { 271 String key = ( String ) e.nextElement(); 272 xNewDoc.replaceByName( key, ht.get( key ) ); 273 } 274 275 xContainer2.insertByName( "d" + i, xNewDoc ); 276 } 277 // commit changes 278 XChangesBatch xBatch2 = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer2 ); 279 xBatch2.commitChanges(); 280 281 } 282 catch ( Exception ex ) 283 { 284 ex.printStackTrace(); 285 } 286 } 287 288 public void loadConfiguration() 289 { 290 m_WikiConnections.clear(); 291 try 292 { 293 // get configuration service 294 // connect to configmanager 295 XNameAccess xAccess = Helper.GetConfigNameAccess( m_xContext, "org.openoffice.Office.Custom.WikiExtension" ); 296 297 if ( xAccess != null ) 298 { 299 Object oList = xAccess.getByName( "ConnectionList" ); 300 XNameAccess xConnectionList = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oList ); 301 String [] allCons = xConnectionList.getElementNames(); 302 for ( int i=0; i<allCons.length; i++ ) 303 { 304 Hashtable ht = new Hashtable(); 305 ht.put( "Url", allCons[i] ); 306 ht.put( "Username", "" ); 307 ht.put( "Password", "" ); 308 309 try 310 { 311 XPropertySet xProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xConnectionList.getByName( allCons[i] ) ); 312 if ( xProps != null ) 313 { 314 String aUsername = AnyConverter.toString( xProps.getPropertyValue( "UserName" ) ); 315 if ( aUsername != null && aUsername.length() > 0 ) 316 ht.put( "Username", aUsername ); 317 } 318 } 319 catch( Exception e ) 320 { 321 e.printStackTrace(); 322 } 323 324 addWikiCon( ht ); 325 } 326 327 Object oDocs = xAccess.getByName( "RecentDocs" ); 328 XNameAccess xRecentDocs = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDocs ); 329 String [] allDocs = xRecentDocs.getElementNames(); 330 for ( int i=0; i<allDocs.length; i++ ) 331 { 332 Object oDoc = xRecentDocs.getByName( allDocs[i] ); 333 XNameAccess xDoc = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oDoc ); 334 Hashtable ht = new Hashtable(); 335 ht.put( "Url", xDoc.getByName( "Url" ) ); 336 ht.put( "CompleteUrl", xDoc.getByName( "CompleteUrl" ) ); 337 ht.put( "Doc", xDoc.getByName( "Doc" ) ); 338 addWikiDoc( ht ); 339 } 340 } 341 } 342 catch ( Exception ex ) 343 { 344 ex.printStackTrace(); 345 } 346 } 347 } 348