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.

Query.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 WritableFrameContainer implements
  48. IPrivateAction, 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.setFrameIcon(imageIcon);
  74. if (!Config.getOptionBool("general", "hidequeries")) {
  75. frame.open();
  76. }
  77. tabCompleter = new TabCompleter(server.getTabCompleter());
  78. tabCompleter.addEntries(CommandManager.getQueryCommandNames());
  79. frame.getInputHandler().setTabCompleter(tabCompleter);
  80. reregister();
  81. updateTitle();
  82. }
  83. /**
  84. * Shows this query's frame.
  85. */
  86. public void show() {
  87. frame.open();
  88. }
  89. /**
  90. * Returns the internal frame belonging to this object.
  91. *
  92. * @return This object's internal frame
  93. */
  94. public InputWindow getFrame() {
  95. return frame;
  96. }
  97. /**
  98. * Returns the tab completer for this query.
  99. *
  100. * @return This query's tab completer
  101. */
  102. public TabCompleter getTabCompleter() {
  103. return tabCompleter;
  104. }
  105. /** {@inheritDoc} */
  106. public void sendLine(final String line) {
  107. final ClientInfo client = server.getParser().getMyself();
  108. if (line.length() <= getMaxLineLength()) {
  109. server.getParser().sendMessage(ClientInfo.parseHost(host), line);
  110. final StringBuffer buff = new StringBuffer("querySelfMessage");
  111. ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, line);
  112. frame.addLine(buff, client.getNickname(), client.getIdent(), client.getHost(), line);
  113. } else {
  114. sendLine(line.substring(0, getMaxLineLength()));
  115. sendLine(line.substring(getMaxLineLength()));
  116. }
  117. }
  118. /** {@inheritDoc} */
  119. public int getMaxLineLength() {
  120. return server.getParser().getMaxLength("PRIVMSG", host);
  121. }
  122. /**
  123. * Sends a private action to the remote user.
  124. *
  125. * @param action action text to send
  126. */
  127. public void sendAction(final String action) {
  128. final ClientInfo client = server.getParser().getMyself();
  129. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  130. if (maxLineLength >= action.length() + 2) {
  131. server.getParser().sendAction(ClientInfo.parseHost(host), action);
  132. final StringBuffer buff = new StringBuffer("querySelfAction");
  133. ActionManager.processEvent(CoreActionType.QUERY_SELF_ACTION, buff, this, action);
  134. frame.addLine(buff, client.getNickname(), client.getIdent(), client.getHost(), action);
  135. } else {
  136. frame.addLine("actionTooLong", action.length());
  137. }
  138. }
  139. /**
  140. * Handles a private message event from the parser.
  141. *
  142. * @param parser Parser receiving the event
  143. * @param message message received
  144. * @param remoteHost remote user host
  145. */
  146. public void onPrivateMessage(final IRCParser parser, final String message,
  147. final String remoteHost) {
  148. final String[] parts = ClientInfo.parseHostFull(remoteHost);
  149. final StringBuffer buff = new StringBuffer("queryMessage");
  150. ActionManager.processEvent(CoreActionType.QUERY_MESSAGE, buff, this, message);
  151. frame.addLine(buff, parts[0], parts[1], parts[2], message);
  152. }
  153. /**
  154. * Handles a private action event from the parser.
  155. *
  156. * @param parser Parser receiving the event
  157. * @param message message received
  158. * @param remoteHost remote host
  159. */
  160. public void onPrivateAction(final IRCParser parser, final String message,
  161. final String remoteHost) {
  162. final String[] parts = ClientInfo.parseHostFull(host);
  163. final StringBuffer buff = new StringBuffer("queryAction");
  164. ActionManager.processEvent(CoreActionType.QUERY_ACTION, buff, this, message);
  165. frame.addLine(buff, parts[0], parts[1], parts[2], message);
  166. }
  167. /**
  168. * Updates the QueryFrame title.
  169. */
  170. private void updateTitle() {
  171. final String title = ClientInfo.parseHost(host);
  172. frame.setTitle(title);
  173. if (frame.isMaximum() && frame.equals(MainFrame.getMainFrame().getActiveFrame())) {
  174. MainFrame.getMainFrame().setTitle(MainFrame.getMainFrame().getTitlePrefix() + " - " + title);
  175. }
  176. }
  177. /**
  178. * Reregisters query callbacks. Called when reconnecting to the server.
  179. */
  180. public void reregister() {
  181. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  182. try {
  183. callbackManager.addCallback("onPrivateAction", this, ClientInfo.parseHost(host));
  184. callbackManager.addCallback("onPrivateMessage", this, ClientInfo.parseHost(host));
  185. callbackManager.addCallback("onNickChanged", this);
  186. } catch (CallbackNotFound ex) {
  187. Logger.error(ErrorLevel.ERROR, "Unable to get query events", ex);
  188. }
  189. }
  190. /** {@inheritDoc} */
  191. public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
  192. final String sOldNick) {
  193. if (sOldNick.equals(ClientInfo.parseHost(host))) {
  194. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  195. callbackManager.delCallback("onPrivateAction", this);
  196. callbackManager.delCallback("onPrivateMessage", this);
  197. try {
  198. callbackManager.addCallback("onPrivateAction", this, cClient.getNickname());
  199. callbackManager.addCallback("onPrivateMessage", this, cClient.getNickname());
  200. } catch (CallbackNotFound ex) {
  201. Logger.error(ErrorLevel.ERROR, "Unable to get query events", ex);
  202. }
  203. // TODO: Action hook!
  204. frame.addLine("queryNickChanged", sOldNick, cClient.getIdent(),
  205. cClient.getHost(), cClient.getNickname());
  206. host = cClient.getNickname() + "!" + cClient.getIdent() + "@" + cClient.getHost();
  207. updateTitle();
  208. }
  209. }
  210. /**
  211. * Returns the Server assocaited with this query.
  212. *
  213. * @return asscoaited Server
  214. */
  215. public Server getServer() {
  216. return server;
  217. }
  218. /**
  219. * Closes the query and associated frame.
  220. */
  221. public void close() {
  222. server.getParser().getCallbackManager().delCallback("onPrivateAction", this);
  223. server.getParser().getCallbackManager().delCallback("onPrivateMessage", this);
  224. server.getParser().getCallbackManager().delCallback("onNickChanged", this);
  225. ActionManager.processEvent(CoreActionType.QUERY_CLOSED, null, this);
  226. frame.setVisible(false);
  227. server.delQuery(host);
  228. MainFrame.getMainFrame().delChild(frame);
  229. frame = null;
  230. server = null;
  231. }
  232. /**
  233. * Returns this query's name.
  234. *
  235. * @return A string representation of this query (i.e., the user's name)
  236. */
  237. public String toString() {
  238. return ClientInfo.parseHost(host);
  239. }
  240. /**
  241. * Returns the host that this query is with.
  242. *
  243. * @return The full host that this query is with
  244. */
  245. public String getHost() {
  246. return host;
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. public void activateFrame() {
  251. if (!frame.isVisible()) {
  252. show();
  253. }
  254. MainFrame.getMainFrame().setActiveFrame(frame);
  255. }
  256. }