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

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