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.

GroupChatHandler.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.jpq;
  23. import com.dmdirc.config.ConfigBinder;
  24. import com.dmdirc.config.ConfigBinding;
  25. import com.dmdirc.events.ChannelJoinEvent;
  26. import com.dmdirc.events.ChannelNickChangeEvent;
  27. import com.dmdirc.events.ChannelPartEvent;
  28. import com.dmdirc.events.ChannelQuitEvent;
  29. import com.dmdirc.events.DisplayProperty;
  30. import com.dmdirc.events.DisplayableEvent;
  31. import com.dmdirc.interfaces.GroupChat;
  32. import com.google.common.annotations.VisibleForTesting;
  33. import net.engio.mbassy.listener.Handler;
  34. /**
  35. * Handles {@link ChannelJoinEvent}, {@link ChannelPartEvent}, {@link ChannelQuitEvent} events and
  36. * hides them if the required.
  37. */
  38. public class GroupChatHandler {
  39. private final GroupChat groupChat;
  40. private final ConfigBinder binder;
  41. private boolean hideEvents;
  42. private boolean hideNickChanges;
  43. public GroupChatHandler(final String domain, final GroupChat groupChat) {
  44. this.groupChat = groupChat;
  45. binder = groupChat.getWindowModel().getConfigManager().getBinder()
  46. .withDefaultDomain(domain);
  47. }
  48. /**
  49. * Loads this handler, adds required listeners and bindings.
  50. */
  51. public void load() {
  52. groupChat.getEventBus().subscribe(this);
  53. binder.bind(this, GroupChatHandler.class);
  54. }
  55. /**
  56. * Unloads this handler, removes required listeners and bindings.
  57. */
  58. public void unload() {
  59. groupChat.getEventBus().unsubscribe(this);
  60. binder.unbind(this);
  61. }
  62. @VisibleForTesting
  63. @ConfigBinding(key = "hidejpq")
  64. void handleSettingChange(final boolean value) {
  65. hideEvents = value;
  66. }
  67. @VisibleForTesting
  68. @ConfigBinding(key = "hidenickchanges")
  69. void handleSettingChangeNickname(final boolean value) {
  70. hideNickChanges = value;
  71. }
  72. @SuppressWarnings("TypeMayBeWeakened")
  73. @VisibleForTesting
  74. @Handler
  75. void handleJoin(final ChannelJoinEvent event) {
  76. if (event.getChannel().equals(groupChat)) {
  77. hideEvent(event);
  78. }
  79. }
  80. @SuppressWarnings("TypeMayBeWeakened")
  81. @VisibleForTesting
  82. @Handler
  83. void handlePart(final ChannelPartEvent event) {
  84. if (event.getChannel().equals(groupChat)) {
  85. hideEvent(event);
  86. }
  87. }
  88. @SuppressWarnings("TypeMayBeWeakened")
  89. @VisibleForTesting
  90. @Handler
  91. void handleQuit(final ChannelQuitEvent event) {
  92. if (event.getChannel().equals(groupChat)) {
  93. hideEvent(event);
  94. }
  95. }
  96. @SuppressWarnings("TypeMayBeWeakened")
  97. @VisibleForTesting
  98. @Handler
  99. void handleNickChange(final ChannelNickChangeEvent event) {
  100. if (event.getChannel().equals(groupChat) && hideNickChanges) {
  101. event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  102. }
  103. }
  104. private void hideEvent(final DisplayableEvent event) {
  105. if (hideEvents) {
  106. event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  107. }
  108. }
  109. }