Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

NickColourManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.addons.ui_swing.EDTInvocation;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.addons.ui_swing.components.IconManager;
  26. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  27. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  28. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  29. import com.dmdirc.config.ConfigBinder;
  30. import com.dmdirc.config.ConfigBinding;
  31. import com.dmdirc.config.GlobalConfig;
  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. private static final String[] DEFAULT_RANDOM_COLOURS =
  64. {"E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3"};
  65. /** Manager to parse colours with. */
  66. private final ColourManager colourManager;
  67. private final ConfigBinder configBinder;
  68. private final IconManager iconManager;
  69. private final Provider<Window> mainWindowProvider;
  70. private final EventBus eventBus;
  71. private final NickColourYamlStore nickColourYamlStore;
  72. private final Path path;
  73. private final PluginInfo pluginInfo;
  74. private String[] randColours = DEFAULT_RANDOM_COLOURS;
  75. private boolean useowncolour;
  76. private String owncolour;
  77. private boolean userandomcolour;
  78. private Map<String, Color> nickColours;
  79. @Inject
  80. public NickColourManager(
  81. @GlobalConfig final ColourManager colourManager,
  82. @PluginDomain(NickColourPlugin.class) final String domain,
  83. @GlobalConfig final AggregateConfigProvider globalConfig,
  84. final IconManager iconManager,
  85. @MainWindow final Provider<Window> mainWindowProvider,
  86. @PluginDomain(NickColourPlugin.class) final PluginInfo pluginInfo,
  87. final EventBus eventBus,
  88. final NickColourYamlStore nickColourYamlStore,
  89. @Directory(DirectoryType.BASE) final Path path) {
  90. this.colourManager = colourManager;
  91. this.iconManager = iconManager;
  92. this.mainWindowProvider = mainWindowProvider;
  93. this.pluginInfo = pluginInfo;
  94. this.eventBus = eventBus;
  95. this.nickColourYamlStore = nickColourYamlStore;
  96. this.path = path;
  97. configBinder = globalConfig.getBinder().withDefaultDomain(domain);
  98. }
  99. @Handler
  100. public void handleChannelNames(final ChannelGotNamesEvent event) {
  101. final String network = event.getChannel().getConnection().get().getNetwork();
  102. event.getChannel().getUsers().forEach(client -> colourClient(network, client));
  103. }
  104. @Handler
  105. public void handleChannelJoin(final ChannelJoinEvent event) {
  106. final String network = event.getChannel().getConnection().get().getNetwork();
  107. colourClient(network, event.getClient());
  108. }
  109. /**
  110. * Colours the specified client according to the user's config.
  111. *
  112. * @param network The network to use for the colouring
  113. * @param client The client to be coloured
  114. */
  115. private void colourClient(final String network, final GroupChatUser client) {
  116. final StringConverter sc = client.getUser().getConnection().getParser().get()
  117. .getStringConverter();
  118. final User myself = client.getUser().getConnection().getLocalUser().orElse(null);
  119. final String nickOption1 = sc.toLowerCase(network + ':' + client.getNickname());
  120. final String nickOption2 = sc.toLowerCase("*:" + client.getNickname());
  121. if (useowncolour && client.getUser().equals(myself)) {
  122. final Colour color = colourManager.getColourFromString(owncolour, null);
  123. putColour(client, color);
  124. } else if (userandomcolour) {
  125. putColour(client, getColour(client.getNickname()));
  126. }
  127. Color color = null;
  128. if (nickColours.containsKey(nickOption1)) {
  129. color = nickColours.get(nickOption1);
  130. } else if (nickColours.containsKey(nickOption2)) {
  131. color = nickColours.get(nickOption2);
  132. }
  133. if (color != null) {
  134. putColour(client, NickColourUtils.getColourfromColor(color));
  135. }
  136. }
  137. /**
  138. * Puts the specified colour into the given map. The keys are determined by config settings.
  139. *
  140. * @param user The map to colour
  141. * @param colour Text colour to be inserted
  142. */
  143. private void putColour(final GroupChatUser user, final Colour colour) {
  144. user.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, colour);
  145. }
  146. /**
  147. * Retrieves a pseudo-random colour for the specified nickname.
  148. *
  149. * @param nick The nickname of the client whose colour we're determining
  150. *
  151. * @return Colour of the specified nickname
  152. */
  153. private Colour getColour(final CharSequence nick) {
  154. int count = 0;
  155. for (int i = 0; i < nick.length(); i++) {
  156. count += nick.charAt(i);
  157. }
  158. count %= randColours.length;
  159. return colourManager.getColourFromString(randColours[count], null);
  160. }
  161. /**
  162. * Loads this plugin.
  163. */
  164. public void onLoad() {
  165. eventBus.subscribe(this);
  166. configBinder.bind(this, NickColourManager.class);
  167. nickColours = nickColourYamlStore.readNickColourEntries(path.resolve("nickcolours.yml"));
  168. }
  169. /**
  170. * Unloads this plugin.
  171. */
  172. public void onUnload() {
  173. eventBus.unsubscribe(this);
  174. configBinder.unbind(this);
  175. saveNickColourStore(nickColours);
  176. }
  177. public void saveNickColourStore(final Map<String, Color> savingNickColours) {
  178. nickColourYamlStore.writeNickColourEntries(path.resolve("nickcolours.yml"), savingNickColours);
  179. }
  180. @ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
  181. public void handleUseOwnColour(final boolean value) {
  182. useowncolour = value;
  183. }
  184. @ConfigBinding(key = "userandomcolour", invocation = EDTInvocation.class)
  185. public void handleUseRandomColour(final boolean value) {
  186. userandomcolour = value;
  187. }
  188. @ConfigBinding(key = "owncolour", invocation = EDTInvocation.class)
  189. public void handleOwnColour(final String value) {
  190. owncolour = value;
  191. }
  192. @ConfigBinding(key = "randomcolours", invocation = EDTInvocation.class)
  193. public void handleRandomColours(final List<String> value) {
  194. randColours = value.isEmpty() ? DEFAULT_RANDOM_COLOURS : value.toArray(new String[value.size()]);
  195. }
  196. @Handler
  197. public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
  198. final PreferencesCategory general =
  199. new PluginPreferencesCategory(pluginInfo, "Nick Colours",
  200. "General configuration for NickColour plugin.");
  201. final PreferencesCategory colours = new PluginPreferencesCategory(pluginInfo, "Colours",
  202. "Set colours for specific nicknames.", UIUtilities.invokeAndWait(
  203. () -> new NickColourPanel(mainWindowProvider.get(), iconManager,
  204. colourManager, this, nickColours)));
  205. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  206. "userandomcolour", "Use random colour",
  207. "Use a pseudo-random colour for each person?",
  208. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  209. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
  210. "useowncolour", "Use colour for own nick",
  211. "Always use the same colour for our own nickname?",
  212. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  213. general.addSetting(new PreferencesSetting(PreferencesType.COLOUR, pluginInfo.getDomain(),
  214. "owncolour", "Colour to use for own nick",
  215. "Colour used for our own nickname, if above setting is enabled.",
  216. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  217. general.addSubCategory(colours);
  218. event.getModel().getCategory("Plugins").addSubCategory(general);
  219. }
  220. }