1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail me at eric@jeti.tk |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.util; |
---|
24 | |
---|
25 | import java.io.*; |
---|
26 | import java.net.URL; |
---|
27 | import java.util.*; |
---|
28 | |
---|
29 | import javax.xml.parsers.SAXParser; |
---|
30 | |
---|
31 | import nu.fw.jeti.backend.Start; |
---|
32 | import nu.fw.jeti.backend.XMLDataFile; |
---|
33 | import nu.fw.jeti.jabber.Backend; |
---|
34 | import nu.fw.jeti.jabber.elements.IQPrivate; |
---|
35 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
36 | import nu.fw.jeti.jabber.elements.JetiPrivateExtension; |
---|
37 | import nu.fw.jeti.jabber.elements.Presence; |
---|
38 | |
---|
39 | import org.xml.sax.SAXException; |
---|
40 | |
---|
41 | /** |
---|
42 | * @author E.S. de Boer |
---|
43 | * 2001 |
---|
44 | */ |
---|
45 | |
---|
46 | public class Preferences extends XMLDataFile |
---|
47 | { |
---|
48 | //private static Map map; |
---|
49 | private static Map mapMessages = new HashMap(); |
---|
50 | private static Backend backend; |
---|
51 | private static Map plugable = new HashMap(); |
---|
52 | private static Map preferences = new HashMap(); |
---|
53 | private static Map defaults = new HashMap(); |
---|
54 | |
---|
55 | public Preferences() |
---|
56 | {} |
---|
57 | |
---|
58 | public Preferences(Map p) |
---|
59 | { |
---|
60 | //copy instead of put all? |
---|
61 | preferences.putAll(p); |
---|
62 | } |
---|
63 | |
---|
64 | public Preferences(Backend backend, SAXParser parser) |
---|
65 | { |
---|
66 | Preferences.backend = backend; |
---|
67 | |
---|
68 | InputStream data = null; |
---|
69 | |
---|
70 | try |
---|
71 | { |
---|
72 | if ( Start.applet ) |
---|
73 | { |
---|
74 | URL url = new URL(Start.dataURL + "default.xml"); |
---|
75 | data = url.openStream(); |
---|
76 | } |
---|
77 | addPreferences(parser, data, defaults); |
---|
78 | } |
---|
79 | catch(IOException ex) |
---|
80 | { |
---|
81 | // Do Nothing |
---|
82 | //javax.swing.JOptionPane.showMessageDialog(null, |
---|
83 | // "Comentar esta linha em Preferences.java\n" + "\nERRO : \n" + ex.getMessage() + "\n" + ex + "\n" ); |
---|
84 | } |
---|
85 | |
---|
86 | // Initialize presence messages if none have been specified |
---|
87 | if ( mapMessages.isEmpty()) |
---|
88 | { |
---|
89 | initMessages(mapMessages); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | private void addPreferences(SAXParser parser, InputStream data,Map store) { |
---|
94 | try { |
---|
95 | parser.parse(data,new PreferencesHandler(store)); |
---|
96 | } catch (SAXException ex) { |
---|
97 | ex.printStackTrace(); |
---|
98 | } catch (IOException ex) { |
---|
99 | ex.printStackTrace(); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | private static Map initMessages(Map mapMessages) |
---|
104 | { |
---|
105 | /** |
---|
106 | * @author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
107 | * @brief : Adicionado as traduções para os items do menu ( presença ) |
---|
108 | */ |
---|
109 | |
---|
110 | List tempList = new ArrayList(10); |
---|
111 | tempList.add("DisponÃvel"); |
---|
112 | tempList.add("Livre p/ Chat"); |
---|
113 | mapMessages.put("chat", tempList); |
---|
114 | mapMessages.put("available", tempList); |
---|
115 | tempList = new ArrayList(10); |
---|
116 | tempList.add("Em reunião"); |
---|
117 | tempList.add("Ocupado"); |
---|
118 | tempList.add("Trabalhando"); |
---|
119 | mapMessages.put("dnd", tempList); |
---|
120 | tempList = new ArrayList(10); |
---|
121 | tempList.add("No telefone"); |
---|
122 | tempList.add("Volto Logo"); |
---|
123 | mapMessages.put("away", tempList); |
---|
124 | tempList = new ArrayList(10); |
---|
125 | tempList.add("Férias"); |
---|
126 | tempList.add("Em casa"); |
---|
127 | tempList.add("Ausente"); |
---|
128 | mapMessages.put("xa", tempList); |
---|
129 | return mapMessages; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Adds a deafult preference, which cannot be overriden byt the user |
---|
134 | * and is not saved to the preferences file of the user |
---|
135 | * @param prefixKey prefix and key in prefix.key formant |
---|
136 | * @param value The value of the preference |
---|
137 | */ |
---|
138 | public static void addDefaultPreferences(String prefixKey,String value) |
---|
139 | { |
---|
140 | defaults.put(prefixKey,value); |
---|
141 | } |
---|
142 | |
---|
143 | public static void load(JetiPrivateExtension jetiExtension) |
---|
144 | { |
---|
145 | mapMessages.putAll(jetiExtension.getMessages()); |
---|
146 | //removed save message version check because it gives problems when |
---|
147 | //the server has removed the messages so there is no version |
---|
148 | // if(jetiExtension.getXmlVersion()==null) |
---|
149 | // {//on extension not found set save to true to try to overwrite |
---|
150 | // save = true; |
---|
151 | // } |
---|
152 | // else |
---|
153 | // { |
---|
154 | // try |
---|
155 | // { |
---|
156 | // //save if loaded xml version <= own xmlversion (backwards compatible) |
---|
157 | // save = (Integer.parseInt(jetiExtension.getXmlVersion().substring(1)) <= Integer.parseInt(JetiPrivateExtension.XML_VERSION.substring(1))); |
---|
158 | // } |
---|
159 | // catch (Exception e) |
---|
160 | // { |
---|
161 | // } //exception not saven |
---|
162 | // } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * saves status messages on a server. |
---|
167 | * @param key |
---|
168 | * @param value |
---|
169 | */ |
---|
170 | public static void saveStatusMessages(int key, List value) |
---|
171 | { |
---|
172 | mapMessages.put(convertPresenceKey(key), value); |
---|
173 | //if(save)backend.savePreferences(map,mapMessages); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * saves preferences (saved with put) to server |
---|
178 | */ |
---|
179 | public static void saveToServer() |
---|
180 | { |
---|
181 | backend.send(new InfoQuery("set", new IQPrivate(new JetiPrivateExtension(null, mapMessages)))); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * loads status messages from server |
---|
186 | * @param key |
---|
187 | * @return |
---|
188 | */ |
---|
189 | public static List getStatusMessages(int key) |
---|
190 | { |
---|
191 | return (List) mapMessages.get(convertPresenceKey(key)); |
---|
192 | } |
---|
193 | |
---|
194 | private static String convertPresenceKey(int key) |
---|
195 | { |
---|
196 | String stringKey; |
---|
197 | switch(key) |
---|
198 | { |
---|
199 | case Presence.FREE_FOR_CHAT: stringKey="chat"; break; |
---|
200 | case Presence.AWAY: stringKey="away"; break; |
---|
201 | case Presence.XA: stringKey="xa"; break; |
---|
202 | case Presence.DND: stringKey="dnd"; break; |
---|
203 | default: stringKey="available"; |
---|
204 | } |
---|
205 | return stringKey; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * gets a boolean preference |
---|
210 | * @param prefix a prefix so there are no name clashes |
---|
211 | * @param def the value to be returned in the event that this preference has no value associated with key or the associated value cannot be interpreted as an boolean |
---|
212 | * @param key key whose associated value is to be returned as an boolean. |
---|
213 | * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist. |
---|
214 | */ |
---|
215 | public static boolean getBoolean(String prefix,String key,boolean def) |
---|
216 | { |
---|
217 | String value = get(prefix,key); |
---|
218 | if(value==null) return def; |
---|
219 | return Boolean.valueOf(value).booleanValue(); |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Method saves a boolean preference |
---|
224 | * @param prefix a prefix so there are no name clashes |
---|
225 | * @param key |
---|
226 | * @param value |
---|
227 | */ |
---|
228 | public static void putBoolean(String prefix, String key, boolean value) |
---|
229 | { |
---|
230 | preferences.put(prefix + "." + key,String.valueOf(value)); |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * gets a integer preference |
---|
235 | * @param prefix a prefix so there are no name clashes |
---|
236 | * @param def the value to be returned in the event that this preference has no value associated with key or the associated value cannot be interpreted as an int |
---|
237 | * @param key key whose associated value is to be returned as an boolean. |
---|
238 | * @return int the integer value represented by the string associated with key, or def if the associated value does not exist. |
---|
239 | */ |
---|
240 | public static int getInteger(String prefix,String key,int def) |
---|
241 | { |
---|
242 | String value = get(prefix,key); |
---|
243 | if(value==null) return def; |
---|
244 | try{ |
---|
245 | return Integer.parseInt(value); |
---|
246 | } catch (NumberFormatException e) |
---|
247 | { |
---|
248 | return def; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Method saves a integer preference |
---|
254 | * @param prefix a prefix so there are no name clashes |
---|
255 | * @param key |
---|
256 | * @param value |
---|
257 | */ |
---|
258 | public static void putInteger(String prefix, String key,int value) |
---|
259 | { |
---|
260 | preferences.put(prefix + "." + key,String.valueOf(value)); |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Method put saves a string preference. |
---|
265 | * @param prefix a prefix so there are no name clashes |
---|
266 | * @param key |
---|
267 | * @param value |
---|
268 | */ |
---|
269 | public static void putString(String prefix, String key, String value) |
---|
270 | { |
---|
271 | preferences.put(prefix + "." + key, value); |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Method getPreference gets a string preference. |
---|
276 | * @param prefix a prefix so there are no name clashes |
---|
277 | * @param def the value to be returned in the event that this preference has no value associated with key or the associated value cannot be interpreted as an boolean |
---|
278 | * @param key key whose associated value is to be returned as an boolean. |
---|
279 | * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist. |
---|
280 | */ |
---|
281 | public static String getString(String prefix, String key,String def) |
---|
282 | { |
---|
283 | String value = get(prefix,key); |
---|
284 | if(value==null) return def; |
---|
285 | return value; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Method getPreference gets a preference from file. |
---|
290 | * first tries defaults to get a default value that |
---|
291 | * can't be overriden by the users preferences file |
---|
292 | * @param prefix a prefix so there are no name clashes |
---|
293 | * @param key |
---|
294 | * @param value |
---|
295 | */ |
---|
296 | private static String get(String prefix, String key) |
---|
297 | { |
---|
298 | String defaultValue = (String)defaults.get(prefix + "." + key); |
---|
299 | if(defaultValue!=null)return defaultValue; |
---|
300 | return (String) preferences.get(prefix + "." + key); |
---|
301 | } |
---|
302 | |
---|
303 | public static List getPlugins() |
---|
304 | { |
---|
305 | //put in new class? |
---|
306 | return getPlugable("plugins"); |
---|
307 | } |
---|
308 | |
---|
309 | public static List getTranslatedPlugins() |
---|
310 | { |
---|
311 | //put in new class? |
---|
312 | List temp = new ArrayList(); |
---|
313 | for (Iterator i = getPlugins().iterator(); i.hasNext();) |
---|
314 | { |
---|
315 | Object[] tempArray = new Object[6]; |
---|
316 | //System.arraycopy(i.next(), 0, tempArray, 0, 6); |
---|
317 | Object[] plugins = (Object[])i.next(); |
---|
318 | tempArray[0]= plugins[0]; |
---|
319 | tempArray[1]= plugins[1]; |
---|
320 | tempArray[2]= I18N.gettext((String)plugins[2]); |
---|
321 | tempArray[3]= plugins[3]; |
---|
322 | tempArray[4]= plugins[4]; |
---|
323 | tempArray[5]= plugins[5]; |
---|
324 | temp.add(tempArray); |
---|
325 | } |
---|
326 | return temp; |
---|
327 | } |
---|
328 | |
---|
329 | public static List getPlugable(String name) |
---|
330 | { |
---|
331 | List list = (List) plugable.get(name); |
---|
332 | if (list == null) |
---|
333 | { |
---|
334 | list = new ArrayList(); |
---|
335 | plugable.put(name, list); |
---|
336 | } |
---|
337 | return list; |
---|
338 | } |
---|
339 | |
---|
340 | public static List getPlugableCopy(String name) |
---|
341 | { |
---|
342 | List temp = new ArrayList(); |
---|
343 | for (Iterator i = getPlugable(name).iterator(); i.hasNext();) |
---|
344 | { |
---|
345 | Object[] tempArray = new Object[6]; |
---|
346 | System.arraycopy(i.next(), 0, tempArray, 0, 6); |
---|
347 | temp.add(tempArray); |
---|
348 | } |
---|
349 | return temp; |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * Saves preferences to disk (in preferences.xml) |
---|
354 | */ |
---|
355 | public static void save() |
---|
356 | { |
---|
357 | StringBuffer xml = new StringBuffer(); |
---|
358 | new Preferences().appendToXML(xml); |
---|
359 | xml.insert(0,"<?xml version='1.0'?>"); |
---|
360 | //System.out.println(xml.toString()); |
---|
361 | try |
---|
362 | { |
---|
363 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
---|
364 | new FileOutputStream(Start.path + "preferences.xml"), "UTF8")); |
---|
365 | writer.write(xml.toString()); |
---|
366 | writer.close(); |
---|
367 | } |
---|
368 | catch (IOException ex2) |
---|
369 | { |
---|
370 | ex2.printStackTrace(); |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | public void appendToXML(StringBuffer xml) |
---|
375 | { |
---|
376 | //appendHeader(xml); |
---|
377 | appendOpenTag(xml, "<preferences>"); |
---|
378 | for (Iterator i = preferences.entrySet().iterator(); i.hasNext();) |
---|
379 | { |
---|
380 | Map.Entry entry = (Map.Entry) i.next(); |
---|
381 | String key = (String) entry.getKey(); |
---|
382 | String value = (String) entry.getValue(); |
---|
383 | if(value!=null) |
---|
384 | {//only preference with a value, to reset old ones |
---|
385 | appendOpenTag(xml, "<preference"); |
---|
386 | appendAttribute(xml, "key", key); |
---|
387 | appendAttribute(xml, "value", value); |
---|
388 | appendCloseSymbol(xml); |
---|
389 | } |
---|
390 | } |
---|
391 | appendOpenTag(xml, "<plugins>"); |
---|
392 | for (Iterator i = plugable.entrySet().iterator(); i.hasNext();) |
---|
393 | { |
---|
394 | Map.Entry entry = (Map.Entry) i.next(); |
---|
395 | String type = (String) entry.getKey(); |
---|
396 | for (Iterator j = ((List) entry.getValue()).iterator(); j.hasNext();) |
---|
397 | { |
---|
398 | Object[] temp = (Object[]) j.next(); |
---|
399 | appendOpenTag(xml, "<plugin"); |
---|
400 | appendAttribute(xml, "type", type); |
---|
401 | appendAttribute(xml, "name", temp[0]); |
---|
402 | appendAttribute(xml, "enabled", temp[1]); |
---|
403 | appendAttribute(xml, "transport", temp[3]); |
---|
404 | appendCloseSymbol(xml); |
---|
405 | } |
---|
406 | } |
---|
407 | appendCloseTag(xml, "</plugins>"); |
---|
408 | appendCloseTag(xml, "</preferences>"); |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | /* |
---|
413 | * Overrides for emacs |
---|
414 | * Local variables: |
---|
415 | * tab-width: 4 |
---|
416 | * End: |
---|
417 | */ |
---|