Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NowPlayingManager.java 10.0KB

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