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.

AddonToggle.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.addonpanel;
  18. import com.dmdirc.config.provider.ConfigProvider;
  19. import com.dmdirc.plugins.PluginInfo;
  20. import com.dmdirc.plugins.PluginManager;
  21. import com.dmdirc.ui.themes.Theme;
  22. import com.dmdirc.ui.themes.ThemeManager;
  23. import com.dmdirc.updater.UpdateComponent;
  24. import com.dmdirc.updater.manager.CachingUpdateManager;
  25. import com.dmdirc.updater.manager.UpdateStatus;
  26. import com.dmdirc.util.collections.ListenerList;
  27. /**
  28. * Wraps a Addon object (Theme or Plugin) with a boolean to indicate whether it should be toggled or
  29. * not.
  30. */
  31. public final class AddonToggle {
  32. /** The PluginInfo object we're wrapping. */
  33. private final PluginInfo pi;
  34. /** The Theme object we're wrapping. */
  35. private final Theme theme;
  36. /** Addon state. */
  37. private boolean state;
  38. /** Whether or nor the addon update state should be toggled. */
  39. private boolean updateState;
  40. /** Listener list. */
  41. private final ListenerList listeners = new ListenerList();
  42. /** Identity to change update settings in. */
  43. private final ConfigProvider identity;
  44. /** The manager to update when toggling a plugin. */
  45. private final PluginManager pluginManager;
  46. /** The manager to update when toggling a theme. */
  47. private final ThemeManager themeManager;
  48. /**
  49. * Creates a new instance of AddonToggle to wrap the specified PluginInfo.
  50. *
  51. * @param updateManager The update manager to use to retrieve component info.
  52. * @param identity Identity to change update settings in.
  53. * @param pi The PluginInfo to be wrapped.
  54. */
  55. public AddonToggle(
  56. final CachingUpdateManager updateManager,
  57. final ConfigProvider identity,
  58. final PluginManager pluginManager,
  59. final PluginInfo pi) {
  60. this.identity = identity;
  61. this.pluginManager = pluginManager;
  62. this.pi = pi;
  63. this.themeManager = null;
  64. this.theme = null;
  65. state = pi.isLoaded();
  66. checkUpdateState(updateManager);
  67. }
  68. /**
  69. * Creates a new instance of AddonToggle to wrap the specified Theme.
  70. *
  71. * @param updateManager The update manager to use to retrieve component info.
  72. * @param identity Identity to change update settings in.
  73. * @param themeManager The manager to update when toggling a theme.
  74. * @param theme The Theme to be wrapped
  75. */
  76. public AddonToggle(
  77. final CachingUpdateManager updateManager,
  78. final ConfigProvider identity,
  79. final ThemeManager themeManager,
  80. final Theme theme) {
  81. this.identity = identity;
  82. this.pluginManager = null;
  83. this.pi = null;
  84. this.themeManager = themeManager;
  85. this.theme = theme;
  86. state = theme.isEnabled();
  87. checkUpdateState(updateManager);
  88. }
  89. /**
  90. * Checks whether or not the component that is controlled by this toggle is enabled.
  91. *
  92. * @param updateManager The update manager to use to retrieve component info.
  93. */
  94. private void checkUpdateState(final CachingUpdateManager updateManager) {
  95. for (UpdateComponent comp : updateManager.getComponents()) {
  96. if (comp.getName().equals("addon-" + getID())) {
  97. updateState = updateManager.getStatus(comp) != UpdateStatus.CHECKING_NOT_PERMITTED;
  98. break;
  99. }
  100. }
  101. }
  102. /**
  103. * Sets the state of this adddon.
  104. *
  105. * @param state New state
  106. */
  107. public void setState(final boolean state) {
  108. this.state = state;
  109. triggerListener();
  110. }
  111. /**
  112. * Sets the update state of this addon.
  113. *
  114. * @param updateState New update state
  115. */
  116. public void setUpdateState(final boolean updateState) {
  117. this.updateState = updateState;
  118. triggerListener();
  119. }
  120. /**
  121. * Gets the state of this PluginInfo, taking into account the state of the toggle setting.
  122. *
  123. * @return True if the plugin is or should be loaded, false otherwise.
  124. */
  125. public boolean getState() {
  126. return state;
  127. }
  128. /**
  129. * Gets the update state of this PluginInfo, taking into account the state of the update toggle
  130. * setting.
  131. *
  132. * @return True if the plugin is or should be updated, false otherwise.
  133. */
  134. public boolean getUpdateState() {
  135. return pi != null && updateState;
  136. }
  137. /**
  138. * Retrieves the PluginInfo object associated with this toggle.
  139. *
  140. * @return This toggle's PluginInfo object.
  141. */
  142. public PluginInfo getPluginInfo() {
  143. return pi;
  144. }
  145. /**
  146. * Retrieves the Theme object associated with this toggle.
  147. *
  148. * @return This toggle's Theme object.
  149. */
  150. public Theme getTheme() {
  151. return theme;
  152. }
  153. /**
  154. * Applies the changes to the PluginInfo, if any.
  155. */
  156. public void apply() {
  157. new Thread("Addon-Load-Unload") {
  158. @Override
  159. public void run() {
  160. if (pi != null) {
  161. if (AddonToggle.this.getState()) {
  162. pi.loadPlugin();
  163. } else {
  164. pi.unloadPlugin();
  165. }
  166. pluginManager.updateAutoLoad(pi);
  167. if (getID() != -1) {
  168. if (getUpdateState()) {
  169. identity.unsetOption("updater", "enable-addon-"
  170. + getID());
  171. } else {
  172. identity.setOption("updater", "enable-addon-"
  173. + getID(), false);
  174. }
  175. }
  176. }
  177. if (theme != null) {
  178. if (AddonToggle.this.getState()) {
  179. theme.applyTheme();
  180. } else {
  181. theme.removeTheme();
  182. }
  183. themeManager.synchroniseAutoLoad(theme);
  184. }
  185. }
  186. }.start();
  187. }
  188. /**
  189. * Is this addon unloadable?
  190. *
  191. * @return true iff unloadable
  192. */
  193. public boolean isUnloadable() {
  194. return pi == null || pi.isUnloadable();
  195. }
  196. /**
  197. * Returns the name of this addon.
  198. *
  199. * @return Addon name
  200. */
  201. public String getName() {
  202. if (pi != null) {
  203. return pi.getMetaData().getFriendlyName();
  204. }
  205. if (theme != null) {
  206. return theme.getName();
  207. }
  208. return "Unknown addon name";
  209. }
  210. /**
  211. * Returns the ID of this addon. This will return -1 for any theme.
  212. *
  213. * @return Addon ID
  214. */
  215. public int getID() {
  216. if (pi != null) {
  217. return pi.getMetaData().getUpdaterId();
  218. }
  219. return -1;
  220. }
  221. /**
  222. * Returns the friendly version of this addon.
  223. *
  224. * @return Addon version
  225. */
  226. public String getVersion() {
  227. if (pi != null) {
  228. return pi.getMetaData().getFriendlyVersion();
  229. }
  230. if (theme != null) {
  231. return theme.getVersion();
  232. }
  233. return "Unknown";
  234. }
  235. /**
  236. * Returns the author of this addon.
  237. *
  238. * @return Addon author
  239. */
  240. public String getAuthor() {
  241. if (pi != null) {
  242. return pi.getMetaData().getAuthor();
  243. }
  244. if (theme != null) {
  245. return theme.getAuthor();
  246. }
  247. return "Unknown";
  248. }
  249. /**
  250. * Returns the description of this addon.
  251. *
  252. * @return Addon description
  253. */
  254. public String getDescription() {
  255. if (pi != null) {
  256. return pi.getMetaData().getDescription();
  257. }
  258. if (theme != null) {
  259. return theme.getDescription();
  260. }
  261. return "There is an error with this addon.";
  262. }
  263. /**
  264. * Adds an addon toggle listener to this panel.
  265. *
  266. * @param listener Listener to add
  267. */
  268. public void addListener(final AddonToggleListener listener) {
  269. listeners.add(AddonToggleListener.class, listener);
  270. }
  271. /**
  272. * Removes an addon toggle listener from this panel.
  273. *
  274. * @param listener Listener to remove
  275. */
  276. public void removeListener(final AddonToggleListener listener) {
  277. listeners.remove(AddonToggleListener.class, listener);
  278. }
  279. /**
  280. * Triggers this listener to be called across all it's listeners.
  281. */
  282. public void triggerListener() {
  283. listeners.getCallable(AddonToggleListener.class).addonToggled();
  284. }
  285. }