/*********************************************************************************************
 * Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: CurSet.cs $     $Revision: 2 $
 * $Author: Jsj $   $Date: 5/17/05 11:51a $
 *
 * $History: CurSet.cs $
 * 
 * *****************  Version 2  *****************
 * User: Jsj          Date: 5/17/05    Time: 11:51a
 * Updated in $/LibSource/Utils
 * cleanup
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 7/27/04    Time: 8:34a
 * Created in $/LibSource/Utils
 *********************************************************************************************/

using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace Utils
{
	/// <summary>
	/// Summary description for CurSet.
	/// </summary>
	public class CurSet
	{
		public string PathName;
		public string DefaultDestFName; //[MAXPATH]
		public string DefaultPlantFmt; //[10];
		public string DefaultPrinter; //[4];
		public ArrayList DefaultUserCBMsg; //[2][10];
		public string DefaultUserFmt; //[3];
		public ArrayList DefPenColors;  //[8]
		public short DefaultChangeBars;
		public short DefaultDestination;
		public short DefaultOriginals;
		public short DefaultPagination;
		public short DefaultPaperMacro;
		public short DefaultPlotterType,DefaultPlotterPort,DefaultCIEType;
		public short DontPrintStatusTree;
		public short DisableDuplexPrinting;

		private string[] SettingFiles={"..\\procs\\curset.dat", "~curset.dat"};
		
		public CurSet(string pname)
		{
			PathName = pname;
		
		}
		
		public bool Create(UserRunTime usrRT)
		{
			// find file to copy from procs or system.
			bool found = false;
			int i=0;
			while(found==false)
			{
				if (File.Exists(usrRT.ExeAdjust(SettingFiles[i])) == true) 
					found=true;
				else
					i++;
			}
			if (found==false) return false;
			File.Copy(usrRT.ExeAdjust(SettingFiles[i]),"curset.dat");
			PathName = "curset.dat";
			return true;
		}

		private string ReadTheString(BinaryReader bw, int maxlen)
		{
			StringBuilder retStr = new StringBuilder(maxlen+1);
			// read a series of characters until a null is found.
			char ac;
			bool done = false;
			while(done==false)
			{
				ac = bw.ReadChar();
				if (ac=='\0') 
					done=true;
				else
					retStr.Append(ac);
			}
			return retStr.ToString();
		}

		public bool Read()
		{
			BinaryReader br;
			ArrayList DefaultUserCBMsg = new ArrayList(2);
			ArrayList DefPenColors = new ArrayList(8);
			FileStream fs;
			FileInfo fi = new FileInfo(PathName);
			try
			{
				fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
				br = new BinaryReader(fs,System.Text.ASCIIEncoding.ASCII);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.ToString(),"CURSET.DAT Error");
				return false;
			}

			try
			{
				DefaultDestination=br.ReadSByte();
				DefaultChangeBars= br.ReadSByte();
				DefaultOriginals=br.ReadSByte();
				DefaultPagination=br.ReadSByte();
				DefaultPrinter = ReadTheString(br,4);
				DefaultPlantFmt = ReadTheString(br,10);
				DefaultDestFName = ReadTheString(br,128);
				DefaultPlotterType = br.ReadSByte();
				DefaultPlotterPort = br.ReadSByte();
				DefaultCIEType = br.ReadSByte();
				short tmp;
				for (int i=0;i<8;i++)
				{
					tmp = br.ReadSByte();
					DefPenColors.Add(tmp);
				}
			
				DefaultPaperMacro = br.ReadSByte();
				string stmp;
				stmp = ReadTheString(br,10);
				DefaultUserCBMsg.Add(stmp);
				stmp = ReadTheString(br,10);
				DefaultUserCBMsg.Add(stmp);
				DontPrintStatusTree = br.ReadSByte();
				DefaultUserFmt = ReadTheString(br,10);
				DisableDuplexPrinting = br.ReadSByte();
				br.Close();
			}
			catch(Exception e)
			{
				if(br!=null) br.Close();
			}
			fs.Close();
			return true;
		}

		public bool Save(string filename)
		{
			// may be a different pathname, if so, change it.
			if (filename.ToUpper() != PathName.ToUpper()) PathName = filename;

			FileStream fs = new FileStream(PathName, FileMode.Create);
			BinaryWriter w = new BinaryWriter(fs);

			w.Write(DefaultDestination);
			w.Write(DefaultChangeBars);
			w.Write(DefaultOriginals);
			w.Write(DefaultPagination);
			w.Write(DefaultPrinter);
			w.Write(DefaultPlantFmt);
			w.Write(DefaultDestFName);
			w.Write(DefaultPlotterType);
			w.Write(DefaultPlotterPort);
			w.Write(DefaultCIEType);
			w.Write((short)DefPenColors[0]);
			w.Write((short)DefPenColors[1]);
			w.Write((short)DefPenColors[2]);
			w.Write((short)DefPenColors[3]);
			w.Write((short)DefPenColors[4]);
			w.Write((short)DefPenColors[5]);
			w.Write((short)DefPenColors[6]);
			w.Write((short)DefPenColors[7]);
			w.Write(DefaultPaperMacro);
			w.Write((string)DefaultUserCBMsg[0]);
			w.Write((string)DefaultUserCBMsg[1]);
			w.Write(DontPrintStatusTree);
			w.Write(DefaultUserFmt);
			w.Write(DisableDuplexPrinting);
			w.Close();
			fs.Close();
			return true;
		}
	}
}
