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.

NickColourPlugin.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2006-2013 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.actions.ActionManager;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.addons.ui_swing.SwingController;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.config.ConfigManager;
  30. import com.dmdirc.config.IdentityManager;
  31. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  32. import com.dmdirc.config.prefs.PreferencesCategory;
  33. import com.dmdirc.config.prefs.PreferencesDialogModel;
  34. import com.dmdirc.config.prefs.PreferencesSetting;
  35. import com.dmdirc.config.prefs.PreferencesType;
  36. import com.dmdirc.interfaces.ActionListener;
  37. import com.dmdirc.interfaces.ConfigChangeListener;
  38. import com.dmdirc.interfaces.actions.ActionType;
  39. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  40. import com.dmdirc.parser.interfaces.ChannelInfo;
  41. import com.dmdirc.parser.interfaces.ClientInfo;
  42. import com.dmdirc.plugins.BasePlugin;
  43. import com.dmdirc.plugins.PluginInfo;
  44. import com.dmdirc.ui.Colour;
  45. import com.dmdirc.ui.messages.ColourManager;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.concurrent.Callable;
  50. /**
  51. * Provides various features related to nickname colouring.
  52. */
  53. public final class NickColourPlugin extends BasePlugin implements ActionListener,
  54. ConfigChangeListener {
  55. /** "Random" colours to use to colour nicknames. */
  56. private String[] randColours = new String[]{
  57. "E90E7F", "8E55E9", "B30E0E", "18B33C",
  58. "58ADB3", "9E54B3", "B39875", "3176B3", };
  59. private boolean useowncolour;
  60. private String owncolour;
  61. private boolean userandomcolour;
  62. private boolean settext;
  63. private boolean setnicklist;
  64. /** This plugin's plugin info. */
  65. private final PluginInfo pluginInfo;
  66. /**
  67. * Creates a new instance of this plugin.
  68. *
  69. * @param pluginInfo This plugin's plugin info
  70. */
  71. public NickColourPlugin(final PluginInfo pluginInfo) {
  72. super();
  73. this.pluginInfo = pluginInfo;
  74. }
  75. /**
  76. * Get our PluginInfo
  77. *
  78. * @return our PluginInfo
  79. */
  80. public PluginInfo getPluginInfo() {
  81. return pluginInfo;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public void processEvent(final ActionType type, final StringBuffer format,
  86. final Object... arguments) {
  87. if (type.equals(CoreActionType.CHANNEL_GOTNAMES)) {
  88. final ChannelInfo chanInfo =
  89. ((Channel) arguments[0]).getChannelInfo();
  90. final String network = ((Channel) arguments[0]).getServer().
  91. getNetwork();
  92. for (ChannelClientInfo client : chanInfo.getChannelClients()) {
  93. colourClient(network, client);
  94. }
  95. } else if (type.equals(CoreActionType.CHANNEL_JOIN)) {
  96. final String network = ((Channel) arguments[0]).getServer().
  97. getNetwork();
  98. colourClient(network, (ChannelClientInfo) arguments[1]);
  99. }
  100. }
  101. /**
  102. * Colours the specified client according to the user's config.
  103. *
  104. * @param network The network to use for the colouring
  105. * @param client The client to be coloured
  106. */
  107. private void colourClient(final String network,
  108. final ChannelClientInfo client) {
  109. final Map<Object, Object> map = client.getMap();
  110. final ClientInfo myself =
  111. client.getClient().getParser().getLocalClient();
  112. final String nickOption1 = "color:"
  113. + client.getClient().getParser().getStringConverter().
  114. toLowerCase(network + ":" + client.getClient().getNickname());
  115. final String nickOption2 = "color:"
  116. + client.getClient().getParser().getStringConverter().
  117. toLowerCase("*:" + client.getClient().getNickname());
  118. if (useowncolour && client.getClient().equals(myself)) {
  119. final Colour color = ColourManager.parseColour(owncolour);
  120. putColour(map, color, color);
  121. } else if (userandomcolour) {
  122. putColour(map, getColour(client.getClient().getNickname()), getColour(client.
  123. getClient().getNickname()));
  124. }
  125. String[] parts = null;
  126. if (IdentityManager.getIdentityManager().getGlobalConfiguration()
  127. .hasOptionString(getDomain(), nickOption1)) {
  128. parts = getParts(nickOption1);
  129. } else if (IdentityManager.getIdentityManager().getGlobalConfiguration().hasOptionString(
  130. getDomain(), nickOption2)) {
  131. parts = getParts(nickOption2);
  132. }
  133. if (parts != null) {
  134. Colour textColor = null;
  135. Colour nickColor = null;
  136. if (parts[0] != null) {
  137. textColor = ColourManager.parseColour(parts[0], null);
  138. }
  139. if (parts[1] != null) {
  140. nickColor = ColourManager.parseColour(parts[1], null);
  141. }
  142. putColour(map, textColor, nickColor);
  143. }
  144. }
  145. /**
  146. * Puts the specified colour into the given map. The keys are determined
  147. * by config settings.
  148. *
  149. * @param map The map to use
  150. * @param textColour Text colour to be inserted
  151. * @param nickColour Nick colour to be inserted
  152. */
  153. private void putColour(final Map<Object, Object> map, final Colour textColour,
  154. final Colour nickColour) {
  155. if (settext && textColour != null) {
  156. map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
  157. }
  158. if (setnicklist && nickColour != null) {
  159. map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
  160. }
  161. }
  162. /**
  163. * Retrieves a pseudo-random colour for the specified nickname.
  164. *
  165. * @param nick The nickname of the client whose colour we're determining
  166. * @return Colour of the specified nickname
  167. */
  168. private Colour getColour(final String nick) {
  169. int count = 0;
  170. for (int i = 0; i < nick.length(); i++) {
  171. count += nick.charAt(i);
  172. }
  173. count %= randColours.length;
  174. return ColourManager.parseColour(randColours[count]);
  175. }
  176. /**
  177. * Reads the nick colour data from the config.
  178. *
  179. * @return A multi-dimensional array of nick colour info.
  180. */
  181. public Object[][] getData() {
  182. final List<Object[]> data = new ArrayList<Object[]>();
  183. for (String key : IdentityManager.getIdentityManager().getGlobalConfiguration()
  184. .getOptions(getDomain()).keySet()) {
  185. if (key.startsWith("color:")) {
  186. final String network = key.substring(6, key.indexOf(':', 6));
  187. final String user = key.substring(1 + key.indexOf(':', 6));
  188. final String[] parts = getParts(key);
  189. data.add(new Object[]{network, user, parts[0], parts[1]});
  190. }
  191. }
  192. final Object[][] res = new Object[data.size()][4];
  193. int i = 0;
  194. for (Object[] row : data) {
  195. res[i] = row;
  196. i++;
  197. }
  198. return res;
  199. }
  200. /**
  201. * Retrieves the config option with the specified key, and returns an
  202. * array of the colours that should be used for it.
  203. *
  204. * @param key The config key to look up
  205. * @return The colours specified by the given key
  206. */
  207. private String[] getParts(final String key) {
  208. String[] parts = IdentityManager.getIdentityManager().getGlobalConfiguration()
  209. .getOption(getDomain(), key).split(":");
  210. if (parts.length == 0) {
  211. parts = new String[]{null, null};
  212. } else if (parts.length == 1) {
  213. parts = new String[]{parts[0], null};
  214. }
  215. return parts;
  216. }
  217. /** {@inheritDoc} */
  218. @Override
  219. public void onLoad() {
  220. setCachedSettings();
  221. ActionManager.getActionManager().registerListener(this,
  222. CoreActionType.CHANNEL_GOTNAMES, CoreActionType.CHANNEL_JOIN);
  223. }
  224. /** {@inheritDoc} */
  225. @Override
  226. public void onUnload() {
  227. ActionManager.getActionManager().unregisterListener(this);
  228. }
  229. /** {@inheritDoc} */
  230. @Override
  231. public void showConfig(final PreferencesDialogModel manager) {
  232. final PreferencesCategory general = new PluginPreferencesCategory(
  233. pluginInfo, "Nick Colours",
  234. "General configuration for NickColour plugin.");
  235. final PreferencesCategory colours = new PluginPreferencesCategory(
  236. pluginInfo, "Colours",
  237. "Set colours for specific nicknames.", UIUtilities.invokeAndWait(
  238. new Callable<NickColourPanel>() {
  239. /** {@inheritDoc} */
  240. @Override
  241. public NickColourPanel call() {
  242. return new NickColourPanel(
  243. (SwingController) pluginInfo.getMetaData().getManager()
  244. .getPluginInfoByName("ui_swing").getPlugin(),
  245. NickColourPlugin.this);
  246. }
  247. }));
  248. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  249. "ui", "shownickcoloursintext", "Show colours in text area",
  250. "Colour nicknames in main text area?",
  251. manager.getConfigManager(), manager.getIdentity()));
  252. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  253. "ui", "shownickcoloursinnicklist", "Show colours in"
  254. + " nick list", "Colour nicknames in channel nick lists?",
  255. manager.getConfigManager(), manager.getIdentity()));
  256. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  257. getDomain(), "settext", "Set colours in textarea",
  258. "Should the plugin set the textarea colour of nicks?",
  259. manager.getConfigManager(), manager.getIdentity()));
  260. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  261. getDomain(), "setnicklist", "Set colours in nick list",
  262. "Should the plugin set the nick list colour of nicks?",
  263. manager.getConfigManager(), manager.getIdentity()));
  264. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  265. getDomain(), "userandomcolour", "Use random colour",
  266. "Use a pseudo-random colour for each person?",
  267. manager.getConfigManager(), manager.getIdentity()));
  268. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  269. getDomain(), "useowncolour", "Use colour for own nick",
  270. "Always use the same colour for our own nickname?",
  271. manager.getConfigManager(), manager.getIdentity()));
  272. general.addSetting(
  273. new PreferencesSetting(PreferencesType.COLOUR, getDomain(),
  274. "owncolour", "Colour to use for own nick",
  275. "Colour used for our own nickname, if above setting is "
  276. + "enabled.", manager.getConfigManager(), manager.getIdentity()));
  277. general.addSubCategory(colours);
  278. manager.getCategory("Plugins").addSubCategory(general);
  279. }
  280. /**
  281. * Updates cached settings.
  282. */
  283. private void setCachedSettings() {
  284. final ConfigManager config = IdentityManager.getIdentityManager()
  285. .getGlobalConfiguration();
  286. useowncolour = config.getOptionBool(getDomain(), "useowncolour");
  287. owncolour = config.getOption(getDomain(), "owncolour");
  288. userandomcolour = config.getOptionBool(getDomain(), "userandomcolour");
  289. settext = config.getOptionBool(getDomain(), "settext");
  290. setnicklist = config.getOptionBool(getDomain(), "setnicklist");
  291. if (config.hasOptionString(getDomain(),
  292. "randomcolours")) {
  293. randColours = config.getOptionList(getDomain(), "randomcolours")
  294. .toArray(new String[0]);
  295. }
  296. }
  297. /** {@inheritDoc} */
  298. @Override
  299. public void configChanged(final String domain, final String key) {
  300. setCachedSettings();
  301. }
  302. }