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

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