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.

NotificationsPlugin.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2006-2013 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.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesDialogModel;
  30. import com.dmdirc.interfaces.ActionListener;
  31. import com.dmdirc.interfaces.CommandController;
  32. import com.dmdirc.interfaces.actions.ActionType;
  33. import com.dmdirc.plugins.PluginInfo;
  34. import com.dmdirc.plugins.implementations.BaseCommandPlugin;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.concurrent.Callable;
  38. /**
  39. * Notification Manager plugin, aggregates notification sources exposing them
  40. * via a single command.
  41. */
  42. public class NotificationsPlugin extends BaseCommandPlugin implements ActionListener {
  43. /** The notification methods that we know of. */
  44. private final List<String> methods = new ArrayList<String>();
  45. /** The user's preferred order for method usage. */
  46. private List<String> order;
  47. /** This plugin's plugin info. */
  48. private final PluginInfo pluginInfo;
  49. /**
  50. * Creates a new instance of this plugin.
  51. *
  52. * @param pluginInfo This plugin's plugin info
  53. * @param commandController Command controller to register commands
  54. */
  55. public NotificationsPlugin(final PluginInfo pluginInfo,
  56. final CommandController commandController) {
  57. super(commandController);
  58. this.pluginInfo = pluginInfo;
  59. registerCommand(new NotificationCommand(this),
  60. NotificationCommand.INFO);
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public void onLoad() {
  65. methods.clear();
  66. loadSettings();
  67. ActionManager.getActionManager().registerListener(this,
  68. CoreActionType.PLUGIN_LOADED, CoreActionType.PLUGIN_UNLOADED);
  69. for (PluginInfo target : pluginInfo.getMetaData().getManager()
  70. .getPluginInfos()) {
  71. if (target.isLoaded()) {
  72. addPlugin(target);
  73. }
  74. }
  75. super.onLoad();
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public void onUnload() {
  80. methods.clear();
  81. ActionManager.getActionManager().unregisterListener(this);
  82. super.onUnload();
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void showConfig(final PreferencesDialogModel manager) {
  87. final NotificationConfig configPanel = UIUtilities.invokeAndWait(
  88. new Callable<NotificationConfig>() {
  89. /** {@inheritDoc} */
  90. @Override
  91. public NotificationConfig call() {
  92. return new NotificationConfig(NotificationsPlugin.this,
  93. order);
  94. }
  95. });
  96. final PreferencesCategory category = new PluginPreferencesCategory(
  97. pluginInfo, "Notifications", "", "category-notifications",
  98. configPanel);
  99. manager.getCategory("Plugins").addSubCategory(category);
  100. }
  101. /** Loads the plugins settings. */
  102. private void loadSettings() {
  103. if (IdentityManager.getIdentityManager().getGlobalConfiguration()
  104. .hasOptionString(getDomain(), "methodOrder")) {
  105. order = IdentityManager.getIdentityManager()
  106. .getGlobalConfiguration().getOptionList(getDomain(), "methodOrder");
  107. } else {
  108. order = new ArrayList<String>();
  109. }
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public void processEvent(final ActionType type, final StringBuffer format,
  114. final Object... arguments) {
  115. if (type == CoreActionType.PLUGIN_LOADED) {
  116. addPlugin((PluginInfo) arguments[0]);
  117. } else if (type == CoreActionType.PLUGIN_UNLOADED) {
  118. removePlugin((PluginInfo) arguments[0]);
  119. }
  120. }
  121. /**
  122. * Checks to see if a plugin implements the notification method interface
  123. * and if it does, adds the method to our list.
  124. *
  125. * @param target The plugin to be tested
  126. */
  127. private void addPlugin(final PluginInfo target) {
  128. if (target.hasExportedService("showNotification")) {
  129. methods.add(target.getMetaData().getName());
  130. addMethodToOrder(target);
  131. }
  132. }
  133. /**
  134. * Checks to see if the specified notification method needs to be added to
  135. * our order list, and adds it if neccessary.
  136. *
  137. * @param source The notification method to be tested
  138. */
  139. private void addMethodToOrder(final PluginInfo source) {
  140. if (!order.contains(source.getMetaData().getName())) {
  141. order.add(source.getMetaData().getName());
  142. }
  143. }
  144. /**
  145. * Checks to see if a plugin implements the notification method interface
  146. * and if it does, removes the method from our list.
  147. *
  148. * @param target The plugin to be tested
  149. */
  150. private void removePlugin(final PluginInfo target) {
  151. methods.remove(target.getMetaData().getName());
  152. }
  153. /**
  154. * Retrieves a method based on its name.
  155. *
  156. * @param name The name to search for
  157. * @return The method with the specified name or null if none were found.
  158. */
  159. public PluginInfo getMethod(final String name) {
  160. return pluginInfo.getMetaData().getManager().getPluginInfoByName(name);
  161. }
  162. /**
  163. * Retrieves all the methods registered with this plugin.
  164. *
  165. * @return All known notification sources
  166. */
  167. public List<PluginInfo> getMethods() {
  168. final List<PluginInfo> plugins = new ArrayList<PluginInfo>();
  169. for (String method : methods) {
  170. plugins.add(pluginInfo.getMetaData().getManager()
  171. .getPluginInfoByName(method));
  172. }
  173. return plugins;
  174. }
  175. /**
  176. * Does this plugin have any active notification methods?
  177. *
  178. * @return true iif active notification methods are registered
  179. */
  180. public boolean hasActiveMethod() {
  181. return !methods.isEmpty();
  182. }
  183. /**
  184. * Returns the user's preferred method if loaded, or null if none loaded.
  185. *
  186. * @return Preferred notification method
  187. */
  188. public PluginInfo getPreferredMethod() {
  189. if (methods.isEmpty()) {
  190. return null;
  191. }
  192. for (String method : order) {
  193. if (methods.contains(method)) {
  194. return pluginInfo.getMetaData().getManager().getPluginInfoByName(
  195. method);
  196. }
  197. }
  198. return null;
  199. }
  200. /**
  201. * Saves the plugins settings.
  202. *
  203. * @param newOrder The new order for methods
  204. */
  205. protected void saveSettings(final List<String> newOrder) {
  206. order = newOrder;
  207. IdentityManager.getIdentityManager().getGlobalConfigIdentity()
  208. .setOption(getDomain(), "methodOrder", order);
  209. }
  210. }