Debian Codegraph @

swext/mediawiki/src/com/sun/star/wiki/Settings.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package com.sun.star.wiki;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
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
36 public class Settings
37 {
38
39 /* Singleton */
40 private static Settings m_instance;
41
42
43 private final XComponentContext m_xContext;
44 private final List<Map<String, String>> m_WikiConnections = new ArrayList<Map<String, String>>();
45 private final List<Map<String, Object>> m_aWikiDocs = new ArrayList<Map<String, Object>>();
46
47 private Settings( XComponentContext ctx )
48 {
49 m_xContext=ctx;
50 loadConfiguration();
51 }
52
53
54 public static synchronized Settings getSettings( XComponentContext ctx )
55 {
56 if ( m_instance == null )
57 m_instance = new Settings( ctx );
58 return m_instance;
59 }
60
61
62 public void addWikiCon ( Map<String, String> wikiCon )
63 {
64 m_WikiConnections.add( wikiCon );
65 }
66
67
68 private String getWikiConUrlByNumber( int num )
69 {
70 String url = "";
71 if ( num >=0 && num < m_WikiConnections.size() )
72 {
73 Map<String,String> ht = m_WikiConnections.get( num );
74 url = ht.get( "Url" );
75 }
76 return url;
77 }
78
79
80 public void addWikiDoc ( Map<String, Object> aWikiDoc )
81 {
82 String sURL = ( String ) aWikiDoc.get( "CompleteUrl" );
83 Map<String,Object> aEntry = getDocByCompleteUrl( sURL );
84
85 if ( aEntry != null )
86 {
87 // add doc to the end, even if it has been added before
88 m_aWikiDocs.remove( aEntry );
89 }
90 else if ( m_aWikiDocs.size() > 10 )
91 {
92 // if the number of elements has reached maximum the oldest element should be removed
93 m_aWikiDocs.remove( 0 );
94 }
95
96 m_aWikiDocs.add( aWikiDoc );
97 }
98
99
100 public Object[] getWikiDocList( int serverid )
101 {
102 String wikiserverurl = getWikiConUrlByNumber( serverid );
103 List<String> theDocs = new ArrayList<String>();
104 String [] docs = new String[0];
105 for ( int i=0; i<m_aWikiDocs.size(); i++ )
106 {
107 Map<String,Object> ht = m_aWikiDocs.get( i );
108 String docurl = ( String ) ht.get( "Url" );
109 if ( docurl.equals( wikiserverurl ) )
110 {
111 theDocs.add( (String ) ht.get( "Doc" ) );
112 }
113 }
114 return theDocs.toArray( docs );
115 }
116
117 public String[] getWikiURLs()
118 {
119 String [] WikiList = new String [m_WikiConnections.size()];
120 for ( int i=0; i<m_WikiConnections.size(); i++ )
121 {
122 Map<String,String> ht = m_WikiConnections.get( i );
123 WikiList[i] = ht.get( "Url" );
124 }
125 return WikiList;
126 }
127
128
129 public Map<String, String> getSettingByUrl( String sUrl )
130 {
131 Map<String, String> ht = null;
132 for( int i=0;i<m_WikiConnections.size();i++ )
133 {
134 Map<String, String> h1 = m_WikiConnections.get( i );
135 String u1 = h1.get( "Url" );
136 if ( u1.equals( sUrl ) )
137 {
138 ht = h1;
139 try
140 {
141 String sUserName = ht.get( "Username" );
142 String aPassword = ht.get( "Password" );
143 if ( sUserName != null && sUserName.length() > 0 && ( aPassword == null || aPassword.length() == 0 ) )
144 {
145 String[] pPasswords = Helper.GetPasswordsForURLAndUser( m_xContext, sUrl, sUserName );
146 if ( pPasswords != null && pPasswords.length > 0 )
147 ht.put( "Password", pPasswords[0] );
148 }
149 }
150 catch( Exception e )
151 {
152 e.printStackTrace();
153 }
154
155 break;
156 }
157 }
158 return ht;
159 }
160
161 private Map<String,Object> getDocByCompleteUrl( String curl )
162 {
163 Map<String,Object> ht = null;
164 for( int i=0;i<m_aWikiDocs.size();i++ )
165 {
166 Map<String,Object> h1 = m_aWikiDocs.get( i );
167 String u1 = ( String ) h1.get( "CompleteUrl" );
168 if ( u1.equals( curl ) )
169 {
170 ht = h1;
171 }
172 }
173 return ht;
174 }
175
176
177 public void removeSettingByUrl( String sUrl )
178 {
179 for( int i=0;i<m_WikiConnections.size();i++ )
180 {
181 Map<String,String> h1 = m_WikiConnections.get( i );
182 String u1 = h1.get( "Url" );
183 if ( u1.equals( sUrl ) )
184 {
185 m_WikiConnections.remove( i );
186 }
187 }
188 }
189
190
191 public void storeConfiguration()
192 {
193 try
194 {
195 {
196 // remove stored connection information
197 XNameContainer xContainer = Helper.GetConfigNameContainer(m_xContext, "org.openoffice.Office.Custom.WikiExtension/ConnectionList");
198 String[] pNames = xContainer.getElementNames();
199 for (String pName : pNames)
200 {
201 xContainer.removeByName(pName);
202 }
203 // store all connections
204 XSingleServiceFactory xConnectionFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, xContainer);
205 for (Map<String, String> ht : m_WikiConnections)
206 {
207 Object oNewConnection = xConnectionFactory.createInstance();
208 XNameReplace xNewConn = UnoRuntime.queryInterface(XNameReplace.class, oNewConnection);
209 if (xNewConn != null)
210 {
211 xNewConn.replaceByName("UserName", ht.get("Username"));
212 }
213 xContainer.insertByName(ht.get("Url"), xNewConn);
214 }
215 // commit changes
216 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xContainer);
217 xBatch.commitChanges();
218 }
219
220 {
221 // remove stored connection information
222 XNameContainer xContainer = Helper.GetConfigNameContainer(m_xContext, "org.openoffice.Office.Custom.WikiExtension/RecentDocs");
223 String[] pNames = xContainer.getElementNames();
224 for (String pName : pNames)
225 {
226 xContainer.removeByName(pName);
227 }
228 // store all Docs
229 XSingleServiceFactory xDocListFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, xContainer);
230 int i = 0;
231 for (Map<String, Object> ht : m_aWikiDocs)
232 {
233 Object oNewDoc = xDocListFactory.createInstance();
234 XNameReplace xNewDoc = UnoRuntime.queryInterface(XNameReplace.class, oNewDoc);
235 for (Map.Entry<String, Object> entry : ht.entrySet())
236 {
237 xNewDoc.replaceByName(entry.getKey(), entry.getValue());
238 }
239 xContainer.insertByName("d" + i++, xNewDoc);
240 }
241 // commit changes
242 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xContainer);
243 xBatch.commitChanges();
244 }
245
246 }
247 catch ( Exception ex )
248 {
249 ex.printStackTrace();
250 }
251 }
252
253 public void loadConfiguration()
254 {
255 m_WikiConnections.clear();
256 try
257 {
258 // get configuration service
259 // connect to configmanager
260 XNameAccess xAccess = Helper.GetConfigNameAccess( m_xContext, "org.openoffice.Office.Custom.WikiExtension" );
261
262 if ( xAccess != null )
263 {
264 Object oList = xAccess.getByName( "ConnectionList" );
265 XNameAccess xConnectionList = UnoRuntime.queryInterface( XNameAccess.class, oList );
266 String [] allCons = xConnectionList.getElementNames();
267 for (String aConnection : allCons)
268 {
269 Map<String, String> ht = new HashMap<String, String>();
270 ht.put("Url", aConnection);
271 ht.put( "Username", "" );
272 ht.put( "Password", "" );
273 try
274 {
275 XPropertySet xProps = UnoRuntime.queryInterface(XPropertySet.class, xConnectionList.getByName(aConnection));
276 if ( xProps != null )
277 {
278 String aUsername = AnyConverter.toString( xProps.getPropertyValue( "UserName" ) );
279 if ( aUsername != null && aUsername.length() > 0 )
280 ht.put( "Username", aUsername );
281 }
282 }
283 catch( Exception e )
284 {
285 e.printStackTrace();
286 }
287 addWikiCon( ht );
288 }
289
290 Object oDocs = xAccess.getByName( "RecentDocs" );
291 XNameAccess xRecentDocs = UnoRuntime.queryInterface( XNameAccess.class, oDocs );
292 String [] allDocs = xRecentDocs.getElementNames();
293 for (String aDocument : allDocs)
294 {
295 Object oDoc = xRecentDocs.getByName(aDocument);
296 XNameAccess xDoc = UnoRuntime.queryInterface( XNameAccess.class, oDoc );
297 Map<String, Object> ht = new HashMap<String, Object>();
298 ht.put( "Url", xDoc.getByName( "Url" ) );
299 ht.put( "CompleteUrl", xDoc.getByName( "CompleteUrl" ) );
300 ht.put( "Doc", xDoc.getByName( "Doc" ) );
301 addWikiDoc( ht );
302 }
303 }
304 }
305 catch ( Exception ex )
306 {
307 ex.printStackTrace();
308 }
309 }
310 }