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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.MessageTarget;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.commands.ChatCommand;
  27. import com.dmdirc.commandparser.CommandManager;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import com.dmdirc.ui.interfaces.InputWindow;
  32. import java.util.List;
  33. /**
  34. * The now playing command retrieves the currently playing song from a
  35. * variety of media players.
  36. * @author chris
  37. */
  38. public final class NowPlayingCommand extends ChatCommand implements IntelligentCommand {
  39. /** The plugin that's using this command. */
  40. final NowPlayingPlugin parent;
  41. /**
  42. * Creates a new instance of NowPlayingCommand.
  43. *
  44. * @param parent The plugin that's instansiating this command
  45. */
  46. public NowPlayingCommand(final NowPlayingPlugin parent) {
  47. super();
  48. this.parent = parent;
  49. CommandManager.registerCommand(this);
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void execute(final InputWindow origin, final Server server,
  54. final MessageTarget target, final boolean isSilent, final CommandArguments args) {
  55. if (args.getArguments().length > 0 && args.getArguments()[0]
  56. .equalsIgnoreCase("--sources")) {
  57. doSourceList(origin, isSilent);
  58. } else if (args.getArguments().length > 0 && args.getArguments()[0]
  59. .equalsIgnoreCase("--source")) {
  60. if (args.getArguments().length > 1) {
  61. final String sourceName = args.getArguments()[1];
  62. final MediaSource source = parent.getSource(sourceName);
  63. if (source == null) {
  64. sendLine(origin, isSilent, FORMAT_ERROR, "Source not found.");
  65. } else {
  66. if (source.getState() != MediaSourceState.CLOSED) {
  67. target.getFrame().getCommandParser().parseCommand(origin,
  68. getInformation(source));
  69. } else {
  70. sendLine(origin, isSilent, FORMAT_ERROR, "Source is not running.");
  71. }
  72. }
  73. } else {
  74. sendLine(origin, isSilent, FORMAT_ERROR,
  75. "You must specify a source when using --source.");
  76. }
  77. } else {
  78. if (parent.hasRunningSource()) {
  79. target.getFrame().getCommandParser().parseCommand(origin,
  80. getInformation(parent.getBestSource()));
  81. } else {
  82. sendLine(origin, isSilent, FORMAT_ERROR, "No running media sources available.");
  83. }
  84. }
  85. }
  86. /**
  87. * Outputs a list of sources for the nowplaying command.
  88. *
  89. * @param origin The input window where the command was entered
  90. * @param isSilent Whether this command is being silenced
  91. */
  92. private void doSourceList(final InputWindow origin, final boolean isSilent) {
  93. final List<MediaSource> sources = parent.getSources();
  94. if (sources.isEmpty()) {
  95. sendLine(origin, isSilent, FORMAT_ERROR, "No media sources available.");
  96. } else {
  97. final String[] headers = {"Source", "Status", "Information"};
  98. final String[][] data = new String[sources.size()][3];
  99. int i = 0;
  100. for (MediaSource source : sources) {
  101. data[i][0] = source.getAppName();
  102. if (source.getState() != MediaSourceState.CLOSED) {
  103. data[i][1] = source.getState().getNiceName().toLowerCase();
  104. data[i][2] = getInformation(source);
  105. } else {
  106. data[i][1] = "not running";
  107. data[i][2] = "-";
  108. }
  109. i++;
  110. }
  111. sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
  112. }
  113. }
  114. /**
  115. * Returns a formatted information string from the requested soruce.
  116. *
  117. * @param source MediaSource to query
  118. *
  119. * @return Formatted information string
  120. */
  121. private String getInformation(final MediaSource source) {
  122. return parent.doSubstitution(IdentityManager.getGlobalConfig()
  123. .getOption(parent.getDomain(), "format"), source);
  124. }
  125. /** {@inheritDoc}. */
  126. @Override
  127. public String getName() {
  128. return "nowplaying";
  129. }
  130. /** {@inheritDoc}. */
  131. @Override
  132. public boolean showInHelp() {
  133. return true;
  134. }
  135. /** {@inheritDoc}. */
  136. @Override
  137. public String getHelp() {
  138. return "nowplaying [--sources|--source <source>] - " +
  139. "tells the channel the song you're currently playing";
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public AdditionalTabTargets getSuggestions(final int arg,
  144. final List<String> previousArgs) {
  145. final AdditionalTabTargets res = new AdditionalTabTargets();
  146. res.excludeAll();
  147. if (arg == 0) {
  148. res.add("--sources");
  149. res.add("--source");
  150. } else if (arg == 1 && previousArgs.get(0).equalsIgnoreCase("--source")) {
  151. for (MediaSource source : parent.getSources()) {
  152. if (source.getState() != MediaSourceState.CLOSED) {
  153. res.add(source.getAppName());
  154. }
  155. }
  156. }
  157. return res;
  158. }
  159. }