package modules.playlist;

import com.one.Application;
import com.one.IniFile;
import com.one.file.Connector;
import com.one.file.FileConnection;
import com.vmx.InputStreamDecoder;
import com.one.PlayList;
import filemanager.main;
import java.io.IOException;
import java.util.Vector;

public class M3UModule implements Application
{
	public void openFile(String filename, PlayList filelist) throws IOException
	{
		FileConnection fc = (FileConnection)Connector.open("file:///" + filename);
		InputStreamDecoder isd = InputStreamDecoder.getFileDecoder(fc);

		String disk = filename.substring(0, filename.indexOf('/'));
		String path = filename.substring(0, filename.lastIndexOf('/') + 1);

		Vector files = new Vector();
		String s;

		while((s = isd.readIniLine()) != null)
		{
			if(s.startsWith("#"))
			{
				continue;
			}

			s = s.replace('\\', '/');

			if(s.indexOf(':') >= 0)
			{
				files.addElement(s);
			}
			else if(s.startsWith("/"))
			{
				files.addElement(disk + s);
			}
			else
			{
				files.addElement(path + s);
			}
		}
		
		isd.close();
		fc.close();

		main.FileSelect.executeFile(new PlayList(files.elements()), null, main.manager.currentPanel());
	}
}
