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.

Query.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2006-2007 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;
  23. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import com.dmdirc.parser.ClientInfo;
  29. import com.dmdirc.parser.IRCParser;
  30. import com.dmdirc.parser.callbacks.CallbackManager;
  31. import com.dmdirc.parser.callbacks.CallbackNotFound;
  32. import com.dmdirc.parser.callbacks.interfaces.INickChanged;
  33. import com.dmdirc.parser.callbacks.interfaces.IPrivateAction;
  34. import com.dmdirc.parser.callbacks.interfaces.IPrivateMessage;
  35. import com.dmdirc.ui.interfaces.InputWindow;
  36. import com.dmdirc.ui.MainFrame;
  37. import com.dmdirc.ui.QueryFrame;
  38. import com.dmdirc.ui.input.TabCompleter;
  39. import java.net.URL;
  40. import javax.swing.ImageIcon;
  41. /**
  42. * The Query class represents the client's view of a query with another user.
  43. * It handles callbacks for query events from the parser, maintains the
  44. * corresponding ServerFrame, and handles user input to a ServerFrame.
  45. * @author chris
  46. */
  47. public final class Query extends FrameContainer implements IPrivateAction,
  48. IPrivateMessage, INickChanged {
  49. /** The Server this Query is on. */
  50. private Server server;
  51. /** The QueryFrame used for this Query. */
  52. private QueryFrame frame;
  53. /** The full host of the client associated with this Query. */
  54. private String host;
  55. /** The tab completer for the query frame. */
  56. private final TabCompleter tabCompleter;
  57. /**
  58. * Creates a new instance of Query.
  59. *
  60. * @param newHost host of the remove client
  61. * @param newServer The server object that this Query belongs to
  62. */
  63. public Query(final Server newServer, final String newHost) {
  64. super();
  65. this.server = newServer;
  66. this.host = newHost;
  67. final ClassLoader cldr = this.getClass().getClassLoader();
  68. final URL imageURL = cldr.getResource("com/dmdirc/res/query.png");
  69. imageIcon = new ImageIcon(imageURL);
  70. frame = new QueryFrame(this);
  71. ActionManager.processEvent(CoreActionType.QUERY_OPENED, null, this);
  72. MainFrame.getMainFrame().addChild(frame);
  73. frame.addInternalFrameListener(this);
  74. frame.setFrameIcon(imageIcon);
  75. if (!Config.getOptionBool("general", "hidequeries")) {
  76. frame.open();
  77. }
  78. tabCompleter = new TabCompleter(server.getTabCompleter());
  79. tabCompleter.addEntries(CommandManager.getQueryCommandNames());
  80. frame.getInputHandler().setTabCompleter(tabCompleter);
  81. reregister();
  82. updateTitle();
  83. }
  84. /**
  85. * Shows this query's frame.
  86. */
  87. public void show() {
  88. frame.open();
  89. }
  90. /**
  91. * Returns the internal frame belonging to this object.
  92. *
  93. * @return This object's internal frame
  94. */
  95. public InputWindow getFrame() {
  96. return frame;
  97. }
  98. /**
  99. * Returns the tab completer for this query.
  100. *
  101. * @return This query's tab completer
  102. */
  103. public TabCompleter getTabCompleter() {
  104. return tabCompleter;
  105. }
  106. /**
  107. * Sends a private message to the remote user.
  108. *
  109. * @param line message text to send
  110. */
  111. public void sendLine(final String line) {
  112. final ClientInfo client = server.getParser().getMyself();
  113. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  114. if (maxLineLength >= line.length()) {
  115. server.getParser().sendMessage(ClientInfo.parseHost(host), line);
  116. final StringBuffer buff = new StringBuffer("querySelfMessage");
  117. ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, line);
  118. frame.addLine(buff, client.getNickname(), client.getIdent(), client.getHost(), line);
  119. } else {
  120. sendLine(line.substring(0, maxLineLength));
  121. sendLine(line.substring(maxLineLength));
  122. }
  123. }
  124. /**
  125. * Sends a private action to the remote user.
  126. *
  127. * @param action action text to send
  128. */
  129. public void sendAction(final String action) {
  130. final ClientInfo client = server.getParser().getMyself();
  131. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  132. if (maxLineLength >= action.length() + 2) {
  133. server.getParser().sendAction(ClientInfo.parseHost(host), action);
  134. final StringBuffer buff = new StringBuffer("querySelfAction");
  135. ActionManager.processEvent(CoreActionType.QUERY_SELF_ACTION, buff, this, action);
  136. frame.addLine(buff, client.getNickname(), client.getIdent(), client.getHost(), action);
  137. } else {
  138. frame.addLine("actionTooLong", action.length());
  139. }
  140. }
  141. /**
  142. * Handles a private message event from the parser.
  143. *
  144. * @param parser Parser receiving the event
  145. * @param message message received
  146. * @param remoteHost remote user host
  147. */
  148. public void onPrivateMessage(final IRCParser parser, final String message,
  149. final String remoteHost) {
  150. final String[] parts = ClientInfo.parseHostFull(remoteHost);
  151. final StringBuffer buff = new StringBuffer("queryMessage");
  152. ActionManager.processEvent(CoreActionType.QUERY_MESSAGE, buff, this, message);
  153. frame.addLine(buff, parts[0], parts[1], parts[2], message);
  154. }
  155. /**
  156. * Handles a private action event from the parser.
  157. *
  158. * @param parser Parser receiving the event
  159. * @param message message received
  160. * @param remoteHost remote host
  161. */
  162. public void onPrivateAction(final IRCParser parser, final String message,
  163. final String remoteHost) {
  164. final String[] parts = ClientInfo.parseHostFull(host);
  165. final StringBuffer buff = new StringBuffer("queryAction");
  166. ActionManager.processEvent(CoreActionType.QUERY_ACTION, buff, this, message);
  167. frame.addLine(buff, parts[0], parts[1], parts[2], message);
  168. }
  169. /**
  170. * Updates the QueryFrame title.
  171. */
  172. private void updateTitle() {
  173. final String title = ClientInfo.parseHost(host);
  174. frame.setTitle(title);
  175. if (frame.isMaximum() && frame.equals(MainFrame.getMainFrame().getActiveFrame())) {
  176. MainFrame.getMainFrame().setTitle(MainFrame.getMainFrame().getTitlePrefix() + " - " + title);
  177. }
  178. }
  179. /**
  180. * Reregisters query callbacks. Called when reconnecting to the server.
  181. */
  182. public void reregister() {
  183. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  184. try {
  185. callbackManager.addCallback("onPrivateAction", this, ClientInfo.parseHost(host));
  186. callbackManager.addCallback("onPrivateMessage", this, ClientInfo.parseHost(host));
  187. callbackManager.addCallback("onNickChanged", this);
  188. } catch (CallbackNotFound ex) {
  189. Logger.error(ErrorLevel.ERROR, "Unable to get query events", ex);
  190. }
  191. }
  192. /** {@inheritDoc} */
  193. public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
  194. final String sOldNick) {
  195. if (sOldNick.equals(ClientInfo.parseHost(host))) {
  196. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  197. callbackManager.delCallback("onPrivateAction", this);
  198. callbackManager.delCallback("onPrivateMessage", this);
  199. try {
  200. callbackManager.addCallback("onPrivateAction", this, cClient.getNickname());
  201. callbackManager.addCallback("onPrivateMessage", this, cClient.getNickname());
  202. } catch (CallbackNotFound ex) {
  203. Logger.error(ErrorLevel.ERROR, "Unable to get query events", ex);
  204. }
  205. // TODO: Action hook!
  206. frame.addLine("queryNickChanged", sOldNick, cClient.getIdent(),
  207. cClient.getHost(), cClient.getNickname());
  208. host = cClient.getNickname() + "!" + cClient.getIdent() + "@" + cClient.getHost();
  209. updateTitle();
  210. }
  211. }
  212. /**
  213. * Returns the Server assocaited with this query.
  214. *
  215. * @return asscoaited Server
  216. */
  217. public Server getServer() {
  218. return server;
  219. }
  220. /**
  221. * Closes the query and associated frame.
  222. */
  223. public void close() {
  224. server.getParser().getCallbackManager().delCallback("onPrivateAction", this);
  225. server.getParser().getCallbackManager().delCallback("onPrivateMessage", this);
  226. server.getParser().getCallbackManager().delCallback("onNickChanged", this);
  227. ActionManager.processEvent(CoreActionType.QUERY_CLOSED, null, this);
  228. frame.setVisible(false);
  229. server.delQuery(host);
  230. MainFrame.getMainFrame().delChild(frame);
  231. frame = null;
  232. server = null;
  233. }
  234. /**
  235. * Returns this query's name.
  236. *
  237. * @return A string representation of this query (i.e., the user's name)
  238. */
  239. public String toString() {
  240. return ClientInfo.parseHost(host);
  241. }
  242. /**
  243. * Returns the host that this query is with.
  244. *
  245. * @return The full host that this query is with
  246. */
  247. public String getHost() {
  248. return host;
  249. }
  250. /** {@inheritDoc} */
  251. @Override
  252. public void activateFrame() {
  253. if (!frame.isVisible()) {
  254. show();
  255. }
  256. MainFrame.getMainFrame().setActiveFrame(frame);
  257. }
  258. }