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 11KB

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