Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PluginComponent.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.updater.components;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.plugins.PluginInfo;
  26. import com.dmdirc.updater.FileComponent;
  27. import com.dmdirc.updater.UpdateChecker;
  28. import com.dmdirc.updater.UpdateComponent;
  29. import com.dmdirc.updater.Version;
  30. import java.io.File;
  31. /**
  32. * An update component for plugins.
  33. */
  34. public class PluginComponent implements UpdateComponent, FileComponent {
  35. /** The config to use. */
  36. private static final ConfigManager CONFIG = IdentityManager.getGlobalConfig();
  37. /** The plugin this component is for. */
  38. private final PluginInfo plugin;
  39. /**
  40. * Creates a new PluginComponent for the specified plugin, to enable it to
  41. * be updated automatically.
  42. *
  43. * @param plugin The plugin to be added to the updater
  44. */
  45. public PluginComponent(final PluginInfo plugin) {
  46. this.plugin = plugin;
  47. if ((plugin.getMetaData().getUpdaterId() > 0 && plugin.getMetaData().getVersion().isValid())
  48. || (CONFIG.hasOptionInt("plugin-addonid", plugin.getMetaData().getName()))) {
  49. UpdateChecker.removeComponent(getName());
  50. UpdateChecker.registerComponent(this);
  51. }
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. public String getName() {
  56. if (plugin.getMetaData().getUpdaterId() > 0) {
  57. return "addon-" + plugin.getMetaData().getUpdaterId();
  58. } else {
  59. return "addon-" + CONFIG.getOption("plugin-addonid", plugin.getMetaData().getName());
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public String getFriendlyName() {
  65. return plugin.getMetaData().getFriendlyName();
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. public String getFriendlyVersion() {
  70. return plugin.getMetaData().getFriendlyVersion();
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public Version getVersion() {
  75. return plugin.getMetaData().getVersion();
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public boolean requiresRestart() {
  80. return !plugin.isUnloadable() && plugin.isLoaded();
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public boolean requiresManualInstall() {
  85. return false;
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public String getManualInstructions(final String path) {
  90. return "";
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public boolean doInstall(final String path) {
  95. final File target = new File(plugin.getMetaData().getPluginUrl().getPath());
  96. boolean returnCode = false;
  97. final boolean wasLoaded = plugin.isLoaded();
  98. // Unload old version of plugin before we update the metadata.
  99. if (wasLoaded && plugin.isUnloadable()) {
  100. plugin.unloadPlugin();
  101. }
  102. if ((plugin.isUnloadable() || !plugin.isLoaded()) && target.exists()) {
  103. target.delete();
  104. }
  105. // Try and move the downloaded plugin to the new location.
  106. // If it doesn't work then keep the plugin in a .update file untill
  107. // the next restart.
  108. // If it does, update the metadata.
  109. if (requiresRestart() || !new File(path).renameTo(target)) {
  110. // Windows rocks!
  111. final File newTarget = new File(plugin.getMetaData().getPluginUrl().getPath() + ".update");
  112. if (newTarget.exists()) {
  113. newTarget.delete();
  114. }
  115. new File(path).renameTo(newTarget);
  116. returnCode = true;
  117. } else {
  118. plugin.pluginUpdated();
  119. }
  120. // If the plugin was loaded before, load it again.
  121. if (wasLoaded) { plugin.loadPlugin(); }
  122. return returnCode;
  123. }
  124. @Override
  125. public String getFileName() {
  126. return plugin.getFilename();
  127. }
  128. }