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.

LicenseLoader.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. *
  3. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.dialogs.about;
  24. import com.dmdirc.addons.ui_swing.components.GenericListModel;
  25. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import com.dmdirc.plugins.PluginInfo;
  29. import com.dmdirc.plugins.PluginManager;
  30. import com.dmdirc.util.resourcemanager.ResourceManager;
  31. import java.io.BufferedReader;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.InputStreamReader;
  35. import java.util.Map;
  36. import java.util.Map.Entry;
  37. import java.util.TreeMap;
  38. /**
  39. * Background loader of licenses into a list.
  40. */
  41. public class LicenseLoader extends LoggingSwingWorker<Void, Void> {
  42. /** Model to load licenses into. */
  43. private GenericListModel<License> model;
  44. /**
  45. * Instantiates a new license loader.
  46. *
  47. * @param model Model to load licenses into
  48. */
  49. public LicenseLoader(final GenericListModel<License> model) {
  50. this.model = model;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. *
  55. * @throws Exception on any exception in the background task
  56. */
  57. @Override
  58. protected Void doInBackground() throws Exception {
  59. final ResourceManager rm = ResourceManager.getResourceManager();
  60. if (rm == null) {
  61. Logger.userError(ErrorLevel.LOW, "Unable to load licenses, " +
  62. "no resource manager");
  63. } else {
  64. final Map<String, InputStream> licenses =
  65. new TreeMap<String, InputStream>(String.CASE_INSENSITIVE_ORDER);
  66. licenses.putAll(rm.getResourcesStartingWithAsInputStreams(
  67. "com/dmdirc/licenses/"));
  68. for (PluginInfo pi : PluginManager.getPluginManager().getPluginInfos()) {
  69. licenses.putAll(pi.getLicenseStreams());
  70. }
  71. for (Entry<String, InputStream> entry : licenses.entrySet()) {
  72. final String licenseString = entry.getKey().substring(entry.
  73. getKey().
  74. lastIndexOf('/') + 1);
  75. if (licenseString.length() > 1) {
  76. final String licenseStringParts[] = licenseString.split(
  77. " - ");
  78. final License license = new License(licenseStringParts[1],
  79. licenseStringParts[0], "<html><h1>" +
  80. licenseStringParts[1] + "</h1><p>" + readInputStream(
  81. entry.getValue()).replaceAll("\n", "<br>") +
  82. "</p></html>");
  83. model.add(license);
  84. }
  85. }
  86. }
  87. return null;
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. protected void done() {
  92. super.done();
  93. }
  94. /**
  95. * Converts an input stream into a string.
  96. *
  97. * @param stream Stream to convert
  98. *
  99. * @return Contents of the input stream
  100. */
  101. private String readInputStream(final InputStream stream) {
  102. String line;
  103. final BufferedReader input =
  104. new BufferedReader(new InputStreamReader(stream));
  105. final StringBuilder text = new StringBuilder();
  106. try {
  107. line = input.readLine();
  108. while (line != null) {
  109. text.append(line).append("\n");
  110. line = input.readLine();
  111. }
  112. } catch (IOException ex) {
  113. //Ignore
  114. }
  115. return text.toString();
  116. }
  117. }