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

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