Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

NowPlayingCommand.java 9.1KB

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