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.

ChannelWhoManager.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.channelwho;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.config.prefs.PreferencesCategory;
  25. import com.dmdirc.config.prefs.PreferencesSetting;
  26. import com.dmdirc.config.prefs.PreferencesType;
  27. import com.dmdirc.events.ClientPrefsOpenedEvent;
  28. import com.dmdirc.events.GroupChatPrefsRequestedEvent;
  29. import com.dmdirc.events.ServerConnectingEvent;
  30. import com.dmdirc.events.ServerDisconnectedEvent;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.ConnectionManager;
  33. import com.dmdirc.plugins.PluginDomain;
  34. import com.dmdirc.util.validators.NumericalValidator;
  35. import com.google.common.annotations.VisibleForTesting;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import javax.inject.Inject;
  39. import net.engio.mbassy.listener.Handler;
  40. /**
  41. * Provides channel who support in DMDirc.
  42. */
  43. public class ChannelWhoManager {
  44. private final String domain;
  45. private final ConnectionHandlerFactory connectionHandlerFactory;
  46. private final ConnectionManager connectionManager;
  47. private final DMDircMBassador eventBus;
  48. private final Map<Connection, ConnectionHandler> connectionHandlers;
  49. @Inject
  50. public ChannelWhoManager(
  51. @PluginDomain(ChannelWhoPlugin.class) final String domain,
  52. final ConnectionHandlerFactory connectionHandlerFactory,
  53. final ConnectionManager connectionManager,
  54. final DMDircMBassador eventBus) {
  55. this.domain = domain;
  56. this.connectionHandlerFactory = connectionHandlerFactory;
  57. this.connectionManager = connectionManager;
  58. this.eventBus = eventBus;
  59. connectionHandlers = new HashMap<>();
  60. }
  61. public void load() {
  62. eventBus.subscribe(this);
  63. connectionManager.getConnections().forEach(this::addConnectionHandler);
  64. }
  65. public void unload() {
  66. connectionManager.getConnections().forEach(this::removeConnectionHandler);
  67. eventBus.unsubscribe(this);
  68. }
  69. private void addConnectionHandler(final Connection connection) {
  70. connectionHandlers.computeIfAbsent(connection, connectionHandlerFactory::get);
  71. }
  72. private void removeConnectionHandler(final Connection connection) {
  73. final ConnectionHandler connectionHandler = connectionHandlers.remove(connection);
  74. if (connectionHandler != null) {
  75. connectionHandler.unload();
  76. }
  77. }
  78. @VisibleForTesting
  79. @Handler
  80. void handleGroupChatPrefsRequestedEvent(final GroupChatPrefsRequestedEvent event) {
  81. event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, domain,
  82. "sendWho", "Send Who Requests", "Should we send who requests to the channel?",
  83. event.getConfig(), event.getIdentity()));
  84. }
  85. @VisibleForTesting
  86. @Handler
  87. void handlePrefsDialog(final ClientPrefsOpenedEvent event) {
  88. final PreferencesCategory category = new PreferencesCategory("Channel Who", "Provides " +
  89. "support for sendinw WHO requests to channels at regular intervals");
  90. category.addSetting(new PreferencesSetting(PreferencesType.DURATION,
  91. new NumericalValidator(0, Integer.MAX_VALUE), domain, "whointerval",
  92. "Who Interval", "The interval WHO requests will be sent to channels",
  93. event.getModel().getConfigManager(), event.getModel().getIdentity()));
  94. }
  95. @VisibleForTesting
  96. @Handler
  97. void handleServerConnectingEvent(final ServerConnectingEvent event) {
  98. addConnectionHandler(event.getConnection());
  99. }
  100. @VisibleForTesting
  101. @Handler
  102. void handleServerDisconnectedEvent(final ServerDisconnectedEvent event) {
  103. removeConnectionHandler(event.getConnection());
  104. }
  105. }