/* * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail me at eric@jeti.tk */ package nu.fw.jeti.util; import java.io.*; import java.net.URL; import java.util.*; import javax.xml.parsers.SAXParser; import nu.fw.jeti.backend.Start; import nu.fw.jeti.backend.XMLDataFile; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.elements.IQPrivate; import nu.fw.jeti.jabber.elements.InfoQuery; import nu.fw.jeti.jabber.elements.JetiPrivateExtension; import nu.fw.jeti.jabber.elements.Presence; import org.xml.sax.SAXException; /** * @author E.S. de Boer * 2001 */ public class Preferences extends XMLDataFile { //private static Map map; private static Map mapMessages = new HashMap(); private static Backend backend; private static Map plugable = new HashMap(); private static Map preferences = new HashMap(); private static Map defaults = new HashMap(); public Preferences() {} public Preferences(Map p) { //copy instead of put all? preferences.putAll(p); } public Preferences(Backend backend, SAXParser parser) { Preferences.backend = backend; InputStream data = null; try { if ( Start.applet ) { URL url = new URL(Start.dataURL + "default.xml"); data = url.openStream(); } addPreferences(parser, data, defaults); } catch(IOException ex){} // Initialize presence messages if none have been specified if ( mapMessages.isEmpty()) { initMessages(mapMessages); } } private void addPreferences(SAXParser parser, InputStream data,Map store) { try { parser.parse(data,new PreferencesHandler(store)); } catch (SAXException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } private static Map initMessages(Map mapMessages) { /** * @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br * @description : Adicionado as traducoes para os items do menu ( presenca ) */ List tempList = new ArrayList(10); tempList.add(I18N.gettext("initMessages.Available")); tempList.add(I18N.gettext("initMessages.Free_for_Chat")); mapMessages.put("chat", tempList); mapMessages.put("available", tempList); tempList = new ArrayList(10); tempList.add(I18N.gettext("initMessages.In_a_meeting")); tempList.add(I18N.gettext("initMessages.Busy")); tempList.add(I18N.gettext("initMessages.Working")); mapMessages.put("dnd", tempList); tempList = new ArrayList(10); tempList.add(I18N.gettext("initMessages.On_the_phone")); tempList.add(I18N.gettext("initMessages.Be_right_back")); mapMessages.put("away", tempList); tempList = new ArrayList(10); tempList.add(I18N.gettext("initMessages.On_vacation")); tempList.add(I18N.gettext("initMessages.Gone_home")); tempList.add(I18N.gettext("initMessages.Extend_Away")); mapMessages.put("xa", tempList); return mapMessages; } /** * Adds a deafult preference, which cannot be overriden byt the user * and is not saved to the preferences file of the user * @param prefixKey prefix and key in prefix.key formant * @param value The value of the preference */ public static void addDefaultPreferences(String prefixKey,String value) { defaults.put(prefixKey,value); } public static void load(JetiPrivateExtension jetiExtension) { mapMessages.putAll(jetiExtension.getMessages()); } /** * saves status messages on a server. * @param key * @param value */ public static void saveStatusMessages(int key, List value) { mapMessages.put(convertPresenceKey(key), value); } /** * saves preferences (saved with put) to server */ public static void saveToServer() { backend.send(new InfoQuery("set", new IQPrivate(new JetiPrivateExtension(null, mapMessages)))); } /** * loads status messages from server * @param key * @return */ public static List getStatusMessages(int key) { return (List) mapMessages.get(convertPresenceKey(key)); } private static String convertPresenceKey(int key) { String stringKey; switch(key) { case Presence.FREE_FOR_CHAT: stringKey="chat"; break; case Presence.AWAY: stringKey="away"; break; case Presence.XA: stringKey="xa"; break; case Presence.DND: stringKey="dnd"; break; default: stringKey="available"; } return stringKey; } /** * gets a boolean preference * @param prefix a prefix so there are no name clashes * @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 * @param key key whose associated value is to be returned as an boolean. * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist. */ public static boolean getBoolean(String prefix,String key,boolean def) { String value = get(prefix,key); if(value==null) return def; return Boolean.valueOf(value).booleanValue(); } /** * Method saves a boolean preference * @param prefix a prefix so there are no name clashes * @param key * @param value */ public static void putBoolean(String prefix, String key, boolean value) { preferences.put(prefix + "." + key,String.valueOf(value)); } /** * gets a integer preference * @param prefix a prefix so there are no name clashes * @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 * @param key key whose associated value is to be returned as an boolean. * @return int the integer value represented by the string associated with key, or def if the associated value does not exist. */ public static int getInteger(String prefix,String key,int def) { String value = get(prefix,key); if(value==null) return def; try{ return Integer.parseInt(value); } catch (NumberFormatException e) { return def; } } /** * Method saves a integer preference * @param prefix a prefix so there are no name clashes * @param key * @param value */ public static void putInteger(String prefix, String key,int value) { preferences.put(prefix + "." + key,String.valueOf(value)); } /** * Method put saves a string preference. * @param prefix a prefix so there are no name clashes * @param key * @param value */ public static void putString(String prefix, String key, String value) { preferences.put(prefix + "." + key, value); } /** * Method getPreference gets a string preference. * @param prefix a prefix so there are no name clashes * @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 * @param key key whose associated value is to be returned as an boolean. * @return boolean the boolean value represented by the string associated with key, or def if the associated value does not exist. */ public static String getString(String prefix, String key,String def) { String value = get(prefix,key); if(value==null) return def; return value; } /** * Method getPreference gets a preference from file. * first tries defaults to get a default value that * can't be overriden by the users preferences file * @param prefix a prefix so there are no name clashes * @param key * @param value */ private static String get(String prefix, String key) { String defaultValue = (String)defaults.get(prefix + "." + key); if(defaultValue!=null)return defaultValue; return (String) preferences.get(prefix + "." + key); } public static List getPlugins() { //put in new class? return getPlugable("plugins"); } public static List getTranslatedPlugins() { //put in new class? List temp = new ArrayList(); for (Iterator i = getPlugins().iterator(); i.hasNext();) { Object[] tempArray = new Object[6]; //System.arraycopy(i.next(), 0, tempArray, 0, 6); Object[] plugins = (Object[])i.next(); tempArray[0]= plugins[0]; tempArray[1]= plugins[1]; tempArray[2]= I18N.gettext((String)plugins[2]); tempArray[3]= plugins[3]; tempArray[4]= plugins[4]; tempArray[5]= plugins[5]; temp.add(tempArray); } return temp; } public static List getPlugable(String name) { List list = (List) plugable.get(name); if (list == null) { list = new ArrayList(); plugable.put(name, list); } return list; } public static List getPlugableCopy(String name) { List temp = new ArrayList(); for (Iterator i = getPlugable(name).iterator(); i.hasNext();) { Object[] tempArray = new Object[6]; System.arraycopy(i.next(), 0, tempArray, 0, 6); temp.add(tempArray); } return temp; } /** * Saves preferences to disk (in preferences.xml) */ public static void save() { StringBuffer xml = new StringBuffer(); new Preferences().appendToXML(xml); xml.insert(0,""); //System.out.println(xml.toString()); try { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(Start.path + "preferences.xml"), "UTF8")); writer.write(xml.toString()); writer.close(); } catch (IOException ex2) { ex2.printStackTrace(); } } public void appendToXML(StringBuffer xml) { //appendHeader(xml); appendOpenTag(xml, ""); for (Iterator i = preferences.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); if(value!=null) {//only preference with a value, to reset old ones appendOpenTag(xml, ""); for (Iterator i = plugable.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String type = (String) entry.getKey(); for (Iterator j = ((List) entry.getValue()).iterator(); j.hasNext();) { Object[] temp = (Object[]) j.next(); appendOpenTag(xml, ""); appendCloseTag(xml, ""); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */