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

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