Package org.jampa.model.playlists

Source Code of org.jampa.model.playlists.AudioItem

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.model.playlists;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.IAudioItem;
import org.jampa.utils.Constants;
import org.jampa.utils.SystemUtils;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.tag.Tag;


public class AudioItem implements IAudioItem {
 
  public static final String TITLE = "TITLE";
  public static final String ARTIST = "ARTIST";
  public static final String ALBUM = "ALBUM";
  public static final String GENRE = "GENRE";
  public static final String YEAR = "YEAR";
  public static final String COMMENT = "COMMENT";
 
  private String _fileName;
 
  private String _title; 
  private String _artist;
  private String _album;
  private String _trackNumber;
  private int _trackLength;
  private String _genre;
  private String _year;
  private String _comment;
 
  private boolean _boTagsLoaded; 
  private boolean _boPlaying;
 
  private boolean _isRemovable;
 
  private Map<String, String> _propertiesList;
  private Map<String, String> _propertiesLabelList;
 
  public AudioItem(AudioItem original) {
    _fileName = original._fileName;
    _artist = original._artist;
    _title = original._title;
    _album = original._album;
    _trackNumber = original._trackNumber;
    _trackLength = original._trackLength;
    _genre = original._genre;
    _year = original._year;
    _comment = original._comment;
    _boTagsLoaded = original._boTagsLoaded;
    _isRemovable = original._isRemovable;
    _boPlaying = false
  }
 
  public AudioItem(String fileName, boolean isRemovable) {
    _fileName = fileName;   
    setBoPlaying(false);   
   
    _artist = ""; //$NON-NLS-1$
    _title = ""; //$NON-NLS-1$
    _album = ""; //$NON-NLS-1$
    _trackLength = 0;
    _trackNumber = ""; //$NON-NLS-1$
    _genre = ""; //$NON-NLS-1$
    _year = ""; //$NON-NLS-1$
    _comment = ""; //$NON-NLS-1$
    _boTagsLoaded = false;
   
    _isRemovable = isRemovable;
  }
 
  public AudioItem(String fileName, boolean loadTagsNow, boolean isRemovable) {
    this(fileName, isRemovable);
   
    if (loadTagsNow) {
      readTags();
    }
  }
 
  private void buildLists() {
    _propertiesLabelList = new HashMap<String, String>();
    _propertiesLabelList.put(TITLE, Messages.getString("Commons.Title"));
    _propertiesLabelList.put(ARTIST, Messages.getString("Commons.Artist"));
    _propertiesLabelList.put(ALBUM, Messages.getString("Commons.Album"));
    _propertiesLabelList.put(GENRE, Messages.getString("Commons.Genre"));
    _propertiesLabelList.put(YEAR, Messages.getString("Commons.Year"));
    _propertiesLabelList.put(COMMENT, Messages.getString("Commons.Comment"));
   
    _propertiesList = new HashMap<String, String>();
    _propertiesList.put(TITLE, _title);
    _propertiesList.put(ARTIST, _artist);
    _propertiesList.put(ALBUM, _album);
    _propertiesList.put(GENRE, _genre);
    _propertiesList.put(YEAR, _year);
    _propertiesList.put(COMMENT, _comment);
  }
 
  private void verifyInformations() {
    if ((_title == null) ||
        (_title.isEmpty())) {
      _title = _fileName.substring(_fileName.lastIndexOf(SystemUtils.fileSeparator) + 1);       
    }
   
    // Fields must be not null in order to sort correctly.
    if (_artist == null)
      _artist = ""; //$NON-NLS-1$
   
    if (_album == null)
      _album = ""; //$NON-NLS-1$
   
    if (_trackNumber == null)
      _trackNumber = ""; //$NON-NLS-1$
   
    if (_genre == null)
      _genre = ""; //$NON-NLS-1$
   
    if (_year == null)
      _year = "";     //$NON-NLS-1$
   
    if (_comment == null)
      _comment = "";     //$NON-NLS-1$
  }
 
  private void readTagsFromFile() {
    try {
      AudioFile f = AudioFileIO.read(new File(_fileName));
     
      AudioHeader ah = f.getAudioHeader();
      Tag tag = f.getTag();
     
      _title = tag.getFirstTitle();
      _artist = tag.getFirstArtist();
      _album = tag.getFirstAlbum();
      _trackNumber = tag.getFirstTrack();
      _trackLength = ah.getTrackLength()
      _genre = tag.getFirstGenre();
      _year = tag.getFirstYear()
      _comment = tag.getFirstComment();

    } catch (Exception e) {
      Log.getInstance(AudioItem.class).warn("Error while reading file properties : " + _fileName + " (" + e.toString() +")."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    } finally {
     
      verifyInformations();     
      _boTagsLoaded = true;
     
    }
  }
 
  private void readTags() {
    ResultSet rs = Controller.getInstance().getHSQLController().getLibraryItemData(_fileName);
    try {     
      rs.next();
       
      _title = rs.getString("TITLE"); //$NON-NLS-1$
      _artist = rs.getString("ARTIST"); //$NON-NLS-1$
      _album = rs.getString("ALBUM"); //$NON-NLS-1$
      _trackNumber = rs.getString("TRACKNUMBER"); //$NON-NLS-1$
      _trackLength = rs.getInt("TRACKLENGTH"); //$NON-NLS-1$
      _genre = rs.getString("GENRE"); //$NON-NLS-1$
      _year = rs.getString("YEAR"); //$NON-NLS-1$
      _comment = rs.getString("COMMENT"); //$NON-NLS-1$

    } catch (SQLException e) {
      readTagsFromFile();
      Log.getInstance(AudioItem.class).warn("Item not found in database, reading tags : " + _fileName); //$NON-NLS-1$
    } finally {
     
      verifyInformations();     
      _boTagsLoaded = true;
     
    }
   
    buildLists();
  }
   
  public String getFileName() {
    return _fileName; 
  }
 
  public Map<String, String> getPropertiesLabelList() {
    if (!_boTagsLoaded)
      readTags();
    return _propertiesLabelList;
  }
 
  public Map<String, String> getPropertiesList() {
    if (!_boTagsLoaded)
      readTags();
    return _propertiesList;
  }
 
  public String getOSDMessage() {
    return Messages.getString("Commons.Artist") + " " + _artist + "\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      Messages.getString("Commons.Album") + " " + _album + "\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      Messages.getString("Commons.Title") + " " + _title; //$NON-NLS-1$ //$NON-NLS-2$
  }
 
  public String getToolTipMessage() {
    return Constants.APP_NAME + " - " + //$NON-NLS-1$
    (Controller.getInstance().getEngine().isPaused() ? Messages.getString("Controller.TooltipPaused") : Messages.getString("Controller.TooltipPlaying")) + "\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Messages.getString("Commons.Artist") + " " + _artist + "\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Messages.getString("Commons.Album") + " " + _album + "\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Messages.getString("Commons.Title") + " " + _title; //$NON-NLS-1$ //$NON-NLS-2$
  }
 
  public String getTitleBarMessage() {
    return (!_artist.equals("") ? _artist + " - " : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    (!_album.equals("") ? _album + " - " : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    (!_title.equals("") ? _title + " - " : "") + Constants.APP_NAME; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  }
 
  public String getStatusBarMessageOnPlay() {
    return Messages.getString("Commons.PlayNewAudioItem") + " " + _artist +
    " (" + _artist + " / " + _album + ").";
  }
 
  public String getTrackNumber() {
    if (!_boTagsLoaded)
      readTags();
    return _trackNumber;
  }

  public int getTrackLength() {
    if (!_boTagsLoaded)
      readTags();
    return _trackLength;
  }

  public void setBoPlaying(boolean _boPlaying) {
    this._boPlaying = _boPlaying;
  }

  public boolean isBoPlaying() {
    return _boPlaying;
  }   
 
  public void setBoTagsLoaded(boolean value) {
    this._boTagsLoaded = value;
  }
 
  public boolean isRemovable() {
    return _isRemovable;
  }

}
TOP

Related Classes of org.jampa.model.playlists.AudioItem

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.