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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.nickcolours;
  18. import com.dmdirc.addons.ui_swing.EDTInvocation;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.addons.ui_swing.components.IconManager;
  21. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  22. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  23. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  24. import com.dmdirc.config.ConfigBinder;
  25. import com.dmdirc.config.ConfigBinding;
  26. import com.dmdirc.config.GlobalConfig;
  27. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.events.ChannelGotNamesEvent;
  32. import com.dmdirc.events.ChannelJoinEvent;
  33. import com.dmdirc.events.ClientPrefsOpenedEvent;
  34. import com.dmdirc.events.DisplayProperty;
  35. import com.dmdirc.events.eventbus.EventBus;
  36. import com.dmdirc.interfaces.GroupChatUser;
  37. import com.dmdirc.interfaces.User;
  38. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  39. import com.dmdirc.parser.interfaces.StringConverter;
  40. import com.dmdirc.plugins.PluginDomain;
  41. import com.dmdirc.plugins.PluginInfo;
  42. import com.dmdirc.ui.messages.ColourManager;
  43. import com.dmdirc.util.colours.Colour;
  44. import java.awt.Color;
  45. import java.awt.Window;
  46. import java.nio.file.Path;
  47. import java.util.List;
  48. import java.util.Map;
  49. import javax.inject.Inject;
  50. import javax.inject.Provider;
  51. import javax.inject.Singleton;
  52. import net.engio.mbassy.listener.Handler;
  53. /**
  54. * Provides various features related to nickname colouring.
  55. */
  56. @Singleton
  57. public class NickColourManager {
  58. private static final String[] DEFAULT_RANDOM_COLOURS =
  59. {"E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3"};
  60. /** Manager to parse colours with. */
  61. private final ColourManager colourManager;
  62. private final ConfigBinder configBinder;
  63. private final IconManager iconManager;
  64. private final Provider<Window> mainWindowProvider;
  65. private final EventBus eventBus;
  66. private final NickColourYamlStore nickColourYamlStore;
  67. private final Path path;
  68. private final PluginInfo pluginInfo;
  69. private String[] randColours = DEFAULT_RANDOM_COLOURS;
  70. private boolean useowncolour;
  71. private String owncolour;
  72. private boolean userandomcolour;
  73. private Map<String, Color> nickColours;
  74. @Inject
  75. public NickColourManager(
  76. @GlobalConfig final ColourManager colourManager,
  77. @PluginDomain(NickColourPlugin.class) final String domain,
  78. @GlobalConfig final AggregateConfigProvider globalConfig,
  79. final IconManager iconManager,
  80. @MainWindow final Provider<Window> mainWindowProvider,
  81. @PluginDomain(NickColourPlugin.class) final PluginInfo pluginInfo,
  82. final EventBus eventBus,
  83. final NickColourYamlStore nickColourYamlStore,
  84. @Directory(DirectoryType.BASE) final Path path) {
  85. this.colourManager = colourManager;
  86. this.iconManager = iconManager;
  87. this.mainWindowProvider = mainWindowProvider;
  88. this.pluginInfo = pluginInfo;
  89. this.eventBus = eventBus;
  90. this.nickColourYamlStore = nickColourYamlStore;
  91. this.path = path;
  92. configBinder = globalConfig.getBinder().withDefaultDomain(domain);
  93. }
  94. @Handler
  95. public void handleChannelNames(final ChannelGotNamesEvent event) {
  96. final String network = event.getChannel().getConnection().get().getNetwork();
  97. event.getChannel().getUsers().forEach(client -> colourClient(network, client));
  98. }
  99. @Handler
  100. public void handleChannelJoin(final ChannelJoinEvent event) {
  101. final String network = event.getChannel().getConnection().get().getNetwork();
  102. colourClient(network, event.getClient());
  103. }
  104. /**
  105. * Colours the specified client according to the user's config.
  106. *
  107. * @param network The network to use for the colouring
  108. * @param client The client to be coloured
  109. */
  110. private void colourClient(final String network, final GroupChatUser client) {
  111. final StringConverter sc = client.getUser().getConnection().getParser().get()
  112. .getStringConverter();
  113. final User myself = client.getUser().getConnection().getLocalUser().orElse(null);
  114. final String nickOption1 = sc.toLowerCase(network + ':' + client.getNickname());
  115. final String nickOption2 = sc.toLowerCase("*:" + client.getNickname());
  116. if (useowncolour && client.getUser().equals(myself)) {
  117. final Colour color = colourManager.getColourFromString(owncolour, null);
  118. putColour(client, color);
  119. } else if (userandomcolour) {
  120. putColour(client, getColour(client.getNickname()));
  121. }
  122. Color color = null;
  123. if (nickColours.containsKey(nickOption1)) {
  124. color = nickColours.get(nickOption1);
  125. } else if (nickColours.containsKey(nickOption2)) {
  126. color = nickColours.get(nickOption2);
  127. }
  128. if (color != null) {
  129. putColour(client, NickColourUtils.getColourfromColor(color));
  130. }
  131. }
  132. /**
  133. * Puts the specified colour into the given map. The keys are determined by config settings.
  134. *
  135. * @param user The map to colour
  136. * @param colour Text colour to be inserted
  137. */
  138. private void putColour(final GroupChatUser user, final Colour colour) {
  139. user.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, colour);
  140. }
  141. /**
  142. * Retrieves a pseudo-random colour for the specified nickname.
  143. *
  144. * @param nick The nickname of the client whose colour we're determining
  145. *
  146. * @return Colour of the specified nickname
  147. */
  148. private Colour getColour(final CharSequence nick) {
  149. int count = 0;
  150. for (int i = 0; i < nick.length(); i++) {
  151. count += nick.charAt(i);
  152. }
  153. count %= randColours.length;
  154. return colourManager.getColourFromString(randColours[count], null);
  155. }
  156. /**
  157. * Loads this plugin.
  158. */
  159. public void onLoad() {
  160. eventBus.subscribe(this);
  161. configBinder.bind(this, NickColourManager.class);
  162. nickColours = nickColourYamlStore.readNickColourEntries(path.resolve("nickcolours.yml"));
  163. }
  164. /**
  165. * Unloads this plugin.
  166. */
  167. public void onUnload() {
  168. eventBus.unsubscribe(this);
  169. configBinder.unbind(this);
  170. saveNickColourStore(nickColours);
  171. }
  172. public void saveNickColourStore(final Map<String, Color> savingNickColours) {
  173. nickColourYamlStore.writeNickColourEntries(path.resolve("nickcolours.yml"), savingNickColours);
  174. }
  175. @ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
  176. public void handleUseOwnColour(final boolean value) {
  177. useowncolour = value;
  178. }
  179. @ConfigBinding(key = "userandomcolour", invocation = EDTInvocation.class)
  180. public void handleUseRandomColour(final boolean value) {
  181. userandomcolour = value;
  182. }
  183. @ConfigBinding(key = "owncolour", invocation = EDTInvocation.class)
  184. public void handleOwnColour(final String value) {
  185. owncolour = value;
  186. }
  187. @ConfigBinding(key = "randomcolours", invocation = EDTInvocation.class)
  188. public void handleRandomColours(final List<String> value) {
  189. randColours = value.isEmpty() ? DEFAULT_RANDOM_COLOURS : value.toArray(new String[value.size()]);
  190. }
  191. @Handler
  192. public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
  193. final PreferencesCategory general =
  194. new PluginPreferencesCategory(pluginInfo, "Nick Colours",
  195. "General configuration for NickColour plugin.");
  196. final PreferencesCategory colours = new PluginPreferencesCategory(pluginInfo, "Colours",
  197. "Set colours for specific nicknames.", UIUtilities.invokeAndWait(
  198. () -> new NickColourPanel(mainWindowProvider.get(), iconManager,
  199. colourManager, this, nickColours)));
  200. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  201. "userandomcolour", "Use random colour",
  202. "Use a pseudo-random colour for each person?",
  203. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  204. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  205. "useowncolour", "Use colour for own nick",
  206. "Always use the same colour for our own nickname?",
  207. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  208. general.addSetting(new PreferencesSetting(PreferencesType.COLOUR, pluginInfo.getDomain(),
  209. "owncolour", "Colour to use for own nick",
  210. "Colour used for our own nickname, if above setting is enabled.",
  211. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  212. general.addSubCategory(colours);
  213. event.getModel().getCategory("Plugins").addSubCategory(general);
  214. }
  215. }