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.

ContactListListener.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.contactlist;
  18. import com.dmdirc.events.ChannelUserAwayEvent;
  19. import com.dmdirc.events.ChannelUserBackEvent;
  20. import com.dmdirc.events.FrameClosingEvent;
  21. import com.dmdirc.events.NickListClientAddedEvent;
  22. import com.dmdirc.events.NickListClientsChangedEvent;
  23. import com.dmdirc.interfaces.EventBus;
  24. import com.dmdirc.interfaces.GroupChat;
  25. import com.dmdirc.interfaces.GroupChatUser;
  26. import com.dmdirc.interfaces.PrivateChat;
  27. import net.engio.mbassy.listener.Handler;
  28. /**
  29. * Listens for contact list related events.
  30. */
  31. public class ContactListListener {
  32. /** The group chat this listener is for. */
  33. private final GroupChat groupChat;
  34. /** Event bus to register listeners with. */
  35. private final EventBus eventBus;
  36. /**
  37. * Creates a new ContactListListener for the specified group chat.
  38. *
  39. * @param groupChat The group chat to show a contact list for
  40. */
  41. public ContactListListener(final GroupChat groupChat, final EventBus eventBus) {
  42. this.groupChat = groupChat;
  43. this.eventBus = eventBus;
  44. }
  45. /**
  46. * Adds all necessary listeners for this contact list listener to function.
  47. */
  48. public void addListeners() {
  49. eventBus.subscribe(this);
  50. }
  51. /**
  52. * Removes the listeners added by {@link #addListeners()}.
  53. */
  54. public void removeListeners() {
  55. eventBus.unsubscribe(this);
  56. }
  57. @Handler
  58. public void handleClientsUpdated(final NickListClientsChangedEvent event) {
  59. if (event.getChannel().equals(groupChat)) {
  60. event.getUsers().forEach(this::clientAdded);
  61. }
  62. }
  63. @Handler
  64. public void handleClientAdded(final NickListClientAddedEvent event) {
  65. if (event.getChannel().equals(groupChat)) {
  66. clientAdded(event.getUser());
  67. }
  68. }
  69. @Handler
  70. public void handleUserAway(final ChannelUserAwayEvent event) {
  71. if (event.getChannel().equals(groupChat)) {
  72. clientAdded(event.getUser());
  73. }
  74. }
  75. @Handler
  76. public void handleUserBack(final ChannelUserBackEvent event) {
  77. if (event.getChannel().equals(groupChat)) {
  78. clientAdded(event.getUser());
  79. }
  80. }
  81. @Handler
  82. public void windowClosing(final FrameClosingEvent event) {
  83. if (event.getSource().equals(groupChat.getWindowModel())) {
  84. removeListeners();
  85. }
  86. }
  87. void clientAdded(final GroupChatUser client) {
  88. final PrivateChat query =
  89. groupChat.getConnection().get().getQuery(client.getNickname(), false);
  90. query.getWindowModel()
  91. .setIcon("query-" + client.getUser().getAwayState().name().toLowerCase());
  92. }
  93. }