Package org.jampa.gui.runnables

Source Code of org.jampa.gui.runnables.PlaylistExporter

/*
* 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.gui.runnables;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.playlists.Playlist;
import org.jampa.utils.SystemUtils;
import org.jampa.utils.SystemUtils.PlaylistFormat;

public class PlaylistExporter implements IRunnableWithProgress {
 
  private String _path;
  private boolean _isDirectory;
  private List<String> _playlists;
  private PlaylistFormat _format;
 
  public PlaylistExporter(String path, PlaylistFormat format, boolean isDirectory, List<String> playlists) {
    _path = path;
    _isDirectory = isDirectory;
    _playlists = playlists;
    _format = format;
  }
 
  public void writeSelectedPlaylistsToZip(IProgressMonitor monitor) {
    File tmpFile = new File(_path);
    if (tmpFile.exists()) {
      tmpFile.delete();
    }
   
    byte[] buf = new byte[1024];
    try {
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(_path));
     
      Playlist playlist;
      InputStream in;
      String name;
      Iterator<String> iter = _playlists.iterator();
      while (iter.hasNext()) {
        if (monitor.isCanceled()) {
          out.close();
          return;
        }       
       
        playlist = Controller.getInstance().getPlaylistController().getPlaylistByName(iter.next());
       
        monitor.subTask(Messages.getString("PlaylistExporter.ExportPlaylist") + " " + playlist.getName()); //$NON-NLS-1$ //$NON-NLS-2$
       
        if (playlist != null) {     
          if (_format == PlaylistFormat.XSPF) {
            name = playlist.getName() + SystemUtils.playlistXSPFExtension;
            in = playlist.getPlaylistStreamAsXSPF();
          } else {
            name = playlist.getName() + SystemUtils.playlistM3UExtension;
            in = playlist.getPlaylistStreamAsM3U();
          }
         
          // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(name));

                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
               
                // Complete the entry
                out.closeEntry();
                in.close();
                monitor.worked(1);
        }
      }
      // Complete the ZIP file
          out.close();     
    } catch (Exception e) {
      Log.getInstance(PlaylistExporter.class).error("Error while writting file : " + _path);     //$NON-NLS-1$
    }   
  }
 
  private void writeSelectedPlaylistsToDirectory(IProgressMonitor monitor) {
    Playlist playlist;
    Iterator<String> iter = _playlists.iterator();
    while (iter.hasNext()) {
      if (monitor.isCanceled())
        return;     
     
      playlist = Controller.getInstance().getPlaylistController().getPlaylistByName(iter.next());
     
      monitor.subTask(Messages.getString("PlaylistExporter.ExportPlaylist") + " " + playlist.getName()); //$NON-NLS-1$ //$NON-NLS-2$
     
      if (playlist != null) {
        playlist.writePlaylistToDirectory(_path, _format);
      }
      monitor.worked(1);
    }
  }

  @Override
  public void run(IProgressMonitor monitor) throws InvocationTargetException,  InterruptedException {
    monitor.beginTask(Messages.getString("PlaylistExporter.PlaylistExportTitle"), _playlists.size()); //$NON-NLS-1$
   
    if (_isDirectory) {
      writeSelectedPlaylistsToDirectory(monitor);
    } else {
      writeSelectedPlaylistsToZip(monitor);
    }
   
    monitor.done();
  }

}
TOP

Related Classes of org.jampa.gui.runnables.PlaylistExporter

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.