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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.nowplaying;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.ChatCommandContext;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.config.GlobalConfig;
  27. import com.dmdirc.interfaces.Chat;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.InputModel;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.config.provider.AggregateConfigProvider;
  32. import com.dmdirc.plugins.PluginDomain;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.input.TabCompleterUtils;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. import java.util.Arrays;
  38. import java.util.List;
  39. import java.util.stream.Collectors;
  40. /**
  41. * The now playing command retrieves the currently playing song from a variety of media players.
  42. */
  43. public class NowPlayingCommand extends BaseCommand implements IntelligentCommand {
  44. /** A command info object for this command. */
  45. public static final CommandInfo INFO = new BaseCommandInfo("nowplaying",
  46. "nowplaying [--sources|--source <source>] [format] - "
  47. + "tells the channel the song you're currently playing",
  48. CommandType.TYPE_CHAT);
  49. /** Now playing manager to get and handle sources. */
  50. private final NowPlayingManager manager;
  51. /** Tab-completer utilities. */
  52. private final TabCompleterUtils tabCompleterUtils;
  53. /** Global configuration to read settings from. */
  54. private final AggregateConfigProvider globalConfig;
  55. /** This plugin's settings domain. */
  56. private final String domain;
  57. /**
  58. * Creates a new instance of this command.
  59. *
  60. * @param controller The controller to use for command information.
  61. * @param manager Now playing manager to get and handle sources.
  62. * @param globalConfig Global config to read from
  63. * @param domain This plugin's settings domain
  64. */
  65. @Inject
  66. public NowPlayingCommand(
  67. final CommandController controller,
  68. final NowPlayingManager manager,
  69. final TabCompleterUtils tabCompleterUtils,
  70. @GlobalConfig final AggregateConfigProvider globalConfig,
  71. @PluginDomain(NowPlayingPlugin.class) final String domain) {
  72. super(controller);
  73. this.manager = manager;
  74. this.tabCompleterUtils = tabCompleterUtils;
  75. this.globalConfig = globalConfig;
  76. this.domain = domain;
  77. }
  78. @Override
  79. public void execute(@Nonnull final WindowModel origin,
  80. final CommandArguments args, final CommandContext context) {
  81. final Chat target = ((ChatCommandContext) context).getChat();
  82. if (args.getArguments().length > 0
  83. && "--sources".equalsIgnoreCase(args.getArguments()[0])) {
  84. doSourceList(origin, args.isSilent(), args.getArgumentsAsString(1));
  85. } else if (args.getArguments().length > 0
  86. && "--source".equalsIgnoreCase(args.getArguments()[0])) {
  87. if (args.getArguments().length > 1) {
  88. final String sourceName = args.getArguments()[1];
  89. final MediaSource source = manager.getSource(sourceName);
  90. if (source == null) {
  91. showError(origin, args.isSilent(), "Source not found.");
  92. } else {
  93. if (source.getState() == MediaSourceState.CLOSED) {
  94. showError(origin, args.isSilent(), "Source is not running.");
  95. } else {
  96. target.getWindowModel().getInputModel().map(InputModel::getCommandParser)
  97. .ifPresent(cp -> cp.parseCommand(origin,
  98. getInformation(source, args.getArgumentsAsString(2))));
  99. }
  100. }
  101. } else {
  102. showError(origin, args.isSilent(),
  103. "You must specify a source when using --source.");
  104. }
  105. } else {
  106. if (manager.hasRunningSource()) {
  107. target.getWindowModel().getInputModel().map(InputModel::getCommandParser)
  108. .ifPresent(cp -> cp.parseCommand(origin,
  109. getInformation(manager.getBestSource(),
  110. args.getArgumentsAsString(0))));
  111. } else {
  112. showError(origin, args.isSilent(), "No running media sources available.");
  113. }
  114. }
  115. }
  116. /**
  117. * Outputs a list of sources for the nowplaying command.
  118. *
  119. * @param origin The input window where the command was entered
  120. * @param isSilent Whether this command is being silenced
  121. * @param format Format to be passed to getInformation
  122. */
  123. private void doSourceList(final WindowModel origin, final boolean isSilent,
  124. final String format) {
  125. final List<MediaSource> sources = manager.getSources();
  126. if (sources.isEmpty()) {
  127. showError(origin, isSilent, "No media sources available.");
  128. } else {
  129. final String[] headers = {"Source", "Status", "Information"};
  130. final String[][] data = new String[sources.size()][3];
  131. int i = 0;
  132. for (MediaSource source : sources) {
  133. data[i][0] = source.getAppName();
  134. if (source.getState() == MediaSourceState.CLOSED) {
  135. data[i][1] = "not running";
  136. data[i][2] = "-";
  137. } else {
  138. data[i][1] = source.getState().getNiceName().toLowerCase();
  139. data[i][2] = getInformation(source, format);
  140. }
  141. i++;
  142. }
  143. showOutput(origin, isSilent, doTable(headers, data));
  144. }
  145. }
  146. /**
  147. * Returns a formatted information string from the requested source.
  148. *
  149. * @param source MediaSource to query
  150. * @param format Format to use
  151. *
  152. * @return Formatted information string
  153. */
  154. private String getInformation(final MediaSource source, final String format) {
  155. if (format.isEmpty()) {
  156. return manager.doSubstitution(globalConfig.getOption(domain, "format"), source);
  157. } else {
  158. return manager.doSubstitution(format, source);
  159. }
  160. }
  161. @Override
  162. public AdditionalTabTargets getSuggestions(final int arg,
  163. final IntelligentCommandContext context) {
  164. final List<String> subsList = Arrays.asList("$artist", "$title", "$album", "$app",
  165. "$bitrate", "$format", "$length", "$state", "$time");
  166. if (arg == 0) {
  167. final AdditionalTabTargets res = tabCompleterUtils.
  168. getIntelligentResults(arg, context, 0);
  169. res.add("--sources");
  170. res.add("--source");
  171. res.addAll(subsList);
  172. return res;
  173. } else if (arg == 1 && "--source".equalsIgnoreCase(context.getPreviousArgs().get(0))) {
  174. final AdditionalTabTargets res = new AdditionalTabTargets();
  175. res.excludeAll();
  176. res.addAll(manager.getSources().stream()
  177. .filter(source -> source.getState() != MediaSourceState.CLOSED)
  178. .map(MediaSource::getAppName).collect(Collectors.toList()));
  179. return res;
  180. } else if (arg > 1 && "--source".equalsIgnoreCase(context.getPreviousArgs().get(0))) {
  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. "--sources".equalsIgnoreCase(context.getPreviousArgs().get(0)) ? 1 : 0);
  189. res.addAll(subsList);
  190. return res;
  191. }
  192. }
  193. }