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.

NotificationsManager.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.notifications;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.config.GlobalConfig;
  20. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  21. import com.dmdirc.config.prefs.PreferencesCategory;
  22. import com.dmdirc.config.prefs.PreferencesDialogModel;
  23. import com.dmdirc.events.ClientPrefsOpenedEvent;
  24. import com.dmdirc.events.PluginLoadedEvent;
  25. import com.dmdirc.events.PluginUnloadedEvent;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.dmdirc.plugins.PluginDomain;
  29. import com.dmdirc.plugins.PluginInfo;
  30. import com.dmdirc.plugins.PluginManager;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import javax.inject.Inject;
  37. import net.engio.mbassy.listener.Handler;
  38. public class NotificationsManager {
  39. /** The notification handlers that we know of. */
  40. private final Map<String, NotificationHandler> handlers = new HashMap<>();
  41. /** The user's preferred order for method usage. */
  42. private List<String> order;
  43. /** This plugin's settings domain. */
  44. private final String domain;
  45. private final PluginInfo pluginInfo;
  46. /** Global config to read settings from. */
  47. private final AggregateConfigProvider globalConfig;
  48. /** Plugin manager. */
  49. private final PluginManager pluginManager;
  50. /** Event bus to listen for events on. */
  51. private final EventBus eventBus;
  52. @Inject
  53. public NotificationsManager(@PluginDomain(NotificationsPlugin.class) final String domain,
  54. @PluginDomain(NotificationsPlugin.class) final PluginInfo pluginInfo,
  55. @GlobalConfig final AggregateConfigProvider globalConfig, final EventBus eventBus,
  56. final PluginManager pluginManager) {
  57. this.domain = domain;
  58. this.pluginInfo = pluginInfo;
  59. this.globalConfig = globalConfig;
  60. this.pluginManager = pluginManager;
  61. this.eventBus = eventBus;
  62. }
  63. public void onLoad() {
  64. handlers.clear();
  65. loadSettings();
  66. eventBus.subscribe(this);
  67. pluginManager.getPluginInfos().stream()
  68. .filter(PluginInfo::isLoaded)
  69. .forEach(this::addPlugin);
  70. }
  71. public void onUnload() {
  72. handlers.clear();
  73. eventBus.unsubscribe(this);
  74. }
  75. @Handler
  76. public void handlePluginLoaded(final PluginLoadedEvent event) {
  77. addPlugin(event.getPlugin());
  78. }
  79. @Handler
  80. public void handlePluginUnloaded(final PluginUnloadedEvent event) {
  81. removePlugin(event.getPlugin());
  82. }
  83. /** Loads the plugins settings. */
  84. private void loadSettings() {
  85. if (globalConfig.hasOptionString(domain, "methodOrder")) {
  86. order = globalConfig.getOptionList(domain, "methodOrder");
  87. } else {
  88. order = new ArrayList<>();
  89. }
  90. }
  91. /**
  92. * Checks to see if a plugin implements the notification method interface and if it does, adds
  93. * the method to our list.
  94. *
  95. * @param target The plugin to be tested
  96. */
  97. private void addPlugin(final PluginInfo target) {
  98. if (target.hasExportedService("showNotification")) {
  99. handlers.put(target.getMetaData().getName(), new LegacyNotificationHandler(pluginInfo));
  100. addHandlerToOrder(target);
  101. }
  102. }
  103. /**
  104. * Checks to see if the specified notification method needs to be added to our order list, and
  105. * adds it if necessary.
  106. *
  107. * @param source The notification method to be tested
  108. */
  109. private void addHandlerToOrder(final PluginInfo source) {
  110. if (!order.contains(source.getMetaData().getName())) {
  111. order.add(source.getMetaData().getName());
  112. }
  113. }
  114. /**
  115. * Checks to see if a plugin implements the notification method interface and if it does,
  116. * removes the method from our list.
  117. *
  118. * @param target The plugin to be tested
  119. */
  120. private void removePlugin(final PluginInfo target) {
  121. handlers.remove(target.getMetaData().getName());
  122. }
  123. /**
  124. * Retrieves a handler based on its name.
  125. *
  126. * @param name The name to search for
  127. *
  128. * @return The handler with the specified name or null if none were found.
  129. */
  130. public NotificationHandler getHandler(final String name) {
  131. return handlers.get(name);
  132. }
  133. /**
  134. * Retrieves the names of all the handlers registered with this plugin.
  135. *
  136. * @return All known notification handler names
  137. */
  138. public Collection<String> getHandlerNames() {
  139. return handlers.keySet();
  140. }
  141. /**
  142. * Does this plugin have any active notification handler?
  143. *
  144. * @return true iif active notification handlers are registered
  145. */
  146. public boolean hasActiveHandler() {
  147. return !handlers.isEmpty();
  148. }
  149. /**
  150. * Returns the user's preferred handler if loaded, or null if none loaded.
  151. *
  152. * @return Preferred notification handler
  153. */
  154. public NotificationHandler getPreferredHandler() {
  155. if (handlers.isEmpty()) {
  156. return null;
  157. }
  158. for (String method : order) {
  159. if (handlers.containsKey(method)) {
  160. return handlers.get(method);
  161. }
  162. }
  163. return null;
  164. }
  165. @Handler
  166. public void showConfig(final ClientPrefsOpenedEvent event) {
  167. final PreferencesDialogModel manager = event.getModel();
  168. final NotificationConfig configPanel = UIUtilities.invokeAndWait(
  169. () -> new NotificationConfig(manager.getIdentity(), pluginInfo.getDomain(),
  170. manager.getConfigManager()
  171. .getOptionList(pluginInfo.getDomain(), "methodOrder")));
  172. final PreferencesCategory category = new PluginPreferencesCategory(
  173. pluginInfo, "Notifications", "", "category-notifications",
  174. configPanel);
  175. manager.getCategory("Plugins").addSubCategory(category);
  176. }
  177. }