/*
 * @(#)Connect.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 * The start of a connection to CDWserver.
 *
 * @version 20021217
 * @author Jörg P. M. Haeger
 */
public class Connect extends Command {
	private String serverName;
	private int portNo;
	JOptionPane pane;

	public Connect(String serverName, int portNo) {
		this.serverName = serverName;
		this.portNo = portNo;
	}

	void processHint(String str) {
		super.processHint(str);
		Integer val;
		if (str.indexOf("anonymousUse") >= 0)
			CDcreator.anonymousUse = true;
		else
		if (str.indexOf("audioCDSupport") >= 0)
			CDcreator.audioCDSupport = true;
		else
		if (str.indexOf("cdrdao") >= 0)
			CDcreator.cdrdao = true;
		else
		if (str.indexOf("copy") >= 0)
			CDcreator.copySupport = true;
		else
		if ((val = parse(str, "defaultSpeed")) != null)
			Options.speed = val.intValue();
		else
		if (str.indexOf("DVDplusRWSupport") >= 0)
			CDcreator.DVDplusRWSupport = true;
		else
		if ((val = parse(str, "maxCopies")) != null)
			CDcreator.maxCopies = val.intValue();
		else
		if ((val = parse(str, "maxSpeed")) != null)
			CDcreator.maxSpeed = val.intValue();
		else
		if (str.indexOf("mp3") >= 0)
			CDcreator.mp3Support = true;
		else
		if (str.indexOf("overburnSupport") >= 0)
			CDcreator.overburnSupport = true;
		else
		if (str.indexOf("verify") >= 0)
			CDcreator.verifySupport = true;
	}

	void processInfo(String str) {
		super.processInfo(str);
		if (pane != null)
			pane.setMessage(infoStr);
	}

	public void run() {
		// try to connect to CDWserver
		pane = new JOptionPane(
			i18n("tryToConnect"),
			JOptionPane.INFORMATION_MESSAGE,
			JOptionPane.DEFAULT_OPTION,
			null, cancelButton);
		JDialog dialog = pane.createDialog(
			CDcreator.mainWin, i18n("titleTryToConnect"));
		showDialog(dialog);

		Socket socket;
		try {
			try {
				socket = new Socket(serverName, portNo);
			} catch (Exception e) {
				netscape.security.PrivilegeManager
					.enablePrivilege(
						"UniversalConnect");
				socket = new Socket(serverName, portNo);
			}

			CDcreator.inStream = new BufferedReader(
				new InputStreamReader(
					socket.getInputStream()));

			CDcreator.outStream = new DataOutputStream(
				socket.getOutputStream());
			CDcreator.outStream.writeBytes("CDWP\n");

			// get greeting
			readResult();
		}
		catch (Exception e)
		{
			socket = null;
		}
		CDcreator.socket = socket;
		hideDialog();

		if (socket == null) {
			status = ERROR;
			Version.showDebugInfo("no Connection");
			pane = new JOptionPane(
				i18n("noConnection"),
				JOptionPane.WARNING_MESSAGE,
				JOptionPane.DEFAULT_OPTION,
				null, okButton);
			dialog = pane.createDialog(
				CDcreator.mainWin, i18n("error"));
		}
		else
		if (!lastResultOK()) {
			CDcreator.socket = null;
			pane = new JOptionPane(
				lastResult,
				JOptionPane.WARNING_MESSAGE,
				JOptionPane.DEFAULT_OPTION,
				null, okButton);
			dialog = pane.createDialog(
				CDcreator.mainWin, i18n("error"));
		}
		else {
			execCommand("client " + Version.product
				+ " " + Version.version);
			execCommand("setProtocol 4");
			execCommand("system " + CDcreator.osName
				+ " " + CDcreator.osVersion);
			execCommand("vm " + CDcreator.vmName
				+ " " + CDcreator.vmVersion);
			execCommand("user noID " + State.userID);

			pane = new JOptionPane(
				infoStr,
				JOptionPane.INFORMATION_MESSAGE,
				JOptionPane.DEFAULT_OPTION,
				null, okButton);
			dialog = pane.createDialog(CDcreator.mainWin,
				i18n("titleConnectedTo"));
		}
		showInfoWin(dialog);
		super.run();
	}
}
