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 7.9KB

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