1*13efc523SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*13efc523SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*13efc523SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*13efc523SAndrew Rist  * distributed with this work for additional information
6*13efc523SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*13efc523SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*13efc523SAndrew Rist  * "License"); you may not use this file except in compliance
9*13efc523SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*13efc523SAndrew Rist  *
11*13efc523SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*13efc523SAndrew Rist  *
13*13efc523SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*13efc523SAndrew Rist  * software distributed under the License is distributed on an
15*13efc523SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*13efc523SAndrew Rist  * KIND, either express or implied.  See the License for the
17*13efc523SAndrew Rist  * specific language governing permissions and limitations
18*13efc523SAndrew Rist  * under the License.
19*13efc523SAndrew Rist  *
20*13efc523SAndrew Rist  *************************************************************/
21*13efc523SAndrew Rist 
22*13efc523SAndrew 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 {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir     private XComponentContext m_xContext;
43cdf0e10cSrcweir     private int lastUsedWikiServer = 0;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /* Singelton */
47cdf0e10cSrcweir     private static Settings m_instance;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     private Vector m_WikiConnections = new Vector();
51cdf0e10cSrcweir     private Vector m_aWikiDocs = new Vector();
52cdf0e10cSrcweir 
Settings( XComponentContext ctx )53cdf0e10cSrcweir     private Settings( XComponentContext ctx )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         m_xContext=ctx;
56cdf0e10cSrcweir         loadConfiguration();
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
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     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
addWikiCon( Hashtable wikiCon )69cdf0e10cSrcweir     public void addWikiCon ( Hashtable wikiCon )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         m_WikiConnections.add( wikiCon );
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
getWikiCons()75cdf0e10cSrcweir     public Vector getWikiCons()
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         return m_WikiConnections;
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
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     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 
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     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 
getWikiDocs()112cdf0e10cSrcweir     public Vector getWikiDocs()
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         return m_aWikiDocs;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
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     }
133cdf0e10cSrcweir 
getLastUsedWikiServer()134cdf0e10cSrcweir     public int getLastUsedWikiServer()
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         return lastUsedWikiServer;
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
setLastUsedWikiServer( int l )139cdf0e10cSrcweir     public void setLastUsedWikiServer( int l )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         lastUsedWikiServer = l;
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
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     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 
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     }
187cdf0e10cSrcweir 
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     }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
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     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
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++ )
227cdf0e10cSrcweir             {
228cdf0e10cSrcweir                 xContainer.removeByName( pNames[i] );
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir 
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 );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir                 if ( xNewConn != null )
240cdf0e10cSrcweir                     xNewConn.replaceByName( "UserName", ht.get( "Username" ) );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir                 xContainer.insertByName( (String)ht.get( "Url" ), xNewConn );
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir             // commit changes
245cdf0e10cSrcweir             XChangesBatch xBatch = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer );
246cdf0e10cSrcweir             xBatch.commitChanges();
247cdf0e10cSrcweir 
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++ )
252cdf0e10cSrcweir             {
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 );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir                 Object oNewDoc = xDocListFactory.createInstance();
262cdf0e10cSrcweir                 XNameReplace xNewDoc = ( XNameReplace ) UnoRuntime.queryInterface( XNameReplace.class, oNewDoc );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir                 Enumeration e = ht.keys();
265cdf0e10cSrcweir                 while ( e.hasMoreElements() )
266cdf0e10cSrcweir                 {
267cdf0e10cSrcweir                     String key = ( String ) e.nextElement();
268cdf0e10cSrcweir                     xNewDoc.replaceByName( key, ht.get( key ) );
269cdf0e10cSrcweir                 }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir                 xContainer2.insertByName( "d" + i, xNewDoc );
272cdf0e10cSrcweir             }
273cdf0e10cSrcweir             // commit changes
274cdf0e10cSrcweir             XChangesBatch xBatch2 = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xContainer2 );
275cdf0e10cSrcweir             xBatch2.commitChanges();
276cdf0e10cSrcweir 
277cdf0e10cSrcweir         }
278cdf0e10cSrcweir         catch ( Exception ex )
279cdf0e10cSrcweir         {
280cdf0e10cSrcweir             ex.printStackTrace();
281cdf0e10cSrcweir         }
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir 
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" );
292cdf0e10cSrcweir 
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                     }
319cdf0e10cSrcweir 
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();
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir     }
343cdf0e10cSrcweir }
344