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

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