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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2006-2014 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.ClientModule.UserConfig;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.interfaces.ActionListener;
  28. import com.dmdirc.interfaces.actions.ActionType;
  29. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  30. import com.dmdirc.interfaces.config.ConfigProvider;
  31. import com.dmdirc.plugins.PluginDomain;
  32. import com.dmdirc.plugins.PluginInfo;
  33. import com.dmdirc.plugins.PluginManager;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import javax.inject.Inject;
  37. public class NotificationsManager implements ActionListener {
  38. /** The notification methods that we know of. */
  39. private final List<String> methods = new ArrayList<>();
  40. /** The user's preferred order for method usage. */
  41. private List<String> order;
  42. /** This plugin's settings domain. */
  43. private final String domain;
  44. /** Global config to read settings from. */
  45. private final AggregateConfigProvider globalConfig;
  46. /** Plugin manager. */
  47. private final PluginManager pluginManager;
  48. @Inject
  49. public NotificationsManager(
  50. @PluginDomain(NotificationsPlugin.class) final String domain,
  51. @GlobalConfig final AggregateConfigProvider globalConfig,
  52. @UserConfig final ConfigProvider userSettings,
  53. final PluginManager pluginManager) {
  54. this.domain = domain;
  55. this.globalConfig = globalConfig;
  56. this.pluginManager = pluginManager;
  57. }
  58. public void onLoad() {
  59. methods.clear();
  60. loadSettings();
  61. ActionManager.getActionManager().registerListener(this,
  62. CoreActionType.PLUGIN_LOADED, CoreActionType.PLUGIN_UNLOADED);
  63. for (PluginInfo target : pluginManager.getPluginInfos()) {
  64. if (target.isLoaded()) {
  65. addPlugin(target);
  66. }
  67. }
  68. }
  69. public void onUnload() {
  70. methods.clear();
  71. ActionManager.getActionManager().unregisterListener(this);
  72. }
  73. /** Loads the plugins settings. */
  74. private void loadSettings() {
  75. if (globalConfig.hasOptionString(domain, "methodOrder")) {
  76. order = globalConfig.getOptionList(domain, "methodOrder");
  77. } else {
  78. order = new ArrayList<>();
  79. }
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void processEvent(final ActionType type, final StringBuffer format,
  84. final Object... arguments) {
  85. if (type == CoreActionType.PLUGIN_LOADED) {
  86. addPlugin((PluginInfo) arguments[0]);
  87. } else if (type == CoreActionType.PLUGIN_UNLOADED) {
  88. removePlugin((PluginInfo) arguments[0]);
  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. methods.add(target.getMetaData().getName());
  100. addMethodToOrder(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 neccessary.
  106. *
  107. * @param source The notification method to be tested
  108. */
  109. private void addMethodToOrder(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. methods.remove(target.getMetaData().getName());
  122. }
  123. /**
  124. * Retrieves a method based on its name.
  125. *
  126. * @param name The name to search for
  127. *
  128. * @return The method with the specified name or null if none were found.
  129. */
  130. public PluginInfo getMethod(final String name) {
  131. return pluginManager.getPluginInfoByName(name);
  132. }
  133. /**
  134. * Retrieves all the methods registered with this plugin.
  135. *
  136. * @return All known notification sources
  137. */
  138. public List<PluginInfo> getMethods() {
  139. final List<PluginInfo> plugins = new ArrayList<>();
  140. for (String method : methods) {
  141. plugins.add(pluginManager.getPluginInfoByName(method));
  142. }
  143. return plugins;
  144. }
  145. /**
  146. * Does this plugin have any active notification methods?
  147. *
  148. * @return true iif active notification methods are registered
  149. */
  150. public boolean hasActiveMethod() {
  151. return !methods.isEmpty();
  152. }
  153. /**
  154. * Returns the user's preferred method if loaded, or null if none loaded.
  155. *
  156. * @return Preferred notification method
  157. */
  158. public PluginInfo getPreferredMethod() {
  159. if (methods.isEmpty()) {
  160. return null;
  161. }
  162. for (String method : order) {
  163. if (methods.contains(method)) {
  164. return pluginManager.getPluginInfoByName(method);
  165. }
  166. }
  167. return null;
  168. }
  169. }