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.

WindowStatusManager.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.windowstatus;
  18. import com.dmdirc.addons.ui_swing.EDTInvocation;
  19. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  20. import com.dmdirc.addons.ui_swing.UIUtilities;
  21. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  22. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  23. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  24. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  25. import com.dmdirc.config.binding.ConfigBinder;
  26. import com.dmdirc.config.binding.ConfigBinding;
  27. import com.dmdirc.config.GlobalConfig;
  28. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesCategory;
  30. import com.dmdirc.config.prefs.PreferencesDialogModel;
  31. import com.dmdirc.config.prefs.PreferencesSetting;
  32. import com.dmdirc.config.prefs.PreferencesType;
  33. import com.dmdirc.events.ChannelUserModeChangeEvent;
  34. import com.dmdirc.events.ClientPrefsOpenedEvent;
  35. import com.dmdirc.events.StatusBarComponentAddedEvent;
  36. import com.dmdirc.events.StatusBarComponentRemovedEvent;
  37. import com.dmdirc.interfaces.Connection;
  38. import com.dmdirc.events.eventbus.EventBus;
  39. import com.dmdirc.interfaces.GroupChat;
  40. import com.dmdirc.interfaces.PrivateChat;
  41. import com.dmdirc.interfaces.WindowModel;
  42. import com.dmdirc.config.provider.AggregateConfigProvider;
  43. import com.dmdirc.plugins.PluginDomain;
  44. import com.dmdirc.plugins.PluginInfo;
  45. import javax.inject.Inject;
  46. import net.engio.mbassy.listener.Handler;
  47. /**
  48. * Displays information related to the current window in the status bar.
  49. */
  50. public class WindowStatusManager {
  51. /** Active frame manager. */
  52. private final ActiveFrameManager activeFrameManager;
  53. private final PluginInfo pluginInfo;
  54. /** Config to read settings from. */
  55. private final ConfigBinder configBinder;
  56. /** The event bus to post events to. */
  57. private final EventBus eventBus;
  58. /** The swing event bus to register for events on. */
  59. private final SwingEventBus swingEventBus;
  60. /** The panel we use in the status bar. */
  61. private WindowStatusPanel panel;
  62. /** Should we show the real name in queries? */
  63. private boolean showname;
  64. /** Should we show users without modes? */
  65. private boolean shownone;
  66. /** Prefix for users without modes. */
  67. private String nonePrefix;
  68. @Inject
  69. public WindowStatusManager(final ActiveFrameManager activeFrameManager,
  70. @GlobalConfig final AggregateConfigProvider config,
  71. @PluginDomain(WindowStatusPlugin.class) final String domain,
  72. final EventBus eventBus,
  73. final SwingEventBus swingEventBus,
  74. @PluginDomain(WindowStatusPlugin.class) final PluginInfo pluginInfo) {
  75. this.activeFrameManager = activeFrameManager;
  76. this.pluginInfo = pluginInfo;
  77. this.configBinder = config.getBinder().withDefaultDomain(domain);
  78. this.eventBus = eventBus;
  79. this.swingEventBus = swingEventBus;
  80. }
  81. /**
  82. * Loads the plugin.
  83. */
  84. public void onLoad() {
  85. panel = UIUtilities.invokeAndWait(WindowStatusPanel::new);
  86. eventBus.publishAsync(new StatusBarComponentAddedEvent(panel));
  87. swingEventBus.subscribe(this);
  88. eventBus.subscribe(this);
  89. configBinder.bind(this, WindowStatusManager.class);
  90. UIUtilities.invokeLater(this::updateStatus);
  91. }
  92. /**
  93. * Unloads the plugin.
  94. */
  95. public void onUnload() {
  96. swingEventBus.unsubscribe(this);
  97. eventBus.unsubscribe(this);
  98. eventBus.publishAsync(new StatusBarComponentRemovedEvent(panel));
  99. configBinder.unbind(this);
  100. panel = null;
  101. }
  102. @Handler(invocation = EdtHandlerInvocation.class)
  103. public void selectionChanged(final SwingWindowSelectedEvent event) {
  104. event.getWindow().map(TextFrame::getContainer).ifPresent(this::updateStatus);
  105. }
  106. @Handler
  107. public void usermodeChange(final ChannelUserModeChangeEvent event) {
  108. updateStatus();
  109. }
  110. /** Update the window status using the current active window. */
  111. public void updateStatus() {
  112. activeFrameManager.getActiveFrame().ifPresent(c -> updateStatus(c.getContainer()));
  113. }
  114. /**
  115. * Update the window status using a given FrameContainer as the active frame.
  116. *
  117. * @param current Window to use when adding status.
  118. */
  119. public void updateStatus(final WindowModel current) {
  120. if (current == null) {
  121. return;
  122. }
  123. if (panel == null) {
  124. return;
  125. }
  126. final String textString;
  127. if (current instanceof Connection) {
  128. textString = updateStatusConnection((Connection) current);
  129. } else if (current instanceof GroupChat) {
  130. textString = updateStatusChannel((GroupChat) current);
  131. } else if (current instanceof PrivateChat) {
  132. textString = updateStatusQuery((PrivateChat) current);
  133. } else {
  134. textString = "???";
  135. }
  136. panel.setText(textString);
  137. }
  138. private String updateStatusConnection(final Connection connection) {
  139. return connection.getAddress();
  140. }
  141. private String updateStatusChannel(final GroupChat frame) {
  142. final StringBuilder textString = new StringBuilder();
  143. textString.append(frame.getName());
  144. textString.append(" - Nicks: ");
  145. textString.append(frame.getUsers().size());
  146. textString.append(" (");
  147. final String channelUserModes = ' ' + frame.getConnection()
  148. .map(Connection::getUserModes).orElse("");
  149. final int[] usersWithMode = new int[channelUserModes.length()];
  150. frame.getUsers().forEach(user -> {
  151. final String mode = user.getImportantMode();
  152. final int index = channelUserModes.indexOf(mode);
  153. usersWithMode[index]++;
  154. });
  155. boolean isFirst = true;
  156. for (int i = channelUserModes.length() - 1; i >= 0; i--) {
  157. final int count = usersWithMode[i];
  158. if (count > 0 && (shownone || i > 0)) {
  159. if (!isFirst) {
  160. textString.append(' ');
  161. }
  162. final String name = i > 0 ? Character.toString(channelUserModes.charAt(i)) : nonePrefix;
  163. textString.append(name).append(count);
  164. isFirst = false;
  165. }
  166. }
  167. textString.append(')');
  168. return textString.toString();
  169. }
  170. private String updateStatusQuery(final PrivateChat frame) {
  171. final StringBuilder textString = new StringBuilder();
  172. textString
  173. .append(frame.getUser().getNickname())
  174. .append('!').append(frame.getUser().getRealname().orElse(""))
  175. .append('@').append(frame.getUser().getHostname().orElse(""));
  176. frame.getConnection().ifPresent(c -> {
  177. if (showname) {
  178. frame.getUser().getRealname().ifPresent(s -> textString.append(" - ").append(s));
  179. }
  180. });
  181. return textString.toString();
  182. }
  183. @ConfigBinding(key = "client.showname", invocation = EDTInvocation.class)
  184. public void handleShowName(final String value) {
  185. showname = Boolean.valueOf(value);
  186. updateStatus();
  187. }
  188. @ConfigBinding(key = "channel.shownone", invocation = EDTInvocation.class)
  189. public void handleShowNone(final String value) {
  190. shownone = Boolean.valueOf(value);
  191. updateStatus();
  192. }
  193. @ConfigBinding(key = "channel.noneprefix", invocation = EDTInvocation.class)
  194. public void handleShowPrefix(final String value) {
  195. nonePrefix = value;
  196. updateStatus();
  197. }
  198. @Handler
  199. public void showConfig(final ClientPrefsOpenedEvent event) {
  200. final PreferencesDialogModel manager = event.getModel();
  201. final PreferencesCategory category = new PluginPreferencesCategory(
  202. pluginInfo, "Window status", "");
  203. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  204. pluginInfo.getDomain(), "channel.shownone", "Show 'none' count",
  205. "Should the count for users with no state be shown?",
  206. manager.getConfigManager(), manager.getIdentity()));
  207. category.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  208. pluginInfo.getDomain(), "channel.noneprefix", "'None' count prefix",
  209. "The Prefix to use when showing the 'none' count",
  210. manager.getConfigManager(), manager.getIdentity()));
  211. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  212. pluginInfo.getDomain(), "client.showname", "Show real name",
  213. "Should the realname for clients be shown if known?",
  214. manager.getConfigManager(), manager.getIdentity()));
  215. manager.getCategory("Plugins").addSubCategory(category);
  216. }
  217. }