選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NotificationsPlugin.java 7.6KB

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