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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.addons.ui_swing.EDTInvocation;
  26. import com.dmdirc.config.ConfigBinder;
  27. import com.dmdirc.config.ConfigBinding;
  28. import com.dmdirc.events.ChannelGotnamesEvent;
  29. import com.dmdirc.events.ChannelJoinEvent;
  30. import com.dmdirc.events.DisplayProperty;
  31. import com.dmdirc.interfaces.GroupChatUser;
  32. import com.dmdirc.interfaces.User;
  33. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  34. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  35. import com.dmdirc.parser.interfaces.StringConverter;
  36. import com.dmdirc.plugins.PluginDomain;
  37. import com.dmdirc.ui.messages.ColourManager;
  38. import com.dmdirc.util.colours.Colour;
  39. import java.util.ArrayList;
  40. import java.util.Collection;
  41. import java.util.List;
  42. import javax.inject.Inject;
  43. import javax.inject.Singleton;
  44. import net.engio.mbassy.listener.Handler;
  45. /**
  46. * Provides various features related to nickname colouring.
  47. */
  48. @Singleton
  49. public class NickColourManager {
  50. /** Manager to parse colours with. */
  51. private final ColourManager colourManager;
  52. /** Config to read settings from. */
  53. private final AggregateConfigProvider globalConfig;
  54. /** Config binder. */
  55. private final ConfigBinder configBinder;
  56. /** Plugin's setting domain. */
  57. private final String domain;
  58. /** Event bus to subscribe to events on . */
  59. private final DMDircMBassador eventBus;
  60. /** "Random" colours to use to colour nicknames. */
  61. private String[] randColours = {
  62. "E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
  63. private boolean useowncolour;
  64. private String owncolour;
  65. private boolean userandomcolour;
  66. @Inject
  67. public NickColourManager(@GlobalConfig final ColourManager colourManager,
  68. @PluginDomain(NickColourPlugin.class) final String domain,
  69. @GlobalConfig final AggregateConfigProvider globalConfig,
  70. final DMDircMBassador eventBus) {
  71. this.domain = domain;
  72. this.globalConfig = globalConfig;
  73. this.colourManager = colourManager;
  74. this.eventBus = eventBus;
  75. configBinder = globalConfig.getBinder().withDefaultDomain(domain);
  76. }
  77. @Handler
  78. public void handleChannelNames(final ChannelGotnamesEvent event) {
  79. final String network = event.getChannel().getConnection().get().getNetwork();
  80. event.getChannel().getUsers().forEach(client -> colourClient(network, client));
  81. }
  82. @Handler
  83. public void handleChannelJoin(final ChannelJoinEvent event) {
  84. final String network = event.getChannel().getConnection().get().getNetwork();
  85. colourClient(network, event.getClient());
  86. }
  87. /**
  88. * Colours the specified client according to the user's config.
  89. *
  90. * @param network The network to use for the colouring
  91. * @param client The client to be coloured
  92. */
  93. private void colourClient(final String network, final GroupChatUser client) {
  94. final StringConverter sc = client.getUser().getConnection().getParser().get()
  95. .getStringConverter();
  96. final User myself = client.getUser();
  97. final String nickOption1 = "color:" + sc.toLowerCase(network + ':' + client.getNickname());
  98. final String nickOption2 = "color:" + sc.toLowerCase("*:" + client.getNickname());
  99. if (useowncolour && client.getUser().equals(myself)) {
  100. final Colour color = colourManager.getColourFromString(owncolour, null);
  101. putColour(client, color);
  102. } else if (userandomcolour) {
  103. putColour(client, getColour(client.getNickname()));
  104. }
  105. String[] parts = null;
  106. if (globalConfig.hasOptionString(domain, nickOption1)) {
  107. parts = getParts(globalConfig, domain, nickOption1);
  108. } else if (globalConfig.hasOptionString(domain, nickOption2)) {
  109. parts = getParts(globalConfig, domain, nickOption2);
  110. }
  111. if (parts != null) {
  112. Colour textColor = null;
  113. if (parts[0] != null) {
  114. textColor = colourManager.getColourFromString(parts[0], null);
  115. }
  116. putColour(client, textColor);
  117. }
  118. }
  119. /**
  120. * Puts the specified colour into the given map. The keys are determined by config settings.
  121. *
  122. * @param user The map to colour
  123. * @param colour Text colour to be inserted
  124. */
  125. private void putColour(final GroupChatUser user, final Colour colour) {
  126. user.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, colour);
  127. }
  128. /**
  129. * Retrieves a pseudo-random colour for the specified nickname.
  130. *
  131. * @param nick The nickname of the client whose colour we're determining
  132. *
  133. * @return Colour of the specified nickname
  134. */
  135. private Colour getColour(final CharSequence nick) {
  136. int count = 0;
  137. for (int i = 0; i < nick.length(); i++) {
  138. count += nick.charAt(i);
  139. }
  140. count %= randColours.length;
  141. return colourManager.getColourFromString(randColours[count], null);
  142. }
  143. /**
  144. * Reads the nick colour data from the config.
  145. *
  146. * @param config Config to read settings from
  147. * @param domain Config domain
  148. *
  149. * @return A multi-dimensional array of nick colour info.
  150. */
  151. public static Object[][] getData(final ReadOnlyConfigProvider config, final String domain) {
  152. final Collection<Object[]> data = new ArrayList<>();
  153. config.getOptions(domain).keySet().stream().filter(key -> key.startsWith("color:"))
  154. .forEach(key -> {
  155. final String network = key.substring(6, key.indexOf(':', 6));
  156. final String user = key.substring(1 + key.indexOf(':', 6));
  157. final String[] parts = getParts(config, domain, key);
  158. data.add(new Object[]{network, user, parts[0], parts[1]});
  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 array of the colours that
  170. * should be used for it.
  171. *
  172. * @param config Config to read settings from
  173. * @param domain Config domain
  174. * @param key The config key to look up
  175. *
  176. * @return The colours specified by the given key
  177. */
  178. private static String[] getParts(final ReadOnlyConfigProvider config, final String domain,
  179. final String key) {
  180. String[] parts = config.getOption(domain, key).split(":");
  181. if (parts.length == 0) {
  182. parts = new String[]{null, null};
  183. } else if (parts.length == 1) {
  184. parts = new String[]{parts[0], null};
  185. } else if (parts.length == 2) {
  186. parts = new String[]{parts[0], parts[1]};
  187. }
  188. return parts;
  189. }
  190. /**
  191. * Loads this plugin.
  192. */
  193. public void onLoad() {
  194. eventBus.subscribe(this);
  195. configBinder.bind(this, NickColourManager.class);
  196. }
  197. /**
  198. * Unloads this plugin.
  199. */
  200. public void onUnload() {
  201. eventBus.unsubscribe(this);
  202. configBinder.unbind(this);
  203. }
  204. @ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
  205. public void handleUseOwnColour(final boolean value) {
  206. useowncolour = value;
  207. }
  208. @ConfigBinding(key = "userandomcolour", invocation = EDTInvocation.class)
  209. public void handleUseRandomColour(final boolean value) {
  210. userandomcolour = value;
  211. }
  212. @ConfigBinding(key = "owncolour", invocation = EDTInvocation.class)
  213. public void handleOwnColour(final String value) {
  214. owncolour = value;
  215. }
  216. @ConfigBinding(key = "randomcolours", invocation = EDTInvocation.class)
  217. public void handleRandomColours(final List<String> value) {
  218. randColours = value.toArray(new String[value.size()]);
  219. }
  220. }