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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.FrameContainer;
  25. import com.dmdirc.Query;
  26. import com.dmdirc.addons.ui_swing.MainFrame;
  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.components.statusbar.SwingStatusBar;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.config.ConfigChangeListener;
  33. import com.dmdirc.interfaces.config.IdentityController;
  34. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  35. import com.dmdirc.parser.interfaces.ChannelInfo;
  36. import com.dmdirc.parser.interfaces.ClientInfo;
  37. import com.dmdirc.plugins.PluginDomain;
  38. import java.util.HashMap;
  39. import java.util.Map;
  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. /** Main frame. */
  47. private final MainFrame mainFrame;
  48. /** Status bar we're adding to. */
  49. private final SwingStatusBar statusBar;
  50. /** Identity controller to read settings from. */
  51. private final IdentityController identityController;
  52. /** Plugin settings domain. */
  53. private final String domain;
  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 MainFrame mainFrame, final SwingStatusBar statusBar,
  64. final IdentityController identityController,
  65. @PluginDomain(WindowStatusPlugin.class) final String domain) {
  66. this.domain = domain;
  67. this.mainFrame = mainFrame;
  68. this.statusBar = statusBar;
  69. this.identityController = identityController;
  70. }
  71. /**
  72. * Loads the plugin.
  73. */
  74. public void onLoad() {
  75. panel = UIUtilities.invokeAndWait(new Callable<WindowStatusPanel>() {
  76. /** {@inheritDoc} */
  77. @Override
  78. public WindowStatusPanel call() {
  79. return new WindowStatusPanel();
  80. }
  81. });
  82. statusBar.addComponent(panel);
  83. mainFrame.addSelectionListener(this);
  84. identityController.getGlobalConfiguration().addChangeListener(domain, this);
  85. updateCache();
  86. }
  87. /**
  88. * Unloads the plugin.
  89. */
  90. public void onUnload() {
  91. mainFrame.removeSelectionListener(this);
  92. statusBar.removeComponent(panel);
  93. panel = null;
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public void selectionChanged(final TextFrame window) {
  98. if (window != null) {
  99. updateStatus(window.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. final TextFrame active = mainFrame.getActiveFrame();
  115. if (active != null) {
  116. updateStatus(active.getContainer());
  117. }
  118. }
  119. /**
  120. * Update the window status using a given FrameContainer as the active frame.
  121. *
  122. * @param current Window to use when adding status.
  123. */
  124. public void updateStatus(final FrameContainer current) {
  125. if (current == null) {
  126. return;
  127. }
  128. final StringBuffer textString = new StringBuffer();
  129. if (current instanceof Connection) {
  130. textString.append(((Connection) current).getAddress());
  131. } else if (current instanceof Channel) {
  132. final ChannelInfo chan = ((Channel) current).getChannelInfo();
  133. final Map<Integer, String> names = new HashMap<>();
  134. final Map<Integer, Integer> types = new HashMap<>();
  135. textString.append(chan.getName());
  136. textString.append(" - Nicks: ");
  137. textString.append(chan.getChannelClientCount());
  138. textString.append(" (");
  139. for (ChannelClientInfo client : chan.getChannelClients()) {
  140. String mode = client.getImportantModePrefix();
  141. final Integer im = client.getClient().getParser()
  142. .getChannelUserModes().indexOf(mode);
  143. if (!names.containsKey(im)) {
  144. if (mode.isEmpty()) {
  145. if (shownone) {
  146. mode = nonePrefix;
  147. } else {
  148. continue;
  149. }
  150. }
  151. names.put(im, mode);
  152. }
  153. Integer count = types.get(im);
  154. if (count == null) {
  155. count = Integer.valueOf(1);
  156. } else {
  157. count++;
  158. }
  159. types.put(im, count);
  160. }
  161. boolean isFirst = true;
  162. for (Map.Entry<Integer, Integer> entry : types.entrySet()) {
  163. if (isFirst) {
  164. isFirst = false;
  165. } else {
  166. textString.append(' ');
  167. }
  168. textString.append(names.get(entry.getKey()));
  169. textString.append(entry.getValue());
  170. }
  171. textString.append(')');
  172. } else if (current instanceof Query) {
  173. final Query frame = (Query) current;
  174. textString.append(frame.getHost());
  175. if (showname && frame.getConnection().getParser() != null) {
  176. final ClientInfo client = frame.getConnection().getParser()
  177. .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. } else {
  185. textString.append("???");
  186. }
  187. if (panel != null) {
  188. panel.setText(textString.toString());
  189. }
  190. }
  191. /** {@inheritDoc} */
  192. @Override
  193. public void configChanged(final String domain, final String key) {
  194. updateCache();
  195. }
  196. }