Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

WindowStatusManager.java 7.9KB

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