]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/PixxConfigurationLoader.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / gui / pixx / PixxConfigurationLoader.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc.gui.pixx;
31
32 import irc.FileTextProvider;
33 import irc.IRCConfiguration;
34 import irc.ParameterProvider;
35 import irc.StringParser;
36 import irc.TextProvider;
37
38 import java.awt.Color;
39 import java.util.Vector;
40
41 /**
42  * Toolkit for Configuration creation.
43  */
44 public class PixxConfigurationLoader {
45         private IRCConfiguration _config;
46         private ParameterProvider _provider;
47
48         /**
49          * Create a new PixxConfigurationLoader.
50          * 
51          * @param config
52          *          the irc configuration.
53          */
54         public PixxConfigurationLoader(IRCConfiguration config) {
55                 _config = config;
56                 _provider = _config.getInterfaceParameterProvider();
57
58         }
59
60         /**
61          * Create a new PixxConfiguration object, using the given IRCConfiguration.
62          * 
63          * @return a new PixxConfiguration instance.
64          * @throws Exception
65          *           if an error occurs.
66          */
67         public PixxConfiguration loadPixxConfiguration() throws Exception {
68                 return getPixxConfiguration();
69         }
70
71         private String getParameter(String key) {
72                 return _provider.getParameter(key);
73         }
74
75         private boolean getBoolean(String key, boolean def) {
76                 String v = getParameter(key);
77                 if (v == null)
78                         return def;
79                 v = v.toLowerCase(java.util.Locale.ENGLISH).trim();
80                 if (v.equals("true") || v.equals("on") || v.equals("1"))
81                         return true;
82                 return false;
83         }
84
85         private String getString(String key, String def) {
86                 String v = getParameter(key);
87                 if (v == null)
88                         return def;
89                 return v;
90         }
91
92         private int getInt(String key, int def) {
93                 String v = getParameter(key);
94                 if (v == null)
95                         return def;
96                 try {
97                         return Integer.parseInt(v);
98                 } catch (Exception e) {
99                         return def;
100                 }
101
102         }
103
104         private TextProvider getTextProvider() {
105                 String lang = getString("language", "pixx-english");
106                 String encoding = getString("languageencoding", "");
107                 String extension = getString("lngextension", "lng");
108                 String backlang = getString("backuplanguage", "pixx-english");
109                 String backencoding = getString("backuplanguageencoding", "");
110                 return new FileTextProvider(lang + "." + extension, encoding, backlang + "." + extension, backencoding,
111                                 _config.getFileHandler());
112         }
113
114         private String[] getArray(String name) {
115                 Vector v = new Vector();
116                 String cmd;
117                 int i = 1;
118                 do {
119                         cmd = getParameter(name + i);
120                         if (cmd != null)
121                                 v.insertElementAt(cmd, v.size());
122                         i++;
123                 } while (cmd != null);
124                 String[] ans = new String[v.size()];
125                 for (i = 0; i < v.size(); i++)
126                         ans[i] = (String) v.elementAt(i);
127                 return ans;
128         }
129
130         private void readDocking(PixxConfiguration config) {
131                 StringParser parser = new StringParser();
132                 String[] arr = getArray("dockingconfig");
133                 for (int i = 0; i < arr.length; i++) {
134                         String cmd = arr[i];
135                         String back[] = parser.parseString(cmd);
136                         if (back.length >= 3) {
137                                 String type = back[0].toLowerCase(java.util.Locale.ENGLISH);
138                                 String name = back[1].toLowerCase(java.util.Locale.ENGLISH);
139                                 String action = back[2].toLowerCase(java.util.Locale.ENGLISH);
140                                 boolean undock = false;
141                                 if (action.equals("undock"))
142                                         undock = true;
143                                 config.setDockingPolicy(type, name, undock);
144                         }
145                 }
146         }
147
148         private void readMouse(PixxConfiguration config, String name, int button, int click) throws Exception {
149                 String s = getString("mouse" + name.toLowerCase(java.util.Locale.ENGLISH), button + " " + click);
150                 int pos = s.indexOf(' ');
151                 if (pos < 0)
152                         throw new Exception("mouse" + name.toLowerCase(java.util.Locale.ENGLISH) + " parameter syntax error");
153                 String before = s.substring(0, pos).trim();
154                 String after = s.substring(pos + 1).trim();
155                 config.setMouseConfiguration(name, new Integer(before).intValue(), new Integer(after).intValue());
156         }
157
158         private String[] getHighLightWords() {
159                 String words = getParameter("highlightwords");
160                 if (words == null)
161                         return new String[0];
162                 return new StringParser().parseString(words);
163         }
164
165         private void add(Vector v, Object o) {
166                 v.insertElementAt(o, v.size());
167         }
168
169         private void str2(Vector v, PixxConfiguration c, int id, String str) {
170                 add(v, new String[] { c.getText(id), str });
171         }
172
173         /**
174          * Load default popup menu configuration, based on the global
175          * IRCConfiguration.
176          * 
177          * @param config
178          *          global IRCConfiguration.
179          */
180         public void loadDefaultPopup(PixxConfiguration config) {
181                 Vector v = config.getNickMenuVector();
182                 String[] str;
183
184                 boolean b = config.getB("showstatus") && !config.getIRCConfiguration().getB("useinfo");
185                 if (b)
186                         str2(v, config, PixxTextProvider.GUI_WHOIS, "/whois %1");
187                 str2(v, config, PixxTextProvider.GUI_QUERY, "/query %1");
188                 str2(v, config, PixxTextProvider.GUI_KICK, "/kick %2 %1");
189                 str2(v, config, PixxTextProvider.GUI_BAN, "/mode %2 +b %1");
190                 str = new String[4];
191                 str[0] = config.getText(PixxTextProvider.GUI_KICKBAN);
192                 str[1] = "/mode %2 -o %1";
193                 str[2] = "/mode %2 +b %1";
194                 str[3] = "/kick %2 %1";
195                 add(v, str);
196
197                 add(v, new String[] { "--" });
198                 str2(v, config, PixxTextProvider.GUI_OP, "/mode %2 +o %1");
199                 str2(v, config, PixxTextProvider.GUI_DEOP, "/mode %2 -o %1");
200                 str2(v, config, PixxTextProvider.GUI_VOICE, "/mode %2 +v %1");
201                 str2(v, config, PixxTextProvider.GUI_DEVOICE, "/mode %2 -v %1");
202                 if (b) {
203                         add(v, new String[] { "--" });
204                         str2(v, config, PixxTextProvider.GUI_PING, "/ctcp ping %1");
205                         str2(v, config, PixxTextProvider.GUI_VERSION, "/ctcp version %1");
206                         str2(v, config, PixxTextProvider.GUI_TIME, "/ctcp time %1");
207                         str2(v, config, PixxTextProvider.GUI_FINGER, "/ctcp finger %1");
208                 }
209         }
210
211         private void readPopup(PixxConfiguration config) {
212                 if (!getBoolean("configurepopup", false)) {
213                         loadDefaultPopup(config);
214                         return;
215                 }
216                 Vector v = config.getNickMenuVector();
217                 String[] str = getArray("popupmenustring");
218                 for (int i = 0; i < str.length; i++) {
219
220                         String cmd[] = getArray("popupmenucommand" + (i + 1) + "_");
221                         String[] fin = new String[1 + cmd.length];
222                         fin[0] = str[i];
223                         for (int j = 0; j < cmd.length; j++)
224                                 fin[j + 1] = cmd[j];
225                         add(v, fin);
226                 }
227         }
228
229         private PixxColorModel getColorModel() {
230                 PixxColorModel model = new PixxColorModel();
231
232                 for (int i = 0; i < model.getColorCount(); i++) {
233                         String color = getParameter("color" + i);
234                         try {
235                                 if (color != null) {
236                                         model.setColor(i, new Color(Integer.parseInt(color, 16)));
237                                 }
238                         } catch (Exception e) {
239                         }
240                 }
241                 return model;
242         }
243
244         private PixxConfiguration getPixxConfiguration() throws Exception {
245                 PixxConfiguration config = new PixxConfiguration(_config);
246                 config.setTextProvider(getTextProvider());
247
248                 Vector v = new Vector();
249                 String[] words = getHighLightWords();
250                 for (int i = 0; i < words.length; i++)
251                         v.insertElementAt(words[i], v.size());
252                 config.setHighLightConfig(getInt("highlightcolor", 5), getBoolean("highlightnick", false), v);
253                 config.set("highlight", getBoolean("highlight", false));
254
255                 config.set("timestamp", getBoolean("timestamp", false));
256                 config.set("showclose", getBoolean("showclose", true));
257                 config.set("showconnect", getBoolean("showconnect", true));
258                 config.set("showchanlist", getBoolean("showchanlist", true));
259                 config.set("showabout", getBoolean("showabout", true));
260                 config.set("showhelp", getBoolean("showhelp", true));
261                 config.set("nicklistwidth", getInt("nicklistwidth", 130));
262                 config.set("nickfield", getBoolean("nickfield", false));
263                 config.set("showstatus", getBoolean("showstatus", true));
264                 config.set("styleselector", getBoolean("styleselector", false));
265                 config.set("setfontonstyle", getBoolean("setfontonstyle", false));
266                 config.set("helppage", getString("helppage", "http://www.pjirc.com/help.php"));
267                 config.set("showchannelnickchanged", getBoolean("showchannelnickchanged", true));
268                 config.set("showchannelnickmodeapply", getBoolean("showchannelnickmodeapply", true));
269                 config.set("showchannelmodeapply", getBoolean("showchannelmodeapply", true));
270                 config.set("showchanneltopicchanged", getBoolean("showchanneltopicchanged", true));
271                 config.set("showchannelnickquit", getBoolean("showchannelnickquit", true));
272                 config.set("showchannelnickkick", getBoolean("showchannelnickkick", true));
273                 config.set("showchannelnickpart", getBoolean("showchannelnickpart", true));
274                 config.set("showchannelnickjoin", getBoolean("showchannelnickjoin", true));
275                 config.set("showdock", getBoolean("showdock", true));
276                 config.set("automaticqueries", getBoolean("automaticqueries", true));
277                 config.set("leftnickalign", getBoolean("leftnickalign", false));
278                 config.set("taskbaritemwidth", getInt("taskbaritemwidth", 100));
279                 config.set("scrollspeed", getInt("scrollspeed", 0));
280                 config.set("leaveonundockedwindowclose", getBoolean("leaveonundockedwindowclose", false));
281                 config.set("nickprefix", getString("nickprefix", "<"));
282                 config.set("nickpostfix", getString("nickpostfix", "> "));
283                 config.set("showchannelyoujoin", getBoolean("showchannelyoujoin", false));
284
285                 config.set("displayentertexthere", getBoolean("displayentertexthere", false));
286                 config.set("ignoreallmouseevents", getBoolean("ignoreallmouseevents", false));
287                 config.set("hideundockedsources", getBoolean("hideundockedsources", false));
288
289                 config.set("displaychannelname", getBoolean("displaychannelname", true));
290                 config.set("displaychannelmode", getBoolean("displaychannelmode", true));
291                 config.set("displaychannelcount", getBoolean("displaychannelcount", true));
292                 config.set("displaychanneltopic", getBoolean("displaychanneltopic", true));
293
294                 config.setColorModel(getColorModel());
295
296                 readMouse(config, "nickquery", 1, 2);
297                 readMouse(config, "urlopen", 1, 2);
298                 readMouse(config, "channeljoin", 1, 2);
299                 readMouse(config, "nickpopup", 3, 1);
300                 readMouse(config, "taskbarpopup", 3, 1);
301
302                 readPopup(config);
303
304                 readDocking(config);
305
306                 return config;
307         }
308
309 }