1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
32 import irc.security.SecurityProvider;
\r
34 import java.awt.Color;
\r
35 import java.awt.Font;
\r
36 import java.awt.Image;
\r
37 import java.util.Hashtable;
\r
47 * Global IRC configuration. Any call is synchronized and can be performed from
\r
50 public class IRCConfiguration {
\r
52 * Image is centered.
\r
54 public static final int TILING_CENTER = 0;
\r
56 * Image is stretched.
\r
58 public static final int TILING_STRETCH = 1;
\r
62 public static final int TILING_TILE = 2;
\r
64 * Image position is fixed.
\r
66 public static final int TILING_FIXED = 3;
\r
69 * Image position is fixed on the left.
\r
71 public static final int TILING_HORIZONTAL_LEFT = 0;
\r
73 * Image position is fixed on the right.
\r
75 public static final int TILING_HORIZONTAL_RIGHT = 256;
\r
77 * Image position if fixed upside.
\r
79 public static final int TILING_VERTICAL_UP = 0;
\r
81 * Image position is fixed downside.
\r
83 public static final int TILING_VERTICAL_DOWN = 512;
\r
85 private NullItem NULL_ITEM = new NullItem();
\r
87 private TextProvider _textProvider;
\r
88 private ImageLoader _loader;
\r
89 private URLHandler _handler;
\r
90 private FileHandler _file;
\r
91 private AudioConfiguration _audioConfig;
\r
93 private RuleList _backgroundImageRules;
\r
94 private RuleList _backgroundTilingRules;
\r
95 private RuleList _colorsRules;
\r
96 private RuleList _fontRules;
\r
98 private SmileyTable _table;
\r
99 private ListHandler _mayJoinList;
\r
100 private ListHandler _mayLeaveList;
\r
101 private ListHandler _mayCommandList;
\r
102 private Hashtable _htable;
\r
103 private SecurityProvider _provider;
\r
104 private ParameterProvider _paramProvider;
\r
105 private ParameterProvider _interfaceParamProvider;
\r
107 private String _guiInfoString;
\r
109 private String[] _initCommands;
\r
112 * Create a new IRCConfiguration.
\r
115 * text provider to use.
\r
117 * URL handler to use.
\r
119 * image loader to use.
\r
121 * sound handler to use.
\r
123 * file handler to use.
\r
124 * @param paramProvider
\r
125 * parameter provider.
\r
126 * @param interfaceParamProvider
\r
127 * interface parameter provider.
\r
129 public IRCConfiguration(TextProvider text, URLHandler handler, ImageLoader loader, SoundHandler sound,
\r
130 FileHandler file, ParameterProvider paramProvider, ParameterProvider interfaceParamProvider) {
\r
131 _provider = new SecurityProvider();
\r
133 _paramProvider = paramProvider;
\r
134 _interfaceParamProvider = interfaceParamProvider;
\r
136 _htable = new Hashtable();
\r
138 _backgroundImageRules = new RuleList();
\r
139 _backgroundTilingRules = new RuleList();
\r
140 _backgroundTilingRules.setDefaultValue(new Integer(0));
\r
142 _fontRules = new RuleList();
\r
143 _fontRules.setDefaultValue(new Font("Monospaced", Font.PLAIN, 12));
\r
145 _colorsRules = new RuleList();
\r
146 Color[] def = new Color[16];
\r
147 loadDefaultColors(def);
\r
148 _colorsRules.setDefaultValue(def);
\r
150 _audioConfig = new AudioConfiguration(sound);
\r
151 _table = new SmileyTable();
\r
155 _textProvider = text;
\r
156 _handler = handler;
\r
158 _guiInfoString = "";
\r
160 _initCommands = new String[0];
\r
164 * Create a new dummy IRCConfiguration. All parameters are set to default.
\r
165 * Note that the 'gui' parameter is not set.
\r
167 * @return a new IRCConfiguration instance.
\r
169 public static IRCConfiguration createDummyIRCConfiguration() {
\r
171 FileHandler file = new LocalFileHandler();
\r
172 StreamParameterProvider provider = new StreamParameterProvider(null);
\r
173 ConfigurationLoader loader = new ConfigurationLoader(provider, new NullURLHandler(), new AWTImageLoader(),
\r
174 new NullSoundHandler(), file);
\r
175 return loader.loadIRCConfiguration();
\r
176 } catch (Exception ex) {
\r
177 /* should not reach here : bug */
\r
178 throw new Error("Error creating dummy IRCConfiguration : " + ex);
\r
183 * Get the initialisation commands.
\r
185 * @return the initialisation commands.
\r
187 public String[] getInitialization() {
\r
188 return _initCommands;
\r
192 * Set the initialisation commands.
\r
195 * the initialisation commands.
\r
197 public void setInitialisation(String[] init) {
\r
198 _initCommands = init;
\r
202 * Get the GUI information string.
\r
204 * @return the GUI info string.
\r
206 public String getGUIInfoString() {
\r
207 return _guiInfoString;
\r
211 * Set the GUI information string.
\r
214 * the GUI information string.
\r
216 public void setGUIInfoString(String string) {
\r
217 _guiInfoString = string;
\r
221 * Get the file handler.
\r
223 * @return the file handler.
\r
225 public FileHandler getFileHandler() {
\r
230 * Display the about page.
\r
232 public void displayAboutPage() {
\r
233 new AboutDialog(this);
\r
237 * Get the parameter provider used for creating this IRCConfiguration.
\r
239 * @return the parameter provider.
\r
241 public ParameterProvider getParameterProvider() {
\r
242 return _paramProvider;
\r
246 * Get the parameter provider whose should be used by an user interface.
\r
248 * @return the interface parameter provider.
\r
250 public ParameterProvider getInterfaceParameterProvider() {
\r
251 return _interfaceParamProvider;
\r
255 * Get the high version number.
\r
257 * @return the high version number.
\r
259 public int getVersionHigh() {
\r
264 * Get the middle version number.
\r
266 * @return the middle version number.
\r
268 public int getVersionMed() {
\r
273 * Get the low version number.
\r
275 * @return the low version number.
\r
277 public int getVersionLow() {
\r
282 * Get the version modifiers.
\r
284 * @return version modifiers.
\r
286 public String getVersionModifiers() {
\r
291 * Get version number as a string.
\r
293 * @return high.med.lowmod version number.
\r
295 public String getVersion() {
\r
296 return getVersionHigh() + "." + getVersionMed() + "." + getVersionLow() + getVersionModifiers();
\r
300 * Get the security provider.
\r
302 * @return the security provider.
\r
304 public SecurityProvider getSecurityProvider() {
\r
309 * Set the given property to the given value. This value may be null.
\r
316 public synchronized void set(String key, Object obj) {
\r
319 _htable.put(key.toLowerCase(java.util.Locale.ENGLISH), obj);
\r
323 * Set the given property to the given int value.
\r
330 public synchronized void set(String key, int val) {
\r
331 set(key, new Integer(val));
\r
335 * Set the given property to the given boolean value.
\r
342 public synchronized void set(String key, boolean val) {
\r
343 set(key, new Boolean(val));
\r
347 * Get the given property value.
\r
351 * @return the property value.
\r
352 * @throws RuntimeException
\r
353 * if the property is unknown.
\r
355 public synchronized Object get(String key) {
\r
356 Object v = _htable.get(key.toLowerCase(java.util.Locale.ENGLISH));
\r
358 throw new RuntimeException("Unknown configuration property " + key);
\r
359 if (v == NULL_ITEM)
\r
365 * Get the given property value as an int value.
\r
369 * @return the property value.
\r
370 * @throws RuntimeException
\r
371 * if the property is unknown.
\r
373 public synchronized int getI(String key) {
\r
374 Integer i = (Integer) get(key);
\r
375 return i.intValue();
\r
379 * Get the given property value as a boolean value.
\r
383 * @return the property value.
\r
384 * @throws RuntimeException
\r
385 * if the property is unknown.
\r
387 public synchronized boolean getB(String key) {
\r
388 Boolean b = (Boolean) get(key);
\r
389 return b.booleanValue();
\r
393 * Get the given property value as String value.
\r
397 * @return the property value.
\r
398 * @throws RuntimeException
\r
399 * if the property is unknown.
\r
401 public synchronized String getS(String key) {
\r
402 return (String) get(key);
\r
406 * Test wether the given channel may be left.
\r
409 * channel to be left.
\r
410 * @return true if channel may be left, false otherwise.
\r
412 public synchronized boolean mayLeave(String channel) {
\r
413 return _mayLeaveList.inList(channel);
\r
417 * Test wether the given channel may be joined.
\r
420 * channel to be joined.
\r
421 * @return true if channel may be joined, false otherwise.
\r
423 public synchronized boolean mayJoin(String channel) {
\r
424 return _mayJoinList.inList(channel);
\r
428 * Set the "may join" list.
\r
433 public synchronized void setJoinList(String list) {
\r
434 _mayJoinList = new ListHandler(list);
\r
438 * Set the "may leave" list.
\r
443 public synchronized void setLeaveList(String list) {
\r
444 _mayLeaveList = new ListHandler(list);
\r
448 * Set the authorized command list.
\r
451 * authorized command list.
\r
453 public synchronized void setCommandList(String list) {
\r
454 _mayCommandList = new ListHandler(list);
\r
458 * Check wether the given command may be executed by the user.
\r
461 * command to test, not prefixed by /.
\r
462 * @return true if command may be executed, false otherwise.
\r
464 public synchronized boolean mayCommand(String cmd) {
\r
465 if (cmd.startsWith("/"))
\r
466 cmd = cmd.substring(1);
\r
467 return _mayCommandList.inList(cmd);
\r
471 * Get the background image associated with the given source type and name.
\r
477 * @return background image, or null if no image is to be displayed.
\r
479 public synchronized Image getBackgroundImage(String type, String name) {
\r
480 if (!getB("style:backgroundImage"))
\r
482 return (Image) _backgroundImageRules.findValue(new String[] { type, name });
\r
486 * Get the background image tiling associated with the given source type and
\r
493 * @return tiling mode for background image.
\r
495 public synchronized int getBackgroundTiling(String type, String name) {
\r
496 return ((Integer) _backgroundTilingRules.findValue(new String[] { type, name })).intValue();
\r
500 * Set the background image to be used for the given source type and name.
\r
509 public synchronized void setBackgroundImage(String type, String name, String image) {
\r
510 _backgroundImageRules.addRule(new String[] { type, name }, getImageLoader().getImage(image));
\r
514 * Set the background image tiling to be used for the given source type and
\r
522 * tiling mode to be used.
\r
524 public synchronized void setBackgroundTiling(String type, String name, int tiling) {
\r
525 _backgroundTilingRules.addRule(new String[] { type, name }, new Integer(tiling));
\r
529 * Set the font to be used for the given source type and name.
\r
538 public synchronized void setFont(String type, String name, Font f) {
\r
539 _fontRules.addRule(new String[] { type, name }, f);
\r
543 * Format a string given asl info and current settings.
\r
546 * user whois information.
\r
547 * @return formatted asl information. An empty string shouldn't be displayed.
\r
549 public synchronized String formatASL(String info) {
\r
550 String noprefix = getS("noasldisplayprefix");
\r
551 if ((noprefix.length() > 0) && (info.startsWith(noprefix)))
\r
554 String separator = getS("aslseparatorstring");
\r
555 if (separator.length() > 0) {
\r
556 int pos = info.indexOf(separator);
\r
558 info = info.substring(0, pos);
\r
561 String orig = info;
\r
562 int pos = info.indexOf(' ');
\r
566 String age = info.substring(0, pos).trim();
\r
567 info = info.substring(pos + 1).trim();
\r
568 pos = info.indexOf(' ');
\r
571 String gender = info.substring(0, pos).trim().toLowerCase(java.util.Locale.ENGLISH);
\r
572 String location = info.substring(pos + 1).trim();
\r
575 if (gender.equals(getS("aslmale").toLowerCase(java.util.Locale.ENGLISH)))
\r
576 text = IRCTextProvider.ASL_MALE;
\r
577 else if (gender.equals(getS("aslfemale").toLowerCase(java.util.Locale.ENGLISH)))
\r
578 text = IRCTextProvider.ASL_FEMALE;
\r
579 else if (gender.equals(getS("aslunknown").toLowerCase(java.util.Locale.ENGLISH)))
\r
580 text = IRCTextProvider.ASL_UNKNOWN;
\r
584 return getText(text, age, location);
\r
588 * Find the correct ASL color for the given info.
\r
591 * user whois information.
\r
597 * undefined gender color.
\r
598 * @return asl color.
\r
600 public synchronized Color getASLColor(String info, Color male, Color femeale, Color undef) {
\r
601 int pos = info.indexOf(' ');
\r
604 info = info.substring(pos).trim();
\r
605 pos = info.indexOf(' ');
\r
608 info = info.substring(0, pos).trim().toLowerCase(java.util.Locale.ENGLISH);
\r
609 if (info.equals(getS("aslmale").toLowerCase(java.util.Locale.ENGLISH)))
\r
611 if (info.equals(getS("aslfemale").toLowerCase(java.util.Locale.ENGLISH)))
\r
617 * Get the audio configuration.
\r
619 * @return the audio configuration.
\r
621 public AudioConfiguration getAudioConfiguration() {
\r
622 return _audioConfig;
\r
626 * Get the default style context.
\r
628 * @return the default style context.
\r
630 public synchronized StyleContext getDefaultStyleContext() {
\r
631 return getStyleContext("", "");
\r
635 * Get the style context associated with the given source type and name.
\r
641 * @return associated style context.
\r
643 public synchronized StyleContext getStyleContext(String type, String name) {
\r
644 StyleContext ctx = new StyleContext();
\r
651 * Load the given color array with the default source colors. The array must
\r
655 * color array to be filled.
\r
657 public synchronized void loadDefaultColors(Color[] cols) {
\r
658 cols[0] = new Color(0xFFFFFF);
\r
659 cols[1] = new Color(0x000000);
\r
660 cols[2] = new Color(0x00007F);
\r
661 cols[3] = new Color(0x009300);
\r
662 cols[4] = new Color(0xFF0000);
\r
663 cols[5] = new Color(0x7F0000);
\r
664 cols[6] = new Color(0x9C009C);
\r
665 cols[7] = new Color(0xFC7F00);
\r
666 cols[8] = new Color(0xFFFF00);
\r
667 cols[9] = new Color(0x00FC00);
\r
668 cols[10] = new Color(0x009393);
\r
669 cols[11] = new Color(0x00FFFF);
\r
670 cols[12] = new Color(0x0000FC);
\r
671 cols[13] = new Color(0xFF00FF);
\r
672 cols[14] = new Color(0x7F7F7F);
\r
673 cols[15] = new Color(0xD2D2D2);
\r
677 * Set the source colors.
\r
686 public synchronized void setSourceColor(String type, String name, Color c[]) {
\r
687 _colorsRules.addRule(new String[] { type, name }, c);
\r
691 * Get the colors associated with the given style context.
\r
694 * the context to get colors from.
\r
695 * @return color array for the given context.
\r
697 public synchronized Color[] getStyleColors(StyleContext context) {
\r
698 return (Color[]) _colorsRules.findValue(new String[] { context.type, context.name });
\r
702 * Get the font associated with the given style context.
\r
705 * the context to get font from.
\r
706 * @return font for the given context.
\r
708 public synchronized Font getStyleFont(StyleContext context) {
\r
709 return (Font) _fontRules.findValue(new String[] { context.type, context.name });
\r
713 * Get the image background associated with the given style context.
\r
716 * the context to get background from.
\r
717 * @return image for the given context.
\r
719 public synchronized Image getStyleBackgroundImage(StyleContext context) {
\r
720 return getBackgroundImage(context.type, context.name);
\r
724 * Get the tiling background associated with the given style context.
\r
727 * the context to get tiling from.
\r
728 * @return tiling for the given context.
\r
730 public synchronized int getStyleBackgroundTiling(StyleContext context) {
\r
731 return getBackgroundTiling(context.type, context.name);
\r
735 * Add a smiley in the smiley table.
\r
738 * the matching text to replace.
\r
742 public synchronized void addSmiley(String match, String file) {
\r
743 _table.addSmiley(match, _loader.getImage(file));
\r
747 * Get the image loader.
\r
749 * @return the image loader.
\r
751 public ImageLoader getImageLoader() {
\r
756 * Get the smileys table.
\r
758 * @return the smiley table.
\r
760 public SmileyTable getSmileyTable() {
\r
765 * Return wether asl should be processed, because asl field is enabled or
\r
766 * floating asl is enabled.
\r
768 * @return master asl status.
\r
770 public synchronized boolean getASLMaster() {
\r
771 return getB("asl") | getB("style:floatingasl");
\r
777 * @return URLHandler.
\r
779 public URLHandler getURLHandler() {
\r
784 * Get text provider.
\r
786 * @return text provider.
\r
788 public TextProvider getTextProvider() {
\r
789 return _textProvider;
\r
793 * Report an internal error.
\r
798 * optional (can be null) cause exception.
\r
800 * mail address where the bug report should be sent.
\r
802 public void internalError(String message, Throwable ex, String mail) {
\r
803 System.err.println("************ Internal error ************");
\r
804 System.err.println("Please submit a bug report to " + mail + " including the following information:");
\r
805 System.err.println("Message:");
\r
806 System.err.println(message);
\r
809 System.err.println("Root cause:");
\r
810 ex.printStackTrace();
\r
812 System.err.println("Stack trace:");
\r
813 Thread.dumpStack();
\r
817 * Get formatted text associated with the given text code, with no parameter.
\r
821 * @return formatted text.
\r
823 public synchronized String getText(int code) {
\r
824 return _textProvider.getString(code);
\r
828 * Get formatted text associated with the given text code, with one parameter.
\r
834 * @return formatted text.
\r
836 public synchronized String getText(int code, String p1) {
\r
837 return _textProvider.getString(code, p1);
\r
841 * Get formatted text associated with the given text code, with two
\r
849 * second parameter.
\r
850 * @return formatted text.
\r
852 public synchronized String getText(int code, String p1, String p2) {
\r
853 return _textProvider.getString(code, p1, p2);
\r
857 * Get formatted text associated with the given text code, with three
\r
865 * second parameter.
\r
868 * @return formatted text.
\r
870 public synchronized String getText(int code, String p1, String p2, String p3) {
\r
871 return _textProvider.getString(code, p1, p2, p3);
\r