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

Revision 1064, 11.5 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                 * @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               
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                //removed save message version check because it gives problems when
148                //the server has removed the messages so there is no version
149//              if(jetiExtension.getXmlVersion()==null)
150//              {//on extension not found set save to true to try to overwrite
151//                      save = true;
152//              }
153//              else
154//              {
155//                      try
156//                      {
157//                              //save if loaded xml version <= own xmlversion (backwards compatible)
158//                              save = (Integer.parseInt(jetiExtension.getXmlVersion().substring(1)) <= Integer.parseInt(JetiPrivateExtension.XML_VERSION.substring(1)));
159//                      }
160//                      catch (Exception e)
161//                      {
162//                      } //exception not saven
163//              }
164        }
165
166        /**
167         * saves status messages on a server.
168         * @param key
169         * @param value
170         */
171        public static void saveStatusMessages(int key, List value)
172        {
173                mapMessages.put(convertPresenceKey(key), value);
174                //if(save)backend.savePreferences(map,mapMessages);
175        }
176       
177        /**
178         * saves preferences (saved with put) to server
179         */
180        public static void saveToServer()
181        {
182                backend.send(new InfoQuery("set", new IQPrivate(new JetiPrivateExtension(null, mapMessages))));
183        }
184       
185        /**
186         * loads status messages from server
187         * @param key
188         * @return
189         */
190        public static List getStatusMessages(int key)
191        {
192                return (List) mapMessages.get(convertPresenceKey(key));
193        }
194
195        private static String convertPresenceKey(int key)
196        {
197                String stringKey;
198                switch(key)
199                {
200                        case Presence.FREE_FOR_CHAT: stringKey="chat"; break;
201                        case Presence.AWAY: stringKey="away"; break;
202                        case Presence.XA: stringKey="xa"; break;
203                        case Presence.DND: stringKey="dnd"; break;
204                        default: stringKey="available";
205                }
206                return stringKey;
207        }
208
209        /**
210         * gets a boolean preference
211         * @param prefix a prefix so there are no name clashes
212         * @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   
213         * @param key  key whose associated value is to be returned as an boolean.
214         * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist.
215         */
216        public static boolean getBoolean(String prefix,String key,boolean def)
217        {
218                String value = get(prefix,key);
219                if(value==null) return def;
220                return Boolean.valueOf(value).booleanValue();
221        }
222       
223        /**
224        * Method saves a boolean preference
225        * @param prefix a prefix so there are no name clashes
226        * @param key
227        * @param value
228        */
229        public static void putBoolean(String prefix, String key, boolean value)
230        {
231                preferences.put(prefix + "." + key,String.valueOf(value));
232        }
233       
234        /**
235         * gets a integer preference
236         * @param prefix a prefix so there are no name clashes
237         * @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       
238         * @param key  key whose associated value is to be returned as an boolean.
239         * @return int the integer value represented by the string associated with key, or def if the associated value does not exist.
240         */
241        public static int getInteger(String prefix,String key,int def)
242        {
243                String value = get(prefix,key);
244                if(value==null) return def;
245                try{
246                        return Integer.parseInt(value);
247                } catch (NumberFormatException e)
248                {
249                        return def;
250                }
251        }
252
253        /**
254        * Method saves a integer preference
255        * @param prefix a prefix so there are no name clashes
256        * @param key
257        * @param value
258        */
259        public static void putInteger(String prefix, String key,int value)
260        {
261                preferences.put(prefix + "." + key,String.valueOf(value));
262        }
263               
264        /**
265        * Method put saves a string preference.
266        * @param prefix a prefix so there are no name clashes
267        * @param key
268        * @param value
269        */
270        public static void putString(String prefix, String key, String value)
271        {
272                preferences.put(prefix + "." + key, value);
273        }
274
275        /**
276         * Method getPreference gets a string preference.
277         * @param prefix a prefix so there are no name clashes
278        * @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   
279         * @param key  key whose associated value is to be returned as an boolean.
280         * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist.
281         */
282        public static String getString(String prefix, String key,String def)
283        {
284                String value = get(prefix,key);
285                if(value==null) return def;
286                return value;
287        }
288
289        /**
290         * Method getPreference gets a preference from file.
291         * first tries defaults to get a default value that
292         * can't be overriden by the users preferences file
293         * @param prefix a prefix so there are no name clashes
294         * @param key
295         * @param value
296         */
297        private static String get(String prefix, String key)
298        {
299                String defaultValue = (String)defaults.get(prefix + "." + key);
300                if(defaultValue!=null)return defaultValue;
301                return (String) preferences.get(prefix + "." + key);
302        }
303
304        public static List getPlugins()
305        {
306                //put in new class?
307                return getPlugable("plugins");
308        }
309
310        public static List getTranslatedPlugins()
311        {
312                //put in new class?
313                List temp = new ArrayList();
314                for (Iterator i = getPlugins().iterator(); i.hasNext();)
315                {
316                        Object[] tempArray = new Object[6];
317                        //System.arraycopy(i.next(), 0, tempArray, 0, 6);
318                        Object[] plugins = (Object[])i.next();
319                        tempArray[0]= plugins[0];
320                        tempArray[1]= plugins[1];
321                        tempArray[2]= I18N.gettext((String)plugins[2]);
322                        tempArray[3]= plugins[3];
323                        tempArray[4]= plugins[4];
324                        tempArray[5]= plugins[5];
325                        temp.add(tempArray);
326                }
327                return temp;
328        }
329
330        public static List getPlugable(String name)
331        {
332                List list = (List) plugable.get(name);
333                if (list == null)
334                {
335                        list = new ArrayList();
336                        plugable.put(name, list);
337                }
338                return list;
339        }
340
341        public static List getPlugableCopy(String name)
342        {
343                List temp = new ArrayList();
344                for (Iterator i = getPlugable(name).iterator(); i.hasNext();)
345                {
346                        Object[] tempArray = new Object[6];
347                        System.arraycopy(i.next(), 0, tempArray, 0, 6);
348                        temp.add(tempArray);
349                }
350                return temp;
351        }
352       
353        /**
354         * Saves preferences to disk (in preferences.xml)
355         */
356        public static void save()
357        {
358                StringBuffer xml = new StringBuffer();
359                new Preferences().appendToXML(xml);
360                xml.insert(0,"<?xml version='1.0'?>");
361                //System.out.println(xml.toString());
362                try
363                {
364                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
365                                        new FileOutputStream(Start.path + "preferences.xml"), "UTF8"));
366                        writer.write(xml.toString());
367                        writer.close();
368                }
369                catch (IOException ex2)
370                {
371                        ex2.printStackTrace();
372                }
373        }
374
375        public void appendToXML(StringBuffer xml)
376        {
377                //appendHeader(xml);
378                appendOpenTag(xml, "<preferences>");
379                for (Iterator i = preferences.entrySet().iterator(); i.hasNext();)
380                {
381                        Map.Entry entry = (Map.Entry) i.next();
382                        String key = (String) entry.getKey();
383                        String value = (String) entry.getValue();
384                        if(value!=null)
385                        {//only preference with a value, to reset old ones
386                                appendOpenTag(xml, "<preference");
387                                appendAttribute(xml, "key", key);
388                                appendAttribute(xml, "value", value);
389                                appendCloseSymbol(xml);
390                        }
391                }
392                appendOpenTag(xml, "<plugins>");
393                for (Iterator i = plugable.entrySet().iterator(); i.hasNext();)
394                {
395                        Map.Entry entry = (Map.Entry) i.next();
396                        String type = (String) entry.getKey();
397                        for (Iterator j = ((List) entry.getValue()).iterator(); j.hasNext();)
398                        {
399                                Object[] temp = (Object[]) j.next();
400                                appendOpenTag(xml, "<plugin");
401                                appendAttribute(xml, "type", type);
402                                appendAttribute(xml, "name", temp[0]);
403                                appendAttribute(xml, "enabled", temp[1]);
404                                appendAttribute(xml, "transport", temp[3]);
405                                appendCloseSymbol(xml);
406                        }
407                }
408                appendCloseTag(xml, "</plugins>");
409                appendCloseTag(xml, "</preferences>");
410        }
411}
412
413/*
414 * Overrides for emacs
415 * Local variables:
416 * tab-width: 4
417 * End:
418 */
Note: See TracBrowser for help on using the repository browser.