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 8.2KB

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