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.

ClientComponent.java 5.3KB

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.Main;
  19. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  20. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  21. import com.dmdirc.events.StatusBarMessageEvent;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import com.dmdirc.interfaces.config.IdentityController;
  24. import com.dmdirc.ui.StatusMessage;
  25. import com.dmdirc.updater.UpdateComponent;
  26. import com.dmdirc.updater.Version;
  27. import com.dmdirc.util.io.FileUtils;
  28. import java.io.IOException;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import javax.inject.Inject;
  32. /**
  33. * Represents the client component, which covers the core client resources.
  34. */
  35. public class ClientComponent implements UpdateComponent {
  36. /** The controller to read settings from. */
  37. private final IdentityController identityController;
  38. /** The event bus to post messages to. */
  39. private final EventBus eventBus;
  40. /** Base directory to move updates to. */
  41. private final Path baseDirectory;
  42. /**
  43. * Creates a new instance of {@link ClientComponent}.
  44. *
  45. * @param identityController The controller to read settings from.
  46. * @param eventBus The event bus to post messages to.
  47. */
  48. @Inject
  49. public ClientComponent(
  50. final IdentityController identityController,
  51. final EventBus eventBus,
  52. @Directory(DirectoryType.BASE) final Path baseDirectory) {
  53. this.identityController = identityController;
  54. this.eventBus = eventBus;
  55. this.baseDirectory = baseDirectory;
  56. }
  57. @Override
  58. public String getName() {
  59. return "client";
  60. }
  61. @Override
  62. public String getFriendlyName() {
  63. return "DMDirc client";
  64. }
  65. @Override
  66. public Version getVersion() {
  67. return new Version(getFriendlyVersion());
  68. }
  69. @Override
  70. public boolean requiresRestart() {
  71. return true;
  72. }
  73. @Override
  74. public boolean requiresManualInstall() {
  75. return !LauncherComponent.isUsingLauncher();
  76. }
  77. @Override
  78. public String getManualInstructions(final Path path) {
  79. final Path targetFile = baseDirectory.resolve(".DMDirc.jar");
  80. if (requiresManualInstall()) {
  81. if (FileUtils.isRunningFromJar(Main.class)) {
  82. return "A new version of DMDirc has been downloaded, but as you\n"
  83. + "do not seem to be using the DMDirc launcher, it will\n"
  84. + "not be installed automatically.\n\n"
  85. + "To install this update manually, please replace the\n"
  86. + "existing DMDirc.jar file, located at:\n"
  87. + ' ' + FileUtils.getApplicationPath(Main.class)
  88. + "\n with the following file:\n "
  89. + targetFile.toAbsolutePath();
  90. } else {
  91. return "A new version of DMDirc has been downloaded, but as you\n"
  92. + "do not seem to be using the DMDirc launcher, it will\n"
  93. + "not be installed automatically.\n\n"
  94. + "To install this update manually, please extract the\n"
  95. + "new DMDirc.jar file, located at:\n"
  96. + ' ' + targetFile.toAbsolutePath() + '\n'
  97. + "over your existing DMDirc install located in:\n"
  98. + " " + FileUtils.getApplicationPath(Main.class);
  99. }
  100. }
  101. return "";
  102. }
  103. @Override
  104. public String getFriendlyVersion() {
  105. return identityController.getGlobalConfiguration().getOption("version", "version");
  106. }
  107. @Override
  108. public boolean doInstall(final Path path) throws IOException {
  109. final Path targetFile = baseDirectory.resolve(".DMDirc.jar");
  110. Files.deleteIfExists(targetFile);
  111. Files.move(path, targetFile);
  112. if (requiresManualInstall()) {
  113. // @deprecated Should be removed when updater UI changes are
  114. // implemented.
  115. final String message = getManualInstructions(path);
  116. eventBus.publishAsync(new StatusBarMessageEvent(new StatusMessage(message,
  117. identityController.getGlobalConfiguration())));
  118. }
  119. return true;
  120. }
  121. }