/*********************************************************************************************
 * Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: DataRoot.cs $     $Revision: 2 $
 * $Author: Jsj $   $Date: 3/06/07 1:29p $
 *
 * $History: DataRoot.cs $
 * 
 * *****************  Version 2  *****************
 * User: Jsj          Date: 3/06/07    Time: 1:29p
 * Updated in $/LibSource/VEObject
 * Property page check
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 7/27/04    Time: 8:53a
 * Created in $/LibSource/VEObject
 *********************************************************************************************/
using System;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using VENetwork;
using Utils;
namespace VEObject
{
	/// 
	/// This module defines the DataRoot class, which is the root of all
	/// data that is accessible to this user. (i.e. it is the container
	/// for all data within the datapath).  Applications accessing the database 
	/// should create a DataRoot.
	/// 
	public class VEO_DataRoot : VEO_Base
	{
		protected string tmpLocation;
		protected bool changeLocation;
		public VEO_DataRoot(UserRunTime iusrRunTime)
		{
			VEObjectType=(int)VEObjectTypesDefs.System;
			iconStates = new int[5] {6,21,8,22,7};
			_Title = "VE-PROMS Data "+iusrRunTime.ucfgpath;
			_Location = iusrRunTime.ucfgpath;
			changeLocation = false;
			usrRunTime = iusrRunTime;
			isNew=false;
			LoadLockInfo();
			icon = iconStates[(int)Lock.LockStatus];
		}
		public override void LoadLockInfo()
		{
			Lock = new VELock(usrRunTime.syspath, usrRunTime.myUserData, usrRunTime.InMultiUserMode?VENetwork.LockTypes.System:VENetwork.LockTypes.None);
		}
		// The following properties are used in the 'Properties' dialog. This set is used
		// for modify.
		[Description("Title"),Category("VE-PROMS Data Paths (CFG File)"),ReadOnly(true)]public string Title
		{
			get{return _Title;}
			set{_Title=value;}
		}
		
		//[Description("Location"),Category("VE-PROMS Data Paths (CFG File)"),EditorAttribute(typeof(DataRootFileDlg), typeof(System.Drawing.Design.UITypeEditor))]public string Location
		[Description("Location"),Category("VE-PROMS Data Paths (CFG File)"),ReadOnly(true)]public string Location
		{
			get
			{
				if (!changeLocation)
					return _Location;
				else
					return tmpLocation;
			}
			set
			{
				FileInfo fi = new FileInfo(value);
				if (!fi.Exists)
				{
					MessageBox.Show("The file that was specified does not exist.");
					return;
				}
				changeLocation = true;
				tmpLocation=value;
			}
		}
		public override bool Open()
		{
			isOpen = true;
			// check current multi-user status
			if (Connection!=null)Connection.Enter(false);
			return true;
		}
		// Close clears any multi-user settings for the system level
		public override bool Close()
		{
			isOpen = false;
			if (Connection!=null)Connection.Exit();
			return true;
		}
		public override void Restore()
		{
			changeLocation = false;
		}
		public override bool mnuAllowDelete()
		{
			return false;
		}
		public override bool mnuAllowLckDB()
		{
			if (usrRunTime.InMultiUserMode && usrRunTime.sec.PermissionToLockFlag&&Lock.LockStatus==VENetwork.Status.NoLock) return true;
			return false;
		}
		public override bool mnuAllowNew()
		{
//			if (usrRunTime.InMultiUserMode && Lock.LockStatus==VENetwork.Status.LockedByOther)return false;
			// B2007-005
			// Standardize the need for a lock. Prevent NEW button on first tree node
			// if a lock is not set
			if (amILockedByMe()==false) return false;
			return true;
		}
		public override bool mnuAllowUnlckDB()
		{
			if (usrRunTime.InMultiUserMode && usrRunTime.sec.PermissionToLockFlag&&(Lock.LockStatus==VENetwork.Status.Locked||
				Lock.LockStatus==VENetwork.Status.LockPending)) return true;
			return false;
		}
		// Add a Data Path. Private method used below.
		private void AddDataPath(string entry)
		{
			int comma;
			string tmp;
			string loc;
			string title;
			comma = entry.IndexOf(",");
			tmp=entry.Substring(0,comma);
			loc=tmp.Trim();
			//Make sure datapath has an ending backslash
			if (!(loc.EndsWith("\\")))
				loc = loc + "\\";
			tmp=entry.Substring(comma+1,entry.Length-comma-2);  // -2, don't copy ";"
			title=tmp.Trim();
			VEO_DataPath dp = new VEO_DataPath(usrRunTime,title,loc);
			dp.parentObj = (VEO_Base) this;
			Children.Add(dp);
		}
		// Get the datapath list. If in demo mode, use sample list; otherwise read in
		// from the user's cfg file.
		public override bool Read(bool dummy)
		{
			PrivateProfile ppCfg;
			string dpath;
			string entry;
			int semi;
			int strtindx=0;
			int netpth;
			if (usrRunTime.sec.isDemoMode)
			{
				dpath = usrRunTime.syspath + "\\samples\\,Demo Data;";
			}
			else
			{
				// for this data root, read in the datapath. If a user's cfg exists
				// use that data. Otherwise, use the menuwin2.xml data which is part
				// of the user's run time.
				ppCfg = new PrivateProfile(usrRunTime.ucfgpath);
//				dpath=ppCfg.Attr("/ini/section[@name='Menu']/param[@name='DataPath']/@value");
				dpath=ppCfg.Attr("Menu","DataPath");
				if (dpath==null || dpath=="")return false;
			}
			semi=dpath.IndexOf(";",strtindx);
			if (semi == -1) 
			{
				MessageBox.Show("Must have semi-colon in datapath");
				Environment.Exit(-1);
			}
			while (semi !=-1)
			{
				entry = dpath.Substring(strtindx,semi-strtindx+1);
				netpth = entry.IndexOf("////",0);
				if (netpth != -1)
					//if(GetNetTempDisk()) AddDataPath(entry);
					MessageBox.Show("Need to support network temp disk");
				else
					AddDataPath(entry);
				if (semi+1>dpath.Length)
					strtindx=-1;
				else
				{
					strtindx=semi+1;
					semi=dpath.IndexOf(";",strtindx);
				}
			}
			
			return true;
		}
		// use the change to the cfg file.
		public override bool Write()
		{
			// check for cfg file existence
			FileInfo fi = new FileInfo(Location);
			if (fi.Exists)
			{
				if(changeLocation)_Location=tmpLocation;
				changeLocation=false;
				this._Title = "VE-PROMS Data " + Location;
				usrRunTime.ucfgpath = Location;
				return true;
			}
			else
			{
				MessageBox.Show("The file that was specified does not exist.");
				return false;
			}
		}
		// add a new datapath
		public override Object MakeNewChild()
		{
			VEO_DataPathN dp = new VEO_DataPathN(usrRunTime,null,null);
			return dp;
		}
		// check that the directory input by the user exists. If so, add the
		// directory & title to the cfg file.
		public override bool SaveChild(Object obj)
		{
			VEO_DataPathN dp = (VEO_DataPathN) obj;
			// B2007-005
			// If the user didn't enter a location, then show an error message
			//and don't continue with the save.
			if (dp.Location == null || dp.Location.Length == 0)
			{
				MessageBox.Show("No data path Location specified.","Error");
				return false;
			}
			dp.SaveFields();
			// Check for existance of directory if it doesn't exist give message
			// and return;
			string pth = dp._Location;
			DirectoryInfo di = new DirectoryInfo(pth);
			if (di.Exists == false)
			{
				try
				{
					di.Create();
				}
				catch
				{
					MessageBox.Show("Directory for data path doesn't exist, and could not be created.");
				}
				return false;
			}
			string newcfgpth = dp._Location+","+dp._Title+";";
			usrRunTime.AddPathToCfg(newcfgpth);
			return true;
		}
		public override void DoListView(ListView veoListView)
		{
			ListViewItem item=null;
			veoListView.Columns.Add("Location", 120, HorizontalAlignment.Left);
			veoListView.Columns.Add("Title", 300, HorizontalAlignment.Left);
			
			for (int i=0; i