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.

Plugin.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.plugins;
  23. import com.dmdirc.config.prefs.PreferencesDialogModel;
  24. import com.dmdirc.util.validators.ValidationResponse;
  25. import java.io.File;
  26. /**
  27. * Defines the standard methods that should be implemented by plugins.
  28. */
  29. public abstract class Plugin implements Comparable<Plugin> {
  30. /** Domain name for the settings in this plugin. */
  31. private String myDomain = "plugin-unknown";
  32. /** Has the domain been set? */
  33. private boolean domainSet = false;
  34. /** Associated Plugin info. */
  35. private PluginInfo pluginInfo;
  36. /** Files directory for this plugin. */
  37. private File filesDir = null;
  38. /**
  39. * Called by PluginInfo to set the domain name.
  40. * This can only be called once, all other attempts will be ignored.
  41. *
  42. * @param newDomain Domain name for plugin settings
  43. */
  44. public void setDomain(final String newDomain) {
  45. if (!domainSet) {
  46. domainSet = true;
  47. myDomain = newDomain;
  48. domainUpdated();
  49. }
  50. }
  51. /**
  52. * Sets the associated plugin info for this plugin.
  53. *
  54. * @param pluginInfo Associated plugin info
  55. */
  56. public void setPluginInfo(final PluginInfo pluginInfo) {
  57. this.pluginInfo = pluginInfo;
  58. }
  59. /**
  60. * Returns the plugin info associated with this plugin.
  61. *
  62. * @return Plugin info or null
  63. */
  64. public PluginInfo getPluginInfo() {
  65. return pluginInfo;
  66. }
  67. /**
  68. * Get the domain name settings for this plugin should be stored in.
  69. *
  70. * @return Domain name for plugin settings
  71. */
  72. public String getDomain() {
  73. return myDomain;
  74. }
  75. /**
  76. * Called when the domain for plugin settings has been set.
  77. * This will only be called once (either when the plugin is loading, or when
  78. * its config is being shown).
  79. */
  80. public void domainUpdated() {
  81. }
  82. /**
  83. * Get the files directory for this plugin.
  84. * This will attempt to create the directory if it doesn't exist the first
  85. * time the directory name is requested.
  86. *
  87. * @return Files directory for this plugin.
  88. */
  89. public File getFilesDir() {
  90. if (filesDir == null) {
  91. final String fs = System.getProperty("file.separator");
  92. final String dir = PluginManager.getPluginManager().getFilesDirectory();
  93. filesDir = new File(dir + pluginInfo.getName() + fs);
  94. if (!filesDir.exists()) {
  95. filesDir.mkdirs();
  96. }
  97. }
  98. return filesDir;
  99. }
  100. /**
  101. * Convenience Method.
  102. *
  103. * @return Filesdir as a string with trailing path separator
  104. */
  105. public String getFilesDirString() {
  106. return getFilesDir().getAbsolutePath() + System.getProperty("file.separator");
  107. }
  108. /**
  109. * Called when the plugin is loaded.
  110. */
  111. public abstract void onLoad();
  112. /**
  113. * Check any further Prerequisites for this plugin to load that can not be
  114. * checked using metainfo.
  115. *
  116. * @return ValidationResponse detailign if the plugin passes any extra checks
  117. * that plugin.info can't handle
  118. */
  119. public ValidationResponse checkPrerequisites() {
  120. return new ValidationResponse();
  121. }
  122. /**
  123. * Called when the plugin is about to be unloaded.
  124. */
  125. public abstract void onUnload();
  126. /**
  127. * Called to allow plugins to add their configuration options to the manager.
  128. * PreferencesCategories added from this method should be of type
  129. * {@link com.dmdirc.config.prefs.PluginPreferencesCategory} as this gives
  130. * the user feedback on the status of your plugin.
  131. *
  132. * @param manager The preferences manager that configuration options
  133. * need to be added to.
  134. */
  135. public void showConfig(final PreferencesDialogModel manager) {
  136. }
  137. /**
  138. * Compares this object with the specified object for order.
  139. * Returns a negative integer, zero, or a positive integer as per String.compareTo();
  140. *
  141. * @param o Object to compare to
  142. * @return a negative integer, zero, or a positive integer.
  143. */
  144. @Override
  145. public int compareTo(final Plugin o) {
  146. return toString().compareTo(o.toString());
  147. }
  148. }