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.2KB

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