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.

NickColourManager.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.ClientModule.GlobalConfig;
  26. import com.dmdirc.ClientModule.UserConfig;
  27. import com.dmdirc.actions.ActionManager;
  28. import com.dmdirc.actions.CoreActionType;
  29. import com.dmdirc.interfaces.ActionListener;
  30. import com.dmdirc.interfaces.actions.ActionType;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import com.dmdirc.interfaces.config.ConfigChangeListener;
  33. import com.dmdirc.interfaces.config.ConfigProvider;
  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.PluginDomain;
  38. import com.dmdirc.ui.Colour;
  39. import com.dmdirc.ui.messages.ColourManager;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. import java.util.Map;
  43. import javax.inject.Inject;
  44. import javax.inject.Singleton;
  45. /**
  46. * Provides various features related to nickname colouring.
  47. */
  48. @Singleton
  49. public class NickColourManager implements ActionListener, ConfigChangeListener {
  50. /** "Random" colours to use to colour nicknames. */
  51. private String[] randColours = new String[]{
  52. "E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
  53. private boolean useowncolour;
  54. private String owncolour;
  55. private boolean userandomcolour;
  56. private boolean settext;
  57. private boolean setnicklist;
  58. /** Manager to parse colours with. */
  59. private final ColourManager colourManager;
  60. /** Config to read settings from. */
  61. private final AggregateConfigProvider globalConfig;
  62. /** Plugin's setting domain. */
  63. private final String domain;
  64. @Inject
  65. public NickColourManager(final ColourManager colourManager,
  66. @PluginDomain(NickColourPlugin.class) final String domain,
  67. @GlobalConfig final AggregateConfigProvider globalConfig,
  68. @UserConfig final ConfigProvider userConfig) {
  69. this.domain = domain;
  70. this.globalConfig = globalConfig;
  71. this.colourManager = colourManager;
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public void processEvent(final ActionType type, final StringBuffer format,
  76. final Object... arguments) {
  77. if (type.equals(CoreActionType.CHANNEL_GOTNAMES)) {
  78. final ChannelInfo chanInfo = ((Channel) arguments[0]).getChannelInfo();
  79. final String network = ((Channel) arguments[0]).getConnection().getNetwork();
  80. for (ChannelClientInfo client : chanInfo.getChannelClients()) {
  81. colourClient(network, client);
  82. }
  83. } else if (type.equals(CoreActionType.CHANNEL_JOIN)) {
  84. final String network = ((Channel) arguments[0]).getConnection().getNetwork();
  85. colourClient(network, (ChannelClientInfo) arguments[1]);
  86. }
  87. }
  88. /**
  89. * Colours the specified client according to the user's config.
  90. *
  91. * @param network The network to use for the colouring
  92. * @param client The client to be coloured
  93. */
  94. private void colourClient(final String network,
  95. final ChannelClientInfo client) {
  96. final Map<Object, Object> map = client.getMap();
  97. final ClientInfo myself = client.getClient().getParser().getLocalClient();
  98. final String nickOption1 = "color:"
  99. + client.getClient().getParser().getStringConverter().
  100. toLowerCase(network + ":" + client.getClient().getNickname());
  101. final String nickOption2 = "color:"
  102. + client.getClient().getParser().getStringConverter().
  103. toLowerCase("*:" + client.getClient().getNickname());
  104. if (useowncolour && client.getClient().equals(myself)) {
  105. final Colour color = colourManager.getColourFromString(owncolour, null);
  106. putColour(map, color, color);
  107. } else if (userandomcolour) {
  108. putColour(map, getColour(client.getClient().getNickname()), getColour(client.
  109. getClient().getNickname()));
  110. }
  111. String[] parts = null;
  112. if (globalConfig.hasOptionString(domain, nickOption1)) {
  113. parts = getParts(globalConfig, domain, nickOption1);
  114. } else if (globalConfig.hasOptionString(domain, nickOption2)) {
  115. parts = getParts(globalConfig, domain, nickOption2);
  116. }
  117. if (parts != null) {
  118. Colour textColor = null;
  119. Colour nickColor = null;
  120. if (parts[0] != null) {
  121. textColor = colourManager.getColourFromString(parts[0], null);
  122. }
  123. if (parts[1] != null) {
  124. nickColor = colourManager.getColourFromString(parts[1], null);
  125. }
  126. putColour(map, textColor, nickColor);
  127. }
  128. }
  129. /**
  130. * Puts the specified colour into the given map. The keys are determined by config settings.
  131. *
  132. * @param map The map to use
  133. * @param textColour Text colour to be inserted
  134. * @param nickColour Nick colour to be inserted
  135. */
  136. private void putColour(final Map<Object, Object> map, final Colour textColour,
  137. final Colour nickColour) {
  138. if (settext && textColour != null) {
  139. map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
  140. }
  141. if (setnicklist && nickColour != null) {
  142. map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
  143. }
  144. }
  145. /**
  146. * Retrieves a pseudo-random colour for the specified nickname.
  147. *
  148. * @param nick The nickname of the client whose colour we're determining
  149. *
  150. * @return Colour of the specified nickname
  151. */
  152. private Colour getColour(final String nick) {
  153. int count = 0;
  154. for (int i = 0; i < nick.length(); i++) {
  155. count += nick.charAt(i);
  156. }
  157. count %= randColours.length;
  158. return colourManager.getColourFromString(randColours[count], null);
  159. }
  160. /**
  161. * Reads the nick colour data from the config.
  162. *
  163. * @param config Config to read settings from
  164. * @param domain Config domain
  165. *
  166. * @return A multi-dimensional array of nick colour info.
  167. */
  168. public static Object[][] getData(final AggregateConfigProvider config, final String domain) {
  169. final List<Object[]> data = new ArrayList<>();
  170. for (String key : config.getOptions(domain).keySet()) {
  171. if (key.startsWith("color:")) {
  172. final String network = key.substring(6, key.indexOf(':', 6));
  173. final String user = key.substring(1 + key.indexOf(':', 6));
  174. final String[] parts = getParts(config, domain, key);
  175. data.add(new Object[]{network, user, parts[0], parts[1]});
  176. }
  177. }
  178. final Object[][] res = new Object[data.size()][4];
  179. int i = 0;
  180. for (Object[] row : data) {
  181. res[i] = row;
  182. i++;
  183. }
  184. return res;
  185. }
  186. /**
  187. * Retrieves the config option with the specified key, and returns an array of the colours that
  188. * should be used for it.
  189. *
  190. * @param config Config to read settings from
  191. * @param domain Config domain
  192. * @param key The config key to look up
  193. *
  194. * @return The colours specified by the given key
  195. */
  196. private static String[] getParts(final AggregateConfigProvider config, final String domain,
  197. final String key) {
  198. String[] parts = config.getOption(domain, key).split(":");
  199. if (parts.length == 0) {
  200. parts = new String[]{null, null};
  201. } else if (parts.length == 1) {
  202. parts = new String[]{parts[0], null};
  203. }
  204. return parts;
  205. }
  206. /**
  207. * Loads this plugin.
  208. */
  209. public void onLoad() {
  210. setCachedSettings();
  211. ActionManager.getActionManager().registerListener(this,
  212. CoreActionType.CHANNEL_GOTNAMES, CoreActionType.CHANNEL_JOIN);
  213. }
  214. /**
  215. * Unloads this plugin.
  216. */
  217. public void onUnload() {
  218. ActionManager.getActionManager().unregisterListener(this);
  219. }
  220. /**
  221. * Updates cached settings.
  222. */
  223. private void setCachedSettings() {
  224. useowncolour = globalConfig.getOptionBool(domain, "useowncolour");
  225. owncolour = globalConfig.getOption(domain, "owncolour");
  226. userandomcolour = globalConfig.getOptionBool(domain, "userandomcolour");
  227. settext = globalConfig.getOptionBool(domain, "settext");
  228. setnicklist = globalConfig.getOptionBool(domain, "setnicklist");
  229. if (globalConfig.hasOptionString(domain, "randomcolours")) {
  230. randColours = globalConfig.getOptionList(domain, "randomcolours").toArray(new String[0]);
  231. }
  232. }
  233. /** {@inheritDoc} */
  234. @Override
  235. public void configChanged(final String domain, final String key) {
  236. setCachedSettings();
  237. }
  238. /**
  239. * Returns this plugin's settings domain.
  240. *
  241. * @return Settings domain
  242. */
  243. public String getDomain() {
  244. return domain;
  245. }
  246. }