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

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