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.5KB

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