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

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