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

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