/*****************************************************/
/*          This java file is a part of the          */
/*                                                   */
/*           -  Plouf's Java IRC Client  -           */
/*                                                   */
/*   Copyright (C)  2002 - 2004 Philippe Detournay   */
/*                                                   */
/*         All contacts : theplouf@yahoo.com         */
/*                                                   */
/*  PJIRC is free software; you can redistribute     */
/*  it and/or modify it under the terms of the GNU   */
/*  General Public License as published by the       */
/*  Free Software Foundation; version 2 or later of  */
/*  the License.                                     */
/*                                                   */
/*  PJIRC 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.         */
/*                                                   */
/*  You should have received a copy of the GNU       */
/*  General Public License along with PJIRC; if      */
/*  not, write to the Free Software Foundation,      */
/*  Inc., 59 Temple Place, Suite 330, Boston,        */
/*  MA  02111-1307  USA                              */
/*                                                   */
/*****************************************************/

package irc.security;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Default secured provider.
 */
public class DefaultSecuredProvider implements SecuredProvider {

	@Override
	public Socket getSocket(String host, Integer port) throws UnknownHostException, IOException {
		return new Socket(host, port.intValue());
	}

	@Override
	public ServerSocket getServerSocket(Integer port) throws IOException {
		return new ServerSocket(port.intValue());
	}

	@Override
	public FileInputStream getFileInputStream(File file) throws IOException {
		return new FileInputStream(file);
	}

	@Override
	public FileOutputStream getFileOutputStream(File file) throws IOException {
		return new FileOutputStream(file);
	}

	@Override
	public Integer getFileSize(File file) {
		return new Integer((int) file.length());
	}

	@Override
	public File getLoadFile(String title) {
		Frame f = new Frame();
		FileDialog dlg = new FileDialog(f, title, FileDialog.LOAD);
		dlg.show();
		File ans = null;
		if (dlg.getFile() != null)
			ans = new File(dlg.getDirectory() + dlg.getFile());
		dlg.hide();
		dlg.dispose();
		f.dispose();
		return ans;
	}

	@Override
	public File getSaveFile(String title) {
		Frame f = new Frame();
		FileDialog dlg = new FileDialog(f, title, FileDialog.SAVE);
		dlg.show();
		File ans = null;
		if (dlg.getFile() != null)
			ans = new File(dlg.getDirectory() + dlg.getFile());
		dlg.hide();
		dlg.dispose();
		f.dispose();
		return ans;
	}

	@Override
	public File getSaveFile(String file, String title) {
		Frame f = new Frame();
		FileDialog dlg = new FileDialog(f, title, FileDialog.SAVE);
		dlg.setFile(file);
		dlg.show();
		File ans = null;
		if (dlg.getFile() != null)
			ans = new File(dlg.getDirectory() + dlg.getFile());
		dlg.hide();
		dlg.dispose();
		f.dispose();
		return ans;
	}

	@Override
	public InetAddress getLocalHost() throws UnknownHostException {
		InetAddress[] addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
		return addresses[addresses.length - 1];
	}

	@Override
	public String resolve(InetAddress addr) {
		return addr.getHostName();
	}

	@Override
	public boolean tryProvider() {
		return true;
	}

	@Override
	public String getName() {
		return "Default Security Provider";
	}

}
