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.

UnreadStatusManagerImpl.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.ui.messages;
  23. import com.dmdirc.config.ConfigBinding;
  24. import com.dmdirc.events.BaseChannelTextEvent;
  25. import com.dmdirc.events.BaseQueryTextEvent;
  26. import com.dmdirc.events.ChannelHighlightEvent;
  27. import com.dmdirc.events.DisplayProperty;
  28. import com.dmdirc.events.DisplayableEvent;
  29. import com.dmdirc.events.QueryHighlightEvent;
  30. import com.dmdirc.events.UnreadStatusChangedEvent;
  31. import com.dmdirc.interfaces.EventBus;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.util.colours.Colour;
  34. import java.util.Optional;
  35. import net.engio.mbassy.listener.Handler;
  36. /**
  37. * Tracks unread messages and other notifications.
  38. */
  39. public class UnreadStatusManagerImpl implements UnreadStatusManager {
  40. private final EventBus eventBus;
  41. private final WindowModel container;
  42. private final ColourManager colourManager;
  43. private int unreadLines;
  44. private Optional<Colour> notificationColour = Optional.empty();
  45. private Optional<Colour> miscellaneousColour = Optional.of(Colour.GREEN);
  46. private Optional<Colour> messageColour = Optional.of(Colour.BLUE);
  47. private Optional<Colour> highlightColour = Optional.of(Colour.RED);
  48. public UnreadStatusManagerImpl(final WindowModel container) {
  49. this.container = container;
  50. this.eventBus = container.getEventBus();
  51. this.colourManager = new ColourManagerImpl(container.getConfigManager());
  52. }
  53. @Handler
  54. public void handleDisplayableEvent(final DisplayableEvent event) {
  55. if (includeEvent(event)) {
  56. updateStatus(miscellaneousColour, unreadLines + 1);
  57. }
  58. }
  59. @Handler
  60. public void handleChannelTextEvent(final BaseChannelTextEvent event) {
  61. if (includeEvent(event)) {
  62. updateStatus(messageColour);
  63. }
  64. }
  65. @Handler
  66. public void handleQueryTextEvent(final BaseQueryTextEvent event) {
  67. if (includeEvent(event)) {
  68. updateStatus(messageColour);
  69. }
  70. }
  71. @Handler
  72. public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
  73. if (includeEvent(event)) {
  74. updateStatus(highlightColour);
  75. }
  76. }
  77. @Handler
  78. public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
  79. if (includeEvent(event)) {
  80. updateStatus(highlightColour);
  81. }
  82. }
  83. private boolean includeEvent(final DisplayableEvent event) {
  84. return event.getSource().equals(container)
  85. && !event.getDisplayProperty(DisplayProperty.DO_NOT_DISPLAY).orElse(false);
  86. }
  87. @Override
  88. public int getUnreadLines() {
  89. return unreadLines;
  90. }
  91. @Override
  92. public Optional<Colour> getNotificationColour() {
  93. return notificationColour;
  94. }
  95. @Override
  96. public void clearStatus() {
  97. updateStatus(Optional.empty(), 0);
  98. }
  99. private void updateStatus(final Optional<Colour> desiredColour) {
  100. updateStatus(desiredColour, unreadLines);
  101. }
  102. private void updateStatus(final Optional<Colour> desiredColour, final int newUnreadCount) {
  103. final Optional<Colour> newColour = getBestColour(desiredColour, notificationColour);
  104. final boolean updated = !newColour.equals(notificationColour)
  105. || newUnreadCount != unreadLines;
  106. notificationColour = newColour;
  107. unreadLines = newUnreadCount;
  108. if (updated) {
  109. publishChangedEvent();
  110. }
  111. }
  112. private Optional<Colour> getBestColour(
  113. final Optional<Colour> desiredColour,
  114. final Optional<Colour> existingColour) {
  115. if (!desiredColour.isPresent()) {
  116. // If we're trying to explicitly reset, go with the empty one.
  117. return desiredColour;
  118. }
  119. if (desiredColour.equals(highlightColour)
  120. || !existingColour.isPresent()
  121. || existingColour.equals(miscellaneousColour)) {
  122. return desiredColour;
  123. } else {
  124. return existingColour;
  125. }
  126. }
  127. @ConfigBinding(domain = "ui", key = "miscellaneousNotificationColour")
  128. void handleMiscellaneousColour(final String colour) {
  129. final Optional<Colour> newColour = Optional.ofNullable(
  130. colourManager.getColourFromString(colour, Colour.GREEN));
  131. if (notificationColour.equals(miscellaneousColour)) {
  132. notificationColour = newColour;
  133. publishChangedEvent();
  134. }
  135. miscellaneousColour = newColour;
  136. }
  137. @ConfigBinding(domain = "ui", key = "messageNotificationColour")
  138. void handleMessageColour(final String colour) {
  139. final Optional<Colour> newColour = Optional.ofNullable(
  140. colourManager.getColourFromString(colour, Colour.BLUE));
  141. if (notificationColour.equals(messageColour)) {
  142. notificationColour = newColour;
  143. publishChangedEvent();
  144. }
  145. messageColour = newColour;
  146. }
  147. @ConfigBinding(domain = "ui", key = "highlightNotificationColour")
  148. void handleHighlightColour(final String colour) {
  149. final Optional<Colour> newColour = Optional.ofNullable(
  150. colourManager.getColourFromString(colour, Colour.RED));
  151. if (notificationColour.equals(highlightColour)) {
  152. notificationColour = newColour;
  153. publishChangedEvent();
  154. }
  155. highlightColour = newColour;
  156. }
  157. private void publishChangedEvent() {
  158. eventBus.publishAsync(new UnreadStatusChangedEvent(container, this, notificationColour,
  159. unreadLines));
  160. }
  161. }