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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2006-2009 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.commandparser.CommandType;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import com.dmdirc.parser.interfaces.ClientInfo;
  30. import com.dmdirc.parser.interfaces.Parser;
  31. import com.dmdirc.parser.irc.callbacks.CallbackManager;
  32. import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
  33. import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
  34. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  35. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  36. import com.dmdirc.parser.interfaces.callbacks.QuitListener;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.input.TabCompleter;
  39. import com.dmdirc.ui.input.TabCompletionType;
  40. import com.dmdirc.ui.interfaces.InputWindow;
  41. import com.dmdirc.ui.interfaces.QueryWindow;
  42. import java.awt.Toolkit;
  43. import java.io.Serializable;
  44. /**
  45. * The Query class represents the client's view of a query with another user.
  46. * It handles callbacks for query events from the parser, maintains the
  47. * corresponding QueryWindow, and handles user input for the query.
  48. * @author chris
  49. */
  50. public final class Query extends MessageTarget implements
  51. PrivateActionListener, PrivateMessageListener, NickChangeListener, QuitListener, Serializable {
  52. /**
  53. * A version number for this class. It should be changed whenever the class
  54. * structure is changed (or anything else that would prevent serialized
  55. * objects being unserialized with the new class).
  56. */
  57. private static final long serialVersionUID = 1;
  58. /** The Server this Query is on. */
  59. private Server server;
  60. /** The QueryWindow used for this Query. */
  61. private QueryWindow window;
  62. /** The full host of the client associated with this Query. */
  63. private String host;
  64. /** The tab completer for the query window. */
  65. private final TabCompleter tabCompleter;
  66. /**
  67. * Creates a new instance of Query.
  68. *
  69. * @param newHost host of the remove client
  70. * @param newServer The server object that this Query belongs to
  71. */
  72. public Query(final Server newServer, final String newHost) {
  73. super("query", newServer.getParser().parseHostmask(newHost)[0],
  74. newServer.getConfigManager());
  75. this.server = newServer;
  76. this.host = newHost;
  77. window = Main.getUI().getQuery(this);
  78. WindowManager.addWindow(server.getFrame(), window);
  79. ActionManager.processEvent(CoreActionType.QUERY_OPENED, null, this);
  80. if (!server.getConfigManager().getOptionBool("general", "hidequeries")) {
  81. window.open();
  82. }
  83. tabCompleter = new TabCompleter(server.getTabCompleter());
  84. tabCompleter.addEntries(TabCompletionType.COMMAND,
  85. CommandManager.getCommandNames(CommandType.TYPE_QUERY));
  86. tabCompleter.addEntries(TabCompletionType.COMMAND,
  87. CommandManager.getCommandNames(CommandType.TYPE_CHAT));
  88. window.getInputHandler().setTabCompleter(tabCompleter);
  89. reregister();
  90. updateTitle();
  91. }
  92. /**
  93. * Shows this query's window.
  94. */
  95. public void show() {
  96. window.open();
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public InputWindow getFrame() {
  101. return window;
  102. }
  103. /**
  104. * Returns the tab completer for this query.
  105. *
  106. * @return This query's tab completer
  107. */
  108. public TabCompleter getTabCompleter() {
  109. return tabCompleter;
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public void sendLine(final String line) {
  114. if (server.getState() != ServerState.CONNECTED) {
  115. Toolkit.getDefaultToolkit().beep();
  116. return;
  117. }
  118. final ClientInfo client = server.getParser().getLocalClient();
  119. for (String part : splitLine(window.getTranscoder().encode(line))) {
  120. if (!part.isEmpty()) {
  121. server.getParser().sendMessage(getNickname(), part);
  122. final StringBuffer buff = new StringBuffer("querySelfMessage");
  123. ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, part);
  124. addLine(buff, client.getNickname(), client.getUsername(),
  125. client.getHostname(), part);
  126. }
  127. }
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public int getMaxLineLength() {
  132. return server.getState() == ServerState.CONNECTED ? server.getParser()
  133. .getMaxLength("PRIVMSG", host) : -1;
  134. }
  135. /**
  136. * Sends a private action to the remote user.
  137. *
  138. * @param action action text to send
  139. */
  140. @Override
  141. public void sendAction(final String action) {
  142. if (server.getState() != ServerState.CONNECTED) {
  143. Toolkit.getDefaultToolkit().beep();
  144. return;
  145. }
  146. final ClientInfo client = server.getParser().getLocalClient();
  147. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  148. if (maxLineLength >= action.length() + 2) {
  149. server.getParser().sendAction(getNickname(),
  150. window.getTranscoder().encode(action));
  151. final StringBuffer buff = new StringBuffer("querySelfAction");
  152. ActionManager.processEvent(CoreActionType.QUERY_SELF_ACTION, buff, this, action);
  153. addLine(buff, client.getNickname(), client.getUsername(),
  154. client.getHostname(), window.getTranscoder().encode(action));
  155. } else {
  156. addLine("actionTooLong", action.length());
  157. }
  158. }
  159. /**
  160. * Handles a private message event from the parser.
  161. *
  162. * @param parser Parser receiving the event
  163. * @param message message received
  164. * @param remoteHost remote user host
  165. */
  166. @Override
  167. public void onPrivateMessage(final Parser parser, final String message,
  168. final String remoteHost) {
  169. final String[] parts = parser.parseHostmask(host);
  170. final StringBuffer buff = new StringBuffer("queryMessage");
  171. ActionManager.processEvent(CoreActionType.QUERY_MESSAGE, buff, this, message);
  172. addLine(buff, parts[0], parts[1], parts[2], message);
  173. }
  174. /**
  175. * Handles a private action event from the parser.
  176. *
  177. * @param parser Parser receiving the event
  178. * @param message message received
  179. * @param remoteHost remote host
  180. */
  181. @Override
  182. public void onPrivateAction(final Parser parser, final String message,
  183. final String remoteHost) {
  184. final String[] parts = parser.parseHostmask(host);
  185. final StringBuffer buff = new StringBuffer("queryAction");
  186. ActionManager.processEvent(CoreActionType.QUERY_ACTION, buff, this, message);
  187. addLine(buff, parts[0], parts[1], parts[2], message);
  188. }
  189. /**
  190. * Updates the QueryWindow's title.
  191. */
  192. private void updateTitle() {
  193. window.setTitle(getNickname());
  194. }
  195. /**
  196. * Reregisters query callbacks. Called when reconnecting to the server.
  197. */
  198. public void reregister() {
  199. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  200. final String nick = getNickname();
  201. try {
  202. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  203. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  204. callbackManager.addCallback(QuitListener.class, this);
  205. callbackManager.addCallback(NickChangeListener.class, this);
  206. } catch (CallbackNotFoundException ex) {
  207. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  208. }
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void onNickChanged(final Parser tParser, final ClientInfo cClient,
  213. final String sOldNick) {
  214. if (sOldNick.equals(getNickname())) {
  215. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  216. callbackManager.delCallback(PrivateActionListener.class, this);
  217. callbackManager.delCallback(PrivateMessageListener.class, this);
  218. try {
  219. callbackManager.addCallback(PrivateActionListener.class, this, cClient.getNickname());
  220. callbackManager.addCallback(PrivateMessageListener.class, this, cClient.getNickname());
  221. } catch (CallbackNotFoundException ex) {
  222. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  223. }
  224. final StringBuffer format = new StringBuffer("queryNickChanged");
  225. ActionManager.processEvent(CoreActionType.QUERY_NICKCHANGE, format, this, sOldNick);
  226. server.getTabCompleter().removeEntry(TabCompletionType.QUERY_NICK, sOldNick);
  227. server.getTabCompleter().addEntry(TabCompletionType.QUERY_NICK, cClient.getNickname());
  228. addLine(format, sOldNick, cClient.getUsername(),
  229. cClient.getHostname(), cClient.getNickname());
  230. host = cClient.getNickname() + "!" + cClient.getUsername() + "@" + cClient.getHostname();
  231. updateTitle();
  232. setName(cClient.getNickname());
  233. }
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. public void onQuit(final Parser tParser, final ClientInfo cClient,
  238. final String sReason) {
  239. if (cClient.getNickname().equals(getNickname())) {
  240. final StringBuffer format = new StringBuffer(sReason.isEmpty()
  241. ? "queryQuit" : "queryQuitReason");
  242. ActionManager.processEvent(CoreActionType.QUERY_QUIT, format, this, sReason);
  243. addLine(format, cClient.getNickname(),
  244. cClient.getUsername(), cClient.getHostname(), sReason);
  245. }
  246. }
  247. /**
  248. * Returns the Server associated with this query.
  249. *
  250. * @return associated Server
  251. */
  252. @Override
  253. public Server getServer() {
  254. return server;
  255. }
  256. /** {@inheritDoc} */
  257. @Override
  258. public void windowClosing() {
  259. // 1: Make the window non-visible
  260. window.setVisible(false);
  261. // 2: Remove any callbacks or listeners
  262. if (server != null && server.getParser() != null) {
  263. server.getParser().getCallbackManager().delAllCallback(this);
  264. }
  265. // 3: Trigger any actions neccessary
  266. // 4: Trigger action for the window closing
  267. ActionManager.processEvent(CoreActionType.QUERY_CLOSED, null, this);
  268. // 5: Inform any parents that the window is closing
  269. if (server != null) {
  270. server.delQuery(this);
  271. }
  272. // 6: Remove the window from the window manager
  273. WindowManager.removeWindow(window);
  274. // 7: Remove any references to the window and parents
  275. window = null;
  276. server = null;
  277. }
  278. /**
  279. * Returns the host that this query is with.
  280. *
  281. * @return The full host that this query is with
  282. */
  283. public String getHost() {
  284. return host;
  285. }
  286. /**
  287. * Returns the current nickname of the user that this query is with.
  288. *
  289. * @return The nickname of this query's user
  290. */
  291. public String getNickname() {
  292. return server.getParser().parseHostmask(host)[0];
  293. }
  294. /** {@inheritDoc} */
  295. @Override
  296. public void activateFrame() {
  297. if (window == null) {
  298. return;
  299. }
  300. if (!window.isVisible()) {
  301. show();
  302. }
  303. super.activateFrame();
  304. }
  305. }