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.

BasePlugin.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.plugins;
  23. import com.dmdirc.commandparser.CommandInfo;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.config.prefs.PreferencesDialogModel;
  27. import com.dmdirc.util.validators.ValidationResponse;
  28. import java.io.File;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.Map.Entry;
  32. /**
  33. * Base implementation of the Plugin interface.
  34. */
  35. public abstract class BasePlugin implements Plugin {
  36. /** Domain name for the settings in this plugin. */
  37. private String myDomain = "plugin-unknown";
  38. /** Has the domain been set? */
  39. private boolean domainSet;
  40. /** Associated Plugin info. */
  41. private PluginInfo pluginInfo;
  42. /** Files directory for this plugin. */
  43. private File filesDir;
  44. /** List of commands to load and unload. */
  45. private final Map<CommandInfo, Command> commands =
  46. new HashMap<CommandInfo, Command>();
  47. /** {@inheritDoc} */
  48. @Override
  49. public void setDomain(final String newDomain) {
  50. if (!domainSet) {
  51. domainSet = true;
  52. myDomain = newDomain;
  53. domainUpdated();
  54. }
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public void onLoad() {
  59. loadCommands();
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public void onUnload() {
  64. unloadCommands();
  65. }
  66. /**
  67. * Registers a command from this plugin.
  68. *
  69. * @param command Command to register
  70. * @param commandInfo Command info to register
  71. */
  72. protected void registerCommand(final Command command,
  73. final CommandInfo commandInfo) {
  74. commands.put(commandInfo, command);
  75. }
  76. /**
  77. * Unregisters a command from this plugin.
  78. *
  79. * @param commandInfo Command info to register
  80. */
  81. protected void unregisterCommand(final CommandInfo commandInfo) {
  82. commands.remove(commandInfo);
  83. CommandManager.getCommandManager().unregisterCommand(commandInfo);
  84. }
  85. /** Loads the commands provided by this plugin. */
  86. private void loadCommands() {
  87. for (Entry<CommandInfo, Command> command : commands.entrySet()) {
  88. CommandManager.getCommandManager().registerCommand(
  89. command.getValue(), command.getKey());
  90. }
  91. }
  92. /** Unloads the commands loaded by this plugin. */
  93. private void unloadCommands() {
  94. for (CommandInfo command : commands.keySet()) {
  95. CommandManager.getCommandManager().unregisterCommand(command);
  96. }
  97. }
  98. /**
  99. * {@inheritDoc}
  100. *
  101. * @deprecated PluginInfo should be obtained using a constructor parameter,
  102. * if required
  103. */
  104. @Override
  105. @Deprecated
  106. public void setPluginInfo(final PluginInfo pluginInfo) {
  107. this.pluginInfo = pluginInfo;
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public PluginInfo getPluginInfo() {
  112. return pluginInfo;
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public String getDomain() {
  117. return myDomain;
  118. }
  119. /**
  120. * Called when the domain for plugin settings has been set.
  121. * This will only be called once (either when the plugin is loading, or when
  122. * its config is being shown).
  123. */
  124. protected void domainUpdated() {
  125. //Define this here so only implementations that care have to override
  126. }
  127. /**
  128. * Get the files directory for this plugin.
  129. * This will attempt to create the directory if it doesn't exist the first
  130. * time the directory name is requested.
  131. *
  132. * @return Files directory for this plugin.
  133. */
  134. protected File getFilesDir() {
  135. if (filesDir == null) {
  136. final String fs = System.getProperty("file.separator");
  137. final String dir = PluginManager.getPluginManager()
  138. .getFilesDirectory();
  139. filesDir = new File(dir + pluginInfo.getMetaData().getName() + fs);
  140. if (!filesDir.exists()) {
  141. filesDir.mkdirs();
  142. }
  143. }
  144. return filesDir;
  145. }
  146. /**
  147. * Convenience Method.
  148. *
  149. * @return Filesdir as a string with trailing path separator
  150. */
  151. protected String getFilesDirString() {
  152. return getFilesDir().getAbsolutePath()
  153. + System.getProperty("file.separator");
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public ValidationResponse checkPrerequisites() {
  158. return new ValidationResponse();
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public void showConfig(final PreferencesDialogModel manager) {
  163. //Define this here so only implementations that care have to override
  164. }
  165. }