File Source: NowPlayingCommand.java

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








SofCheck Inspector Build Version : 2.17854
NowPlayingCommand.java 2009-Jun-25 01:54:24
NowPlayingCommand.class 2009-Sep-02 17:04:17