source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/util/Preferences.java @ 1070

Revision 1070, 11.2 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #474 - Arquivos modificados para a tradução do Applet.

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                        //System.arraycopy(i.next(), 0, tempArray, 0, 6);
300                        Object[] plugins = (Object[])i.next();
301                        tempArray[0]= plugins[0];
302                        tempArray[1]= plugins[1];
303                        tempArray[2]= I18N.gettext((String)plugins[2]);
304                        tempArray[3]= plugins[3];
305                        tempArray[4]= plugins[4];
306                        tempArray[5]= plugins[5];
307                        temp.add(tempArray);
308                }
309                return temp;
310        }
311
312        public static List getPlugable(String name)
313        {
314                List list = (List) plugable.get(name);
315                if (list == null)
316                {
317                        list = new ArrayList();
318                        plugable.put(name, list);
319                }
320                return list;
321        }
322
323        public static List getPlugableCopy(String name)
324        {
325                List temp = new ArrayList();
326                for (Iterator i = getPlugable(name).iterator(); i.hasNext();)
327                {
328                        Object[] tempArray = new Object[6];
329                        System.arraycopy(i.next(), 0, tempArray, 0, 6);
330                        temp.add(tempArray);
331                }
332                return temp;
333        }
334       
335        /**
336         * Saves preferences to disk (in preferences.xml)
337         */
338        public static void save()
339        {
340                StringBuffer xml = new StringBuffer();
341                new Preferences().appendToXML(xml);
342                xml.insert(0,"<?xml version='1.0'?>");
343                //System.out.println(xml.toString());
344                try
345                {
346                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
347                                        new FileOutputStream(Start.path + "preferences.xml"), "UTF8"));
348                        writer.write(xml.toString());
349                        writer.close();
350                }
351                catch (IOException ex2)
352                {
353                        ex2.printStackTrace();
354                }
355        }
356
357        public void appendToXML(StringBuffer xml)
358        {
359                //appendHeader(xml);
360                appendOpenTag(xml, "<preferences>");
361                for (Iterator i = preferences.entrySet().iterator(); i.hasNext();)
362                {
363                        Map.Entry entry = (Map.Entry) i.next();
364                        String key = (String) entry.getKey();
365                        String value = (String) entry.getValue();
366                        if(value!=null)
367                        {//only preference with a value, to reset old ones
368                                appendOpenTag(xml, "<preference");
369                                appendAttribute(xml, "key", key);
370                                appendAttribute(xml, "value", value);
371                                appendCloseSymbol(xml);
372                        }
373                }
374                appendOpenTag(xml, "<plugins>");
375                for (Iterator i = plugable.entrySet().iterator(); i.hasNext();)
376                {
377                        Map.Entry entry = (Map.Entry) i.next();
378                        String type = (String) entry.getKey();
379                        for (Iterator j = ((List) entry.getValue()).iterator(); j.hasNext();)
380                        {
381                                Object[] temp = (Object[]) j.next();
382                                appendOpenTag(xml, "<plugin");
383                                appendAttribute(xml, "type", type);
384                                appendAttribute(xml, "name", temp[0]);
385                                appendAttribute(xml, "enabled", temp[1]);
386                                appendAttribute(xml, "transport", temp[3]);
387                                appendCloseSymbol(xml);
388                        }
389                }
390                appendCloseTag(xml, "</plugins>");
391                appendCloseTag(xml, "</preferences>");
392        }
393}
394
395/*
396 * Overrides for emacs
397 * Local variables:
398 * tab-width: 4
399 * End:
400 */
Note: See TracBrowser for help on using the repository browser.