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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.EDTInvocation;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.addons.ui_swing.components.IconManager;
  27. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  28. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  29. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  30. import com.dmdirc.config.ConfigBinder;
  31. import com.dmdirc.config.ConfigBinding;
  32. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  33. import com.dmdirc.config.prefs.PreferencesCategory;
  34. import com.dmdirc.config.prefs.PreferencesSetting;
  35. import com.dmdirc.config.prefs.PreferencesType;
  36. import com.dmdirc.events.ChannelGotNamesEvent;
  37. import com.dmdirc.events.ChannelJoinEvent;
  38. import com.dmdirc.events.ClientPrefsOpenedEvent;
  39. import com.dmdirc.events.DisplayProperty;
  40. import com.dmdirc.interfaces.EventBus;
  41. import com.dmdirc.interfaces.GroupChatUser;
  42. import com.dmdirc.interfaces.User;
  43. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  44. import com.dmdirc.parser.interfaces.StringConverter;
  45. import com.dmdirc.plugins.PluginDomain;
  46. import com.dmdirc.plugins.PluginInfo;
  47. import com.dmdirc.ui.messages.ColourManager;
  48. import com.dmdirc.util.colours.Colour;
  49. import java.awt.Color;
  50. import java.awt.Window;
  51. import java.nio.file.Path;
  52. import java.util.List;
  53. import java.util.Map;
  54. import javax.inject.Inject;
  55. import javax.inject.Provider;
  56. import javax.inject.Singleton;
  57. import net.engio.mbassy.listener.Handler;
  58. /**
  59. * Provides various features related to nickname colouring.
  60. */
  61. @Singleton
  62. public class NickColourManager {
  63. /** Manager to parse colours with. */
  64. private final ColourManager colourManager;
  65. private final ConfigBinder configBinder;
  66. private final IconManager iconManager;
  67. private final Provider<Window> mainWindowProvider;
  68. private final EventBus eventBus;
  69. private final NickColourYamlStore nickColourYamlStore;
  70. private final Path path;
  71. private final PluginInfo pluginInfo;
  72. private String[] randColours = {
  73. "E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
  74. private boolean useowncolour;
  75. private String owncolour;
  76. private boolean userandomcolour;
  77. private Map<String, Color> nickColours;
  78. @Inject
  79. public NickColourManager(
  80. @GlobalConfig final ColourManager colourManager,
  81. @PluginDomain(NickColourPlugin.class) final String domain,
  82. @GlobalConfig final AggregateConfigProvider globalConfig,
  83. final IconManager iconManager,
  84. @MainWindow final Provider<Window> mainWindowProvider,
  85. @PluginDomain(NickColourPlugin.class) final PluginInfo pluginInfo,
  86. final EventBus eventBus,
  87. final NickColourYamlStore nickColourYamlStore,
  88. @Directory(DirectoryType.BASE) final Path path) {
  89. this.colourManager = colourManager;
  90. this.iconManager = iconManager;
  91. this.mainWindowProvider = mainWindowProvider;
  92. this.pluginInfo = pluginInfo;
  93. this.eventBus = eventBus;
  94. this.nickColourYamlStore = nickColourYamlStore;
  95. this.path = path;
  96. configBinder = globalConfig.getBinder().withDefaultDomain(domain);
  97. }
  98. @Handler
  99. public void handleChannelNames(final ChannelGotNamesEvent event) {
  100. final String network = event.getChannel().getConnection().get().getNetwork();
  101. event.getChannel().getUsers().forEach(client -> colourClient(network, client));
  102. }
  103. @Handler
  104. public void handleChannelJoin(final ChannelJoinEvent event) {
  105. final String network = event.getChannel().getConnection().get().getNetwork();
  106. colourClient(network, event.getClient());
  107. }
  108. /**
  109. * Colours the specified client according to the user's config.
  110. *
  111. * @param network The network to use for the colouring
  112. * @param client The client to be coloured
  113. */
  114. private void colourClient(final String network, final GroupChatUser client) {
  115. final StringConverter sc = client.getUser().getConnection().getParser().get()
  116. .getStringConverter();
  117. final User myself = client.getUser();
  118. final String nickOption1 = sc.toLowerCase(network + ':' + client.getNickname());
  119. final String nickOption2 = sc.toLowerCase("*:" + client.getNickname());
  120. if (useowncolour && client.getUser().equals(myself)) {
  121. final Colour color = colourManager.getColourFromString(owncolour, null);
  122. putColour(client, color);
  123. } else if (userandomcolour) {
  124. putColour(client, getColour(client.getNickname()));
  125. }
  126. Color color = null;
  127. if (nickColours.containsKey(nickOption1)) {
  128. color = nickColours.get(nickOption1);
  129. } else if (nickColours.containsKey(nickOption2)) {
  130. color = nickColours.get(nickOption2);
  131. }
  132. if (color != null) {
  133. putColour(client, NickColourUtils.getColourfromColor(color));
  134. }
  135. }
  136. /**
  137. * Puts the specified colour into the given map. The keys are determined by config settings.
  138. *
  139. * @param user The map to colour
  140. * @param colour Text colour to be inserted
  141. */
  142. private void putColour(final GroupChatUser user, final Colour colour) {
  143. user.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, colour);
  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 CharSequence 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. * Loads this plugin.
  162. */
  163. public void onLoad() {
  164. eventBus.subscribe(this);
  165. configBinder.bind(this, NickColourManager.class);
  166. nickColours = nickColourYamlStore.readNickColourEntries(path.resolve("nickcolours.yml"));
  167. }
  168. /**
  169. * Unloads this plugin.
  170. */
  171. public void onUnload() {
  172. eventBus.unsubscribe(this);
  173. configBinder.unbind(this);
  174. saveNickColourStore(nickColours);
  175. }
  176. public void saveNickColourStore(final Map<String, Color> savingNickColours) {
  177. nickColourYamlStore.writeNickColourEntries(path.resolve("nickcolours.yml"), savingNickColours);
  178. }
  179. @ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
  180. public void handleUseOwnColour(final boolean value) {
  181. useowncolour = value;
  182. }
  183. @ConfigBinding(key = "userandomcolour", invocation = EDTInvocation.class)
  184. public void handleUseRandomColour(final boolean value) {
  185. userandomcolour = value;
  186. }
  187. @ConfigBinding(key = "owncolour", invocation = EDTInvocation.class)
  188. public void handleOwnColour(final String value) {
  189. owncolour = value;
  190. }
  191. @ConfigBinding(key = "randomcolours", invocation = EDTInvocation.class)
  192. public void handleRandomColours(final List<String> value) {
  193. randColours = value.toArray(new String[value.size()]);
  194. }
  195. @Handler
  196. public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
  197. final PreferencesCategory general =
  198. new PluginPreferencesCategory(pluginInfo, "Nick Colours",
  199. "General configuration for NickColour plugin.");
  200. final PreferencesCategory colours = new PluginPreferencesCategory(pluginInfo, "Colours",
  201. "Set colours for specific nicknames.", UIUtilities.invokeAndWait(
  202. () -> new NickColourPanel(mainWindowProvider.get(), iconManager,
  203. colourManager, this, nickColours)));
  204. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  205. "userandomcolour", "Use random colour",
  206. "Use a pseudo-random colour for each person?",
  207. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  208. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  209. "useowncolour", "Use colour for own nick",
  210. "Always use the same colour for our own nickname?",
  211. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  212. general.addSetting(new PreferencesSetting(PreferencesType.COLOUR, pluginInfo.getDomain(),
  213. "owncolour", "Colour to use for own nick",
  214. "Colour used for our own nickname, if above setting is enabled.",
  215. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  216. general.addSubCategory(colours);
  217. event.getModel().getCategory("Plugins").addSubCategory(general);
  218. }
  219. }