File Source: NowPlayingPlugin.java

         /* 
    P/P   *  Method: com.dmdirc.addons.nowplaying.NowPlayingPlugin__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.actions.ActionManager;
    26  import com.dmdirc.actions.interfaces.ActionType;
    27  import com.dmdirc.actions.CoreActionType;
    28  import com.dmdirc.commandparser.CommandManager;
    29  import com.dmdirc.config.IdentityManager;
    30  import com.dmdirc.config.prefs.PreferencesCategory;
    31  import com.dmdirc.config.prefs.PreferencesManager;
    32  import com.dmdirc.interfaces.ActionListener;
    33  import com.dmdirc.plugins.Plugin;
    34  import com.dmdirc.plugins.PluginInfo;
    35  import com.dmdirc.plugins.PluginManager;
    36  
    37  import java.util.ArrayList;
    38  import java.util.Collections;
    39  import java.util.List;
    40  
    41  /**
    42   * Plugin that allows users to advertise what they're currently playing or
    43   * listening to.
    44   * 
    45   * @author chris
    46   */
    47  public class NowPlayingPlugin extends Plugin implements ActionListener  {
    48      
    49      /** The sources that we know of. */
    50      private final List<MediaSource> sources = new ArrayList<MediaSource>();
    51  
    52      /** The managers that we know of. */
    53      private final List<MediaSourceManager> managers = new ArrayList<MediaSourceManager>();
    54      
    55      /** The now playing command we're registering. */
    56      private NowPlayingCommand command;
    57      
    58      /** The user's preferred order for source usage. */
    59      private List<String> order;
    60      
    61      /**
    62       * Creates a new instance of NowPlayingPlugin.
    63       */
    64      public NowPlayingPlugin() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nowplaying.NowPlayingPlugin()
                  * 
                  *  Postconditions:
                  *    this.managers == &amp;new ArrayList(NowPlayingPlugin#2)
                  *    this.sources == &amp;new ArrayList(NowPlayingPlugin#1)
                  *    new ArrayList(NowPlayingPlugin#1) num objects == 1
                  *    new ArrayList(NowPlayingPlugin#2) num objects == 1
                  */
    65          super();
    66      }
    67      
    68      /** {@inheritDoc} */
    69      @Override
    70      public void onLoad() {
                 /* 
    P/P           *  Method: void onLoad()
                  * 
                  *  Preconditions:
                  *    this.managers != null
                  *    this.sources != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionType.PLUGIN_LOADED)
                  *    init'ed(com.dmdirc.actions.CoreActionType.PLUGIN_UNLOADED)
                  *    com.dmdirc.plugins.PluginManager:getPluginInfos(...)@79 != null
                  *    com.dmdirc.plugins.PluginManager:getPluginManager(...)@79 != null
                  *    java.util.Iterator:next(...)@79 != null
                  * 
                  *  Postconditions:
                  *    this.command == &amp;new NowPlayingCommand(onLoad#2)
                  *    init'ed(this.order)
                  *    new ArrayList(loadSettings#1) num objects <= 1
                  *    new NowPlayingCommand(onLoad#2) num objects == 1
                  *    this.command.parent == this
                  *    this.command.parent != null
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.plugins.PluginInfo:isLoaded(...)@80: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@79: {0}, {1}
                  */
    71          sources.clear();
    72          managers.clear();
    73          
    74          loadSettings();
    75          
    76          ActionManager.addListener(this, CoreActionType.PLUGIN_LOADED,
    77                  CoreActionType.PLUGIN_UNLOADED);
    78          
    79          for (PluginInfo target : PluginManager.getPluginManager().getPluginInfos()) {
    80              if (target.isLoaded()) {
    81                  addPlugin(target);
    82              }
    83          }
    84          
    85          command = new NowPlayingCommand(this);
    86      }
    87      
    88      /** {@inheritDoc} */
    89      @Override
    90      public void onUnload() {
                 /* 
    P/P           *  Method: void onUnload()
                  * 
                  *  Preconditions:
                  *    init'ed(this.command)
                  *    this.managers != null
                  *    this.sources != null
                  */
    91          sources.clear();
    92          managers.clear();
    93          
    94          ActionManager.removeListener(this);
    95          
    96          CommandManager.unregisterCommand(command);
    97      }
    98      
    99      /** {@inheritDoc} */
   100      @Override
   101      public void showConfig(final PreferencesManager manager) {
                 /* 
    P/P           *  Method: void showConfig(PreferencesManager)
                  * 
                  *  Preconditions:
                  *    manager != null
                  *    init'ed(this.order)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.prefs.PreferencesManager:getCategory(...)@106 != null
                  */
   102          final ConfigPanel configPanel = new ConfigPanel(this, order);
   103          
   104          final PreferencesCategory category = new PreferencesCategory("Now Playing",
   105                  "", "category-nowplaying", configPanel);
   106          manager.getCategory("Plugins").addSubCategory(category);
   107      }
   108      
   109      /**
   110       * Saves the plugins settings.
   111       * 
   112       * @param newOrder The new order for sources
   113       */
   114      protected void saveSettings(final List<String> newOrder) {
                 /* 
    P/P           *  Method: void saveSettings(List)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@116 != null
                  * 
                  *  Postconditions:
                  *    this.order == newOrder
                  *    init'ed(this.order)
                  */
   115          order = newOrder;
   116          IdentityManager.getConfigIdentity().setOption(getDomain(), "sourceOrder", order);
   117      }
   118      
   119      /** Loads the plugins settings. */
   120      private void loadSettings() {
                 /* 
    P/P           *  Method: void loadSettings()
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@121 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@122 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.order)
                  *    new ArrayList(loadSettings#1) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@121: {0}, {1}
                  */
   121          if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), "sourceOrder")) {
   122              order = IdentityManager.getGlobalConfig().getOptionList(getDomain(), "sourceOrder");
   123          } else {
   124              order = new ArrayList<String>();
   125          }
   126      }
   127      
   128      /** {@inheritDoc} */
   129      @Override
   130      public void processEvent(final ActionType type, final StringBuffer format,
   131              final Object... arguments) {
                 /* 
    P/P           *  Method: void processEvent(ActionType, StringBuffer, Object[])
                  * 
                  *  Preconditions:
                  *    (soft) arguments != null
                  *    (soft) arguments.length >= 1
                  *    (soft) arguments[0] != null
                  *    (soft) this.managers != null
                  *    (soft) this.order != null
                  *    (soft) this.sources != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionType.PLUGIN_LOADED)
                  *    init'ed(com.dmdirc.actions.CoreActionType.PLUGIN_UNLOADED)
                  */
   132          if (type == CoreActionType.PLUGIN_LOADED) {
   133              addPlugin((PluginInfo) arguments[0]);
   134          } else if (type == CoreActionType.PLUGIN_UNLOADED) {
   135              removePlugin((PluginInfo) arguments[0]);
   136          }
   137      }
   138      
   139      /**
   140       * Checks to see if a plugin implements one of the Media Source interfaces
   141       * and if it does, adds the source(s) to our list.
   142       *
   143       * @param target The plugin to be tested
   144       */
   145      private void addPlugin(final PluginInfo target) {
                 /* 
    P/P           *  Method: void addPlugin(PluginInfo)
                  * 
                  *  Preconditions:
                  *    target != null
                  *    (soft) this.managers != null
                  *    (soft) this.order != null
                  *    (soft) this.sources != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@156 != null
                  *    targetPlugin.sources@146 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@156: {0}, {1}
                  */
   146          final Plugin targetPlugin = target.getPlugin();
   147          if (targetPlugin instanceof MediaSource) {
   148              sources.add((MediaSource) targetPlugin);
   149              addSourceToOrder((MediaSource) targetPlugin);
   150          }
   151          
   152          if (targetPlugin instanceof MediaSourceManager) {
   153              managers.add((MediaSourceManager) targetPlugin);
   154  
   155              if (((MediaSourceManager) targetPlugin).getSources() != null) {
   156                  for (MediaSource source : ((MediaSourceManager) targetPlugin).getSources()) {
   157                      addSourceToOrder(source);
   158                  }
   159              }
   160          }
   161      }
   162      
   163      /**
   164       * Checks to see if the specified media source needs to be added to our
   165       * order list, and adds it if neccessary.
   166       *
   167       * @param source The media source to be tested
   168       */
   169      private void addSourceToOrder(final MediaSource source) {
                 /* 
    P/P           *  Method: void addSourceToOrder(MediaSource)
                  * 
                  *  Preconditions:
                  *    source != null
                  *    this.order != null
                  * 
                  *  Test Vectors:
                  *    java.util.List:contains(...)@170: {1}, {0}
                  */
   170          if (!order.contains(source.getAppName())) {
   171              order.add(source.getAppName());
   172          }
   173      }
   174      
   175      /**
   176       * Checks to see if a plugin implements one of the Media Source interfaces
   177       * and if it does, removes the source(s) from our list.
   178       *
   179       * @param target The plugin to be tested
   180       */
   181      private void removePlugin(final PluginInfo target) {
                 /* 
    P/P           *  Method: void removePlugin(PluginInfo)
                  * 
                  *  Preconditions:
                  *    target != null
                  *    (soft) this.managers != null
                  *    (soft) this.sources != null
                  */
   182          final Plugin targetPlugin = target.getPlugin();
   183          if (targetPlugin instanceof MediaSource) {
   184              sources.remove(targetPlugin);
   185          }
   186          
   187          if (targetPlugin instanceof MediaSourceManager) {
   188              managers.remove((MediaSourceManager) targetPlugin);
   189          }
   190      }
   191      
   192      /**
   193       * Determines if there are any valid sources (paused or not).
   194       *
   195       * @return True if there are running sources, false otherwise
   196       */
   197      public boolean hasRunningSource() {
                 /* 
    P/P           *  Method: bool hasRunningSource()
                  * 
                  *  Preconditions:
                  *    this.managers != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@198 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    getState(...)@199: 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.util.Iterator:hasNext(...)@198: {0}, {1}
                  */
   198          for (final MediaSource source : getSources()) {
   199              if (source.getState() != MediaSourceState.CLOSED) {
   200                  return true;
   201              }
   202          }
   203          
   204          return false;
   205      }
   206      
   207      /**
   208       * Retrieves the "best" source to use for displaying media information.
   209       * The best source is defined as the earliest in the list that is running
   210       * and not paused, or, if no such source exists, the earliest in the list
   211       * that is running and paused. If neither condition is satisified returns
   212       * null.
   213       *
   214       * @return The best source to use for media info
   215       */
   216      public MediaSource getBestSource() {
                 /* 
    P/P           *  Method: MediaSource getBestSource()
                  * 
                  *  Preconditions:
                  *    this.managers != null
                  *    init'ed(this.order)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@221 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    getState(...)@222: 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)}
                  *    getState(...)@223: Inverse{&amp;com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#4)}, Addr_Set{&amp;com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#4)}
                  *    java.util.Iterator:hasNext(...)@221: {0}, {1}
                  */
   217          MediaSource paused = null;
   218          
   219          Collections.sort(sources, new MediaSourceComparator(order));
   220          
   221          for (final MediaSource source : getSources()) {
   222              if (source.getState() != MediaSourceState.CLOSED) {
   223                  if (source.getState() == MediaSourceState.PLAYING) {
   224                      return source;
   225                  } else if (paused == null) {
   226                      paused = source;
   227                  }
   228              }
   229          }
   230          
   231          return paused;
   232      }
   233      
   234      /**
   235       * Substitutes the keywords in the specified format with the values with
   236       * values from the specified source.
   237       * 
   238       * @param format The format to be substituted
   239       * @param source The source whose values should be used
   240       * @return The substituted string
   241       */
   242      public String doSubstitution(final String format, final MediaSource source) {
                 /* 
    P/P           *  Method: String doSubstitution(String, MediaSource)
                  * 
                  *  Preconditions:
                  *    format != null
                  *    source != null
                  * 
                  *  Presumptions:
                  *    getState(...)@251 != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   243          final String artist = source.getArtist();
   244          final String title = source.getTitle();
   245          final String album = source.getAlbum();
   246          final String app = source.getAppName();
   247          final String bitrate = source.getBitrate();
   248          final String filetype = source.getFormat();
   249          final String length = source.getLength();
   250          final String time = source.getTime();
   251          final String state = source.getState().getNiceName();
   252          
   253          return format.replaceAll("\\$artist", artist)
   254                       .replaceAll("\\$title", title)
   255                       .replaceAll("\\$album", album)
   256                       .replaceAll("\\$app", app)
   257                       .replaceAll("\\$bitrate", bitrate)
   258                       .replaceAll("\\$format", filetype)
   259                       .replaceAll("\\$length", length)
   260                       .replaceAll("\\$state", state)
   261                       .replaceAll("\\$time", time);
   262      }
   263      
   264      /**
   265       * Retrieves a source based on its name.
   266       *
   267       * @param name The name to search for
   268       * @return The source with the specified name or null if none were found.
   269       */
   270      public MediaSource getSource(final String name) {
                 /* 
    P/P           *  Method: MediaSource getSource(String)
                  * 
                  *  Preconditions:
                  *    this.managers != null
                  * 
                  *  Presumptions:
                  *    getAppName(...)@272 != null
                  *    java.util.Iterator:next(...)@271 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equalsIgnoreCase(...)@272: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@271: {0}, {1}
                  */
   271          for (final MediaSource source : getSources()) {
   272              if (source.getAppName().equalsIgnoreCase(name)) {
   273                  return source;
   274              }
   275          }
   276          
   277          return null;
   278      }
   279      
   280      /**
   281       * Retrieves all the sources registered with this plugin.
   282       *
   283       * @return All known media sources
   284       */
   285      public List<MediaSource> getSources() {
                 /* 
    P/P           *  Method: List getSources()
                  * 
                  *  Preconditions:
                  *    this.managers != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@288 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getSources#1)
                  *    new ArrayList(getSources#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@288: {0}, {1}
                  */
   286          final List<MediaSource> res = new ArrayList<MediaSource>(sources);
   287  
   288          for (MediaSourceManager manager : managers) {
   289              res.addAll(manager.getSources());
   290          }
   291  
   292          return res;
   293      }
   294  }








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