Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CoreAboutDialogModel.java 7.7KB

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