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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2006-2012 DMDirc Developers
  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.commandparser.parsers.QueryCommandParser;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.logger.Logger;
  30. import com.dmdirc.parser.common.CallbackManager;
  31. import com.dmdirc.parser.common.CallbackNotFoundException;
  32. import com.dmdirc.parser.common.CompositionState;
  33. import com.dmdirc.parser.interfaces.ClientInfo;
  34. import com.dmdirc.parser.interfaces.Parser;
  35. import com.dmdirc.parser.interfaces.callbacks.CompositionStateChangeListener;
  36. import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
  37. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  38. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  39. import com.dmdirc.parser.interfaces.callbacks.QuitListener;
  40. import com.dmdirc.ui.WindowManager;
  41. import com.dmdirc.ui.core.components.WindowComponent;
  42. import com.dmdirc.ui.input.TabCompleter;
  43. import com.dmdirc.ui.input.TabCompletionType;
  44. import java.awt.Toolkit;
  45. import java.util.Arrays;
  46. import java.util.Date;
  47. import java.util.List;
  48. /**
  49. * The Query class represents the client's view of a query with another user.
  50. * It handles callbacks for query events from the parser, maintains the
  51. * corresponding QueryWindow, and handles user input for the query.
  52. */
  53. public class Query extends MessageTarget implements PrivateActionListener,
  54. PrivateMessageListener, NickChangeListener, QuitListener,
  55. CompositionStateChangeListener {
  56. /** The Server this Query is on. */
  57. private Server server;
  58. /** The full host and nickname of the client associated with this Query. */
  59. private String host, nickname;
  60. /** The tab completer for the query window. */
  61. private final TabCompleter tabCompleter;
  62. /**
  63. * Creates a new instance of Query.
  64. *
  65. * @param newHost host of the remove client
  66. * @param newServer The server object that this Query belongs to
  67. */
  68. public Query(final Server newServer, final String newHost) {
  69. this(newServer, newHost, false);
  70. }
  71. /**
  72. * Creates a new instance of Query.
  73. *
  74. * @param newHost host of the remove client
  75. * @param newServer The server object that this Query belongs to
  76. * @param focus Should we focus the query on open?
  77. */
  78. public Query(final Server newServer, final String newHost,
  79. final boolean focus) {
  80. super("query", newServer.parseHostmask(newHost)[0],
  81. newServer.parseHostmask(newHost)[0],
  82. newServer.getConfigManager(), new QueryCommandParser(newServer),
  83. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  84. WindowComponent.INPUTFIELD.getIdentifier()));
  85. this.server = newServer;
  86. this.host = newHost;
  87. this.nickname = server.parseHostmask(host)[0];
  88. WindowManager.getWindowManager().addWindow(server, this, focus);
  89. ActionManager.getActionManager().triggerEvent(
  90. CoreActionType.QUERY_OPENED, null, this);
  91. tabCompleter = new TabCompleter(server.getTabCompleter());
  92. tabCompleter.addEntries(TabCompletionType.COMMAND,
  93. CommandManager.getCommandManager().getCommandNames(CommandType.TYPE_QUERY));
  94. tabCompleter.addEntries(TabCompletionType.COMMAND,
  95. CommandManager.getCommandManager().getCommandNames(CommandType.TYPE_CHAT));
  96. if (!server.getState().isDisconnected()) {
  97. reregister();
  98. }
  99. updateTitle();
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public TabCompleter getTabCompleter() {
  104. return tabCompleter;
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void sendLine(final String line) {
  109. sendLine(line, getNickname());
  110. }
  111. /**
  112. * Sends a line to the recipient of this query using the specified
  113. * nickname or hostmask as a target. This allows for users to send
  114. * messages with a server specified (e.g. <code>nick@server</code>)
  115. * as though the query wasn't open.
  116. * <p>
  117. * The caller is responsible for ensuring that the <code>target</code>
  118. * does actually correspond to this query.
  119. *
  120. * @since 0.6.4
  121. * @param line The line to be sent
  122. * @param target The target to send the line to
  123. */
  124. public void sendLine(final String line, final String target) {
  125. if (server.getState() != ServerState.CONNECTED) {
  126. Toolkit.getDefaultToolkit().beep();
  127. return;
  128. }
  129. for (String part : splitLine(line)) {
  130. if (!part.isEmpty()) {
  131. server.getParser().sendMessage(target, part);
  132. doNotification("querySelfMessage",
  133. CoreActionType.QUERY_SELF_MESSAGE,
  134. server.getParser().getLocalClient(), line);
  135. }
  136. }
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  141. if (arg instanceof ClientInfo) {
  142. final ClientInfo clientInfo = (ClientInfo) arg;
  143. args.add(clientInfo.getNickname());
  144. args.add(clientInfo.getUsername());
  145. args.add(clientInfo.getHostname());
  146. return true;
  147. } else {
  148. return super.processNotificationArg(arg, args);
  149. }
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. public int getMaxLineLength() {
  154. return server.getState() == ServerState.CONNECTED ? server.getParser()
  155. .getMaxLength("PRIVMSG", host) : -1;
  156. }
  157. /**
  158. * Sends a private action to the remote user.
  159. *
  160. * @param action action text to send
  161. */
  162. @Override
  163. public void sendAction(final String action) {
  164. if (server.getState() != ServerState.CONNECTED) {
  165. Toolkit.getDefaultToolkit().beep();
  166. return;
  167. }
  168. final ClientInfo client = server.getParser().getLocalClient();
  169. final int maxLineLength = server.getParser().getMaxLength("PRIVMSG", host);
  170. if (maxLineLength >= action.length() + 2) {
  171. server.getParser().sendAction(getNickname(), action);
  172. doNotification("querySelfAction", CoreActionType.QUERY_SELF_ACTION,
  173. client, action);
  174. } else {
  175. addLine("actionTooLong", action.length());
  176. }
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public void onPrivateMessage(final Parser parser, final Date date,
  181. final String message, final String host) {
  182. final String[] parts = server.parseHostmask(host);
  183. final StringBuffer buff = new StringBuffer("queryMessage");
  184. ActionManager.getActionManager().triggerEvent(
  185. CoreActionType.QUERY_MESSAGE, buff, this,
  186. parser.getClient(host), message);
  187. addLine(buff, parts[0], parts[1], parts[2], message);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public void onPrivateAction(final Parser parser, final Date date,
  192. final String message, final String host) {
  193. final String[] parts = server.parseHostmask(host);
  194. final StringBuffer buff = new StringBuffer("queryAction");
  195. ActionManager.getActionManager().triggerEvent(
  196. CoreActionType.QUERY_ACTION, buff, this, parser.getClient(host),
  197. message);
  198. addLine(buff, parts[0], parts[1], parts[2], message);
  199. }
  200. /**
  201. * Updates the QueryWindow's title.
  202. */
  203. private void updateTitle() {
  204. setTitle(getNickname());
  205. }
  206. /**
  207. * Reregisters query callbacks. Called when reconnecting to the server.
  208. */
  209. public void reregister() {
  210. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  211. final String nick = getNickname();
  212. try {
  213. callbackManager.addCallback(PrivateActionListener.class, this, nick);
  214. callbackManager.addCallback(PrivateMessageListener.class, this, nick);
  215. callbackManager.addCallback(CompositionStateChangeListener.class, this, nick);
  216. callbackManager.addCallback(QuitListener.class, this);
  217. callbackManager.addCallback(NickChangeListener.class, this);
  218. } catch (CallbackNotFoundException ex) {
  219. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  220. }
  221. }
  222. /** {@inheritDoc} */
  223. @Override
  224. public void onNickChanged(final Parser parser, final Date date,
  225. final ClientInfo client, final String oldNick) {
  226. if (oldNick.equals(getNickname())) {
  227. final CallbackManager callbackManager = server.getParser().getCallbackManager();
  228. callbackManager.delCallback(PrivateActionListener.class, this);
  229. callbackManager.delCallback(PrivateMessageListener.class, this);
  230. callbackManager.delCallback(CompositionStateChangeListener.class, this);
  231. try {
  232. callbackManager.addCallback(PrivateActionListener.class, this, client.getNickname());
  233. callbackManager.addCallback(PrivateMessageListener.class, this, client.getNickname());
  234. callbackManager.addCallback(CompositionStateChangeListener.class, this, client.getNickname());
  235. } catch (CallbackNotFoundException ex) {
  236. Logger.appError(ErrorLevel.HIGH, "Unable to get query events", ex);
  237. }
  238. final StringBuffer format = new StringBuffer("queryNickChanged");
  239. ActionManager.getActionManager().triggerEvent(
  240. CoreActionType.QUERY_NICKCHANGE, format, this, oldNick);
  241. server.updateQuery(this, oldNick, client.getNickname());
  242. addLine(format, oldNick, client.getUsername(),
  243. client.getHostname(), client.getNickname());
  244. host = client.getNickname() + "!" + client.getUsername() + "@" + client.getHostname();
  245. nickname = client.getNickname();
  246. updateTitle();
  247. setName(client.getNickname());
  248. }
  249. }
  250. /** {@inheritDoc} */
  251. @Override
  252. public void onQuit(final Parser parser, final Date date,
  253. final ClientInfo client, final String reason) {
  254. if (client.getNickname().equals(getNickname())) {
  255. final StringBuffer format = new StringBuffer(reason.isEmpty()
  256. ? "queryQuit" : "queryQuitReason");
  257. ActionManager.getActionManager().triggerEvent(
  258. CoreActionType.QUERY_QUIT, format, this, reason);
  259. addLine(format, client.getNickname(),
  260. client.getUsername(), client.getHostname(), reason);
  261. }
  262. }
  263. /** {@inheritDoc} */
  264. @Override
  265. public void onCompositionStateCanged(final Parser parser, final Date date,
  266. final CompositionState state, final String host) {
  267. if (state == CompositionState.TYPING) {
  268. addComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  269. } else {
  270. removeComponent(WindowComponent.TYPING_INDICATOR.getIdentifier());
  271. }
  272. }
  273. /**
  274. * Returns the Server associated with this query.
  275. *
  276. * @return associated Server
  277. */
  278. @Override
  279. public Server getServer() {
  280. return server;
  281. }
  282. /** {@inheritDoc} */
  283. @Override
  284. public void windowClosing() {
  285. // 2: Remove any callbacks or listeners
  286. if (server != null && server.getParser() != null) {
  287. server.getParser().getCallbackManager().delAllCallback(this);
  288. }
  289. // 3: Trigger any actions neccessary
  290. // 4: Trigger action for the window closing
  291. ActionManager.getActionManager().triggerEvent(
  292. CoreActionType.QUERY_CLOSED, null, this);
  293. // 5: Inform any parents that the window is closing
  294. if (server != null) {
  295. server.delQuery(this);
  296. }
  297. }
  298. /** {@inheritDoc} */
  299. @Override
  300. public void windowClosed() {
  301. // 7: Remove any references to the window and parents
  302. server = null;
  303. }
  304. /**
  305. * Returns the host that this query is with.
  306. *
  307. * @return The full host that this query is with
  308. */
  309. public String getHost() {
  310. return host;
  311. }
  312. /**
  313. * Returns the current nickname of the user that this query is with.
  314. *
  315. * @return The nickname of this query's user
  316. */
  317. public String getNickname() {
  318. return nickname;
  319. }
  320. /** {@inheritDoc} */
  321. @Override
  322. public void setCompositionState(final CompositionState state) {
  323. getServer().getParser().setCompositionState(host, state);
  324. }
  325. }