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.

NowPlayingManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.nowplaying;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.config.GlobalConfig;
  20. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  21. import com.dmdirc.config.prefs.PreferencesCategory;
  22. import com.dmdirc.config.prefs.PreferencesDialogModel;
  23. import com.dmdirc.events.ClientPrefsOpenedEvent;
  24. import com.dmdirc.events.PluginLoadedEvent;
  25. import com.dmdirc.events.PluginUnloadedEvent;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.dmdirc.plugins.Plugin;
  29. import com.dmdirc.plugins.PluginDomain;
  30. import com.dmdirc.plugins.PluginInfo;
  31. import com.dmdirc.plugins.PluginManager;
  32. import java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.List;
  35. import javax.inject.Inject;
  36. import net.engio.mbassy.listener.Handler;
  37. public class NowPlayingManager {
  38. /** Plugin manager to get plugins from. */
  39. private final PluginManager pluginManager;
  40. /** Global configuration to read settings from. */
  41. private final AggregateConfigProvider globalConfig;
  42. /** Event bus to subscribe to events on. */
  43. private final EventBus eventBus;
  44. private final PluginInfo pluginInfo;
  45. /** This plugin's settings domain. */
  46. private final String domain;
  47. /** The sources that we know of. */
  48. private final List<MediaSource> sources = new ArrayList<>();
  49. /** The managers that we know of. */
  50. private final Collection<MediaSourceManager> managers = new ArrayList<>();
  51. /** The user's preferred order for source usage. */
  52. private List<String> order;
  53. @Inject
  54. public NowPlayingManager(final PluginManager pluginManager, final EventBus eventBus,
  55. @GlobalConfig final AggregateConfigProvider globalConfig,
  56. @PluginDomain(NowPlayingPlugin.class) final String domain,
  57. @PluginDomain(NowPlayingPlugin.class) final PluginInfo pluginInfo) {
  58. this.pluginManager = pluginManager;
  59. this.globalConfig = globalConfig;
  60. this.domain = domain;
  61. this.eventBus = eventBus;
  62. this.pluginInfo = pluginInfo;
  63. }
  64. /**
  65. * Loads the plugin.
  66. */
  67. public void onLoad() {
  68. sources.clear();
  69. managers.clear();
  70. order = getSettings();
  71. eventBus.subscribe(this);
  72. pluginManager.getPluginInfos().stream()
  73. .filter(PluginInfo::isLoaded)
  74. .forEach(this::addPlugin);
  75. }
  76. /**
  77. * Unloads the plugin.
  78. */
  79. public void onUnload() {
  80. sources.clear();
  81. managers.clear();
  82. eventBus.unsubscribe(this);
  83. }
  84. @Handler
  85. public void handlePluginLoaded(final PluginLoadedEvent event) {
  86. addPlugin(event.getPlugin());
  87. }
  88. @Handler
  89. public void handlePluginUnloaded(final PluginUnloadedEvent event) {
  90. removePlugin(event.getPlugin());
  91. }
  92. /**
  93. * Loads the plugins settings.
  94. *
  95. * @return A list of sources to be used by the plugin.
  96. */
  97. public List<String> getSettings() {
  98. if (globalConfig.hasOptionString(domain, "sourceOrder")) {
  99. return globalConfig.getOptionList(domain, "sourceOrder");
  100. } else {
  101. return new ArrayList<>();
  102. }
  103. }
  104. /**
  105. * Checks to see if a plugin implements one of the Media Source interfaces and if it does, adds
  106. * the source(s) to our list.
  107. *
  108. * @param target The plugin to be tested
  109. */
  110. private void addPlugin(final PluginInfo target) {
  111. final Plugin targetPlugin = target.getPlugin();
  112. if (targetPlugin instanceof MediaSource) {
  113. sources.add((MediaSource) targetPlugin);
  114. addSourceToOrder((MediaSource) targetPlugin);
  115. }
  116. if (targetPlugin instanceof MediaSourceManager) {
  117. managers.add((MediaSourceManager) targetPlugin);
  118. if (((MediaSourceManager) targetPlugin).getSources() != null) {
  119. ((MediaSourceManager) targetPlugin).getSources().forEach(this::addSourceToOrder);
  120. }
  121. }
  122. }
  123. /**
  124. * Checks to see if the specified media source needs to be added to our order list, and adds it
  125. * if necessary.
  126. *
  127. * @param source The media source to be tested
  128. */
  129. private void addSourceToOrder(final MediaSource source) {
  130. if (!order.contains(source.getAppName())) {
  131. order.add(source.getAppName());
  132. }
  133. }
  134. /**
  135. * Checks to see if a plugin implements one of the Media Source interfaces and if it does,
  136. * removes the source(s) from our list.
  137. *
  138. * @param target The plugin to be tested
  139. */
  140. private void removePlugin(final PluginInfo target) {
  141. final Plugin targetPlugin = target.getPlugin();
  142. if (targetPlugin instanceof MediaSource) {
  143. sources.remove(targetPlugin);
  144. }
  145. if (targetPlugin instanceof MediaSourceManager) {
  146. managers.remove(targetPlugin);
  147. }
  148. }
  149. /**
  150. * Determines if there are any valid sources (paused or not).
  151. *
  152. * @return True if there are running sources, false otherwise
  153. */
  154. public boolean hasRunningSource() {
  155. for (final MediaSource source : getSources()) {
  156. if (source.getState() != MediaSourceState.CLOSED) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * Retrieves the "best" source to use for displaying media information. The best source is
  164. * defined as the earliest in the list that is running and not paused, or, if no such source
  165. * exists, the earliest in the list that is running and paused. If neither condition is
  166. * satisfied returns null.
  167. *
  168. * @return The best source to use for media info
  169. */
  170. public MediaSource getBestSource() {
  171. final List<MediaSource> possibleSources = getSources();
  172. possibleSources.sort(new MediaSourceComparator(order));
  173. MediaSource paused = null;
  174. for (final MediaSource source : possibleSources) {
  175. if (source.getState() != MediaSourceState.CLOSED) {
  176. if (source.getState() == MediaSourceState.PLAYING) {
  177. return source;
  178. } else if (paused == null) {
  179. paused = source;
  180. }
  181. }
  182. }
  183. return paused;
  184. }
  185. /**
  186. * Substitutes the keywords in the specified format with the values with values from the
  187. * specified source.
  188. *
  189. * @param format The format to be substituted
  190. * @param source The source whose values should be used
  191. *
  192. * @return The substituted string
  193. */
  194. public String doSubstitution(final String format, final MediaSource source) {
  195. final String artist = source.getArtist();
  196. final String title = source.getTitle();
  197. final String album = source.getAlbum();
  198. final String app = source.getAppName();
  199. final String bitrate = source.getBitrate();
  200. final String filetype = source.getFormat();
  201. final String length = source.getLength();
  202. final String time = source.getTime();
  203. final String state = source.getState().getNiceName();
  204. return format.replace("$artist", sanitise(artist))
  205. .replace("$title", sanitise(title))
  206. .replace("$album", sanitise(album))
  207. .replace("$app", sanitise(app))
  208. .replace("$bitrate", sanitise(bitrate))
  209. .replace("$format", sanitise(filetype))
  210. .replace("$length", sanitise(length))
  211. .replace("$state", sanitise(state))
  212. .replace("$time", sanitise(time));
  213. }
  214. /**
  215. * Sanitises the specified String so that it may be used as the replacement in a call to
  216. * String.replaceAll. Namely, at present, this method returns an empty String if it is passed a
  217. * null value; otherwise the input is returned as-is.
  218. *
  219. * @param input The string to be sanitised
  220. *
  221. * @return A sanitised version of the String
  222. *
  223. * @since 0.6.3
  224. */
  225. protected static String sanitise(final String input) {
  226. return input == null ? "" : input;
  227. }
  228. /**
  229. * Retrieves a source based on its name.
  230. *
  231. * @param name The name to search for
  232. *
  233. * @return The source with the specified name or null if none were found.
  234. */
  235. public MediaSource getSource(final String name) {
  236. for (final MediaSource source : getSources()) {
  237. if (source.getAppName().equalsIgnoreCase(name)) {
  238. return source;
  239. }
  240. }
  241. return null;
  242. }
  243. /**
  244. * Retrieves all the sources registered with this plugin.
  245. *
  246. * @return All known media sources
  247. */
  248. public List<MediaSource> getSources() {
  249. final List<MediaSource> res = new ArrayList<>(sources);
  250. for (MediaSourceManager manager : managers) {
  251. res.addAll(manager.getSources());
  252. }
  253. return res;
  254. }
  255. @Handler
  256. public void showConfig(final ClientPrefsOpenedEvent event) {
  257. final PreferencesDialogModel manager = event.getModel();
  258. final ConfigPanel configPanel = UIUtilities.invokeAndWait(
  259. () -> new ConfigPanel(this, manager.getConfigManager(),
  260. manager.getIdentity(), domain, getSettings()));
  261. final PreferencesCategory category = new PluginPreferencesCategory(
  262. pluginInfo, "Now Playing",
  263. "", "category-nowplaying", configPanel);
  264. manager.getCategory("Plugins").addSubCategory(category);
  265. }
  266. }