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.

DataLoaderWorker.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.addons.ui_swing.components.addonbrowser;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  20. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  21. import com.dmdirc.config.provider.AggregateConfigProvider;
  22. import com.dmdirc.updater.manager.UpdateManager;
  23. import com.dmdirc.util.URLBuilder;
  24. import com.dmdirc.util.io.ConfigFile;
  25. import com.dmdirc.util.io.DownloadListener;
  26. import com.dmdirc.util.io.Downloader;
  27. import com.dmdirc.util.io.InvalidConfigFileException;
  28. import java.io.IOException;
  29. import java.nio.file.Path;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.concurrent.ExecutionException;
  33. import java.util.stream.Collectors;
  34. import javax.swing.Box;
  35. import javax.swing.JPanel;
  36. import javax.swing.JProgressBar;
  37. import javax.swing.JScrollPane;
  38. import javax.swing.text.StyleConstants;
  39. import net.miginfocom.swing.MigLayout;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42. import static com.dmdirc.util.LogUtils.USER_ERROR;
  43. /**
  44. * Loads the addon data feed into the addon browser.
  45. */
  46. public class DataLoaderWorker
  47. extends LoggingSwingWorker<Collection<AddonInfo>, Object>
  48. implements DownloadListener {
  49. private static final Logger LOG = LoggerFactory.getLogger(DataLoaderWorker.class);
  50. /** List to load data into. */
  51. private final AddonTable table;
  52. /** Browser window to pass to addon info objects. */
  53. private final BrowserWindow browserWindow;
  54. /** Table's parent scrollpane. */
  55. private final JScrollPane scrollPane;
  56. /** Downloader progress bar. */
  57. private final JProgressBar jpb = new JProgressBar(0, 100);
  58. /** Refresh addons feed? */
  59. private final boolean download;
  60. /** Directory to store temporary files in. */
  61. private final Path tempDirectory;
  62. /** URL Builder to use when loading addon resources. */
  63. private final URLBuilder urlBuilder;
  64. /** Factory to use to produce install workers. */
  65. private final InstallWorkerFactory workerFactory;
  66. /** The manager to use to retrieve update information. */
  67. private final UpdateManager updateManager;
  68. /** Configuration to read settings from. */
  69. private final AggregateConfigProvider globalConfig;
  70. /** Downloader to download files. */
  71. private final Downloader downloader;
  72. /**
  73. * Creates a new data loader worker.
  74. *
  75. * @param downloader Used to download files
  76. * @param globalConfig Configuration to read settings from.
  77. * @param urlBuilder URL Builder to use when loading addon resources.
  78. * @param workerFactory Factory to use to produce install workers.
  79. * @param updateManager Manager to use to retrieve update information.
  80. * @param tempDirectory The directory to store temporary items in, such as the addons feed.
  81. * @param table Table to load data into
  82. * @param download Download new addons feed?
  83. * @param browserWindow Browser window to pass to table objects
  84. * @param scrollPane Table's parent scrollpane
  85. */
  86. public DataLoaderWorker(
  87. final Downloader downloader,
  88. final AggregateConfigProvider globalConfig,
  89. final URLBuilder urlBuilder,
  90. final InstallWorkerFactory workerFactory,
  91. final UpdateManager updateManager,
  92. final Path tempDirectory,
  93. final AddonTable table,
  94. final boolean download,
  95. final BrowserWindow browserWindow,
  96. final JScrollPane scrollPane) {
  97. this.downloader = downloader;
  98. this.globalConfig = globalConfig;
  99. this.urlBuilder = urlBuilder;
  100. this.workerFactory = workerFactory;
  101. this.download = download;
  102. this.table = table;
  103. this.updateManager = updateManager;
  104. this.tempDirectory = tempDirectory;
  105. this.browserWindow = browserWindow;
  106. this.scrollPane = scrollPane;
  107. }
  108. @Override
  109. protected Collection<AddonInfo> doInBackground() {
  110. final JPanel loadingPanel = new JPanel(
  111. new MigLayout("fill, alignx 50%, aligny 50%"));
  112. scrollPane.setViewportView(loadingPanel);
  113. if (download) {
  114. final TextLabel label = new TextLabel(
  115. "Downloading addon info, please wait...");
  116. label.setAlignment(StyleConstants.ALIGN_CENTER);
  117. loadingPanel.add(Box.createVerticalGlue(), "growy, pushy, wrap");
  118. loadingPanel.add(label, "growx, wrap");
  119. loadingPanel.add(jpb, "growx, wrap");
  120. loadingPanel.add(Box.createVerticalGlue(), "growy, pushy");
  121. try {
  122. downloader.downloadPage("https://addons.dmdirc.com/feed",
  123. tempDirectory.resolve("addons.feed"), this);
  124. } catch (final IOException ex) {
  125. loadingPanel.removeAll();
  126. loadingPanel.add(new TextLabel("Unable to download feeds: " + ex.getMessage()));
  127. return Collections.emptyList();
  128. }
  129. }
  130. loadingPanel.removeAll();
  131. loadingPanel.add(new TextLabel("Loading addon info, please wait."));
  132. final ConfigFile data = new ConfigFile(tempDirectory.resolve("addons.feed"));
  133. try {
  134. data.read();
  135. } catch (final IOException | InvalidConfigFileException ex) {
  136. return Collections.emptyList();
  137. }
  138. return data.getKeyDomains().values().stream()
  139. .map(entry -> new AddonInfo(globalConfig, updateManager, urlBuilder, entry))
  140. .collect(Collectors.toList());
  141. }
  142. @Override
  143. protected void done() {
  144. if (isCancelled()) {
  145. return;
  146. }
  147. Collection<AddonInfo> data;
  148. try {
  149. data = get();
  150. } catch (final InterruptedException ex) {
  151. data = Collections.emptyList();
  152. } catch (final ExecutionException ex) {
  153. LOG.warn(USER_ERROR, ex.getMessage(), ex);
  154. data = Collections.emptyList();
  155. }
  156. final int selectedRow;
  157. if (table.getRowCount() > 0 && table.getSelectedRow() > 0) {
  158. selectedRow = table.getSelectedRow();
  159. } else {
  160. selectedRow = 0;
  161. }
  162. table.getModel().setRowCount(0);
  163. for (final AddonInfo info : data) {
  164. table.getModel().addRow(new Object[]{
  165. new AddonInfoLabel(info, browserWindow, workerFactory),});
  166. }
  167. table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
  168. scrollPane.setViewportView(table);
  169. }
  170. @Override
  171. public void downloadProgress(final float percent) {
  172. UIUtilities.invokeLater(() -> jpb.setValue((int) percent));
  173. }
  174. @Override
  175. public void setIndeterminate(final boolean indeterminate) {
  176. UIUtilities.invokeLater(() -> jpb.setIndeterminate(indeterminate));
  177. }
  178. }