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

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