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.

CoreAboutDialogModel.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.ui.core.about;
  18. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  19. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  20. import com.dmdirc.commandline.CommandLineParser;
  21. import com.dmdirc.events.ClientInfoRequestEvent;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import com.dmdirc.interfaces.ui.AboutDialogModel;
  24. import com.dmdirc.plugins.PluginManager;
  25. import com.dmdirc.util.ClientInfo;
  26. import com.dmdirc.util.DateUtils;
  27. import com.dmdirc.util.io.FileUtils;
  28. import com.google.common.base.Joiner;
  29. import com.google.common.collect.Lists;
  30. import java.io.IOException;
  31. import java.net.URISyntaxException;
  32. import java.nio.charset.Charset;
  33. import java.nio.file.DirectoryStream;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import javax.inject.Inject;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42. public class CoreAboutDialogModel implements AboutDialogModel {
  43. private static final Logger LOGGER = LoggerFactory.getLogger(CoreAboutDialogModel.class);
  44. private final Path baseDirectory;
  45. private final ClientInfo clientInfo;
  46. private final EventBus eventBus;
  47. private final PluginManager pluginManager;
  48. private final CommandLineParser commandLineParser;
  49. private String about;
  50. private List<Developer> mainDevelopers;
  51. private List<Developer> otherDevelopers;
  52. private List<InfoItem> info;
  53. private List<LicensedComponent> licences;
  54. @Inject
  55. public CoreAboutDialogModel(@Directory(DirectoryType.BASE) final Path baseDirectory,
  56. final ClientInfo clientInfo, final EventBus eventBus,
  57. final PluginManager pluginManager, final CommandLineParser commandLineParser) {
  58. this.baseDirectory = baseDirectory;
  59. this.clientInfo = clientInfo;
  60. this.eventBus = eventBus;
  61. this.pluginManager = pluginManager;
  62. this.commandLineParser = commandLineParser;
  63. about = "";
  64. mainDevelopers = new ArrayList<>();
  65. otherDevelopers = new ArrayList<>();
  66. info = new ArrayList<>();
  67. licences = new ArrayList<>();
  68. }
  69. @Override
  70. public void load() {
  71. about = "<html><center><h1 style=\"margin-bottom: 0px;\">DMDirc</h1>" +
  72. "<span style=\"font-style: italic;\">The intelligent IRC client.</span>" +
  73. "<p>Easy to use, cross-platform IRC client.</p>" +
  74. "<p><a href=\"https://www.dmdirc.com\">www.dmdirc.com</a></p></center></html>";
  75. mainDevelopers = createMainDevelopers();
  76. otherDevelopers = createOtherDevelopers();
  77. info = createInfoItems();
  78. final ClientInfoRequestEvent event = new ClientInfoRequestEvent();
  79. eventBus.publish(event);
  80. info.addAll(event.getNewInfoItems());
  81. licences = createLicensedComponents();
  82. }
  83. private List<Developer> createMainDevelopers() {
  84. return Lists.newArrayList(Developer.create("Chris 'MD87' Smith", "https://www.md87.co.uk"),
  85. Developer.create("Gregory 'Greboid' Holmes", "https://www.greboid.com"),
  86. Developer.create("Shane 'Dataforce' Mc Cormack", "http://home.dataforce.org.uk"));
  87. }
  88. private List<Developer> createOtherDevelopers() {
  89. return Lists.newArrayList(
  90. Developer.create("Simon 'Demented-Idiot' Mott", "http://simonmott.co.uk/"));
  91. }
  92. private List<InfoItem> createInfoItems() {
  93. return Lists
  94. .newArrayList(InfoItem.create("DMDirc version", clientInfo.getVersionInformation()),
  95. InfoItem.create("OS Version", clientInfo.getOperatingSystemInformation()),
  96. InfoItem.create("Profile directory", baseDirectory.toString()),
  97. InfoItem.create("Java version", clientInfo.getJavaInformation()),
  98. InfoItem.create("Java Default charset",
  99. Charset.defaultCharset().displayName()),
  100. InfoItem.create("Client Uptime",
  101. DateUtils.formatDuration((int) clientInfo.getUptime() / 1000)),
  102. InfoItem.create("Launcher Version",
  103. commandLineParser.getLauncherVersion().orElse("Unknown")));
  104. }
  105. private List<LicensedComponent> createLicensedComponents() {
  106. final List<LicensedComponent> components = new ArrayList<>();
  107. try {
  108. components.add(LicensedComponent.create("DMDirc", getLicences(FileUtils
  109. .getPathForResource(getClass().getResource("/com/dmdirc/licences")))));
  110. pluginManager.getPluginInfos().forEach(p -> {
  111. final List<Licence> componentLicences = getLicences(
  112. p.getPath("/META-INF/licences/"));
  113. if (!componentLicences.isEmpty()) {
  114. components.add(LicensedComponent
  115. .create(p.getMetaData().getFriendlyName(), componentLicences));
  116. }
  117. }
  118. );
  119. } catch (URISyntaxException ex) {
  120. LOGGER.warn("Unable to create component", ex);
  121. }
  122. return components;
  123. }
  124. private List<Licence> getLicences(final Path path) {
  125. final List<Licence> componentLicences = new ArrayList<>();
  126. try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
  127. stream.forEach(p -> {
  128. try {
  129. final String filename = p.getFileName().toString();
  130. final String[] parts = filename.split(" - ");
  131. componentLicences.add(Licence.create(parts[0], parts[1],
  132. Joiner.on('\n').join(Files.readAllLines(p))));
  133. } catch (IOException ex) {
  134. LOGGER.warn("Error reading licence", ex);
  135. }
  136. });
  137. } catch (IOException ex) {
  138. LOGGER.warn("Error reading licence directory", ex);
  139. }
  140. return componentLicences;
  141. }
  142. @Override
  143. public String getAbout() {
  144. return about;
  145. }
  146. @Override
  147. public List<Developer> getMainDevelopers() {
  148. return Collections.unmodifiableList(mainDevelopers);
  149. }
  150. @Override
  151. public List<Developer> getOtherDevelopers() {
  152. return Collections.unmodifiableList(otherDevelopers);
  153. }
  154. @Override
  155. public List<InfoItem> getInfo() {
  156. return Collections.unmodifiableList(info);
  157. }
  158. @Override
  159. public List<LicensedComponent> getLicensedComponents() {
  160. return Collections.unmodifiableList(licences);
  161. }
  162. }