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.

PluginComponent.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.updater.components;
  18. import com.dmdirc.config.provider.AggregateConfigProvider;
  19. import com.dmdirc.plugins.PluginException;
  20. import com.dmdirc.plugins.PluginInfo;
  21. import com.dmdirc.updater.UpdateComponent;
  22. import com.dmdirc.updater.Version;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.nio.file.Path;
  26. import java.util.zip.ZipFile;
  27. /**
  28. * An update component for plugins.
  29. */
  30. public class PluginComponent implements UpdateComponent {
  31. /** The config to use. */
  32. private final AggregateConfigProvider globalConfig;
  33. /** The plugin this component is for. */
  34. private final PluginInfo plugin;
  35. public PluginComponent(final AggregateConfigProvider globalConfig, final PluginInfo plugin) {
  36. this.globalConfig = globalConfig;
  37. this.plugin = plugin;
  38. }
  39. @Override
  40. public String getName() {
  41. if (plugin.getMetaData().getUpdaterId() > 0) {
  42. return "addon-" + plugin.getMetaData().getUpdaterId();
  43. } else {
  44. return "addon-" + globalConfig.getOption("plugin-addonid", plugin.getMetaData().
  45. getName());
  46. }
  47. }
  48. @Override
  49. public String getFriendlyName() {
  50. return plugin.getMetaData().getFriendlyName();
  51. }
  52. @Override
  53. public String getFriendlyVersion() {
  54. return plugin.getMetaData().getFriendlyVersion();
  55. }
  56. @Override
  57. public Version getVersion() {
  58. return plugin.getMetaData().getVersion();
  59. }
  60. @Override
  61. public boolean requiresRestart() {
  62. return plugin.isLoaded();
  63. }
  64. @Override
  65. public boolean requiresManualInstall() {
  66. return false;
  67. }
  68. @Override
  69. public String getManualInstructions(final Path path) {
  70. return "";
  71. }
  72. @Override
  73. public boolean doInstall(final Path path) {
  74. final File target = plugin.getMetaData().getPluginPath().toFile();
  75. boolean returnCode = false;
  76. final boolean wasLoaded = plugin.isLoaded();
  77. if (!wasLoaded && target.exists()) {
  78. target.delete();
  79. }
  80. // Try and move the downloaded plugin to the new location.
  81. // If it doesn't work then keep the plugin in a .update file until the next restart.
  82. // If it does, update the metadata.
  83. final File newPlugin = path.toFile();
  84. if (!isValid(newPlugin)) {
  85. return false;
  86. }
  87. if (requiresRestart() || !newPlugin.renameTo(target)) {
  88. // Windows rocks!
  89. final File newTarget = new File(plugin.getMetaData().getPluginPath().toAbsolutePath()
  90. + ".update");
  91. if (newTarget.exists()) {
  92. newTarget.delete();
  93. }
  94. path.toFile().renameTo(newTarget);
  95. returnCode = true;
  96. } else {
  97. try {
  98. plugin.pluginUpdated();
  99. } catch (PluginException ex) {
  100. returnCode = true;
  101. }
  102. }
  103. return returnCode;
  104. }
  105. /**
  106. * Test is a file is a valid zip file.
  107. *
  108. * @param file Zip file
  109. *
  110. * @return true if the file is valid
  111. */
  112. private boolean isValid(final File file) {
  113. try (ZipFile ignored = new ZipFile(file)) {
  114. return true;
  115. } catch (IOException e) {
  116. return false;
  117. }
  118. }
  119. }