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.5KB

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