
// Project: webCDwriter - Network CD Writing
// Author: Jörg Haeger, 14.05.1999
//	joerg.haeger@Uni-Bielefeld.de

import java.awt.Graphics;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import netscape.security.PrivilegeManager;

public class CDcreator extends JApplet
	implements java.awt.event.ActionListener
{
	String serverName = Config.serverName;
	int serverPortNo = Config.serverPortNo;
	public static String osName, osVersion,
		vmName, vmVersion, userName;
	public static Socket socket;
	public static BufferedReader inStream;
	public static DataOutputStream outStream;
	public static CDcreator applet = null;
	public static java.net.URL codeBase = null;
	public static MainWin mainWin = null;
	private JButton startButton;
	public String startCmd = "Starten";
	public String stopCmd = "Beenden";
	public static Locale locale;
	static ResourceBundle messages;
	final public static int multiSession = 0,
		imageSession = 1, audioSession = 2;
	public static int sessionType = multiSession;
	public static int maxSpeed = 4;
	public static boolean mp3Support = false;

	public void actionPerformed(java.awt.event.ActionEvent e)
	{
		if (e.getActionCommand().equals(stopCmd))
		{
			closeConnection();
			mainWin.setVisible(false);
			mainWin = null;
			startButton.setEnabled(true);
			synchronized(this) {
				notifyAll();
			}
			return;
		}

		if (isNetscapeVM())
		try {
			PrivilegeManager.enablePrivilege(
				"UniversalPropertyRead");
			PrivilegeManager.enablePrivilege(
				"UniversalFileRead");
		}
		catch (Exception e2) {
		}

		// check the privileges
		try {
			File f = new SecFile("C:\\");
			if (!f.exists())
				f = new SecFile("/");
			f.exists();
		}
		catch (java.security.AccessControlException e1)
		{
			new JOptionPane(
				i18n("accessError"),
				JOptionPane.ERROR_MESSAGE,
				JOptionPane.DEFAULT_OPTION)
			.createDialog(this, i18n("titleAccessError")).show();
			return;
		}
		catch (Exception e2)
		{
			new JOptionPane(
				"unknown Error",
				JOptionPane.ERROR_MESSAGE,
				JOptionPane.DEFAULT_OPTION)
			.createDialog(this, "Error").show();
			return;
		}

		startButton.setEnabled(false);
		openMainWin();
	}

	void closeConnection()
	{
		try {
			if (socket != null)
			{
				socket.close();
				socket = null;
			}
		} catch (java.io.IOException e)
		{
			System.out.println(
				"closeConnection(): java.io.IOException");
		}
	}

	public void closeMainWin()
	{
		if (mainWin == null)
			return;

		try {
			if (socket != null)
			{
				socket.close();
				socket = null;
			}
		} catch (java.io.IOException e)
		{
			System.out.println(
				"closeMainWin(): java.io.IOException");
		}

		mainWin.setVisible(false);
		mainWin = null;
		if (isApplet())
//			startButton.setText(startCmd);
			startButton.setEnabled(true);
		else
			System.exit(0);
	}

	public void destroy()
	{
		Version.showDebugInfo(20, "Applet.destroy() start");
		synchronized(this) {
			while (mainWin != null)
			try {
				wait();
			} catch (InterruptedException e)
			{
			}
		}
		Version.showDebugInfo(20, "Applet.destroy() end");
	}

	static public String i18n(String key)
	{
		try {
			return messages.getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

	public void init()
	{
		readProperties();

		if (getParameter("language") == null
			|| getParameter("country") == null)
			locale = Locale.getDefault();
		else
			locale = new Locale(
				getParameter("language"),
				getParameter("country"));
		messages = ResourceBundle.getBundle(
			"Messages", locale);

		if (getParameter("server") != null)
			serverName = getParameter("server");
		if (getParameter("port") != null)
			serverPortNo = new Integer(getParameter("port"))
				.intValue();
		if (getParameter("debugNo") != null)
			Version.debugNo = new Integer(
				getParameter("debugNo")).intValue();
		Version.showDebugInfo(20, "Applet.init()");
		applet = this;
		codeBase = getCodeBase();
		startCmd = i18n("startCmd");
		stopCmd = i18n("quitCmd");
		startButton = new JButton(startCmd);
		startButton.addActionListener(this);
		startButton.setToolTipText(i18n("TTstart"));
		getContentPane().add(startButton);
//		getContentPane().add(new MainPanel());
	}

	public boolean isApplet()
	{
		return applet != null;
	}

	public static boolean isNetscapeVM()
	{
		return vmName.equals("unknown");
	}

	public static void main(String args[])
	{
		CDcreator app = new CDcreator();
		app.readProperties();
		String country = null, language = null;

		boolean error = false;
		for (int i = 0; i < args.length; i++)
			if (args[i].startsWith("server="))
				app.serverName = args[i].substring(7);
			else
			if (args[i].startsWith("port="))
				app.serverPortNo = new Integer(
					args[i].substring(5)).intValue();
			else
			if (args[i].startsWith("country="))
				country = args[i].substring(8);
			else
			if (args[i].startsWith("language="))
				language = args[i].substring(9);
			else
			if (args[i].startsWith("debugNo="))
				Version.debugNo = new Integer(
					args[i].substring(8)).intValue();
			else
				error = true;

		if (language == null || country == null)
			locale = Locale.getDefault();
		else
			locale = new Locale(language, country);
		messages = ResourceBundle.getBundle(
			"Messages", locale);

		if (error)
		{
			System.out.println(Version.product + " "
				+ Version.version);
//			System.out.println("Author: " + Version.author);
			System.out.println("Arguments and their defaults:");
			System.out.println("country=" + locale.getCountry());
			System.out.println("language=" + locale.getLanguage());
			System.out.println("port=" + Config.serverPortNo);
			System.out.println("server=" + Config.serverName);
			System.exit(0);
		}

		app.openMainWin();
	}

	public void openMainWin()
	{
		mainWin = new MainWin();
		if (true || applet == null)
			mainWin.setBounds(200, 200, 600, 450);
		else
		{
			java.awt.Point p = applet.getLocation();
			mainWin.setBounds(p.x, p.y, 600, 450);
			System.out.println(p.x + ", " + p.y);
		}
		mainWin.setVisible(true);

		userName = System.getProperty("user.name")
			.replace(' ', '_');

		Options.reset();

		try {
			new Connect(serverName, serverPortNo).exec();
			mainWin.typeSelection();
		}
		catch (CommandError cmdErr)
		{
			closeMainWin();
		}
	}

	private void readProperties()
	{
		osName = System.getProperty("os.name").replace(' ', '_');
		osVersion = System.getProperty("os.version")
			.replace(' ', '_');

		try {
			vmName = System.getProperty("java.vm.name");
			vmVersion = System.getProperty("java.vm.version");
		}
		catch (Exception e1)
		{
			try {
//				PrivilegeManager.enablePrivilege(
//					"UniversalPropertyRead");
				vmName = System.getProperty("java.vm.name");
				vmVersion = System.getProperty(
					"java.vm.version");
			}
			catch (Exception e2)
			{
				vmName = "unknown";
				vmVersion = "unknown";
			}
		}

		if (vmName == null || vmName.equals(""))
			vmName = "unknown";
		else
			vmName = vmName.replace(' ', '_');
		if (vmVersion == null || vmVersion.equals(""))
			vmVersion = "unknown";
		else
			vmVersion = vmVersion.replace(' ', '_');
	}

	public void stop()
	{
		Version.showDebugInfo(20, "Applet.stop()");
	}

	public void start()
	{
		Version.showDebugInfo(20, "Applet.start()");
	}
}
