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.

NowPlayingCommand.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.FrameContainer;
  24. import com.dmdirc.MessageTarget;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.CommandInfo;
  27. import com.dmdirc.commandparser.CommandManager;
  28. import com.dmdirc.commandparser.CommandType;
  29. import com.dmdirc.commandparser.commands.Command;
  30. import com.dmdirc.commandparser.commands.IntelligentCommand;
  31. import com.dmdirc.commandparser.commands.context.ChatCommandContext;
  32. import com.dmdirc.commandparser.commands.context.CommandContext;
  33. import com.dmdirc.config.IdentityManager;
  34. import com.dmdirc.ui.input.AdditionalTabTargets;
  35. import com.dmdirc.ui.input.TabCompleter;
  36. import java.util.Arrays;
  37. import java.util.List;
  38. /**
  39. * The now playing command retrieves the currently playing song from a
  40. * variety of media players.
  41. * @author chris
  42. */
  43. public final class NowPlayingCommand extends Command implements
  44. IntelligentCommand, CommandInfo {
  45. /** The plugin that's using this command. */
  46. final NowPlayingPlugin parent;
  47. /**
  48. * Creates a new instance of NowPlayingCommand.
  49. *
  50. * @param parent The plugin that's instansiating this command
  51. */
  52. public NowPlayingCommand(final NowPlayingPlugin parent) {
  53. super();
  54. this.parent = parent;
  55. CommandManager.registerCommand(this);
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public void execute(final FrameContainer<?> origin,
  60. final CommandArguments args, final CommandContext context) {
  61. final MessageTarget<?> target = ((ChatCommandContext) context).getChat();
  62. if (args.getArguments().length > 0 && args.getArguments()[0]
  63. .equalsIgnoreCase("--sources")) {
  64. doSourceList(origin, args.isSilent(), args.getArgumentsAsString(1));
  65. } else if (args.getArguments().length > 0 && args.getArguments()[0]
  66. .equalsIgnoreCase("--source")) {
  67. if (args.getArguments().length > 1) {
  68. final String sourceName = args.getArguments()[1];
  69. final MediaSource source = parent.getSource(sourceName);
  70. if (source == null) {
  71. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Source not found.");
  72. } else {
  73. if (source.getState() != MediaSourceState.CLOSED) {
  74. target.getCommandParser().parseCommand(origin, context.getSource(),
  75. getInformation(source, args.getArgumentsAsString(2)));
  76. } else {
  77. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Source is not running.");
  78. }
  79. }
  80. } else {
  81. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  82. "You must specify a source when using --source.");
  83. }
  84. } else {
  85. if (parent.hasRunningSource()) {
  86. target.getCommandParser().parseCommand(origin, context.getSource(),
  87. getInformation(parent.getBestSource(), args.
  88. getArgumentsAsString(0)));
  89. } else {
  90. sendLine(origin, args.isSilent(), FORMAT_ERROR, "No running media sources available.");
  91. }
  92. }
  93. }
  94. /**
  95. * Outputs a list of sources for the nowplaying command.
  96. *
  97. * @param origin The input window where the command was entered
  98. * @param isSilent Whether this command is being silenced
  99. * @param format Format to be passed to getInformation
  100. */
  101. private void doSourceList(final FrameContainer<?> origin, final boolean isSilent,
  102. final String format) {
  103. final List<MediaSource> sources = parent.getSources();
  104. if (sources.isEmpty()) {
  105. sendLine(origin, isSilent, FORMAT_ERROR, "No media sources available.");
  106. } else {
  107. final String[] headers = {"Source", "Status", "Information"};
  108. final String[][] data = new String[sources.size()][3];
  109. int i = 0;
  110. for (MediaSource source : sources) {
  111. data[i][0] = source.getAppName();
  112. if (source.getState() != MediaSourceState.CLOSED) {
  113. data[i][1] = source.getState().getNiceName().toLowerCase();
  114. data[i][2] = getInformation(source, format);
  115. } else {
  116. data[i][1] = "not running";
  117. data[i][2] = "-";
  118. }
  119. i++;
  120. }
  121. sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
  122. }
  123. }
  124. /**
  125. * Returns a formatted information string from the requested soruce.
  126. *
  127. * @param source MediaSource to query
  128. * @param format Format to use
  129. *
  130. * @return Formatted information string
  131. * @since 0.6.3
  132. */
  133. private String getInformation(final MediaSource source, final String format) {
  134. if (format.isEmpty()) {
  135. return parent.doSubstitution(IdentityManager.getGlobalConfig()
  136. .getOption(parent.getDomain(), "format"), source);
  137. } else {
  138. return parent.doSubstitution(format, source);
  139. }
  140. }
  141. /** {@inheritDoc}. */
  142. @Override
  143. public String getName() {
  144. return "nowplaying";
  145. }
  146. /** {@inheritDoc}. */
  147. @Override
  148. public boolean showInHelp() {
  149. return true;
  150. }
  151. /** {@inheritDoc}. */
  152. @Override
  153. public String getHelp() {
  154. return "nowplaying [--sources|--source <source>] [format] - " +
  155. "tells the channel the song you're currently playing";
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public CommandType getType() {
  160. return CommandType.TYPE_CHAT;
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public AdditionalTabTargets getSuggestions(final int arg,
  165. final IntelligentCommandContext context) {
  166. final List<String> subsList = Arrays.asList(new String[] {
  167. "$artist", "$title", "$album", "$app", "$bitrate", "$format",
  168. "$length", "$state", "$time"
  169. });
  170. if (arg == 0) {
  171. final AdditionalTabTargets res = TabCompleter.
  172. getIntelligentResults(arg, context, 0);
  173. res.add("--sources");
  174. res.add("--source");
  175. res.addAll(subsList);
  176. return res;
  177. } else if (arg == 1 && context.getPreviousArgs().get(0).equalsIgnoreCase("--source")) {
  178. final AdditionalTabTargets res = new AdditionalTabTargets();
  179. res.excludeAll();
  180. for (MediaSource source : parent.getSources()) {
  181. if (source.getState() != MediaSourceState.CLOSED) {
  182. res.add(source.getAppName());
  183. }
  184. }
  185. return res;
  186. } else if (arg > 1 && context.getPreviousArgs().get(0).equalsIgnoreCase("--source")) {
  187. final AdditionalTabTargets res = TabCompleter
  188. .getIntelligentResults(arg, context, 2);
  189. res.addAll(subsList);
  190. return res;
  191. } else {
  192. final AdditionalTabTargets res = TabCompleter
  193. .getIntelligentResults(arg, context, context
  194. .getPreviousArgs().get(0).equalsIgnoreCase("--sources") ? 1 : 0);
  195. res.addAll(subsList);
  196. return res;
  197. }
  198. }
  199. }