2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package com
.sun
.star
.wiki
;
21 import java
.util
.ArrayList
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
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
;
40 private static Settings m_instance
;
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
>>();
47 private Settings( XComponentContext ctx
)
54 public static synchronized Settings
getSettings( XComponentContext ctx
)
56 if ( m_instance
== null )
57 m_instance
= new Settings( ctx
);
62 public void addWikiCon ( Map
<String
, String
> wikiCon
)
64 m_WikiConnections
.add( wikiCon
);
68 private String
getWikiConUrlByNumber( int num
)
71 if ( num
>=0 && num
< m_WikiConnections
.size() )
73 Map
<String
,String
> ht
= m_WikiConnections
.get( num
);
74 url
= ht
.get( "Url" );
80 public void addWikiDoc ( Map
<String
, Object
> aWikiDoc
)
82 String sURL
= ( String
) aWikiDoc
.get( "CompleteUrl" );
83 Map
<String
,Object
> aEntry
= getDocByCompleteUrl( sURL
);
87 // add doc to the end, even if it has been added before
88 m_aWikiDocs
.remove( aEntry
);
90 else if ( m_aWikiDocs
.size() > 10 )
92 // if the number of elements has reached maximum the oldest element should be removed
93 m_aWikiDocs
.remove( 0 );
96 m_aWikiDocs
.add( aWikiDoc
);
100 public Object
[] getWikiDocList( int serverid
)
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
++ )
107 Map
<String
,Object
> ht
= m_aWikiDocs
.get( i
);
108 String docurl
= ( String
) ht
.get( "Url" );
109 if ( docurl
.equals( wikiserverurl
) )
111 theDocs
.add( (String
) ht
.get( "Doc" ) );
114 return theDocs
.toArray( docs
);
117 public String
[] getWikiURLs()
119 String
[] WikiList
= new String
[m_WikiConnections
.size()];
120 for ( int i
=0; i
<m_WikiConnections
.size(); i
++ )
122 Map
<String
,String
> ht
= m_WikiConnections
.get( i
);
123 WikiList
[i
] = ht
.get( "Url" );
129 public Map
<String
, String
> getSettingByUrl( String sUrl
)
131 Map
<String
, String
> ht
= null;
132 for( int i
=0;i
<m_WikiConnections
.size();i
++ )
134 Map
<String
, String
> h1
= m_WikiConnections
.get( i
);
135 String u1
= h1
.get( "Url" );
136 if ( u1
.equals( sUrl
) )
141 String sUserName
= ht
.get( "Username" );
142 String aPassword
= ht
.get( "Password" );
143 if ( sUserName
!= null && sUserName
.length() > 0 && ( aPassword
== null || aPassword
.length() == 0 ) )
145 String
[] pPasswords
= Helper
.GetPasswordsForURLAndUser( m_xContext
, sUrl
, sUserName
);
146 if ( pPasswords
!= null && pPasswords
.length
> 0 )
147 ht
.put( "Password", pPasswords
[0] );
161 private Map
<String
,Object
> getDocByCompleteUrl( String curl
)
163 Map
<String
,Object
> ht
= null;
164 for( int i
=0;i
<m_aWikiDocs
.size();i
++ )
166 Map
<String
,Object
> h1
= m_aWikiDocs
.get( i
);
167 String u1
= ( String
) h1
.get( "CompleteUrl" );
168 if ( u1
.equals( curl
) )
177 public void removeSettingByUrl( String sUrl
)
179 for( int i
=0;i
<m_WikiConnections
.size();i
++ )
181 Map
<String
,String
> h1
= m_WikiConnections
.get( i
);
182 String u1
= h1
.get( "Url" );
183 if ( u1
.equals( sUrl
) )
185 m_WikiConnections
.remove( i
);
191 public void storeConfiguration()
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
)
201 xContainer
.removeByName(pName
);
203 // store all connections
204 XSingleServiceFactory xConnectionFactory
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, xContainer
);
205 for (Map
<String
, String
> ht
: m_WikiConnections
)
207 Object oNewConnection
= xConnectionFactory
.createInstance();
208 XNameReplace xNewConn
= UnoRuntime
.queryInterface(XNameReplace
.class, oNewConnection
);
209 if (xNewConn
!= null)
211 xNewConn
.replaceByName("UserName", ht
.get("Username"));
213 xContainer
.insertByName(ht
.get("Url"), xNewConn
);
216 XChangesBatch xBatch
= UnoRuntime
.queryInterface(XChangesBatch
.class, xContainer
);
217 xBatch
.commitChanges();
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
)
226 xContainer
.removeByName(pName
);
229 XSingleServiceFactory xDocListFactory
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, xContainer
);
231 for (Map
<String
, Object
> ht
: m_aWikiDocs
)
233 Object oNewDoc
= xDocListFactory
.createInstance();
234 XNameReplace xNewDoc
= UnoRuntime
.queryInterface(XNameReplace
.class, oNewDoc
);
235 for (Map
.Entry
<String
, Object
> entry
: ht
.entrySet())
237 xNewDoc
.replaceByName(entry
.getKey(), entry
.getValue());
239 xContainer
.insertByName("d" + i
++, xNewDoc
);
242 XChangesBatch xBatch
= UnoRuntime
.queryInterface(XChangesBatch
.class, xContainer
);
243 xBatch
.commitChanges();
247 catch ( Exception ex
)
249 ex
.printStackTrace();
253 public void loadConfiguration()
255 m_WikiConnections
.clear();
258 // get configuration service
259 // connect to configmanager
260 XNameAccess xAccess
= Helper
.GetConfigNameAccess( m_xContext
, "org.openoffice.Office.Custom.WikiExtension" );
262 if ( xAccess
!= null )
264 Object oList
= xAccess
.getByName( "ConnectionList" );
265 XNameAccess xConnectionList
= UnoRuntime
.queryInterface( XNameAccess
.class, oList
);
266 String
[] allCons
= xConnectionList
.getElementNames();
267 for (String aConnection
: allCons
)
269 Map
<String
, String
> ht
= new HashMap
<String
, String
>();
270 ht
.put("Url", aConnection
);
271 ht
.put( "Username", "" );
272 ht
.put( "Password", "" );
275 XPropertySet xProps
= UnoRuntime
.queryInterface(XPropertySet
.class, xConnectionList
.getByName(aConnection
));
276 if ( xProps
!= null )
278 String aUsername
= AnyConverter
.toString( xProps
.getPropertyValue( "UserName" ) );
279 if ( aUsername
!= null && aUsername
.length() > 0 )
280 ht
.put( "Username", aUsername
);
290 Object oDocs
= xAccess
.getByName( "RecentDocs" );
291 XNameAccess xRecentDocs
= UnoRuntime
.queryInterface( XNameAccess
.class, oDocs
);
292 String
[] allDocs
= xRecentDocs
.getElementNames();
293 for (String aDocument
: allDocs
)
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" ) );
305 catch ( Exception ex
)
307 ex
.printStackTrace();