source: 3thparty/jmessenger/src/nu/fw/jeti/util/Preferences.java @ 3952

Revision 3952, 11.1 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1710 - Adicao do codigo fonte java do componente jmessenger(jabberit_messenger)

  • Property svn:executable set to *
Line 
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
23package nu.fw.jeti.util;
24
25import java.io.*;
26import java.net.URL;
27import java.util.*;
28
29import javax.xml.parsers.SAXParser;
30
31import nu.fw.jeti.backend.Start;
32import nu.fw.jeti.backend.XMLDataFile;
33import nu.fw.jeti.jabber.Backend;
34import nu.fw.jeti.jabber.elements.IQPrivate;
35import nu.fw.jeti.jabber.elements.InfoQuery;
36import nu.fw.jeti.jabber.elements.JetiPrivateExtension;
37import nu.fw.jeti.jabber.elements.Presence;
38
39import org.xml.sax.SAXException;
40
41/**
42 * @author E.S. de Boer
43 * 2001
44 */
45
46public 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                InputStream data = null;
68
69                try     
70                {
71                        if ( Start.applet )
72                        {
73                                URL url = new URL(Start.dataURL + "default.xml");
74                                data = url.openStream();
75                        }
76                        addPreferences(parser, data, defaults);
77                }
78                catch(IOException ex){}
79
80                // Initialize presence messages if none have been specified
81        if ( mapMessages.isEmpty())
82        {
83            initMessages(mapMessages);
84        }               
85        }
86
87    private void addPreferences(SAXParser parser, InputStream data,Map store)
88    {
89        try
90        {
91            parser.parse(data,new PreferencesHandler(store));
92        }
93        catch (SAXException ex)
94        {
95            ex.printStackTrace();
96        }
97        catch (IOException ex)
98        {
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                 * @description : Adicionado as traducoes para os items do menu ( presenca )
108                 */
109               
110                List tempList = new ArrayList(10);
111                        tempList.add(I18N.gettext("initMessages.Available"));
112                        tempList.add(I18N.gettext("initMessages.Free_for_Chat"));
113                        mapMessages.put("chat", tempList);
114                        mapMessages.put("available", tempList);
115                        tempList = new ArrayList(10);
116                        tempList.add(I18N.gettext("initMessages.In_a_meeting"));
117                        tempList.add(I18N.gettext("initMessages.Busy"));
118                        tempList.add(I18N.gettext("initMessages.Working"));
119                        mapMessages.put("dnd", tempList);
120                        tempList = new ArrayList(10);
121                        tempList.add(I18N.gettext("initMessages.On_the_phone"));
122                        tempList.add(I18N.gettext("initMessages.Be_right_back"));
123                        mapMessages.put("away", tempList);
124                        tempList = new ArrayList(10);
125                        tempList.add(I18N.gettext("initMessages.On_vacation"));
126                        tempList.add(I18N.gettext("initMessages.Gone_home"));
127                        tempList.add(I18N.gettext("initMessages.Extend_Away"));
128                        mapMessages.put("xa", tempList);
129               
130                return mapMessages;
131        }
132       
133        /**
134         * Adds a deafult preference, which cannot be overriden byt the user
135         * and is not saved to the preferences file of the user
136         * @param prefixKey prefix and key in prefix.key formant
137         * @param value The value of the preference
138         */
139        public static void addDefaultPreferences(String prefixKey,String value)
140        {
141                defaults.put(prefixKey,value);
142        }
143
144        public static void load(JetiPrivateExtension jetiExtension)
145        {
146                mapMessages.putAll(jetiExtension.getMessages());
147        }
148
149        /**
150         * saves status messages on a server.
151         * @param key
152         * @param value
153         */
154        public static void saveStatusMessages(int key, List value)
155        {
156                mapMessages.put(convertPresenceKey(key), value);
157        }
158       
159        /**
160         * saves preferences (saved with put) to server
161         */
162        public static void saveToServer()
163        {
164                backend.send(new InfoQuery("set", new IQPrivate(new JetiPrivateExtension(null, mapMessages))));
165        }
166       
167        /**
168         * loads status messages from server
169         * @param key
170         * @return
171         */
172        public static List getStatusMessages(int key)
173        {
174                return (List) mapMessages.get(convertPresenceKey(key));
175        }
176
177        private static String convertPresenceKey(int key)
178        {
179                String stringKey;
180                switch(key)
181                {
182                        case Presence.FREE_FOR_CHAT: stringKey="chat"; break;
183                        case Presence.AWAY: stringKey="away"; break;
184                        case Presence.XA: stringKey="xa"; break;
185                        case Presence.DND: stringKey="dnd"; break;
186                        default: stringKey="available";
187                }
188                return stringKey;
189        }
190
191        /**
192         * gets a boolean preference
193         * @param prefix a prefix so there are no name clashes
194         * @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   
195         * @param key  key whose associated value is to be returned as an boolean.
196         * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist.
197         */
198        public static boolean getBoolean(String prefix,String key,boolean def)
199        {
200                String value = get(prefix,key);
201                if(value==null) return def;
202                return Boolean.valueOf(value).booleanValue();
203        }
204       
205        /**
206        * Method saves a boolean preference
207        * @param prefix a prefix so there are no name clashes
208        * @param key
209        * @param value
210        */
211        public static void putBoolean(String prefix, String key, boolean value)
212        {
213                preferences.put(prefix + "." + key,String.valueOf(value));
214        }
215       
216        /**
217         * gets a integer preference
218         * @param prefix a prefix so there are no name clashes
219         * @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       
220         * @param key  key whose associated value is to be returned as an boolean.
221         * @return int the integer value represented by the string associated with key, or def if the associated value does not exist.
222         */
223        public static int getInteger(String prefix,String key,int def)
224        {
225                String value = get(prefix,key);
226                if(value==null) return def;
227                try{
228                        return Integer.parseInt(value);
229                } catch (NumberFormatException e)
230                {
231                        return def;
232                }
233        }
234
235        /**
236        * Method saves a integer preference
237        * @param prefix a prefix so there are no name clashes
238        * @param key
239        * @param value
240        */
241        public static void putInteger(String prefix, String key,int value)
242        {
243                preferences.put(prefix + "." + key,String.valueOf(value));
244        }
245               
246        /**
247        * Method put saves a string preference.
248        * @param prefix a prefix so there are no name clashes
249        * @param key
250        * @param value
251        */
252        public static void putString(String prefix, String key, String value)
253        {
254                preferences.put(prefix + "." + key, value);
255        }
256
257        /**
258         * Method getPreference gets a string preference.
259         * @param prefix a prefix so there are no name clashes
260        * @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   
261         * @param key  key whose associated value is to be returned as an boolean.
262         * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist.
263         */
264        public static String getString(String prefix, String key,String def)
265        {
266                String value = get(prefix,key);
267                if(value==null) return def;
268                return value;
269        }
270
271        /**
272         * Method getPreference gets a preference from file.
273         * first tries defaults to get a default value that
274         * can't be overriden by the users preferences file
275         * @param prefix a prefix so there are no name clashes
276         * @param key
277         * @param value
278         */
279        private static String get(String prefix, String key)
280        {
281                String defaultValue = (String)defaults.get(prefix + "." + key);
282                if(defaultValue!=null)return defaultValue;
283                return (String) preferences.get(prefix + "." + key);
284        }
285
286        public static List getPlugins()
287        {
288                //put in new class?
289                return getPlugable("plugins");
290        }
291
292        public static List getTranslatedPlugins()
293        {
294                //put in new class?
295                List temp = new ArrayList();
296                for (Iterator i = getPlugins().iterator(); i.hasNext();)
297                {
298                        Object[] tempArray = new Object[6];
299                        Object[] plugins = (Object[])i.next();
300                        tempArray[0]= plugins[0];
301                        tempArray[1]= plugins[1];
302                        tempArray[2]= I18N.gettext((String)plugins[2]);
303                        tempArray[3]= plugins[3];
304                        tempArray[4]= plugins[4];
305                        tempArray[5]= plugins[5];
306                        temp.add(tempArray);
307                }
308                return temp;
309        }
310
311        public static List getPlugable(String name)
312        {
313                List list = (List) plugable.get(name);
314                if (list == null)
315                {
316                        list = new ArrayList();
317                        plugable.put(name, list);
318                }
319                return list;
320        }
321
322        public static List getPlugableCopy(String name)
323        {
324                List temp = new ArrayList();
325                for (Iterator i = getPlugable(name).iterator(); i.hasNext();)
326                {
327                        Object[] tempArray = new Object[6];
328                        System.arraycopy(i.next(), 0, tempArray, 0, 6);
329                        temp.add(tempArray);
330                }
331                return temp;
332        }
333       
334        /**
335         * Saves preferences to disk (in preferences.xml)
336         */
337        public static void save()
338        {
339                StringBuffer xml = new StringBuffer();
340                new Preferences().appendToXML(xml);
341                xml.insert(0,"<?xml version='1.0'?>");
342                //System.out.println(xml.toString());
343                try
344                {
345                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
346                                        new FileOutputStream(Start.path + "preferences.xml"), "UTF8"));
347                        writer.write(xml.toString());
348                        writer.close();
349                }
350                catch (IOException ex2)
351                {
352                        ex2.printStackTrace();
353                }
354        }
355
356        public void appendToXML(StringBuffer xml)
357        {
358                //appendHeader(xml);
359                appendOpenTag(xml, "<preferences>");
360                for (Iterator i = preferences.entrySet().iterator(); i.hasNext();)
361                {
362                        Map.Entry entry = (Map.Entry) i.next();
363                        String key = (String) entry.getKey();
364                        String value = (String) entry.getValue();
365                        if(value!=null)
366                        {//only preference with a value, to reset old ones
367                                appendOpenTag(xml, "<preference");
368                                appendAttribute(xml, "key", key);
369                                appendAttribute(xml, "value", value);
370                                appendCloseSymbol(xml);
371                        }
372                }
373                appendOpenTag(xml, "<plugins>");
374                for (Iterator i = plugable.entrySet().iterator(); i.hasNext();)
375                {
376                        Map.Entry entry = (Map.Entry) i.next();
377                        String type = (String) entry.getKey();
378                        for (Iterator j = ((List) entry.getValue()).iterator(); j.hasNext();)
379                        {
380                                Object[] temp = (Object[]) j.next();
381                                appendOpenTag(xml, "<plugin");
382                                appendAttribute(xml, "type", type);
383                                appendAttribute(xml, "name", temp[0]);
384                                appendAttribute(xml, "enabled", temp[1]);
385                                appendAttribute(xml, "transport", temp[3]);
386                                appendCloseSymbol(xml);
387                        }
388                }
389                appendCloseTag(xml, "</plugins>");
390                appendCloseTag(xml, "</preferences>");
391        }
392}
393
394/*
395 * Overrides for emacs
396 * Local variables:
397 * tab-width: 4
398 * End:
399 */
Note: See TracBrowser for help on using the repository browser.