You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NickColourPlugin.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.nickcolours;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.ChannelClientProperty;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.interfaces.ActionType;
  27. import com.dmdirc.actions.CoreActionType;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.config.prefs.PreferencesCategory;
  30. import com.dmdirc.config.prefs.PreferencesManager;
  31. import com.dmdirc.config.prefs.PreferencesSetting;
  32. import com.dmdirc.config.prefs.PreferencesType;
  33. import com.dmdirc.interfaces.ActionListener;
  34. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  35. import com.dmdirc.parser.interfaces.ChannelInfo;
  36. import com.dmdirc.parser.interfaces.ClientInfo;
  37. import com.dmdirc.plugins.Plugin;
  38. import com.dmdirc.ui.messages.ColourManager;
  39. import java.awt.Color;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. import java.util.Map;
  43. /**
  44. * Provides various features related to nickname colouring.
  45. *
  46. * @author chris
  47. */
  48. public final class NickColourPlugin extends Plugin implements ActionListener {
  49. /** "Random" colours to use to colour nicknames. */
  50. private String[] randColours = new String[] {
  51. "E90E7F", "8E55E9", "B30E0E", "18B33C",
  52. "58ADB3", "9E54B3", "B39875", "3176B3",
  53. };
  54. /** Creates a new instance of NickColourPlugin. */
  55. public NickColourPlugin() {
  56. super();
  57. }
  58. /** {@inheritDoc} */
  59. @Override
  60. public void processEvent(final ActionType type, final StringBuffer format,
  61. final Object... arguments) {
  62. if (type.equals(CoreActionType.CHANNEL_GOTNAMES)) {
  63. final ChannelInfo chanInfo = ((Channel) arguments[0]).getChannelInfo();
  64. final String network = ((Channel) arguments[0]).getServer().getNetwork();
  65. for (ChannelClientInfo client : chanInfo.getChannelClients()) {
  66. colourClient(network, client);
  67. }
  68. } else if (type.equals(CoreActionType.CHANNEL_JOIN)) {
  69. final String network = ((Channel) arguments[0]).getServer().getNetwork();
  70. colourClient(network, (ChannelClientInfo) arguments[1]);
  71. }
  72. }
  73. /**
  74. * Colours the specified client according to the user's config.
  75. *
  76. * @param network The network to use for the colouring
  77. * @param client The client to be coloured
  78. */
  79. private void colourClient(final String network, final ChannelClientInfo client) {
  80. final Map map = client.getMap();
  81. final ClientInfo myself = client.getClient().getParser().getLocalClient();
  82. final String nickOption1 = "color:"
  83. + client.getClient().getParser().getStringConverter().toLowerCase(network + ":" + client.getClient().getNickname());
  84. final String nickOption2 = "color:"
  85. + client.getClient().getParser().getStringConverter().toLowerCase("*:" + client.getClient().getNickname());
  86. if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "useowncolour")
  87. && client.getClient().equals(myself)) {
  88. final Color color = ColourManager.parseColour(
  89. IdentityManager.getGlobalConfig().getOption(getDomain(), "owncolour"));
  90. putColour(map, color, color);
  91. } else if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "userandomcolour")) {
  92. putColour(map, getColour(client.getClient().getNickname()), getColour(client.getClient().getNickname()));
  93. }
  94. String[] parts = null;
  95. if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), nickOption1)) {
  96. parts = getParts(nickOption1);
  97. } else if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), nickOption2)) {
  98. parts = getParts(nickOption2);
  99. }
  100. if (parts != null) {
  101. Color textColor = null;
  102. Color nickColor = null;
  103. if (parts[0] != null) {
  104. textColor = ColourManager.parseColour(parts[0], null);
  105. }
  106. if (parts[1] != null) {
  107. nickColor = ColourManager.parseColour(parts[1], null);
  108. }
  109. putColour(map, textColor, nickColor);
  110. }
  111. }
  112. /**
  113. * Puts the specified colour into the given map. The keys are determined
  114. * by config settings.
  115. *
  116. * @param map The map to use
  117. * @param textColour Text colour to be inserted
  118. * @param nickColour Nick colour to be inserted
  119. */
  120. @SuppressWarnings("unchecked")
  121. private void putColour(final Map map, final Color textColour, final Color nickColour) {
  122. if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "settext")
  123. && textColour != null) {
  124. map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
  125. }
  126. if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "setnicklist")
  127. && nickColour != null) {
  128. map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
  129. }
  130. }
  131. /**
  132. * Retrieves a pseudo-random colour for the specified nickname.
  133. *
  134. * @param nick The nickname of the client whose colour we're determining
  135. * @return Colour of the specified nickname
  136. */
  137. private Color getColour(final String nick) {
  138. int count = 0;
  139. for (int i = 0; i < nick.length(); i++) {
  140. count += nick.charAt(i);
  141. }
  142. count = count % randColours.length;
  143. return ColourManager.parseColour(randColours[count]);
  144. }
  145. /**
  146. * Reads the nick colour data from the config.
  147. *
  148. * @return A multi-dimensional array of nick colour info.
  149. */
  150. public Object[][] getData() {
  151. final List<Object[]> data = new ArrayList<Object[]>();
  152. for (String key : IdentityManager.getGlobalConfig().getOptions(getDomain()).keySet()) {
  153. if (key.startsWith("color:")) {
  154. final String network = key.substring(6, key.indexOf(':', 6));
  155. final String user = key.substring(1 + key.indexOf(':', 6));
  156. final String[] parts = getParts(key);
  157. data.add(new Object[]{network, user, parts[0], parts[1]});
  158. }
  159. }
  160. final Object[][] res = new Object[data.size()][4];
  161. int i = 0;
  162. for (Object[] row : data) {
  163. res[i] = row;
  164. i++;
  165. }
  166. return res;
  167. }
  168. /**
  169. * Retrieves the config option with the specified key, and returns an
  170. * array of the colours that should be used for it.
  171. *
  172. * @param key The config key to look up
  173. * @return The colours specified by the given key
  174. */
  175. private String[] getParts(final String key) {
  176. String[] parts = IdentityManager.getGlobalConfig().getOption(getDomain(), key).split(":");
  177. if (parts.length == 0) {
  178. parts = new String[]{null, null};
  179. } else if (parts.length == 1) {
  180. parts = new String[]{parts[0], null};
  181. }
  182. return parts;
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public void onLoad() {
  187. if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), "randomcolours")) {
  188. randColours = IdentityManager.getGlobalConfig().getOptionList(getDomain(), "randomcolours").toArray(new String[0]);
  189. }
  190. ActionManager.addListener(this, CoreActionType.CHANNEL_GOTNAMES,
  191. CoreActionType.CHANNEL_JOIN);
  192. }
  193. /** {@inheritDoc} */
  194. @Override
  195. public void onUnload() {
  196. ActionManager.removeListener(this);
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public void showConfig(final PreferencesManager manager) {
  201. final PreferencesCategory general = new PreferencesCategory("Nick Colours",
  202. "General configuration for NickColour plugin.");
  203. final PreferencesCategory colours = new PreferencesCategory("Colours",
  204. "Set colours for specific nicknames.", new NickColourPanel(this));
  205. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  206. "ui", "shownickcoloursintext", "Show colours in text" +
  207. "area", "Colour nicknames in main text area?"));
  208. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  209. "ui", "shownickcoloursinnicklist", "Show colours in" +
  210. " nick list", "Colour nicknames in channel nick lists?"));
  211. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  212. getDomain(), "settext", "Set colours in textarea",
  213. "Should the plugin set the textarea colour of nicks?"));
  214. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  215. getDomain(), "setnicklist", "Set colours in nick list",
  216. "Should the plugin set the nick list colour of nicks?"));
  217. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  218. getDomain(), "userandomcolour", "Use random colour",
  219. "Use a pseudo-random colour for each person?"));
  220. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  221. getDomain(), "useowncolour", "Use colour for own nick",
  222. "Always use the same colour for our own nickname?"));
  223. general.addSetting(new PreferencesSetting(PreferencesType.COLOUR,
  224. getDomain(), "owncolour", "Colour to use for own nick",
  225. "Colour used for our own nickname, if above setting is enabled."));
  226. general.addSubCategory(colours);
  227. manager.getCategory("Plugins").addSubCategory(general);
  228. }
  229. }