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

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