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.

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