package application.settings;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import application.constants.SettingsVariables;
import application.threads.SMTPThread;

public class SettingsSingleton implements Serializable
{
	private static final long serialVersionUID = 3840045425215974390L;
	private int port;
	private transient static SettingsSingleton instance;
	private transient Thread smtpThread;
	private transient SMTPThread smtp;
	private SettingsSingleton(int port)
	{
		// No direct creation
		this.port = port; 
	}
	
	public static SettingsSingleton getInstance()
	{
		if(instance == null)
		{
			// Load from the serialized file if there is one
			File file = new File(SettingsVariables.settingsFile);
			if(file.exists())
			{
				try
				{
					FileInputStream fin = new FileInputStream(SettingsVariables.settingsFile);
					ObjectInputStream oin = new ObjectInputStream(fin);
					instance = (SettingsSingleton)oin.readObject();
					oin.close();
					fin.close();
				}
				catch(Exception ex)
				{
					ex.printStackTrace(System.err);
				}
			}
			// if not just create a new instance
			else
			{
				instance = new SettingsSingleton(2525);
			}
		}
		return instance;
	}
	
	public void serialize()
	{
		try
		{
			File settingsFolder = new File("settings");
			if(settingsFolder.exists())
			{
				settingsFolder.setWritable(true,false);
				settingsFolder.mkdir();
			}
			
			FileOutputStream fout = new FileOutputStream("origami_settings.ser");
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			oout.writeObject(this);
			oout.close();
			fout.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void startSMTPServer()
	{
		System.out.println("Starting SMTP Server Thread");
		smtp = new SMTPThread(this.port);
		smtpThread = new Thread(smtp);
		smtpThread.start();
	}
	
	public void stopSMTPServer()
	{
		System.out.println("Interupting SMTP threads");
		try
		{
			smtp.stop();
		}
		catch(Exception ex)
		{
			System.err.println("Error: " + ex.getMessage());
			ex.printStackTrace(System.err);
		}
		smtpThread.interrupt();
		System.out.println("SMTP threads interupted");
	}
	
	public int getSMTPPort()
	{
		return this.port;
	}
	
	public void setSMTPPort(int port)
	{
		this.port = port;
	}
	
	public void restartSMTPServer()
	{
		stopSMTPServer();
		startSMTPServer();
	}

}

@@ -1579,25 +1579,34 @@
 eam(
-%22origami_
+SettingsVariables.
 settings
 .ser
@@ -1605,13 +1605,12 @@
 ings
-.ser%22
+File
 );%0A%09

Remove magic string