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.

InstallWorker.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.components.addonbrowser;
  23. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  24. import com.dmdirc.plugins.PluginManager;
  25. import com.dmdirc.util.io.Downloader;
  26. import java.io.IOException;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.nio.file.Paths;
  30. import java.util.concurrent.ExecutionException;
  31. /**
  32. * Downloads a given addon from the addon site and loads it into the client.
  33. */
  34. public class InstallWorker extends LoggingSwingWorker<String, Void> {
  35. /** Addon info. */
  36. private final AddonInfo info;
  37. /** Window to show installer progress. */
  38. private final InstallerWindow installer;
  39. /** Temporary directory to download files to. */
  40. private final String tempDirectory;
  41. /** Directory to install plugins in. */
  42. private final String pluginDirectory;
  43. /** Directory to install themes in. */
  44. private final String themeDirectory;
  45. /** Plugin manager to inform of new plugins. */
  46. private final PluginManager pluginManager;
  47. /** Downloader to download files. */
  48. private final Downloader downloader;
  49. public InstallWorker(
  50. final Downloader downloader,
  51. final String tempDirectory,
  52. final String pluginDirectory,
  53. final String themeDirectory,
  54. final PluginManager pluginManager,
  55. final AddonInfo info,
  56. final InstallerWindow window) {
  57. this.downloader = downloader;
  58. this.info = info;
  59. this.installer = window;
  60. this.tempDirectory = tempDirectory;
  61. this.pluginDirectory = pluginDirectory;
  62. this.themeDirectory = themeDirectory;
  63. this.pluginManager = pluginManager;
  64. }
  65. @Override
  66. protected String doInBackground() {
  67. try {
  68. final Path file = Paths.get(tempDirectory, "." + info.getId());
  69. downloader.downloadPage(info.getDownload(), file);
  70. switch (info.getType()) {
  71. case TYPE_PLUGIN:
  72. final Path newFile = Paths.get(pluginDirectory, info.getTitle() + ".jar");
  73. try {
  74. Files.move(file, newFile);
  75. pluginManager.refreshPlugins();
  76. } catch (IOException ex) {
  77. return "Unable to install addon, failed to move file: "
  78. + file.toAbsolutePath();
  79. }
  80. break;
  81. case TYPE_THEME:
  82. try {
  83. Files.move(file, Paths.get(themeDirectory, info.getTitle() + ".zip"));
  84. } catch (IOException ex) {
  85. return "Unable to install addon, failed to move file: "
  86. + file.toAbsolutePath();
  87. }
  88. break;
  89. default:
  90. return "Unknown addon type";
  91. }
  92. } catch (IOException ex) {
  93. return "Unable to download addon: " + ex.getMessage();
  94. }
  95. return "";
  96. }
  97. @Override
  98. protected void done() {
  99. String message = "";
  100. try {
  101. message = get();
  102. } catch (InterruptedException ex) {
  103. message = "Interrupted during execution, unable to complete.";
  104. } catch (ExecutionException ex) {
  105. message = "Unable to install: " + ex.getMessage();
  106. } finally {
  107. if (message.isEmpty()) {
  108. installer.finished("");
  109. } else {
  110. installer.finished(message);
  111. }
  112. }
  113. }
  114. }