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.

WindowStatusPlugin.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Server;
  27. import com.dmdirc.actions.ActionManager;
  28. import com.dmdirc.actions.CoreActionType;
  29. import com.dmdirc.actions.interfaces.ActionType;
  30. import com.dmdirc.addons.ui_swing.SwingController;
  31. import com.dmdirc.addons.ui_swing.UIUtilities;
  32. import com.dmdirc.config.IdentityManager;
  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.interfaces.ActionListener;
  39. import com.dmdirc.interfaces.ConfigChangeListener;
  40. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  41. import com.dmdirc.parser.interfaces.ChannelInfo;
  42. import com.dmdirc.parser.interfaces.ClientInfo;
  43. import com.dmdirc.plugins.Plugin;
  44. import com.dmdirc.plugins.PluginManager;
  45. import com.dmdirc.ui.WindowManager;
  46. import com.dmdirc.util.ReturnableThread;
  47. import java.util.HashMap;
  48. import java.util.Map;
  49. import java.util.Map.Entry;
  50. /**
  51. * Displays information related to the current window in the status bar.
  52. *
  53. * @author Shane 'Dataforce' McCormack
  54. */
  55. public final class WindowStatusPlugin extends Plugin implements ActionListener, ConfigChangeListener {
  56. /** The panel we use in the status bar. */
  57. private final WindowStatusPanel panel;
  58. private boolean showname, shownone;
  59. private String nonePrefix;
  60. /** Creates a new instance of WindowStatusPlugin. */
  61. public WindowStatusPlugin() {
  62. super();
  63. panel = UIUtilities.invokeAndWait(
  64. new ReturnableThread<WindowStatusPanel>() {
  65. /** {@inheritDoc} */
  66. @Override
  67. public void run() {
  68. setObject(new WindowStatusPanel());
  69. }
  70. });
  71. }
  72. /**
  73. * Called when the plugin is loaded.
  74. */
  75. @Override
  76. public void onLoad() {
  77. ((SwingController) PluginManager.getPluginManager()
  78. .getPluginInfoByName("ui_swing").getPlugin())
  79. .getSwingStatusBar().addComponent(panel);
  80. IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
  81. updateCache();
  82. ActionManager.addListener(this, CoreActionType.CLIENT_FRAME_CHANGED);
  83. }
  84. /**
  85. * Called when this plugin is unloaded.
  86. */
  87. @Override
  88. public void onUnload() {
  89. ((SwingController) PluginManager.getPluginManager()
  90. .getPluginInfoByName("ui_swing").getPlugin())
  91. .getSwingStatusBar().removeComponent(panel);
  92. ActionManager.removeListener(this);
  93. }
  94. /**
  95. * Process an event of the specified type.
  96. *
  97. * @param type The type of the event to process
  98. * @param format Format of messages that are about to be sent. (May be null)
  99. * @param arguments The arguments for the event
  100. */
  101. @Override
  102. public void processEvent(final ActionType type, final StringBuffer format, final Object... arguments) {
  103. if (type.equals(CoreActionType.CLIENT_FRAME_CHANGED)) {
  104. updateStatus((FrameContainer<?>) arguments[0]);
  105. }
  106. }
  107. private void updateCache() {
  108. showname = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "client.showname");
  109. shownone = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "channel.shownone");
  110. nonePrefix = IdentityManager.getGlobalConfig().getOption(getDomain(), "channel.noneprefix");
  111. updateStatus();
  112. }
  113. /**
  114. * Update the window status using the current active window.
  115. */
  116. public void updateStatus() {
  117. final FrameContainer<?> active = WindowManager.getActiveWindow();
  118. if (active != null) {
  119. updateStatus(active);
  120. }
  121. }
  122. /**
  123. * Update the window status using a given FrameContainer as the active frame.
  124. *
  125. * @param current Window to use when adding status.
  126. */
  127. public void updateStatus(final FrameContainer<?> current) {
  128. if (current == null) {
  129. return;
  130. }
  131. final StringBuffer textString = new StringBuffer();
  132. if (current instanceof Server) {
  133. final Server frame = (Server) current;
  134. textString.append(frame.getName());
  135. } else if (current instanceof Channel) {
  136. final Channel frame = (Channel) current;
  137. final ChannelInfo chan = frame.getChannelInfo();
  138. final Map<Integer, String> names = new HashMap<Integer, String>();
  139. final Map<Integer, Integer> types = new HashMap<Integer, Integer>();
  140. textString.append(chan.getName());
  141. textString.append(" - Nicks: " + chan.getChannelClientCount() + " (");
  142. for (ChannelClientInfo client : chan.getChannelClients()) {
  143. String mode = client.getImportantModePrefix();
  144. final Integer im = client.getClient().getParser().getChannelUserModes().indexOf(mode);
  145. if (!names.containsKey(im)) {
  146. if (mode.isEmpty()) {
  147. if (shownone) {
  148. mode = nonePrefix;
  149. } else {
  150. continue;
  151. }
  152. }
  153. names.put(im, mode);
  154. }
  155. Integer count = types.get(im);
  156. if (count == null) {
  157. count = Integer.valueOf(1);
  158. } else {
  159. count++;
  160. }
  161. types.put(im, count);
  162. }
  163. boolean isFirst = true;
  164. for (Entry<Integer, Integer> entry : types.entrySet()) {
  165. if (isFirst) {
  166. isFirst = false;
  167. } else {
  168. textString.append(' ');
  169. }
  170. textString.append(names.get(entry.getKey()));
  171. textString.append(entry.getValue());
  172. }
  173. textString.append(')');
  174. } else if (current instanceof Query) {
  175. final Query frame = (Query) current;
  176. textString.append(frame.getHost());
  177. if (showname && frame.getServer().getParser() != null) {
  178. final ClientInfo client = frame.getServer().getParser().getClient(frame.getHost());
  179. final String realname = client.getRealname();
  180. if (!realname.isEmpty()) {
  181. textString.append(" - ");
  182. textString.append(client.getRealname());
  183. }
  184. }
  185. } else {
  186. textString.append("???");
  187. }
  188. panel.setText(textString.toString());
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. public void showConfig(final PreferencesDialogModel manager) {
  193. final PreferencesCategory category = new PluginPreferencesCategory(
  194. getPluginInfo(), "Window status", "");
  195. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  196. getDomain(), "channel.shownone", "Show 'none' count",
  197. "Should the count for users with no state be shown?"));
  198. category.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  199. getDomain(), "channel.noneprefix", "'None' count prefix",
  200. "The Prefix to use when showing the 'none' count"));
  201. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  202. getDomain(), "client.showname", "Show real name",
  203. "Should the realname for clients be shown if known?"));
  204. manager.getCategory("Plugins").addSubCategory(category);
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public void configChanged(final String domain, final String key) {
  209. updateCache();
  210. }
  211. }