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

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