/******************************************************************************* * SDR Trunk * Copyright (C) 2014 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see ******************************************************************************/ package playlist; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import properties.SystemProperties; public class PlaylistManager { private final static Logger mLog = LoggerFactory.getLogger( PlaylistManager.class ); private Playlist mPlaylist = new Playlist(); public PlaylistManager() { init(); } public Playlist getPlayist() { return mPlaylist; } /** * Loads playlist from the current playlist file, or the default playlist file, * as specified in the current SDRTRunk system settings */ private void init() { SystemProperties props = SystemProperties.getInstance(); Path playlistFolder = props.getApplicationFolder( "playlist" ); String defaultPlaylistFile = props.get( "playlist.defaultfilename", "playlist.xml" ); String playlistFile = props.get( "playlist.currentfilename", defaultPlaylistFile ); load( playlistFolder.resolve( playlistFile ) ); } public void save() { JAXBContext context = null; SystemProperties props = SystemProperties.getInstance(); Path playlistPath = props.getApplicationFolder( "playlist" ); String playlistDefault = props.get( "playlist.defaultfilename", "playlist.xml" ); String playlistCurrent = props.get( "playlist.currentfilename", playlistDefault ); Path filePath = playlistPath.resolve( playlistCurrent ); File outputFile = new File( filePath.toString() ); try { if( !outputFile.exists() ) { outputFile.createNewFile(); } } catch( Exception e ) { mLog.error( "couldn't create file to save " + "playlist [" + filePath.toString() + "]" ); } OutputStream out = null; try { out = new FileOutputStream( outputFile ); try { context = JAXBContext.newInstance( Playlist.class ); Marshaller m = context.createMarshaller(); m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true ); m.marshal( mPlaylist, out ); } catch ( JAXBException e ) { mLog.error( "jaxb exception while saving playlist: ", e ); } } catch ( Exception e ) { mLog.error( "coulcn't open outputstream to save playlist [" + filePath.toString() + "]", e ); } finally { if( out != null ) { try { out.close(); } catch ( IOException e ) { e.printStackTrace(); } } } } /** * Erases current playlist and loads playlist from the playlistPath filename, * if it exists. */ public void load( Path playlistPath ) { if( Files.exists( playlistPath ) ) { mLog.info( "loading playlist file [" + playlistPath.toString() + "]" ); JAXBContext context = null; InputStream in = null; try { in = new FileInputStream( playlistPath.toString() ); try { context = JAXBContext.newInstance( Playlist.class ); Unmarshaller m = context.createUnmarshaller(); mPlaylist = (Playlist)m.unmarshal( in ); } catch ( JAXBException e ) { mLog.error( "jaxb exception while loading playlist: ", e ); } } catch ( Exception e ) { mLog.error( "coulcn't open inputstream to load playlist [" + playlistPath.toString() + "]", e ); } finally { if( in != null ) { try { in.close(); } catch ( IOException e ) { mLog.error( "exception while closing " + "the playlist file inputstream reader", e ); } } } } else { mLog.info( "playlist does not exist [" + playlistPath.toString() + "]" ); } if( mPlaylist == null ) { mPlaylist = new Playlist(); } } }