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

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