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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2014 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.windowstatus;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.Query;
  27. import com.dmdirc.addons.ui_swing.injection.EdtHandlerInvocation;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  30. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  31. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  32. import com.dmdirc.events.StatusBarComponentAddedEvent;
  33. import com.dmdirc.events.StatusBarComponentRemovedEvent;
  34. import com.dmdirc.interfaces.Connection;
  35. import com.dmdirc.interfaces.config.ConfigChangeListener;
  36. import com.dmdirc.interfaces.config.IdentityController;
  37. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  38. import com.dmdirc.parser.interfaces.ChannelInfo;
  39. import com.dmdirc.parser.interfaces.ClientInfo;
  40. import com.dmdirc.plugins.PluginDomain;
  41. import java.util.Optional;
  42. import javax.inject.Inject;
  43. import net.engio.mbassy.listener.Handler;
  44. /**
  45. * Displays information related to the current window in the status bar.
  46. */
  47. public class WindowStatusManager implements ConfigChangeListener {
  48. /** Active frame manager. */
  49. private final ActiveFrameManager activeFrameManager;
  50. /** Identity controller to read settings from. */
  51. private final IdentityController identityController;
  52. /** Plugin settings domain. */
  53. private final String domain;
  54. /** The event bus to post events to. */
  55. private final DMDircMBassador eventBus;
  56. /** The swing event bus to register for events on. */
  57. private final SwingEventBus swingEventBus;
  58. /** The panel we use in the status bar. */
  59. private WindowStatusPanel panel;
  60. /** Should we show the real name in queries? */
  61. private boolean showname;
  62. /** Should we show users without modes? */
  63. private boolean shownone;
  64. /** Prefix for users without modes. */
  65. private String nonePrefix;
  66. @Inject
  67. public WindowStatusManager(final ActiveFrameManager activeFrameManager,
  68. final IdentityController identityController,
  69. @PluginDomain(WindowStatusPlugin.class) final String domain,
  70. final DMDircMBassador eventBus,
  71. final SwingEventBus swingEventBus) {
  72. this.domain = domain;
  73. this.activeFrameManager = activeFrameManager;
  74. this.identityController = identityController;
  75. this.eventBus = eventBus;
  76. this.swingEventBus = swingEventBus;
  77. }
  78. /**
  79. * Loads the plugin.
  80. */
  81. public void onLoad() {
  82. panel = UIUtilities.invokeAndWait(WindowStatusPanel::new);
  83. eventBus.publishAsync(new StatusBarComponentAddedEvent(panel));
  84. swingEventBus.subscribe(this);
  85. identityController.getGlobalConfiguration().addChangeListener(domain, this);
  86. updateCache();
  87. }
  88. /**
  89. * Unloads the plugin.
  90. */
  91. public void onUnload() {
  92. swingEventBus.unsubscribe(this);
  93. eventBus.publishAsync(new StatusBarComponentRemovedEvent(panel));
  94. panel = null;
  95. }
  96. @Handler(invocation = EdtHandlerInvocation.class)
  97. public void selectionChanged(final SwingWindowSelectedEvent event) {
  98. if (event.getWindow().isPresent()) {
  99. updateStatus(event.getWindow().get().getContainer());
  100. }
  101. }
  102. /** Updates the cached config settings. */
  103. private void updateCache() {
  104. showname = identityController.getGlobalConfiguration()
  105. .getOptionBool(domain, "client.showname");
  106. shownone = identityController.getGlobalConfiguration()
  107. .getOptionBool(domain, "channel.shownone");
  108. nonePrefix = identityController.getGlobalConfiguration()
  109. .getOption(domain, "channel.noneprefix");
  110. updateStatus();
  111. }
  112. /** Update the window status using the current active window. */
  113. public void updateStatus() {
  114. activeFrameManager.getActiveFrame().ifPresent(c -> updateStatus(c.getContainer()));
  115. }
  116. /**
  117. * Update the window status using a given FrameContainer as the active frame.
  118. *
  119. * @param current Window to use when adding status.
  120. */
  121. public void updateStatus(final FrameContainer current) {
  122. if (current == null) {
  123. return;
  124. }
  125. final String textString;
  126. if (current instanceof Connection) {
  127. textString = updateStatusConnection((Connection) current);
  128. } else if (current instanceof Channel) {
  129. textString = updateStatusChannel((Channel) current);
  130. } else if (current instanceof Query) {
  131. textString = updateStatusQuery((Query) current);
  132. } else {
  133. textString = "???";
  134. }
  135. if (panel != null) {
  136. panel.setText(textString);
  137. }
  138. }
  139. private String updateStatusConnection(final Connection connection) {
  140. return connection.getAddress();
  141. }
  142. private String updateStatusChannel(final Channel frame) {
  143. final StringBuilder textString = new StringBuilder();
  144. final ChannelInfo chan = frame.getChannelInfo();
  145. textString.append(chan.getName());
  146. textString.append(" - Nicks: ");
  147. textString.append(chan.getChannelClientCount());
  148. textString.append(" (");
  149. final String channelUserModes = ' ' + chan.getParser().getChannelUserModes();
  150. final int[] usersWithMode = new int[channelUserModes.length()];
  151. for (ChannelClientInfo client : chan.getChannelClients()) {
  152. final String mode = client.getImportantModePrefix();
  153. final int index = channelUserModes.indexOf(mode);
  154. usersWithMode[index]++;
  155. }
  156. boolean isFirst = true;
  157. for (int i = channelUserModes.length() - 1; i >= 0; i--) {
  158. final int count = usersWithMode[i];
  159. if (count > 0 && (shownone || i > 0)) {
  160. if (!isFirst) {
  161. textString.append(' ');
  162. }
  163. final String name = i > 0 ?
  164. Character.toString(channelUserModes.charAt(i)) : nonePrefix;
  165. textString.append(name).append(count);
  166. isFirst = false;
  167. }
  168. }
  169. textString.append(')');
  170. return textString.toString();
  171. }
  172. private String updateStatusQuery(final Query frame) {
  173. final StringBuilder textString = new StringBuilder();
  174. textString.append(frame.getHost());
  175. final Optional<Connection> connection = frame.getOptionalConnection();
  176. if (showname && connection.isPresent()) {
  177. final ClientInfo client = connection.get().getParser().getClient(frame.getHost());
  178. final String realname = client.getRealname();
  179. if (realname != null && !realname.isEmpty()) {
  180. textString.append(" - ");
  181. textString.append(client.getRealname());
  182. }
  183. }
  184. return textString.toString();
  185. }
  186. @Override
  187. public void configChanged(final String domain, final String key) {
  188. updateCache();
  189. }
  190. }