Package org.rssowl.contrib.podcast.player

Source Code of org.rssowl.contrib.podcast.player.PlayerLogic

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2006 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   All rights reserved                                                    **
**                                                                          **
**   This program and the accompanying materials are made available under   **
**   the terms of the Eclipse Public License v1.0 which accompanies this    **
**   distribution, and is available at:                                     **
**   http://www.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     Christophe Bouhier - podcast plugin                         **
**                                                                          **
**  **********************************************************************  */

package org.rssowl.contrib.podcast.player;

import java.util.Collection;
import java.util.Iterator;

import org.rssowl.contrib.podcast.content.Content;
import org.rssowl.contrib.podcast.content.ContentException;
import org.rssowl.contrib.podcast.content.ContentLogic;
import org.rssowl.contrib.podcast.model.IXFile;
import org.rssowl.contrib.podcast.util.Logger;

/**
* A player function. The migration from jPodder supported only external players
* loaded through a plugin.
*
* The function will be enhanced by being able to register internal java based
* players.
*
*
* @author <a href="mailto:christophe@kualasoft.com">Christophe Bouhier </a>
* @author <a href="mailto:andreas.schaefer@madplanet.com">Andreas Schaefer </a>
* @version 1.1
*/
public class PlayerLogic {

  private Logger mLog = new Logger(getClass().getName());

  private static PlayerLogic sSelf;

  private IPlayer mDefaultPlayer = null;

  public static PlayerLogic getInstance() {
    if (sSelf == null) {
      sSelf = new PlayerLogic();
      sSelf.loadPlayers();
    }
    return sSelf;
  }

  /**
   * Get the plugin names, and compare with the default player. If we don't
   * have a plugin for the default player we set to &quotNo Player&quot.
   */
  public String[] loadPlayers() {

    Collection<IPlayer> lPlayers = PlayerPluginService.getInstance()
        .getPlayers();
    int lSize = lPlayers.size();
    // We need one extra player for "No Player"
    String[] lPlayerNames = new String[lSize + 1];
    mDefaultPlayer = new NoPlayer();
    lPlayerNames[0] = mDefaultPlayer.getName();

    Iterator<IPlayer> lIter = lPlayers.iterator();
    boolean lDefaultFound = false;
    for (int i = 0; i < lSize; i++) {
      if (lIter.hasNext()) {
        lPlayerNames[i + 1] = lIter.next().getName();
        if (lPlayerNames[i + 1].equals(mDefaultPlayer.getName())) {
          lDefaultFound = true;
        }
      }
    }

    // if the default player is not in the list of players
    // any more, we should override the default selection.
    if (!lDefaultFound) {
      // The default player doesn't exist anymore.
      mLog.info("The default player: " + mDefaultPlayer.getName()
          + " is not in \\plugin folder");
    }
    return lPlayerNames;
  }

  /**
   * Store the file in the player.
   *
   * @param pFileWrapper
   */
  public void storeInPlayer(String pPlaylist, IXFile pFileWrapper) {

    if (pFileWrapper.isLocal()) {
      addPlayList(pPlaylist);
      if (addTrack(pPlaylist, pFileWrapper)) {
        pFileWrapper.setInPlayer(true);
      }
    }
  }

  /**
   * Play a track.
   *
   * @param pPlaylist
   * @param pFile
   * @return
   */
  public boolean playTrack(String pPlaylist, IXFile pFile) {
    boolean lSuccess = false;

    IPlayer lPlayer = getBestPlayer(pFile);
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }
    String lTrackPath = pFile.getFile().getAbsolutePath();
    try {
      lPlayer.play(pPlaylist, lTrackPath);
      lSuccess = true;
      mLog.info("Succesfully playing: " + lTrackPath + "in "
          + lPlayer.getName());
    } catch (Exception e1) {
      mLog.warn("Failed to play: " + lTrackPath + "from "
          + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }
    return lSuccess;
  }

  /**
   * Delete a track.
   *
   * @param pPlaylist
   * @param pFile
   * @return
   */
  public boolean deleteTrack(String pPlaylist, IXFile pFile) {
    boolean lSuccess = false;

    IPlayer lPlayer = getBestPlayer(pFile);
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }
    String lTrackPath = pFile.getFile().getAbsolutePath();
    try {
      lPlayer.deleteTrack(pPlaylist, lTrackPath);
      lSuccess = true;
      mLog.info("Succesfully deleted: " + lTrackPath + "from "
          + lPlayer.getName());
    } catch (Exception e1) {
      mLog.warn("Failed to delete: " + lTrackPath + "from "
          + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }
    return lSuccess;
  }

  /**
   * Add a media item to the provided playlist. The best player is selected.
   *
   * @param pPlayList
   * @param pFile
   * @return
   */
  public boolean addTrack(String pPlayList, IXFile pFile) {
    boolean lSuccess = false;

    if (pFile.getInPlayer()) {
      return false; // Sheez don't call us!
    }

    IPlayer lPlayer = getBestPlayer(pFile);
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }

    String lTrackPath = pFile.getFile().getAbsolutePath();

// CB TODO, migrate .torrent check.
// if (pFile instanceof IPersonalAttachment) {
// if (((IPersonalAttachment) pFile).isTorrent()) {
// lTrackPath = ((IPersonalAttachment) pFile)
// .getTorrentFile().getPath();
// mLog.info("The file to be added is a .torrent");
// }
// }

    try {
      lPlayer.addPlaylist(pPlayList);
      lPlayer.addTrack(pPlayList, lTrackPath);
      if (lPlayer.hasTrack(pPlayList, lTrackPath)) {
        lSuccess = true;
        mLog.info("Succesfully added: " + lTrackPath + "to "
            + lPlayer.getName());
      }
    } catch (Exception e1) {
      mLog.warn("Failed to add: " + lTrackPath + "to "
          + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }
    return lSuccess;
  }

  /**
   * Check if a track is available.
   *
   * @param pPlayList
   * @param pFile
   * @return
   */
  public boolean hasTrack(String pPlayList, IXFile pFile) {
    boolean lSuccess = false;

    IPlayer lPlayer = getBestPlayer(pFile);
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }

    String lTrackPath = pFile.getFile().getAbsolutePath();

    try {
      lSuccess = lPlayer.hasTrack(pPlayList, lTrackPath);
    } catch (Exception ie) {
      mLog.warn("Failed to check if: " + lTrackPath + "is stored in "
          + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }
    return lSuccess;
  }

  /**
   * Remove a playlist.
   *
   * @param pPlayList
   * @return
   */
  public boolean removePlayList(String pPlayList) {
    boolean lSuccess = false;

    IPlayer lPlayer = mDefaultPlayer;
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }
    try {
      lPlayer.removePlayList(pPlayList);
      lSuccess = true;
      mLog.info("Succesfully removed: " + pPlayList + "from "
          + lPlayer.getName());
    } catch (Exception e1) {
      mLog.warn("Failed to remove: " + pPlayList + "from "
          + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }

    return lSuccess;
  }

  public boolean addPlayList(String pPlayList) {
    boolean lSuccess = false;

    IPlayer lPlayer = mDefaultPlayer;
    if (lPlayer instanceof NoPlayer || !initPlayer(lPlayer)) {
      return false;
    }

    try {
      lPlayer.addPlaylist(pPlayList);
      lSuccess = true;
      mLog.info("Succesfully add: " + pPlayList + "to "
          + lPlayer.getName());
    } catch (Exception e1) {
      mLog
          .warn("Failed to add: " + pPlayList + "to "
              + lPlayer.getName());
    } finally {
      lPlayer.finalize();
    }
    return lSuccess;
  }

  public boolean initPlayer(IPlayer lPlayer) {
    try {
      lPlayer.initialize();
      return true;
    } catch (Exception e) {
      mLog
          .warn("Attempt to access a player, which was not loaded properly");
      mLog.warn("See the \\bin\\jPodder.log for details of the failure");
      return false;
    }
  }

  /**
   * Satisfies the property interface. We want to know the selected player We
   * update the status bar.
   *
   * @param e
   *            PropertyEvent
   */

  // Migrate paramaters.
// public void configurationChanged(ConfigurationEvent e) {
// if (!e.getSource().equals(ConfigurationLogic.class)) {
// return;
// }
// String lPlayerName = Configuration.getInstance().getPlayer();
// mDefaultPlayer = (IPlayer) PluginLogic.getInstance().getPluginByName(
// lPlayerName);
// if (mDefaultPlayer == null) {
// mDefaultPlayer = new NoPlayer();
// // We could fire an event as player type is changed by the logic.
// Configuration.getInstance().setPlayer(mDefaultPlayer.getName());
// ConfigurationLogic.getInstance().fireConfigurationChanged(
// new ConfigurationEvent(this, Configuration.CONFIG_PLAYER));
// }
// mLog.info("Player set to: " + mDefaultPlayer.getName());
// }
  /**
   * Get the MIME for this file. this is either a defined type or translated
   * from the file extension. If the default is not <code>NoPlayer</code> we
   * grab an application which supports this MIME type. If none is found, we
   * return the <code>NoPlayer</code>
   *
   * @param pFileWrapper
   * @return
   */
  public IPlayer getBestPlayer(IXFile pFileWrapper) {
    String lMimeType = pFileWrapper.getFileType();
    if (lMimeType == null || lMimeType.length() == 0) {
      lMimeType = Content.getContentFromFileName(pFileWrapper.getFile()
          .getName());
    }

    IPlayer lPlayer = mDefaultPlayer;
    if (lMimeType != null) {
      if (!ContentLogic.getInstance().supportsContent(lMimeType, lPlayer)) {
        if (!(mDefaultPlayer instanceof NoPlayer)) {
          try {
            lPlayer = ContentLogic.getInstance().getPlayer(
                new Content(lMimeType));
          } catch (ContentException ce) {
            // Mime type is not recognized.
          }
          if (lPlayer == null) {
            lPlayer = new NoPlayer();
          }
        }
      }
    }
    return lPlayer;
  }

  public IPlayer getDefaultPlayer() {
    return mDefaultPlayer;
  }
}
TOP

Related Classes of org.rssowl.contrib.podcast.player.PlayerLogic

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.