Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WindowStatusPlugin.java 8.2KB

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