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.1KB

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