/*********************************************************************************************
 * Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: DataPath.cs $     $Revision: 5 $
 * $Author: Jsj $   $Date: 3/06/07 1:26p $
 *
 * $History: DataPath.cs $
 * 
 * *****************  Version 5  *****************
 * User: Jsj          Date: 3/06/07    Time: 1:26p
 * Updated in $/LibSource/VEObject
 * check for a null Location.
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 1/24/05    Time: 2:45p
 * Updated in $/LibSource/VEObject
 * B2005-004 fixes
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 1/19/05    Time: 11:59a
 * Updated in $/LibSource/VEObject
 * Fix B2005-003 - missing plant security setting
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 1/14/05    Time: 10:38a
 * Updated in $/LibSource/VEObject
 * B2004-061: fix security options
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 7/27/04    Time: 8:53a
 * Created in $/LibSource/VEObject
 *********************************************************************************************/
using System;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.IO;
using System.ComponentModel;
using Utils;
namespace VEObject
{
	/// 
	/// This defines the DataPath class, which handles the datapath information
	/// which tells the program where to search for veproms data. A datapath is of 
	/// the form "[path],[title];" for example G:\,GDRIVE;
	/// Its children are vedata directories as found in the menuwin.xml file.
	/// These vedata directories represent plant data.
	/// 
	public class VEO_DataPath : VEO_Base
	{
		protected string tmpTitle;
		protected string tmpLocation;
		protected bool changeLocation;
		protected bool changeTitle;
		private string PrevDirectory;
		public VEO_DataPath(UserRunTime iusrRunTime, string ititle, string iloc)
		{
			iconStates = new int[5] {6,21,8,22,7};
			_Title = ititle;
			_Location = iloc;
			usrRunTime = iusrRunTime;
			changeTitle=false;
			changeLocation=false;
			isNew=false;
			LoadLockInfo();
			icon = iconStates[(int)Lock.LockStatus];
		}
		
		// the following properties are used in the properties dialog for modification
		// of 'property' data.
		[Description("Location"),Category("Data Path"),ReadOnly(true)]public string Location
		{
			get{return _Location;}
			set
			{
				ShortName sname = new ShortName(value);
				_Location=sname.ShortFileName;
				sname = null;
			}
		}
		[Description("Title"),Category("Data Path")]public string Title
		{
			get
			{
				if (!changeTitle)
					return _Title;
				else
					return tmpTitle;
			}
			set
			{
				changeTitle=true;
				tmpTitle=value;
			}
		}
	
		public override void Restore()
		{
			changeTitle = false;
		}
		// Make a new child, which is a plant node.
		public override Object MakeNewChild()
		{
			VEO_Plant pl = new VEO_PlantN(usrRunTime,null,null);
			return (Object) pl;
			
		}
		// 'Open' positions in the datapath directory (for creation of plant
		// directories (store the previous directory, to position out of this
		// directory on end).
		public override bool Open()
		{
			if (Directory.Exists(_Location) == false) return false;
			isOpen = true;
			PrevDirectory = Directory.GetCurrentDirectory();
			if(_Location!=null)Directory.SetCurrentDirectory(_Location);
			return true;
		}
		// Close clears any multi-user settings for the system level
		public override bool Close()
		{
			if (!isOpen)return false;
			isOpen = false;
			Directory.SetCurrentDirectory(PrevDirectory);
			return true;
		}
		// Save the new plant & add to children if it succeeds. 
		public override bool SaveChild(Object obj)
		{
			VEO_Plant plnt = (VEO_Plant)obj;
			bool succeed = plnt.SaveNew(this._Location);
			if (succeed == true) 
			{
				plnt._Location = this._Location + plnt._Location;
				plnt.parentObj = this;
			}
			return succeed;
		}
		public override bool Delete()
		{
			// concatenate title/loc so that it can be removed from the datapath string
			string delpth = _Location + "," + _Title + ";";
			bool success = usrRunTime.DeletePathFromCfg(delpth);
			return success;
		}
		// for this data path, read in which plants are available, and add to 
		// the list.
		public override bool Read(bool dummy)
		{
			ArrayList tmpChildren = new ArrayList();
			// using the . do exist tests to see if they exist on this datapath.
			XmlDocument xmldoc = usrRunTime.menuwinXML.GetXmlDoc();
			XmlElement top = (XmlElement) xmldoc.FirstChild;
			XmlElement sys = (XmlElement) top.SelectSingleNode("SystemAttach");
			XmlElement ele = (XmlElement) sys.SelectSingleNode("Plant");
			while (ele != null)
			{
				// get plants and test for existence with the location for
				// this datapath. If exists, add it to the PlantList.
				//get the TemplateName & if there is a {t} replace the {t}
				// with this datapath's path.
				XmlElement tn = (XmlElement) ele.SelectSingleNode("TemplateName");
				if (tn != null)
				{
					string templatename = tn.InnerText;
					if (templatename.IndexOf("{t}",0) != -1)
					{
						string tpath = templatename.Replace("{t}",this.Location);
						string path = tpath.Substring(0,tpath.IndexOf(" menu"));
						DirectoryInfo source = new DirectoryInfo(path.Replace("/","//"));
						if (source.Exists)
						{
							XmlElement etitle = (XmlElement) ele.SelectSingleNode("MenuName");
							VEO_Plant plnt = new VEO_Plant(usrRunTime, etitle.InnerText,path);
							plnt.parentObj = this;
							tmpChildren.Add(plnt);
						}
					}
				}
				ele = (XmlElement) ele.NextSibling;
			}
			Security lsec = usrRunTime.sec;
			if (lsec.OpenFile()==true)
			{
				for(int i=0;i