Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PluginComponent.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2014 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.interfaces.config.AggregateConfigProvider;
  24. import com.dmdirc.plugins.PluginInfo;
  25. import com.dmdirc.updater.UpdateComponent;
  26. import com.dmdirc.updater.Version;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.zip.ZipFile;
  30. /**
  31. * An update component for plugins.
  32. */
  33. public class PluginComponent implements UpdateComponent {
  34. /** The config to use. */
  35. private final AggregateConfigProvider globalConfig;
  36. /** The plugin this component is for. */
  37. private final PluginInfo plugin;
  38. public PluginComponent(final AggregateConfigProvider globalConfig, final PluginInfo plugin) {
  39. this.globalConfig = globalConfig;
  40. this.plugin = plugin;
  41. }
  42. /** {@inheritDoc} */
  43. @Override
  44. public String getName() {
  45. if (plugin.getMetaData().getUpdaterId() > 0) {
  46. return "addon-" + plugin.getMetaData().getUpdaterId();
  47. } else {
  48. return "addon-" + globalConfig.getOption("plugin-addonid", plugin.getMetaData().
  49. getName());
  50. }
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public String getFriendlyName() {
  55. return plugin.getMetaData().getFriendlyName();
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public String getFriendlyVersion() {
  60. return plugin.getMetaData().getFriendlyVersion();
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public Version getVersion() {
  65. return plugin.getMetaData().getVersion();
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. public boolean requiresRestart() {
  70. return plugin.isLoaded();
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public boolean requiresManualInstall() {
  75. return false;
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public String getManualInstructions(final String path) {
  80. return "";
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public boolean doInstall(final String path) {
  85. final File target = new File(plugin.getMetaData().getPluginUrl().getPath());
  86. boolean returnCode = false;
  87. final boolean wasLoaded = plugin.isLoaded();
  88. if (!wasLoaded && target.exists()) {
  89. target.delete();
  90. }
  91. // Try and move the downloaded plugin to the new location.
  92. // If it doesn't work then keep the plugin in a .update file until the next restart.
  93. // If it does, update the metadata.
  94. final File newPlugin = new File(path);
  95. if (!isValid(newPlugin)) {
  96. return false;
  97. }
  98. if (requiresRestart() || !newPlugin.renameTo(target)) {
  99. // Windows rocks!
  100. final File newTarget = new File(plugin.getMetaData().getPluginUrl().getPath()
  101. + ".update");
  102. if (newTarget.exists()) {
  103. newTarget.delete();
  104. }
  105. new File(path).renameTo(newTarget);
  106. returnCode = true;
  107. } else {
  108. plugin.pluginUpdated();
  109. }
  110. return returnCode;
  111. }
  112. /**
  113. * Test is a file is a valid zip file.
  114. *
  115. * @param file Zip file
  116. *
  117. * @return true if the file is valid
  118. */
  119. private boolean isValid(final File file) {
  120. try (ZipFile ignored = new ZipFile(file)) {
  121. return true;
  122. } catch (IOException e) {
  123. return false;
  124. }
  125. }
  126. }