Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

NotificationsManager.java 7.0KB

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