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.7KB

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