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.

NowPlayingPlugin.java 10KB

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