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