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.

UnreadStatusManager.java 6.3KB

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