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.

AddonInfo.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.config.provider.AggregateConfigProvider;
  19. import com.dmdirc.updater.UpdateChannel;
  20. import com.dmdirc.updater.UpdateComponent;
  21. import com.dmdirc.updater.manager.UpdateManager;
  22. import com.dmdirc.util.URLBuilder;
  23. import java.awt.Image;
  24. import java.util.Map;
  25. import javax.swing.ImageIcon;
  26. /**
  27. * Describes an addon.
  28. */
  29. public class AddonInfo {
  30. /** Addon site ID. */
  31. private final int id;
  32. /** Stable download name. */
  33. private final String stableDownload;
  34. /** Unstable download name. */
  35. private final String unstableDownload;
  36. /** Nightly download name. */
  37. private final String nightlyDownload;
  38. /** Addon title. */
  39. private final String title;
  40. /** Addon author and email. */
  41. private final String author;
  42. /** Addon rating from 0-10. */
  43. private final int rating;
  44. /** Full text description. */
  45. private final String description;
  46. /** Addon type. */
  47. private final AddonType type;
  48. /** Has this addon been verified by the developers? */
  49. private final boolean verified;
  50. /** Date this addon was updated. */
  51. private final int date;
  52. /** Screenshot image. */
  53. private final ImageIcon screenshot;
  54. /** Current client update channel. */
  55. private final UpdateChannel channel;
  56. /** The manager to use to check for update information. */
  57. private final UpdateManager updateManager;
  58. /**
  59. * Creates a new addon info class with the specified entries.
  60. *
  61. * @param configManager The config provider to use to find settings.
  62. * @param updateManager The manager to use to check for update information.
  63. * @param urlBuilder The URL builder to use to retrieve image URLs.
  64. * @param entry List of entries
  65. */
  66. public AddonInfo(
  67. final AggregateConfigProvider configManager,
  68. final UpdateManager updateManager,
  69. final URLBuilder urlBuilder,
  70. final Map<String, String> entry) {
  71. id = Integer.parseInt(entry.get("id"));
  72. title = entry.get("title");
  73. author = entry.get("user");
  74. rating = Integer.parseInt(entry.get("rating"));
  75. type = entry.get("type").equals("plugin") ? AddonType.TYPE_PLUGIN : AddonType.TYPE_THEME;
  76. stableDownload = entry.containsKey("stable") ? entry.get("stable") : "";
  77. unstableDownload = entry.containsKey("unstable") ? entry
  78. .get("unstable") : "";
  79. nightlyDownload = entry.containsKey("nightly") ? entry.get("nightly")
  80. : "";
  81. description = entry.get("description");
  82. verified = entry.get("verified").equals("yes");
  83. date = Integer.parseInt(entry.get("date"));
  84. if (entry.get("screenshot").equals("yes")) {
  85. screenshot = new ImageIcon(
  86. urlBuilder.getUrl("https://addons.dmdirc.com/addonimg/" + id));
  87. screenshot.setImage(screenshot.getImage().
  88. getScaledInstance(150, 150, Image.SCALE_SMOOTH));
  89. } else {
  90. screenshot = new ImageIcon(urlBuilder.getUrl("dmdirc://com/dmdirc/res/logo.png"));
  91. }
  92. UpdateChannel tempChannel;
  93. try {
  94. tempChannel = UpdateChannel.valueOf(configManager.getOption(
  95. "updater", "channel"));
  96. } catch (final IllegalArgumentException ex) {
  97. tempChannel = UpdateChannel.NONE;
  98. }
  99. channel = tempChannel;
  100. this.updateManager = updateManager;
  101. }
  102. public int getId() {
  103. return id;
  104. }
  105. public String getStableDownload() {
  106. return stableDownload;
  107. }
  108. public String getUnstableDownload() {
  109. return unstableDownload;
  110. }
  111. public String getNightlyDownload() {
  112. return nightlyDownload;
  113. }
  114. public String getTitle() {
  115. return title;
  116. }
  117. public String getAuthor() {
  118. return author;
  119. }
  120. public int getRating() {
  121. return rating;
  122. }
  123. public String getDescription() {
  124. return description;
  125. }
  126. public AddonType getType() {
  127. return type;
  128. }
  129. public boolean isVerified() {
  130. return verified;
  131. }
  132. public int getDate() {
  133. return date;
  134. }
  135. public ImageIcon getScreenshot() {
  136. return screenshot;
  137. }
  138. public UpdateChannel getChannel() {
  139. return channel;
  140. }
  141. /**
  142. * Is the plugin installed?
  143. *
  144. * @return true iff installed
  145. */
  146. public boolean isInstalled() {
  147. for (UpdateComponent comp : updateManager.getComponents()) {
  148. if (comp.getName().equals("addon-" + getId())) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * Is the plugin downloadable?
  156. *
  157. * @return true iff the plugin is downloadable
  158. */
  159. public boolean isDownloadable() {
  160. return !getDownload().isEmpty();
  161. }
  162. /**
  163. * Returns the download location for this addoninfo, or an empty string.
  164. *
  165. * @return Download location or empty string
  166. */
  167. @SuppressWarnings("fallthrough")
  168. public String getDownload() {
  169. switch (channel) { // NOPMD
  170. case NONE:
  171. // fallthrough
  172. case NIGHTLY:
  173. if (!nightlyDownload.isEmpty()) {
  174. return nightlyDownload;
  175. }
  176. // fallthrough
  177. case UNSTABLE:
  178. if (!unstableDownload.isEmpty()) {
  179. return unstableDownload;
  180. }
  181. // fallthrough
  182. case STABLE:
  183. if (!stableDownload.isEmpty()) {
  184. return stableDownload;
  185. }
  186. return "";
  187. default:
  188. return "";
  189. }
  190. }
  191. /**
  192. * Checks if the text matches this plugin
  193. *
  194. * @param text Comparison addon text.
  195. *
  196. * @return true iff the plugin matches
  197. */
  198. public boolean matches(final String text) {
  199. return title.toLowerCase().contains(text.toLowerCase())
  200. || description.toLowerCase().contains(text.toLowerCase());
  201. }
  202. }