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

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